Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +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 | |
Viet-Hoa Do | ce3c48c | 2023-07-03 13:44:43 +0100 | [diff] [blame] | 25 | #ifndef CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H |
| 26 | #define CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 27 | |
| 28 | #include "ckw/OperandBase.h" |
| 29 | #include "ckw/TensorInfo.h" |
| 30 | #include "ckw/TensorTileSampler.h" |
| 31 | #include "ckw/TileOperand.h" |
Nikolaj Jensen | 5ff4802 | 2023-06-27 14:13:24 +0100 | [diff] [blame] | 32 | #include "ckw/types/DataType.h" |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 33 | |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace ckw |
| 37 | { |
| 38 | |
| 39 | class TensorComponentOperand; |
| 40 | |
| 41 | // ================================================================================================= |
| 42 | // TensorOperand |
| 43 | // ================================================================================================= |
| 44 | |
| 45 | /** Tensor operand */ |
| 46 | class TensorOperand : public OperandBase |
| 47 | { |
| 48 | public: |
| 49 | /** Initialize a new instance of @ref TensorOperand class. |
| 50 | * |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 51 | * @param[in] name The name of the tensor. |
| 52 | * @param[in] info The tensor info. |
| 53 | * @param[in] storage_type The tensor storage type. |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 54 | */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 55 | TensorOperand(const ::std::string &name, const TensorInfo &info, TensorStorageType storage_type); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 56 | |
| 57 | /** No copy constructor. */ |
| 58 | TensorOperand(const TensorOperand &other) = delete; |
| 59 | |
| 60 | /** No copy assignment. */ |
| 61 | TensorOperand &operator=(const TensorOperand &other) = delete; |
| 62 | |
| 63 | /** (Internal use only) Create the implementation operand. |
| 64 | * |
| 65 | * @param[in] writer The implementation kernel writer. |
| 66 | */ |
| 67 | virtual prototype::Operand create_impl_operand(prototype::IGpuKernelWriter *writer) const override; |
| 68 | |
| 69 | /** Get the tensor info. */ |
| 70 | const TensorInfo &info() const; |
| 71 | |
| 72 | /** Get the tensor info. */ |
| 73 | TensorInfo &info(); |
| 74 | |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 75 | /** Get the tensor storage type. */ |
| 76 | TensorStorageType storage_type() const; |
| 77 | |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 78 | /** Get the data type. */ |
| 79 | virtual DataType data_type() const override; |
| 80 | |
| 81 | /** Get whether the tensor is compile-time constant. */ |
| 82 | virtual bool is_constant() const override; |
| 83 | |
| 84 | /** Get the default tile attached to the tensor. */ |
| 85 | const TileOperand &tile() const; |
| 86 | |
| 87 | /** Get the default tile attached to the tensor. */ |
| 88 | TileOperand &tile(); |
| 89 | |
| 90 | /** Set the default tile attached to the tensor. */ |
| 91 | TensorOperand &tile(TileOperand &tile); |
| 92 | |
| 93 | /** Get the tensor sampler of the default tile. */ |
| 94 | const TensorTileSampler &tile_sampler() const; |
| 95 | |
| 96 | /** Get the tensor sampler of the default tile. */ |
| 97 | TensorTileSampler &tile_sampler(); |
| 98 | |
| 99 | /** Set the tensor sampler of the default tile. */ |
| 100 | TensorOperand &tile_sampler(const TensorTileSampler &value); |
| 101 | |
| 102 | /** Get the operand that contains the stride in y dimension of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 103 | TensorComponentOperand &stride1(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 104 | |
| 105 | /** Get the operand that contains the stride in z dimension of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 106 | TensorComponentOperand &stride2(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 107 | |
| 108 | /** Get the operand that contains the stride in w dimension of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 109 | TensorComponentOperand &stride3(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 110 | |
| 111 | /** Get the operand that contains the stride in w dimension of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 112 | TensorComponentOperand &stride4(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 113 | |
| 114 | /** Get the operand that contains the size of dimension 0 of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 115 | TensorComponentOperand &dim0(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 116 | |
| 117 | /** Get the operand that contains the size of dimension 1 of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 118 | TensorComponentOperand &dim1(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 119 | |
| 120 | /** Get the operand that contains the size of dimension 2 of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 121 | TensorComponentOperand &dim2(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 122 | |
| 123 | /** Get the operand that contains the size of dimension 3 of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 124 | TensorComponentOperand &dim3(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 125 | |
| 126 | /** Get the operand that contains the size of dimension 4 of the tensor. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 127 | TensorComponentOperand &dim4(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 128 | |
| 129 | /** Get the operand that contains the size of dimensions 1 and 2 collapsed. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 130 | TensorComponentOperand &dim1_dim2(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 131 | |
| 132 | /** Get the operand that contains the size of dimensions 1, 2 and 3 collapsed. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 133 | TensorComponentOperand &dim1_dim2_dim3(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 134 | |
| 135 | /** Get the operand that contains the offset in bytes to the first element. */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 136 | TensorComponentOperand &offset_first_element_in_bytes(); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 137 | |
| 138 | private: |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 139 | TensorInfo _info; |
| 140 | TensorStorageType _storage_type; |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 141 | |
Nikolaj Jensen | acea407 | 2023-07-03 09:44:42 +0100 | [diff] [blame] | 142 | TileOperand *_tile{ nullptr }; |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 143 | TensorTileSampler _tile_sampler{}; |
| 144 | |
| 145 | ::std::unique_ptr<TensorComponentOperand> _stride1{ nullptr }; |
| 146 | ::std::unique_ptr<TensorComponentOperand> _stride2{ nullptr }; |
| 147 | ::std::unique_ptr<TensorComponentOperand> _stride3{ nullptr }; |
| 148 | ::std::unique_ptr<TensorComponentOperand> _stride4{ nullptr }; |
| 149 | ::std::unique_ptr<TensorComponentOperand> _dim0{ nullptr }; |
| 150 | ::std::unique_ptr<TensorComponentOperand> _dim1{ nullptr }; |
| 151 | ::std::unique_ptr<TensorComponentOperand> _dim2{ nullptr }; |
| 152 | ::std::unique_ptr<TensorComponentOperand> _dim3{ nullptr }; |
| 153 | ::std::unique_ptr<TensorComponentOperand> _dim4{ nullptr }; |
| 154 | ::std::unique_ptr<TensorComponentOperand> _dim1_dim2{ nullptr }; |
| 155 | ::std::unique_ptr<TensorComponentOperand> _dim1_dim2_dim3{ nullptr }; |
| 156 | ::std::unique_ptr<TensorComponentOperand> _offset_first_element_in_bytes{ nullptr }; |
| 157 | }; |
| 158 | |
| 159 | // ================================================================================================= |
| 160 | // TensorComponentOperand |
| 161 | // ================================================================================================= |
| 162 | |
| 163 | /** Tile operand that contains tensor information. */ |
| 164 | class TensorComponentOperand : public TileOperand |
| 165 | { |
| 166 | public: |
| 167 | /** Initialize a new instance of @ref TensorComponentOperand class. |
| 168 | * |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 169 | * @param[in] tensor The tensor operand. |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 170 | * @param[in] component The tensor info component. |
| 171 | */ |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 172 | TensorComponentOperand(TensorOperand &tensor, TensorComponentType component); |
| 173 | |
| 174 | /** Get the tensor operand. */ |
| 175 | TensorOperand &tensor(); |
| 176 | |
| 177 | /** Get the tensor operand. */ |
| 178 | const TensorOperand &tensor() const; |
| 179 | |
| 180 | /** Get the tensor component. */ |
| 181 | TensorComponentType component_type() const; |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 182 | |
| 183 | /** (Internal use only) Create the implementation operand. |
| 184 | * |
| 185 | * @param[in] writer The implementation kernel writer. |
| 186 | */ |
| 187 | virtual prototype::Operand create_impl_operand(prototype::IGpuKernelWriter *writer) const override; |
| 188 | |
| 189 | private: |
Viet-Hoa Do | c8e1617 | 2023-06-27 14:09:46 +0100 | [diff] [blame] | 190 | TensorOperand &_tensor; |
| 191 | TensorComponentType _component; |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace ckw |
| 195 | |
Viet-Hoa Do | ce3c48c | 2023-07-03 13:44:43 +0100 | [diff] [blame] | 196 | #endif // CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H |