xref: /aosp_15_r20/external/ComputeLibrary/src/core/ITensor.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-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 #include "arm_compute/core/ITensor.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/Window.h"
29 
30 #include <cstring>
31 #include <limits>
32 
33 namespace arm_compute
34 {
copy_from(const ITensor & src)35 void ITensor::copy_from(const ITensor &src)
36 {
37     if(&src == this)
38     {
39         return;
40     }
41 
42     const ITensorInfo *src_info = src.info();
43     ITensorInfo       *dst_info = this->info();
44 
45     ARM_COMPUTE_ERROR_ON(src_info->num_dimensions() > dst_info->num_dimensions());
46     ARM_COMPUTE_ERROR_ON(src_info->num_channels() != dst_info->num_channels());
47     ARM_COMPUTE_ERROR_ON(src_info->element_size() != dst_info->element_size());
48 
49     for(size_t d = 0; d < src_info->num_dimensions(); d++)
50     {
51         ARM_COMPUTE_ERROR_ON(src_info->dimension(d) > dst_info->dimension(d));
52     }
53 
54     // Copy information about valid region
55     dst_info->set_valid_region(src_info->valid_region());
56 
57     Window win_src;
58     win_src.use_tensor_dimensions(src_info->tensor_shape(), Window::DimY);
59     Window win_dst;
60     win_dst.use_tensor_dimensions(dst_info->tensor_shape(), Window::DimY);
61 
62     Iterator src_it(&src, win_src);
63     Iterator dst_it(this, win_dst);
64 
65     const size_t line_size = src_info->element_size() * src_info->dimension(0);
66 
67     execute_window_loop(
68         win_src, [&](const Coordinates &)
69     {
70         memcpy(dst_it.ptr(), src_it.ptr(), line_size);
71     },
72     src_it, dst_it);
73 }
74 
75 #ifdef ARM_COMPUTE_ASSERTS_ENABLED
print(std::ostream & s,IOFormatInfo io_fmt) const76 void ITensor::print(std::ostream &s, IOFormatInfo io_fmt) const
77 {
78     ARM_COMPUTE_ERROR_ON(this->buffer() == nullptr);
79 
80     const DataType    dt           = this->info()->data_type();
81     const size_t      slices2D     = this->info()->tensor_shape().total_size_upper(2);
82     const Strides     strides      = this->info()->strides_in_bytes();
83     const PaddingSize padding      = this->info()->padding();
84     const size_t      num_channels = this->info()->num_channels();
85     std::ios          stream_status(nullptr);
86     stream_status.copyfmt(s);
87 
88     // Set precision
89     if(is_data_type_float(dt) && (io_fmt.precision_type != IOFormatInfo::PrecisionType::Default))
90     {
91         int precision = io_fmt.precision;
92         if(io_fmt.precision_type == IOFormatInfo::PrecisionType::Full)
93         {
94             precision = std::numeric_limits<float>().max_digits10;
95         }
96         s.precision(precision);
97     }
98 
99     // Define region to print
100     size_t print_width  = 0;
101     size_t print_height = 0;
102     int    start_offset = 0;
103     switch(io_fmt.print_region)
104     {
105         case IOFormatInfo::PrintRegion::NoPadding:
106             print_width  = this->info()->dimension(0);
107             print_height = this->info()->dimension(1);
108             start_offset = this->info()->offset_first_element_in_bytes();
109             break;
110         case IOFormatInfo::PrintRegion::ValidRegion:
111             print_width  = this->info()->valid_region().shape.x();
112             print_height = this->info()->valid_region().shape.y();
113             start_offset = this->info()->offset_element_in_bytes(Coordinates(this->info()->valid_region().anchor.x(),
114                                                                              this->info()->valid_region().anchor.y()));
115             break;
116         case IOFormatInfo::PrintRegion::Full:
117             print_width  = padding.left + this->info()->dimension(0) + padding.right;
118             print_height = padding.top + this->info()->dimension(1) + padding.bottom;
119             start_offset = static_cast<int>(this->info()->offset_first_element_in_bytes()) - padding.top * strides[1] - padding.left * strides[0];
120             break;
121         default:
122             break;
123     }
124 
125     print_width = print_width * num_channels;
126 
127     // Set pointer to start
128     const uint8_t *ptr = this->buffer() + start_offset;
129 
130     // Start printing
131     for(size_t i = 0; i < slices2D; ++i)
132     {
133         // Find max_width of elements in slice to align columns
134         int max_element_width = 0;
135         if(io_fmt.align_columns)
136         {
137             size_t offset = i * strides[2];
138             for(size_t h = 0; h < print_height; ++h)
139             {
140                 max_element_width = std::max<int>(max_element_width, max_consecutive_elements_display_width(s, dt, ptr + offset, print_width));
141                 offset += strides[1];
142             }
143         }
144 
145         // Print slice
146         {
147             size_t offset = i * strides[2];
148             for(size_t h = 0; h < print_height; ++h)
149             {
150                 print_consecutive_elements(s, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim);
151                 offset += strides[1];
152                 s << io_fmt.row_delim;
153             }
154             s << io_fmt.row_delim;
155         }
156     }
157 
158     // Restore output stream flags
159     s.copyfmt(stream_status);
160 }
161 #endif /* ARM_COMPUTE_ASSERTS_ENABLED */
162 
is_used() const163 bool ITensor::is_used() const
164 {
165     return _is_used;
166 }
167 
mark_as_unused() const168 void ITensor::mark_as_unused() const
169 {
170     _is_used = false;
171 }
172 
mark_as_used() const173 void ITensor::mark_as_used() const
174 {
175     _is_used = true;
176 }
177 } // namespace arm_compute