Skip to content

[XPU] Enable FP8 training on XPU - #12535

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
JoshuaL3000:feature/xpu-enable-fp8
Oct 4, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
JoshuaL3000:feature/xpu-enable-fp8

Conversation

@JoshuaL3000

Copy link
Copy Markdown
Contributor

What This PR Does

Unsloth already supports FP8, this PR just adds an XPU path to unblock Intel GPUs when training with load_in_fp8=True. The results from the Validation section show that FP8 training uses less memory, but has lower token/s compared to bf16. XPU does not support fbgemm, so this only verifies that XPU reaches the fallback path with less VRAM overhead.
To verify the FP8 on XPU, this PR also adds Intel XPU compatibility for FP8 related tests found under tests/. The results are shown under Validation section.

Changes

In verify_fp8_support_if_applicable, added the xpu option:

if quant_method in ["fbgemm_fp8", "fp8"] and DEVICE_TYPE not in ("cuda", "xpu"):

From the code, the XPU paths will follow:
Path 1 - Rowwise path
Case: unsloth/Llama-3.2-1B-FP8-Dynamic — uses quant_method="compressed-tensors", and weights strategy channel
To verify this path, the env var UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1 was set for the FP8 kernels opt-in. This var was added in commit 9d15a59
FbgemmFp8Linear_matmul class calls _probe_fp8_rowwise_backend function which resolves rowwise backend to torch._scaled_mm since XPU does not have fbgemm_gpu available.

Path 2 - Block path
Case: unsloth/Qwen3-0.6B with load_in_fp8=True
In FP8BlockQuantLinear class and runs act_quant Triton path.

Validation

FP8 Trace Test

I have included the run results for a trivial fp8 trace test to capture some predefined ops (found in unsloth/kernels/fp8.py) using the following models on XPU.
No validation results for NVIDIA A100 because it does not meet the compute requirement to support fp8.

Environment:

  • Hardware: Intel Arc Pro B60 24GB (XPU) x4
  • torch: 2.14.0+xpu
  • triton-xpu: 3.8.0
  • triton: 3.8.0
  • unsloth: 2026.9.10
  • unsloth-zoo: 2026.9.8
  • transformers: 5.18.0
  • trl: 0.24.0
Model unsloth/Qwen3-0.6B (with load_in_fp8=True) unsloth/Llama-3.2-1B-FP8-Dynamic (with UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1)
quant_method fp8 compressed-tensors
Strategy / scale block [128, 128] channel (rowwise)
FP8 weight tensors 196 112
Path Block Rowwise, _scaled_mm
FP8BlockQuantLinear.forward 224 –
act_quant 224 –
fp8_block_matmul 224 –
_blockwise_weight_dequant_any_shape 168 –
weight_dequant_block 168 –
weight_dequant 168 128
fbgemm_fp8_linear – 128
FbgemmFp8Linear_matmul.forward – 128
torch._scaled_mm – 112
torch_matmul – 16

FP8 SFT LoRA

To verify FP8 path correctly fallbacks on XPU, I ran a simple SFT LoRA finetuning script on both models to cover both forward paths.
Both paths show that FP8 has lower peak memory and lower token/s.

Config:

  • max_steps - 10
  • batch size - 1
  • dataset - unsloth/alpaca-cleaned

Results for unsloth/Qwen3-0.6B (block path):

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.3486 1.3395 1.2535 1.2615 54.4 56.0 920 1340
2 211 1.5603 1.5463 1.5813 1.5658 309.5 406.8 901 1321
3 84 2.5006 2.4746 2.9502 2.9058 129.4 143.4 820 1240
4 60 3.6653 3.6752 3.6980 3.8904 91.6 117.6 805 1225
5 186 2.0034 1.9888 1.5501 1.5485 282.6 370.2 885 1305
6 256 2.3632 2.3473 1.0139 1.0313 393.5 779.2 930 1350
7 172 1.5732 1.5478 1.2594 1.2494 262.6 453.3 876 1296
8 170 2.3409 2.3210 1.7792 1.7498 260.1 485.8 875 1295
9 256 1.8596 1.8497 1.3360 1.3217 395.0 763.7 930 1350
10 256 1.9978 1.9795 1.5461 1.5353 394.8 763.6 930 1350
Summary 930 (max) 1350 (max)

Results for unsloth/Llama-3.2-1B-FP8-Dynamic with UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1 (rowwise path):

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.0761 1.0725 0.5165 0.5117 46.3 37.1 1628 2556
2 200 1.2845 1.2799 0.6310 0.6171 134.3 634.0 1607 2535
3 77 1.7980 1.8153 0.9616 0.9671 105.5 108.3 1588 2466
4 54 2.8637 2.8781 1.8419 1.8372 54.0 133.2 1585 2453
5 179 1.5299 1.5254 0.7116 0.7143 299.9 301.2 1601 2523
6 256 1.9236 1.9205 0.6250 0.6154 858.0 1180.2 1639 2567
7 162 1.3682 1.3696 0.8021 0.8047 514.5 697.7 1599 2514
8 160 1.6859 1.6920 0.8061 0.8276 515.6 695.6 1598 2513
9 256 1.5457 1.5277 0.7222 0.7227 860.3 1203.1 1639 2567
10 256 1.5293 1.5110 0.7306 0.7349 894.1 1214.6 1639 2567
Summary 1639 (max) 2567 (max)

FP8 Test Validation

Added Intel XPU compatibility for existing FP8 related tests and ran them to verify FP8 does not raise any errors on XPU.
Modified tests files are validated by running the pytest command:

