Skip to content

fix(fp8): guard Triton kernel launches with the tensor's CUDA device … - #6912

Closed
xvdev09 wants to merge 1 commit into
unslothai:mainfrom
xvdev09:fix/fp8-multi-gpu-device-ctx
Closed

xvdev09 wants to merge 1 commit into
unslothai:mainfrom
xvdev09:fix/fp8-multi-gpu-device-ctx

Conversation

@xvdev09

@xvdev09 xvdev09 commented Jul 6, 2026

Copy link
Copy Markdown

What

Triton kernel launches in unsloth/kernels/fp8.py run on whatever CUDA device is
current, not the device the tensors live on. This PR wraps the three launch sites
(weight_dequant_kernel, act_quant_kernel, _w8a8_block_fp8_matmul) in
with torch.cuda.device(<tensor>.device):.

Why

On a multi-GPU device_map model, layer weights/activations live on cuda:1..cuda:N
while the current CUDA device typically stays cuda:0. When Triton launches a kernel
under the wrong current device, the kernel accesses pointers belonging to another GPU.
Depending on timing/topology this either:

  • crashes hard: ValueError: Pointer argument (at 0) cannot be accessed from Triton, or
  • corrupts silently: the dequantized weights come back as garbage/NaN with no error,
    which surfaces as nondeterministic loss=nan or unstable loss during training.

The silent case is the dangerous one — training appears to run but the FP8 dequant path
is producing wrong weights for every layer not on cuda:0.

Repro / evidence (from #6831)

GLM-5.2 FP8 (block-quantized MoE), 8x H200, explicit device_map spreading 78 layers
across GPUs, attention-only LoRA:

  • Per-layer dequant probe: layer 3 (on cuda:0) always clean; layer 40 (on cuda:4)
    crashed with the Triton pointer error or returned corrupted values.
  • Same-batch full-model forward: loss wobbled nondeterministically (0.157→0.198 across
    repeated forwards with zero dropout) and intermittently went NaN.
  • After wrapping the launch in torch.cuda.device(x.device): layer 40 output became
    bit-identical to the layer-3 reference path, and the same batch produced a stable
    loss of 0.077 (down from a corrupted 0.15–0.30/NaN) across runs.

Notes

Fixes #6831

@xvdev09
xvdev09 requested a review from Datta0 as a code owner July 6, 2026 18:04
@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.

@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 wraps Triton kernel launches in unsloth/kernels/fp8.py with torch.cuda.device to ensure they execute on the correct tensor device in multi-GPU setups. The reviewer suggests using the existing torch_gpu_device context manager from unsloth/kernels/utils.py instead of torch.cuda.device directly. This change ensures compatibility with other backends like Intel XPU, avoids overhead on single-GPU setups, and should be imported locally within the functions to prevent circular dependencies.

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.

Comment thread unsloth/kernels/fp8.py
Comment on lines +101 to +102
with torch.cuda.device(x.device):
weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE = block_size)

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.

medium

Using torch.cuda.device directly assumes the device is always a CUDA/HIP device. However, Unsloth supports other backends such as Intel XPU (as seen in unsloth/kernels/utils.py). To maintain compatibility and avoid potential runtime errors on non-CUDA platforms, we should use the existing torch_gpu_device context manager abstraction defined in unsloth/kernels/utils.py.

Furthermore, torch_gpu_device is optimized to return a nullcontext when DEVICE_COUNT <= 1, which avoids the overhead of entering a device context manager on single-GPU setups.

Since unsloth/kernels/utils.py imports from unsloth/kernels/fp8.py, we should import torch_gpu_device inside the function to prevent circular dependency issues.

Suggested change
with torch.cuda.device(x.device):
weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE = block_size)
from .utils import torch_gpu_device
with torch_gpu_device(x.device):
weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE = block_size)

Comment thread unsloth/kernels/fp8.py
Comment on lines +156 to +157
with torch.cuda.device(x.device):
act_quant_kernel[grid](x, y, s, BLOCK_SIZE = block_size)

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.

medium

Using torch.cuda.device directly assumes the device is always a CUDA/HIP device. However, Unsloth supports other backends such as Intel XPU (as seen in unsloth/kernels/utils.py). To maintain compatibility and avoid potential runtime errors on non-CUDA platforms, we should use the existing torch_gpu_device context manager abstraction defined in unsloth/kernels/utils.py.

