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 batch normalization. |
| 27 | * |
| 28 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F32 |
| 29 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 30 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 31 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 32 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 33 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 34 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 35 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 36 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 37 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 38 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 39 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 40 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 41 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 42 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 43 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 44 | * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: F32 |
| 45 | * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) |
| 46 | * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) |
| 47 | * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor |
| 48 | * @param[in] var_ptr Pointer to the var tensor. Supported data types: F32 |
| 49 | * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) |
| 50 | * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) |
| 51 | * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor |
| 52 | * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: F32 |
| 53 | * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes) |
| 54 | * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes) |
| 55 | * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor |
| 56 | * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: F32 |
| 57 | * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes) |
| 58 | * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes) |
| 59 | * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor |
| 60 | * @param[in] epsilon Epsilon parameter in the batch normalization equation |
| 61 | */ |
| 62 | __kernel void batchnormalization_layer(TENSOR3D_DECLARATION(input), |
| 63 | TENSOR3D_DECLARATION(output), |
| 64 | VECTOR_DECLARATION(mean), |
| 65 | VECTOR_DECLARATION(var), |
| 66 | VECTOR_DECLARATION(beta), |
| 67 | VECTOR_DECLARATION(gamma), |
| 68 | float epsilon) |
| 69 | { |
| 70 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 71 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 72 | Vector mean = CONVERT_TO_VECTOR_STRUCT(mean); |
| 73 | Vector var = CONVERT_TO_VECTOR_STRUCT(var); |
| 74 | Vector beta = CONVERT_TO_VECTOR_STRUCT(beta); |
| 75 | Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma); |
| 76 | |
| 77 | float4 _in = 0; |
| 78 | float4 denominator = 0; |
| 79 | float4 numerator = 0; |
| 80 | float4 x_bar = 0; |
| 81 | float4 gamma_vec = 0; |
| 82 | float4 beta_vec = 0; |
| 83 | |
| 84 | const int current_slice = get_global_id(2); |
| 85 | |
| 86 | _in = vload4(0, (__global float *)in.ptr); |
| 87 | denominator = *((__global float *)(var.ptr + current_slice * var.stride_x)); |
| 88 | denominator = rsqrt(denominator + epsilon); |
| 89 | |
| 90 | // Calculate x bar and store results |
| 91 | numerator = *((__global float *)(mean.ptr + current_slice * mean.stride_x)); |
| 92 | numerator = _in - numerator; |
| 93 | x_bar = numerator * denominator; |
| 94 | |
| 95 | gamma_vec = *((__global float *)(gamma.ptr + current_slice * beta.stride_x)); |
| 96 | beta_vec = *((__global float *)(beta.ptr + current_slice * beta.stride_x)); |
| 97 | |
| 98 | vstore4(gamma_vec * x_bar + beta_vec, 0, (__global float *)out.ptr); |
| 99 | } |