Fused attention never returns when the bool mask is a run-time tensor, at 2176 to 2304 query rows
Maintainer thường phản hồi trong vòng 6 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 8/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python, pytorch
- Lĩnh vực
- ai-infra-agents
Hướng nghiên cứu
The hang is in the _gemm_mha_v2 fused attention kernel for a bool mask that is a runtime input, at query lengths 2176 to 2304; the repro in the issue is a single script repro.py that takes the length as an argument. Start by running it at 2176 and at 2048 to confirm the size dependence, then compare with --const. A fix needs a kernel-level change in the closed-source TensorRT attention path, so there is no open source file to edit here.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What is wrong
An IAttention layer (bf16, softmax, decomposable=True) with a bool mask that is a network input hangs on its
first execute. One _gemm_mha_v2 kernel (grid 132 x 384 on H100) stays busy forever, and execute_async_v3 never
finishes. CUDA_LAUNCH_BLOCKING=1 shows the kernel hangs on its own. It does not depend on the input values.
It depends on the size and on the mask pattern. A square causal mask hangs for N = 2176, 2240, 2303 and 2304. It
runs for 1024, 2048, 2305, 2368, 2432, 3456, 3584 and 4096. 2184 and 2240 queries against 3467 keys also hang. The
same mask as a build-time constant runs in 0.47 ms with correct results. An all-true run-time mask also runs.
Anyone who builds attention masks inside the network can hit this at these sizes. That is the default when a framework keeps mask
computation out of constant folding, which Torch-TensorRT does.
What this blocks
No policy in pytorch/executorch#24353 is blocked by this today.
Any model that builds its mask inside the network, which is the Torch-TensorRT default, never returns from its first execute at these sizes, with no error.
A fix lets such models keep run-time masks at every size.
Repro
Save as repro.py. Needs tensorrt and torch (torch only for buffers and the reference). It prints HANG after 60 s.
import argparse, os, threading, time
import numpy as np
import tensorrt as trt
import torch
p = argparse.ArgumentParser()
p.add_argument("n", type=int, help="query length (and key length unless --keys)")
p.add_argument("--keys", type=int, default=None)
p.add_argument("--hq", type=int, default=24)
p.add_argument("--hkv", type=int, default=8)
p.add_argument("--const", action="store_true", help="mask as a build-time constant")
p.add_argument("--ones", action="store_true", help="all-true mask instead of causal")
p.add_argument("--timeout", type=float, default=60)
a = p.parse_args()
sq, skv, d = a.n, a.keys or a.n, 128
mask = torch.ones(sq, skv, dtype=torch.bool) if a.ones else torch.arange(sq)[:, None] + (skv - sq) >= torch.arange(skv)[None, :]
q = torch.randn(1, a.hq, sq, d, device="cuda", dtype=torch.bfloat16)
k = torch.randn(1, a.hkv, skv, d, device="cuda", dtype=torch.bfloat16)
v = torch.randn(1, a.hkv, skv, d, device="cuda", dtype=torch.bfloat16)
logger = trt.Logger(trt.Logger.WARNING)
builder = trt.Builder(logger)
net = builder.create_network(0)
tq = net.add_input("q", trt.bfloat16, tuple(q.shape))
tk = net.add_input("k", trt.bfloat16, tuple(k.shape))
tv = net.add_input("v", trt.bfloat16, tuple(v.shape))
if a.const:
m = mask.reshape(1, 1, sq, skv).numpy()
tm = net.add_constant(m.shape, trt.Weights(m)).get_output(0)
else:
tm = net.add_input("mask", trt.bool, (1, 1, sq, skv))
s = np.full((1, 1, 1, 1), 1 / np.sqrt(d), dtype=np.float32)
sc = net.add_cast(net.add_constant(s.shape, trt.Weights(s)).get_output(0), trt.bfloat16).get_output(0)
sq_ = net.add_elementwise(tq, sc, trt.ElementWiseOperation.PROD).get_output(0)
att = net.add_attention_v2(sq_, tk, tv, trt.AttentionNormalizationOp.SOFTMAX, trt.CausalMaskKind.NONE)
att.mask = tm
att.decomposable = True
net.mark_output(att.get_output(0))
plan = builder.build_serialized_network(net, builder.create_builder_config())
eng = trt.Runtime(logger).deserialize_cuda_engine(plan)
ctx = eng.create_execution_context()
out = torch.empty(1, a.hq, sq, d, device="cuda", dtype=torch.bfloat16)
bufs = {"q": q, "k": k, "v": v}
if not a.const:
bufs["mask"] = mask.reshape(1, 1, sq, skv).cuda().contiguous()
for name, t in bufs.items():
ctx.set_tensor_address(name, t.data_ptr())
ctx.set_tensor_address(eng.get_tensor_name(eng.num_io_tensors - 1), out.data_ptr())
threading.Timer(a.timeout, lambda: (print(f"HANG: execute did not finish in {a.timeout:.0f} s", flush=True), os._exit(2))).start()
stream = torch.cuda.Stream()
t = time.time()
ctx.execute_async_v3(stream.cuda_stream)
stream.synchronize()
ms = (time.time() - t) * 1e3
ref = torch.nn.functional.scaled_dot_product_attention(q.float(), k.float(), v.float(), attn_mask=mask.cuda(), enable_gqa=True)
err = ((out.float() - ref).abs().max() / ref.abs().max()).item()
print(f"ok: {ms:.2f} ms (first call), max abs error / max abs = {err:.2e}", flush=True)
os._exit(0)
python repro.py 2304 # HANG: execute did not finish in 60 s
python repro.py 2176 # HANG
python repro.py 2048 # ok, about 0.24 ms
python repro.py 2305 # ok, about 0.35 ms
python repro.py 2304 --const # ok: same mask as a constant
python repro.py 2304 --ones # ok: all-true mask
Versions: H100 80GB HBM3 (sm_90), driver 580.126.09, TensorRT 11.4.0.106 (pip wheel), bf16, 24 query heads and
8 key/value heads (24/24 hangs too), head size 128.
Expected
The layer finishes, as it does with a constant mask or at nearby sizes.
What we tried
- Builder optimization levels 0 to 5: all hang.
- Padding keys to a multiple of 64 or 128: still hangs.
decomposable=False: the build fails ("not supported by a dedicated kernel").- A bool mask turned into an additive mask inside the network: still hangs. An additive bf16 mask given as an
input runs. - Workaround: make the mask a build-time constant when it does not change between calls.
- Ngôn ngữ chính
- C++
- Star
- 13.5k
- Fork
- 2.4k
- Merge trung bình
- 6 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của NVIDIA/TensorRT
-
Module:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 6 ngày
-
Polygraphy NumPy Top-K sorts unsigned and minimum signed integers incorrectlyCó thể đã có người làm @0z5a đã nhận 16 ngày trước. Đang mởModule:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 6 ngày
-
Polygraphy INT32 comparison can overflow and falsely pass at zero toleranceCó thể đã có người làm @0z5a đã nhận 16 ngày trước. Đang mởModule:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 6 ngày
-
onnx-graphsurgeon: Graph.toposort() hits RecursionError when a graph has a local function and an If/Loop nodeCó thể đã có người làm @gabortar đã nhận 39 ngày trước. Đang mởModule:GraphSurgeon
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 6 ngày
-
Polygraphy parse_meta accepts a shape with a missing bracketCó thể đã có người làm @SashaMIT đã nhận 51 ngày trước. Đang mởModule:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 6 ngày
Tất cả issue của NVIDIA/TensorRT
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 69/100
maplibre/maplibre-native#5072 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[UBSan] spawn_simulator passes null child_options.args to uv_spawn (execvp nonnull violation)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
tenstorrent/tt-umd#4017 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
difficulty:easy func:gui prio:low type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
NatronGitHub/Natron#1538 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
hyprwm/hyprsunset#103 · 1 bình luận ·