Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Window.h" |
| 30 | |
| 31 | #include <cstring> |
| 32 | #include <limits> |
| 33 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | void ITensor::copy_from(const ITensor &src) |
| 37 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 38 | if (&src == this) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | const ITensorInfo *src_info = src.info(); |
| 44 | ITensorInfo *dst_info = this->info(); |
| 45 | |
| 46 | ARM_COMPUTE_ERROR_ON(src_info->num_dimensions() > dst_info->num_dimensions()); |
| 47 | ARM_COMPUTE_ERROR_ON(src_info->num_channels() != dst_info->num_channels()); |
| 48 | ARM_COMPUTE_ERROR_ON(src_info->element_size() != dst_info->element_size()); |
| 49 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | for (size_t d = 0; d < src_info->num_dimensions(); d++) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON(src_info->dimension(d) > dst_info->dimension(d)); |
| 53 | } |
| 54 | |
| 55 | // Copy information about valid region |
| 56 | dst_info->set_valid_region(src_info->valid_region()); |
| 57 | |
| 58 | Window win_src; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 59 | win_src.use_tensor_dimensions(src_info->tensor_shape(), Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | Window win_dst; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 61 | win_dst.use_tensor_dimensions(dst_info->tensor_shape(), Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | |
| 63 | Iterator src_it(&src, win_src); |
| 64 | Iterator dst_it(this, win_dst); |
| 65 | |
Anthony Barbier | 5376c4c | 2018-07-13 09:48:58 +0100 | [diff] [blame] | 66 | const size_t line_size = src_info->element_size() * src_info->dimension(0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 68 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | win_src, [&](const Coordinates &) { memcpy(dst_it.ptr(), src_it.ptr(), line_size); }, src_it, dst_it); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | } |
| 71 | |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 72 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | void ITensor::print(std::ostream &s, IOFormatInfo io_fmt) const |
| 74 | { |
| 75 | ARM_COMPUTE_ERROR_ON(this->buffer() == nullptr); |
| 76 | |
Ioan-Cristian Szabo | 5712091 | 2017-12-12 09:56:21 +0000 | [diff] [blame] | 77 | const DataType dt = this->info()->data_type(); |
| 78 | const size_t slices2D = this->info()->tensor_shape().total_size_upper(2); |
| 79 | const Strides strides = this->info()->strides_in_bytes(); |
| 80 | const PaddingSize padding = this->info()->padding(); |
| 81 | const size_t num_channels = this->info()->num_channels(); |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 82 | std::ios stream_status(nullptr); |
| 83 | stream_status.copyfmt(s); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | |
| 85 | // Set precision |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | if (is_data_type_float(dt) && (io_fmt.precision_type != IOFormatInfo::PrecisionType::Default)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | { |
| 88 | int precision = io_fmt.precision; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | if (io_fmt.precision_type == IOFormatInfo::PrecisionType::Full) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
| 91 | precision = std::numeric_limits<float>().max_digits10; |
| 92 | } |
| 93 | s.precision(precision); |
| 94 | } |
| 95 | |
| 96 | // Define region to print |
| 97 | size_t print_width = 0; |
| 98 | size_t print_height = 0; |
| 99 | int start_offset = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | switch (io_fmt.print_region) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | { |
| 102 | case IOFormatInfo::PrintRegion::NoPadding: |
| 103 | print_width = this->info()->dimension(0); |
| 104 | print_height = this->info()->dimension(1); |
| 105 | start_offset = this->info()->offset_first_element_in_bytes(); |
| 106 | break; |
| 107 | case IOFormatInfo::PrintRegion::ValidRegion: |
| 108 | print_width = this->info()->valid_region().shape.x(); |
| 109 | print_height = this->info()->valid_region().shape.y(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | start_offset = this->info()->offset_element_in_bytes( |
| 111 | Coordinates(this->info()->valid_region().anchor.x(), this->info()->valid_region().anchor.y())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | break; |
| 113 | case IOFormatInfo::PrintRegion::Full: |
| 114 | print_width = padding.left + this->info()->dimension(0) + padding.right; |
| 115 | print_height = padding.top + this->info()->dimension(1) + padding.bottom; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 116 | start_offset = static_cast<int>(this->info()->offset_first_element_in_bytes()) - padding.top * strides[1] - |
| 117 | padding.left * strides[0]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
Ioan-Cristian Szabo | 5712091 | 2017-12-12 09:56:21 +0000 | [diff] [blame] | 123 | print_width = print_width * num_channels; |
| 124 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | // Set pointer to start |
| 126 | const uint8_t *ptr = this->buffer() + start_offset; |
| 127 | |
| 128 | // Start printing |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | for (size_t i = 0; i < slices2D; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | { |
| 131 | // Find max_width of elements in slice to align columns |
| 132 | int max_element_width = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | if (io_fmt.align_columns) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | { |
| 135 | size_t offset = i * strides[2]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | for (size_t h = 0; h < print_height; ++h) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | max_element_width = std::max<int>( |
| 139 | max_element_width, max_consecutive_elements_display_width(s, dt, ptr + offset, print_width)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | offset += strides[1]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Print slice |
| 145 | { |
| 146 | size_t offset = i * strides[2]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | for (size_t h = 0; h < print_height; ++h) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | { |
| 149 | print_consecutive_elements(s, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim); |
| 150 | offset += strides[1]; |
| 151 | s << io_fmt.row_delim; |
| 152 | } |
| 153 | s << io_fmt.row_delim; |
| 154 | } |
| 155 | } |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 156 | |
| 157 | // Restore output stream flags |
| 158 | s.copyfmt(stream_status); |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 159 | } |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 160 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 161 | |
| 162 | bool ITensor::is_used() const |
| 163 | { |
| 164 | return _is_used; |
| 165 | } |
| 166 | |
| 167 | void ITensor::mark_as_unused() const |
| 168 | { |
| 169 | _is_used = false; |
| 170 | } |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 171 | |
| 172 | void ITensor::mark_as_used() const |
| 173 | { |
| 174 | _is_used = true; |
| 175 | } |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 176 | } // namespace arm_compute |