Compute Library
 22.05
SimpleTensorPrinter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018, 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 
25 #include "arm_compute/core/Error.h"
26 
27 #include "tests/RawTensor.h"
28 #include "tests/SimpleTensor.h"
29 
30 #include <iostream>
31 #include <sstream>
32 
33 namespace arm_compute
34 {
35 namespace test
36 {
37 template <typename T>
39 {
40  ARM_COMPUTE_ERROR_ON(input.data() == nullptr);
41 
42  RawTensor tensor(std::move(SimpleTensor<T>(input)));
43 
44  TensorInfo info(tensor.shape(), tensor.num_channels(), tensor.data_type());
45 
46  const DataType dt = info.data_type();
47  const size_t slices2D = info.tensor_shape().total_size_upper(2);
48  const Strides strides = info.strides_in_bytes();
49  const PaddingSize padding = info.padding();
50  const size_t num_channels = info.num_channels();
51 
52  std::ostringstream os;
53 
54  // Set precision
55  if(is_data_type_float(dt) && (io_fmt.precision_type != IOFormatInfo::PrecisionType::Default))
56  {
57  int precision = io_fmt.precision;
58  if(io_fmt.precision_type == IOFormatInfo::PrecisionType::Full)
59  {
60  precision = std::numeric_limits<float>().max_digits10;
61  }
62  os.precision(precision);
63  }
64 
65  // Define region to print
66  size_t print_width = 0;
67  size_t print_height = 0;
68  int start_offset = 0;
69  switch(io_fmt.print_region)
70  {
72  print_width = info.dimension(0);
73  print_height = info.dimension(1);
74  start_offset = info.offset_first_element_in_bytes();
75  break;
77  print_width = info.valid_region().shape.x();
78  print_height = info.valid_region().shape.y();
79  start_offset = info.offset_element_in_bytes(Coordinates(info.valid_region().anchor.x(),
80  info.valid_region().anchor.y()));
81  break;
83  print_width = padding.left + info.dimension(0) + padding.right;
84  print_height = padding.top + info.dimension(1) + padding.bottom;
85  start_offset = static_cast<int>(info.offset_first_element_in_bytes()) - padding.top * strides[1] - padding.left * strides[0];
86  break;
87  default:
88  break;
89  }
90 
91  print_width = print_width * num_channels;
92 
93  // Set pointer to start
94  const uint8_t *ptr = tensor.data() + start_offset;
95 
96  // Start printing
97  for(size_t i = 0; i < slices2D; ++i)
98  {
99  // Find max_width of elements in slice to align columns
100  int max_element_width = 0;
101  if(io_fmt.align_columns)
102  {
103  size_t offset = i * strides[2];
104  for(size_t h = 0; h < print_height; ++h)
105  {
106  max_element_width = std::max<int>(max_element_width, max_consecutive_elements_display_width(os, dt, ptr + offset, print_width));
107  offset += strides[1];
108  }
109  }
110 
111  // Print slice
112  {
113  size_t offset = i * strides[2];
114  for(size_t h = 0; h < print_height; ++h)
115  {
116  print_consecutive_elements(os, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim);
117  offset += strides[1];
118  os << io_fmt.row_delim;
119  }
120  os << io_fmt.row_delim;
121  }
122  }
123 
124  return os.str();
125 }
126 
127 template <typename T>
128 inline std::ostream &operator<<(std::ostream &os, const SimpleTensor<T> &tensor)
129 {
131  return os;
132 }
133 
134 template <typename T>
135 inline std::string to_string(const SimpleTensor<T> &tensor)
136 {
137  std::stringstream ss;
138  ss << tensor;
139  return ss.str();
140 }
141 
142 #if PRINT_TENSOR_LIMIT
143 template <typename T>
144 void print_simpletensor(const SimpleTensor<T> &tensor, const std::string &title, const IOFormatInfo::PrintRegion &region = IOFormatInfo::PrintRegion::NoPadding)
145 {
146  if(tensor.num_elements() < PRINT_TENSOR_LIMIT)
147  {
148  std::cout << title << ":" << std::endl;
149  std::cout << prettify_tensor(tensor, IOFormatInfo{ region });
150  }
151 }
152 #endif // PRINT_TENSOR_LIMIT
153 } // namespace test
154 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1083
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:2351
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:2362
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