depyf: Open the Opaque Box of PyTorch Compiler for Machine Learning Researchers

Kaichao You, Runsheng Bai, Meng Cao, Jianmin Wang, Ion Stoica, Mingsheng LongarXiv 2024 · DOI

Abstract

The PyTorch 2.x compiler offers substantial performance gains through torch.compile, but it operates as an "opaque box" by transforming user code into cryptic Python bytecode. This transformation hinders transparency and makes debugging optimized models nearly impossible with standard tools. We introduce depyf, a tool designed to decompile this bytecode back into human-readable source code. By utilizing symbolic execution and function execution hijacking, depyf allows researchers to use standard debuggers like pdb or VS Code on compiled models. Our evaluation shows that depyf achieves a 100% success rate across multiple Python versions and complex model suites, significantly outperforming existing decompilers.

Introduction

The introduction of PyTorch 2.0 brought torch.compile, a powerful feature for optimizing deep learning models. However, its frontend, Dynamo, intercepts Python bytecode and decomposes user code into optimized "subgraphs" and "resume functions" for non-optimizable parts. This process creates a "black box" effect: if a compiled model produces errors like NaNs, researchers cannot easily inspect the internal state using standard Python debugging workflows. Reading raw bytecode instructions (e.g., LOAD_FAST, BINARY_SUBSCR) is impractical for most algorithm developers, creating a significant barrier to troubleshooting and adoption.

Methodology

depyf (decompile Python functions) addresses these challenges through two core techniques:

  1. Bytecode Decompilation via Symbolic Execution: Unlike traditional decompilers that rely on rigid pattern matching, depyf uses symbolic execution to traverse bytecode. This approach allows it to handle approximately 200 types of Python instructions and remain compatible with newer Python versions (3.9+), reconstructing equivalent, readable source code.
  2. Function Execution Hijacking: To enable line-by-line debugging, depyf intercepts PyTorch’s internal function generation. It replaces in-memory objects with versions linked to physical source files on disk, allowing standard debuggers to "see" and step through the compiled code.
The PyTorch Compiler Workflow and how depyf provides human-readable decompiled source code for debugging.

The tool is non-intrusive, utilizing context managers like with depyf.prepare_debug() to capture state and with depyf.debug() to facilitate breakpoint setting.

Evaluation and Results

depyf was validated across a diverse set of models and environments:

  • Model Suites: Tested on TorchBench (e.g., Segment Anything), Huggingface Transformers (LLaMA, BERT), and TIMM (ResNet, ViT).
  • Compatibility: Verified across Python 3.8 through 3.11.
  • Syntax Robustness: Evaluated against over 80 complex Python syntax test cases.
Comparison of depyf with other decompilers across Python versions and PyTorch-specific tests, showing depyf's 100% success rate.

As shown in the results, depyf achieved a 100% success rate, whereas existing tools like pycdc failed on over 80% of PyTorch-specific tests. This robustness ensures that researchers can reliably inspect their models regardless of complexity or Python version.

Conclusion

depyf democratizes the use of the PyTorch compiler by making its internal operations transparent to researchers. By converting bytecode back into Python, it allows developers to stay within their familiar debugging ecosystems. While it currently focuses on the Python-level transformations and does not decompile low-level C++ or Triton kernels, it provides the necessary visibility to understand "resume functions" and optimization boundaries. Ongoing development includes a daily CI system to ensure compatibility with the rapidly evolving PyTorch codebase.

Summary updated

BibTeX
@misc{you2024depyf,
  author = {Kaichao You and Runsheng Bai and Meng Cao and Jianmin Wang and Ion Stoica and Mingsheng Long},
  title = {{depyf: Open the Opaque Box of PyTorch Compiler for Machine Learning Researchers}},
  year = {2024},
  eprint = {2403.13839},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.13839},
}
Figure