Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Expand for Attention mask broadcasting instead of Concat #18159

Closed
Closed
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
32 changes: 20 additions & 12 deletions onnxruntime/python/tools/transformers/fusion_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,31 @@ def reshape_add_qk(self, add_qk: str):
# B = batch size, N = num heads, S = source sequence length, T = target sequence length
mask_output_name = add_qk + "_mask"

# Check if concat node for (B,1,S,T) --> (B,N,S,T) already exists
concat_node = list(filter(lambda node: node.output[0] == mask_output_name, self.nodes_to_add))
if len(concat_node) == 1:
# Check if expand node for (B,1,S,T) --> (B,N,S,T) already exists
expand_node = list(filter(lambda node: node.output[0] == mask_output_name, self.nodes_to_add))
if len(expand_node) == 1:
return mask_output_name

assert len(concat_node) == 0
concat_node_name = self.model.create_node_name("Concat")
concat_add_qk_fp32 = helper.make_node(
"Concat",
inputs=[add_qk for _ in range(self.num_heads)],
assert len(expand_node) == 0
expand_node_name = self.model.create_node_name("Expand")

expand_add_qk_shape = self.add_initializer(
name=mask_output_name + "_shape",
data_type=TensorProto.INT64,
dims=[4],
vals=[1, self.num_heads, 1, 1],
raw=False,
)

expand_add_qk_fp32 = helper.make_node(
"Expand",
inputs=[add_qk, expand_add_qk_shape.name],
outputs=[mask_output_name],
name=concat_node_name,
axis=1,
name=expand_node_name,
)
# Add new node to graph
self.nodes_to_add.append(concat_add_qk_fp32)
self.node_name_to_graph_name[concat_node_name] = self.this_graph_name
self.nodes_to_add.append(expand_add_qk_fp32)
self.node_name_to_graph_name[expand_node_name] = self.this_graph_name

return mask_output_name

Expand Down
Loading