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