eager.cc 31.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
// disable numpy compile error
#include <Python.h>

#include <string>
#include <vector>

17
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
18 19 20
#include "paddle/fluid/eager/api/all.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/utils.h"
21
#include "paddle/fluid/framework/convert_utils.h"
22 23 24 25 26
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/pybind/eager.h"
#include "paddle/fluid/pybind/eager_utils.h"
27 28 29
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
30
#include "pybind11/detail/internals.h"
31 32
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
33
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
34
#include "paddle/fluid/framework/python_headers.h"
35
#include "paddle/fluid/pybind/eager_op_function_impl.h"
36
#include "paddle/fluid/pybind/tensor_py.h"
37 38
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
39 40 41 42 43
namespace paddle {
namespace pybind {

namespace py = ::pybind11;

44
PyTypeObject* p_tensor_type;
45
extern PyTypeObject* g_vartype_pytype;
46
extern PyTypeObject* g_framework_tensor_pytype;
47

48
PyObject* TensorNew(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
49 50
  PyObject* obj = type->tp_alloc(type, 0);
  if (obj) {
51 52
    auto v = reinterpret_cast<TensorObject*>(obj);
    new (&(v->tensor)) paddle::experimental::Tensor();
53 54 55 56
  }
  return obj;
}

57
// TODO(jiabin): Overload this once we need more constructor in Python
58 59
void EmptyTensorInitializer(TensorObject* self, const std::string& name,
                            const paddle::platform::Place& place,
60
                            bool persistable = false, int stop_gradient = -1,
61 62 63 64 65
                            framework::proto::VarType::Type dtype =
                                paddle::framework::proto::VarType::FP32,
                            const std::vector<int>& dims = {},
                            framework::proto::VarType::Type var_type =
                                paddle::framework::proto::VarType::LOD_TENSOR) {
66
  auto ddims = phi::make_ddim(dims);
67
  PADDLE_ENFORCE_GE(
68
      phi::product(ddims), 0,
69 70 71 72
      paddle::platform::errors::InvalidArgument(
          "Create Eager Tensor with dims contain minus num is ilegal"
          "Please check your code and make sure you new a "
          "eager tensor with fixed shape instead of using -1."));
73 74
  self->tensor.set_name(name);
  auto autograd_meta = egr::EagerUtils::autograd_meta(&(self->tensor));
75
  autograd_meta->SetPersistable(persistable);
76 77 78
  if (stop_gradient != -1) {
    autograd_meta->SetStopGradient(static_cast<bool>(stop_gradient));
  }
79 80
  if (var_type == paddle::framework::proto::VarType::LOD_TENSOR) {
    // TODO(jiabin): Maybe support LOD later
81 82 83 84 85
    std::shared_ptr<phi::DenseTensor> dense_tensor =
        std::make_shared<phi::DenseTensor>(
            phi::make_intrusive<paddle::experimental::SharedStorage>(place),
            phi::DenseTensorMeta(paddle::framework::TransToPtenDataType(dtype),
                                 ddims));
86
    dense_tensor->mutable_data(place);
87
    self->tensor.set_impl(dense_tensor);
88 89 90 91 92 93 94 95 96 97 98
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "We only support LoDTensor to be constructed by this initializer, "
        "please check your var type first and make sure you are going to "
        "construct LoDTensor."));
  }

  if (!autograd_meta->GetMutableGradNode()) {
    VLOG(3) << "Tensor(" << name
            << ") have not GradNode, add GradNodeAccumulation for it.";
    autograd_meta->SetGradNode(std::make_shared<egr::GradNodeAccumulation>());
99 100 101
  }
}

