Spaces:
Sleeping
Sleeping
File size: 9,634 Bytes
dc2106c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# Copyright (c) ONNX Project Contributors
# SPDX-License-Identifier: Apache-2.0
import typing
import unittest
import onnx
import onnx.parser
import onnx.shape_inference
class TestModelInference(unittest.TestCase):
def _check(self, model_text: str, *expected: int):
"""Check that the model inference infers the expected types for outputs.
Restricted to the simple case of tensor types, so expected types specify
only the element type (ints corresponding to onnx.TensorProto.DataType).
"""
model = onnx.parser.parse_model(model_text)
inferred = onnx.shape_inference.infer_shapes(model)
outputs = inferred.graph.output
for output, expected_elem_type in zip(outputs, expected):
inferred_type = output.type
self.assertTrue(inferred_type.HasField("tensor_type"))
tensor_type = inferred_type.tensor_type
self.assertTrue(tensor_type.HasField("elem_type"))
elem_type = tensor_type.elem_type
self.assertEqual(elem_type, expected_elem_type)
def _check_inference_error(self, model_text: str):
"""Check that the model inference raises an InferenceError."""
model = onnx.parser.parse_model(model_text)
with self.assertRaises(onnx.shape_inference.InferenceError):
onnx.shape_inference.infer_shapes(model, True, True)
def test_unknown_op(self):
"""Test that model inference handles unknown ops.
This special treatment is to support custom ops.
See comments in shape inference code for details.
"""
model = """
<ir_version: 7, opset_import: [ "" : 17]>
agraph (float[N] x) => (y)
{
y = SomeUnknownOp (x)
}
"""
# No output types are inferred for unknown ops.
# But ensure that the inference does not fail.
self._check(model)
def test_mi_basic(self):
"""Test that model inference infers model output type."""
model = """
<
ir_version: 7,
opset_import: [ "" : 17]
>
agraph (float[N] x) => (y)
{
y = Cast<to=6> (x)
}
"""
self._check(model, onnx.TensorProto.INT32)
def test_mi_function(self):
"""Test use of functions."""
model = """
<
ir_version: 7,
opset_import: [ "" : 17, "local" : 1]
>
agraph (float[N] x) => (y)
{
y = local.cast(x)
}
<
opset_import: [ "" : 17 ],
domain: "local"
>
cast (x) => (y)
{
y = Cast<to=6> (x)
}
"""
self._check(model, onnx.TensorProto.INT32)
def test_mi_function_attr(self):
"""Test use of functions with attribute parameters."""
model = """
<
ir_version: 7,
opset_import: [ "" : 17, "local" : 1]
>
agraph (float[N] x) => (y)
{
y = local.cast<target=6>(x)
}
<
opset_import: [ "" : 17 ],
domain: "local"
>
cast<target>(x) => (y)
{
y = Cast<to:int = @target> (x)
}
"""
self._check(model, onnx.TensorProto.INT32)
def test_mi_function_subgraph_attr(self):
"""Test use of function attributes within subgraphs."""
model = """
<
ir_version: 7,
opset_import: [ "" : 17, "local" : 1]
>
agraph (float[N] x, bool flag) => (y)
{
y = local.cast<target=6>(x, flag)
}
<
opset_import: [ "" : 17 ],
domain: "local"
>
cast<target>(x, flag) => (y)
{
y = If (flag) <
then_branch = g1 () => (z_then) { z_then = Cast<to:int = @target> (x) },
else_branch = g2 () => (z_else) { z_else = Cast<to:int = @target> (x) }
>
}
"""
self._check(model, onnx.TensorProto.INT32)
def test_mi_function_multiple_calls(self):
"""Test use of multiple invocation of functions."""
model = """
<
ir_version: 7,
opset_import: [ "" : 17, "local" : 1]
>
agraph (float[N] x, bool flag) => (y, z)
{
y = local.cast<target=6>(x, flag)
z = local.cast<target=7>(x, flag)
}
<
opset_import: [ "" : 17 ],
domain: "local"
>
cast<target>(x, flag) => (y)
{
y = If (flag) <
then_branch = g1 () => (z_then) { z_then = Cast<to:int = @target> (x) },
else_branch = g2 () => (z_else) { z_else = Cast<to:int = @target> (x) }
>
}
"""
self._check(model, onnx.TensorProto.INT32, onnx.TensorProto.INT64)
def _check_shape(self, model_text: str, *expected: typing.Sequence[int]):
"""Check that the model inference infers the expected shapes for outputs.
Restricted to the simple case of tensor type outputs with completely
known shapes.
"""
model = onnx.parser.parse_model(model_text)
inferred = onnx.shape_inference.infer_shapes(model, True, True, True)
outputs = inferred.graph.output
for output, expected_shape in zip(outputs, expected):
inferred_type = output.type
self.assertTrue(inferred_type.HasField("tensor_type"))
tensor_type = inferred_type.tensor_type
self.assertTrue(tensor_type.HasField("shape"))
inferred_shape = tensor_type.shape
self.assertEqual(len(inferred_shape.dim), len(expected_shape))
for inferred_dim, expected_dim in zip(inferred_shape.dim, expected_shape):
self.assertTrue(inferred_dim.HasField("dim_value"))
self.assertEqual(inferred_dim.dim_value, expected_dim)
def test_mi_constant(self):
model = """
<
ir_version: 7,
opset_import: [ "" : 17]
>
mymodel (float[4, 8, 16] x) => (y) {
shape = Constant<value_ints=[8,4,16]>()
y = Reshape(x, shape)
}
"""
self._check_shape(model, [8, 4, 16])
def test_mi_constant_2(self):
model = """
<
ir_version: 7,
opset_import: [ "" : 17]
>
mymodel (float[4, 8, 16] x) => (y) {
shape = Constant<value_ints=[4,2,8]>()
two = Constant<value_int=2>()
shape2 = Mul(shape, two)
y = Reshape(x, shape2)
}
"""
self._check_shape(model, [8, 4, 16])
def test_mi_constant_in_function(self):
model = """
<
ir_version: 7,
opset_import: [ "" : 17, "local" : 1]
>
main (float x) => (y, z) {
y, z = local.expand(x)
}
<
opset_import: [ "" : 17 ],
domain: "local"
>
expand (x) => (y, z) {
shape1 = Constant<value = int64[2] {4,4}>()
shape2 = Constant<value = int64[3] {8,8,8}>()
z = Expand (x, shape2)
y = Expand (x, shape1)
}
"""
self._check_shape(model, [4, 4], [8, 8, 8])
def test_mi_function_default_attr(self):
"""Test use of default values of function attributes."""
model = """
<ir_version: 7, opset_import: [ "" : 17, "local" : 1]>
agraph (float[N] x) => (y, z)
{
y = local.cast <target=6> (x) # casts to INT32 type (encoding value 6)
z = local.cast (x) # uses default-attribute value of 1 (FLOAT type)
}
<opset_import: [ "" : 17 ], domain: "local">
cast <target: int = 1> (x) => (y)
{
y = Cast <to:int = @target> (x)
}
"""
self._check(model, onnx.TensorProto.INT32, onnx.TensorProto.FLOAT)
def test_mi_overloaded_function(self):
"""Test use of functions."""
model = """
<ir_version: 10, opset_import: [ "" : 17, "local" : 1]>
agraph (float[N] x) => (y, z)
{
y = local.cast:to_int32 (x)
z = local.cast:to_int64 (x)
}
<opset_import: [ "" : 17 ], domain: "local", overload: "to_int32">
cast (x) => (y)
{
y = Cast<to=6> (x)
}
<opset_import: [ "" : 17 ], domain: "local", overload: "to_int64">
cast (x) => (y)
{
y = Cast<to=7> (x)
}
"""
self._check(model, onnx.TensorProto.INT32, onnx.TensorProto.INT64)
if __name__ == "__main__":
unittest.main()
|