Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [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 | #ifndef COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TENSORINFO_H |
| 26 | #define COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TENSORINFO_H |
| 27 | |
| 28 | #include "ckw/Types.h" |
| 29 | |
| 30 | #include <array> |
| 31 | #include <cstdint> |
| 32 | |
| 33 | namespace ckw |
| 34 | { |
| 35 | /** Compute Kernel Writer tensor data layout (or memory format) */ |
| 36 | enum class TensorDataLayout |
| 37 | { |
| 38 | Unknown, |
| 39 | Nhwc, |
| 40 | Ndhwc |
| 41 | }; |
| 42 | |
| 43 | /** Compute Kernel Writer tensor data layout component */ |
| 44 | enum 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 | */ |
| 57 | enum 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 | */ |
| 70 | enum class TensorComponent : uint32_t |
| 71 | { |
| 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 shape |
| 90 | * Negative dimensions can be interpreted as dynamic dimensions by the Compute Kernel Writer |
| 91 | */ |
| 92 | using TensorShape = std::array<int32_t, 5>; |
| 93 | |
| 94 | /** Compute Kernel Writer tensor info */ |
| 95 | class TensorInfo |
| 96 | { |
| 97 | public: |
| 98 | /** Constructor |
| 99 | * |
| 100 | * @param[in] dt Tensor data type |
| 101 | * @param[in] shape Tensor shape |
| 102 | * @param[in] dl Tensor data layout |
| 103 | * @param[in] id Tensor id. The id is used to keep track of the user tensor binded. Through the id, |
| 104 | * the user can know what tensor has been used by the Compute Kernel Writer. |
| 105 | * Possible id values: |
| 106 | * - greater than or equal to 0: bind a user specific tensors |
| 107 | * - less than 0: bind a virtual tensor (tile) |
| 108 | */ |
| 109 | TensorInfo(DataType dt, const TensorShape &shape, TensorDataLayout dl, int32_t id); |
| 110 | /** Set shape */ |
| 111 | TensorInfo &shape(const TensorShape &shape); |
| 112 | /** Get shape */ |
| 113 | TensorShape shape() const; |
| 114 | /** Set data type */ |
| 115 | TensorInfo &data_type(DataType dt); |
| 116 | /** Get data type */ |
| 117 | DataType data_type() const; |
| 118 | /** Set data layout */ |
| 119 | TensorInfo &data_layout(TensorDataLayout dl); |
| 120 | /** Get data layout */ |
| 121 | TensorDataLayout data_layout() const; |
| 122 | /** Set id */ |
| 123 | TensorInfo &id(int32_t id); |
| 124 | /** Get layout */ |
| 125 | int32_t id() const; |
| 126 | |
| 127 | private: |
| 128 | TensorShape _shape{ { 0 } }; |
| 129 | DataType _dt{ DataType::Unknown }; |
| 130 | TensorDataLayout _dl{ TensorDataLayout::Unknown }; |
| 131 | int32_t _id{ -1 }; |
| 132 | }; |
| 133 | } // namespace kw |
| 134 | |
| 135 | #endif /* COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TENSORINFO_H */ |