Compute Library
 19.08
ITensor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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 
26 #include "arm_compute/core/Error.h"
29 
30 #include <cstring>
31 #include <limits>
32 
33 using namespace arm_compute;
34 
36 {
37  if(&src == this)
38  {
39  return;
40  }
41 
42  const ITensorInfo *src_info = src.info();
43  ITensorInfo *dst_info = this->info();
44 
45  ARM_COMPUTE_ERROR_ON(src_info->num_dimensions() > dst_info->num_dimensions());
48 
49  for(size_t d = 0; d < src_info->num_dimensions(); d++)
50  {
52  }
53 
54  // Copy information about valid region
56 
57  Window win_src;
59  Window win_dst;
60  win_dst.use_tensor_dimensions(dst_info->tensor_shape(), Window::DimY);
61 
62  Iterator src_it(&src, win_src);
63  Iterator dst_it(this, win_dst);
64 
65  const size_t line_size = src_info->element_size() * src_info->dimension(0);
66 
67  execute_window_loop(win_src, [&](const Coordinates &)
68  {
69  memcpy(dst_it.ptr(), src_it.ptr(), line_size);
70  },
71  src_it, dst_it);
72 }
73 
74 #ifdef ARM_COMPUTE_ASSERTS_ENABLED
75 void ITensor::print(std::ostream &s, IOFormatInfo io_fmt) const
76 {
77  ARM_COMPUTE_ERROR_ON(this->buffer() == nullptr);
78 
79  const DataType dt = this->info()->data_type();
80  const size_t slices2D = this->info()->tensor_shape().total_size_upper(2);
81  const Strides strides = this->info()->strides_in_bytes();
82  const PaddingSize padding = this->info()->padding();
83  const size_t num_channels = this->info()->num_channels();
84  std::ios stream_status(nullptr);
85  stream_status.copyfmt(s);
86 
87  // Set precision
89  {
90  int precision = io_fmt.precision;
92  {
93  precision = std::numeric_limits<float>().max_digits10;
94  }
95  s.precision(precision);
96  }
97 
98  // Define region to print
99  size_t print_width = 0;
100  size_t print_height = 0;
101  int start_offset = 0;
102  switch(io_fmt.print_region)
103  {
105  print_width = this->info()->dimension(0);
106  print_height = this->info()->dimension(1);
107  start_offset = this->info()->offset_first_element_in_bytes();
108  break;
110  print_width = this->info()->valid_region().shape.x();
111  print_height = this->info()->valid_region().shape.y();
112  start_offset = this->info()->offset_element_in_bytes(Coordinates(this->info()->valid_region().anchor.x(),
113  this->info()->valid_region().anchor.y()));
114  break;
116  print_width = padding.left + this->info()->dimension(0) + padding.right;
117  print_height = padding.top + this->info()->dimension(1) + padding.bottom;
118  start_offset = static_cast<int>(this->info()->offset_first_element_in_bytes()) - padding.top * strides[1] - padding.left * strides[0];
119  break;
120  default:
121  break;
122  }
123 
124  print_width = print_width * num_channels;
125 
126  // Set pointer to start
127  const uint8_t *ptr = this->buffer() + start_offset;
128 
129  // Start printing
130  for(size_t i = 0; i < slices2D; ++i)
131  {
132  // Find max_width of elements in slice to align columns
133  int max_element_width = 0;
134  if(io_fmt.align_columns)
135  {
136  size_t offset = i * strides[2];
137  for(size_t h = 0; h < print_height; ++h)
138  {
139  max_element_width = std::max<int>(max_element_width, max_consecutive_elements_display_width(s, dt, ptr + offset, print_width));
140  offset += strides[1];
141  }
142  }
143 
144  // Print slice
145  {
146  size_t offset = i * strides[2];
147  for(size_t h = 0; h < print_height; ++h)
148  {
149  print_consecutive_elements(s, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim);
150  offset += strides[1];
151  s << io_fmt.row_delim;
152  }
153  s << io_fmt.row_delim;
154  }
155  }
156 
157  // Restore output stream flags
158  s.copyfmt(stream_status);
159 }
160 #endif /* ARM_COMPUTE_ASSERTS_ENABLED */
161 
162 bool ITensor::is_used() const
163 {
164  return _is_used;
165 }
166 
168 {
169  _is_used = false;
170 }
PrecisionType precision_type
Floating point precision type.
Definition: Types.h:2111
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:328
unsigned int top
top of the border
Definition: Types.h:339
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
TensorShape shape
Shape of the valid region.
Definition: Types.h:247
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:259
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:285
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:248
Prints the Tensor object without the padding.
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:223
virtual DataType data_type() const =0
Data type used for each element of the tensor.
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
virtual size_t offset_element_in_bytes(const Coordinates &pos) const =0
The offset in bytes from the beginning of the memory allocation to access the element at position (x,...
unsigned int precision
Floating point precision.
Definition: Types.h:2113
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
size_t total_size_upper(size_t dimension) const
Collapses given dimension and above.
Definition: TensorShape.h:181
Store the tensor's metadata.
Definition: ITensorInfo.h:40
unsigned int bottom
bottom of the border
Definition: Types.h:341
PrintRegion print_region
Area to be printed by Tensor objects.
Definition: Types.h:2109
Interface for NEON tensor.
Definition: ITensor.h:36
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:250
Copyright (c) 2017-2018 ARM Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
size_t num_dimensions() const override
The number of dimensions of the tensor (rank)
Definition: TensorInfo.h:244
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
std::string row_delim
Row delimeter.
Definition: Types.h:2117
Print the tensor object including padding.
IO formatting information class.
Definition: Types.h:2066
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:184
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
virtual PaddingSize padding() const =0
Padding of tensor.
bool align_columns
Align columns.
Definition: Types.h:2119
unsigned int left
left of the border
Definition: Types.h:342
unsigned int right
right of the border
Definition: Types.h:340
void copy_from(const ITensor &src)
Copy the content of another tensor.
Definition: ITensor.cpp:35
TensorInfo src_info(src_shape, 1, data_type)
Strides of an item in bytes.
Definition: Strides.h:37
virtual size_t offset_first_element_in_bytes() const =0
The offset from the beginning of the memory allocation to the first element of the tensor.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:122
void print(std::ostream &s, IOFormatInfo io_fmt=IOFormatInfo()) const
Print a tensor to a given stream using user defined formatting information.
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
Prints the valid region of the Tensor object.
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:252
Default precision to the one that the current stream has.
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:318
DataType
Available data types.
Definition: Types.h:74
The maximum precision of the floating point representation.
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: TensorInfo.h:240
Describe a multidimensional execution window.
Definition: Window.h:39
virtual size_t num_channels() const =0
The number of channels for each tensor element.
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:246
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
Definition: Utils.h:990
std::string element_delim
Element delimeter.
Definition: Types.h:2115
cast configure & src
Definition: Cast.cpp:169