MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / _astensor1d_cpp

Function _astensor1d_cpp

imperative/python/src/tensor_utils.cpp:482–587  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

480}
481
482py::object _astensor1d_cpp(
483 py::handle value, py::handle dtype, py::handle device, py::handle ref) {
484 py::object ret;
485 py::object device_obj = py::none();
486 py::object ndim_obj = py::none();
487 if (device.ptr() != Py_None) {
488 device_obj = device2obj(device);
489 }
490
491 if (PyObject_TypeCheck(value.ptr(), py_varnode_type)) {
492 try {
493 getattr(value, "ndim");
494 } catch (py::error_already_set& err) {
495 if (dtype.ptr() != Py_None) {
496 ret = _astype_cpp(value, dtype);
497 } else {
498 ret = py::reinterpret_borrow<py::object>(value);
499 }
500 if (device.ptr() != Py_None) {
501 std::shared_ptr<OpDef> op = Copy::make(device_obj.cast<CompNode>());
502 py::object Op = py::cast(op);
503 PyObject* p[2] = {Op.ptr(), ret.ptr()};
504 py::tuple copy_ret =
505 py::reinterpret_steal<py::object>(py_apply(NULL, p, 2));
506 return copy_ret[0];
507 }
508 return ret;
509 }
510 }
511
512 size_t ndim = 999;
513 if (hasattr(value, "ndim")) {
514 ndim = getattr(value, "ndim").cast<size_t>();
515 if (ndim != 0 && ndim != 1) {
516 throw py::value_error("ndim != 1 or 0, get : " + std::to_string(ndim));
517 }
518 if (!is_tensor(value)) {
519 return get_res_by_refhdl(value, dtype, device, ref);
520 } else {
521 return py::reinterpret_borrow<py::object>(value);
522 }
523 }
524 if (!is_py_sequence(value)) {
525 throw py::type_error();
526 }
527 py::list lis = py::reinterpret_steal<py::list>(PySequence_List(value.ptr()));
528 bool need_concat = false;
529 for (size_t i = 0; i < lis.size(); ++i) {
530 if (is_tensor(lis[i])) {
531 need_concat = true;
532 break;
533 }
534 }
535 if (!need_concat) {
536 return get_res_by_refhdl(value, dtype, device, ref);
537 }
538 if (lis.size() > 1) {
539 py::list flat_list;

Callers 5

_expand_bool_dimFunction · 0.85
_broadcast_cppFunction · 0.85
_reshape_cppFunction · 0.85
_adaptive_pool2d_cppFunction · 0.85
tensor_utils.cppFile · 0.85

Calls 15

device2objFunction · 0.85
_astype_cppFunction · 0.85
castFunction · 0.85
py_applyFunction · 0.85
to_stringFunction · 0.85
is_tensorFunction · 0.85
get_res_by_refhdlFunction · 0.85
is_py_sequenceFunction · 0.85
_get_deviceFunction · 0.85
resizeMethod · 0.80
makeFunction · 0.50
ptrMethod · 0.45

Tested by

no test coverage detected