torch.accelerator: A Unified, Device-Agnostic Runtime API for Stream-Based Accelerators - Yu Guangye

PyTorch · Advanced ·🔧 Backend Engineering ·1y ago

Key Takeaways

Introduces torch.accelerator, a unified device-agnostic runtime API for stream-based accelerators in PyTorch

Full Transcript

Thanks everyone for being here. Uh I'm from Intel. Uh today I'm excited to introduce a new API set that is coming in pouch uh 2.6 uh touch. Accelerator uh an unified device agnostic runtime API for streambased accelerator. Um, pyarch as the mo as the most widely used machine learning uh framework already support a variety of acceleration uh hardware beyond uh CPU such as CUDA uh MPS, XPU, HPU and MPU. um for uh it's well uh design architecture enable seamless backend integration uh through two uh key uh passes. Uh for uh eagle mode uh is it's based on operators and device runtime system. uh for graph mode uh is powered by inductor on top of treat uh and device uh runtime uh and each back end has its own uh runtime such as uh CUDA runtime, MPS runtime and XP runtime. Uh in eagle mode uh operators are inherently uh DS agnostic. uh most ATN operators uh accept uh device uh argument that can be said uh CUDA or CPU or other back ends. Um for graph mode inductor provide a flexible uh registration uh mechanism uh through the device interface uh abstraction. Uh this base class uh defined an API set that uh each back end uh should implement. Uh inductor use this interface to uh support uh various device back ends. Uh however the pouch runtime uh is slightly uh coupled with device specific module. uh for for for instance if you called touch.cuda current device in your code it will only works on kuda device and will fail on other back ends. uh this uh device specific logic uh limits code portability and prevent user uh from writing device agnostic code. Uh uh here is an example of how to support uh uh this scribed by uh using uh device specific uh runtime API. uh it requires using uh so many if else statements to handle each uh back end type. Uh this approach this approach post uh uh significant uh uh challenge for hardware vendor who attempt to uh integrate support for a new back end into pouch or its ecosystem. uh it's not scalable and maintainable uh for um to use this if else conditionals in these uh repositories. Uh if pyarch could provide a unified agnostic uh runtime API, it would greatly simplify the progress of uh adding new backend support and reduce the integration uh burden for hardware vendor and uh ecosystem developers. Uh as pouch continue to expand to support uh a growing variety of hardware accelerators um the need for a device agnostic programming interface become increasingly critical. Uh so we have a board vision uh make your code run on any device through a unified device agnostic runtime API. Uh fortunately uh a significant advance uh happened in parch 2.5 uh which introduce an accelerator concept. Uh an accelerator is explicitly uh defined uh as a torch do device used alongside the CPU to speed up computation. Um it adopts an uh synchronous uh execution model to handle uh and handle uh synchronization through stream and event. Uh pouch currently assume only one uh accelerator type is available uh on a given host at a time. Uh the supported uh accelerator uh includes CUDA uh XPU, MPS uh MTI, HPU and private user one. Uh notably CPU is not uh classified as an accelerator under this definition. Uh inspired by accelerator concept uh we proposed a unified device agnostic runtime API uh in pyarch 2.6 Six. Uh this API are designed uh to closely measure uh existing uh device specific uh API uh offering almost a one one mapping functionality and they uh accept the same uh input argument uh ensure minimal code change are needed when migrating from device specific API code. Uh in addition uh we introduce a new util utility touch accelerator current accelerator uh on the right list. The first one uh it uh uh returns the currently uh active uh accelerator type um as determined at compare time. Uh it means if you uh if patch build with CUDA support it return CUDA. Uh if build with XPU support it return XPU. Um if Pach is built only uh CPU support it will return none. uh minimal uh patch also provide the corresponding C++ API to uh easier for uh developer to write device agnostic code in C++. uh they are offering uh one one mapping functionality uh with touch accelerator passing API uh as a unified runtime uh touch accelerator uh overlaps with some device specific uh uh API to maintain uh backward compatibility uh both will continue to coexist uh and uh for functional consistence uh they are designed um work seamlessly together. Uh it means uh if you uh use a device specific API to uh set the current device to index one uh this change will be reflected uh by uh the touch accelerator when he curing the uh current device interface uh device index and vice versa. Uh now we already have the generic uh uh runtime API as mentioned above. But how do we handle the uh other uh runtime components uh comp components uh such as the stream and the event. Uh this issue was uh addressed in pyarch 2.5 uh pouch refactor touch stream and touch invent uh into a base class that support some some uh common uh method. Uh these common methods are available across different uh back ends. Uh and the device and the device specific stream or event inherence this base class and can override uh method as needed. Uh now uh and touch data stream uh can represent a CUDA stream on CUDA device and uh XP stream on uh XP device. Uh this b uh this design uh the benefit of this design is that since touch stream and the touche event is base class uh we can capture any device specific uh counterpart as the base class uh even if uh uh in in in Dynamo or other components uh especially for auto tree back And uh when discussing the design philosophy uh of touch accelerator here is a quick uh story. Uh our initial idea was to put a unified runtime uh under touch name space directly um uh such as touch.fu. Uh this uh this is inspired by uh how touch.xpu.stream was generalized into touch.stream. Uh following this model uh touch.xpu.fu uh would be generalized to uh touch.fu. uh but uh uh after discussing uh with Elvin and and other uh community contributor uh we decide to put uh this API uh and touch accelerator uh name space. This is a new uh name space introduced in pyarch 2.6. Um and we have and the uh le uh clear uh design ruler. Uh if uh API work across uh CPU and accelerator uh it should uh uh goes in uh touch n space and uh accept a device type argument. Um and if uh API is specific to accelerator it would be place uh touch accelerator name space and doesn't take a device type step. Uh before touch accelerator pouch use different method to support various uh device back ends. uh for example FSDP uh use touch.get device module which u delegate to uh device specific uh module and inductor use an inheritance pattern uh where each back end should uh inherent from an base uh class and implement it subclass. Uh this delegation and inheritance patterns are also commonly used in other uh popular uh repositories such as for hug and force hug and fist. Um compared to previous approaches uh touch accelerator provide a unified uh built-in API that is easier to uh use in Dynamo and more uh pass. It provide a more elegant and uh consistent uh uh interface for device agnostic programming. The main uh but the main uh limitation is that uh touch accelerator currently only support a subset of API uh that are available through two other uh approaches provided uh for example uh for some memory mag uh uh for some memory related faction uh such such as touch uh device tab uh empty cache uh is currently not yet available uh in touch now accelerator name space. Um how to support touch accelerator for new back end uh is is straightforward. uh you only need to uh implement uh uh the device guard implement interface and register it uh instant instance. uh this leverage an exciting uh existing uh mechanism uh through uh the stream guard and the device guard uh in C++ reuse. Uh in pouch we can categorize uh runtime uh into six components uh such uh device stream event uh got generator and allocator. Um we uh now we already generalize uh device stream and event. The next challenge is how to uh handle stream guard and uh uh device guard. [Music] Uh previously pouch offered touch device uh type stream as uh uh context manager to ensure the operation is running on the expected stream. Um but now uh from parch 2.7 uh the dessert stream can be used directly through this statement. Uh it's easier to uh use to generalize uh for different uh back end. uh similar pouch uh provide touch device type device as a context manager to ensure the operation is running on the expected device. Uh it's a front end pass API. Um but now uh touch accelerator provide a new uh API touch. Accelerator device index to do the same thing. uh is um uh it can be used uh in uh on different uh uh device back end. Uh after analyzing usage across different uh popular repositories, we found that some memory related API are commonly used in line with RFC 134 uh 078. uh we propose uh introducing a set of uh touch uh set of device memory API um and uh would put them uh under touch accelerator name space to better support uh the common uh user case. As another followup, uh we plan to generalize pyarch unit test uh to improve uh reusability acrossing different uh u back ends. Uh now uh I'd like to revisit the uh example mentioned earlier. Uh with touch accelerator the implementation becomes much clearer and uh simpler. It makes the refined code uh can run on any device without any adaptation. Uh finally I would like to express my sincere gratitude to the community maintainer airbend. Uh his insights and guidance have been instrumental in driving uh this work forward. Uh thanks

