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
23 changes: 20 additions & 3 deletions graph_net/torch/backend/unstable_to_stable_backend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import torch
import sys
import inspect
import ast
from .graph_compiler_backend import GraphCompilerBackend
from ..fx_graph_serialize_util import serialize_graph_module_to_str

Expand Down Expand Up @@ -318,7 +316,26 @@ def replace_in_graph(graph_mod):

return gm

# replace this line with modification code for task 126 (torch._C._nn.scaled_dot_product_attention)
def _impl_unstable_to_stable_sdpa(self, gm):
"""
Convert torch._C._nn.scaled_dot_product_attention to torch.nn.functional.scaled_dot_product_attention
"""
issue_nodes = (
node
for node in gm.graph.nodes
if node.op == "call_function"
if hasattr(node.target, "__module__")
if node.target.__module__ == "torch._C._nn"
if hasattr(node.target, "__name__")
if node.target.__name__ == "scaled_dot_product_attention"
)

for node in issue_nodes:
node.target = torch.nn.functional.scaled_dot_product_attention

gm.recompile()

return gm

def _impl_unstable_to_stable_linear_to_functional_linear(self, gm):
"""
Expand Down
5 changes: 4 additions & 1 deletion graph_net/torch/fx_graph_serialize_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ def serialize_graph_module_to_str(gm: torch.fx.GraphModule) -> str:
# replace this line with modification code for task 122 (torch._C._log_api_usage_once)
(r"torch\._C\._nn\.pad\(", "torch.nn.functional.pad("),
(r"torch\._C\._nn\.gelu\(", "torch.nn.functional.gelu("),
# replace this line with modification code for task 126 (torch._C._nn.scaled_dot_product_attention)
(
r"torch\._C\._nn\.scaled_dot_product_attention\(",
"torch.nn.functional.scaled_dot_product_attention(",
),
(r"torch\._C\._nn\.linear\(", "torch.nn.functional.linear("),
]
for pattern, repl in replacements:
Expand Down