vinx13 commented on code in PR #14608:
URL: https://github.com/apache/tvm/pull/14608#discussion_r1164638951
##########
python/tvm/contrib/cutlass/attention_operation.py:
##########
@@ -19,45 +19,87 @@
from .library import *
-def instantiate_attention_template(attrs, func_args):
+def instantiate_attention_template(attrs):
"""Return CUTLASS host code for fused multi head attention
based on a template and the provided attribute map."""
bias_template = {
"B11S'": """
- CHECK(${arg3}->ndim == 2); // B, 1, 1, S'
+ CHECK(${bias}->ndim == 2); // B, 1, 1, S'
- p.attn_bias_ptr = reinterpret_cast<T *>(${arg3}->data);
+ p.attn_bias_ptr = reinterpret_cast<T *>(${bias}->data);
p.bias_strideM = 0; // 0
p.bias_strideH = 0; // 0
p.bias_strideB = p.num_keys; // S'
""",
"B1SS'": """
- CHECK(${arg3}->ndim == 3); // B, 1, S, S'
+ CHECK(${bias}->ndim == 3); // B, 1, S, S'
- p.attn_bias_ptr = reinterpret_cast<T *>(${arg3}->data);
+ p.attn_bias_ptr = reinterpret_cast<T *>(${bias}->data);
p.bias_strideM = p.num_keys; // S'
p.bias_strideH = 0; // 0
p.bias_strideB = p.bias_strideM * p.num_queries; // S' * S
""",
"BNSS'": """
- CHECK(${arg3}->ndim == 4); // B, N, S, S'
+ CHECK(${bias}->ndim == 4); // B, N, S, S'
- p.attn_bias_ptr = reinterpret_cast<T *>(${arg3}->data);
+ p.attn_bias_ptr = reinterpret_cast<T *>(${bias}->data);
p.bias_strideM = p.num_keys; // S'
p.bias_strideH = p.bias_strideM * p.num_queries; // S' * S
p.bias_strideB = p.bias_strideH * p.num_heads; // S' * S * N
""",
}
+ qkv_template = {
+ "default": """
+ p.query_ptr = reinterpret_cast<T *>(${query}->data);
+ p.key_ptr = reinterpret_cast<T *>(${key}->data);
+ p.value_ptr = reinterpret_cast<T *>(${value}->data);
+ CHECK(${query}->ndim == 4); // B, S, N, H
+ CHECK(${key}->ndim == 4); // B, S', N, H
+ CHECK(${value}->ndim == 4); // B, S', N, H'
+
+ // stride for N
+ p.q_strideH = p.head_dim; // H
+ p.k_strideH = p.head_dim; // H
+ p.v_strideH = p.head_dim_value; // H'
+
+ // stride for S
+ p.q_strideM = p.q_strideH * p.num_heads; // H * N
+ p.k_strideM = p.k_strideH * p.num_heads; // H * N
+ p.v_strideM = p.v_strideH * p.num_heads; // H' * N
+
+ // stride for B
+ p.q_strideB = p.q_strideM * p.num_queries; // H * N * S
+ p.k_strideB = p.k_strideM * p.num_keys; // H * N * S'
+ p.v_strideB = p.v_strideM * p.num_keys; // H'* N * S'
+""",
+ "qkv_stacked": """
+ p.query_ptr = reinterpret_cast<T *>(${qkv}->data);
+ p.key_ptr = reinterpret_cast<T *>(${qkv}->data) + p.head_dim * p.num_heads;
+ p.value_ptr = reinterpret_cast<T *>(${qkv}->data) + p.head_dim * p.num_heads
* 2;
+ CHECK(${qkv}->ndim == 3); // B, S, NH + NH + NH'
+
+ // stride for N
+ p.q_strideH = p.head_dim; // H
+ p.k_strideH = p.head_dim; // H
+ p.v_strideH = p.head_dim_value; // H'
Review Comment:
should these be (H+H+H')?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]