102 103
void InitTensorWithNumpyValue(TensorObject* self, const py::object& array,
                              bool zero_copy = false) {
104
  PADDLE_ENFORCE_EQ(
105
      self->tensor.defined(), true,
106
      paddle::platform::errors::Fatal(
107 108
          "Calling InitTensorWithNumpyValue of Eager Tensor without "
          "EmptyTensorInitializer is "
109 110
          "forbidden. Please check your code and make sure you new a "
          "eager tensor before init it with NumPy."));
111 112
  phi::DenseTensor* impl_ptr =
      static_cast<phi::DenseTensor*>(self->tensor.impl().get());
113 114
  paddle::platform::Place place = impl_ptr->place();
  if (platform::is_cpu_place(place)) {
115
    SetTensorFromPyArray<platform::CPUPlace>(impl_ptr, array, place, zero_copy);
116
  } else if (platform::is_xpu_place(place)) {
117
    SetTensorFromPyArray<platform::XPUPlace>(impl_ptr, array, place, zero_copy);
118
  } else if (platform::is_gpu_place(place)) {
119
    SetTensorFromPyArray<platform::CUDAPlace>(impl_ptr, array, place,
120
                                              zero_copy);
121
  } else if (platform::is_cuda_pinned_place(place)) {
122
    SetTensorFromPyArray<platform::CUDAPinnedPlace>(impl_ptr, array, place,
123
                                                    zero_copy);
124
  } else if (platform::is_npu_place(place)) {
125
    SetTensorFromPyArray<platform::NPUPlace>(impl_ptr, array, place, zero_copy);
126 127 128 129 130 131 132
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Place should be one of "
        "CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace/NPUPlace"));
  }
}

133 134 135 136
void InitTensorWithTensor(TensorObject* self,
                          const paddle::experimental::Tensor& src,
                          const paddle::platform::Place& place,
                          const std::string& name) {
137 138
  self->tensor.set_name(name);
  if (place == src.inner_place()) {
139
    auto impl = std::static_pointer_cast<phi::DenseTensor>(src.impl());
140
    self->tensor.set_impl(impl);
141 142
    VLOG(4) << "Same place, do ShareDataWith";
  } else {
143
    self->tensor.set_impl(
144
        src.copy_to(phi::TransToPtenBackend(place), true).impl());
145 146 147
    VLOG(4) << "Different place, do TensorCopy";
  }
  if (src.get_autograd_meta()) {
148
    egr::EagerUtils::autograd_meta(&(self->tensor))
149 150 151
        ->SetPersistable(
            egr::EagerUtils::unsafe_autograd_meta(src)->Persistable());
  } else {
152
    egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
153 154 155
  }
}

156 157 158 159
void InitTensorWithFrameworkTensor(TensorObject* self,
                                   const framework::Tensor& src,
                                   const paddle::platform::Place& place,
                                   const std::string& name) {
160
  self->tensor.set_name(name);
161
  if (place == src.place()) {
162
    self->tensor.set_impl(std::make_shared<phi::DenseTensor>(src));
163 164
    VLOG(4) << "Same place, do ShareDataWith";
  } else {
165
    auto temp =
166
        paddle::experimental::Tensor(std::make_shared<phi::DenseTensor>(src));
167
    self->tensor.set_impl(
168
        temp.copy_to(phi::TransToPtenBackend(place), true).impl());
169 170
    VLOG(4) << "Different place, do TensorCopy";
  }
171
  egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
172
}
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

py::object ParsePyArray(
    std::unordered_map<std::string, PyObject*> kws_map,
    std::unordered_map<std::string, Py_ssize_t> kw_order_map, PyObject* args,
    bool flag_kwargs, Py_ssize_t args_num) {
  py::object numpy_value = py::object();

  if (kw_order_map["value"] <= args_num) {
    numpy_value = py::object(
        py::handle(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1)), true);
  } else {
    if (flag_kwargs && kws_map["value"] != NULL) {
      numpy_value = py::object(py::handle(kws_map["value"]), true);
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "The first expected arguments is {value: PyArray}, "
          "but could not parse the first argument {value: PyArray} "
          "successfully. "
          "Please check your input first and make sure you are on the right "
          "way."));
    }
  }
  return numpy_value;
}

