diff --git a/src/granite_switch/kernels/switch_lora_kernel.py b/src/granite_switch/kernels/switch_lora_kernel.py index cb24740d..3219e89a 100644 --- a/src/granite_switch/kernels/switch_lora_kernel.py +++ b/src/granite_switch/kernels/switch_lora_kernel.py @@ -222,8 +222,15 @@ 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) + # 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 mask_n = n_range < N @@ -267,7 +274,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 +299,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 +321,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 +343,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 +365,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 +387,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 +537,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 +559,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 +581,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 +603,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 +625,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 +647,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 @@ -700,9 +707,10 @@ 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. - m_range = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) - h_range = pid_h * BLOCK_N + tl.arange(0, BLOCK_N) + # 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 mask_h = h_range < H base_mask = mask_m[:, None] & mask_h[None, :] @@ -918,8 +926,9 @@ 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) + # 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 mask_n = n_range < N @@ -950,7 +959,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 +981,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 +1003,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 +1025,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 +1047,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 +1069,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 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