# Conditional computation that saves computation

**URL:** <https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878>\
**Category:** Uncategorized\
**Created:** [March 13, 2018, 11:49pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878 "2018-03-13T23:49:21Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![rahul](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rahul](https://discuss.pytorch.org/u/rahul)\
**Post date:** [March 17, 2018, 8:39pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/21 "2018-03-17T20:39:49Z")

</div>

Hi Alban,  
it seems that the technique you suggested here, doesn’t work. I am having issues with index\_add. The code that you provided works fine. Bu when I try to do something similar in my algorithm, it does not work. I could not find much information related to index\_add. Is it a popularly used function?

---

<div class="post-metadata">

**Author:** ![rahul](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rahul](https://discuss.pytorch.org/u/rahul)\
**Post date:** [March 19, 2018, 11:19pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/22 "2018-03-19T23:19:52Z")

</div>

Hi,  
its not possible to backpropagate through the graphs! I get the following error  
"NoneType object has no attribute data "

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 19, 2018, 11:32pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/23 "2018-03-19T23:32:21Z")

</div>

If I take Alban’s example, wrap x and y in Variables, and add `out.sum().backward()` at the end, then it backpropagates with no problem.

Unless you can show us your code and a full stack trace, then I don’t think anyone can help you track down your bug.

---

<div class="post-metadata">

**Author:** ![rahul](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rahul](https://discuss.pytorch.org/u/rahul)\
**Post date:** [March 20, 2018, 12:01am UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/24 "2018-03-20T00:01:14Z")

</div>

Hi,  
so in your case, you are differentiating with respect to the inputs (say input images)? Isn’t that unconventional?  
In my case, the graph is more complex. I use a NN to make decisions whether or not to use the next layer in MLP. However, when I try to do backpropagation, I get the error.  
Thanks

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 20, 2018, 12:14am UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/25 "2018-03-20T00:14:08Z")

</div>

The code examples work and will backpropagate correctly and unless you provide code I can’t help you any more than that.

If you don’t want to show your code can you at least produce a minimal code example that demonstrates the error?

---

<div class="post-metadata">

**Author:** ![rahul](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rahul](https://discuss.pytorch.org/u/rahul)\
**Post date:** [March 20, 2018, 12:16am UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/26 "2018-03-20T00:16:06Z")

</div>

Hi,  
sorry for that, my code is very long. I am working to produce a very simple working example. I will get back to you very soon.  
Thanks

---

<div class="post-metadata">

**Author:** ![sshkhr](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sshkhr/32/8575_2.png) [@sshkhr](https://discuss.pytorch.org/u/sshkhr)\
**Post date:** [July 29, 2021, 3:44pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/27 "2021-07-29T15:44:18Z")

</div>

If I want to use this approach to conditional computation in the “central” part of my model it will lead to an autodiff issue.

The `out` tensor here is a Leaf node. In my case, the tensor where I split my input batch and then combine them is an intermediate node in the computational graph, so I am performing further operations on it. Hence I initialize it with a `requires_grad=true` flag.

As a result, when I try to use `index_add_` it throws a RuntimeError:

> a leaf Variable that requires grad is being used in an in-place operation.

What would be a workaround for it? This thread suggests making a clone or editing the data object of the variable directly: [Leaf variable was used in an inplace operation](https://discuss.pytorch.org/t/leaf-variable-was-used-in-an-inplace-operation/308)

Would love to hear either of your feedbacks too @jpeg729 or @rahul since this thread has been around for quite a while!

Thanks in advance 🙂

EDIT: Nevermind I used `torch.Tensor.index_add` (the out of place version). However, I am still facing issues with training as my loss does not seem to be changing. So I suspect backprop is not working right.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [July 29, 2021, 5:35pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/28 "2021-07-29T17:35:42Z")

</div>

Hi,

It is completely ok to have out not require grad and then modify it inplace with something that does. It will make out require gradients and the gradients will propagate as expected 🙂

Note that if you are not sure if backprop is working properly for a function (and the function is small enough), you can always try to use `gradcheck`.  
Be aware though that you must use double precision, you function must be smooth at the point where you’re checking the gradients and that if your function is too big, the numerical evaluation of the gradient might be too imprecise and the test might fail for no reason.

---

<div class="post-metadata">

**Author:** ![sshkhr](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sshkhr/32/8575_2.png) [@sshkhr](https://discuss.pytorch.org/u/sshkhr)\
**Post date:** [July 29, 2021, 7:44pm UTC](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878/29 "2021-07-29T19:44:07Z")

</div>

Thanks for your feedback. In your example `nn2_ind` and `nn2_ind` will have `requires_grad = False` and as far as I can tell these tensors cannot be set to have `requires_grad = True` Could that potentially be leading to issues with autodiff?

[Previous page](https://discuss.pytorch.org/t/conditional-computation-that-saves-computation/14878.md?page=1)
