Skip to content

FP8 on XPU: allow on the fly FP8 loading through the torchao path - #19024

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
JoshuaL3000:feature/xpu-torchao-fp8
Nov 23, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
JoshuaL3000:feature/xpu-torchao-fp8

Conversation

@JoshuaL3000

@JoshuaL3000 JoshuaL3000 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Enables on the fly load_in_fp8 on Intel XPU through the torchao path. Currently _get_fp8_mode_and_check_settings only allows CUDA sm90+, so on XPU FastLanguageModel.from_pretrained(..., load_in_fp8 = True) on a model with no pre-quantized FP8 repo fails with the error: On the fly load_in_fp8 requires H100 GPUs or after. XPU is already supported on torchao's FP8 hardware check for rowwise and 1x128/128x128 block scaling.

Changes

unsloth/models/loader_utils.py, _get_fp8_mode_and_check_settings:

  • XPU skips the CUDA capability check when fast_inference = False.
  • XPU with fast_inference = True raises needs fast_inference = False. (vLLM's on the fly FP8 on XPU is not validated in this PR)
  • XPU requires torchao 0.18.0+, the version validated here.

tests/test_fp8_onthefly_device_gate.py - New tests on a mocked device. Tests:

  • XPU is admitted for rowwise, and for block on transformers 4
  • Refused with fast_inference = True
  • Refused for both modes if torchao < 0.18.0
  • A block mode Float8Tensor round trips through torchao's safetensors loader
  • CUDA behavior and the refusal of other devices are unchanged

Behavior on CUDA

Unchanged. CUDA still needs sm90+ and torchao 0.15.0+.

Tests

Unit Tests

  • tests/test_fp8_onthefly_device_gate.py: 16 passed.
  • tests/test_fp8_offline_quantize_gate.py and tests/test_fp8_device_gate.py: 50 passed.

Ran 10 LoRA SFT steps using unsloth/alpaca-cleaned dataset on an Intel Arc Pro B60 with unsloth/Qwen2.5-0.5B-Instruct, which has no FP8 repo, on transformers 5 and torchao 0.18. The script sets load_in_fp8 = True for rowwise torchao path.

Environment:

  • torch 2.13.0+xpu
  • triton-xpu 3.7.2
  • torchao 0.18.0
  • transformers 5.17.0
  • trl 0.24.0
  • peft 0.20.0
  • unsloth 2026.11.10
  • unsloth_zoo 2026.11.9

Summary:

  • torchao quantizes 168 weights to Float8Tensor
  • The run completed with lower max allocated memory per training step than a bf16 run (1961 MB vs 2301 MB) and lower tokens/s (477 vs 715, mean of steps 2–10).
Run results (FP8 vs bf16)
Step Tokens FP8 loss bf16 loss FP8 grad_norm bf16 grad_norm FP8 tok/s bf16 tok/s FP8 peak MB bf16 peak MB
1 256 1.3468 1.3691 2.5264 2.2000 55.0 53.4 1952 2292
2 228 1.5504 1.5525 2.5635 2.4810 461.4 706.6 1816 2156
3 101 2.2823 2.2234 3.0245 2.9177 189.1 276.5 1329 1669
4 77 2.8950 2.7296 4.9330 4.7558 161.1 228.9 1288 1628
5 203 1.7920 1.7460 1.8521 1.7878 494.6 802.0 1686 2027
6 256 2.2839 2.2406 1.6812 1.6677 618.1 1061.1 1961 2301
7 189 1.3214 1.2616 1.8652 1.9064 449.1 583.2 1614 1954
8 187 1.8790 1.8541 2.1095 2.2503 598.6 893.0 1604 1944
9 256 1.4682 1.4243 1.4809 1.4056 826.5 1317.4 1961 2301
10 256 1.6347 1.5777 1.6285 1.5373 841.3 1256.3 1961 2301
Summary 476.6 (mean, steps 2-10) 715.4 (mean, steps 2-10) 1961 (max) 2301 (max)

10 LoRA SFT steps on transformers 4.57.3, with the same model, dataset, LoRA settings and other packages as above:

  • load_in_fp8 = "block": Unsloth saves the quantized checkpoint as pytorch_model.bin, reloads it with 168 Float8Tensor weights and trains (loss 1.3337 -> 1.6110).
  • load_in_fp8 = True: 168 Float8Tensor weights, trains (loss 1.3468 -> 1.6234).

Block mode on transformers 5 (all devices)

Observed with transformers 5.17.0 / 5.18.0 and torchao 0.18.0: load_in_fp8 = "block" failed to reload its quantized checkpoint with Failed to find class PerBlock, on CUDA as well as XPU and CPU. transformers 5 writes model.safetensors even with safe_serialization = False, and torchao's safetensors loader allow-lists PerRow but not PerBlock (torchao 0.15 through main). _get_fp8_mode_and_check_settings now registers PerBlock in torchao's ALLOWED_CLASSES (idempotent, skipped when the module is absent), so block mode loads and trains on transformers 5 on every device.

Verified on a Colab RTX PRO 6000 (sm120), torch 2.14.1, transformers 5.18.0, torchao 0.18.0, unsloth/Qwen2.5-0.5B-Instruct, 3 LoRA steps:

Arm Mode Result Float8 weights Losses
main block conversion error (Failed to find class PerBlock) - -
main row ok 168 1.5045, 1.2483, 1.0502
this PR block ok 168 1.5215, 1.2635, 1.0550
this PR row ok 168 1.5045, 1.2483, 1.0502

@danielhanchen danielhanchen self-assigned this Nov 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T09:38:48.685765Z 1bcfc0b 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

Copy link
Copy Markdown

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

Reviewed commit: 1bcfc0b9a3

ℹ️ 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 that referenced this pull request Nov 23, 2026
…ut, kill on timeout (#19044)

## Summary

`(Python 3.13, diffusion)` intermittently fails
`studio/backend/tests/test_diffusion_ddp_effective_precision.py::test_real_gloo_two_ranks[fp8,fp8-SAME]`
with `subprocess.TimeoutExpired` after 180 s, for example the staging
run for #19024 on a base that already has #19027. Locally the test takes
about 1.4 s per case, so this is a hang, not a slow runner.

The log has nothing to go on: the two ranks' stderr is not captured, a
rank stuck in a Gloo collective waits out Gloo's 30 minute default, and
on timeout the children are left running.

## Change

`studio/backend/tests/test_diffusion_ddp_effective_precision.py` only;
no product code changes, and the checks are the same.

- Child: `init_process_group(..., timeout=timedelta(seconds=60))`, so a
stuck collective raises inside the rank with its own error instead of
hanging.
- Parent: each rank writes stdout and stderr to files under `tmp_path`
instead of pipes, so one rank cannot block on a full pipe while the
other is being waited on.
- Both ranks share one 180 s deadline. On expiry both are killed and the
test fails with each rank's return code, stdout and stderr, so the next
occurrence names the stuck rank and where it was.
- A nonzero exit also reports both ranks' output.

This bounds the hang and makes it diagnosable; it does not claim a root
cause, since no failing run has captured the ranks' output yet.

## Testing

- The file: 13 passed, also under `-n 2`.
- Sabotage, rank 1 stalled before `init_process_group` (deadline
shortened for the check): the test fails after the deadline with `rank 0
rc=-9` / `rank 1 rc=-9` and both outputs, and no child process is left
behind.
- Sabotage, rank 1 exits right after init: rank 0 reports the closed
connection and the test fails on the verdict within seconds.
@danielhanchen
danielhanchen merged commit 1a36c8f into unslothai:main Nov 23, 2026
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.

2 participants