prepared_operator.cc 25.8 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

17
#include "paddle/fluid/framework/data_type_transform.h"
18
#include "paddle/fluid/framework/details/nan_inf_utils.h"
19
#include "paddle/fluid/imperative/infer_shape_context.h"
20
#include "paddle/fluid/imperative/tracer.h"
21
#include "paddle/pten/common/scalar.h"
22
#include "paddle/pten/common/scalar_array.h"
23
#include "paddle/utils/small_vector.h"
Q
QingshuChen 已提交
24
#ifdef PADDLE_WITH_XPU
25
#include "paddle/fluid/platform/device/xpu/xpu_op_list.h"
Q
QingshuChen 已提交
26
#endif
27
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
28
#include "paddle/fluid/platform/profiler.h"
29

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

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

38 39 40 41 42 43 44 45 46 47
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 已提交
48 49 50
const framework::Tensor* GetTensorFromVar(const framework::Variable& var) {
  if (var.IsType<framework::LoDTensor>()) {
    return &(var.Get<framework::LoDTensor>());
51 52
  } else if (var.IsType<pten::SelectedRows>()) {
    return &(var.Get<pten::SelectedRows>().value());
J
Jiabin Yang 已提交
53 54 55 56 57
  } else {
    return nullptr;
  }
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
static const framework::Attribute& GetAttr(
    const framework::AttributeMap& attrs,
    const framework::AttributeMap& default_attrs, const std::string& name) {
  auto it = attrs.find(name);
  bool found = it != attrs.end();
  if (!found) {
    it = default_attrs.find(name);
    found = it != default_attrs.end();
  }
  PADDLE_ENFORCE_EQ(
      found, true,
      platform::errors::NotFound("(%s) is not found in AttributeMap.", name));
  return it->second;
}

73
template <typename VarType>
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static void HandleComplexGradToRealGrad(const NameVarMap<VarType>& outs) {
  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 已提交
91
      if (tensor && tensor->IsInitialized()) {
92 93 94 95 96 97 98 99
        VLOG(6) << "Transform " << framework::DataTypeToString(var->DataType())
                << " var `" << var->Name() << "` to "
                << framework::DataTypeToString(var->ForwardDataType())
                << " real var in dynamic graph.";
        framework::Tensor out;
        framework::TransComplexToReal(var->ForwardDataType(), var->DataType(),
                                      *tensor, &out);
        SetTensorToVariable(var->Var(), out, var->MutableVar());
J
Jiabin Yang 已提交
100 101 102 103 104 105 106
      }
    }
  }
}

PreparedOp::PreparedOp(const framework::OperatorBase& op,
                       const framework::RuntimeContext& ctx,
107
                       const framework::OpKernelType& kernel_type,
108
                       const framework::OperatorWithKernel::OpKernelFunc& func,
109
                       platform::DeviceContext* dev_ctx)
110 111 112 113 114 115
    : op_(op),
      ctx_(ctx),
      kernel_type_(kernel_type),
      func_(func),
      dev_ctx_(dev_ctx) {}

116 117 118 119 120 121 122 123 124 125 126 127 128
PreparedOp::PreparedOp(const framework::OperatorBase& op,
                       const framework::RuntimeContext& ctx,
                       const framework::OpKernelType& kernel_type,
                       const framework::KernelSignature& kernel_signature,
                       const pten::Kernel& pt_kernel,
                       platform::DeviceContext* dev_ctx)
    : op_(op),
      ctx_(ctx),
      kernel_type_(kernel_type),
      func_(nullptr),
      dev_ctx_(dev_ctx),
      run_pten_kernel_(true),
      pt_kernel_signature_(kernel_signature),
129
      pt_kernel_(pt_kernel) {}
130

131 132 133 134 135
template <typename VarType>
PreparedOp PrepareImpl(const NameVarMap<VarType>& ins,
                       const NameVarMap<VarType>& outs,
                       const framework::OperatorWithKernel& op,
                       const platform::Place& place,
136
                       const framework::AttributeMap& attrs,
137
                       const framework::AttributeMap& default_attrs) {
138
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
139
  auto* dev_ctx = pool.Get(place);
140

141 142 143 144 145 146 147 148
  framework::RuntimeContext ctx({}, {});

#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());
149 150 151 152
    mutable_op_attrs = default_attrs;
    for (auto& attr : attrs) {
      mutable_op_attrs[attr.first] = attr.second;
    }
153 154
  }
