blob: acef6412a4595350c9b21a79fb909676fc433621 [file] [log] [blame]
Gunes Bayir5e842512023-07-25 22:32:05 +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#include "Tensor3dMapper.h"
26
27#include "ckw/Error.h"
28#include "ckw/types/TensorSamplerTypes.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Gunes Bayir5e842512023-07-25 22:32:05 +010030#include "src/ITensor.h"
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010031#include "src/ITile.h"
Gunes Bayir5e842512023-07-25 22:32:05 +010032
33namespace ckw
34{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035Tensor3dMapper::Tensor3dMapper(ITensor *tensor, TensorSamplerFormat format) : _tensor(tensor), _format(format)
Gunes Bayir5e842512023-07-25 22:32:05 +010036{
37}
38
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010039TileVariable Tensor3dMapper::dim_x() const
Gunes Bayir5e842512023-07-25 22:32:05 +010040{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +010042 {
43 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
44 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010045 return _tensor->component(TensorComponentType::Dim0).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010046 default:
47 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010048 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010049 }
50}
51
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010052TileVariable Tensor3dMapper::dim_y() const
Gunes Bayir5e842512023-07-25 22:32:05 +010053{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +010055 {
56 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010057 return _tensor->component(TensorComponentType::Dim1xDim2).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010058 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010059 return _tensor->component(TensorComponentType::Dim1).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010060 default:
61 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010062 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010063 }
64}
65
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010066TileVariable Tensor3dMapper::dim_z() const
Gunes Bayir5e842512023-07-25 22:32:05 +010067{
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010068 TileVariable dim_one;
69
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +010071 {
72 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 dim_one = _tensor->component(TensorComponentType::Dim3).scalar(0, 0);
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010074 dim_one.str = "1";
75 return dim_one;
Gunes Bayir5e842512023-07-25 22:32:05 +010076 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010077 return _tensor->component(TensorComponentType::Dim2).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010078 default:
79 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010080 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010081 }
82}
83
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010084TileVariable Tensor3dMapper::dim_batch() const
Gunes Bayir5e842512023-07-25 22:32:05 +010085{
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010086 TileVariable dim_one;
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +010089 {
90 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
91 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010092 return _tensor->component(TensorComponentType::Dim3).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010093 default:
94 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010095 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +010096 }
97}
98
Gian Marco Iodicee32acd62023-08-14 16:25:48 +010099TileVariable Tensor3dMapper::stride_x() const
Gunes Bayir5e842512023-07-25 22:32:05 +0100100{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +0100102 {
103 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
104 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100105 return _tensor->component(TensorComponentType::Stride0).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +0100106 default:
107 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100108 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +0100109 }
110}
111
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100112TileVariable Tensor3dMapper::stride_y() const
Gunes Bayir5e842512023-07-25 22:32:05 +0100113{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 switch (_format)
Gunes Bayir5e842512023-07-25 22:32:05 +0100115 {
116 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
Gunes Bayir5e842512023-07-25 22:32:05 +0100117 case TensorSamplerFormat::Dim0_Dim1_Dim2:
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100118 return _tensor->component(TensorComponentType::Stride1).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +0100119 default:
120 CKW_THROW_MSG("Unsupported tensor format");
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100121 return _tensor->component(TensorComponentType::Unknown).scalar(0, 0);
Gunes Bayir5e842512023-07-25 22:32:05 +0100122 }
123}
124
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100125TileVariable Tensor3dMapper::stride_z() const
Gunes Bayir5e842512023-07-25 22:32:05 +0100126{
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100127 TileVariable stride_zero;
128
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 switch (_format)
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100130 {
131 case TensorSamplerFormat::Dim0_Dim1xDim2_1:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 stride_zero = _tensor->component(TensorComponentType::Stride3).scalar(0, 0);
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100133 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 Bayir5e842512023-07-25 22:32:05 +0100141}
142
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100143TileVariable Tensor3dMapper::stride_batch() const
Gunes Bayir5e842512023-07-25 22:32:05 +0100144{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 switch (_format)
Gian Marco Iodicee32acd62023-08-14 16:25:48 +0100146 {
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 Bayir5e842512023-07-25 22:32:05 +0100154}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155} // namespace ckw