Skip to content

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

Merged
danielhanchen merged 6 commits into
mainfrom
xpu-bf16-current-device-r9-followup
Oct 21, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
xpu-bf16-current-device-r9-followup

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #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 Gate XPU bf16 on native support, hint CUDA_DEVICE_ORDER on mixed NVIDIA hosts, read the Metal GPU family for MLX bf16 #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.

…inetuning keeps its default dtype

- xpu_supports_native_bf16 judges the current XPU with its own probe validity, matching
  unsloth_zoo, so an unused non-native iGPU no longer turns a native dGPU to float16.
- An explicit bf16 request on XPU stays bf16 (emulated if need be) in every loader, and the
  trainer autocasts a bf16 model in bf16 there, as before the native-bf16 probe.
- MLX: no float16 default for full finetuning, which zoo would train in float32.
- Test: scope the os.name patch so Python 3.13 pytest reporting does not see it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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 🔄 Running since 2026-10-21T15:46:41.542763Z cf44d02 Manual request
🔒 Security Review ⚠️ Failed 2026-10-21T11:07:21.702420Z
We couldn’t complete this review. Comment @codex security review to try again.
28c21f5 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 28c21f59a8

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

Comment thread unsloth/device_type.py Outdated
Comment on lines +250 to +251
index = 0 if torch.xpu.device_count() <= 1 else torch.xpu.current_device()
props = torch.xpu.get_device_properties(index)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Probe the XPU selected by the device map

When multiple XPUs are visible and the caller loads onto a non-current device, such as device_map="xpu:1", this probes whichever device was current during module initialization rather than the load destination. Explicit device maps are preserved without calling torch.xpu.set_device, so a native xpu:0 can make a model targeting an emulated xpu:1 default to bf16, while the inverse unnecessarily downgrades a native target to fp16. Derive the capability from the selected placement or retain the conservative all-visible result when the destination is not known.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4b44445. This answer is process-wide (it sets SUPPORTS_BFLOAT16 at import and drives the trainer's autocast), so it cannot know a later device_map. It is back to the conservative all-visible rule, as for CUDA: any visible XPU proven to lack native bf16 turns bf16 off, whichever device is current or targeted. What remains of the change is that each device is judged by its own aspects, so a device whose aspects all read False (no libocloc) is no longer counted as non-native because another device's aspects read True.

Comment thread unsloth/device_type.py Outdated
Comment on lines +250 to +251
index = 0 if torch.xpu.device_count() <= 1 else torch.xpu.current_device()
props = torch.xpu.get_device_properties(index)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Require the zoo release with matching XPU semantics

When users upgrade Unsloth while retaining the currently allowed unsloth_zoo>=2026.10.7, that zoo's _xpu_bf16_supported still scans every visible XPU, whereas this helper now checks only the current one. zoo_shares_xpu_bf16() gates only on the helper's existence, so heterogeneous-XPU hosts can have core load a model with one dtype while zoo's gradient-checkpointing and vLLM paths select another. Raise the dependency floor to the paired zoo release or explicitly distinguish the old helper behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 4b44445 core uses the same all-visible rule as zoo's existing _xpu_bf16_supported (on zoo main since #1894), so a newer core beside that zoo gives the same answer on every real host. The only difference is the per-device aspect check, which matters only if one XPU reads its SYCL aspects and another reads none in the same process; libocloc is found per process, so that does not happen in practice. Published unsloth_zoo 2026.10.7 has no _xpu_bf16_supported, so core keeps its old answer there, matching that zoo.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b44445f1c

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

Comment thread unsloth/models/loader.py Outdated
Comment on lines +2131 to +2132
# 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 DEVICE_TYPE != "xpu":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align DecisionTrainer with preserved XPU bfloat16

When FastDecisionModel loads a Clef model with an explicit dtype=torch.bfloat16 on an XPU without native bf16, it delegates to this path, so the model now remains bf16. However, decision.py::_clef_mixed_precision still derives its default from _clef_amp_dtype()/is_bfloat16_supported() and therefore enables fp16 autocast (lines 1083–1087); its evaluation paths make the same choice. This runs bf16 weights under fp16 autocast and can overflow or corrupt training and evaluation, so the decision-model precision helper also needs to follow an explicit bf16 model on XPU, as the updated RL path does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aff9c67. On XPU, _clef_amp_dtype now follows a bf16 backbone (autocast bf16 over bf16 weights), and _clef_bnb_config keeps a bf16 compute dtype for an explicit bf16 request, matching the loaders and the RL path. CUDA and an fp16 load are unchanged. test_clef_keeps_an_explicit_bf16_model_in_bf16_on_xpu fails on the previous source.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: aff9c670cc

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 982f661a1f

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

Source lint fails on the hoisted-import guard. These are from this PR's changes; the gate failure next to it is only the GPU capacity notice and can be ignored:

=== unsloth/models/diffusion.py: BLOCKERS ===
  [BLOCKER] unsloth/models/diffusion.py: HOISTED-IMPORT-UNUSED 'DEVICE_TYPE' (['from:..device_type:DEVICE_TYPE']) added but unused
=== unsloth/models/sentence_transformer.py: BLOCKERS ===
  [BLOCKER] unsloth/models/sentence_transformer.py: HOISTED-IMPORT-UNUSED 'DEVICE_TYPE' (['from:..device_type:DEVICE_TYPE']) added but unused

Both files gain from ..device_type import DEVICE_TYPE without a use. Either use it where the XPU check was meant to read it, or drop the import from those two files.

@danielhanchen
danielhanchen merged commit a7a37d3 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the xpu-bf16-current-device-r9-followup branch October 21, 2026 17:55
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 25, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant