Accelerate PyTorch transformer model training with ONNX Runtime – a deep dive

Published Jul 13 2021 09:00 AM 3,979 Views
Microsoft

Authors: Ravi shankar Kolli (@Ravi_Kolli) , Aishwarya Bhandare (@ashbhandare), M. Zeeshan Siddiqui (@mzs-msft) , Kshama Pawar (@kshama-msft) , Sherlock Huang (@SherlockNoMad) and the ONNX Runtime Training Team

 

Why ONNX Runtime for PyTorch?

ONNX Runtime (ORT) for PyTorch accelerates training large scale models across multiple GPUs with up to 37% increase in training throughput over PyTorch and up to 86% speed up when combined with DeepSpeed. Today, transformer models are fundamental to Natural Language Processing (NLP) applications. These models with billions of parameters utilize multiple GPUs for distributed training. This large scale is costly and time consuming for pre-training and fine-tuning such complex models. Training with ONNX Runtime for PyTorch, through its torch_ort.ORTModule API, speeds up training through efficient memory utilization, highly-optimized computational graph, mixed precision execution, all through a quick and easy, couple-line change to existing PyTorch training scripts. It also provides hardware support for both Nvidia and AMD GPUs and extensibility with custom operators, optimizers and hardware accelerator support. ONNX Runtime for PyTorch empowers AI developers to take full advantage of the PyTorch ecosystem – with the flexibility of PyTorch and the performance using ONNX Runtime.

 

Flexibility in Integration

To use ONNX Runtime as the backend for training your PyTorch model, you begin by installing the torch-ort package and making the following 2-line change to your training script. ORTModule class is a simple wrapper for torch.nn.Module that optimizes the memory and computations required for training.

 

from torch_ort import ORTModule
model = ORTModule(model)
  1. import torch_ort – allows you to access all the APIs and features of ONNX Runtime
  2. model = torch_ort.ORTModule(model) – wraps the torch.nn.Module in the PyTorch training script with ORTModule to allow acceleration using ONNX Runtime

The rest of the training loop is unmodified. ORTModule can be flexibly composed with torch.nn.Module, allowing the user to wrap part or whole of the model to run with ORT. For instance, users can choose to wrap the encoder-decoder portion of the model while leaving the loss function in PyTorch. ORT will speed up the wrapped portion of the model.

 

ONNX Runtime for PyTorch has been integrated to run popular Hugging Face models with a centralized code change. Additional installation instructions can be found at pytorch/ort and samples can be found at ONNX Runtime Training Examples.

 

Performance Results

We have validated Hugging Face Transformer models with performance gains in samples/second ranging from 37% (baseline PyTorch) to 86% (combined with DeepSpeed) for different models for pre-training and fine-tuning scenarios. The performance for all runs was measured with models running on the Azure NDv2 SKU on a single node (except for the A100 results), with torch autocast as the mixed precision solution. Please refer to the configuration mentioned in the onnx-runtime-training-examples repo to reproduce the results. 

 

kshamamsft_0-1626160648567.png

 

Figure 1: ORT throughput for PyTorch Hugging Face models

 

ONNX Runtime for PyTorch also supports A100 Tensor Core GPU showing upto 1.31x throughput improvements on some Hugging Face models as seen in Figure 2.

 

kshamamsft_1-1626135034004.png

 

Figure 2: ORT throughput on A100 Tensor Core GPU

 

The speedup is a result of various graph optimizations, use of fused and optimized GPU kernels, and efficient memory handling that ORT performs in the backend. We will discuss more details in the following sections.

 

10,000 foot view of ORTModule internals

 

Overview

ORTModule is a python wrapper around torch.nn.Module (Figure 3, ORTModule wraps around torch.nn.Module) that intercepts forward and backward calls and delegates them to ONNX Runtime backend to achieve better training performance. ORTModule serves as a drop-in replacement for any torch.nn.Module for ease of use. Upon the initial forward call, the PyTorch module is exported to ONNX graph using torch-onnx exporter, which is then used to create a session. ORT’s native auto-differentiation is invoked during session creation by augmenting the forward graph to insert gradient nodes (backward graph). Static graph transformations such as constant folding, redundant operation elimination, and operator fusions are further applied to optimize the computation graph. ORTModule backend uses highly optimized kernels to execute the graph with optimal use of the GPU resources.

 

