# Torch.compile as a toolkit / manipulating dynamo outputs

**URL:** <https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306>\
**Category:** compiler\
**Created:** [February 6, 2026, 11:20am UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306 "2026-02-06T11:20:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/tom/32/26_2.png) [@tom](https://dev-discuss.pytorch.org/u/tom)\
**Post date:** [February 6, 2026, 11:20am UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306/1 "2026-02-06T11:20:29Z")

</div>

Hi,

so with great interest, I heard @ezyang ‘s comments about torch.compile as a toolkit at PTC.

I’ve been trying to access/manipulate some of the things that dynamo produces. So there is a lot of material on what to do with the fx traces, but to my mind, there are two more important bits it produces:

- there is a cache entry of type `torch._C._dynamo.eval_frame._CacheEntry`
- the guards, which I can view from the `cache_entry.guard_manager`, but I haven’t found a way to manipulate easily,
- the code to actually collect the fx inputs and do whatever with the outputs. My understanding is that the `cache_entry.code` part is executed instead of the frame of the compiled model.

I would appreciate a hint / pointer to docs, source code etc. or even a quick hint if it’s not currently possible to manipulate those.

Thank you and best regards

Thomas

---

<div class="post-metadata">

**Author:** ![williamwen42](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/williamwen42/32/1156_2.png) [@williamwen42](https://dev-discuss.pytorch.org/u/williamwen42)\
**Post date:** [February 10, 2026, 6:47pm UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306/2 "2026-02-10T18:47:24Z")

</div>

Hi Thomas,

If you could provide more details as to exactly how you want to manipulate guards or dynamo-generated bytecode, then I could provide more concrete guidance.

We don’t have a lot of public API to manipulate guards since it’s often tricky to manipulate guards will still maintaining correctness. There are options such as `guard_filter_fn` but this is an unsafe API with no BC guarantees (see [https://docs.pytorch.org/docs/stable/generated/torch.compile.html](https://docs.pytorch.org/docs/stable/generated/torch.compile.html)).

General code pointers for guards are [https://github.com/pytorch/pytorch/blob/main/torch/\_dynamo/guards.py](https://github.com/pytorch/pytorch/blob/main/torch/%5C_dynamo/guards.py) , [https://github.com/pytorch/pytorch/blob/main/torch/\_dynamo/source.py](https://github.com/pytorch/pytorch/blob/main/torch/%5C_dynamo/source.py) , [https://github.com/pytorch/pytorch/blob/main/torch/\_C/\_dynamo/guards.pyi](https://github.com/pytorch/pytorch/blob/main/torch/%5C_C/%5C_dynamo/guards.pyi) [pytorch/torch/csrc/dynamo/guards.cpp at main · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/blob/main/torch/csrc/dynamo/guards.cpp)

For dynamo-generated bytecode, TORCH\_LOGS=”bytecode” will output the generated bytecode. If you want more interaction, there is a bytecode hook that you can use to intercept and inspect bytecode ([pytorch/torch/\_dynamo/convert\_frame.py at cb8853182c8f56f0b3ab1ddb866df5dbbf03d2cc · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/blob/cb8853182c8f56f0b3ab1ddb866df5dbbf03d2cc/torch/_dynamo/convert_frame.py#L782)). I am also currently working on a dynamo-bytecode debugger that can step through bytecode one at a time and inspect the state of the python stack at each step ([[dynamo] dynamo bytecode debugger by williamwen42 · Pull Request #173859 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/pull/173859)).

Hope this helps,

William

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/tom/32/26_2.png) [@tom](https://dev-discuss.pytorch.org/u/tom)\
**Post date:** [February 12, 2026, 10:54am UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306/3 "2026-02-12T10:54:40Z")

</div>

Thank you William!

so basically, my aim is to take a cache entry and generate a new one (copy with slight modifications), in particular for the guards and possibly also how to get the values fed into the fx graph (i.e. the .code). I would then love to manually run those two bits after modification.

I’ll take a closer look a the code, but I suspect it’s not quite there yet.

Best regards

Thomas

---

<div class="post-metadata">

**Author:** ![williamwen42](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/williamwen42/32/1156_2.png) [@williamwen42](https://dev-discuss.pytorch.org/u/williamwen42)\
**Post date:** [February 17, 2026, 8:59pm UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306/4 "2026-02-17T20:59:52Z")

</div>

So you want to generate a new cache entry with custom guards and custom optimized bytecode, and you would like to register that back to the Dynamo cache? Could you provide more details as to what custom guard behavior you would like and what custom bytecode you want to run?

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/tom/32/26_2.png) [@tom](https://dev-discuss.pytorch.org/u/tom)\
**Post date:** [April 5, 2026, 12:49pm UTC](https://dev-discuss.pytorch.org/t/torch-compile-as-a-toolkit-manipulating-dynamo-outputs/3306/5 "2026-04-05T12:49:32Z")

</div>

Hi @williamwen42 ,

it took a while, but I finally have a thorough answer (for the PyTorch Conference EU talk). The gist is that I want to change/swap the module/weights along with changing the code operates on after compiling. (I also have the wrapping-bytecode-modifying things, but it didn’t fit into the talk.)

It seems that hooking into `torch._dynamo.convert_frame.compile_frame` works nicely enough if we’re willing to have it all ready when that runs.

These below are the draft slides for the talk.

Best regards

Thomas

> **[Model Changing Transforms PyTorch Conference 2026](https://docs.google.com/presentation/d/1v4zth6ICNRyh2oVevMrBJnEKEvELPaExj6z6OvQgJ9s/edit?usp=sharing)**
>
> Model-changing transforms with torch.compile Separating the what and the how Thomas Viehmann | Lightning AI
