eager.cc 31.8 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 21 22 23 24 25 26
#include "paddle/fluid/eager/api/all.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/utils.h"
#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"
#include "paddle/pten/common/data_type.h"
27
#include "paddle/pten/core/compat/convert_utils.h"
28
#include "paddle/pten/core/dense_tensor.h"
29
#include "pybind11/detail/internals.h"
30 31
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
32
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
33
#include "paddle/fluid/framework/python_headers.h"
34
#include "paddle/fluid/pybind/eager_op_function_impl.h"
35 36 37
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/pten/api/lib/utils/storage.h"
#include "paddle/pten/api/lib/utils/tensor_utils.h"
38 39 40 41 42
namespace paddle {
namespace pybind {

namespace py = ::pybind11;

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

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

57 58
// TODO(jiabin): Overload this once we need more constructor in Python
void EmptyEagerTensorInitializer(
59
    TensorObject* self, const std::string& name,
60 61 62 63 64 65
    const paddle::platform::Place& place, bool persistable = false,
    bool stop_gradient = true, 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 67 68 69 70 71 72
  auto ddims = paddle::framework::make_ddim(dims);
  PADDLE_ENFORCE_GE(
      paddle::framework::product(ddims), 0,
      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 76 77 78 79 80 81
  autograd_meta->SetPersistable(persistable);
  autograd_meta->SetStopGradient(stop_gradient);
  if (var_type == paddle::framework::proto::VarType::LOD_TENSOR) {
    // TODO(jiabin): Maybe support LOD later
    std::shared_ptr<pten::DenseTensor> dense_tensor =
        std::make_shared<pten::DenseTensor>(
            pten::make_intrusive<paddle::experimental::SharedStorage>(place),
82
            pten::DenseTensorMeta(pten::TransToPtenDataType(dtype), ddims));
83
    dense_tensor->mutable_data(place);
84
    self->tensor.set_impl(dense_tensor);
85 86 87 88 89 90 91 92 93 94 95
  } 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>());
96 97 98
  }
}

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

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

159
void InitEagerTensorWithFrameworkTensor(TensorObject* self,
160 161 162
                                        const framework::Tensor& src,
                                        const paddle::platform::Place& place,
                                        const std::string& name) {
163
  self->tensor.set_name(name);
164
  if (place == src.place()) {
165
    self->tensor.set_impl(std::make_shared<pten::DenseTensor>(src));
166 167
    VLOG(4) << "Same place, do ShareDataWith";
  } else {
168 169 170
    auto temp =
        paddle::experimental::Tensor(std::make_shared<pten::DenseTensor>(src));
    self->tensor.set_impl(
171 172 173
        temp.copy_to(pten::TransToPtenBackend(place), true).impl());
    VLOG(4) << "Different place, do TensorCopy";
  }
174 175
  egr::EagerUtils::autograd_meta(&(self->tensor))->SetStopGradient(true);
  egr::EagerUtils::unsafe_autograd_meta(self->tensor)->SetPersistable(false);
176
}
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

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
bool 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) {
  bool res = false;
  if (key == "stop_gradient") res = true;

  if (kw_order_map[key] <= args_num) {
    res = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, kw_order_map[key] - 1),
                                kw_order_map[key] - 1);
  } else {
    if (flag_kwargs && kws_map[key] != NULL) {
      res = CastPyArg2AttrBoolean(kws_map[key], 0);
    } else {
      return res;
    }
  }
  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;
}

// initialize EagerTensor by PyArray(first argument is PyArray,
// mix args and kwargs) automatically.
void AutoInitEagerTensorByPyArray(
275
    TensorObject* py_tensor_ptr,
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    std::unordered_map<std::string, PyObject*> kws_map, PyObject* args,
    bool flag_kwargs, Py_ssize_t args_num) {
  // The first argument of the EagerTensor constructor is PyArray,
  // there are 6 arguments to construct the new EagerTensor,
  // 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 = "";
  bool stop_gradient = true;

  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);
  persistable = ParseBooleanArgs("persistable", kws_map, kw_order_map, args,
                                 flag_kwargs, args_num);
  zero_copy = ParseBooleanArgs("zero_copy", kws_map, kw_order_map, args,
                               flag_kwargs, args_num);
  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);

  EmptyEagerTensorInitializer(py_tensor_ptr, act_name, place, persistable,
                              stop_gradient);
  InitEagerTensorWithNumpyValue(py_tensor_ptr, numpy_value, zero_copy);
}

