Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions python/tvm/relax/transform/legalize_ops/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,18 +714,19 @@ def _te_attention(
q = topi.transpose(q, [0, 2, 1, 3])
k = topi.transpose(k, [0, 2, 1, 3])
v = topi.transpose(v, [0, 2, 1, 3])
q = topi.reshape(q, [batch_size * num_head, seq_len, head_dim])
k = topi.reshape(k, [batch_size * num_head, seq_len_kv, head_dim])
v = topi.reshape(v, [batch_size * num_head, seq_len_kv, head_dim_v])
p = topi.nn.batch_matmul(q, k)
bs = batch_size * num_head
q = topi.reshape(q, [bs, seq_len, head_dim])
k = topi.reshape(k, [bs, seq_len_kv, head_dim])
v = topi.reshape(v, [bs, seq_len_kv, head_dim_v])
p = topi.nn.batch_matmul(q, k, oshape=[bs, seq_len, seq_len_kv])
if scale is not None:
p = topi.multiply(p, scale)
else:
p = topi.divide(p, tirx.sqrt(tirx.Cast(p.dtype, head_dim)))
if bias is not None:
p = topi.reshape(p, [batch_size, num_head, seq_len, seq_len_kv])
p = topi.add(p, bias)
p = topi.reshape(p, [batch_size * num_head, seq_len, seq_len_kv])
p = topi.reshape(p, [bs, seq_len, seq_len_kv])
if causal_mask is None:
s = topi.nn.softmax(p)
else:
Expand All @@ -741,7 +742,7 @@ def _te_attention(
)
p_masked_sum = topi.sum(p_masked_exp, axis=-1, keepdims=True)
s = topi.divide(p_masked_exp, p_masked_sum)
o = topi.nn.batch_matmul(s, v, transpose_b=False)
o = topi.nn.batch_matmul(s, v, transpose_b=False, oshape=[bs, seq_len, head_dim_v])
o = topi.reshape(o, [batch_size, num_head, seq_len, head_dim_v])
return topi.transpose(o, [0, 2, 1, 3])

Expand Down
35 changes: 35 additions & 0 deletions tests/python/relax/test_transform_legalize_ops_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,41 @@ def main(
LegalizeOps()(Attention)


def test_dynamic_batch_attention():
"""The batch dimension may be dynamic (symbolic).

fix https://github.com/apache/tvm/issues/19696
"""

@tvm.script.ir_module
class Attention:
@R.function
def main(
q: R.Tensor(("batch_size", 16, 32, 8), "float32"),
k: R.Tensor(("batch_size", 8, 32, 8), "float32"),
v: R.Tensor(("batch_size", 8, 32, 16), "float32"),
):
gv = R.nn.attention(q, k, v)
return gv

LegalizeOps()(Attention)

@tvm.script.ir_module
class AttentionBias:
@R.function
def main(
q: R.Tensor(("batch_size", 16, 32, 8), "float32"),
k: R.Tensor(("batch_size", 8, 32, 8), "float32"),
v: R.Tensor(("batch_size", 8, 32, 16), "float32"),
bias: R.Tensor(("batch_size", 32, 16, 8), "float32"),
):
scale = T.FloatImm("float32", 0.1)
gv = R.nn.attention(q, k, v, bias, scale=scale, causal_mask="BottomRight")
return gv

LegalizeOps()(AttentionBias)


def test_nll_loss():
# fmt: off
@tvm.script.ir_module
Expand Down
Loading