pybind.cc 89.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2
Copyright (c) 2022 NVIDIA Authors. All Rights Reserved.
3 4 5 6 7

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

8
http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14

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. */
L
lgone2000 已提交
15
#include <Python.h>
16

C
chengduoZH 已提交
17
#include <algorithm>
18
#include <cctype>
19
#include <cstdlib>
20
#include <iterator>
C
chengduoZH 已提交
21
#include <map>
S
sneaxiy 已提交
22
#include <memory>
C
chengduoZH 已提交
23 24
#include <mutex>  // NOLINT // for call_once
#include <string>
25 26
#include <tuple>
#include <type_traits>
C
chengduoZH 已提交
27
#include <unordered_map>
28
#include <unordered_set>
C
chengduoZH 已提交
29 30
#include <utility>
#include <vector>
31

32
#include "paddle/fluid/framework/convert_utils.h"
33
#include "paddle/fluid/framework/custom_operator.h"
34
#include "paddle/fluid/framework/data_layout.h"
L
Leo Chen 已提交
35
#include "paddle/fluid/framework/data_type_transform.h"
Y
Yi Wang 已提交
36
#include "paddle/fluid/framework/executor.h"
37
#include "paddle/fluid/framework/executor_cache.h"
38
#include "paddle/fluid/framework/executor_gc_helper.h"
Y
Yi Wang 已提交
39
#include "paddle/fluid/framework/feed_fetch_method.h"
Z
Zhen Wang 已提交
40
#include "paddle/fluid/framework/feed_fetch_type.h"
S
sneaxiy 已提交
41
#include "paddle/fluid/framework/garbage_collector.h"
H
hutuxian 已提交
42
#include "paddle/fluid/framework/io/fs.h"
43
#include "paddle/fluid/framework/ir/coalesce_grad_tensor_pass.h"
H
Huihuang Zheng 已提交
44
#include "paddle/fluid/framework/ir/cost_model.h"
45
#include "paddle/fluid/framework/ir/generate_pass.h"
46
#include "paddle/fluid/framework/ir/pass_builder.h"
Y
Yi Wang 已提交
47 48
#include "paddle/fluid/framework/lod_rank_table.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
L
liutiexing 已提交
49
#include "paddle/fluid/framework/new_executor/executor_statistics.h"
50
#include "paddle/fluid/framework/new_executor/standalone_executor.h"
S
sneaxiy 已提交
51
#include "paddle/fluid/framework/op_info.h"
52
#include "paddle/fluid/framework/op_registry.h"
53
#include "paddle/fluid/framework/op_version_registry.h"
Y
Yu Yang 已提交
54
#include "paddle/fluid/framework/parallel_executor.h"
55
#include "paddle/fluid/framework/phi_utils.h"
Y
Yi Wang 已提交
56
#include "paddle/fluid/framework/prune.h"
Y
Refine  
Yu Yang 已提交
57
#include "paddle/fluid/framework/reader.h"
H
hong 已提交
58
#include "paddle/fluid/framework/save_load_util.h"
S
sneaxiy 已提交
59
#include "paddle/fluid/framework/scope_pool.h"
60
#include "paddle/fluid/framework/selected_rows_utils.h"
61
#include "paddle/fluid/framework/tensor_util.h"
62
#include "paddle/fluid/framework/trainer.h"
63
#include "paddle/fluid/framework/type_defs.h"
X
Xin Pan 已提交
64
#include "paddle/fluid/framework/version.h"
L
Leo Chen 已提交
65
#include "paddle/fluid/imperative/amp_auto_cast.h"
H
hong 已提交
66
#include "paddle/fluid/imperative/layer.h"
Y
Refine  
Yu Yang 已提交
67
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
68 69 70
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/memory/allocation/cuda_ipc_allocator.h"
#endif
71
#include "paddle/fluid/memory/allocation/mmap_allocator.h"
D
dzhwinter 已提交
72
#include "paddle/fluid/operators/activation_op.h"
L
Leo Chen 已提交
73
#include "paddle/fluid/operators/common_infer_shape_functions.h"
S
sneaxiy 已提交
74
#include "paddle/fluid/operators/py_func_op.h"
75
#include "paddle/fluid/platform/cpu_helper.h"
Y
Yu Yang 已提交
76
#include "paddle/fluid/platform/cpu_info.h"
77
#include "paddle/fluid/platform/device/device_wrapper.h"
78
#include "paddle/fluid/platform/device_context.h"
79
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
Y
Yi Wang 已提交
80
#include "paddle/fluid/platform/enforce.h"
81
#include "paddle/fluid/platform/init.h"
H
hutuxian 已提交
82
#include "paddle/fluid/platform/monitor.h"
Y
Yi Wang 已提交
83 84
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
C
chenjian 已提交
85 86 87
#include "paddle/fluid/platform/profiler/event_python.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/profiler.h"
88
#include "paddle/fluid/pybind/cuda_streams_py.h"
89
#include "paddle/fluid/pybind/distributed_py.h"
90
#include "paddle/fluid/pybind/eager.h"
J
Jiabin Yang 已提交
91
#include "paddle/fluid/pybind/imperative.h"
92
#include "paddle/fluid/pybind/io.h"
93
#include "paddle/fluid/pybind/jit.h"
94 95
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/lod_utils.h"
96
#include "paddle/utils/none.h"
97 98 99
#ifdef PADDLE_WITH_ASCEND
#include "paddle/fluid/pybind/ascend_wrapper_py.h"
#endif
H
Huihuang Zheng 已提交
100
#include "paddle/fluid/pybind/bind_cost_model.h"
L
LiYuRio 已提交
101
#include "paddle/fluid/pybind/bind_fleet_executor.h"
H
hutuxian 已提交
102
#include "paddle/fluid/pybind/box_helper_py.h"
103
#include "paddle/fluid/pybind/communication.h"
104
#include "paddle/fluid/pybind/compatible.h"
Y
Yi Wang 已提交
105
#include "paddle/fluid/pybind/const_value.h"
D
dongdaxiang 已提交
106
#include "paddle/fluid/pybind/data_set_py.h"
Y
Yi Wang 已提交
107
#include "paddle/fluid/pybind/exception.h"
D
dongdaxiang 已提交
108
#include "paddle/fluid/pybind/fleet_wrapper_py.h"
Y
yaoxuefeng 已提交
109
#include "paddle/fluid/pybind/generator_py.h"
110
#include "paddle/fluid/pybind/global_value_getter_setter.h"
111
#include "paddle/fluid/pybind/gloo_context_py.h"
112
#include "paddle/fluid/pybind/gloo_wrapper_py.h"
T
Thunderbrook 已提交
113
#include "paddle/fluid/pybind/heter_wrapper_py.h"
F
flame 已提交
114
#include "paddle/fluid/pybind/inference_api.h"
F
flame 已提交
115
#include "paddle/fluid/pybind/ir.h"
116
#include "paddle/fluid/pybind/metrics_py.h"
T
Thunderbrook 已提交
117
#include "paddle/fluid/pybind/ps_gpu_wrapper_py.h"
118
#include "paddle/fluid/pybind/pybind_variant_caster.h"
119
#include "paddle/phi/backends/device_manager.h"
120

121
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
D
dongdaxiang 已提交
122
#include "paddle/fluid/pybind/nccl_wrapper_py.h"
W
wopeizl 已提交
123
#endif
124
#include "paddle/fluid/framework/data_type.h"
125 126
#include "paddle/fluid/pybind/parallel_executor.h"
#include "paddle/fluid/pybind/place.h"
127 128
#include "paddle/fluid/pybind/protobuf.h"
#include "paddle/fluid/pybind/pybind.h"  // NOLINT
S
sneaxiy 已提交
129
#include "paddle/fluid/pybind/reader_py.h"
130
#include "paddle/fluid/pybind/tensor.h"
Y
Yi Wang 已提交
131
#include "paddle/fluid/pybind/tensor_py.h"
132
#include "paddle/fluid/string/to_string.h"
133 134
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
Y
Yi Wang 已提交
135
#include "paddle/fluid/operators/nccl/nccl_gpu_common.h"
P
peizhilin 已提交
136
#endif
137
#ifndef PADDLE_WITH_HIP
138
#include "paddle/fluid/platform/device/gpu/cuda/cuda_profiler.h"
139
#endif
140
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
D
Dong Zhihong 已提交
141 142
#endif

143
#ifdef PADDLE_WITH_ASCEND_CL
144
#include "paddle/fluid/platform/collective_helper.h"
145 146
#include "paddle/fluid/platform/device/npu/npu_info.h"
#include "paddle/fluid/platform/device/npu/npu_profiler.h"
147 148
#endif

149
#ifdef PADDLE_WITH_XPU
150
#include "paddle/fluid/platform/device/xpu/xpu_info.h"
T
TTerror 已提交
151
#include "paddle/fluid/platform/device/xpu/xpu_op_list.h"
152 153
#endif

154 155 156 157
#ifdef PADDLE_WITH_CUSTOM_DEVICE
#include "paddle/phi/capi/capi.h"
#endif

158
#include "paddle/fluid/platform/cuda_graph_with_memory_pool.h"
A
Allen Guo 已提交
159

J
jianghaicheng 已提交
160
#ifdef PADDLE_WITH_IPU
A
Allen Guo 已提交
161 162
#include "paddle/fluid/platform/device/ipu/ipu_backend.h"
#include "paddle/fluid/platform/device/ipu/ipu_info.h"
J
jianghaicheng 已提交
163
#endif
164

165 166 167 168
#ifdef PADDLE_WITH_MLU
#include "paddle/fluid/platform/device/mlu/mlu_info.h"
#endif

Y
Yanghello 已提交
169 170 171 172
#ifdef PADDLE_WITH_CRYPTO
#include "paddle/fluid/pybind/crypto.h"
#endif

T
tangwei12 已提交
173
#if defined PADDLE_WITH_PSCORE
T
tangwei12 已提交
174 175 176
#include "paddle/fluid/pybind/fleet_py.h"
#endif

177 178 179 180
#ifdef PADDLE_WITH_CINN
#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h"
#endif

181
#include "paddle/fluid/eager/api/utils/global_utils.h"
182
#include "paddle/fluid/imperative/layout_autotune.h"
183 184
#include "paddle/fluid/pybind/eager_utils.h"
#include "paddle/phi/api/ext/op_meta_info.h"
185 186
#include "paddle/phi/kernels/autotune/cache.h"
#include "paddle/phi/kernels/autotune/switch_autotune.h"
M
minqiyang 已提交
187 188
#include "pybind11/stl.h"

189
DECLARE_bool(use_mkldnn);
190

Q
Qiao Longfei 已提交
191 192
// disable auto conversion to list in Python
PYBIND11_MAKE_OPAQUE(paddle::framework::LoDTensorArray);
193 194 195
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchUnmergedList);
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchList);
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchType);
Q
Qiao Longfei 已提交
196

