File size: 11,885 Bytes
923499f
 
 
 
 
44ff3d3
 
264905e
 
44ff3d3
 
 
f1dd006
 
923499f
264905e
 
 
 
1a9dab2
264905e
 
 
 
deb4641
 
264905e
 
 
 
 
 
 
 
 
 
 
 
 
cd47cca
f99f763
 
97fb4ba
 
f99f763
 
 
 
 
cfad286
 
964124e
4774f98
964124e
cfad286
964124e
4774f98
964124e
f99f763
ce21df3
 
 
 
 
 
 
 
 
05566dd
ce21df3
 
 
 
 
8933bba
ce21df3
 
 
 
 
4774f98
 
ce21df3
c1b4ea2
9a6f7f7
ce21df3
083e006
ce21df3
 
f99f763
cd47cca
 
7bb6fbf
cd47cca
 
 
 
 
 
 
 
96b0161
bf189c1
cd47cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de04131
06e9a10
 
 
de04131
06e9a10
 
99eb79b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06e9a10
99eb79b
06e9a10
de04131
d649506
06e9a10
 
de04131
 
 
 
 
 
06e9a10
 
cd47cca
05566dd
8933bba
cd47cca
 
99eb79b
de04131
 
cd47cca
de04131
 
cd47cca
 
de04131
 
 
 
 
 
 
cd47cca
 
 
 
9a6f7f7
c994d20
4774f98
 
cd47cca
 
 
 
 
 
 
 
 
 
 
 
 
721f70c
 
5cafa9f
721f70c
89d8d09
 
721f70c
 
 
 
 
 
 
 
 
 
 
 
 
5cafa9f
721f70c
89d8d09
 
721f70c
 
 
 
 
 
 
 
 
 
 
 
264905e
f598b29
264905e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9c463c
 
264905e
 
 
 
 
 
 
 
 
 
 
44ff3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
---
pipeline_tag: text-to-image
license: other
license_name: faipl-1.0-sd
license_link: LICENSE
datasets: Disty0/sotediffusion-text_only
base_model: Disty0/sotediffusion-wuerstchen3
tags:
- text-to-image
- anime
language: en
library_name: diffusers
prior:
- Disty0/sotediffusion-v2-prior
---


# SoteDiffusion V2

An Anime finetune of Würstchen V3 / Stable Cascade. 

# Release Notes

- This release is sponsored by <a href="https://fal.ai/grants?rel=sote-diffusion" target="_blank">fal.ai/grants</a>  
- Trained on 12M text & image paris including WD tags and natural language captions for a single epoch on 8xH100 80GB SXM5 GPUs.  
- Trained with Full FP32 and MAE Loss.  

<style>
.image {
    float: left;
    margin-left: 10px;
}
</style>

<table>
<img class="image" src="https://cdn-uploads.huggingface.co/production/uploads/6456af6195082f722d178522/KJTHqR3otoKoiXxvbudp8.png" width="320">
<img class="image" src="https://cdn-uploads.huggingface.co/production/uploads/6456af6195082f722d178522/uua4L9aaqJ0LI8gYv4xmC.png" width="320">
</table>


# ComfyUI

Use these arguments when starting ComfyUI: `--fp16-vae --fp16-unet`  

Download the Stage C to unet folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_c.safetensors  
Download the Stage C Text Encoder to clip folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_c_text_encoder.safetensors  
Download the Stage B to unet folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_b.safetensors  
Download the Stage A to vae folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/stage_a_ft_hq.safetensors  

Download the workflow and load it: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/comfyui_workflow.json?download=true

Stage C sampler: DPMPP 2M or DPMPP 2M SDE with SGM Uniform scheduler    
Stage C steps: 28  
Stage C CFG: 6.0  

Stage B sampler: LCM with Exponential scheduler  
Stage B steps: 14  
Stage B CFG: 1.0  

## SD.Next
URL: https://github.com/vladmandic/automatic/

Go to Models -> Huggingface and type `Disty0/sotediffusion-v2` into the model name and press download.  
Load `Disty0/sotediffusion-v2` after the download process is complete.  

Prompt:  
```
your prompt goes here
very aesthetic, best quality, newest,
```
(New lines act the same way as BREAK in SD.Next)  

Negative Prompt:  
```
very displeasing, displeasing, worst quality, bad quality, low quality, realistic, monochrome, comic, sketch, oldest, early, artist name, signature, blurry, simple background, upside down,
```

Parameters:  
Sampler: Default  

Steps: 28  
Refiner Steps: 14  

CFG: 5.0 to 6.0  
Secondary CFG: 1.0 to 1.5  

Resolution: 1280x1280, 1024x1536, 1024x2048, 2048x1152  
Anything works as long as it's a multiply of 128.  

# Diffusers

```shell
pip install git+https://github.com/huggingface/diffusers
```

