File size: 6,494 Bytes
5b60657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""v6m memory policy: «Экономия памяти» (chunked feed-forward, own implementation) and releasing what a run does
not need (ControlNet patch, preprocessors) with a short log line."""
import torch
import pytest

from h3s import h3s_core as core
from h3s import h3s_gen as gen
from h3s import h3s_control as control
from h3s import h3s_memory as mem
from fakes import tiny_patcher, FakeClip, FakeVAE
from test_control import dirs, project, _video, _pinfo   # noqa: F401  (fixtures)


@pytest.fixture()
def patcher():
    return tiny_patcher()


def test_chunked_ffn_equals_the_plain_mlp(patcher):
    import comfy.ops
    mlp = patcher.model.diffusion_model.blocks[0].mlp
    torch.manual_seed(1)
    for shape in ((1, 1031, 128), (1037, 128), (2, 600, 128)):
        x = torch.randn(*shape)
        ref = comfy.ops.linear_input_act(mlp.fc2, mlp.fc1(x), "swiglu")
        for chunks in (2, 3, 7):
            got = mem.chunked_ffn(chunks, 256)(mlp, x)
            assert got.shape == ref.shape and torch.allclose(got, ref, atol=1e-6, rtol=1e-5), (shape, chunks)
    small = torch.randn(1, 100, 128)   # under the threshold: the plain path
    assert torch.equal(mem.chunked_ffn(4, 256)(mlp, small), comfy.ops.linear_input_act(mlp.fc2, mlp.fc1(small), "swiglu"))


def test_modes_and_the_patched_clone(patcher):
    assert not mem.wants_ffn({"mode": "off"}, cn=True, tokens=10 ** 6)
    assert mem.wants_ffn({"mode": "auto"}, cn=True) and not mem.wants_ffn({"mode": "auto"}, cn=False, tokens=100)
    assert mem.wants_ffn({"mode": "auto", "threshold": 1000}, tokens=2001) and mem.wants_ffn({"mode": "on"})
    assert mem.apply_ffn(patcher, {"mode": "off"}) is patcher
    m2 = mem.apply_ffn(patcher, {"mode": "on", "chunks": 2, "threshold": 512})
    assert m2 is not patcher and not patcher.object_patches   # the source patcher is untouched
    keys = [k for k in m2.object_patches if k.endswith(".mlp.forward")]
    assert len(keys) == len(patcher.model.diffusion_model.blocks)
    assert mem.apply_ffn(m2, {"mode": "on"}) is m2   # never stacked
    assert core.normalize_project({"settings": {"memsave": {"mode": "x", "chunks": 99, "threshold": 5}}})["settings"]["memsave"] == {"mode": "auto", "chunks": 16, "threshold": 256}


def test_generation_with_memsave_gives_the_same_latents(patcher, dirs, monkeypatch):
    out, inp = dirs
    seen = []
    real = mem.chunked_ffn

    def spy(chunks, thr):
        f = real(chunks, thr)

        def g(self, x):
            seen.append(int(x.shape[-2]))
            return f(self, x)
        g._h3zs_ffn = f._h3zs_ffn
        return g
    monkeypatch.setattr(mem, "chunked_ffn", spy)
    base = project()
    base["settings"]["cache"] = False
    base["settings"]["memsave"] = {"mode": "off"}
    a = gen.run_project(patcher, FakeClip(), FakeVAE(), base)
    assert not seen
    on = project()
    on["settings"]["cache"] = False
    on["settings"]["memsave"] = {"mode": "on", "chunks": 3, "threshold": 256}
    monkeypatch.setattr(core, "normalize_memsave", lambda v: {"mode": "on", "chunks": 3, "threshold": 8})   # tiny clips: force chunking
    b = gen.run_project(patcher, FakeClip(), FakeVAE(), on)
    assert seen and max(seen) > 8
    va, vb = a[0], b[0]
    assert torch.allclose(va, vb, atol=1e-4, rtol=1e-4)


class _FakePatcher:
    def __init__(self, name):
        self.name, self.model = name, object()


def test_release_unloads_and_logs(monkeypatch, capsys):
    import comfy.model_management as mm
    gone = []
    monkeypatch.setattr(mm, "unload_model_and_clones", lambda p, *a, **k: gone.append(p.name), raising=False)
    mem.hold("cn_patch:x.safetensors", _FakePatcher("cn"), "ControlNet patch x")
    mem.hold("pre:pose", (_FakePatcher("sdpose"), None, _FakePatcher("rtdetr")), "SDPose + RT-DETR")
    control.release()
    assert sorted(gone) == ["rtdetr", "sdpose"] and mem.get("cn_patch:x.safetensors") is not None
    control.release_patch()
    assert gone[-1] == "cn" and not mem.HOT
    assert "[H3ZS] unloaded: SDPose + RT-DETR" in capsys.readouterr().out


def test_preprocessors_are_released_right_after_the_control_video(dirs, monkeypatch):
    out, inp = dirs
    a = _video(inp)
    held = []
    monkeypatch.setattr(control, "preprocess", lambda fr, mode, params: (mem.get("pre:da3:x") or mem.hold("pre:da3:x", _FakePatcher("da3")), fr)[1])
    import comfy.model_management as mm
    monkeypatch.setattr(mm, "unload_model_and_clones", lambda p, *a_, **k: held.append(p.name), raising=False)
    path = control.build_processed(str(inp / "h3labstudio" / a["file"]), "depth", core.normalize_control_params({}))
    assert path and held == ["da3"] and mem.get("pre:da3:x") is None


def test_run_without_control_releases_the_patch(patcher, dirs, monkeypatch):
    import comfy.model_management as mm
    gone = []
    monkeypatch.setattr(mm, "unload_model_and_clones", lambda p, *a, **k: gone.append(p.name), raising=False)
    mem.hold("cn_patch:fake_2.0.safetensors", _FakePatcher("cn"), "ControlNet patch")
    gen.run_project(patcher, FakeClip(), FakeVAE(), project())
    assert gone == ["cn"] and not any(k.startswith("cn_patch:") for k in mem.HOT)


def test_run_with_control_keeps_the_patch_hot(patcher, dirs, monkeypatch):
    out, inp = dirs
    _video(inp)
    import comfy.model_management as mm
    import comfy_extras.nodes_minimax_h3 as nm
    gone, loads = [], []
    monkeypatch.setattr(mm, "unload_model_and_clones", lambda p, *a, **k: gone.append(p.name), raising=False)

    class FakeApply:
        @staticmethod
        def execute(model, patch, vae, strength, start, end, control_video=None):
            return (model,)
    monkeypatch.setattr(nm, "MiniMaxH3FunControlNetApply", FakeApply)
    monkeypatch.setattr(control, "patch_file", lambda: ("fake_2.0.safetensors", "/x"))
    monkeypatch.setattr(control, "patch_info", _pinfo("fake_2.0.safetensors"))
    import comfy_extras.nodes_model_patch as mp

    class FakeLoader:
        def load_model_patch(self, name):
            loads.append(name)
            return (_FakePatcher("cn"),)
    monkeypatch.setattr(mp, "ModelPatchLoader", FakeLoader)
    p = project({"mode": "gray", "clips": [{"asset": "v1", "start": 0.0, "in": 0.0, "length": 1.0}]})
    p["settings"]["cache"] = False
    gen.run_project(patcher, FakeClip(), FakeVAE(), p)
    gen.run_project(patcher, FakeClip(), FakeVAE(), p)
    assert loads == ["fake_2.0.safetensors"] and gone == []   # same setup: loaded once, no churn
    control.release_patch()