/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/batch_norm_op.h" #include "paddle/fluid/operators/inplace_abn_op.h" #include "paddle/fluid/operators/sync_batch_norm_op.cu.h" #include "paddle/phi/kernels/batch_norm_grad_kernel.h" #include "paddle/phi/kernels/batch_norm_kernel.h" namespace paddle { namespace operators { template class InplaceABNKernel : public paddle::operators::SyncBatchNormKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* y = ctx.Output("Y"); auto* x = ctx.Input("X"); PADDLE_ENFORCE_EQ(x, y, platform::errors::InvalidArgument( "X and Y not inplaced in inplace mode")); auto activation = GetInplaceABNActivationType(ctx.Attr("activation")); auto& place = *ctx.template device_context().eigen_device(); if (ctx.Attr("use_sync_bn")) { SyncBatchNormKernel::Compute(ctx); } else { // BatchNormKernel::Compute(ctx); auto* scale = ctx.Input("Scale"); auto* bias = ctx.Input("Bias"); auto* mean = ctx.Input("Mean"); auto* variance = ctx.Input("Variance"); auto momentum = ctx.Attr("momentum"); auto epsilon = ctx.Attr("epsilon"); auto data_layout = ctx.Attr("data_layout"); auto is_test = ctx.Attr("is_test"); auto use_global_stats = ctx.Attr("use_global_stats"); auto trainable_statistics = ctx.Attr("trainable_statistics"); auto fuse_with_relu = ctx.Attr("fuse_with_relu"); auto* mean_out = ctx.Output("MeanOut"); auto* variance_out = ctx.Output("VarianceOut"); auto* saved_mean = ctx.Output("SavedMean"); auto* saved_variance = ctx.Output("SavedVariance"); auto* reserve_space = ctx.Output("ReserveSpace"); auto& dev_ctx = ctx.device_context(); phi::BatchNormKernel( static_cast::TYPE&>(dev_ctx), *x, *scale, *bias, *mean, *variance, momentum, epsilon, data_layout, is_test, use_global_stats, trainable_statistics, fuse_with_relu, y, mean_out, variance_out, saved_mean, saved_variance, reserve_space); } auto cur_y = EigenVector::Flatten(*y); InplaceABNActivation functor; functor.Compute(ctx, activation, place, cur_y, cur_y); } }; // Deriving the Gradient for the Backward Pass of Batch Normalization // https://kevinzakka.github.io/2016/09/14/batch_normalization/ template class InplaceABNGradKernel : public paddle::operators::SyncBatchNormGradKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const auto* y = ctx.Input("Y"); auto* d_y = ctx.Input(framework::GradVarName("Y")); auto* d_x = ctx.Output(framework::GradVarName("X")); PADDLE_ENFORCE_EQ(d_x, d_y, platform::errors::InvalidArgument( "X@GRAD and Y@GRAD not inplaced in inplace mode")); auto& place = *ctx.template device_context().eigen_device(); auto activation = GetInplaceABNActivationType(ctx.Attr("activation")); auto py = *y; auto pd_y = *d_y; auto cur_y = EigenVector::Flatten(py); auto cur_dy = EigenVector::Flatten(pd_y); InplaceABNActivation functor; functor.GradCompute(ctx, activation, place, cur_y, cur_y, cur_dy, cur_dy); if (ctx.Attr("use_sync_bn")) { SyncBatchNormGradKernel::Compute(ctx); } else { auto* scale = ctx.Input("Scale"); auto* bias = ctx.Input("Bias"); auto* saved_mean = ctx.Input("SavedMean"); auto* saved_variance = ctx.Input("SavedVariance"); auto momentum = ctx.Attr("momentum"); auto epsilon = ctx.Attr("epsilon"); auto data_layout = ctx.Attr("data_layout"); auto is_test = ctx.Attr("is_test"); auto use_global_stats = ctx.Attr("use_global_stats"); auto trainable_statistics = ctx.Attr("trainable_statistics"); auto fuse_with_relu = ctx.Attr("fuse_with_relu"); auto* scale_grad = ctx.Output(framework::GradVarName("Scale")); auto* bias_grad = ctx.Output(framework::GradVarName("Bias")); auto* reserve_space = ctx.Input("ReserveSpace"); auto* mean = ctx.Input("ReserveSpace"); auto* variance = ctx.Input("ReserveSpace"); paddle::optional space_opt = paddle::none; paddle::optional mean_opt = paddle::none; paddle::optional variance_opt = paddle::none; if (reserve_space != nullptr) { space_opt = *reserve_space; } if (mean != nullptr) { mean_opt = *mean; } if (variance != nullptr) { variance_opt = *variance; } auto& dev_ctx = ctx.device_context(); phi::BatchNormGradRawKernel( static_cast::TYPE&>(dev_ctx), *d_y, *y, *scale, *bias, *saved_mean, *saved_variance, space_opt, mean_opt, variance_opt, momentum, epsilon, data_layout, is_test, use_global_stats, trainable_statistics, fuse_with_relu, true, d_x, scale_grad, bias_grad); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; namespace plat = paddle::platform; #ifdef PADDLE_WITH_HIP // MIOPEN do not support double REGISTER_OP_CUDA_KERNEL(inplace_abn, ops::InplaceABNKernel); REGISTER_OP_CUDA_KERNEL( inplace_abn_grad, ops::InplaceABNGradKernel); #else REGISTER_OP_CUDA_KERNEL(inplace_abn, ops::InplaceABNKernel, ops::InplaceABNKernel); REGISTER_OP_CUDA_KERNEL( inplace_abn_grad, ops::InplaceABNGradKernel, ops::InplaceABNGradKernel); #endif