197
namespace paddle {
198
namespace pybind {
199

0
0x45f 已提交
200
PyTypeObject *g_framework_scope_pytype = nullptr;
201
PyTypeObject *g_framework_lodtensorarray_pytype = nullptr;
202
PyTypeObject *g_custom_op_kernel_ctx_pytype = nullptr;
203

204
bool IsCompiledWithCUDA() {
205 206 207 208 209 210 211
#if !defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
  return false;
#else
  return true;
#endif
}

212 213 214 215 216 217 218 219
bool IsCompiledWithNCCL() {
#ifdef PADDLE_WITH_NCCL
  return true;
#else
  return false;
#endif
}

220 221
bool IsCompiledWithROCM() {
#ifndef PADDLE_WITH_HIP
Q
qijun 已提交
222 223 224 225 226 227
  return false;
#else
  return true;
#endif
}

228 229 230 231 232 233 234 235
bool IsCompiledWithAscend() {
#ifndef PADDLE_WITH_ASCEND
  return false;
#else
  return true;
#endif
}

236 237 238 239 240 241 242 243
bool IsCompiledWithXPU() {
#ifndef PADDLE_WITH_XPU
  return false;
#else
  return true;
#endif
}

244 245 246 247 248 249 250 251
bool IsCompiledWithNPU() {
#ifndef PADDLE_WITH_ASCEND_CL
  return false;
#else
  return true;
#endif
}

J
jianghaicheng 已提交
252 253 254 255 256 257 258 259
bool IsCompiledWithIPU() {
#ifndef PADDLE_WITH_IPU
  return false;
#else
  return true;
#endif
}

260 261 262 263 264 265 266 267
bool IsCompiledWithMKLDNN() {
#ifndef PADDLE_WITH_MKLDNN
  return false;
#else
  return true;
#endif
}

268 269 270 271 272 273 274 275
bool IsCompiledWithCINN() {
#ifndef PADDLE_WITH_CINN
  return false;
#else
  return true;
#endif
}

276 277 278 279 280 281 282 283
bool IsCompiledWithMLU() {
#ifndef PADDLE_WITH_MLU
  return false;
#else
  return true;
#endif
}

284 285 286 287 288 289 290 291
bool IsCompiledWithHETERPS() {
#ifndef PADDLE_WITH_HETERPS
  return false;
#else
  return true;
#endif
}

292 293 294 295 296 297 298 299 300 301 302
bool SupportsBfloat16() {
#ifndef PADDLE_WITH_MKLDNN
  return false;
#else
  if (platform::MayIUse(platform::cpu_isa_t::avx512_core))
    return true;
  else
    return false;
#endif
}

303 304 305 306 307 308 309 310 311 312 313
bool SupportsBfloat16FastPerformance() {
#ifndef PADDLE_WITH_MKLDNN
  return false;
#else
  if (platform::MayIUse(platform::cpu_isa_t::avx512_bf16))
    return true;
  else
    return false;
#endif
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
bool SupportsInt8() {
#ifndef PADDLE_WITH_MKLDNN
  return false;
#else
  return (platform::MayIUse(platform::cpu_isa_t::avx2) ||
          platform::MayIUse(platform::cpu_isa_t::avx512f));
#endif
}

bool SupportsVNNI() {
#ifndef PADDLE_WITH_MKLDNN
  return false;
#else
  return platform::MayIUse(platform::cpu_isa_t::avx512_core_vnni);
#endif
}

331
bool IsCompiledWithBrpc() {
332
#ifndef PADDLE_WITH_DISTRIBUTE
333
  return false;
334
#else
335
  return true;
336
#endif
337 338
}

Y
update  
Yancey1989 已提交
339
bool IsCompiledWithDIST() {
Y
Yancey1989 已提交
340
#ifdef PADDLE_WITH_DISTRIBUTE
Y
update  
Yancey1989 已提交
341 342 343 344 345 346
  return true;
#else
  return false;
#endif
}

H
hong 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
static PyObject *GetPythonAttribute(PyObject *obj, const char *attr_name) {
  // NOTE(zjl): PyObject_GetAttrString would return nullptr when attr_name
  // is not inside obj, but it would also set the error flag of Python.
  // If the error flag is set in C++, C++ code would not raise Exception,
  // but Python would raise Exception once C++ call ends.
  // To avoid unexpected Exception raised in Python, we check whether
  // attribute exists before calling PyObject_GetAttrString.
  //
  // Caution: PyObject_GetAttrString would increase reference count of PyObject.
  // Developer should call Py_DECREF manually after the attribute is not used.
  if (PyObject_HasAttrString(obj, attr_name)) {
    return PyObject_GetAttrString(obj, attr_name);
  } else {
    return nullptr;
  }
}

template <typename T>
static T PyObjectCast(PyObject *obj) {
  try {
    return py::cast<T>(py::handle(obj));
  } catch (py::cast_error &) {
369 370
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Python object is not type of %s, the real type is %s",
371 372
        typeid(T).name(),
        obj->ob_type->tp_name));
H
hong 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385
  }
}

using PyNameVarBaseMap = std::unordered_map<std::string, py::handle>;

static std::vector<std::shared_ptr<imperative::VarBase>> GetVarBaseList(
    const PyNameVarBaseMap &state_dict) {
  std::vector<std::shared_ptr<imperative::VarBase>> vec_res;
  vec_res.reserve(state_dict.size());

  for (auto &para : state_dict) {
    PyObject *py_obj = para.second.ptr();
    if (!py_obj || py_obj == Py_None) {
386 387
      PADDLE_THROW(platform::errors::InvalidArgument(
          "The parameter [%s] to save is None", para.first));
H
hong 已提交
388 389
    }
    vec_res.emplace_back(
390
        PyObjectCast<std::shared_ptr<imperative::VarBase>>(py_obj));
H
hong 已提交
391 392 393 394 395 396 397 398 399 400 401 402
  }

  return vec_res;
}

static std::vector<std::string> inline GetNameList(
    const py::handle &py_handle) {
  std::vector<std::string> vec_res;

  PyObject *py_obj = py_handle.ptr();  // get underlying PyObject
  // Python None is not nullptr in C++!
  if (!py_obj || py_obj == Py_None) {
403 404
    PADDLE_THROW(platform::errors::InvalidArgument(
        "The parameter list to save is None"));
H
hong 已提交
405 406 407 408 409 410 411 412 413 414 415 416
  }

  if (PyList_Check(py_obj)) {
    size_t len = PyList_GET_SIZE(py_obj);

    vec_res.reserve(len);

    const char *kNameField = "name";

    for (size_t i = 0; i < len; ++i) {
      PyObject *py_name =
          PyObject_GetAttrString(PyList_GET_ITEM(py_obj, i), kNameField);
417 418 419
      PADDLE_ENFORCE_NOT_NULL(py_name,
                              platform::errors::InvalidArgument(
                                  "The name of parameter to save is None"));
H
hong 已提交
420 421 422 423
      vec_res.emplace_back(PyObjectCast<std::string>(py_name));
      Py_DECREF(py_name);
    }
  } else {
424 425
    PADDLE_THROW(platform::errors::InvalidArgument(
        "The parameters to save is not a list"));
H
hong 已提交
426 427 428 429
  }
  return vec_res;
}

430
static void inline CreateVariableIfNotExit(
431 432
    const py::handle &py_handle,
    const framework::Scope &scope,
433 434 435 436 437 438
    const framework::Executor *exe = nullptr) {
  std::vector<std::string> vec_res;

  PyObject *py_obj = py_handle.ptr();  // get underlying PyObject
  // Python None is not nullptr in C++!
  if (!py_obj || py_obj == Py_None) {
439 440
    PADDLE_THROW(
        platform::errors::InvalidArgument("The parameter list to set is None"));
441 442 443 444 445 446 447 448 449 450 451 452 453
  }

  if (PyList_Check(py_obj)) {
    size_t len = PyList_GET_SIZE(py_obj);

    vec_res.reserve(len);

    const char *kNameField = "name";
    const char *kVarDescField = "desc";

    for (size_t i = 0; i < len; ++i) {
      PyObject *py_name =
          PyObject_GetAttrString(PyList_GET_ITEM(py_obj, i), kNameField);
454 455 456
      PADDLE_ENFORCE_NOT_NULL(py_name,
                              platform::errors::InvalidArgument(
                                  "The name of parameter to set is None"));
457 458 459 460 461
      auto para_name = PyObjectCast<std::string>(py_name);
      Py_DECREF(py_name);

      auto var = scope.FindVar(para_name);
      if (var == nullptr) {
462 463 464 465 466
        PADDLE_ENFORCE_NOT_NULL(exe,
                                platform::errors::InvalidArgument(
                                    "Parameter not Initialized, "
                                    "Please set argument [executor] not None "
                                    "or run startup program first"));
467 468
        PyObject *py_var_desc =
            PyObject_GetAttrString(PyList_GET_ITEM(py_obj, i), kVarDescField);
469
        PADDLE_ENFORCE_NOT_NULL(
470 471 472
            py_var_desc,
            platform::errors::InvalidArgument(
                "The var_desc of parameter to set is None"));
473 474 475 476
        auto var_desc = PyObjectCast<framework::VarDesc>(py_var_desc);
        Py_DECREF(py_var_desc);
        var = const_cast<framework::Scope *>(&scope)->Var(para_name);
        auto *tensor_temp = var->GetMutable<framework::LoDTensor>();
477
        tensor_temp->Resize(phi::make_ddim(var_desc.GetShape()));
478 479
        tensor_temp->mutable_data(
            exe->GetPlace(),
480
            framework::TransToPhiDataType(var_desc.GetDataType()));
481 482 483
      }
    }
  } else {
484 485
    PADDLE_THROW(platform::errors::InvalidArgument(
        "The parameters to set is not a list"));
486 487 488 489 490
  }

  return;
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
static void AssertStaticGraphAndDygraphGradMakerNoDiff() {
  std::set<std::string> ops;
  for (auto &pair : framework::OpInfoMap::Instance().map()) {
    bool has_static_grad_maker = (pair.second.grad_op_maker_ != nullptr);
    bool has_dygraph_grad_maker =
        (pair.second.dygraph_grad_op_maker_ != nullptr);
    if (has_static_grad_maker ^ has_dygraph_grad_maker) {
      bool has_kernel =
          (framework::OperatorWithKernel::AllOpKernels().count(pair.first) > 0);
      if (has_kernel) {
        ops.insert(pair.first);
      } else {
        VLOG(5) << pair.first << " has no kernels, skip";
      }
    }
  }
507 508
  PADDLE_ENFORCE_EQ(ops.empty(),
                    true,
509 510 511 512 513 514 515
                    platform::errors::Unimplemented(
                        "OperatorWithKernel [%s] have only static graph grad "
                        "maker or have only dygraph grad maker, which is not "
                        "allowed",
                        string::join_strings(ops, ',')));
}

Z
Zeng Jinle 已提交
516 517 518 519
#ifdef PADDLE_WITH_NCCL
static int GetNCCLVersion() {
#if NCCL_VERSION_CODE >= 2304
  int ver;
520
  PADDLE_ENFORCE_GPU_SUCCESS(platform::dynload::ncclGetVersion(&ver));
Z
Zeng Jinle 已提交
521 522 523 524 525 526 527 528
  return ver;
#else
  PADDLE_THROW(platform::errors::External(
      "Cannot get NCCL version successfully when nccl version < 2.3.4"));
#endif
}
#endif

529 530 531 532 533 534
#ifdef PADDLE_WITH_AVX
PYBIND11_MODULE(core_avx, m) {
#else
PYBIND11_MODULE(core_noavx, m) {
#endif

J
Jiabin Yang 已提交
535
  BindImperative(&m);
536
  BindEager(&m);
J
Jack Zhou 已提交
537
  BindEagerStringTensor(&m);
538
  BindCudaStream(&m);
539
  BindJit(&m);
540

Y
Yu Yang 已提交
541 542 543
  // Not used, just make sure cpu_info.cc is linked.
  paddle::platform::CpuTotalPhysicalMemory();

Y
Refine  
Yu Yang 已提交
544
  paddle::memory::allocation::UseAllocatorStrategyGFlag();
S
sneaxiy 已提交
545

546 547
  AssertStaticGraphAndDygraphGradMakerNoDiff();

548
  m.doc() = "C++ core of PaddlePaddle";
549

550 551 552 553
  // using framework in this function. Since it is inside a function, it will
  // not cause namespace pollution.
  using namespace paddle::framework;  // NOLINT

554
  BindException(&m);
Y
Yu Yang 已提交
555

556 557
  m.def("set_num_threads", &platform::SetNumThreads);

558 559
  m.def("disable_signal_handler", &DisableSignalHandler);

560 561 562 563 564 565 566 567
  m.def("clear_gradients",
        [](std::vector<std::shared_ptr<imperative::VarBase>> param_list,
           bool set_to_zero) {
          for (auto param : param_list) {
            param->ClearGradient(set_to_zero);
          }
        });

568
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
569
  m.def("cudnn_version", &platform::DnnVersion);
570 571 572 573 574 575
  m.def("gpu_memory_available", []() {
    size_t available = 0;
    size_t total = 0;
    paddle::platform::GpuMemoryUsage(&available, &total);
    return available;
  });
576
#endif
577

Z
Zeng Jinle 已提交
578 579 580 581
#ifdef PADDLE_WITH_NCCL
  m.def("nccl_version", &GetNCCLVersion);
#endif

582 583 584 585 586 587 588 589 590
  m.def("is_cuda_graph_capturing", &platform::IsCUDAGraphCapturing);
#ifdef PADDLE_WITH_CUDA
  py::class_<platform::CUDAGraph>(m, "CUDAGraph")
      .def_static("begin_capture",
                  [](platform::CUDAPlace place, int mode) {
                    platform::BeginCUDAGraphCapture(
                        place, static_cast<cudaStreamCaptureMode>(mode));
                  })
      .def_static("end_capture", &platform::EndCUDAGraphCapture)
591 592
      .def_static("gen_new_memory_pool_id",
                  &platform::CUDAGraph::UniqueMemoryPoolID)
593
      .def("replay", &platform::CUDAGraph::Replay)
594 595
      .def("reset", &platform::CUDAGraph::Reset)
      .def("print_to_dot_files", &platform::CUDAGraph::PrintToDotFiles);
596 597
#endif

Z
Zeng Jinle 已提交
598 599 600 601
  m.def("wait_device", [](const platform::Place &place) {
    platform::DeviceContextPool::Instance().Get(place)->Wait();
  });

6
633WHU 已提交
602 603 604
  m.def("from_dlpack", [](py::capsule *dltensor) {
    DLManagedTensor *dmt = reinterpret_cast<DLManagedTensor *>(
        PyCapsule_GetPointer(dltensor->ptr(), "dltensor"));
605 606

    PADDLE_ENFORCE_NOT_NULL(
607 608 609 610
        dmt,
        platform::errors::InvalidArgument(
            "from_dlpack received an invalid capsule. "
            "Note that a DLPack tensor can be consumed only once."));
611

6
633WHU 已提交
612 613
    PyCapsule_SetName(dltensor->ptr(), "used_dltensor");
    DLTensor dl = dmt->dl_tensor;
614
    framework::Tensor tensor;
6
633WHU 已提交
615

S
Siming Dai 已提交
616
    if (dl.device.device_type == kDLCPU) {
6
633WHU 已提交
617 618
      paddle::framework::TensorFromDLPack(dl, &tensor);
    }
619
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
S
Siming Dai 已提交
620
    if (dl.device.device_type == kDLGPU) {
6
633WHU 已提交
621 622 623 624 625
      paddle::framework::TensorFromDLPack(dl, &tensor);
    }
#endif
    return tensor;
  });
H
hong 已提交
626

627
  m.def("_create_loaded_parameter",
628 629
        [](const py::handle &vec_var_list,
           const Scope &scope,
630 631 632 633
           const Executor *executor) {
          CreateVariableIfNotExit(vec_var_list, scope, executor);
        });

634 635 636 637 638 639
  m.def("save_op_version_info", [](framework::ProgramDesc &desc) {
    framework::compatible::pb::OpVersionMap pb_vmap{desc.OpVersionMap()};
    framework::compatible::SaveOpVersions(
        framework::compatible::OpVersionRegistrar::GetInstance()
            .GetVersionMap(),
        &pb_vmap);
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
  m.def("set_printoptions", [](const py::kwargs &kwargs) {
    auto &print_opt = framework::PrintOptions::Instance();
    if (kwargs.contains("precision")) {
      print_opt.precision = kwargs["precision"].cast<int>();
    }
    if (kwargs.contains("threshold")) {
      print_opt.threshold = kwargs["threshold"].cast<int>();
    }
    if (kwargs.contains("edgeitems")) {
      print_opt.edgeitems = kwargs["edgeitems"].cast<int>();
    }
    if (kwargs.contains("linewidth")) {
      print_opt.linewidth = kwargs["linewidth"].cast<int>();
    }
    if (kwargs.contains("sci_mode")) {
      print_opt.sci_mode = kwargs["sci_mode"].cast<bool>();
    }

    VLOG(4) << "Set printoptions: precision=" << print_opt.precision
            << ", threshold=" << print_opt.threshold
            << ", edgeitems=" << print_opt.edgeitems
            << ", linewidth=" << print_opt.linewidth
            << ", sci_mode=" << print_opt.sci_mode;
  });

667 668 669 670 671 672
  m.def(
      "broadcast_shape",
      [](const std::vector<int64_t> &x_dim, const std::vector<int64_t> &y_dim) {
        return phi::vectorize(operators::details::BroadcastTwoDims(
            phi::make_ddim(x_dim), phi::make_ddim(y_dim), -1));
      });
L
Leo Chen 已提交
673

S
sneaxiy 已提交
674
  m.def(
S
sneaxiy 已提交
675
      "_append_python_callable_object_and_return_id",
S
sneaxiy 已提交
676 677 678 679
      [](py::object py_obj) -> size_t {
        return paddle::operators::AppendPythonCallableObjectAndReturnId(py_obj);
      });

S
sneaxiy 已提交
680 681 682
  m.def("_get_use_default_grad_op_desc_maker_ops",
        [] { return OpInfoMap::Instance().GetUseDefaultGradOpDescMakerOps(); });

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  m.def(
      "_get_all_register_op_kernels",
      [](const std::string &lib) {
        std::unordered_map<std::string, std::vector<std::string>>
            all_kernels_info;
        if (lib == "fluid" || lib == "all") {
          auto &all_kernels =
              paddle::framework::OperatorWithKernel::AllOpKernels();

          for (auto &kernel_pair : all_kernels) {
            auto op_type = kernel_pair.first;
            std::vector<std::string> kernel_types;
            for (auto &info_pair : kernel_pair.second) {
              paddle::framework::OpKernelType kernel_type = info_pair.first;
              kernel_types.emplace_back(
                  paddle::framework::KernelTypeToString(kernel_type));
699
            }
700
            all_kernels_info.emplace(op_type, kernel_types);
701
          }
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
        }
        if (lib == "phi" || lib == "all") {
          auto phi_kernels = phi::KernelFactory::Instance().kernels();
          for (auto &kernel_pair : phi_kernels) {
            auto op_type = phi::TransToFluidOpName(kernel_pair.first);
            std::vector<std::string> kernel_types;
            for (auto &info_pair : kernel_pair.second) {
              framework::OpKernelType kernel_type =
                  framework::TransPhiKernelKeyToOpKernelType(info_pair.first);
              auto kernel_type_str = framework::KernelTypeToString(kernel_type);
              if (all_kernels_info.count(op_type)) {
                if (std::find(all_kernels_info[op_type].begin(),
                              all_kernels_info[op_type].end(),
                              kernel_type_str) ==
                    all_kernels_info[op_type].end()) {
                  all_kernels_info[op_type].emplace_back(kernel_type_str);
718
                }
719 720
              } else {
                kernel_types.emplace_back(kernel_type_str);
721
              }
722
            }
723 724 725
            if (!kernel_types.empty()) {
              all_kernels_info.emplace(op_type, kernel_types);
            }
726
          }
727
        }
728

729 730 731 732
        return all_kernels_info;
      },
      py::arg("lib") = "all",
      R"DOC(
733 734 735
           Return the registered kernels in paddle.

           Args:
736
               lib[string]: the libarary, could be 'phi', 'fluid' and 'all'.
737
           )DOC");
738

739 740 741 742 743 744
  // NOTE(Aganlengzi): KernelFactory static instance is initialized BEFORE
  // plugins are loaded for custom kernels, but de-initialized AFTER they are
  // unloaded. We need manually clear symbols(may contain plugins' symbols)
  // stored in this static instance to avoid illegal memory access.
  m.def("clear_kernel_factory",
        []() { phi::KernelFactory::Instance().kernels().clear(); });
745 746 747 748 749
  m.def("clear_device_manager", []() {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
    phi::DeviceManager::Clear();
#endif
  });
750

S
sneaxiy 已提交
751 752 753
  // NOTE(zjl): ctest would load environment variables at the beginning even
  // though we have not `import paddle.fluid as fluid`. So we add this API
  // to enable eager deletion mode in unittest.
S
sneaxiy 已提交
754
  m.def("_set_eager_deletion_mode", &paddle::framework::SetEagerDeletionMode);
S
sneaxiy 已提交
755

756
  m.def("_set_fuse_parameter_group_size",
757
        &paddle::framework::ir::SetFuseParameterGroupsSize);
758
  m.def("_set_fuse_parameter_memory_size",
759
        &paddle::framework::ir::SetFuseParameterMemorySize);
760

S
sneaxiy 已提交
761 762 763
  m.add_object("_cleanup",
               py::capsule([]() { ScopePool::Instance().Clear(); }));

764 765
  m.def("_set_paddle_lib_path", &paddle::platform::dynload::SetPaddleLibPath);

766 767 768
  m.def("_promote_types_if_complex_exists",
        &paddle::framework::PromoteTypesIfComplexExists);

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  py::class_<paddle::CustomOpKernelContext> custom_op_kernel_ctx(
      m, "CustomOpKernelContext", R"DOC()DOC");
  g_custom_op_kernel_ctx_pytype =
      reinterpret_cast<PyTypeObject *>(custom_op_kernel_ctx.ptr());
  custom_op_kernel_ctx.def(py::init<>())
      .def("add_inputs",
           [](paddle::CustomOpKernelContext &self, const py::handle &input) {
             PyObject *obj = input.ptr();
             if (PyList_Check(obj) || PyTuple_Check(obj)) {
               self.EmplaceBackInputs(
                   std::move(CastPyArg2VectorOfTensor(obj, 1)));
             } else {
               self.EmplaceBackInput(std::move(CastPyArg2Tensor(obj, 1)));
             }
           })
      .def("add_outputs",
           [](paddle::CustomOpKernelContext &self, py::handle &outputs) {
             PyObject *obj = outputs.ptr();
             if (PyList_Check(obj) || PyTuple_Check(obj)) {
               self.EmplaceBackOutputs(
                   std::move(CastPyArg2VectorOfTensor(obj, 1)));
             } else {
               self.EmplaceBackOutput(std::move(CastPyArg2Tensor(obj, 1)));
             }
           })
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self, bool attr) {
             self.EmplaceBackAttr(attr);
           })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self, int attr) {
             self.EmplaceBackAttr(attr);
           })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self, float attr) {
             self.EmplaceBackAttr(attr);
           })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self, int64_t attr) {
             self.EmplaceBackAttr(attr);
           })