paddle::platform::Place ParsePlace(
    std::unordered_map<std::string, PyObject*> kws_map,
    std::unordered_map<std::string, Py_ssize_t> kw_order_map, PyObject* args,
    bool flag_kwargs, Py_ssize_t args_num) {
  paddle::platform::Place place =
      egr::Controller::Instance().GetExpectedPlace();

  if (kw_order_map["place"] <= args_num) {
    place = CastPyArg2Place(PyTuple_GET_ITEM(args, kw_order_map["place"] - 1),
                            kw_order_map["place"] - 1);
  } else {
    if (flag_kwargs && kws_map["place"] != NULL) {
      place = CastPyArg2Place(kws_map["place"], 0);
    } else {
      // default
      return place;
    }
  }
  return place;
}

// boolean arguments: zero_copy, stop_gradient, persistable
220 221 222 223 224
int ParseBooleanArgs(std::string key,
                     std::unordered_map<std::string, PyObject*> kws_map,
                     std::unordered_map<std::string, Py_ssize_t> kw_order_map,
                     PyObject* args, bool flag_kwargs, Py_ssize_t args_num) {
  int res = -1;
225 226

  if (kw_order_map[key] <= args_num) {
227 228
    res = static_cast<int>(CastPyArg2AttrBoolean(
        PyTuple_GET_ITEM(args, kw_order_map[key] - 1), kw_order_map[key] - 1));
229 230
  } else {
    if (flag_kwargs && kws_map[key] != NULL) {
231
      res = static_cast<int>(CastPyArg2AttrBoolean(kws_map[key], 0));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    }
  }
  return res;
}

std::string ParseName(std::unordered_map<std::string, PyObject*> kws_map,
                      std::unordered_map<std::string, Py_ssize_t> kw_order_map,
                      PyObject* args, bool flag_kwargs, Py_ssize_t args_num) {
  std::string act_name = "";
  if (kw_order_map["name"] <= args_num) {
    PyObject* name_obj = PyTuple_GET_ITEM(args, kw_order_map["name"] - 1);
    if (name_obj == Py_None) {
      act_name =
          egr::Controller::Instance().GenerateUniqueName("generated_tensor");
    } else {
      act_name = CastPyArg2AttrString(name_obj, kw_order_map["name"] - 1);
    }
  } else {
    if (flag_kwargs) {
      if (kws_map["name"] == NULL) {
        act_name =
            egr::Controller::Instance().GenerateUniqueName("generated_tensor");
      } else {
        act_name = CastPyArg2AttrString(kws_map["name"], 0);
      }
    } else {
      act_name =
          egr::Controller::Instance().GenerateUniqueName("generated_tensor");
    }
  }
  return act_name;
}

265
// initialize Tensor by PyArray(first argument is PyArray,
266
// mix args and kwargs) automatically.
267 268 269 270 271 272
void AutoInitTensorByPyArray(TensorObject* py_tensor_ptr,
                             std::unordered_map<std::string, PyObject*> kws_map,
                             PyObject* args, bool flag_kwargs,
                             Py_ssize_t args_num) {
  // The first argument of the Tensor constructor is PyArray,
  // there are 6 arguments to construct the new Tensor,
273 274 275 276 277 278 279 280 281 282 283 284 285 286
  // kw_order_map's key is every arguments of the constructor,
  // kw_order_map's value is the position of the arguments respectively.
  // If u want to update this constructor with new arguments,
  // need to update this map and to add or change related code.
  std::unordered_map<std::string, Py_ssize_t> kw_order_map{
      {"value", 1},     {"place", 2}, {"persistable", 3},
      {"zero_copy", 4}, {"name", 5},  {"stop_gradient", 6}};

  py::object numpy_value = py::object();
  paddle::platform::Place place =
      egr::Controller::Instance().GetExpectedPlace();
  bool persistable = false;
  bool zero_copy = false;
  std::string act_name = "";
287
  int stop_gradient = -1;
288 289 290 291

  numpy_value =
      ParsePyArray(kws_map, kw_order_map, args, flag_kwargs, args_num);
  place = ParsePlace(kws_map, kw_order_map, args, flag_kwargs, args_num);
292 293 294 295
  persistable = (1 == ParseBooleanArgs("persistable", kws_map, kw_order_map,
                                       args, flag_kwargs, args_num));
  zero_copy = (1 == ParseBooleanArgs("zero_copy", kws_map, kw_order_map, args,
                                     flag_kwargs, args_num));
296 297 298 299
  act_name = ParseName(kws_map, kw_order_map, args, flag_kwargs, args_num);
  stop_gradient = ParseBooleanArgs("stop_gradient", kws_map, kw_order_map, args,
                                   flag_kwargs, args_num);

300 301 302
  EmptyTensorInitializer(py_tensor_ptr, act_name, place, persistable,
                         stop_gradient);
  InitTensorWithNumpyValue(py_tensor_ptr, numpy_value, zero_copy);
303 304
}

