depyf

0.20.0 · active · verified Thu Apr 09

depyf is a Python library designed to decompile Python functions from bytecode to source code, primarily to demystify the internal workings of PyTorch's `torch.compile`. It helps users understand, adapt to, and tune their PyTorch code for maximum performance. The library is currently at version 0.20.0 and maintains a frequent release cadence, often synchronizing with PyTorch updates to ensure compatibility.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `depyf` to inspect the code generated by `torch.compile`. Wrapping the `main` function (which calls the `torch.compile`d `toy_example`) with `depyf.prepare_debug` will decompile and dump the generated source code into the specified directory (`./debug_dir`). You can then examine these files to understand PyTorch's compiler optimizations. The `depyf.debug()` context manager can be used to pause execution for interactive debugging with breakpoints.

import torch
import depyf

@torch.compile
def toy_example(a, b):
    x = a / (torch.abs(a) + 1)
    if b.sum() < 0:
        b = b * -1
    return x * b

def main():
    for _ in range(100):
        toy_example(torch.randn(10), torch.randn(10))

# Wrap the code that triggers compilation within depyf.prepare_debug
# This will dump decompiled source code to './debug_dir'
with depyf.prepare_debug("./debug_dir"):
    main()

# Optional: Use depyf.debug() to pause execution and set breakpoints
# The program will pause here, allowing you to browse files in ./debug_dir
# and set breakpoints before continuing execution.
# with depyf.debug():
#    output = toy_example(torch.randn(10), torch.randn(10))

print("Decompiled code and debug info available in ./debug_dir")

view raw JSON →