Instructions to use ShimaRinC/Distill-to-Loop with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use ShimaRinC/Distill-to-Loop with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("ShimaRinC/Distill-to-Loop", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Add D2L looped SD3-medium students (d2l-sd3-medium, d2l-sd3-medium-turbo), LICENSE, NOTICE, model card
Browse files- LICENSE +29 -0
- NOTICE +50 -0
- README.md +146 -0
- d2l-sd3-medium-turbo/config.json +20 -0
- d2l-sd3-medium-turbo/diffusion_pytorch_model.safetensors +3 -0
- d2l-sd3-medium/config.json +20 -0
- d2l-sd3-medium/diffusion_pytorch_model.safetensors +3 -0
LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
STABILITY AI NON-COMMERCIAL RESEARCH COMMUNITY LICENSE AGREEMENT
|
| 2 |
+
Dated: December 06, 2023
|
| 3 |
+
|
| 4 |
+
By using or distributing any portion or element of the Models, Software, Software Products or Derivative Works, you agree to be bound by this Agreement.
|
| 5 |
+
|
| 6 |
+
"Agreement" means this Stable Non-Commercial Research Community License Agreement.
|
| 7 |
+
|
| 8 |
+
“AUP” means the Stability AI Acceptable Use Policy available at https://stability.ai/use-policy, as may be updated from time to time.
|
| 9 |
+
"Derivative Work(s)” means (a) any derivative work of the Software Products as recognized by U.S. copyright laws and (b) any modifications to a Model, and any other model created which is based on or derived from the Model or the Model’s output. For clarity, Derivative Works do not include the output of any Model.
|
| 10 |
+
“Documentation” means any specifications, manuals, documentation, and other written information provided by Stability AI related to the Software.
|
| 11 |
+
"Licensee" or "you" means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity's behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf.
|
| 12 |
+
“Model(s)" means, collectively, Stability AI’s proprietary models and algorithms, including machine-learning models, trained model weights and other elements of the foregoing, made available under this Agreement.
|
| 13 |
+
“Non-Commercial Uses” means exercising any of the rights granted herein for the purpose of research or non-commercial purposes. Non-Commercial Uses does not include any production use of the Software Products or any Derivative Works.
|
| 14 |
+
"Stability AI" or "we" means Stability AI Ltd. and its affiliates.
|
| 15 |
+
"Software" means Stability AI’s proprietary software made available under this Agreement.
|
| 16 |
+
“Software Products” means the Models, Software and Documentation, individually or in any combination.
|
| 17 |
+
1. License Rights and Redistribution.
|
| 18 |
+
a. Subject to your compliance with this Agreement, the AUP (which is hereby incorporated herein by reference), and the Documentation, Stability AI grants you a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable, royalty free and limited license under Stability AI’s intellectual property or other rights owned or controlled by Stability AI embodied in the Software Products to use, reproduce, distribute, and create Derivative Works of, the Software Products, in each case for Non-Commercial Uses only.
|
| 19 |
+
b. You may not use the Software Products or Derivative Works to enable third parties to use the Software Products or Derivative Works as part of your hosted service or via your APIs, whether you are adding substantial additional functionality thereto or not. Merely distributing the Software Products or Derivative Works for download online without offering any related service (ex. by distributing the Models on HuggingFace) is not a violation of this subsection. If you wish to use the Software Products or any Derivative Works for commercial or production use or you wish to make the Software Products or any Derivative Works available to third parties via your hosted service or your APIs, contact Stability AI at https://stability.ai/contact.
|
| 20 |
+
c. If you distribute or make the Software Products, or any Derivative Works thereof, available to a third party, the Software Products, Derivative Works, or any portion thereof, respectively, will remain subject to this Agreement and you must (i) provide a copy of this Agreement to such third party, and (ii) retain the following attribution notice within a "Notice" text file distributed as a part of such copies: "This Stability AI Model is licensed under the Stability AI Non-Commercial Research Community License, Copyright (c) Stability AI Ltd. All Rights Reserved.” If you create a Derivative Work of a Software Product, you may add your own attribution notices to the Notice file included with the Software Product, provided that you clearly indicate which attributions apply to the Software Product and you must state in the NOTICE file that you changed the Software Product and how it was modified.
|
| 21 |
+
2. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE SOFTWARE PRODUCTS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE SOFTWARE PRODUCTS, DERIVATIVE WORKS OR ANY OUTPUT OR RESULTS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE SOFTWARE PRODUCTS, DERIVATIVE WORKS AND ANY OUTPUT AND RESULTS.
|
| 22 |
+
3. Limitation of Liability. IN NO EVENT WILL STABILITY AI OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF STABILITY AI OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
|
| 23 |
+
4. Intellectual Property.
|
| 24 |
+
a. No trademark licenses are granted under this Agreement, and in connection with the Software Products or Derivative Works, neither Stability AI nor Licensee may use any name or mark owned by or associated with the other or any of its affiliates, except as required for reasonable and customary use in describing and redistributing the Software Products or Derivative Works.
|
| 25 |
+
b. Subject to Stability AI’s ownership of the Software Products and Derivative Works made by or for Stability AI, with respect to any Derivative Works that are made by you, as between you and Stability AI, you are and will be the owner of such Derivative Works
|
| 26 |
+
c. If you institute litigation or other proceedings against Stability AI (including a cross-claim or counterclaim in a lawsuit) alleging that the Software Products, Derivative Works or associated outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Stability AI from and against any claim by any third party arising out of or related to your use or distribution of the Software Products or Derivative Works in violation of this Agreement.
|
| 27 |
+
5. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the Software Products and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Stability AI may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of any Software Products or Derivative Works. Sections 2-4 shall survive the termination of this Agreement.
|
| 28 |
+
6. Governing Law. This Agreement will be governed by and construed in accordance with the laws of the United States and the State of California without regard to choice of law
|
| 29 |
+
principles.
|
NOTICE
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This Stability AI Model is licensed under the Stability AI Non-Commercial Research Community License, Copyright (c) Stability AI Ltd. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
The attribution notice above applies to Stable Diffusion 3 Medium (stabilityai/stable-diffusion-3-medium-diffusers) and to both weight sets
|
| 4 |
+
distributed here, d2l-sd3-medium and d2l-sd3-medium-turbo, which are Derivative Works of it. The license is in the LICENSE file.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
Changes to Stable Diffusion 3 Medium (Distill to Loop)
|
| 8 |
+
=======================================================
|
| 9 |
+
|
| 10 |
+
Both weight sets replace the 24-block MMDiT transformer of Stable Diffusion 3 Medium by a looped student transformer. The VAE, the three
|
| 11 |
+
text encoders, the tokenizers and the scheduler are not changed and are not distributed here; they are loaded from
|
| 12 |
+
stabilityai/stable-diffusion-3-medium-diffusers.
|
| 13 |
+
|
| 14 |
+
Architecture (identical for both weight sets): 9 MMDiT blocks instead of 24, namely 2 input blocks, 5 shared blocks that are applied K times
|
| 15 |
+
in a loop (K = 4 by default; K can be changed at sampling time), and 2 output blocks; 756,415,672 parameters. Added to the teacher's
|
| 16 |
+
modules: a depth embedding that conditions the shared blocks on the loop position, a fixed depth-step update of both token streams after
|
| 17 |
+
each loop, per-stream QK-norm gains on the shared and output blocks, and a soft attention modification (XSA) on the shared blocks. The patch
|
| 18 |
+
embedding, positional embedding, time and text embeddings, context embedder, output norm and output projection keep the teacher's
|
| 19 |
+
structure.
|
| 20 |
+
|
| 21 |
+
Initialization: the student's blocks are copies of teacher blocks. The input blocks copy teacher blocks 0 and 1, the shared blocks copy
|
| 22 |
+
teacher blocks 2, 6, 10, 14 and 18, and the output blocks copy teacher blocks 22 and 23. The embeddings, the output norm and the output
|
| 23 |
+
projection are copied from the teacher. The added modules start from their own initialization.
|
| 24 |
+
|
| 25 |
+
Training: the student was trained by distillation from the teacher transformer on COCO 2017 train images (512 px) and captions, with the
|
| 26 |
+
outputs after every loop trained toward the teacher, and an exponential moving average (EMA, decay 0.9995) of its weights was kept. The
|
| 27 |
+
released weights are post-hoc combinations of EMA weights, as follows.
|
| 28 |
+
|
| 29 |
+
d2l-sd3-medium
|
| 30 |
+
Teacher: the Stable Diffusion 3 Medium transformer; the student's blocks were initialized from it.
|
| 31 |
+
Weights: 0.5 x the EMA weights at step 100,000 of the distillation run + 0.5 x the uniform average of the EMA weights at steps 105,000,
|
| 32 |
+
110,000, 115,000 and 120,000 of a continuation of that run from step 100,000 in which the training target was the flow-matching
|
| 33 |
+
velocity of the training images instead of the teacher's prediction.
|
| 34 |
+
|
| 35 |
+
d2l-sd3-medium-turbo
|
| 36 |
+
Teacher: the transformer of byliutao/stable-diffusion-3-medium-turbo (revision 9ebe6142e456b3d262e8739d7b99fa4213d2c008), a four-step
|
| 37 |
+
model distilled from Stable Diffusion 3 Medium with Continuous-Time Distribution Matching (CDM); the student's blocks were initialized
|
| 38 |
+
from this transformer.
|
| 39 |
+
Weights: the uniform average of the EMA weights at steps 100,000, 105,000, 110,000, 115,000 and 120,000.
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Attribution for d2l-sd3-medium-turbo only
|
| 43 |
+
=========================================
|
| 44 |
+
|
| 45 |
+
The teacher of d2l-sd3-medium-turbo is the transformer of byliutao/stable-diffusion-3-medium-turbo
|
| 46 |
+
(https://huggingface.co/byliutao/stable-diffusion-3-medium-turbo, revision 9ebe6142e456b3d262e8739d7b99fa4213d2c008), from
|
| 47 |
+
"Continuous-Time Distribution Matching for Few-Step Diffusion Distillation" by Tao Liu, Hao Yan, Mengting Chen, Taihang Hu, Zhengrong Yue,
|
| 48 |
+
Zihao Pan, Jinsong Lan, Xiaoyong Zhu, Ming-Ming Cheng, Bo Zheng and Yaxing Wang (arXiv:2605.06376; code https://github.com/byliutao/cdm).
|
| 49 |
+
The README of that repository states: "This project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details."
|
| 50 |
+
That model is itself a Derivative Work of Stable Diffusion 3 Medium.
|
README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: stabilityai-ai-nc-research-community
|
| 4 |
+
license_link: LICENSE
|
| 5 |
+
base_model:
|
| 6 |
+
- stabilityai/stable-diffusion-3-medium-diffusers
|
| 7 |
+
- byliutao/stable-diffusion-3-medium-turbo
|
| 8 |
+
pipeline_tag: text-to-image
|
| 9 |
+
tags:
|
| 10 |
+
- text-to-image
|
| 11 |
+
- looped-transformer
|
| 12 |
+
- distillation
|
| 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 |
+
| Folder | What it is | Sampling setting | Size | sha256 of `diffusion_pytorch_model.safetensors` |
|
| 25 |
+
|---|---|---|---|---|
|
| 26 |
+
| `d2l-sd3-medium` | Student distilled from the SD3-medium transformer; weights are a post-hoc combination of EMA weights (see `NOTICE`) | K = 4, 28 Euler steps, CFG 7, 512 px | 3,252,186,136 bytes | `fe3f7d65b6d6ca9f7c0de1751526eee97bb27810319f5872289d92ff7ea689c3` |
|
| 27 |
+
| `d2l-sd3-medium-turbo` | Student distilled from the transformer of `byliutao/stable-diffusion-3-medium-turbo`, a four-step SD3-medium; weights are the uniform average of five EMA checkpoints (see `NOTICE`) | K = 4, 4 Euler steps at sigmas 1.0, 0.75, 0.5, 0.25, CFG 1, 512 px | 3,252,186,232 bytes | `cdeb150208aa8a6b26c478ef2e0caf02d1f50bb1b0da0497741df557835b0a38` |
|
| 28 |
+
|
| 29 |
+
Each folder holds `config.json` and `diffusion_pytorch_model.safetensors` (fp32).
|
| 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 |
+
|
| 140 |
+
```bibtex
|
| 141 |
+
@article{d2l,
|
| 142 |
+
title = {Distill to Loop: Turning Pretrained Diffusion Transformers into Looped Models with Adjustable Depth},
|
| 143 |
+
journal = {arXiv preprint arXiv:<ARXIV_ID>},
|
| 144 |
+
year = {<YEAR>}
|
| 145 |
+
}
|
| 146 |
+
```
|
d2l-sd3-medium-turbo/config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "LoopedSD3Transformer",
|
| 3 |
+
"_diffusers_version": "0.40.0",
|
| 4 |
+
"attention_head_dim": 64,
|
| 5 |
+
"caption_projection_dim": 1536,
|
| 6 |
+
"depth_embed_freqs": 256,
|
| 7 |
+
"in_channels": 16,
|
| 8 |
+
"joint_attention_dim": 4096,
|
| 9 |
+
"n_loop": 5,
|
| 10 |
+
"n_post": 2,
|
| 11 |
+
"n_pre": 2,
|
| 12 |
+
"n_rounds": 4,
|
| 13 |
+
"num_attention_heads": 24,
|
| 14 |
+
"out_channels": 16,
|
| 15 |
+
"patch_size": 2,
|
| 16 |
+
"pooled_projection_dim": 2048,
|
| 17 |
+
"pos_embed_max_size": 192,
|
| 18 |
+
"qk_norm": null,
|
| 19 |
+
"sample_size": 128
|
| 20 |
+
}
|
d2l-sd3-medium-turbo/diffusion_pytorch_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cdeb150208aa8a6b26c478ef2e0caf02d1f50bb1b0da0497741df557835b0a38
|
| 3 |
+
size 3252186232
|
d2l-sd3-medium/config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "LoopedSD3Transformer",
|
| 3 |
+
"_diffusers_version": "0.40.0",
|
| 4 |
+
"attention_head_dim": 64,
|
| 5 |
+
"caption_projection_dim": 1536,
|
| 6 |
+
"depth_embed_freqs": 256,
|
| 7 |
+
"in_channels": 16,
|
| 8 |
+
"joint_attention_dim": 4096,
|
| 9 |
+
"n_loop": 5,
|
| 10 |
+
"n_post": 2,
|
| 11 |
+
"n_pre": 2,
|
| 12 |
+
"n_rounds": 4,
|
| 13 |
+
"num_attention_heads": 24,
|
| 14 |
+
"out_channels": 16,
|
| 15 |
+
"patch_size": 2,
|
| 16 |
+
"pooled_projection_dim": 2048,
|
| 17 |
+
"pos_embed_max_size": 192,
|
| 18 |
+
"qk_norm": null,
|
| 19 |
+
"sample_size": 128
|
| 20 |
+
}
|
d2l-sd3-medium/diffusion_pytorch_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fe3f7d65b6d6ca9f7c0de1751526eee97bb27810319f5872289d92ff7ea689c3
|
| 3 |
+
size 3252186136
|