Compute Library
 21.05
PReluLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/core/Types.h"
28 #include "tests/CL/CLAccessor.h"
30 #include "tests/datasets/ConvertPolicyDataset.h"
31 #include "tests/datasets/ShapeDatasets.h"
33 #include "tests/framework/Macros.h"
36 #include "tests/validation/fixtures/ElementwiseOperationsFixture.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 RelativeTolerance<float> tolerance_fp32(0.000001f);
47 RelativeTolerance<float> tolerance_fp16(0.001f);
48 
49 /** Input data sets **/
50 const auto PReluLayerU8Dataset = combine(combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)),
51  framework::dataset::make("DataType",
52  DataType::U8));
53 const auto PReluLayerQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
54  framework::dataset::make("DataType",
56 const auto PReluLayerQASYMM8SIGNEDDataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8_SIGNED), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
57  framework::dataset::make("DataType",
59 const auto PReluLayerS16Dataset = combine(combine(framework::dataset::make("DataType", { DataType::U8, DataType::S16 }), framework::dataset::make("DataType", DataType::S16)),
61 const auto PReluLayerFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
63 const auto PReluLayerFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
65 } // namespace
66 
67 TEST_SUITE(CL)
68 TEST_SUITE(PReluLayer)
69 
70 // *INDENT-OFF*
71 // clang-format off
73  framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
74  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
75  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
76  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
77  }),
78  framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
79  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
80  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
81  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
82  })),
83  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
84  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
85  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
86  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
87  })),
88  framework::dataset::make("Expected", { true, true, false, false})),
89  input1_info, input2_info, output_info, expected)
90 {
91  ARM_COMPUTE_EXPECT(bool(CLPReluLayer::validate(&input1_info.clone()->set_is_resizable(false), &input2_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS);
92 }
93 // clang-format on
94 // *INDENT-ON*
95 
96 TEST_SUITE(InPlace)
97 TEST_CASE(Validate, framework::DatasetMode::ALL)
98 {
99  // PRelu operaotr should be able to take nullptr as output and do the in-place computation.
100  // Shape and data type are selected randomly since they shouldn't matter
101  const auto tensor_info = TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32);
102  const auto result = arm_compute::CLPReluLayer::validate(&tensor_info, &tensor_info, nullptr);
103  ARM_COMPUTE_EXPECT(bool(result) == true, framework::LogLevel::ERRORS);
104 }
105 
107 {
108  SimpleTensor<float> ref_src1{ tensor_info.tensor_shape(), tensor_info.data_type() };
109  SimpleTensor<float> ref_src2{ tensor_info.tensor_shape(), tensor_info.data_type() };
110  SimpleTensor<float> ref_dst{ tensor_info.tensor_shape(), tensor_info.data_type() };
111 
112  library->fill_tensor_uniform(ref_src1, 0);
113  library->fill_tensor_uniform(ref_src2, 1);
114 
115  return reference::arithmetic_operation<float>(ArithmeticOperation::PRELU, ref_src1, ref_src2, ref_dst);
116 }
117 
118 void compute_float_target_in_place(CLTensor &src1, CLTensor &src2, bool use_nullptr_output)
119 {
120  auto fn = arm_compute::CLPReluLayer{};
121  fn.configure(&src1, &src2, use_nullptr_output ? nullptr : &src1);
122 
123  src1.allocator()->allocate();
124  src2.allocator()->allocate();
125 
126  library->fill_tensor_uniform(CLAccessor(src1), 0);
127  library->fill_tensor_uniform(CLAccessor(src2), 1);
128 
129  fn.run();
130 }
131 
133 {
134  const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
135 
136  auto src1 = create_tensor<CLTensor>(tensor_info);
137  auto src2 = create_tensor<CLTensor>(tensor_info);
138  compute_float_target_in_place(src1, src2, true);
139  validate(CLAccessor(src1), compute_float_reference(tensor_info));
140 }
141 
142 TEST_CASE(ComputeWithSameTensor, framework::DatasetMode::ALL)
143 {
144  const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
145 
146  auto src1 = create_tensor<CLTensor>(tensor_info);
147  auto src2 = create_tensor<CLTensor>(tensor_info);
148  compute_float_target_in_place(src1, src2, false);
149  validate(CLAccessor(src1), compute_float_reference(tensor_info));
150 }
151 TEST_SUITE_END() // InPlace
152 
153 template <typename T>
154 using CLPReluLayerFixture = PReluLayerValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
155 
156 TEST_SUITE(U8)
157 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), PReluLayerU8Dataset))
158 {
159  // Validate output
160  validate(CLAccessor(_target), _reference);
161 }
163 
164 template <typename T>
165 using CLPReluLayerQuantizedFixture = PReluLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
166 
167 TEST_SUITE(Quantized)
168 TEST_SUITE(QASYMM8)
169 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
170  PReluLayerQASYMM8Dataset),
171  framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 255.f, 20) })),
172  framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
173  framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 5) }))
174 
175  )
176 {
177  // Validate output
178  validate(CLAccessor(_target), _reference);
179 }
181 
182 TEST_SUITE(QASYMM8_SIGNED)
183 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
184  PReluLayerQASYMM8SIGNEDDataset),
185  framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 127.f, 20) })),
186  framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 127.f, 10) })),
187  framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 127.f, 5) }))
188 
189  )
190 {
191  // Validate output
192  validate(CLAccessor(_target), _reference);
193 }
196 
197 TEST_SUITE(S16)
198 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<int16_t>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerS16Dataset))
199 {
200  // Validate output
201  validate(CLAccessor(_target), _reference);
202 }
204 
205 TEST_SUITE(Float)
206 TEST_SUITE(FP16)
207 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<half>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP16Dataset))
208 {
209  // Validate output
210  validate(CLAccessor(_target), _reference, tolerance_fp16, 0.01);
211 }
213 
214 TEST_SUITE(FP32)
215 FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP32Dataset))
216 {
217  // Validate output
218  validate(CLAccessor(_target), _reference, tolerance_fp32);
219 }
220 template <typename T>
221 using CLPReluLayerBroadcastFixture = PReluLayerBroadcastValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
222 
224  PReluLayerFP32Dataset))
225 {
226  // Validate output
227  validate(CLAccessor(_target), _reference, tolerance_fp32);
228 }
231 
234 } // namespace validation
235 } // namespace test
236 } // namespace arm_compute
Basic function to run opencl::kernels::ClArithmeticKernel for PRELU.
Definition: CLPReluLayer.h:39
Shape of a tensor.
Definition: TensorShape.h:39
void compute_float_target_in_place(CLTensor &src1, CLTensor &src2, bool use_nullptr_output)
Definition: PReluLayer.cpp:118
1 channel, 1 U8 per channel
half_float::half half
16-bit floating point type
Definition: Types.h:46
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
RelativeTolerance< float > tolerance_fp32(0.001f)
PReluLayerBroadcastValidationFixture< CLTensor, CLAccessor, CLPReluLayer, T > CLPReluLayerBroadcastFixture
Definition: PReluLayer.cpp:221
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:242
void configure(ICLTensor *input, ICLTensor *alpha, ICLTensor *output)
Set the input and output tensor.
Quantization information.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:75
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:36
static Status validate(const ITensorInfo *input, const ITensorInfo *alpha, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLPReluLayer.
TEST_SUITE(U8_to_S8) FIXTURE_DATA_TEST_CASE(RunSmall
validate(CLAccessor(output_state), expected_output)
PReluLayerValidationQuantizedFixture< CLTensor, CLAccessor, CLPReluLayer, T > CLPReluLayerQuantizedFixture
Definition: PReluLayer.cpp:165
1 channel, 1 S16 per channel
SimpleTensor< float > compute_float_reference(const TensorInfo &tensor_info)
Definition: PReluLayer.cpp:106
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
Store the tensor's metadata.
Definition: TensorInfo.h:43
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
quantized, asymmetric fixed-point 8-bit number signed
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:234
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41