|
| 1 | +from hashlib import sha256 |
| 2 | +from pathlib import Path |
| 3 | +from warnings import warn |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | +from huggingface_hub import hf_hub_download # type: ignore |
| 8 | + |
| 9 | +from refiners.fluxion.utils import load_tensors |
| 10 | +from refiners.foundationals.latent_diffusion import StableDiffusion_1 |
| 11 | +from refiners.foundationals.latent_diffusion.lora import Lora, SDLoraManager |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def manager(refiners_sd15: StableDiffusion_1) -> SDLoraManager: |
| 16 | + return SDLoraManager(refiners_sd15) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def pokemon_lora_weights( |
| 21 | + test_weights_path: Path, |
| 22 | + use_local_weights: bool, |
| 23 | +) -> dict[str, torch.Tensor]: |
| 24 | + if use_local_weights: |
| 25 | + weights_path = test_weights_path / "loras" / "pokemon-lora" / "pytorch_lora_weights.bin" |
| 26 | + if not weights_path.is_file(): |
| 27 | + warn(f"could not find weights at {weights_path}, skipping") |
| 28 | + pytest.skip(allow_module_level=True) |
| 29 | + else: |
| 30 | + weights_path = Path( |
| 31 | + hf_hub_download( |
| 32 | + repo_id="pcuenq/pokemon-lora", |
| 33 | + filename="pytorch_lora_weights.bin", |
| 34 | + revision="bc3cb5256ebc303457acab170ca6219a66dd31f5", |
| 35 | + ) |
| 36 | + ) |
| 37 | + |
| 38 | + expected_sha256 = "f712fcfb6618da14d25a4f3e0c9460a878fc2417e2df95cdd683a73f71b50384" |
| 39 | + retrieved_sha256 = sha256(weights_path.read_bytes()).hexdigest().lower() |
| 40 | + assert retrieved_sha256 == expected_sha256, f"expected {expected_sha256}, got {retrieved_sha256}" |
| 41 | + |
| 42 | + return load_tensors(weights_path) |
| 43 | + |
| 44 | + |
| 45 | +def test_add_loras(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 46 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights) |
| 47 | + assert "pokemon-lora" in manager.names |
| 48 | + |
| 49 | + with pytest.raises(AssertionError) as exc: |
| 50 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights) |
| 51 | + assert "already exists" in str(exc.value) |
| 52 | + |
| 53 | + |
| 54 | +def test_add_multiple_loras(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 55 | + manager.add_loras("pokemon-lora", pokemon_lora_weights) |
| 56 | + manager.add_loras("pokemon-lora2", pokemon_lora_weights) |
| 57 | + assert "pokemon-lora" in manager.names |
| 58 | + assert "pokemon-lora2" in manager.names |
| 59 | + |
| 60 | + |
| 61 | +def test_remove_loras(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 62 | + manager.add_loras("pokemon-lora", pokemon_lora_weights) |
| 63 | + manager.add_loras("pokemon-lora2", pokemon_lora_weights) |
| 64 | + manager.remove_loras("pokemon-lora") |
| 65 | + assert "pokemon-lora" not in manager.names |
| 66 | + assert "pokemon-lora2" in manager.names |
| 67 | + |
| 68 | + manager.remove_loras("pokemon-lora2") |
| 69 | + assert "pokemon-lora2" not in manager.names |
| 70 | + assert len(manager.names) == 0 |
| 71 | + |
| 72 | + |
| 73 | +def test_remove_all(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 74 | + manager.add_loras("pokemon-lora", pokemon_lora_weights) |
| 75 | + manager.add_loras("pokemon-lora2", pokemon_lora_weights) |
| 76 | + manager.remove_all() |
| 77 | + assert len(manager.names) == 0 |
| 78 | + |
| 79 | + |
| 80 | +def test_get_lora(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 81 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights) |
| 82 | + assert all(isinstance(lora, Lora) for lora in manager.get_loras_by_name("pokemon-lora")) |
| 83 | + |
| 84 | + |
| 85 | +def test_get_scale(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 86 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights, scale=0.4) |
| 87 | + assert manager.get_scale("pokemon-lora") == 0.4 |
| 88 | + |
| 89 | + |
| 90 | +def test_names(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 91 | + assert manager.names == [] |
| 92 | + |
| 93 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights) |
| 94 | + assert manager.names == ["pokemon-lora"] |
| 95 | + |
| 96 | + manager.add_loras("pokemon-lora2", tensors=pokemon_lora_weights) |
| 97 | + assert set(manager.names) == set(["pokemon-lora", "pokemon-lora2"]) |
| 98 | + |
| 99 | + |
| 100 | +def test_scales(manager: SDLoraManager, pokemon_lora_weights: dict[str, torch.Tensor]) -> None: |
| 101 | + assert manager.scales == {} |
| 102 | + |
| 103 | + manager.add_loras("pokemon-lora", tensors=pokemon_lora_weights, scale=0.4) |
| 104 | + assert manager.scales == {"pokemon-lora": 0.4} |
| 105 | + |
| 106 | + manager.add_loras("pokemon-lora2", tensors=pokemon_lora_weights, scale=0.5) |
| 107 | + assert manager.scales == {"pokemon-lora": 0.4, "pokemon-lora2": 0.5} |
0 commit comments