• Stars
    star
    407
  • Rank 105,535 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 1 year ago
  • Updated 22 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

depyf is a tool to help you understand and adapt to PyTorch compiler torch.compile.

Logo

Documentation Status Supported Python Versions Python Decompilation Tests PyTorch Integration Tests MIT License

Have you ever felt overwhelmed by the complexities of torch.compile? Diving into its workings can feel like black magic, with bytecode and Python internal details that many users fail to understand, hindering them from understanding and adapting to torch.compile.

If you also face the problem, then you might be interested in depyf. As the logo suggests, depyf is a software tool to leverage advanced Python features (the Python snake symbol) to open up internal details (the internal gears symbol) of PyTorch's compiler torch.compile (the PyTorch logo), so that users can understand it, adapt to it, and tune their code (the debugger symbol) to get maximum performance benefit out of it.

โš ๏ธ This project is developed under close collaborations with the PyTorch team. We frequently require new features from PyTorch to support better understanding of torch.compile. Therefore, please use this project along with PyTorch nightly. Visit the PyTorch website for how to install nightly version of PyTorch. We recommend updating your PyTorch nightly installation every week or so.

โš ๏ธ During development, we seek suggestions from the community quite a lot. You may find some early usage examples from some discussion forums or social media platforms. Please follow the latest documentation for how to use this tool.

Why depyf?

If you want to understand bytecode generated by torch.compile, then depyf might be the only choice for you. Below we tested several existing decompilers, they struggle to decompile simple Python bytecode across versions, and have poor support for PyTorch.

Decompiler Python 3.8 Python 3.9 Python 3.10 Python 3.11 PyTorch
decompyle3 90.6% (77/85) ร— ร— ร— ร—
uncompyle6 91.8% (78/85) ร— ร— ร— ร—
pycdc 74.1% (63/85) 74.1% (63/85) 74.1% (63/85) 67.1% (57/85) 19.3% (27/140)
depyf 100% (85/85) 100% (85/85) 100% (85/85) 100% (85/85) 100% (140/140)

Installation

Stable release: pip install depyf

Nightly version (recommended): pip install git+https://github.com/thuml/depyf.git

Usage

The main usage is quite simple: just wrap your code within a context manager:

import torch
from torch import _dynamo as torchdynamo
from typing import List

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

if __name__ == "__main__":
-     main()
+     import depyf
+     with depyf.prepare_debug("./dump_src_dir"):
+         main()

Then you can see all the details of torch.compile inside the directory ./dump_src_dir. The details are organized into the following:

  • full_code_for_xxx.py for each function using torch.compile
  • __transformed_code_for_xxx.py for Python code associated with each graph.
  • __transformed_code_for_xxx.py.xxx_bytecode for Python bytecode, dumped code object, can be loaded via dill.load(open("/path/to/file", "wb")). Note that the load function might import some modules like transformers. Make sure you have these modules installed.
  • __compiled_fn_xxx.py for each computation graph and its optimization:
    • Captured Graph: a plain forward computation graph
    • Joint Graph: joint forward-backward graph from AOTAutograd
    • Forward Graph: forward graph from AOTAutograd
    • Backward Graph: backward graph from AOTAutograd
    • kernel xxx: compiled CPU/GPU kernel wrapper from Inductor.

We collect all the compilation artifacts when testing over 100 deep learning models. You can take a look to learn how the PyTorch compiler works.

If you want to use debugger to step through the above code, just add another context manager (and launch the script through debuggers):

import torch
from torch import _dynamo as torchdynamo
from typing import List

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

if __name__ == "__main__":
    import depyf
    with depyf.prepare_debug("./dump_src_dir"):
        main()
+     with depyf.debug():
+         main()

Calling depyf.debug() will pause the program for you to set breakpoints, and then you can use debuggers to hit breakpoints in these files under the ./dump_src_dir directory you specified above.

Contact

If you have any question about depyf, feel free to open issues to reach out! Any discussion/issue report/PR is welcome. Or contact [email protected] if you have any other questions.

More Repositories

1

Time-Series-Library

A Library for Advanced Deep Time Series Models.
Python
6,099
star
2

Transfer-Learning-Library

Transfer Learning Library for Domain Adaptation, Task Adaptation, and Domain Generalization
Python
3,318
star
3

Autoformer

About Code release for "Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting" (NeurIPS 2021), https://arxiv.org/abs/2106.13008
Jupyter Notebook
1,882
star
4

iTransformer

Official implementation for "iTransformer: Inverted Transformers Are Effective for Time Series Forecasting" (ICLR 2024 Spotlight), https://openreview.net/forum?id=JePfAI8fah
Python
1,097
star
5

Anomaly-Transformer

About Code release for "Anomaly Transformer: Time Series Anomaly Detection with Association Discrepancy" (ICLR 2022 Spotlight), https://openreview.net/forum?id=LzQQ89U1qm_
Python
708
star
6

TimesNet

About Code release for "TimesNet: Temporal 2D-Variation Modeling for General Time Series Analysis" (ICLR 2023), https://openreview.net/pdf?id=ju_Uqw384Oq
673
star
7

