Compute Library
 22.11
SimpleTensorPrinter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018, 2021-2022 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 
25 #ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER
26 #define ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER
27 
28 #include "arm_compute/core/Error.h"
29 
30 #include "tests/RawTensor.h"
31 #include "tests/SimpleTensor.h"
32 
33 #include <iostream>
34 #include <sstream>
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 template <typename T>
42 {
43  ARM_COMPUTE_ERROR_ON(input.data() == nullptr);
44 
45  RawTensor tensor(std::move(SimpleTensor<T>(input)));
46 
47  TensorInfo info(tensor.shape(), tensor.num_channels(), tensor.data_type());
48 
49  const DataType dt = info.data_type();
50  const size_t slices2D = info.tensor_shape().total_size_upper(2);
51  const Strides strides = info.strides_in_bytes();
52  const PaddingSize padding = info.padding();
53  const size_t num_channels = info.num_channels();
54 
55  std::ostringstream os;
56 
57  // Set precision
58  if(is_data_type_float(dt) && (io_fmt.precision_type != IOFormatInfo::PrecisionType::Default))
59  {
60  int precision = io_fmt.precision;
61  if(io_fmt.precision_type == IOFormatInfo::PrecisionType::Full)
62  {
63  precision = std::numeric_limits<float>().max_digits10;
64  }
65  os.precision(precision);
66  }
67 
68  // Define region to print
69  size_t print_width = 0;
70  size_t print_height = 0;
71  int start_offset = 0;
72  switch(io_fmt.print_region)
73  {
75  print_width = info.dimension(0);
76  print_height = info.dimension(1);
77  start_offset = info.offset_first_element_in_bytes();
78  break;
80  print_width = info.valid_region().shape.x();
81  print_height = info.valid_region().shape.y();
82  start_offset = info.offset_element_in_bytes(Coordinates(info.valid_region().anchor.x(),
83  info.valid_region().anchor.y()));
84  break;
86  print_width = padding.left + info.dimension(0) + padding.right;
87  print_height = padding.top + info.dimension(1) + padding.bottom;
88  start_offset = static_cast<int>(info.offset_first_element_in_bytes()) - padding.top * strides[1] - padding.left * strides[0];
89  break;
90  default:
91  break;
92  }
93 
94  print_width = print_width * num_channels;
95 
96  // Set pointer to start
97  const uint8_t *ptr = tensor.data() + start_offset;
98 
99  // Start printing
100  for(size_t i = 0; i < slices2D; ++i)
101  {
102  // Find max_width of elements in slice to align columns
103  int max_element_width = 0;
104  if(io_fmt.align_columns)
105  {
106  size_t offset = i * strides[2];
107  for(size_t h = 0; h < print_height; ++h)
108  {
109  max_element_width = std::max<int>(max_element_width, max_consecutive_elements_display_width(os, dt, ptr + offset, print_width));
110  offset += strides[1];
111  }
112  }
113 
114  // Print slice
115  {
116  size_t offset = i * strides[2];
117  for(size_t h = 0; h < print_height; ++h)
118  {
119  print_consecutive_elements(os, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim);
120  offset += strides[1];
121  os << io_fmt.row_delim;
122  }
123  os << io_fmt.row_delim;
124  }
125  }
126 
127  return os.str();
128 }
129 
130 template <typename T>
131 inline std::ostream &operator<<(std::ostream &os, const SimpleTensor<T> &tensor)
132 {
134  return os;
135 }
136 
137 template <typename T>
138 inline std::string to_string(const SimpleTensor<T> &tensor)
139 {
140  std::stringstream ss;
141  ss << tensor;
142  return ss.str();
143 }
144 
145 #if PRINT_TENSOR_LIMIT
146 template <typename T>
147 void print_simpletensor(const SimpleTensor<T> &tensor, const std::string &title, const IOFormatInfo::PrintRegion &region = IOFormatInfo::PrintRegion::NoPadding)
148 {
149  if(tensor.num_elements() < PRINT_TENSOR_LIMIT)
150  {
151  std::cout << title << ":" << std::endl;
152  std::cout << prettify_tensor(tensor, IOFormatInfo{ region });
153  }
154 }
155 #endif // PRINT_TENSOR_LIMIT
156 } // namespace test
157 } // namespace arm_compute
158 #endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER */
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1084
unsigned int top
top of the border
Definition: Types.h:390
Subclass of SimpleTensor using uint8_t as value type.
Definition: RawTensor.h:38
std::string prettify_tensor(const SimpleTensor< T > &input, const IOFormatInfo &io_fmt=IOFormatInfo{ IOFormatInfo::PrintRegion::NoPadding })
Container for 2D border size.
Definition: Types.h:282
Prints the Tensor object without the padding.
std::stringstream ss(mlgo_str)
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
unsigned int bottom
bottom of the border
Definition: Types.h:392
Copyright (c) 2017-2022 Arm Limited.
Print the tensor object including padding.
IO formatting information class.
Definition: Types.h:2640
Coordinates of an item.
Definition: Coordinates.h:37
unsigned int left
left of the border
Definition: Types.h:393
unsigned int right
right of the border
Definition: Types.h:391
Strides of an item in bytes.
Definition: Strides.h:37
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
PrintRegion
Specifies the area to be printed, used by Tensor objects.
Definition: Types.h:2651
CLTensor * tensor
Pointer to the auxiliary tensor.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
Prints the valid region of the Tensor object.
Default precision to the one that the current stream has.
DataType
Available data types.
Definition: Types.h:79
The maximum precision of the floating point representation.
std::string to_string(const SimpleTensor< T > &tensor)
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
Definition: Utils.h:1010
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:418