-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from ModECI/development
Development
- Loading branch information
Showing
10 changed files
with
555 additions
and
17 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"ONNX Model": { | ||
"format": "ModECI MDF v0.2", | ||
"generating_application": "Python modeci-mdf v0.2.1", | ||
"graphs": { | ||
"torch-jit-export": { | ||
"nodes": { | ||
"Sin_0": { | ||
"input_ports": { | ||
"input": { | ||
"shape": "(2, 3)" | ||
} | ||
}, | ||
"parameters": { | ||
"Sin_0": { | ||
"function": "onnx::Sin", | ||
"args": { | ||
"input": "input" | ||
} | ||
} | ||
}, | ||
"output_ports": { | ||
"_1": { | ||
"value": "Sin_0" | ||
} | ||
} | ||
}, | ||
"Sin_1": { | ||
"input_ports": { | ||
"_1": { | ||
"shape": "(2, 3)" | ||
} | ||
}, | ||
"parameters": { | ||
"Sin_1": { | ||
"function": "onnx::Sin", | ||
"args": { | ||
"input": "_1" | ||
} | ||
} | ||
}, | ||
"output_ports": { | ||
"_2": { | ||
"value": "Sin_1" | ||
} | ||
} | ||
}, | ||
"Cos_2": { | ||
"input_ports": { | ||
"_2": { | ||
"shape": "(2, 3)" | ||
} | ||
}, | ||
"parameters": { | ||
"Cos_2": { | ||
"function": "onnx::Cos", | ||
"args": { | ||
"input": "_2" | ||
} | ||
} | ||
}, | ||
"output_ports": { | ||
"_3": { | ||
"value": "Cos_2" | ||
} | ||
} | ||
} | ||
}, | ||
"edges": { | ||
"Sin_0._1_Sin_1._1": { | ||
"sender": "Sin_0", | ||
"receiver": "Sin_1", | ||
"sender_port": "_1", | ||
"receiver_port": "_1" | ||
}, | ||
"Sin_1._2_Cos_2._2": { | ||
"sender": "Sin_1", | ||
"receiver": "Cos_2", | ||
"sender_port": "_2", | ||
"receiver_port": "_2" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
ONNX Model: | ||
format: ModECI MDF v0.2 | ||
generating_application: Python modeci-mdf v0.2.1 | ||
graphs: | ||
torch-jit-export: | ||
nodes: | ||
Sin_0: | ||
input_ports: | ||
input: | ||
shape: (2, 3) | ||
parameters: | ||
Sin_0: | ||
function: onnx::Sin | ||
args: | ||
input: input | ||
output_ports: | ||
_1: | ||
value: Sin_0 | ||
Sin_1: | ||
input_ports: | ||
_1: | ||
shape: (2, 3) | ||
parameters: | ||
Sin_1: | ||
function: onnx::Sin | ||
args: | ||
input: _1 | ||
output_ports: | ||
_2: | ||
value: Sin_1 | ||
Cos_2: | ||
input_ports: | ||
_2: | ||
shape: (2, 3) | ||
parameters: | ||
Cos_2: | ||
function: onnx::Cos | ||
args: | ||
input: _2 | ||
output_ports: | ||
_3: | ||
value: Cos_2 | ||
edges: | ||
Sin_0._1_Sin_1._1: | ||
sender: Sin_0 | ||
receiver: Sin_1 | ||
sender_port: _1 | ||
receiver_port: _1 | ||
Sin_1._2_Cos_2._2: | ||
sender: Sin_1 | ||
receiver: Cos_2 | ||
sender_port: _2 | ||
receiver_port: _2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
This file does three things: | ||
- It implements a simple PyTorch model. | ||
- Exports in to ONNX using a combination of tracing and scripting | ||
- Converts it to MDF | ||
""" | ||
import torch | ||
import onnx | ||
|
||
from onnx import helper | ||
|
||
from modeci_mdf.interfaces.onnx import onnx_to_mdf, convert_file | ||
|
||
|
||
class A(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.sin(x) | ||
|
||
|
||
class B(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.sin(x) | ||
|
||
|
||
class C(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.cos(x) | ||
|
||
|
||
class ABC(torch.nn.Module): | ||
def __init__(self): | ||
super(ABC, self).__init__() | ||
self.A = A() | ||
self.B = B() | ||
self.C = C() | ||
|
||
def forward(self, x): | ||
|
||
# Run A | ||
y = self.A(x) | ||
|
||
# Run B (loop_count times) | ||
y = self.B(y) | ||
|
||
# Run C | ||
y = self.C(y) | ||
|
||
return y | ||
|
||
def main(): | ||
|
||
model = ABC() | ||
dummy_input = torch.zeros(2, 3) | ||
# loop_count = torch.tensor(5, dtype=torch.long) | ||
torch.onnx.export(model, | ||
(dummy_input), | ||
'abc_basic.onnx', | ||
verbose=True, | ||
input_names=['input']) | ||
|
||
|
||
# Load it back in using ONNX package | ||
onnx_model = onnx.load("abc_basic.onnx") | ||
print(onnx_model) | ||
onnx.checker.check_model(onnx_model) | ||
|
||
# Extract the loop or if body as a sub-model, this is just because I want | ||
# to view it in netron and sub-graphs can't be rendered | ||
for node in [node for node in onnx_model.graph.node if node.op_type in ["Loop", 'If']]: | ||
|
||
# Get the GraphProto of the body | ||
body_graph = node.attribute[0].g | ||
|
||
# Turn it into a model | ||
model_def = helper.make_model(body_graph, producer_name='abc_basic.py') | ||
|
||
onnx.save(model_def, f'examples/{node.name}_body.onnx') | ||
|
||
|
||
convert_file("abc_basic.onnx") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .exporter import onnx_to_mdf, find_subgraphs, convert_file | ||
|
||
from .importer import mdf_to_onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.