[training scripts] `--logit_mean` biases timesteps in the opposite direction to the SD3 paper (positive m oversamples low noise)

未关闭
#15,166 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@mlzerotoheroyoutube 已经在做这个了。

开始于 2026年11月3日。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, pytorch

调研方向

Start with the sampling helper compute_density_for_timestep_sampling in src/diffusers/training_utils.py, then the indexing line in examples/dreambooth/train_dreambooth_lora_sd3.py that maps u onto noise_scheduler_copy.timesteps. Run the issue's reproduction script first to confirm the reversed E[sigma]. Done means the sign of logit_mean matches the SD3 paper across the 28 affected scripts, with the reproduction assertion passing. The maintainers still need to decide whether to flip the sampler or the indexing, since that changes behaviour for existing users.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

The SD3/FLUX-style training scripts sample timesteps with compute_density_for_timestep_sampling(weighting_scheme="logit_normal", logit_mean=m, logit_std=s), which returns u = sigmoid(N(m, s)). They then index the scheduler's timestep table with indices = (u * num_train_timesteps).long(). FlowMatchEulerDiscreteScheduler's table is ordered noise-first (timesteps[0] = 1000, sigmas[0] = 1), so the realised noise level is sigma ≈ shift(1 - u), i.e. sigma follows a (shifted) logit-normal(-m, s) distribution.

The SD3 paper (Esser et al. 2024, Sec. 3.1, Eq. 19), which the helper's docstring and README_sd3.md refer to, samples the noise level t of z_t = (1 - t) x_0 + t eps (the scripts' own comment: zt = (1 - texp) * x + texp * z1) from logit-normal(m, s), and says m biases training "towards either data p0 (negative m) or noise p1 (positive m)". In the scripts the sign of --logit_mean is therefore reversed: --logit_mean 1.0 --logit_std 0.6 (the paper's lognorm(1.00, 0.60)) concentrates training on low noise levels (FLUX: E[sigma] 0.284 instead of 0.717; P(sigma > 0.5) 0.048 instead of 0.952).

The defaults are not affected: logit_mean=0 and the mode / uniform schemes are symmetric. Only users who set --logit_mean get the opposite of what the paper describes.

Affected: the 28 scripts that use this indexing line (list below). Not affected:

Reproduction

This uses the real compute_density_for_timestep_sampling and FlowMatchEulerDiscreteScheduler with the FLUX.1 and SD3 scheduler configs, and the scripts' indexing:

# The SD3/FLUX training scripts sample timesteps like this (e.g. examples/dreambooth/train_dreambooth_sd3.py):
#   u = compute_density_for_timestep_sampling(weighting_scheme="logit_normal", logit_mean=m, logit_std=s, ...)
#   indices = (u * noise_scheduler_copy.config.num_train_timesteps).long()
#   timesteps = noise_scheduler_copy.timesteps[indices]          -> sigmas via get_sigmas(timesteps)
#   noisy_model_input = (1.0 - sigmas) * model_input + sigmas * noise
# SD3 paper (Esser et al. 2024, Sec. 3.1, Eq. 19): t ~ logit-normal(m, s) is the noise level of
# z_t = (1 - t) x_0 + t eps, and m > 0 biases training towards noise. Here sigma plays the role of t.
import torch
import diffusers
from diffusers import FlowMatchEulerDiscreteScheduler
from diffusers.training_utils import compute_density_for_timestep_sampling

print(f"diffusers {diffusers.__version__}, torch {torch.__version__}")
m, s, n = 1.0, 0.6, 200_000
configs = {  # scheduler configs of the FLUX.1 and SD3 checkpoints (scheduler/scheduler_config.json)
    "FLUX.1": dict(shift=3.0, use_dynamic_shifting=True, base_shift=0.5, max_shift=1.15,
                   base_image_seq_len=256, max_image_seq_len=4096),
    "SD3": dict(shift=3.0),
}
for name, cfg in configs.items():
    sched = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, **cfg)
    torch.manual_seed(0)
    u = compute_density_for_timestep_sampling(weighting_scheme="logit_normal", batch_size=n,
                                              logit_mean=m, logit_std=s, mode_scale=1.29)
    indices = (u * sched.config.num_train_timesteps).long()  # as in the scripts
    timesteps = sched.timesteps[indices]
    # get_sigmas() looks up each timestep's position in the same table, i.e. `indices` (checked on a subset)
    assert all((sched.timesteps == t).nonzero().item() == i for t, i in zip(timesteps[:500], indices[:500]))
    sigma = sched.sigmas[indices]
    # Paper direction: the noise level itself is logit-normal(m, s) (then the same table/shift)
    sigma_paper = sched.sigmas[((1 - u) * 1000).long().clamp(max=999)]
    print(f"{name:6s} logit_mean={m:+.1f}, logit_std={s}: E[sigma] scripts {sigma.mean():.3f}, paper {sigma_paper.mean():.3f}"
          f" | P(sigma > 0.5) scripts {(sigma > 0.5).float().mean():.3f}, paper {(sigma_paper > 0.5).float().mean():.3f}")
    assert sigma.mean() < 0.55 < sigma_paper.mean()  # m > 0 should bias towards noise, the scripts bias towards data
