`pytest tests/` previously crashed during collection and 7 of 17 test files were dead: 61 tests were reachable, all via ad-hoc standalone scripts. Now a bare `pytest` collects everything and passes 238 tests with ComfyUI absent (verified by running the suite from outside the ComfyUI tree, where `import comfy` raises ModuleNotFoundError). Import structure: - Drop tests/__init__.py. With it, pytest walks up to the project root's __init__.py -- the ComfyUI node entry point -- and imports ComfyUI before any test runs. - Import project code as `src.<module>` instead of putting src/ on sys.path and importing bare `merge.algorithms` / `validation` / `types`. Modules in src/ use package-relative imports (`from ..types import ...`) that cannot resolve when loaded top-level, and `types` collided with the stdlib module. Same change for the mock.patch targets in test_algorithms. - Consolidate conftest.py in tests/, mocking comfy, folder_paths, comfy_extras and nodes. It stays in tests/ rather than the project root because pytest imports a root-level conftest as part of the root package, executing the ComfyUI entry point. - Guard the script-style runners behind `if __name__ == "__main__":` so they no longer sys.exit() during collection. Those files still run standalone. - Drop run_pytest.py: a mocking wrapper made redundant by conftest, unused and pointing at an unresolvable default path. Bugs the dead tests were hiding: - validators: the INCOMPATIBLE_DIMENSIONS check sat after the `continue` that skips the reference tensor, so a lone LoRA with mismatched up/down ranks passed validation unchecked. It is a per-LoRA check and now runs for every entry. - decomposition: __init__ exported a QRDecomposer that exists nowhere, so `import src.decomposition` raised ImportError. Export and tests removed. Stale expectations corrected: - return_statistics is a constructor argument, not a decompose() kwarg. - The zero-matrix rank guard only applies under dynamic rank selection; the test now exercises that path, plus a new case pinning fixed-rank behavior. - `reconstruction_error < 0.5` for a rank-10 truncation of a random 100x50 Gaussian is unreachable -- the optimum is 0.7557 and the decomposer hits 0.7568. Assert near-optimality instead, and add a genuinely low-rank case that reconstructs to 0.003. - sym/asym distributions differ only by float32 rounding (~5e-7), below the default atol of 1e-8. RUN_TESTS.md is rewritten against the real setup: correct interpreter path, the two test-file styles, the import rules for adding tests, and a per-file coverage table. It no longer documents test_gradient_analyzer_integration.py, which is not in the repo. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
# tests/test_interp_fidelity.py
|
|
# Informational: delta-space blend should align better with the intended merge
|
|
# than the old factored path. Asserts a loose lower bound so it is not brittle.
|
|
import os, sys, traceback
|
|
|
|
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
PARENT = os.path.dirname(REPO)
|
|
COMFY_ROOT = os.path.dirname(PARENT)
|
|
sys.path.insert(0, COMFY_ROOT)
|
|
sys.path.insert(0, PARENT)
|
|
|
|
import torch
|
|
import importlib.util
|
|
PKG = "LoRA_Merger_ComfyUI_test"
|
|
spec = importlib.util.spec_from_file_location(
|
|
PKG, os.path.join(REPO, "__init__.py"), submodule_search_locations=[REPO])
|
|
pkg = importlib.util.module_from_spec(spec)
|
|
sys.modules[PKG] = pkg
|
|
spec.loader.exec_module(pkg)
|
|
|
|
from LoRA_Merger_ComfyUI_test.src.merge.algorithms import (
|
|
interp_delta_merge, slerp_merge, nuslerp_merge, karcher_merge)
|
|
|
|
|
|
def _delta(seed):
|
|
torch.manual_seed(seed)
|
|
return (torch.randn(96, 8) * 0.1) @ (torch.randn(8, 128) * 0.1)
|
|
|
|
|
|
def _cos(a, b):
|
|
return torch.nn.functional.cosine_similarity(a.flatten(), b.flatten(), dim=0).item()
|
|
|
|
|
|
def test_delta_space_blend_aligns_with_average():
|
|
margs = {
|
|
"slerp": (slerp_merge, {"mode": "slerp", "t": 0.5, "lambda_": 1.0}),
|
|
"nuslerp": (nuslerp_merge, {"mode": "nuslerp", "nuslerp_flatten": True,
|
|
"nuslerp_row_wise": False, "lambda_": 1.0}),
|
|
"karcher": (karcher_merge, {"mode": "karcher", "max_iter": 10, "tol": 1e-5,
|
|
"lambda_": 1.0}),
|
|
}
|
|
for name, (fn, ma) in margs.items():
|
|
deltas = [_delta(1), _delta(2)]
|
|
ref = 0.5 * (deltas[0] + deltas[1])
|
|
out = interp_delta_merge(fn, [d.clone() for d in deltas], torch.tensor([1.0, 1.0]),
|
|
dict(ma), key="k", normalize=True)
|
|
c = _cos(out, ref)
|
|
print(f" {name}: cos(delta-space blend, mean-delta) = {c:+.3f}")
|
|
assert c > 0.6, f"{name}: unexpectedly low alignment {c}"
|
|
|
|
|
|
def run(tests):
|
|
failed = 0
|
|
for name, fn in tests:
|
|
try:
|
|
fn(); print(f"PASS {name}")
|
|
except Exception:
|
|
failed += 1; print(f"FAIL {name}"); traceback.print_exc()
|
|
if failed:
|
|
print(f"\n{failed} FAILED"); sys.exit(1)
|
|
print(f"\nAll {len(tests)} passed")
|
|
|
|
|
|
# Runnable as a plain script (`python tests/<file>.py`); under pytest the
|
|
# test_* functions are collected directly, so the script runner must not fire
|
|
# at import time -- it calls sys.exit() and would abort collection.
|
|
if __name__ == "__main__":
|
|
run([("delta_space_blend_aligns", test_delta_space_blend_aligns_with_average)])
|