305
// initialize Tensor by Tensor or framework::Tensor (mix args and
306
// kwargs) automatically.
307 308 309 310 311 312
void AutoInitTensorByTensor(TensorObject* py_tensor_ptr,
                            std::unordered_map<std::string, PyObject*> kws_map,
                            PyObject* args, bool flag_kwargs,
                            Py_ssize_t args_num,
                            bool init_by_egr_tensor = true) {
  // The first argument of the Tensor constructor is Tensor or
313
  // framework Tensor,
314
  // there are 3 arguments to construct the new Tensor,
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  // kw_order_map's key is every arguments of the constructor,
  // kw_order_map's value is the position of the arguments respectively.
  // If u want to update this constructor with new arguments,
  // need to update this map and to add or change related code.
  std::unordered_map<std::string, Py_ssize_t> kw_order_map{
      {"value", 1}, {"place", 2}, {"name", 3}};

  paddle::platform::Place place =
      egr::Controller::Instance().GetExpectedPlace();
  std::string act_name = "";

  place = ParsePlace(kws_map, kw_order_map, args, flag_kwargs, args_num);
  act_name = ParseName(kws_map, kw_order_map, args, flag_kwargs, args_num);

  if (init_by_egr_tensor) {
330
    paddle::experimental::Tensor src_tensor;
331
    if (kw_order_map["value"] <= args_num) {
332 333 334
      src_tensor =
          CastPyArg2Tensor(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
                           kw_order_map["value"] - 1);
335 336
    } else {
      if (flag_kwargs && kws_map["value"] != NULL) {
337
        src_tensor = CastPyArg2Tensor(kws_map["value"], 0);
338 339
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
340 341
            "The first expected kwargs is {value: Tensor}, "
            "but could not parse the first argument {value: Tensor} "
342 343 344 345 346
            "successfully. "
            "Please check your input first and make sure you are on the right "
            "way."));
      }
    }
347
    InitTensorWithTensor(py_tensor_ptr, src_tensor, place, act_name);
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  } else {
    // init by framework tensor
    framework::Tensor src_tensor;
    if (kw_order_map["value"] <= args_num) {
      src_tensor = CastPyArg2FrameworkTensor(
          PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
          kw_order_map["value"] - 1);
    } else {
      if (flag_kwargs && kws_map["value"] != NULL) {
        src_tensor = CastPyArg2FrameworkTensor(kws_map["value"], 0);
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
            "The first expected arguments is {value: framework::Tensor}, "
            "but could not parse the first argument {value: framework::Tensor} "
            "successfully. "
            "Please check your input first and make sure you are on the right "
            "way."));
      }
    }
367
    InitTensorWithFrameworkTensor(py_tensor_ptr, src_tensor, place, act_name);
368 369 370 371
  }
}

