depyf
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
- breaking depyf requires a recent version of PyTorch, specifically `>=2.2.0`, and often recommends using PyTorch nightly builds due to its close integration with the rapidly evolving `torch.compile` stack. Older PyTorch versions may lead to compatibility issues or incorrect decompilation.
- gotcha depyf is a specialized decompiler focused on bytecode generated by `torch.compile`. It is not a general-purpose Python bytecode decompiler and may not fully support all Python syntax (e.g., `async/await`, complex `while` loops, or intricate `if/else` patterns outside of PyTorch's optimized graphs).
- gotcha The output source code generated by depyf is semantically equivalent to the original bytecode but may not be syntactically identical. It often includes verbose details (e.g., explicit `return None`) that are typically implicit in human-written Python code.
- deprecated The `TORCH_COMPILE_DEBUG` environment variable, while providing debug information for `torch.compile`, produces logs that are generally much harder to parse and understand compared to `depyf`'s human-readable decompiled source. Relying solely on `TORCH_COMPILE_DEBUG` is inefficient for deep analysis.
- gotcha The `DEPYF_REMOVE_TEMP` environment variable, introduced in v0.20.0, can affect whether temporary files generated during decompilation are removed. If you need to inspect intermediate artifacts, ensure this is not set or set appropriately.
Install
-
pip install depyf -
pip install git+https://github.com/thuml/depyf.git
Imports
- decompile
from depyf import decompile
- prepare_debug
import depyf with depyf.prepare_debug(...)
- debug
import depyf with depyf.debug()
Quickstart
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")