blob: e4ca66bb362b86b6f3af69ddb04e7b7ad334464f [file] [log] [blame]
Abe Mbise1b993382017-12-19 13:51:59 +00001/*
Ramy Elgammal73f19af2022-10-23 11:44:49 +01002 * Copyright (c) 2017-2018, 2021-2022 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
Ramy Elgammal73f19af2022-10-23 11:44:49 +010025#ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER
26#define ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER
27
Abe Mbise1b993382017-12-19 13:51:59 +000028#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
36namespace arm_compute
37{
38namespace test
39{
Abe Mbise1b993382017-12-19 13:51:59 +000040template <typename T>
41inline std::string prettify_tensor(const SimpleTensor<T> &input, const IOFormatInfo &io_fmt = IOFormatInfo{ IOFormatInfo::PrintRegion::NoPadding })
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 {
74 case IOFormatInfo::PrintRegion::NoPadding:
75 print_width = info.dimension(0);
76 print_height = info.dimension(1);
77 start_offset = info.offset_first_element_in_bytes();
78 break;
79 case IOFormatInfo::PrintRegion::ValidRegion:
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;
85 case IOFormatInfo::PrintRegion::Full:
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
130template <typename T>
131inline std::ostream &operator<<(std::ostream &os, const SimpleTensor<T> &tensor)
132{
133 os << prettify_tensor(tensor, IOFormatInfo{ IOFormatInfo::PrintRegion::NoPadding });
134 return os;
135}
136
137template <typename T>
138inline 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
146template <typename T>
147void 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
Abe Mbise1b993382017-12-19 13:51:59 +0000156} // namespace test
157} // namespace arm_compute
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100158#endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_PRINTER */