TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

[Bug]: causal_conv1d_triton overwrites conv_state while the convolution is still reading it

Open truong-v opened this issue 3 months ago • 2 comments

System Info

  • GPU: 1x NVIDIA B200 (sm_100), 183 GB
  • TensorRT-LLM: main at commit e61a6e93c9f09fe234a129dc67dcd42ce6a5aa32 (kernel unchanged since it was added)
  • torch 2.11.0+cu130, triton 3.6.0, CUDA 13.0, NVIDIA driver 595.71.05
  • OS: Linux x86_64, Python 3.12

Who can help?

@tomeras91 (Mamba/GDN modules)

Information

  • [x] The official example scripts
  • [ ] My own modified scripts

Tasks

  • [x] An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • [ ] My own task or dataset (give details below)

Reproduction

Both Triton kernels in tensorrt_llm/_torch/modules/mamba/causal_conv1d_triton.py read the conv_state history taps in STEP 1 with a 1-D [BLOCK_N] thread layout, and then overwrite those same cells in STEP 2 from a 2-D [NP2_STATELEN, BLOCK_N] tile. The two layouts assign different threads to a given feature, so the thread that overwrites a cell is not the thread that read it, and nothing in the program orders the two steps: the three tl.debug_barrier() calls that did are commented out — lines 197 and 231 in _causal_conv1d_fwd_kernel, line 725 in _causal_conv1d_update_kernel.

https://github.com/NVIDIA/TensorRT-LLM/blob/e61a6e93c9f09fe234a129dc67dcd42ce6a5aa32/tensorrt_llm/_torch/modules/mamba/causal_conv1d_triton.py#L719-L738

Whether the overwrite is actually ordered against the reads is therefore decided by whether Triton happens to emit a layout-conversion barrier between them, which depends on the compiled block shape. The script below runs causal_conv1d_update on identical inputs at three launch configurations and reports the bar.sync count of the compiled PTX alongside the result:

import glob, os, tempfile
os.environ["TRITON_CACHE_DIR"] = cache = tempfile.mkdtemp()  # must precede the triton import

import torch, torch.nn.functional as F
from tensorrt_llm._torch.modules.mamba.causal_conv1d_triton import causal_conv1d_update

batch, dim, width, seqlen = 8, 4096, 4, 2
weight = torch.randn(dim, width, device="cuda") * 0.5
bias = torch.randn(dim, device="cuda")
indices = torch.arange(batch, device="cuda", dtype=torch.int32)

def bar_sync():
    p = max(glob.glob(f"{cache}/**/_causal_conv1d_update_kernel.ptx", recursive=True), key=os.path.getmtime)
    return sum(1 for ln in open(p) if "bar.sync" in ln)

for num_warps, block_n in [(4, 128), (8, 32), (16, 64)]:
    wrong = nondet = 0
    for _ in range(20):
        x = torch.randn(batch, seqlen, dim, device="cuda").transpose(1, 2)  # stride(1) == 1
        state0 = torch.randn(batch, dim, width - 1, device="cuda")
        s1 = state0.clone()
        y1 = causal_conv1d_update(x.clone(), s1, weight, bias, "silu", conv_state_indices=indices, _num_warps=num_warps, _block_n=block_n)
        s2 = state0.clone()
        y2 = causal_conv1d_update(x.clone(), s2, weight, bias, "silu", conv_state_indices=indices, _num_warps=num_warps, _block_n=block_n)
        padded = torch.cat([state0, x], dim=2)
        ref = F.silu(F.conv1d(padded, weight.unsqueeze(1), bias=bias, groups=dim))
        wrong += (y1 - ref).abs().max().item() > 1e-3
        nondet += not torch.equal(y1, y2)
    print(f"num_warps={num_warps:<3} BLOCK_N={block_n:<4} bar.sync={bar_sync()}  wrong {wrong}/20  nondeterministic {nondet}/20")
num_warps=4   BLOCK_N=128  bar.sync=3  wrong 0/20  nondeterministic 0/20
num_warps=8   BLOCK_N=32   bar.sync=0  wrong 20/20  nondeterministic 20/20
num_warps=16  BLOCK_N=64   bar.sync=0  wrong 20/20  nondeterministic 20/20

Max absolute error against the reference at the failing configurations is 7.44, on values of order 1.

Expected behavior

causal_conv1d_update returns the causal FIR convolution of [conv_state, x] followed by SiLU, and leaves conv_state holding the trailing width - 1 tokens — the same result at every launch configuration, and the same result on every run for the same inputs.

actual behavior

Wherever the compiler emits no barrier between the two steps, the convolution consumes conv_state cells that a different thread has already overwritten with the new tokens. The output and the updated conv_state are both wrong, and vary from run to run on identical inputs. The same happens at seqlen=1, so it is not restricted to the multi-token speculative-decoding path.

additional notes

At the launcher's own defaults (BLOCK_N=128, num_warps=4) the compiled kernel currently does contain three bar.sync, inserted by Triton for the layout change rather than by the kernel — so the shipped path is correct here by accident, not by construction, and only on this GPU and Triton version. The same reasoning applies to _causal_conv1d_fwd_kernel: its two commented-out barriers guard the identical read-then-overwrite pair on the cached-prefix chunked-prefill path (has_initial_state=True), and at its fixed launch config it is likewise incidentally ordered on this stack. Neither kernel has any unit test today.

The file was adapted from vLLM's causal_conv1d.py, which keeps all of these barriers live, with the same comments they carry here; they were commented out when this copy was added in ff0dd6076 (#10754).

Before submitting a new issue...

  • [x] Make sure you already searched for relevant issues, and checked the documentation and examples for answers to frequently asked questions.

truong-v avatar Aug 11 '26 21:08 truong-v

#17539 is open for this. It restores the three barriers, matching what vLLM keeps live in its copy of these kernels.

On regression coverage: a test driven through the public callers passes with or without the barriers. Both launchers hard-code their block config (forward at BLOCK_M=8/BLOCK_N=256, update at BLOCK_N=128/num_warps=4) and nothing in the tree passes the private _block_n/_num_warps overrides, so the compiler emits a layout-conversion barrier at those shapes either way. Reaching the reordering needs a config no caller currently uses.

wilyan09007 avatar Aug 12 '26 18:08 wilyan09007

Fixed by https://github.com/NVIDIA/TensorRT-LLM/pull/17539

bo-nv avatar Aug 26 '26 06:08 bo-nv