blob: e53de2830d2ff5931b3b1c600a7cb6311050f8f0 [file] [log] [blame]
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +01001/*
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010027#include "ckw/Error.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010029#include "src/cl/CLHelpers.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010030#include "src/cl/CLTensorComponent.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "src/ITensorArgument.h"
32#include "src/ITensorComponent.h"
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010033#include "src/types/TensorComponentType.h"
34
35#include <algorithm>
36#include <vector>
37
38namespace ckw
39{
40CLTensorArgument::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 Do0b23e0e2023-07-25 14:00:46 +010047CLTensorArgument::~CLTensorArgument() = default;
48
49CLTensorComponent &CLTensorArgument::cl_component(TensorComponentType x)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010050{
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010051 // Return the component if it has already been created.
52 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 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 Do0b23e0e2023-07-25 14:00:46 +010056
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 if (it != _components_used.end())
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010058 {
59 return **it;
60 }
61 }
62
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 if (_return_dims_by_value)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010064 {
65 uint32_t component_type = static_cast<uint32_t>(x);
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 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 Iodiceebfdb5a2023-07-07 11:25:57 +010070
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (is_dimension == true || is_folded_dimensions == true)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010087 {
88 component_type = component_type & bitmask_all;
89
90 int32_t idx = 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 for (int32_t i = 0; i < tensor_component_index_max_count; ++i)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010092 {
93 uint32_t dim_idx = component_type & bitmask_index_0;
94
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 if (dim_idx == 0)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010096 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (dim_val == kDynamicTensorDimensionValue)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100108 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 if (idx != kDynamicTensorDimensionValue)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100122 {
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100123 _components_used.emplace_back(std::make_unique<CLTensorComponent>(*this, x, idx));
124
125 return *_components_used.back();
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100126 }
127 }
128 }
129
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100130 _components_used.emplace_back(std::make_unique<CLTensorComponent>(*this, x));
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100131
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100132 return *_components_used.back();
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100133}
134
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100135ITile &CLTensorArgument::component(TensorComponentType x)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100136{
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100137 return cl_component(x);
138}
139
140TensorStorageVariable &CLTensorArgument::storage(TensorStorageType x)
141{
142 // Return the storage if it has already been created.
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100143 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 const auto it = std::find_if(_storages_used.begin(), _storages_used.end(),
145 [=](const TensorStorageVariable &item) { return item.type == x; });
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100146
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 if (it != _storages_used.end())
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100148 {
149 return *it;
150 }
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100151 }
152
153 TensorStorageVariable t;
154 t.val = create_storage_name(x);
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100155 t.type = x;
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100156
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100157 _storages_used.emplace_back(t);
158
159 return _storages_used.back();
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100160}
161
162std::string CLTensorArgument::create_storage_name(TensorStorageType x) const
163{
164 std::string var_name = _basename;
165
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 switch (x)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100167 {
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 Iodiceebfdb5a2023-07-07 11:25:57 +0100183std::vector<TensorStorageVariable> CLTensorArgument::storages() const
184{
185 std::vector<TensorStorageVariable> storages;
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100186 storages.reserve(_storages_used.size());
187
188 std::copy(_storages_used.begin(), _storages_used.end(), std::back_inserter(storages));
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100189
190 return storages;
191}
192
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100193std::vector<const ITensorComponent *> CLTensorArgument::components() const
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100194{
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100195 std::vector<const ITensorComponent *> components;
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100196
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 for (const auto &component : _components_used)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100198 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 if (component->is_assignable())
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100200 {
201 components.push_back(component.get());
202 }
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100203 }
204
205 return components;
206}
207} // namespace ckw