Determine the type description (PyArray_Descr) of a numpy ndarray to be created to represent an output Tensor.
| 343 | // Determine the type description (PyArray_Descr) of a numpy ndarray to be |
| 344 | // created to represent an output Tensor. |
| 345 | Status GetPyArrayDescrForTensor(const TF_Tensor* tensor, |
| 346 | PyArray_Descr** descr) { |
| 347 | if (TF_TensorType(tensor) == TF_RESOURCE) { |
| 348 | PyObject* field = PyTuple_New(3); |
| 349 | #if PY_MAJOR_VERSION < 3 |
| 350 | PyTuple_SetItem(field, 0, PyBytes_FromString("resource")); |
| 351 | #else |
| 352 | PyTuple_SetItem(field, 0, PyUnicode_FromString("resource")); |
| 353 | #endif |
| 354 | PyTuple_SetItem(field, 1, PyArray_TypeObjectFromType(NPY_UBYTE)); |
| 355 | PyTuple_SetItem(field, 2, PyLong_FromLong(1)); |
| 356 | PyObject* fields = PyList_New(1); |
| 357 | PyList_SetItem(fields, 0, field); |
| 358 | int convert_result = PyArray_DescrConverter(fields, descr); |
| 359 | Py_CLEAR(field); |
| 360 | Py_CLEAR(fields); |
| 361 | if (convert_result != 1) { |
| 362 | return errors::Internal("Failed to create numpy array description for ", |
| 363 | "TF_RESOURCE-type tensor"); |
| 364 | } |
| 365 | } else { |
| 366 | int type_num = -1; |
| 367 | TF_RETURN_IF_ERROR( |
| 368 | TF_DataType_to_PyArray_TYPE(TF_TensorType(tensor), &type_num)); |
| 369 | *descr = PyArray_DescrFromType(type_num); |
| 370 | } |
| 371 | |
| 372 | return Status::OK(); |
| 373 | } |
| 374 | |
| 375 | inline void FastMemcpy(void* dst, const void* src, size_t size) { |
| 376 | // clang-format off |
no test coverage detected