print("BUG REPRODUCED: the sign of --logit_mean is reversed relative to the SD3 paper")
Logs
diffusers 0.42.1, torch 2.15.0+cpu
FLUX.1 logit_mean=+1.0, logit_std=0.6: E[sigma] scripts 0.284, paper 0.717 | P(sigma > 0.5) scripts 0.048, paper 0.952
SD3    logit_mean=+1.0, logit_std=0.6: E[sigma] scripts 0.524, paper 0.877 | P(sigma > 0.5) scripts 0.567, paper 1.000
BUG REPRODUCED: the sign of --logit_mean is reversed relative to the SD3 paper

Expected: with logit_mean=+1.0 the noise level should be biased towards noise, as in the paper (E[sigma] 0.717 for FLUX.1, 0.877 for SD3).

Where: training_utils.py#L366-L390 (v0.42.1, 2a14339) returns u, which correctly plays the role of the paper's t. The table is noise-first (scheduling_flow_match_euler_discrete.py#L127-L136). The scripts index it with u, e.g. train_dreambooth_lora_sd3.py#L1784-L1797:

                u = compute_density_for_timestep_sampling(
                    weighting_scheme=args.weighting_scheme,
                    batch_size=bsz,
                    logit_mean=args.logit_mean,
                    logit_std=args.logit_std,
                    mode_scale=args.mode_scale,
                )
                indices = (u * noise_scheduler_copy.config.num_train_timesteps).long()
                timesteps = noise_scheduler_copy.timesteps[indices].to(device=model_input.device)

                # Add noise according to flow matching.
                # zt = (1 - texp) * x + texp * z1
                sigmas = get_sigmas(timesteps, n_dim=model_input.ndim, dtype=model_input.dtype)
                noisy_model_input = (1.0 - sigmas) * model_input + sigmas * noise

The indexing line is identical, at the same line number, in all 28 scripts at v0.42.1 and on main (86ef420).

The 28 affected scripts (indexing line)

Proposed fix. The same one-line change in each of the 28 scripts. The shared helper cannot be flipped, because the ideogram4 script uses its output directly as sigma:

-                indices = (u * noise_scheduler_copy.config.num_train_timesteps).long()
+                indices = ((1 - u) * noise_scheduler_copy.config.num_train_timesteps).long().clamp(
+                    max=noise_scheduler_copy.config.num_train_timesteps - 1
+                )

The mode and uniform schemes, and logit_normal with logit_mean=0, are symmetric under u -> 1 - u, so their distributions do not change.

Verifying the fix. fix_check.py applies the fixed indexing with the same real helper and schedulers. It compares the realised sigmas with the paper's distribution sigma = shift(sigmoid(x)), x ~ N(m, s) (quantiles at 5/25/50/75/95%), and checks that the default settings keep the same distribution:

released = sched.sigmas[(u * N).long()]
fixed = sched.sigmas[((1 - u) * N).long().clamp(max=N - 1)]
t = torch.sigmoid(torch.randn(n) * s + m)            # paper: the noise level t ~ logit-normal(m, s)
paper = shift * t / (1 + (shift - 1) * t)            # shift = 1 for FLUX.1 (dynamic shifting), 3 for SD3
assert (fixed.quantile(qs) - paper.quantile(qs)).abs().max() < 5e-3
diffusers 0.42.1, torch 2.15.0+cpu
FLUX.1 logit_normal m=+1.0 s=0.6: E[sigma] released 0.284, fixed 0.717, paper 0.716 | max quantile gap fixed vs paper 0.0012
FLUX.1 logit_normal m=-0.5 s=1.0: E[sigma] released 0.603, fixed 0.398, paper 0.398 | max quantile gap fixed vs paper 0.0007
FLUX.1 logit_normal m=+0.0 s=1.0: E[sigma] released 0.501, fixed 0.500, paper 0.500 | max quantile gap fixed vs paper 0.0007 | max quantile gap fixed vs released 0.0020
FLUX.1 mode         m=+0.0 s=1.0: E[sigma] released 0.501, fixed 0.500 | max quantile gap fixed vs released 0.0020
FLUX.1 none         m=+0.0 s=1.0: E[sigma] released 0.500, fixed 0.501 | max quantile gap fixed vs released 0.0030
SD3    logit_normal m=+1.0 s=0.6: E[sigma] released 0.524, fixed 0.877, paper 0.877 | max quantile gap fixed vs paper 0.0009
SD3    logit_normal m=-0.5 s=1.0: E[sigma] released 0.794, fixed 0.621, paper 0.621 | max quantile gap fixed vs paper 0.0010
SD3    logit_normal m=+0.0 s=1.0: E[sigma] released 0.715, fixed 0.714, paper 0.714 | max quantile gap fixed vs paper 0.0011 | max quantile gap fixed vs released 0.0021
SD3    mode         m=+0.0 s=1.0: E[sigma] released 0.710, fixed 0.710 | max quantile gap fixed vs released 0.0020
SD3    none         m=+0.0 s=1.0: E[sigma] released 0.676, fixed 0.677 | max quantile gap fixed vs released 0.0027
FIX OK: the realised noise levels follow the SD3 paper's logit-normal(m, s); defaults unchanged
System Info
  • 🤗 Diffusers version: 0.42.1
  • Platform: Linux x86_64
  • Python version: 3.13.13
  • PyTorch version: 2.15.0+cpu (CPU only)
  • Transformers version: 5.22.1
  • Accelerate version: 1.15.0
Who can help?

No response

I would be happy to open a PR with this fix for the 28 scripts.

主要语言
Python
星标
34.9k
派生
7.5k
平均合并
3 天 16 小时
30 天内合并 PR
65

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

huggingface/diffusers 的其他 Issue

查看 huggingface/diffusers 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。