awesome-multi-task-learning

2024 up-to-date list of DATASETS, CODEBASES and PAPERS on Multi-Task Learning (MTL), from Machine Learning perspective.
625
star
8

Xlearn

Transfer Learning Library
Jupyter Notebook
459
star
9

Nonstationary_Transformers

Code release for "Non-stationary Transformers: Exploring the Stationarity in Time Series Forecasting" (NeurIPS 2022), https://arxiv.org/abs/2205.14415
Python
455
star
10

predrnn-pytorch

Official implementation for NIPS'17 paper: PredRNN: Recurrent Neural Networks for Predictive Learning Using Spatiotemporal LSTMs.
Python
438
star
11

CDAN

Code release for "Conditional Adversarial Domain Adaptation" (NIPS 2018)
Jupyter Notebook
392
star
12

Flowformer

About Code release for "Flowformer: Linearizing Transformers with Conservation Flows" (ICML 2022), https://arxiv.org/pdf/2202.06258.pdf
Python
300
star
13

Universal-Domain-Adaptation

Code release for Universal Domain Adaptation(CVPR 2019)
Python
272
star
14

HashNet

Code release for "HashNet: Deep Learning to Hash by Continuation" (ICCV 2017)
Jupyter Notebook
240
star
15

Large-Time-Series-Model

Official code, datasets and checkpoints for "Timer: Generative Pre-trained Transformers Are Large Time Series Models" (ICML 2024)
Python
214
star
16

LogME

Code release for "LogME: Practical Assessment of Pre-trained Models for Transfer Learning" (ICML 2021) and Ranking and Tuning Pre-trained Models: A New Paradigm for Exploiting Model Hubs (JMLR 2022)
Python
200
star
17

Koopa

Code release for "Koopa: Learning Non-stationary Time Series Dynamics with Koopman Predictors" (NeurIPS 2023), https://arxiv.org/abs/2305.18803
Python
173
star
18

Corrformer

About code release of "Interpretable Weather Forecasting for Worldwide Stations with a Unified Deep Model", Nature Machine Intelligence, 2023. https://www.nature.com/articles/s42256-023-00667-9
Python
155
star
19

A-Roadmap-for-Transfer-Learning

151
star
20

MDD

Code released for ICML 2019 paper "Bridging Theory and Algorithm for Domain Adaptation".
Python
129
star
21

Self-Tuning

Code release for "Self-Tuning for Data-Efficient Deep Learning" (ICML 2021)
Python
109
star
22

SimMTM

About Code release for "SimMTM: A Simple Pre-Training Framework for Masked Time-Series Modeling" (NeurIPS 2023 Spotlight), https://arxiv.org/abs/2302.00861
Python
103
star
23

PADA

Code release for "Partial Adversarial Domain Adaptation" (ECCV 2018)
Python
100
star
24

Batch-Spectral-Penalization

Code release for Transferability vs. Discriminability: Batch Spectral Penalization for Adversarial Domain Adaptation (ICML 2019)
Python
91
star
25

Transferable-Adversarial-Training

Code release for Transferable Adversarial Training: A General Approach to Adapting Deep Classi๏ฌers (ICML2019)
Python
80
star
26

TransNorm

Code release for "Transferable Normalization: Towards Improving Transferability of Deep Neural Networks" (NeurIPS 2019)
Python
78
star
27

MTlearn

Code release for "Learning Multiple Tasks with Multilinear Relationship Networks" (NIPS 2017)
Python
70
star
28

SAN

Code release for "Partial Transfer Learning with Selective Adversarial Networks" (CVPR 2018)
Jupyter Notebook
69
star
29

Domain-Adaptation-Regression

Code release for Representation Subspace Distance for Domain Adaptation Regression (ICML 2021)
Python
69
star
30

HashGAN

HashGAN: Deep Learning to Hash with Pair Conditional Wasserstein GAN
Python
68
star
31

Deep-Embedded-Validation

Code release for Towards Accurate Model Selection in Deep Unsupervised Domain Adaptation (ICML 2019)
Python
61
star
32

Latent-Spectral-Models

About Code Release for "Solving High-Dimensional PDEs with Latent Spectral Models" (ICML 2023), https://arxiv.org/abs/2301.12664
Python
59
star
33

CLIPood

About Code Release for "CLIPood: Generalizing CLIP to Out-of-Distributions" (ICML 2023), https://arxiv.org/abs/2302.00864
Python
58
star
34

iVideoGPT

Official repo for "iVideoGPT: Interactive VideoGPTs are Scalable World Models", https://arxiv.org/abs/2405.15223
Python
58
star
35

Transolver

About code release of "Transolver: A Fast Transformer Solver for PDEs on General Geometries", ICML 2024 Spotlight. https://arxiv.org/abs/2402.02366
Python
57
star
36

MADA

Code release for "Multi-Adversarial Domain Adaptation" (AAAI 2018)
C++
56
star
37

MotionRNN

