Text-to-Image
Diffusers
Safetensors
looped-transformer
distillation
sd3
ShimaRinC commited on
Commit
7faa44c
·
verified ·
1 Parent(s): 9941819

Model card: shorten to the level of detail of the StepX-OPD card

Browse files
Files changed (1) hide show
  1. README.md +14 -106
README.md CHANGED
@@ -13,127 +13,35 @@ tags:
13
  - sd3
14
  ---
15
 
16
- # Distill to Loop (D2L): looped SD3-medium students
17
 
18
- This repository holds two looped students of SD3-medium from the paper "Distill to Loop: Turning Pretrained Diffusion Transformers into Looped Models with Adjustable Depth". Each student replaces the 24-block MMDiT transformer of SD3-medium by 2 input blocks, 5 looped blocks that share weights, and 2 output blocks (756,415,672 parameters). The loop count K is chosen at sampling time, and both students were trained with K = 4. The weights are not a plain diffusers model: the `distill_to_loop` package of the code repository is required to load them. Code: https://github.com/WinterCCC/Distill-to-Loop
19
 
20
- The VAE, the text encoders, the tokenizers and the scheduler are not in these files. Both students load them from `stabilityai/stable-diffusion-3-medium-diffusers`.
21
 
22
- ## Weights
23
-
24
- - `d2l-sd3-medium`
25
- - `d2l-sd3-medium-turbo`
26
-
27
- ## Relation to the released code
28
-
29
- The default config of the code (`configs/sd3m_d2l.yaml`) reproduces the plain distilled student after 100k steps, which gives the "Distill" rows of the paper (see the table in the GitHub README). `d2l-sd3-medium` is that 100k EMA combined 1:1 with the average of EMA weights from a continuation that regresses the flow-matching velocity of the training images (data target, no rollout rows). The continuation and the weight averaging are not part of the code release. `d2l-sd3-medium-turbo` was trained with the CDM four-step teacher (the transformer of `byliutao/stable-diffusion-3-medium-turbo`, with teacher CFG 1.0 in the rollout instead of 4.5), and its weights are the uniform average of five EMA checkpoints. The code supports sampling it (`--sigmas`), but its training configuration and the averaging are not included.
30
-
31
- ## Results
32
-
33
- The numbers below are from the paper. Avg. is the mean of the four text-to-image metrics, in percent relative to the teacher.
34
-
35
- `d2l-sd3-medium` (28 Euler steps, CFG 7, 512 px):
36
-
37
- | Model | K | GenEval overall | ImageReward | PickScore | HPSv2.1 | Avg. (%) |
38
- |---|---|---|---|---|---|---|
39
- | SD3-medium (teacher) | - | 0.648 | 0.908 | 22.214 | 0.278 | 100.0 |
40
- | D2L | 1 | 0.641 | 0.770 | 21.831 | 0.267 | 94.5 |
41
- | D2L | 2 | 0.707 | 1.021 | 22.282 | 0.281 | 105.8 |
42
- | D2L | 3 | 0.707 | 1.039 | 22.391 | 0.284 | 106.6 |
43
- | D2L | 4 | 0.710 | 1.037 | 22.390 | 0.283 | 106.6 |
44
-
45
- `d2l-sd3-medium-turbo` (4 steps at sigmas 1.0, 0.75, 0.5, 0.25, CFG 1, 512 px, K = 4):
46
-
47
- | Model | GenEval overall | ImageReward | PickScore | HPSv2.1 | Avg. (%) |
48
- |---|---|---|---|---|---|
49
- | CDM four-step SD3-medium (teacher) | 0.651 | 1.026 | 22.822 | 0.298 | 100.0 |
50
- | D2L | 0.592 | 0.739 | 22.381 | 0.273 | 88.2 |
51
-
52
- In the paper, loop guidance raises the average of the student in the last table to 99.2. Loop guidance is not included in the code release.
53
 
54
  ## Usage
55
 
