Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 25 | #include "src/cl/CLTensorArgument.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 26 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 27 | #include "ckw/Error.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 28 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 29 | #include "src/cl/CLHelpers.h" |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 30 | #include "src/cl/CLTensorComponent.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 31 | #include "src/ITensorArgument.h" |
| 32 | #include "src/ITensorComponent.h" |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 33 | #include "src/types/TensorComponentType.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <vector> |
| 37 | |
| 38 | namespace ckw |
| 39 | { |
| 40 | CLTensorArgument::CLTensorArgument(const std::string &name, const TensorInfo &info, bool return_dims_by_value) |
| 41 | { |
| 42 | _return_dims_by_value = return_dims_by_value; |
| 43 | _basename = name; |
| 44 | _info = info; |
| 45 | } |
| 46 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 47 | CLTensorArgument::~CLTensorArgument() = default; |
| 48 | |
| 49 | CLTensorComponent &CLTensorArgument::cl_component(TensorComponentType x) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 50 | { |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 51 | // Return the component if it has already been created. |
| 52 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 53 | const auto it = |
| 54 | std::find_if(_components_used.begin(), _components_used.end(), |
| 55 | [=](const std::unique_ptr<CLTensorComponent> &item) { return item->component_type() == x; }); |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 56 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 57 | if (it != _components_used.end()) |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 58 | { |
| 59 | return **it; |
| 60 | } |
| 61 | } |
| 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 63 | if (_return_dims_by_value) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 64 | { |
| 65 | uint32_t component_type = static_cast<uint32_t>(x); |
| 66 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 67 | const bool is_dimension = (component_type & static_cast<uint32_t>(TensorComponentBitmask::Dimension)) != 0; |
| 68 | const bool is_folded_dimensions = |
| 69 | (component_type & static_cast<uint32_t>(TensorComponentBitmask::FoldedDimensions)) != 0; |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 70 | |
| 71 | constexpr auto bitmask_all = static_cast<uint32_t>(TensorComponentIndexBitmask::All); |
| 72 | constexpr auto bitmask_index_0 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index0); |
| 73 | #ifdef COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED |
| 74 | constexpr auto bitmask_index_1 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index1); |
| 75 | constexpr auto bitmask_index_2 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index2); |
| 76 | constexpr auto bitmask_index_3 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index3); |
| 77 | #endif // COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED |
| 78 | |
| 79 | // Make sure that the encoding of component type hasn't changed and each nibble is 4 bits apart. |
| 80 | CKW_ASSERT(bitmask_all == (bitmask_index_0 | bitmask_index_1 | bitmask_index_2 | bitmask_index_3)); |
| 81 | CKW_ASSERT(bitmask_index_0 == bitmask_index_1 >> 4); |
| 82 | CKW_ASSERT(bitmask_index_1 == bitmask_index_2 >> 4); |
| 83 | CKW_ASSERT(bitmask_index_2 == bitmask_index_3 >> 4); |
| 84 | |
| 85 | // If we have a dimension or folded dimensions, we can return the corresponding value if it is not dynamic (not equal to -1) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 86 | if (is_dimension == true || is_folded_dimensions == true) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 87 | { |
| 88 | component_type = component_type & bitmask_all; |
| 89 | |
| 90 | int32_t idx = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 91 | for (int32_t i = 0; i < tensor_component_index_max_count; ++i) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 92 | { |
| 93 | uint32_t dim_idx = component_type & bitmask_index_0; |
| 94 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 95 | if (dim_idx == 0) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 96 | { |
| 97 | // Stop at the first nibble containing 0 |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | // Subtract - 1. Please refer to the TensorComponentIndexBitmask documentation |
| 102 | dim_idx -= 1; |
| 103 | |
| 104 | // Get the dimension value |
| 105 | const int32_t dim_val = _info.shape()[dim_idx]; |
| 106 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 107 | if (dim_val == kDynamicTensorDimensionValue) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 108 | { |
| 109 | // We cannot return the dimension by value if it is dynamic. |
| 110 | // Therefore, force the idx variable to kDynamicTensorDimensionValue and break the loop. |
| 111 | idx = kDynamicTensorDimensionValue; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | idx *= dim_val; |
| 116 | |
| 117 | // Go to the next nibble |
| 118 | component_type >>= 4; |
| 119 | } |
| 120 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 121 | if (idx != kDynamicTensorDimensionValue) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 122 | { |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 123 | _components_used.emplace_back(std::make_unique<CLTensorComponent>(*this, x, idx)); |
| 124 | |
| 125 | return *_components_used.back(); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 130 | _components_used.emplace_back(std::make_unique<CLTensorComponent>(*this, x)); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 131 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 132 | return *_components_used.back(); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 135 | ITile &CLTensorArgument::component(TensorComponentType x) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 136 | { |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 137 | return cl_component(x); |
| 138 | } |
| 139 | |
| 140 | TensorStorageVariable &CLTensorArgument::storage(TensorStorageType x) |
| 141 | { |
| 142 | // Return the storage if it has already been created. |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 143 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 144 | const auto it = std::find_if(_storages_used.begin(), _storages_used.end(), |
| 145 | [=](const TensorStorageVariable &item) { return item.type == x; }); |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 146 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 147 | if (it != _storages_used.end()) |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 148 | { |
| 149 | return *it; |
| 150 | } |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TensorStorageVariable t; |
| 154 | t.val = create_storage_name(x); |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 155 | t.type = x; |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 156 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 157 | _storages_used.emplace_back(t); |
| 158 | |
| 159 | return _storages_used.back(); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | std::string CLTensorArgument::create_storage_name(TensorStorageType x) const |
| 163 | { |
| 164 | std::string var_name = _basename; |
| 165 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 166 | switch (x) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 167 | { |
| 168 | case TensorStorageType::BufferUint8Ptr: |
| 169 | var_name += "_ptr"; |
| 170 | break; |
| 171 | case TensorStorageType::Texture2dReadOnly: |
| 172 | case TensorStorageType::Texture2dWriteOnly: |
| 173 | var_name += "_img2d"; |
| 174 | break; |
| 175 | default: |
| 176 | CKW_ASSERT_FAILED_MSG("Unsupported tensor storage"); |
| 177 | return ""; |
| 178 | } |
| 179 | |
| 180 | return var_name; |
| 181 | } |
| 182 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 183 | std::vector<TensorStorageVariable> CLTensorArgument::storages() const |
| 184 | { |
| 185 | std::vector<TensorStorageVariable> storages; |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 186 | storages.reserve(_storages_used.size()); |
| 187 | |
| 188 | std::copy(_storages_used.begin(), _storages_used.end(), std::back_inserter(storages)); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 189 | |
| 190 | return storages; |
| 191 | } |
| 192 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 193 | std::vector<const ITensorComponent *> CLTensorArgument::components() const |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 194 | { |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 195 | std::vector<const ITensorComponent *> components; |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 196 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 197 | for (const auto &component : _components_used) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 198 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 199 | if (component->is_assignable()) |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 200 | { |
| 201 | components.push_back(component.get()); |
| 202 | } |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return components; |
| 206 | } |
| 207 | } // namespace ckw |