#endif
J
Jiabin Yang 已提交
155

156
  // 1. get expected kernel key
157 158 159
  auto dygraph_exe_ctx = DygraphExecutionContext<VarType>(
      op, framework::Scope(), *dev_ctx, ctx, ins, outs, attrs, default_attrs);
  auto expected_kernel_key = op.GetExpectedKernelType(dygraph_exe_ctx);
160 161
  VLOG(3) << "expected_kernel_key:" << expected_kernel_key;

162 163 164
  if (FLAGS_run_pten_kernel &&
      pten::KernelFactory::Instance().HasCompatiblePtenKernel(op.Type())) {
    auto pt_kernel_signature = op.GetExpectedPtenKernelArgs(dygraph_exe_ctx);
165
    VLOG(6) << pt_kernel_signature;
166

Y
YuanRisheng 已提交
167
    auto pt_kernel_name = pt_kernel_signature.name;
168 169 170 171 172
    auto pt_kernel_key = TransOpKernelTypeToPtenKernelKey(expected_kernel_key);
    auto pt_kernel = pten::KernelFactory::Instance().SelectKernel(
        pt_kernel_name, pt_kernel_key);

    if (pt_kernel.IsValid()) {
C
Chen Weihang 已提交
173
      VLOG(6) << "Dynamic mode PrepareImpl - kernel name: " << pt_kernel_name
174 175 176
              << " | kernel key: " << pt_kernel_key
              << " | kernel: " << pt_kernel;

W
Wilber 已提交
177 178 179 180 181
      if (platform::is_cpu_place(expected_kernel_key.place_)) {
        auto* cpu_ctx = pool.Get(paddle::platform::CPUPlace());
        return PreparedOp(op, ctx, expected_kernel_key, pt_kernel_signature,
                          pt_kernel, cpu_ctx);
      }
182 183
      // TODO(chenweihang): using CPUKernel when miss device kernel case
      return PreparedOp(op, ctx, expected_kernel_key, pt_kernel_signature,
184
                        pt_kernel, dev_ctx);
185
    } else {
C
Chen Weihang 已提交
186
      VLOG(6) << "Dynamic mode ChoosePtenKernel - kernel `" << pt_kernel_name
187 188 189 190
              << "` not found.";
    }
  }

191
  // 2. check if op[type] has kernel registered.
J
Jiabin Yang 已提交
192 193
  auto& all_op_kernels = op.AllOpKernels();
  auto kernels_iter = all_op_kernels.find(op.Type());
194 195 196 197 198
  PADDLE_ENFORCE_NE(
      kernels_iter, all_op_kernels.end(),
      platform::errors::NotFound(
          "There are no kernels which are registered in the %s operator.",
          op.Type()));
J
Jiabin Yang 已提交
199 200 201

  auto& kernels = kernels_iter->second;
  auto kernel_iter = kernels.find(expected_kernel_key);
202
#ifdef PADDLE_WITH_XPU
203
  if (paddle::platform::is_xpu_place(expected_kernel_key.place_) &&
Q
QingshuChen 已提交
204 205 206
      (kernel_iter == kernels.end() ||
       !paddle::platform::is_xpu_support_op(op.Type(), expected_kernel_key) ||
       paddle::platform::is_in_xpu_black_list(op.Type()))) {
207 208 209
    VLOG(3) << "missing XPU kernel: " << op.Type()
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
210 211 212
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
213 214 215
#endif
#ifdef PADDLE_WITH_ASCEND_CL
  if (kernel_iter == kernels.end() &&
216
      paddle::platform::is_npu_place(expected_kernel_key.place_)) {
217 218 219
    VLOG(3) << "missing NPU kernel: " << op.Type()
            << ", expected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
220 221 222
    expected_kernel_key.place_ = platform::CPUPlace();
    kernel_iter = kernels.find(expected_kernel_key);
  }
223 224 225
#endif
#ifdef PADDLE_WITH_MLU
  if (kernel_iter == kernels.end() &&
226
      paddle::platform::is_mlu_place(expected_kernel_key.place_)) {
227 228 229 230 231 232
    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);
  }
233
#endif
234 235
  // TODO(jiabin): Add operator.cc's line 1000 part back when we need that
  // case
236 237 238 239
  PADDLE_ENFORCE_NE(kernel_iter, kernels.end(),
                    platform::errors::NotFound(
                        "Operator %s does not have kernel for %s.", op.Type(),
                        KernelTypeToString(expected_kernel_key)));
