Skip to content

Commit 7b5aaa2

Browse files
committed
Merge remote-tracking branch 'upstream/sd3' into sd3-upstream
2 parents a86dacb + fa53f71 commit 7b5aaa2

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

README-ja.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Stable Diffusion等の画像生成モデルの学習、モデルによる画像
5050

5151
### 更新履歴
5252

53+
- **Version 0.10.3 (2026-04-02):**
54+
- Animaでfp16で学習する際の安定性をさらに改善しました。[PR #2302](https://github.com/kohya-ss/sd-scripts/pull/2302) 問題をご報告いただいた方々に深く感謝します。
55+
5356
- **Version 0.10.2 (2026-03-30):**
5457
- SD/SDXLのLECO学習に対応しました。[PR #2285](https://github.com/kohya-ss/sd-scripts/pull/2285) および [PR #2294](https://github.com/kohya-ss/sd-scripts/pull/2294) umisetokikaze氏に深く感謝します。
5558
- 詳細は[ドキュメント](./docs/train_leco.md)をご覧ください。

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ If you find this project helpful, please consider supporting its development via
4747

4848
### Change History
4949

50+
- **Version 0.10.3 (2026-04-02):**
51+
- Stability when training with fp16 on Anima has been further improved. See [PR #2302](https://github.com/kohya-ss/sd-scripts/pull/2302) for details. We deeply appreciate those who reported the issue.
52+
5053
- **Version 0.10.2 (2026-03-30):**
5154
- LECO training for SD/SDXL is now supported. Many thanks to umisetokikaze for [PR #2285](https://github.com/kohya-ss/sd-scripts/pull/2285) and [PR #2294](https://github.com/kohya-ss/sd-scripts/pull/2294).
5255
- Please refer to the [documentation](./docs/train_leco.md) for details.

library/anima_models.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ def forward(
736736
x_B_T_H_W_D: torch.Tensor,
737737
emb_B_T_D: torch.Tensor,
738738
adaln_lora_B_T_3D: Optional[torch.Tensor] = None,
739+
use_fp32: bool = False,
739740
):
740741
# Compute AdaLN modulation parameters (in float32 when fp16 to avoid overflow in Linear layers)
741-
use_fp32 = x_B_T_H_W_D.dtype == torch.float16
742742
with torch.autocast(device_type=x_B_T_H_W_D.device.type, dtype=torch.float32, enabled=use_fp32):
743743
if self.use_adaln_lora:
744744
assert adaln_lora_B_T_3D is not None
@@ -861,11 +861,11 @@ def _forward(
861861
emb_B_T_D: torch.Tensor,
862862
crossattn_emb: torch.Tensor,
863863
attn_params: attention.AttentionParams,
864+
use_fp32: bool = False,
864865
rope_emb_L_1_1_D: Optional[torch.Tensor] = None,
865866
adaln_lora_B_T_3D: Optional[torch.Tensor] = None,
866867
extra_per_block_pos_emb: Optional[torch.Tensor] = None,
867868
) -> torch.Tensor:
868-
use_fp32 = x_B_T_H_W_D.dtype == torch.float16
869869
if use_fp32:
870870
# Cast to float32 for better numerical stability in residual connections. Each module will cast back to float16 by enclosing autocast context.
871871
x_B_T_H_W_D = x_B_T_H_W_D.float()
@@ -957,6 +957,7 @@ def forward(
957957
emb_B_T_D: torch.Tensor,
958958
crossattn_emb: torch.Tensor,
959959
attn_params: attention.AttentionParams,
960+
use_fp32: bool = False,
960961
rope_emb_L_1_1_D: Optional[torch.Tensor] = None,
961962
adaln_lora_B_T_3D: Optional[torch.Tensor] = None,
962963
extra_per_block_pos_emb: Optional[torch.Tensor] = None,
@@ -970,6 +971,7 @@ def forward(
970971
emb_B_T_D,
971972
crossattn_emb,
972973
attn_params,
974+
use_fp32,
973975
rope_emb_L_1_1_D,
974976
adaln_lora_B_T_3D,
975977
extra_per_block_pos_emb,
@@ -991,6 +993,7 @@ def custom_forward(*inputs):
991993
emb_B_T_D,
992994
crossattn_emb,
993995
attn_params,
996+
use_fp32,
994997
rope_emb_L_1_1_D,
995998
adaln_lora_B_T_3D,
996999
extra_per_block_pos_emb,
@@ -1004,6 +1007,7 @@ def custom_forward(*inputs):
10041007
emb_B_T_D,
10051008
crossattn_emb,
10061009
attn_params,
1010+
use_fp32,
10071011
rope_emb_L_1_1_D,
10081012
adaln_lora_B_T_3D,
10091013
extra_per_block_pos_emb,
@@ -1015,6 +1019,7 @@ def custom_forward(*inputs):
10151019
emb_B_T_D,
10161020
crossattn_emb,
10171021
attn_params,
1022+
use_fp32,
10181023
rope_emb_L_1_1_D,
10191024
adaln_lora_B_T_3D,
10201025
extra_per_block_pos_emb,
@@ -1335,16 +1340,19 @@ def forward_mini_train_dit(
13351340

13361341
attn_params = attention.AttentionParams.create_attention_params(self.attn_mode, self.split_attn)
13371342

1343+
# Determine whether to use float32 for block computations based on input dtype (use float32 for better stability when input is float16)
1344+
use_fp32 = x_B_T_H_W_D.dtype == torch.float16
1345+
13381346
for block_idx, block in enumerate(self.blocks):
13391347
if self.blocks_to_swap:
13401348
self.offloader.wait_for_block(block_idx)
13411349

1342-
x_B_T_H_W_D = block(x_B_T_H_W_D, t_embedding_B_T_D, crossattn_emb, attn_params, **block_kwargs)
1350+
x_B_T_H_W_D = block(x_B_T_H_W_D, t_embedding_B_T_D, crossattn_emb, attn_params, use_fp32, **block_kwargs)
13431351

13441352
if self.blocks_to_swap:
13451353
self.offloader.submit_move_blocks(self.blocks, block_idx)
13461354

1347-
x_B_T_H_W_O = self.final_layer(x_B_T_H_W_D, t_embedding_B_T_D, adaln_lora_B_T_3D=adaln_lora_B_T_3D)
1355+
x_B_T_H_W_O = self.final_layer(x_B_T_H_W_D, t_embedding_B_T_D, adaln_lora_B_T_3D=adaln_lora_B_T_3D, use_fp32=use_fp32)
13481356
x_B_C_Tt_Hp_Wp = self.unpatchify(x_B_T_H_W_O)
13491357
return x_B_C_Tt_Hp_Wp
13501358

networks/resize_lora.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ def rank_resize(S, rank, dynamic_method, dynamic_param, scale=1):
212212
def resize_lora_model(lora_sd, new_rank, new_conv_rank, save_dtype, device, dynamic_method, dynamic_param, verbose, svd_lowrank_niter=2):
213213
max_old_rank = None
214214
new_alpha = None
215-
verbose_str = "\n"
216215
fro_list = []
217216

218217
if dynamic_method:
@@ -285,15 +284,13 @@ def resize_lora_model(lora_sd, new_rank, new_conv_rank, save_dtype, device, dyna
285284
if not np.isnan(fro_retained):
286285
fro_list.append(float(fro_retained))
287286

288-
verbose_str += f"{block_down_name:75} | "
287+
verbose_str = f"{block_down_name:75} | "
289288
verbose_str += (
290289
f"sum(S) retained: {sum_retained:.1%}, fro retained: {fro_retained:.1%}, max(S) ratio: {max_ratio:0.1f}"
291290
)
292-
293-
if verbose and dynamic_method:
294-
verbose_str += f", dynamic | dim: {param_dict['new_rank']}, alpha: {param_dict['new_alpha']}\n"
295-
else:
296-
verbose_str += "\n"
291+
if dynamic_method:
292+
verbose_str += f", dynamic | dim: {param_dict['new_rank']}, alpha: {param_dict['new_alpha']}"
293+
tqdm.write(verbose_str)
297294

298295
new_alpha = param_dict["new_alpha"]
299296
o_lora_sd[block_down_name + lora_down_name + weight_name] = param_dict["lora_down"].to(save_dtype).contiguous()
@@ -308,7 +305,6 @@ def resize_lora_model(lora_sd, new_rank, new_conv_rank, save_dtype, device, dyna
308305
del param_dict
309306

310307
if verbose:
311-
print(verbose_str)
312308
print(f"Average Frobenius norm retention: {np.mean(fro_list):.2%} | std: {np.std(fro_list):0.3f}")
313309
logger.info("resizing complete")
314310
return o_lora_sd, max_old_rank, new_alpha

0 commit comments

Comments
 (0)