// 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 Op, class T> struct BaseOp : public CUDABackendNode { protected: using wrapper_type = GetCUDABackendWrapperType; 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(); static_cast*>(this)->calculate(output, input); } } }; template class ReLUOp final : public BaseOp { public: ReLUOp(csl::Stream stream_, T slope_) : stream(std::move(stream_)), slope{ slope_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::relu(stream, output, input, slope); } private: csl::Stream stream; const T slope; }; template class ClippedReLUOp final : public BaseOp { public: ClippedReLUOp(csl::Stream stream_, T min_, T max_) : stream(std::move(stream_)), min{ min_ }, max{ max_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::clipped_relu(stream, output, input, min, max); } private: csl::Stream stream; const T min, max; }; template class ChannelwiseReLUOp final : public BaseOp { public: 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 calculate(csl::TensorSpan output, csl::TensorView input) const { 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 BaseOp { public: TanHOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::tanh(stream, output, input); } private: csl::Stream stream; }; template class SwishOp final : public BaseOp { public: SwishOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::swish(stream, output, input); } private: csl::Stream stream; }; template class MishOp final : public BaseOp { public: MishOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::mish(stream, output, input); } private: csl::Stream stream; }; template class SigmoidOp final : public BaseOp { public: SigmoidOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::sigmoid(stream, output, input); } private: csl::Stream stream; }; template class ELUOp final : public BaseOp { public: ELUOp(csl::Stream stream_, T alpha_) : stream(std::move(stream_)), alpha(alpha_) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::elu(stream, output, input, alpha); } private: csl::Stream stream; T alpha; }; template class AbsValOp final : public BaseOp { public: AbsValOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::abs(stream, output, input); } private: csl::Stream stream; }; template class BNLLOp final : public BaseOp { public: BNLLOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::bnll(stream, output, input); } private: csl::Stream stream; }; template class CeilOp final : public BaseOp { public: CeilOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::ceil(stream, output, input); } private: csl::Stream stream; }; template class FloorOp final : public BaseOp { public: FloorOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::floor(stream, output, input); } private: csl::Stream stream; }; template class LogOp final : public BaseOp { public: LogOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::log(stream, output, input); } private: csl::Stream stream; }; template class RoundOp final : public BaseOp { public: RoundOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::rint(stream, output, input); } private: csl::Stream stream; }; template class SqrtOp final : public BaseOp { public: SqrtOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::sqrt(stream, output, input); } private: csl::Stream stream; }; template class NotOp final : public BaseOp { public: NotOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::not_k(stream, output, input); } private: csl::Stream stream; }; template class AcosOp final : public BaseOp { public: AcosOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::acos(stream, output, input); } private: csl::Stream stream; }; template class AcoshOp final : public BaseOp { public: AcoshOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::acosh(stream, output, input); } private: csl::Stream stream; }; template class AsinOp final : public BaseOp { public: AsinOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::asin(stream, output, input); } private: csl::Stream stream; }; template class AsinhOp final : public BaseOp { public: AsinhOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::asinh(stream, output, input); } private: csl::Stream stream; }; template class AtanOp final : public BaseOp { public: AtanOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::atan(stream, output, input); } private: csl::Stream stream; }; template class AtanhOp final : public BaseOp { public: AtanhOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::atanh(stream, output, input); } private: csl::Stream stream; }; template class CosOp final : public BaseOp { public: CosOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::cos(stream, output, input); } private: csl::Stream stream; }; template class CoshOp final : public BaseOp { public: CoshOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::cosh(stream, output, input); } private: csl::Stream stream; }; template class ErfOp final : public BaseOp { public: ErfOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::erf(stream, output, input); } private: csl::Stream stream; }; template class HardSwishOp final : public BaseOp { public: HardSwishOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::hardswish(stream, output, input); } private: csl::Stream stream; }; template class SinOp final : public BaseOp { public: SinOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::sin(stream, output, input); } private: csl::Stream stream; }; template class SinhOp final : public BaseOp { public: SinhOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::sinh(stream, output, input); } private: csl::Stream stream; }; template class SoftplusOp final : public BaseOp { public: SoftplusOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::softplus(stream, output, input); } private: csl::Stream stream; }; template class SoftsignOp final : public BaseOp { public: SoftsignOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::softsign(stream, output, input); } private: csl::Stream stream; }; template class TanOp final : public BaseOp { public: TanOp(csl::Stream stream_) : stream(std::move(stream_)) { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::tan(stream, output, input); } private: csl::Stream stream; }; template class CeluOp final : public BaseOp { public: CeluOp(csl::Stream stream_, T alpha_) : stream(std::move(stream_)), alpha{ alpha_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::celu(stream, output, input, alpha); } private: csl::Stream stream; const T alpha; }; template class HardSigmoidOp final : public BaseOp { public: HardSigmoidOp(csl::Stream stream_, T alpha_, T beta_) : stream(std::move(stream_)), alpha{ alpha_ }, beta{ beta_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::hardsigmoid(stream, output, input, alpha, beta); } private: csl::Stream stream; const T alpha, beta; }; template class SeluOp final : public BaseOp { public: SeluOp(csl::Stream stream_, T alpha_, T gamma_) : stream(std::move(stream_)), alpha{ alpha_ }, gamma{ gamma_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::selu(stream, output, input, alpha, gamma); } private: csl::Stream stream; const T alpha, gamma; }; template class ThresholdedReluOp final : public BaseOp { public: ThresholdedReluOp(csl::Stream stream_, T alpha_) : stream(std::move(stream_)), alpha{ alpha_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::thresholdedrelu(stream, output, input, alpha); } private: csl::Stream stream; const T alpha; }; template class PowerOp final : public BaseOp { public: PowerOp(csl::Stream stream_, T exp_, T scale_, T shift_) : stream(std::move(stream_)), exp{ exp_ }, scale{ scale_ }, shift{ shift_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { kernels::power(stream, output, input, exp, scale, shift); } private: csl::Stream stream; const T exp, scale, shift; }; template class ExpOp final : public BaseOp { public: ExpOp(csl::Stream stream_, T nScale_, T nShift_) : stream(std::move(stream_)), normScale{ nScale_ }, normShift{ nShift_ } { } void calculate(csl::TensorSpan output, csl::TensorView input) const { 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 */