{"id":1989,"library":"depyf","title":"depyf","description":"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.","status":"active","version":"0.20.0","language":"en","source_language":"en","source_url":"https://github.com/thuml/depyf","tags":["pytorch","debugging","decompilation","bytecode","torch.compile","performance"],"install":[{"cmd":"pip install depyf","lang":"bash","label":"Install stable release"},{"cmd":"pip install git+https://github.com/thuml/depyf.git","lang":"bash","label":"Install nightly version (recommended)"}],"dependencies":[{"reason":"depyf is designed to work with PyTorch's `torch.compile` and requires PyTorch>=2.2.0 (PyTorch nightly is often recommended for best compatibility).","package":"torch","optional":false}],"imports":[{"symbol":"decompile","correct":"from depyf import decompile"},{"symbol":"prepare_debug","correct":"import depyf\nwith depyf.prepare_debug(...)"},{"symbol":"debug","correct":"import depyf\nwith depyf.debug()"}],"quickstart":{"code":"import torch\nimport depyf\n\n@torch.compile\ndef toy_example(a, b):\n    x = a / (torch.abs(a) + 1)\n    if b.sum() < 0:\n        b = b * -1\n    return x * b\n\ndef main():\n    for _ in range(100):\n        toy_example(torch.randn(10), torch.randn(10))\n\n# Wrap the code that triggers compilation within depyf.prepare_debug\n# This will dump decompiled source code to './debug_dir'\nwith depyf.prepare_debug(\"./debug_dir\"):\n    main()\n\n# Optional: Use depyf.debug() to pause execution and set breakpoints\n# The program will pause here, allowing you to browse files in ./debug_dir\n# and set breakpoints before continuing execution.\n# with depyf.debug():\n#    output = toy_example(torch.randn(10), torch.randn(10))\n\nprint(\"Decompiled code and debug info available in ./debug_dir\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade PyTorch to version 2.2.0 or newer, preferably using a nightly build if encountering issues.","message":"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.","severity":"breaking","affected_versions":"<2.2.0 of PyTorch"},{"fix":"Understand that depyf's scope is primarily for PyTorch-generated bytecode. For general Python decompilation, other tools might be necessary, though they often struggle with PyTorch bytecode.","message":"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).","severity":"gotcha","affected_versions":"All"},{"fix":"Be aware that the decompiled output serves as a detailed representation of the bytecode's operations, not a precise reconstruction of the original Python source. Focus on the logical flow rather than exact syntax.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Prioritize using `depyf`'s context managers (`prepare_debug`, `debug`) for a clearer and more organized understanding of `torch.compile`'s internal workings.","message":"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.","severity":"deprecated","affected_versions":"PyTorch 2.x"},{"fix":"Set `DEPYF_REMOVE_TEMP=0` in your environment if you wish to retain temporary files generated by depyf for further inspection.","message":"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.","severity":"gotcha","affected_versions":">=0.20.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}