/** We should have init function with signature:
372 373 374 375 376 377 378
   * 1.
   * def __init__ ()
   * 2.
   * def __init__ (
   * ** dtype: paddle::framework::proto::VarType::Type,
   * ** dims: vector<int>,
   * ** name: std::string,
379
   * ** type: paddle::framework::proto::VarType::LodTensor,
380
   * ** persistable: bool)
381 382 383
   * 3. (multi-place)
   * (should have at least one parameter, one parameter equals to case 4, zero
   * parameter equals to case 1)
384 385 386 387 388 389 390 391 392 393 394 395
   * def __init__ (
   * ** value: ndarray,
   * ** place: paddle::platform::Place,
   * ** persistable: bool,
   * ** zero_copy: bool,
   * ** name: std::string,
   * ** stop_gradient: bool)
   * 4.
   * def __init__ (
   * ** value: ndarray)
   * 5.
   * def __init__ (
396
   * ** tensor: Tensor)
397 398 399
   * 6. (multi-place)
   * (should have at least one parameter, one parameter equals to case 5, zero
   * parameter equals to case 1.)
400
   * def __init__ (
401
   * ** tensor: Tensor,
402 403
   * ** place: paddle::platform::Place,
   * ** name: std::string)
404 405
   * 7. (multi-place) (should have at least one parameter, one parameter similar
   * to case 5, zero parameter equals to case 1.)
406 407 408 409
   * def __init__ (
   * ** tensor: FrameworkTensor,
   * ** place: paddle::platform::Place,
   * ** name: std::string)
410
   *  **/
411
int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
412 413 414 415 416 417 418 419 420
  // set a flag to record use kwargs or not
  bool flag_kwargs = false;
  if (kwargs) flag_kwargs = true;

  // all kwargs
  PyObject* kw_zero_copy = NULL;
  PyObject* kw_persistable = NULL;
  PyObject* kw_stop_gradient = NULL;

421
  PyObject* kw_value = NULL;  // receive PyArray or Tensor
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  PyObject* kw_place = NULL;
  PyObject* kw_name = NULL;
  PyObject* kw_dims = NULL;
  PyObject* kw_dtype = NULL;
  PyObject* kw_type = NULL;

  // the keywords argument
  static char* kwlist[] = {
      const_cast<char*>("value"),       const_cast<char*>("place"),
      const_cast<char*>("persistable"), const_cast<char*>("zero_copy"),
      const_cast<char*>("name"),        const_cast<char*>("stop_gradient"),
      const_cast<char*>("dims"),        const_cast<char*>("dtype"),
      const_cast<char*>("type"),        NULL};

  // 'O' Store a Python object (without any conversion) in a C object pointer,
  // '|' Indicates that the remaining arguments in the Python argument list are
  // optional.
  // PyArg_ParseTupleAndKeywords can Parse the parameters of a function that
  // takes both positional and keyword parameters into local variables,
  // which enhance case2, case3, case4, case5, case6, case7.
  bool flag_ = PyArg_ParseTupleAndKeywords(
      args, kwargs, "|OOOOOOOOO", kwlist, &kw_value, &kw_place, &kw_persistable,
      &kw_zero_copy, &kw_name, &kw_stop_gradient, &kw_dims, &kw_dtype,
      &kw_type);

  // helper map
  std::unordered_map<std::string, PyObject*> kws_map{
      {"value", kw_value},
      {"place", kw_place},
      {"persistable", kw_persistable},
      {"zero_copy", kw_zero_copy},
      {"name", kw_name},
      {"stop_gradient", kw_stop_gradient},
      {"dims", kw_dims},
      {"dtype", kw_dtype},
      {"type", kw_type}};

  PADDLE_ENFORCE_EQ(flag_, true,
                    paddle::platform::errors::PreconditionNotMet(
                        "Could not parse args and kwargs successfully, "
                        "please check your input first and make"
                        "sure you are on the right way. "
                        "The expected arguments as follow: ("
                        "value, place, persistable, zero_copy, "
                        "name, stop_gradient, dims, dtype, type)"));

468 469 470 471 472 473
  PADDLE_ENFORCE_NOT_NULL(
      self, paddle::platform::errors::Fatal(
                "Calling __init__ of Eager Tensor without __new__ is "
                "forbidden. Please check your code and make sure you new a "
                "eager tensor before init it."));

474
  auto py_tensor_ptr = reinterpret_cast<TensorObject*>(self);
475 476

  Py_ssize_t args_num = PyTuple_Size(args);
