Views
No views yet
scale parameter.Prompt: A cat is sitting on a stone.
| base model | scale=1.0 | scale=2.5 |
|---|---|---|
![]() | ![]() | ![]() |
Prompt: A cute anime girl with pink hair and cat ears, pastel colors.
| base model | scale=1.0 | scale=2.5 |
|---|---|---|
![]() | ![]() | ![]() |
Prompt: A cyberpunk apartment with a view of neon lights.
| base model | scale=1.0 | scale=2.5 |
|---|---|---|
![]() | ![]() | ![]() |
git clone https://github.com/modelscope/DiffSynth-Studio.git
cd DiffSynth-Studio
pip install -e .1from diffsynth.diffusion.template import TemplatePipeline
2from diffsynth.pipelines.flux2_image import Flux2ImagePipeline, ModelConfig
3import torch
4
5
6pipe = Flux2ImagePipeline.from_pretrained(
7 torch_dtype=torch.bfloat16,
8 device="cuda",
9 model_configs=[
10 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-base-4B", origin_file_pattern="transformer/*.safetensors"),
11 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="text_encoder/*.safetensors"),
12 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
13 ],
14 tokenizer_config=ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="tokenizer/"),
15)
16pipe.dit = pipe.enable_lora_hot_loading(pipe.dit) # Important!
17template = TemplatePipeline.from_pretrained(
18 torch_dtype=torch.bfloat16,
19 device="cuda",
20 model_configs=[ModelConfig(model_id="DiffSynth-Studio/Template-KleinBase4B-Aesthetic")],
21)
22image = template(
23 pipe,
24 prompt="A cat is sitting on a stone.",
25 seed=0, cfg_scale=4, num_inference_steps=50,
26 template_inputs = [{
27 "lora_ids": list(range(1, 180, 2)),
28 "lora_scales": 1.0,
29 "merge_type": "mean",
30 }],
31 negative_template_inputs = [{
32 "lora_ids": list(range(1, 180, 2)),
33 "lora_scales": 1.0,
34 "merge_type": "mean",
35 }],
36)
37image.save("image_Aesthetic_1.0.jpg")
38image = template(
39 pipe,
40 prompt="A cat is sitting on a stone.",
41 seed=0, cfg_scale=4, num_inference_steps=50,
42 template_inputs = [{
43 "lora_ids": list(range(1, 180, 2)),
44 "lora_scales": 2.5,
45 "merge_type": "mean",
46 }],
47 negative_template_inputs = [{
48 "lora_ids": list(range(1, 180, 2)),
49 "lora_scales": 2.5,
50 "merge_type": "mean",
51 }],
52)
53image.save("image_Aesthetic_2.5.jpg")1from diffsynth.diffusion.template import TemplatePipeline
2from diffsynth.pipelines.flux2_image import Flux2ImagePipeline, ModelConfig
3import torch
4
5vram_config = {
6 "offload_dtype": "disk",
7 "offload_device": "disk",
8 "onload_dtype": torch.float8_e4m3fn,
9 "onload_device": "cpu",
10 "preparing_dtype": torch.float8_e4m3fn,
11 "preparing_device": "cuda",
12 "computation_dtype": torch.bfloat16,
13 "computation_device": "cuda",
14}
15pipe = Flux2ImagePipeline.from_pretrained(
16 torch_dtype=torch.bfloat16,
17 device="cuda",
18 model_configs=[
19 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-base-4B", origin_file_pattern="transformer/*.safetensors", **vram_config),
20 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="text_encoder/*.safetensors", **vram_config),
21 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
22 ],
23 tokenizer_config=ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="tokenizer/"),
24 vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
25)
26template = TemplatePipeline.from_pretrained(
27 torch_dtype=torch.bfloat16,
28 device="cuda",
29 model_configs=[ModelConfig(model_id="DiffSynth-Studio/Template-KleinBase4B-Aesthetic")],
30 lazy_loading=True,
31)
32image = template(
33 pipe,
34 prompt="A cat is sitting on a stone.",
35 seed=0, cfg_scale=4, num_inference_steps=50,
36 template_inputs = [{
37 "lora_ids": list(range(1, 180, 2)),
38 "lora_scales": 1.0,
39 "merge_type": "mean",
40 }],
41 negative_template_inputs = [{
42 "lora_ids": list(range(1, 180, 2)),
43 "lora_scales": 1.0,
44 "merge_type": "mean",
45 }],
46)
47image.save("image_Aesthetic_1.0.jpg")
48image = template(
49 pipe,
50 prompt="A cat is sitting on a stone.",
51 seed=0, cfg_scale=4, num_inference_steps=50,
52 template_inputs = [{
53 "lora_ids": list(range(1, 180, 2)),
54 "lora_scales": 2.5,
55 "merge_type": "mean",
56 }],
57 negative_template_inputs = [{
58 "lora_ids": list(range(1, 180, 2)),
59 "lora_scales": 2.5,
60 "merge_type": "mean",
61 }],
62)
63image.save("image_Aesthetic_2.5.jpg")
641modelscope download --dataset DiffSynth-Studio/diffsynth_example_dataset --include "flux2/Template-KleinBase4B-Aesthetic/*" --local_dir ./data/diffsynth_example_dataset
2
3accelerate launch examples/flux2/model_training/train.py \
4 --dataset_base_path data/diffsynth_example_dataset/flux2/Template-KleinBase4B-Aesthetic \
5 --dataset_metadata_path data/diffsynth_example_dataset/flux2/Template-KleinBase4B-Aesthetic/metadata.jsonl \
6 --extra_inputs "template_inputs" \
7 --max_pixels 1048576 \
8 --dataset_repeat 50 \
9 --model_id_with_origin_paths "black-forest-labs/FLUX.2-klein-4B:text_encoder/*.safetensors,black-forest-labs/FLUX.2-klein-base-4B:transformer/*.safetensors,black-forest-labs/FLUX.2-klein-4B:vae/diffusion_pytorch_model.safetensors" \
10 --template_model_id_or_path "DiffSynth-Studio/Template-KleinBase4B-Aesthetic:" \
11 --tokenizer_path "black-forest-labs/FLUX.2-klein-4B:tokenizer/" \
12 --learning_rate 1e-4 \
13 --num_epochs 2 \
14 --remove_prefix_in_ckpt "pipe.template_model." \
15 --output_path "./models/train/Template-KleinBase4B-Aesthetic_full" \
16 --trainable_models "template_model" \
17 --use_gradient_checkpointing \
18 --find_unused_parameters \
19 --enable_lora_hot_loading