权重改为fp32, 其他部分保持bf16

目前参数--encoder_mode有三种选择:
1. fp32: 全部使用fp32, 适合显存充足的情况
2. autocast: 使用torch.cuda.amp.autocast自动混合精度, 稍微快一些, psnr下降较多
3. bf16_full: 全部使用bf16, 精度较高
This commit is contained in:
2026-01-18 20:24:37 +08:00
parent 44379f3e31
commit e1b029201e
4 changed files with 75 additions and 16 deletions

View File

@@ -81,4 +81,29 @@ BF16 projector比FP32 projector更准的可能原因
这也解释了为什么你看到 BF16 projector 反而更准。
embedder
改成 autocast only权重 FP32预处理 FP32仅主干 BF16
改成 autocast only权重 FP32预处理 FP32仅主干 BF16
- 效果差的那次encoder autocast set to bf16 (weights=fp32)
也就是“权重 FP32 + autocast BF16我现在的 autocast only 方案)”
- 效果好的那次cond_stage_model / embedder param dtype: torch.bfloat16
也就是“权重 BF16全 BF16
为什么 FP32 反而掉分大?主要是“混精度路径不一致”:
1. autocast only 不是等价于 full BF16
autocast 会把部分算子留在 FP32比如 LayerNorm / Softmax另一部分用 BF16
这会让 encoder 输出分布和“全 BF16”不一样尤其当扩散主干也是 BF16 时,条件向量分布一偏就会影响 PSNR。
2. 先 FP32 计算再转 BF16
encoder 的输出最后还是要拼到 BF16 主干里,等于“先高精度 → 再截断”。
训练时如果你用的是 BF16模型更适应“低精度直接计算”的分布。
3. 你的现象和 projector 一样
你已经观察到 projector BF16 更准,这说明模型很可能“更偏好一致的 BF16 路径”。
结论:
对你这个模型full BF16权重 BF16更匹配训练分布反而 PSNR 更高。
如果你要我改回“bf16=全 BF16权重 BF16并且保留现在的 autocast only 作为可选模式,我可以这样做:
1. 新增 --encoder_mode {fp32, autocast, bf16_full}
2. bf16_full = 权重 BF16 + 前向 BF16
3. autocast = 权重 FP32 + 仅主干 autocast现在的实现