A-image / tests /test_lora_loader.py
cachebot
feat: create A-image Anima Studio with dual default LoRAs, Civitai API hot-mount, and ZeroGPU xlarge reservation
085a3d8
Raw History Blame Contribute Delete
4.93 kB
"""Unit tests for Anima LoRA, AI-Toolkit, PEFT, and LyCORIS LoKr weight fusion & rollback."""
from __future__ import annotations
import types
import unittest
from tests.torch_stub import StubTensor, ensure_torch_and_diffusers_stubs
ensure_torch_and_diffusers_stubs()
from anima_studio.lora_loader import LoraManager, parse_key # noqa: E402
class _FakeLinear:
def __init__(self, rows: list[list[float]]) -> None:
self.weight = StubTensor(rows, dtype="bfloat16")
class _FakePipeline:
def __init__(self) -> None:
q_proj = _FakeLinear(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)
attn2 = types.SimpleNamespace(to_q=q_proj)
block0 = types.SimpleNamespace(attn2=attn2)
self.transformer = types.SimpleNamespace(
transformer_blocks=[block0],
state_dict=lambda: {"transformer_blocks.0.attn2.to_q.weight": q_proj.weight},
)
down_proj = _FakeLinear(
[
[2.0, 0.0, 0.0, 0.0],
[0.0, 2.0, 0.0, 0.0],
[0.0, 0.0, 2.0, 0.0],
[0.0, 0.0, 0.0, 2.0],
]
)
layer0 = types.SimpleNamespace(mlp=types.SimpleNamespace(down_proj=down_proj))
self.text_encoder = types.SimpleNamespace(
model=types.SimpleNamespace(layers=[layer0]),
state_dict=lambda: {"model.layers.0.mlp.down_proj.weight": down_proj.weight},
)
cond_q = _FakeLinear(
[
[0.5, 0.0, 0.0, 0.0],
[0.0, 0.5, 0.0, 0.0],
[0.0, 0.0, 0.5, 0.0],
[0.0, 0.0, 0.0, 0.5],
]
)
cond_blk0 = types.SimpleNamespace(cross_attn=types.SimpleNamespace(q_proj=cond_q))
self.text_conditioner = types.SimpleNamespace(
blocks=[cond_blk0],
state_dict=lambda: {"blocks.0.cross_attn.q_proj.weight": cond_q.weight},
)
class TestLoraLoader(unittest.TestCase):
def test_parse_key_all_supported_layouts(self) -> None:
self.assertEqual(
parse_key("lora_unet_blocks_0_cross_attn_q_proj.lora_down.weight"),
("transformer", "kohya", "blocks_0_cross_attn_q_proj", "A"),
)
self.assertEqual(
parse_key("diffusion_model.blocks.0.cross_attn.q_proj.lora_A.weight"),
("transformer", "native", "blocks.0.cross_attn.q_proj", "A"),
)
self.assertEqual(
parse_key("transformer.transformer_blocks.0.attn2.to_q.lora_A.weight"),
("transformer", "native", "transformer_blocks.0.attn2.to_q", "A"),
)
self.assertEqual(
parse_key("lycoris_blocks_0_cross_attn_q_proj.lokr_w1"),
("transformer", "kohya", "blocks_0_cross_attn_q_proj", "w1"),
)
self.assertIsNone(parse_key("unrelated.weight"))
def test_dual_default_loras_fusion_and_clean_reset(self) -> None:
pipe = _FakePipeline()
mgr = LoraManager(pipe)
base_snapshot = pipe.transformer.transformer_blocks[0].attn2.to_q.weight.flat_values()
# Simulate Civitai 2540444 (AI-Toolkit native layout)
highres_raw = {
"diffusion_model.blocks.0.cross_attn.q_proj.lora_A.weight": StubTensor(
[[1.0, 0.0, 0.0, 0.0]]
),
"diffusion_model.blocks.0.cross_attn.q_proj.lora_B.weight": StubTensor(
[[0.5], [0.0], [0.0], [0.0]]
),
}
c1, u1, s1 = mgr.register("highres_boost", highres_raw)
self.assertEqual((c1, u1, s1), (1, [], ["native:lora"]))
# Simulate Civitai 2612570 (Kohya layout with alpha=32, rank=1 -> scale 32)
slider_raw = {
"lora_unet_blocks_0_cross_attn_q_proj.lora_down.weight": StubTensor(
[[1.0, 0.0, 0.0, 0.0]]
),
"lora_unet_blocks_0_cross_attn_q_proj.lora_up.weight": StubTensor(
[[1.0], [0.0], [0.0], [0.0]]
),
"lora_unet_blocks_0_cross_attn_q_proj.alpha": StubTensor([1.0]),
}
c2, u2, s2 = mgr.register("quality_slider", slider_raw)
self.assertEqual((c2, u2, s2), (1, [], ["kohya:lora"]))
# Fuse default strengths: highres_boost=1.0, quality_slider=0.9
mgr.apply([("highres_boost", 1.0), ("quality_slider", 0.9)])
fused = pipe.transformer.transformer_blocks[0].attn2.to_q.weight.flat_values()
# [0,0] = 1.0 + 1.0*0.5 + 0.9*1.0 = 2.4
self.assertAlmostEqual(fused[0], 2.4, places=5)
mgr.apply([])
restored = pipe.transformer.transformer_blocks[0].attn2.to_q.weight.flat_values()
for a, b in zip(base_snapshot, restored, strict=True):
self.assertAlmostEqual(a, b, places=6)
if __name__ == "__main__":
unittest.main()