phi_utils.cc 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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 <sstream>

17
#include "paddle/fluid/framework/convert_utils.h"
18
#include "paddle/fluid/framework/phi_utils.h"
19 20

#include "paddle/fluid/framework/lod_tensor.h"
Z
Zeng Jinle 已提交
21
#include "paddle/fluid/framework/op_info.h"
22
#include "paddle/fluid/framework/selected_rows_utils.h"
23 24
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/string/string_helper.h"
25 26 27
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/compat/op_utils.h"
#include "paddle/phi/core/kernel_factory.h"
28 29 30 31

namespace paddle {
namespace framework {

Z
Zeng Jinle 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
class KernelArgsNameMakerByOpProto : public KernelArgsNameMaker {
 public:
  explicit KernelArgsNameMakerByOpProto(
      const framework::proto::OpProto* op_proto)
      : op_proto_(op_proto) {
    PADDLE_ENFORCE_NOT_NULL(op_proto_, platform::errors::InvalidArgument(
                                           "Op proto cannot be nullptr."));
  }

  ~KernelArgsNameMakerByOpProto() {}

  const paddle::SmallVector<std::string>& GetInputArgsNames() override;
  const paddle::SmallVector<std::string>& GetOutputArgsNames() override;
  const paddle::SmallVector<std::string>& GetAttrsArgsNames() override;

  KernelSignature GetKernelSignature();

 private:
  DISABLE_COPY_AND_ASSIGN(KernelArgsNameMakerByOpProto);

 private:
  const framework::proto::OpProto* op_proto_;

  paddle::SmallVector<std::string> input_names_;
  paddle::SmallVector<std::string> output_names_;
  paddle::SmallVector<std::string> attr_names_;
};

60
OpKernelType TransPhiKernelKeyToOpKernelType(const phi::KernelKey& kernel_key) {
61
  proto::VarType::Type data_type =
62
      paddle::framework::TransToProtoVarType(kernel_key.dtype());
63
  // no need to set current device id here
64
  platform::Place place = phi::TransToPhiPlace(kernel_key.backend(), false);
65
  DataLayout data_layout = kernel_key.layout();
66
  LibraryType library_type = LibraryType::kPlain;
67
  if (kernel_key.backend() == phi::Backend::MKLDNN) {
68
    library_type = LibraryType::kMKLDNN;
69
  } else if (kernel_key.backend() == phi::Backend::GPUDNN) {
70
    library_type = LibraryType::kCUDNN;
71 72
  } else if (kernel_key.backend() == phi::Backend::KPS) {
    library_type = LibraryType::kKP;
73 74 75 76 77 78 79
  } else {
    // do nothing
  }
  // TODO(chenweihang): the customized_type_value is lost
  return OpKernelType(data_type, place, data_layout, library_type);
}

80
phi::KernelKey TransOpKernelTypeToPhiKernelKey(
81
    const OpKernelType& kernel_type) {
82
  phi::Backend backend = phi::TransToPhiBackend(kernel_type.place_);
83
  if (kernel_type.library_type_ == LibraryType::kMKLDNN) {
84
    backend = phi::Backend::MKLDNN;
85
  } else if (kernel_type.library_type_ == LibraryType::kCUDNN) {
86
    backend = phi::Backend::GPUDNN;
87 88
  } else if (kernel_type.library_type_ == LibraryType::kKP) {
    backend = phi::Backend::KPS;
89
  } else {
90
    // do nothing
91
  }
92
  paddle::experimental::DataLayout layout = kernel_type.data_layout_;
93
  paddle::experimental::DataType dtype =
94
      paddle::framework::TransToPhiDataType(kernel_type.data_type_);
95
  return phi::KernelKey(backend, layout, dtype);
96 97
}

98 99 100
phi::KernelKey FallBackToCpu(const OpKernelType& expected_kernel_key,
                             const phi::KernelKey& kernel_key,
                             const framework::OperatorBase& op) {
101 102 103
#ifdef PADDLE_WITH_XPU
  if (platform::is_xpu_place(expected_kernel_key.place_) ||
      paddle::platform::is_in_xpu_black_list(op.Type())) {
104
    VLOG(3) << "phi missing XPU kernel: " << op.Type()
105
            << ", phipected_kernel_key:" << expected_kernel_key
106
            << ", fallbacking to CPU one!";
107 108
    return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
                          kernel_key.dtype());
109 110 111 112
  }
#endif
#ifdef PADDLE_WITH_ASCEND_CL
  if (platform::is_npu_place(expected_kernel_key.place_)) {
113
    VLOG(3) << "phi missing NPU kernel: " << op.Type()
114
            << ", phipected_kernel_key:" << expected_kernel_key
115
            << ", fallbacking to CPU one!";
116 117
    return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
                          kernel_key.dtype());
118 119 120 121
  }
#endif
#ifdef PADDLE_WITH_MLU
  if (platform::is_mlu_place(expected_kernel_key.place_)) {
122
    VLOG(3) << "phi missing MLU kernel: " << op.Type()
123
            << ", phipected_kernel_key:" << expected_kernel_key
124
            << ", fallbacking to CPU one!";
125 126 127 128 129 130
    return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
                          kernel_key.dtype());
  }
#endif
#ifdef PADDLE_WITH_IPU
  if (platform::is_ipu_place(expected_kernel_key.place_)) {
131 132 133 134 135 136 137 138 139 140 141 142
    VLOG(3) << "phi missing IPU kernel: " << op.Type()
            << ", phipected_kernel_key:" << expected_kernel_key
            << ", fallbacking to CPU one!";
    return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
                          kernel_key.dtype());
  }
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
  if (platform::is_custom_place(expected_kernel_key.place_)) {
    VLOG(3) << "phi missing " << expected_kernel_key.place_.GetDeviceType()
            << " kernel: " << op.Type()
            << ", phipected_kernel_key:" << expected_kernel_key
143
            << ", fallbacking to CPU one!";
144 145
    return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
                          kernel_key.dtype());
146 147
  }
#endif
148
  return phi::KernelKey();
149 150
}

151 152 153 154 155 156
const paddle::SmallVector<std::string>&
KernelArgsNameMakerByOpProto::GetInputArgsNames() {
  for (int i = 0; i < op_proto_->inputs_size(); ++i) {
    auto& in = op_proto_->inputs()[i];
    auto& in_name = in.name();
    if ((in.has_extra() && in.extra()) || (in.has_quant() && in.quant())) {
157
      VLOG(6) << "Parse PhiKernel input: skip extra & quant input - "
158 159 160 161
              << in_name;
      continue;
    }
    // If contains dispensable input, we should override the
162
    // OpArgumentMapping method self in phi/ops/compat dir
163
    if (in.has_dispensable() && in.dispensable()) {
164
      VLOG(6) << "Parse PhiKernel input: skip dispensable input - " << in_name;
165 166
      continue;
    }
167
    VLOG(6) << "Parse PhiKernel input: " << in_name;
168 169 170 171 172 173 174 175 176 177
    input_names_.emplace_back(in_name);
  }
  return input_names_;
}

const paddle::SmallVector<std::string>&
KernelArgsNameMakerByOpProto::GetOutputArgsNames() {
  for (int i = 0; i < op_proto_->outputs_size(); ++i) {
    auto& out = op_proto_->outputs()[i];
    auto& out_name = out.name();
178
    if ((out.has_extra() && out.extra()) || (out.has_quant() && out.quant())) {
179
      VLOG(6) << "Parse PhiKernel output: skip extra & quant output - "
180 181 182
              << out_name;
      continue;
    }
183
    VLOG(6) << "Parse PhiKernel output: " << out_name;
184 185 186 187 188 189 190 191 192 193
    output_names_.emplace_back(out_name);
  }
  return output_names_;
}

const paddle::SmallVector<std::string>&
KernelArgsNameMakerByOpProto::GetAttrsArgsNames() {
  for (int i = 0; i < op_proto_->attrs_size(); ++i) {
    auto& attr = op_proto_->attrs()[i];
    auto& attr_name = attr.name();
194 195 196 197
    if (attr_name == "use_mkldnn" || attr_name == "use_cudnn" ||
        attr_name == "op_role" || attr_name == "op_role_var" ||
        attr_name == "op_namescope" || attr_name == "op_callstack" ||
        attr_name == "op_device") {
198
      VLOG(6) << "Parse PhiKernel attribute: skip needless attr - "
199 200 201 202 203
              << attr_name;
      continue;
    }
    if ((attr.has_extra() && attr.extra()) ||
        (attr.has_quant() && attr.quant())) {
204
      VLOG(6) << "Parse PhiKernel attribute: skip extra & quant attr - "
205 206 207
              << attr_name;
      continue;
    }
208
    VLOG(6) << "Parse PhiKernel attribute: " << attr_name;
209 210 211 212 213 214 215
    attr_names_.emplace_back(attr_name);
  }

  return attr_names_;
}

KernelSignature KernelArgsNameMakerByOpProto::GetKernelSignature() {
216
  return KernelSignature(phi::TransToPhiKernelName(op_proto_->type()),
217 218
                         GetInputArgsNames(), GetAttrsArgsNames(),
                         GetOutputArgsNames());
219 220
}

221 222 223 224 225 226 227
std::once_flag kernel_sig_map_init_flag;

void InitDefaultKernelSignatureMap() {
  std::call_once(kernel_sig_map_init_flag, [] {
    for (const auto& pair : paddle::framework::OpInfoMap::Instance().map()) {
      const auto& op_type = pair.first;
      const auto* op_proto = pair.second.proto_;
228
      if (phi::KernelFactory::Instance().HasCompatiblePhiKernel(op_type) &&
229 230 231
          op_proto) {
        paddle::framework::KernelArgsNameMakerByOpProto maker(op_proto);
        VLOG(10) << "Register kernel signature for " << op_type;
232
        phi::DefaultKernelSignatureMap::Instance().Insert(
233 234 235 236 237 238
            op_type, std::move(maker.GetKernelSignature()));
      }
    }
  });
}

239
static void SetAllocationForUninitializedDenseTensor(
240
    phi::DenseTensor* dense_tensor, const platform::Place& place) {
241 242 243 244 245 246 247 248 249
  int dtype_size = dense_tensor->dtype() == DataType::UNDEFINED
                       ? 0
                       : experimental::SizeOf(dense_tensor->dtype());
  int64_t numels = product(dense_tensor->dims());
  numels = numels < 0 ? 0 : numels;
  auto tmp_allocation_ptr = memory::Alloc(place, numels * dtype_size);
  auto& deleter = tmp_allocation_ptr.get_deleter();
  auto* allocation_ptr = tmp_allocation_ptr.release();
  auto shared_allocation =
250
      std::shared_ptr<phi::Allocation>(allocation_ptr, deleter);
251 252 253 254

  dense_tensor->ResetHolder(shared_allocation);
}

255 256
}  // namespace framework
}  // namespace paddle