Skip to content

XPU: train an explicit bf16 load in bf16 under the fp16 idiom, and match vLLM's dtype when it refuses bf16 - #14606

Merged
danielhanchen merged 11 commits into
mainfrom
xpu-explicit-bf16-agreement
Oct 25, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
xpu-explicit-bf16-agreement

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #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 XPU bf16 on the current device, keep explicit bf16 on XPU, MLX full finetuning keeps its default dtype #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 [RunPod] - llama_model_load: error loading model: error loading model vocabulary: cannot find tokenizer merges in model file #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 (#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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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 ✅ Completed 2026-10-25T06:25:01.096017Z 05fcfb3 Manual request
ℹ️ 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.

@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: d7576db78e

ℹ️ 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/vision.py
Comment on lines +3389 to +3394
if (
dtype == torch.bfloat16
and DEVICE_TYPE == "xpu"
and fast_inference
and is_vLLM_available()
and xpu_vllm_refuses_bf16()

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 Preserve forced-float32 models when vLLM rejects bf16

When FastModel loads a FORCE_FLOAT32 architecture such as Gemma 3/4 or Qwen3.5 on an affected XPU with fast_inference=True, the outer loader has deliberately changed the default fp16 dtype to bf16 and set UNSLOTH_FORCE_FLOAT32 because these models produce NaN gradients in float16. This gate unconditionally changes that bf16 back to fp16; the resulting dtype is then passed to both load_vllm and convert_vllm_to_huggingface, defeating the forced-float32 safeguard and risking corrupted training. This combination should be rejected or fall back from vLLM rather than loading the shared weights as fp16.

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: for a model loaded under UNSLOTH_FORCE_FLOAT32 (float16 gives NaN gradients) a vLLM refusal of bf16 now raises before loading, telling the user to load with fast_inference = False, instead of switching both sides to fp16 (test_a_forced_float32_model_refuses_to_load_in_fp16, for llama.py and vision.py).

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 22, 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. 🎉

Reviewed commit: fc1b7ac6b2

ℹ️ 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 danielhanchen/unsloth-staging-2 that referenced this pull request Oct 22, 2026
danielhanchen added a commit that referenced this pull request Oct 22, 2026
…sed decode RoPE kernels (#14632)

`tests/utils/test_single_row_cached_attention.py` fails all 64 cases on
main, in the Core jobs' "generation correctness guards (HARD GATE)" step
(e.g. #14606, job 119685454271; reproduced on main `0ae76cc9f7`):

```
NameError: name 'decode_rope_inplace' is not defined
```

The test rebuilds `LlamaAttention_fast_forward_inference` and
`Qwen3Attention_fast_forward_inference` from source inside a
hand-written namespace. #14599 made them call the fused decode kernels
`decode_rope_inplace` (llama) and `decode_qk_norm_rope` (qwen3), which
that namespace did not have. The product is fine: both are imported in
the real modules.

## Change

- Both names are added with their CPU behaviour: the real functions
return `False` / `None` (nothing written) as soon as the tensors are not
on CUDA, and the unfused path runs. That is the path this test has
always exercised.
- The fixture now lists the decode function's free names and fails with
one clear message if any is not supplied, instead of a `NameError` in
every parametrized case the next time a dependency is added:

```
Qwen3Attention_fast_forward_inference now reads ['decode_qk_norm_rope']; give the harness a CPU stand-in for each
```

The test's rotary stub is the identity (cos 1, sin 0), so it checks
masking and the KV cache rather than RoPE. The fused kernels' own
correctness is covered by `tests/test_decode_fused_rope.py` on CUDA.

## Testing

- CPU torch 2.10: main 64 failed; this branch 64 passed.
- Removing the qwen3 stand-in fails the fixture with the message above.
…FLOAT32, so a stale value from an earlier load no longer refuses it

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

Follow-up from a re-review: the vLLM bf16 gate in llama.py read UNSLOTH_FORCE_FLOAT32, but the FastLanguageModel path never sets it, so a value left at "1" by an earlier FastModel load in the same process (for example Gemma 3) would refuse a later Llama-path load on an Arc A770 with a spurious RuntimeError. The language-model gate now always switches to fp16 with the notice; vision.py keeps the forced-float32 refusal, since that path sets the variable for the model being loaded. New test: a stale "1" no longer fails the language-model gate (fails before this commit, passes after).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 5eb3ada1fc

ℹ️ 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 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: 58b27e42d3

ℹ️ 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/vision.py
and is_vLLM_available()
and xpu_vllm_refuses_bf16()
):
if os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "1":

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 Reject unsafe fp16 fallback for explicit bf16 family loads

When a user explicitly loads Gemma 3/4 or Qwen3.5 with dtype=torch.bfloat16 and fast_inference=True on an XPU where vLLM rejects bf16, this guard still allows the unsafe fp16 fallback. The additional evidence is in FastModel.from_pretrained (unsloth/models/loader.py:2968–2985): it resets UNSLOTH_FORCE_FLOAT32 to "0" and deliberately leaves it unset for an explicit bf16 request, even for these float16-unsafe architectures. Consequently, the new exception protects the default-dtype case but not explicit bf16 loads, which now reach vLLM and the shared HF weights as float16 and can produce NaN gradients. Check the architecture's float16 restriction independently of whether this particular load activated forced-float32 mode.

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 05fcfb3: FastModel now refuses a bf16 fast_inference load of a FORCE_FLOAT32 family on XPU when vLLM refuses bf16, also for explicit bf16 and auto dtype on native-bf16 cards; tests/test_xpu_vllm_refuses_bf16.py covers it.

…ails early instead of dropping to fp16 when vLLM refuses bf16

[skip ci]
@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. Another round soon, please!

Reviewed commit: 05fcfb375b

ℹ️ 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 danielhanchen changed the title XPU: train an explicit bf16 load in bf16 under the fp16 idiom, match vLLM's dtype, agree with the #1894 zoo XPU: train an explicit bf16 load in bf16 under the fp16 idiom, and match vLLM's dtype when it refuses bf16 Oct 25, 2026
@danielhanchen
danielhanchen merged commit cdd0900 into main Oct 25, 2026
28 of 37 checks passed
@danielhanchen
danielhanchen deleted the xpu-explicit-bf16-agreement branch October 25, 2026 09:10
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