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.

XPU bf16 on the current device, keep explicit bf16 on XPU, MLX full finetuning keeps its default dtype (#14493)

Daniel Hana7a37d3db2c8
diff --git a/tests/test_cuda_device_order_warning.py b/tests/test_cuda_device_order_warning.py
index e889e8ce68a..d19e5e16747 100644
--- a/tests/test_cuda_device_order_warning.py
+++ b/tests/test_cuda_device_order_warning.py
@@ -165,8 +165,10 @@ def test_hint_command_fits_the_shell(monkeypatch, capsys, os_name, command):
_set_order(monkeypatch, None)
import unsloth._gpu_init as gpu_init
- monkeypatch.setattr(gpu_init.os, "name", os_name)
- _run_cuda_arm(_fake_torch([A100, L4]), monkeypatch)
+ # Restore os.name before pytest reports the call: Python 3.13 pathlib reads it there.
+ with monkeypatch.context() as patch:
+ patch.setattr(gpu_init.os, "name", os_name)
+ _run_cuda_arm(_fake_torch([A100, L4]), monkeypatch)
assert f"`{command}`" in capsys.readouterr().out
diff --git a/tests/test_decision_model.py b/tests/test_decision_model.py
index c9ce3ada7db..179e47ea7b4 100644
--- a/tests/test_decision_model.py
+++ b/tests/test_decision_model.py
@@ -1465,6 +1465,26 @@ def test_clef_autocasts_as_it_trains_and_never_on_the_float32_path(monkeypatch):
assert decision._amp_dtype(xpu) == torch.bfloat16
+def test_clef_keeps_an_explicit_bf16_model_in_bf16_on_xpu(monkeypatch):
+ # The loaders keep an explicit bf16 request on an XPU without native bf16; autocast and the
+ # 4-bit compute dtype must follow it, never fp16 autocast over bf16 weights.
+ pytest.importorskip("transformers")
+ monkeypatch.setattr(decision, "is_bfloat16_supported", lambda: False)
+ xpu, cuda = torch.device("xpu"), torch.device("cuda")
+ model = torch.nn.Linear(1, 1).to(torch.bfloat16)
+ assert decision._clef_amp_dtype(model, xpu) == torch.bfloat16
+ assert decision._clef_amp_dtype(model, cuda) == torch.float16
+ assert decision._clef_amp_dtype(torch.nn.Linear(1, 1).half(), xpu) == torch.float16
+ model._unsloth_forced_float32 = True
+ assert decision._clef_amp_dtype(model, xpu) is None
+ monkeypatch.setattr(decision, "_device", lambda: xpu)
+ monkeypatch.setattr(decision, "xpu_keeps_explicit_bf16", lambda: True)
+ assert decision._clef_bnb_config(torch.bfloat16).bnb_4bit_compute_dtype == torch.bfloat16
+ assert decision._clef_bnb_config(None).bnb_4bit_compute_dtype == torch.float16
+ monkeypatch.setattr(decision, "_device", lambda: cuda)
+ assert decision._clef_bnb_config(torch.bfloat16).bnb_4bit_compute_dtype == torch.float16
+
+
def test_a_clef_prompt_that_fits_exactly_is_not_truncated(monkeypatch):
# Serving (Studio's Decision API) and predict share this rule.
from unsloth.models import clef
diff --git a/tests/test_float32_no_fp16_autocast.py b/tests/test_float32_no_fp16_autocast.py
index a4b5f3b5f5a..c2855cf0274 100644
--- a/tests/test_float32_no_fp16_autocast.py
+++ b/tests/test_float32_no_fp16_autocast.py
@@ -75,6 +75,7 @@ def _run(
full_finetuning = "0",
mixed_precision = "float32",
user_float32 = None,
+ device_type = "cuda",
):
"""Execute the block and report what it decided."""
config = types.SimpleNamespace(dtype = model_dtype, torch_dtype = model_dtype)
@@ -108,6 +109,7 @@ def _run(
# Stub the parent too: a raising unsloth_zoo __init__ would route through the torch.cuda fallback.
mod = types.ModuleType("unsloth_zoo.device_type")
mod.device_is_bf16_supported = lambda: bf16_supported
+ mod.DEVICE_TYPE = device_type
utils = types.ModuleType("unsloth_zoo.utils")
utils._get_dtype = _get_dtype
parent = types.ModuleType("unsloth_zoo")
@@ -269,3 +271,15 @@ def test_block_still_compiles():
if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, "-q"]))
+
+
+def test_explicit_bf16_model_on_emulated_xpu_autocasts_bf16():
+ """XPU keeps an explicit bf16 load (emulated); fp16 autocast over bf16 weights would overflow."""
+ args, env = _run(torch.bfloat16, bf16_supported = False, device_type = "xpu")
+ assert (args.bf16, args.fp16) == (True, False)
+ assert env["ACCELERATE_MIXED_PRECISION"] == "bf16"
+ # A default XPU load there is float16 and keeps float16 autocast; CUDA is unchanged.
+ args, _ = _run(torch.float16, bf16_supported = False, device_type = "xpu")
+ assert (args.bf16, args.fp16) == (False, True)
+ args, _ = _run(torch.bfloat16, bf16_supported = False, device_type = "cuda")
+ assert (args.bf16, args.fp16) == (False, True)
diff --git a/tests/test_mlx_bf16_gpu_family.py b/tests/test_mlx_bf16_gpu_family.py
index 067df94680e..a9f5eea7a4f 100644
--- a/tests/test_mlx_bf16_gpu_family.py
+++ b/tests/test_mlx_bf16_gpu_family.py
@@ -4,6 +4,7 @@
"""MLX is_bfloat16_supported(): Metal GPU family first (g13/g14 lack bf16), chip name only as fallback."""
import ast
+import inspect
import sys
import types
from pathlib import Path
@@ -112,9 +113,32 @@ def _mlx_from_pretrained(bf16):
if isinstance(n, ast.FunctionDef) and n.name == "_mlx_from_pretrained"
)
calls = []
+
+ # zoo's FastMLXModel.from_pretrained positional order.
+ def signature(
+ model_name = None,
+ max_seq_length = 2048,
+ dtype = None,
+ load_in_4bit = True,
+ load_in_8bit = False,
+ load_in_16bit = False,
+ load_in_fp8 = False,
+ load_in_mxfp4 = False,
+ load_in_nvfp4 = False,
+ full_finetuning = False,
+ **kwargs,
+ ):
+ pass
+
+ def from_pretrained(*a, **k):
+ calls.append((a, k))
+
+ from_pretrained.__signature__ = inspect.signature(signature)
+
namespace = {
"is_bfloat16_supported": lambda: bf16,
- "FastMLXModel": types.SimpleNamespace(from_pretrained = lambda *a, **k: calls.append((a, k))),
+ "_inspect": inspect,
+ "FastMLXModel": types.SimpleNamespace(from_pretrained = from_pretrained),
}
exec(compile(ast.Module(body = [node], type_ignores = []), str(INIT), "exec"), namespace)
return namespace["_mlx_from_pretrained"], calls
@@ -139,6 +163,28 @@ def _mlx_from_pretrained(bf16):
pytest.param(
("m", 2048, "float32"), {}, False, (("m", 2048, "float32"), {}), id = "explicit-pos-wins"
),
+ # zoo trains a non-bf16 full finetune in float32: leave its default alone there.
+ pytest.param(
+ ("m",),
+ {"full_finetuning": True},
+ False,
+ (("m",), {"full_finetuning": True}),
+ id = "full-ft-kw-untouched",
+ ),
+ pytest.param(
+ ("m", 2048, None, False, False, False, False, False, False, True),
+ {},
+ False,
+ (("m", 2048, None, False, False, False, False, False, False, True), {}),
+ id = "full-ft-pos-untouched",
+ ),
+ pytest.param(
+ ("m",),
+ {"full_finetuning": False},
+ False,
+ (("m",), {"full_finetuning": False, "dtype": "float16"}),
+ id = "lora-still-float16",
+ ),
],
)
def test_loader_default_dtype_follows_gpu_family(args, kwargs, bf16, expected):
diff --git a/tests/test_xpu_explicit_bf16_kept.py b/tests/test_xpu_explicit_bf16_kept.py
new file mode 100644
index 00000000000..c238fa3b4ad
--- /dev/null
+++ b/tests/test_xpu_explicit_bf16_kept.py
@@ -0,0 +1,101 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""An explicit bf16 request on XPU stays bf16 (emulated if need be), as before the native-bf16 probe."""
+
+import ast
+import os
+import types
+from pathlib import Path
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+MODELS = Path(__file__).resolve().parents[1] / "unsloth" / "models"
+FILES = ["loader.py", "llama.py", "vision.py", "diffusion.py", "sentence_transformer.py"]
+
+
+def _override_branch(path):
+ tree = ast.parse(path.read_text(encoding = "utf-8"))
+ for node in ast.walk(tree):
+ test = ast.unparse(node.test) if isinstance(node, ast.If) else ""
+ if test.startswith("dtype == torch.bfloat16 and") and "SUPPORTS_BFLOAT16" in test:
+ return compile(ast.Module(body = [node], type_ignores = []), str(path), "exec")
+ raise AssertionError(f"bf16 override not found in {path.name}")
+
+
+def _resolve(
+ path,
+ device_type,
+ dtype,
+ supports,
+ zoo_keeps = True,
+):
+ namespace = {
+ "xpu_keeps_explicit_bf16": lambda: device_type == "xpu" and zoo_keeps,
+ "torch": torch,
+ "os": types.SimpleNamespace(environ = {}),
+ "dtype": dtype,
+ "SUPPORTS_BFLOAT16": supports,
+ "DEVICE_TYPE": device_type,
+ "logger": types.SimpleNamespace(warning_once = lambda *a, **k: None),
+ "print": lambda *a, **k: None,
+ }
+ exec(_override_branch(path), namespace)
+ return namespace["dtype"]
+
+
[email protected]("name", FILES)
+def test_explicit_bf16_kept_on_xpu_only(name):
+ path = MODELS / name
+ assert _resolve(path, "xpu", torch.bfloat16, False) is torch.bfloat16
+ assert _resolve(path, "cuda", torch.bfloat16, False) is torch.float16
+ assert _resolve(path, "hip", torch.bfloat16, False) is torch.float16
+ assert _resolve(path, "xpu", torch.bfloat16, True) is torch.bfloat16
+ # Beside a zoo whose vLLM path still downgrades, the load downgrades too, so both agree.
+ assert _resolve(path, "xpu", torch.bfloat16, False, zoo_keeps = False) is torch.float16
+
+
+def test_explicit_bf16_on_xpu_follows_the_paired_zoo(monkeypatch):
+ from unsloth import device_type as dt
+
+ zoo = pytest.importorskip("unsloth_zoo.device_type")
+ monkeypatch.setattr(dt, "DEVICE_TYPE", "xpu")
+ monkeypatch.delattr(zoo, "XPU_KEEPS_EXPLICIT_BF16", raising = False)
+ assert dt.xpu_keeps_explicit_bf16() is False
+ monkeypatch.setattr(zoo, "XPU_KEEPS_EXPLICIT_BF16", True, raising = False)
+ assert dt.xpu_keeps_explicit_bf16() is True
+ monkeypatch.setattr(dt, "DEVICE_TYPE", "cuda")
+ assert dt.xpu_keeps_explicit_bf16() is False
+
+
+def _forced_float32_dtype_gate():
+ tree = ast.parse((MODELS / "loader.py").read_text(encoding = "utf-8"))
+ for node in ast.walk(tree):
+ if (
+ isinstance(node, ast.For)
+ and isinstance(node.iter, ast.Name)
+ and node.iter.id == "FORCE_FLOAT32"
+ ):
+ test = node.body[0].test
+ return compile(ast.Expression(test.values[1]), "gate", "eval")
+ raise AssertionError("FORCE_FLOAT32 gate not found")
+
+
+ "dtype, supports, forced",
+ [
+ (torch.float16, True, True),
+ (torch.float16, False, True),
+ (torch.bfloat16, True, False),
+ (torch.bfloat16, False, False), # explicit bf16 kept on XPU: no fp16 + float32 path
+ (torch.float32, False, True),
+ (torch.float32, True, False),
+ ],
+)
+def test_forced_float32_only_for_a_float16_load(dtype, supports, forced):
+ gate = _forced_float32_dtype_gate()
+ assert (
+ bool(eval(gate, {"torch": torch, "dtype": dtype, "SUPPORTS_BFLOAT16": supports})) is forced
+ )
diff --git a/tests/test_xpu_native_bf16.py b/tests/test_xpu_native_bf16.py
index 4b4f45ae7a9..51d6601cc99 100644
--- a/tests/test_xpu_native_bf16.py
+++ b/tests/test_xpu_native_bf16.py
@@ -1,7 +1,8 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""XPU bf16 must be native on every visible XPU (torch's default probe counts emulation)."""
+"""XPU bf16 must be native on every visible XPU, each judged by its own aspects (torch's default
+probe counts emulation)."""
import re
import types
@@ -27,26 +28,33 @@ NATIVE = {"has_bfloat16_conversions": True}
EMULATED = {"has_bfloat16_conversions": False, "has_subgroup_matrix_multiply_accumulate": True}
NO_OCLOC = {"has_bfloat16_conversions": False}
+# (devices, current device, native bf16)
CASES = [
- pytest.param([NATIVE], True, id = "1dev-native"),
- pytest.param([EMULATED], False, id = "1dev-emulated"),
- pytest.param([NO_OCLOC], True, id = "1dev-no-libocloc"),
- pytest.param(["old"], True, id = "1dev-old-torch"),
- pytest.param([None], True, id = "1dev-unreadable"),
- pytest.param([NATIVE, NATIVE], True, id = "2dev-native"),
- pytest.param([NATIVE, EMULATED], False, id = "2dev-mixed"),
- pytest.param([EMULATED, NATIVE], False, id = "2dev-mixed-rev"),
- pytest.param([EMULATED, EMULATED], False, id = "2dev-emulated"),
- pytest.param([NO_OCLOC, NO_OCLOC], True, id = "2dev-no-libocloc"),
- pytest.param(["old", "old"], True, id = "2dev-old-torch"),
- pytest.param(["old", EMULATED], False, id = "2dev-old-attr+emulated"),
- pytest.param([None, NO_OCLOC], True, id = "2dev-unreadable+no-libocloc"),
+ pytest.param([NATIVE], 0, True, id = "1dev-native"),
+ pytest.param([EMULATED], 0, False, id = "1dev-emulated"),
+ pytest.param([NO_OCLOC], 0, True, id = "1dev-no-libocloc"),
+ pytest.param(["old"], 0, True, id = "1dev-old-torch"),
+ pytest.param([None], 0, True, id = "1dev-unreadable"),
+ pytest.param([NATIVE, NATIVE], 0, True, id = "2dev-native"),
+ pytest.param([NATIVE, EMULATED], 0, False, id = "2dev-mixed"),
+ pytest.param([EMULATED, NATIVE], 1, False, id = "2dev-mixed-rev"),
+ pytest.param([NATIVE, EMULATED], 1, False, id = "2dev-mixed-current-emulated"),
+ pytest.param([EMULATED, NATIVE], 0, False, id = "2dev-mixed-rev-current-emulated"),
+ pytest.param([EMULATED, EMULATED], 0, False, id = "2dev-emulated"),
+ pytest.param([EMULATED, EMULATED], 1, False, id = "2dev-emulated-dev1"),
+ pytest.param([NO_OCLOC, NO_OCLOC], 0, True, id = "2dev-no-libocloc"),
+ pytest.param([EMULATED, NO_OCLOC], 1, False, id = "2dev-emulated+no-libocloc"),
+ pytest.param([NATIVE, NO_OCLOC], 0, True, id = "2dev-native+no-libocloc"),
+ pytest.param([NO_OCLOC, NATIVE], 1, True, id = "2dev-no-libocloc+native"),
+ pytest.param(["old", "old"], 0, True, id = "2dev-old-torch"),
+ pytest.param(["old", EMULATED], 0, False, id = "2dev-old-attr+emulated"),
+ pytest.param([None, NO_OCLOC], 0, True, id = "2dev-unreadable+no-libocloc"),
]
-def _fake_torch(devices):
+def _fake_torch(devices, current = 0):
def get_device_properties(device = None):
- spec = devices[device or 0]
+ spec = devices[current if device is None else device]
if spec is None:
raise RuntimeError("unreadable device")
props = types.SimpleNamespace(name = f"Intel XPU {device}")
@@ -63,7 +71,7 @@ def _fake_torch(devices):
return types.SimpleNamespace(
xpu = types.SimpleNamespace(
device_count = lambda: len(devices),
- current_device = lambda: 0,
+ current_device = lambda: current,
is_available = lambda: True,
get_device_properties = get_device_properties,
is_bf16_supported = is_bf16_supported,
@@ -95,20 +103,20 @@ def _run(
return namespace
[email protected]("devices,bf16", CASES)
-def test_gpu_init_xpu_bf16_is_native_on_every_device(monkeypatch, devices, bf16):
[email protected]("devices,current,bf16", CASES)
+def test_gpu_init_xpu_bf16_is_native_on_every_device(monkeypatch, devices, current, bf16):
import unsloth._gpu_init as gpu_init
- fake = _fake_torch(devices)
+ fake = _fake_torch(devices, current)
original = fake.xpu.is_bf16_supported
assert _run(GPU_INIT, gpu_init, fake, monkeypatch)["SUPPORTS_BFLOAT16"] is bf16
assert fake.xpu.is_bf16_supported is original
[email protected]("devices,bf16", CASES)
-def test_model_utils_xpu_bf16_is_native_on_every_device(monkeypatch, devices, bf16):
[email protected]("devices,current,bf16", CASES)
+def test_model_utils_xpu_bf16_is_native_on_every_device(monkeypatch, devices, current, bf16):
import unsloth.models._utils as model_utils
- namespace = _run(MODEL_UTILS, model_utils, _fake_torch(devices), monkeypatch)
+ namespace = _run(MODEL_UTILS, model_utils, _fake_torch(devices, current), monkeypatch)
assert namespace["SUPPORTS_BFLOAT16"] is bf16
@@ -121,13 +129,13 @@ def test_xpu_unreadable_count_keeps_true(monkeypatch):
assert dt.xpu_supports_native_bf16() is True
[email protected]("devices,_bf16", CASES)
-def test_matches_zoo_answer(monkeypatch, devices, _bf16):
[email protected]("devices,current,_bf16", CASES)
+def test_matches_zoo_answer(monkeypatch, devices, current, _bf16):
"""Must match zoo: a mismatch loads bf16 then trains in float16."""
zoo_dt = pytest.importorskip("unsloth_zoo.device_type")
if not hasattr(zoo_dt, "_xpu_bf16_supported"):
pytest.skip("installed unsloth_zoo predates _xpu_bf16_supported")
- fake = _fake_torch(devices)
+ fake = _fake_torch(devices, current)
monkeypatch.setattr(dt, "torch", fake)
monkeypatch.setattr(zoo_dt, "torch", fake)
assert dt.xpu_supports_native_bf16() is zoo_dt._xpu_bf16_supported()
diff --git a/unsloth/__init__.py b/unsloth/__init__.py
index 352aa3fd6bf..016ea4bd124 100644
--- a/unsloth/__init__.py
+++ b/unsloth/__init__.py
@@ -376,7 +376,13 @@ if _IS_MLX:
def _mlx_from_pretrained(*args, **kwargs):
# zoo's loader picks the default dtype by device name only; fill it from the GPU family.
- if not is_bfloat16_supported():
+ # Not for full finetuning: zoo trains any non-bf16 target in float32, doubling weight memory.
+ try:
+ bound = _inspect.signature(FastMLXModel.from_pretrained).bind_partial(*args, **kwargs)
+ full_finetuning = bound.arguments.get("full_finetuning", False)
+ except Exception:
+ full_finetuning = kwargs.get("full_finetuning", False)
+ if not full_finetuning and not is_bfloat16_supported():
if len(args) > 2:
if args[2] is None:
args = (*args[:2], "float16", *args[3:])
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
index c71d1e957bc..78603e14ed5 100644
--- a/unsloth/device_type.py
+++ b/unsloth/device_type.py
@@ -237,8 +237,10 @@ def cuda_order_mismatch_names():
def xpu_supports_native_bf16():
- """Without libocloc (absent in torch-xpu wheels) every aspect reads False, so trust the probe
- only once some aspect reads True. Must match unsloth_zoo.device_type._xpu_bf16_supported."""
+ """Process-wide like CUDA: any visible XPU proven to lack native bf16 turns it off, since the
+ load target is not known here. Each device is judged by its own aspects: without libocloc
+ (absent in torch-xpu wheels) every aspect reads False, so trust the probe only once some
+ aspect reads True on that device. Must match unsloth_zoo.device_type._xpu_bf16_supported."""
aspects = (
"has_bfloat16_conversions",
"has_subgroup_matrix_multiply_accumulate",
@@ -249,15 +251,27 @@ def xpu_supports_native_bf16():
count = torch.xpu.device_count()
except Exception:
return True
- lacks_bf16 = probe_works = False
for index in range(count):
try:
props = torch.xpu.get_device_properties(index)
except Exception:
continue
- lacks_bf16 |= getattr(props, "has_bfloat16_conversions", True) is False
- probe_works |= any(getattr(props, name, False) is True for name in aspects)
- return not (lacks_bf16 and probe_works)
+ lacks_bf16 = getattr(props, "has_bfloat16_conversions", True) is False
+ if lacks_bf16 and any(getattr(props, name, False) is True for name in aspects):
+ return False
+ return True
+
+
+def xpu_keeps_explicit_bf16():
+ """An explicit bf16 request on XPU stays bf16 (emulated if need be) only beside an unsloth_zoo
+ that does the same for vLLM, so the engine and the HF weights it shares agree on one dtype."""
+ if DEVICE_TYPE != "xpu":
+ return False
+ try:
+ import unsloth_zoo.device_type as zoo_device_type
+ except Exception:
+ return False
+ return bool(getattr(zoo_device_type, "XPU_KEEPS_EXPLICIT_BF16", False))
def zoo_shares_xpu_bf16():
diff --git a/unsloth/models/decision.py b/unsloth/models/decision.py
index 4b088f236f1..cf2167c82df 100644
--- a/unsloth/models/decision.py
+++ b/unsloth/models/decision.py
@@ -29,6 +29,7 @@ from ._utils import (
is_bfloat16_supported,
)
from .loader_utils import is_distributed
+from ..device_type import xpu_keeps_explicit_bf16
from ._decision_fast import compiled_encoder, pad_length
from ._decision_common import * # noqa: F401,F403
@@ -630,7 +631,9 @@ def _clef_bnb_config(dtype):
from transformers import BitsAndBytesConfig
from unsloth_zoo.peft_utils import SKIP_QUANTIZATION_MODULES
- float16 = dtype == torch.float16 or not is_bfloat16_supported()
+ # XPU keeps an explicit bf16 request in bf16 (emulated if need be), as the loaders do.
+ keep_bf16 = dtype == torch.bfloat16 and _device().type == "xpu" and xpu_keeps_explicit_bf16()
+ float16 = dtype == torch.float16 or (not is_bfloat16_supported() and not keep_bf16)
return BitsAndBytesConfig(
load_in_4bit = True,
bnb_4bit_use_double_quant = True,
@@ -646,8 +649,18 @@ def _clef_forced_float32(model) -> bool:
def _clef_amp_dtype(model, device):
# As _clef_mixed_precision trains (serving uses it too): the float32 path runs as loaded (Qwen3.5's
- # gated delta net overflows in fp16); anything else autocasts, fp16 on a T4.
- return None if _clef_forced_float32(model) else _amp_dtype(device)
+ # gated delta net overflows in fp16); anything else autocasts, fp16 on a T4. XPU keeps an
+ # explicit bf16 load in bf16, so its autocast follows the weights.
+ if _clef_forced_float32(model):
+ return None
+ if device.type == "xpu":
+ backbone = model._backbone() if hasattr(model, "_backbone") else model
+ dtype = getattr(backbone, "dtype", None)
+ if dtype is None:
+ dtype = next((p.dtype for p in backbone.parameters() if p.is_floating_point()), None)
+ if dtype == torch.bfloat16:
+ return torch.bfloat16
+ return _amp_dtype(device)
def _load_clef(
diff --git a/unsloth/models/diffusion.py b/unsloth/models/diffusion.py
index 29e0fefc58d..4dfcf53f897 100644
--- a/unsloth/models/diffusion.py
+++ b/unsloth/models/diffusion.py
@@ -24,6 +24,7 @@ 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 ..device_type import DEVICE_TYPE, xpu_keeps_explicit_bf16
from .diffusion_profiles import (
diffusion_model_types,
resolve_diffusion_profile,
@@ -234,7 +235,8 @@ class FastDiffusionModel:
SUPPORTS_BFLOAT16 = is_bfloat16_supported()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
- elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
+ # XPU runs an explicit bf16 request (emulated if need be), as before the native-bf16 probe.
+ elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16 and not xpu_keeps_explicit_bf16():
logger.warning_once("Device does not support bfloat16. Will change to float16.")
dtype = torch.float16
assert dtype in (torch.float16, torch.bfloat16, torch.float32)
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 7ced84fb87b..7f8590ee5ed 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -118,6 +118,7 @@ except ImportError:
# Older unsloth_zoo: fall back to reading the config fields directly.
detect_logit_transforms = None
from ..device_type import (
+ xpu_keeps_explicit_bf16,
is_hip,
get_device_type,
DEVICE_TYPE,
@@ -3345,7 +3346,8 @@ class FastLlamaModel:
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
- elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
+ # XPU runs an explicit bf16 request (emulated if need be), as before the native-bf16 probe.
+ elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16 and not xpu_keeps_explicit_bf16():
logger.warning_once("Device does not support bfloat16. Will change to float16.")
dtype = torch.float16
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index d428c181307..1c3b0279f63 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -83,6 +83,7 @@ except:
from huggingface_hub.utils._token import get_token
import importlib.util
from ..device_type import (
+ xpu_keeps_explicit_bf16,
is_hip,
get_device_type,
DEVICE_TYPE,
@@ -2151,7 +2152,8 @@ class FastModel(FastBaseModel):
SUPPORTS_BFLOAT16 = is_bfloat16_supported()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
- elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
+ # XPU runs an explicit bf16 request (emulated if need be), as before the native-bf16 probe.
+ elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16 and not xpu_keeps_explicit_bf16():
logger.warning_once("Device does not support bfloat16. Will change to float16.")
dtype = torch.float16
assert dtype in (torch.float16, torch.bfloat16, torch.float32)
@@ -2856,7 +2858,10 @@ class FastModel(FastBaseModel):
if (
disable_name.lower() == model_type_arch.lower().replace("-", "").replace("_", "")
or disable_name.lower() in model_types_all
- ) and ((dtype == torch.float16) or not SUPPORTS_BFLOAT16):
+ ) and (
+ # An explicit bf16 load kept on XPU stays bf16, not the fp16 + float32 path.
+ dtype == torch.float16 or (not SUPPORTS_BFLOAT16 and dtype != torch.bfloat16)
+ ):
os.environ["UNSLOTH_FORCE_FLOAT32"] = "1"
do_forced_float32 = True
dtype = torch.bfloat16 # Change to bfloat16 loading
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 00c73a9086a..2f724174e41 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -2589,6 +2589,10 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
" from unsloth_zoo.device_type import device_is_bf16_supported as _bf16_supported\n"
"except Exception:\n"
" _bf16_supported = torch.cuda.is_bf16_supported\n"
+ "try:\n"
+ " from unsloth_zoo.device_type import DEVICE_TYPE as _unsloth_device_type\n"
+ "except Exception:\n"
+ " _unsloth_device_type = None\n"
# FORCE_FLOAT32 models (Gemma3, gpt_oss) cannot use float16: without bf16 keep float32, with bf16 full finetuning may still autocast. Stamped by from_pretrained, since the env is process wide and a later load would otherwise answer for this trainer.
"full_finetuning = getattr(model, '_unsloth_full_finetuning', None)\n"
"if full_finetuning is None: full_finetuning = os.environ.get('UNSLOTH_ENABLE_FULL_FINETUNING', '0') == '1'\n"
@@ -2629,7 +2633,8 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
" if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'\n"
"elif (not use_bf16 and not use_fp16) and mixed_precision_dtype == 'float32':\n"
" # Mixed precision training. bf16 only if the GPU supports it; V100/T4 use fp16.\n"
- " use_bf16_amp = (not float16) and _bf16_supported()\n"
+ # XPU keeps an explicit bf16 model in bf16 (emulated if need be), so autocast follows it.
+ " use_bf16_amp = (not float16) and (_bf16_supported() or (bfloat16 and _unsloth_device_type == 'xpu'))\n"
" args.fp16 = not use_bf16_amp\n"
" args.bf16 = use_bf16_amp\n"
" os.environ['ACCELERATE_MIXED_PRECISION'] = 'bf16' if use_bf16_amp else 'fp16'\n"
diff --git a/unsloth/models/sentence_transformer.py b/unsloth/models/sentence_transformer.py
index 2ea71a4d448..f0c1700480c 100644
--- a/unsloth/models/sentence_transformer.py
+++ b/unsloth/models/sentence_transformer.py
@@ -19,6 +19,7 @@ from .loader_utils import (
requested_device_map,
unmarked_device_map,
)
+from ..device_type import DEVICE_TYPE, xpu_keeps_explicit_bf16
from ._utils import (
config_return_dict,
SUPPORTS_BFLOAT16,
@@ -2663,7 +2664,10 @@ class FastSentenceTransformer(FastModel):
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
else:
dtype = torch.float32
- elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
+ # XPU runs an explicit bf16 request (emulated if need be), as before the native-bf16 probe.
+ elif (
+ dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16 and not xpu_keeps_explicit_bf16()
+ ):
print("Unsloth: Device does not support bfloat16. Using float16 instead.")
dtype = torch.float16
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 856aca0f260..64b325fa279 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -243,6 +243,7 @@ try:
except:
from huggingface_hub.utils._token import get_token
from ..device_type import (
+ xpu_keeps_explicit_bf16,
get_device_stats,
clean_gpu_cache,
is_hip,
@@ -3380,7 +3381,8 @@ class FastBaseModel:
elif os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "1":
if dtype == torch.float16:
dtype = torch.bfloat16
- elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
+ # XPU runs an explicit bf16 request (emulated if need be), as before the native-bf16 probe.
+ elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16 and not xpu_keeps_explicit_bf16():
logger.warning_once("Device does not support bfloat16. Will change to float16.")
dtype = torch.float16
assert dtype in (torch.float16, torch.bfloat16, torch.float32)