ZE_AFFINITY_MASK=3 pytest -v -m "not server and not slow" \
  tests/test_fp8_ragged_block_dequant.py \
  tests/test_compressed_tensors_fp8_kernel_route.py \
  tests/test_fp8_compile_fullgraph.py \
  tests/test_fp8_device_context.py \
  tests/test_fp8_fbgemm_block_shapes.py \
  tests/test_fp8_leftover_dequant_16bit.py \
  tests/test_fp8_rowwise_backend.py \
  tests/test_fp8_to_nf4_load.py \
  tests/test_fp8_transposed_weight.py \
  tests/test_fp8linear_high_precision_weight.py \
  tests/test_fp8linear_module_block_size.py \
  tests/test_modelopt_fp8_loading.py \
  tests/test_fp8_restore_dropped_scale.py \
  tests/test_fp8_tiny_e8m0.py \
  tests/test_fp8_unscaled_checkpoint_linears.py \
  tests/python/test_get_lora_parameters_fp8_block_size.py \
  tests/python/test_get_lora_parameters_bias_fp8_block_size.py

Summary:
200 passed, 24 skipped

All skipped tests are expected as CUDA-only tests:

  • tests/test_compressed_tensors_fp8_kernel_route.py::test_decode_gemv_matches_the_dequantized_matmul x18 (gemv is CUDA-only)
  • tests/test_compressed_tensors_fp8_kernel_route.py::test_decode_gemv_refuses_what_it_cannot_compute (gemv is CUDA-only)
  • tests/test_fp8_rowwise_backend.py::test_every_backend_matches_reference[fbgemm] (fbgemm is CUDA-only)
  • tests/test_fp8_rowwise_backend.py::test_quantize_matches_fbgemm(fbgemm is CUDA-only)
  • tests/test_fp8_fbgemm_block_shapes.py::test_output_tile_grid_battery_matches_reference (fbgemm is CUDA-only)
  • tests/test_modelopt_fp8_loading.py::test_transformers_without_per_tensor_fp8_keep_the_config (reason: this transformers loads per-tensor fp8)

Remaining Work

  • Checking if gemv can be supported in XPU as it is currently CUDA gated in can_use_fp8_rowwise_gemv. My plan is to benchmark the speeds with the scaled_mm and dequant + matmul fallback.

Please let me know if any additional info is needed to verify this, thanks.

@danielhanchen

Copy link
Copy Markdown
Member

Two reds on head 34e4136e, one from this PR and one from main: (1) Repo tests (CPU, studio): tests/python/test_accelerator_skip_guards.py flags three tests in test_modelopt_fp8_loading.py (lines 441, 654, 865) that ask for a CUDA device but gate on has_real_accelerator(), which is also true on an XPU-only or Ascend-only host; gating them on has_real_cuda() clears it. (2) Repo tests (CPU, rest): test_studio_evaluated_unions_do_not_grow is red on main since #11491; #12556 fixes it.

@danielhanchen

Copy link
Copy Markdown
Member

The new Core / Core zoo reds are main's: test_studio_evaluated_unions_do_not_grow is fixed on main by #12556 (merged), and the Core zoo test_mlx_generate_metal.py NAX scope failures are unsloth-zoo main's, fixed by unslothai/unsloth-zoo#1547. The test_accelerator_skip_guards.py finding from my earlier comment is still this PR's.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the FP8 device check in unsloth/models/_utils.py still rejects XPU on main, so this unblocks load_in_fp8 on Intel GPUs. Could you switch the three modelopt FP8 tests to gate on has_real_cuda() so the skip-guard check goes green?

JoshuaL3000 and others added 3 commits October 3, 2026 23:36
@JoshuaL3000
JoshuaL3000 force-pushed the feature/xpu-enable-fp8 branch from f2d49c3 to ae965ec Compare October 4, 2026 06:37
@JoshuaL3000

Copy link
Copy Markdown
Contributor Author

Ok, switched the three modelopt FP8 tests to gate on has_real_cuda().

@danielhanchen danielhanchen self-assigned this Oct 4, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #12535, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#387.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror c94799a28 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on unsloth/models/_utils.py (on the mirror)

@@ -6044,10 +6044,10 @@ def make_trainable(self):
 
 def verify_fp8_support_if_applicable(model_config):
     quant_method = get_quant_type(model_config)
-    if quant_method in ["fbgemm_fp8", "fp8"] and DEVICE_TYPE != "cuda":
+    if quant_method in ["fbgemm_fp8", "fp8"] and DEVICE_TYPE not in ("cuda", "xpu"):

P1 Badge Enable XPU in the on-the-fly FP8 gate

When an XPU user passes load_in_fp8=True for a model that has no pre-quantized alias, loader.py:1019-1030 calls _get_fp8_mode_and_check_settings, whose hardware check at loader_utils.py:3124-3130 still requires torch.cuda.is_available(), CUDA, and compute capability 9.0. Consequently this new allowance only helps already-quantized checkpoints; the advertised on-the-fly XPU flow still immediately raises the H100-only error. Extend that hardware check to accept the supported XPU configuration as well.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Out of scope for this PR: the XPU flow it enables and validated is pre-quantized FP8 checkpoints (unsloth/Qwen3-0.6B with load_in_fp8=True resolves through the mapper to unsloth/Qwen3-0.6B-FP8). On-the-fly quantization on XPU still gets an explicit refusal, not a crash or wrong result, and enabling it needs torchao offline FP8 quantization validated on XPU hardware, so it belongs in a follow-up.

Round 2: reviewed mirror 8d52b9c458 (this PR's d35058aab)

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: 8d52b9c458

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 8d52b9c458 (this PR's d35058aab).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

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.

2 participants