/* Copyright (c) 2021 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. */ #pragma once #include #include #include "gtest/gtest.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/memory/memory.h" #include "paddle/fluid/string/printf.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/kernels/funcs/layer_norm_impl.cu.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/layer_norm_kernel.h" namespace framework = paddle::framework; namespace platform = paddle::platform; namespace memory = paddle::memory; USE_OP_ITSELF(dropout); USE_OP_ITSELF(layer_norm); template using CudnnDataType = phi::backends::gpu::CudnnDataType; template using LayerNormParamType = typename CudnnDataType::BatchNormParamType; /** * @brief call paddle dropout op */ template void Dropout(const std::vector &x, const framework::DDim &x_dim, std::vector *out, std::vector *mask, const phi::GPUContext &ctx, uint64_t seed, float dropout_prob, bool is_upscale_in_train, bool is_test) { framework::Scope scope; auto var_x = scope.Var("X"); auto tensor_x = var_x->GetMutable(); framework::TensorFromVector(x, ctx, tensor_x); tensor_x->Resize(x_dim); auto var_out = scope.Var("Out"); auto tensor_out = var_out->GetMutable(); auto var_mask = scope.Var("Mask"); auto tensor_mask = var_mask->GetMutable(); framework::AttributeMap attrs; attrs.insert({"fix_seed", 1}); attrs.insert({"seed", static_cast(seed)}); attrs.insert({"dropout_prob", dropout_prob}); if (is_upscale_in_train) { attrs.insert({"dropout_implementation", std::string("upscale_in_train")}); } if (is_test) { attrs.insert({"is_test", true}); } auto op = framework::OpRegistry::CreateOp( "dropout", {{"X", {"X"}}}, {{"Out", {"Out"}}, {"Mask", {"Mask"}}}, attrs); op->Run(scope, ctx.GetPlace()); framework::TensorToVector(*tensor_out, ctx, out); if (!is_test) { framework::TensorToVector(*tensor_mask, ctx, mask); } ctx.Wait(); } /** * @brief call paddle dropout_grad op */ template void DropoutGrad(std::vector *dx, const framework::DDim &x_dim, const std::vector &dout, const std::vector &mask, const phi::GPUContext &ctx, float dropout_prob, bool is_upscale_in_train) { framework::Scope scope; const size_t n = x_dim[0] * x_dim[1]; auto var_out = scope.Var("DOut"); auto tensor_out = var_out->GetMutable(); framework::TensorFromVector(dout, ctx, tensor_out); tensor_out->Resize(x_dim); auto var_mask = scope.Var("Mask"); auto tensor_mask = var_mask->GetMutable(); framework::TensorFromVector(mask, ctx, tensor_mask); tensor_mask->Resize(x_dim); auto var_dx = scope.Var("DX"); auto tensor_dx = var_dx->GetMutable(); framework::AttributeMap attrs; attrs.insert({"dropout_prob", dropout_prob}); attrs.insert({"is_test", false}); if (is_upscale_in_train) { attrs.insert({"dropout_implementation", std::string("upscale_in_train")}); } else { attrs.insert({"dropout_implementation", std::string("downgrade_in_infer")}); } auto op = framework::OpRegistry::CreateOp( "dropout_grad", {{"Out@GRAD", {"DOut"}}, {"Mask", {"Mask"}}}, {{"X@GRAD", {"DX"}}}, attrs); op->Run(scope, ctx.GetPlace()); framework::TensorToVector(*tensor_dx, ctx, dx); ctx.Wait(); } /** * @brief call paddle layer_norm op */ template void LayerNorm(const std::vector> &scale, const std::vector> &bias, const std::vector &x, std::vector> *means, std::vector> *vars, std::vector *y, const float epsilon, const int rows, const int cols, const phi::GPUContext &ctx) { framework::Scope scope; auto place = ctx.GetPlace(); paddle::optional scale_opt; if (scale.size() > 0) { auto var_scale = scope.Var("Scale"); auto tensor_scale = var_scale->GetMutable(); framework::TensorFromVector(scale, ctx, tensor_scale); tensor_scale->Resize({cols}); scale_opt = *tensor_scale; } paddle::optional bias_opt; if (bias.size() > 0) { auto var_bias = scope.Var("Bias"); auto tensor_bias = var_bias->GetMutable(); framework::TensorFromVector(bias, ctx, tensor_bias); tensor_bias->Resize({cols}); bias_opt = *tensor_bias; } auto var_x = scope.Var("X"); auto tensor_x = var_x->GetMutable(); framework::TensorFromVector(x, ctx, tensor_x); tensor_x->Resize({rows, cols}); auto var_y = scope.Var("Y"); auto tensor_y = var_y->GetMutable(); tensor_y->Resize({rows, cols}); auto var_mean = scope.Var("Mean"); auto tensor_mean = var_mean->GetMutable(); tensor_mean->Resize({rows}); auto var_variance = scope.Var("Variance"); auto tensor_variance = var_variance->GetMutable(); tensor_variance->Resize({rows}); ctx.Wait(); phi::LayerNormKernel(static_cast(ctx), *tensor_x, scale_opt, bias_opt, 1e-5, 1, tensor_y, tensor_mean, tensor_variance); framework::TensorToVector(*tensor_y, ctx, y); framework::TensorToVector(*tensor_mean, ctx, means); framework::TensorToVector(*tensor_variance, ctx, vars); ctx.Wait(); } template inline void ReduceSum(const std::vector &dout, std::vector *dbias, const int rows, const int cols) { for (int j = 0; j < cols; j++) { std::vector tmp_dbias(rows); for (int i = 0; i < rows; i++) { tmp_dbias[i] = dout[i * cols + j]; } int tmp_rows = rows / 2; while (tmp_rows) { for (int i = 0; i < tmp_rows; i++) { tmp_dbias[i] += tmp_dbias[i + tmp_rows]; } tmp_rows /= 2; } (*dbias)[j] = tmp_dbias[0]; } }