From 2cbdeb84a2b7f2c6e878fda678470266256618d3 Mon Sep 17 00:00:00 2001 From: Alexei Karve Date: Sun, 1 Nov 2026 09:39:08 -0500 Subject: [PATCH 1/2] fix(kernels): int64 index offsets in SWITCH LoRA expand to avoid int32 overflow on large intermediate_size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SWITCH fused-LoRA expand kernels compute the packed lora_B element offset as `lb_off_X + a*(N*X) + rX + n_range*X`. These index expressions were int32, so with a large `N` (= intermediate_size) the offset exceeds 2^31 and the kernel issues an out-of-bounds load: switch_lora_kernel.py _accum_slice_delta / _switch_lora_expand_kernel RuntimeError: Triton Error [CUDA]: an illegal memory access was encountered This reproduces on granite-4.1-30b (intermediate_size=32768) for any composed adapter at inference time; compose succeeds, the crash is on the first forward. granite-4.1-3b and -8b (intermediate_size 12800) stay under the int32 limit, which is why only the 30B class is affected. The trigger is intermediate_size, not hidden_size (both 8B and 30B are 4096) or layer count. Fix: promote the index tensor operands to int64 so the whole offset arithmetic is 64-bit — `n_range`/`h_range`/`m_range` and the per-tier `r16..r512` ranges via `.to(tl.int64)` — across the three LBPACK kernels (`_switch_lora_expand_kernel`, `_accum_slice_delta`, `_switch_lora_shrink_expand_kernel`). The `lb_off_*` scalars stay Python ints; promotion happens because a tensor operand in each sum is now int64. (Note: `tl.int64(x)` is not callable — the cast must be `tensor.to(tl.int64)`.) Validated on BlueVela (H200): granite-4.1-3b load+generate unchanged (no regression on the common path), and granite-4.1-30b now loads and generates where it previously hit the illegal memory access; a 12-domain 30B eval sweep then ran with zero illegal-memory failures. Signed-off-by: Alexei Karve --- .../kernels/switch_lora_kernel.py | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/granite_switch/kernels/switch_lora_kernel.py b/src/granite_switch/kernels/switch_lora_kernel.py index cb24740d..84151957 100644 --- a/src/granite_switch/kernels/switch_lora_kernel.py +++ b/src/granite_switch/kernels/switch_lora_kernel.py @@ -222,8 +222,8 @@ def _switch_lora_expand_kernel( return pid_n = tl.program_id(1) - m_range = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) - n_range = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) + m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) + n_range = (pid_n * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M mask_n = n_range < N @@ -267,7 +267,7 @@ def _switch_lora_expand_kernel( # output delta. The static_range unrolls at compile time, so absent tiers # (NA_r == 0) emit no code. Only the tier-16 loop is annotated; tiers 32..512 # are the identical pattern with r and the tier offsets substituted. - r16 = tl.arange(0, 16) + r16 = tl.arange(0, 16).to(tl.int64) for a in tl.static_range(NA_16): if (bitmask >> a) & 1: # adapter (a+1) in this tile? mask_a = (adap_ids == (a + 1)) & mask_m # rows that select it @@ -292,7 +292,7 @@ def _switch_lora_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r32 = tl.arange(0, 32) + r32 = tl.arange(0, 32).to(tl.int64) for a in tl.static_range(NA_32): if (bitmask >> (id_off_32 + a)) & 1: mask_a = (adap_ids == (id_off_32 + a + 1)) & mask_m @@ -314,7 +314,7 @@ def _switch_lora_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r64 = tl.arange(0, 64) + r64 = tl.arange(0, 64).to(tl.int64) for a in tl.static_range(NA_64): if (bitmask >> (id_off_64 + a)) & 1: mask_a = (adap_ids == (id_off_64 + a + 1)) & mask_m @@ -336,7 +336,7 @@ def _switch_lora_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r128 = tl.arange(0, 128) + r128 = tl.arange(0, 128).to(tl.int64) for a in tl.static_range(NA_128): if (bitmask >> (id_off_128 + a)) & 1: mask_a = (adap_ids == (id_off_128 + a + 1)) & mask_m @@ -358,7 +358,7 @@ def _switch_lora_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r256 = tl.arange(0, 256) + r256 = tl.arange(0, 256).to(tl.int64) for a in tl.static_range(NA_256): if (bitmask >> (id_off_256 + a)) & 1: mask_a = (adap_ids == (id_off_256 + a + 1)) & mask_m @@ -380,7 +380,7 @@ def _switch_lora_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r512 = tl.arange(0, 512) + r512 = tl.arange(0, 512).to(tl.int64) for a in tl.static_range(NA_512): if (bitmask >> (id_off_512 + a)) & 1: mask_a = (adap_ids == (id_off_512 + a + 1)) & mask_m @@ -530,7 +530,7 @@ def _accum_slice_delta( # Same per-tier accumulation as the in-place expand kernel; see there for the # annotated tier-16 loop. Tiers 32..512 repeat the pattern. - r16 = tl.arange(0, 16) + r16 = tl.arange(0, 16).to(tl.int64) for a in tl.static_range(NA_16): if (bitmask >> a) & 1: mask_a = (adap_ids == (a + 1)) & mask_m @@ -552,7 +552,7 @@ def _accum_slice_delta( ).to(tl.float32) delta += tl.dot(shrink, lb) - r32 = tl.arange(0, 32) + r32 = tl.arange(0, 32).to(tl.int64) for a in tl.static_range(NA_32): if (bitmask >> (id_off_32 + a)) & 1: mask_a = (adap_ids == (id_off_32 + a + 1)) & mask_m @@ -574,7 +574,7 @@ def _accum_slice_delta( ).to(tl.float32) delta += tl.dot(shrink, lb) - r64 = tl.arange(0, 64) + r64 = tl.arange(0, 64).to(tl.int64) for a in tl.static_range(NA_64): if (bitmask >> (id_off_64 + a)) & 1: mask_a = (adap_ids == (id_off_64 + a + 1)) & mask_m @@ -596,7 +596,7 @@ def _accum_slice_delta( ).to(tl.float32) delta += tl.dot(shrink, lb) - r128 = tl.arange(0, 128) + r128 = tl.arange(0, 128).to(tl.int64) for a in tl.static_range(NA_128): if (bitmask >> (id_off_128 + a)) & 1: mask_a = (adap_ids == (id_off_128 + a + 1)) & mask_m @@ -618,7 +618,7 @@ def _accum_slice_delta( ).to(tl.float32) delta += tl.dot(shrink, lb) - r256 = tl.arange(0, 256) + r256 = tl.arange(0, 256).to(tl.int64) for a in tl.static_range(NA_256): if (bitmask >> (id_off_256 + a)) & 1: mask_a = (adap_ids == (id_off_256 + a + 1)) & mask_m @@ -640,7 +640,7 @@ def _accum_slice_delta( ).to(tl.float32) delta += tl.dot(shrink, lb) - r512 = tl.arange(0, 512) + r512 = tl.arange(0, 512).to(tl.int64) for a in tl.static_range(NA_512): if (bitmask >> (id_off_512 + a)) & 1: mask_a = (adap_ids == (id_off_512 + a + 1)) & mask_m @@ -701,8 +701,8 @@ def _switch_lora_expand_swiglu_kernel( # equals the token count behind AdapIdx/Bitmask, because routing is per token # and the decoder preserves token count. The unmasked Bitmask load relies on # it. - m_range = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) - h_range = pid_h * BLOCK_N + tl.arange(0, BLOCK_N) + m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) + h_range = (pid_h * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M mask_h = h_range < H base_mask = mask_m[:, None] & mask_h[None, :] @@ -918,8 +918,8 @@ def _switch_lora_shrink_expand_kernel( return pid_n = tl.program_id(1) - m_range = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) - n_range = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) + m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) + n_range = (pid_n * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M mask_n = n_range < N @@ -950,7 +950,7 @@ def _switch_lora_shrink_expand_kernel( lb_off_256 = lb_off_128 + NA_128 * N * 128 lb_off_512 = lb_off_256 + NA_256 * N * 256 - r16 = tl.arange(0, 16) + r16 = tl.arange(0, 16).to(tl.int64) for a in tl.static_range(NA_16): if (bitmask >> a) & 1: # adapter (a+1) in this tile? mask_a = (adap_ids == (a + 1)) & mask_m # rows that select it @@ -972,7 +972,7 @@ def _switch_lora_shrink_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r32 = tl.arange(0, 32) + r32 = tl.arange(0, 32).to(tl.int64) for a in tl.static_range(NA_32): if (bitmask >> (id_off_32 + a)) & 1: mask_a = (adap_ids == (id_off_32 + a + 1)) & mask_m @@ -994,7 +994,7 @@ def _switch_lora_shrink_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r64 = tl.arange(0, 64) + r64 = tl.arange(0, 64).to(tl.int64) for a in tl.static_range(NA_64): if (bitmask >> (id_off_64 + a)) & 1: mask_a = (adap_ids == (id_off_64 + a + 1)) & mask_m @@ -1016,7 +1016,7 @@ def _switch_lora_shrink_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r128 = tl.arange(0, 128) + r128 = tl.arange(0, 128).to(tl.int64) for a in tl.static_range(NA_128): if (bitmask >> (id_off_128 + a)) & 1: mask_a = (adap_ids == (id_off_128 + a + 1)) & mask_m @@ -1038,7 +1038,7 @@ def _switch_lora_shrink_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r256 = tl.arange(0, 256) + r256 = tl.arange(0, 256).to(tl.int64) for a in tl.static_range(NA_256): if (bitmask >> (id_off_256 + a)) & 1: mask_a = (adap_ids == (id_off_256 + a + 1)) & mask_m @@ -1060,7 +1060,7 @@ def _switch_lora_shrink_expand_kernel( ).to(tl.float32) delta += tl.dot(shrink, lb) - r512 = tl.arange(0, 512) + r512 = tl.arange(0, 512).to(tl.int64) for a in tl.static_range(NA_512): if (bitmask >> (id_off_512 + a)) & 1: mask_a = (adap_ids == (id_off_512 + a + 1)) & mask_m From 3044b268c36bfdae4e0d88826b7fd1230c210b1f Mon Sep 17 00:00:00 2001 From: yairallouche Date: Mon, 16 Nov 2026 14:32:40 +0200 Subject: [PATCH 2/2] Document the x_ext row-offset overflow and add an int32-limit regression test The int64 casts fix #160, but the term that actually outgrows int32 is the x_ext row offset m * stride_xe_m, not the packed lora_B offset: on granite-4.1-30b the gate/up row width is ~65.5k, so a forward of ~32.8k tokens passes 2^31. The lora_B offset only gets there with sum(NA_r * r) > 2^31 / N, which no realistic checkpoint reaches. Say so in the kernel, so a later cleanup does not drop the m_range casts. The new GPU test drives switch_lora_expand and switch_lora_expand_swiglu at the 30B gate/up width with a last row-tile past the int32 limit, and checks the first and last tiles against a torch reference. It needs ~7 GB of free GPU memory and skips otherwise. Signed-off-by: yairallouche Signed-off-by: Alexei Karve --- .../kernels/switch_lora_kernel.py | 11 +- .../unit/test_switch_kernel_int64_offsets.py | 153 ++++++++++++++++++ 2 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_switch_kernel_int64_offsets.py diff --git a/src/granite_switch/kernels/switch_lora_kernel.py b/src/granite_switch/kernels/switch_lora_kernel.py index 84151957..3219e89a 100644 --- a/src/granite_switch/kernels/switch_lora_kernel.py +++ b/src/granite_switch/kernels/switch_lora_kernel.py @@ -222,6 +222,13 @@ def _switch_lora_expand_kernel( return pid_n = tl.program_id(1) + # int64 index ranges: every pointer sum below includes one of these, so the + # whole offset is computed in 64 bits. The term that outgrows int32 in + # practice is the x_ext row offset m * stride_xe_m: on granite-4.1-30b the + # gate/up row width is ~65.5k, so a forward of ~32.8k tokens passes 2^31 and + # an int32 offset wraps to an illegal address. The packed lora_B offset + # (lb_off + a*N*r + n*r + k) only gets there with sum(NA_r * r) > 2^31 / N. + # The same casts are made in the swiglu and shrink-only kernels below. m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) n_range = (pid_n * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M @@ -700,7 +707,8 @@ def _switch_lora_expand_swiglu_kernel( # Same by-design invariant as _switch_lora_expand_kernel: M (x_ext rows) # equals the token count behind AdapIdx/Bitmask, because routing is per token # and the decoder preserves token count. The unmasked Bitmask load relies on - # it. + # it. int64 ranges guard the m * stride_xe_m row offset against int32 + # overflow (see _switch_lora_expand_kernel). m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) h_range = (pid_h * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M @@ -918,6 +926,7 @@ def _switch_lora_shrink_expand_kernel( return pid_n = tl.program_id(1) + # int64 ranges: see the overflow note in _switch_lora_expand_kernel. m_range = (pid_m * BLOCK_M + tl.arange(0, BLOCK_M)).to(tl.int64) n_range = (pid_n * BLOCK_N + tl.arange(0, BLOCK_N)).to(tl.int64) mask_m = m_range < M diff --git a/tests/unit/test_switch_kernel_int64_offsets.py b/tests/unit/test_switch_kernel_int64_offsets.py new file mode 100644 index 00000000..8890b0c1 --- /dev/null +++ b/tests/unit/test_switch_kernel_int64_offsets.py @@ -0,0 +1,153 @@ +# SPDX-License-Identifier: Apache-2.0 +"""Regression: SWITCH expand kernels must index x_ext with 64-bit offsets. + +The expand kernels address x_ext as ``m * stride_xe_m + col``. On granite-4.1-30b +the shared-MLP gate/up row width is 2 * 32768 + shrink columns (~65.5k), so a +forward of ~32.8k tokens pushes ``m * stride_xe_m`` past 2^31. With int32 index +math the offset wrapped and the kernel hit a CUDA illegal memory access (#160). + +Each test builds an x_ext whose last row-tile sits past the int32 limit, puts an +adapter on the first and the last tile, and checks both against a pure-torch +reference. Only those two tiles hold data; the rest of x_ext is zero, which keeps +the reference cheap. Needs ~7 GB of free GPU memory; skipped otherwise. + +Requires CUDA GPU (Triton kernel). +""" + +import pytest +import torch + +_CUDA_AVAILABLE = torch.cuda.is_available() + +pytestmark = [ + pytest.mark.gpu, + pytest.mark.skipif(not _CUDA_AVAILABLE, reason="requires CUDA GPU (Triton kernel)"), +] + +if _CUDA_AVAILABLE: + from granite_switch.kernels import ( + BLOCK_M, + BLOCK_N, + switch_lora_expand, + switch_lora_expand_swiglu, + ) + +_INT32_LIMIT = 2**31 +_RANK = 16 +# granite-4.1-30b intermediate_size: the shape that overflowed in #160. +_H = 32768 + + +def _rows_past_int32(width): + """Row count (a multiple of BLOCK_M) whose last tile starts past 2^31 elements.""" + first_bad_row = _INT32_LIMIT // width + 1 + return (first_bad_row // BLOCK_M + 2) * BLOCK_M + + +def _require_free_bytes(n): + free, _total = torch.cuda.mem_get_info() + if free < n: + pytest.skip( + f"needs {n / 2**30:.1f} GiB free GPU memory, has {free / 2**30:.1f}" + ) + + +def _active_rows(M, dev): + """Rows of the first and last row-tile: the only rows that select the adapter.""" + first = torch.arange(0, BLOCK_M, device=dev) + last = torch.arange(M - BLOCK_M, M, device=dev) + return torch.cat([first, last]) + + +def _ids_and_bitmask(M, rows, dev): + ids = torch.zeros(M, dtype=torch.int32, device=dev) + ids[rows] = 1 + bitmask = torch.zeros((M + BLOCK_M - 1) // BLOCK_M, dtype=torch.int64, device=dev) + bitmask[rows // BLOCK_M] = 1 + return ids, bitmask + + +def test_expand_in_place_past_int32_row_offset(): + """switch_lora_expand: one rank-16 adapter, N = 2 * 32768 output columns.""" + dev = "cuda" + N = 2 * _H + width = N + _RANK # base columns + one adapter's shrink columns (S=1) + M = _rows_past_int32(width) + assert (M - 1) * width >= _INT32_LIMIT + _require_free_bytes(M * width * 2 + 2**30) + + torch.manual_seed(0) + rows = _active_rows(M, dev) + x_ext = torch.zeros(M, width, device=dev, dtype=torch.bfloat16) + x_ext[rows] = torch.randn(rows.numel(), width, device=dev, dtype=torch.bfloat16) + B = torch.randn(N, _RANK, device=dev, dtype=torch.bfloat16) * 0.1 + before = x_ext[rows].float() + ref = before[:, :N] + before[:, N:] @ B.float().t() + + ids, bitmask = _ids_and_bitmask(M, rows, dev) + tile_slice = torch.zeros(N // BLOCK_N, dtype=torch.int32, device=dev) + slice_col = torch.zeros(6, dtype=torch.int32, device=dev) + slice_col[0] = N # tier-16 shrink columns start right after the base region + switch_lora_expand( + x_ext, + ids, + bitmask, + B.reshape(-1).contiguous(), + tile_slice, + slice_col, + (1, 0, 0, 0, 0, 0), + 1, + BLOCK_N, + N, + ) + torch.cuda.synchronize() + + torch.testing.assert_close(x_ext[rows, :N].float(), ref, rtol=2e-2, atol=2e-2) + # A base row between the two tiles is skipped by the bitmask and stays zero. + assert torch.count_nonzero(x_ext[M // 2]) == 0 + + +def test_expand_swiglu_past_int32_row_offset(): + """switch_lora_expand_swiglu: the 30B shared-MLP gate/up path, H = 32768.""" + dev = "cuda" + N = 2 * _H + width = N + 2 * _RANK # gate|up base columns + gate and up shrink columns + M = _rows_past_int32(width) + assert (M - 1) * width >= _INT32_LIMIT + _require_free_bytes(M * width * 2 + M * _H * 2 + 2**30) + + torch.manual_seed(0) + rows = _active_rows(M, dev) + x_ext = torch.zeros(M, width, device=dev, dtype=torch.bfloat16) + x_ext[rows] = torch.randn(rows.numel(), width, device=dev, dtype=torch.bfloat16) + # Packed lora_B for one adapter, [N_total, r]: gate rows [0, H), up rows [H, 2H). + B = torch.randn(N, _RANK, device=dev, dtype=torch.bfloat16) * 0.1 + xa = x_ext[rows].float() + gate = xa[:, :_H] + xa[:, N : N + _RANK] @ B[:_H].float().t() + up = xa[:, _H:N] + xa[:, N + _RANK :] @ B[_H:].float().t() + ref = torch.nn.functional.silu(gate) * up + + ids, bitmask = _ids_and_bitmask(M, rows, dev) + # build_w_ext order is tier -> adapter -> slice: gate's shrink block, then up's. + slice_col = torch.zeros(2 * 6, dtype=torch.int32, device=dev) + slice_col[0] = N + slice_col[6] = N + _RANK + out = torch.empty(M, _H, device=dev, dtype=torch.bfloat16) + switch_lora_expand_swiglu( + out, + x_ext, + ids, + bitmask, + B.reshape(-1).contiguous(), + slice_col, + (1, 0, 0, 0, 0, 0), + 2, + BLOCK_N, + _H, + N, + ) + torch.cuda.synchronize() + + torch.testing.assert_close(out[rows].float(), ref, rtol=2e-2, atol=3e-2) + # Zero gate/up on a base row gives silu(0) * 0 == 0. + assert torch.count_nonzero(out[M // 2]) == 0