MCPcopy Create free account
hub / github.com/apache/tvm / test_attention_rewrite_multi_query

Function test_attention_rewrite_multi_query

tests/python/relax/test_codegen_cutlass.py:2087–2130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2085
2086
2087def test_attention_rewrite_multi_query():
2088 @I.ir_module(s_tir=True)
2089 class Module:
2090 @R.function
2091 def main(
2092 q: R.Tensor((4, 16, 32, 16), dtype="float16"),
2093 k_single: R.Tensor((4, 16, 1, 16), dtype="float16"),
2094 v_single: R.Tensor((4, 16, 1, 16), dtype="float16"),
2095 ) -> R.Tensor((4, 16, 32, 8), dtype="float16"):
2096 with R.dataflow():
2097 k = R.repeat(k_single, 32, axis=2)
2098 v = R.repeat(v_single, 32, axis=2)
2099
2100 lv = R.permute_dims(q, axes=[0, 2, 1, 3])
2101 lv1 = R.reshape(lv, R.shape([128, 16, 16]))
2102 lv2 = R.permute_dims(k, axes=[0, 2, 1, 3])
2103 lv3 = R.reshape(lv2, R.shape([128, 16, 16]))
2104 lv4 = R.permute_dims(v, axes=[0, 2, 1, 3])
2105 lv5 = R.reshape(lv4, R.shape([128, 16, 16]))
2106
2107 lv6 = R.permute_dims(lv3, axes=[0, 2, 1])
2108 lv7 = R.matmul(lv1, lv6, out_dtype="float16")
2109 lv3_1 = R.astype(R.const(0.25, "float32"), "float16")
2110 lv8 = R.multiply(lv7, lv3_1)
2111 lv11 = R.astype(R.nn.softmax(R.astype(lv8, "float32"), axis=2), "float16")
2112 lv12 = R.matmul(lv11, lv5, out_dtype="float16")
2113 lv13 = R.reshape(lv12, R.shape([4, 32, 16, 16]))
2114 lv6_1 = R.permute_dims(lv13, axes=[0, 2, 1, 3])
2115 R.output(lv6_1)
2116 return lv6_1
2117
2118 q_np = np.random.randn(4, 16, 32, 16).astype("float16")
2119 k_np = np.random.randn(4, 16, 1, 16).astype("float16")
2120 v_np = np.random.randn(4, 16, 1, 16).astype("float16")
2121 args = [q_np, k_np, v_np]
2122 ref = build_and_run(Module, args, "llvm", legalize=True)
2123
2124 mod = partition_for_cutlass(Module, use_flash_mqa=True)
2125 codegen_pass = relax.transform.RunCodegen({"cutlass": {"sm": 80}})
2126 mod = codegen_pass(mod)
2127
2128 out = build_and_run(mod, args, "cuda")
2129
2130 tvm.testing.assert_allclose(out, ref, rtol=1e-2, atol=1e-2)
2131
2132
2133def _test_batched_var_len_attention(

Callers

nothing calls this directly

Calls 3

partition_for_cutlassFunction · 0.90
build_and_runFunction · 0.70
astypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…