# Graph traversal of torch.nn.Module

**URL:** <https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821>\
**Category:** Uncategorized\
**Created:** [February 3, 2021, 4:13pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821 "2021-02-03T16:13:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nscotto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nscotto/32/29758_2.png) [@nscotto](https://discuss.pytorch.org/u/nscotto)\
**Post date:** [February 3, 2021, 4:13pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/1 "2021-02-03T16:13:02Z")

</div>

Hi, I already asked a question about it but after coming back to it I think the answer is missing some points.  
So I would like to traverse the graph of modules, starting from the input in a depth/breadth first search manner.  
My goal is to automatically search & replace blocks (subgraphs) in a model (graph).

Here is my [previous answered question](https://discuss.pytorch.org/t/graph-exploration-of-unknown-models/103404/4)

So after looking at the doc it seems that `torch.nn.Module.named_modules()` iterates over all `torch.nn.Module` found in the constructor, I imagine respecting the order in which they are declared. The problem is that it does not necessarily follow a depth first/breadth first, even more if the network doesn’t follow a sequential structure.  
The computation graph is defined by the order of computation in the forward method, not the order of declaration in the constructor…  
Another (“more minor”) problem is that when using functional api for some computation (like using `torch.nn.functional.relu()` in the `forward` method), `named_modules()` can’t see it.

So I am still looking for a way to do a depth/breadth first graph traversal in order to operate search and replace of blocks.  
Is there a way I can achieve this?

---

<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:** [February 3, 2021, 4:22pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/2 "2021-02-03T16:22:14Z")

</div>

Hi,

One thing you most likely want to checkout is [torch.fx — PyTorch master documentation](https://pytorch.org/docs/master/fx.html?highlight=fx#module-torch.fx)

---

<div class="post-metadata">

**Author:** ![nscotto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nscotto/32/29758_2.png) [@nscotto](https://discuss.pytorch.org/u/nscotto)\
**Post date:** [February 4, 2021, 2:27pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/3 "2021-02-04T14:27:20Z")

</div>

It looks promising, and will probably help me achieving what I want to do right now.  
Any idea on when it could be released?

---

<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:** [February 4, 2021, 5:31pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/4 "2021-02-04T17:31:31Z")

</div>

It will be a beta feature in the upcoming 1.8 release.

---

<div class="post-metadata">

**Author:** ![nscotto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nscotto/32/29758_2.png) [@nscotto](https://discuss.pytorch.org/u/nscotto)\
**Post date:** [February 5, 2021, 3:03pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/5 "2021-02-05T15:03:41Z")

</div>

Right, I imagine you don’t want to adventure into release date forecast 😅  
Can’t wait for it! 😀

Cheers

---

<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:** [February 5, 2021, 3:26pm UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/6 "2021-02-05T15:26:49Z")

</div>

> [@nscotto](#):
>
> Right, I imagine you don’t want to adventure into release date forecast 😅

The release branch is planned to be cut later today.  
So between testing, release notes and all, you should have it at the end of the month 🙂

---

<div class="post-metadata">

**Author:** ![nscotto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nscotto/32/29758_2.png) [@nscotto](https://discuss.pytorch.org/u/nscotto)\
**Post date:** [February 8, 2021, 9:24am UTC](https://discuss.pytorch.org/t/graph-traversal-of-torch-nn-module/110821/7 "2021-02-08T09:24:10Z")

</div>

Wow, good work guys that’s awesome!
