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

Add attention op insertion code #1539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions shark/shark_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,51 @@ def get_f16_inputs(inputs, is_f16, f16_input_mask):
return tuple(f16_masked_inputs)


def insert_attention_block(fx_g):
import torch

unary_ops = [
torch.ops.aten._unsafe_view,
torch.ops.aten.view,
torch.ops.aten.expand,
torch.ops.aten.clone,
]

def traverse(node):
while node.target in unary_ops:
node = node.args[0]
return node

for node in fx_g.graph.nodes:
if node.target in [torch.ops.aten.bmm]:
outer_bmm = node
node = traverse(outer_bmm.args[0])
if node.target in [torch.ops.aten._softmax]:
softmax_node = node
node = traverse(softmax_node.args[0])
if node.target in [torch.ops.aten.bmm]:
inner_bmm = node
value = outer_bmm.args[1]
key = inner_bmm.args[1]
with fx_g.graph.inserting_before(outer_bmm):
key = fx_g.graph.call_function(
torch.ops.aten.transpose,
args=(key, -2, -1),
kwargs={},
)
query = inner_bmm.args[0]
new_node = fx_g.graph.call_function(
torch.ops.aten.scaled_dot_product_attention,
args=(query, key, value),
kwargs={},
)
outer_bmm.append(new_node)
outer_bmm.append(key)
outer_bmm.replace_all_uses_with(new_node)

fx_g.graph.lint()


# Upcasts the block/list of ops.
def add_upcast(fx_g):
import torch
Expand Down Expand Up @@ -640,6 +685,8 @@ def strip_overloads(gm):

strip_overloads(fx_g)

insert_attention_block(fx_g)

if is_f16:
fx_g = fx_g.half()
transform_fx(fx_g)
Expand All @@ -659,6 +706,7 @@ def strip_overloads(gm):
return ts_graph

inputs = get_f16_inputs(inputs, is_f16, f16_input_mask)

mlir_importer = SharkImporter(
ts_graph,
inputs,
Expand Down
Loading