blob: 17fc9547ae728c99fc176fc79403e2b98e43a49f [file] [log] [blame]
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +00001/*
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
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010025#include "src/TensorUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000027#include "ckw/Error.h"
28#include "ckw/TensorInfo.h"
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010029#include "ckw/types/TensorComponentType.h"
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000030
31namespace ckw
32{
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010033TensorComponentType get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000034{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035 switch (layout)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000036 {
37 case TensorDataLayout::Nhwc:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038 switch (component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000039 {
40 case TensorDataLayoutComponent::C:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010041 return TensorComponentType::Dim0;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000042 case TensorDataLayoutComponent::W:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010043 return TensorComponentType::Dim1;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000044 case TensorDataLayoutComponent::H:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010045 return TensorComponentType::Dim2;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000046 case TensorDataLayoutComponent::N:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010047 return TensorComponentType::Dim3;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000048 default:
49 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010050 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000051 }
52 case TensorDataLayout::Ndhwc:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 switch (component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000054 {
55 case TensorDataLayoutComponent::C:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010056 return TensorComponentType::Dim0;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000057 case TensorDataLayoutComponent::W:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010058 return TensorComponentType::Dim1;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000059 case TensorDataLayoutComponent::H:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010060 return TensorComponentType::Dim2;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000061 case TensorDataLayoutComponent::D:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010062 return TensorComponentType::Dim3;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000063 case TensorDataLayoutComponent::N:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010064 return TensorComponentType::Dim4;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000065 default:
66 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010067 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000068 }
69 default:
70 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010071 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000072 }
73}
74
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010075TensorComponentType get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000076{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 switch (layout)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000078 {
79 case TensorDataLayout::Nhwc:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 switch (component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000081 {
82 case TensorDataLayoutComponent::C:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010083 return TensorComponentType::Stride0;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000084 case TensorDataLayoutComponent::W:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010085 return TensorComponentType::Stride1;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000086 case TensorDataLayoutComponent::H:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010087 return TensorComponentType::Stride2;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000088 case TensorDataLayoutComponent::N:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010089 return TensorComponentType::Stride3;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000090 default:
91 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010092 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000093 }
94 case TensorDataLayout::Ndhwc:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 switch (component)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000096 {
97 case TensorDataLayoutComponent::C:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010098 return TensorComponentType::Stride0;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000099 case TensorDataLayoutComponent::W:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100100 return TensorComponentType::Stride1;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000101 case TensorDataLayoutComponent::H:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100102 return TensorComponentType::Stride2;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000103 case TensorDataLayoutComponent::D:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100104 return TensorComponentType::Stride3;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000105 case TensorDataLayoutComponent::N:
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100106 return TensorComponentType::Stride4;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000107 default:
108 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100109 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000110 }
111 default:
112 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100113 return TensorComponentType::Unknown;
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000114 }
115}
116} // namespace ckw