// initialize EagerTensor by EagerTensor or framework::Tensor (mix args and
// kwargs) automatically.
void AutoInitEagerTensorByTensor(
315
    TensorObject* py_tensor_ptr,
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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 EagerTensor constructor is EagerTensor or
  // framework Tensor,
  // there are 3 arguments to construct the new EagerTensor,
  // 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) {
336
    paddle::experimental::Tensor src_tensor;
337
    if (kw_order_map["value"] <= args_num) {
338 339 340
      src_tensor =
          CastPyArg2Tensor(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
                           kw_order_map["value"] - 1);
341 342
    } else {
      if (flag_kwargs && kws_map["value"] != NULL) {
343
        src_tensor = CastPyArg2Tensor(kws_map["value"], 0);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
            "The first expected kwargs is {value: EagerTensor}, "
            "but could not parse the first argument {value: EagerTensor} "
            "successfully. "
            "Please check your input first and make sure you are on the right "
            "way."));
      }
    }
    InitEagerTensorWithEagerTensor(py_tensor_ptr, src_tensor, place, act_name);
  } 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."));
      }
    }
    InitEagerTensorWithFrameworkTensor(py_tensor_ptr, src_tensor, place,
                                       act_name);
  }
}

/** We should have init function with signature:
379 380 381 382 383 384 385
   * 1.
   * def __init__ ()
   * 2.
   * def __init__ (
   * ** dtype: paddle::framework::proto::VarType::Type,
   * ** dims: vector<int>,
   * ** name: std::string,
386
   * ** type: paddle::framework::proto::VarType::LodTensor,
387
   * ** persistable: bool)
388 389 390
   * 3. (multi-place)
   * (should have at least one parameter, one parameter equals to case 4, zero
   * parameter equals to case 1)
391 392 393 394 395 396 397 398 399 400 401 402 403
   * 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__ (
   * ** tensor: EagerTensor)
404 405 406
   * 6. (multi-place)
   * (should have at least one parameter, one parameter equals to case 5, zero
   * parameter equals to case 1.)
407 408 409 410
   * def __init__ (
   * ** tensor: EagerTensor,
   * ** place: paddle::platform::Place,
   * ** name: std::string)
411 412
   * 7. (multi-place) (should have at least one parameter, one parameter similar
   * to case 5, zero parameter equals to case 1.)
413 414 415 416
   * def __init__ (
   * ** tensor: FrameworkTensor,
   * ** place: paddle::platform::Place,
   * ** name: std::string)
417
   *  **/
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
int EagerTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
  // 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;

  PyObject* kw_value = NULL;  // receive PyArray or EagerTensor
  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)"));

475 476 477 478 479 480
  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."));

481
  auto py_tensor_ptr = reinterpret_cast<TensorObject*>(self);
482 483

  Py_ssize_t args_num = PyTuple_Size(args);
484 485 486 487 488
  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) {
489 490 491 492 493 494 495
      // case 1
      VLOG(6) << "Calling case1's initializer.";
      EmptyEagerTensorInitializer(
          py_tensor_ptr,
          egr::Controller::Instance().GenerateUniqueName("generated_tensor"),
          egr::Controller::Instance().GetExpectedPlace());
      return 0;
496 497 498 499 500 501 502
    } 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";
          AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args,
                                       flag_kwargs, args_num);
          return 0;
503 504
        } else if (PyObject_IsInstance(
                       kw_value, reinterpret_cast<PyObject*>(p_tensor_type))) {
505 506 507 508 509 510 511 512 513 514 515 516
          VLOG(6) << "Calling case5's or case6's initializer";
          AutoInitEagerTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                                      args_num);
          return 0;
        } else if (PyObject_IsInstance(kw_value,
                                       reinterpret_cast<PyObject*>(
                                           g_framework_tensor_pytype))) {
          VLOG(6) << "Calling case7's initializer.";
          AutoInitEagerTensorByTensor(
              py_tensor_ptr, kws_map, args, flag_kwargs, args_num,
              /* false means not init by egr tensor*/ false);
          return 0;
517
        } else {
518 519 520 521 522 523
          PADDLE_THROW(platform::errors::InvalidArgument(
              "Could not parse the first keyword argument successfully, "
              "the first keyword argument is value, but it should be PyArray "
              "or EagerTensor or framework::Tensor. "
              "Please check your input first and make sure you are on the "
              "right way."));
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 555 556 557 558 559 560 561
      } 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);

562
        std::string act_name = "";
563
        if (kw_name == Py_None) {
564 565 566
          act_name = egr::Controller::Instance().GenerateUniqueName(
              "generated_tensor");
        } else {
567
          act_name = CastPyArg2AttrString(kw_name, 0);
568
        }
569 570 571 572 573 574 575 576 577 578

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

        EmptyEagerTensorInitializer(
            py_tensor_ptr, act_name,
            egr::Controller::Instance().GetExpectedPlace(), persistable,
            /* stop_gradient */ true, dtype, dims, var_type);

579
        return 0;
580 581
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
582 583 584 585 586 587
            "We not only support construct EagerTensor from numpy value "
            "or tensor(EagerTensor or framework::Tensor) "
            "with python kwargs by this initializer, "
            "but also even support dtype to init a empty EagerTensor. "
            "Please check your input first and make sure you call the existed "
            "constructor."));
588
      }