810 811 812 813 814 815 816 817 818 819 820 821 822
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self, const std::string &attr) {
             self.EmplaceBackAttr(attr);
           })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self,
              const std::vector<int> &attr) { self.EmplaceBackAttr(attr); })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self,
              const std::vector<float> &attr) { self.EmplaceBackAttr(attr); })
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self,
              const std::vector<int64_t> &attr) { self.EmplaceBackAttr(attr); })
823 824 825 826 827
      .def("add_attr",
           [](paddle::CustomOpKernelContext &self,
              const std::vector<std::string> &attr) {
             self.EmplaceBackAttr(attr);
           });
828

829
  py::class_<Variable>(m, "Variable", R"DOC(Variable Class.
830 831 832

All parameter, weight, gradient are variables in Paddle.
)DOC")
S
sneaxiy 已提交
833
      .def(py::init<>())
834
      .def("is_int", [](const Variable &var) { return var.IsType<int>(); })
835
      .def("set_int",
836 837
           [](Variable &var, int val) -> void { *var.GetMutable<int>() = val; })
      .def("get_int", [](const Variable &var) -> int { return var.Get<int>(); })
838 839 840 841 842 843 844
      .def("is_float", [](const Variable &var) { return var.IsType<float>(); })
      .def("set_float",
           [](Variable &var, float val) -> void {
             *var.GetMutable<float>() = val;
           })
      .def("get_float",
           [](const Variable &var) -> float { return var.Get<float>(); })
845 846 847 848 849 850
      .def(
          "get_tensor",
          [](Variable &self) -> LoDTensor * {
            return self.GetMutable<LoDTensor>();
          },
          py::return_value_policy::reference)
851 852 853 854
      .def("get_bytes",
           [](Variable &self) {
             return py::bytes(*self.GetMutable<std::string>());
           })
S
Steffy-zxf 已提交
855 856 857 858
      .def("set_string_list",
           [](Variable &self, Strings str_list) {
             *self.GetMutable<Strings>() = str_list;
           })
859 860 861 862
      .def("set_vocab",
           [](Variable &self, Vocab vocab) {
             *self.GetMutable<Vocab>() = vocab;
           })
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
      .def(
          "get_string_tensor",
          [](Variable &self) { return self.GetMutable<Strings>(); },
          py::return_value_policy::reference)
      .def(
          "get_map_tensor",
          [](Variable &self) { return self.GetMutable<Vocab>(); },
          py::return_value_policy::reference)
      .def(
          "get_lod_rank_table",
          [](Variable &self) { return self.GetMutable<LoDRankTable>(); },
          py::return_value_policy::reference)
      .def(
          "get_selected_rows",
          [](Variable &self) -> phi::SelectedRows * {
            return self.GetMutable<phi::SelectedRows>();
          },
          py::return_value_policy::reference)
      .def(
          "get_lod_tensor_array",
          [](Variable &self) { return self.GetMutable<LoDTensorArray>(); },
          py::return_value_policy::reference)
      .def(
          "get_fetch_list",
          [](Variable &self) { return self.GetMutable<FetchList>(); },
          py::return_value_policy::reference)
889
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
890 891 892 893 894 895
      .def(
          "get_communicator",
          [](Variable &self) -> platform::Communicator * {
            return self.GetMutable<platform::Communicator>();
          },
          py::return_value_policy::reference)
Y
Yu Yang 已提交
896
#endif
897 898 899
      .def(
          "get_reader",
          [](Variable &self) -> framework::ReaderHolder * {
900 901
            PADDLE_ENFORCE_EQ(self.IsType<framework::ReaderHolder>(),
                              true,
902 903 904 905 906 907 908 909 910 911
                              platform::errors::InvalidArgument(
                                  "The variable is not type of ReaderHolder."));
            return self.GetMutable<framework::ReaderHolder>();
          },
          py::return_value_policy::reference)
      .def(
          "get_scope",
          [](Variable &self) -> Scope * {
            auto scope_vec = self.GetMutable<std::vector<framework::Scope *>>();
            PADDLE_ENFORCE_GT(
912 913
                scope_vec->size(),
                0,
914 915 916 917 918
                platform::errors::InvalidArgument(
                    "The size of scope_vec should be greater than 0"));
            return scope_vec->front();
          },
          py::return_value_policy::reference)
919 920 921 922
      .def("set_scope", [](Variable &self, Scope &scope) {
        auto scope_vec = self.GetMutable<std::vector<framework::Scope *>>();
        scope_vec->emplace_back(&scope);
      });
923

S
sneaxiy 已提交
924
  BindReader(&m);
Y
Refine  
Yu Yang 已提交
925

0
0x45f 已提交
926
  py::class_<Scope> _Scope(m, "_Scope", R"DOC(
Q
Qiao Longfei 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939
    Scope is an association of a name to Variable. All variables belong to Scope.

    Variables in a parent scope can be retrieved from local scope.

    You need to specify a scope to run a Net, i.e., `exe.Run(&scope)`.
    One net can run in different scopes and update different variable in the
    scope.

    You can create var in a scope and get it from the scope.

    Examples:
        .. code-block:: python

940
          import paddle.fluid as fluid
Q
Qiao Longfei 已提交
941 942 943 944 945
          # create tensor from a scope and set value to it.
          param = scope.var('Param').get_tensor()
          param_array = np.full((height, row_numel), 5.0).astype("float32")
          param.set(param_array, place)

0
0x45f 已提交
946 947 948
        )DOC");
  g_framework_scope_pytype = reinterpret_cast<PyTypeObject *>(_Scope.ptr());
  _Scope
S
sneaxiy 已提交
949 950
      .def("_remove_from_pool",
           [](Scope &self) { ScopePool::Instance().Remove(&self); })
951 952 953 954 955 956 957
      .def(
          "var",
          [](Scope &self, const std::string &name) -> Variable * {
            return self.Var(name);
          },
          py::arg("name"),
          R"DOC(
958
           Find or create variable named :code:`name` in the current scope.
S
sneaxiy 已提交
959

960
           If the variable named :code:`name` does not exist in the
S
sneaxiy 已提交
961
           current scope, the variable would be created. Otherwise,
962
           return the existing variable.
S
sneaxiy 已提交
963 964

           Args:
965 966
               name (str): the variable name.

S
sneaxiy 已提交
967
           Returns:
968
               out (core.Variable): the found or created variable.
S
sneaxiy 已提交
969
           )DOC",
