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

Function TFE_Py_TapeGradient

tensorflow/python/eager/pywrap_tfe_src.cc:1979–2088  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1977}
1978
1979PyObject* TFE_Py_TapeGradient(PyObject* tape, PyObject* target,
1980 PyObject* sources, PyObject* output_gradients,
1981 PyObject* sources_raw,
1982 PyObject* unconnected_gradients,
1983 TF_Status* status) {
1984 TFE_Py_Tape* tape_obj = reinterpret_cast<TFE_Py_Tape*>(tape);
1985 if (!tape_obj->tape->IsPersistent()) {
1986 auto* tape_set = GetTapeSet();
1987 if (tape_set->find(tape_obj) != tape_set->end()) {
1988 PyErr_SetString(PyExc_RuntimeError,
1989 "gradient() cannot be invoked within the "
1990 "GradientTape context (i.e., while operations are being "
1991 "recorded). Either move the call to gradient() to be "
1992 "outside the 'with tf.GradientTape' block, or "
1993 "use a persistent tape: "
1994 "'with tf.GradientTape(persistent=true)'");
1995 return nullptr;
1996 }
1997 }
1998
1999 std::vector<tensorflow::int64> target_vec = MakeTensorIDList(target);
2000 if (PyErr_Occurred()) {
2001 return nullptr;
2002 }
2003 std::vector<tensorflow::int64> sources_vec = MakeTensorIDList(sources);
2004 if (PyErr_Occurred()) {
2005 return nullptr;
2006 }
2007 tensorflow::gtl::FlatSet<tensorflow::int64> sources_set(sources_vec.begin(),
2008 sources_vec.end());
2009
2010 tensorflow::Safe_PyObjectPtr seq =
2011 tensorflow::make_safe(PySequence_Fast(target, "expected a sequence"));
2012 int len = PySequence_Fast_GET_SIZE(seq.get());
2013 std::unordered_map<tensorflow::int64, PyTapeTensor>
2014 source_tensors_that_are_targets;
2015 for (int i = 0; i < len; ++i) {
2016 tensorflow::int64 target_id = target_vec[i];
2017 if (sources_set.find(target_id) != sources_set.end()) {
2018 auto tensor = PySequence_Fast_GET_ITEM(seq.get(), i);
2019 source_tensors_that_are_targets.insert(
2020 std::make_pair(target_id, TapeTensorFromTensor(tensor)));
2021 }
2022 if (PyErr_Occurred()) {
2023 return nullptr;
2024 }
2025 }
2026 if (PyErr_Occurred()) {
2027 return nullptr;
2028 }
2029
2030 std::vector<PyObject*> outgrad_vec;
2031 if (output_gradients != Py_None) {
2032 outgrad_vec = MakeTensorList(output_gradients);
2033 if (PyErr_Occurred()) {
2034 return nullptr;
2035 }
2036 for (PyObject* tensor : outgrad_vec) {

Callers

nothing calls this directly

Calls 15

MakeTensorIDListFunction · 0.85
TapeTensorFromTensorFunction · 0.85
MakeTensorListFunction · 0.85
TFE_GetPythonStringFunction · 0.85
FastTensorDtypeFunction · 0.85
PyTapeTensorClass · 0.85
IsPersistentMethod · 0.80
ComputeGradientMethod · 0.80
make_safeFunction · 0.50
findMethod · 0.45
endMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected