IMvision12 commited on
Commit
affebce
·
verified ·
1 Parent(s): 9520d48

Add stable-diffusion-3.5-medium: zeromodels Keras 3 conversion of stabilityai/stable-diffusion-3.5-medium

Browse files
README.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: text-to-image
3
+ license: other
4
+ base_model: stabilityai/stable-diffusion-3.5-medium
5
+ library_name: zeromodels
6
+ language:
7
+ - en
8
+ tags:
9
+ - keras
10
+ - zeromodels
11
+ - stable-diffusion
12
+ - stable-diffusion-3
13
+ - sd3
14
+ - mmdit
15
+ - text-to-image
16
+ - diffusion
17
+ - rectified-flow
18
+ - arxiv:2403.03206
19
+ - pytorch
20
+ - jax
21
+ - tf
22
+ ---
23
+ *See [our collection](https://huggingface.co/collections/zeromodels/stable-diffusion-v35-6aa7963880e76656e5646ffc) for all Stable Diffusion 3.5 checkpoints.*
24
+
25
+ # Run Stable Diffusion 3.5 with Keras 3: JAX, PyTorch, or TensorFlow
26
+
27
+ [![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-181717?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-Stable_Diffusion_3.5-1f6feb)](https://imvision12.github.io/ZeroModels/stable_diffusion_3_5/) [![HuggingFace](https://img.shields.io/badge/HuggingFace-Stable_Diffusion_3.5-ffd21e?logo=huggingface&logoColor=black)](https://huggingface.co/collections/zeromodels/stable-diffusion-v35-6aa7963880e76656e5646ffc)
28
+
29
+ # zeromodels/stable-diffusion-3.5-medium
30
+
31
+ Paper: [Scaling Rectified Flow Transformers for High-Resolution Image Synthesis (arXiv:2403.03206)](https://arxiv.org/abs/2403.03206) | [HF Papers](https://huggingface.co/papers/2403.03206)
32
+
33
+ Pure-**Keras 3** conversion of [`stabilityai/stable-diffusion-3.5-medium`](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium) for
34
+ [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on
35
+ **TensorFlow / Torch / JAX**. The container ships the MMDiT denoiser, the 16-channel VAE
36
+ and the two CLIP text encoders in `model.weights.json` shards (3.37B parameters,
37
+ 6.44 GB), plus `zm_config.json` (the component configs, the checkpoint's
38
+ `FlowMatchEulerDiscreteScheduler` (shift 3.0) and the default
39
+ generation settings) and the two tokenizers (`tokenizer.json`, the CLIP BPE, and
40
+ `tokenizer_3.json`, the T5 SentencePiece). Weights are stored in **float16**, the
41
+ checkpoint's native precision (the VAE in float32), and load in float16 by default; pass
42
+ `load_dtype="float32"` to `from_weights` for a float32 model. This checkpoint generates
43
+ **1024x1024** images (a 128x128x16 latent).
44
+
45
+ The third text encoder, the 4.7B-parameter **T5-XXL**, is shared by every SD 3 / 3.5
46
+ checkpoint and hosted once at
47
+ [`zeromodels/t5-v1_1-xxl-encoder`](https://huggingface.co/zeromodels/t5-v1_1-xxl-encoder); attach it
48
+ with `text_encoder_3=` (below) or leave it out (the T5 features are zeroed, SD 3's
49
+ memory-saving mode).
50
+
51
+ For model details, intended use and limitations, see the upstream
52
+ [model card](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium).
53
+
54
+ ## Architecture
55
+
56
+ | Component | zeromodels class | Details |
57
+ | --- | --- | --- |
58
+ | Denoiser | `SD3Transformer2DModel` | MMDiT: 24 joint blocks of 24 x 64-d heads (1536 wide), patch 2, 4096-d text features projected to 1536, 2048-d pooled conditioning, 384x384 position grid, RMS-normalized queries and keys and dual attention in blocks 0 to 12 |
59
+ | Autoencoder | `AutoencoderKL` | (128, 256, 512, 512) channels, x8 spatial compression to 16 latent channels, `scaling_factor` 1.5305, `shift_factor` 0.0609, float32 (`force_upcast`) |
60
+ | Text encoder | functional CLIP text tower | CLIP ViT-L/14: 768-d, 12 layers, `quick_gelu`, 768-d projection; penultimate hidden state + projected pooled state |
61
+ | Text encoder 2 | functional CLIP text tower | OpenCLIP ViT-bigG/14: 1280-d, 32 layers, `gelu`, 1280-d projection; penultimate hidden state + projected pooled state |
62
+ | Text encoder 3 | `SD3T5EncoderModel` (separate repo) | T5 v1.1 XXL encoder: 4096-d, 24 layers, gated GELU, 256 tokens |
63
+ | Scheduler | `FlowMatchEulerDiscreteScheduler` | rectified flow over 1000 timesteps, shift 3.0 |
64
+
65
+ ## Quick start
66
+
67
+ ```python
68
+ import os
69
+ os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
70
+
71
+ from PIL import Image
72
+ from zeromodels.models.stable_diffusion_3_5 import StableDiffusion3_5TextToImage, StableDiffusion3_5Tokenizer
73
+
74
+ model = StableDiffusion3_5TextToImage.from_weights(
75
+ "zeromodels/stable-diffusion-3.5-medium",
76
+ text_encoder_3="zeromodels/t5-v1_1-xxl-encoder", # optional: omit to zero the T5 features
77
+ )
78
+ tokenizer = StableDiffusion3_5Tokenizer.from_weights("zeromodels/stable-diffusion-3.5-medium")
79
+
80
+ inputs = tokenizer("a photograph of an astronaut riding a horse")
81
+ images = model.generate(**inputs, num_inference_steps=40, guidance_scale=4.5, seed=0)
82
+ Image.fromarray(images[0]).save("astronaut.png") # (1024, 1024, 3) uint8
83
+ ```
84
+
85
+ `generate` takes the tokenizer's `input_ids` / `attention_mask` / `input_ids_3` (batch them
86
+ for several prompts), an optional tokenized negative prompt (`negative_input_ids` /
87
+ `negative_input_ids_3`), `num_inference_steps`, `guidance_scale`, a `seed`, or explicit
88
+ `latents` of shape `(batch, 128, 128, 16)` for results that are identical across
89
+ backends; `image` / `strength` refine an image instead.
90
+
91
+ Load any Stable Diffusion 3.5 checkpoint the same way with `from_weights("zeromodels/<variant>")`:
92
+
93
+ | Variant | Hub | Training |
94
+ | --- | --- | --- |
95
+ | `stable-diffusion-3.5-large` | [zeromodels/stable-diffusion-3.5-large](https://huggingface.co/zeromodels/stable-diffusion-3.5-large) | 1024px, rectified flow (shift 3), 28 steps at guidance 3.5: the 8B MMDiT with RMS-normalized queries and keys |
96
+ | `stable-diffusion-3.5-large-turbo` | [zeromodels/stable-diffusion-3.5-large-turbo](https://huggingface.co/zeromodels/stable-diffusion-3.5-large-turbo) | 1024px, 4 steps, no guidance: SD 3.5 large distilled with Adversarial Diffusion Distillation |
97
+ | `stable-diffusion-3.5-medium` | [zeromodels/stable-diffusion-3.5-medium](https://huggingface.co/zeromodels/stable-diffusion-3.5-medium) | 1024px (up to 2 MP), rectified flow (shift 3), 40 steps at guidance 4.5: the 2.5B MMDiT-X with dual-attention blocks |
98
+
99
+ ## Tips
100
+
101
+ - Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
102
+ - The graphs are built for 1024px. Pass `transformer_sample_size=<px / 8>, vae_sample_size=<px>` to
103
+ `from_weights` to build for another multiple of 16px (the weights are resolution-independent
104
+ up to the 6144px position grid).
105
+ - `StableDiffusion3_5Model.from_weights(...)` loads the same repo as the bare container
106
+ (`.transformer` / `.vae` / `.text_encoder` / `.text_encoder_2`) without the generation loop.
107
+ - `model.text_encoder_3` can be any `SD3T5EncoderModel` (for example one loaded with
108
+ `quantization="int8"`); it is not part of the container's weights.
109
+ - Both `channels_last` and `channels_first` are supported (`keras.config.set_image_data_format`
110
+ before loading); `generate` always returns `(batch, H, W, 3)` uint8.
111
+ - On-the-fly `hf:` conversion is not supported for diffusion models; the checkpoints are
112
+ hosted here, converted once.
113
+ - See the [Stable Diffusion 3.5 docs](https://imvision12.github.io/ZeroModels/stable_diffusion_3_5/).
114
+
115
+ ## License
116
+
117
+ The weights are redistributed under the
118
+ [Stability AI Community License](https://huggingface.co/stabilityai/stable-diffusion-3.5-large/blob/main/LICENSE.md) of the upstream checkpoint,
119
+ including its use-based restrictions. By using them you agree to those terms.
120
+
121
+ ## Special Thanks
122
+
123
+ Thank you to Stability AI for training and releasing Stable Diffusion, and to the
124
+ Hugging Face diffusers team, whose implementation this port was verified against.
model.weights.json ADDED
The diff for this file is too large to render. See raw diff
 
model_00000.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b975072fd41d76c5c294926ce36678cdab6d8f6842dd1faecf73cd9c1d4c716f
3
+ size 5360876896
model_00001.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:227820b84981e8b140355154e955d4e61bc2d27aec4b05cac6b8146f5b320d2e
3
+ size 1554114672
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_3.json ADDED
The diff for this file is too large to render. See raw diff
 
zm_config.json ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "library_name": "zeromodels",
3
+ "zeromodels_version": "1.3.3",
4
+ "model_module": "zeromodels.models.stable_diffusion_3_5",
5
+ "model_class": "StableDiffusion3_5Model",
6
+ "variant": "stable-diffusion-3.5-medium",
7
+ "weights": "model.weights.json",
8
+ "schema_version": 2,
9
+ "weight_dtype": "float16",
10
+ "weight_dtype_overrides": {
11
+ "vae_encoder": "float32",
12
+ "vae_decoder": "float32"
13
+ },
14
+ "model_type": "stable_diffusion_3_5",
15
+ "transformer_config": {
16
+ "sample_size": 128,
17
+ "patch_size": 2,
18
+ "in_channels": 16,
19
+ "out_channels": 16,
20
+ "num_layers": 24,
21
+ "attention_head_dim": 64,
22
+ "num_attention_heads": 24,
23
+ "joint_attention_dim": 4096,
24
+ "caption_projection_dim": 1536,
25
+ "pooled_projection_dim": 2048,
26
+ "pos_embed_max_size": 384,
27
+ "qk_norm": "rms_norm",
28
+ "dual_attention_layers": [
29
+ 0,
30
+ 1,
31
+ 2,
32
+ 3,
33
+ 4,
34
+ 5,
35
+ 6,
36
+ 7,
37
+ 8,
38
+ 9,
39
+ 10,
40
+ 11,
41
+ 12
42
+ ],
43
+ "text_seq_len": 333
44
+ },
45
+ "vae_config": {
46
+ "in_channels": 3,
47
+ "out_channels": 3,
48
+ "latent_channels": 16,
49
+ "block_out_channels": [
50
+ 128,
51
+ 256,
52
+ 512,
53
+ 512
54
+ ],
55
+ "layers_per_block": 2,
56
+ "norm_num_groups": 32,
57
+ "sample_size": 1024,
58
+ "scaling_factor": 1.5305,
59
+ "force_upcast": true,
60
+ "shift_factor": 0.0609,
61
+ "use_quant_conv": false,
62
+ "use_post_quant_conv": false
63
+ },
64
+ "text_config": {
65
+ "hidden_dim": 768,
66
+ "num_heads": 12,
67
+ "num_layers": 12,
68
+ "mlp_ratio": 4.0,
69
+ "vocab_size": 49408,
70
+ "max_seq_len": 77,
71
+ "projection_dim": 768,
72
+ "hidden_act": "quick_gelu"
73
+ },
74
+ "text_config_2": {
75
+ "hidden_dim": 1280,
76
+ "num_heads": 20,
77
+ "num_layers": 32,
78
+ "mlp_ratio": 4.0,
79
+ "vocab_size": 49408,
80
+ "max_seq_len": 77,
81
+ "projection_dim": 1280,
82
+ "hidden_act": "gelu"
83
+ },
84
+ "hidden_act": "quick_gelu",
85
+ "layer_norm_eps": 1e-05,
86
+ "bos_token_id": 49406,
87
+ "eos_token_id": 49407,
88
+ "pad_token_id": 49407,
89
+ "pad_token_id_2": 0,
90
+ "max_sequence_length": 256,
91
+ "eos_token_id_3": 1,
92
+ "pad_token_id_3": 0,
93
+ "scheduler_config": {
94
+ "_class_name": "FlowMatchEulerDiscreteScheduler",
95
+ "num_train_timesteps": 1000,
96
+ "shift": 3.0
97
+ },
98
+ "generate_args": {
99
+ "num_inference_steps": 40,
100
+ "guidance_scale": 4.5
101
+ }
102
+ }