Original Description

torch.accelerator: A Unified, Device-Agnostic Runtime API for Stream-Based Accelerators - Yu Guangye, Intel Motivation PyTorch supports a wide range of acceleration hardware beyond CPUs, including CUDA, XPU, MPS, NPU, HPU, and more. Its architecture allows new backend integration through two key components: ATen operators and device runtime. While ATen operators are device-agnostic, the runtime remains device-specific, relying on APIs like torch.cuda and torch.xpu. This fragmentation complicates writing portable, hardware-agnostic code across PyTorch and its ecosystem. To address this challenge, we propose torch.accelerator: a unified, device-agnostic runtime API for stream-based accelerators. Design An Accelerator refers to a device that collaborates with the CPU to accelerate computation, typically via asynchronous execution using Stream and Event for synchronization. Our design assumes a single active accelerator per host. The torch.accelerator API provides a consistent interface for device and stream management, with backend support integrated via the existing DeviceGuardImplInterface registration mechanism. Further Work We are actively working on a unified device memory API. These will streamline the library, model, and UTs. Reference - https://github.com/pytorch/pytorch/pull/132204
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from PyTorch · PyTorch · 0 of 60

← Previous Next →
1 What is PyTorch?
What is PyTorch?
PyTorch
2 PyTorch Tutorial: A Quick Preview
PyTorch Tutorial: A Quick Preview
PyTorch
3 PyTorch Summer Hackathon 2019
PyTorch Summer Hackathon 2019
PyTorch
4 Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
PyTorch
5 PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch
6 Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
PyTorch
7 Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
PyTorch
8 Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
PyTorch
9 Introduction to Machine Learning for Developers at F8 2019
Introduction to Machine Learning for Developers at F8 2019
PyTorch
10 Powered by PyTorch at F8 2019
Powered by PyTorch at F8 2019
PyTorch
11 Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
PyTorch
12 New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
PyTorch
13 PyTorch Developer Conference 2018: Recap
PyTorch Developer Conference 2018: Recap
PyTorch
14 PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch
15 PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch
16 PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch
17 PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch
18 PyTorch Developer Conference 2019 | Full Livestream
PyTorch Developer Conference 2019 | Full Livestream
PyTorch
19 PyTorch Developer Conference 2019: Recap
PyTorch Developer Conference 2019: Recap
PyTorch
20 PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch
21 What’s new in PyTorch 1.3 - Lin Qiao
What’s new in PyTorch 1.3 - Lin Qiao
PyTorch
22 PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch
23 Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
PyTorch
24 Quantization - Dmytro Dzhulgakov
Quantization - Dmytro Dzhulgakov
PyTorch
25 PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch
26 Apex -  Michael Carilli, NVIDIA
Apex - Michael Carilli, NVIDIA
PyTorch
27 Dataloader Design for PyTorch - Tongzhou Wang, MIT
Dataloader Design for PyTorch - Tongzhou Wang, MIT
PyTorch
28 Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
PyTorch
29 PyTorch Mobile - David Reiss
PyTorch Mobile - David Reiss
PyTorch
30 Model Interpretability with Captum - Narine Kokhilkyan
Model Interpretability with Captum - Narine Kokhilkyan
PyTorch
31 Detectron2 - Next Gen Object Detection Library - Yuxin Wu
Detectron2 - Next Gen Object Detection Library - Yuxin Wu
PyTorch
32 Speech Extensions to Fairseq - Dmytro Okhonko
Speech Extensions to Fairseq - Dmytro Okhonko
PyTorch
33 PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch
34 PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch
35 PyTorch in Robotics - Yisong Yue, Caltech
PyTorch in Robotics - Yisong Yue, Caltech
PyTorch
36 StanfordNLP - Yuhao Zhang, Stanford
StanfordNLP - Yuhao Zhang, Stanford
PyTorch
37 Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
PyTorch
38 Collaborative Natural Language Inference - Sasha Rush, Cornell
Collaborative Natural Language Inference - Sasha Rush, Cornell
PyTorch
39 Privacy Preserving AI - Andrew Trask, OpenMined
Privacy Preserving AI - Andrew Trask, OpenMined
PyTorch
40 CrypTen - Laurens van der Maaten
CrypTen - Laurens van der Maaten
PyTorch
41 PyTorch at Uber - Sidney Zhang, Uber
PyTorch at Uber - Sidney Zhang, Uber
PyTorch
42 PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch
43 PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch
44 PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch
45 PyTorch Developer Conference 2019 - Panel Discussion
PyTorch Developer Conference 2019 - Panel Discussion
PyTorch
46 Using deep learning and PyTorch to power next gen aircraft at Caltech
Using deep learning and PyTorch to power next gen aircraft at Caltech
PyTorch
47 Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
PyTorch
48 TorchScript and PyTorch JIT | Deep Dive
TorchScript and PyTorch JIT | Deep Dive
PyTorch
49 Announcing the PyTorch Global Summer Hackathon 2020
Announcing the PyTorch Global Summer Hackathon 2020
PyTorch
50 Opening Up the Black Box: Model Understanding with Captum and PyTorch
Opening Up the Black Box: Model Understanding with Captum and PyTorch
PyTorch
51 PyTorch Mobile Runtime for Android
PyTorch Mobile Runtime for Android
PyTorch
52 Torchvision in 5 minutes
Torchvision in 5 minutes
PyTorch
53 3D Deep Learning with PyTorch3D
3D Deep Learning with PyTorch3D
PyTorch
54 What is Torchtext?
What is Torchtext?
PyTorch
55 TorchAudio: A Quick Intro
TorchAudio: A Quick Intro
PyTorch
56 PyTorch Mobile Runtime for iOS
PyTorch Mobile Runtime for iOS
PyTorch
57 PySlowFast: Deep learning with Video
PySlowFast: Deep learning with Video
PyTorch
58 PyTorch Pruning | How it's Made by Michela Paganini
PyTorch Pruning | How it's Made by Michela Paganini
PyTorch
59 Measuring Fairness in Machine Learning Systems
Measuring Fairness in Machine Learning Systems
PyTorch
60 PyTorch for Hackathons
PyTorch for Hackathons
PyTorch

Related Reads

Up next
/dev/push: An Open Vercel Alternative to Ship Your Apps Quickly
Ian Wootten
Watch →