/* 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/sync_batch_norm_op.cu.h" namespace paddle { namespace operators { template class SyncBatchNormKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { double epsilon = static_cast(ctx.Attr("epsilon")); const float momentum = ctx.Attr("momentum"); const bool is_test = ctx.Attr("is_test"); const std::string layout_str = ctx.Attr("data_layout"); const DataLayout layout = framework::StringToDataLayout(layout_str); const bool use_global_stats = ctx.Attr("use_global_stats"); const bool trainable_stats = ctx.Attr("trainable_statistics"); PADDLE_ENFORCE_EQ(use_global_stats, false, platform::errors::InvalidArgument( "sync_batch_norm doesn't support " "to set use_global_stats True. Please use batch_norm " "in this case.")); const auto *x = ctx.Input("X"); auto *y = ctx.Output("Y"); const auto *est_mean = ctx.Input("Mean"); const auto *est_var = ctx.Input("Variance"); // moving mean/variance auto *mean_out = ctx.Output("MeanOut"); auto *variance_out = ctx.Output("VarianceOut"); auto *saved_mean = ctx.Output("SavedMean"); auto *saved_inv_variance = ctx.Output("SavedVariance"); bool test_mode = is_test && (!trainable_stats); SyncBatchNormFunctor( ctx, layout, x, y, est_mean, est_var, mean_out, variance_out, saved_mean, saved_inv_variance, epsilon, momentum, test_mode, use_global_stats); } }; template class SyncBatchNormGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { PADDLE_ENFORCE_EQ( platform::is_gpu_place(ctx.GetPlace()), true, platform::errors::InvalidArgument("It must use CUDAPlace.")); double epsilon = static_cast(ctx.Attr("epsilon")); const std::string layout_str = ctx.Attr("data_layout"); const DataLayout layout = framework::StringToDataLayout(layout_str); const auto *d_y = ctx.Input(framework::GradVarName("Y")); const auto *scale = ctx.Input("Scale"); const auto *bias = ctx.Input("Bias"); // init output auto *d_x = ctx.Output(framework::GradVarName("X")); auto *d_scale = ctx.Output(framework::GradVarName("Scale")); auto *d_bias = ctx.Output(framework::GradVarName("Bias")); const auto *saved_mean = ctx.Input("SavedMean"); const auto *saved_inv_var = ctx.Input("SavedVariance"); SyncBatchNormGradFunctor( ctx, layout, scale, bias, d_x, d_y, d_scale, d_bias, saved_mean, saved_inv_var, epsilon); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; namespace plat = paddle::platform; REGISTER_OP_CUDA_KERNEL( sync_batch_norm, ops::SyncBatchNormKernel, ops::SyncBatchNormKernel, ops::SyncBatchNormKernel); REGISTER_OP_CUDA_KERNEL( sync_batch_norm_grad, ops::SyncBatchNormGradKernel, ops::SyncBatchNormGradKernel, ops::SyncBatchNormGradKernel); // clang-format on