Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #include "helpers.h" |
| 25 | |
| 26 | /** Apply cross map normalization. |
| 27 | * |
| 28 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 29 | * |
| 30 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16, F32 |
| 31 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 32 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 33 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 34 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 35 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 36 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 37 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 38 | * @param[in] squared_input_ptr Pointer to the second source tensor. Supported data types: F16, F32 |
| 39 | * @param[in] squared_input_stride_x Stride of the second source tensor in X dimension (in bytes) |
| 40 | * @param[in] squared_input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 41 | * @param[in] squared_input_stride_y Stride of the second source tensor in Y dimension (in bytes) |
| 42 | * @param[in] squared_input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 43 | * @param[in] squared_input_stride_z Stride of the second source tensor in Z dimension (in bytes) |
| 44 | * @param[in] squared_input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 45 | * @param[in] squared_input_offset_first_element_in_bytes The offset of the second element in the second source tensor |
| 46 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16, F32 |
| 47 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 48 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 49 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 50 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 51 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 52 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 53 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 54 | * @param[in] coeff Alpha parameter / norm_size |
| 55 | * @param[in] beta Beta parameter in the normalization equation |
| 56 | * @param[in] kappa Kappa parameter in the normalization equation |
| 57 | * @param[in] radius Number of elements on the right or left side to normalize across |
| 58 | */ |
| 59 | __kernel void normalization_layer_cross_map(TENSOR3D_DECLARATION(input), |
| 60 | TENSOR3D_DECLARATION(squared_input), |
| 61 | TENSOR3D_DECLARATION(output), |
| 62 | float coeff, |
| 63 | float beta, |
| 64 | float kappa, |
| 65 | uint radius) |
| 66 | { |
| 67 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 68 | Tensor3D squared_in = CONVERT_TO_TENSOR3D_STRUCT(squared_input); |
| 69 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 70 | |
| 71 | DATA_TYPE acc = 0; |
| 72 | |
| 73 | const int num_of_slices = get_global_size(2); |
| 74 | const int current_slice = get_global_id(2); |
| 75 | |
| 76 | const int left_slice = max(current_slice - (int)radius, (int)0); |
| 77 | const int right_slice = min(current_slice + (int)radius, (int)(num_of_slices - 1)); |
| 78 | |
| 79 | for(int i = left_slice; i <= right_slice; i++) |
| 80 | { |
| 81 | acc += *(__global DATA_TYPE *)tensor3D_offset(&squared_in, 0, 0, i - current_slice); |
| 82 | } |
| 83 | |
| 84 | const float normalized = pow(kappa + coeff * (float)acc, beta); |
| 85 | |
| 86 | const float normalized_pixel = (float) * ((__global DATA_TYPE *)in.ptr) / normalized; |
| 87 | |
| 88 | *(__global DATA_TYPE *)out.ptr = CONVERT(normalized_pixel, DATA_TYPE); |
| 89 | } |
| 90 | |
| 91 | /** Apply in map normalization. |
| 92 | * |
| 93 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 94 | * |
| 95 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16, F32 |
| 96 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 97 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 98 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 99 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 100 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 101 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 102 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 103 | * @param[in] squared_input_ptr Pointer to the second source tensor. Supported data types: F16, F32 |
| 104 | * @param[in] squared_input_stride_x Stride of the second source tensor in X dimension (in bytes) |
| 105 | * @param[in] squared_input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 106 | * @param[in] squared_input_stride_y Stride of the second source tensor in Y dimension (in bytes) |
| 107 | * @param[in] squared_input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 108 | * @param[in] squared_input_stride_z Stride of the second source tensor in Z dimension (in bytes) |
| 109 | * @param[in] squared_input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 110 | * @param[in] squared_input_offset_first_element_in_bytes The offset of the second element in the second source tensor |
| 111 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16, F32 |
| 112 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 113 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 114 | * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes) |
| 115 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 116 | * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 117 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 118 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 119 | * @param[in] coeff Alpha parameter / norm_size |
| 120 | * @param[in] beta Beta parameter in the normalization equation |
| 121 | * @param[in] kappa Kappa parameter in the normalization equation |
| 122 | * @param[in] radius Number of elements on the right or left side to normalize across |
| 123 | */ |
| 124 | __kernel void normalization_layer_in_map_1D(TENSOR3D_DECLARATION(input), |
| 125 | TENSOR3D_DECLARATION(squared_input), |
| 126 | TENSOR3D_DECLARATION(output), |
| 127 | float coeff, |
| 128 | float beta, |
| 129 | float kappa, |
| 130 | uint radius) |
| 131 | { |
| 132 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 133 | Tensor3D squared_in = CONVERT_TO_TENSOR3D_STRUCT(squared_input); |
| 134 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 135 | |
| 136 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 137 | acc_vec = 0; |
| 138 | |
| 139 | const int current_pos = get_global_id(0) << 2; |
| 140 | |
| 141 | const int left_pos = max(current_pos - (int)radius, -3); |
| 142 | const int right_pos = min(current_pos + (int)radius, (int)((get_global_size(0) << 2) + 3 - 1)); |
| 143 | |
| 144 | for(int i = left_pos; i <= right_pos; i += 1) |
| 145 | { |
| 146 | acc_vec += vload4(0, (__global DATA_TYPE *)tensor3D_offset(&squared_in, i - current_pos, 0, 0)); |
| 147 | } |
| 148 | |
| 149 | const float4 normalized = pow((float4)kappa + coeff * (float4)acc_vec, beta); |
| 150 | |
| 151 | const float4 normalized_pixel = CONVERT(vload4(0, (__global DATA_TYPE *)in.ptr), float4) / normalized; |
| 152 | |
| 153 | vstore4(CONVERT(normalized_pixel, VEC_DATA_TYPE(DATA_TYPE, 4)), 0, (__global DATA_TYPE *)out.ptr); |
| 154 | } |