[Training] FP16 sigma_sqrt weighting overflows with default scheduler settings
Maintainer thường phản hồi trong vòng 1 ngày
@taking-lying-flat đang làm issue này rồi.
Từ ngày 26/11/2026.
- #15408 của @taking-lying-flat — đang mở
Đánh giá
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức phù hợp với người mới
- 30/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- backend, machine-learning
Hướng nghiên cứu
Read compute_loss_weighting_for_sd3() in src/diffusers/training_utils.py and the linked pull request #15408, which already contains the proposed fix and regression test. Check how the function computes sigma_sqrt weights for FP16 inputs; done means the weights, loss, and gradients stay finite for the default scheduler settings.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
FP16 sigma_sqrt loss weighting produces Inf for valid, nonzero training sigmas, including those generated by the scheduler's default configuration. The expected weights, loss and gradients are finite.
In compute_loss_weighting_for_sd3(), (sigmas**-2.0).float() computes the inverse square in FP16 and only then casts the result. The returned tensor is FP32, but the overflow has already occurred.
This is reproduced through the actual public function and scheduler on CPU, with local loss/gradient checks. Full pretrained-model GPU fine-tuning has not been run. The one-line fix and regression test are in #15408.
Reproduction: default first, followed by other shifts
Run this single block on the affected implementation. It first uses the scheduler with no overrides, then checks shifts 0.5, 2, 3 and 4. Each case counts non-finite weights across all 1,000 training sigmas and checks loss/gradient at its smallest nonzero sigma. No model download or dataset is needed.
import torch
from diffusers import FlowMatchEulerDiscreteScheduler
from diffusers.training_utils import compute_loss_weighting_for_sd3
for label, kwargs in zip(
("default", "shift=0.5", "shift=2", "shift=3", "shift=4"),
({}, {"shift": 0.5}, {"shift": 2.0}, {"shift": 3.0}, {"shift": 4.0}),
):
scheduler = FlowMatchEulerDiscreteScheduler(**kwargs)
sigmas = scheduler.sigmas.to(torch.float16)
current = compute_loss_weighting_for_sd3("sigma_sqrt", sigmas)
reference = sigmas.float().pow(-2.0)
sigma = sigmas[-1:]
print(f"\n{label}: min_sigma={sigma.item()}")
for name, weights in zip(("current", "reference"), (current, reference)):
prediction = torch.ones(1, dtype=torch.float32, requires_grad=True)
loss = (weights[-1:] * (-sigma * prediction).square()).mean()
loss.backward()
print(
name,
f"nonfinite={int((~torch.isfinite(weights)).sum())}/{len(sigmas)}",
f"min_sigma_weight={weights[-1].item()}",
f"loss={loss.item():.6f}",
f"gradient={prediction.grad.item():.6f}",
)
Output:
default: min_sigma=0.0010004043579101562
current nonfinite=3/1000 min_sigma_weight=inf loss=inf gradient=inf
reference nonfinite=0/1000 min_sigma_weight=999191.75 loss=1.000000 gradient=2.000000
shift=0.5: min_sigma=0.0005002021789550781
current nonfinite=7/1000 min_sigma_weight=inf loss=inf gradient=inf
reference nonfinite=0/1000 min_sigma_weight=3996767.0 loss=1.000000 gradient=2.000000
shift=2: min_sigma=0.0019989013671875
current nonfinite=1/1000 min_sigma_weight=inf loss=inf gradient=inf
reference nonfinite=0/1000 min_sigma_weight=250274.890625 loss=1.000000 gradient=2.000000
shift=3: min_sigma=0.002994537353515625
current nonfinite=1/1000 min_sigma_weight=inf loss=inf gradient=inf
reference nonfinite=0/1000 min_sigma_weight=111516.859375 loss=1.000000 gradient=2.000000
shift=4: min_sigma=0.003986358642578125
current nonfinite=0/1000 min_sigma_weight=62944.0 loss=1.000247 gradient=2.000493
reference nonfinite=0/1000 min_sigma_weight=62928.484375 loss=1.000000 gradient=2.000000
The analytical local objective is sigma**-2 * (-sigma * prediction)**2 = prediction**2. At prediction=1, the expected loss is 1 and gradient is 2. Shift=4 provides a finite FP16 control; default shift=1 and shifts 0.5, 2 and 3 reproduce the overflow.
Batch size increases the probability of encountering the bug
With default scheduler settings, 3 of the 1,000 training sigmas produce infinite FP16 inverse-square weights. The sigma_sqrt path samples a uniform timestep independently for each image.
For B independent timestep draws in a batch, the approximate probability of at least one affected sample is:
P = 1 - (1 - 3/1000)**B
| Batch size | Calculated probability | Observed in 20,000 batches |
|---|---|---|
| 1 | 0.30% | 0.410% |
| 4 | 1.19% | 1.325% |
| 16 | 4.69% | 4.850% |
| 32 | 9.17% | 9.255% |
| 64 | 17.49% | 17.350% |
| 128 | 31.93% | 32.155% |
| 256 | 53.66% | 53.880% |
The observed column uses the real compute_density_for_timestep_sampling("sigma_sqrt", ...) and weighting function on the affected commit, with seed=0; finite sampling explains the differences.
Batch averaging does not restore a finite loss. A separate two-sample local check, with one normal sigma and one affected sigma, produced mean loss=Inf and prediction gradients [1.0, Inf]. After the fix, the same inputs produced mean loss=1 and gradients approximately [1.0, 1.0].
Additional controls
BF16 and FP32 controls had no non-finite weights at any of these shifts. The repaired weights satisfy weight * sigma.float()**2 ≈ 1 across all tested sigmas. The logit_normal and cosmap controls remained finite and unchanged.
Training path and fix
The official SD3 DreamBooth script sets FP16 training inputs, obtains sigmas in the input dtype, and multiplies the weights into the loss before backward.
The affected supported combination is FP16 + --weighting_scheme=sigma_sqrt. The script's default weighting is logit_normal, which follows another branch. Scheduler defaults are sufficient to reproduce the overflow once sigma_sqrt is selected.
- weighting = (sigmas**-2.0).float()
+ weighting = sigmas.float().pow(-2.0)
System Info
- Diffusers:
0.44.0.dev0, affected commit16eb5cc4fd8262fd33b3c9119a12977c25cc4985 - Python: 3.12.13; PyTorch: 2.13.0+cu130
- Linux; all results above executed on CPU
- Ngôn ngữ chính
- Python
- Star
- 35k
- Fork
- 7.5k
- Merge trung bình
- 2 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 58
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của huggingface/diffusers
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
huggingface/diffusers#15436 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
InpaintProcessor crashes when padding_mask_crop is set and a dimension is omittedCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
huggingface/diffusers#15433 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
LTXEulerAncestralRFScheduler skips signal scaling with s_noise=0Có thể đã có người làm @hparik11 đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
huggingface/diffusers#15423 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Docs: 13 docstrings document parameters their functions don't haveCó thể đã có người làm @M-Colley đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
huggingface/diffusers#15363 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`ZImageImg2ImgPipeline` / `ZImageInpaintPipeline`: explicit height/width different from the input image raises RuntimeErrorCó thể đã có người làm @chessing234 đã nhận 11 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
huggingface/diffusers#15337 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của huggingface/diffusers
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 79/100
developmentseed/titiler-covjson#164 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
awslabs/visual-asset-management-system#2164 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
MakazhanAlpamys/Soup#3589 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 1 ngày