dikdimon commited on
Commit
1ac20ad
·
verified ·
1 Parent(s): b77f66b

Delete test_preset_pcfg.py

Browse files
Files changed (1) hide show
  1. test_preset_pcfg.py +0 -166
test_preset_pcfg.py DELETED
@@ -1,166 +0,0 @@
1
- import pathlib, sys
2
- exec(open(str(pathlib.Path(__file__).parent / 'mock_torch.py')).read())
3
- import sys, math, types, dataclasses, json
4
- import numpy as np
5
- import pathlib; sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
6
- import importlib.util
7
-
8
- def load_mod(name, path):
9
- spec = importlib.util.spec_from_file_location(name, path)
10
- m = importlib.util.module_from_spec(spec); sys.modules[name]=m; spec.loader.exec_module(m); return m
11
-
12
- lib_pkg = types.ModuleType('lib_mega_freeu'); sys.modules['lib_mega_freeu'] = lib_pkg
13
- GS = load_mod('lib_mega_freeu.global_state', str(pathlib.Path(__file__).parent.parent / 'lib_mega_freeu' / 'global_state.py'))
14
- UN = load_mod('lib_mega_freeu.unet', str(pathlib.Path(__file__).parent.parent / 'lib_mega_freeu' / 'unet.py'))
15
-
16
- P=0; F=0; ERRS=[]
17
- def ok(t): global P; P+=1; print(f" ✓ {t}")
18
- def ng(t,m=""): global F; F+=1; ERRS.append(f"{t}: {m}"); print(f" ✗ {t} {m}")
19
- def chk(t, c, m=""): ok(t) if c else ng(t, m)
20
-
21
- print("═"*52)
22
- print("FIX 1: State has pcfg_ and verbose fields")
23
- print("═"*52)
24
- st = GS.State()
25
- chk("pcfg_enabled default False", not st.pcfg_enabled)
26
- chk("pcfg_steps default 20", st.pcfg_steps == 20)
27
- chk("pcfg_mode default inject", st.pcfg_mode == "inject")
28
- chk("verbose default False", not st.verbose)
29
-
30
- st2 = GS.State(pcfg_enabled=True, pcfg_b=1.5, pcfg_steps=10, verbose=True)
31
- chk("pcfg_enabled=True", st2.pcfg_enabled)
32
- chk("pcfg_b=1.5", st2.pcfg_b == 1.5)
33
- chk("verbose=True", st2.verbose)
34
-
35
- print("\n═"*27)
36
- print("FIX 2: to_dict() round-trip includes pcfg fields")
37
- d = st2.to_dict()
38
- chk("to_dict has pcfg_enabled", "pcfg_enabled" in d)
39
- chk("to_dict has pcfg_b", "pcfg_b" in d)
40
- chk("to_dict has verbose", "verbose" in d)
41
- chk("to_dict no 'enable'", "enable" not in d)
42
- st3 = GS.State(**{k:v for k,v in d.items() if k in {f.name for f in dataclasses.fields(GS.State)}})
43
- chk("round-trip pcfg_b=1.5", st3.pcfg_b == 1.5)
44
- chk("round-trip verbose=True", st3.verbose)
45
-
46
- print("\n═"*27)
47
- print("FIX 3: _load_user_presets saves/restores pcfg")
48
- import json, tempfile, os, pathlib
49
- preset_data = {
50
- "my_pcfg_preset": {
51
- "start_ratio": 0.0, "stop_ratio": 1.0, "transition_smoothness": 0.0,
52
- "version": "2", "multiscale_mode": "Default", "multiscale_strength": 1.0,
53
- "override_scales": "", "channel_threshold": 96,
54
- "pcfg_enabled": True, "pcfg_b": 1.8, "pcfg_steps": 5,
55
- "pcfg_mode": "lerp", "pcfg_blend": 0.7, "verbose": True,
56
- "stage_infos": []
57
- }
58
- }
59
- with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
60
- json.dump(preset_data, f); tmp = f.name
61
- GS.PRESETS_PATH = pathlib.Path(tmp)
62
- res = GS._load_user_presets()
63
- chk("preset loaded", "my_pcfg_preset" in res)
64
- p = res.get("my_pcfg_preset")
65
- chk("pcfg_enabled restored", p and p.pcfg_enabled == True)
66
- chk("pcfg_b restored", p and p.pcfg_b == 1.8)
67
- chk("pcfg_steps restored", p and p.pcfg_steps == 5)
68
- chk("verbose restored", p and p.verbose == True)
69
- os.unlink(tmp)
70
-
71
- print("\n═"*27)
72
- print("FIX 4: _write_generation_params (simulate)")
73
- class FakePNG:
74
- extra_generation_params = {}
75
-
76
- st4 = GS.State(
77
- start_ratio=0.1, stop_ratio=0.9, transition_smoothness=0.5,
78
- version="2", multiscale_mode="Multi-Bandpass", multiscale_strength=0.8,
79
- override_scales="10, 1.5\n20, 0.8", channel_threshold=64,
80
- pcfg_enabled=True, pcfg_b=1.2, pcfg_steps=15, pcfg_mode="inject",
81
- pcfg_blend=1.0, pcfg_fourier=False, pcfg_ms_mode="Default",
82
- pcfg_ms_str=1.0, pcfg_threshold=1, pcfg_s=0.5, pcfg_gain=1.0,
83
- verbose=True,
84
- stage_infos=[GS.StageInfo(backbone_factor=1.3)]
85
- )
86
-
87
- # Simulate _write_generation_params
88
- fp = FakePNG()
89
- fp.extra_generation_params["MegaFreeU Schedule"] = f"{st4.start_ratio}, {st4.stop_ratio}, {st4.transition_smoothness}"
90
- fp.extra_generation_params["MegaFreeU Stages"] = json.dumps([si.to_dict() for si in st4.stage_infos])
91
- fp.extra_generation_params["MegaFreeU Version"] = st4.version
92
- fp.extra_generation_params["MegaFreeU Multiscale Mode"] = st4.multiscale_mode
93
- fp.extra_generation_params["MegaFreeU Multiscale Strength"] = str(st4.multiscale_strength)
94
- fp.extra_generation_params["MegaFreeU Override Scales"] = st4.override_scales
95
- fp.extra_generation_params["MegaFreeU Channel Threshold"] = str(st4.channel_threshold)
96
- fp.extra_generation_params["MegaFreeU Verbose"] = str(st4.verbose)
97
- if st4.pcfg_enabled:
98
- fp.extra_generation_params["MegaFreeU PostCFG"] = json.dumps({
99
- "enabled": st4.pcfg_enabled, "steps": st4.pcfg_steps,
100
- "mode": st4.pcfg_mode, "blend": st4.pcfg_blend,
101
- "b": st4.pcfg_b, "fourier": st4.pcfg_fourier,
102
- "ms_mode": st4.pcfg_ms_mode, "ms_str": st4.pcfg_ms_str,
103
- "threshold": st4.pcfg_threshold, "s": st4.pcfg_s, "gain": st4.pcfg_gain,
104
- })
105
-
106
- eg = fp.extra_generation_params
107
- chk("PNG has Schedule", "MegaFreeU Schedule" in eg)
108
- chk("PNG has Stages", "MegaFreeU Stages" in eg)
109
- chk("PNG has Version", "MegaFreeU Version" in eg)
110
- chk("PNG has Multiscale Mode", "MegaFreeU Multiscale Mode" in eg)
111
- chk("PNG has Multiscale Strength","MegaFreeU Multiscale Strength" in eg)
112
- chk("PNG has Override Scales", "MegaFreeU Override Scales" in eg)
113
- chk("PNG has Channel Threshold", "MegaFreeU Channel Threshold" in eg)
114
- chk("PNG has Verbose", "MegaFreeU Verbose" in eg)
115
- chk("PNG has PostCFG", "MegaFreeU PostCFG" in eg)
116
-
117
- # Verify PostCFG round-trip
118
- pcfg_d = json.loads(eg["MegaFreeU PostCFG"])
119
- chk("PostCFG b=1.2", pcfg_d["b"] == 1.2)
120
- chk("PostCFG steps=15",pcfg_d["steps"] == 15)
121
-
122
- # Verify multiscale restore
123
- chk("ms_mode=Multi-Bandpass", eg["MegaFreeU Multiscale Mode"] == "Multi-Bandpass")
124
- chk("ch_thresh=64", eg["MegaFreeU Channel Threshold"] == "64")
125
-
126
- print("\n═"*27)
127
- print("FIX 5: Post-CFG independent of Enable (simulate process logic)")
128
- # The fix: pcfg is set BEFORE checking st.enable
129
- # Test: when enabled=False but pcfg_enabled=True, pcfg still created
130
-
131
- class FakeP2:
132
- extra_generation_params = {}
133
- _mega_pcfg = None
134
-
135
- fp2 = FakeP2()
136
- # Simulate new process() logic for disabled main FreeU + enabled Post-CFG
137
- st_disabled = GS.State(enable=False, pcfg_enabled=True, pcfg_b=1.5, pcfg_steps=10)
138
- # Post-CFG created regardless
139
- if st_disabled.pcfg_enabled:
140
- fp2._mega_pcfg = {"enabled": True, "b": st_disabled.pcfg_b, "steps": st_disabled.pcfg_steps, "step": 0}
141
- else:
142
- fp2._mega_pcfg = {"enabled": False}
143
-
144
- chk("pcfg set even when main disabled", fp2._mega_pcfg["enabled"] == True)
145
- chk("pcfg_b=1.5 propagated", fp2._mega_pcfg["b"] == 1.5)
146
-
147
- print("\n═"*27)
148
- print("FIX 6: dict-API compat (old sd-webui-freeu alwayson_scripts)")
149
- # Simulate the dict branch of process()
150
- dict_args = {
151
- "enable": True, "start_ratio": 0.2, "stop_ratio": 0.8,
152
- "version": "2", "multiscale_mode": "Default"
153
- }
154
- fields = {f.name for f in dataclasses.fields(GS.State)}
155
- GS.instance = GS.State(**{k:v for k,v in dict_args.items() if k in fields})
156
- chk("dict API: start_ratio=0.2", GS.instance.start_ratio == 0.2)
157
- chk("dict API: version coerced '2'", GS.instance.version == "2")
158
- chk("dict API: pcfg defaults", not GS.instance.pcfg_enabled)
159
-
160
- print(f"\n{'═'*52}")
161
- print(f"NEW FIXES: {P} PASS {F} FAIL")
162
- if ERRS:
163
- print("\nFailed:")
164
- for e in ERRS: print(f" • {e}")
165
- else:
166
- print("ALL NEW FIX TESTS PASSED ✓")