About Code release for "MotionRNN: A Flexible Model for Video Prediction with Spacetime-Varying Motions" (CVPR 2021) https://arxiv.org/abs/2103.02243
Python
50
star
38

ETN

Code released for CVPR 2019 paper "Learning to Transfer Examples for Partial Domain Adaptation"
Python
50
star
39

Debiased-Self-Training

Code release of paper Debiased Self-Training for Semi-Supervised Learning (NeurIPS 2022 Oral)
50
star
40

Versatile-Domain-Adaptation

Code Release for "Minimum Class Confusion for Versatile Domain Adaptation"(ECCV2020)
Python
50
star
41

ContextWM

Code release for "Pre-training Contextualized World Models with In-the-wild Videos for Reinforcement Learning" (NeurIPS 2023), https://arxiv.org/abs/2305.18499
Python
50
star
42

Separate_to_Adapt

Code release for Separate to Adapt: Open Set Domain Adaptation via Progressive Separation (CVPR 2019)
Python
49
star
43

AutoTimes

Official implementation for "AutoTimes: Autoregressive Time Series Forecasters via Large Language Models"
Python
45
star
44

CoTuning

Code release for NeurIPS 2020 paper "Co-Tuning for Transfer Learning"
Python
39
star
45

OpenDG-DAML

Code release for Open Domain Generalization with Domain-Augmented Meta-Learning (CVPR2021)
Python
32
star
46

Calibrated-Multiple-Uncertainties

Code Release for "Learning to Detect Open Classes for Universal Domain Adaptation"(ECCV2020)
Python
30
star
47

TimeSiam

Python
25
star
48

Batch-Spectral-Shrinkage

Code release for Catastrophic Forgetting Meets Negative Transfer: Batch Spectral Shrinkage for Safe Transfer Learning (NeurIPS 2019)
Python
24
star
49

StochNorm

Code release for NeurIPS 2020 paper "Stochastic Normalization"
Python
23
star
50

Transferable-Query-Selection

Code Release for "Transferable Query Selection for Active Domain Adaptation"(CVPR2021)
Python
23
star
51

Decoupled-Adaptation-for-Cross-Domain-Object-Detection

Code for ICLR2022 Decoupled Adaptation for Cross-Domain Object Detection (D-adapt) https://arxiv.org/abs/2110.02578
22
star
52

few-shot

A lightweight library that implements state-of-the-art few-shot learning algorithms.
Python
21
star
53

HarmonyDream

Code release for "HarmonyDream: Task Harmonization Inside World Models" (ICML 2024), https://arxiv.org/abs/2310.00344
Python
21
star
54

transferable-memory

Python
20
star
55

VideoDG

Python
20
star
56

TCL

Code release for Transferable Curriculum for Weakly-Supervised Domain Adaptation (AAAI2019)
Python
18
star
57

SPOT

Code release for "Supported Policy Optimization for Offline Reinforcement Learning" (NeurIPS 2022), https://arxiv.org/abs/2202.06239
Python
18
star
58

DPH

Code release for "Deep Priority Hashing" (ACMMM 2018)
C++
18
star
59

MMHH

Python
15
star
60

Metasets

Python
15
star
61

PAN

Python
15
star
62

DCN

Deep Calibration Network
Python
15
star
63

ModeRNN

Python
14
star
64

ForkMerge

Code release of paper "ForkMerge: Mitigating Negative Transfer in Auxiliary-Task Learning" (NeurIPS 2023)
14
star
65

TAH

Code release for "Transfer Adversarial Hashing for Hamming Space Retrieval" (AAAI 2018)
C++
13
star
66

TransCal

Python
12
star
67

learn_torch.compile

torch.compile artifacts for common deep learning models, can be used as a learning resource for torch.compile
Python
12
star
68

HelmFluid

About code release of "HelmFluid: Learning Helmholtz Dynamics for Interpretable Fluid Prediction", ICML 2024. https://arxiv.org/pdf/2310.10565
Python
11
star
69

Multi-Embedding

About Code Release for "On the Embedding Collapse When Scaling Up Recommendation Models" (ICML 2024)
Python
11
star
70

Zoo-Tuning

Code release for Zoo-Tuning: Adaptive Transfer from A Zoo of Models (ICML2021)
Python
7
star
71

timer

See the official code and checkpoints for "Timer: Generative Pre-trained Transformers Are Large Time Series Models"
HTML
5
star
72

Regressive-Domain-Adaptation-for-Unsupervised-Keypoint-Detection

Code for CVPR 2021 Regressive Domain Adaptation for Unsupervised Keypoint Detection (RegDA) https://arxiv.org/abs/2103.06175
5
star
73

MitNet

About Code Release for "Estimating Heterogeneous Treatment Effects: Mutual Information Bounds and Learning Algorithms" (ICML 2023)
Python
4
star
74

MobileAttention

Official implementation of "Mobile Attention: Mobile-Friendly Linear-Attention for Vision Transformers in PyTorch". To run the code, you can refer to https://github.com/thuml/Flowformer.
Python
1
star