initialize Tensor by PyArray(first argument is PyArray, mix args and kwargs) automatically.
| 535 | // initialize Tensor by PyArray(first argument is PyArray, |
| 536 | // mix args and kwargs) automatically. |
| 537 | void AutoInitTensorByPyArray(TensorObject* py_tensor_ptr, |
| 538 | std::unordered_map<std::string, PyObject*> kws_map, |
| 539 | PyObject* args, |
| 540 | bool flag_kwargs, |
| 541 | Py_ssize_t args_num) { |
| 542 | // The first argument of the Tensor constructor is PyArray, |
| 543 | // there are 6 arguments to construct the new Tensor, |
| 544 | // kw_order_map's key is every arguments of the constructor, |
| 545 | // kw_order_map's value is the position of the arguments respectively. |
| 546 | // If u want to update this constructor with new arguments, |
| 547 | // need to update this map and to add or change related code. |
| 548 | std::unordered_map<std::string, Py_ssize_t> kw_order_map{ |
| 549 | {"value", 1}, |
| 550 | {"place", 2}, |
| 551 | {"persistable", 3}, |
| 552 | {"zero_copy", 4}, |
| 553 | {"name", 5}, |
| 554 | {"stop_gradient", 6}}; |
| 555 | |
| 556 | py::object numpy_value = py::object(); |
| 557 | Place place = egr::Controller::Instance().GetExpectedPlace(); |
| 558 | bool persistable = false; |
| 559 | bool zero_copy = false; |
| 560 | std::string act_name = ""; |
| 561 | int stop_gradient = -1; |
| 562 | |
| 563 | numpy_value = |
| 564 | ParsePyArray(kws_map, kw_order_map, args, flag_kwargs, args_num); |
| 565 | place = ParsePlace(kws_map, kw_order_map, args, flag_kwargs, args_num); |
| 566 | persistable = |
| 567 | (1 == |
| 568 | ParseBooleanArgs( |
| 569 | "persistable", kws_map, kw_order_map, args, flag_kwargs, args_num)); |
| 570 | zero_copy = |
| 571 | (1 == |
| 572 | ParseBooleanArgs( |
| 573 | "zero_copy", kws_map, kw_order_map, args, flag_kwargs, args_num)); |
| 574 | act_name = ParseName(kws_map, kw_order_map, args, flag_kwargs, args_num); |
| 575 | stop_gradient = ParseBooleanArgs( |
| 576 | "stop_gradient", kws_map, kw_order_map, args, flag_kwargs, args_num); |
| 577 | |
| 578 | EmptyTensorInitializer( |
| 579 | py_tensor_ptr, act_name, place, persistable, stop_gradient); |
| 580 | InitTensorWithNumpyValue(py_tensor_ptr, numpy_value, place, zero_copy); |
| 581 | } |
| 582 | |
| 583 | // initialize Tensor by Tensor or DenseTensor (mix args and |
| 584 | // kwargs) automatically. |
no test coverage detected