```python
import torch
import diffusers

device = "cuda"
dtype = torch.float16
model_path = "Disty0/sotediffusion-v2"
pipe = diffusers.AutoPipelineForText2Image.from_pretrained(model_path, torch_dtype=dtype)

# de-dupe
pipe.decoder_pipe.text_encoder = pipe.text_encoder = None # nothing uses this
del pipe.decoder_pipe.text_encoder
del pipe.prior_prior
del pipe.prior_text_encoder
del pipe.prior_tokenizer
del pipe.prior_scheduler
del pipe.prior_feature_extractor
del pipe.prior_image_encoder

pipe = pipe.to(device, dtype=dtype)
pipe.prior_pipe = pipe.prior_pipe.to(device, dtype=dtype)


def encode_prompt(
    prior_pipe,
    device,
    num_images_per_prompt,
    prompt=""
    ):

    if prompt == "":
        text_inputs = prior_pipe.tokenizer(
            prompt,
            padding="max_length",
            max_length=77,
            truncation=False,
            return_tensors="pt",
        )
        input_ids = text_inputs.input_ids
        attention_mask=None
    else:   
        text_inputs = prior_pipe.tokenizer(
            prompt,
            padding="longest",
            truncation=False,
            return_tensors="pt",
        )
        chunk = []
        padding = []
        max_len = 75
        start_token = text_inputs.input_ids[:,0].unsqueeze(0)
        end_token = text_inputs.input_ids[:,-1].unsqueeze(0)
        raw_input_ids = text_inputs.input_ids[:,1:-1]
        prompt_len = len(raw_input_ids[0])
        last_lenght = prompt_len % max_len
        
        for i in range(int((prompt_len - last_lenght) / max_len)):
            chunk.append(torch.cat([start_token, raw_input_ids[:,i*max_len:(i+1)*max_len], end_token], dim=1))
        for i in range(max_len - last_lenght):
            padding.append(text_inputs.input_ids[:,-1])
        
        last_chunk = torch.cat([raw_input_ids[:,prompt_len-last_lenght:], torch.tensor([padding])], dim=1)
        chunk.append(torch.cat([start_token, last_chunk, end_token], dim=1))
        input_ids = torch.cat(chunk, dim=0)
        attention_mask = torch.ones(input_ids.shape, device=device, dtype=torch.int64)
        attention_mask[-1,last_lenght+1:] = 0

    text_encoder_output = prior_pipe.text_encoder(
        input_ids.to(device), attention_mask=attention_mask, output_hidden_states=True
    )

    prompt_embeds = text_encoder_output.hidden_states[-1].reshape(1,-1,1280)
    prompt_embeds = prompt_embeds.to(dtype=prior_pipe.text_encoder.dtype, device=device)
    prompt_embeds = prompt_embeds.repeat_interleave(num_images_per_prompt, dim=0)

    prompt_embeds_pooled = text_encoder_output.text_embeds[0].unsqueeze(0).unsqueeze(1)
    prompt_embeds_pooled = prompt_embeds_pooled.to(dtype=prior_pipe.text_encoder.dtype, device=device)
    prompt_embeds_pooled = prompt_embeds_pooled.repeat_interleave(num_images_per_prompt, dim=0)

    return prompt_embeds, prompt_embeds_pooled


prompt = "1girl, solo, looking at viewer, open mouth, blue eyes, medium breasts, blonde hair, gloves, dress, bow, hair between eyes, bare shoulders, upper body, hair bow, indoors, elbow gloves, hand on own chest, bridal gauntlets, candlestand, smile, rim lighting, from side, castle interior, looking side,"
quality_prompt = "very aesthetic, best quality, newest"
negative_prompt = "very displeasing, displeasing, worst quality, bad quality, low quality, realistic, monochrome, comic, sketch, oldest, early, artist name, signature, blurry, simple background, upside down,"
num_images_per_prompt=1

# Encode prompts and quality prompts eperately, long prompt support and don't use attention masks for empty prompts:
# pipe, device, num_images_per_prompt, prompt
empty_prompt_embeds, _ = encode_prompt(pipe.prior_pipe, device, num_images_per_prompt, prompt="")

prompt_embeds, prompt_embeds_pooled = encode_prompt(pipe.prior_pipe, device, num_images_per_prompt, prompt=prompt)
quality_prompt_embeds, _ = encode_prompt(pipe.prior_pipe, device, num_images_per_prompt, prompt=quality_prompt)
prompt_embeds = torch.cat([prompt_embeds, quality_prompt_embeds], dim=1)

negative_prompt_embeds, negative_prompt_embeds_pooled = encode_prompt(pipe.prior_pipe, device, num_images_per_prompt, prompt=negative_prompt)

while prompt_embeds.shape[1] < negative_prompt_embeds.shape[1]:
    prompt_embeds = torch.cat([prompt_embeds, empty_prompt_embeds], dim=1)

while negative_prompt_embeds.shape[1] < prompt_embeds.shape[1]:
    negative_prompt_embeds = torch.cat([negative_prompt_embeds, empty_prompt_embeds], dim=1)

output = pipe(
    width=1024,
    height=1536,
    decoder_guidance_scale=1.0,
    prior_guidance_scale=5.0,
    prior_num_inference_steps=28,
    num_inference_steps=14,
    output_type="pil",
    prompt=prompt + " " + quality_prompt,
    negative_prompt=negative_prompt,
    prompt_embeds=prompt_embeds,
    prompt_embeds_pooled=prompt_embeds_pooled,
    negative_prompt_embeds=negative_prompt_embeds,
    negative_prompt_embeds_pooled=negative_prompt_embeds_pooled,
    num_images_per_prompt=num_images_per_prompt,
).images[0]

display(output)
```