240

241 242 243 244
  if (!(expected_kernel_key.place_ == place)) {
    dev_ctx = pool.Get(expected_kernel_key.place_);
  }

245
  return PreparedOp(op, ctx, expected_kernel_key, kernel_iter->second, dev_ctx);
246 247
}

248 249 250 251
PreparedOp PreparedOp::Prepare(const NameVarMap<VarBase>& ins,
                               const NameVarMap<VarBase>& outs,
                               const framework::OperatorWithKernel& op,
                               const platform::Place& place,
252
                               const framework::AttributeMap& attrs,
253 254
                               const framework::AttributeMap& default_attrs) {
  return PrepareImpl<VarBase>(ins, outs, op, place, attrs, default_attrs);
255 256 257 258 259 260
}

PreparedOp PreparedOp::Prepare(const NameVarMap<VariableWrapper>& ins,
                               const NameVarMap<VariableWrapper>& outs,
                               const framework::OperatorWithKernel& op,
                               const platform::Place& place,
261
                               const framework::AttributeMap& attrs,
262
                               const framework::AttributeMap& default_attrs) {
263
  return PrepareImpl<VariableWrapper>(ins, outs, op, place, attrs,
264
                                      default_attrs);
265 266
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
template <typename VarType>
void PreparePtenData(const pten::Kernel& pt_kernel,
                     const framework::KernelSignature& pt_kernel_signature,
                     const NameVarMap<VarType>& ins) {
  auto& input_names = std::get<0>(pt_kernel_signature.args);
  auto& input_defs = pt_kernel.args_def().input_defs();

  PADDLE_ENFORCE_EQ(input_names.size(), input_defs.size(),
                    platform::errors::InvalidArgument(
                        "the size of inputs_args names (%d) must be equal to "
                        "the size of kernel input_defs (%d).",
                        input_names.size(), input_defs.size()));

  for (size_t i = 0; i < input_names.size(); ++i) {
    auto& in_def = input_defs.at(i);
    auto& ins_vector = ins.at(input_names[i]);

    for (size_t offset = 0; offset < ins_vector.size(); ++offset) {
      auto var_base = ins_vector[offset];
      const auto* tensor_in = GetTensorFromVar(var_base->Var());
      if (tensor_in && tensor_in->IsInitialized()) {
        auto expected_place = pten::TransToFluidPlace(in_def.backend);
        if (platform::is_same_place(tensor_in->place(), expected_place)) {
          continue;
        }

        // TODO(zyfncg): Now there is no kernel which need to transform input
        // data, so we commented out following code temporarily,
        // and it will be used in the future.

        // VLOG(3) << "Pten Transform Variable " << var_base->Name() << " from "
        //         << tensor_in->place() << " to " << expected_place;

        // framework::Tensor tmp_tensor;
        // framework::TensorCopySync(*tensor_in, expected_place, &tmp_tensor);

        // SetTensorToVariable(var_base->Var(), tmp_tensor,
        //                     var_base->MutableVar());
      }
    }
  }
}

310
template <typename VarType>
311
static void BuildDygraphPtenKernelContext(
312 313 314 315
    const framework::KernelSignature& pt_kernel_signature,
    const pten::Kernel& pt_kernel, const NameVarMap<VarType>& ins,
    const NameVarMap<VarType>& outs, const framework::AttributeMap& attrs,
    const framework::AttributeMap& default_attrs,
316 317
    platform::DeviceContext* dev_ctx, pten::KernelContext* kernel_ctx) {
  kernel_ctx->SetDeviceContext(dev_ctx);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

  auto& input_names = std::get<0>(pt_kernel_signature.args);
  auto& attr_names = std::get<1>(pt_kernel_signature.args);
  auto& output_names = std::get<2>(pt_kernel_signature.args);

  auto& input_defs = pt_kernel.args_def().input_defs();
  auto& output_defs = pt_kernel.args_def().output_defs();
  auto& attr_defs = pt_kernel.args_def().attribute_defs();

  PADDLE_ENFORCE_EQ(input_names.size(), input_defs.size(),
                    platform::errors::InvalidArgument(
                        "the size of inputs_args names (%d) must be equal to "
                        "the size of kernel input_defs (%d).",
                        input_names.size(), input_defs.size()));

  PADDLE_ENFORCE_EQ(output_names.size(), output_defs.size(),
                    platform::errors::InvalidArgument(
                        "the size of outputs_args names (%d) must be equal to "
                        "the size of kernel output_defs (%d).",
                        output_names.size(), output_defs.size()));

  PADDLE_ENFORCE_EQ(attr_names.size(), attr_defs.size(),
                    platform::errors::InvalidArgument(
                        "the size of attribute_args names (%d) must be equal "
                        "to the size of kernel attribute_defs (%d).",
                        attr_names.size(), attr_defs.size()));

  for (size_t i = 0; i < input_names.size(); ++i) {
    auto& ins_vector = ins.at(input_names[i]);
347 348 349

    size_t start_idx = (i == 0 ? 0 : kernel_ctx->InputRangeAt(i - 1).second);
    size_t end_idx = start_idx + ins_vector.size();
350 351

    for (size_t offset = 0; offset < ins_vector.size(); ++offset) {
352 353
      const auto* tensor_in = GetTensorFromVar(ins_vector[offset]->Var());
      kernel_ctx->EmplaceBackInputWithoutSetRange(tensor_in);
354
    }
355
    kernel_ctx->AssignInputRange(std::make_pair(start_idx, end_idx), i);
356 357 358
  }

  for (size_t i = 0; i < output_names.size(); ++i) {
359
    size_t start_idx = (i == 0 ? 0 : kernel_ctx->OutputRangeAt(i - 1).second);
360 361 362

    auto iter = outs.find(output_names[i]);
    if (iter == outs.end()) {
363
      kernel_ctx->EmplaceBackOutputWithoutSetRange({nullptr});
364 365 366 367 368 369 370 371
      kernel_ctx->AssignOutputRange(std::make_pair(start_idx, start_idx + 1),
                                    i);
      continue;
    }

    auto& outs_vector = iter->second;
    size_t end_idx = start_idx + outs_vector.size();

372
    for (size_t offset = 0; offset < outs_vector.size(); ++offset) {
373 374 375 376
      if (outs_vector[offset] == nullptr) {
        kernel_ctx->EmplaceBackOutputWithoutSetRange({nullptr});
        continue;
      }
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
      auto* var = outs_vector[offset]->MutableVar();
      framework::Tensor* tensor_out = nullptr;
      if (var->template IsType<framework::LoDTensor>()) {
        tensor_out = var->template GetMutable<framework::LoDTensor>();
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported output `%s` type when call pt kernel.",
            framework::ToTypeName(var->Type())));
      }  // TODO(zyfncg): Add support for SelectedRows

      experimental::ResetTensorByArgDef(tensor_out, output_defs.at(i));
      framework::SetAllocationForOutputTenosr(
          tensor_out, pten::TransToFluidPlace(output_defs.at(i).backend));

      kernel_ctx->EmplaceBackOutputWithoutSetRange(tensor_out);
392
    }
393
    kernel_ctx->AssignOutputRange(std::make_pair(start_idx, end_idx), i);
394 395 396
  }

  for (size_t i = 0; i < attr_names.size(); ++i) {
397 398 399 400 401 402 403 404
    if (attr_defs[i].type_index == std::type_index(typeid(pten::ScalarArray))) {
      if (attrs.find(attr_names[i]) !=
          attrs.end()) {  // shape is in the attribute
        auto& attr = GetAttr(attrs, default_attrs, attr_names[i]);
        if (std::type_index(attr.type()) ==
            std::type_index(typeid(std::vector<int64_t>))) {
          kernel_ctx->EmplaceBackAttr(std::move(
              pten::ScalarArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
405 406 407 408
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(std::vector<int32_t>))) {
          kernel_ctx->EmplaceBackAttr(std::move(
              pten::ScalarArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        } else {
          PADDLE_THROW(platform::errors::Unimplemented(
              "Unsupported cast op attribute `%s` to VectorTensor when "
              "construct KernelContext.",
              attr_names[i]));
        }
      } else {  // shape is in the input
        auto& ins_vector = ins.at(attr_names[i]);
        if (ins_vector.size() == 1) {  // ShapeTensor
          kernel_ctx->EmplaceBackAttr(std::move(
              experimental::MakePtenScalarArrayFromVar(ins_vector[0]->Var())));
        } else {  // ShapeTensorList
          std::vector<framework::Variable*> variables;
          variables.reserve(ins_vector.size());
          for (const auto& var_base : ins_vector) {
            variables.push_back(var_base->MutableVar());
          }
          kernel_ctx->EmplaceBackAttr(std::move(
              experimental::MakePtenScalarArrayFromVarList(variables)));
        }
      }
    } else if (attr_defs[i].type_index ==
               std::type_index(typeid(pten::Scalar))) {
432 433 434
      // TODO(chenweihang): support other attrs later
      // TODO(zhangyunfei): Scalar should hold scaler type, and we should check
      // attribtue type by attr_defs
435 436 437 438 439 440 441 442 443 444 445
      if (attrs.find(attr_names[i]) != attrs.end() ||
          default_attrs.find(attr_names[i]) !=
              default_attrs.end()) {  // scalar is in the attribute
        auto& attr = GetAttr(attrs, default_attrs, attr_names[i]);
        if (std::type_index(attr.type()) == std::type_index(typeid(float))) {
          kernel_ctx->EmplaceBackAttr(
              std::move(pten::Scalar(BOOST_GET_CONST(float, attr))));
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(std::string))) {
          kernel_ctx->EmplaceBackAttr(
              std::move(pten::Scalar(BOOST_GET_CONST(std::string, attr))));
446 447 448 449
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(int))) {
          kernel_ctx->EmplaceBackAttr(
              std::move(pten::Scalar(BOOST_GET_CONST(int, attr))));
450 451 452 453 454 455 456 457 458 459
        } else {
          PADDLE_THROW(platform::errors::Unimplemented(
              "Unsupported cast op attribute `%s` to Scalar when construct "
              "KernelContext in dygraph.",
              attr_names[i]));
        }
      } else {  // scalar is in the input
        auto& ins_vector = ins.at(attr_names[i]);
        kernel_ctx->EmplaceBackAttr(std::move(
            experimental::MakePtenScalarFromVar(ins_vector[0]->Var())));
460
      }
461

462 463
    } else {
      // TODO(chenweihang): support other attrs later
464
      auto& attr = GetAttr(attrs, default_attrs, attr_names[i]);
465
      if (attr_defs[i].type_index == std::type_index(typeid(int))) {
466
        kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(int, attr));
467
      } else if (attr_defs[i].type_index == std::type_index(typeid(float))) {
468
        kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(float, attr));
469
      } else if (attr_defs[i].type_index == std::type_index(typeid(bool))) {
470
        kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
471
      } else if (attr_defs[i].type_index ==
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                 std::type_index(typeid(pten::DataType))) {
        auto data_type = pten::TransToPtenDataType(
            static_cast<framework::proto::VarType::Type>(
                BOOST_GET_CONST(int, attr)));
        kernel_ctx->EmplaceBackAttr(data_type);
      } else if (attr_defs[i].type_index ==
                 std::type_index(typeid(std::vector<int64_t>))) {
        if (std::type_index(attr.type()) ==
            std::type_index(typeid(std::vector<int>))) {
          // Emplace Back Attr according to the type of Pten_Kernel args.
          const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr);
          const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(),
                                                       vector_int_attr.end());
          kernel_ctx->EmplaceBackAttr(vector_int64_attr);
        }
        // TODO(YuanRisheng) Need support vector<int64_t> attr
