memcpy_op.cc 4.7 KB
Newer Older
J
JZ-LIANG 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2020 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/memcpy_op.h"

#include <string>

19 20 21
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/infermeta/unary.h"

J
JZ-LIANG 已提交
22 23 24
namespace paddle {
namespace framework {
class OpDesc;
25 26 27
class InferShapeContext;
template <typename T>
class EmptyGradOpMaker;
J
JZ-LIANG 已提交
28 29 30 31 32 33 34 35 36 37 38
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

namespace paddle {
namespace operators {

class MemcpyOp : public framework::OperatorWithKernel {
 public:
39 40
  MemcpyOp(const std::string &type,
           const framework::VariableNameMap &inputs,
J
JZ-LIANG 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
           const framework::VariableNameMap &outputs,
           const framework::AttributeMap &attrs)
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

  void InferShape(framework::InferShapeContext *ctx) const override {
    auto type = ctx->GetInputsVarType("X")[0];
    if (type == framework::proto::VarType::SELECTED_ROWS ||
        type == framework::proto::VarType::LOD_TENSOR) {
      ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
      if (type == framework::proto::VarType::LOD_TENSOR) {
        ctx->ShareLoD("X", /*->*/ "Out");
      }
    }
  }

 protected:
57
  phi::KernelKey GetKernelTypeForVar(
58
      const std::string &var_name,
59
      const phi::DenseTensor &tensor,
60 61 62 63
      const phi::KernelKey &expected_kernel_type) const override {
    return phi::KernelKey(phi::Backend::ALL_BACKEND,
                          tensor.layout(),
                          expected_kernel_type.dtype());
J
JZ-LIANG 已提交
64 65
  }

66
  phi::KernelKey GetExpectedKernelType(
J
JZ-LIANG 已提交
67
      const framework::ExecutionContext &ctx) const override {
68 69
    return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"),
                          ctx.GetPlace());
J
JZ-LIANG 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  }
};

class MemcpyInferVarType : public framework::VarTypeInference {
 public:
  void operator()(framework::InferVarTypeContext *ctx) const override {
    ctx->SyncTypeAndDataType("X", "Out");
  }
};

class MemcpyKernel {
 public:
  void operator()(const framework::ExecutionContext &ctx) const {
    auto *x = ctx.InputVar("X");
    if (x == nullptr) {
      return;
    }
    PADDLE_ENFORCE_EQ(
88 89
        ctx.HasOutput("Out"),
        true,
J
JZ-LIANG 已提交
90 91 92 93 94 95 96 97 98 99 100 101
        platform::errors::NotFound("Output(Out) of memcpy_op is not found."));
    auto *out = ctx.OutputVar("Out");
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
    auto &dev_ctx = *pool.Get(ctx.GetPlace());
    auto dst_place_type = ctx.Attr<int>("dst_place_type");
    framework::VisitVarType(*x, MemcpyFunctor(out, dev_ctx, dst_place_type));
  }
};

class MemcpyOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
102
    AddInput("X", "(phi::DenseTensor) The input variable ");
J
JZ-LIANG 已提交
103
    AddOutput("Out",
104
              "(phi::DenseTensor) The type of output "
J
JZ-LIANG 已提交
105 106 107
              "is the same as input X.");
    AddAttr<int>("dst_place_type",
                 "Determine the dst place of tensor copy. "
张春乔 已提交
108
                 "By Now it ONLY support CUDAPlace <-> CUDAPinnedPlace."
109
                 "Other place type is Unimplemented and will cause ERROR."
J
JZ-LIANG 已提交
110 111 112
                 "0: dst is on CPUPlace. "
                 "1: dst is on CUDAPlace. "
                 "2: dst is on CUDAPinnedPlace. "
113
                 "3: dst is on XPUPlace. "
张春乔 已提交
114
                 "5: dst is on CustomDevicePlace");
J
JZ-LIANG 已提交
115 116
    AddComment(R"DOC(
    Memcpy Operator.
张春乔 已提交
117
    By now, it ONLY supports the memcopy between CUDAPinnedPlace <-> CUDAPlace, and used as an internal op by Recompute-Offload.
J
JZ-LIANG 已提交
118 119
    You would have to update it if you want other more capacities.

120
Out = X,  when type in [phi::DenseTensor]
J
JZ-LIANG 已提交
121 122 123 124 125 126 127 128 129
raise error if the type is not listed above.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
130 131 132 133 134

DECLARE_INFER_SHAPE_FUNCTOR(memcpy,
                            MemcpyInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

J
JZ-LIANG 已提交
135
REGISTER_OPERATOR(
136 137 138 139
    memcpy,
    ops::MemcpyOp,
    ops::MemcpyOpProtoMaker,
    ops::MemcpyInferVarType,
J
JZ-LIANG 已提交
140
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
141 142
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    MemcpyInferShapeFunctor);