test_lora_reset.py 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os

import torch
from diffusers import FluxPipeline

from nunchaku import NunchakuFluxTransformer2dModel
from nunchaku.utils import get_precision

from ..utils import compute_lpips


def test_lora_reset():
    precision = get_precision()  # auto-detect your precision is 'int4' or 'fp4' based on your GPU
    transformer = NunchakuFluxTransformer2dModel.from_pretrained(
15
        f"mit-han-lab/nunchaku-flux.1-dev/svdq-{precision}_r32-flux.1-dev.safetensors", offload=True
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
    )
    pipeline = FluxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-dev", transformer=transformer, torch_dtype=torch.bfloat16
    )
    pipeline.enable_sequential_cpu_offload()

    save_dir = os.path.join("test_results", "bf16", "flux", "lora_reset")
    os.makedirs(save_dir, exist_ok=True)

    image = pipeline(
        "cozy mountain cabin covered in snow, with smoke curling from the chimney and a warm, inviting light spilling through the windows",  # noqa: E501
        num_inference_steps=8,
        guidance_scale=3.5,
        generator=torch.Generator().manual_seed(23),
    ).images[0]
    image.save(os.path.join(save_dir, "before.png"))

    transformer.update_lora_params("alimama-creative/FLUX.1-Turbo-Alpha/diffusion_pytorch_model.safetensors")
    transformer.set_lora_strength(50)
    transformer.reset_lora()

    image = pipeline(
        "cozy mountain cabin covered in snow, with smoke curling from the chimney and a warm, inviting light spilling through the windows",  # noqa: E501
        num_inference_steps=8,
        guidance_scale=3.5,
        generator=torch.Generator().manual_seed(23),
    ).images[0]
    image.save(os.path.join(save_dir, "after.png"))

    lpips = compute_lpips(os.path.join(save_dir, "before.png"), os.path.join(save_dir, "after.png"))
    print(f"LPIPS: {lpips}")
47
    assert lpips < 0.232 * 1.1