blob: 4970de75a6fa1e42be2dd6d9fef44e417e2f080f [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
25#include "ckw/Error.h"
26#include "ckw/TensorInfo.h"
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000027
28#include "src/TensorUtils.h"
29
30namespace ckw
31{
32TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component)
33{
34 switch(layout)
35 {
36 case TensorDataLayout::Nhwc:
37 switch(component)
38 {
39 case TensorDataLayoutComponent::C:
40 return TensorComponent::Dim0;
41 case TensorDataLayoutComponent::W:
42 return TensorComponent::Dim1;
43 case TensorDataLayoutComponent::H:
44 return TensorComponent::Dim2;
45 case TensorDataLayoutComponent::N:
46 return TensorComponent::Dim3;
47 default:
48 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC");
49 return TensorComponent::Unknown;
50 }
51 case TensorDataLayout::Ndhwc:
52 switch(component)
53 {
54 case TensorDataLayoutComponent::C:
55 return TensorComponent::Dim0;
56 case TensorDataLayoutComponent::W:
57 return TensorComponent::Dim1;
58 case TensorDataLayoutComponent::H:
59 return TensorComponent::Dim2;
60 case TensorDataLayoutComponent::D:
61 return TensorComponent::Dim3;
62 case TensorDataLayoutComponent::N:
63 return TensorComponent::Dim4;
64 default:
65 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC");
66 return TensorComponent::Unknown;
67 }
68 default:
69 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout");
70 return TensorComponent::Unknown;
71 }
72}
73
74TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component)
75{
76 switch(layout)
77 {
78 case TensorDataLayout::Nhwc:
79 switch(component)
80 {
81 case TensorDataLayoutComponent::C:
82 return TensorComponent::Stride0;
83 case TensorDataLayoutComponent::W:
84 return TensorComponent::Stride1;
85 case TensorDataLayoutComponent::H:
86 return TensorComponent::Stride2;
87 case TensorDataLayoutComponent::N:
88 return TensorComponent::Stride3;
89 default:
90 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC");
91 return TensorComponent::Unknown;
92 }
93 case TensorDataLayout::Ndhwc:
94 switch(component)
95 {
96 case TensorDataLayoutComponent::C:
97 return TensorComponent::Stride0;
98 case TensorDataLayoutComponent::W:
99 return TensorComponent::Stride1;
100 case TensorDataLayoutComponent::H:
101 return TensorComponent::Stride2;
102 case TensorDataLayoutComponent::D:
103 return TensorComponent::Stride3;
104 case TensorDataLayoutComponent::N:
105 return TensorComponent::Stride4;
106 default:
107 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC");
108 return TensorComponent::Unknown;
109 }
110 default:
111 COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout");
112 return TensorComponent::Unknown;
113 }
114}
115} // namespace ckw