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
|
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from __future__ import annotations
from typing import Tuple, Union
import numpy as np
from onnx import TensorProto
from onnx.helper import make_tensor
from onnxscript import script
from onnxscript.onnx_opset import opset15 as op
from onnxscript.onnx_types import INT32, INT64
from tests.common.onnx_script_test_case import FunctionTestParams
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=np.int32)
zero = np.array(0, dtype=np.int64)
# Inputs/Outputs of test-cases are specified as numpy arrays, or tuples of numpy arrays,
# or as a list of values, e.g. [0, 1, 2], converted to an int32 numpy array.
IOType = Union[np.ndarray, Tuple[np.ndarray, ...], list]
def wrap_input_output(x: IOType) -> list(np.ndarray):
if isinstance(x, np.ndarray):
return [x]
elif isinstance(x, tuple):
return list(x)
else:
return [np.array(x, dtype=np.int32)]
def test(f, input: IOType, output: IOType) -> FunctionTestParams:
return FunctionTestParams(f, wrap_input_output(input), wrap_input_output(output))
@script(default_opset=op)
def get_first_row(A: INT32[...]) -> INT32[...]:
return A[0]
get_first_row_test = test(get_first_row, input=x, output=[0, 1, 2])
@script(default_opset=op)
def get_last_row(A: INT32[...]) -> INT32[...]:
return A[-1]
get_last_row_test = test(get_last_row, input=x, output=[9, 10, 11])
@script(default_opset=op)
def get_column(A: INT32[...]) -> INT32[...]:
return A[:, 1]
get_column_test = test(get_column, input=x, output=[1, 4, 7, 10])
@script(default_opset=op)
def get_unknown_row(A: INT32[...], i: INT64[...]) -> INT32[...]:
return A[i]
get_unknown_row_test = test(get_unknown_row, input=(x, zero), output=[0, 1, 2])
@script(default_opset=op)
def get_computed_row(A: INT32[...]) -> INT32[...]:
scalar_zero = op.Constant(value=make_tensor("scalar_zero", TensorProto.INT64, [], [0]))
return A[scalar_zero + 1]
get_computed_row_test = test(get_computed_row, input=x, output=[3, 4, 5])
@script(default_opset=op)
def slice_from_1_to_2(A: INT32[...]) -> INT32[...]:
return A[1:2]
slice_from_to = test(slice_from_1_to_2, input=x, output=[[3, 4, 5]])
@script(default_opset=op)
def slice_from_1(A: INT32[...]) -> INT32[...]:
return A[1:]
slice_from = test(slice_from_1, input=x, output=[[3, 4, 5], [6, 7, 8], [9, 10, 11]])
@script(default_opset=op)
def slice_to_2(A: INT32[...]) -> INT32[...]:
return A[:2]
slice_to = test(slice_to_2, input=x, output=[[0, 1, 2], [3, 4, 5]])
@script(default_opset=op)
def slice_step_minus1(A: INT32[...]) -> INT32[...]:
return A[::-1]
slice_neg_step = test(
slice_step_minus1, input=x, output=[[9, 10, 11], [6, 7, 8], [3, 4, 5], [0, 1, 2]]
)
@script(default_opset=op)
def slice_from_1_to_minus1(A: INT32[...]) -> INT32[...]:
return A[1:-1]
slice_from_neg_to = test(slice_from_1_to_minus1, input=x, output=[[3, 4, 5], [6, 7, 8]])
@script(default_opset=op)
def slice_to_0_step_minus1(A: INT32[...]) -> INT32[...]:
return A[:0:-1]
slice_to_neg_step = test(slice_to_0_step_minus1, input=x, output=x[:0:-1])
@script(default_opset=op)
def get_row_slice_column(A: INT32[...]) -> INT32[...]:
return A[0, :0:-1]
index_and_slice = test(get_row_slice_column, input=x, output=x[0, :0:-1])
@script(default_opset=op)
def slice_row_get_column(A: INT32[...]) -> INT32[...]:
return A[:2, 0]
slice_and_index = test(slice_row_get_column, input=x, output=x[:2, 0])
@script(default_opset=op)
def slice_row_and_column(A: INT32[...]) -> INT32[...]:
return A[:2, :1]
slice_and_slice = test(slice_row_and_column, input=x, output=x[:2, :1])
@script(default_opset=op)
def slice_from_2_to_0_step_minus1(A: INT32[...]) -> INT32[...]:
return A[2:0:-1]
slice_from_to_neg_step = test(slice_from_2_to_0_step_minus1, input=x, output=x[2:0:-1])
@script()
def slice_computed_range(A: INT32[...]) -> INT32[...]:
scalar_zero = op.Constant(value=make_tensor("scalar_zero", TensorProto.INT64, [], [0]))
return A[scalar_zero + 1 : scalar_zero + 2]
slice_computed_range_test = test(slice_computed_range, input=x, output=x[1:2])
@script(default_opset=op)
def nested_expr(A: INT32[...]) -> INT32[...]:
r = (A + 1)[0]
return r
nested_expr_test = test(nested_expr, input=x, output=[1, 2, 3])
# This notation is not possible with ONNX but is allowed by numpy.
# @script()
# def getitem_i_slice_right_step(A):
# r = A[1::-1]
# return r
|