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

Function ParseScalar

oneflow/api/python/functional/indexing.cpp:88–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86}
87
88void ParseScalar(PyObject* object, char* data, const DataType& dtype) {
89 if (dtype == DataType::kInt64) {
90 CHECK_OR_THROW(PyLong_Check(object) || numpy::PyArrayCheckLongScalar(object))
91 << "Expected a long value.";
92 *(reinterpret_cast<int64_t*>(data)) = PyLong_AsLongLong(object);
93 } else if (dtype == DataType::kInt32) {
94 CHECK_OR_THROW(PyLong_Check(object) || numpy::PyArrayCheckLongScalar(object))
95 << "Expected a long value.";
96 *(reinterpret_cast<int32_t*>(data)) = PyLong_AsLongLong(object);
97 } else if (dtype == DataType::kUInt8 || dtype == DataType::kBool) {
98 CHECK_OR_THROW(PyBool_Check(object) || PyLong_Check(object)
99 || numpy::PyArrayCheckLongScalar(object))
100 << "Expected a boolean or long value.";
101 if (PyBool_Check(object) || numpy::PyArrayCheckBoolScalar(object)) {
102 *(reinterpret_cast<bool*>(data)) = (object == Py_True);
103 } else {
104 int64_t value = PyLong_AsLongLong(object);
105 CHECK_OR_THROW(value >= 0 && value <= 255) << "Out of range 0-255.";
106 *(reinterpret_cast<uint8_t*>(data)) = static_cast<uint8_t>(value);
107 }
108 } else {
109 THROW(TypeError) << "Can't parse scalar with data type " << dtype;
110 }
111}
112
113void RecursiveParseAndAssign(PyObject* object, char* data, const int& ndims, const int& dim,
114 const ShapeView& shape, const DimVector& strides,

Callers 1

RecursiveParseAndAssignFunction · 0.85

Calls 2

PyArrayCheckLongScalarFunction · 0.85
PyArrayCheckBoolScalarFunction · 0.85

Tested by

no test coverage detected