MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ConvertToTensor

Function ConvertToTensor

tensorflow/python/eager/pywrap_tfe_src.cc:2696–2726  ·  view source on GitHub ↗

Supports 3 cases at the moment: i) input is an EagerTensor. ii) input is a ResourceVariable - in this case, the is_variable param is set to true. iii) input is an arbitrary python list/tuple (note, this handling doesn't support packing). NOTE: dtype_hint_getter must *always* return a PyObject that can be decref'd. So if no hint is found, Py_RETURN_NONE (which correctly increfs Py_None). NOTE: Th

Source from the content-addressed store, hash-verified

2694// NOTE: This function sets a python error directly, and returns false.
2695// TF_Status is only passed since we don't want to have to reallocate it.
2696bool ConvertToTensor(
2697 const FastPathOpExecInfo& op_exec_info, PyObject* input,
2698 tensorflow::Safe_PyObjectPtr* output_handle,
2699 // This gets a hint for this particular input.
2700 const std::function<tensorflow::DataType()>& dtype_hint_getter,
2701 // This sets the dtype after conversion is complete.
2702 const std::function<void(const tensorflow::DataType dtype)>& dtype_setter,
2703 TF_Status* status) {
2704 if (EagerTensor_CheckExact(input)) {
2705 Py_INCREF(input);
2706 output_handle->reset(input);
2707 return true;
2708 } else if (CheckResourceVariable(input)) {
2709 return ReadVariableOp(op_exec_info, input, output_handle, status);
2710 }
2711
2712 // The hint comes from a supposedly similarly typed tensor.
2713 tensorflow::DataType dtype_hint = dtype_hint_getter();
2714
2715 TFE_TensorHandle* handle = tensorflow::ConvertToEagerTensor(
2716 op_exec_info.ctx, input, dtype_hint, op_exec_info.device_name);
2717 if (handle == nullptr) {
2718 return MaybeRaiseExceptionFromTFStatus(status, nullptr);
2719 }
2720
2721 output_handle->reset(EagerTensorFromHandle(handle));
2722 dtype_setter(
2723 static_cast<tensorflow::DataType>(TFE_TensorHandleDataType(handle)));
2724
2725 return true;
2726}
2727
2728// Adds input and type attr to the op, and to the list of flattened
2729// inputs/attrs.

Callers 2

AddInputToOpFunction · 0.70
TFE_Py_FastPathExecute_CFunction · 0.70

Calls 7

CheckResourceVariableFunction · 0.85
ConvertToEagerTensorFunction · 0.85
EagerTensorFromHandleFunction · 0.85
TFE_TensorHandleDataTypeFunction · 0.85
ReadVariableOpFunction · 0.70
resetMethod · 0.45

Tested by

no test coverage detected