MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / Grad

Function Grad

oneflow/api/python/autograd/autograd.cpp:117–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117Maybe<one::TensorTuple> Grad(const one::TensorTuple& outputs, const one::TensorTuple& inputs,
118 const one::TensorTuple& out_grads, bool retain_graph,
119 bool create_graph, bool allow_unused, bool is_grads_batched) {
120 PythonFrameGuard pf;
121 BackwardPassScopeGuard backward_guard;
122 if (create_graph) { retain_graph = true; }
123 if (inputs.empty()) { return Backward(outputs, out_grads, retain_graph, create_graph); }
124 CHECK_OR_RETURN(std::all_of(
125 inputs.begin(), inputs.end(),
126 [](const std::shared_ptr<one::Tensor>& tensor) { return tensor->requires_grad(); }))
127 << "All input tensors `.requires_grad` should be true";
128 std::shared_ptr<one::TensorTuple> gradients =
129 JUST(CheckAndInitOutGrads(outputs, out_grads, is_grads_batched));
130 return one::GetThreadLocalAutogradEngine()->RunBackwardAndReturnInputsTensorGradIf(
131 outputs, inputs, *gradients, retain_graph, create_graph, allow_unused);
132}
133
134namespace py = pybind11;
135

Callers

nothing calls this directly

Calls 8

BackwardFunction · 0.85
CheckAndInitOutGradsFunction · 0.85
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
requires_gradMethod · 0.45

Tested by

no test coverage detected