Furthermore, torch_gpu_device is optimized to return a nullcontext when DEVICE_COUNT <= 1, which avoids the overhead of entering a device context manager on single-GPU setups.

Since unsloth/kernels/utils.py imports from unsloth/kernels/fp8.py, we should import torch_gpu_device inside the function to prevent circular dependency issues.

Suggested change
with torch.cuda.device(x.device):
act_quant_kernel[grid](x, y, s, BLOCK_SIZE = block_size)
from .utils import torch_gpu_device
with torch_gpu_device(x.device):
act_quant_kernel[grid](x, y, s, BLOCK_SIZE = block_size)

Comment thread unsloth/kernels/fp8.py
Comment on lines +282 to +308
with torch.cuda.device(A.device):
_w8a8_block_fp8_matmul[grid](
A,
B,
C,
As,
Bs,
M,
N,
K,
block_n,
block_k,
A.stride(-2),
A.stride(-1),
B.stride(1),
B.stride(0),
C.stride(-2),
C.stride(-1),
As.stride(-2),
As.stride(-1),
Bs.stride(1),
Bs.stride(0),
BLOCK_SIZE_M = BLOCK_SIZE_M,
BLOCK_SIZE_N = BLOCK_SIZE_N,
BLOCK_SIZE_K = BLOCK_SIZE_K,
GROUP_SIZE_M = 8,
)

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.

medium

Using torch.cuda.device directly assumes the device is always a CUDA/HIP device. However, Unsloth supports other backends such as Intel XPU (as seen in unsloth/kernels/utils.py). To maintain compatibility and avoid potential runtime errors on non-CUDA platforms, we should use the existing torch_gpu_device context manager abstraction defined in unsloth/kernels/utils.py.

Furthermore, torch_gpu_device is optimized to return a nullcontext when DEVICE_COUNT <= 1, which avoids the overhead of entering a device context manager on single-GPU setups.

Since unsloth/kernels/utils.py imports from unsloth/kernels/fp8.py, we should import torch_gpu_device inside the function to prevent circular dependency issues.

    from .utils import torch_gpu_device
    with torch_gpu_device(A.device):
        _w8a8_block_fp8_matmul[grid](
            A,
            B,
            C,
            As,
            Bs,
            M,
            N,
            K,
            block_n,
            block_k,
            A.stride(-2),
            A.stride(-1),
            B.stride(1),
            B.stride(0),
            C.stride(-2),
            C.stride(-1),
            As.stride(-2),
            As.stride(-1),
            Bs.stride(1),
            Bs.stride(0),
            BLOCK_SIZE_M = BLOCK_SIZE_M,
            BLOCK_SIZE_N = BLOCK_SIZE_N,
            BLOCK_SIZE_K = BLOCK_SIZE_K,
            GROUP_SIZE_M = 8,
        )

@xvdev09

xvdev09 commented Jul 7, 2026 •

Copy link
Copy Markdown
Author

For context: this implements the fix from my original investigation in #6831 — verified on the actual failing workload (GLM-5.2 FP8 block-quantized MoE, 8x H200, 78-layer device_map). After the guard, the layer-40 (cuda:4) dequant output is bit-identical to the cuda:0 reference path, and same-batch loss stabilizes at 0.077 vs the corrupted 0.15–0.30/NaN before. Diff kept intentionally minimal, same style as the merged #6884: plain torch.cuda.device(<tensor>.device) at the three launch sites, no new helpers.

@oobabooga

Copy link
Copy Markdown
Member

Thanks for this, and especially for the original #6831 investigation and the 8x H200 repro. That diagnosis is what both this and #6888 build on. I reproduced the bug and the fix independently on a 2-GPU box, so the root cause and the guard are confirmed either way.

Going with #6888 as the superset: it guards the same three launches, plus the FBGEMM quant and torchao block-matmul paths this one leaves untouched, and adds XPU handling and tests. Your fix here is correct; #6888 just covers more, so I am closing this as superseded. Thanks again.

@oobabooga oobabooga closed this Jul 8, 2026
@xvdev09

xvdev09 commented Jul 9, 2026

Copy link
Copy Markdown
Author

Thanks for the independent repro and for acknowledging the original investigation - much appreciated. Glad the fix landed.

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