Torch.compile as a toolkit / manipulating dynamo outputs

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

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).

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/\_dynamo/source.py , https://github.com/pytorch/pytorch/blob/main/torch/\_C/\_dynamo/guards.pyi pytorch/torch/csrc/dynamo/guards.cpp at main · pytorch/pytorch · GitHub

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). 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).

Hope this helps,

William

1 Like

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

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?