Skip to content
Draft
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
34 changes: 34 additions & 0 deletions onnxoptimizer/test/function_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import io
import onnx
import onnxoptimizer
import pytest
import unittest

try:
import torch
import torchvision as tv

has_tv = True
except:
has_tv = False


@pytest.mark.skipif(not has_tv, reason="This test needs torchvision")
def test_function_preserved():
with io.BytesIO() as f:
module = tv.models.resnet18()
torch.onnx.export(
module,
(torch.ones([1, 3, 224, 224], dtype=torch.float32), ),
f,
opset_version=15,
export_modules_as_functions={
Copy link
Member

Choose a reason for hiding this comment

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

This option is deprecated in torch 2.8. I would consider directly creating an onnx function and not relying on PyTorch. You may use onnx-ir to simplify the creation code if you like.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. I'll check that also.

torch.nn.BatchNorm2d,
torch.nn.Conv2d,
}
)

model = onnx.load_model_from_string(f.getvalue())
opt_model = onnxoptimizer.optimize(model)
assert len(model.functions) > 0
assert len(model.functions) == len(opt_model.functions)
Loading