Repository navigation
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
There was a problem hiding this comment.
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.
| with torch.cuda.device(x.device): | ||
| weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE = block_size) |
There was a problem hiding this comment.
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.
| 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) |
| with torch.cuda.device(x.device): | ||
| act_quant_kernel[grid](x, y, s, BLOCK_SIZE = block_size) |
There was a problem hiding this comment.
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.
| 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) |
| 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, | ||
| ) |
There was a problem hiding this comment.
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,
)|
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 |
|
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. |
|
Thanks for the independent repro and for acknowledging the original investigation - much appreciated. Glad the fix landed. |
What
Triton kernel launches in
unsloth/kernels/fp8.pyrun on whatever CUDA device iscurrent, not the device the tensors live on. This PR wraps the three launch sites
(
weight_dequant_kernel,act_quant_kernel,_w8a8_block_fp8_matmul) inwith torch.cuda.device(<tensor>.device):.Why
On a multi-GPU
device_mapmodel, layer weights/activations live oncuda:1..cuda:Nwhile the current CUDA device typically stays
cuda:0. When Triton launches a kernelunder the wrong current device, the kernel accesses pointers belonging to another GPU.
Depending on timing/topology this either:
ValueError: Pointer argument (at 0) cannot be accessed from Triton, orwhich surfaces as nondeterministic
loss=nanor 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_mapspreading 78 layersacross GPUs, attention-only LoRA:
cuda:0) always clean; layer 40 (oncuda:4)crashed with the Triton pointer error or returned corrupted values.
repeated forwards with zero dropout) and intermittently went NaN.
torch.cuda.device(x.device): layer 40 output becamebit-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
single-GPU behavior and performance are unchanged.
act_quantandw8a8_block_fp8_matmulare the same launch pattern; they get the sameguard preventively so the FP8 path is consistent.
multi-GPU FP8 MoE workload (IMA crash, with the probe repro #6830 / silent multi-GPU corruption, with the layer-40 repro and before/after evidence #6831).
Fixes #6831