Compute Library
 20.02.1
Utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 "Utils.h"
25 
27 
28 namespace arm_compute
29 {
30 namespace test
31 {
32 namespace validation
33 {
34 // Return the bilinear value at a specified coordinate with different border modes
35 template <typename T>
36 T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value)
37 {
38  const int idx = std::floor(xn);
39  const int idy = std::floor(yn);
40 
41  const float dx = xn - idx;
42  const float dy = yn - idy;
43  const float dx_1 = 1.0f - dx;
44  const float dy_1 = 1.0f - dy;
45 
46  const T border_value = constant_border_value;
47 
48  id.set(0, idx);
49  id.set(1, idy);
50  const float tl = tensor_elem_at(in, id, border_mode, border_value);
51  id.set(0, idx + 1);
52  id.set(1, idy);
53  const float tr = tensor_elem_at(in, id, border_mode, border_value);
54  id.set(0, idx);
55  id.set(1, idy + 1);
56  const float bl = tensor_elem_at(in, id, border_mode, border_value);
57  id.set(0, idx + 1);
58  id.set(1, idy + 1);
59  const float br = tensor_elem_at(in, id, border_mode, border_value);
60 
61  return static_cast<T>(tl * (dx_1 * dy_1) + tr * (dx * dy_1) + bl * (dx_1 * dy) + br * (dx * dy));
62 }
63 
64 template int8_t bilinear_policy(const SimpleTensor<int8_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int8_t constant_border_value);
65 template uint8_t bilinear_policy(const SimpleTensor<uint8_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, uint8_t constant_border_value);
66 template int16_t bilinear_policy(const SimpleTensor<int16_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int16_t constant_border_value);
67 template half bilinear_policy(const SimpleTensor<half> &in, Coordinates id, float xn, float yn, BorderMode border_mode, half constant_border_value);
68 template float bilinear_policy(const SimpleTensor<float> &in, Coordinates id, float xn, float yn, BorderMode border_mode, float constant_border_value);
69 
70 RawTensor transpose(const RawTensor &src, int chunk_width)
71 {
72  // Create reference
73  TensorShape dst_shape(src.shape());
74  dst_shape.set(0, src.shape().y() * chunk_width);
75  dst_shape.set(1, std::ceil(src.shape().x() / static_cast<float>(chunk_width)));
76 
77  RawTensor dst{ dst_shape, src.data_type() };
78 
79  // Compute reference
80  uint8_t *out_ptr = dst.data();
81 
82  for(int i = 0; i < dst.num_elements(); i += chunk_width)
83  {
84  Coordinates coord = index2coord(dst.shape(), i);
85  size_t coord_x = coord.x();
86  coord.set(0, coord.y() * chunk_width);
87  coord.set(1, coord_x / chunk_width);
88 
89  const int num_elements = std::min<int>(chunk_width, src.shape().x() - coord.x());
90 
91  std::copy_n(static_cast<const uint8_t *>(src(coord)), num_elements * src.element_size(), out_ptr);
92 
93  out_ptr += chunk_width * dst.element_size();
94  }
95 
96  return dst;
97 }
98 
99 bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode)
100 {
102  {
103  return true;
104  }
105  if((0 <= yn + 1) && (yn + 1 < height) && (0 <= xn + 1) && (xn + 1 < width))
106  {
107  return true;
108  }
109  return false;
110 }
111 } // namespace validation
112 } // namespace test
113 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:261
T tensor_elem_at(const SimpleTensor< T > &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
Definition: Utils.h:61
Subclass of SimpleTensor using uint8_t as value type.
Definition: RawTensor.h:38
Shape of a tensor.
Definition: TensorShape.h:39
half_float::half half
16-bit floating point type
Definition: Types.h:45
void set(size_t dimension, T value)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:74
Copyright (c) 2017-2020 ARM Limited.
RawTensor transpose(const RawTensor &src, int chunk_width)
Definition: Utils.cpp:70
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
Coordinates of an item.
Definition: Coordinates.h:37
T bilinear_policy(const SimpleTensor< T > &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value)
Definition: Utils.cpp:36
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:456
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
Borders are left undefined.
bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode)
Definition: Utils.cpp:99
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
cast configure & src
Definition: Cast.cpp:169