Skip to content

Add a converter for PyTorch's packed variable-length attention (varlen_attn) #5039

Open

Description

@shoumikhin

What is wrong

PyTorch 2.15 has torch.nn.attention.varlen.varlen_attn: attention over several sequences packed into one tensor, with cumulative lengths cu_seq_q and cu_seq_k marking where each sequence starts. It is exported as the custom op torch_attn::_varlen_attn. Vision encoders that pack several images or attention windows into one sequence use it (Qwen2.5-VL style windowed attention, and robot policies built on such encoders).

Torch-TensorRT has no converter for it, so every such attention runs outside TensorRT and splits the graph around it. In one public robot policy, most of the parts left outside TensorRT are varlen_attn.

What this blocks

wall_x, evo1, lingbot_va and wall_oss_05 (pytorch/executorch#24353) leave every varlen_attn outside TensorRT.
wall_x is split into dozens of TensorRT engines and CUDA backend programs, and most of those CUDA programs are this attention.
A converter would let these policies build one engine or a few.

Repro

import torch
import torch_tensorrt
from torch.nn.attention.varlen import varlen_attn


class PackedWindows(torch.nn.Module):
    """3 windows of 64, 32 and 160 tokens packed into one sequence of 256."""

    def __init__(self):
        super().__init__()
        self.register_buffer("cu", torch.tensor([0, 64, 96, 256], dtype=torch.int32))
        self.qkv = torch.nn.Linear(512, 3 * 512)

    def forward(self, x):  # x: (256, 512)
        q, k, v = self.qkv(x).view(256, 3, 8, 64).unbind(1)
        out = varlen_attn(q, k, v, self.cu, self.cu, 160, 160)
        return out.reshape(256, 512)


model = PackedWindows().cuda().bfloat16().eval()
x = torch.randn(256, 512, device="cuda", dtype=torch.bfloat16)
ep = torch.export.export(model, (x,))
trt_gm = torch_tensorrt.dynamo.compile(ep, arg_inputs=[x], min_block_size=1)
print([name for name, _ in trt_gm.named_children()])
# today: the attention is in a _run_on_gpu submodule, not in the engine

Actual

The graph is split in 3, with the attention left to PyTorch in the middle:

['_run_on_acc_0', '_run_on_gpu_1', '_run_on_acc_2']

Expected

varlen_attn converts. A simple first version could cover the common case where cu_seq_q and cu_seq_k are known at build time: build a block-diagonal mask from them and use a masked IAttention. With run-time lengths, the same mask can be computed in the network by comparing each position with the segment bounds.

Versions

torch 2.15.0 (CUDA 13.2), torch_tensorrt 2.15.0, TensorRT 11.4.0.106, H100, driver 580.126.09.

What was tried

Rewriting each model to use scaled_dot_product_attention with an explicit block mask should avoid it, since masked attention converts, but every model would need that change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions