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

Function TapeTensorFromTensor

tensorflow/python/eager/pywrap_tfe_src.cc:1711–1762  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1709}
1710
1711static PyTapeTensor TapeTensorFromTensor(PyObject* tensor) {
1712 if (EagerTensor_CheckExact(tensor)) {
1713 TFE_TensorHandle* t = EagerTensor_Handle(tensor);
1714 tensorflow::int64 id = PyEagerTensor_ID(tensor);
1715 tensorflow::TensorShape tensor_shape;
1716 const tensorflow::Status status = t->handle->Shape(&tensor_shape);
1717
1718 if (MaybeRaiseExceptionFromStatus(status, nullptr)) {
1719 return PyTapeTensor(id, static_cast<tensorflow::DataType>(0),
1720 tensorflow::TensorShape({}));
1721 } else {
1722 return PyTapeTensor(id, t->handle->dtype, tensor_shape);
1723 }
1724 }
1725 tensorflow::int64 id = FastTensorId(tensor);
1726 if (PyErr_Occurred()) {
1727 return PyTapeTensor(id, static_cast<tensorflow::DataType>(0),
1728 tensorflow::TensorShape({}));
1729 }
1730 PyObject* dtype_object = PyObject_GetAttrString(tensor, "dtype");
1731 PyObject* dtype_enum = PyObject_GetAttrString(dtype_object, "_type_enum");
1732 Py_DECREF(dtype_object);
1733 tensorflow::DataType dtype =
1734 static_cast<tensorflow::DataType>(MakeInt(dtype_enum));
1735 Py_DECREF(dtype_enum);
1736 if (PyErr_Occurred()) {
1737 return PyTapeTensor(id, static_cast<tensorflow::DataType>(0),
1738 tensorflow::TensorShape({}));
1739 }
1740 static char _shape_tuple[] = "_shape_tuple";
1741 PyObject* shape_tuple = PyObject_CallMethod(tensor, _shape_tuple, nullptr);
1742 if (PyErr_Occurred()) {
1743 return PyTapeTensor(id, static_cast<tensorflow::DataType>(0),
1744 tensorflow::TensorShape({}));
1745 }
1746
1747 if (ListContainsNone(shape_tuple)) {
1748 return PyTapeTensor(id, dtype, tensor);
1749 }
1750
1751 auto l = MakeIntList(shape_tuple);
1752 Py_DECREF(shape_tuple);
1753 // Replace -1, which represents accidental Nones which can occur in graph mode
1754 // and can cause errors in shape cosntruction with 0s.
1755 for (auto& c : l) {
1756 if (c < 0) {
1757 c = 0;
1758 }
1759 }
1760 tensorflow::TensorShape shape(l);
1761 return PyTapeTensor(id, dtype, shape);
1762}
1763
1764std::vector<tensorflow::int64> MakeTensorIDList(PyObject* tensors) {
1765 PyObject* seq = PySequence_Fast(tensors, "expected a sequence");

Callers 3

TapeSetRecordOperationFunction · 0.85
TFE_Py_TapeGradientFunction · 0.85

Calls 9

PyEagerTensor_IDFunction · 0.85
PyTapeTensorClass · 0.85
FastTensorIdFunction · 0.85
MakeIntFunction · 0.85
ListContainsNoneFunction · 0.85
MakeIntListFunction · 0.85
TensorShapeClass · 0.50
ShapeMethod · 0.45

Tested by

no test coverage detected