MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / PyTensorObject_new

Function PyTensorObject_new

oneflow/api/python/framework/tensor_functions.cpp:853–907  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

851}
852
853static PyObject* PyTensorObject_new(PyObject* self, PyObject* args, PyObject* kwargs) {
854 HANDLE_ERRORS
855 auto self_tensor = PyTensor_Unpack(self);
856
857 if (!kwargs) {
858 if (PyTuple_Size(args) == 1 && PyTensor_Check(PyTuple_GET_ITEM(args, 0))) {
859 // tensor.new(other)
860 auto other_tensor = PyTensor_Unpack(PyTuple_GET_ITEM(args, 0));
861 CHECK_OR_THROW(!self_tensor->is_global() && !other_tensor->is_global())
862 << "Tensor.new(Tensor) only support local tensor.";
863 CHECK_OR_THROW(self_tensor->dtype() == other_tensor->dtype())
864 << "Tensor.new() expect " << self_tensor->dtype()->name() << " dtype tensor, but got "
865 << other_tensor->dtype()->name() << " dtype tensor.";
866 CHECK_OR_THROW(ASSERT(self_tensor->device())->enum_type()
867 == ASSERT(other_tensor->device())->enum_type())
868 << "Tensor.new() expect tensor on " << ASSERT(self_tensor->device())->type()
869 << ", but got tensor on " << ASSERT(other_tensor->device())->type() << ".";
870 return PyTensor_New(ASSERT_PTR(functional::TensorWithOtherCtor(other_tensor)));
871 }
872 kwargs = PyDict_New();
873 }
874 PyObjectPtr dtype_key(PyUnicode_FromString("dtype"));
875 PyObjectPtr dtype_value(functional::CastToPyObject(self_tensor->dtype()));
876 CHECK_OR_THROW(PyDict_Contains(kwargs, dtype_key.get()) < 1);
877 CHECK_OR_THROW(PyDict_SetItemString(kwargs, "dtype", dtype_value.get()) > -1);
878
879 if (self_tensor->is_global()) {
880 PyObjectPtr placement_key(PyUnicode_FromString("placement"));
881 PyObjectPtr sbp_key(PyUnicode_FromString("sbp"));
882 CHECK_OR_THROW(PyDict_Contains(kwargs, placement_key.get()) < 1);
883 CHECK_OR_THROW(PyDict_Contains(kwargs, sbp_key.get()) < 1);
884
885 Symbol<ParallelDesc> placement = ASSERT(self_tensor->parallel_desc());
886 std::vector<Symbol<SbpParallel>> sbp;
887 auto ndsbp = ASSERT(self_tensor->nd_sbp());
888 for (int32_t i = 0; i < ndsbp->sbp_parallel_size(); i++) {
889 sbp.emplace_back(ndsbp->sbp_parallel(i));
890 }
891
892 PyObjectPtr placement_value(functional::CastToPyObject(placement));
893 PyObjectPtr sbp_value(functional::CastToPyObject(sbp));
894 CHECK_OR_THROW(PyDict_SetItemString(kwargs, "placement", placement_value.get()) > -1);
895 CHECK_OR_THROW(PyDict_SetItemString(kwargs, "sbp", sbp_value.get()) > -1);
896 } else {
897 auto device = ASSERT(self_tensor->device());
898
899 PyObjectPtr device_key(PyUnicode_FromString("device"));
900 CHECK_OR_THROW(PyDict_Contains(kwargs, device_key.get()) < 1)
901 << "Some of the keywords were incorrect: device";
902 PyObjectPtr device_value(functional::CastToPyObject(device));
903 CHECK_OR_THROW(PyDict_SetItemString(kwargs, "device", device_value.get()) > -1);
904 }
905 return functional::_legacy_tensor_generic_ctor(NULL, args, kwargs);
906 END_HANDLE_ERRORS
907}
908
909int PyTensorObject_setitem(PyObject* self, PyObject* item, PyObject* value) {
910 HANDLE_ERRORS

Callers

nothing calls this directly

Calls 13

PyTensor_CheckFunction · 0.85
PyTensor_NewFunction · 0.85
CastToPyObjectFunction · 0.85
enum_typeMethod · 0.80
is_globalMethod · 0.45
dtypeMethod · 0.45
nameMethod · 0.45
deviceMethod · 0.45
typeMethod · 0.45
getMethod · 0.45
parallel_descMethod · 0.45
nd_sbpMethod · 0.45

Tested by

no test coverage detected