56
- Install the code, then download the weights. Run the commands below from the root of the code checkout.
57
-
58
- ```bash
59
- git clone https://github.com/WinterCCC/Distill-to-Loop
60
- cd Distill-to-Loop
61
- pip install -e .
62
- ```
63
-
64
- The SD3-medium components must be in the Hugging Face cache before sampling. Accept the license of `stabilityai/stable-diffusion-3-medium-diffusers` on the Hub, log in, and download it:
65
-
66
- ```bash
67
- hf auth login
68
- hf download stabilityai/stable-diffusion-3-medium-diffusers
69
- ```
70
-
71
- Then download the weights of this repository:
72
-
73
  ```python
74
- from huggingface_hub import snapshot_download
75
-
76
- stage = snapshot_download("ShimaRinC/Distill-to-Loop") # contains d2l-sd3-medium/ and d2l-sd3-medium-turbo/
77
- print(stage)
78
- ```
79
-
80
- Command line, with `STAGE` set to the printed directory. The defaults of `scripts/generate.py` are those of `d2l-sd3-medium` (K 4, 28 steps, CFG 7, 512 px). `d2l-sd3-medium-turbo` needs `--sigmas` and `--cfg`. `prompts.txt` holds one prompt per line, and the image size must be a multiple of 16.
81
-
82
- ```bash
83
- python scripts/generate.py --student "$STAGE/d2l-sd3-medium" --prompts prompts.txt --out samples_d2l_sd3_medium --size 512 --micro-batch 2 --device cpu
84
- python scripts/generate.py --student "$STAGE/d2l-sd3-medium-turbo" --prompts prompts.txt --out samples_d2l_sd3_medium_turbo \
85
- --K 4 --sigmas 1.0 0.75 0.5 0.25 --cfg 1.0 --size 512 --micro-batch 2 --device cpu
86
- ```
87
-
88
- On a GPU, drop `--device cpu` (CUDA is the default when available). Use `--K` to change the loop count at sampling time.
89
-
90
- Python, with `STAGE` set to the directory printed above and `WORK` set to an output directory:
91
-
92
- ```python
93
- import os
94
- import time
95
-
96
  import torch
97
- from distill_to_loop import encode_prompts, generate_images, load_pipeline, load_student, load_vae_and_scheduler_factory
98
-
99
- stage, work = os.environ["STAGE"], os.environ["WORK"] # the two student directories live in STAGE
100
- model_id = "stabilityai/stable-diffusion-3-medium-diffusers" # VAE, text encoders and scheduler of both students
101
- device = "cuda" if torch.cuda.is_available() else "cpu"
102
- prompt = "a photo of a red bicycle leaning against a brick wall"
103
-
104
- encodings = encode_prompts([prompt], model_id, device) # prompts encoded once (T5 length 77)
105
- vae, scheduler_factory = load_vae_and_scheduler_factory(model_id, device)
106
-
107
- # d2l-sd3-medium: K = 4 loops, 28 Euler steps on the scheduler's grid, CFG 7, 512 px (the defaults of generate_images)
108
- t = time.time()
109
- student = load_student(f"{stage}/d2l-sd3-medium", device=device, loop_rounds=4)
110
- (_, _, image), = generate_images(student, vae, scheduler_factory, encodings, loop_rounds=4, steps=28, cfg_scale=7.0, resolution=512, seed=0,
111
- micro_batch_size=1, device=device)
112
- image.save(f"{work}/d2l_sd3_medium_k4.png")
113
- print("d2l-sd3-medium:", sum(p.numel() for p in student.parameters()), "parameters", f"{time.time() - t:.0f} s")
114
-
115
- # d2l-sd3-medium-turbo: K = 4 loops, 4 Euler steps at the sigmas 1.0, 0.75, 0.5, 0.25 (before the scheduler's shift), CFG 1, 512 px
116
- t = time.time()
117
- student = load_student(f"{stage}/d2l-sd3-medium-turbo", device=device, loop_rounds=4)
118
- (_, _, image), = generate_images(student, vae, scheduler_factory, encodings, loop_rounds=4, sigmas=[1.0, 0.75, 0.5, 0.25], cfg_scale=1.0,
119
- resolution=512, seed=0, micro_batch_size=1, device=device)
120
- image.save(f"{work}/d2l_sd3_medium_turbo_k4.png")
121
- print("d2l-sd3-medium-turbo:", sum(p.numel() for p in student.parameters()), "parameters", f"{time.time() - t:.0f} s")
122
 
123
- # The same student as the transformer of a diffusers StableDiffusion3Pipeline (LoopedSD3Pipeline: T5 length 77, bf16 autocast on CUDA)
124
- pipe = load_pipeline(f"{stage}/d2l-sd3-medium-turbo", device=device)
125
  pipe.transformer.loop_rounds = 4
126
- image = pipe(prompt, sigmas=[1.0, 0.75, 0.5, 0.25], guidance_scale=1.0, height=512, width=512,
127
- generator=torch.Generator(device).manual_seed(0)).images[0]
128
- image.save(f"{work}/d2l_sd3_medium_turbo_pipeline_k4.png")
129
- print("pipeline:", type(pipe).__name__, "timesteps", pipe.scheduler.timesteps.tolist())
130
  ```
