Path: blob/master/modules/dnn/src/opencl/softmax_loss.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)4445#if defined(cl_intel_subgroups)46#pragma OPENCL EXTENSION cl_intel_subgroups : enable47#endif4849#if defined(cl_khr_fp16)50#pragma OPENCL EXTENSION cl_khr_fp16 : enable51#endif5253__kernel void TEMPLATE(softmax_forward_slm,Dtype)(const int num, const int channels,54const int spatial_dim,55__global Dtype* scale,56__global const Dtype* data,57__global Dtype* out,58__local Dtype *out_tmp,59__local Dtype *scale_tmp,60__local Dtype *group_tmp) {6162int n = get_global_id(1);63for (int index = get_global_id(0), s = 0; index < spatial_dim * get_local_size(0); index +=64get_global_size(0), ++s) {65Dtype maxval = -DTYPE_MAX;66for (int c = get_global_id(0); c < channels; c += get_global_size(0)) {67Dtype tmp = data[(n * channels + c) * spatial_dim + s];68maxval = max((Dtype)tmp, (Dtype)maxval);69}70maxval = sub_group_reduce_max(maxval);71//if (get_sub_group_local_id() == 0)72group_tmp[get_sub_group_id() * spatial_dim + s] = maxval;73}7475barrier(CLK_LOCAL_MEM_FENCE);7677for (int index = get_global_id(0); index < spatial_dim * get_max_sub_group_size(); index +=78get_global_size(0)) {79int s = index / get_max_sub_group_size();80Dtype maxval = sub_group_reduce_max(group_tmp[get_sub_group_local_id() * spatial_dim + s]);81//if (get_sub_group_local_id() == 0)82scale_tmp[s] = maxval;83}8485barrier(CLK_LOCAL_MEM_FENCE);8687for (int index = get_global_id(0); index < channels * spatial_dim;88index += get_global_size(0)) {89int s = index % spatial_dim;90out_tmp[index] = exp(data[n * channels * spatial_dim + index] - scale_tmp[s]);91}92barrier(CLK_LOCAL_MEM_FENCE);9394for (int index = get_global_id(0), s = 0; index < spatial_dim * get_local_size(0); index +=95get_global_size(0), ++s) {96Dtype sum = 0;97for (int c = get_global_id(0); c < channels; c += get_global_size(0)) {98sum += out_tmp[c * spatial_dim + s];99}100sum = sub_group_reduce_add(sum);101group_tmp[get_sub_group_id() * spatial_dim + s] = sum;102}103barrier(CLK_LOCAL_MEM_FENCE);104105for (int index = get_global_id(0); index < spatial_dim * get_max_sub_group_size(); index +=106get_global_size(0)) {107int s = index / get_max_sub_group_size();108Dtype sum = sub_group_reduce_add(group_tmp[get_sub_group_local_id() * spatial_dim + s]);109//if (get_sub_group_local_id() == 0)110scale_tmp[s] = sum;111}112barrier(CLK_LOCAL_MEM_FENCE);113114for (int index = get_global_id(0); index < channels * spatial_dim;115index += get_global_size(0)) {116int s = index % spatial_dim;117Dtype v = out_tmp[index] / scale_tmp[s];118#ifdef LOG_SOFTMAX119v = log(v);120#endif121out[n * channels * spatial_dim + index] = v;122}123}124125__kernel void TEMPLATE(softmax_forward,Dtype)(const int num, const int channels,126const int spatial_dim,127__global Dtype* scale,128__global const Dtype* data,129__global Dtype* out) {130131int n = get_global_id(1);132__global Dtype *group_tmp = scale + spatial_dim * num + n * get_max_sub_group_size() * spatial_dim;133for (int index = get_global_id(0), s = 0; index < spatial_dim * get_local_size(0); index +=134get_global_size(0), ++s) {135Dtype maxval = -DTYPE_MAX;136for (int c = get_global_id(0); c < channels; c += get_global_size(0)) {137Dtype tmp = data[(n * channels + c) * spatial_dim + s];138maxval = max((Dtype)tmp, (Dtype)maxval);139}140maxval = sub_group_reduce_max(maxval);141//if (get_sub_group_local_id() == 0)142group_tmp[get_sub_group_id() * spatial_dim + s] = maxval;143}144barrier(CLK_GLOBAL_MEM_FENCE);145146for (int index = get_global_id(0); index < spatial_dim * get_max_sub_group_size(); index +=147get_global_size(0)) {148int s = index / get_max_sub_group_size();149Dtype maxval = sub_group_reduce_max(group_tmp[get_sub_group_local_id() * spatial_dim + s]);150//if (get_sub_group_local_id() == 0)151scale[n * spatial_dim + s] = maxval;152}153154barrier(CLK_GLOBAL_MEM_FENCE);155156for (int index = get_global_id(0); index < channels * spatial_dim;157index += get_global_size(0)) {158int s = index % spatial_dim;159out[n * channels * spatial_dim + index] = exp(data[n * channels * spatial_dim + index] - scale[n * spatial_dim + s]);160}161barrier(CLK_GLOBAL_MEM_FENCE);162163for (int index = get_global_id(0), s = 0; index < spatial_dim * get_local_size(0); index +=164get_global_size(0), ++s) {165Dtype sum = 0;166for (int c = get_global_id(0); c < channels; c += get_global_size(0)) {167sum += out[n * channels * spatial_dim + c * spatial_dim + s];168}169sum = sub_group_reduce_add(sum);170group_tmp[get_sub_group_id() * spatial_dim + s] = sum;171}172barrier(CLK_GLOBAL_MEM_FENCE);173174for (int index = get_global_id(0); index < spatial_dim * get_max_sub_group_size(); index +=175get_global_size(0)) {176int s = index / get_max_sub_group_size();177Dtype sum = sub_group_reduce_add(group_tmp[get_sub_group_local_id() * spatial_dim + s]);178//if (get_sub_group_local_id() == 0)179scale[n * spatial_dim + s] = sum;180}181barrier(CLK_GLOBAL_MEM_FENCE);182183for (int index = get_global_id(0); index < channels * spatial_dim;184index += get_global_size(0)) {185int s = index % spatial_dim;186Dtype v = out[n * channels * spatial_dim + index] / scale[n * spatial_dim + s];187#ifdef LOG_SOFTMAX188v = log(v);189#endif190out[n * channels * spatial_dim + index] = v;191}192}193194195