blob: 5d0299a6962a51d77496180c08676f9dfebe82ef [file] [log] [blame]
Abe Mbise1b993382017-12-19 13:51:59 +00001/*
SiCongLiaed61f22021-08-26 17:44:08 +01002 * Copyright (c) 2017-2018, 2021 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{
Abe Mbise1b993382017-12-19 13:51:59 +000037template <typename T>
38inline std::string prettify_tensor(const SimpleTensor<T> &input, const IOFormatInfo &io_fmt = IOFormatInfo{ IOFormatInfo::PrintRegion::NoPadding })
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 {
71 case IOFormatInfo::PrintRegion::NoPadding:
72 print_width = info.dimension(0);
73 print_height = info.dimension(1);
74 start_offset = info.offset_first_element_in_bytes();
75 break;
76 case IOFormatInfo::PrintRegion::ValidRegion:
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;
82 case IOFormatInfo::PrintRegion::Full:
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
127template <typename T>
128inline std::ostream &operator<<(std::ostream &os, const SimpleTensor<T> &tensor)
129{
130 os << prettify_tensor(tensor, IOFormatInfo{ IOFormatInfo::PrintRegion::NoPadding });
131 return os;
132}
133
134template <typename T>
135inline 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
143template <typename T>
144void 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
Abe Mbise1b993382017-12-19 13:51:59 +0000153} // namespace test
154} // namespace arm_compute