fix(grpo): use 64-bit row offsets in the GRPO loss kernels - #192
Open
Lfan-ke wants to merge 1 commit into
Open
Conversation
grpo_kernel and grpo_bwd_kernel index the logits with `pid * V`, where pid comes from tl.program_id and is therefore int32. The product wraps once BL * V exceeds 2^31, so the kernels read — and, in the backward pass, write — outside the tensors. The threshold is reachable in ordinary use: with a 128k vocabulary it is crossed at 16744 tokens, and this function already defaults max_completion_length to 8192. Promote pid to int64 so every derived offset is 64-bit, matching the fix already applied to permute_bwd_kernel. Also correct the import in benchmarks/grpo.py, which named the package `dlblas.kernel` rather than `dlblas.kernels` and so could never run. Signed-off-by: Leo Cheng (heke1228) <chengkelfan@qq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
grpo_kernelandgrpo_bwd_kerneladdress the logits withpid * V, wherepidcomes fromtl.program_idand is int32. The product wraps onceBL * Vexceeds 2^31, so the kernels read — and in the backward pass write — outside the tensors. The launch isgrid = (BL,)withBL, V = new_logits.shape, so nothing else bounds it.The threshold is reachable in ordinary use. With a 128k vocabulary it is crossed at 16744 tokens, and
grpo_loss_triton_optimizedalready defaultsmax_completion_lengthto 8192.Fix
Promote
pidto int64 in both kernels, so every derived offset is 64-bit. This is the same fix already applied topermute_bwd_kernelin #172.Also corrected the import in
benchmarks/grpo.py, which named the packagedlblas.kernelinstead ofdlblas.kernelsand therefore could never run.Verification
The overflow itself needs ~8.6 TB of logits to reproduce with real tensors, so the address arithmetic was reproduced on its own, with the same expression the kernel uses (RTX 3050, triton 3.7.1):
A negative offset is an out-of-bounds access; in
grpo_bwd_kernelit is an out-of-bounds store.No behaviour change below the threshold —
tests/kernels/test_grpo_loss_logits.pygives a bit-identical loss before and after: