test_flux_dev_loras.py 4.2 KB
Newer Older
muyangli's avatar
muyangli committed
1
import pytest
2
3
import torch
from diffusers import FluxPipeline
muyangli's avatar
muyangli committed
4

5
from nunchaku import NunchakuFluxTransformer2dModel
6
from nunchaku.utils import get_precision, is_turing
Muyang Li's avatar
Muyang Li committed
7

8
from .utils import run_test
muyangli's avatar
muyangli committed
9
10


muyangli's avatar
muyangli committed
11
@pytest.mark.skipif(is_turing(), reason="Skip tests due to using Turing GPUs")
muyangli's avatar
muyangli committed
12
@pytest.mark.parametrize(
13
    "num_inference_steps,lora_name,lora_strength,cpu_offload,expected_lpips",
muyangli's avatar
muyangli committed
14
    [
15
        (25, "realism", 0.9, True, 0.136 if get_precision() == "int4" else 0.112),
muyangli's avatar
update  
muyangli committed
16
        # (25, "ghibsky", 1, False, 0.186),
muyangli's avatar
muyangli committed
17
        # (28, "anime", 1, False, 0.284),
Muyang Li's avatar
Muyang Li committed
18
        (24, "sketch", 1, True, 0.291 if get_precision() == "int4" else 0.221),
muyangli's avatar
muyangli committed
19
20
        # (28, "yarn", 1, False, 0.211),
        # (25, "haunted_linework", 1, True, 0.317),
muyangli's avatar
muyangli committed
21
22
    ],
)
23
24
25
26
27
def test_flux_dev_loras(num_inference_steps, lora_name, lora_strength, cpu_offload, expected_lpips):
    run_test(
        precision=get_precision(),
        model_name="flux.1-dev",
        dataset_name=lora_name,
muyangli's avatar
muyangli committed
28
29
30
31
32
        height=1024,
        width=1024,
        num_inference_steps=num_inference_steps,
        guidance_scale=3.5,
        use_qencoder=False,
muyangli's avatar
muyangli committed
33
        attention_impl="nunchaku-fp16",
muyangli's avatar
muyangli committed
34
        cpu_offload=cpu_offload,
35
36
        lora_names=lora_name,
        lora_strengths=lora_strength,
muyangli's avatar
muyangli committed
37
38
39
40
41
        cache_threshold=0,
        expected_lpips=expected_lpips,
    )


muyangli's avatar
muyangli committed
42
# lora composition & large rank loras
muyangli's avatar
muyangli committed
43
@pytest.mark.skipif(is_turing(), reason="Skip tests due to using Turing GPUs")
muyangli's avatar
update  
muyangli committed
44
def test_flux_dev_turbo8_ghibsky_1024x1024():
45
46
47
    run_test(
        precision=get_precision(),
        model_name="flux.1-dev",
muyangli's avatar
muyangli committed
48
        dataset_name="haunted_linework",
49
50
51
52
53
54
55
        height=1024,
        width=1024,
        num_inference_steps=8,
        guidance_scale=3.5,
        use_qencoder=False,
        cpu_offload=True,
        lora_names=["realism", "ghibsky", "anime", "sketch", "yarn", "haunted_linework", "turbo8"],
muyangli's avatar
update  
muyangli committed
56
        lora_strengths=[0, 1, 0, 0, 0, 0, 1],
muyangli's avatar
muyangli committed
57
        cache_threshold=0,
58
        expected_lpips=0.310 if get_precision() == "int4" else 0.168,
muyangli's avatar
muyangli committed
59
    )
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94


def test_kohya_lora():
    precision = get_precision()  # auto-detect your precision is 'int4' or 'fp4' based on your GPU
    transformer = NunchakuFluxTransformer2dModel.from_pretrained(
        f"mit-han-lab/nunchaku-flux.1-dev/svdq-{precision}_r32-flux.1-dev.safetensors"
    )
    pipeline = FluxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-dev", transformer=transformer, torch_dtype=torch.bfloat16
    ).to("cuda")

    transformer.update_lora_params("mit-han-lab/nunchaku-test-models/hand_drawn_game.safetensors")
    transformer.set_lora_strength(1)

    prompt = (
        "masterful impressionism oil painting titled 'the violinist', the composition follows the rule of thirds, "
        "placing the violinist centrally in the frame. the subject is a young woman with fair skin and light blonde "
        "hair is styled in a long, flowing hairstyle with natural waves. she is dressed in an opulent, "
        "luxurious silver silk gown with a high waist and intricate gold detailing along the bodice. "
        "the gown's texture is smooth and reflective. she holds a violin under her chin, "
        "her right hand poised to play, and her left hand supporting the neck of the instrument. "
        "she wears a delicate gold necklace with small, sparkling gemstones that catch the light. "
        "her beautiful eyes focused on the viewer. the background features an elegantly furnished room "
        "with classical late 19th century decor. to the left, there is a large, ornate portrait of "
        "a man in a dark suit, set in a gilded frame. below this, a wooden desk with a closed book. "
        "to the right, a red upholstered chair with a wooden frame is partially visible. "
        "the room is bathed in natural light streaming through a window with red curtains, "
        "creating a warm, inviting atmosphere. the lighting highlights the violinist, "
        "casting soft shadows that enhance the depth and realism of the scene, highly aesthetic, "
        "harmonious colors, impressioniststrokes, "
        "<lora:style-impressionist_strokes-flux-by_daalis:1.0> <lora:image_upgrade-flux-by_zeronwo7829:1.0>"
    )

    image = pipeline(prompt, num_inference_steps=20, guidance_scale=3.5).images[0]
    image.save(f"flux.1-dev-{precision}-1.png")