# How does torch.compile work with autograd?

**URL:** <https://dev-discuss.pytorch.org/t/how-does-torch-compile-work-with-autograd/1621>\
**Category:** Uncategorized\
**Created:** [October 30, 2023, 1:43pm UTC](https://dev-discuss.pytorch.org/t/how-does-torch-compile-work-with-autograd/1621 "2023-10-30T13:43:13Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![youkaichao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/youkaichao/32/1278_2.png) [@youkaichao](https://dev-discuss.pytorch.org/u/youkaichao)\
**Post date:** [November 21, 2023, 8:19am UTC](https://dev-discuss.pytorch.org/t/how-does-torch-compile-work-with-autograd/1621/14 "2023-11-21T08:19:39Z")

</div>

@Chillee I’m trying to understand what AOT autograd does to the computation `1.0 / (exp(-x) + 5)`, and I write down the manual joint graph as follows:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pytorch1/original/2X/0/086d08dd1a9a35588a89ac57ed3cf1f07c079c03.png)

Red circles are what would be saved by the eager mode autograd engine.

I expect that AOT autograd can do some optimization, **e.g. only save x2 for backward, and recompute x4 during backward** , so that memory cost can be reduced. However, after running the AOT autograd engine, I find that both x2 and x4 are saved for backward.

Is it an expected case? How can I only save x2 if I’m striving for memory efficiency?

---

_[View the full topic](https://dev-discuss.pytorch.org/t/how-does-torch-compile-work-with-autograd/1621)._