477 478 479 480 481
  VLOG(6) << " args_num: " << args_num;

  // args_num = 0, means that there is no position arguments.
  if (args_num == (Py_ssize_t)0) {
    if (!flag_kwargs) {
482 483
      // case 1
      VLOG(6) << "Calling case1's initializer.";
484
      EmptyTensorInitializer(
485 486 487 488
          py_tensor_ptr,
          egr::Controller::Instance().GenerateUniqueName("generated_tensor"),
          egr::Controller::Instance().GetExpectedPlace());
      return 0;
489 490 491 492
    } else {  // no position args, all arguments are kwargs
      if (kw_value != NULL) {
        if (pybind11::detail::npy_api::get().PyArray_Check_(kw_value)) {
          VLOG(6) << "Calling case3's or case4's initializer";
493 494
          AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                  args_num);
495
          return 0;
496 497
        } else if (PyObject_IsInstance(
                       kw_value, reinterpret_cast<PyObject*>(p_tensor_type))) {
498
          VLOG(6) << "Calling case5's or case6's initializer";
499 500
          AutoInitTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                                 args_num);
501 502 503 504 505
          return 0;
        } else if (PyObject_IsInstance(kw_value,
                                       reinterpret_cast<PyObject*>(
                                           g_framework_tensor_pytype))) {
          VLOG(6) << "Calling case7's initializer.";
506 507 508
          AutoInitTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                                 args_num,
                                 /* false means not init by egr tensor*/ false);
509
          return 0;
510
        } else {
511 512 513
          PADDLE_THROW(platform::errors::InvalidArgument(
              "Could not parse the first keyword argument successfully, "
              "the first keyword argument is value, but it should be PyArray "
514
              "or Tensor or framework::Tensor. "
515 516
              "Please check your input first and make sure you are on the "
              "right way."));
517
        }
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
      } else if (kw_dtype != NULL &&
                 PyObject_IsInstance(
                     kw_dtype, reinterpret_cast<PyObject*>(g_vartype_pytype))) {
        VLOG(6) << "Calling case2's initializer";

        PADDLE_ENFORCE_NOT_NULL(
            kw_dims,
            paddle::platform::errors::InvalidArgument(
                "Calling __init__ of Eager Tensor with NULL dims is "
                "forbidden. Please check your code and make sure you new a "
                "dims before calling this constructor."));

        PADDLE_ENFORCE_NOT_NULL(
            kw_name,
            paddle::platform::errors::InvalidArgument(
                "Calling __init__ of Eager Tensor with NULL name is "
                "forbidden. Please check your code and make sure you new a "
                "name before calling this constructor."));

        PADDLE_ENFORCE_NOT_NULL(
            kw_dtype,
            paddle::platform::errors::InvalidArgument(
                "Calling __init__ of Eager Tensor with NULL dtype is "
                "forbidden. Please check your code and make sure you new a "
                "dtype before calling this constructor."));

        PADDLE_ENFORCE_NOT_NULL(
            kw_persistable,
            paddle::platform::errors::InvalidArgument(
                "Calling __init__ of Eager Tensor with NULL persistable is "
                "forbidden. Please check your code and make sure you new a "
                "persistable before calling this constructor."));

        paddle::framework::proto::VarType::Type dtype =
            CastPyArg2ProtoType(kw_dtype, 0);
        std::vector<int> dims = CastPyArg2VectorOfInt(kw_dims, 0);

555
        std::string act_name = "";
556
        if (kw_name == Py_None) {
557 558 559
          act_name = egr::Controller::Instance().GenerateUniqueName(
              "generated_tensor");
        } else {
560
          act_name = CastPyArg2AttrString(kw_name, 0);
561
        }
562 563 564 565 566

        paddle::framework::proto::VarType::Type var_type =
            CastPyArg2ProtoType(kw_type, 0);
        bool persistable = CastPyArg2AttrBoolean(kw_persistable, 0);

567 568 569
        EmptyTensorInitializer(py_tensor_ptr, act_name,
                               egr::Controller::Instance().GetExpectedPlace(),
                               persistable,
570
                               /* stop_gradient */ -1, dtype, dims, var_type);
571

572
        return 0;
573 574
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
575 576
            "We not only support construct Tensor from numpy value "
            "or tensor(Tensor or framework::Tensor) "
577
            "with python kwargs by this initializer, "
578
            "but also even support dtype to init a empty Tensor. "
579 580
            "Please check your input first and make sure you call the existed "
            "constructor."));
