blob: 55f8101a53c68d09703c848fdc4214ea3ea7b4fd [file] [log] [blame]
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +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#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H
26#define CKW_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H
27
Nikolaj Jensen5ff48022023-06-27 14:13:24 +010028#include "ckw/types/DataType.h"
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010029
30#include <array>
31#include <cstdint>
32
33namespace ckw
34{
35/** Compute Kernel Writer tensor data layout (or memory format) */
36enum class TensorDataLayout
37{
38 Unknown,
39 Nhwc,
40 Ndhwc
41};
42
43/** Compute Kernel Writer tensor data layout component */
44enum class TensorDataLayoutComponent
45{
46 Unknown,
47 N,
48 D,
49 H,
50 W,
51 C,
52};
53
54/** Compute Kernel Writer tensor component bitmask. The bitmask can be used to retrieve
55 * the info from @ref TensorComponent.
56 */
57enum class TensorComponentBitmask : uint32_t
58{
59 OffsetFirstElement = 0x01000000, // For example, OffsetFirstElement in @ref TensorComponent
60 Stride = 0x02000000, // For example, stride0 in @ref TensorComponent
61 Dimension = 0x04000000, // For example, Dim0 in @ref TensorComponent
62 FoldedDimensions = 0x08000000, // For example, Dim0xDim1 in @ref TensorComponent
63};
64
65/** Compute Kernel Writer tensor component. The tensor components are used to access specific backend-agnostic tensor arguments,
66 * such as the tensor dimensions and tensor strides.
67 * The data type is represented as an integer. The value of the integer value
68 * is assigned to retrieve the information through the @ref TensorComponentBitmask.
69 */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +010070enum class TensorComponentType : uint32_t
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010071{
72 Unknown = 0x00000000,
73 OffsetFirstElement = 0x01000000,
74 Stride0 = 0x02000001,
75 Stride1 = 0x02000010,
76 Stride2 = 0x02000100,
77 Stride3 = 0x02001000,
78 Stride4 = 0x02010000,
79 Dim0 = 0x04000001,
80 Dim1 = 0x04000010,
81 Dim2 = 0x04000100,
82 Dim3 = 0x04001000,
83 Dim4 = 0x04010000,
84 Dim1xDim2 = 0x08000110,
85 Dim2xDim3 = 0x08001100,
86 Dim1xDim2xDim3 = 0x08001110
87};
88
89/** Compute Kernel Writer tensor storage. The tensor storage represents the type of tensor memory object.
90 */
Viet-Hoa Doc8e16172023-06-27 14:09:46 +010091enum class TensorStorageType : uint32_t
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010092{
93 Unknown = 0x00000000,
94 BufferUint8Ptr = 0x01000000,
95 Texture2dReadOnly = 0x02000001,
96 Texture2dWriteOnly = 0x02000010,
97};
98
99/** Compute Kernel Writer tensor shape
100 * Negative dimensions can be interpreted as dynamic dimensions by the Compute Kernel Writer
101 */
102using TensorShape = std::array<int32_t, 5>;
103
104/** Compute Kernel Writer tensor info */
105class TensorInfo
106{
107public:
108 /** Constructor
109 *
110 * @param[in] dt Tensor data type
111 * @param[in] shape Tensor shape
112 * @param[in] dl Tensor data layout
113 * @param[in] id Tensor id. The id is used to keep track of the bound user tensor. Through the id,
114 * the user can know what tensor has been used by the Compute Kernel Writer.
115 * Possible id values:
116 * - greater than or equal to 0: bind a user specific tensors
117 * - less than 0: bind a virtual tensor (tile)
118 */
119 TensorInfo(DataType dt, const TensorShape &shape, TensorDataLayout dl, int32_t id);
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100120
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100121 /** Set shape */
122 TensorInfo &shape(const TensorShape &shape);
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100123
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100124 /** Get shape */
125 TensorShape shape() const;
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100126
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100127 /** Set data type */
128 TensorInfo &data_type(DataType dt);
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100129
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100130 /** Get data type */
131 DataType data_type() const;
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100132
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100133 /** Set data layout */
134 TensorInfo &data_layout(TensorDataLayout dl);
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100135
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100136 /** Get data layout */
137 TensorDataLayout data_layout() const;
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100138
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100139 /** Set id */
140 TensorInfo &id(int32_t id);
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100141
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100142 /** Get layout */
143 int32_t id() const;
144
145private:
146 TensorShape _shape{ { 0 } };
147 DataType _dt{ DataType::Unknown };
148 TensorDataLayout _dl{ TensorDataLayout::Unknown };
149 int32_t _id{ -1 };
150};
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100151} // namespace ckw
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100152
153#endif /* CKW_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H */