From b5d0749568d973b3188d8eb86ce177a24d57258a Mon Sep 17 00:00:00 2001 From: ziyixiong-nv <219238287+ziyixiong-nv@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:51:16 +0000 Subject: [PATCH] [TRTLLM-16622][fix] DeepSeek-V4.1: kernel fixes for 64-bit addressing, PDL asm ordering, mHC boundary alignment and constant zeros Five fixes; each applies wherever its code runs, for every model: - fusedCatFp4 / fusedCatFp8 (FP4 / FP8 index queries of the DSA indexers): the output offsets were 32-bit ints, so from 2^25 / 2^24 rows (tokens x index heads, e.g. 512K / 256K tokens at 64 heads) they overflowed and the kernels stored outside their output. The offsets are 64-bit. - CSA2 glue kernels: tensor1d/tensor2d took CuTe DSL's default unit stride, which some DSL builds type as 32-bit, so element offsets of 2^31 and more (2 GiB of a byte tensor) wrapped. The unit stride is Int64. - CuTe DSL griddepcontrol.wait / launch_dependents had no memory clobber, so LLVM could move ordinary global loads (e.g. .nc loads of a predecessor's output) above the PDL wait and read the output before the predecessor finished. Both asm statements carry "~{memory}", as CUTLASS C++. - The mHC all-reduce boundary is compiled for 16-byte-aligned operands; a contiguous view whose storage offset is not a multiple of 16 bytes (an un-finalized MoE output can be one) faulted on misaligned vector accesses. Such operands are copied to aligned storage first. - CSA2 staging asked for a zero constant of each step's row count and kept one tensor per count it had seen, so a long-running eager context server kept growing its HBM use. Those per-row zeros now come from the shared buffer pool (get_memory_buffers(), reserved while a CUDA graph is captured) and are zeroed per call. Not measured separately. With the memory clobber, a GB300 decode step at attention TP4 (6 decode rows, CUDA graphs) is unchanged: 3.796 ms vs 3.801 / 3.815 ms. The pooled zeros cost one small fill per staging launch without a main mapping. Test: test_mhc_allreduce_boundary_matches_allreduce_then_boundary also feeds the MoE-finalize boundary contiguous views at a one-element storage offset, eager and in CUDA-graph replays, and compares with the all-reduce then the boundary bit for bit. Signed-off-by: ziyixiong-nv <219238287+ziyixiong-nv@users.noreply.github.com> --- cpp/tensorrt_llm/kernels/fusedCatFp4.cu | 2 +- cpp/tensorrt_llm/kernels/fusedCatFp8.cu | 2 +- .../attention/backends/sparse/csa2/kernel.py | 16 ++++++++-- .../_torch/kernels/blackwell/utils.py | 6 ++-- tensorrt_llm/_torch/modules/mhc/mhc_lowm.py | 7 +++++ .../_torch/multi_gpu/test_allreduce.py | 29 +++++++++++++++++++ 6 files changed, 55 insertions(+), 7 deletions(-) diff --git a/cpp/tensorrt_llm/kernels/fusedCatFp4.cu b/cpp/tensorrt_llm/kernels/fusedCatFp4.cu index 8cf81aac1871..f5f3b7ec2adf 100644 --- a/cpp/tensorrt_llm/kernels/fusedCatFp4.cu +++ b/cpp/tensorrt_llm/kernels/fusedCatFp4.cu @@ -154,7 +154,7 @@ __global__ __launch_bounds__(WARP_SIZE* ROWS_PER_BLOCK) void fusedCatFp4Kernel(i uint8_t byte1 = static_cast(c2 | (c3 << 4)); // ---- Stage 6: Store packed bytes (2 bytes per lane) ---- - int base_out = row * (HEAD_DIM / 2) + lane * 2; + int64_t const base_out = static_cast(row) * (HEAD_DIM / 2) + lane * 2; packed_out[base_out + 0] = static_cast(byte0); packed_out[base_out + 1] = static_cast(byte1); diff --git a/cpp/tensorrt_llm/kernels/fusedCatFp8.cu b/cpp/tensorrt_llm/kernels/fusedCatFp8.cu index 98dacf65f0ba..49604843611c 100644 --- a/cpp/tensorrt_llm/kernels/fusedCatFp8.cu +++ b/cpp/tensorrt_llm/kernels/fusedCatFp8.cu @@ -164,7 +164,7 @@ __global__ __launch_bounds__(WARP_SIZE* ROWS_PER_BLOCK) void fusedCatFp8Kernel(_ packed.fp8[2] = quantize(v2); packed.fp8[3] = quantize(v3); - int base_out = row * HEAD_DIM + lane * ELEMS_PER_THREAD; + int64_t const base_out = static_cast(row) * HEAD_DIM + lane * ELEMS_PER_THREAD; *reinterpret_cast(fp8_out + base_out) = packed.u32; if (lane == 0) diff --git a/tensorrt_llm/_torch/attention/backends/sparse/csa2/kernel.py b/tensorrt_llm/_torch/attention/backends/sparse/csa2/kernel.py index a6aec407ca30..00cb70213e23 100644 --- a/tensorrt_llm/_torch/attention/backends/sparse/csa2/kernel.py +++ b/tensorrt_llm/_torch/attention/backends/sparse/csa2/kernel.py @@ -148,11 +148,12 @@ def floor_mod(a: Int64, b: Int64) -> Int64: @cute.jit def tensor1d(ptr: cute.Pointer, count: Int64): - return cute.make_tensor(ptr, cute.make_layout(count)) + # Keep offsets beyond 2 GiB in 64 bits on CuTe DSL builds with 32-bit default strides. + return cute.make_tensor(ptr, cute.make_layout(count, stride=Int64(1))) @cute.jit def tensor2d(ptr: cute.Pointer, rows: Int64, cols: Int64, row_stride: Int64): - return cute.make_tensor(ptr, cute.make_layout((rows, cols), stride=(row_stride, 1))) + return cute.make_tensor(ptr, cute.make_layout((rows, cols), stride=(row_stride, Int64(1)))) @cute.jit def _int32_ptr(address: Int64): @@ -7825,13 +7826,22 @@ def _mapping_args(main_mapping, main_slots, main_logical, count: int, device): ``(page_table, page_size, max_positions, requests, visible)``; otherwise ``main_slots`` already hold physical rows. """ + from tensorrt_llm._torch.memory_buffer_utils import get_memory_buffers + identity = _constant_zeros(device, (1, 1), torch.int32) if main_mapping is None: _check(main_slots is not None, "CSA2 staging requires physical main slots or a mapping") slots = main_slots if main_slots.dtype == torch.int64 else main_slots.long() logical = main_logical if main_logical is not None else slots table_args = (_ptr(identity), 1, 1, 1, 1, 2**62) - zeros = _constant_zeros(device, (count,), torch.int64) + # Pooled rather than a constant per shape: the row count changes from step to step. + zeros = get_memory_buffers().get_buffer( + [count], + torch.int64, + buffer_name="csa2_unmapped_rows", + reserve_buffer=torch.cuda.is_current_stream_capturing(), + ) + zeros.zero_() return ( True, logical, diff --git a/tensorrt_llm/_torch/kernels/blackwell/utils.py b/tensorrt_llm/_torch/kernels/blackwell/utils.py index e9e3ff4f1e48..af1cdac94906 100644 --- a/tensorrt_llm/_torch/kernels/blackwell/utils.py +++ b/tensorrt_llm/_torch/kernels/blackwell/utils.py @@ -57,6 +57,8 @@ from cutlass.cutlass_dsl import T, dsl_user_op TRTLLM_ENABLE_PDL = os.environ.get("TRTLLM_ENABLE_PDL", "1") == "1" +# A memory clobber, as in CUTLASS C++: no memory access moves across the asm. +_PDL_ASM_CONSTRAINTS = "~{memory}" # WAR for CuTeDSL make_ptr implementation @@ -471,7 +473,7 @@ def griddepcontrol_wait(*, loc=None, ip=None) -> None: res=None, operands_=[], asm_string="griddepcontrol.wait;", - constraints="", + constraints=_PDL_ASM_CONSTRAINTS, has_side_effects=True, asm_dialect=llvm.AsmDialect.AD_ATT, loc=loc, @@ -491,7 +493,7 @@ def griddepcontrol_launch_dependents(*, loc=None, ip=None) -> None: res=None, operands_=[], asm_string="griddepcontrol.launch_dependents;", - constraints="", + constraints=_PDL_ASM_CONSTRAINTS, has_side_effects=True, asm_dialect=llvm.AsmDialect.AD_ATT, loc=loc, diff --git a/tensorrt_llm/_torch/modules/mhc/mhc_lowm.py b/tensorrt_llm/_torch/modules/mhc/mhc_lowm.py index 3126783d3562..244d77e135c4 100644 --- a/tensorrt_llm/_torch/modules/mhc/mhc_lowm.py +++ b/tensorrt_llm/_torch/modules/mhc/mhc_lowm.py @@ -411,6 +411,13 @@ def mhc_allreduce_post_collapse_norm( res_out = torch.empty_like(res_flat) quant, quantized, scale_addr = _quantized_outputs(m, hidden, out.device, mxfp8, nvfp4_scale) table = allreduce.prepare(allreduce.nranks * m * hidden * res_flat.element_size()) + # A contiguous offset view can still violate the compiled kernel's 16-byte alignment. + res_flat, post, comb, pre, weight, fc2, scales, perm, shared = [ + t.clone() if t.data_ptr() % 16 else t + for t in (res_flat, post, comb, pre, weight, fc2, scales, perm, shared) + ] + # Unfinalized MoE uses shared as x too; keep the alias to avoid a second copy. + x = shared.view(m, hidden) if moe_key is not None else (x.clone() if x.data_ptr() % 16 else x) fn = _compiled_allreduce_boundary( m, hidden, diff --git a/tests/unittest/_torch/multi_gpu/test_allreduce.py b/tests/unittest/_torch/multi_gpu/test_allreduce.py index a080f43a11cd..ae85f896acf0 100644 --- a/tests/unittest/_torch/multi_gpu/test_allreduce.py +++ b/tests/unittest/_torch/multi_gpu/test_allreduce.py @@ -1255,6 +1255,35 @@ def fused(x, res, post, comb, pre, weight): want = reference(parts.finalize(), *args) for g, w in zip(fused(parts, *args), want): torch.testing.assert_close(g, w, rtol=0, atol=0) + + # Contiguous MoE views can have storage offsets that violate CuTe's alignment. + def offset_copy(value): + storage = torch.empty(value.numel() + 1, + dtype=value.dtype, + device=value.device) + view = storage[1:].view(value.shape) + view.copy_(value) + assert view.is_contiguous() and view.data_ptr() % 16 + return view + + parts = MoeUnfinalized( + **{ + name: offset_copy(getattr(parts, name)) + for name in ('shared', 'fc2', 'scales', 'perm') + }) + want = reference(parts.finalize(), *args) + for g, w in zip(fused(parts, *args), want): + torch.testing.assert_close(g, w, rtol=0, atol=0) + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + captured = fused(parts, *args) + parts.shared.mul_(.875) + want = reference(parts.finalize(), *args) + for _ in range(32): + graph.replay() + torch.cuda.synchronize() + for g, w in zip(captured, want): + torch.testing.assert_close(g, w, rtol=0, atol=0) except Exception: traceback.print_exc() raise