|
| 1 | +/* Copyright (c) 2016 paddlepaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/math/maxouting.h" |
| 16 | +#include "paddle/platform/cuda_helper.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | +namespace math { |
| 21 | + |
| 22 | +template <typename T> |
| 23 | +__global__ void KernelMaxOut(const int nthreads, const T* input_data, |
| 24 | + const int channels, |
| 25 | + const int input_height, const int input_width, |
| 26 | + int groups, T* output_data ) { |
| 27 | + const int size = input_height * input_width * channels / groups; |
| 28 | + const int feat_len = input_height * input_width; |
| 29 | + int index = blockIdx.x * blockDim.x + threadIdx.x; |
| 30 | + int offset = blockDim.x * gridDim.x; |
| 31 | + for (int i = index; i < nthreads; i += offset) { |
| 32 | + int batch_idx = i / size; |
| 33 | + int batch_offset = i % size; |
| 34 | + int channel_idx = batch_offset / feat_len; |
| 35 | + int feat_idx = batch_offset % feat_len; |
| 36 | + int data_idx = |
| 37 | + (batch_idx * size + channel_idx * feat_len) * groups + feat_idx; |
| 38 | + T ele = static_cast<T>(-FLT_MAX); |
| 39 | + for (int g = 0; g < groups; ++g) { |
| 40 | + T x = input_data[data_idx + g * feat_len]; |
| 41 | + ele = ele > x ? ele : x; |
| 42 | + } |
| 43 | + output_data[i] = ele; |
| 44 | + } |
| 45 | +} |
| 46 | +template <typename T> |
| 47 | +__global__ void KernelMaxoutGrad( |
| 48 | + const int nthreads, const T* input_data, const T* output_data, |
| 49 | + const T* output_grad, T* input_grad, const int channels, |
| 50 | + const int input_height, const int input_width, int groups) { |
| 51 | + const int size = input_height * input_width * channels / groups; |
| 52 | + const int feat_len = input_height * input_width; |
| 53 | + int index = blockIdx.x * blockDim.x + threadIdx.x; |
| 54 | + int offset = blockDim.x * gridDim.x; |
| 55 | + for (int i = index; i < nthreads; i += offset) { |
| 56 | + int batch_idx = i / size; |
| 57 | + int batch_offset = i % size; |
| 58 | + int channel_idx = batch_offset / feat_len; |
| 59 | + int feat_idx = batch_offset % feat_len; |
| 60 | + int data_idx = |
| 61 | + (batch_idx * size + channel_idx * feat_len) * groups + feat_idx; |
| 62 | + int max_index = -1; |
| 63 | + bool continue_match = true; |
| 64 | + for (int g = 0; g < groups && continue_match; ++g) { |
| 65 | + if (input_data[data_idx + g * feat_len] == output_data[i]) { |
| 66 | + max_index = data_idx + g * feat_len; |
| 67 | + continue_match = false; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + if (max_index != -1) { |
| 72 | + input_grad[max_index] += output_grad[index]; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +/* |
| 77 | + * All tensors are in NCHW format. |
| 78 | + */ |
| 79 | +template <typename T> |
| 80 | +class MaxOutFunctor<platform::GPUPlace, T> { |
| 81 | + public: |
| 82 | + void operator()(const platform::DeviceContext& context, |
| 83 | + const framework::Tensor& input, framework::Tensor * output, |
| 84 | + int groups) { |
| 85 | + const int batch_size = input.dims()[0]; |
| 86 | + const int input_channels = input.dims()[1]; |
| 87 | + const int input_height = input.dims()[2]; |
| 88 | + const int input_width = input.dims()[3]; |
| 89 | + const int output_channels = output->dims()[1]; |
| 90 | + const int output_height = output->dims()[2]; |
| 91 | + const int output_width = output->dims()[3]; |
| 92 | + |
| 93 | + const T* input_data = input.data<T>(); |
| 94 | + T* output_data = output->mutable_data<T>(context.GetPlace()); |
| 95 | + int nthreads = output->numel(); |
| 96 | + int blocks = (nthreads + 1024 - 1) / 1024; |
| 97 | + dim3 threads(1024, 1); |
| 98 | + dim3 grid(blocks, 1); |
| 99 | + |
| 100 | + KernelMaxOut< |
| 101 | + T><<<grid, threads, 0, |
| 102 | + reinterpret_cast<const platform::CUDADeviceContext&>(context) |
| 103 | + .stream()>>>(nthreads, input_data, input_channels, |
| 104 | + input_height, input_width, groups, |
| 105 | + output_data); |
| 106 | + } |
| 107 | +}; |
| 108 | +/* |
| 109 | + * All tensors are in NCHW format. |
| 110 | + */ |
| 111 | +template <typename T> |
| 112 | +class MaxOutGradFunctor<platform::GPUPlace, T> { |
| 113 | + public: |
| 114 | + void operator()(const platform::DeviceContext& context, |
| 115 | + const framework::Tensor& input, |
| 116 | + framework::Tensor * input_grad, |
| 117 | + const framework::Tensor& output, |
| 118 | + const framework::Tensor& output_grad, |
| 119 | + int groups) { |
| 120 | + const int batch_size = input.dims()[0]; |
| 121 | + const int input_channels = input.dims()[1]; |
| 122 | + const int input_height = input.dims()[2]; |
| 123 | + const int input_width = input.dims()[3]; |
| 124 | + const int output_channels = output.dims()[1]; |
| 125 | + const int output_height = output.dims()[2]; |
| 126 | + const int output_width = output.dims()[3]; |
| 127 | + |
| 128 | + const T* input_data = input.data<T>(); |
| 129 | + const T* output_data = output.data<T>(); |
| 130 | + const T* output_grad_data = output_grad.data<T>(); |
| 131 | + T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace()); |
| 132 | + int nthreads = output.numel(); |
| 133 | + int blocks = (nthreads + 1024 - 1) / 1024; |
| 134 | + dim3 threads(1024, 1); |
| 135 | + dim3 grid(blocks, 1); |
| 136 | + |
| 137 | + KernelMaxoutGrad< |
| 138 | + T><<<grid, threads, 0, |
| 139 | + reinterpret_cast<const platform::CUDADeviceContext&>(context) |
| 140 | + .stream()>>>( |
| 141 | + nthreads, input_data, output_data, output_grad_data, input_grad_data, |
| 142 | + input_channels, input_height, input_width, groups); |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +template class MaxOutGradFunctor<platform::GPUPlace, float>; |
| 147 | +template class MaxOutGradFunctor<platform::GPUPlace, double>; |
| 148 | + |
| 149 | +template class MaxOutFunctor<platform::GPUPlace, float>; |
| 150 | +template class MaxOutFunctor<platform::GPUPlace, double>; |
| 151 | + |
| 152 | +} // namespace math |
| 153 | +} // namespace operators |
| 154 | +} // namespace paddle |
0 commit comments