|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +import unittest |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import onnxruntime as ort |
| 7 | + |
| 8 | +from onnxscript import script |
| 9 | +from onnxscript.onnx_opset import opset15 as op |
| 10 | +from onnxscript.onnx_types import FLOAT |
| 11 | +from onnxscript.utils.model_proto_to_function_proto import ( |
| 12 | + convert_model_proto_to_function_proto, |
| 13 | +) |
| 14 | +from onnxscript.values import Opset |
| 15 | + |
| 16 | + |
| 17 | +class TestModelProtoToFunctionProto(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + """Set up test fixtures.""" |
| 20 | + # Create a fresh custom opset for each test |
| 21 | + self.local = Opset("local", 1) |
| 22 | + |
| 23 | + # Define test functions |
| 24 | + @script(self.local, default_opset=op) |
| 25 | + def diff_square(x, y): |
| 26 | + diff = x - y |
| 27 | + return diff * diff |
| 28 | + |
| 29 | + @script(self.local) |
| 30 | + def sum_func(z): |
| 31 | + return op.ReduceSum(z, keepdims=1) |
| 32 | + |
| 33 | + @script() |
| 34 | + def l2norm(x: FLOAT["N"], y: FLOAT["N"]) -> FLOAT[1]: # noqa: F821 |
| 35 | + return op.Sqrt(sum_func(diff_square(x, y))) |
| 36 | + |
| 37 | + @script() |
| 38 | + def l2norm_with_functions(x: FLOAT["N"], y: FLOAT["N"]) -> FLOAT[1]: # noqa: F821 |
| 39 | + return op.Sqrt(sum_func(diff_square(x, y))) |
| 40 | + |
| 41 | + self.diff_square = diff_square |
| 42 | + self.sum_func = sum_func |
| 43 | + self.l2norm = l2norm |
| 44 | + self.l2norm_with_functions = l2norm_with_functions |
| 45 | + |
| 46 | + def test_multiple_functions_in_model_proto(self): |
| 47 | + """Test that multiple functions can be included in a single model proto.""" |
| 48 | + # Add sum function to opset |
| 49 | + sum_model = self.sum_func.to_model_proto() |
| 50 | + sum_function_proto = convert_model_proto_to_function_proto( |
| 51 | + sum_model, "local", "sum_func" |
| 52 | + ) |
| 53 | + |
| 54 | + model = self.l2norm_with_functions.to_model_proto( |
| 55 | + functions=[sum_function_proto, self.diff_square] |
| 56 | + ) |
| 57 | + |
| 58 | + # Test execution |
| 59 | + session = ort.InferenceSession(model.SerializeToString()) |
| 60 | + result = session.run( |
| 61 | + None, |
| 62 | + { |
| 63 | + "x": np.array([1.0, 2.0, 3.0]).astype(np.float32), |
| 64 | + "y": np.array([4.0, 5.0, 6.0]).astype(np.float32), |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + # Verify result |
| 69 | + self.assertEqual(len(result), 1) |
| 70 | + self.assertAlmostEqual(np.sqrt(27.0), result[0][0], places=5) # L2 norm of [3, 3, 3] |
0 commit comments