// 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_INSTANCE_NORM_HPP #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_INSTANCE_NORM_HPP #include "../../op_cuda.hpp" #include "../csl/stream.hpp" #include "../csl/span.hpp" #include "../csl/tensor.hpp" #include "../csl/workspace.hpp" #include "../kernels/fill_copy.hpp" #include "../kernels/mvn.hpp" #include #include #include #include namespace cv { namespace dnn { namespace cuda4dnn { template class InstanceNormOp final : public CUDABackendNode { public: using wrapper_type = GetCUDABackendWrapperType; InstanceNormOp(csl::Stream stream_, float epsilon_, size_t loops) : stream(std::move(stream_)), epsilon(epsilon_) { csl::WorkspaceBuilder builder; builder.require(loops); builder.require(loops); scratch_mem_in_bytes = builder.required_workspace_size(); } void forward(const std::vector>& inputs, const std::vector>& outputs, csl::Workspace& workspace) override { auto input_wrapper = inputs[0].dynamicCast(); auto scale_wrapper = inputs[1].dynamicCast(); auto bias_wrapper = inputs[2].dynamicCast(); auto input = input_wrapper->getView(); auto scale = scale_wrapper->getView(); auto bias = bias_wrapper->getView(); auto output_wrapper = outputs[0].dynamicCast(); auto output = output_wrapper->getSpan(); auto C = input.get_axis_size(1); auto loops = input.size_range(0, 2); auto norm_size = input.size_range(2, input.rank()); if (norm_size == 1) { kernels::fill(stream, output, 0.f); return; } else { auto ws_allocator = csl::WorkspaceAllocator(workspace); auto mean = ws_allocator.get_span(loops); kernels::fill(stream, mean, 0.f); auto stdev = ws_allocator.get_span(loops); kernels::fill(stream, stdev, 0.f); kernels::reduce_mean_sqr_sum(stream, mean, stdev, input, norm_size); kernels::compute_normalization_scale(stream, stdev, mean, stdev, norm_size, epsilon); kernels::normalize_mean_variance_channelwise(stream, output, input, scale, bias, mean, stdev, norm_size, C); } } std::size_t get_workspace_memory_in_bytes() const noexcept override { return scratch_mem_in_bytes; } private: csl::Stream stream; float epsilon; std::size_t scratch_mem_in_bytes; }; }}} // cv::dnn::cuda4dnn #endif // OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_INSTANCE_NORM_HPP