Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
inisis committed Jun 9, 2024
1 parent 0800c69 commit f7374fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
15 changes: 3 additions & 12 deletions onnxslim/core/slim.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import logging
import os
import sys
import tempfile
from typing import Dict, List

import numpy as np
import onnx
Expand All @@ -12,13 +9,7 @@
from onnxslim.core.optimizer import delete_node, optimize_model
from onnxslim.core.symbolic_shape_infer import SymbolicShapeInference
from onnxslim.onnx_graphsurgeon.ir.tensor import Constant
from onnxslim.utils import (
dump_model_info_to_disk,
gen_onnxruntime_input_data,
logger,
onnxruntime_inference,
print_model_info_as_table,
)
from onnxslim.utils import logger, save

DEBUG = bool(os.getenv("ONNXSLIM_DEBUG"))
AUTO_MERGE = True if os.getenv("ONNXSLIM_AUTO_MERGE") is None else bool(int(os.getenv("ONNXSLIM_AUTO_MERGE")))
Expand Down Expand Up @@ -91,8 +82,8 @@ def shape_infer(model: onnx.ModelProto):
try:
logger.debug("try onnxruntime shape infer.")
model = SymbolicShapeInference.infer_shapes(model, auto_merge=AUTO_MERGE)
except Exception:
logger.debug("onnxruntime shape infer failed, try onnx shape infer.")
except Exception as err:
logger.debug(f"onnxruntime shape infer failed, try onnx shape infer. {err}")
if model.ByteSize() >= checker.MAXIMUM_PROTOBUF:
tmp_dir = tempfile.TemporaryDirectory()
tmp_path = os.path.join(tmp_dir.name, "tmp.onnx")
Expand Down
20 changes: 11 additions & 9 deletions tests/test_onnx_nets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import warnings
import shutil

import pytest
import timm
Expand All @@ -27,10 +28,11 @@ def test_torchvision(self, request, model, shape=(1, 3, 224, 224)):
"""Test various TorchVision models with random input tensors of a specified shape."""
model = model(pretrained=PRETRAINED)
x = torch.rand(shape)
os.makedirs(f"tmp/{request.node.name}", exist_ok=True)
directory = "tmp/" + request.node.name
os.makedirs(directory, exist_ok=True)

filename = f"tmp/{request.node.name}/{request.node.name}.onnx"
slim_filename = f"tmp/{request.node.name}/{request.node.name}_slim.onnx"
filename = f"{directory}/{request.node.name}.onnx"
slim_filename = f"{directory}/{request.node.name}_slim.onnx"

torch.onnx.export(model, x, filename)

Expand All @@ -41,7 +43,7 @@ def test_torchvision(self, request, model, shape=(1, 3, 224, 224)):
print(output)
assert result.returncode == 0

os.remove(filename)
shutil.rmtree(directory, ignore_errors=True)


class TestTimmClass:
Expand All @@ -55,12 +57,12 @@ def test_timm(self, request, model_name):
model = timm.create_model(model_name, pretrained=PRETRAINED)
input_size = model.default_cfg.get("input_size")
x = torch.randn((1,) + input_size)

directory = "tmp/" + request.node.name
try:
os.makedirs(f"tmp/{request.node.name}", exist_ok=True)
os.makedirs(directory, exist_ok=True)

filename = f"tmp/{request.node.name}/{request.node.name}.onnx"
slim_filename = f"tmp/{request.node.name}/{request.node.name}_slim.onnx"
filename = f"{directory}/{request.node.name}.onnx"
slim_filename = f"{directory}/{request.node.name}_slim.onnx"
torch.onnx.export(model, x, filename)
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
Expand All @@ -75,7 +77,7 @@ def test_timm(self, request, model_name):
print(output)
assert result.returncode == 0

os.remove(filename)
shutil.rmtree(directory, ignore_errors=True)


if __name__ == "__main__":
Expand Down

0 comments on commit f7374fa

Please sign in to comment.