131
 
132
- The pipeline draws its noise from `generator`, so its image differs from that of `generate_images`, whose noise is seeded per image. The sigmas of `d2l-sd3-medium-turbo` are given before the scheduler's shift of 3.0, so the transformer sees the timesteps 1000, 900, 750 and 500.
133
 
134
  ## License
135
 
136
- These weights are Derivative Works of Stable Diffusion 3 Medium and are licensed under the Stability AI Non-Commercial Research Community License (see `LICENSE`). Use is limited to non-commercial research. See `NOTICE` for the required attribution and the description of the changes made to Stable Diffusion 3 Medium. `d2l-sd3-medium-turbo` was distilled from `byliutao/stable-diffusion-3-medium-turbo`; see `NOTICE` for its attribution. The code is released under Apache-2.0 in the GitHub repository.
137
 
138
  ## Citation
139
 
 
13
  - sd3
14
  ---
15
 
16
+ # Distill to Loop (SD3-medium)
17
 
18
+ Looped students of SD3-medium from the paper "Distill to Loop: Turning Pretrained Diffusion Transformers into Looped Models with Adjustable Depth". The loop count is chosen at sampling time. Load the weights with the code at https://github.com/WinterCCC/Distill-to-Loop. The VAE, text encoders and scheduler come from `stabilityai/stable-diffusion-3-medium-diffusers`.
19
 
20
+ ## Files
21
 
22
+ - `d2l-sd3-medium/`: distilled from SD3-medium.
23
+ - `d2l-sd3-medium-turbo/`: distilled from the four-step CDM SD3-medium, `byliutao/stable-diffusion-3-medium-turbo`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  ## Usage
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  import torch
29
+ from huggingface_hub import snapshot_download
30
+ from distill_to_loop import load_pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ student_dir = snapshot_download("ShimaRinC/Distill-to-Loop") + "/d2l-sd3-medium"
33
+ pipe = load_pipeline(student_dir, device="cuda")
34
  pipe.transformer.loop_rounds = 4
35
+ image = pipe("a lighthouse on a cliff at sunset", num_inference_steps=28, guidance_scale=7.0, height=512, width=512,
36
+ generator=torch.Generator("cuda").manual_seed(0)).images[0]
37
+ image.save("out.png")
 
38
  ```
39
 
40
+ `d2l-sd3-medium-turbo` samples in four steps with `sigmas=[1.0, 0.75, 0.5, 0.25]` and `guidance_scale=1.0`.
41
 
42
  ## License
43
 
44
+ These weights are Derivative Works of Stable Diffusion 3 Medium and are licensed under the Stability AI Non-Commercial Research Community License. See [`LICENSE`](LICENSE) and [`NOTICE`](NOTICE).
45
 
46
  ## Citation
47