prepared_operator.cc 28.4 KB
Newer Older
J
Jiabin Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 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/imperative/prepared_operator.h"
16

J
Jiabin Yang 已提交
17
#include "paddle/fluid/eager/eager_tensor.h"
18
#include "paddle/fluid/framework/data_type_transform.h"
19
#include "paddle/fluid/framework/details/nan_inf_utils.h"
20
#include "paddle/fluid/imperative/infer_shape_context.h"
21
#include "paddle/fluid/imperative/tracer.h"
22
#include "paddle/phi/common/int_array.h"
23
#include "paddle/phi/common/scalar.h"
24
#include "paddle/utils/small_vector.h"
Q
QingshuChen 已提交
25
#ifdef PADDLE_WITH_XPU
26
#include "paddle/fluid/platform/device/xpu/xpu_op_list.h"
Q
QingshuChen 已提交
27
#endif
L
Liu-xiandong 已提交
28
#include "paddle/fluid/framework/library_type.h"
29
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
C
chenjian 已提交
30
#include "paddle/fluid/platform/profiler/event_tracing.h"
31

32
DECLARE_bool(check_nan_inf);
33
DECLARE_bool(benchmark);
F
Feng Xing 已提交
34
DECLARE_bool(run_kp_kernel);
35

J
Jiabin Yang 已提交
36 37 38
namespace paddle {
namespace imperative {

39
static const phi::Kernel empty_kernel;
40 41
static const framework::RuntimeContext empty_ctx({}, {});
static const framework::Scope empty_scope;
42

43 44 45 46 47 48 49
const phi::KernelFactory& PreparedOp::phi_kernel_factory =
    phi::KernelFactory::Instance();
const phi::OpUtilsMap& PreparedOp::phi_op_utils_map =
    phi::OpUtilsMap::Instance();
const phi::DefaultKernelSignatureMap& PreparedOp::default_phi_kernel_sig_map =
    phi::DefaultKernelSignatureMap::Instance();

50 51 52 53 54 55 56 57 58 59
const std::shared_ptr<VariableWrapper>& GetVariableWrapper(
    const std::shared_ptr<paddle::imperative::VarBase>& var) {
  return var->SharedVar();
}

const std::shared_ptr<VariableWrapper>& GetVariableWrapper(
    const std::shared_ptr<VariableWrapper>& var) {
  return var;
}

J
Jiabin Yang 已提交
60 61 62
const framework::Tensor* GetTensorFromVar(const framework::Variable& var) {
  if (var.IsType<framework::LoDTensor>()) {
    return &(var.Get<framework::LoDTensor>());
63 64
  } else if (var.IsType<phi::SelectedRows>()) {
    return &(var.Get<phi::SelectedRows>().value());
J
Jiabin Yang 已提交
65 66 67 68 69
  } else {
    return nullptr;
  }
}

70
template <typename VarType>
J
Jiabin Yang 已提交
71
void HandleComplexGradToRealGrad(const NameVarMap<VarType>& outs) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  for (auto& pair : outs) {
    for (auto& var : pair.second) {
      if (var == nullptr) {
        continue;
      }
      if (var->ForwardDataType() ==
          static_cast<framework::proto::VarType::Type>(-1)) {
        VLOG(6) << "Var (" << var->Name()
                << ")'s forward data type is not set.";
        continue;
      }
      if (!framework::IsComplexType(var->DataType()) ||
          framework::IsComplexType(var->ForwardDataType())) {
        continue;
      }
      const auto* tensor = GetTensorFromVar(var->Var());
J
Jiabin Yang 已提交
88
      if (tensor && tensor->IsInitialized()) {
89 90 91 92 93
        VLOG(6) << "Transform " << framework::DataTypeToString(var->DataType())
                << " var `" << var->Name() << "` to "
                << framework::DataTypeToString(var->ForwardDataType())
                << " real var in dynamic graph.";
        framework::Tensor out;
94 95
        framework::TransComplexToReal(
            var->ForwardDataType(), var->DataType(), *tensor, &out);
96
        SetTensorToVariable(var->Var(), out, var->MutableVar());
J
Jiabin Yang 已提交
97 98 99 100 101
      }
    }
  }
}

J
Jiabin Yang 已提交
102
template <>
103 104
void HandleComplexGradToRealGrad<egr::EagerVariable>(
    const NameVarMap<egr::EagerVariable>& outs) {
J
Jiabin Yang 已提交
105 106 107
  // TODO(jiabin): Support Complex here.
}

108 109 110 111 112
void TestHandleComplexGradToRealGradEager(
    const NameVarMap<egr::EagerVariable>& outs) {
  HandleComplexGradToRealGrad<egr::EagerVariable>(outs);
}

J
Jiabin Yang 已提交
113 114
PreparedOp::PreparedOp(const framework::OperatorBase& op,
                       const framework::RuntimeContext& ctx,
115
                       const framework::OpKernelType& kernel_type,
116
                       const framework::OperatorWithKernel::OpKernelFunc& func,
117 118
                       const phi::ArgumentMappingFn* arg_map_fn,
                       const phi::KernelSignature* default_kernel_signature,
119
                       platform::DeviceContext* dev_ctx)
120 121 122 123
    : op_(op),
      ctx_(ctx),
      kernel_type_(kernel_type),
      func_(func),
124
      dev_ctx_(dev_ctx),
125 126 127
      arg_map_fn_(arg_map_fn),
      default_kernel_signature_(default_kernel_signature),
      phi_kernel_(empty_kernel) {}
128

129 130 131
PreparedOp::PreparedOp(const framework::OperatorBase& op,
                       const framework::RuntimeContext& ctx,
                       const framework::OpKernelType& kernel_type,
132 133 134 135
                       const phi::ArgumentMappingFn* arg_map_fn,
                       const phi::KernelSignature* default_kernel_signature,
                       phi::KernelSignature&& kernel_signature,
                       const phi::Kernel& phi_kernel,
136 137 138 139 140 141
                       platform::DeviceContext* dev_ctx)
    : op_(op),
      ctx_(ctx),
      kernel_type_(kernel_type),
      func_(nullptr),
      dev_ctx_(dev_ctx),
142
      run_phi_kernel_(true),
143 144 145 146
      arg_map_fn_(arg_map_fn),
      default_kernel_signature_(default_kernel_signature),
      kernel_signature_(std::move(kernel_signature)),
      phi_kernel_(phi_kernel) {}
147

148
template <typename VarType>
149
PreparedOp PrepareImpl(
150 151 152 153
    const NameVarMap<VarType>& ins,
    const NameVarMap<VarType>& outs,
    const framework::OperatorWithKernel& op,
    const platform::Place& place,
154 155 156 157 158
    const framework::AttributeMap& attrs,
    const framework::AttributeMap& default_attrs,
    const phi::KernelFactory& phi_kernel_factory,
    const phi::OpUtilsMap& phi_op_utils_map,
    const phi::DefaultKernelSignatureMap& default_phi_kernel_sig_map) {
159
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
160
  auto* dev_ctx = pool.Get(place);
161

162 163 164 165 166 167
#ifdef PADDLE_WITH_MKLDNN
  // MKLDNN variant of code reads attributes in some of GetKernelTypeForVar and
  // GetKernelType functions, so we need to copy the attributes there.
  // Const qualifier of Attrs had to be discarded to overwrite it.
  if (FLAGS_use_mkldnn) {
    auto& mutable_op_attrs = const_cast<framework::AttributeMap&>(op.Attrs());
168 169 170 171
    mutable_op_attrs = default_attrs;
    for (auto& attr : attrs) {
      mutable_op_attrs[attr.first] = attr.second;
    }
172 173
  }
#endif
174 175
  // NOTE(zhiqiu): for kernels on given device, for example NPU, the order to
  // choose is:
176
  // phi npu kernel > fluid npu kernel > phi cpu kernel > fluid cpu kernel
J
Jiabin Yang 已提交
177

178
  // 1. get expected kernel key
179
  auto dygraph_exe_ctx = DygraphExecutionContext<VarType>(
180
      op, empty_scope, *dev_ctx, empty_ctx, ins, outs, attrs, default_attrs);
181
  auto expected_kernel_key = op.GetExpectedKernelType(dygraph_exe_ctx);
182

183 184
  const phi::KernelSignature* default_kernel_signature = nullptr;
  phi::KernelSignature kernel_signature;
185
  phi::KernelKey pt_kernel_key;
186
  std::string pt_kernel_name;
L
Liu-xiandong 已提交
187
#if defined(PADDLE_WITH_XPU)
188 189 190 191 192
  bool is_xpu_unsupport =
      paddle::platform::is_xpu_place(expected_kernel_key.place_) &&
          !paddle::platform::is_xpu_support_op(op.Type(),
                                               expected_kernel_key) ||
      paddle::platform::is_in_xpu_black_list(op.Type());
L
Liu-xiandong 已提交
193

194
#endif
195

196 197
  bool has_phi_kernel = false;

198 199
  const auto* arg_map_fn = phi_op_utils_map.GetArgumentMappingFn(op.Type());

200 201
  if (arg_map_fn) {
    has_phi_kernel = true;
202
    kernel_signature = (*arg_map_fn)(
203 204
        framework::ExecutionArgumentMappingContext(dygraph_exe_ctx));
  } else {
205
    default_kernel_signature =
206
        default_phi_kernel_sig_map.GetNullable(op.Type());
207
    if (default_kernel_signature) {
208
      has_phi_kernel = true;
209
      kernel_signature = *default_kernel_signature;
210 211 212 213
    }
  }

  if (has_phi_kernel) {
214 215
    VLOG(6) << kernel_signature;
    pt_kernel_name = kernel_signature.name;
216 217 218
// NOTE(Liu-xiandong): The register kernel used KP have library_type[KP],
// But the default library_type is Plain, so we need to modify the
// library_type here, otherwise it can't work.
L
Liu-xiandong 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#ifdef PADDLE_WITH_XPU_KP
    if (paddle::platform::is_xpu_place(expected_kernel_key.place_)) {
      bool use_xpu_kp_kernel_rt =
          FLAGS_run_kp_kernel && paddle::platform::is_xpu_kp_support_op(
                                     op.Type(), expected_kernel_key);
      bool use_xpu_kp_kernel_debug =
          paddle::platform::is_in_xpu_kpwhite_list(op.Type());
      if (use_xpu_kp_kernel_rt) {
        VLOG(3) << "phi xpu_kp using rt mode ";
      }
      if (use_xpu_kp_kernel_debug) {
        VLOG(3) << "phi xpu_kp using debug mode ";
      }
      bool is_xpu_kp_support =
          (use_xpu_kp_kernel_rt || use_xpu_kp_kernel_debug);
      if (is_xpu_kp_support) {
235 236
        auto expected_kernel_key_library_type =
            expected_kernel_key.library_type_;
L
Liu-xiandong 已提交
237
        expected_kernel_key.library_type_ = paddle::framework::LibraryType::kKP;
238
        VLOG(3) << "modifing XPU KP kernel: " << pt_kernel_name
L
Liu-xiandong 已提交
239
                << ", using_kernel_key:" << expected_kernel_key;
240

241 242
        phi::KernelKey try_pt_kernel_key =
            TransOpKernelTypeToPhiKernelKey(expected_kernel_key);
243
        if (!phi_kernel_factory.HasKernel(pt_kernel_name, try_pt_kernel_key)) {
244
          expected_kernel_key.library_type_ = expected_kernel_key_library_type;
245 246 247 248 249
          VLOG(3) << "modify XPU KP kernel: " << pt_kernel_name
                  << " in dynamic graph is failed " << expected_kernel_key;
        } else {
          VLOG(3) << "modify XPU KP kernel: " << pt_kernel_name
                  << " in dynamic graph is succeed " << expected_kernel_key;
250
        }
L
Liu-xiandong 已提交
251 252 253
      }
    }
#endif
254

255
    pt_kernel_key = TransOpKernelTypeToPhiKernelKey(expected_kernel_key);
256 257
    auto& phi_kernel =
        phi_kernel_factory.SelectKernel(pt_kernel_name, pt_kernel_key);
258

259
    if (phi_kernel.IsValid()
L
Liu-xiandong 已提交
260
#if defined(PADDLE_WITH_XPU) && !defined(PADDLE_WITH_XPU_KP)
261 262
        && !is_xpu_unsupport
#endif
263
    ) {
C
Chen Weihang 已提交
264
      VLOG(6) << "Dynamic mode PrepareImpl - kernel name: " << pt_kernel_name
265
              << " | kernel key: " << pt_kernel_key
266
              << " | kernel: " << phi_kernel;
267

F
From00 已提交
268 269
      if (expected_kernel_key.place_ != place) {
        dev_ctx = pool.Get(expected_kernel_key.place_);
W
Wilber 已提交
270
      }
F
From00 已提交
271

272 273 274 275 276 277 278 279
      return PreparedOp(op,
                        empty_ctx,
                        expected_kernel_key,
                        arg_map_fn,
                        default_kernel_signature,
                        std::move(kernel_signature),
                        phi_kernel,
                        dev_ctx);
280
    } else {
281
      VLOG(6) << "Dynamic mode ChoosePhiKernel - kernel `" << pt_kernel_name
282 283 284 285
              << "` not found.";
    }
  }

286
  // 2. check if op[type] has kernel registered.
J
Jiabin Yang 已提交
287 288
  auto& all_op_kernels = op.AllOpKernels();
  auto kernels_iter = all_op_kernels.find(op.Type());
289

290 291 292
// NOTE(Liu-xiandong): If we can't find heterogeneous kernel in phi,
// we need to select the heterogeneous kernel in fluid, but the kernel
// registered in KP use library_type[KP], we need to modify it.
293 294 295 296 297 298 299 300 301 302 303 304 305 306
#ifdef PADDLE_WITH_XPU_KP
  bool use_xpu_kp_kernel_rt =
      paddle::platform::is_xpu_place(expected_kernel_key.place_) &&
      FLAGS_run_kp_kernel &&
      paddle::platform::is_xpu_kp_support_op(op.Type(), expected_kernel_key);
  bool use_xpu_kp_kernel_debug =
      paddle::platform::is_xpu_place(expected_kernel_key.place_) &&
      paddle::platform::is_in_xpu_kpwhite_list(op.Type());
  bool is_xpu_kp_support = (use_xpu_kp_kernel_rt || use_xpu_kp_kernel_debug);
  if (is_xpu_kp_support) {
    expected_kernel_key.library_type_ = paddle::framework::LibraryType::kKP;
  }
#endif

307 308 309
  if ((kernels_iter == all_op_kernels.end() ||
       kernels_iter->second.find(expected_kernel_key) ==
           kernels_iter->second.end())
L
Liu-xiandong 已提交
310
#if defined(PADDLE_WITH_XPU) && !defined(PADDLE_WITH_XPU_KP)
311
      || is_xpu_unsupport
312
#endif
313 314 315
#if defined(PADDLE_WITH_XPU_KP)
      || (is_xpu_unsupport && !is_xpu_kp_support)
#endif
316
  ) {
317
    if (has_phi_kernel) {
318 319
      auto pt_cpu_kernel_key =
          FallBackToCpu(expected_kernel_key, pt_kernel_key, op);
320 321
      auto& pt_cpu_kernel =
          phi_kernel_factory.SelectKernel(pt_kernel_name, pt_cpu_kernel_key);
322 323 324 325 326
      if (pt_cpu_kernel.IsValid()) {
        VLOG(6) << "Dynamic mode PrepareImpl - kernel name: " << pt_kernel_name
                << " | kernel key: " << pt_cpu_kernel_key
                << " | kernel: " << pt_cpu_kernel;
        auto* cpu_ctx = pool.Get(paddle::platform::CPUPlace());
327
        return PreparedOp(
328 329
            op,
            empty_ctx,
330
            framework::TransPhiKernelKeyToOpKernelType(pt_cpu_kernel_key),
331 332 333 334 335
            arg_map_fn,
            default_kernel_signature,
            std::move(kernel_signature),
            pt_cpu_kernel,
            cpu_ctx);
336 337 338 339
      }
    }
  }

340
  PADDLE_ENFORCE_NE(
341 342
      kernels_iter,
      all_op_kernels.end(),
343 344 345
      platform::errors::NotFound(
          "There are no kernels which are registered in the %s operator.",
          op.Type()));
346

J
Jiabin Yang 已提交
347 348
  auto& kernels = kernels_iter->second;
  auto kernel_iter = kernels.find(expected_kernel_key);
349

350
#if defined(PADDLE_WITH_XPU) && !defined(PADDLE_WITH_XPU_KP)
351
  if (paddle::platform::is_xpu_place(expected_kernel_key.place_) &&
352
      (kernel_iter == kernels.end() || is_xpu_unsupport)) {
353
    VLOG(3) << "fluid missing XPU kernel: " << op.Type()
354 355
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
356 357 358
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
359
#endif
L
Liu-xiandong 已提交
360 361

#ifdef PADDLE_WITH_XPU_KP
362 363
  if (paddle::platform::is_xpu_place(expected_kernel_key.place_)) {
    if (use_xpu_kp_kernel_rt) {
364
      VLOG(3) << "fluid xpu_kp using rt mode ";
365 366
    }
    if (use_xpu_kp_kernel_debug) {
367
      VLOG(3) << "fluid xpu_kp using debug mode ";
368 369 370 371
    }
    if (is_xpu_kp_support) {
      expected_kernel_key.library_type_ = paddle::framework::LibraryType::kKP;
      kernel_iter = kernels.find(expected_kernel_key);
372
      VLOG(3) << "using fluid XPU KP kernel: " << op.Type()
373 374 375 376
              << ", using_kernel_key:" << expected_kernel_key;
    }
    if (!is_xpu_kp_support &&
        (kernel_iter == kernels.end() || is_xpu_unsupport)) {
377
      VLOG(3) << "fluid missing XPU kernel: " << op.Type()
378 379 380 381 382
              << ", expected_kernel_key:" << expected_kernel_key
              << ", fallbacking to CPU one!";
      expected_kernel_key.place_ = platform::CPUPlace();
      kernel_iter = kernels.find(expected_kernel_key);
    }
L
Liu-xiandong 已提交
383 384 385
  }
#endif

386 387
#ifdef PADDLE_WITH_ASCEND_CL
  if (kernel_iter == kernels.end() &&
388
      paddle::platform::is_npu_place(expected_kernel_key.place_)) {
389 390 391
    VLOG(3) << "missing NPU kernel: " << op.Type()
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
392 393 394
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
395 396 397
#endif
#ifdef PADDLE_WITH_MLU
  if (kernel_iter == kernels.end() &&
398
      paddle::platform::is_mlu_place(expected_kernel_key.place_)) {
399 400 401 402 403 404
    VLOG(3) << "missing MLU kernel: " << op.Type()
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
405 406 407 408 409 410 411 412 413 414
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
  if (kernel_iter == kernels.end() &&
      paddle::platform::is_custom_place(expected_kernel_key.place_)) {
    VLOG(3) << "missing " << place.GetDeviceType() << " kernel: " << op.Type()
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
415
#endif
416 417
  // TODO(jiabin): Add operator.cc's line 1000 part back when we need that
  // case
418 419 420 421 422 423
  PADDLE_ENFORCE_NE(
      kernel_iter,
      kernels.end(),
      platform::errors::NotFound("Operator %s does not have kernel for %s.",
                                 op.Type(),
                                 KernelTypeToString(expected_kernel_key)));
424

425 426 427 428
  if (!(expected_kernel_key.place_ == place)) {
    dev_ctx = pool.Get(expected_kernel_key.place_);
  }

429 430 431 432 433 434 435
  return PreparedOp(op,
                    empty_ctx,
                    expected_kernel_key,
                    kernel_iter->second,
                    arg_map_fn,
                    default_kernel_signature,
                    dev_ctx);
436 437
}

438 439 440 441
PreparedOp PreparedOp::Prepare(const NameVarMap<VarBase>& ins,
                               const NameVarMap<VarBase>& outs,
                               const framework::OperatorWithKernel& op,
                               const platform::Place& place,
442
                               const framework::AttributeMap& attrs,
443
                               const framework::AttributeMap& default_attrs) {
444 445 446 447 448 449 450 451
  return PrepareImpl<VarBase>(ins,
                              outs,
                              op,
                              place,
                              attrs,
                              default_attrs,
                              phi_kernel_factory,
                              phi_op_utils_map,
452
                              default_phi_kernel_sig_map);
453 454 455 456 457 458
}

PreparedOp PreparedOp::Prepare(const NameVarMap<VariableWrapper>& ins,
                               const NameVarMap<VariableWrapper>& outs,
                               const framework::OperatorWithKernel& op,
                               const platform::Place& place,
459
                               const framework::AttributeMap& attrs,
460
                               const framework::AttributeMap& default_attrs) {
461 462 463 464 465 466 467 468 469
  return PrepareImpl<VariableWrapper>(ins,
                                      outs,
                                      op,
                                      place,
                                      attrs,
                                      default_attrs,
                                      phi_kernel_factory,
                                      phi_op_utils_map,
                                      default_phi_kernel_sig_map);
470 471
}

472 473
PreparedOp PreparedOp::Prepare(const NameVarMap<egr::EagerVariable>& ins,
                               const NameVarMap<egr::EagerVariable>& outs,
J
Jiabin Yang 已提交
474 475 476 477
                               const framework::OperatorWithKernel& op,
                               const platform::Place& place,
                               const framework::AttributeMap& attrs,
                               const framework::AttributeMap& default_attrs) {
478 479 480 481 482 483 484 485 486
  return PrepareImpl<egr::EagerVariable>(ins,
                                         outs,
                                         op,
                                         place,
                                         attrs,
                                         default_attrs,
                                         phi_kernel_factory,
                                         phi_op_utils_map,
                                         default_phi_kernel_sig_map);
J
Jiabin Yang 已提交
487
}
488 489
template <typename VarType>
static void PreparedOpRunImpl(
490 491
    const framework::OperatorBase& op,
    const framework::RuntimeContext& ctx,
492
    const framework::OpKernelType& kernel_type,
493
    const framework::OperatorWithKernel::OpKernelFunc& func,
494 495
    const phi::ArgumentMappingFn* arg_map_fn,
    const phi::KernelSignature* default_kernel_signature,
496 497 498 499
    platform::DeviceContext* dev_ctx,
    const NameVarMap<VarType>& ins,
    const NameVarMap<VarType>& outs,
    const framework::AttributeMap& attrs,
500
    const framework::AttributeMap& default_attrs) {
J
Jiabin Yang 已提交
501
  // TODO(zjl): remove scope in dygraph
H
hong 已提交
502

503
  {
504
    platform::RecordEvent record_event("infer_shape",
C
chenjian 已提交
505
                                       platform::TracerEventType::OperatorInner,
506 507 508 509 510 511 512 513 514 515
                                       1,
                                       platform::EventRole::kInnerOp);
    DygraphInferShapeContext<VarType> infer_shape_ctx(&ins,
                                                      &outs,
                                                      &attrs,
                                                      &default_attrs,
                                                      op.Type(),
                                                      &kernel_type,
                                                      arg_map_fn,
                                                      default_kernel_signature);
516 517 518 519
    op.Info().infer_shape_(&infer_shape_ctx);
  }

  {
520
    platform::RecordEvent record_event("compute",
C
chenjian 已提交
521
                                       platform::TracerEventType::OperatorInner,
522 523
                                       1,
                                       platform::EventRole::kInnerOp);
H
hong 已提交
524

525 526
    func(DygraphExecutionContext<VarType>(
        op, empty_scope, *dev_ctx, ctx, ins, outs, attrs, default_attrs));
527
  }
528

529 530 531 532 533
  if (FLAGS_check_nan_inf) {
    framework::details::CheckOpHasNanOrInfInDygraph<VarType>(
        op.Type(), outs, dev_ctx->GetPlace());
  }

L
Leo Chen 已提交
534 535 536 537 538 539 540 541
  if (FLAGS_benchmark) {
    dev_ctx->Wait();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    PADDLE_ENFORCE_GPU_SUCCESS(platform::GpuGetLastError());
    VLOG(4) << "Operator(" << op.Type() << "): context wait and get last error";
#endif
  }

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  /**
   * [ Why need handle complex gradient to real gradient? ]
   *
   * After the introduction of complex number calculations, Ops that support
   * complex number calculations generally support type promotion, such as
   * x(float32) + y(complex64) = out(complex64), then the type of the grad
   * tensor should be dout(complex64), dx(float32), dy (complex64).
   *
   * But because the dout is complex64, the dx is also complex64 after
   * grad op kernel executed, we need to recognize this situation and
   * convert dx to float32 type. HandleComplexGradToRealGrad does this thing.
   */
  if (framework::IsComplexType(kernel_type.data_type_)) {
    HandleComplexGradToRealGrad<VarType>(outs);
  }
557
}
H
hong 已提交
558

559 560 561
template <typename VarType>
static void PreparedOpRunPtImpl(
    const framework::OperatorBase& op,
562
    const framework::OpKernelType& kernel_type,
563 564
    const phi::ArgumentMappingFn* arg_map_fn,
    const phi::KernelSignature* default_kernel_signature,
565 566 567 568 569 570
    const phi::KernelSignature& kernel_signature,
    const phi::Kernel& phi_kernel,
    platform::DeviceContext* dev_ctx,
    const NameVarMap<VarType>& ins,
    const NameVarMap<VarType>& outs,
    const framework::AttributeMap& attrs,
571
    const framework::AttributeMap& default_attrs) {
572
  {
573
    platform::RecordEvent record_event("infer_shape",
C
chenjian 已提交
574
                                       platform::TracerEventType::OperatorInner,
575 576 577 578 579 580 581 582 583 584
                                       1,
                                       platform::EventRole::kInnerOp);
    DygraphInferShapeContext<VarType> infer_shape_ctx(&ins,
                                                      &outs,
                                                      &attrs,
                                                      &default_attrs,
                                                      op.Type(),
                                                      &kernel_type,
                                                      arg_map_fn,
                                                      default_kernel_signature);
585 586 587 588
    op.Info().infer_shape_(&infer_shape_ctx);
  }

  {
589
    platform::RecordEvent record_event("compute",
C
chenjian 已提交
590
                                       platform::TracerEventType::OperatorInner,
591 592
                                       1,
                                       platform::EventRole::kInnerOp);
593

594
    PreparePhiData<VarType>(phi_kernel, kernel_signature, ins);
595

596
    phi::KernelContext pt_kernel_context;
597 598 599 600 601 602 603
    BuildDygraphPhiKernelContext<VarType>(kernel_signature,
                                          phi_kernel,
                                          ins,
                                          outs,
                                          attrs,
                                          default_attrs,
                                          dev_ctx,
604
                                          &pt_kernel_context);
605

606
    phi_kernel(&pt_kernel_context);
607
  }
608

609 610 611 612 613
  if (FLAGS_check_nan_inf) {
    framework::details::CheckOpHasNanOrInfInDygraph<VarType>(
        op.Type(), outs, dev_ctx->GetPlace());
  }

614 615
  if (FLAGS_benchmark) {
    dev_ctx->Wait();
616 617
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    PADDLE_ENFORCE_GPU_SUCCESS(platform::GpuGetLastError());
618 619 620 621
    VLOG(4) << "Operator(" << op.Type() << "): context wait and get last error";
#endif
  }

622 623 624
  if (framework::IsComplexType(kernel_type.data_type_)) {
    HandleComplexGradToRealGrad<VarType>(outs);
  }
625 626
}

627 628
void PreparedOp::Run(const NameVarMap<VarBase>& ins,
                     const NameVarMap<VarBase>& outs,
629 630
                     const framework::AttributeMap& attrs,
                     const framework::AttributeMap& default_attrs) {
631
  if (run_phi_kernel_) {
632 633 634 635 636 637 638 639 640 641
    PreparedOpRunPtImpl<VarBase>(op_,
                                 kernel_type_,
                                 arg_map_fn_,
                                 default_kernel_signature_,
                                 kernel_signature_,
                                 phi_kernel_,
                                 dev_ctx_,
                                 ins,
                                 outs,
                                 attrs,
642
                                 default_attrs);
643
  } else {
644 645 646 647 648 649 650 651 652 653 654
    PreparedOpRunImpl<VarBase>(op_,
                               ctx_,
                               kernel_type_,
                               func_,
                               arg_map_fn_,
                               default_kernel_signature_,
                               dev_ctx_,
                               ins,
                               outs,
                               attrs,
                               default_attrs);
655
  }
656
}
H
hong 已提交
657

658 659
void PreparedOp::Run(const NameVarMap<VariableWrapper>& ins,
                     const NameVarMap<VariableWrapper>& outs,
660 661
                     const framework::AttributeMap& attrs,
                     const framework::AttributeMap& default_attrs) {
662
  if (run_phi_kernel_) {
663 664 665 666 667 668 669 670 671 672 673
    PreparedOpRunPtImpl<VariableWrapper>(op_,
                                         kernel_type_,
                                         arg_map_fn_,
                                         default_kernel_signature_,
                                         kernel_signature_,
                                         phi_kernel_,
                                         dev_ctx_,
                                         ins,
                                         outs,
                                         attrs,
                                         default_attrs);
674
  } else {
675 676 677 678 679 680 681 682 683 684 685
    PreparedOpRunImpl<VariableWrapper>(op_,
                                       ctx_,
                                       kernel_type_,
                                       func_,
                                       arg_map_fn_,
                                       default_kernel_signature_,
                                       dev_ctx_,
                                       ins,
                                       outs,
                                       attrs,
                                       default_attrs);
686
  }
J
Jiabin Yang 已提交
687 688
}

689 690
void PreparedOp::Run(const NameVarMap<egr::EagerVariable>& ins,
                     const NameVarMap<egr::EagerVariable>& outs,
J
Jiabin Yang 已提交
691 692
                     const framework::AttributeMap& attrs,
                     const framework::AttributeMap& default_attrs) {
693
  if (run_phi_kernel_) {
694 695 696 697 698 699 700 701 702 703 704
    PreparedOpRunPtImpl<egr::EagerVariable>(op_,
                                            kernel_type_,
                                            arg_map_fn_,
                                            default_kernel_signature_,
                                            kernel_signature_,
                                            phi_kernel_,
                                            dev_ctx_,
                                            ins,
                                            outs,
                                            attrs,
                                            default_attrs);
J
Jiabin Yang 已提交
705
  } else {
706 707 708 709 710 711 712 713 714 715 716
    PreparedOpRunImpl<egr::EagerVariable>(op_,
                                          ctx_,
                                          kernel_type_,
                                          func_,
                                          arg_map_fn_,
                                          default_kernel_signature_,
                                          dev_ctx_,
                                          ins,
                                          outs,
                                          attrs,
                                          default_attrs);
J
Jiabin Yang 已提交
717 718 719
  }
}

J
Jiabin Yang 已提交
720 721
}  // namespace imperative
}  // namespace paddle