|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <random> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "gtest/gtest.h" |
| 21 | +#include "paddle/fluid/framework/op_registry.h" |
| 22 | +#include "paddle/fluid/framework/operator.h" |
| 23 | +#include "paddle/fluid/framework/program_desc.h" |
| 24 | +#include "paddle/fluid/framework/tensor_util.h" |
| 25 | +#include "paddle/fluid/memory/memory.h" |
| 26 | +#include "paddle/fluid/operators/math/math_function.h" |
| 27 | +#include "paddle/fluid/string/printf.h" |
| 28 | + |
| 29 | +namespace framework = paddle::framework; |
| 30 | +namespace platform = paddle::platform; |
| 31 | +namespace memory = paddle::memory; |
| 32 | + |
| 33 | +USE_OP(dropout); |
| 34 | + |
| 35 | +/** |
| 36 | + * @brief call paddle dropout op |
| 37 | + */ |
| 38 | +template <typename T> |
| 39 | +void Dropout(const std::vector<T> &x, const framework::DDim &x_dim, |
| 40 | + std::vector<T> *out, std::vector<uint8_t> *mask, |
| 41 | + const platform::CUDADeviceContext &ctx, uint64_t seed, |
| 42 | + float dropout_prob, bool is_upscale_in_train, bool is_test) { |
| 43 | + framework::Scope scope; |
| 44 | + auto var_x = scope.Var("X"); |
| 45 | + auto tensor_x = var_x->GetMutable<framework::LoDTensor>(); |
| 46 | + framework::TensorFromVector(x, ctx, tensor_x); |
| 47 | + tensor_x->Resize(x_dim); |
| 48 | + |
| 49 | + auto var_out = scope.Var("Out"); |
| 50 | + auto tensor_out = var_out->GetMutable<framework::LoDTensor>(); |
| 51 | + |
| 52 | + auto var_mask = scope.Var("Mask"); |
| 53 | + auto tensor_mask = var_mask->GetMutable<framework::LoDTensor>(); |
| 54 | + |
| 55 | + framework::AttributeMap attrs; |
| 56 | + attrs.insert({"fix_seed", 1}); |
| 57 | + attrs.insert({"seed", static_cast<int>(seed)}); |
| 58 | + attrs.insert({"dropout_prob", dropout_prob}); |
| 59 | + if (is_upscale_in_train) { |
| 60 | + attrs.insert({"dropout_implementation", std::string("upscale_in_train")}); |
| 61 | + } |
| 62 | + |
| 63 | + if (is_test) { |
| 64 | + attrs.insert({"is_test", true}); |
| 65 | + } |
| 66 | + |
| 67 | + auto op = framework::OpRegistry::CreateOp( |
| 68 | + "dropout", {{"X", {"X"}}}, {{"Out", {"Out"}}, {"Mask", {"Mask"}}}, attrs); |
| 69 | + op->Run(scope, ctx.GetPlace()); |
| 70 | + |
| 71 | + framework::TensorToVector<T>(*tensor_out, ctx, out); |
| 72 | + if (!is_test) { |
| 73 | + framework::TensorToVector<uint8_t>(*tensor_mask, ctx, mask); |
| 74 | + } |
| 75 | + ctx.Wait(); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * @brief call paddle dropout_grad op |
| 80 | + */ |
| 81 | +template <typename T> |
| 82 | +void DropoutGrad(std::vector<T> *dx, const framework::DDim &x_dim, |
| 83 | + const std::vector<T> &dout, const std::vector<uint8_t> &mask, |
| 84 | + const platform::CUDADeviceContext &ctx, float dropout_prob, |
| 85 | + bool is_upscale_in_train) { |
| 86 | + framework::Scope scope; |
| 87 | + const size_t n = x_dim[0] * x_dim[1]; |
| 88 | + auto var_out = scope.Var("DOut"); |
| 89 | + auto tensor_out = var_out->GetMutable<framework::LoDTensor>(); |
| 90 | + framework::TensorFromVector(dout, ctx, tensor_out); |
| 91 | + tensor_out->Resize(x_dim); |
| 92 | + |
| 93 | + auto var_mask = scope.Var("Mask"); |
| 94 | + auto tensor_mask = var_mask->GetMutable<framework::LoDTensor>(); |
| 95 | + framework::TensorFromVector(mask, ctx, tensor_mask); |
| 96 | + tensor_mask->Resize(x_dim); |
| 97 | + |
| 98 | + auto var_dx = scope.Var("DX"); |
| 99 | + auto tensor_dx = var_dx->GetMutable<framework::LoDTensor>(); |
| 100 | + |
| 101 | + framework::AttributeMap attrs; |
| 102 | + attrs.insert({"dropout_prob", dropout_prob}); |
| 103 | + attrs.insert({"is_test", false}); |
| 104 | + if (is_upscale_in_train) { |
| 105 | + attrs.insert({"dropout_implementation", std::string("upscale_in_train")}); |
| 106 | + } else { |
| 107 | + attrs.insert({"dropout_implementation", std::string("downgrade_in_infer")}); |
| 108 | + } |
| 109 | + |
| 110 | + auto op = framework::OpRegistry::CreateOp( |
| 111 | + "dropout_grad", {{"Out@GRAD", {"DOut"}}, {"Mask", {"Mask"}}}, |
| 112 | + {{"X@GRAD", {"DX"}}}, attrs); |
| 113 | + op->Run(scope, ctx.GetPlace()); |
| 114 | + |
| 115 | + framework::TensorToVector(*tensor_dx, ctx, dx); |
| 116 | + ctx.Wait(); |
| 117 | +} |
0 commit comments