Skip to content

Commit

Permalink
Merge branch 'fix_torch_v2' into nml_examples2
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Sep 6, 2023
2 parents 5dc0f56 + ffee019 commit 8f5d8df
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 79 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,26 @@ jobs:
pip list
- name: Install graphviz
if: ${{ matrix.runs-on != 'windows-latest' }}
run: |
if [[ ${{ matrix.runs-on }} == *"macos"* ]]; then brew install graphviz ; fi
if [[ ${{ matrix.runs-on }} == *"ubuntu"* ]]; then sudo apt install graphviz ; fi
uses: ts-graphviz/setup-graphviz@v1

- name: Test interface ACT-R
if: ${{ matrix.python-version != '3.10' || matrix.runs-on != 'windows-latest' }}
run: |
python -m pytest -v -m "actr" tests/
- name: Test interface PyTorch
if: ${{ matrix.python-version != '3.10' || matrix.runs-on != 'windows-latest' }}
run: |
python -m pytest -v -m "pytorch" tests/
- name: Test interface NeuroML
if: ${{ matrix.python-version != '3.10' || matrix.runs-on != 'windows-latest' }}
run: |
python -m pytest -v -m "neuroml" tests/
- name: Test interface TensorFlow linux/mac
if: ${{ matrix.runs-on != 'windows-latest' }}
run: |
dot -V
python -m pytest -v -m "tensorflow" tests/
- name: Test interface TensorFlow windows
if: ${{ matrix.python-version != '3.10' && matrix.runs-on == 'windows-latest' }}
- name: Test interface TensorFlow
run: |
choco install graphviz
dot -V
python -m pytest -v -m "tensorflow" tests/
- name: Test interface PsyNeuLink
if: ${{ matrix.python-version != '3.10' }}
run: |
python -m pip install .[psyneulink]
python -m pytest -v -m "psyneulink" tests/
Expand Down
1 change: 1 addition & 0 deletions docs/sphinx/source/api/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ make clean
make html
```


### 7) Change directory into html folder and run the documentation offline
```
# for Windows go into build\html folder and double click on the index.html file, or:
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx/source/api/export_format/ONNX/simple_ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def main():
for t in test_values:
print("===================\nEvaluating MDF model with input: %s" % t)
eg.evaluate(initializer={"input": t})
print("Output: %s" % eg.enodes["Mul_3"].evaluable_outputs["_4"].curr_value)
print("Output: %s" % eg.enodes["/B/Mul"].evaluable_outputs["_4"].curr_value)


if __name__ == "__main__":
Expand Down
30 changes: 15 additions & 15 deletions examples/ONNX/ab.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"format": "ModECI MDF v0.4",
"generating_application": "Python modeci-mdf v0.4.7",
"graphs": {
"torch-jit-export": {
"torch_jit": {
"nodes": {
"Add_1": {
"/A/Add": {
"input_ports": {
"input": {
"shape": [
Expand All @@ -19,7 +19,7 @@
"B": {
"value": 1.0
},
"Add_1": {
"/A/Add": {
"function": "onnx::Add",
"args": {
"A": "input",
Expand All @@ -28,14 +28,14 @@
}
},
"output_ports": {
"onnx_Mul_2": {
"value": "Add_1"
"_A_Add_output_0": {
"value": "/A/Add"
}
}
},
"Mul_3": {
"/B/Mul": {
"input_ports": {
"onnx_Mul_2": {
"_A_Add_output_0": {
"shape": [
2,
3
Expand All @@ -47,27 +47,27 @@
"B": {
"value": 5.0
},
"Mul_3": {
"/B/Mul": {
"function": "onnx::Mul",
"args": {
"A": "onnx_Mul_2",
"A": "_A_Add_output_0",
"B": "B"
}
}
},
"output_ports": {
"_4": {
"value": "Mul_3"
"value": "/B/Mul"
}
}
}
},
"edges": {
"Add_1.onnx_Mul_2_Mul_3.onnx_Mul_2": {
"sender": "Add_1",
"receiver": "Mul_3",
"sender_port": "onnx_Mul_2",
"receiver_port": "onnx_Mul_2"
"/A/Add._A_Add_output_0_/B/Mul._A_Add_output_0": {
"sender": "/A/Add",
"receiver": "/B/Mul",
"sender_port": "_A_Add_output_0",
"receiver_port": "_A_Add_output_0"
}
}
}
Expand Down
Binary file modified examples/ONNX/ab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions examples/ONNX/ab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ ONNX Model:
format: ModECI MDF v0.4
generating_application: Python modeci-mdf v0.4.7
graphs:
torch-jit-export:
torch_jit:
nodes:
Add_1:
/A/Add:
input_ports:
input:
shape:
Expand All @@ -14,35 +14,35 @@ ONNX Model:
parameters:
B:
value: 1.0
Add_1:
/A/Add:
function: onnx::Add
args:
A: input
B: B
output_ports:
onnx_Mul_2:
value: Add_1
Mul_3:
_A_Add_output_0:
value: /A/Add
/B/Mul:
input_ports:
onnx_Mul_2:
_A_Add_output_0:
shape:
- 2
- 3
type: float
parameters:
B:
value: 5.0
Mul_3:
/B/Mul:
function: onnx::Mul
args:
A: onnx_Mul_2
A: _A_Add_output_0
B: B
output_ports:
_4:
value: Mul_3
value: /B/Mul
edges:
Add_1.onnx_Mul_2_Mul_3.onnx_Mul_2:
sender: Add_1
receiver: Mul_3
sender_port: onnx_Mul_2
receiver_port: onnx_Mul_2
/A/Add._A_Add_output_0_/B/Mul._A_Add_output_0:
sender: /A/Add
receiver: /B/Mul
sender_port: _A_Add_output_0
receiver_port: _A_Add_output_0
2 changes: 1 addition & 1 deletion examples/ONNX/simple_ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def main():
for t in test_values:
print("===================\nEvaluating MDF model with input: %s" % t)
eg.evaluate(initializer={"input": t})
print("Output: %s" % eg.enodes["Mul_3"].evaluable_outputs["_4"].curr_value)
print("Output: %s" % eg.enodes["/B/Mul"].evaluable_outputs["_4"].curr_value)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/TensorFlow/Keras/keras2MDFfunc_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from tensorflow.keras.layers import Input, Dense, Flatten


new_model = tf.keras.models.load_model("kr_N_model.h5")
new_model = tf.keras.models.load_model("kr_N_model.h5", compile=False)
for i in new_model.layers:
print(i.name)

Expand Down
2 changes: 1 addition & 1 deletion examples/TensorFlow/Keras/keras_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# tf.__version__

from keras.layers import Dense
from keras.utils.vis_utils import plot_model
from keras.utils import plot_model
from keras.models import Sequential

# from keras_visualizer import visualizer
Expand Down
2 changes: 1 addition & 1 deletion examples/TensorFlow/Keras/keras_to_MDF.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"keras_to_MDF": {
"format": "ModECI MDF v0.4",
"generating_application": "Python modeci-mdf v0.4.4",
"generating_application": "Python modeci-mdf v0.4.7",
"graphs": {
"Keras_to_MDF_graph": {
"nodes": {
Expand Down
2 changes: 1 addition & 1 deletion examples/TensorFlow/Keras/keras_to_MDF.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
keras_to_MDF:
format: ModECI MDF v0.4
generating_application: Python modeci-mdf v0.4.4
generating_application: Python modeci-mdf v0.4.7
graphs:
Keras_to_MDF_graph:
nodes:
Expand Down
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ install_requires =
matplotlib
graphviz
h5py
onnxruntime==1.13.1
onnx==1.12.0
skl2onnx==1.14.0
onnxruntime
onnx
skl2onnx
attrs>=21.1.0
cattrs
modelspec<0.4,>=0.3.0
Expand Down Expand Up @@ -89,7 +89,7 @@ optional =
torchviz
netron
torch>=1.11.0
torchvision<=0.12.0
torchvision
h5py

all_except_psyneulink =
Expand Down
9 changes: 6 additions & 3 deletions src/modeci_mdf/functions/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ def run_onnx_op(
if op_name == "Pad":
if "constant_value" in inputs:
cval = inputs["constant_value"]
data = list(inputs.values())[0]
if cval.dtype != data.dtype:
inputs["constant_value"] = cval.astype(data.dtype)
if cval is None:
inputs.pop("constant_value")
else:
data = list(inputs.values())[0]
if cval.dtype != data.dtype:
inputs["constant_value"] = cval.astype(data.dtype)

# SkLearn ONNX doesn't seem to support ConcatFromSequence, see
# https://github.com/onnx/sklearn-onnx/issues/710
Expand Down
11 changes: 10 additions & 1 deletion src/modeci_mdf/interfaces/onnx/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from onnx import AttributeProto, TensorProto, GraphProto
from onnx.defs import get_schema

import onnxruntime

from ast import literal_eval

import argparse
Expand Down Expand Up @@ -44,7 +46,14 @@ def mdf_to_onnx(mdf_model):
onnx_graph = generate_onnx_graph(graph, nodenames_in_execution_order)

# Make an onnx model from graph
onnx_model = helper.make_model(onnx_graph)

# Check to see if onnxruntime version is less than 1.15, if so ir_version should
# be 8 for now. See: https://github.com/microsoft/onnxruntime/issues/15874
make_model_kwargs = {}
if onnxruntime.__version__ < "1.15":
make_model_kwargs = {"ir_version": 8}

onnx_model = helper.make_model(onnx_graph, **make_model_kwargs)

# Infer shapes
onnx_model = shape_inference.infer_shapes(onnx_model)
Expand Down
4 changes: 4 additions & 0 deletions src/modeci_mdf/interfaces/onnx/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
def id_to_port(id: str):
"""Turn unique ONNX output and input value names into valid MDF input and outport names"""

# Get rid of periods in names
new_name = str(id).replace(".", "_")

# Get rid of slashes in names
new_name = str(id).replace("/", "_")

# Get rid of double colon in id names, this causes issues with execution engine.
new_name = new_name.replace("::", "_")

Expand Down
22 changes: 17 additions & 5 deletions src/modeci_mdf/interfaces/pytorch/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@

import torch

# We need to monkey patch the torch._C.Node class to add a __getitem__ method
# This is for torch 2.0
# From https://github.com/openai/CLIP/issues/79#issuecomment-1624202950
def _node_get(node: torch._C.Node, key: str):
"""Gets attributes of a node which is polymorphic over return type."""
sel = node.kindOf(key)
return getattr(node, sel)(key)


torch._C.Node.__getitem__ = _node_get


from modeci_mdf.mdf import Model, Graph, Node, Edge, InputPort, OutputPort, Parameter
from modeci_mdf.functions.onnx import onnx_opset_version as modeci_onnx_opset_version

Expand Down Expand Up @@ -293,15 +305,15 @@ def torchnode_to_mdfnode(
"""
op = node.kind()

# Lookup the schema. For some reason we cannot just call node.schema(), it returns "(no schema)", huh?
# We need to do this the hard way.
schema = onnx.defs.get_schema(op.replace("onnx::", ""), modeci_onnx_opset_version)

# Exclude constants (as nodes) from the MDF graph. We will instead insert them as parameters to the nodes that
# they project to.
if op in ("prim::Constant", "onnx::Constant"):
return None

# Lookup the schema. For some reason we cannot just call node.schema(), it returns "(no schema)", huh?
# We need to do this the hard way.
schema = onnx.defs.get_schema(op.replace("onnx::", ""), modeci_onnx_opset_version)

# If we are dealing with a loop node, we need to recursively create a sub-graph for the loop body
if op == "onnx::Loop":
sub_mdf_graph = Graph(id=f"LoopSubgraph{make_node_id(node)}")
Expand Down Expand Up @@ -504,7 +516,7 @@ def pytorch_to_mdf(
graph = None

if use_onnx_ops:
operator_export_type = torch._C._onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK
operator_export_type = torch._C._onnx.OperatorExportTypes.ONNX
else:
operator_export_type = torch._C._onnx.OperatorExportTypes.RAW

Expand Down
2 changes: 1 addition & 1 deletion tests/interfaces/onnx/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_ab():
mdf_executable = EvaluableGraph(mdf_model.graphs[0], verbose=False)

mdf_executable.evaluate(initializer={"input": test_input})
mdf_output = mdf_executable.enodes["Mul_3"].evaluable_outputs["_4"].curr_value
mdf_output = mdf_executable.enodes["/B/Mul"].evaluable_outputs["_4"].curr_value

# Get the translated ONNX model
onnx_models = mdf_to_onnx(mdf_model)
Expand Down
Loading

0 comments on commit 8f5d8df

Please sign in to comment.