488 489
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
490
            "Unsupported cast op attribute `%s` when construct "
491 492 493 494 495 496 497
            "KernelContext in dygraph.",
            attr_names[i]));
      }
    }
  }
}

498 499 500
template <typename VarType>
static void PreparedOpRunImpl(
    const framework::OperatorBase& op, const framework::RuntimeContext& ctx,
501
    const framework::OpKernelType& kernel_type,
502
    const framework::OperatorWithKernel::OpKernelFunc& func,
503
    platform::DeviceContext* dev_ctx, const NameVarMap<VarType>& ins,
504 505
    const NameVarMap<VarType>& outs, const framework::AttributeMap& attrs,
    const framework::AttributeMap& default_attrs) {
J
Jiabin Yang 已提交
506 507
  // TODO(zjl): remove scope in dygraph
  framework::Scope scope;
H
hong 已提交
508

509 510 511 512 513 514 515 516 517 518 519
  {
    platform::RecordEvent record_event(op.Type() + " infer_shape",
                                       platform::EventRole::kInnerOp);
    DygraphInferShapeContext<VarType> infer_shape_ctx(
        &ins, &outs, &attrs, &default_attrs, op.Type(), &kernel_type);
    op.Info().infer_shape_(&infer_shape_ctx);
  }

  {
    platform::RecordEvent record_event(op.Type() + " compute",
                                       platform::EventRole::kInnerOp);
H
hong 已提交
520

521 522 523
    func(DygraphExecutionContext<VarType>(op, scope, *dev_ctx, ctx, ins, outs,
                                          attrs, default_attrs));
  }
524

525 526 527 528 529
  if (FLAGS_check_nan_inf) {
    framework::details::CheckOpHasNanOrInfInDygraph<VarType>(
        op.Type(), outs, dev_ctx->GetPlace());
  }

L
Leo Chen 已提交
530 531 532 533 534 535 536 537
  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
  }

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  /**
   * [ 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);
  }
553
}
H
hong 已提交
554

555 556 557
template <typename VarType>
static void PreparedOpRunPtImpl(
    const framework::OperatorBase& op,
558
    const framework::OpKernelType& kernel_type,
559
    const framework::KernelSignature& pt_kernel_signature,
560 561 562
    const pten::Kernel& pt_kernel, platform::DeviceContext* dev_ctx,
    const NameVarMap<VarType>& ins, const NameVarMap<VarType>& outs,
    const framework::AttributeMap& attrs,
563
    const framework::AttributeMap& default_attrs) {
564 565 566 567 568 569 570 571 572 573 574
  {
    platform::RecordEvent record_event(op.Type() + " infer_shape",
                                       platform::EventRole::kInnerOp);
    DygraphInferShapeContext<VarType> infer_shape_ctx(
        &ins, &outs, &attrs, &default_attrs, op.Type(), &kernel_type);
    op.Info().infer_shape_(&infer_shape_ctx);
  }

  {
    platform::RecordEvent record_event(op.Type() + " compute",
                                       platform::EventRole::kInnerOp);
575

576
    PreparePtenData<VarType>(pt_kernel, pt_kernel_signature, ins);
577

578 579 580 581
    pten::KernelContext pt_kernel_context;
    BuildDygraphPtenKernelContext<VarType>(pt_kernel_signature, pt_kernel, ins,
                                           outs, attrs, default_attrs, dev_ctx,
                                           &pt_kernel_context);
582

583 584
    pt_kernel(&pt_kernel_context);
  }
585

586 587
  if (FLAGS_benchmark) {
    dev_ctx->Wait();
588 589
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    PADDLE_ENFORCE_GPU_SUCCESS(platform::GpuGetLastError());
590 591 592 593
    VLOG(4) << "Operator(" << op.Type() << "): context wait and get last error";
#endif
  }

594
  // TODO(chenweihang): add debug flags later
595 596 597
  if (framework::IsComplexType(kernel_type.data_type_)) {
    HandleComplexGradToRealGrad<VarType>(outs);
  }
598 599
}

600 601
void PreparedOp::Run(const NameVarMap<VarBase>& ins,
                     const NameVarMap<VarBase>& outs,
602 603
                     const framework::AttributeMap& attrs,
                     const framework::AttributeMap& default_attrs) {
604
  if (run_pten_kernel_) {
605
    PreparedOpRunPtImpl<VarBase>(op_, kernel_type_, pt_kernel_signature_,
606 607
                                 pt_kernel_, dev_ctx_, ins, outs, attrs,
                                 default_attrs);
608 609 610 611
  } else {
    PreparedOpRunImpl<VarBase>(op_, ctx_, kernel_type_, func_, dev_ctx_, ins,
                               outs, attrs, default_attrs);
  }
612
}
H
hong 已提交
613

614 615
void PreparedOp::Run(const NameVarMap<VariableWrapper>& ins,
                     const NameVarMap<VariableWrapper>& outs,
616 617
                     const framework::AttributeMap& attrs,
                     const framework::AttributeMap& default_attrs) {
618
  if (run_pten_kernel_) {
619
    PreparedOpRunPtImpl<VariableWrapper>(
620 621
        op_, kernel_type_, pt_kernel_signature_, pt_kernel_, dev_ctx_, ins,
        outs, attrs, default_attrs);
622 623 624 625
  } else {
    PreparedOpRunImpl<VariableWrapper>(op_, ctx_, kernel_type_, func_, dev_ctx_,
                                       ins, outs, attrs, default_attrs);
  }
J
Jiabin Yang 已提交
626 627 628 629
}

}  // namespace imperative
}  // namespace paddle