Skip to content

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

Merged
danielhanchen merged 8 commits into
mainfrom
xpu-native-bf16-cuda-order-hint
Oct 20, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
xpu-native-bf16-cuda-order-hint

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Intel XPU bf16 now reflects native support on every visible XPU, with the same answer as unsloth_zoo's device_is_bf16_supported (which the trainer reads). Mixed NVIDIA hosts get a one-time CUDA_DEVICE_ORDER hint. MLX bf16 reads the Metal GPU family, so M1/M2 hosts behind a VM or a renamed device stop claiming bf16.

What changes

  • device_type.xpu_supports_native_bf16() replaces torch.xpu.is_bf16_supported() in _gpu_init.py, which counts emulation and so is always True, and the hard-coded True in models/_utils.py. Its answers:
    • False only when some XPU reports has_bfloat16_conversions=False AND the SYCL aspect query is shown to work, meaning some aspect reads True on some device. SYCL answers aspects through libocloc, which torch-xpu wheels do not ship; without it every aspect reads False everywhere, so an all-False answer is treated as unknown and bf16 stays on.
    • True for an older torch without the attribute, and for an unreadable device.
    • It is an inlined copy of unsloth_zoo.device_type._xpu_bf16_supported (Detect native bf16 on Intel XPU and Apple MLX, fall back when XPU free memory is unreadable unsloth-zoo#1894). It applies only when the installed zoo has that helper: an older zoo still counts emulated bf16 for trainer AMP, gradient checkpointing and vLLM, so core keeps its previous XPU answer there and loading and training never disagree.
    • The training decision's XPU autocast dtype follows the same answer.
  • device_type.cuda_order_mismatch_names() adds one Unsloth: message at import when the visible NVIDIA cards differ and CUDA_DEVICE_ORDER is unset. Message only. An explicit order, including FASTEST_FIRST, is the user's choice and stays silent. The suggested command matches the shell ($env:CUDA_DEVICE_ORDER = 'PCI_BUS_ID' on Windows).
  • MLX is_bfloat16_supported() decides by the Metal GPU family when MLX reports it: applegpu_g13 / g14 (M1 / M2) lack native bf16. Otherwise the existing M1/M2 name check decides, as in unsloth_zoo. The MLX FastLanguageModel / FastVisionModel loaders pass dtype="float16" to zoo's loader when that check says no bf16 and the caller gave no dtype, so the default load matches.

Hosts affected

  • Intel XPU hosts where a device provably lacks native bf16 now get float16 instead of bf16. Unchanged: hosts without libocloc, older torch, and unreadable devices.
  • NVIDIA hosts with 2 or more different visible GPUs and no CUDA_DEVICE_ORDER print one extra line. Silent: single GPU, homogeneous, ROCm, and any host with the order set.
  • Apple Silicon M1/M2 reporting a non-M1/M2 device name (for example a macOS VM) now gets float16.

Tests

  • New tests with spoofed devices:
    • tests/test_xpu_native_bf16.py: 1 and 2 XPUs that are native, proven non-native, without libocloc, older torch, or unreadable.
    • tests/test_cuda_device_order_warning.py: mixed and homogeneous names, HIP, and each order value.
    • tests/test_mlx_bf16_gpu_family.py: Apple families and names.
  • Parity checks against unsloth_zoo run when it has the matching helpers: 70 passed against unsloth-zoo#1894.
  • The new cells fail on main and pass here. Single-GPU and homogeneous controls keep today's answer.
  • Related suites fail on the same test IDs before and after.
  • Real B200 import: no order message and bf16 on, as before.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-20T19:02:15.997883Z
We couldn’t complete this review. Comment @codex review to try again.
ce016a0 New commits
🔒 Security Review ⚠️ Failed 2026-10-20T15:32:47.095335Z
We couldn’t complete this review. Comment @codex security review to try again.
9d2a069 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 9b803173ae

ℹ️ 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".

…ype from the GPU family, decision XPU AMP follows bf16 support, shell-specific order hint

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 0e4781addb

ℹ️ 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".

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 20, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 20, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 20, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 20, 2026
@danielhanchen
danielhanchen merged commit e172530 into main Oct 20, 2026
@danielhanchen
danielhanchen deleted the xpu-native-bf16-cuda-order-hint branch October 20, 2026 20:23
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Oct 21, 2026
…inetuning keeps its default dtype (unslothai#14493)

## Summary
Follow-up to unslothai#14371 from a post-merge regression review. Pairs with the
matching unsloth-zoo change so core and zoo keep one rule.

## What changes
- **An explicit bf16 request on XPU stays bf16 only beside the paired
unsloth-zoo.** zoo publishes `XPU_KEEPS_EXPLICIT_BF16` with the matching
vLLM change; without it (current zoo main) core downgrades to fp16 as
main does, so vLLM and the HF weights it shares never end up in
different dtypes.
- **Each visible XPU is judged by its own SYCL capability reads.**
Before, one device whose aspects read True made another device whose
aspects all read False (no libocloc there) count as non-native, which
turned bf16 off. The answer stays process-wide like the CUDA one: any
visible XPU proven to lack native bf16 still turns it off, since the
load target (a `device_map`, or vLLM's device) is not known when this is
decided. Single-XPU and homogeneous hosts are unchanged.
- **An explicit bf16 request on XPU stays bf16** (emulated if needed),
as before unslothai#14371, across the core loaders; the trainer autocasts a bf16
model in bf16 on XPU.
- **MLX full finetuning on M1 / M2 keeps its previous default dtype.**
The wrapper injected float16, which zoo's MLX loader promotes to float32
for full finetuning, doubling weight memory. float16 is now injected
only for LoRA.
- `tests/test_cuda_device_order_warning.py` scopes its `os.name` patch
(Python 3.13 + pytest 9).
- CUDA and HIP behaviour is unchanged (asserted).

## Tests
- Related tests: 114 passed, 31 skipped; 16 fail against main's source.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant