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

Enable ResNext #34

Draft
wants to merge 2 commits into
base: cpu-proto
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions projects/pt1/e2e_testing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ def _get_argparse():
default=False,
action="store_true",
help="Enable debug timings collection.")
parser.add_argument("--aten-transform",
default=False,
action="store_true",
help="Replace aten.add.Tensor aten.add.Scalar, for ResNet like models.")
return parser

def main():
args = _get_argparse().parse_args()
if args.aten_transform:
args.dump.append("aten-transform")
opts = TestOptions(dumps=args.dump, use_kernels=args.use_kernels, debug_timer=args.enable_timer)

all_test_unique_names = set(
Expand Down
39 changes: 39 additions & 0 deletions projects/pt1/python/torch_mlir_e2e_test/configs/torchdynamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ def _returns_empty_tuple(fx_graph: torch.fx.GraphModule) -> bool:
return False
return True


def transform(gm: torch.fx.GraphModule):
print("============transform============")
# Modify gm.graph
for node in gm.graph.nodes:
# Checks if we're calling a function (i.e:
# torch.add)
if node.op == 'call_function':
# The target attribute is the function
# that call_function calls.
# call_function[target=torch.ops.aten.add.Tensor](args = (%arg64_1, 1), kwargs = {})
if node.target == torch.ops.aten.add.Tensor:
if len(node.args) != 2 or node.kwargs != {}:
print("skipping --- node: ", node, "args: ", node.args, " kwargs: ", node.kwargs)
elif not isinstance(node.args[1], torch.fx.node.Node):
node.target = torch.ops.aten.add.Scalar
print("node: ", node, "args: ", node.args, " kwargs: ", node.kwargs)
print("argtypes: ", type(node.args[0]), type(node.args[1]))
if node.target == torch.ops.aten.mul.Tensor:
if len(node.args) != 2 or node.kwargs != {}:
print("skipping --- node: ", node, "args: ", node.args, " kwargs: ", node.kwargs)
elif not isinstance(node.args[1], torch.fx.node.Node):
node.target = torch.ops.aten.mul.Scalar
print("node: ", node, "args: ", node.args)
# node.target = torch.mul

gm.graph.lint() # Does some checks to make sure the
# Recompile the forward() method of `gm` from its Graph
gm.recompile()
print("============transform============")

# Replaces torch.aten.add.Tensor/torch.aten.mul.Tensor to
# torch.aten.add.Scalar/torch.aten.mul.Scalar in case of Scalar argument
# Cannot be done on earlier stage, e.g. in _FXGraphImporter as it
Expand Down Expand Up @@ -136,6 +167,14 @@ def my_aot_autograd_backend(gm: torch.fx.GraphModule,
with open(f"{model._get_name()}.{symbol}-fx-graph.txt", "w") as f:
print(gm.graph, file=f)

if opts.is_dump_enabled("aten-transform"):
transform(gm)
if opts.is_dump_enabled("fx-graph"):
with open(f"{model._get_name()}.{symbol}-fx-graph-xformed.txt", "w") as f:
print(gm.graph, file=f)
with open(f"{model._get_name()}.{symbol}-fx-graph-xformed.py", "w") as f:
print(gm.code, file=f)

nonlocal mlir_module
*_, model_name, nth_graph = get_aot_compilation_context()
mlir_module = import_fx_graph_as_func(gm.graph, model_name)
Expand Down
7 changes: 5 additions & 2 deletions projects/pt1/python/torch_mlir_e2e_test/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def wrapper_debug_timer(*args, **kwargs):
class TestOptions:
"""Test run options."""

dump_choices = ["all", "fx-graph", "torch-mlir", "linalg-mlir", "llvm-mlir", "torch-mlir-lowering", "linalg-mlir-lowering", "obj"]
dump_choices = ["all", "fx-graph", "aten-transform", "torch-mlir", "linalg-mlir", "llvm-mlir", "torch-mlir-lowering", "linalg-mlir-lowering", "obj"]

def __init__(self, *, dumps: List[str] = [], use_kernels=False, debug_timer=False, use_omp=True):
self.dumps = {opt for opt in dumps}
Expand All @@ -176,7 +176,10 @@ def __init__(self, *, dumps: List[str] = [], use_kernels=False, debug_timer=Fals
self.use_omp = use_omp

def is_dump_enabled(self, dump: str):
return dump in self.dumps or "all" in self.dumps
if dump != "aten-transform":
return dump in self.dumps or "all" in self.dumps
else:
return dump in self.dumps

def is_debug_timer_enabled(self):
return self.debug_timer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,17 @@ def forward(self, img):
@register_test_case(module_factory=lambda: MobilenetV3Module())
def MobilenetV3Module_basic(module, tu: TestUtils):
module.forward(tu.rand(1, 3, 224, 224))


def ResNext():
model = models.resnext50_32x4d()
model.eval()
return model

@register_test_case(module_factory=lambda: ResNext())
def ResNext_basic(module, tu: TestUtils):
# out = module.forward(tu.randint(1, 11, high=13000))
out = module.forward(tu.rand(1, 3, 224, 224))
# model.forward(input_ids=input_ids.input_ids, attention_mask=input_ids.attention_mask, output_hidden_states=False, use_cache=False)
# print("gen tokens: ", gen_tokens)
return out