memcpy_d2h_op.cc 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* 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. */

#include "paddle/fluid/operators/memcpy_d2h_op.h"

#include <string>

namespace paddle {
namespace framework {
class OpDesc;
class InferShapeContext;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

namespace paddle {
namespace operators {

class MemcpyD2HOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  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:
  framework::OpKernelType GetKernelTypeForVar(
48 49
      const std::string &var_name,
      const framework::Tensor &tensor,
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      const framework::OpKernelType &expected_kernel_type) const override {
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   expected_kernel_type.place_,
                                   tensor.layout());
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
  }
};

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

class MemcpyD2HKernel {
 public:
  void operator()(const framework::ExecutionContext &ctx) const {
    auto *x = ctx.InputVar("X");
    if (x == nullptr) {
      return;
    }
78 79
    PADDLE_ENFORCE_EQ(ctx.HasOutput("Out"),
                      true,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                      platform::errors::NotFound(
                          "Output(Out) of memcpy_d2h_op is not found."));
    auto *out = ctx.OutputVar("Out");
    // Get dev_ctx from ExecutionContext, it's D2H stream
    auto &dev_ctx = ctx.device_context();
    auto dst_place_type = ctx.Attr<int>("dst_place_type");
    framework::VisitVarType(*x, MemcpyD2HFunctor(out, dev_ctx, dst_place_type));
  }
};

class MemcpyD2HOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(LoDTensor) The input variable ");
    AddOutput("Out",
              "(LoDTensor) The type of output "
              "is the same as input X.");
    AddAttr<int>(
        "dst_place_type",
        "Determine the dst place of tensor copy. "
100
        "By Now it ONLY support XPU/NPUPlace/CUDAPlace <-> CUDAPinnedPlace/CPU"
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        "Other place type is Unimplemented and will cause ERROR."
        "0: dst is on CPUPlace. "
        "1: dst is on CUDAPinnedPlace. ");
    AddComment(R"DOC(
    MemcpyD2H Operator.
    By now, it ONLY supports the memcopy between NPUPlace/CUDAPlace <-> CUDAPinnedPlace/CPU.
    You would have to update it if you want other more capacities.
Out = X,  when type in [LoDTensor]
raise error if the type is not listed above.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OPERATOR(
120 121 122
    memcpy_d2h,
    ops::MemcpyD2HOp,
    ops::MemcpyD2HOpProtoMaker,
123 124 125 126
    ops::MemcpyD2HInferVarType,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
REGISTER_OP_CPU_KERNEL_FUNCTOR(memcpy_d2h,
                               float,
                               ops::MemcpyD2HKernel,
                               double,
                               ops::MemcpyD2HKernel,
                               int8_t,
                               ops::MemcpyD2HKernel,
                               uint8_t,
                               ops::MemcpyD2HKernel,
                               int,
                               ops::MemcpyD2HKernel,
                               int64_t,
                               ops::MemcpyD2HKernel,
                               bool,
                               ops::MemcpyD2HKernel,
                               paddle::platform::bfloat16,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<float>,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<double>,
                               ops::MemcpyD2HKernel,
                               plat::float16,
                               ops::MemcpyD2HKernel,
                               int16_t,
                               ops::MemcpyD2HKernel);
152

153
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
REGISTER_OP_CUDA_KERNEL_FUNCTOR(memcpy_d2h,
                                float,
                                ops::MemcpyD2HKernel,
                                double,
                                ops::MemcpyD2HKernel,
                                int8_t,
                                ops::MemcpyD2HKernel,
                                uint8_t,
                                ops::MemcpyD2HKernel,
                                int,
                                ops::MemcpyD2HKernel,
                                int64_t,
                                ops::MemcpyD2HKernel,
                                bool,
                                ops::MemcpyD2HKernel,
                                paddle::platform::bfloat16,
                                ops::MemcpyD2HKernel,
                                paddle::platform::complex<float>,
                                ops::MemcpyD2HKernel,
                                paddle::platform::complex<double>,
                                ops::MemcpyD2HKernel,
                                plat::float16,
                                ops::MemcpyD2HKernel,
                                int16_t,
                                ops::MemcpyD2HKernel);
179 180 181
#endif

#ifdef PADDLE_WITH_XPU
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
REGISTER_OP_XPU_KERNEL_FUNCTOR(memcpy_d2h,
                               float,
                               ops::MemcpyD2HKernel,
                               double,
                               ops::MemcpyD2HKernel,
                               int8_t,
                               ops::MemcpyD2HKernel,
                               uint8_t,
                               ops::MemcpyD2HKernel,
                               int,
                               ops::MemcpyD2HKernel,
                               int64_t,
                               ops::MemcpyD2HKernel,
                               bool,
                               ops::MemcpyD2HKernel,
                               paddle::platform::bfloat16,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<float>,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<double>,
                               ops::MemcpyD2HKernel,
                               plat::float16,
                               ops::MemcpyD2HKernel,
                               int16_t,
                               ops::MemcpyD2HKernel);
207 208 209
#endif

#ifdef PADDLE_WITH_ASCEND_CL
210 211 212 213 214
REGISTER_OP_NPU_KERNEL_FUNCTOR(memcpy_d2h,
                               float,
                               ops::MemcpyD2HKernel,
                               double,
                               ops::MemcpyD2HKernel,
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                               int8_t,
                               ops::MemcpyD2HKernel,
                               uint8_t,
                               ops::MemcpyD2HKernel,
                               int,
                               ops::MemcpyD2HKernel,
                               int64_t,
                               ops::MemcpyD2HKernel,
                               bool,
                               ops::MemcpyD2HKernel,
                               paddle::platform::bfloat16,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<float>,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<double>,
                               ops::MemcpyD2HKernel,
                               plat::float16,
                               ops::MemcpyD2HKernel,
                               int16_t,
                               ops::MemcpyD2HKernel);
#endif

#ifdef PADDLE_WITH_IPU
REGISTER_OP_IPU_KERNEL_FUNCTOR(memcpy_d2h,
                               float,
                               ops::MemcpyD2HKernel,
                               double,
                               ops::MemcpyD2HKernel,
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                               int8_t,
                               ops::MemcpyD2HKernel,
                               uint8_t,
                               ops::MemcpyD2HKernel,
                               int,
                               ops::MemcpyD2HKernel,
                               int64_t,
                               ops::MemcpyD2HKernel,
                               bool,
                               ops::MemcpyD2HKernel,
                               paddle::platform::bfloat16,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<float>,
                               ops::MemcpyD2HKernel,
                               paddle::platform::complex<double>,
                               ops::MemcpyD2HKernel,
                               plat::float16,
                               ops::MemcpyD2HKernel,
                               int16_t,
                               ops::MemcpyD2HKernel);
263
#endif