Skip to content

Commit 0ccc7ec

Browse files
authored
[TRTLLM-13445][test] Add SageAttention backend-routing and SAGE recip… (NVIDIA#15746)
Signed-off-by: Ying Guo <244492186+yingguo-trt@users.noreply.github.com>
1 parent 1abcc63 commit 0ccc7ec

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

tests/unittest/_torch/visual_gen/test_attention_integration.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
RingAttention,
2727
UlyssesAttention,
2828
)
29+
from tensorrt_llm._torch.visual_gen.attention_backend.trtllm import TrtllmAttention
2930
from tensorrt_llm._torch.visual_gen.attention_backend.vanilla import VanillaAttention
3031
from tensorrt_llm._torch.visual_gen.config import (
3132
DiffusionModelConfig,
@@ -292,6 +293,43 @@ def test_attn2d_with_sequence_parallel_disabled_allowed(self):
292293
assert isinstance(attn.attn, VanillaAttention)
293294

294295

296+
def _build_sage_routed_attention(qkv_mode: QKVMode):
297+
"""Build a TRTLLM-SAGE-configured Attention for backend-routing checks."""
298+
quant_cfg = QuantAttentionConfig(
299+
qk_dtype="int8", q_block_size=1, k_block_size=16, v_block_size=1
300+
)
301+
config = create_model_config(
302+
hidden_size=512,
303+
num_heads=4,
304+
head_dim=128,
305+
attn_backend="TRTLLM",
306+
quant_attention_config=quant_cfg,
307+
skip_create_weights_in_init=True,
308+
)
309+
attn = Attention(
310+
hidden_size=512,
311+
num_attention_heads=4,
312+
head_dim=128,
313+
qkv_mode=qkv_mode,
314+
config=config,
315+
)
316+
return attn, quant_cfg
317+
318+
319+
class TestSageAttentionBackendRouting:
320+
def test_self_attention_uses_trtllm_sage_backend(self):
321+
attn, quant_cfg = _build_sage_routed_attention(QKVMode.FUSE_QKV)
322+
assert attn.attn_backend == "TRTLLM"
323+
assert isinstance(attn.attn, TrtllmAttention)
324+
assert attn.attn.quant_attention_config == quant_cfg
325+
assert not attn.attn.support_fused_qkv()
326+
327+
def test_cross_attention_with_sage_config_falls_back_to_vanilla(self):
328+
attn, _ = _build_sage_routed_attention(QKVMode.SEPARATE_QKV)
329+
assert attn.attn_backend == "VANILLA"
330+
assert isinstance(attn.attn, VanillaAttention)
331+
332+
295333
# ============================================================================
296334
# Test functions
297335
# ============================================================================

tests/unittest/_torch/visual_gen/test_visual_gen_args.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,24 @@ def test_quant_config_rejected_when_unsupported(self):
8989
),
9090
)
9191

92-
def test_supported_quant_config_sage(self):
92+
@pytest.mark.parametrize(
93+
("qk_dtype", "q_block_size", "k_block_size", "v_block_size"),
94+
[
95+
("int8", 1, 1, 1),
96+
("int8", 1, 4, 1),
97+
("int8", 1, 16, 1),
98+
("fp8", 1, 1, 1),
99+
("fp8", 1, 4, 1),
100+
],
101+
)
102+
def test_supported_quant_config_sage(self, qk_dtype, q_block_size, k_block_size, v_block_size):
93103
attention = AttentionConfig(
94104
backend="TRTLLM",
95105
quant_attention_config=QuantAttentionConfig(
96-
qk_dtype="int8", q_block_size=1, k_block_size=16, v_block_size=1
106+
qk_dtype=qk_dtype,
107+
q_block_size=q_block_size,
108+
k_block_size=k_block_size,
109+
v_block_size=v_block_size,
97110
),
98111
)
99112

0 commit comments

Comments
 (0)