Repository navigation
XPU bf16: judge the XPU in use, not every visible one - #14769
Conversation
xpu_supports_native_bf16() now asks the current XPU (or the devices passed in), the device torch.xpu.is_bf16_supported() asks. An unused sibling that emulates bf16 no longer turns SUPPORTS_BFLOAT16 off on a native XPU, so the automatic dtype stays bf16 there as it was before the native-bf16 probe. Emulated bf16 still runs on any XPU a split model reaches, so this only widens support. Matches unsloth_zoo.device_type._xpu_bf16_supported.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e722aeecd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if item is None: | ||
| index = torch.xpu.current_device() |
There was a problem hiding this comment.
Recompute bf16 support after selecting the target XPU
When Unsloth is imported before the target XPU is selected—such as bare torchrun, where prepare_device_map() calls torch.xpu.set_device(local_rank) only during model loading—this probes the process-default device (normally XPU 0) and caches that result in both _gpu_init.SUPPORTS_BFLOAT16 and models._utils.SUPPORTS_BFLOAT16. On a mixed host, every rank therefore inherits XPU 0's capability: a native rank can be unnecessarily loaded as fp16, while an emulating rank can be automatically loaded as bf16. The new explicit-device argument does not prevent this because neither initialization call passes the eventual device_map; the probe must run after placement is selected or be given the target indices.
Useful? React with 👍 / 👎.
torchrun imports unsloth before prepare_device_map calls torch.xpu.set_device(local_rank), so the import-time probe read XPU 0 on every rank and cached it in SUPPORTS_BFLOAT16. Under a launch (LOCAL_WORLD_SIZE / WORLD_SIZE > 1) judge the XPUs of this node's ranks together: every rank gets one dtype, and the answer is never narrower than the all-visible rule. Single-process runs still judge the current XPU.
The loaders read is_bfloat16_supported(), which returned the value cached at import. After torch.xpu.set_device(1) a single-process load still answered for XPU 0, while the trainer's unsloth_zoo probe answered for XPU 1. On XPU (beside a zoo with the native probe) it now re-probes the current device; the launch rule still keeps every rank on one answer. CUDA is unchanged. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806, #14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775, #14774, #14773, #14772, #14770, #14769, #14768, and others): "files were modified by this hook". pre-commit.ci runs the hooks on all files, and 20 files on main (26 by the time this lands) are not in the formatter's output, so every PR is reformatted outside its own diff. Where pre-commit.ci can push, its autofix commit drags those unrelated files into the PR (for example #14790's touches `tests/test_block_compile.py`, `tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`). The drift came in with PRs merged today, among them #14702, #14751, #14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode, GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target changes. ### Change `python scripts/run_ruff_format.py` (the hook's own entry point, `ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those 20 files, plus 6 more from #14739, #14779 and #14806, which merged while this was open. Formatting only. ### Verification - Every changed file parses to an identical AST before and after. - A second run of the hook over the same files changes nothing (fixed point), and over all tracked Python files on this branch it changes nothing. - `ruff check` passes on the changed files; `tests/test_formatter_fixed_point.py` passes (16). Open PRs pick this up on their next pre-commit.ci run after merging main.
`unsloth/device_type.py` and `tests/test_xpu_native_bf16.py` merged in #14769 (04:41 UTC) without the repo formatter, so pre-commit.ci's `ruff-format-with-kwargs` hook (which runs on all files) rewrites them on every PR queued after that. Same class as #14817, #14852, #14878 and #14885. `python scripts/run_ruff_format.py` (the hook's entry point, `ruff==0.6.9`) applied to those two files. Both parse to identical ASTs before and after, `ruff check` passes, and a full run of the hook over every tracked Python file on this branch changes nothing else.
…evice bf16 probe, drop the #1894-zoo pooling (no zoo release had it)
…tch vLLM's dtype when it refuses bf16 (unslothai#14606) ## Summary Follow-up to unslothai#14493 from a post-merge review, paired with unslothai/unsloth-zoo#1947. Two cases where loading, training and vLLM disagreed on dtype on Intel XPU. ## What changes - **Explicit bf16 plus the notebook idiom.** Since unslothai#14493 an explicit `dtype = torch.bfloat16` stays bf16 on an XPU without native bf16. The usual `fp16 = not is_bfloat16_supported()` then asks for fp16, and the generated trainer raised `TypeError: Model is in bfloat16 precision but you want to use float16`. On XPU, when the card has no native bf16 and the model is bf16, training now uses bf16 mixed precision with a notice. Every other device and combination keeps today's check, including CUDA, ROCm, MPS, a native-bf16 XPU with an explicit fp16, and forced float32. - **vLLM refusing bf16 (Arc A770).** With `fast_inference = True`, when the installed vLLM refuses bf16 on this card (asked through the zoo helper from #1947), the Hugging Face side loads fp16 before loading, so RoPE caches, the bnb compute dtype and the post-patch dtype match the weights vLLM shares. A model family that cannot train in fp16 (Gemma 3/4, Qwen3.5 and the rest of FORCE_FLOAT32) fails early with a clear message instead, whether bf16 was explicit, automatic or forced. With an older unsloth_zoo the helper is missing and nothing changes. Only XPU paths change. The native bf16 probe itself is main's (unslothai#14769, judged on the XPU in use, matching unsloth-zoo#1985); an earlier revision of this PR pooled aspects beside the #1894 zoo, which was dropped on merging main since no unsloth_zoo release ever shipped that function (2026.10.7 predates it, 2026.10.8 already has #1985). ## Tests - `tests/test_xpu_explicit_bf16_trainer_precision.py` (9 cases): XPU without native bf16 + bf16 model + fp16 asked trains bf16; CUDA, HIP, MPS, native-bf16 XPU with explicit fp16 and forced float32 keep today's outcome. - `tests/test_xpu_vllm_refuses_bf16.py`: refused gives fp16 on the HF side for a vLLM load only, accepted keeps bf16, missing zoo helper changes nothing, forced float32 vision fails early, a stale float32 env does not fail a language model load, and the FastModel family gate refuses fp16 for FORCE_FLOAT32 families only when vLLM refuses bf16 on XPU. - Base vs head (`test_differential.py`): 6 FIXED, 7 unchanged negative controls, 0 head failures. Related XPU / precision suites: 165 passed. Not run on a real XPU or A770. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Problem
xpu_supports_native_bf16()answered False if any visible XPU emulates bf16, soSUPPORTS_BFLOAT16was False on a native current XPU next to an unused emulating one. The automatic load dtype and trainer mixed precision then fell to fp16. Before the native bf16 probe this was bf16, andtorch.xpu.is_bf16_supported()asks only the current device.Fix
xpu_supports_native_bf16(device = None)judges the given device(s), else the current XPU, each by its own aspects. Unreadable means True. Emulated bf16 still runs on any XPU a split model reaches, so this only widens support. It matchesunsloth_zoo.device_type._xpu_bf16_supportedfrom unslothai/unsloth-zoo#1985. A released zoo already returns this function's answer, so both sides agree with either zoo.CUDA keeps its all-visible bf16 and pre-Volta compile gates: the default device map can split a model over every visible card, and the compile switch guards a hard Inductor failure on pre-Volta cards.
Tests
tests/test_xpu_native_bf16.py: current-device cases for_gpu_init.pyandmodels/_utils.py, explicit device arguments, unreadable current device.