Repository navigation
Fix bug on the gradients graph computation caused by an Assign node - C++ API - #12397
Conversation
|
Can one of the admins verify this patch? |
skye
left a comment
There was a problem hiding this comment.
Question: does this bug also manifest in the following situation?
Tanh1 Tanh2
| |
MatMul1 MatMul2
/ \ / \
Const1 Const2 Const3
And then we take the gradient of Tanh2 w.r.t. Const2. Will Const2 still have a pending count of 2, which only gets decreased to 1 since the BFS from Const2 to Tanh2 doesn't traverse MatMul1?
| // Construct forward graph. | ||
| auto x = Const(scope, {{1.0, 2.0}, {3.0, 4.0}}); | ||
| auto y = Const(scope, {{1.0, 0.0}, {0.0, 1.0}}); | ||
| auto x = ThreeNodeVariable(scope, {{1.0, 2.0}, {3.0, 4.0}}); |
There was a problem hiding this comment.
Please keep the existing tests as-is, unless there's a good reason to switch to ThreeNodeVariable (which I don't think there is, since we're take the gradient w.r.t. to the Const nodes so it shouldn't make a difference). The Const nodes are simpler to reason about, and also your changes make the comment diagrams inaccurate.
Instead, can you add a new test case that repros the problem? This should make it easier to see the potential problem, and should also remove the need for the VariableHelper class and multiple inheritance.
There was a problem hiding this comment.
@skye: I chose to edit the tests because as far as I know we don't differentiate w.r.t a Const. Else people will differentiate w.r.t a Const W1 for instance and then create an ApplyGradientDescent node that updates W1 using the collected gradient. Updating a Const is in opposition to the term const.
So a test passing with Const nodes is worthless because in reality it will not be done like that but with Variables (I may be wrong?).
And more importantly, everything was working fine with Const nodes but wasn't with Variable nodes.
There was a problem hiding this comment.
You're right that differentiating w.r.t. Const nodes isn't realistic and prevented us from catching this error. However, it makes the unit tests simpler and easier to understand. The tests aren't worthless, they just test specific functionality, which is the idea of a unit test.
I think a good solution to the lack of coverage would be to have one or two "real world" tests that differentiate realistic graphs (we'll hopefully get these automatically when we switch the Python code to use this code under the hood). But I don't think we should add this much complexity to all of our existing unit tests in order to get slightly more coverage. For now, let's stick with the new tests you wrote that cover this particular case.
|
@skye: Yes the bug also manifest in this situation. I'll open an issue and work on a solution. |
|
@skye: I made changes in the gradient computation method to handle the case you outlined, I create a vector of boolean called I added two more test cases for the case you outlined. I did not remove the stuff about |
| // If a node is not reachable when doing a BFS from the outputs | ||
| // we don't expect it to receive a backpropagated gradient. | ||
| // It will not be counted in num_expected_backprops. | ||
| std::vector<bool> reachable_nodes(scope_.graph()->num_node_ids(), false); |
There was a problem hiding this comment.
This function is already quite long, can you break this out into a new function? Something like GetReachableNodes().
| // handle computing gradients over small subgraphs from a very large graph. | ||
| pending_.resize(scope_.graph()->num_node_ids(), 0); | ||
| { | ||
| // If a node is not reachable when doing a BFS from the outputs |
There was a problem hiding this comment.
Please move this comment to where you use reachable_nodes. I think it makes more sense there (e.g. num_expected_backprops isn't defined yet).
| // Construct forward graph. | ||
| auto x = Const(scope, {{1.0, 2.0}, {3.0, 4.0}}); | ||
| auto y = Const(scope, {{1.0, 0.0}, {0.0, 1.0}}); | ||
| auto x = ThreeNodeVariable(scope, {{1.0, 2.0}, {3.0, 4.0}}); |
There was a problem hiding this comment.
You're right that differentiating w.r.t. Const nodes isn't realistic and prevented us from catching this error. However, it makes the unit tests simpler and easier to understand. The tests aren't worthless, they just test specific functionality, which is the idea of a unit test.
I think a good solution to the lack of coverage would be to have one or two "real world" tests that differentiate realistic graphs (we'll hopefully get these automatically when we switch the Python code to use this code under the hood). But I don't think we should add this much complexity to all of our existing unit tests in order to get slightly more coverage. For now, let's stick with the new tests you wrote that cover this particular case.
|
@skye: Normally all your comments have been followed. |
skye
left a comment
There was a problem hiding this comment.
Looking good! It's mostly style comments at this point.
| void GetTensors(const Scope& scope, const std::vector<Output>& assign_vars, | ||
| OutputList tensors, std::vector<Tensor>* out) { | ||
| ClientSession session(scope); | ||
| TF_CHECK_OK(session.Run(assign_vars, nullptr)); |
There was a problem hiding this comment.
Instead of making special GetTensors overloads that do two Run calls, just call GetTensors() twice in your tests.
There was a problem hiding this comment.
Actually as-is GetTensors instantiates a new ClientSession at each call nonetheless I need to use the same session twice. If you don't want the overload I can instantiate and use a session directly from the test, what do you think?
There was a problem hiding this comment.
Oh duh. What you have is fine then.
|
|
||
| // If a node is reachable when doing a BFS from outputs toward inputs | ||
| // its id used as index in reachable_nodes is set to true. | ||
| void GetReachableNodes(std::vector<bool>* reachable_nodes); |
There was a problem hiding this comment.
Return the vector directly instead of using an output param (move semantics make this efficient).
| reachable_nodes->clear(); | ||
| reachable_nodes->resize(scope_.graph()->num_node_ids(), false); | ||
|
|
||
| std::deque<Node*> queue_reachable; |
There was a problem hiding this comment.
nit: queue_reachable sounds strange to me, just rename to 'queue'
|
|
||
| std::deque<Node*> queue_reachable; | ||
| for (const Output& out : outputs_) { | ||
| if (!reachable_nodes->at(out.node()->id())) { |
There was a problem hiding this comment.
use [] instead of at() (it's more idiomatic, and we don't use exceptions) (here and below)
| queue_reachable.pop_front(); | ||
| for (const Edge* e : n->in_edges()) { | ||
| if (e->IsControlEdge()) continue; | ||
| LOG(INFO) << e->src()->DebugString(); |
| for (const Edge* e : n->out_edges()) { | ||
| if (e->IsControlEdge()) continue; | ||
| ++num_expected_backprops; | ||
| // If a node is not reachable when doing a BFS from the outputs |
There was a problem hiding this comment.
You can just say: "If a node is not reachable from outputs_, ..."
| std::vector<Output>* grad_outputs); | ||
|
|
||
| // If a node is reachable when doing a BFS from outputs toward inputs | ||
| // its id used as index in reachable_nodes is set to true. |
There was a problem hiding this comment.
How about: "Returns a list mapping whether each node in the graph is reachable from outputs_. Keyed by node id."
| { | ||
| std::vector<bool> reachable_nodes; | ||
| GetReachableNodes(&reachable_nodes); | ||
|
|
There was a problem hiding this comment.
Can you add a TODO to check that inputs_ are reachable from outputs_ here? (It would be easy to add the check now, but let's save it for another PR since it will require another test case.)
There was a problem hiding this comment.
I moved std::vector<bool> reachable_nodes = GetReachableNodes(); at the beginning of the method as we'll check the errors at the beginning (the TODO).
| {grad_outputs[0]}, &outputs); | ||
|
|
||
| // the gradients from m1 and m2 will be summed to compute the gradient | ||
| // w.r.t y |
There was a problem hiding this comment.
Can you add this comment to the above test as well?
There was a problem hiding this comment.
Above the output given to AddSymbolicGradients is m1 so the gradient is backpropagated from m1 but not from m2, so there is no sum. Here the output given to AddSymbolicGradients is {m1, m2} so the gradients from m1 and m2 are summed. That's why the comment is only here. I guess it should be like that?
Else I agree with all others comments and will update soon.
There was a problem hiding this comment.
Ah right. Can you comment how you computed the gradient for above test then?
|
|
||
| // Computes the outputs listed in 'tensors', returns the tensors in 'out'. | ||
| // Take a vector of Output as extra parameters if some Variable should | ||
| // be assigned before the session run call. |
There was a problem hiding this comment.
Grammar nit: change the second sentence to:
assign_vars are extra outputs that should be run e.g. to assign values to variables.
There was a problem hiding this comment.
It looks like you changed the comment below, also change it up here please.
|
@skye Updated following your comments. |
skye
left a comment
There was a problem hiding this comment.
I'll approve after you fix the one last comment :)
skye
left a comment
There was a problem hiding this comment.
Looks good, thanks for this contribution!
|
Jenkins, test this please |
|
@jhseu The failing tests seem unrelated to the PR content no? |
|
@theflofly I had those same two checks fail and fixed them just by merging master back into my branch. Worth a try? |
|
@bpiel done, let's see. |
|
@martinwicke why does it say "TF Code Review — Waiting for status to be reported"? Do I need to do something else? |
|
It means someone has to run the tests. Jenkins, test this please! |
|
One test failing. https://ci.tensorflow.org/job/tensorflow-pull-requests-cpu-python3/6212/console |
|
@bpiel I merged with master once again... If someone has some insights? |
|
I have a branch with this branch merged into it. I attempted to run the failing test (from above), but it passed for me. Not sure what that means. |
|
@bpiel Thanks for your help on the issue. Let's wait to hear from the google team but it would be great if this could be merged. |
|
@theflofly Thank you for taking this on! I'm relying on the C API |
|
@bpiel Thanks for the kind words. Does it work if us contributor are writing: |
|
Jenkins, test this please. No it doesn't work. Sorry. |
|
🎉 thanks! |
|
Thanks! :) |
The gradient computation in the C++ API works as follow.
Let's say that we have the following graph:
Here our Output is Tanh and our Input is Var. The gradient method does a BFS from Var to Tanh to count for each node how many backprop we should expect. If a node has two outgoing edges, it will be ready only when both would have been backpropagated and the gradient summed. In our case, Var has 2, Assign 0, MatMul 1. These values are saved into a pending array.
Then the gradient method does a BFS from Tanh to Var, this is the actual backpropagation, the error is backpropagated until we reach Var. When a Node is reached, pending is decreased by one and if pending == 0, the Node is ready and is added to the queue of Nodes to be processed. If it is not ready, it will be reached again in the future and will be ready at some point.
In our case, doing a BFS from Var to Tanh give us 2 expected gradients, one from Assign and one from MatMul, whereas doing a BFS from Tanh to Var, we will reach Var only once, because we can't reach Assign from Tanh. In that case, the pending count will never reach Zero, Var will never be ready and the BFS will end. At the end, a check is done and if pending nodes are still there, an error is raised.
This PR updates the gradient method to ignore nodes that have 0 outgoing edges and are not in the list of Output (Tanh is the only one in the list of outputs in our case).
The unit tests have been updated to use Variable with Assign nodes and not Const, because differentiating w.r.t Const make less sense and the error would have been catched before.