Skip to content
Open
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
24 changes: 24 additions & 0 deletions python/models/models_onnx/model_conv2d.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pytorch2.7.1:�
�
input
conv.weight
conv.biasoutput
/conv/Conv"Conv*
dilations@@�*
group�*
kernel_shape@@�*
pads@@@@�*
strides@@�
main_graph*=B conv.weightJ$� >�_�>���=`ͩ��X8>`��=x�8�V�=��>*B conv.biasJ�CS�Z$
input

batch


b%
output

batch


B
24 changes: 24 additions & 0 deletions python/models/models_onnx/model_redundant.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pytorch2.7.1:�
@
input/model/model.0/Relu_output_0/model/model.0/Relu"Relu
W
/model/model.0/Relu_output_0/model/model.1/Relu_output_0/model/model.1/Relu"Relu
W
/model/model.1/Relu_output_0/model/model.2/Relu_output_0/model/model.2/Relu"Relu
m
/model/model.2/Relu_output_0/model/model.3/Flatten_output_0/model/model.3/Flatten"Flatten*
axis�
W
/model/model.3/Flatten_output_0output/model/model.4/Flatten"Flatten*
axis�
main_graphZ$
input

batch


b
output

batch
�B
Binary file not shown.
Binary file not shown.
33 changes: 33 additions & 0 deletions python/scripts/export_wrong_dimension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
print("Exporting Wrong Dimension model to ONNX format...")

import torch
import sys
import os

# Add the models directory to Python path - using relative path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx')))

try:
from wrong_dimension_model import WrongDimensionModel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably missed this but I'm not sure the git commit included this class


model = WrongDimensionModel()
model.eval()
dummy_input = torch.randn(1, 1, 28, 28)

print("Testing forward pass...")
with torch.no_grad():
output = model(dummy_input)
print(f"Model output shape: {output.shape}")

print("Exporting to ONNX...")
torch.onnx.export(
model, dummy_input, "model_wrong_dimension.onnx",
input_names=["input"], output_names=["output"],
opset_version=11
)
print("✅ Exported model_wrong_dimension.onnx successfully!")

except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you open this file in the files tab you may note this do not enter sign; it indicates the file isn't POSIX compliant due to the lack of newline at the end. This is not really a big deal since we're no longer in the 19th century but still preferable to include a newline at the end if possible :)

Image

26 changes: 26 additions & 0 deletions python/scripts/generate_io_conv2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
print("Generating input and output for Conv2D ONNX model...")

import onnxruntime as ort
import torch
import json
import numpy as np

try:
input_tensor = torch.randn(1, 1, 28, 28)
input_numpy = input_tensor.numpy()

session = ort.InferenceSession("/Users/elenapashkova/GravyTesting-Internal/python/models/models_onnx/model_conv2d.onnx")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
session = ort.InferenceSession("/Users/elenapashkova/GravyTesting-Internal/python/models/models_onnx/model_conv2d.onnx")
session = ort.InferenceSession(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx', 'model_conv2d.onnx')))

print("ONNX model loaded")

outputs = session.run(None, {"input": input_numpy})
print("Inference complete")

with open("input.json", "w") as f:
json.dump(input_numpy.tolist(), f)
print("Input saved")

with open("expected_output.json", "w") as f:
json.dump(outputs[0].tolist(), f)
print("Output saved")
except Exception as e:
print("Error:", e)
38 changes: 38 additions & 0 deletions python/scripts/generate_io_redundant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
print("Generating input/output for Redundant model...")

import torch
import json
import os
import sys

# Add the models directory to Python path - using relative path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx')))

try:
from redundant_layers_model import RedundantModel

model = RedundantModel()
model.eval()
dummy_input = torch.randn(1, 1, 28, 28)

with torch.no_grad():
output = model(dummy_input)

input_data = dummy_input.numpy().tolist()
output_data = output.detach().numpy().tolist()

io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'testing', 'core', 'input_output_data', 'model_redundant'))
os.makedirs(io_dir, exist_ok=True)

