blob: c221b449fa28aea94a3c612186ec0ad1df2ab58b [file] [log] [blame]
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +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
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010025#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H
26#define CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010027
28#include "ckw/OperandBase.h"
29#include "ckw/TensorInfo.h"
30#include "ckw/TensorTileSampler.h"
31#include "ckw/TileOperand.h"
Nikolaj Jensen5ff48022023-06-27 14:13:24 +010032#include "ckw/types/DataType.h"
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010033
34#include <memory>
35
36namespace ckw
37{
38
39class TensorComponentOperand;
40
41// =================================================================================================
42// TensorOperand
43// =================================================================================================
44
45/** Tensor operand */
46class TensorOperand : public OperandBase
47{
48public:
49 /** Initialize a new instance of @ref TensorOperand class.
50 *
Viet-Hoa Doc8e16172023-06-27 14:09:46 +010051 * @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 Dobd4f6b92023-05-30 09:34:32 +010054 */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +010055 TensorOperand(const ::std::string &name, const TensorInfo &info, TensorStorageType storage_type);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010056
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 Doc8e16172023-06-27 14:09:46 +010075 /** Get the tensor storage type. */
76 TensorStorageType storage_type() const;
77
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010078 /** 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 Doc8e16172023-06-27 14:09:46 +0100103 TensorComponentOperand &stride1();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100104
105 /** Get the operand that contains the stride in z dimension of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100106 TensorComponentOperand &stride2();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100107
108 /** Get the operand that contains the stride in w dimension of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100109 TensorComponentOperand &stride3();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100110
111 /** Get the operand that contains the stride in w dimension of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100112 TensorComponentOperand &stride4();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100113
114 /** Get the operand that contains the size of dimension 0 of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100115 TensorComponentOperand &dim0();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100116
117 /** Get the operand that contains the size of dimension 1 of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100118 TensorComponentOperand &dim1();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100119
120 /** Get the operand that contains the size of dimension 2 of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100121 TensorComponentOperand &dim2();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100122
123 /** Get the operand that contains the size of dimension 3 of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100124 TensorComponentOperand &dim3();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100125
126 /** Get the operand that contains the size of dimension 4 of the tensor. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100127 TensorComponentOperand &dim4();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100128
129 /** Get the operand that contains the size of dimensions 1 and 2 collapsed. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100130 TensorComponentOperand &dim1_dim2();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100131
132 /** Get the operand that contains the size of dimensions 1, 2 and 3 collapsed. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100133 TensorComponentOperand &dim1_dim2_dim3();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100134
135 /** Get the operand that contains the offset in bytes to the first element. */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100136 TensorComponentOperand &offset_first_element_in_bytes();
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100137
138private:
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100139 TensorInfo _info;
140 TensorStorageType _storage_type;
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100141
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 TileOperand *_tile{nullptr};
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100143 TensorTileSampler _tile_sampler{};
144
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 ::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};
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100157};
158
159// =================================================================================================
160// TensorComponentOperand
161// =================================================================================================
162
163/** Tile operand that contains tensor information. */
164class TensorComponentOperand : public TileOperand
165{
166public:
167 /** Initialize a new instance of @ref TensorComponentOperand class.
168 *
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100169 * @param[in] tensor The tensor operand.
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100170 * @param[in] component The tensor info component.
171 */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100172 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 Dobd4f6b92023-05-30 09:34:32 +0100182
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
189private:
Viet-Hoa Doc8e16172023-06-27 14:09:46 +0100190 TensorOperand &_tensor;
191 TensorComponentType _component;
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100192};
193
194} // namespace ckw
195
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100196#endif // CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H