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.

Gate XPU bf16 on native support, hint CUDA_DEVICE_ORDER on mixed NVIDIA hosts, read the Metal GPU family for MLX bf16 (#14371)

Daniel Hane172530d14fd
diff --git a/tests/conftest.py b/tests/conftest.py
index f7a25d952cf..54fd1fcdf27 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -168,6 +168,9 @@ def _install_device_type_stub(name: str) -> None:
stub.hip_visible_archs = lambda: []
stub.cuda_visible_capabilities = lambda *a, **k: []
stub.cuda_gate_capabilities = lambda: []
+ stub.cuda_order_mismatch_names = lambda: []
+ stub.xpu_supports_native_bf16 = lambda: True
+ stub.zoo_shares_xpu_bf16 = lambda: False
sys.modules[name] = stub
diff --git a/tests/test_cuda_device_order_warning.py b/tests/test_cuda_device_order_warning.py
new file mode 100644
index 00000000000..e889e8ce68a
--- /dev/null
+++ b/tests/test_cuda_device_order_warning.py
@@ -0,0 +1,176 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Mixed NVIDIA cards with CUDA_DEVICE_ORDER unset: warn once at import, change nothing else."""
+
+import functools
+import inspect
+import types
+from pathlib import Path
+
+import pytest
+
+pytest.importorskip("torch")
+
+import unsloth.device_type as dt
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+GPU_INIT = REPO_ROOT / "unsloth" / "_gpu_init.py"
+
+if not hasattr(dt, "torch"):
+ pytest.skip(
+ "device_type stub or MLX host; the real probes are not loaded", allow_module_level = True
+ )
+
+A100, L4, T4 = "NVIDIA A100-SXM4-80GB", "NVIDIA L4", "Tesla T4"
+CAPS = {A100: (8, 0), L4: (8, 9), T4: (7, 5)}
+MARK = "Unsloth: visible GPUs differ"
+
+
[email protected](autouse = True)
+def _nvidia_host(monkeypatch):
+ # is_hip() is cached from the real host; these cases simulate NVIDIA.
+ monkeypatch.setattr(dt, "is_hip", lambda: False)
+
+
+CASES = [
+ pytest.param([A100], None, False, id = "single"),
+ pytest.param([A100, A100], None, False, id = "homogeneous"),
+ pytest.param([A100, L4], None, True, id = "mixed-unset"),
+ pytest.param([L4, A100, A100], None, True, id = "mixed3-unset"),
+ pytest.param([A100, L4], "FASTEST_FIRST", False, id = "mixed-fastest-first-explicit"),
+ pytest.param([A100, L4], " ", True, id = "mixed-blank"),
+ pytest.param([A100, L4], "PCI_BUS_ID", False, id = "mixed-pci"),
+ pytest.param([A100, L4], " pci_bus_id ", False, id = "mixed-pci-lower"),
+ pytest.param([A100, T4], None, True, id = "mixed-with-pre-ampere"),
+ pytest.param([A100, None], None, False, id = "mixed-one-unreadable"),
+ pytest.param([], None, False, id = "no-devices"),
+]
+
+
+def _fake_torch(names):
+ def get_device_name(device = None):
+ if names[device or 0] is None:
+ raise RuntimeError("unreadable")
+ return names[device or 0]
+
+ def get_device_capability(device = None):
+ return CAPS.get(names[device or 0], (8, 0))
+
+ return types.SimpleNamespace(
+ device = __import__("torch").device,
+ version = types.SimpleNamespace(hip = None, cuda = "12.8"),
+ cuda = types.SimpleNamespace(
+ device_count = lambda: len(names),
+ current_device = lambda: 0,
+ is_available = lambda: True,
+ get_device_name = get_device_name,
+ get_device_capability = get_device_capability,
+ get_device_properties = lambda device = None: types.SimpleNamespace(
+ name = get_device_name(device)
+ ),
+ is_bf16_supported = lambda including_emulation = True: True,
+ ),
+ )
+
+
+def _run_cuda_arm(fake, monkeypatch):
+ import unsloth._gpu_init as gpu_init
+
+ source = GPU_INIT.read_text(encoding = "utf-8")
+ start = 'if DEVICE_TYPE == "cuda" and not torch.cuda.is_available():'
+ body = start + source.split(start, 1)[1].split('\nelif DEVICE_TYPE == "hip":', 1)[0]
+ monkeypatch.setattr(dt, "torch", fake)
+ namespace = dict(vars(gpu_init))
+ namespace.update(torch = fake, DEVICE_TYPE = "cuda", inspect = inspect, functools = functools)
+ exec(compile(body, str(GPU_INIT), "exec"), namespace)
+
+
+def _set_order(monkeypatch, order):
+ if order is None:
+ monkeypatch.delenv("CUDA_DEVICE_ORDER", raising = False)
+ else:
+ monkeypatch.setenv("CUDA_DEVICE_ORDER", order)
+
+
[email protected]("names,order,warn", CASES)
+def test_import_warns_once_on_mixed_nvidia_without_pci_order(
+ monkeypatch, capsys, names, order, warn
+):
+ _set_order(monkeypatch, order)
+ if names:
+ _run_cuda_arm(_fake_torch(names), monkeypatch)
+ else:
+ monkeypatch.setattr(dt, "torch", _fake_torch(names))
+ assert dt.cuda_order_mismatch_names() == []
+ return
+ out = capsys.readouterr().out
+ assert out.count(MARK) == (1 if warn else 0)
+ if warn:
+ for name in set(names):
+ assert name in out
+
+
[email protected]("names,order,warn", CASES)
+def test_helper_matches_the_warning(monkeypatch, names, order, warn):
+ _set_order(monkeypatch, order)
+ monkeypatch.setattr(dt, "torch", _fake_torch(names))
+ assert bool(dt.cuda_order_mismatch_names()) is warn
+
+
+def test_hip_is_silent(monkeypatch):
+ monkeypatch.delenv("CUDA_DEVICE_ORDER", raising = False)
+ monkeypatch.setattr(dt, "torch", _fake_torch([A100, L4]))
+ monkeypatch.setattr(dt, "is_hip", lambda: True)
+ assert dt.cuda_order_mismatch_names() == []
+
+
+def test_unreadable_count_is_silent(monkeypatch):
+ def boom():
+ raise RuntimeError("no driver")
+
+ monkeypatch.delenv("CUDA_DEVICE_ORDER", raising = False)
+ monkeypatch.setattr(
+ dt, "torch", types.SimpleNamespace(cuda = types.SimpleNamespace(device_count = boom))
+ )
+ assert dt.cuda_order_mismatch_names() == []
+
+
+def test_warning_changes_nothing_else(monkeypatch):
+ import unsloth._gpu_init as gpu_init
+
+ results = []
+ for order in (None, "PCI_BUS_ID"):
+ _set_order(monkeypatch, order)
+ fake = _fake_torch([A100, L4])
+ source = GPU_INIT.read_text(encoding = "utf-8")
+ start = 'if DEVICE_TYPE == "cuda" and not torch.cuda.is_available():'
+ body = start + source.split(start, 1)[1].split('\nelif DEVICE_TYPE == "hip":', 1)[0]
+ monkeypatch.setattr(dt, "torch", fake)
+ namespace = dict(vars(gpu_init))
+ namespace.update(torch = fake, DEVICE_TYPE = "cuda", inspect = inspect, functools = functools)
+ exec(compile(body, str(GPU_INIT), "exec"), namespace)
+ results.append((namespace["SUPPORTS_BFLOAT16"], fake.cuda.is_bf16_supported()))
+ assert results[0] == results[1]
+
+
+ "os_name,command",
+ [
+ ("posix", "export CUDA_DEVICE_ORDER=PCI_BUS_ID"),
+ ("nt", "$env:CUDA_DEVICE_ORDER = 'PCI_BUS_ID'"),
+ ],
+)
+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)
+ assert f"`{command}`" in capsys.readouterr().out
+
+
+def test_repeated_names_listed_once(monkeypatch):
+ _set_order(monkeypatch, None)
+ monkeypatch.setattr(dt, "torch", _fake_torch([A100, A100, L4, L4]))
+ assert dt.cuda_order_mismatch_names() == [A100, L4]
diff --git a/tests/test_decision_model.py b/tests/test_decision_model.py
index 55623f16ca2..c9ce3ada7db 100644
--- a/tests/test_decision_model.py
+++ b/tests/test_decision_model.py
@@ -1458,6 +1458,11 @@ def test_clef_autocasts_as_it_trains_and_never_on_the_float32_path(monkeypatch):
assert decision._clef_amp_dtype(model, cuda) is None
model._unsloth_forced_float32 = False
assert decision._clef_amp_dtype(model, torch.device("cpu")) is None
+ # An XPU without native bf16 loads fp16, so it autocasts fp16 too.
+ xpu = torch.device("xpu")
+ assert decision._amp_dtype(xpu) == torch.float16
+ monkeypatch.setattr(decision, "is_bfloat16_supported", lambda: True)
+ assert decision._amp_dtype(xpu) == torch.bfloat16
def test_a_clef_prompt_that_fits_exactly_is_not_truncated(monkeypatch):
diff --git a/tests/test_mlx_bf16_gpu_family.py b/tests/test_mlx_bf16_gpu_family.py
new file mode 100644
index 00000000000..067df94680e
--- /dev/null
+++ b/tests/test_mlx_bf16_gpu_family.py
@@ -0,0 +1,148 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""MLX is_bfloat16_supported(): Metal GPU family first (g13/g14 lack bf16), chip name only as fallback."""
+
+import ast
+import sys
+import types
+from pathlib import Path
+
+import pytest
+
+INIT = Path(__file__).resolve().parents[1] / "unsloth" / "__init__.py"
+
+CASES = [
+ pytest.param({"device_name": "Apple M1 Max", "architecture": "applegpu_g13s"}, False, id = "m1"),
+ pytest.param({"device_name": "Apple M2", "architecture": "applegpu_g14g"}, False, id = "m2"),
+ pytest.param({"device_name": "Apple M3 Pro", "architecture": "applegpu_g15s"}, True, id = "m3"),
+ pytest.param({"device_name": "Apple M4", "architecture": "applegpu_g16g"}, True, id = "m4"),
+ pytest.param({"device_name": "Apple M5", "architecture": "applegpu_g17g"}, True, id = "m5"),
+ pytest.param(
+ {"device_name": "Apple Paravirtual device", "architecture": "applegpu_g14s"},
+ False,
+ id = "vm-on-m2",
+ ),
+ pytest.param(
+ {"device_name": "Apple Paravirtual device", "architecture": "applegpu_g16s"},
+ True,
+ id = "vm-on-m4",
+ ),
+ pytest.param({"device_name": "Apple M1"}, False, id = "name-only-m1"),
+ pytest.param({"device_name": "Apple M2 Ultra", "architecture": ""}, False, id = "name-only-m2"),
+ pytest.param({"device_name": "Apple M3"}, True, id = "name-only-m3"),
+ pytest.param({"device_name": "Apple Paravirtual device"}, True, id = "name-only-vm"),
+ pytest.param({}, True, id = "empty"),
+ pytest.param(
+ {"device_name": "Apple M2", "architecture": "applegpu_g16g"}, True, id = "family-wins"
+ ),
+ # MLX's device.cpp always reads a class letter after the two family digits; without one, fall back.
+ pytest.param(
+ {"device_name": "Apple M3", "architecture": "applegpu_g13"},
+ True,
+ id = "no-class-letter-uses-name",
+ ),
+]
+
+
+def _is_bfloat16_supported():
+ tree = ast.parse(INIT.read_text(encoding = "utf-8"))
+ node = next(
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.FunctionDef) and n.name == "is_bfloat16_supported"
+ )
+ namespace = {}
+ exec(compile(ast.Module(body = [node], type_ignores = []), str(INIT), "exec"), namespace)
+ return namespace["is_bfloat16_supported"]
+
+
+def _install_mlx(
+ monkeypatch,
+ info,
+ top_level = True,
+):
+ core = types.ModuleType("mlx.core")
+ core.metal = types.SimpleNamespace(device_info = lambda: dict(info))
+ if top_level:
+ core.device_info = lambda: dict(info)
+ mlx = types.ModuleType("mlx")
+ mlx.core = core
+ monkeypatch.setitem(sys.modules, "mlx", mlx)
+ monkeypatch.setitem(sys.modules, "mlx.core", core)
+
+
[email protected]("info,expected", CASES)
+def test_bf16_from_gpu_family_then_name(monkeypatch, info, expected):
+ _install_mlx(monkeypatch, info)
+ assert _is_bfloat16_supported()() is expected
+
+
+def test_old_mlx_without_top_level_device_info(monkeypatch):
+ _install_mlx(monkeypatch, {"device_name": "Apple M1", "architecture": "applegpu_g13g"}, False)
+ assert _is_bfloat16_supported()() is False
+
+
+def test_probe_failure_keeps_true(monkeypatch):
+ core = types.ModuleType("mlx.core")
+
+ def boom():
+ raise RuntimeError("no Metal")
+
+ core.device_info = boom
+ monkeypatch.setitem(sys.modules, "mlx", types.ModuleType("mlx"))
+ monkeypatch.setitem(sys.modules, "mlx.core", core)
+ assert _is_bfloat16_supported()() is True
+
+
[email protected]("info,_expected", CASES)
+def test_matches_zoo_answer(monkeypatch, info, _expected):
+ zoo_dt = pytest.importorskip("unsloth_zoo.device_type")
+ if not hasattr(zoo_dt, "_apple_bf16_supported"):
+ pytest.skip("installed unsloth_zoo predates _apple_bf16_supported")
+ _install_mlx(monkeypatch, info)
+ assert _is_bfloat16_supported()() is zoo_dt._apple_bf16_supported()
+
+
+def _mlx_from_pretrained(bf16):
+ tree = ast.parse(INIT.read_text(encoding = "utf-8"))
+ node = next(
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.FunctionDef) and n.name == "_mlx_from_pretrained"
+ )
+ calls = []
+ namespace = {
+ "is_bfloat16_supported": lambda: bf16,
+ "FastMLXModel": types.SimpleNamespace(from_pretrained = lambda *a, **k: calls.append((a, k))),
+ }
+ exec(compile(ast.Module(body = [node], type_ignores = []), str(INIT), "exec"), namespace)
+ return namespace["_mlx_from_pretrained"], calls
+
+
+ "args,kwargs,bf16,expected",
+ [
+ pytest.param(("m",), {}, True, (("m",), {}), id = "bf16-untouched"),
+ pytest.param(("m",), {}, False, (("m",), {"dtype": "float16"}), id = "default-kw"),
+ pytest.param(("m",), {"dtype": None}, False, (("m",), {"dtype": "float16"}), id = "none-kw"),
+ pytest.param(
+ ("m", 2048, None, True), {}, False, (("m", 2048, "float16", True), {}), id = "none-pos"
+ ),
+ pytest.param(
+ ("m",),
+ {"dtype": "bfloat16"},
+ False,
+ (("m",), {"dtype": "bfloat16"}),
+ id = "explicit-wins",
+ ),
+ pytest.param(
+ ("m", 2048, "float32"), {}, False, (("m", 2048, "float32"), {}), id = "explicit-pos-wins"
+ ),
+ ],
+)
+def test_loader_default_dtype_follows_gpu_family(args, kwargs, bf16, expected):
+ """zoo's loader defaults by name only, so a g13/g14 VM would load bf16 without this."""
+ load, calls = _mlx_from_pretrained(bf16)
+ load(*args, **kwargs)
+ assert calls == [expected]
diff --git a/tests/test_xpu_native_bf16.py b/tests/test_xpu_native_bf16.py
new file mode 100644
index 00000000000..4b4f45ae7a9
--- /dev/null
+++ b/tests/test_xpu_native_bf16.py
@@ -0,0 +1,153 @@
+# 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)."""
+
+import re
+import types
+from pathlib import Path
+
+import pytest
+
+pytest.importorskip("torch")
+
+import unsloth.device_type as dt
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+GPU_INIT = REPO_ROOT / "unsloth" / "_gpu_init.py"
+MODEL_UTILS = REPO_ROOT / "unsloth" / "models" / "_utils.py"
+
+if not hasattr(dt, "torch"):
+ pytest.skip(
+ "device_type stub or MLX host; the real probes are not loaded", allow_module_level = True
+ )
+
+# Per device: a dict of aspect values, "old" (torch without the aspect) or None (unreadable).
+NATIVE = {"has_bfloat16_conversions": True}
+EMULATED = {"has_bfloat16_conversions": False, "has_subgroup_matrix_multiply_accumulate": True}
+NO_OCLOC = {"has_bfloat16_conversions": False}
+
+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"),
+]
+
+
+def _fake_torch(devices):
+ def get_device_properties(device = None):
+ spec = devices[device or 0]
+ if spec is None:
+ raise RuntimeError("unreadable device")
+ props = types.SimpleNamespace(name = f"Intel XPU {device}")
+ if spec != "old":
+ for name, value in spec.items():
+ setattr(props, name, value)
+ return props
+
+ def is_bf16_supported(including_emulation: bool = True) -> bool:
+ return including_emulation or bool(
+ getattr(get_device_properties(), "has_bfloat16_conversions", True)
+ )
+
+ return types.SimpleNamespace(
+ xpu = types.SimpleNamespace(
+ device_count = lambda: len(devices),
+ current_device = lambda: 0,
+ is_available = lambda: True,
+ get_device_properties = get_device_properties,
+ is_bf16_supported = is_bf16_supported,
+ ),
+ )
+
+
+def _xpu_arm(path):
+ source = path.read_text(encoding = "utf-8")
+ match = re.search(
+ r'\nelif DEVICE_TYPE == "xpu":\n((?: #.*\n)* SUPPORTS_BFLOAT16 = .*?)\nelif DEVICE_TYPE == "npu":',
+ source,
+ re.S,
+ )
+ return 'if DEVICE_TYPE == "xpu":\n' + match.group(1)
+
+
+def _run(
+ path,
+ module,
+ fake,
+ monkeypatch,
+ zoo_shares = True,
+):
+ monkeypatch.setattr(dt, "torch", fake)
+ namespace = dict(vars(module))
+ namespace.update(torch = fake, DEVICE_TYPE = "xpu", zoo_shares_xpu_bf16 = lambda: zoo_shares)
+ exec(compile(_xpu_arm(path), str(path), "exec"), namespace)
+ return namespace
+
+
[email protected]("devices,bf16", CASES)
+def test_gpu_init_xpu_bf16_is_native_on_every_device(monkeypatch, devices, bf16):
+ import unsloth._gpu_init as gpu_init
+
+ fake = _fake_torch(devices)
+ 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):
+ import unsloth.models._utils as model_utils
+ namespace = _run(MODEL_UTILS, model_utils, _fake_torch(devices), monkeypatch)
+ assert namespace["SUPPORTS_BFLOAT16"] is bf16
+
+
+def test_xpu_unreadable_count_keeps_true(monkeypatch):
+ def boom():
+ raise RuntimeError("no driver")
+
+ fake = types.SimpleNamespace(xpu = types.SimpleNamespace(device_count = boom))
+ monkeypatch.setattr(dt, "torch", fake)
+ assert dt.xpu_supports_native_bf16() is True
+
+
[email protected]("devices,_bf16", CASES)
+def test_matches_zoo_answer(monkeypatch, devices, _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)
+ monkeypatch.setattr(dt, "torch", fake)
+ monkeypatch.setattr(zoo_dt, "torch", fake)
+ assert dt.xpu_supports_native_bf16() is zoo_dt._xpu_bf16_supported()
+
+
[email protected]("path_name", ["GPU_INIT", "MODEL_UTILS"])
+def test_older_zoo_keeps_the_old_xpu_answer(monkeypatch, path_name):
+ """An older zoo still trains in bf16 on emulated XPUs; loading fp16 would disagree with it."""
+ import unsloth._gpu_init as gpu_init
+ import unsloth.models._utils as model_utils
+
+ module = gpu_init if path_name == "GPU_INIT" else model_utils
+ fake = _fake_torch([EMULATED])
+ namespace = _run(globals()[path_name], module, fake, monkeypatch, zoo_shares = False)
+ assert namespace["SUPPORTS_BFLOAT16"] is True
+
+
+def test_zoo_shares_xpu_bf16_tracks_the_helper(monkeypatch):
+ zoo_dt = pytest.importorskip("unsloth_zoo.device_type")
+ monkeypatch.setattr(zoo_dt, "_xpu_bf16_supported", lambda: True, raising = False)
+ assert dt.zoo_shares_xpu_bf16() is True
+ monkeypatch.delattr(zoo_dt, "_xpu_bf16_supported")
+ assert dt.zoo_shares_xpu_bf16() is False
diff --git a/unsloth/__init__.py b/unsloth/__init__.py
index f24def56590..959dd90274b 100644
--- a/unsloth/__init__.py
+++ b/unsloth/__init__.py
@@ -372,10 +372,20 @@ if _IS_MLX:
TextPreprocessor = _raw_text.TextPreprocessor
del _raw_text, _raw_text_spec, _raw_text_path, _Path
+ 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():
+ if len(args) > 2:
+ if args[2] is None:
+ args = (*args[:2], "float16", *args[3:])
+ elif kwargs.get("dtype") is None:
+ kwargs["dtype"] = "float16"
+ return FastMLXModel.from_pretrained(*args, **kwargs)
+
class FastLanguageModel:
@staticmethod
def from_pretrained(*args, **kwargs):
- return FastMLXModel.from_pretrained(*args, **kwargs)
+ return _mlx_from_pretrained(*args, **kwargs)
@staticmethod
def get_peft_model(*args, **kwargs):
@@ -389,7 +399,7 @@ if _IS_MLX:
@staticmethod
def from_pretrained(*args, **kwargs):
kwargs.setdefault("text_only", False)
- return FastMLXModel.from_pretrained(*args, **kwargs)
+ return _mlx_from_pretrained(*args, **kwargs)
@staticmethod
def for_training(*args, **kwargs):
@@ -425,8 +435,15 @@ if _IS_MLX:
def is_bfloat16_supported():
try:
+ import re
import mlx.core as mx
- name = mx.device_info().get("device_name", "") or ""
+
+ info = mx.device_info() if hasattr(mx, "device_info") else mx.metal.device_info()
+ # Family over name (VMs report "Apple Paravirtual device"); matches zoo's _apple_bf16_supported.
+ match = re.fullmatch(r"applegpu_g(\d+)[a-z]", str(info.get("architecture", "") or ""))
+ if match:
+ return int(match.group(1)) >= 15
+ name = info.get("device_name", "") or ""
return not name.startswith(("Apple M1", "Apple M2"))
except Exception:
return True
diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py
index 91f655791b7..5b3ad131f6d 100644
--- a/unsloth/_gpu_init.py
+++ b/unsloth/_gpu_init.py
@@ -330,8 +330,11 @@ try:
arch_lacks_buffer_ops,
apply_gfx101x_triton_workaround,
cuda_gate_capabilities,
+ cuda_order_mismatch_names,
cuda_visible_capabilities,
hip_visible_archs,
+ xpu_supports_native_bf16,
+ zoo_shares_xpu_bf16,
)
except NotImplementedError as device_type_error:
_reraise_device_type_error_with_gpu_hint(device_type_error)
@@ -588,6 +591,21 @@ elif DEVICE_TYPE == "cuda":
return SUPPORTS_BFLOAT16
torch.cuda.is_bf16_supported = is_bf16_supported
+
+ _mixed_names = cuda_order_mismatch_names()
+ if _mixed_names:
+ _order_hint = (
+ "$env:CUDA_DEVICE_ORDER = 'PCI_BUS_ID'"
+ if os.name == "nt"
+ else "export CUDA_DEVICE_ORDER=PCI_BUS_ID"
+ )
+ print(
+ f"Unsloth: visible GPUs differ ({', '.join(_mixed_names)}), so CUDA's device indices may "
+ "not match nvidia-smi's and CUDA_VISIBLE_DEVICES can select another card. "
+ f"Run `{_order_hint}` before starting Python to make them match."
+ )
+ del _order_hint
+ del _mixed_names
elif DEVICE_TYPE == "hip":
old_is_bf16_supported = torch.cuda.is_bf16_supported
@@ -602,9 +620,10 @@ elif DEVICE_TYPE == "hip":
torch.cuda.is_bf16_supported = is_bf16_supported
del old_is_bf16_supported
elif DEVICE_TYPE == "xpu":
- # torch.xpu.is_bf16_supported() does not have including_emulation set SUPPORTS_BFLOAT16 as
- # torch.xpu.is_bf16_supported()
- SUPPORTS_BFLOAT16 = torch.xpu.is_bf16_supported()
+ # Don't patch torch.xpu.is_bf16_supported: Studio reads its strict answer.
+ SUPPORTS_BFLOAT16 = (
+ xpu_supports_native_bf16() if zoo_shares_xpu_bf16() else torch.xpu.is_bf16_supported()
+ )
elif DEVICE_TYPE == "npu":
# No arm left the name unbound, so consumers fell back to their own False.
SUPPORTS_BFLOAT16 = torch.npu.is_bf16_supported()
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
index fc1532cbcf5..c71d1e957bc 100644
--- a/unsloth/device_type.py
+++ b/unsloth/device_type.py
@@ -216,6 +216,60 @@ def cuda_gate_capabilities():
return []
+def cuda_order_mismatch_names():
+ """Visible NVIDIA names when they differ and CUDA_DEVICE_ORDER is unset, else []."""
+ if is_hip() or os.environ.get("CUDA_DEVICE_ORDER", "").strip():
+ return []
+ try:
+ count = torch.cuda.device_count()
+ except Exception:
+ return []
+ if count <= 1:
+ return []
+ names = []
+ for i in range(count):
+ try:
+ names.append(str(torch.cuda.get_device_name(i)))
+ except Exception:
+ continue
+ names = list(dict.fromkeys(names))
+ return names if len(names) > 1 else []
+
+
+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."""
+ aspects = (
+ "has_bfloat16_conversions",
+ "has_subgroup_matrix_multiply_accumulate",
+ "has_subgroup_matrix_multiply_accumulate_tensor_float32",
+ "has_subgroup_2d_block_io",
+ )
+ try:
+ 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)
+
+
+def zoo_shares_xpu_bf16():
+ """True once unsloth_zoo has _xpu_bf16_supported. An older zoo still counts emulated bf16 for
+ trainer AMP, gradient checkpointing and vLLM, so keep the old XPU answer there to load alike."""
+ try:
+ import unsloth_zoo.device_type as zoo_device_type
+ except Exception:
+ return False
+ return hasattr(zoo_device_type, "_xpu_bf16_supported")
+
+
# Pre-Volta NVIDIA (Maxwell / Pascal, sm < 7.0): Inductor refuses to emit Triton for these
# (GPUTooOldForTriton mid-training), while Unsloth's own Triton kernels still run, so train eagerly.
def apply_pre_volta_compile_workaround(
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 5ccab86768b..4a47f1dbbc0 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -169,6 +169,8 @@ from ..device_type import (
apply_gfx101x_triton_workaround,
cuda_gate_capabilities,
gfx101x_triton_workaround_applied,
+ xpu_supports_native_bf16,
+ zoo_shares_xpu_bf16,
)
from ..import_fixes import UNSLOTH_ENABLE_LOGGING, stale_kernel_hint
from unsloth_zoo.log import logger
@@ -3996,7 +3998,7 @@ elif DEVICE_TYPE == "hip":
HAS_FLASH_ATTENTION = False
elif DEVICE_TYPE == "xpu":
- SUPPORTS_BFLOAT16 = True
+ SUPPORTS_BFLOAT16 = xpu_supports_native_bf16() if zoo_shares_xpu_bf16() else True
elif DEVICE_TYPE == "npu":
# Ask torch_npu: the module-level False silently rewrote an explicit bfloat16 to float16.
SUPPORTS_BFLOAT16 = torch.npu.is_bf16_supported()
diff --git a/unsloth/models/decision.py b/unsloth/models/decision.py
index 04f30bbc484..4b088f236f1 100644
--- a/unsloth/models/decision.py
+++ b/unsloth/models/decision.py
@@ -92,9 +92,9 @@ def _pin_device_map(kwargs) -> None:
def _amp_dtype(device):
- if device.type == "cuda":
+ if device.type in ("cuda", "xpu"):
return torch.bfloat16 if is_bfloat16_supported() else torch.float16
- return torch.bfloat16 if device.type == "xpu" else None
+ return None
def _no_cudnn_attention():