kshamamsft_2-1626127015497.png

Figure 3: ORTModule execution flow

 

Partial Graph Execution

PyTorch executes the model with separate forward and backward calls, whereas ORT represents the model as a single static computation graph. ORTModule implements a partial graph executor to mimic PyTorch’s forward and backward calls. Graph state, such as the stashed intermediate tensor, between a pair of forward and backward calls is captured and shared through RunContext (Figure 3).

Tensor Exchange

The tensors such as module input, outputs, gradients, etc. are exchanged between PyTorch and ORT using DLPack to avoid any memory copy.

 

Unified Memory Allocator

ORTModule uses PyTorch’s allocator for GPU tensor memory management. This is done to avoid having two allocators that can hide free memory from each other leading to inefficient memory utilization and reducing the maximum batch size that can be reached.

 

kshamamsft_3-1626127015501.png

 

Figure 4: Unified memory allocator

 

Composability with Acceleration Libraries

 

Integration with DeepSpeed

ONNX Runtime for PyTorch supports a seamless integration with DeepSpeed to further accelerate distributed training for increasingly large models. The gains compose well as we see significant gains over a variety of Hugging Face models with DeepSpeed and ORT. We see gains ranging from 58% to 86% for Hugging Face models over PyTorch by using DeepSpeed ZeRO Stage 1 with ORT (Figure 5). Currently, ORTModule supports composing with DeepSpeed FP16, ZeRO Stage 1 and 2. Further improvements for ZeRO Stage 2 are in progress.

 

kshamamsft_2-1626135650107.png

 

Figure 5: ORT throughput improvements with DeepSpeed ZeRO Stage 1

 

Mixed precision support

ONNX Runtime supports mixed precision training with a variety of solutions like PyTorch’s native AMP, Nvidia’s Apex O1, as well as with DeepSpeed FP16. This allows the user with flexibility to avoid changing their current set up to bring ORT’s acceleration capabilities to their training workloads.

 

Figure 1 in the "Performance results" section above shows speedup with ORT for Pytorch autocast. We see gains ranging from 11% to 37% by using ONNX Runtime for Pytorch.

 

Figure 6 shows speedup with ORT combined with DeepSpeed FP16 against baseline PyTorch. We see gains ranging from 22% to 86% on popular Hugging Face models.

 

kshamamsft_3-1626135944666.png

 

Figure 6: ORT throughput improvements with DeepSpeed FP16

 

Figure 7 shows speedup for using ORT with NVIDIA’s Apex O1, giving 8% to 23% gains over PyTorch.

.

kshamamsft_4-1626136002444.png

 

Figure 7: ORT throughput improvements with Apex O1 mixed precision

 

Looking Forward

The ONNX Runtime team is working on more exciting optimizations to make training large workloads even faster. ONNX Runtime for PyTorch plans to add support for custom torch.autograd functions which would allow the graph execution to switch back to PyTorch for user-defined autograd functions. This would allow us to support advanced scenarios like horizontal parallelism and Mixture of Expert models. Further improvements and support for DeepSpeed ZeRO Stage 2 and 3 have also been planned for future releases. The team will also be adding samples to the ONNX Runtime Training Examples in the future as we support more types of models and scenarios. Be sure to check out the links below to learn more and get started with ONNX Runtime for PyTorch!

 

Getting Started