with open(os.path.join(io_dir, "input.json"), "w") as f:
json.dump({"input": input_data}, f)

with open(os.path.join(io_dir, "output.json"), "w") as f:
json.dump({"output": output_data}, f)

print("✅ Successfully saved input/output JSONs to:", io_dir)

except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
39 changes: 39 additions & 0 deletions python/scripts/generate_io_wrong_dimension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
print("Generating input/output for Wrong Dimension model...")

import torch
import json
import os
import sys

# Add the models directory to Python path - using relative path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx')))

try:
from wrong_dimension_model import WrongDimensionModel

model = WrongDimensionModel()
model.eval()
dummy_input = torch.randn(1, 1, 28, 28)

with torch.no_grad():
output = model(dummy_input)

input_data = dummy_input.numpy().tolist()
output_data = output.detach().numpy().tolist()

print("Creating output directory...")
io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'testing', 'core', 'input_output_data', 'model_wrong_dimension'))
os.makedirs(io_dir, exist_ok=True)

with open(os.path.join(io_dir, "input.json"), "w") as f:
json.dump({"input": input_data}, f)

with open(os.path.join(io_dir, "output.json"), "w") as f:
json.dump({"output": output_data}, f)

print("✅ Successfully saved input/output JSONs to:", io_dir)

except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
36 changes: 36 additions & 0 deletions python/testing/core/tests/test_conv2d_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import onnxruntime as ort
import numpy as np
import json
import os

def test_conv2d_model_output():
# Paths - using relative paths since files are in the same directory as the test
onnx_model_path = "python/models/models_onnx/model_conv2d.onnx"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onnx_model_path = "python/models/models_onnx/model_conv2d.onnx"
onnx_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx', 'model_conv2d.onnx')

input_json_path = "input.json" # File is in the same directory as this test
expected_output_json_path = "expected_output.json" # File is in the same directory as this test

# Get the directory where this test file is located
test_dir = os.path.dirname(os.path.abspath(__file__))

# Build full paths to the JSON files
input_json_path = os.path.join(test_dir, "input.json")
expected_output_json_path = os.path.join(test_dir, "expected_output.json")

# Load input and expected output
with open(input_json_path, "r") as f:
input_data = np.array(json.load(f)).astype(np.float32)

with open(expected_output_json_path, "r") as f:
expected_output = np.array(json.load(f)).astype(np.float32)

# Run ONNX inference
session = ort.InferenceSession(onnx_model_path)
outputs = session.run(None, {"input": input_data})
output = outputs[0]

# Check that output is close enough
np.testing.assert_allclose(output, expected_output, rtol=1e-3, atol=1e-5)
print("Test passed!")

if __name__ == "__main__":
test_conv2d_model_output()
45 changes: 45 additions & 0 deletions python/testing/core/tests/test_model_redundant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
print("Testing Redundant Model...")

import torch
import json
import os
import sys

# Add the models directory to Python path - using relative path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'models', 'models_onnx')))

try:
print("Importing RedundantModel...")
from redundant_layers_model import RedundantModel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this one might be left out of the commit too or potentially I missed it somehow again too


model = RedundantModel()
model.eval()
dummy_input = torch.randn(1, 1, 28, 28)

with torch.no_grad():
output = model(dummy_input)

input_data = dummy_input.numpy().tolist()
output_data = output.detach().numpy().tolist()

print("Creating output directory...")
# Save input/output
io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'input_output_data', 'model_redundant'))
os.makedirs(io_dir, exist_ok=True)

with open(os.path.join(io_dir, "input.json"), "w") as f:
json.dump({"input": input_data}, f)

with open(os.path.join(io_dir, "output.json"), "w") as f:
json.dump({"output": output_data}, f)

print("✅ Successfully saved input/output JSONs")

except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()

def test_redundant_model():
print("Test passed!")
assert True