Skip to content

Guard FP8 Triton launches with tensor device context - #6888

Merged
oobabooga merged 6 commits into
unslothai:mainfrom
ramisworld:codex/fix-fp8-device-context
Jul 8, 2026
Merged

oobabooga merged 6 commits into
unslothai:mainfrom
ramisworld:codex/fix-fp8-device-context

Conversation

@ramisworld

Copy link
Copy Markdown
Contributor

Summary

Fixes #6831.

unsloth/kernels/fp8.py launches several FP8 Triton kernels without first selecting the device where the input tensor lives. In a multi-GPU device_map setup, the current CUDA device can still be cuda:0 while an FP8 tensor is on cuda:1 or another device. That can make Triton launch against the wrong device and produce silent corruption, NaNs, or illegal memory access.

This adds a local FP8 device-context helper and wraps the FP8 Triton-style launch paths so they run under the tensor's CUDA/XPU device context.

Changes

  • Add _fp8_triton_device_context in fp8.py.
  • Guard weight_dequant_kernel in weight_dequant_block.
  • Guard act_quant_kernel in act_quant.
  • Guard _w8a8_block_fp8_matmul in w8a8_block_fp8_matmul_triton.
  • Guard triton_quantize_fp8_block in the FBGEMM block path.
  • Add CPU-safe AST/helper tests plus skip-gated CUDA runtime tests.

The helper is local instead of importing torch_gpu_device from unsloth.kernels.utils because utils.py already imports fp8.py, so importing back from utils.py would risk a circular import.

Tests

Passed locally:

git diff --check
python3 -m py_compile unsloth/kernels/fp8.py tests/test_fp8_device_context.py
pytest -q tests/test_fp8_device_context.py -rs
6 passed, 3 skipped

Repo default pytest selection:

pytest -q -rs
169 passed

Larger bounded CPU/static suite:

301 passed, 3 skipped

The three skipped tests are CUDA/Triton runtime checks that require a 2+ GPU CUDA environment. I could not run those locally because this machine has no CUDA GPUs and Triton is not installable on macOS, but the tests are included for maintainers/CI with suitable hardware.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a device context manager, _fp8_triton_device_context, to ensure that Triton FP8 kernels are executed on the correct GPU or XPU device in multi-device environments. It wraps several kernel launches with this context and adds a comprehensive test suite in tests/test_fp8_device_context.py to verify correct device selection and execution behavior. I have no additional feedback to provide as the implementation is clean and well-tested.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: cf20bb697d

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

@ramisworld
ramisworld marked this pull request as ready for review July 7, 2026 01:19
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@ramisworld

Copy link
Copy Markdown
Contributor Author

I looked at the failing Core (HF=4.57.6 + TRL<1) check appears unrelated to this FP8 change. It fails in unsloth_zoo MoE/bnb4bit tests with WeightConverter.target_patterns missing, and the same Core workflow is currently failing on the upstream main as well. The FP8 specific changes and added tests are passing.

@ramisworld

Copy link
Copy Markdown
Contributor Author

Noting overlap with #6912, which appears to address the same #6831 FP8 device-context root cause and has useful validation on the original 8x H200 repro.

This PR takes a slightly broader approach: it adds regression tests, uses a local device-context helper for CUDA/XPU/single-device behavior and also guards the FBGEMM triton_quantize_fp8_block path.

Happy to adapt this PR to the preferred style @danielhanchen or split out the tests as a follow-up.

@oobabooga

Copy link
Copy Markdown
Member

Verified your fix live on a 2-GPU box. On main, a weight_dequant with the tensor on a non-current device reproduces the exact #6831 failure (the Triton cross-device pointer error). With your guard it runs clean: output on the tensor's device, current device untouched. Suite green.

Folded in two follow-ups on top of your commits: a capability skip so the act_quant runtime test skips on non-FP8 hardware instead of failing at kernel compile, and a guard on the torchao block-matmul path (the preferred backend) the diff left uncovered. Checked that one against real torchao: unguarded it crashes the same way; guarded, it runs clean. Your original change is unchanged. Merging.

@oobabooga
oobabooga merged commit 81f789b into unslothai:main Jul 8, 2026
47 checks passed
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.

silent multi-GPU corruption, with the layer-40 repro and before/after evidence

3 participants