%3CLINGO-SUB%20id%3D%22lingo-sub-2540471%22%20slang%3D%22en-US%22%3EAccelerate%20PyTorch%20transformer%20model%20training%20with%20ONNX%20Runtime%20%E2%80%93%20a%20deep%20dive%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2540471%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3EAuthors%3A%20Ravi%20shankar%20Kolli%20(%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F484531%22%20target%3D%22_blank%22%3E%40Ravi_Kolli%3C%2FA%3E)%20%2C%20Aishwarya%20Bhandare%20(%40ashbhandare)%2C%20M.%20Zeeshan%20Siddiqui%20(%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1098974%22%20target%3D%22_blank%22%3E%40mzs-msft%3C%2FA%3E)%20%2C%20Kshama%20Pawar%20(%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1099824%22%20target%3D%22_blank%22%3E%40kshama-msft%3C%2FA%3E)%26nbsp%3B%2C%20Sherlock%20Huang%20(%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F670116%22%20target%3D%22_blank%22%3E%40SherlockNoMad%3C%2FA%3E)%20and%20the%20ONNX%20Runtime%20Training%20Team%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--410658604%22%20id%3D%22toc-hId--410658604%22%3EWhy%20ONNX%20Runtime%20for%20PyTorch%3F%3C%2FH2%3E%0A%3CP%3EONNX%20Runtime%20(ORT)%20for%20PyTorch%20accelerates%20training%20large%20scale%20models%20across%20multiple%20GPUs%20with%20up%20to%2037%25%20increase%20in%20training%20throughput%20over%20PyTorch%20and%20up%20to%2086%25%20speed%20up%20when%20combined%20with%20%3CA%20href%3D%22https%3A%2F%2Fwww.deepspeed.ai%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EDeepSpeed%3C%2FA%3E.%20Today%2C%20transformer%20models%20are%20fundamental%20to%20Natural%20Language%20Processing%20(NLP)%20applications.%20These%20models%20with%20billions%20of%20parameters%20utilize%20multiple%20GPUs%20for%20distributed%20training.%20This%20large%20scale%20is%20costly%20and%20time%20consuming%20for%20pre-training%20and%20fine-tuning%20such%20complex%20models.%20Training%20with%20ONNX%20Runtime%20for%20PyTorch%2C%20through%20its%26nbsp%3B%3CSPAN%3Etorch_ort.ORTModule%20API%2C%3C%2FSPAN%3E%20speeds%20up%20training%20through%20efficient%20memory%20utilization%2C%20highly-optimized%20computational%20graph%2C%20mixed%20precision%20execution%2C%20all%20through%20a%20quick%20and%20easy%2C%20couple-line%20change%20to%20existing%20PyTorch%20training%20scripts.%20It%20also%20provides%20hardware%20support%20for%20both%20Nvidia%20and%20%3CA%20href%3D%22https%3A%2F%2Fcloudblogs.microsoft.com%2Fopensource%2F%3Fp%3D86937%26amp%3Bpreview%3D1%26amp%3B_ppp%3D0afa9b5a7d%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAMD%20GPUs%3C%2FA%3E%20and%20extensibility%20with%20custom%20operators%2C%20optimizers%20and%20hardware%20accelerator%20support.%20ONNX%20Runtime%20for%20PyTorch%20empowers%20AI%20developers%20to%20take%20full%20advantage%20of%20the%20PyTorch%20ecosystem%20%E2%80%93%20with%20the%20flexibility%20of%20PyTorch%20and%20the%20performance%20using%20ONNX%20Runtime.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2076854229%22%20id%3D%22toc-hId-2076854229%22%3EFlexibility%20in%20Integration%3C%2FH2%3E%0A%3CP%3ETo%20use%20ONNX%20Runtime%20as%20the%20backend%20for%20training%20your%20PyTorch%20model%2C%20you%20begin%20by%20installing%20the%20torch-ort%20package%20and%20making%20the%20following%202-line%20change%20to%20your%20training%20script.%26nbsp%3B%3CSPAN%3EORTModule%20class%20is%20a%20simple%20wrapper%20for%20torch.nn.Module%20that%20optimizes%20the%20memory%20and%20computations%20required%20for%20training.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Efrom%20torch_ort%20import%20ORTModule%3CBR%20%2F%3Emodel%20%3D%20ORTModule(model)%3C%2FPRE%3E%0A%3COL%3E%0A%3CLI%3E%3CEM%3Eimport%20torch_ort%3C%2FEM%3E%20%E2%80%93%20allows%20you%20to%20access%20all%20the%20APIs%20and%20features%20of%20ONNX%20Runtime%3C%2FLI%3E%0A%3CLI%3E%3CEM%3Emodel%20%3D%20torch_ort.ORTModule(model)%3C%2FEM%3E%20%E2%80%93%20wraps%20the%20torch.nn.Module%20in%20the%20PyTorch%20training%20script%20with%20ORTModule%20to%20allow%20acceleration%20using%20ONNX%20Runtime%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EThe%20rest%20of%20the%20training%20loop%20is%20unmodified.%20ORTModule%20can%20be%20flexibly%20composed%20with%20%3CA%20href%3D%22https%3A%2F%2Fpytorch.org%2Fdocs%2Fstable%2Fgenerated%2Ftorch.nn.Module.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Etorch.nn.Module%3C%2FA%3E%2C%20allowing%20the%20user%20to%20wrap%20part%20or%20whole%20of%20the%20model%20to%20run%20with%20ORT.%20For%20instance%2C%20users%20can%20choose%20to%20wrap%20the%20encoder-decoder%20portion%20of%20the%20model%20while%20leaving%20the%20loss%20function%20in%20PyTorch.%20ORT%20will%20speed%20up%20the%20wrapped%20portion%20of%20the%20model.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EONNX%20Runtime%20for%20PyTorch%20has%20been%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fhuggingface-transformers%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eintegrated%3C%2FA%3E%20to%20run%20popular%20Hugging%20Face%20models%20with%20a%20centralized%20code%20change.%20Additional%20installation%20instructions%20can%20be%20found%20at%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpytorch%2Fort%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Epytorch%2Fort%3C%2FA%3E%20and%20samples%20can%20be%20found%20at%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime-training-examples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20Training%20Examples.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-269399766%22%20id%3D%22toc-hId-269399766%22%3EPerformance%20Results%3C%2FH2%3E%0A%3CP%3EWe%20have%20validated%20Hugging%20Face%20Transformer%20models%20with%20performance%20gains%20in%20samples%2Fsecond%20ranging%20from%2037%25%20(baseline%20PyTorch)%20to%2086%25%20(combined%20with%20DeepSpeed)%20for%20different%20models%20for%20pre-training%20and%20fine-tuning%20scenarios.%20The%20performance%20for%20all%20runs%20was%20measured%20with%20models%20running%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Fndv2-series%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20NDv2%20SKU%3C%2FA%3E%20on%20a%20single%20node%20(except%20for%20the%20A100%20results)%2C%20with%20torch%20autocast%20as%20the%20mixed%20precision%20solution.%20Please%20refer%20to%20the%20configuration%20mentioned%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime-training-examples%2Ftree%2Fmaster%2Fhuggingface%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eonnx-runtime-training-examples%3C%2FA%3E%20repo%20to%20reproduce%20the%20results.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_0-1626160648567.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295403i11DF200351A067D8%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_0-1626160648567.png%22%20alt%3D%22kshamamsft_0-1626160648567.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%201%3C%2FEM%3E%3CEM%3E%3A%20ORT%20throughput%20for%20PyTorch%20Hugging%20Face%20models%3C%2FEM%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EONNX%20Runtime%20for%20PyTorch%20also%20supports%20%3CA%20href%3D%22https%3A%2F%2Fwww.nvidia.com%2Fen-us%2Fdata-center%2Fa100%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EA100%20Tensor%20Core%20GPU%3C%2FA%3E%20showing%20upto%201.31x%20throughput%20improvements%20on%20some%20Hugging%20Face%20models%20as%20seen%20in%20Figure%202.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_1-1626135034004.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295295i58DB07421EFAAD6C%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_1-1626135034004.png%22%20alt%3D%22kshamamsft_1-1626135034004.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%202%3C%2FEM%3E%3CEM%3E%3A%20ORT%20throughput%20on%20A100%20Tensor%20Core%20GPU%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20speedup%20is%20a%20result%20of%20various%20graph%20optimizations%2C%20use%20of%20fused%20and%20optimized%20GPU%20kernels%2C%20and%20efficient%20memory%20handling%20that%20ORT%20performs%20in%20the%20backend.%20We%20will%20discuss%20more%20details%20in%20the%20following%20sections.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1538054697%22%20id%3D%22toc-hId--1538054697%22%3E10%2C000%20foot%20view%20of%20ORTModule%20internals%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--847493223%22%20id%3D%22toc-hId--847493223%22%3E%3CSTRONG%3EOverview%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3EORTModule%20is%20a%20python%20wrapper%20around%20torch.nn.Module%20(Figure%203%2C%20ORTModule%20wraps%20around%20torch.nn.Module)%20that%20intercepts%20forward%20and%20backward%20calls%20and%20delegates%20them%20to%20ONNX%20Runtime%20backend%20to%20achieve%20better%20training%20performance.%20ORTModule%20serves%20as%20a%20drop-in%20replacement%20for%20any%20torch.nn.Module%20for%20ease%20of%20use.%20Upon%20the%20initial%20forward%20call%2C%20the%20PyTorch%20module%20is%20exported%20to%20ONNX%20graph%20using%20%3CA%20href%3D%22https%3A%2F%2Fpytorch.org%2Fdocs%2Fstable%2Fonnx.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Etorch-onnx%3C%2FA%3E%20exporter%2C%20which%20is%20then%20used%20to%20create%20a%20session.%20ORT%E2%80%99s%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime%2Fblob%2Fmaster%2Forttraining%2Forttraining%2Fcore%2Fframework%2Fortmodule_graph_builder.cc%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Enative%20auto-differentiation%3C%2FA%3E%20is%20invoked%20during%20session%20creation%20by%20augmenting%20the%20forward%20graph%20to%20insert%20gradient%20nodes%20(backward%20graph).%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-ai%2Fonnx-runtime-training-technical-deep-dive%2Fba-p%2F1398310%22%20target%3D%22_self%22%3EStatic%20graph%20transformations%3C%2FA%3E%20such%20as%20constant%20folding%2C%20redundant%20operation%20elimination%2C%20and%20operator%20fusions%20are%20further%20applied%20to%20optimize%20the%20computation%20graph.%20ORTModule%20backend%20uses%20highly%20optimized%20kernels%20to%20execute%20the%20graph%20with%20optimal%20use%20of%20the%20GPU%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-right%22%20image-alt%3D%22kshamamsft_2-1626127015497.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295233i8FC24BEF8007B9AC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_2-1626127015497.png%22%20alt%3D%22kshamamsft_2-1626127015497.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CEM%3EFigure%203%3A%20ORTModule%20execution%20flow%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1640019610%22%20id%3D%22toc-hId-1640019610%22%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EPartial%20Graph%20Execution%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3EPyTorch%20executes%20the%20model%20with%20separate%20forward%20and%20backward%20calls%2C%20whereas%20ORT%20represents%20the%20model%20as%20a%20single%20static%20computation%20graph.%20ORTModule%20implements%20a%20partial%20graph%20executor%20to%20mimic%20PyTorch%E2%80%99s%20forward%20and%20backward%20calls.%20Graph%20state%2C%20such%20as%20the%20stashed%20intermediate%20tensor%2C%20between%20a%20pair%20of%20forward%20and%20backward%20calls%20is%20captured%20and%20shared%20through%20RunContext%20(Figure%203).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--167434853%22%20id%3D%22toc-hId--167434853%22%3E%3CSTRONG%3ETensor%20Exchange%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3EThe%20tensors%20such%20as%20module%20input%2C%20outputs%2C%20gradients%2C%20etc.%20are%20exchanged%20between%20PyTorch%20and%20ORT%20using%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fdmlc%2Fdlpack%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EDLPack%3C%2FA%3E%20to%20avoid%20any%20memory%20copy.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1974889316%22%20id%3D%22toc-hId--1974889316%22%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EUnified%20Memory%20Allocator%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3EORTModule%20uses%20PyTorch%E2%80%99s%20allocator%20for%20GPU%20tensor%20memory%20management.%20This%20is%20done%20to%20avoid%20having%20two%20allocators%20that%20can%20hide%20free%20memory%20from%20each%20other%20leading%20to%20inefficient%20memory%20utilization%20and%20reducing%20the%20maximum%20batch%20size%20that%20can%20be%20reached.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_3-1626127015501.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295235iD14FEA81F4A459EE%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22kshamamsft_3-1626127015501.png%22%20alt%3D%22kshamamsft_3-1626127015501.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%204%3A%20Unified%20memory%20allocator%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1985392420%22%20id%3D%22toc-hId--1985392420%22%3EComposability%20with%20Acceleration%20Libraries%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1294830946%22%20id%3D%22toc-hId--1294830946%22%3E%3CSTRONG%3EIntegration%20with%20DeepSpeed%20%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3EONNX%20Runtime%20for%20PyTorch%20supports%20a%20seamless%20integration%20with%20%3CA%20href%3D%22https%3A%2F%2Fwww.deepspeed.ai%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EDeepSpeed%3C%2FA%3E%20to%20further%20accelerate%20distributed%20training%20for%20increasingly%20large%20models.%20The%20gains%20compose%20well%20as%20we%20see%20significant%20gains%20over%20a%20variety%20of%20Hugging%20Face%20models%20with%20DeepSpeed%20and%20ORT.%20We%20see%20gains%20ranging%20from%2058%25%20to%2086%25%20for%20Hugging%20Face%20models%20over%20PyTorch%20by%20using%20DeepSpeed%20ZeRO%20Stage%201%20with%20ORT%20(Figure%205).%20Currently%2C%20ORTModule%20supports%20composing%20with%20DeepSpeed%20FP16%2C%20ZeRO%20Stage%201%20and%202.%20Further%20improvements%20for%20ZeRO%20Stage%202%20are%20in%20progress.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_2-1626135650107.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295296iBB6861BF96E34DB7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_2-1626135650107.png%22%20alt%3D%22kshamamsft_2-1626135650107.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%205%3C%2FEM%3E%3CEM%3E%3A%20ORT%20throughput%20improvements%20with%20DeepSpeed%20ZeRO%20Stage%201%20%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-497518408%22%20id%3D%22toc-hId-497518408%22%3E%3CSTRONG%3EMixed%20precision%20support%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3EONNX%20Runtime%20supports%20mixed%20precision%20training%20with%20a%20variety%20of%20solutions%20like%20PyTorch%E2%80%99s%20native%20%3CA%20href%3D%22https%3A%2F%2Fpytorch.org%2Fdocs%2Fstable%2Famp.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAMP%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FNVIDIA%2Fapex%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ENvidia%E2%80%99s%20Apex%20O1%3C%2FA%3E%2C%20as%20well%20as%20with%20%3CA%20href%3D%22https%3A%2F%2Fwww.deepspeed.ai%2Fdocs%2Fconfig-json%2F%23fp16-training-options%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EDeepSpeed%20FP16%3C%2FA%3E.%20This%20allows%20the%20user%20with%20flexibility%20to%20avoid%20changing%20their%20current%20set%20up%20to%20bring%20ORT%E2%80%99s%20acceleration%20capabilities%20to%20their%20training%20workloads.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFigure%201%20in%20the%20%22Performance%20results%22%20section%20above%20shows%20speedup%20with%20ORT%20for%20Pytorch%20autocast.%20We%20see%20gains%20ranging%20from%2011%25%20to%2037%25%20by%20using%20ONNX%20Runtime%20for%20Pytorch.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFigure%206%20shows%20speedup%20with%20ORT%20combined%20with%20DeepSpeed%20FP16%20against%20baseline%20PyTorch.%20We%20see%20gains%20ranging%20from%2022%25%20to%2086%25%20on%20popular%20Hugging%20Face%20models.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_3-1626135944666.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295297i035CCD02F68886E3%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_3-1626135944666.png%22%20alt%3D%22kshamamsft_3-1626135944666.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%206%3C%2FEM%3E%3CEM%3E%3A%20ORT%20throughput%20improvements%20with%20DeepSpeed%20FP16%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFigure%207%20shows%20speedup%20for%20using%20ORT%20with%20NVIDIA%E2%80%99s%20Apex%20O1%2C%20giving%208%25%20to%2023%25%20gains%20over%20PyTorch.%3C%2FP%3E%0A%3CP%3E.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22kshamamsft_4-1626136002444.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F295298iE63B2FADAEEE0208%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22kshamamsft_4-1626136002444.png%22%20alt%3D%22kshamamsft_4-1626136002444.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CEM%3EFigure%207%3C%2FEM%3E%3CEM%3E%3A%20ORT%20throughput%20improvements%20with%20Apex%20O1%20mixed%20precision%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1439018774%22%20id%3D%22toc-hId--1439018774%22%3ELooking%20Forward%3C%2FH2%3E%0A%3CP%3EThe%20ONNX%20Runtime%20team%20is%20working%20on%20more%20exciting%20optimizations%20to%20make%20training%20large%20workloads%20even%20faster.%20ONNX%20Runtime%20for%20PyTorch%20plans%20to%20add%20support%20for%20custom%20%3CA%20href%3D%22https%3A%2F%2Fpytorch.org%2Fdocs%2Fstable%2Fautograd.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Etorch.autograd%3C%2FA%3E%20functions%20which%20would%20allow%20the%20graph%20execution%20to%20switch%20back%20to%20PyTorch%20for%20user-defined%20autograd%20functions.%20This%20would%20allow%20us%20to%20support%20advanced%20scenarios%20like%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FNVIDIA%2FMegatron-LM%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Ehorizontal%20parallelism%3C%2FA%3E%20and%20Mixture%20of%20Expert%20models.%20Further%20improvements%20and%20support%20for%20DeepSpeed%20ZeRO%20Stage%202%20and%203%20have%20also%20been%20planned%20for%20future%20releases.%20The%20team%20will%20also%20be%20adding%20samples%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime-training-examples%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20Training%20Examples%3C%2FA%3E%20in%20the%20future%20as%20we%20support%20more%20types%20of%20models%20and%20scenarios.%20Be%20sure%20to%20check%20out%20the%20links%20below%20to%20learn%20more%20and%20get%20started%20with%20ONNX%20Runtime%20for%20PyTorch!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1048494059%22%20id%3D%22toc-hId-1048494059%22%3EGetting%20Started%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fcloudblogs.microsoft.com%2Fopensource%2F%3Fp%3D86985%26amp%3Bpreview%3D1%26amp%3B_ppp%3Da2c682f23a%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EOverview%20of%20ONNX%20Runtime%20Training%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fcloudblogs.microsoft.com%2Fopensource%2F%3Fp%3D86937%26amp%3Bpreview%3D1%26amp%3B_ppp%3D0afa9b5a7d%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20Training%20on%20AMD%20GPUs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime%2Ftree%2Fmaster%2Forttraining%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20Training%20Github%20repo%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime-training-examples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20Training%20Examples%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fresearch%2Fblog%2Fzero-deepspeed-new-system-optimizations-enable-training-models-with-over-100-billion-parameters%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EZeRO%20%26amp%3B%20DeepSpeed%3CSPAN%3E%3A%20New%20system%20optimizations%20enable%20training%20models%20with%20over%20100%20billion%20parameters%3C%2FSPAN%3E%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2540471%22%20slang%3D%22en-US%22%3E%3CP%3EGet%20a%20closer%20look%20at%20how%20ONNX%20Runtime%20(ORT)%20for%20PyTorch%20brings%20about%20significant%20throughput%20improvements%20for%20large%20scale%20transformer%20model%20training.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2540471%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Machine%20Learning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMachine%20Learning%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jul 13 2021 09:03 AM
Updated by: