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

Minimal updates to work with modelspec v0.3 - onnx examples use floats #476

Merged
merged 2 commits into from
Aug 14, 2023
Merged
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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -278,3 +278,16 @@ Thumbs.db #thumbnail cache on Windows
/examples/NeuroML/arm64
/examples/MDF/RNN/iaf.net
/examples/MDF/RNN/iaf.net2
/examples/NeuroML/PyNN/All*
/examples/NeuroML/PyNN/*dat
/examples/NeuroML/PyNN/*.png
/examples/NeuroML/PyNN/*.1
/examples/NeuroML/PyNN/InputWeights*
/examples/NeuroML/PyNN/OneCell*
/examples/NeuroML/PyNN/Sim*
/examples/NeuroML/PyNN/LEMS_Sim*
/examples/NeuroML/PyNN/HH*
/examples/NeuroML/PyNN/arm64
/examples/NeuroML/PyNN/*.pkl
*_code.gen.c
/examples/NeuroML/PyNN/Net1*
Binary file modified docs/sphinx/source/api/export_format/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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/NeuroML/FN__lems.xml
Original file line number Diff line number Diff line change
@@ -54,5 +54,5 @@
</OnEvent>
</Dynamics>
</ComponentType>
<Component id="fn" type="fnCell" initial_w="0" initial_v="-1" a_v="-0.3333333333333333" b_v="0" c_v="1" d_v="1" e_v="-1" f_v="1" time_constant_v="1" a_w="1" b_w="-0.8" c_w="0.7" time_constant_w="12.5" threshold_exc="-1.0" mode="1" uncorrelated_activity="0" Iext="0" threshold="-1"/>
<Component id="fn" type="fnCell" initial_w="0.0" initial_v="-1" a_v="-0.3333333333333333" b_v="0.0" c_v="1.0" d_v="1" e_v="-1.0" f_v="1.0" time_constant_v="1.0" a_w="1.0" b_w="-0.8" c_w="0.7" time_constant_w="12.5" threshold_exc="-1.0" mode="1.0" uncorrelated_activity="0.0" Iext="0" threshold="-1.0"/>
</Lems>
6 changes: 4 additions & 2 deletions examples/ONNX/ab.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@
"shape": [
2,
3
]
],
"type": "float"
}
},
"parameters": {
@@ -38,7 +39,8 @@
"shape": [
2,
3
]
],
"type": "float"
}
},
"parameters": {
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.
2 changes: 2 additions & 0 deletions examples/ONNX/ab.yaml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ ONNX Model:
shape:
- 2
- 3
type: float
parameters:
B:
value: 1.0
@@ -27,6 +28,7 @@ ONNX Model:
shape:
- 2
- 3
type: float
parameters:
B:
value: 5.0
15 changes: 10 additions & 5 deletions examples/ONNX/abc.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@
"shape": [
2,
3
]
],
"type": "float"
}
},
"parameters": {
@@ -37,13 +38,15 @@
"B_loop_count": {
"shape": [
1
]
],
"type": "float"
},
"onnx_Loop_3": {
"shape": [
2,
3
]
],
"type": "float"
}
},
"parameters": {
@@ -58,7 +61,8 @@
"shape": [
2,
3
]
],
"type": "float"
}
},
"parameters": {
@@ -124,7 +128,8 @@
"x_3": {
"shape": [
1
]
],
"type": "float"
}
},
"parameters": {
5 changes: 5 additions & 0 deletions examples/ONNX/abc.yaml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ ONNX Model:
shape:
- 2
- 3
type: float
parameters:
B:
value: 1.0
@@ -26,10 +27,12 @@ ONNX Model:
B_loop_count:
shape:
- 1
type: float
onnx_Loop_3:
shape:
- 2
- 3
type: float
parameters:
body:
value:
@@ -42,6 +45,7 @@ ONNX Model:
shape:
- 2
- 3
type: float
parameters:
B:
value: 10.0
@@ -80,6 +84,7 @@ ONNX Model:
x_3:
shape:
- 1
type: float
parameters:
B:
value: 100.0
21 changes: 14 additions & 7 deletions examples/ONNX/abcd.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -42,7 +43,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -73,7 +75,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -104,7 +107,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -135,7 +139,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -166,7 +171,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
@@ -197,7 +203,8 @@
"shape": [
1,
1
]
],
"type": "float"
}
},
"parameters": {
7 changes: 7 additions & 0 deletions examples/ONNX/abcd.yaml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
A:
value:
@@ -28,6 +29,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
B:
value:
@@ -46,6 +48,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
A:
value:
@@ -64,6 +67,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
B:
value:
@@ -82,6 +86,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
A:
value:
@@ -100,6 +105,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
B:
value:
@@ -118,6 +124,7 @@ ONNX Model:
shape:
- 1
- 1
type: float
parameters:
B:
value:
8 changes: 4 additions & 4 deletions examples/ONNX/simple_ab.py
Original file line number Diff line number Diff line change
@@ -86,16 +86,16 @@ def main():
), # Makes sure test of this doesn't fail on Windows on GitHub Actions
)
if "-run" in sys.argv:
verbose = True
verbose = False

verbose = "-v" in sys.argv

from modeci_mdf.execution_engine import EvaluableGraph

eg = EvaluableGraph(mdf_model.graphs[0], verbose=verbose)

print("Evaluating graph...")
test_values = [0, 1, [1, 2], dummy_input.numpy()]
test_values = [0, 1, [1, 2]]
test_values = [0.0, 1.0, [1.0, 2.0], dummy_input.numpy()]
test_values = [0.0, 1.0, [1.0, 2.0]]

for t in test_values:
print("===================\nEvaluating MDF model with input: %s" % t)
Binary file modified examples/PyTorch/simple_pytorch_to_mdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ install_requires =
skl2onnx==1.14.0
attrs>=21.1.0
cattrs
modelspec<0.3,>=0.2.6
modelspec<0.4,>=0.3.0
glom

python_requires = >=3.7
5 changes: 4 additions & 1 deletion src/modeci_mdf/execution_engine.py
Original file line number Diff line number Diff line change
@@ -686,7 +686,10 @@ class EvaluableInput:
def __init__(self, input_port: InputPort, verbose: Optional[bool] = False):
self.verbose = verbose
self.input_port = input_port
self.curr_value = np.full(input_port.shape, 0)
default = 0
if input_port.type and "float" in input_port.type:
default = 0.0
self.curr_value = np.full(input_port.shape, default)

def set_input_value(self, value: Union[str, int, np.ndarray]):
"""Set a new value at input port
17 changes: 14 additions & 3 deletions src/modeci_mdf/interfaces/graphviz/exporter.py
Original file line number Diff line number Diff line change
@@ -286,12 +286,23 @@ def mdf_to_graphviz(

if node.input_ports and len(node.input_ports) > 0:
for ip in node.input_ports:

additional = ""
if ip.shape is not None:
additional += "shape: %s, " % str(ip.shape)
"""if ip.reduce is not None:
if ip.reduce != "overwrite": # since this is the default...
additional += "reduce: %s, " % str(ip.reduce)"""
if ip.type is not None:
additional += "type: %s, " % str(ip.type)

if len(additional) > 0:
additional = "(%s)" % additional[:-2]

info += "<tr><td>{}{} {}</td></tr>".format(
format_label("IN"),
format_input(ip.id),
"(shape: %s)" % str(ip.shape)
if level >= LEVEL_2 and ip.shape is not None
else "",
additional,
)

if node.parameters and len(node.parameters) > 0:
2 changes: 1 addition & 1 deletion src/modeci_mdf/interfaces/onnx/importer.py
Original file line number Diff line number Diff line change
@@ -141,7 +141,7 @@ def onnx_node_to_mdf(
for inp in non_constant_inputs:
param_info = onnx_initializer.get(inp, None)
shape = param_info["shape"] if param_info else None
ip = InputPort(id=id_to_port(inp), shape=shape)
ip = InputPort(id=id_to_port(inp), shape=shape, type="float")
mdf_node.input_ports.append(ip)

for out in node.output:
7 changes: 3 additions & 4 deletions tests/interfaces/onnx/test_importer.py
Original file line number Diff line number Diff line change
@@ -20,13 +20,12 @@ def test_ab():
mdf_model = load_mdf(str(file_path))

# Test input
test_input = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.float32)
test_input = np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]], dtype=np.float32)

# Get the result of MDF execution
mdf_executable = EvaluableGraph(mdf_model.graphs[0], verbose=False)
# TODO: the int type cast is necessaryf or now because the nodes' parameters are constants and inputs must have
# the same type
mdf_executable.evaluate(initializer={"input": test_input.astype(int)})

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

# Get the translated ONNX model
12 changes: 12 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
@@ -46,6 +46,10 @@
),
### Conditions
(0, {"x": 3}, 1, "x<4", 1, None),
(0, {"x": 3}, 0, "x<1", 1, None),
(0, {"x": 3}, 1, "x==3", 1, None),
(0, {"x": 3}, 1, "x==3 and x<=5", 1, None),
(2, {"x": 3}, 3, "x<4", 3, None),
(0.6, {"x": 3}, 0.6, "x>=4", 1.6, None),
("y+z", {"x": 3, "y": 2, "z": 3}, 3, "x>y", "z", None),
(
@@ -56,6 +60,14 @@
[3, 3],
None,
),
(
numpy.array([0, 0]),
{"x": numpy.array([1, 1]), "y": numpy.array([0, 0])},
numpy.array([1, 1]),
"y<x",
[1, 1],
None,
),
# ([0,0], {"x": 3}, 1, 'x<4', 1, None),
# (0, {}, 1, 'p1>2', 1, None),
# ("x", {"x": 'abc'}, 'abc'),