/* Copyright (c) 2016 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. */ #ifdef PADDLE_WITH_XPU #include "paddle/fluid/operators/batch_norm_op.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using DDim = framework::DDim; template class BatchNormXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const auto epsilon = ctx.Attr("epsilon"); const auto momentum = ctx.Attr("momentum"); const auto is_test = ctx.Attr("is_test"); const auto use_global_stats = ctx.Attr("use_global_stats"); const auto trainable_stats = ctx.Attr("trainable_statistics"); bool test_mode = is_test && (!trainable_stats); bool global_stats = test_mode || use_global_stats; const auto& data_layout_str = ctx.Attr("data_layout"); const auto data_layout = framework::StringToDataLayout(data_layout_str); PADDLE_ENFORCE_EQ(data_layout, DataLayout::kNCHW, platform::errors::InvalidArgument( "The 'data_layout' attribute must be NCHW. But " "recevived 'data_layout' is [%s].", data_layout_str)); const auto* x = ctx.Input("X"); const auto& x_dims = x->dims(); PADDLE_ENFORCE_EQ(x_dims.size(), 4, platform::errors::InvalidArgument( "The input tensor X's dimension must equal to 4. But " "received X's shape = [%s], X's dimension = [%d].", x_dims, x_dims.size())); const int N = x_dims[0]; const int C = x_dims[1]; const int H = x_dims[2]; const int W = x_dims[3]; const auto* scale = ctx.Input("Scale"); const auto* bias = ctx.Input("Bias"); const auto* x_data = x->data(); const auto* scale_data = scale->data(); const auto* bias_data = bias->data(); auto* y = ctx.Output("Y"); auto* y_data = y->mutable_data(ctx.GetPlace()); auto& dev_ctx = ctx.template device_context(); if (!global_stats) { auto* mean_out = ctx.Output("MeanOut"); auto* variance_out = ctx.Output("VarianceOut"); auto* saved_mean = ctx.Output("SavedMean"); auto* saved_variance = ctx.Output("SavedVariance"); mean_out->mutable_data(ctx.GetPlace()); variance_out->mutable_data(ctx.GetPlace()); saved_mean->mutable_data(ctx.GetPlace()); saved_variance->mutable_data(ctx.GetPlace()); auto* mean_out_data = mean_out->data(); auto* variance_out_data = variance_out->data(); auto* saved_mean_data = saved_mean->data(); auto* saved_variance_data = saved_variance->data(); int r = xpu::batch_norm(dev_ctx.x_context(), x_data, y_data, N, C, H, W, epsilon, momentum, scale_data, bias_data, saved_mean_data, saved_variance_data, mean_out_data, variance_out_data, true); PADDLE_ENFORCE_EQ( r, XPU_SUCCESS, platform::errors::External("XPU API(batch_norm_train_forward) return " "wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", r)); } else { const auto* mean = ctx.Input("Mean"); const auto* variance = ctx.Input("Variance"); const auto* mean_data = mean->data(); const auto* variance_data = variance->data(); int r = xpu::batch_norm_infer_forward( dev_ctx.x_context(), epsilon, N, C, H, W, x_data, y_data, scale_data, bias_data, mean_data, variance_data); PADDLE_ENFORCE_EQ( r, XPU_SUCCESS, platform::errors::External("XPU API(batch_norm_infer_forward) return " "wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", r)); } } }; template class BatchNormGradXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const auto* x = ctx.Input("X"); const auto* dy = ctx.Input(framework::GradVarName("Y")); const auto* scale = ctx.Input("Scale"); const auto* saved_mean = ctx.Input("SavedMean"); // SavedVariance have been reverted in forward operator const auto* saved_inv_variance = ctx.Input("SavedVariance"); const auto& data_layout_str = ctx.Attr("data_layout"); const auto data_layout = framework::StringToDataLayout(data_layout_str); PADDLE_ENFORCE_EQ(data_layout, DataLayout::kNCHW, platform::errors::InvalidArgument( "The 'data_layout' attribute must be NCHW. But " "recevived 'data_layout' is [%s].", data_layout_str)); const auto& x_dims = x->dims(); PADDLE_ENFORCE_EQ(x_dims.size(), 4, platform::errors::InvalidArgument( "The input tensor X's dimension must equal to 4. But " "received X's shape = [%s], X's dimension = [%d].", x_dims, x_dims.size())); const int N = x_dims[0]; const int C = x_dims[1]; const int H = x_dims[2]; const int W = x_dims[3]; const auto* x_data = x->data(); const auto* dy_data = dy->data(); const auto* scale_data = scale->data(); const auto* saved_mean_data = saved_mean->data(); const auto* saved_inv_variance_data = saved_inv_variance->data(); auto* dx = ctx.Output(framework::GradVarName("X")); auto* dscale = ctx.Output(framework::GradVarName("Scale")); auto* dbias = ctx.Output(framework::GradVarName("Bias")); auto* dx_data = dx->mutable_data(ctx.GetPlace()); auto* dscale_data = dscale->mutable_data(ctx.GetPlace()); auto* dbias_data = dbias->mutable_data(ctx.GetPlace()); auto& dev_ctx = ctx.template device_context(); int r = xpu::batch_norm_backward(dev_ctx.x_context(), N, C, H, W, x_data, dy_data, scale_data, saved_mean_data, saved_inv_variance_data, dx_data, dscale_data, dbias_data); PADDLE_ENFORCE_EQ( r, XPU_SUCCESS, platform::errors::External("XPU API(batch_norm_infer_forward) return " "wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", r)); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_XPU_KERNEL( batch_norm, ops::BatchNormXPUKernel); REGISTER_OP_XPU_KERNEL( batch_norm_grad, ops::BatchNormGradXPUKernel); #endif // PADDLE_WITH_XPU