// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_ACTIVATION_HPP #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_ACTIVATION_HPP #include "../../op_cuda.hpp" #include "../csl/stream.hpp" #include "../csl/tensor.hpp" #include "../kernels/activations.hpp" #include #include namespace cv { namespace dnn { namespace cuda4dnn { template class ReLUOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; ReLUOp(csl::Stream stream_, T slope_) : stream(std::move(stream_)), slope{ slope_ } { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::relu(stream, output, input, slope); } } private: csl::Stream stream; const T slope; }; template class ClippedReLUOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; ClippedReLUOp(csl::Stream stream_, T min_, T max_) : stream(std::move(stream_)), min{ min_ }, max{ max_ } { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::clipped_relu(stream, output, input, min, max); } } private: csl::Stream stream; const T min, max; }; template class ChannelwiseReLUOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; ChannelwiseReLUOp(csl::Stream stream_, const Mat& slope) : stream(std::move(stream_)) { CV_Assert(!slope.empty()); slopeTensor = csl::makeTensorHeader(slope); csl::copyMatToTensor(slope, slopeTensor, stream); } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); CV_Assert(input.get_axis_size(1) == slopeTensor.size()); std::size_t inner_size = input.size_range(2, input.rank()); kernels::axiswise_relu(stream, output, input, inner_size, slopeTensor); } } private: csl::Stream stream; csl::Tensor slopeTensor; }; template class TanHOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; TanHOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::tanh(stream, output, input); } } private: csl::Stream stream; }; template class SwishOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; SwishOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::swish(stream, output, input); } } private: csl::Stream stream; }; template class MishOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; MishOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::mish(stream, output, input); } } private: csl::Stream stream; }; template class SigmoidOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; SigmoidOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::sigmoid(stream, output, input); } } private: csl::Stream stream; }; template class ELUOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; ELUOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::elu(stream, output, input); } } private: csl::Stream stream; }; template class AbsValOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; AbsValOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::abs(stream, output, input); } } private: csl::Stream stream; }; template class BNLLOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; BNLLOp(csl::Stream stream_) : stream(std::move(stream_)) { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::bnll(stream, output, input); } } private: csl::Stream stream; }; template class PowerOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; PowerOp(csl::Stream stream_, T exp_, T scale_, T shift_) : stream(std::move(stream_)), exp{ exp_ }, scale{ scale_ }, shift{ shift_ } { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::power(stream, output, input, exp, scale, shift); } } private: csl::Stream stream; const T exp, scale, shift; }; template class ExpOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; ExpOp(csl::Stream stream_, T nScale_, T nShift_) : stream(std::move(stream_)), normScale{ nScale_ }, normShift{ nShift_ } { } void forward( const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { for (int i = 0; i < inputs.size(); i++) { auto input_wrapper = inputs[i].dynamicCast(); auto input = input_wrapper->getView(); auto output_wrapper = outputs[i].dynamicCast(); auto output = output_wrapper->getSpan(); kernels::exp(stream, output, input, normScale, normShift); } } private: csl::Stream stream; const T normScale, normShift; }; }}} /* namespace cv::dnn::cuda4dnn */ #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_ACTIVATION_HPP */