| 268 | } |
| 269 | |
| 270 | TFE_TensorHandle* ConvertToEagerTensorUncached(TFE_Context* ctx, |
| 271 | PyObject* value, |
| 272 | tensorflow::DataType dtype, |
| 273 | const char* device_name) { |
| 274 | tensorflow::Safe_PyObjectPtr value_decrefer; |
| 275 | if (PyArray_IsScalar(value, Generic)) { |
| 276 | // Convert numpy scalars to numpy arrays. |
| 277 | value = PyArray_FromScalar(value, nullptr); |
| 278 | // The returned value needs to be DECREF'd, but the original value was |
| 279 | // created in python code, and doesn't need to be DECREF'd. |
| 280 | value_decrefer.reset(value); |
| 281 | } |
| 282 | |
| 283 | Safe_TFE_TensorHandlePtr handle; |
| 284 | if (PyArray_Check(value)) { |
| 285 | int desired_np_dtype = -1; |
| 286 | if (dtype != tensorflow::DT_INVALID) { |
| 287 | if (!tensorflow::TF_DataType_to_PyArray_TYPE( |
| 288 | static_cast<TF_DataType>(dtype), &desired_np_dtype) |
| 289 | .ok()) { |
| 290 | PyErr_SetString( |
| 291 | PyExc_TypeError, |
| 292 | tensorflow::strings::StrCat("Invalid dtype argument value ", dtype) |
| 293 | .c_str()); |
| 294 | return nullptr; |
| 295 | } |
| 296 | } |
| 297 | PyArrayObject* array = reinterpret_cast<PyArrayObject*>(value); |
| 298 | int current_np_dtype = PyArray_TYPE(array); |
| 299 | auto safe_value = tensorflow::make_safe(static_cast<PyObject*>(nullptr)); |
| 300 | if ((desired_np_dtype >= 0 && desired_np_dtype != current_np_dtype) || |
| 301 | !PyArray_ISCARRAY(array)) { |
| 302 | int new_dtype = |
| 303 | desired_np_dtype >= 0 ? desired_np_dtype : current_np_dtype; |
| 304 | safe_value = tensorflow::make_safe( |
| 305 | PyArray_FromAny(value, PyArray_DescrFromType(new_dtype), 0, 0, |
| 306 | NPY_ARRAY_CARRAY_RO | NPY_ARRAY_FORCECAST, nullptr)); |
| 307 | if (PyErr_Occurred()) return nullptr; |
| 308 | if (safe_value == nullptr) { |
| 309 | PyErr_SetString(PyExc_ValueError, "Error while casting a numpy value"); |
| 310 | return nullptr; |
| 311 | } |
| 312 | value = safe_value.get(); |
| 313 | } |
| 314 | handle = make_safe(NumpyToTFE_TensorHandle(value)); |
| 315 | } else { |
| 316 | handle = make_safe(PySeqToTFE_TensorHandle(value, dtype)); |
| 317 | } |
| 318 | |
| 319 | if (handle == nullptr) return nullptr; |
| 320 | |
| 321 | Safe_TF_StatusPtr status = make_safe(TF_NewStatus()); |
| 322 | TF_DataType handle_dtype = TFE_TensorHandleDataType(handle.get()); |
| 323 | if (dtype != tensorflow::DT_INVALID && |
| 324 | dtype != static_cast<DataType>(handle_dtype)) { |
| 325 | if (tensorflow::IsCompatible(dtype, static_cast<DataType>(handle_dtype))) { |
| 326 | handle = tensorflow::make_safe( |
| 327 | tensorflow::EagerCast(ctx, handle.get(), handle_dtype, |
no test coverage detected