## Training:


### Stage C
**Base model**: Disty0/sotediffusion-wuerstchen3  
**GPU used**: 7x Nvidia H100 80GB SXM5  
| parameter | value |
|---|---|
| **amp** | no |
| **weights** | fp32 |
| **save weights** | fp32 |
| **resolution** | 1024x1024 |
| **effective batch size** | 84 |
| **unet learning rate** | 2e-6 |
| **te learning rate** | 1e-7 |
| **optimizer** | AdamW 8bit |
| **images** | 6M * 2 captions per image |
| **epochs** | 1 |


### Stage B
**Base model**: Disty0/sotediffusion-wuerstchen3-decoder  
**GPU used**: 1x Nvidia H100 80GB SXM5  
| parameter | value |
|---|---|
| **amp** | no |
| **weights** | fp32 |
| **save weights** | fp32 |
| **resolution** | 1024x1024 |
| **effective batch size** | 8 |
| **unet learning rate** |  8e-6 |
| **te learning rate** | none |
| **optimizer** | AdamW |
| **images** | 120K |
| **epochs** | 6 |


## WD Tags:

Model is trained with this tag order:  
```
aesthetic tags, quality tags, date tags, custom tags, rating tags, character, series, rest of the tags
```

### Date:

| tag | date |
|---|---|
| **newest** | 2022 to 2024 |
| **recent** | 2019 to 2021 |
| **mid** | 2015 to 2018 |
| **early** | 2011 to 2014 |
| **oldest** | 2005 to 2010 |

### Aesthetic Tags:
**Model used**: shadowlilac/aesthetic-shadow-v2

| score greater than | tag | count |
|---|---|---|
| **0.90** | extremely aesthetic | 125.451 |
| **0.80** | very aesthetic | 887.382 |
| **0.70** | aesthetic | 1.049.857 |
| **0.50** | slightly aesthetic | 1.643.091 |
| **0.40** | not displeasing | 569.543 |
| **0.30** | not aesthetic | 445.188 |
| **0.20** | slightly displeasing | 341.424 |
| **0.10** | displeasing | 237.660 |
| **rest of them** | very displeasing | 328.712 |

### Quality Tags:
**Model used**: https://huggingface.co/hakurei/waifu-diffusion-v1-4/blob/main/models/aes-B32-v0.pth

| score greater than | tag | count |
|---|---|---|
| **0.980** | best quality | 1.270.447 |
| **0.900** | high quality | 498.244 |
| **0.750** | great quality | 351.006 |
| **0.500** | medium quality | 366.448 |
| **0.250** | normal quality | 368.380 |
| **0.125** | bad quality | 279.050 |
| **0.025** | low quality | 538.958 |
| **rest of them** | worst quality | 1.955.966 |

## Rating Tags:

| tag | count |
|---|---|
| **general** | 1.416.451 |
| **sensitive** | 3.447.664 |
| **nsfw** | 427.459 |
| **explicit nsfw** | 336.925 |

## Custom Tags:

| dataset name | custom tag |
|---|---|
| **image boards** | date, |
| **text** | The text says "text", |
| **characters** | character, series
| **pixiv** | art by Display_Name, |
| **visual novel cg** | Full_VN_Name (short_3_letter_name), visual novel cg, |
| **anime wallpaper** | date, anime wallpaper, |


## Limitations and Bias

### Bias

- This model is intended for anime illustrations.  
  Realistic capabilites are not tested at all.  

### Limitations

- Can fall back to realistic.  
  Add "realistic" tag to the negatives when this happens.  
- Far shot eyes and hands can be bad.  
- Still has a lot more room for more training.  


## License

SoteDiffusion models falls under [Fair AI Public License 1.0-SD](https://freedevproject.org/faipl-1.0-sd/) license, which is compatible with Stable Diffusion models’ license. Key points:

1. **Modification Sharing:** If you modify SoteDiffusion models, you must share both your changes and the original license.
2. **Source Code Accessibility:** If your modified version is network-accessible, provide a way (like a download link) for others to get the source code. This applies to derived models too.
3. **Distribution Terms:** Any distribution must be under this license or another with similar rules.
4. **Compliance:** Non-compliance must be fixed within 30 days to avoid license termination, emphasizing transparency and adherence to open-source values.

**Notes**: Anything not covered by Fair AI license is inherited from Stability AI Non-Commercial license which is named as LICENSE_INHERIT.