581
      }
582 583 584 585 586 587 588
    }
  } else if (args_num == (Py_ssize_t)1 || args_num == (Py_ssize_t)2 ||
             args_num == (Py_ssize_t)3) {
    // 1 to 3 position args, remainting arguments are kwargs
    PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
    if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
      VLOG(6) << "Calling case3's or case4's initializer.";
589 590
      AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                              args_num);
591
      return 0;
592 593
    } else if (PyObject_IsInstance(
                   arg0_ptr, reinterpret_cast<PyObject*>(p_tensor_type))) {
594
      VLOG(6) << "Calling case5's or case6's initializer.";
595 596
      AutoInitTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                             args_num);
597 598 599 600
      return 0;
    } else if (PyObject_IsInstance(arg0_ptr, reinterpret_cast<PyObject*>(
                                                 g_framework_tensor_pytype))) {
      VLOG(6) << "Calling case7's initializer.";
601 602 603
      AutoInitTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                             args_num,
                             /* false means not init by egr tensor*/ false);
604 605 606
      return 0;
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
607 608
          "We support construct Tensor from numpy value "
          "or tensor(Tensor or framework::Tensor) "
609
          "with python args and kwargs by this initializer, "
610
          "but the first argument should be PyArray or Tensor or "
611 612 613
          "framework::Tensor. "
          "Please check your input first and make sure you call the existed "
          "constructor."));
614
    }
615 616 617 618 619
  } else if (args_num == (Py_ssize_t)4) {
    // 4 position args, remainting arguments are kwargs
    PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
    if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
      VLOG(6) << "Calling case3's or case4's initializer.";
620 621
      AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                              args_num);
622
      return 0;
623 624 625 626 627 628 629
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Incompatible constructor arguments, "
          "there are 4 position args and remainting arguments arg kwargs,"
          "but the first position args should be PyArray. "
          "Please check your code and make sure the first position args is "
          "PyArray."));
630
    }
631 632
  } else if (args_num == (Py_ssize_t)5) {
    if (!flag_kwargs) {
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
      PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
      if (PyObject_IsInstance(arg0_ptr,
                              reinterpret_cast<PyObject*>(g_vartype_pytype))) {
        VLOG(6) << "Calling case2's initializer.";
        paddle::framework::proto::VarType::Type dtype =
            CastPyArg2ProtoType(PyTuple_GET_ITEM(args, 0), 0);
        std::vector<int> dims =
            CastPyArg2VectorOfInt(PyTuple_GET_ITEM(args, 1), 1);
        std::string act_name = "";
        PyObject* name_obj = PyTuple_GET_ITEM(args, 2);
        if (name_obj == Py_None) {
          act_name = egr::Controller::Instance().GenerateUniqueName(
              "generated_tensor");
        } else {
          act_name = CastPyArg2AttrString(PyTuple_GET_ITEM(args, 2), 2);
        }
        paddle::framework::proto::VarType::Type var_type =
            CastPyArg2ProtoType(PyTuple_GET_ITEM(args, 3), 3);
        bool persistable = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 4), 4);
652 653
        EmptyTensorInitializer(py_tensor_ptr, act_name,
                               egr::Controller::Instance().GetExpectedPlace(),
654
                               persistable, -1, dtype, dims, var_type);
655
        return 0;
656 657
      } else if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
        VLOG(6) << "Calling case3's initializer.";
658 659
        AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                args_num);
660 661 662
        return 0;
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
663 664 665 666 667
            "Incompatible constructor arguments, "
            "there are only 5 position args,"
            "but the first position args should be PyArray or dtype. "
            "Please check your code and make sure you call the existed "
            "constructor."));