970
          py::return_value_policy::reference)
971 972 973
      .def("find_var",
           &Scope::FindVar,
           py::arg("name"),
S
sneaxiy 已提交
974
           R"DOC(
975
           Find variable named :code:`name` in the current scope or
976
           its parent scope. Return None if not found. 
977

S
sneaxiy 已提交
978 979
           Args:
               name (str): the variable name.
980

S
sneaxiy 已提交
981
           Returns:
982
               out (core.Variable|None): the found variable or None.
S
sneaxiy 已提交
983
           )DOC",
984
           py::return_value_policy::reference)
985
      .def("size", &Scope::Size)
986 987 988
      .def("erase",
           &Scope::EraseVars,
           py::arg("names"),
989 990 991 992 993 994 995 996 997 998 999
           R"DOC(
           Find variable named :code:`name` in the current scope or
           its parent scope. Return None if not found. 

           Args:
               name (str): the variable names to be erase.

           Returns:
               None
           )DOC",
           py::return_value_policy::reference)
1000
      .def(
1001 1002
          "new_scope",
          [](Scope &self) -> Scope * { return &self.NewScope(); },
1003
          R"DOC(
S
sneaxiy 已提交
1004 1005 1006 1007 1008
           Create a new sub-scope of the current scope.

           Returns:
               out (core._Scope): the created sub-scope.
           )DOC",
1009
          py::return_value_policy::reference)
1010 1011
      .def("drop_kids",
           &Scope::DropKids,
S
sneaxiy 已提交
1012 1013
           R"DOC(
           Delete all sub-scopes of the current scope.
S
sneaxiy 已提交
1014 1015
           )DOC")
      .def("_kids", &Scope::kids);
1016

1017 1018 1019 1020 1021 1022 1023 1024
  m.def(
      "Scope",
      []() -> Scope * {
        auto *s = new Scope();
        ScopePool::Instance().Insert(std::unique_ptr<Scope>(s));
        return s;
      },
      R"DOC(
S
sneaxiy 已提交
1025
        Create a new scope.
1026

S
sneaxiy 已提交
1027 1028 1029
        Returns:
            out (core._Scope): the created scope.
        )DOC",
1030
      py::return_value_policy::reference);
S
sneaxiy 已提交
1031

Y
Yu Yang 已提交
1032 1033
  //! @note: Be careful! PyBind will return std::string as an unicode, not
  //! Python str. If you want a str object, you should cast them in Python.
Y
Yu Yang 已提交
1034 1035
  m.def("get_all_op_protos", []() -> std::vector<py::bytes> {
    std::vector<py::bytes> ret_values;
1036 1037 1038 1039
    for (auto &iter : OpInfoMap::Instance().map()) {
      auto &info = iter.second;
      if (info.HasOpProtoAndChecker()) {
        std::string str;
C
chengduo 已提交
1040
        PADDLE_ENFORCE_EQ(
1041 1042
            info.Proto().SerializeToString(&str),
            true,
1043 1044
            platform::errors::Fatal(
                "Serialize OpProto Error. This could be a bug of Paddle."));
1045 1046 1047
        ret_values.emplace_back(str);
      }
    }
Y
Yu Yang 已提交
1048 1049
    return ret_values;
  });
1050 1051 1052 1053 1054 1055 1056
  m.def("get_all_op_names", []() {
    std::vector<std::string> op_names;
    for (auto &iter : OpInfoMap::Instance().map()) {
      op_names.emplace_back(iter.first);
    }
    return op_names;
  });
1057 1058 1059 1060 1061 1062 1063 1064
  m.def("get_op_attrs_default_value",
        [](py::bytes byte_name) -> paddle::framework::AttributeMap {
          std::string op_type = byte_name;
          paddle::framework::AttributeMap res;
          auto info = OpInfoMap::Instance().GetNullable(op_type);
          if (info != nullptr) {
            if (info->HasOpProtoAndChecker()) {
              auto op_checker = info->Checker();
1065
              res = op_checker->GetDefaultAttrsMap();
1066 1067 1068 1069
            }
          }
          return res;
        });
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  m.def("get_grad_op_desc",
        [](const OpDesc &op_desc,
           const std::unordered_set<std::string> &no_grad_set,
           const std::vector<BlockDesc *> &grad_sub_block) {
          std::unordered_map<std::string, std::string> grad_to_var;
          std::vector<std::unique_ptr<OpDesc>> grad_op_descs =
              framework::OpInfoMap::Instance()
                  .Get(op_desc.Type())
                  .GradOpMaker()(
                      op_desc, no_grad_set, &grad_to_var, grad_sub_block);
          std::vector<OpDesc *> grad_op_desc_ptrs(grad_op_descs.size());
          std::transform(
              grad_op_descs.begin(),
              grad_op_descs.end(),
              grad_op_desc_ptrs.begin(),
              [](std::unique_ptr<OpDesc> &p) { return p.release(); });
          return std::make_pair(grad_op_desc_ptrs, grad_to_var);
        });
1088 1089 1090
  m.def("has_grad_op_maker", [](const std::string op_type) {
    return framework::OpInfoMap::Instance().Get(op_type).HasGradOpMaker();
  });
1091 1092 1093 1094 1095
  m.def("has_non_empty_grad_op_maker", [](const std::string op_type) {
    return framework::OpInfoMap::Instance()
        .Get(op_type)
        .HasNonEmptyGradOpMaker();
  });
1096 1097 1098
  m.def("has_infer_inplace", [](const std::string op_type) {
    return framework::OpInfoMap::Instance().Get(op_type).HasInferInplace();
  });
1099
  m.def("infer_no_need_buffer_slots",
1100 1101
        [](const std::string op_type,
           const framework::VariableNameMap &inputs,
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
           const framework::VariableNameMap &outputs,
           const framework::AttributeMap &attrs) {
          auto infer_func = framework::OpInfoMap::Instance()
                                .Get(op_type)
                                .NoNeedBufferVarsInferer();
          if (infer_func) {
            return infer_func(inputs, outputs, attrs);
          } else {
            std::unordered_set<std::string> empty = {};
            return empty;
          }
        });
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  m.def("prune",
        [](const ProgramDesc &origin,
           const std::set<std::string> &feeded_var_names,
           const std::vector<std::array<size_t, 2>> &targets) {
          ProgramDesc prog_with_targets(origin);

          for (const auto &t : targets) {
            prog_with_targets.MutableBlock(t[0])->Op(t[1])->SetIsTarget(true);
          }
          proto::ProgramDesc pruned_desc;
          auto pruned_origin_block_id_map =
              Prune(*prog_with_targets.Proto(), feeded_var_names, &pruned_desc);
          return std::make_tuple(ProgramDesc(pruned_desc),
                                 pruned_origin_block_id_map);
        });
1129 1130 1131 1132 1133 1134
  m.def(
      "prune_backward",
      [](const framework::ProgramDesc &program) {
        return PruneBackward(program);
      },
      R"DOC(
1135 1136 1137
             Prune the backward part of a program, mostly called in
             program.clone(for_test=True).
              
1138
            Args:
1139 1140 1141 1142 1143 1144 1145 1146
                   program (ProgramDesc): The original program.

             Returns:
                   tuple(ProgramDesc, map<int, int>): The first part is 
                   the pruned program desc, and the second part is a map
                   which contains the id pair of pruned block and corresponding
                   origin block.
           )DOC");
1147 1148 1149 1150
  m.def("get_serialize_comile_key", [](int64_t compilation_key) {
#ifdef PADDLE_WITH_CINN
    auto compiler = framework::paddle2cinn::CinnCompiler::GetInstance();
    auto s = compiler->SerializeKey(compilation_key);
1151 1152
    VLOG(4) << s;
    return s;
1153 1154 1155 1156 1157 1158
#else
    PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot get compilation key in non-CINN version, "
                 "Please recompile or reinstall Paddle with CINN support."));
#endif
1159
  });
1160 1161 1162 1163
  m.def("empty_var_name",
        []() { return std::string(framework::kEmptyVarName); });
  m.def("grad_var_suffix",
        []() { return std::string(framework::kGradVarSuffix); });
1164 1165 1166
  m.def_submodule(
       "var_names",
       "The module will return special predefined variable name in Paddle")
Y
Yi Wang 已提交
1167 1168
      .def("empty", []() { return kEmptyVarName; })
      .def("temp", []() { return kTempVarName; });
1169

Q
qijun 已提交
1170
  // clang-format off
Y
Yu Yang 已提交
1171
  py::class_<paddle::platform::DeviceContext>(m, "DeviceContext")
Q
qijun 已提交
1172 1173
      .def_static("create",
                  [](paddle::platform::CPUPlace& place)
Q
qijun 已提交
1174
                      -> paddle::platform::DeviceContext* {
L
Leo Chen 已提交
1175
    auto* context = new phi::CPUContext();
W
Wilber 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    context->SetAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
        .GetAllocator(place)
        .get());
    context->SetHostAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
        .GetAllocator(paddle::platform::CPUPlace())
        .get());
    context->SetZeroAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
        .GetZeroAllocator(place)
        .get());
    return context;
Q
qijun 已提交
1189
                  })
1190 1191 1192 1193 1194 1195 1196 1197 1198
      .def_static("create",
                  [](paddle::platform::XPUPlace& place)
                      -> paddle::platform::DeviceContext* {
#ifndef PADDLE_WITH_XPU
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use XPUPlace in CPU/GPU version, "
                 "Please recompile or reinstall Paddle with XPU support."));
#else
W
Wilber 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
      auto* context = new paddle::platform::XPUDeviceContext(place);
      context->SetAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(place)
          .get());
      context->SetHostAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
      context->SetZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetZeroAllocator(place)
          .get());
      return context;
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
#endif
                  })
        .def_static("create",
                  [](paddle::platform::MLUPlace& place)
                      -> paddle::platform::DeviceContext* {
#ifndef PADDLE_WITH_MLU
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use MLUPlace in CPU/GPU version, "
                 "Please recompile or reinstall Paddle with MLU support."));
#else
                    return new paddle::platform::MLUDeviceContext(place);
1225 1226
#endif
                  })
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
        .def_static("create",
                    [](paddle::platform::NPUPlace& place)
                        -> paddle::platform::DeviceContext* {
#ifndef PADDLE_WITH_ASCEND_CL
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use NPUPlace in CPU/GPU/XPU version, "
                 "Please recompile or reinstall Paddle with NPU support."));
#else
                return new paddle::platform::NPUDeviceContext(place);
R
ronnywang 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
#endif
        })
        .def_static("create",
                    [](paddle::platform::CustomPlace& place)
                        -> paddle::platform::DeviceContext* {
#ifndef PADDLE_WITH_CUSTOM_DEVICE
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use CustomPlace in CPU/GPU/XPU version, "
                 "Please recompile or reinstall Paddle with "
                 "CustomDevice support."));
#else
                return new paddle::platform::CustomDeviceContext(place);
1250 1251
#endif
        })
Q
qijun 已提交
1252
      .def_static("create",
D
dzhwinter 已提交
1253
                  [](paddle::platform::CUDAPlace& place)
Q
qijun 已提交
1254
                      -> paddle::platform::DeviceContext* {
1255
#if !defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
1256 1257 1258 1259
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use CUDAPlace in CPU only version, "
                 "Please recompile or reinstall Paddle with CUDA support."));
Q
qijun 已提交
1260
#else
L
Leo Chen 已提交
1261
      auto* context = new phi::GPUContext(place);
W
Wilber 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
      context->SetAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(place, context->stream())
          .get());
      context->SetHostAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
      context->SetZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
        .GetZeroAllocator(place)
        .get());
W
wanghuancoder 已提交
1274 1275 1276 1277
      context->SetPinnedAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CUDAPinnedPlace())
          .get());
W
Wilber 已提交
1278 1279
      context->PartialInitWithAllocator();
      return context;
Q
qijun 已提交
1280
#endif
C
chengduoZH 已提交
1281 1282 1283 1284
                  })
          .def_static("create",
                [](paddle::platform::CUDAPinnedPlace& place)
                        -> paddle::platform::DeviceContext* {
1285
#if !defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
1286 1287 1288 1289
             PADDLE_THROW(
                 platform::errors::PermissionDenied(
                 "Cannot use CUDAPinnedPlace in CPU only version, "
                 "Please recompile or reinstall Paddle with CUDA support."));
C
chengduoZH 已提交
1290 1291 1292 1293
#else
                  return new paddle::platform::CUDAPinnedDeviceContext(place);
#endif
                });;
D
Dong Zhihong 已提交
1294
// clang-format on
1295
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
D
Dong Zhihong 已提交
1296 1297
  py::class_<platform::Communicator>(m, "Communicator").def(py::init<>());
