Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +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 | |
| 25 | #include "Tensor3dMapper.h" |
| 26 | |
| 27 | #include "ckw/Error.h" |
| 28 | #include "ckw/types/TensorSamplerTypes.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 30 | #include "src/ITensor.h" |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 31 | #include "src/ITile.h" |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 32 | |
| 33 | namespace ckw |
| 34 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 35 | Tensor3dMapper::Tensor3dMapper(ITensor *tensor, TensorSamplerFormat format) : _tensor(tensor), _format(format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 39 | TileVariable Tensor3dMapper::dim_x() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 40 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 42 | { |
| 43 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
| 44 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 45 | return _tensor->component(TensorComponentType::Dim0).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 46 | default: |
| 47 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 48 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 52 | TileVariable Tensor3dMapper::dim_y() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 53 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 55 | { |
| 56 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 57 | return _tensor->component(TensorComponentType::Dim1xDim2).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 58 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 59 | return _tensor->component(TensorComponentType::Dim1).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 60 | default: |
| 61 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 62 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 66 | TileVariable Tensor3dMapper::dim_z() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 67 | { |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 68 | TileVariable dim_one; |
| 69 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 71 | { |
| 72 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | dim_one = _tensor->component(TensorComponentType::Dim3).scalar(0, 0); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 74 | dim_one.str = "1"; |
| 75 | return dim_one; |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 76 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 77 | return _tensor->component(TensorComponentType::Dim2).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 78 | default: |
| 79 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 80 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 84 | TileVariable Tensor3dMapper::dim_batch() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 85 | { |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 86 | TileVariable dim_one; |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 89 | { |
| 90 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
| 91 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 92 | return _tensor->component(TensorComponentType::Dim3).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 93 | default: |
| 94 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 95 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 99 | TileVariable Tensor3dMapper::stride_x() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 100 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 102 | { |
| 103 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
| 104 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 105 | return _tensor->component(TensorComponentType::Stride0).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 106 | default: |
| 107 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 108 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 112 | TileVariable Tensor3dMapper::stride_y() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 113 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | switch (_format) |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 115 | { |
| 116 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 117 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 118 | return _tensor->component(TensorComponentType::Stride1).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 119 | default: |
| 120 | CKW_THROW_MSG("Unsupported tensor format"); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 121 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 125 | TileVariable Tensor3dMapper::stride_z() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 126 | { |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 127 | TileVariable stride_zero; |
| 128 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | switch (_format) |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 130 | { |
| 131 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | stride_zero = _tensor->component(TensorComponentType::Stride3).scalar(0, 0); |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 133 | stride_zero.str = "0"; |
| 134 | return stride_zero; |
| 135 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
| 136 | return _tensor->component(TensorComponentType::Stride2).scalar(0, 0); |
| 137 | default: |
| 138 | CKW_THROW_MSG("Unsupported tensor format"); |
| 139 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
| 140 | } |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 143 | TileVariable Tensor3dMapper::stride_batch() const |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 144 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | switch (_format) |
Gian Marco Iodice | e32acd6 | 2023-08-14 16:25:48 +0100 | [diff] [blame] | 146 | { |
| 147 | case TensorSamplerFormat::Dim0_Dim1xDim2_1: |
| 148 | case TensorSamplerFormat::Dim0_Dim1_Dim2: |
| 149 | return _tensor->component(TensorComponentType::Stride3).scalar(0, 0); |
| 150 | default: |
| 151 | CKW_THROW_MSG("Unsupported tensor format"); |
| 152 | return _tensor->component(TensorComponentType::Unknown).scalar(0, 0); |
| 153 | } |
Gunes Bayir | 5e84251 | 2023-07-25 22:32:05 +0100 | [diff] [blame] | 154 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 155 | } // namespace ckw |