2021-02-02 09:59:42 +08:00
|
|
|
# -*- Python -*-
|
|
|
|
# This file is licensed under a pytorch-style license
|
|
|
|
# See frontends/pytorch/LICENSE for license information.
|
|
|
|
|
|
|
|
import typing
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch_mlir
|
|
|
|
|
|
|
|
# RUN: %PYTHON %s | npcomp-opt | FileCheck %s
|
|
|
|
|
|
|
|
mb = torch_mlir.ModuleBuilder()
|
|
|
|
|
|
|
|
class Submodule(torch.nn.Module):
|
|
|
|
def __init__(self, n):
|
|
|
|
super().__init__()
|
|
|
|
self.n = n
|
2021-02-18 03:28:51 +08:00
|
|
|
def forward(self):
|
|
|
|
return self.n
|
2021-02-02 09:59:42 +08:00
|
|
|
|
|
|
|
class TestModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.s1 = Submodule(1)
|
|
|
|
self.s2 = Submodule(2)
|
|
|
|
|
|
|
|
# CHECK-LABEL: func private @{{.*}}TestModule.forward
|
|
|
|
def forward(self, b: bool):
|
|
|
|
# Modules with the same class can be selected between.
|
2021-06-17 01:23:26 +08:00
|
|
|
# CHECK: %[[MOD:.*]] = torch.prim.If
|
2021-02-02 09:59:42 +08:00
|
|
|
s = self.s1 if b else self.s2
|
2021-02-18 03:28:51 +08:00
|
|
|
# CHECK: %[[N:.*]] = torch.prim.CallMethod %[[MOD]]["forward"] ()
|
|
|
|
# CHECK: return %[[N]]
|
|
|
|
return s.forward()
|
2021-02-02 09:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
test_module = TestModule()
|
|
|
|
recursivescriptmodule = torch.jit.script(test_module)
|
|
|
|
# TODO: Automatically handle unpacking Python class RecursiveScriptModule into the underlying ScriptModule.
|
|
|
|
mb.import_module(recursivescriptmodule._c)
|
|
|
|
mb.module.operation.print()
|