blob: 20d448d713a8d525b1926d31a0b9c239bef2d21f [file] [log] [blame]
Louis Verhaardfa2f92a2020-09-21 11:56:18 +02001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Description:
18# Unit tests for support_operators
Michael McGeagh37ded342020-10-01 15:37:44 +010019import numpy as np
20
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020021from ethosu.vela.data_type import DataType
Louis Verhaardaee5d752020-09-30 09:01:52 +020022from ethosu.vela.operation import Op
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020023from ethosu.vela.supported_operators import SupportedOperators
24from ethosu.vela.tensor import create_const_tensor
Michael McGeagh37ded342020-10-01 15:37:44 +010025from ethosu.vela.tensor import QuantizationParameters
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020026from ethosu.vela.tensor import Tensor
27from ethosu.vela.test import testutil
28
29support = SupportedOperators()
30
31
32def create_strided_slice_op(in_shape, out_shape, start_offsets, end_offsets):
33 in0 = Tensor(in_shape, DataType.uint8, "in")
34 in1 = create_const_tensor("begin", [len(start_offsets)], DataType.uint8, start_offsets)
35 in2 = create_const_tensor("end", [len(end_offsets)], DataType.uint8, end_offsets)
36 in3 = create_const_tensor("strides", [len(end_offsets)], DataType.uint8, len(end_offsets) * [1])
37 out = Tensor(out_shape, DataType.uint8, "out")
38 attrs = {"ellipsis_mask": 0, "new_axis_mask": 0, "shrink_axis_mask": 0, "begin_mask": 0, "end_mask": 0}
Louis Verhaardaee5d752020-09-30 09:01:52 +020039 return testutil.create_op(Op.StridedSlice, [in0, in1, in2, in3], out, attrs=attrs)
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020040
41
42def create_strided_slice():
43 # Creates a valid strided slice operator with some valid inputs/outputs
44 op = create_strided_slice_op([1, 10, 10, 10], [1, 5, 5, 10], [127, 2, 2, 0], [0, 7, -3, 0])
45 op.attrs["begin_mask"] = 1
46 op.attrs["end_mask"] = 9
47 assert support.is_operator_supported(op)
48 return op
49
50
51def test_strided_slice():
52 # Tests support for StridedSlice operator
53 op = create_strided_slice()
54 # Setting one of new_axis_mask/shrink_axis_mask to non-zero is ok
55 op.attrs["new_axis_mask"] = 2
56 assert support.is_operator_supported(op)
57 op = create_strided_slice()
58 op.attrs["shrink_axis_mask"] = 3
59 assert support.is_operator_supported(op)
60 # But setting both to non-zero is not supported
61 op.attrs["new_axis_mask"] = 2
62 assert not support.is_operator_supported(op)
63 # begin values must not be None
64 op.inputs[1].values = None
65 assert not support.is_operator_supported(op)
66 # Unsupported strides
67 op = create_strided_slice()
68 op.inputs[3].values = [1, 1, 2, 1]
69 assert not support.is_operator_supported(op)
70 # Wrong number of input tensors
71 op = create_strided_slice()
72 op.add_input_tensor(op.inputs[0].clone())
73 assert not support.is_operator_supported(op)
74 # Unsupported ellipsis mask
75 op = create_strided_slice()
76 op.attrs["ellipsis_mask"] = 1
77 assert not support.is_operator_supported(op)
78 # Examples where end offset <= begin offset
79 op = create_strided_slice()
80 op.inputs[1].values = [0, 7, 2, 0]
81 assert not support.is_operator_supported(op)
82 op = create_strided_slice()
83 op.inputs[2].values = [0, 7, 2, 0]
84 assert not support.is_operator_supported(op)
85 op = create_strided_slice()
86 op.attrs["begin_mask"] = 0
87 assert not support.is_operator_supported(op)
88 op = create_strided_slice()
89 op.attrs["end_mask"] = 0
90 assert not support.is_operator_supported(op)
Michael McGeagh37ded342020-10-01 15:37:44 +010091
92
93def test_constraint_tens_defined_shape():
94 # Tensors cannot have None in them
95 inp = Tensor([1, 8, None, 8], DataType.uint8, "in")
96 out = Tensor([1, 8, 8, 8], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +020097 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +010098 assert not support.is_operator_supported(op)
99
100
101def test_constraint_tens_shapeless():
102 # Shapeless input is allowed if its of a certain type:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200103 op = testutil.create_elemwise_op(Op.Mul, "scalar_mul", [1, 8, 8, 8], [], [1, 8, 8, 8])
Michael McGeagh37ded342020-10-01 15:37:44 +0100104 assert support.is_operator_supported(op)
105 # Shapeless output is not allowed at all:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200106 op = testutil.create_elemwise_op(Op.Mul, "scalar_mul", [1, 8, 8, 8], [1, 8, 8, 8], [])
Michael McGeagh37ded342020-10-01 15:37:44 +0100107 assert not support.is_operator_supported(op)
108 # Invalid shapeless input due to op type:
109 inp = Tensor([], DataType.uint8, "in")
110 out = Tensor([1, 8, 8, 8], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200111 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100112 assert not support.is_operator_supported(op)
113
114
115def test_constraint_tens_shape_size():
116 # Tensors cannot be > 4D
117 inp = Tensor([1, 1, 8, 8, 8], DataType.uint8, "in")
118 out = Tensor([1, 1, 8, 8, 8], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200119 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100120 assert not support.is_operator_supported(op)
121
122
123def test_constraint_tens_dtype():
124 # Tensors can only be of type uint8, int8, int16 (and int32)
125 inp = Tensor([1, 8, 8, 8], DataType.float32, "in")
126 out = Tensor([1, 8, 8, 8], DataType.float32, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200127 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100128 assert not support.is_operator_supported(op)
129 # For int32, only select op types are allowed:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200130 op = testutil.create_elemwise_op(Op.Mul, "scalar_mul", [1, 8, 8, 8], [], [1, 8, 8, 8], DataType.int32)
Michael McGeagh37ded342020-10-01 15:37:44 +0100131 assert support.is_operator_supported(op)
132 inp = Tensor([1, 8, 8, 8], DataType.int32, "in")
133 out = Tensor([1, 8, 8, 8], DataType.int32, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200134 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100135 assert not support.is_operator_supported(op)
136
137
138def test_constraint_tens_dimension():
139 # Tensors can only have values in the inclusive range of 1-65535
140 inp = Tensor([1, 8, 8, 0], DataType.uint8, "in")
141 out = Tensor([1, 8, 8, 0], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200142 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100143 assert not support.is_operator_supported(op)
144 inp = Tensor([1, 8, 8, 65536], DataType.uint8, "in")
145 out = Tensor([1, 8, 8, 65536], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200146 op = testutil.create_op(Op.Relu, [inp], out)
Michael McGeagh37ded342020-10-01 15:37:44 +0100147 assert not support.is_operator_supported(op)
148
149
150def test_constraint_faf():
151 # Fused activation functions, if set, must be a valid op type
152 inp = Tensor([1, 8, 8, 8], DataType.uint8, "in")
153 out = Tensor([1, 8, 8, 8], DataType.uint8, "out")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200154 op = testutil.create_op(Op.Relu, [inp], out)
155 op.activation = Op.Conv2D
Michael McGeagh37ded342020-10-01 15:37:44 +0100156 assert not support.is_operator_supported(op)
157
158
159def test_constraint_tens_quant_scale():
160 # Quantization scale cannot be infinit
Louis Verhaardaee5d752020-09-30 09:01:52 +0200161 op = testutil.create_elemwise_op(Op.Mul, "scalar_mul", [1, 8, 8, 8], [], [1, 8, 8, 8])
Michael McGeagh37ded342020-10-01 15:37:44 +0100162 op.inputs[0].quantization = QuantizationParameters()
163 op.inputs[0].quantization.scale_f32 = np.inf
164 assert not support.is_operator_supported(op)