Path: blob/master/modules/dnn/src/opencl/activations.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 ReLUForward(const int count, __global const T* in, __global T* out51#ifndef RELU_NO_SLOPE52, KERNEL_ARG_DTYPE negative_slope53#endif54) {55int index = get_global_id(0);56if(index < count)57#ifndef RELU_NO_SLOPE58out[index] = in[index] > 0 ? in[index] : in[index] * negative_slope;59#else60out[index] = in[index] > 0 ? in[index] : 0;61#endif62}6364__kernel void ReLU6Forward(const int count, __global const T* in, __global T* out,65const KERNEL_ARG_DTYPE minValue, const KERNEL_ARG_DTYPE maxValue)66{67int index = get_global_id(0);68if(index < count)69{70T x = in[index];71out[index] = clamp(x, convert_T(minValue), convert_T(maxValue));72}73}7475__kernel void PReLUForward(const int count, const int channels, const int plane_size,76__global const T* in, __global T* out,77__global const KERNEL_ARG_DTYPE* slope_data)78{79int index = get_global_id(0);80int c = (index / plane_size) % channels;81if(index < count)82out[index] = in[index] > 0 ? in[index] : in[index] * slope_data[c];83}8485__kernel void TanHForward(const int count, __global T* in, __global T* out) {86int index = get_global_id(0);87if(index < count)88out[index] = tanh(in[index]);89}9091__kernel void SigmoidForward(const int count, __global const T* in, __global T* out) {92int index = get_global_id(0);93if(index < count)94out[index] = 1.0f / (1.0f + exp(-in[index]));95}9697__kernel void BNLLForward(const int n, __global const T* in, __global T* out) {98int index = get_global_id(0);99if (index < n) {100out[index] = in[index] > 0 ? in[index] + log(1.0f + exp(-in[index])) : log(1.0f + exp(in[index]));101}102}103104__kernel void AbsValForward(const int n, __global const T* in, __global T* out) {105int index = get_global_id(0);106if (index < n)107out[index] = fabs(in[index]);108}109110__kernel void PowForward(const int n, __global const T* in, __global T* out,111const KERNEL_ARG_DTYPE power,112const KERNEL_ARG_DTYPE scale,113const KERNEL_ARG_DTYPE shift)114{115int index = get_global_id(0);116if (index < n)117out[index] = pow(shift + scale * in[index], power);118}119120__kernel void ELUForward(const int n, __global const T* in, __global T* out)121{122int index = get_global_id(0);123if (index < n)124{125T src = in[index];126out[index] = (src >= 0.f) ? src : exp(src) - 1;127}128}129130131