|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/fluid/operators/memcpy_d2h_op.h" |
| 13 | + |
| 14 | +#include <string> |
| 15 | + |
| 16 | +namespace paddle { |
| 17 | +namespace framework { |
| 18 | +class OpDesc; |
| 19 | +class InferShapeContext; |
| 20 | +template <typename T> |
| 21 | +class EmptyGradOpMaker; |
| 22 | +} // namespace framework |
| 23 | +namespace imperative { |
| 24 | +class OpBase; |
| 25 | +} // namespace imperative |
| 26 | +namespace platform { |
| 27 | +struct CPUPlace; |
| 28 | +struct CUDAPlace; |
| 29 | +struct float16; |
| 30 | +} // namespace platform |
| 31 | +} // namespace paddle |
| 32 | + |
| 33 | +namespace paddle { |
| 34 | +namespace operators { |
| 35 | + |
| 36 | +class MemcpyD2HOp : public framework::OperatorWithKernel { |
| 37 | + public: |
| 38 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 39 | + |
| 40 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 41 | + auto type = ctx->GetInputsVarType("X")[0]; |
| 42 | + if (type == framework::proto::VarType::SELECTED_ROWS || |
| 43 | + type == framework::proto::VarType::LOD_TENSOR) { |
| 44 | + ctx->SetOutputDim("Out", ctx->GetInputDim("X")); |
| 45 | + if (type == framework::proto::VarType::LOD_TENSOR) { |
| 46 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + protected: |
| 52 | + framework::OpKernelType GetKernelTypeForVar( |
| 53 | + const std::string &var_name, const framework::Tensor &tensor, |
| 54 | + const framework::OpKernelType &expected_kernel_type) const override { |
| 55 | + return framework::OpKernelType(expected_kernel_type.data_type_, |
| 56 | + expected_kernel_type.place_, |
| 57 | + tensor.layout()); |
| 58 | + } |
| 59 | + |
| 60 | + framework::OpKernelType GetExpectedKernelType( |
| 61 | + const framework::ExecutionContext &ctx) const override { |
| 62 | + return framework::OpKernelType( |
| 63 | + OperatorWithKernel::IndicateVarDataType(ctx, "X"), |
| 64 | + ctx.device_context()); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +class MemcpyD2HInferVarType : public framework::VarTypeInference { |
| 69 | + public: |
| 70 | + void operator()(framework::InferVarTypeContext *ctx) const override { |
| 71 | + ctx->SyncTypeAndDataType("X", "Out"); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +class MemcpyD2HKernel { |
| 76 | + public: |
| 77 | + void operator()(const framework::ExecutionContext &ctx) const { |
| 78 | + auto *x = ctx.InputVar("X"); |
| 79 | + if (x == nullptr) { |
| 80 | + return; |
| 81 | + } |
| 82 | + PADDLE_ENFORCE_EQ(ctx.HasOutput("Out"), true, |
| 83 | + platform::errors::NotFound( |
| 84 | + "Output(Out) of memcpy_d2h_op is not found.")); |
| 85 | + auto *out = ctx.OutputVar("Out"); |
| 86 | + // Get dev_ctx from ExecutionContext, it's D2H stream |
| 87 | + auto &dev_ctx = ctx.device_context(); |
| 88 | + auto dst_place_type = ctx.Attr<int>("dst_place_type"); |
| 89 | + framework::VisitVarType(*x, MemcpyD2HFunctor(out, dev_ctx, dst_place_type)); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +class MemcpyD2HOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 94 | + public: |
| 95 | + void Make() override { |
| 96 | + AddInput("X", "(LoDTensor) The input variable "); |
| 97 | + AddOutput("Out", |
| 98 | + "(LoDTensor) The type of output " |
| 99 | + "is the same as input X."); |
| 100 | + AddAttr<int>( |
| 101 | + "dst_place_type", |
| 102 | + "Determine the dst place of tensor copy. " |
| 103 | + "By Now it ONLY support NPUPlace/CUDAPlace <-> CUDAPinnedPlace/CPU" |
| 104 | + "Other place type is Unimplemented and will cause ERROR." |
| 105 | + "0: dst is on CPUPlace. " |
| 106 | + "1: dst is on CUDAPinnedPlace. "); |
| 107 | + AddComment(R"DOC( |
| 108 | + MemcpyD2H Operator. |
| 109 | + By now, it ONLY supports the memcopy between NPUPlace/CUDAPlace <-> CUDAPinnedPlace/CPU. |
| 110 | + You would have to update it if you want other more capacities. |
| 111 | +Out = X, when type in [LoDTensor] |
| 112 | +raise error if the type is not listed above. |
| 113 | +)DOC"); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +} // namespace operators |
| 118 | +} // namespace paddle |
| 119 | + |
| 120 | +namespace ops = paddle::operators; |
| 121 | +namespace plat = paddle::platform; |
| 122 | +REGISTER_OPERATOR( |
| 123 | + memcpy_d2h, ops::MemcpyD2HOp, ops::MemcpyD2HOpProtoMaker, |
| 124 | + ops::MemcpyD2HInferVarType, |
| 125 | + paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, |
| 126 | + paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>); |
| 127 | + |
| 128 | +REGISTER_OP_CPU_KERNEL_FUNCTOR(memcpy_d2h, float, ops::MemcpyD2HKernel, double, |
| 129 | + ops::MemcpyD2HKernel, int, ops::MemcpyD2HKernel, |
| 130 | + int64_t, ops::MemcpyD2HKernel, bool, |
| 131 | + ops::MemcpyD2HKernel, plat::float16, |
| 132 | + ops::MemcpyD2HKernel); |
| 133 | + |
| 134 | +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_ROCM) |
| 135 | +REGISTER_OP_CUDA_KERNEL_FUNCTOR(memcpy_d2h, float, ops::MemcpyD2HKernel, double, |
| 136 | + ops::MemcpyD2HKernel, int, ops::MemcpyD2HKernel, |
| 137 | + int64_t, ops::MemcpyD2HKernel, bool, |
| 138 | + ops::MemcpyD2HKernel, plat::float16, |
| 139 | + ops::MemcpyD2HKernel); |
| 140 | +#endif |
| 141 | + |
| 142 | +#ifdef PADDLE_WITH_ASCEND_CL |
| 143 | +REGISTER_OP_NPU_KERNEL_FUNCTOR(memcpy_d2h, float, ops::MemcpyD2HKernel, double, |
| 144 | + ops::MemcpyD2HKernel, int, ops::MemcpyD2HKernel, |
| 145 | + int64_t, ops::MemcpyD2HKernel, bool, |
| 146 | + ops::MemcpyD2HKernel, plat::float16, |
| 147 | + ops::MemcpyD2HKernel); |
| 148 | +#endif |
0 commit comments