op_runner.cc 7.6 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
// 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/eager/legacy/op_runner.h"
#include <map>
#include <set>
#include <unordered_set>
#include <utility>
#include "paddle/fluid/eager/legacy/amp_auto_cast.h"
#include "paddle/fluid/eager/legacy/infer_var_type_context.h"
#include "paddle/fluid/eager/legacy/prepared_operator.h"
#include "paddle/fluid/eager/legacy/tensor_helper.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/denormal.h"
#include "paddle/fluid/string/string_helper.h"

DECLARE_bool(use_mkldnn);
DECLARE_string(tracer_mkldnn_ops_on);
DECLARE_string(tracer_mkldnn_ops_off);

namespace egr {
33
namespace legacy {
34 35 36 37 38 39 40 41 42 43 44 45 46

void OpRunImpl(const paddle::framework::OperatorBase& op,
               const NameTensorMap& ins, const NameTensorMap& outs,
               const paddle::framework::AttributeMap& attrs,
               const paddle::framework::AttributeMap& default_attrs,
               const paddle::platform::Place& place) {
  auto* op_kernel =
      dynamic_cast<const paddle::framework::OperatorWithKernel*>(&op);
  PADDLE_ENFORCE_NOT_NULL(
      op_kernel, paddle::platform::errors::PermissionDenied(
                     "Only support operator with kernel in Dygraph mode."));
  auto& info = op.Info();
  if (info.infer_var_type_) {
47 48
    egr::legacy::TensorRuntimeInferVarTypeContext infer_var_type_ctx(
        ins, outs, attrs, default_attrs);
49 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 78 79
    info.infer_var_type_(&infer_var_type_ctx);
  }

  // Initialize output tensor
  for (auto& tensor_pair : outs) {
    for (auto& tensor : tensor_pair.second) {
      if (tensor && tensor.get() && (!tensor->Var().IsInitialized())) {
        InitializeVariable(tensor->MutableVar(),
                           paddle::framework::proto::VarType::LOD_TENSOR);
      }
    }
  }

  /**
   * [ Why need temporary inputs here? ]
   *
   * PrepareData should not change original input tensor inplace.
   * Suppose the user defines a tensor(int), enters an op to execute,
   * and then this op rewrites GetExpectedKernelForVar, and converts
   * this tensor to float type during execution. After the dynamic
   * graph is executed, the user-defined variable will be lost, and
   * the user cannot get the originally defined int tensor, because
   * it has been converted to float, this should be regarded as a bug
   * in certain usage scenarios
   *
   * In static graph mode, when op is executed, a temporary scope
   * `transfer_scope` is created before PrepareData, the data after
   * transform is stored in the temporary scope, and then discarded
   * after the execution of op, but the original input is directly
   * overwritten in the previous dynamic graph implemention.
   */
80 81
  auto prepared_op = egr::legacy::PreparedOp::Prepare(
      ins, outs, *op_kernel, place, attrs, default_attrs);
82
  auto tmp_ins_ptr =
83
      egr::legacy::PrepareData(*op_kernel, ins, prepared_op.kernel_type());
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 152 153 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 179 180 181 182 183 184 185 186 187 188 189 190 191
  if (tmp_ins_ptr == nullptr) {
    prepared_op.Run(ins, outs, attrs, default_attrs);
  } else {
    prepared_op.Run(*tmp_ins_ptr, outs, attrs, default_attrs);
  }

  // TODO(jiabin): Set the output var's grad Forward DataType
}

void RunOp(const std::string& type, const NameTensorMap& ins,
           const NameTensorMap& outs, paddle::framework::AttributeMap attrs,
           const paddle::platform::Place& place,
           paddle::framework::AttributeMap* default_attrs,
           bool override_default_attr_map,
           const std::map<std::string, std::string>& inplace_map) {
  VLOG(1) << "Run Op: " << type;
  if (FLAGS_use_mkldnn) {
    // if both lists are empty all ops are enabled (default for
    // FLAGS_use_mkldnn=1)
    // if ops_on list is not empty only ops from that list are enabled
    if (!FLAGS_tracer_mkldnn_ops_on.empty()) {
      auto is_on = FLAGS_tracer_mkldnn_ops_on.find(type) != std::string::npos;
      attrs["use_mkldnn"] = is_on;
    } else {
      // if ops_on list is empty all ops are enabled except types from off_list
      auto is_off = FLAGS_tracer_mkldnn_ops_off.find(type) != std::string::npos;
      attrs["use_mkldnn"] = !is_off;
    }
  }
  auto op = paddle::framework::OpRegistry::CreateOp(type, {}, {}, {}, false);

  PADDLE_ENFORCE_NOT_NULL(default_attrs,
                          paddle::platform::errors::PermissionDenied(
                              "Detected default_attrs = nullptr."));

  if (override_default_attr_map) {
    const auto& op_info = op->Info();
    auto* attr_checker = op_info.Checker();
    if (attr_checker) {
      attr_checker->Check(&attrs, true, /*only_check_exist_value=*/true);
    }

    static paddle::framework::AttributeMap empty_attrs_map = {};
    *default_attrs = attr_checker == nullptr
                         ? empty_attrs_map
                         : attr_checker->GetDefaultAttrMap();
  }

  auto amp_level = egr::Controller::Instance().GetAMPLevel();
  NameTensorMap new_ins = ins;
  if (amp_level == 1) {
    VLOG(5) << "Auto mixed precision run operator: " << type;
    new_ins = AutoCastInputs(type, ins);
  } else if (amp_level == 2) {
    VLOG(5) << "Pure fp16 run operator: " << type;
    new_ins = CastPureFp16Inputs(type, ins);
  }

  try {
    if (paddle::platform::is_gpu_place(place)) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
      paddle::platform::SetDeviceId(
          BOOST_GET_CONST(paddle::platform::CUDAPlace, place).device);
#else
      PADDLE_THROW(paddle::platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with GPU if use CUDAPlace."));
#endif
    } else if (paddle::platform::is_xpu_place(place)) {
#ifdef PADDLE_WITH_XPU
      paddle::platform::SetXPUDeviceId(
          BOOST_GET_CONST(paddle::platform::XPUPlace, place).device);
#else
      PADDLE_THROW(paddle::platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with XPU if use XPUPlace."));
#endif
    } else if (paddle::platform::is_npu_place(place)) {
#ifdef PADDLE_WITH_ASCEND_CL
      paddle::platform::SetNPUDeviceId(
          BOOST_GET_CONST(paddle::platform::NPUPlace, place).device);
#else
      PADDLE_THROW(paddle::platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with NPU if use NPUPlace."));
#endif
    }

    OpRunImpl(*op, new_ins, outs, attrs, *default_attrs, place);
  } catch (paddle::platform::EnforceNotMet& exception) {
    paddle::framework::AppendErrorOpHint(type, &exception);
    throw std::move(exception);
  } catch (std::exception& ex) {
    PADDLE_THROW(paddle::platform::errors::Fatal(
        "Operator %s raises an %s exception.\n"
        "The exception content is\n:%s.",
        type, paddle::platform::demangle(typeid(ex).name()), ex.what()));
  } catch (...) {
    // NOTE: this branch represents a very serious bug with
    // low probability of occurrence, and we can't get its
    // exception content here.
    PADDLE_THROW(paddle::platform::errors::Fatal(
        "Operator %s raises an unknown exception.", type));
  }

  // TODO(jiabin): Support this later
  // if (enable_program_desc_tracing_) {
  //   VLOG(5) << "Trace op " << type << " into ProgramDesc";
  //   program_desc_tracer_->InsertOp(type, new_ins, outs, attrs);
  // }
}
192 193

}  // namespace legacy
194
}  // namespace egr