Path: blob/master/modules/dnn/src/opencl/ocl4dnn_lrn.cl
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2017, Intel Corporation, all rights reserved.13// Copyright (c) 2016-2017 Fabian David Tschopp, all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#define CONCAT(A,B) A##_##B43#define TEMPLATE(name,type) CONCAT(name,type)44#define KERNEL_ARG_DTYPE float4546#if defined(cl_khr_fp16)47#pragma OPENCL EXTENSION cl_khr_fp16 : enable48#endif4950__kernel void TEMPLATE(lrn_full_no_scale,Dtype)(const int nthreads, __global const Dtype* in,51const int num, const int channels,52const int height, const int width, const int size,53const KERNEL_ARG_DTYPE alpha_over_size, const KERNEL_ARG_DTYPE k,54__global Dtype* const out,55const KERNEL_ARG_DTYPE negative_beta) {56for (int index = get_global_id(0); index < nthreads;57index += get_global_size(0)) {58// find out the local offset59const int w = index % width;60const int h = (index / width) % height;61const int n = index / width / height;62const int offset = (n * channels * height + h) * width + w;63const int step = height * width;64__global const Dtype* in_off = in + offset;65__global Dtype* out_off = out + offset;66KERNEL_ARG_DTYPE scale_val;67int head = 0;68const int pre_pad = (size - 1) / 2;69const int post_pad = size - pre_pad - 1;70KERNEL_ARG_DTYPE accum_scale = 0;71// fill the scale at [n, :, h, w]72// accumulate values73while (head < post_pad && head < channels) {74accum_scale += in_off[head * step] * in_off[head * step];75++head;76}77// both add and subtract78while (head < channels) {79accum_scale += in_off[head * step] * in_off[head * step];80if (head - size >= 0) {81accum_scale -= in_off[(head - size) * step]82* in_off[(head - size) * step];83}84scale_val = k + accum_scale * alpha_over_size;85out_off[(head - post_pad) * step] = in_off[(head - post_pad) * step] * (Dtype)native_powr((Dtype)scale_val, (Dtype)negative_beta);86++head;87}88// subtract only89while (head < channels + post_pad) {90if (head - size >= 0) {91accum_scale -= in_off[(head - size) * step]92* in_off[(head - size) * step];93}94scale_val = k + accum_scale * alpha_over_size;95out_off[(head - post_pad) * step] = in_off[(head - post_pad) * step] * (Dtype)native_powr((Dtype)scale_val, (Dtype)negative_beta);96++head;97}98}99}100101102