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.

Format two files merged without the repo formatter in #14769 (#14908)

Daniel Han08a1e394c564
diff --git a/tests/test_xpu_native_bf16.py b/tests/test_xpu_native_bf16.py
index e70cfb2065a..ce02c9711e7 100644
--- a/tests/test_xpu_native_bf16.py
+++ b/tests/test_xpu_native_bf16.py
@@ -151,14 +151,31 @@ def test_xpu_bf16_judges_the_devices_a_model_uses(monkeypatch, device, bf16):
# torchrun imports unsloth before prepare_device_map selects the rank's XPU, so every rank reads
# current device 0 at import: judge the launch's XPUs together instead, one answer for all ranks.
LAUNCH_CASES = [
- pytest.param([NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, False, id = "2ranks-mixed"),
- pytest.param([EMULATED, NATIVE], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, False, id = "2ranks-mixed-rev"),
- pytest.param([NATIVE, NATIVE], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, True, id = "2ranks-native"),
- pytest.param([NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "1", "WORLD_SIZE": "2"}, True, id = "1rank-per-node"),
+ pytest.param(
+ [NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, False, id = "2ranks-mixed"
+ ),
+ pytest.param(
+ [EMULATED, NATIVE],
+ {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"},
+ False,
+ id = "2ranks-mixed-rev",
+ ),
+ pytest.param(
+ [NATIVE, NATIVE], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, True, id = "2ranks-native"
+ ),
+ pytest.param(
+ [NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "1", "WORLD_SIZE": "2"}, True, id = "1rank-per-node"
+ ),
pytest.param([NATIVE, EMULATED], {"WORLD_SIZE": "2"}, False, id = "no-local-world-all-visible"),
- pytest.param([NATIVE], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, True, id = "affinity-mask-one-visible"),
- pytest.param([NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "1", "WORLD_SIZE": "1"}, True, id = "single-process"),
- pytest.param([NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "x", "WORLD_SIZE": "y"}, True, id = "garbage-env"),
+ pytest.param(
+ [NATIVE], {"LOCAL_WORLD_SIZE": "2", "WORLD_SIZE": "2"}, True, id = "affinity-mask-one-visible"
+ ),
+ pytest.param(
+ [NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "1", "WORLD_SIZE": "1"}, True, id = "single-process"
+ ),
+ pytest.param(
+ [NATIVE, EMULATED], {"LOCAL_WORLD_SIZE": "x", "WORLD_SIZE": "y"}, True, id = "garbage-env"
+ ),
]
@@ -195,7 +212,9 @@ def test_launch_answer_matches_zoo(monkeypatch, devices, env, _bf16):
pytest.param([EMULATED, NATIVE], True, id = "emulated-then-native"),
],
)
-def test_is_bfloat16_supported_follows_set_device_after_import(monkeypatch, devices, bf16_after_switch):
+def test_is_bfloat16_supported_follows_set_device_after_import(
+ monkeypatch, devices, bf16_after_switch
+):
# unsloth#14622 on XPU: import on XPU 0, then torch.xpu.set_device(1) before loading. The loader
# asks is_bfloat16_supported(); it must answer for XPU 1, as the trainer's zoo probe does.
import unsloth.models._utils as model_utils
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
index 5aa2feb3471..38940bcfa52 100644
--- a/unsloth/device_type.py
+++ b/unsloth/device_type.py
@@ -241,11 +241,13 @@ def _xpu_launch_indices():
to the visible count), else None. Unsloth is imported before a rank selects its XPU, so the
current device then reads XPU 0 on every rank; judging the ranks' devices together keeps every
rank on one dtype and is never narrower than the all-visible rule."""
+
def _env_int(name):
try:
return int(os.environ.get(name, ""))
except ValueError:
return None
+
local_world = _env_int("LOCAL_WORLD_SIZE")
world = _env_int("WORLD_SIZE")
if (local_world or 1) <= 1 and (world or 1) <= 1: