ascend_wrapper_py.cc 35.8 KB
Newer Older
H
hutuxian 已提交
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
/* 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. */

#ifdef PADDLE_WITH_ASCEND
#include <fcntl.h>

#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif

#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif

#include <ge/ge_api.h>
#include <graph/attr_value.h>
#include <graph/operator_factory.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/fleet/ascend_wrapper.h"
G
gongweibao 已提交
35
#include "paddle/fluid/platform/ascend_npu_info.h"
36
#include "paddle/fluid/platform/enforce.h"
37
#include "paddle/fluid/pybind/ascend_wrapper_py.h"
H
hutuxian 已提交
38 39 40 41 42 43 44

using namespace ge;  // NOLINT
namespace py = pybind11;

namespace paddle {
namespace pybind {

45 46 47 48 49 50
#ifdef PADDLE_WITH_ASCEND_STRING
using AscendString = AscendString;
#else
using AscendString = std::string;
#endif

H
hutuxian 已提交
51 52 53 54 55 56 57 58 59
void BindAscendWrapper(py::module *m) {
  py::class_<framework::AscendInstance,
             std::shared_ptr<framework::AscendInstance>>(*m, "AscendInstance")
      .def(py::init([]() { return framework::AscendInstance::GetInstance(); }))
      .def("init_global_resources",
           &framework::AscendInstance::InitGlobalResouces,
           py::call_guard<py::gil_scoped_release>())
      .def("add_ascend_subgraph", &framework::AscendInstance::AddAscendSubgraph,
           py::call_guard<py::gil_scoped_release>());
60
}
H
hutuxian 已提交
61

62 63 64
std::map<AscendString, AscendString> convert_map(
    const std::map<std::string, std::string> &options) {
  std::map<AscendString, AscendString> rets;
65
  for (auto &option : options) {
66 67
    AscendString key = option.first.c_str();
    AscendString val = option.second.c_str();
68 69 70 71 72
    rets[key] = val;
  }
  return rets;
}

73 74
ge::Status ge_initialize(
    std::map<std::string, std::string> &options) {  // NOLINT
H
hutuxian 已提交
75
  py::gil_scoped_release release;
76
  auto init_options = convert_map(options);
77
  ge::Status res = ge::GEInitialize(init_options);
78 79
  PADDLE_ENFORCE_EQ(res, ge::SUCCESS,
                    platform::errors::Fatal("ge init error:%d", res));
H
hutuxian 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  py::gil_scoped_acquire acquire;
  return res;
}

enum AttrType {
  AT_INT64 = 0,
  AT_INT32,
  AT_UINT32,
  AT_LIST_INT64,
  AT_LIST_INT32,
  AT_LIST_UINT32,
  AT_FLOAT,
  AT_LIST_FLOAT,
  AT_ATTR_VALUE,
  AT_STRING,
  AT_LIST_STRING,
  AT_BOOL,
  AT_LIST_BOOL,
  AT_TENSOR,
  AT_LIST_TENSOR,
  AT_LIST_UINT8,
  AT_LIST_LIST_INT64,
  AT_LIST_DT,
  AT_DT,
  AT_LIST_NAMEATTR,
  AT_NAMEATTR
};

108 109 110 111 112
void BindAscendDevice(py::module *m) {
  py::class_<platform::ascend::NPUDevice>(*m, "NPUDevice")
      .def_static(
          "get_device_count",
          static_cast<int (*)()>(&platform::ascend::NPUDevice::GetDeviceCount));
G
gongweibao 已提交
113 114
}

H
hutuxian 已提交
115 116 117 118
void BindAscendGraph(py::module *m) {
  m->def("ge_initialize", &ge_initialize, "GEInitialize");
  m->def("ge_finalize", &GEFinalize, "GEFinalize");

119
  // enum
H
hutuxian 已提交
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 243 244 245 246
  py::enum_<GraphRunMode>(*m, "GEGraphRunMode")
      .value("PREDICTION", GraphRunMode::PREDICTION)
      .value("TRAIN", GraphRunMode::TRAIN)
      .export_values();

  py::enum_<DataType>(*m, "GEDataType")
      .value("DT_FLOAT", DataType::DT_FLOAT)
      .value("DT_FLOAT16", DataType::DT_FLOAT16)
      .value("DT_INT8", DataType::DT_INT8)
      .value("DT_INT16", DataType::DT_INT16)
      .value("DT_UINT16", DataType::DT_UINT16)
      .value("DT_UINT8", DataType::DT_UINT8)
      .value("DT_INT32", DataType::DT_INT32)
      .value("DT_INT64", DataType::DT_INT64)
      .value("DT_UINT32", DataType::DT_UINT32)
      .value("DT_UINT64", DataType::DT_UINT64)
      .value("DT_BOOL", DataType::DT_BOOL)
      .value("DT_DOUBLE", DataType::DT_DOUBLE)
      .value("DT_STRING", DataType::DT_STRING)
      .value("DT_DUAL_SUB_INT8", DataType::DT_DUAL_SUB_INT8)
      .value("DT_DUAL_SUB_UINT8", DataType::DT_DUAL_SUB_UINT8)
      .value("DT_COMPLEX64", DataType::DT_COMPLEX64)
      .value("DT_COMPLEX128", DataType::DT_COMPLEX128)
      .value("DT_QINT8", DataType::DT_QINT8)
      .value("DT_QINT16", DataType::DT_QINT16)
      .value("DT_QINT32", DataType::DT_QINT32)
      .value("DT_QUINT8", DataType::DT_QUINT8)
      .value("DT_QUINT16", DataType::DT_QUINT16)
      .value("DT_RESOURCE", DataType::DT_RESOURCE)
      .value("DT_STRING_REF", DataType::DT_STRING_REF)
      .value("DT_DUAL", DataType::DT_DUAL)
      .value("DT_UNDEFINED", DataType::DT_UNDEFINED)
      .export_values();

  py::enum_<Format>(*m, "GEFormat")
      .value("FORMAT_NCHW", Format::FORMAT_NCHW)
      .value("FORMAT_NHWC", Format::FORMAT_NHWC)
      .value("FORMAT_ND", Format::FORMAT_ND)
      .value("FORMAT_NC1HWC0", Format::FORMAT_NC1HWC0)
      .value("FORMAT_FRACTAL_Z", Format::FORMAT_FRACTAL_Z)
      .value("FORMAT_NC1C0HWPAD", Format::FORMAT_NC1C0HWPAD)
      .value("FORMAT_NHWC1C0", Format::FORMAT_NHWC1C0)
      .value("FORMAT_FSR_NCHW", Format::FORMAT_FSR_NCHW)
      .value("FORMAT_FRACTAL_DECONV", Format::FORMAT_FRACTAL_DECONV)
      .value("FORMAT_C1HWNC0", Format::FORMAT_C1HWNC0)
      .value("FORMAT_FRACTAL_DECONV_TRANSPOSE",
             Format::FORMAT_FRACTAL_DECONV_TRANSPOSE)
      .value("FORMAT_FRACTAL_DECONV_SP_STRIDE_TRANS",
             Format::FORMAT_FRACTAL_DECONV_SP_STRIDE_TRANS)
      .value("FORMAT_NC1HWC0_C04", Format::FORMAT_NC1HWC0_C04)
      .value("FORMAT_FRACTAL_Z_C04", Format::FORMAT_FRACTAL_Z_C04)
      .value("FORMAT_CHWN", Format::FORMAT_CHWN)
      .value("FORMAT_FRACTAL_DECONV_SP_STRIDE8_TRANS",
             Format::FORMAT_FRACTAL_DECONV_SP_STRIDE8_TRANS)
      .value("FORMAT_HWCN", Format::FORMAT_HWCN)
      .value("FORMAT_NC1KHKWHWC0", Format::FORMAT_NC1KHKWHWC0)
      .value("FORMAT_BN_WEIGHT", Format::FORMAT_BN_WEIGHT)
      .value("FORMAT_FILTER_HWCK", Format::FORMAT_FILTER_HWCK)
      .value("FORMAT_HASHTABLE_LOOKUP_LOOKUPS",
             Format::FORMAT_HASHTABLE_LOOKUP_LOOKUPS)
      .value("FORMAT_HASHTABLE_LOOKUP_KEYS",
             Format::FORMAT_HASHTABLE_LOOKUP_KEYS)
      .value("FORMAT_HASHTABLE_LOOKUP_VALUE",
             Format::FORMAT_HASHTABLE_LOOKUP_VALUE)
      .value("FORMAT_HASHTABLE_LOOKUP_OUTPUT",
             Format::FORMAT_HASHTABLE_LOOKUP_OUTPUT)
      .value("FORMAT_HASHTABLE_LOOKUP_HITS",
             Format::FORMAT_HASHTABLE_LOOKUP_HITS)
      .value("FORMAT_C1HWNCoC0", Format::FORMAT_C1HWNCoC0)
      .value("FORMAT_MD", Format::FORMAT_MD)
      .value("FORMAT_NDHWC", Format::FORMAT_NDHWC)
      .value("FORMAT_FRACTAL_ZZ", Format::FORMAT_FRACTAL_ZZ)
      .value("FORMAT_FRACTAL_NZ", Format::FORMAT_FRACTAL_NZ)
      .value("FORMAT_NCDHW", Format::FORMAT_NCDHW)
      .value("FORMAT_DHWCN", Format::FORMAT_DHWCN)
      .value("FORMAT_NDC1HWC0", Format::FORMAT_NDC1HWC0)
      .value("FORMAT_FRACTAL_Z_3D", Format::FORMAT_FRACTAL_Z_3D)
      .value("FORMAT_CN", Format::FORMAT_CN)
      .value("FORMAT_NC", Format::FORMAT_NC)
      .value("FORMAT_DHWNC", Format::FORMAT_DHWNC)
      .value("FORMAT_FRACTAL_Z_3D_TRANSPOSE",
             Format::FORMAT_FRACTAL_Z_3D_TRANSPOSE)
      .value("FORMAT_FRACTAL_ZN_LSTM", Format::FORMAT_FRACTAL_ZN_LSTM)
      .value("FORMAT_FRACTAL_Z_G", Format::FORMAT_FRACTAL_Z_G)
      .value("FORMAT_RESERVED", Format::FORMAT_RESERVED)
      .value("FORMAT_ALL", Format::FORMAT_ALL)
      .value("FORMAT_NULL", Format::FORMAT_NULL)
      .export_values();

  py::enum_<UnknowShapeOpType>(*m, "GEUnknowShapeOpType")
      .value("DEPEND_IN_SHAPE", UnknowShapeOpType::DEPEND_IN_SHAPE)
      .value("DEPEND_CONST_VALUE", UnknowShapeOpType::DEPEND_CONST_VALUE)
      .value("DEPEND_SHAPE_RANGE", UnknowShapeOpType::DEPEND_SHAPE_RANGE)
      .value("DEPEND_COMPUTE", UnknowShapeOpType::DEPEND_COMPUTE)
      .export_values();

  py::enum_<DeviceType>(*m, "GEDeviceType")
      .value("NPU", DeviceType::NPU)
      .value("CPU", DeviceType::CPU)
      .export_values();

  py::enum_<AttrType>(*m, "GEAttrType")
      .value("AT_INT64", AttrType::AT_INT64)
      .value("AT_INT32", AttrType::AT_INT32)
      .value("AT_UINT32", AttrType::AT_UINT32)
      .value("AT_LIST_INT64", AttrType::AT_LIST_INT64)
      .value("AT_LIST_INT32", AttrType::AT_LIST_INT32)
      .value("AT_LIST_UINT32", AttrType::AT_LIST_UINT32)
      .value("AT_FLOAT", AttrType::AT_FLOAT)
      .value("AT_LIST_FLOAT", AttrType::AT_LIST_FLOAT)
      .value("AT_ATTR_VALUE", AttrType::AT_ATTR_VALUE)
      .value("AT_STRING", AttrType::AT_STRING)
      .value("AT_LIST_STRING", AttrType::AT_LIST_STRING)
      .value("AT_BOOL", AttrType::AT_BOOL)
      .value("AT_LIST_BOOL", AttrType::AT_LIST_BOOL)
      .value("AT_TENSOR", AttrType::AT_TENSOR)
      .value("AT_LIST_TENSOR", AttrType::AT_LIST_TENSOR)
      .value("AT_LIST_UINT8", AttrType::AT_LIST_UINT8)
      .value("AT_LIST_LIST_INT64", AttrType::AT_LIST_LIST_INT64)
      .value("AT_LIST_DT", AttrType::AT_LIST_DT)
      .value("AT_DT", AttrType::AT_DT)
      .value("AT_LIST_NAMEATTR", AttrType::AT_LIST_NAMEATTR)
      .value("AT_NAMEATTR", AttrType::AT_NAMEATTR)
      .export_values();

  // 类封装
  py::class_<Session>(*m, "GESession")
247 248 249 250 251 252
      .def(py::init([](const std::map<std::string, std::string> &options) {
        return std::unique_ptr<ge::Session>(
            new ge::Session(convert_map(options)));
      }))
      .def("add_graph", (ge::Status (Session::*)(uint32_t, const Graph &)) &
                            Session::AddGraph)
H
hutuxian 已提交
253
      .def("add_graph",
254 255 256 257
           [](Session &ss, uint32_t index, const Graph &graph,
              const std::map<std::string, std::string> &options) {
             return ss.AddGraph(index, graph, convert_map(options));
           })
H
hutuxian 已提交
258 259 260 261 262
      .def("remove_graph", &Session::RemoveGraph)
      .def("run_graph",
           [](Session &ss, uint32_t graphId,
              const std::vector<Tensor> &inputs) -> py::tuple {
             std::vector<Tensor> outputs;
263
             ge::Status res = ss.RunGraph(graphId, inputs, outputs);
H
hutuxian 已提交
264 265 266 267 268
             return py::make_tuple(outputs, res);
           },
           py::call_guard<py::gil_scoped_release>())
      .def("build_graph", &Session::BuildGraph)
      .def("run_graph_async", &Session::RunGraphAsync)
269 270 271 272 273 274 275 276 277 278 279 280 281 282
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("register_call_back_func",
           static_cast<ge::Status (ge::Session::*)(  // NOLINT
               const char *, const ge::Session::pCallBackFunc &)>(
               &ge::Session::RegisterCallBackFunc))
#else
      .def("register_call_back_func",
           (Status (Session::*)(  // NOLINT
               const std::string &,
               std::function<uint32_t(
                   uint32_t graph_id,
                   const std::map<std::string, ge::Tensor> &params_list)>)) &
               Session::RegisterCallBackFunc)
#endif
H
hutuxian 已提交
283 284 285 286
      .def("is_graph_need_rebuild", &Session::IsGraphNeedRebuild);

  py::class_<Graph>(*m, "GEGraph")
      .def(py::init<>())
287
      .def(py::init<const char *>())
H
hutuxian 已提交
288 289 290 291 292 293 294 295 296
      .def("set_inputs", &Graph::SetInputs)
      .def("set_outputs", (Graph & (Graph::*)(const std::vector<Operator> &)) &
                              Graph::SetOutputs)
      .def("set_outputs",
           (Graph & (Graph::*)(const std::vector<
                               std::pair<Operator, std::vector<size_t>>> &)) &
               Graph::SetOutputs)
      .def("set_outputs",
           (Graph &
297
            (Graph::*)(const std::vector<std::pair<ge::Operator, AscendString>>
H
hutuxian 已提交
298 299 300 301 302 303
                           &)) &
               Graph::SetOutputs)
      .def("set_targets", &Graph::SetTargets)
      .def("is_valid", &Graph::IsValid)
      .def("add_op", &Graph::AddOp)
      .def("find_op_by_name",
304
           [](Graph &graph, const char *name) -> py::tuple {
H
hutuxian 已提交
305 306 307 308 309
             ge::Operator op;
             graphStatus status = graph.FindOpByName(name, op);
             return py::make_tuple(op, status);
           })
      .def("find_op_by_type",
310
           [](Graph &graph, const char *type) -> py::tuple {
H
hutuxian 已提交
311 312 313 314 315 316
             std::vector<ge::Operator> ops;
             graphStatus status = graph.FindOpByType(type, ops);
             return py::make_tuple(ops, status);
           })
      .def("get_all_op_name",
           [](Graph &graph) -> py::tuple {
317
             std::vector<AscendString> op_name;
H
hutuxian 已提交
318 319 320
             graphStatus status = graph.GetAllOpName(op_name);
             return py::make_tuple(op_name, status);
           })
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("save_to_file",
           static_cast<ge::graphStatus (ge::Graph::*)(const char *) const>(
               &ge::Graph::SaveToFile))
      .def("load_from_file",
           static_cast<ge::graphStatus (ge::Graph::*)(const char *)>(
               &Graph::LoadFromFile))
      .def("get_name",
           static_cast<ge::graphStatus (ge::Graph::*)(AscendString &) const>(
               &Graph::GetName))
#else
      .def("save_to_file", &Graph::SaveToFile)
      .def("load_from_file", &Graph::LoadFromFile)
      .def("get_name", &Graph::GetName)
#endif
H
hutuxian 已提交
336 337 338 339
      .def("set_need_iteration", &Graph::SetNeedIteration);

  py::class_<Operator>(*m, "GEOperator")
      .def(py::init<>())
340
      .def(py::init<const char *>())
341
      .def(py::init<const char *, const char *>())
H
hutuxian 已提交
342
      .def("is_empty", &Operator::IsEmpty)
343 344 345 346 347 348 349
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("get_name",
           static_cast<ge::graphStatus (ge::Operator::*)(AscendString &) const>(
               &Operator::GetName))
      .def("get_op_type",
           static_cast<ge::graphStatus (ge::Operator::*)(AscendString &) const>(
               &Operator::GetOpType))
H
hutuxian 已提交
350
      .def("set_input",
351
           (Operator & (Operator::*)(const char *, const Operator &)) &
H
hutuxian 已提交
352 353
               Operator::SetInput)
      .def("set_input",
354 355
           (Operator &
            (Operator::*)(const char *, const Operator &, const char *)) &
H
hutuxian 已提交
356
               Operator::SetInput)
357
      .def("set_input", (Operator & (Operator::*)(const char *,
H
hutuxian 已提交
358 359
                                                  const Operator &, uint32_t)) &
                            Operator::SetInput)
360 361 362 363 364 365 366 367 368 369 370 371 372 373
#else
      .def("get_name", &Operator::GetName)
      .def("get_op_type", &Operator::GetOpType)
      .def("set_input",
           (Operator & (Operator::*)(const std::string &, const Operator &)) &
               Operator::SetInput)
      .def("set_input",
           (Operator & (Operator::*)(const std::string &, const Operator &,
                                     const std::string &)) &
               Operator::SetInput)
      .def("set_input", (Operator & (Operator::*)(const std::string &,
                                                  const Operator &, uint32_t)) &
                            Operator::SetInput)
#endif
H
hutuxian 已提交
374 375
      .def("add_control_input", &Operator::AddControlInput)
      .def("get_input_const_data",
376
           [](Operator &op, const char *dst_name) -> py::tuple {
H
hutuxian 已提交
377 378 379 380
             Tensor data;
             graphStatus res = op.GetInputConstData(dst_name, data);
             return py::make_tuple(data, res);
           })
381
#ifdef PADDLE_WITH_ASCEND_STRING
H
hutuxian 已提交
382
      .def("get_input_desc",
383
           (TensorDesc (Operator::*)(uint32_t) const) & Operator::GetInputDesc)
H
hutuxian 已提交
384
      .def("get_input_desc",
385 386
           [](Operator &op, const std::string &name) {
             return op.GetInputDescByName(name.c_str());
387
           })
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
      .def("get_dynamic_output_num",
           static_cast<int (ge::Operator::*)(const char *) const>(
               &Operator::GetDynamicOutputNum))
      .def("get_dynamic_input_num",
           static_cast<int (ge::Operator::*)(const char *) const>(
               &Operator::GetDynamicInputNum))
#else
      .def("get_input_desc",
           (TensorDesc (Operator::*)(const std::string &) const) &
               Operator::GetInputDesc)
      .def("get_input_desc",
           (TensorDesc (Operator::*)(uint32_t) const) & Operator::GetInputDesc)
      .def("get_dynamic_output_num", &Operator::GetDynamicOutputNum)
      .def("get_dynamic_input_num", &Operator::GetDynamicInputNum)
#endif
H
hutuxian 已提交
403
      .def("try_get_input_desc",
404
           [](Operator &op, const char *name) -> py::tuple {
H
hutuxian 已提交
405 406 407 408
             TensorDesc tensor_desc;
             graphStatus status = op.TryGetInputDesc(name, tensor_desc);
             return py::make_tuple(tensor_desc, status);
           })
409 410 411 412
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("update_input_desc",
           static_cast<ge::graphStatus (ge::Operator::*)(  // NOLINT
               const char *, const TensorDesc &)>(&Operator::UpdateInputDesc))
H
hutuxian 已提交
413
      .def("get_output_desc",
414 415
           [](Operator &op, const std::string &name) {
             return op.GetOutputDescByName(name.c_str());
416
           })
H
hutuxian 已提交
417 418
      .def("get_output_desc",
           (TensorDesc (Operator::*)(uint32_t) const) & Operator::GetOutputDesc)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      .def("update_output_desc",
           static_cast<ge::graphStatus (ge::Operator::*)(  // NOLINT
               const char *, const TensorDesc &)>(&Operator::UpdateOutputDesc))
      .def("get_dynamic_input_desc",
           static_cast<ge::TensorDesc (ge::Operator::*)(const char *, uint32_t)
                           const>(&Operator::GetDynamicInputDesc))
      .def("update_dynamic_input_desc",
           static_cast<ge::graphStatus (ge::Operator::*)(const char *, uint32_t,
                                                         const TensorDesc &)>(
               &Operator::UpdateDynamicInputDesc))
      .def("get_dynamic_output_desc",
           static_cast<ge::TensorDesc (ge::Operator::*)(const char *, uint32_t)
                           const>(&Operator::GetDynamicOutputDesc))
      .def("update_dynamic_output_desc",
           static_cast<ge::graphStatus (ge::Operator::*)(const char *, uint32_t,
                                                         const TensorDesc &)>(
               &Operator::UpdateDynamicOutputDesc))
#else
      .def("update_input_desc", &Operator::UpdateInputDesc)
      .def("get_output_desc",
           (TensorDesc (Operator::*)(const std::string &) const) &
               Operator::GetOutputDesc)
      .def("get_output_desc",
           (TensorDesc (Operator::*)(uint32_t) const) & Operator::GetOutputDesc)
      .def("update_output_desc", &Operator::UpdateOutputDesc)
      .def("get_dynamic_input_desc", &Operator::GetDynamicInputDesc)
      .def("update_dynamic_input_desc", &Operator::UpdateDynamicInputDesc)
      .def("get_dynamic_output_desc", &Operator::GetDynamicOutputDesc)
      .def("update_dynamic_output_desc", &Operator::UpdateDynamicOutputDesc)
#endif
H
hutuxian 已提交
449 450 451 452 453 454
      .def("infer_shape_and_type", &Operator::InferShapeAndType)
      .def("set_inference_context", &Operator::SetInferenceContext)
      .def("get_inference_context", &Operator::GetInferenceContext)
      .def("verify_all_attr", &Operator::VerifyAllAttr)
      .def("get_inputs_size", &Operator::GetInputsSize)
      .def("get_outputs_size", &Operator::GetOutputsSize)
455 456 457 458 459 460 461 462
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("get_all_attr_names_and_types",
           static_cast<ge::graphStatus (ge::Operator::*)(  // NOLINT
               std::map<AscendString, AscendString> &) const>(
               &Operator::GetAllAttrNamesAndTypes))
#else
      .def("get_all_attr_names_and_types", &Operator::GetAllAttrNamesAndTypes)
#endif
H
hutuxian 已提交
463
      .def("set_attr_int64",
464
           [](Operator &op, const char *name, int64_t value) -> Operator & {
H
hutuxian 已提交
465 466 467 468
             int64_t tar = (int64_t)value;
             return op.SetAttr(name, tar);
           })
      .def("set_attr_int32",
469
           [](Operator &op, const char *name, int32_t value) -> Operator & {
H
hutuxian 已提交
470 471 472 473
             int32_t tar = (int32_t)value;
             return op.SetAttr(name, tar);
           })
      .def("set_attr_uint32",
474
           [](Operator &op, const char *name, uint32_t value) -> Operator & {
H
hutuxian 已提交
475 476 477 478
             uint32_t tar = (uint32_t)value;
             return op.SetAttr(name, tar);
           })
      .def("set_attr_vec_int64",
479
           [](Operator &op, const char *name,
H
hutuxian 已提交
480 481 482 483 484 485 486 487 488 489 490
              const std::vector<int64_t> &value) -> Operator & {
             int len = value.size();
             std::vector<int64_t> tar;
             int64_t tmp;
             for (int i = 0; i < len; i++) {
               tmp = (int64_t)value[i];
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
      .def("set_attr_vec_int32",
491
           [](Operator &op, const char *name,
H
hutuxian 已提交
492 493 494 495 496 497 498 499 500 501 502
              const std::vector<int32_t> &value) -> Operator & {
             int len = value.size();
             std::vector<int32_t> tar;
             int32_t tmp;
             for (int i = 0; i < len; i++) {
               tmp = (int32_t)value[i];
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
      .def("set_attr_vec_uint32",
503
           [](Operator &op, const char *name,
H
hutuxian 已提交
504 505 506 507 508 509 510 511 512 513 514
              const std::vector<uint32_t> &value) -> Operator & {
             int len = value.size();
             std::vector<uint32_t> tar;
             uint32_t tmp;
             for (int i = 0; i < len; i++) {
               tmp = (uint32_t)value[i];
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
      .def("set_attr_list_int64",
515
           [](Operator &op, const char *name,
H
hutuxian 已提交
516 517 518 519
              std::initializer_list<int64_t> &attrValue) -> Operator & {
             return op.SetAttr(name, std::move(attrValue));
           })
      .def("set_attr_attrvalue",
520
           [](Operator &op, const char *name, AttrValue &attrValue)
H
hutuxian 已提交
521
               -> Operator & { return op.SetAttr(name, std::move(attrValue)); })
522 523 524 525 526
      .def("set_attr_float",
           [](Operator &op, const char *name, float value) -> Operator & {
             float tar = static_cast<float>(value);
             return op.SetAttr(name, tar);
           })
H
hutuxian 已提交
527
      .def("set_attr_vec_float",
528
           [](Operator &op, const char *name,
H
hutuxian 已提交
529 530 531 532 533 534 535 536 537 538
              const std::vector<float> &value) -> Operator & {
             int len = value.size();
             std::vector<float> tar;
             float tmp;
             for (int i = 0; i < len; i++) {
               tmp = static_cast<float>(value[i]);
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
539 540 541 542 543 544 545 546 547 548 549
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("set_attr_string",
           (Operator & (Operator::*)(const char *, const char *)) &
               Operator::SetAttr)
      .def("set_attr_vec_string",
           (Operator &
            (Operator::*)(const char *, const std::vector<AscendString> &)) &
               Operator::SetAttr)
#else
      .def("set_attr_string", (Operator & (Operator::*)(const std::string &,
                                                        const std::string &)) &
H
hutuxian 已提交
550 551
                                  Operator::SetAttr)
      .def("set_attr_vec_string",
552 553
           (Operator & (Operator::*)(const std::string &,
                                     const std::vector<std::string> &)) &
H
hutuxian 已提交
554
               Operator::SetAttr)
555
#endif
H
hutuxian 已提交
556
      .def("set_attr_bool",
557
           [](Operator &op, const char *name, bool value) -> Operator & {
H
hutuxian 已提交
558 559 560 561 562 563
             if (value)
               return op.SetAttr(name, true);
             else
               return op.SetAttr(name, false);
           })
      .def("set_attr_vec_bool",
564
           [](Operator &op, const char *name,
H
hutuxian 已提交
565 566 567 568 569 570 571 572 573 574 575
              const std::vector<bool> &value) -> Operator & {
             int len = value.size();
             std::vector<bool> tar;
             for (int i = 0; i < len; i++) {
               if (value[i])
                 tar.push_back(true);
               else
                 tar.push_back(false);
             }
             return op.SetAttr(name, tar);
           })
576
#ifdef PADDLE_WITH_ASCEND_STRING
H
hutuxian 已提交
577
      .def("set_attr_tensor",
578
           (Operator & (Operator::*)(const char *, const Tensor &)) &
H
hutuxian 已提交
579 580 581
               Operator::SetAttr)
      .def("set_attr_vec_tensor",
           (Operator &
582
            (Operator::*)(const char *, const std::vector<Tensor> &)) &
H
hutuxian 已提交
583
               Operator::SetAttr)
584 585 586 587 588 589 590 591 592
#else
      .def("set_attr_tensor",
           (Operator & (Operator::*)(const std::string &, const Tensor &)) &
               Operator::SetAttr)
      .def("set_attr_vec_tensor",
           (Operator &
            (Operator::*)(const std::string &, const std::vector<Tensor> &)) &
               Operator::SetAttr)
#endif
H
hutuxian 已提交
593
      .def("set_attr_vec_uint8",
594
           [](Operator &op, const char *name,
H
hutuxian 已提交
595 596 597 598 599 600 601 602 603 604
              const std::vector<uint8_t> &value) -> Operator & {
             int len = value.size();
             std::vector<uint8_t> tar;
             uint8_t tmp;
             for (int i = 0; i < len; i++) {
               tmp = (uint8_t)value[i];
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
605 606 607 608 609 610 611
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("set_attr_vec_vec_int64",
           (Operator &
            (Operator::*)(const char *,
                          const std::vector<std::vector<int64_t>> &)) &
               Operator::SetAttr)
#else
H
hutuxian 已提交
612 613
      .def("set_attr_vec_vec_int64",
           (Operator &
614
            (Operator::*)(const std::string &,
H
hutuxian 已提交
615 616
                          const std::vector<std::vector<int64_t>> &)) &
               Operator::SetAttr)
617
#endif
H
hutuxian 已提交
618
      .def("set_attr_vec_dtype",
619
           [](Operator &op, const char *name,
H
hutuxian 已提交
620 621 622 623 624 625 626 627 628 629 630
              const std::vector<DataType> &value) -> Operator & {
             int len = value.size();
             std::vector<ge::DataType> tar;
             ge::DataType tmp;
             for (int i = 0; i < len; i++) {
               tmp = (ge::DataType)value[i];
               tar.push_back(tmp);
             }
             return op.SetAttr(name, tar);
           })
      .def("set_attr_dtype",
631
           [](Operator &op, const char *name,
H
hutuxian 已提交
632 633 634 635 636
              const DataType &value) -> Operator & {
             ge::DataType tar = (ge::DataType)value;
             return op.SetAttr(name, tar);
           })
      .def("get_attr",
637
           [](Operator &op, const char *name, AttrType type) -> py::tuple {
H
hutuxian 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
             graphStatus res = -1;
             switch (type) {
               case AT_INT64: {
                 int64_t i_64_av;
                 res = op.GetAttr(name, i_64_av);
                 return py::make_tuple(i_64_av, res);
               } break;
               case AT_INT32: {
                 int32_t i_32_av;
                 res = op.GetAttr(name, i_32_av);
                 return py::make_tuple(i_32_av, res);
               } break;
               case AT_UINT32: {
                 uint32_t ui_32_av;
                 res = op.GetAttr(name, ui_32_av);
                 return py::make_tuple(ui_32_av, res);
               } break;
               case AT_LIST_INT64: {
                 std::vector<int64_t> v_i_64_av;
                 res = op.GetAttr(name, v_i_64_av);
                 return py::make_tuple(v_i_64_av, res);
               } break;
               case AT_LIST_INT32: {
                 std::vector<int32_t> v_i_32_av;
                 res = op.GetAttr(name, v_i_32_av);
                 return py::make_tuple(v_i_32_av, res);
               } break;
               case AT_LIST_UINT32: {
                 std::vector<uint32_t> v_ui_32_av;
                 res = op.GetAttr(name, v_ui_32_av);
                 return py::make_tuple(v_ui_32_av, res);
               } break;
               case AT_FLOAT: {
                 float f_av;
                 res = op.GetAttr(name, f_av);
                 return py::make_tuple(f_av, res);
               } break;
               case AT_LIST_FLOAT: {
                 std::vector<float> v_f_av;
                 res = op.GetAttr(name, v_f_av);
                 return py::make_tuple(v_f_av, res);
               } break;
               case AT_ATTR_VALUE: {
                 AttrValue o_av;
                 res = op.GetAttr(name, o_av);
                 return py::make_tuple(o_av, res);
               } break;
               case AT_STRING: {
686
                 AscendString s_av;
H
hutuxian 已提交
687 688 689 690
                 res = op.GetAttr(name, s_av);
                 return py::make_tuple(s_av, res);
               } break;
               case AT_LIST_STRING: {
691
                 std::vector<AscendString> v_s_av;
H
hutuxian 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
                 res = op.GetAttr(name, v_s_av);
                 return py::make_tuple(v_s_av, res);
               } break;
               case AT_BOOL: {
                 bool b_av;
                 res = op.GetAttr(name, b_av);
                 return py::make_tuple(b_av, res);
               } break;
               case AT_LIST_BOOL: {
                 std::vector<bool> v_b_av;
                 res = op.GetAttr(name, v_b_av);
                 return py::make_tuple(v_b_av, res);
               } break;
               case AT_TENSOR: {
                 Tensor t_av;
                 res = op.GetAttr(name, t_av);
                 return py::make_tuple(t_av, res);
               } break;
               case AT_LIST_TENSOR: {
                 std::vector<Tensor> v_t_av;
                 res = op.GetAttr(name, v_t_av);
                 return py::make_tuple(v_t_av, res);
               } break;
               case AT_LIST_UINT8: {
                 std::vector<uint8_t> v_ui_8_av;
                 res = op.GetAttr(name, v_ui_8_av);
                 return py::make_tuple(v_ui_8_av, res);
               } break;
               case AT_LIST_LIST_INT64: {
                 std::vector<std::vector<int64_t>> v_v_i_64_av;
                 res = op.GetAttr(name, v_v_i_64_av);
                 return py::make_tuple(v_v_i_64_av, res);
               } break;
               case AT_DT: {
                 ge::DataType dt_av;
                 res = op.GetAttr(name, dt_av);
                 return py::make_tuple(dt_av, res);
               } break;
               case AT_LIST_DT: {
                 std::vector<ge::DataType> v_dt_av;
                 res = op.GetAttr(name, v_dt_av);
                 return py::make_tuple(v_dt_av, res);
               } break;
               default:
                 return py::make_tuple(0, res);
                 break;
             }
           })
      .def("break_connect", &Operator::BreakConnect)
      .def("get_subgraph_names_count", &Operator::GetSubgraphNamesCount)
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("get_subgraph_names",
           static_cast<ge::graphStatus (ge::Operator::*)(  // NOLINT
               std::vector<AscendString> &) const>(&Operator::GetSubgraphNames))
      .def("get_subgraph_builder",
           static_cast<ge::SubgraphBuilder (ge::Operator::*)(const char *)
                           const>(&Operator::GetSubgraphBuilder))
      .def("get_subgraph",
           static_cast<ge::Graph (ge::Operator::*)(const char *) const>(
               &Operator::GetSubgraph))
      .def("get_dynamic_subgraph_builder",
           static_cast<ge::SubgraphBuilder (ge::Operator::*)(const char *,
                                                             uint32_t) const>(
               &Operator::GetDynamicSubgraphBuilder))
      .def("get_dynamic_subgraph",
           static_cast<ge::Graph (ge::Operator::*)(const char *, uint32_t)
                           const>(&Operator::GetDynamicSubgraph));
#else
      .def("get_subgraph_names_count", &Operator::GetSubgraphNamesCount)
      .def("get_subgraph_names", &Operator::GetSubgraphNames)
      .def("get_subgraph_builder", &Operator::GetSubgraphBuilder)
      .def("get_subgraph", &Operator::GetSubgraph)
      .def("get_dynamic_subgraph_builder", &Operator::GetDynamicSubgraphBuilder)
      .def("get_dynamic_subgraph", &Operator::GetDynamicSubgraph);
#endif
H
hutuxian 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780

  py::class_<Tensor>(*m, "GETensor")
      .def(py::init<>())
      .def(py::init<const TensorDesc &>())
      .def(py::init<const TensorDesc &, const std::vector<uint8_t> &>())
      .def(py::init<const TensorDesc &, const uint8_t *, size_t>())
      .def("set_tensor_desc", &Tensor::SetTensorDesc)
      .def("get_tensor_desc", &Tensor::GetTensorDesc)
      // .def("set_data", (graphStatus(Tensor::*)(std::vector<uint8_t> &&)) &
      // Tensor::SetData)
      .def("set_data", (graphStatus (Tensor::*)(const std::vector<uint8_t> &)) &
                           Tensor::SetData)
      .def("set_data",
           (graphStatus (Tensor::*)(const uint8_t *, size_t)) & Tensor::SetData)
781
#ifdef PADDLE_WITH_ASCEND_STRING
H
hutuxian 已提交
782
      .def("set_data",
783 784
           (graphStatus (Tensor::*)(const char *)) & Tensor::SetData)
#else
H
hutuxian 已提交
785
      .def("set_data",
786 787 788 789
           (graphStatus (Tensor::*)(const std::string &)) & Tensor::SetData)
#endif
      .def("set_data",
           (graphStatus (Tensor::*)(const std::vector<AscendString> &)) &
H
hutuxian 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
               Tensor::SetData)

      .def("get_data",
           [](Tensor &ts) -> py::list {
             py::list v_data;
             uint8_t *data = ts.GetData();
             size_t size = ts.GetSize();
             for (size_t i = 0; i < size; ++i) {
               v_data.append(data[i]);
             }
             return v_data;
           })
      .def("get_size", &Tensor::GetSize)
      .def("is_valid", &Tensor::IsValid)
      .def("clone", &Tensor::Clone);

  py::class_<TensorDesc>(*m, "GETensorDesc")
      .def(py::init<>())
      .def(py::init<Shape, Format, DataType>(), py::arg("shape"),
           py::arg("format") = FORMAT_ND, py::arg("dt") = DT_FLOAT)
      .def(py::init<const TensorDesc &>())
811 812
      .def("update", (void (TensorDesc::*)(const Shape &, Format, DataType)) &
                         TensorDesc::Update,
H
hutuxian 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
           py::arg("shape"), py::arg("format") = FORMAT_ND,
           py::arg("dt") = DT_FLOAT)
      .def("set_shape", &TensorDesc::SetShape)
      .def("get_shape", &TensorDesc::GetShape)
      .def("set_unknown_dim_num_shape", &TensorDesc::SetUnknownDimNumShape)
      .def("set_shape_range", &TensorDesc::SetShapeRange)
      .def("get_shape_range",
           [](TensorDesc &tensorDesc) -> py::tuple {
             std::vector<std::pair<int64_t, int64_t>> range;
             graphStatus status = tensorDesc.GetShapeRange(range);
             return py::make_tuple(range, status);
           })
      .def("set_format", &TensorDesc::SetFormat)
      .def("get_format", &TensorDesc::GetFormat)
      .def("get_origin_shape", &TensorDesc::GetOriginShape)
      .def("set_origin_shape", &TensorDesc::SetOriginShape)
      .def("set_origin_format", &TensorDesc::SetOriginFormat)
      .def("get_origin_format", &TensorDesc::GetOriginFormat)
      .def("set_data_type", &TensorDesc::SetDataType)
      .def("get_data_type", &TensorDesc::GetDataType)
833 834 835 836 837 838 839 840 841 842
#ifdef PADDLE_WITH_ASCEND_STRING
      .def("set_name", static_cast<void (ge::TensorDesc::*)(const char *)>(
                           &TensorDesc::SetName))
      .def("get_name",
           static_cast<ge::graphStatus (ge::TensorDesc::*)(AscendString &)>(
               &TensorDesc::GetName))
#else
      .def("set_name", &TensorDesc::SetName)
      .def("get_name", &TensorDesc::GetName)
#endif
H
hutuxian 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
      .def("set_size", &TensorDesc::SetSize)
      .def("get_size", &TensorDesc::GetSize)
      .def("set_real_dim_cnt", &TensorDesc::SetRealDimCnt)
      .def("get_real_dim_cnt", &TensorDesc::GetRealDimCnt);

  py::class_<Shape>(*m, "GEShape")
      .def(py::init<>())
      .def(py::init<const std::vector<int64_t> &>())
      .def("get_dim_num", &Shape::GetDimNum)
      .def("set_dim", &Shape::SetDim)
      .def("get_dim", &Shape::GetDim)
      .def("get_dims", &Shape::GetDims)
      .def("get_shape_size", &Shape::GetShapeSize);

  py::class_<AttrValue>(*m, "GEAttrValue").def(py::init<>());

  py::class_<OperatorFactory>(*m, "GEOperatorFactory")
860 861 862 863 864 865 866
#ifdef PADDLE_WITH_ASCEND_STRING
      .def_static("create_operator",
                  static_cast<ge::Operator (*)(const char *, const char *)>(
                      &ge::OperatorFactory::CreateOperator))
#else
      .def("create_operator", &OperatorFactory::CreateOperator)
#endif
H
hutuxian 已提交
867 868
      .def("get_ops_type_list",
           []() -> py::tuple {
869
             std::vector<AscendString> all_ops;
H
hutuxian 已提交
870 871 872
             graphStatus status = OperatorFactory::GetOpsTypeList(all_ops);
             return py::make_tuple(all_ops, status);
           })
873 874 875 876 877 878
#ifdef PADDLE_WITH_ASCEND_STRING
      .def_static("is_exist_op", static_cast<bool (*)(const char *)>(
                                     &OperatorFactory::IsExistOp));
#else
      .def("is_exist_op", &OperatorFactory::IsExistOp);
#endif
H
hutuxian 已提交
879 880
}

881 882
}  // namespace pybind
}  // namespace paddle
H
hutuxian 已提交
883
#endif