Compute Library
 21.02
TensorInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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  */
25 #include "arm_compute/core/Types.h"
27 #include "tests/framework/Macros.h"
30 #include "utils/TypePrinter.h"
31 
32 namespace arm_compute
33 {
34 namespace test
35 {
36 namespace validation
37 {
38 TEST_SUITE(UNIT)
39 TEST_SUITE(TensorInfo)
40 
41 // *INDENT-OFF*
42 // clang-format off
43 /** Validates TensorInfo Autopadding */
44 DATA_TEST_CASE(AutoPadding, framework::DatasetMode::ALL, zip(zip(zip(
45  framework::dataset::make("TensorShape", {
46  TensorShape{},
47  TensorShape{ 10U },
48  TensorShape{ 10U, 10U },
49  TensorShape{ 10U, 10U, 10U },
50  TensorShape{ 10U, 10U, 10U, 10U },
51  TensorShape{ 10U, 10U, 10U, 10U, 10U },
52  TensorShape{ 10U, 10U, 10U, 10U, 10U, 10U }}),
53  framework::dataset::make("PaddingSize", {
54  PaddingSize{ 0, 0, 0, 0 },
55  PaddingSize{ 0, 36, 0, 4 },
56  PaddingSize{ 4, 36, 4, 4 },
57  PaddingSize{ 4, 36, 4, 4 },
58  PaddingSize{ 4, 36, 4, 4 },
59  PaddingSize{ 4, 36, 4, 4 },
60  PaddingSize{ 4, 36, 4, 4 }})),
61  framework::dataset::make("Strides", {
62  Strides{},
63  Strides{ 1U, 50U },
64  Strides{ 1U, 50U },
65  Strides{ 1U, 50U, 900U },
66  Strides{ 1U, 50U, 900U, 9000U },
67  Strides{ 1U, 50U, 900U, 9000U, 90000U },
68  Strides{ 1U, 50U, 900U, 9000U, 90000U, 900000U }})),
69  framework::dataset::make("Offset", { 0U, 4U, 204U, 204U, 204U, 204U, 204U })),
70  shape, auto_padding, strides, offset)
71 {
73 
75 
76  info.auto_padding();
77 
78  validate(info.padding(), auto_padding);
79 
81  ARM_COMPUTE_EXPECT(info.offset_first_element_in_bytes() == offset, framework::LogLevel::ERRORS);
82 }
83 // clang-format on
84 // *INDENT-ON*
85 
86 /** Validates that TensorInfo is clonable */
88 {
89  // Create tensor info
90  TensorInfo info(TensorShape(23U, 17U, 3U), // tensor shape
91  1, // number of channels
92  DataType::F32); // data type
93 
94  // Get clone of current tensor info
95  std::unique_ptr<ITensorInfo> info_clone = info.clone();
96  ARM_COMPUTE_EXPECT(info_clone != nullptr, framework::LogLevel::ERRORS);
97  ARM_COMPUTE_EXPECT(info_clone->total_size() == info.total_size(), framework::LogLevel::ERRORS);
98  ARM_COMPUTE_EXPECT(info_clone->num_channels() == info.num_channels(), framework::LogLevel::ERRORS);
99  ARM_COMPUTE_EXPECT(info_clone->data_type() == info.data_type(), framework::LogLevel::ERRORS);
100 }
101 
102 /** Validates that TensorInfo can chain multiple set commands */
104 {
105  // Create tensor info
106  TensorInfo info(TensorShape(23U, 17U, 3U), // tensor shape
107  1, // number of channels
108  DataType::F32); // data type
109 
110  // Update data type and number of channels
114 
115  // Update data type and set quantization info
119 
120  // Update tensor shape
121  info.set_tensor_shape(TensorShape(13U, 15U));
123 }
124 
125 /** Validates empty quantization info */
127 {
128  // Create tensor info
129  const TensorInfo info(TensorShape(32U, 16U), 1, DataType::F32);
130 
131  // Check quantization information
133 }
134 
135 /** Validates symmetric quantization info */
137 {
138  // Create tensor info
139  const float scale = 0.25f;
141 
142  // Check quantization information
147 
149  ARM_COMPUTE_EXPECT(qinfo.scale == scale, framework::LogLevel::ERRORS);
150  ARM_COMPUTE_EXPECT(qinfo.offset == 0.f, framework::LogLevel::ERRORS);
151 }
152 
153 /** Validates asymmetric quantization info */
154 TEST_CASE(AsymmQuantizationInfo, framework::DatasetMode::ALL)
155 {
156  // Create tensor info
157  const float scale = 0.25f;
158  const int32_t offset = 126;
159  const TensorInfo info(TensorShape(32U, 16U), 1, DataType::QSYMM8, QuantizationInfo(scale, offset));
160 
161  // Check quantization information
167 
169  ARM_COMPUTE_EXPECT(qinfo.scale == scale, framework::LogLevel::ERRORS);
170  ARM_COMPUTE_EXPECT(qinfo.offset == offset, framework::LogLevel::ERRORS);
171 }
172 
173 /** Validates symmetric per channel quantization info */
174 TEST_CASE(SymmPerChannelQuantizationInfo, framework::DatasetMode::ALL)
175 {
176  // Create tensor info
177  const std::vector<float> scale = { 0.25f, 1.4f, 3.2f, 2.3f, 4.7f };
179 
180  // Check quantization information
183  ARM_COMPUTE_EXPECT(info.quantization_info().scale().size() == scale.size(), framework::LogLevel::ERRORS);
185 }
186 
187 TEST_SUITE_END() // TensorInfoValidation
189 } // namespace validation
190 } // namespace test
191 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
const std::vector< int32_t > & offset() const
Offset vector accessor.
virtual ITensorInfo & set_num_channels(int num_channels)=0
Set the number of channels to the specified value.
Shape of a tensor.
Definition: TensorShape.h:39
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:316
Container for 2D border size.
Definition: Types.h:273
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:258
1 channel, 1 U8 per channel
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:311
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:321
Quantization info when assuming per layer quantization.
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
bool compare_dimensions(const Dimensions< T > &dimensions1, const Dimensions< T > &dimensions2, const DataLayout &data_layout=DataLayout::NCHW)
Definition: Validation.h:139
Copyright (c) 2017-2021 Arm Limited.
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:270
1 channel, 1 S32 per channel
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
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:278
UniformQuantizationInfo uniform() const
Return per layer quantization info.
const std::vector< float > & scale() const
Scale vector accessor.
TEST_SUITE(U8_to_S8) FIXTURE_DATA_TEST_CASE(RunSmall
validate(CLAccessor(output_state), expected_output)
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.
quantized, symmetric fixed-point 8-bit number
Strides of an item in bytes.
Definition: Strides.h:37
quantized, symmetric per channel fixed-point 8-bit number
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
bool empty() const
Indicates whether this QuantizationInfo has valid settings or not.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
ITensorInfo & set_tensor_shape(const TensorShape &shape) override
Set the shape of an already initialized tensor.
Definition: TensorInfo.cpp:352
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
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:262