#endif
1298 1299 1300
  m.def("get_all_device_type", []() {
    std::vector<std::string> device_types;
#ifdef PADDLE_WITH_CUSTOM_DEVICE
1301
    device_types = phi::DeviceManager::GetAllDeviceTypes();
1302
#else
R
ronnywang 已提交
1303
          VLOG(1) << string::Sprintf(
1304 1305 1306 1307
              "Cannot use get_all_device_type because you have installed"
              "CPU/GPU version PaddlePaddle.\n"
              "If you want to use get_all_device_type, please try to install"
              "CustomDevice version "
R
ronnywang 已提交
1308
              "PaddlePaddle by: pip install paddlepaddle\n");
1309 1310 1311 1312 1313 1314
#endif
    return device_types;
  });
  m.def("get_all_custom_device_type", []() {
    std::vector<std::string> device_types;
#ifdef PADDLE_WITH_CUSTOM_DEVICE
1315
    device_types = phi::DeviceManager::GetAllCustomDeviceTypes();
1316
#else
R
ronnywang 已提交
1317
          VLOG(1) << string::Sprintf(
1318 1319 1320 1321
              "Cannot use get_all_custom_device_type because you have installed"
              "CPU/GPU version PaddlePaddle.\n"
              "If you want to use get_all_custom_device_type, please try to "
              "install CustomDevice version "
R
ronnywang 已提交
1322
              "PaddlePaddle by: pip install paddlepaddle\n");
1323 1324 1325 1326 1327 1328
#endif
    return device_types;
  });
  m.def("get_available_device", [] {
    std::vector<std::string> devices;
#ifdef PADDLE_WITH_CUSTOM_DEVICE
1329
    devices = phi::DeviceManager::GetAllDeviceList();
1330
#else
R
ronnywang 已提交
1331
          VLOG(1) << string::Sprintf(
1332 1333 1334 1335
              "Cannot use get_available_device because you have installed"
              "CPU/GPU version PaddlePaddle.\n"
              "If you want to use get_available_device, please try to install"
              "CustomDevice version "
R
ronnywang 已提交
1336
              "PaddlePaddle by: pip install paddlepaddle\n");
1337 1338 1339 1340 1341 1342
#endif
    return devices;
  });
  m.def("get_available_custom_device", [] {
    std::vector<std::string> devices;
#ifdef PADDLE_WITH_CUSTOM_DEVICE
1343
    devices = phi::DeviceManager::GetAllCustomDeviceList();
1344
#else
R
ronnywang 已提交
1345
          VLOG(1) << string::Sprintf(
1346 1347 1348 1349 1350 1351
              "Cannot use get_available_custom_device because you have "
              "installed"
              "CPU/GPU version PaddlePaddle.\n"
              "If you want to use get_available_custom_device, please try to "
              "install"
              "CustomDevice version "
R
ronnywang 已提交
1352
              "PaddlePaddle by: pip install paddlepaddle\n");
1353 1354 1355
#endif
    return devices;
  });
Y
Yu Yang 已提交
1356

Y
Yu Yang 已提交
1357
  py::class_<OperatorBase>(m, "Operator")
1358 1359 1360 1361 1362 1363 1364
      .def_static("create",
                  [](py::bytes protobin) {
                    proto::OpDesc desc;
                    PADDLE_ENFORCE_EQ(desc.ParsePartialFromString(protobin),
                                      true,
                                      platform::errors::InvalidArgument(
                                          "Cannot parse user input to OpDesc"));
1365 1366
                    PADDLE_ENFORCE_EQ(desc.IsInitialized(),
                                      true,
1367 1368 1369 1370 1371 1372
                                      platform::errors::InvalidArgument(
                                          "The provided OpDesc is not "
                                          "initialized, the reason is: %s",
                                          desc.InitializationErrorString()));
                    return OpRegistry::CreateOp(desc);
                  })
