nifleisch commited on
Commit
8e05071
·
verified ·
1 Parent(s): 5c04b92

Use float32 Euler updates for exact five-step sampling

Browse files
Files changed (1) hide show
  1. README.md +14 -4
README.md CHANGED
@@ -19,7 +19,7 @@ tags:
19
 
20
  # Qwen-Image-2.1-P-Turbo
21
 
22
- **Improved using Qwen.** A five-step LoRA adapter for [Qwen Image 2.1](https://huggingface.co/Qwen/Qwen-Image-2.1). The adapter weights are a modification of the base model; its license applies. The `_comfyui_bf16.safetensors` file is for ComfyUI. Use LoRA strength 1.0 and sigma schedule `1 → 13/14 → 6/7 → 2/3 → 0.4 → 0`.
23
 
24
  ## Quick Start
25
 
@@ -33,6 +33,16 @@ hf auth login # required while this repository is private
33
  import torch
34
  from PIL import Image
35
  from diffusers import FlowMatchEulerDiscreteScheduler, QwenImage21Pipeline
 
 
 
 
 
 
 
 
 
 
36
 
37
  pipe = QwenImage21Pipeline.from_pretrained(
38
  "Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
@@ -41,7 +51,7 @@ pipe.load_lora_weights(
41
  "PrunaAI/Qwen-Image-2.1-P-Turbo",
42
  weight_name="p_qwen_image_2.1_5step_v1.0.safetensors",
43
  )
44
- pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
45
  pipe.scheduler.config,
46
  use_dynamic_shifting=False,
47
  shift=1.0,
@@ -53,7 +63,7 @@ sigmas = [1.0, 13 / 14, 6 / 7, 2 / 3, 0.4] # terminal 0 is appended
53
  image = pipe(
54
  prompt='A neon shop sign that reads "QWEN IMAGE 2.1", rainy night',
55
  width=1024, height=1024,
56
- num_inference_steps=5, sigmas=sigmas, true_cfg_scale=1.0,
57
  generator=torch.Generator("cuda").manual_seed(42),
58
  ).images[0]
59
  image.save("t2i_example.png")
@@ -63,7 +73,7 @@ input_image = Image.open("input.png").convert("RGB")
63
  image = pipe(
64
  prompt="Change the background to a sunset beach",
65
  image=input_image,
66
- num_inference_steps=5, sigmas=sigmas, true_cfg_scale=1.0,
67
  generator=torch.Generator("cuda").manual_seed(42),
68
  ).images[0]
69
  image.save("edit_example.png")
 
19
 
20
  # Qwen-Image-2.1-P-Turbo
21
 
22
+ **Improved using Qwen.** A five-step LoRA adapter for [Qwen Image 2.1](https://huggingface.co/Qwen/Qwen-Image-2.1). The adapter weights are a modification of the base model; its license applies. The `_comfyui_bf16.safetensors` file is for ComfyUI. Use LoRA strength 1.0, float32 Euler updates, and sigma schedule `1 → 13/14 → 6/7 → 2/3 → 0.4 → 0`.
23
 
24
  ## Quick Start
25
 
 
33
  import torch
34
  from PIL import Image
35
  from diffusers import FlowMatchEulerDiscreteScheduler, QwenImage21Pipeline
36
+ from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteSchedulerOutput
37
+
38
+ class Float32EulerScheduler(FlowMatchEulerDiscreteScheduler):
39
+ def step(self, model_output, timestep, sample, return_dict=True, **kwargs):
40
+ if self.step_index is None:
41
+ self._init_step_index(timestep)
42
+ dt = self.sigmas[self.step_index + 1] - self.sigmas[self.step_index]
43
+ prev = (sample.float() + dt * model_output.float()).to(model_output.dtype)
44
+ self._step_index += 1
45
+ return FlowMatchEulerDiscreteSchedulerOutput(prev_sample=prev) if return_dict else (prev,)
46
 
47
  pipe = QwenImage21Pipeline.from_pretrained(
48
  "Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
 
51
  "PrunaAI/Qwen-Image-2.1-P-Turbo",
52
  weight_name="p_qwen_image_2.1_5step_v1.0.safetensors",
53
  )
54
+ pipe.scheduler = Float32EulerScheduler.from_config(
55
  pipe.scheduler.config,
56
  use_dynamic_shifting=False,
57
  shift=1.0,
 
63
  image = pipe(
64
  prompt='A neon shop sign that reads "QWEN IMAGE 2.1", rainy night',
65
  width=1024, height=1024,
66
+ num_inference_steps=5, sigmas=sigmas, true_cfg_scale=1.0, use_kv_cache=False,
67
  generator=torch.Generator("cuda").manual_seed(42),
68
  ).images[0]
69
  image.save("t2i_example.png")
 
73
  image = pipe(
74
  prompt="Change the background to a sunset beach",
75
  image=input_image,
76
+ num_inference_steps=5, sigmas=sigmas, true_cfg_scale=1.0, use_kv_cache=False,
77
  generator=torch.Generator("cuda").manual_seed(42),
78
  ).images[0]
79
  image.save("edit_example.png")