Limiting graph serialization depth for composite models.
Hi @RameezI
Apologies for the delay.
I was able to replicate the issue in Colab using Tensorflow 2.12 and tf-nightly(2.13.0.dev20230424). Please find the gists -  [TF v2.12](https://colab.sandbox.google.com/gist/synandi/3182c8600aedacc08ad4f8fa04b27b53/60386.ipynb) & [tf-nightly](https://colab.sandbox.google.com/gist/synandi/a0522fcc8f89b9a254ebc56fbb3dfc2c/60386_nightly.ipynb). It seems like we have to dig deep into the issue, we'll update here soon.  Thank you!  Hi @RameezI,

The two models that you have in your colab are structurally different. Lets say for a `_MAX_RECURSION_DEPTH = N`, 
MyModuleMono has ~N objects of type `tf.Module` (`TerminalModule` objects) while in the recursive approach you create double the amount of `tf.Module` objects i.e ~N `MyModule` and ~N `TerminalModule`. The size of the saved model is a result of the inherent difference in the two models. 
Your experiment also demonstrates that the iterative approach is better design than recursive for same usecase (which is working as intended in my opinion). 
If you intend to only work with the top level graphs and do not care for saving the full structure, then before saving you could create another module from MyModule that contains only the parts that you intend to save. However, note that it will be a different module than `MyMonoModule` and `MyModule`.
Thanks,
Hope this helps!
 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Hi @silkyarora 
Thanks a lot for looking into it. Indeed, the structure of the two models are different. The recursive model contains additional symbolic objects that do not enter into computations but makes it easier to define the model in some cases. 

I was basically interested in exporting a new model from recursively constructed model, as you suggested, that does not track/serialize its child models and that how to do it without manually writing the iterative version.  It seems like I can unset  `_setattr_tracking` attribute of  a `tf.Module`  to do this? 


<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Support

### Have you reproduced the bug with TF nightly?

No

### Source

binary

### Tensorflow Version

2.12

### Custom Code

Yes

### OS Platform and Distribution

_No response_

### Mobile device

_No response_

### Python version

3.10

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

I am building a model generator that composes models by putting together assemblies of simpler modules. When exporting such a model, I noticed that all component modules (submodules/sugraphs) are serialized and exported as well and are available at the load time, so if my model is composed of say P, Q the graph for P and Q is also serialized and same for all the children of P and Q. 
 
 1. My understanding is that the top-level graph is self-contained and does not need the graph structures of submodules, exporting them is wasteful if I only need to run the compute function at the top-level graph.
 
 2. Is there a way I could achieve resource restriction using ` tf.saved_model.save`; I only mean to save and work with top level graphs. Ideally, I think it would be great to be able to provide a maximum depth at which the graph serialization/export is done so it excludes everything below this depth.
 
 Any guidance on how to achieve this will be greatly appreciated. Please see the code snippet and the comments for more information, the code demonstrates that a composite model takes considerably more disk space than an equivalent monolithic model, did not look into memory and compute performance as yet...


### Standalone code to reproduce the issue

```shell
https://colab.research.google.com/drive/1FeP_bhVwQ223A9GGts1v--FgXDNeb6jY?usp=sharing
```


### Relevant log output

```shell
https://gist.github.com/RameezI/3d51ab59d745d08b62daec77a3a5439c
```
</details>