668
      }
669
    } else {  // five position args, remainting arguments are kwargs
670
      PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
671 672
      if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
        VLOG(6) << "Calling case3's or case4's initializer";
673 674
        AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                args_num);
675
        return 0;
676
      } else {
677 678 679 680 681 682
        PADDLE_THROW(platform::errors::InvalidArgument(
            "Incompatible constructor arguments, "
            "there are 5 position args and remainting arguments are kwargs,"
            "but the first position args should be PyArray. "
            "Please check your code and make sure the first position args is "
            "PyArray."));
683 684
      }
    }
685 686 687 688
  } else if (args_num == (Py_ssize_t)6) {
    if (!flag_kwargs) {
      // case 3
      VLOG(6) << "Calling case3's initializer.";
689 690
      AutoInitTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                              args_num);
691 692 693 694 695 696 697 698
      return 0;
    } else {  // six position args, remainting arguments are kwargs, but this
              // is not a right way
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Incompatible constructor arguments, "
          "there are 6 position args and the remainting arguments are kwargs. "
          "Please check your code and make sure the first position args is "
          "PyArray."));
699
    }
700 701 702 703
  } else {
    PADDLE_THROW(platform::errors::Fatal(
        "Can't not find expected num of args, please check your call, and "
        "make sure u call the existed constructor."));
704
  }
705 706

  return 1;
707 708
}

709
static void TensorDealloc(TensorObject* self) {
710
  self->tensor.~Tensor();
711 712 713 714 715 716 717
  Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

extern struct PyGetSetDef variable_properties[];

extern PyMethodDef variable_methods[];

W
wanghuancoder 已提交
718 719 720 721
PyNumberMethods number_methods;
PySequenceMethods sequence_methods;
PyMappingMethods mapping_methods;

722 723 724
void BindEager(pybind11::module* module) {
  auto m = module->def_submodule("eager");

725
  auto heap_type = reinterpret_cast<PyHeapTypeObject*>(
726
      PyType_Type.tp_alloc(&PyType_Type, 0));
727 728
  heap_type->ht_name = ToPyObject("Tensor");
  heap_type->ht_qualname = ToPyObject("Tensor");
729
  auto type = &heap_type->ht_type;
730
  type->tp_name = "Tensor";
731
  type->tp_basicsize = sizeof(TensorObject);
732
  type->tp_dealloc = (destructor)TensorDealloc;
733 734 735 736 737
  type->tp_as_number = &number_methods;
  type->tp_as_sequence = &sequence_methods;
  type->tp_as_mapping = &mapping_methods;
  type->tp_methods = variable_methods;
  type->tp_getset = variable_properties;
738 739
  type->tp_init = TensorInit;
  type->tp_new = TensorNew;
740 741
  Py_INCREF(&PyBaseObject_Type);
  type->tp_base = reinterpret_cast<PyTypeObject*>(&PyBaseObject_Type);
742 743 744 745 746
  type->tp_flags |=
      Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
#if PY_VERSION_HEX >= 0x03050000
  type->tp_as_async = &heap_type->as_async;
#endif
747
  p_tensor_type = type;
748 749

  if (PyType_Ready(type) < 0) {
750
    PADDLE_THROW(platform::errors::Fatal(
751
        "Init Paddle error in BindEager(PyType_Ready)."));
752 753 754
    return;
  }

755
  Py_INCREF(type);
756 757
  if (PyModule_AddObject(m.ptr(), "Tensor", reinterpret_cast<PyObject*>(type)) <
      0) {
758
    Py_DECREF(type);
759 760
    Py_DECREF(m.ptr());
    PADDLE_THROW(platform::errors::Fatal(
761
        "Init Paddle error in BindEager(PyModule_AddObject)."));
762 763 764 765
    return;
  }

  BindFunctions(m.ptr());
766
  BindEagerOpFunctions(&m);
767 768 769 770
}

}  // namespace pybind
}  // namespace paddle