1373
      .def("run",
1374 1375
           [](OperatorBase &self,
              const Scope &scope,
1376 1377 1378 1379
              const platform::CPUPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
1380
      .def("run",
1381 1382
           [](OperatorBase &self,
              const Scope &scope,
1383 1384 1385 1386
              const platform::XPUPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
1387
      .def("run",
1388 1389
           [](OperatorBase &self,
              const Scope &scope,
1390 1391 1392 1393
              const platform::NPUPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
D
dzhwinter 已提交
1394
      .def("run",
1395 1396
           [](OperatorBase &self,
              const Scope &scope,
1397 1398 1399 1400
              const platform::CUDAPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
C
chengduoZH 已提交
1401
      .def("run",
1402 1403
           [](OperatorBase &self,
              const Scope &scope,
C
chengduoZH 已提交
1404
              const platform::CUDAPinnedPlace &place) {
1405
             pybind11::gil_scoped_release release;
C
chengduoZH 已提交
1406 1407
             self.Run(scope, place);
           })
1408
      .def("run",
1409 1410
           [](OperatorBase &self,
              const Scope &scope,
1411 1412 1413 1414
              const platform::MLUPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
R
ronnywang 已提交
1415
      .def("run",
1416 1417
           [](OperatorBase &self,
              const Scope &scope,
R
ronnywang 已提交
1418 1419 1420 1421
              const platform::CustomPlace &place) {
             pybind11::gil_scoped_release release;
             self.Run(scope, place);
           })
Y
Yu Yang 已提交
1422 1423 1424 1425 1426
      .def("type",
           [](const OperatorBase &op) -> std::string { return op.Type(); })
      .def("outputs",
           [](const OperatorBase &op)
               -> std::map<std::string, std::vector<std::string>> {
1427 1428
             return op.Outputs();
           })
Q
qijun 已提交
1429 1430
      .def("output_vars",
           [](const OperatorBase &op) { return op.OutputVars(true); })
Y
Yu Yang 已提交
1431
      .def("inputs", [](const OperatorBase &op) { return op.Inputs(); })
Q
qijun 已提交
1432
      .def("input_vars", [](const OperatorBase &op) { return op.InputVars(); })
Y
Yu Yang 已提交
1433 1434 1435 1436
      .def("__str__", &OperatorBase::DebugString)
      .def("no_intermediate_outputs",
           [](const OperatorBase &op) { return op.OutputVars(false); })
      .def("support_gpu", &OperatorBase::SupportGPU);
Y
Yu Yang 已提交
1437

1438 1439 1440
  py::class_<framework::ExecutorPrepareContext>(m, "ExecutorPrepareContext")
      .def(py::init<const ProgramDesc &, size_t>());

1441 1442
  py::class_<framework::TrainerBase, std::shared_ptr<framework::TrainerBase>>(
      m, "TrainerBase")
1443 1444 1445 1446 1447 1448
      .def(
          "get_worker_scope",
          [](TrainerBase &self, int thread_id) -> Scope * {
            return self.GetWorkerScope(thread_id);
          },
          py::return_value_policy::reference)
1449 1450
      .def("finalize", &TrainerBase::Finalize)
      .def("ResetDataset", &TrainerBase::ResetDataset);
1451

1452 1453
  m.def("_get_eager_deletion_vars", &framework::GetEagerDeletionCleanVars);

F
fengjiayi 已提交
1454
  py::class_<framework::Executor>(m, "Executor")
D
dzhwinter 已提交
1455
      .def(py::init<const platform::Place &>())
Y
Yancey1989 已提交
1456
      .def("close", &Executor::Close)
1457 1458
      .def("run_from_dataset",
           &Executor::RunFromDataset,
1459
           py::call_guard<py::gil_scoped_release>())
1460 1461
      .def("release_trainer",
           &Executor::ReleaseTrainer,
D
Dong Daxiang 已提交
1462
           py::call_guard<py::gil_scoped_release>())
1463
      .def("init_for_dataset",
1464 1465 1466 1467
           [](Executor &self,
              const ProgramDesc &prog,
              const std::string &trainer_desc,
              Scope *scope,
1468
              Dataset *dataset) -> std::shared_ptr<TrainerBase> {
D
Dong Daxiang 已提交
1469
             pybind11::gil_scoped_release release;
1470 1471 1472 1473 1474 1475 1476
             return self.InitForDataset(prog, trainer_desc, scope, dataset);
           })
      .def("run_from_dataset",
           [](Executor &self, std::shared_ptr<TrainerBase> trainer) {
             pybind11::gil_scoped_release release;
             self.RunFromDataset(trainer);
           })
1477
      .def("run_prepared_ctx",
1478 1479 1480
           [](Executor &self,
              ExecutorPrepareContext *ctx,
              Scope *scope,
1481
              std::map<std::string, const LoDTensor *> *feed_targets,
1482
              std::map<std::string, FetchType *> *fetch_targets,
1483 1484
              bool create_local_scope = true,
              bool create_vars = true,
1485 1486 1487
              const std::string &feed_holder_name = "feed",
              const std::string &fetch_holder_name = "fetch") {
             pybind11::gil_scoped_release release;
1488 1489 1490 1491 1492 1493 1494 1495
             self.RunPreparedContext(ctx,
                                     scope,
                                     feed_targets,
                                     fetch_targets,
                                     create_local_scope,
                                     create_vars,
                                     feed_holder_name,
                                     fetch_holder_name);
1496
           })
1497
      .def("run_prepared_ctx",
1498 1499 1500 1501 1502
           [](Executor &self,
              ExecutorPrepareContext *ctx,
              Scope *scope,
              bool create_local_scope = true,
              bool create_vars = true,
G
guru4elephant 已提交
1503 1504
              bool keep_kids = false) {
             pybind11::gil_scoped_release release;
1505 1506
             self.RunPreparedContext(
                 ctx, scope, create_local_scope, create_vars, keep_kids);
G
guru4elephant 已提交
1507
           })
1508
      .def("prepare",
1509 1510 1511
           [](Executor &self,
              const ProgramDesc &program,
              int block_id,
1512 1513 1514 1515
              const std::vector<std::string> &skip_ref_cnt_vars =
                  std::vector<std::string>(),
              bool force_disable_gc = false) {
             pybind11::gil_scoped_release release;
1516 1517
             return self.Prepare(
                 program, block_id, skip_ref_cnt_vars, force_disable_gc);
1518 1519
           })
      .def("create_variables", &Executor::CreateVariables)
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
      .def("run",
           [](Executor &self,
              const ProgramDesc &prog,
              Scope *scope,
              int block_id,
              bool create_local_scope,
              bool create_vars,
              const std::vector<std::string> &fetch_vars) {
             pybind11::gil_scoped_release release;
             self.Run(prog,
                      scope,
                      block_id,
                      create_local_scope,
                      create_vars,
                      fetch_vars);
           });
S
sneaxiy 已提交
1536

1537
  py::class_<framework::interpreter::CostInfo>(m, "CostInfo")
1538
      .def(py::init<>())
1539 1540 1541 1542 1543
      .def("total_time",
           [](interpreter::CostInfo &self) { return self.total_time; })
      .def("device_memory_bytes", [](interpreter::CostInfo &self) {
        return self.device_memory_bytes;
      });
1544

1545
  py::class_<framework::StandaloneExecutor>(m, "StandaloneExecutor")
L
Leo Chen 已提交
1546
      .def(py::init<const platform::Place &, const ProgramDesc &>())
1547
      .def("run",
1548
           [](StandaloneExecutor &self,
1549
              Scope *scope,
1550
              std::vector<std::string> feed_names,
1551 1552 1553 1554
              std::vector<std::string> fetch_names) {
             paddle::framework::FetchList ret;
             {
               pybind11::gil_scoped_release release;
1555
               ret = self.Run(scope, feed_names, fetch_names);
1556 1557 1558
             }
             return py::cast(std::move(ret));
           })
1559 1560
      .def("dry_run",
           [](StandaloneExecutor &self,
1561
              Scope *scope,
1562
              const std::unordered_map<std::string, py::array> &input_dict) {
1563
             std::vector<framework::LoDTensor> feed_tensors;
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
             std::vector<std::string> feed_names;

             for (auto &item : input_dict) {
               framework::LoDTensor t;
               SetTensorFromPyArray<platform::CPUPlace>(
                   &t, item.second, platform::CPUPlace(), false);
               feed_names.push_back(item.first);
               feed_tensors.push_back(t);
             }

1574
             framework::interpreter::CostInfo cost_info;
1575 1576
             {
               pybind11::gil_scoped_release release;
1577
               cost_info = self.DryRun(scope, feed_names, feed_tensors);
1578 1579
             }
             return cost_info;
H
hong 已提交
1580 1581
           });

D
dzhwinter 已提交
1582
  m.def("init_gflags", framework::InitGflags);
Y
Yang Yu 已提交
1583
  m.def("init_glog", framework::InitGLOG);
1584 1585 1586 1587
  m.def("load_op_meta_info_and_register_op", [](const std::string dso_name) {
    egr::Controller::Instance().MergeOpMetaInfoMap(
        framework::LoadOpMetaInfoAndRegisterOp(dso_name));
  });
1588
  m.def("init_devices", []() { framework::InitDevices(); });
1589 1590
  m.def("init_default_kernel_signatures",
        []() { framework::InitDefaultKernelSignatureMap(); });
1591
  m.def("is_compiled_with_cuda", IsCompiledWithCUDA);
1592
  m.def("is_compiled_with_ascend", IsCompiledWithAscend);
1593
  m.def("is_compiled_with_rocm", IsCompiledWithROCM);
1594
  m.def("is_compiled_with_npu", IsCompiledWithNPU);
J
jianghaicheng 已提交
1595
  m.def("is_compiled_with_ipu", IsCompiledWithIPU);
1596
  m.def("is_compiled_with_xpu", IsCompiledWithXPU);
1597
  m.def("is_compiled_with_mkldnn", IsCompiledWithMKLDNN);
1598
  m.def("is_compiled_with_nccl", IsCompiledWithNCCL);
1599
  m.def("is_compiled_with_cinn", IsCompiledWithCINN);
1600
  m.def("is_compiled_with_mlu", IsCompiledWithMLU);
1601
  m.def("_is_compiled_with_heterps", IsCompiledWithHETERPS);
1602
  m.def("supports_bfloat16", SupportsBfloat16);
1603
  m.def("supports_bfloat16_fast_performance", SupportsBfloat16FastPerformance);
1604 1605
  m.def("supports_int8", SupportsInt8);
  m.def("supports_vnni", SupportsVNNI);
L
Leo Chen 已提交
1606
  m.def("op_supported_infos", imperative::OpSupportedInfos);
1607
  m.def("is_compiled_with_brpc", IsCompiledWithBrpc);
Y
update  
Yancey1989 已提交
1608
  m.def("is_compiled_with_dist", IsCompiledWithDIST);
1609 1610 1611
  m.def("_cuda_synchronize", [](const platform::CUDAPlace &place) {
    platform::DeviceContextPool::Instance().Get(place)->Wait();
  });
H
hutuxian 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630

  m.def("get_float_stats", []() {
    std::vector<paddle::platform::ExportedStatValue<float>> float_stats;
    paddle::platform::StatRegistry<float>::Instance().publish(float_stats);
    std::unordered_map<std::string, float> stats_map;
    for (const auto &stat : float_stats) {
      stats_map[stat.key] = stat.value;
    }
    return stats_map;
  });
  m.def("get_int_stats", []() {
    std::vector<paddle::platform::ExportedStatValue<int64_t>> int_stats;
    paddle::platform::StatRegistry<int64_t>::Instance().publish(int_stats);
    std::unordered_map<std::string, int64_t> stats_map;
    for (const auto &stat : int_stats) {
      stats_map[stat.key] = stat.value;
    }
    return stats_map;
  });
1631 1632 1633
  m.def("device_memory_stat_current_value",
        memory::DeviceMemoryStatCurrentValue);
  m.def("device_memory_stat_peak_value", memory::DeviceMemoryStatPeakValue);
1634 1635
  m.def(
      "run_cmd",
1636 1637
      [](const std::string &cmd,
         int time_out = -1,
1638
         int sleep_inter = -1) -> const std::string {
1639 1640
        return paddle::framework::shell_get_command_output(
            cmd, time_out, sleep_inter);
1641
      },
1642 1643 1644
      py::arg("cmd"),
      py::arg("time_out") = -1,
      py::arg("sleep_inter") = -1);
1645 1646
  m.def(
      "shell_execute_cmd",
1647 1648 1649
      [](const std::string &cmd,
         int time_out = 0,
         int sleep_inter = 0,
1650
         bool redirect_stderr = false) -> std::vector<std::string> {
1651 1652
        return paddle::framework::shell_execute_cmd(
            cmd, time_out, sleep_inter, redirect_stderr);
1653
      },
1654 1655 1656
      py::arg("cmd"),
      py::arg("time_out") = 0,
      py::arg("sleep_inter") = 0,
1657
      py::arg("redirect_stderr") = false);
G
gongweibao 已提交
1658

1659
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1660 1661
  m.def("is_float16_supported", [](const platform::CUDAPlace &place) -> bool {
    // Only GPUs with Compute Capability >= 53 support float16
1662
    return platform::GetGPUComputeCapability(place.device) >= 53;
1663
  });
1664 1665 1666 1667
  m.def("is_bfloat16_supported", [](const platform::CUDAPlace &place) -> bool {
    // Only GPUs with Compute Capability >= 80 support bfloat16
    return platform::GetGPUComputeCapability(place.device) >= 80;
  });
1668
#endif
1669

S
Steffy-zxf 已提交
1670
  m.def("set_feed_variable",
1671 1672 1673 1674 1675
        static_cast<void (*)(  // NOLINT
            Scope *,
            const LoDTensor &,
            const std::string &,
            size_t)>(&framework::SetFeedVariable));
S
Steffy-zxf 已提交
1676
  m.def("set_feed_variable",
1677 1678 1679 1680 1681
        static_cast<void (*)(  // NOLINT
            Scope *,
            const Strings &,
            const std::string &,
            size_t)>(&framework::SetFeedVariable));
1682
  m.def("get_fetch_variable",
1683 1684
        [](const Scope &scope,
           const std::string &var_name,
1685 1686 1687
           size_t index) -> py::object {
          auto &var = framework::GetFetchVariable(scope, var_name, index);
          if (data_is_lod_tensor(var)) {
R
Ruibiao Chen 已提交
1688
            return py::cast(PADDLE_GET(LoDTensor, var));
1689
          } else {
R
Ruibiao Chen 已提交
1690
            return py::cast(PADDLE_GET(LoDTensorArray, var));
1691 1692
          }
        });
1693
  m.def("get_variable_tensor", framework::GetVariableTensor);
Q
qijun 已提交
1694

X
Xin Pan 已提交
1695 1696
  m.def("_is_program_version_supported", IsProgramVersionSupported);

1697 1698 1699 1700
  BindProgramDesc(&m);
  BindBlockDesc(&m);
  BindVarDsec(&m);
  BindOpDesc(&m);
H
Huihuang Zheng 已提交
1701
  BindCostModel(&m);
1702
  BindConstValue(&m);
1703
  BindGlobalValueGetterSetter(&m);
1704
  BindProcessMeshDesc(&m);
L
LiYuRio 已提交
1705
  BindFleetExecutor(&m);
1706
  BindTCPStore(&m);
1707
  BindJitProperty(&m);
Y
Yu Yang 已提交
1708

Y
Yu Yang 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717
  py::class_<framework::LoDRankTable>(m, "LodRankTable")
      .def("items", [](framework::LoDRankTable &table) {
        std::vector<std::pair<size_t, size_t>> res;
        for (auto &item : table.items()) {
          res.push_back({item.index, item.length});
        }
        return res;
      });

1718
  py::class_<LoDTensorArray> pylodtensorarray(m, "LoDTensorArray", R"DOC(
1719
    LoDTensorArray is array of LoDTensor, it supports operator[], len() and for-loop iteration.
Z
Zeng Jinle 已提交
1720 1721 1722

    Examples:
        .. code-block:: python
1723

Z
Zeng Jinle 已提交
1724 1725 1726
          import paddle.fluid as fluid

          arr = fluid.LoDTensorArray()
1727 1728 1729 1730
)DOC");
  g_framework_lodtensorarray_pytype =
      reinterpret_cast<PyTypeObject *>(pylodtensorarray.ptr());
  pylodtensorarray
S
sneaxiy 已提交
1731 1732
      .def("__init__",
           [](LoDTensorArray &instance) { new (&instance) LoDTensorArray(); })
1733 1734 1735 1736
      .def(
          "__getitem__",
          [](LoDTensorArray &self, size_t i) { return &self.at(i); },
          py::return_value_policy::reference)
Y
Yu Yang 已提交
1737 1738 1739
      .def("__len__", [](LoDTensorArray &self) { return self.size(); })
      .def("__setitem__",
           [](LoDTensorArray &self, size_t i, const LoDTensor &t) {
1740 1741
             PADDLE_ENFORCE_LT(i,
                               self.size(),
1742 1743 1744
                               platform::errors::InvalidArgument(
                                   "The index to set is larger than the size "
                                   "of LoDTensorArray."));
Y
Yu Yang 已提交
1745 1746 1747
             self[i].ShareDataWith(t);
             self[i].set_lod(t.lod());
           })
1748 1749 1750 1751 1752 1753 1754
      .def(
          "append",
          [](LoDTensorArray &self, const LoDTensor &t) {
            self.emplace_back();
            self.back().ShareDataWith(t);
            self.back().set_lod(t.lod());
          },
1755 1756
          py::arg("tensor"),
          R"DOC(
Z
Zeng Jinle 已提交
1757
             Append a LoDensor to LoDTensorArray.
1758 1759 1760 1761 1762 1763
              
             Args:
                   tensor (LoDTensor): The LoDTensor to be appended.

             Returns:
                   None.
Z
Zeng Jinle 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

             Examples:
                 .. code-block:: python

                   import paddle.fluid as fluid
                   import numpy as np

                   arr = fluid.LoDTensorArray()
                   t = fluid.LoDTensor()
                   t.set(np.ndarray([5, 30]), fluid.CPUPlace())
                   arr.append(t)
1775
           )DOC")
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
      .def(
          "_move_to_list",
          [](LoDTensorArray &self) -> py::list {
            py::list res(self.size());
            for (size_t i = 0; i < self.size(); ++i) {
              res[i] = py::cast(std::move(self[i]));
            }
            self.clear();
            return res;
          },
          py::return_value_policy::take_ownership);
Y
Yu Yang 已提交
1787

1788
  py::class_<FetchList>(m, "FetchList", R"DOC( FetchList is a
R
Ruibiao Chen 已提交
1789
        vector of paddle::variant<LoDTensor, LoDTensorArray>.
1790
        )DOC")
1791 1792 1793 1794 1795 1796
      .def(
          "_move_to_list",
          [](FetchList &self) -> py::list {
            py::list res(self.size());
            for (size_t i = 0; i < self.size(); ++i) {
              if (data_is_lod_tensor(self[i])) {
R
Ruibiao Chen 已提交
1797
                auto &data = PADDLE_GET(LoDTensor, self[i]);
1798 1799
                res[i] = py::cast(std::move(data));
              } else {
R
Ruibiao Chen 已提交
1800
                auto &data = PADDLE_GET(LoDTensorArray, self[i]);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
                py::list tmp(data.size());
                for (size_t j = 0; j < data.size(); ++j) {
                  tmp[j] = py::cast(std::move(data[j]));
                }
                res[i] = std::move(tmp);
              }
            }
            self.clear();
            return res;
          },
          py::return_value_policy::take_ownership)
1812

1813 1814 1815 1816
      .def(
          "append",
          [](FetchList &self, const LoDTensor &t) {
            self.emplace_back();
R
Ruibiao Chen 已提交
1817
            auto &lod_tensor = PADDLE_GET(LoDTensor, self.back());
1818 1819 1820 1821 1822 1823 1824 1825 1826
            lod_tensor.ShareDataWith(t);
            lod_tensor.set_lod(t.lod());
          },
          py::arg("var"))

      .def(
          "append",
          [](FetchList &self, const LoDTensorArray &t) {
            self.emplace_back();
R
Ruibiao Chen 已提交
1827
            auto &lod_tensor_array = PADDLE_GET(LoDTensorArray, self.back());
1828 1829 1830 1831 1832 1833
            for (size_t i = 0; i < t.size(); ++i) {
              lod_tensor_array[i].ShareDataWith(t[i]);
              lod_tensor_array[i].set_lod(t[i].lod());
            }
          },
          py::arg("var"));
1834 1835

  py::class_<FetchUnmergedList>(m, "FetchUnmergedList", R"DOC(
R
Ruibiao Chen 已提交
1836
        FetchUnmergedList is 2-D array of FetchType(paddle::variant(LoDTensor, LoDTensorArray)).
Z
Zhen Wang 已提交
1837
        )DOC")
1838 1839 1840 1841 1842 1843 1844 1845
      .def(
          "_move_to_list",
          [](FetchUnmergedList &self) -> py::list {
            py::list res(self.size());
            for (size_t i = 0; i < self.size(); ++i) {
              py::list tmp(self[i].size());
              for (size_t j = 0; j < self[i].size(); ++j) {
                if (data_is_lod_tensor(self[i][j])) {
R
Ruibiao Chen 已提交
1846
                  auto &var = PADDLE_GET(LoDTensor, self[i][j]);
1847 1848
                  tmp[j] = py::cast(std::move(var));
                } else {
R
Ruibiao Chen 已提交
1849
                  auto &var = PADDLE_GET(LoDTensorArray, self[i][j]);
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
                  py::list tmp_array(var.size());
                  for (size_t k = 0; k < var.size(); ++k) {
                    tmp_array[k] = std::move(var[k]);
                  }
                  tmp[j] = std::move(tmp_array);
                }
              }
              res[i] = std::move(tmp);
              self[i].clear();
            }
            self.clear();
            return res;
          },
          py::return_value_policy::take_ownership);
Z
Zhen Wang 已提交
1864

Y
Yu Yang 已提交
1865
  m.def("op_support_gpu", OpSupportGPU);
1866
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1867
  m.def("get_cuda_device_count", platform::GetGPUDeviceCount);
1868
  m.def("get_cuda_current_device_id", &platform::GetCurrentDeviceId);
1869 1870 1871 1872 1873 1874 1875 1876
  m.def("cuda_empty_cache", [] {
    for (int dev_id : platform::GetSelectedDevices()) {
      auto *dev_ctx = platform::DeviceContextPool::Instance().GetByPlace(
          platform::CUDAPlace(dev_id));
      dev_ctx->cudnn_workspace_handle().ResetWorkspace();
    }
    platform::EmptyCache();
  });
1877 1878 1879 1880 1881 1882
  m.def(
      "get_device_properties",
      [](int id) -> const gpuDeviceProp & {
        return platform::GetDeviceProperties(id);
      },
      py::return_value_policy::copy);
1883 1884

  py::class_<gpuDeviceProp>(m, "_gpuDeviceProperties")
Y
Yanxing Shi 已提交
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
      .def_property_readonly(
          "name", [](const gpuDeviceProp &prop) { return prop.name; })
      .def_property_readonly(
          "major", [](const gpuDeviceProp &prop) { return prop.major; })
      .def_property_readonly(
          "minor", [](const gpuDeviceProp &prop) { return prop.minor; })
      .def_property_readonly(
          "total_memory",
          [](const gpuDeviceProp &prop) { return prop.totalGlobalMem; })
      .def_property_readonly(
          "multi_processor_count",
          [](const gpuDeviceProp &prop) { return prop.multiProcessorCount; })
      .def_property_readonly(
          "is_multi_gpu_board",
          [](const gpuDeviceProp &prop) { return prop.isMultiGpuBoard; })
      .def_property_readonly(
          "is_integrated",
          [](const gpuDeviceProp &prop) { return prop.integrated; })
      .def("__repr__", [](const gpuDeviceProp &prop) {
        std::stringstream ostr;
        ostr << "_gpuDeviceProperties(name='" << prop.name
             << "', major=" << prop.major << ", minor=" << prop.minor
             << ", total_memory=" << prop.totalGlobalMem / (1024 * 1024)
             << "MB, multi_processor_count=" << prop.multiProcessorCount << ")";
        return ostr.str();
1910
      });
D
dangqingqing 已提交
1911

1912
#if !defined(PADDLE_WITH_HIP) && !defined(_WIN32)
D
dangqingqing 已提交
1913 1914 1915
  m.def("nvprof_init", platform::CudaProfilerInit);
  m.def("nvprof_start", platform::CudaProfilerStart);
  m.def("nvprof_stop", platform::CudaProfilerStop);
1916 1917 1918 1919
  m.def("nvprof_nvtx_push", platform::CudaNvtxRangePush);
  m.def("nvprof_nvtx_pop", platform::CudaNvtxRangePop);
  m.def("nvprof_enable_record_event", platform::NvprofEnableRecordEvent);
  m.def("nvprof_disable_record_event", platform::NvprofDisableRecordEvent);
D
Dong Zhihong 已提交
1920
#endif
P
peizhilin 已提交
1921
#endif
Y
Yu Yang 已提交
1922

1923 1924
#ifdef PADDLE_WITH_ASCEND_CL
  m.def("get_npu_device_count", platform::GetNPUDeviceCount);
1925
  m.def("npu_finalize", []() {
1926 1927
    platform::HCCLCommContext::Instance().ReleaseHCCLComms();

1928 1929 1930
    auto &pool = platform::DeviceContextPool::Instance();
    auto devices = platform::GetSelectedNPUDevices();
    for (size_t i = 0; i < devices.size(); ++i) {
R
ronnywang 已提交
1931
      platform::NPUDeviceGuard guard(devices[i]);
1932 1933 1934 1935
      pool.Get(platform::NPUPlace(devices[i]))->Wait();
    }
    platform::AclInstance::Instance().Finalize();
  });
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955

  py::class_<platform::NPUProfConfigWrapper>(m, "NPUProfConfigWrapper");

  m.def("npu_prof_init", platform::NPUProfilerInit);
  m.def("npu_prof_start", [](platform::NPUProfConfigWrapper c) {
    platform::NPUProfilerStart(c.ptr());
  });
  m.def("npu_prof_stop", [](platform::NPUProfConfigWrapper c) {
    platform::NPUProfilerStop(c.ptr());
  });
  m.def("npu_prof_finalize", platform::NPUProfilerFinalize);
  m.def("npu_prof_create_config", []() {
    return platform::NPUProfConfigWrapper(platform::NPUProfilerCreateConfig());
  });

  m.def("npu_prof_destropy_config", [](platform::NPUProfConfigWrapper c) {
    platform::NPUProfilerDestroyConfig(c.ptr());
  });
#endif

J
jianghaicheng 已提交
1956 1957 1958 1959
#ifdef PADDLE_WITH_IPU
  m.def("get_ipu_device_count", platform::GetIPUDeviceCount);
#endif

1960 1961 1962 1963
#ifdef PADDLE_WITH_MLU
  m.def("get_mlu_device_count", platform::GetMLUDeviceCount);
#endif

1964 1965 1966 1967 1968 1969
  py::enum_<platform::TracerOption>(m, "TracerOption", py::arithmetic())
      .value("kDefault", platform::TracerOption::kDefault)
      .value("kOpDetail", platform::TracerOption::kOpDetail)
      .value("kAllOpDetail", platform::TracerOption::kAllOpDetail)
      .export_values();

1970 1971 1972 1973
  py::enum_<platform::ProfilerState>(m, "ProfilerState", py::arithmetic())
      .value("kDisabled", platform::ProfilerState::kDisabled)
      .value("kCPU", platform::ProfilerState::kCPU)
      .value("kCUDA", platform::ProfilerState::kCUDA)
1974
      .value("kAll", platform::ProfilerState::kAll)
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
      .export_values();

  py::enum_<platform::EventSortingKey>(m, "EventSortingKey", py::arithmetic())
      .value("kDefault", platform::EventSortingKey::kDefault)
      .value("kCalls", platform::EventSortingKey::kCalls)
      .value("kTotal", platform::EventSortingKey::kTotal)
      .value("kMin", platform::EventSortingKey::kMin)
      .value("kMax", platform::EventSortingKey::kMax)
      .value("kAve", platform::EventSortingKey::kAve)
      .export_values();

1986
  m.def("set_tracer_option", platform::SetTracerOption);
1987 1988
  m.def("enable_profiler", platform::EnableProfiler);
  m.def("disable_profiler", platform::DisableProfiler);
X
Xin Pan 已提交
1989
  m.def("is_profiler_enabled", platform::IsProfileEnabled);
1990
  m.def("reset_profiler", platform::ResetProfiler);
W
wuhuanzhou 已提交
1991
  m.def("register_pass", [](const std::string &pass_type, py::object callable) {
1992
    PADDLE_ENFORCE_EQ(
1993 1994
        framework::ir::PassRegistry::Instance().Has(pass_type),
        false,
1995 1996 1997
        platform::errors::AlreadyExists("Pass '%s' is registered more than "
                                        "once. Please use another name.",
                                        pass_type));
W
wuhuanzhou 已提交
1998
    callable.inc_ref();
1999 2000 2001 2002 2003 2004 2005 2006
    framework::ir::PassRegistry::Instance().Insert(
        pass_type, [pass_type, callable]() {
          py::gil_scoped_acquire guard;
          std::unique_ptr<framework::ir::Pass> pass(
              new framework::ir::GeneratePass(
                  py::cast<std::string>(callable())));
          return pass;
        });
2007
  });
2008
  m.def("get_pass", [](const std::string &pass_type) {
W
WangZhen 已提交
2009 2010 2011
    auto pass = framework::ir::PassRegistry::Instance().Get(pass_type);
    return std::shared_ptr<framework::ir::Pass>(std::move(pass));
  });
Y
Yu Yang 已提交
2012

2013
  m.def("size_of_dtype", framework::SizeOfType);
C
chenjian 已提交
2014 2015
  py::class_<paddle::platform::ProfilerResult>(m, "_ProfilerResult")
      .def(py::init<>())
2016 2017
      .def("get_data",
           &paddle::platform::ProfilerResult::GetData,
C
chenjian 已提交
2018 2019 2020 2021
           py::return_value_policy::automatic_reference)
      .def("save", &paddle::platform::ProfilerResult::Save)
      .def("get_extra_info", &paddle::platform::ProfilerResult::GetExtraInfo);

2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
  py::class_<paddle::platform::MemPythonNode>(m, "MemPythonNode")
      .def(py::init<>())
      .def_readwrite("timestamp_ns",
                     &paddle::platform::MemPythonNode::timestamp_ns)
      .def_readwrite("addr", &paddle::platform::MemPythonNode::addr)
      .def_readwrite("type", &paddle::platform::MemPythonNode::type)
      .def_readwrite("process_id", &paddle::platform::MemPythonNode::process_id)
      .def_readwrite("thread_id", &paddle::platform::MemPythonNode::thread_id)
      .def_readwrite("increase_bytes",
                     &paddle::platform::MemPythonNode::increase_bytes)
      .def_readwrite("place", &paddle::platform::MemPythonNode::place)
      .def_readwrite("current_allocated",
                     &paddle::platform::MemPythonNode::current_allocated)
      .def_readwrite("current_reserved",
                     &paddle::platform::MemPythonNode::current_reserved)
      .def_readwrite("peak_allocated",
                     &paddle::platform::MemPythonNode::peak_allocated)
      .def_readwrite("peak_reserved",
                     &paddle::platform::MemPythonNode::peak_reserved);

C
chenjian 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
  py::class_<paddle::platform::DevicePythonNode>(m, "DevicePythonNode")
      .def(py::init<>())
      .def_readwrite("name", &paddle::platform::DevicePythonNode::name)
      .def_readwrite("type", &paddle::platform::DevicePythonNode::type)
      .def_readwrite("start_ns", &paddle::platform::DevicePythonNode::start_ns)
      .def_readwrite("end_ns", &paddle::platform::DevicePythonNode::end_ns)
      .def_readwrite("device_id",
                     &paddle::platform::DevicePythonNode::device_id)
      .def_readwrite("context_id",
                     &paddle::platform::DevicePythonNode::context_id)
      .def_readwrite("stream_id",
                     &paddle::platform::DevicePythonNode::stream_id);

  py::class_<paddle::platform::HostPythonNode>(m, "HostPythonNode")
      .def(py::init<>())
      .def_readwrite("name", &paddle::platform::HostPythonNode::name)
      .def_readwrite("type", &paddle::platform::HostPythonNode::type)
      .def_readwrite("start_ns", &paddle::platform::HostPythonNode::start_ns)
      .def_readwrite("end_ns", &paddle::platform::HostPythonNode::end_ns)
      .def_readwrite("process_id",
                     &paddle::platform::HostPythonNode::process_id)
      .def_readwrite("thread_id", &paddle::platform::HostPythonNode::thread_id)
2064 2065 2066 2067
      .def_readwrite("input_shapes",
                     &paddle::platform::HostPythonNode::input_shapes)
      .def_readwrite("dtypes", &paddle::platform::HostPythonNode::dtypes)
      .def_readwrite("callstack", &paddle::platform::HostPythonNode::callstack)
C
chenjian 已提交
2068 2069 2070 2071 2072
      .def_readwrite("children_node",
                     &paddle::platform::HostPythonNode::children_node_ptrs)
      .def_readwrite("runtime_node",
                     &paddle::platform::HostPythonNode::runtime_node_ptrs)
      .def_readwrite("device_node",
2073 2074 2075
                     &paddle::platform::HostPythonNode::device_node_ptrs)
      .def_readwrite("mem_node",
                     &paddle::platform::HostPythonNode::mem_node_ptrs);
C
chenjian 已提交
2076 2077

  py::class_<paddle::platform::Profiler>(m, "_Profiler")
2078 2079
      .def("create",
           &paddle::platform::Profiler::Create,
C
chenjian 已提交
2080
           py::return_value_policy::take_ownership)
C
chenjian 已提交
2081
      .def("is_cupti_supported", &paddle::platform::Profiler::IsCuptiSupported)
F
fwenguang 已提交
2082 2083
      .def("is_cnpapi_supported",
           &paddle::platform::Profiler::IsCnpapiSupported)
C
chenjian 已提交
2084 2085 2086 2087 2088 2089
      .def("prepare",
           [](paddle::platform::Profiler *profiler) {
             platform::EnableHostEventRecorder();
             profiler->Prepare();
           })
      .def("start", &paddle::platform::Profiler::Start)
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
      .def(
          "stop",
          [](paddle::platform::Profiler *profiler) {
            platform::DisableHostEventRecorder();
            auto result = profiler->Stop();
            framework::StaticGraphExecutorPerfStatistics(
                result->GetNodeTrees());
            return result;
          },
          py::return_value_policy::automatic_reference);
C
chenjian 已提交
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

  py::class_<paddle::platform::ProfilerOptions>(m, "ProfilerOptions")
      .def(py::init<>())
      .def_readwrite("trace_switch",
                     &paddle::platform::ProfilerOptions::trace_switch);

  py::class_<platform::RecordEvent>(m, "_RecordEvent")
      .def(py::init([](std::string name, platform::TracerEventType type) {
        return std::make_unique<platform::RecordEvent>(
            name, type, 1, paddle::platform::EventRole::kOrdinary);
      }))
      .def("end", [](platform::RecordEvent *event) { event->End(); });

2113 2114 2115 2116 2117 2118 2119 2120
  py::enum_<paddle::platform::TracerMemEventType>(m, "TracerMemEventType")
      .value("Allocate", paddle::platform::TracerMemEventType::Allocate)
      .value("Free", paddle::platform::TracerMemEventType::Free)
      .value("ReservedAllocate",
             paddle::platform::TracerMemEventType::ReservedAllocate)
      .value("ReservedFree",
             paddle::platform::TracerMemEventType::ReservedFree);

C
chenjian 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
  py::enum_<paddle::platform::TracerEventType>(m, "TracerEventType")
      .value("Operator", paddle::platform::TracerEventType::Operator)
      .value("Dataloader", paddle::platform::TracerEventType::Dataloader)
      .value("ProfileStep", paddle::platform::TracerEventType::ProfileStep)
      .value("CudaRuntime", paddle::platform::TracerEventType::CudaRuntime)
      .value("Kernel", paddle::platform::TracerEventType::Kernel)
      .value("Memcpy", paddle::platform::TracerEventType::Memcpy)
      .value("Memset", paddle::platform::TracerEventType::Memset)
      .value("UserDefined", paddle::platform::TracerEventType::UserDefined)
      .value("OperatorInner", paddle::platform::TracerEventType::OperatorInner)
      .value("Forward", paddle::platform::TracerEventType::Forward)
      .value("Backward", paddle::platform::TracerEventType::Backward)
      .value("Optimization", paddle::platform::TracerEventType::Optimization)
      .value("Communication", paddle::platform::TracerEventType::Communication)
      .value("PythonOp", paddle::platform::TracerEventType::PythonOp)
      .value("PythonUserDefined",
             paddle::platform::TracerEventType::PythonUserDefined);
  m.def("load_profiler_result", &paddle::platform::LoadProfilerResult);
2139

2140
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
2141 2142
  m.def("set_cublas_switch", platform::SetAllowTF32Cublas);
  m.def("get_cublas_switch", platform::AllowTF32Cublas);
A
AshburnLee 已提交
2143 2144
  m.def("set_cudnn_switch", platform::SetAllowTF32Cudnn);
  m.def("get_cudnn_switch", platform::AllowTF32Cudnn);
2145
#endif  // PADDLE_WITH_CUDA
2146 2147
  m.def("clear_executor_cache",
        []() { framework::ExecutorInfoCache::Instance().Finalize(); });
2148

J
jianghaicheng 已提交
2149 2150
#ifdef PADDLE_WITH_IPU
  py::class_<platform::ipu::IpuBackend,
2151 2152 2153
             std::unique_ptr<platform::ipu::IpuBackend, py::nodelete>>(
      m, "IpuBackend")
      // manage IpuBackend in C++
2154 2155 2156 2157 2158 2159 2160
      .def(
          "get_instance",
          []() {
            return std::unique_ptr<platform::ipu::IpuBackend, py::nodelete>(
                platform::ipu::IpuBackend::GetInstance());
          },
          py::return_value_policy::reference)
A
Allen Guo 已提交
2161
      .def("weights_to_host", &platform::ipu::IpuBackend::WeightsToHost)
2162 2163
      .def("detach", &platform::ipu::IpuBackend::Detach)
      .def("reset", &platform::ipu::IpuBackend::Reset)
J
jianghaicheng 已提交
2164
      .def("set_scope", &platform::ipu::IpuBackend::SetScope)
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
      .def("set_ipu_strategy", &platform::ipu::IpuBackend::SetIpuStrategy)
      .def("save_model_proto", &platform::ipu::IpuBackend::SaveModelProto);

  py::class_<platform::ipu::IpuStrategy>(m, "IpuStrategy")
      .def(py::init())
      .def("set_options",
           [](platform::ipu::IpuStrategy &self, const py::dict &opt) {
             for (auto element : opt) {
               auto option_name = element.first.cast<std::string>();
               VLOG(10) << "Set option: " << option_name;
A
Allen Guo 已提交
2175 2176 2177 2178
               if (option_name == "compilation_progress_logger") {
                 self.SetCompilationProgressLogger(
                     element.second.cast<py::function>());
               } else if (py::isinstance<py::bool_>(element.second)) {
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
                 self.AddBoolOption(option_name, element.second.cast<bool>());
               } else if (py::isinstance<py::float_>(element.second)) {
                 self.AddDoubleOption(option_name,
                                      element.second.cast<double>());
               } else if (py::isinstance<py::int_>(element.second)) {
                 self.AddUint64Option(option_name,
                                      element.second.cast<std::uint64_t>());
               } else if (py::isinstance<py::str>(element.second)) {
                 self.AddStringOption(option_name,
                                      element.second.cast<std::string>());
               } else if (py::isinstance<py::set>(element.second) ||
                          py::isinstance<py::list>(element.second)) {
                 for (auto option : element.second.cast<py::list>()) {
                   std::string option_val;
                   if (py::isinstance<py::str>(option)) {
                     option_val = option.cast<std::string>();
                   } else if (py::isinstance<py::int_>(option)) {
                     option_val = std::to_string(option.cast<std::uint64_t>());
                   } else {
                     PADDLE_THROW(platform::errors::Unimplemented(
                         "Failed to convert type: %s when set IpuStrategy "
                         "option: %s",
2201 2202
                         option.get_type(),
                         option_name));
2203 2204 2205 2206 2207 2208 2209
                   }
                   self.InsertStringOption(option_name, option_val);
                 }
               } else if (py::isinstance<py::dict>(element.second)) {
                 if (option_name.rfind("location_", 0) == 0) {
                   for (auto option : element.second.cast<py::dict>()) {
                     self.SetTensorLocation(
2210 2211
                         option_name,
                         option.first.cast<std::string>(),
2212 2213
                         option.second.cast<std::uint64_t>());
                   }
2214 2215 2216 2217 2218 2219
                 } else if (option_name == "replicated_collectives_settings") {
                   for (auto option : element.second.cast<py::dict>()) {
                     self.SetReplicatedCollectivesSettings(
                         option.first.cast<std::string>(),
                         option.second.cast<bool>());
                   }
A
Allen Guo 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228
                 } else if (option_name == "accumulate_outer_fragment") {
                   for (auto option : element.second.cast<py::dict>()) {
                     std::vector<int> values;
                     for (auto value : option.second.cast<py::list>()) {
                       values.push_back(value.cast<int>());
                     }
                     self.SetAccumulateOuterFragmentSettings(
                         option.first.cast<std::uint64_t>(), values);
                   }
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
                 } else if (option_name == "custom_op") {
                   std::string paddle_op;
                   std::string popart_op;
                   std::string domain;
                   int version = -1;
                   for (auto option : element.second.cast<py::dict>()) {
                     std::string option_key = option.first.cast<std::string>();
                     if (option_key == "paddle_op") {
                       paddle_op = option.second.cast<std::string>();
                     } else if (option_key == "popart_op") {
                       popart_op = option.second.cast<std::string>();
                     } else if (option_key == "domain") {
                       domain = option.second.cast<std::string>();
                     } else if (option_key == "version") {
                       version = option.second.cast<int>();
                     } else {
                       PADDLE_THROW(platform::errors::InvalidArgument(
                           "Invalid argument, key must be one of paddle_op, "
                           "popart_op, domain or version, but revecived %s",
                           option_key));
                     }
                   }
                   self.AddCustomOp(paddle_op, popart_op, domain, version);
                 } else {
                   for (auto option : element.second.cast<py::dict>()) {
                     std::string option_key = option.first.cast<std::string>();
                     std::string option_val;
                     if (py::isinstance<py::str>(option.second)) {
                       option_val = option.second.cast<std::string>();
                     } else if (py::isinstance<py::int_>(option.second)) {
                       option_val =
                           std::to_string(option.second.cast<std::uint64_t>());
                     } else {
                       PADDLE_THROW(platform::errors::Unimplemented(
                           "Failed to convert value type: %s when set "
                           "IpuStrategy option: %s",
2265 2266
                           option.second.get_type(),
                           option_key));
2267
                     }
2268 2269
                     self.InsertStringPairOption(
                         option_name, option_key, option_val);
2270 2271 2272 2273 2274 2275
                   }
                 }
               } else {
                 PADDLE_THROW(platform::errors::InvalidArgument(
                     "Invalid IpuStrategy option value type: %s, please check "
                     "input value for option: %s",
2276 2277
                     element.second.get_type(),
                     option_name));
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
               }
             }
           })
      .def("get_option",
           [](platform::ipu::IpuStrategy &self, const std::string &name) {
             py::dict res;
             auto option_type = self.GetOptionType(name);
             res["name"] = name;
             res["type"] = option_type;
             if (option_type == "vector") {
               auto value = self.GetVectorOption(name);
               res["value"] = value;
             } else if (option_type == "map") {
               auto value = self.GetMapOption(name);
               res["value"] = value;
             } else {
               auto value_s = self.GetOption(name);
               res["value_s"] = value_s;
               if (option_type == "bool") {
                 res["value"] = static_cast<bool>(std::stoi(value_s));
               } else if (option_type == "uint64") {
                 res["value"] = std::stoul(value_s);
               } else if (option_type == "double") {
                 res["value"] = std::stod(value_s);
               } else if (option_type == "string") {
                 res["value"] = value_s;
               }
             }
             return res;
           })
2308 2309
      .def("get_all_option_names",
           &platform::ipu::IpuStrategy::GetAllOptionNames)
2310 2311 2312
      .def("enable_pattern", &platform::ipu::IpuStrategy::EnablePattern)
      .def("disable_pattern", &platform::ipu::IpuStrategy::DisablePattern)
      .def("is_pattern_enabled", &platform::ipu::IpuStrategy::IsPatternEnabled);
J
jianghaicheng 已提交
2313 2314
#endif

2315 2316 2317 2318 2319 2320 2321 2322
  m.def("enable_autotune", [] {
    return phi::autotune::AutoTuneStatus::Instance().EnableAutoTune();
  });

  m.def("disable_autotune", [] {
    return phi::autotune::AutoTuneStatus::Instance().DisableAutoTune();
  });

2323
  m.def("set_autotune_range", [](int64_t start, int64_t stop) {
2324 2325 2326 2327 2328 2329 2330 2331 2332
    return phi::autotune::AutoTuneStatus::Instance().SetAutoTuneRange(start,
                                                                      stop);
  });

  m.def("update_autotune_status",
        [] { return phi::autotune::AutoTuneStatus::Instance().Update(); });

  m.def("autotune_status", [] {
    py::dict res;
2333
    phi::autotune::AutoTuneCache::Instance().UpdateStatus();
2334 2335 2336 2337 2338 2339 2340
    res["step_id"] = phi::autotune::AutoTuneStatus::Instance().StepID();
    res["cache_size"] = phi::autotune::AutoTuneCache::Instance().Size();
    res["cache_hit_rate"] =
        phi::autotune::AutoTuneCache::Instance().CacheHitRate();
    return res;
  });

2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
  m.def("enable_layout_autotune", [] {
    return paddle::imperative::LayoutAutoTune::Instance()
        .EnableLayoutAutoTune();
  });

  m.def("disable_layout_autotune", [] {
    return paddle::imperative::LayoutAutoTune::Instance()
        .DisableLayoutAutoTune();
  });

  m.def("use_layout_autotune", [] {
    return paddle::imperative::LayoutAutoTune::Instance().UseLayoutAutoTune();
  });

D
dongdaxiang 已提交
2355
  BindFleetWrapper(&m);
2356
  BindIO(&m);
2357 2358 2359
  BindParallelExecutor(m);
  BindPlace(m);
  BindTensor(m);
T
Thunderbrook 已提交
2360

T
Thunderbrook 已提交
2361
#if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS)
T
Thunderbrook 已提交
2362
  BindHeterWrapper(&m);
2363
  BindMetrics(&m);
T
Thunderbrook 已提交
2364
#endif
T
Thunderbrook 已提交
2365
#ifdef PADDLE_WITH_HETERPS
T
Thunderbrook 已提交
2366
  BindPSGPUWrapper(&m);
T
Thunderbrook 已提交
2367 2368 2369
#ifdef PADDLE_WITH_PSLIB
  BindAfsWrapper(&m);
#endif
T
Thunderbrook 已提交
2370
#endif
2371
  BindGlooWrapper(&m);
H
hutuxian 已提交
2372
  BindBoxHelper(&m);
H
hutuxian 已提交
2373 2374 2375
#ifdef PADDLE_WITH_BOX_PS
  BindBoxWrapper(&m);
#endif
2376
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
D
dongdaxiang 已提交
2377
  BindNCCLWrapper(&m);
2378 2379 2380
#endif
#ifdef PADDLE_WITH_GLOO
  BindGlooContext(&m);
W
wopeizl 已提交
2381
#endif
F
flame 已提交
2382 2383
  BindGraph(&m);
  BindNode(&m);
2384
  BindPass(&m);
F
flame 已提交
2385
  BindInferenceApi(&m);
2386
  BindCompatible(&m);
2387
  BindDataset(&m);
Y
yaoxuefeng 已提交
2388
  BindGenerator(&m);
2389 2390 2391
#ifndef PADDLE_ON_INFERENCE
  BindDistributed(&m);
#endif
2392 2393 2394
#ifdef PADDLE_WITH_ASCEND
  BindAscendWrapper(&m);
  BindAscendGraph(&m);
2395
  BindAscendDevice(&m);
2396
#endif
Y
Yanghello 已提交
2397 2398 2399
#ifdef PADDLE_WITH_CRYPTO
  BindCrypto(&m);
#endif
T
tangwei12 已提交
2400

T
tangwei12 已提交
2401
#if defined PADDLE_WITH_PSCORE
T
tangwei12 已提交
2402 2403
  BindDistFleetWrapper(&m);
  BindPSHost(&m);
2404
  BindCommunicatorContext(&m);
T
tangwei12 已提交
2405 2406
  BindDistCommunicator(&m);
  BindHeterClient(&m);
S
seemingwang 已提交
2407 2408 2409 2410 2411
  BindGraphPyFeatureNode(&m);
  BindGraphNode(&m);
  BindGraphPyService(&m);
  BindGraphPyServer(&m);
  BindGraphPyClient(&m);
1
123malin 已提交
2412 2413 2414 2415
  BindIndexNode(&m);
  BindTreeIndex(&m);
  BindIndexWrapper(&m);
  BindIndexSampler(&m);
2416
#ifdef PADDLE_WITH_HETERPS
2417 2418
  BindNodeQueryResult(&m);
  BindNeighborSampleQuery(&m);
2419 2420 2421
  BindNeighborSampleResult(&m);
  BindGraphGpuWrapper(&m);
#endif
2422
#endif
L
Luo Tao 已提交
2423
}
2424
}  // namespace pybind
2425
}  // namespace paddle