forked from PaddlePaddle/Paddle-Lite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_elementwise_pow_op.py
More file actions
182 lines (162 loc) · 7.61 KB
/
Copy pathtest_elementwise_pow_op.py
File metadata and controls
182 lines (162 loc) · 7.61 KB
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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
sys.path.append('../')
from auto_scan_test import AutoScanTest, IgnoreReasons
from program_config import TensorConfig, ProgramConfig, OpConfig, CxxConfig, TargetType, PrecisionType, DataLayoutType, Place
import unittest
import hypothesis
from hypothesis import given, settings, seed, example, assume, reproduce_failure
import hypothesis.strategies as st
import numpy as np
from functools import partial
from test_elementwise_add_op import check_broadcast
class TestElementwisePowOp(AutoScanTest):
def __init__(self, *args, **kwargs):
AutoScanTest.__init__(self, *args, **kwargs)
self.enable_testing_on_place(
TargetType.X86,
PrecisionType.FP32,
DataLayoutType.NCHW,
thread=[1, 4])
self.enable_testing_on_place(
TargetType.ARM, [PrecisionType.FP32],
DataLayoutType.NCHW,
thread=[1, 4])
opencl_valid_places = [
Place(TargetType.OpenCL, PrecisionType.FP16,
DataLayoutType.ImageDefault), Place(
TargetType.OpenCL, PrecisionType.FP16,
DataLayoutType.ImageFolder),
Place(TargetType.OpenCL, PrecisionType.FP32, DataLayoutType.NCHW),
Place(TargetType.OpenCL, PrecisionType.Any,
DataLayoutType.ImageDefault), Place(
TargetType.OpenCL, PrecisionType.Any,
DataLayoutType.ImageFolder),
Place(TargetType.OpenCL, PrecisionType.Any, DataLayoutType.NCHW),
Place(TargetType.Host, PrecisionType.FP32)
]
self.enable_testing_on_place(places=opencl_valid_places)
self.enable_testing_on_place(TargetType.NNAdapter, PrecisionType.FP32)
self.enable_devices_on_nnadapter(device_names=[
"nvidia_tensorrt", "intel_openvino", "kunlunxin_xtcl"
])
def is_program_valid(self,
program_config: ProgramConfig,
predictor_config: CxxConfig) -> bool:
target_type = predictor_config.target()
input_data_type = program_config.inputs["input_data_x"].dtype
# Check config
if target_type in [TargetType.ARM]:
if predictor_config.precision(
) == PrecisionType.FP16 and input_data_type != np.float32:
return False
return True
def sample_program_configs(self, draw):
input_data_x_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=20), min_size=1, max_size=4))
input_data_y_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=20), min_size=1, max_size=4))
input_data_x_shape = draw(st.sampled_from([input_data_x_shape, []]))
input_data_y_shape = draw(st.sampled_from([input_data_y_shape, []]))
axis = draw(st.integers(min_value=-1, max_value=4))
assume(
check_broadcast(input_data_x_shape, input_data_y_shape, axis) ==
True)
if axis < 0:
axis = abs(len(input_data_x_shape) - len(
input_data_y_shape)) + axis + 1
if self.get_target().upper() == 'X86':
input_data_type = draw(
st.sampled_from([np.float32, np.int32, np.int64]))
elif self.get_target().upper() == 'ARM':
input_data_type = draw(st.sampled_from([np.float32, np.int32]))
elif self.get_target().upper() == 'OPENCL':
input_data_type = draw(st.sampled_from([np.float32]))
elif self.get_target().upper() == 'METAL':
input_data_type = draw(st.sampled_from([np.float32]))
elif self.get_target().upper() == 'NNADAPTER':
input_data_type = draw(st.sampled_from([np.float32]))
def gen_input_data(*args, **kwargs):
# The value is small because we do pow operation. The result will be very large if input data is large.
return np.random.randint(
1, 5, size=(kwargs['shape'])).astype(kwargs['dtype'])
elementwise_pow_op = OpConfig(
type="elementwise_pow",
inputs={"X": ["input_data_x"],
"Y": ["input_data_y"]},
outputs={"Out": ["output_data"]},
attrs={"axis": axis})
program_config = ProgramConfig(
ops=[elementwise_pow_op],
weights={},
inputs={
"input_data_x": TensorConfig(data_gen=partial(
gen_input_data,
shape=input_data_x_shape,
dtype=input_data_type)),
"input_data_y": TensorConfig(data_gen=partial(
gen_input_data,
shape=input_data_y_shape,
dtype=input_data_type))
},
outputs=["output_data"])
return program_config
def sample_predictor_configs(self):
return self.get_predictor_configs(), ["elementwise_pow"], (1e-5, 1e-5)
def add_ignore_pass_case(self):
def teller1(program_config, predictor_config):
input_data_shape = program_config.inputs["input_data_x"].shape
if len(input_data_shape) != 4:
return True
return False
self.add_ignore_check_case(
teller1, IgnoreReasons.ACCURACY_ERROR,
"The elementwise_pow op's result is different from paddle, because paddle has bug on this op, wait paddle fix!"
)
def _teller2(program_config, predictor_config):
if "nvidia_tensorrt" in self.get_nnadapter_device_name():
x_shape = program_config.inputs["input_data_x"].shape
y_shape = program_config.inputs["input_data_y"].shape
axis = program_config.ops[0].attrs["axis"]
if len(x_shape) == 1 \
or len(x_shape) != len(y_shape) \
or x_shape[0] != y_shape[0] \
or axis == 0:
return True
self.add_ignore_check_case(
_teller2, IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"Lite does not support 'x_shape_size == 1' or 'x_shape_size != y_shape_size' "
"or 'x_shape[0] != y_shape[0]' or 'axis == 0' on NvidiaTensorrt.")
def _teller3(program_config, predictor_config):
target_type = predictor_config.target()
in_x_shape = list(program_config.inputs["input_data_x"].shape)
in_y_shape = list(program_config.inputs["input_data_y"].shape)
if target_type not in [
TargetType.ARM, TargetType.Host, TargetType.X86,
TargetType.Metal, TargetType.OpenCL
]:
if len(in_x_shape) == 0 or len(in_y_shape) == 0:
return True
self.add_ignore_check_case(
_teller3, IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"0D-tensor is not supported on this target now.")
def test(self, *args, **kwargs):
self.run_and_statis(quant=False, max_examples=300)
if __name__ == "__main__":
unittest.main(argv=[''])