589 590 591 592 593 594 595 596 597
    }
  } 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.";
      AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                   args_num);
598
      return 0;
599 600
    } else if (PyObject_IsInstance(
                   arg0_ptr, reinterpret_cast<PyObject*>(p_tensor_type))) {
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
      VLOG(6) << "Calling case5's or case6's initializer.";
      AutoInitEagerTensorByTensor(py_tensor_ptr, kws_map, args, flag_kwargs,
                                  args_num);
      return 0;
    } else if (PyObject_IsInstance(arg0_ptr, reinterpret_cast<PyObject*>(
                                                 g_framework_tensor_pytype))) {
      VLOG(6) << "Calling case7's initializer.";
      AutoInitEagerTensorByTensor(
          py_tensor_ptr, kws_map, args, flag_kwargs, args_num,
          /* false means not init by egr tensor*/ false);
      return 0;
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "We support construct EagerTensor from numpy value "
          "or tensor(EagerTensor or framework::Tensor) "
          "with python args and kwargs by this initializer, "
          "but the first argument should be PyArray or EagerTensor or "
          "framework::Tensor. "
          "Please check your input first and make sure you call the existed "
          "constructor."));
621
    }
622 623 624 625 626 627 628
  } 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.";
      AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                   args_num);
629
      return 0;
630 631 632 633 634 635 636
    } 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."));
637
    }
638 639
  } else if (args_num == (Py_ssize_t)5) {
    if (!flag_kwargs) {
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
      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);
        EmptyEagerTensorInitializer(
            py_tensor_ptr, act_name,
            egr::Controller::Instance().GetExpectedPlace(), persistable, true,
            dtype, dims, var_type);
        return 0;
664 665 666 667
      } else if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
        VLOG(6) << "Calling case3's initializer.";
        AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                     args_num);
668 669 670
        return 0;
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
671 672 673 674 675
            "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."));
676
      }
677
    } else {  // five position args, remainting arguments are kwargs
678
      PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
679 680 681 682 683
      if (pybind11::detail::npy_api::get().PyArray_Check_(arg0_ptr)) {
        VLOG(6) << "Calling case3's or case4's initializer";
        AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                     args_num);
        return 0;
684
      } else {
685 686 687 688 689 690
        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."));
691 692
      }
    }
693 694 695 696 697 698 699 700 701 702 703 704 705 706
  } else if (args_num == (Py_ssize_t)6) {
    if (!flag_kwargs) {
      // case 3
      VLOG(6) << "Calling case3's initializer.";
      AutoInitEagerTensorByPyArray(py_tensor_ptr, kws_map, args, flag_kwargs,
                                   args_num);
      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."));
707
    }
708 709 710 711
  } 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."));
712
  }
713 714

  return 1;
715 716
}

717 718
static void EagerTensorDealloc(TensorObject* self) {
  self->tensor.~Tensor();
719 720 721 722 723 724 725
  Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

extern struct PyGetSetDef variable_properties[];

extern PyMethodDef variable_methods[];

W
wanghuancoder 已提交
726 727 728 729
PyNumberMethods number_methods;
PySequenceMethods sequence_methods;
PyMappingMethods mapping_methods;

730 731 732
void BindEager(pybind11::module* module) {
  auto m = module->def_submodule("eager");

733 734 735 736 737 738 739
  auto& internals = pybind11::detail::get_internals();
  auto heap_type = reinterpret_cast<PyHeapTypeObject*>(
      internals.default_metaclass->tp_alloc(internals.default_metaclass, 0));
  heap_type->ht_name = ToPyObject("EagerTensor");
  heap_type->ht_qualname = ToPyObject("EagerTensor");
  auto type = &heap_type->ht_type;
  type->tp_name = "EagerTensor";
740
  type->tp_basicsize = sizeof(TensorObject);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  type->tp_dealloc = (destructor)EagerTensorDealloc;
  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;
  type->tp_init = EagerTensorInit;
  type->tp_new = EagerTensorNew;
  Py_INCREF(internals.instance_base);
  type->tp_base = reinterpret_cast<PyTypeObject*>(internals.instance_base);
  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
756
  p_tensor_type = type;
757 758

  if (PyType_Ready(type) < 0) {
759
    PADDLE_THROW(platform::errors::Fatal(
760
        "Init Paddle error in BindEager(PyType_Ready)."));
761 762 763
    return;
  }

764
  Py_INCREF(type);
765
  if (PyModule_AddObject(m.ptr(), "EagerTensor",
766 767
                         reinterpret_cast<PyObject*>(type)) < 0) {
    Py_DECREF(type);
768 769
    Py_DECREF(m.ptr());
    PADDLE_THROW(platform::errors::Fatal(
770
        "Init Paddle error in BindEager(PyModule_AddObject)."));
771 772 773 774
    return;
  }

  BindFunctions(m.ptr());
775
  BindEagerOpFunctions(&m);
776 777 778 779
}

}  // namespace pybind
}  // namespace paddle