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