Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

FastDiffusionModel: record an explicit float32 request so T4 training stays float32 (#15062)

Daniel Han12e8658dd4c3
diff --git a/tests/test_float32_no_fp16_autocast.py b/tests/test_float32_no_fp16_autocast.py
index c2855cf0274..99dbb0ca9b5 100644
--- a/tests/test_float32_no_fp16_autocast.py
+++ b/tests/test_float32_no_fp16_autocast.py
@@ -251,6 +251,23 @@ def test_the_text_diffusion_path_records_it_too():
assert "_mark_requested_float32(model, user_float32)" in statement, statement
+def test_the_direct_diffusion_loader_records_it_too():
+ """FastDiffusionModel.from_pretrained called directly never passes through _dispatch_diffusion, so on a
+ GPU without bf16 a float32 request trained under fp16 mixed precision with a loss scaler."""
+ src = (REPO_ROOT / "unsloth" / "models" / "diffusion.py").read_text(encoding = "utf-8")
+ tree = ast.parse(src)
+ cls = next(
+ n for n in ast.walk(tree) if isinstance(n, ast.ClassDef) and n.name == "FastDiffusionModel"
+ )
+ fn = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "from_pretrained")
+ first = ast.unparse(fn.body[0])
+ assert first == "user_float32 = _requested_float32(dtype)", first
+ returns = [ast.unparse(n) for n in ast.walk(fn) if isinstance(n, ast.Return)]
+ assert returns, "expected the diffusion loader to return a model"
+ for statement in returns:
+ assert "_mark_requested_float32(model, user_float32)" in statement, statement
+
+
def test_the_request_is_read_from_the_model_not_the_environment():
"""A process-global would describe whichever model loaded last, so a
program that loads two before building a trainer would train the first
diff --git a/unsloth/models/diffusion.py b/unsloth/models/diffusion.py
index 4dfcf53f897..d19a92c3879 100644
--- a/unsloth/models/diffusion.py
+++ b/unsloth/models/diffusion.py
@@ -23,7 +23,12 @@ import torch
from transformers import AutoConfig, AutoProcessor, AutoTokenizer, set_seed
from unsloth_zoo.hf_utils import add_dtype_kwargs
-from ._utils import is_bfloat16_supported, maybe_prefetch_hf_snapshot
+from ._utils import (
+ _mark_requested_float32,
+ _requested_float32,
+ is_bfloat16_supported,
+ maybe_prefetch_hf_snapshot,
+)
from ..device_type import DEVICE_TYPE, xpu_keeps_explicit_bf16
from .diffusion_profiles import (
diffusion_model_types,
@@ -232,6 +237,8 @@ class FastDiffusionModel:
return_tokenizer = True,
**kwargs,
):
+ # Read before dtype is normalized: the trainer keeps float32 on a GPU without bf16 only if it was asked for.
+ user_float32 = _requested_float32(dtype)
SUPPORTS_BFLOAT16 = is_bfloat16_supported()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
@@ -453,7 +460,7 @@ class FastDiffusionModel:
if not return_tokenizer:
if profile is not None:
profile.prepare_model(unwrap_diffusion_model(model), None)
- return model, None
+ return _mark_requested_float32(model, user_float32), None
# Prefer the processor (chat template plus tokenizer), falling back to a bare tokenizer, returned as
# "tokenizer" to match the Unsloth (model, tokenizer) contract.
@@ -500,7 +507,7 @@ class FastDiffusionModel:
if profile is not None:
# On the base module: the objectives read these attributes after unwrapping PEFT / DDP.
profile.prepare_model(unwrap_diffusion_model(model), tokenizer)
- return model, tokenizer
+ return _mark_requested_float32(model, user_float32), tokenizer
@staticmethod
def get_peft_model(