diff --git "a/blogs_splitted_dataset.jsonl" "b/blogs_splitted_dataset.jsonl" --- "a/blogs_splitted_dataset.jsonl" +++ "b/blogs_splitted_dataset.jsonl" @@ -1,5150 +1,1958 @@ -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Trace Analysis for the Masses\"\nauthor: Anupam Bhatnagar, Xizhou Feng, Brian Coutinho, Yifan Liu, Sung-Han Lin, Louis Feng, and Yuzhen Huang\n---", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the public release of Holistic Trace Analysis (HTA), an open source performance analysis and visualization Python library for PyTorch users. HTA takes as input [Kineto traces](https://github.com/pytorch/kineto) collected by the [PyTorch profiler](https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/), which are complex and challenging to interpret, and up-levels the performance information contained in these traces. It was initially developed", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "internally at Meta to understand and debug performance problems for large-scale distributed training jobs on GPUs. The multidisciplinary team has made a number of enhancements to HTA\u2019s features and scaled them to support state-of-the-art ML workloads.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "ML researchers and systems engineers often struggle to computationally scale up their models because they are not aware of the performance bottlenecks in their workloads. The resources requested for a job (e.g. GPUs, memory) are often misaligned with the resources actually required due to lack of visibility \u201cunder the hood\u201d. To achieve the best performance from the hardware stack, it is imperative to understand the resource utilization and bottlenecks for distributed training workloads.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "The initial HTA implementation was specifically targeted at Deep Learning Based Recommendation Models (DLRM). To make the features in HTA generic and applicable to use cases such as analyzing Vision and NLP models, we decided to refactor the HTA codebase and make the library available to the larger community. This new codebase has implemented several important ideas which lead to significant efficiency and performance improvements.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Trace Analysis for the Masses\"\nauthor: Anupam Bhatnagar, Xizhou Feng, Brian Coutinho, Yifan Liu, Sung-Han Lin, Louis Feng, and Yuzhen Huang\n---\n\nWe are excited to announce the public release of Holistic Trace Analysis (HTA), an open source performance analysis and visualization Python library for PyTorch users. HTA takes as input [Kineto traces](https://github.com/pytorch/kineto) collected by the [PyTorch profiler](https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/), which are complex and challenging to interpret, and up-levels the performance information contained in these traces. It was initially developed internally at Meta to understand and debug performance problems for large-scale distributed training jobs on GPUs. The multidisciplinary team has made a number of enhancements to HTA\u2019s features and scaled them to support state-of-the-art ML workloads.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "ML researchers and systems engineers often struggle to computationally scale up their models because they are not aware of the performance bottlenecks in their workloads. The resources requested for a job (e.g. GPUs, memory) are often misaligned with the resources actually required due to lack of visibility \u201cunder the hood\u201d. To achieve the best performance from the hardware stack, it is imperative to understand the resource utilization and bottlenecks for distributed training workloads.\n\nThe initial HTA implementation was specifically targeted at Deep Learning Based Recommendation Models (DLRM). To make the features in HTA generic and applicable to use cases such as analyzing Vision and NLP models, we decided to refactor the HTA codebase and make the library available to the larger community. This new codebase has implemented several important ideas which lead to significant efficiency and performance improvements.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} {"page_content": "In this blog, we present several features implemented in the open source version of HTA, which can be used as a Python script as well as interactively in a Jupyter notebook. HTA provides the following features:", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "1. **Breakdown by Dimensions**\n 1. **Temporal**: Breakdown of GPU time in terms of time spent in computation, communication, memory events, and idle time on a single node and across all ranks.\n 1. **Idle Time**: Breakdown of GPU idle time into waiting for the host, waiting for another kernel or attributed to an unknown cause.\n 1. **Kernel**: Find kernels with the longest duration on each rank.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "1. **Communication Computation Overlap**: Calculate the percentage of time when communication overlaps computation.\n1. **Statistical Analysis**\n 1. **Kernel Duration Distribution**: Distribution of average time taken by longest kernels across different ranks.\n 1. **CUDA Kernel Launch**: Distributions of GPU kernels with very small duration, large duration, and excessive launch time.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "1. **Augmented Counters (Memory bandwidth, Queue length)**: Augmented trace files which provide insights into memory copy bandwidth and number of outstanding operations on each CUDA stream.\n1. **Patterns**\n 1. **Frequent CUDA Kernels**: Find the CUDA kernels most frequently launched by any given PyTorch or user defined operator.\n1. **Trace Comparison**\n 1. **Trace Diff**: A trace comparison tool to identify and visualize the differences between traces.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "HTA source code is available to users via [Github](https://github.com/facebookresearch/HolisticTraceAnalysis). Users can request new features or build their own analysis using the core libraries and data structures provided in the codebase in addition to the features mentioned above.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "GPU Training Performance Debugging 101\n\nTo understand the GPU performance in distributed training jobs, we consider how the model operators interact with the GPU devices and how such interactions are reflected in certain measurable metrics.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "At a high level, we can break down the GPU operations in a model execution into three broad categories, henceforth referred to as kernel types: \n1. **Computation (COMP)** - Compute kernels execute compiled routines for matrix multiplication and similar numeric calculations. They are responsible for all of the number-crunching necessary for model execution.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "1. **Communication (COMM)** - Communication kernels are routines which are responsible for exchanging and synchronizing data between different GPU devices in a distributed training job. The NVIDIA Collective Communication Library (NCCL) is a widely used communication library and all its kernels have the prefix \u201cnccl\u201d. Example NCCL kernels include NCCL_AllGather, NCCL_ReduceScatter, NCCL_AllReduce, etc.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "1. **Memory (MEM)** - Memory kernels manage the memory allocations/deallocations on the GPU devices and data movement between the memory space on the host and the GPUs. The memory kernels include Memcpy_H2D, Memcpy_D2H, Memcpy_D2D, Memset, etc. Here, H represents the Host and D represents the GPU Device. Thus, H2D, D2H, D2D stands for Host to Device, Device to Host and Device to Device respectively.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Because a modern GPU device like the NVIDIA A100 GPU is a massively parallel device which is capable of running multiple kernels simultaneously, it is possible to overlap the computation, communication, and memory kernels to reduce the model execution time. One common technique to achieve the overlap is to utilize multiple CUDA streams. A CUDA stream is a sequence of operations that execute on a GPU device in the order in which they are issued by the host code. Different CUDA streams can be interleaved and", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "even run concurrently, thus achieving the effect of kernel overlap.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "To help understand the above concepts, Figure 1 provides a timeline of the GPU kernels in a sample distributed training job on 8 GPUs for one iteration. In the figure below, each rank represents one GPU and the kernels on each GPU run on 6 CUDA streams. In the right column of the figure, you can see names of the GPU kernels used. In the middle of the figure, you see the overlap between compute and communicate kernels. This figure is created using the [plot_timeline example", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "notebook](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/examples/plot_timeline.ipynb) available in HTA.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "![Figure 1. An example of the execution timeline of GPU Kernels across multiple ranks](/assets/images/trace-image6.png){:width=\"100%\"}\n\n*Figure 1. An example of the execution timeline of GPU Kernels across multiple ranks*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "The performance of multiple GPU training jobs is affected by multiple factors. Among these factors, how does a model execution create and orchestrate the GPU kernels plays a critical role. HTA provides insights on how the model execution interacts with the GPU devices and highlights the opportunities for performance improvement.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "With the features we built in HTA, we aim to provide users insights into \u201cwhat is happening under the hood in a distributed GPU training?\u201d We briefly describe these features in the next few paragraphs.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Features in Holistic Trace Analysis \n\nFor most users, understanding the performance of GPU training jobs is nontrivial. Thus, we built this library to simplify the task of trace analysis and provide the user useful insights by examining the model execution traces. As the first step, we developed features which are important and generic enough so that most users can benefit from this library.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "**Temporal Breakdown**: We begin by asking whether the GPU is spending time on computation, communication, memory events, or is it idle? To answer this question, the temporal breakdown feature presents a breakdown in terms of these categories. To achieve high training efficiency the code should maximize time used by computation kernels and minimize idle time and non-compute time (time used by communication or memory kernels). This is accomplished by implementing concurrent execution of computation kernels", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "with communication or memory kernels. *Note that, during concurrent execution of computation kernels with communication/memory kernels the time spent by communication/memory kernels is accounted for under compute time.*", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "![Figure 2: Temporal Breakdown across 8 GPUs](/assets/images/trace-image3.png){:width=\"100%\"}\n\n*Figure 2: Temporal Breakdown across 8 GPUs*\n{: style=\"text-align: center;\"}\n\n**Kernel Breakdown**: It is natural to ask which kernels are taking the most amount of time. The next feature breaks down the time spent within each kernel type (COMM, COMP, MEM) and sorts them by duration. We present this information for each kernel type and for each rank as a pie chart. See figure 3 below.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "![Figure 3: Pie chart of top computation and communication kernels](/assets/images/trace-image1.png){:width=\"100%\"}\n\n*Figure 3: Pie chart of top computation and communication kernels*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "**Kernel Duration Distribution**: Subsequently, one can also ask - for any given kernel, what is the distribution of the time spent across the ranks? To answer this, HTA generates bar graphs for the average duration of a given kernel across all ranks. Additionally, the error bars in the bar graphs show the minimum and maximum amount of time taken by a given kernel on a given rank. Figure 4 below shows a discrepancy between average duration on rank 0 as compared to other ranks. This anomalous behavior on", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "rank 0 guides the user on where to look for possible bugs.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "![Figure 4: Average duration of NCCL AllReduce Kernel across 8 ranks](/assets/images/trace-image4.png){:width=\"100%\"}\n\n*Figure 4: Average duration of NCCL AllReduce Kernel across 8 ranks*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "**Communication Computation Overlap**: In distributed training, a significant amount of time is spent in communication and synchronization events among multiple GPU devices. To achieve high GPU efficiency (i.e. TFLOPS/GPU) it is vital to keep the GPU doing actual computation work. In other words, a GPU should not be blocked because of waiting for data from other GPUs. One way to measure the extent to which computation is blocked by data dependencies is to calculate the computation-communication overlap.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Higher GPU efficiency is observed if communication events overlap computation events. Lack of communication and computation overlap will lead to the GPU being idle, thus the efficiency would be low. Thus, the communication computation overlap feature calculates the percentage of time communication and computation overlap in a job for each rank and generates a bar graph representation. See figure below. More precisely, we measure the following ratio", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "(time spent in computation while communicating) / (time spent in communication)\n{: style=\"text-align: center;\"}\n\n\n![Figure 5: Communication computation overlap](/assets/images/trace-image5.png){:width=\"100%\"}\n\n*Figure 5: Communication computation overlap*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "**Augmented Counters (Queue length, Memory bandwidth)**: To aid in debugging, HTA calculates the memory bandwidth statistics for D2H, H2D and D2D memory copy (memcpy) and memory set (memset) events. Additionally, HTA also computes the number of outstanding CUDA operations on each CUDA stream. We refer to this as queue length. When the queue length on a stream is 1024 or larger new events cannot be scheduled on that stream and the CPU will stall until the GPU events have processed. Additionally, HTA", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "generates a new trace file containing tracks with the memory bandwidth and queue length time series. See Figure 6 below.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "![Figure 6: Memory Bandwidth and Queue Length](/assets/images/trace-image2.png){:width=\"100%\"}\n\n*Figure 6: Memory Bandwidth and Queue Length*\n{: style=\"text-align: center;\"}\n\nThese primary features give us a peek into the system performance and help answer \u201cwhat is happening in the system?\u201d. As HTA evolves, we hope to address \u201cwhy is X happening?\u201d and also suggest possible solutions to overcome the bottlenecks.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Installation and Usage\n\n### Installation\n\nFor installing the HTA please refer to the [README](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/README.md). In brief, the user is required to clone the [repo](https://github.com/facebookresearch/HolisticTraceAnalysis) and install the necessary Python packages via pip.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Usage\n\nThis version of Holistic Trace Analysis is currently in beta and we recommend using HTA in a Jupyter notebook. A [demo notebook](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/examples/trace_analysis_demo.ipynb) is provided for your convenience. To get started, import the hta package in a Jupyter notebook, create a TraceAnalysis object and off we go in exactly two lines of code.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom hta.trace_analysis import TraceAnalysis\nanalyzer = TraceAnalysis(trace_dir = \u201c/trace/folder/path\u201d)\n```", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Requirements\n\n- All trace files for a training or inference job must be stored in a unique folder.\n- Trace files are in json or gzipped json format.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "FAQ\n\n#### Q. How can I install HTA?\n\nPlease see the [README](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/README.md) in the root directory of the repository.\n\n#### Q. Is there any documentation on the features and API in HTA?\n\nThe documentation and detailed API is available [here](https://hta.readthedocs.io/).", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Q. Can you implement feature X?\n\nDepending on how widely the feature is needed and the level of effort required to implement it we would consider developing the feature. Please open a [Github Issue](https://github.com/facebookresearch/HolisticTraceAnalysis/issues) and tag it with the feature-request label.\n\n#### Q. Can I modify the code?\n\nPlease do and [send a PR](https://github.com/facebookresearch/HolisticTraceAnalysis/pulls) along the way, if you think it would be useful for others.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "Q. How can I collect traces in PyTorch?\n\nPlease refer to this tutorial [here](https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html#use-profiler-to-record-execution-events).\n\n#### Q. Can HTA be used at production scale?\n\nYes, please see a use case study [here](https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/).", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch adds new dev tools as it hits production scale'\nauthor: The PyTorch Team\n---\n\n_This is a partial re-post of the original blog post on the Facebook AI Blog. The full post can be [viewed here](https://ai.facebook.com/blog/pytorch-adds-new-dev-tools-as-it-hits-production-scale/)_", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Since its release just a few months ago, [PyTorch 1.0](http://pytorch.org/) has been rapidly adopted as a powerful, flexible deep learning platform that enables engineers and researchers to move quickly from research to production. We are highlighting some of the ways the AI engineering and research community is using PyTorch 1.0. We\u2019re also sharing new details about the latest release, PyTorch 1.1, and showcasing some of the new development tools created by the community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Building on the initial launch of PyTorch in 2017, we partnered with the AI community to ship the stable release of PyTorch 1.0 [last December](https://code.fb.com/ai-research/pytorch-developer-ecosystem-expands-1-0-stable-release/). Along with enhanced production-oriented capabilities and deep integration with leading cloud platforms, PyTorch 1.0 expands on the open source library\u2019s core features, with the addition of PyTorch JIT (Just in time compilation) that seamlessly transitions between eager mode and", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "graph mode to provide both flexibility and speed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Leading businesses across industries are beginning to use PyTorch to both facilitate their research and then also deploy at large scale for applications such as translation, computer vision, conversational interfaces, pharmaceutical research, factory optimization, and automated driving research. Community adoption of PyTorch has also continued to expand. Stanford, UC Berkeley, Caltech, and other universities are using PyTorch as a fundamental tool for their machine learning (ML) courses; new ecosystem", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "projects have launched to support development on PyTorch; and major cloud platforms have expanded their integration with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Using PyTorch across industries\n\nMany leading businesses are moving to PyTorch 1.0 to accelerate development and deployment of new AI systems. Here are some examples:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- Airbnb leveraged PyTorch's rich libraries and APIs for conversational AI and deployed a Smart Reply to help the company\u2019s service agents respond more effectively to customers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [ATOM](https://atomscience.org/) is building a platform to generate and optimize new drug candidates significantly faster and with greater success than conventional processes. Using machine learning frameworks such as PyTorch, ATOM was able to design a variational autoencoder for representing diverse chemical structures and designing new drug candidates.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- Genentech is utilizing PyTorch\u2019s flexible control structures and dynamic graphs to train deep learning models that will aid in the development of individualized cancer therapy.\n- Microsoft is using PyTorch across its organization to develop ML models at scale and deploy them via the ONNX Runtime. Using PyTorch, Microsoft Cognition has built distributed language models that scale to billions of words and are now in production in offerings such as Cognitive Services.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- Toyota Research Institute (TRI) is developing a two-pronged approach toward automated driving with Toyota Guardian and Toyota Chauffeur technologies. The Machine Learning Team at TRI is creating new deep learning algorithms to leverage Toyota's 10 million sales per year data advantage. The flexibility of PyTorch has vastly accelerated their pace of exploration and its new production features will enable faster deployment towards their safety critical applications.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "1. **Breakdown by Dimensions**\n 1. **Temporal**: Breakdown of GPU time in terms of time spent in computation, communication, memory events, and idle time on a single node and across all ranks.\n 1. **Idle Time**: Breakdown of GPU idle time into waiting for the host, waiting for another kernel or attributed to an unknown cause.\n 1. **Kernel**: Find kernels with the longest duration on each rank.\n 1. **Communication Computation Overlap**: Calculate the percentage of time when communication overlaps computation.\n1. **Statistical Analysis**\n 1. **Kernel Duration Distribution**: Distribution of average time taken by longest kernels across different ranks.\n 1. **CUDA Kernel Launch**: Distributions of GPU kernels with very small duration, large duration, and excessive launch time.\n 1. **Augmented Counters (Memory bandwidth, Queue length)**: Augmented trace files which provide insights into memory copy bandwidth and number of outstanding operations on each CUDA stream.\n1. **Patterns**\n 1. **Frequent CUDA Kernels**: Find the CUDA kernels most frequently launched by any given PyTorch or user defined operator.\n1. **Trace Comparison**\n 1. **Trace Diff**: A trace comparison tool to identify and visualize the differences between traces.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "HTA source code is available to users via [Github](https://github.com/facebookresearch/HolisticTraceAnalysis). Users can request new features or build their own analysis using the core libraries and data structures provided in the codebase in addition to the features mentioned above.\n\n## GPU Training Performance Debugging 101\n\nTo understand the GPU performance in distributed training jobs, we consider how the model operators interact with the GPU devices and how such interactions are reflected in certain measurable metrics.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "At a high level, we can break down the GPU operations in a model execution into three broad categories, henceforth referred to as kernel types: \n1. **Computation (COMP)** - Compute kernels execute compiled routines for matrix multiplication and similar numeric calculations. They are responsible for all of the number-crunching necessary for model execution. \n1. **Communication (COMM)** - Communication kernels are routines which are responsible for exchanging and synchronizing data between different GPU devices in a distributed training job. The NVIDIA Collective Communication Library (NCCL) is a widely used communication library and all its kernels have the prefix \u201cnccl\u201d. Example NCCL kernels include NCCL_AllGather, NCCL_ReduceScatter, NCCL_AllReduce, etc. \n1. **Memory (MEM)** - Memory kernels manage the memory allocations/deallocations on the GPU devices and data movement between the memory space on the host and the GPUs. The memory kernels include Memcpy_H2D, Memcpy_D2H, Memcpy_D2D, Memset, etc. Here, H represents the Host and D represents the GPU Device. Thus, H2D, D2H, D2D stands for Host to Device, Device to Host and Device to Device respectively.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "Because a modern GPU device like the NVIDIA A100 GPU is a massively parallel device which is capable of running multiple kernels simultaneously, it is possible to overlap the computation, communication, and memory kernels to reduce the model execution time. One common technique to achieve the overlap is to utilize multiple CUDA streams. A CUDA stream is a sequence of operations that execute on a GPU device in the order in which they are issued by the host code. Different CUDA streams can be interleaved and even run concurrently, thus achieving the effect of kernel overlap.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "To help understand the above concepts, Figure 1 provides a timeline of the GPU kernels in a sample distributed training job on 8 GPUs for one iteration. In the figure below, each rank represents one GPU and the kernels on each GPU run on 6 CUDA streams. In the right column of the figure, you can see names of the GPU kernels used. In the middle of the figure, you see the overlap between compute and communicate kernels. This figure is created using the [plot_timeline example notebook](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/examples/plot_timeline.ipynb) available in HTA.\n\n![Figure 1. An example of the execution timeline of GPU Kernels across multiple ranks](/assets/images/trace-image6.png){:width=\"100%\"}\n\n*Figure 1. An example of the execution timeline of GPU Kernels across multiple ranks*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "The performance of multiple GPU training jobs is affected by multiple factors. Among these factors, how does a model execution create and orchestrate the GPU kernels plays a critical role. HTA provides insights on how the model execution interacts with the GPU devices and highlights the opportunities for performance improvement.\n\nWith the features we built in HTA, we aim to provide users insights into \u201cwhat is happening under the hood in a distributed GPU training?\u201d We briefly describe these features in the next few paragraphs.\n\n## Features in Holistic Trace Analysis \n\nFor most users, understanding the performance of GPU training jobs is nontrivial. Thus, we built this library to simplify the task of trace analysis and provide the user useful insights by examining the model execution traces. As the first step, we developed features which are important and generic enough so that most users can benefit from this library.", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "**Temporal Breakdown**: We begin by asking whether the GPU is spending time on computation, communication, memory events, or is it idle? To answer this question, the temporal breakdown feature presents a breakdown in terms of these categories. To achieve high training efficiency the code should maximize time used by computation kernels and minimize idle time and non-compute time (time used by communication or memory kernels). This is accomplished by implementing concurrent execution of computation kernels with communication or memory kernels. *Note that, during concurrent execution of computation kernels with communication/memory kernels the time spent by communication/memory kernels is accounted for under compute time.*\n\n![Figure 2: Temporal Breakdown across 8 GPUs](/assets/images/trace-image3.png){:width=\"100%\"}\n\n*Figure 2: Temporal Breakdown across 8 GPUs*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "**Kernel Breakdown**: It is natural to ask which kernels are taking the most amount of time. The next feature breaks down the time spent within each kernel type (COMM, COMP, MEM) and sorts them by duration. We present this information for each kernel type and for each rank as a pie chart. See figure 3 below. \n\n![Figure 3: Pie chart of top computation and communication kernels](/assets/images/trace-image1.png){:width=\"100%\"}\n\n*Figure 3: Pie chart of top computation and communication kernels*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "**Kernel Duration Distribution**: Subsequently, one can also ask - for any given kernel, what is the distribution of the time spent across the ranks? To answer this, HTA generates bar graphs for the average duration of a given kernel across all ranks. Additionally, the error bars in the bar graphs show the minimum and maximum amount of time taken by a given kernel on a given rank. Figure 4 below shows a discrepancy between average duration on rank 0 as compared to other ranks. This anomalous behavior on rank 0 guides the user on where to look for possible bugs.\n\n![Figure 4: Average duration of NCCL AllReduce Kernel across 8 ranks](/assets/images/trace-image4.png){:width=\"100%\"}\n\n*Figure 4: Average duration of NCCL AllReduce Kernel across 8 ranks*\n{: style=\"text-align: center;\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "**Communication Computation Overlap**: In distributed training, a significant amount of time is spent in communication and synchronization events among multiple GPU devices. To achieve high GPU efficiency (i.e. TFLOPS/GPU) it is vital to keep the GPU doing actual computation work. In other words, a GPU should not be blocked because of waiting for data from other GPUs. One way to measure the extent to which computation is blocked by data dependencies is to calculate the computation-communication overlap. Higher GPU efficiency is observed if communication events overlap computation events. Lack of communication and computation overlap will lead to the GPU being idle, thus the efficiency would be low. Thus, the communication computation overlap feature calculates the percentage of time communication and computation overlap in a job for each rank and generates a bar graph representation. See figure below. More precisely, we measure the following ratio", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "(time spent in computation while communicating) / (time spent in communication)\n{: style=\"text-align: center;\"}\n\n\n![Figure 5: Communication computation overlap](/assets/images/trace-image5.png){:width=\"100%\"}\n\n*Figure 5: Communication computation overlap*\n{: style=\"text-align: center;\"}\n\n**Augmented Counters (Queue length, Memory bandwidth)**: To aid in debugging, HTA calculates the memory bandwidth statistics for D2H, H2D and D2D memory copy (memcpy) and memory set (memset) events. Additionally, HTA also computes the number of outstanding CUDA operations on each CUDA stream. We refer to this as queue length. When the queue length on a stream is 1024 or larger new events cannot be scheduled on that stream and the CPU will stall until the GPU events have processed. Additionally, HTA generates a new trace file containing tracks with the memory bandwidth and queue length time series. See Figure 6 below.\n\n![Figure 6: Memory Bandwidth and Queue Length](/assets/images/trace-image2.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "*Figure 6: Memory Bandwidth and Queue Length*\n{: style=\"text-align: center;\"}\n\nThese primary features give us a peek into the system performance and help answer \u201cwhat is happening in the system?\u201d. As HTA evolves, we hope to address \u201cwhy is X happening?\u201d and also suggest possible solutions to overcome the bottlenecks.\n\n## Installation and Usage\n\n### Installation\n\nFor installing the HTA please refer to the [README](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/README.md). In brief, the user is required to clone the [repo](https://github.com/facebookresearch/HolisticTraceAnalysis) and install the necessary Python packages via pip.\n\n### Usage", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "### Usage\n\nThis version of Holistic Trace Analysis is currently in beta and we recommend using HTA in a Jupyter notebook. A [demo notebook](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/examples/trace_analysis_demo.ipynb) is provided for your convenience. To get started, import the hta package in a Jupyter notebook, create a TraceAnalysis object and off we go in exactly two lines of code.\n\n```python\nfrom hta.trace_analysis import TraceAnalysis\nanalyzer = TraceAnalysis(trace_dir = \u201c/trace/folder/path\u201d)\n```\n\n### Requirements\n\n- All trace files for a training or inference job must be stored in a unique folder.\n- Trace files are in json or gzipped json format.\n\n## FAQ\n\n#### Q. How can I install HTA?\n\nPlease see the [README](https://github.com/facebookresearch/HolisticTraceAnalysis/blob/main/README.md) in the root directory of the repository.\n\n#### Q. Is there any documentation on the features and API in HTA?", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "The documentation and detailed API is available [here](https://hta.readthedocs.io/).\n\n#### Q. Can you implement feature X?\n\nDepending on how widely the feature is needed and the level of effort required to implement it we would consider developing the feature. Please open a [Github Issue](https://github.com/facebookresearch/HolisticTraceAnalysis/issues) and tag it with the feature-request label.\n\n#### Q. Can I modify the code?\n\nPlease do and [send a PR](https://github.com/facebookresearch/HolisticTraceAnalysis/pulls) along the way, if you think it would be useful for others.\n\n#### Q. How can I collect traces in PyTorch?\n\nPlease refer to this tutorial [here](https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html#use-profiler-to-record-execution-events).\n\n#### Q. Can HTA be used at production scale?\n\nYes, please see a use case study [here](https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/).", "metadata": {"source": "https://pytorch.org/blog/trace-analysis-for-masses/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch adds new dev tools as it hits production scale'\nauthor: The PyTorch Team\n---\n\n_This is a partial re-post of the original blog post on the Facebook AI Blog. The full post can be [viewed here](https://ai.facebook.com/blog/pytorch-adds-new-dev-tools-as-it-hits-production-scale/)_\n\nSince its release just a few months ago, [PyTorch 1.0](http://pytorch.org/) has been rapidly adopted as a powerful, flexible deep learning platform that enables engineers and researchers to move quickly from research to production. We are highlighting some of the ways the AI engineering and research community is using PyTorch 1.0. We\u2019re also sharing new details about the latest release, PyTorch 1.1, and showcasing some of the new development tools created by the community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "Building on the initial launch of PyTorch in 2017, we partnered with the AI community to ship the stable release of PyTorch 1.0 [last December](https://code.fb.com/ai-research/pytorch-developer-ecosystem-expands-1-0-stable-release/). Along with enhanced production-oriented capabilities and deep integration with leading cloud platforms, PyTorch 1.0 expands on the open source library\u2019s core features, with the addition of PyTorch JIT (Just in time compilation) that seamlessly transitions between eager mode and graph mode to provide both flexibility and speed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "Leading businesses across industries are beginning to use PyTorch to both facilitate their research and then also deploy at large scale for applications such as translation, computer vision, conversational interfaces, pharmaceutical research, factory optimization, and automated driving research. Community adoption of PyTorch has also continued to expand. Stanford, UC Berkeley, Caltech, and other universities are using PyTorch as a fundamental tool for their machine learning (ML) courses; new ecosystem projects have launched to support development on PyTorch; and major cloud platforms have expanded their integration with PyTorch.\n\n## Using PyTorch across industries\n\nMany leading businesses are moving to PyTorch 1.0 to accelerate development and deployment of new AI systems. Here are some examples:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "- Airbnb leveraged PyTorch's rich libraries and APIs for conversational AI and deployed a Smart Reply to help the company\u2019s service agents respond more effectively to customers.\n- [ATOM](https://atomscience.org/) is building a platform to generate and optimize new drug candidates significantly faster and with greater success than conventional processes. Using machine learning frameworks such as PyTorch, ATOM was able to design a variational autoencoder for representing diverse chemical structures and designing new drug candidates.\n- Genentech is utilizing PyTorch\u2019s flexible control structures and dynamic graphs to train deep learning models that will aid in the development of individualized cancer therapy.\n- Microsoft is using PyTorch across its organization to develop ML models at scale and deploy them via the ONNX Runtime. Using PyTorch, Microsoft Cognition has built distributed language models that scale to billions of words and are now in production in offerings such as Cognitive Services.\n- Toyota Research Institute (TRI) is developing a two-pronged approach toward automated driving with Toyota Guardian and Toyota Chauffeur technologies. The Machine Learning Team at TRI is creating new deep learning algorithms to leverage Toyota's 10 million sales per year data advantage. The flexibility of PyTorch has vastly accelerated their pace of exploration and its new production features will enable faster deployment towards their safety critical applications.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} {"page_content": "Following the release of PyTorch 1.0 in December 2018, we\u2019re now announcing the availability of v1.1, which improves performance, adds new model understanding and visualization tools to improve usability, and provides new APIs.\n\nKey features of PyTorch v1.1 include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [TensorBoard](https://www.tensorflow.org/tensorboard): First-class and native support for visualization and model debugging with TensorBoard, a web application suite for inspecting and understanding training runs and graphs. PyTorch now natively supports TensorBoard with a simple \u201cfrom torch.utils.tensorboard import SummaryWriter\u201d command.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- JIT compiler: Improvements to just-in-time (JIT) compilation. These include various bug fixes as well as expanded capabilities in TorchScript, such as support for dictionaries, user classes, and attributes.\n- New APIs: Support for Boolean tensors and better support for custom recurrent neural networks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- Distributed Training: Improved performance for common models such as CNNs, added support for multi device modules including the ability to split models across GPUs while still using Distributed Data Parallel (DDP) and support for modules where not all parameters are used in every iteration (e.g. control flow, like adaptive softmax, etc). See the latest tutorials [here](https://pytorch.org/tutorials/intermediate/model_parallel_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "We\u2019ve also continued to partner with the community to foster projects and tools aimed at supporting ML engineers for needs ranging from improved model understanding to auto-tuning using AutoML methods. With the release of Ax and BoTorch (below), we will be sharing some of our core algorithms, including meta-learning for efficiently optimizing hyperparameters from based on historical tasks. We are excited to see this work open-sourced for the community to build on.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "This ecosystem includes open source projects and tools that have been deployed at production scale, as well as products and services from our partnership with industry leaders who share our vision of an open and collaborative AI community. Here are a few of the latest tools:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [BoTorch](https://ai.facebook.com/blog/open-sourcing-ax-and-botorch-new-ai-tools-for-adaptive-experimentation/): BoTorch is a research framework built on top of PyTorch to provide Bayesian optimization, a sample-efficient technique for sequential optimization of costly-to-evaluate black-box functions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [Ax](https://ai.facebook.com/blog/open-sourcing-ax-and-botorch-new-ai-tools-for-adaptive-experimentation/): Ax is an ML platform for managing adaptive experiments. It enables researchers and engineers to systematically explore large configuration spaces in order to optimize machine learning models, infrastructure, and products.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [PyTorch-BigGraph](https://ai.facebook.com/blog/open-sourcing-pytorch-biggraph-for-faster-embeddings-of-extremely-large-graphs/): PBG is a distributed system for creating embeddings of very large graphs with billions of entities and trillions of edges. It includes support for sharding and negative sampling and it offers sample use cases based on Wikidata embeddings.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [Google AI Platform Notebooks](https://cloud.google.com/ai-platform-notebooks/): AI Platform Notebooks is a new, hosted JupyterLab service from Google Cloud Platform. Data scientists can quickly create virtual machines running JupyterLab with the latest version of PyTorch preinstalled. It is also tightly integrated with GCP services such as BigQuery, Cloud Dataproc, Cloud Dataflow, and AI Factory, making it easy to execute the full ML cycle without ever leaving JupyterLab.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re also excited to see many interesting new projects from the broader PyTorch community. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [BigGAN-PyTorch](https://github.com/ajbrock/BigGAN-PyTorch):This is a full PyTorch reimplementation that uses gradient accumulation to provide the benefits of big batches on as few as four GPUs.\n- [GeomLoss](http://www.kernel-operations.io/geomloss/index.html): A Python API that defines PyTorch layers for geometric loss functions between sampled measures, images, and volumes. It includes MMD, Wasserstein, Sinkhorn, and more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [PyTorch Geometric](https://github.com/rusty1s/pytorch_geometric): A deep learning extension library for PyTorch that offers several methods for deep learning on graphs and other irregular structures (also known as [geometric deep learning](http://geometricdeeplearning.com)) from a variety of published papers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "- [Curve-GCN](https://github.com/fidler-lab/curve-gcn): A real-time, interactive image annotation approach that uses an end-to-end-trained graph convolutional network (GCN). It supports object annotation by either polygons or splines, facilitating labeling efficiency for both line-based and curved objects. Curve-GCN runs 10x faster than traditional methods, such as Polygon-RNN++.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Udacity, fast.ai, and others develop new PyTorch resources\n\nPyTorch is ideal for teaching ML development because it enables rapid experimentation through its flexible, dynamic programming environment and user-friendly Pythonic interface. In addition, Google Colab now offers an interactive Jupyter Notebook environment that natively supports PyTorch, allowing developers to run any PyTorch tutorial immediately with free CPU and GPU resources.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "University-level classes \u2014 including [Stanford NLP](http://web.stanford.edu/class/cs224n), [UC Berkeley](https://inst.eecs.berkeley.edu/~cs280/sp18/) Computer Vision, and [Caltech](http://cast.caltech.edu) Robotics courses \u2014 are now being taught on PyTorch. In addition, massive open online courses (MOOCs) are training thousands of new PyTorch developers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Today, we\u2019re announcing a [new Udacity course](https://blog.udacity.com/2019/05/announcing-the-secure-and-private-ai-scholarship-challenge-with-facebook.html), building upon the Intro to Deep Learning course launched last year. This new course, led by Andrew Trask of Oxford University and OpenMined, covers important concepts around privacy in AI, including methods such as differential privacy and federated learning. Facebook will also be providing scholarships to support students as they continue their ML", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "education in Udacity\u2019s full Nanodegree programs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "The [fast.ai](https://www.fast.ai) community is also continuing to invest energy and resources in PyTorch. In June, fast.ai will launch a new course called Deep Learning from the Foundations, which will show developers how to go all the way from writing matrix multiplication from scratch to how to train and implement a state-of-the-art ImageNet model. The course will include deep dives into the underlying implementation of methods in the PyTorch and fast.ai libraries, and will use the code to explain and", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "illustrate the academic papers that underlie these methods.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "As part of the course, fast.ai will also release new software modules, including fastai.audio, which brings the power of fast.ai\u2019s deep abstractions and curated algorithms to the new PyTorch.audio module, and show how fastai.vision can be used to [create stunning high-resolution videos](https://www.fast.ai/2019/05/03/decrappify) from material such as old classic movies, and from cutting-edge microscopy sequences through a collaboration with the [Salk Institute](https://www.salk.edu). In addition, fast.ai is", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "contributing its new X-ResNet module, including a suite of models pretrained on ImageNet.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "Getting started with PyTorch\n\nEveryone in the AI community \u2014 including those new to ML development as well as researchers and engineers looking for ways to accelerate their end-to-end workflows \u2014 can experiment with PyTorch instantly by visiting [pytorch.org](https://pytorch.org) and launching a [tutorial](https://pytorch.org/tutorials) in Colab. There are also many easy ways to [get started](https://pytorch.org/get-started/locally) both locally and on popular cloud platforms.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing Accelerated PyTorch Training on Mac\"\nauthor: PyTorch\nfeatured-img: \"/assets/images/METAPT-002-BarGraph-02-static.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "In collaboration with the Metal engineering team at Apple, we are excited to announce support for GPU-accelerated PyTorch training on Mac. Until now, PyTorch training on Mac only leveraged the CPU, but with the upcoming PyTorch v1.12 release, developers and researchers can take advantage of Apple silicon GPUs for significantly faster model training. This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac.", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "Metal Acceleration\n\nAccelerated GPU training is enabled using Apple\u2019s Metal Performance Shaders (MPS) as a backend for PyTorch. The MPS backend extends the PyTorch framework, providing scripts and capabilities to set up and run operations on Mac. MPS optimizes compute performance with kernels that are fine-tuned for the unique characteristics of each Metal GPU family. The new device maps machine learning computational graphs and primitives on the MPS Graph framework and tuned kernels provided by MPS.", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "Training Benefits on Apple Silicon\n\nEvery Apple silicon Mac has a unified memory architecture, providing the GPU with direct access to the full memory store. This makes Mac a great platform for machine learning, enabling users to train larger networks or batch sizes locally. This reduces costs associated with cloud-based development or the need for additional local GPUs. The Unified Memory architecture also reduces data retrieval latency, improving end-to-end performance.", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "In the graphs below, you can see the performance speedup from accelerated GPU training and evaluation compared to the CPU baseline:\n\n

\n \n

\n\n

\nAccelerated GPU training and evaluation speedups over CPU-only (times faster)\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "Getting Started\n\nTo get started, just install the latest [Preview (Nightly) build](https://pytorch.org/get-started/locally/) on your Apple silicon Mac running macOS 12.3 or later with a native version (arm64) of Python.\n \nYou can also learn more about Metal and MPS on [Apple\u2019s Metal page](https://developer.apple.com/metal/).", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} +{"page_content": "- [TensorBoard](https://www.tensorflow.org/tensorboard): First-class and native support for visualization and model debugging with TensorBoard, a web application suite for inspecting and understanding training runs and graphs. PyTorch now natively supports TensorBoard with a simple \u201cfrom torch.utils.tensorboard import SummaryWriter\u201d command.\n- JIT compiler: Improvements to just-in-time (JIT) compilation. These include various bug fixes as well as expanded capabilities in TorchScript, such as support for dictionaries, user classes, and attributes.\n- New APIs: Support for Boolean tensors and better support for custom recurrent neural networks.\n- Distributed Training: Improved performance for common models such as CNNs, added support for multi device modules including the ability to split models across GPUs while still using Distributed Data Parallel (DDP) and support for modules where not all parameters are used in every iteration (e.g. control flow, like adaptive softmax, etc). See the latest tutorials [here](https://pytorch.org/tutorials/intermediate/model_parallel_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "We\u2019ve also continued to partner with the community to foster projects and tools aimed at supporting ML engineers for needs ranging from improved model understanding to auto-tuning using AutoML methods. With the release of Ax and BoTorch (below), we will be sharing some of our core algorithms, including meta-learning for efficiently optimizing hyperparameters from based on historical tasks. We are excited to see this work open-sourced for the community to build on.\n\nThis ecosystem includes open source projects and tools that have been deployed at production scale, as well as products and services from our partnership with industry leaders who share our vision of an open and collaborative AI community. Here are a few of the latest tools:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "- [BoTorch](https://ai.facebook.com/blog/open-sourcing-ax-and-botorch-new-ai-tools-for-adaptive-experimentation/): BoTorch is a research framework built on top of PyTorch to provide Bayesian optimization, a sample-efficient technique for sequential optimization of costly-to-evaluate black-box functions.\n- [Ax](https://ai.facebook.com/blog/open-sourcing-ax-and-botorch-new-ai-tools-for-adaptive-experimentation/): Ax is an ML platform for managing adaptive experiments. It enables researchers and engineers to systematically explore large configuration spaces in order to optimize machine learning models, infrastructure, and products.\n- [PyTorch-BigGraph](https://ai.facebook.com/blog/open-sourcing-pytorch-biggraph-for-faster-embeddings-of-extremely-large-graphs/): PBG is a distributed system for creating embeddings of very large graphs with billions of entities and trillions of edges. It includes support for sharding and negative sampling and it offers sample use cases based on Wikidata embeddings.\n- [Google AI Platform Notebooks](https://cloud.google.com/ai-platform-notebooks/): AI Platform Notebooks is a new, hosted JupyterLab service from Google Cloud Platform. Data scientists can quickly create virtual machines running JupyterLab with the latest version of PyTorch preinstalled. It is also tightly integrated with GCP services such as BigQuery, Cloud Dataproc, Cloud Dataflow, and AI Factory, making it easy to execute the full ML cycle without ever leaving JupyterLab.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "We\u2019re also excited to see many interesting new projects from the broader PyTorch community. Highlights include:\n\n- [BigGAN-PyTorch](https://github.com/ajbrock/BigGAN-PyTorch):This is a full PyTorch reimplementation that uses gradient accumulation to provide the benefits of big batches on as few as four GPUs.\n- [GeomLoss](http://www.kernel-operations.io/geomloss/index.html): A Python API that defines PyTorch layers for geometric loss functions between sampled measures, images, and volumes. It includes MMD, Wasserstein, Sinkhorn, and more.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "- [PyTorch Geometric](https://github.com/rusty1s/pytorch_geometric): A deep learning extension library for PyTorch that offers several methods for deep learning on graphs and other irregular structures (also known as [geometric deep learning](http://geometricdeeplearning.com)) from a variety of published papers.\n- [Curve-GCN](https://github.com/fidler-lab/curve-gcn): A real-time, interactive image annotation approach that uses an end-to-end-trained graph convolutional network (GCN). It supports object annotation by either polygons or splines, facilitating labeling efficiency for both line-based and curved objects. Curve-GCN runs 10x faster than traditional methods, such as Polygon-RNN++.\n\n## Udacity, fast.ai, and others develop new PyTorch resources", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "PyTorch is ideal for teaching ML development because it enables rapid experimentation through its flexible, dynamic programming environment and user-friendly Pythonic interface. In addition, Google Colab now offers an interactive Jupyter Notebook environment that natively supports PyTorch, allowing developers to run any PyTorch tutorial immediately with free CPU and GPU resources.\n\nUniversity-level classes \u2014 including [Stanford NLP](http://web.stanford.edu/class/cs224n), [UC Berkeley](https://inst.eecs.berkeley.edu/~cs280/sp18/) Computer Vision, and [Caltech](http://cast.caltech.edu) Robotics courses \u2014 are now being taught on PyTorch. In addition, massive open online courses (MOOCs) are training thousands of new PyTorch developers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "Today, we\u2019re announcing a [new Udacity course](https://blog.udacity.com/2019/05/announcing-the-secure-and-private-ai-scholarship-challenge-with-facebook.html), building upon the Intro to Deep Learning course launched last year. This new course, led by Andrew Trask of Oxford University and OpenMined, covers important concepts around privacy in AI, including methods such as differential privacy and federated learning. Facebook will also be providing scholarships to support students as they continue their ML education in Udacity\u2019s full Nanodegree programs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "The [fast.ai](https://www.fast.ai) community is also continuing to invest energy and resources in PyTorch. In June, fast.ai will launch a new course called Deep Learning from the Foundations, which will show developers how to go all the way from writing matrix multiplication from scratch to how to train and implement a state-of-the-art ImageNet model. The course will include deep dives into the underlying implementation of methods in the PyTorch and fast.ai libraries, and will use the code to explain and illustrate the academic papers that underlie these methods.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "As part of the course, fast.ai will also release new software modules, including fastai.audio, which brings the power of fast.ai\u2019s deep abstractions and curated algorithms to the new PyTorch.audio module, and show how fastai.vision can be used to [create stunning high-resolution videos](https://www.fast.ai/2019/05/03/decrappify) from material such as old classic movies, and from cutting-edge microscopy sequences through a collaboration with the [Salk Institute](https://www.salk.edu). In addition, fast.ai is contributing its new X-ResNet module, including a suite of models pretrained on ImageNet.\n\n## Getting started with PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "Everyone in the AI community \u2014 including those new to ML development as well as researchers and engineers looking for ways to accelerate their end-to-end workflows \u2014 can experiment with PyTorch instantly by visiting [pytorch.org](https://pytorch.org) and launching a [tutorial](https://pytorch.org/tutorials) in Colab. There are also many easy ways to [get started](https://pytorch.org/get-started/locally) both locally and on popular cloud platforms.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-dev-tools/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing Accelerated PyTorch Training on Mac\"\nauthor: PyTorch\nfeatured-img: \"/assets/images/METAPT-002-BarGraph-02-static.png\"\n---\n\nIn collaboration with the Metal engineering team at Apple, we are excited to announce support for GPU-accelerated PyTorch training on Mac. Until now, PyTorch training on Mac only leveraged the CPU, but with the upcoming PyTorch v1.12 release, developers and researchers can take advantage of Apple silicon GPUs for significantly faster model training. This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac.\n\n

\n \n

\n\n## Metal Acceleration", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} +{"page_content": "Accelerated GPU training is enabled using Apple\u2019s Metal Performance Shaders (MPS) as a backend for PyTorch. The MPS backend extends the PyTorch framework, providing scripts and capabilities to set up and run operations on Mac. MPS optimizes compute performance with kernels that are fine-tuned for the unique characteristics of each Metal GPU family. The new device maps machine learning computational graphs and primitives on the MPS Graph framework and tuned kernels provided by MPS. \n\n## Training Benefits on Apple Silicon\n\nEvery Apple silicon Mac has a unified memory architecture, providing the GPU with direct access to the full memory store. This makes Mac a great platform for machine learning, enabling users to train larger networks or batch sizes locally. This reduces costs associated with cloud-based development or the need for additional local GPUs. The Unified Memory architecture also reduces data retrieval latency, improving end-to-end performance.", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} +{"page_content": "In the graphs below, you can see the performance speedup from accelerated GPU training and evaluation compared to the CPU baseline:\n\n

\n \n

\n\n

\nAccelerated GPU training and evaluation speedups over CPU-only (times faster)\n

\n\n\n## Getting Started\n\nTo get started, just install the latest [Preview (Nightly) build](https://pytorch.org/get-started/locally/) on your Apple silicon Mac running macOS 12.3 or later with a native version (arm64) of Python.\n \nYou can also learn more about Metal and MPS on [Apple\u2019s Metal page](https://developer.apple.com/metal/).", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} {"page_content": "\\* _Testing conducted by Apple in April 2022 using production Mac Studio systems with Apple M1 Ultra, 20-core CPU, 64-core GPU 128GB of RAM, and 2TB SSD. Tested with macOS Monterey 12.3, prerelease PyTorch 1.12, ResNet50 (batch size=128), HuggingFace BERT (batch size=64), and VGG16 (batch size=64). Performance tests are conducted using specific computer systems and reflect the approximate performance of Mac Studio._", "metadata": {"source": "https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerating Hugging Face and TIMM models with PyTorch 2.0\"\nauthor: Mark Saroufim\nfeatured-img: \"assets/images/pytorch-2.0-feature-img.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "`torch.compile()` makes it easy to experiment with different compiler backends to make PyTorch code faster with a single line decorator `torch.compile()`. It works either directly over an nn.Module as a drop-in replacement for `torch.jit.script()` but without requiring you to make any source code changes. We expect this one line code change to provide you with between 30%-2x training time speedups on the vast majority of models that you\u2019re already running.\n\n```python\n\nopt_module = torch.compile(module)", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "torch.compile supports arbitrary PyTorch code, control flow, mutation and comes with experimental support for dynamic shapes. We\u2019re so excited about this development that we call it PyTorch 2.0.\n\nWhat makes this announcement different for us is we\u2019ve already benchmarked some of the most popular open source PyTorch models and gotten substantial speedups ranging from 30% to 2x [https://github.com/pytorch/torchdynamo/issues/681](https://github.com/pytorch/torchdynamo/issues/681).", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "There are no tricks here, we\u2019ve pip installed popular libraries like [https://github.com/huggingface/transformers](https://github.com/huggingface/transformers), [https://github.com/huggingface/accelerate](https://github.com/huggingface/accelerate) and [https://github.com/rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) and then ran torch.compile() on them and that\u2019s it.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "It\u2019s rare to get both performance and convenience, but this is why the core team finds PyTorch 2.0 so exciting. The Hugging Face team is also excited, in their words:\n\nRoss Wightman the primary maintainer of TIMM: \u201cPT 2.0 works out of the box with majority of timm models for inference and train workloads and no code changes\u201d", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Sylvain Gugger the primary maintainer of transformers and accelerate: \"With just one line of code to add, PyTorch 2.0 gives a speedup between 1.5x and 2.x in training Transformers models. This is the most exciting thing since mixed precision training was introduced!\"\n\nThis tutorial will show you exactly how to replicate those speedups so you can be as excited as to PyTorch 2.0 as we are.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Requirements and Setup\n\nFor GPU (newer generation GPUs will see drastically better performance)\n\n```\npip3 install numpy --pre torch --force-reinstall --extra-index-url https://download.pytorch.org/whl/nightly/cu117\n\n```\n\nFor CPU\n\n```\npip3 install --pre torch --extra-index-url https://download.pytorch.org/whl/nightly/cpu\n\n```\n\nOptional: Verify Installation\n\n```\ngit clone https://github.com/pytorch/pytorch\ncd tools/dynamo\npython verify_dynamo.py", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Optional: Docker installation\n\nWe also provide all the required dependencies in the PyTorch nightly\nbinaries which you can download with\n\n```\ndocker pull ghcr.io/pytorch/pytorch-nightly\n\n```\n\nAnd for ad hoc experiments just make sure that your container has access\nto all your GPUs\n\n```\ndocker run --gpus all -it ghcr.io/pytorch/pytorch-nightly:latest /bin/bash\n\n```", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Getting started", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "a toy exmaple\n\nLet\u2019s start with a simple example and make things more complicated step\nby step. Please note that you\u2019re likely to see more significant speedups the newer your GPU is.\n\n```python\nimport torch\ndef fn(x, y):\n a = torch.sin(x).cuda()\n b = torch.sin(y).cuda()\n return a + b\nnew_fn = torch.compile(fn, backend=\"inductor\")\ninput_tensor = torch.randn(10000).to(device=\"cuda:0\")\na = new_fn(input_tensor, input_tensor)", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "This example won\u2019t actually run faster but it\u2019s educational.\n\nexample that features `torch.cos()` and `torch.sin()` which are examples of pointwise ops as in they operate element by element on a vector. A more famous pointwise op you might actually want to use would be something like `torch.relu()`.\n\nPointwise ops in eager mode are suboptimal because each one would need to read a tensor from memory, make some changes and then write back those changes.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "The single most important optimization that PyTorch 2.0 does for you is fusion.\n\nSo back to our example we can turn 2 reads and 2 writes into 1 read and 1 write which is crucial especially for newer GPUs where the bottleneck is memory bandwidth (how quickly you can send data to a GPU) instead of compute (how quickly your GPU can crunch floating point operations)\n\nThe second most important optimization that PyTorch 2.0 does for you is CUDA graphs", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs help eliminate the overhead from launching individual kernels from a python program.\n\ntorch.compile() supports many different backends but one that we\u2019re particularly excited about is Inductor which generates Triton kernels [https://github.com/openai/triton](https://github.com/openai/triton) which are written in Python yet outperform the vast majority of handwritten CUDA kernels. Suppose our example above was called trig.py we can actually inspect the code generated triton kernels by running.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "```\nTORCH_COMPILE_DEBUG=1 python trig.py", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "```python", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "@pointwise(size_hints=[16384], filename=__file__, meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': 0, 'constants': {}, 'configs': [instance_descriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]})\n@triton.jit\ndef kernel(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):\n xnumel = 10000\n xoffset = tl.program_id(0) * XBLOCK\n xindex = xoffset + tl.reshape(tl.arange(0, XBLOCK), [XBLOCK])\n xmask = xindex < xnumel\n x0 = xindex\n tmp0 = tl.load(in_ptr0 + (x0), xmask)", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "tmp1 = tl.sin(tmp0)\n tmp2 = tl.sin(tmp1)\n tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp2, xmask)", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "And you can verify that fusing the two `sins` did actually occur because the two `sin` operations occur within a single Triton kernel and the temporary variables are held in registers with very fast access.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "a real model\n\nAs a next step let\u2019s try a real model like resnet50 from the PyTorch hub.\n\n```python\nimport torch\nmodel = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)\nopt_model = torch.compile(model, backend=\"inductor\")\nmodel(torch.randn(1,3,64,64))", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "If you actually run you may be surprised that the first run is slow and that\u2019s because the model is being compiled. Subsequent runs will be faster so it's common practice to warm up your model before you start benchmarking it.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "You may have noticed how we also passed in the name of a compiler explicitly here with \u201cinductor\u201d but it\u2019s not the only available backend, you can run in a REPL `torch._dynamo.list_backends()` to see the full list of available backends. For fun you should try out `aot_cudagraphs` or `nvfuser`.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Hugging Face models\n\nLet\u2019s do something a bit more interesting now, our community frequently\nuses pretrained models from transformers [https://github.com/huggingface/transformers](https://github.com/huggingface/transformers) or TIMM [https://github.com/rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) and one of our design goals for PyTorch 2.0 was that any new compiler stack needs to work out of the box with the vast majority of models people actually run.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "So we\u2019re going to directly download a pretrained model from the Hugging Face hub and optimize it\n\n```python", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "import torch\nfrom transformers import BertTokenizer, BertModel\n# Copy pasted from here https://huggingface.co/bert-base-uncased\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\nmodel = BertModel.from_pretrained(\"bert-base-uncased\").to(device=\"cuda:0\")\nmodel = torch.compile(model) # This is the only line of code that we changed\ntext = \"Replace me by any text you'd like.\"\nencoded_input = tokenizer(text, return_tensors='pt').to(device=\"cuda:0\")\noutput = model(**encoded_input)", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "If you remove the `to(device=\"cuda:0\")` from the model and `encoded_input` then PyTorch 2.0 will generate C++ kernels that will be optimized for running on your CPU. You can inspect both Triton or C++ kernels for BERT, they\u2019re obviously more complex than the trigonometry example we had above but you can similarly skim it and understand if you understand PyTorch.\n\nThe same code also works just fine if used with [https://github.com/huggingface/accelerate](https://github.com/huggingface/accelerate) and DDP", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Similarly let\u2019s try out a TIMM example\n\n```python\nimport timm\nimport torch\nmodel = timm.create_model('resnext101_32x8d', pretrained=True, num_classes=2)\nopt_model = torch.compile(model, backend=\"inductor\")\nopt_model(torch.randn(64,3,7,7))", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "Our goal with PyTorch was to build a breadth-first compiler that would speed up the vast majority of actual models people run in open source. The Hugging Face Hub ended up being an extremely valuable benchmarking tool for us, ensuring that any optimization we work on actually helps accelerate models people want to run.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "So please try out PyTorch 2.0, enjoy the free perf and if you\u2019re not seeing it then please open an issue and we will make sure your model is supported [https://github.com/pytorch/torchdynamo/issues](https://github.com/pytorch/torchdynamo/issues)\n\nAfter all, we can\u2019t claim we\u2019re created a breadth-first unless YOUR models actually run faster.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.6 now includes Stochastic Weight Averaging'\nauthor: Pavel Izmailov, Andrew Gordon Wilson and Vincent Queneneville-Belair\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Do you use stochastic gradient descent (SGD) or Adam? Regardless of the procedure you use to train your neural network, you can likely achieve significantly better generalization at virtually no additional cost with a simple new technique now natively supported in PyTorch 1.6, Stochastic Weight Averaging (SWA) [1]. Even if you have already trained your model, it\u2019s easy to realize the benefits of SWA by running SWA for a small number of epochs starting with a pre-trained model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[Again](https://twitter.com/MilesCranmer/status/1282140440892932096) and [again](https://twitter.com/leopd/status/1285969855062192129), researchers are discovering that SWA improves the performance of well-tuned models in a wide array of practical applications with little cost or effort!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "SWA has a wide range of applications and features:\n* SWA significantly improves performance compared to standard training techniques in computer vision (e.g., VGG, ResNets, Wide ResNets and DenseNets on ImageNet and CIFAR benchmarks [1, 2]).\n* SWA provides state-of-the-art performance on key benchmarks in semi-supervised learning and domain adaptation [2].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "* SWA was shown to improve performance in language modeling (e.g., AWD-LSTM on WikiText-2 [4]) and policy-gradient methods in deep reinforcement learning [3].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "* SWAG, an extension of SWA, can approximate Bayesian model averaging in Bayesian deep learning and achieves state-of-the-art uncertainty calibration results in various settings. Moreover, its recent generalization MultiSWAG provides significant additional performance gains and mitigates double-descent [4, 10]. Another approach, Subspace Inference, approximates the Bayesian posterior in a small subspace of the parameter space around the SWA solution [5].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "* SWA for low precision training, SWALP, can match the performance of full-precision SGD training, even with all numbers quantized down to 8 bits, including gradient accumulators [6].\n* SWA in parallel, SWAP, was shown to greatly speed up the training of neural networks by using large batch sizes and, in particular, set a record by training a neural network to 94% accuracy on CIFAR-10 in 27 seconds [11].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "**Figure 1**. *Illustrations of SWA and SGD with a Preactivation ResNet-164 on CIFAR-100 [1]. **Left**: test error surface for three FGE samples and the corresponding SWA solution (averaging in weight space). **Middle** and **Right**: test error and train loss surfaces showing the weights proposed by SGD (at convergence) and SWA, starting from the same initialization of SGD after 125 training epochs. Please see [1] for details on how these figures were constructed*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "In short, SWA performs an equal average of the weights traversed by SGD (or any stochastic optimizer) with a modified learning rate schedule (see the left panel of Figure 1.). SWA solutions end up in the center of a wide flat region of loss, while SGD tends to converge to the boundary of the low-loss region, making it susceptible to the shift between train and test error surfaces (see the middle and right panels of Figure 1). We emphasize that SWA **can be used with any optimizer, such as Adam, and is not", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "specific to SGD**.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Previously, SWA was in PyTorch contrib. In PyTorch 1.6, we provide a new convenient implementation of SWA in [torch.optim.swa_utils](https://pytorch.org/docs/stable/optim.html#stochastic-weight-averaging).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Is this just Averaged SGD?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "At a high level, averaging SGD iterates dates back several decades in convex optimization [7, 8], where it is sometimes referred to as Polyak-Ruppert averaging, or averaged SGD. **But the details matter**. Averaged SGD is often used in conjunction with a decaying learning rate, and an exponential moving average (EMA), typically for convex optimization. In convex optimization, the focus has been on improved rates of convergence. In deep learning, this form of averaged SGD smooths the trajectory of SGD", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "iterates but does not perform very differently.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "By contrast, SWA uses an **equal average** of SGD iterates with a modified **cyclical or high constant learning rate** and exploits the flatness of training objectives [8] specific to **deep learning** for **improved generalization**.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "How does Stochastic Weight Averaging Work?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "There are two important ingredients that make SWA work. First, SWA uses a **modified learning rate** schedule so that SGD (or other optimizers such as Adam) continues to bounce around the optimum and explore diverse models instead of simply converging to a single solution. For example, we can use the standard decaying learning rate strategy for the first 75% of training time and then set the learning rate to a reasonably high constant value for the remaining 25% of the time (see Figure 2 below). The second", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "ingredient is to take an average of the weights **(typically an equal average)** of the networks traversed by SGD. For example, we can maintain a running average of the weights obtained at the end of every epoch within the last 25% of training time (see Figure 2). After training is complete, we then set the weights of the network to the computed SWA averages.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n**Figure 2**. *Illustration of the learning rate schedule adopted by SWA. Standard decaying schedule is used for the first 75% of the training and then a high constant value is used for the remaining 25%. The SWA averages are formed during the last 25% of training*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "One important detail is the batch normalization. Batch normalization layers compute running statistics of activations during training. Note that the SWA averages of the weights are never used to make predictions during training. So the batch normalization layers do not have the activation statistics computed at the end of training. We can compute these statistics by doing a single forward pass on the train data with the SWA model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "While we focus on SGD for simplicity in the description above, SWA can be combined with any optimizer. You can also use cyclical learning rates instead of a high constant value (see e.g., [2]).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "How to use SWA in PyTorch?\n\nIn `torch.optim.swa_utils` we implement all the SWA ingredients to make it convenient to use SWA with any model. In particular, we implement `AveragedModel` class for SWA models, `SWALR` learning rate scheduler, and `update_bn` utility function to update SWA batch normalization statistics at the end of training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "In the example below, `swa_model` is the SWA model that accumulates the averages of the weights. We train the model for a total of 300 epochs, and we switch to the SWA learning rate schedule and start to collect SWA averages of the parameters at epoch 160. \n\n```python\nfrom torch.optim.swa_utils import AveragedModel, SWALR\nfrom torch.optim.lr_scheduler import CosineAnnealingLR", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "loader, optimizer, model, loss_fn = ...\nswa_model = AveragedModel(model)\nscheduler = CosineAnnealingLR(optimizer, T_max=100)\nswa_start = 5\nswa_scheduler = SWALR(optimizer, swa_lr=0.05)\n\nfor epoch in range(100):\n for input, target in loader:\n optimizer.zero_grad()\n loss_fn(model(input), target).backward()\n optimizer.step()\n if epoch > swa_start:\n swa_model.update_parameters(model)\n swa_scheduler.step()\n else:\n scheduler.step()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "# Update bn statistics for the swa_model at the end\ntorch.optim.swa_utils.update_bn(loader, swa_model)\n# Use swa_model to make predictions on test data \npreds = swa_model(test_input)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Next, we explain each component of `torch.optim.swa_utils` in detail.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "`AveragedModel` class serves to compute the weights of the SWA model. You can create an averaged model by running `swa_model = AveragedModel(model)`. You can then update the parameters of the averaged model by `swa_model.update_parameters(model)`. By default, `AveragedModel` computes a running equal average of the parameters that you provide, but you can also use custom averaging functions with the `avg_fn` parameter. In the following example, `ema_model` computes an exponential moving average.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "```python\nema_avg = lambda averaged_model_parameter, model_parameter, num_averaged:\\\n0.1 * averaged_model_parameter + 0.9 * model_parameter\nema_model = torch.optim.swa_utils.AveragedModel(model, avg_fn=ema_avg)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "In practice, we find an equal average with the modified learning rate schedule in Figure 2 provides the best performance.\n\n`SWALR` is a learning rate scheduler that anneals the learning rate to a fixed value, and then keeps it constant. For example, the following code creates a scheduler that linearly anneals the learning rate from its initial value to `0.05` in `5` epochs within each parameter group.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "```python\nswa_scheduler = torch.optim.swa_utils.SWALR(optimizer, \nanneal_strategy=\"linear\", anneal_epochs=5, swa_lr=0.05)\n\n```\nWe also implement cosine annealing to a fixed value (`anneal_strategy=\"cos\"`). In practice, we typically switch to `SWALR` at epoch `swa_start` (e.g. after 75% of the training epochs), and simultaneously start to compute the running averages of the weights:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "```python\nscheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=100)\nswa_start = 75\nfor epoch in range(100):\n # \n if i > swa_start:\n swa_model.update_parameters(model)\n swa_scheduler.step()\n else:\n scheduler.step()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Finally, `update_bn` is a utility function that computes the batchnorm statistics for the SWA model on a given dataloader `loader`:\n```\ntorch.optim.swa_utils.update_bn(loader, swa_model) \n```\n`update_bn` applies the `swa_model` to every element in the dataloader and computes the activation statistics for each batch normalization layer in the model.\n\nOnce you computed the SWA averages and updated the batch normalization layers, you can apply `swa_model` to make predictions on test data.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Why does it work?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "There are large flat regions of the loss surface [9]. In Figure 3 below, we show a visualization of the loss surface in a subspace of the parameter space containing a path connecting two independently trained SGD solutions, such that the loss is similarly low at every point along the path. SGD converges near the boundary of these regions because there isn\u2019t much gradient signal to move inside, as the points in the region all have similarly low values of loss. By increasing the learning rate, SWA spins", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "around this flat region, and then by averaging the iterates, moves towards the center of the flat region.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n**Figure 3**: *visualization of mode connectivity for ResNet-20 with no skip connections on CIFAR-10 dataset. The visualization is created in collaboration with Javier Ideami [(https://losslandscape.com/)](https://losslandscape.com/). For more details, see this [blogpost](https://izmailovpavel.github.io/curves_blogpost/)*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "We expect solutions that are centered in the flat region of the loss to generalize better than those near the boundary. Indeed, train and test error surfaces are not perfectly aligned in the weight space. Solutions that are centered in the flat region are not as susceptible to the shifts between train and test error surfaces as those near the boundary. In Figure 4 below, we show the train loss and test error surfaces along the direction connecting the SWA and SGD solutions. As you can see, while the SWA", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "solution has a higher train loss compared to the SGD solution, it is centered in a region of low loss and has a substantially better test error.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n**Figure 4**. *Train loss and test error along the line connecting the SWA solution (circle) and SGD solution (square). The SWA solution is centered in a wide region of low train loss, while the SGD solution lies near the boundary. Because of the shift between train loss and test error surfaces, the SWA solution leads to much better generalization*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "What are the results achieved with SWA?\n\nWe release a GitHub [repo](https://github.com/izmailovpavel/torch_swa_examples) with examples using the PyTorch implementation of SWA for training DNNs. For example, these examples can be used to achieve the following results on CIFAR-100:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "{:.table.table-striped.table-bordered}\n | | VGG-16 | ResNet-164 | WideResNet-28x10 | \n| ------------- | ------------- | ------------- | ------------- |\n| SGD | 72.8 \u00b1 0.3 | 78.4 \u00b1 0.3 | 81.0 \u00b1 0.3 | \n| SWA | 74.4 \u00b1 0.3 | 79.8 \u00b1 0.4 | 82.5 \u00b1 0.2 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Semi-Supervised Learning", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "In a follow-up [paper](https://arxiv.org/abs/1806.05594) SWA was applied to semi-supervised learning, where it improved the best reported results in multiple settings [2]. For example, with SWA you can get 95% accuracy on CIFAR-10 if you only have the training labels for 4k training data points (the previous best reported result on this problem was 93.7%). This paper also explores averaging multiple times within epochs, which can accelerate convergence and find still flatter solutions in a given time.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n**Figure 5**. Performance of fast-SWA on semi-supervised learning with CIFAR-10. fast-SWA achieves record results in every setting considered.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Reinforcement Learning\n\nIn another follow-up [paper](http://www.gatsby.ucl.ac.uk/~balaji/udl-camera-ready/UDL-24.pdf) SWA was shown to improve the performance of policy gradient methods A2C and DDPG on several Atari games and MuJoCo environments [3]. This application is also an instance of where SWA is used with Adam. Recall that SWA is not specific to SGD and can benefit essentially any optimizer.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "{:.table.table-striped.table-bordered}\n | Environment Name | A2C | A2C + SWA | \n| ------------- | ------------- | ------------- | \n| Breakout | 522 \u00b1 34 | 703 \u00b1 60 |\n| Qbert | 18777 \u00b1 778 | 21272 \u00b1 655 |\n| SpaceInvaders | 7727 \u00b1 1121 | 21676 \u00b1 8897 |\n| Seaquest | 1779 \u00b1 4 | 1795 \u00b1 4 |\n| BeamRider | 9999 \u00b1 402 | 11321 \u00b1 1065 |\n| CrazyClimber | 147030 \u00b1 10239 | 139752 \u00b1 11618 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Low Precision Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "We can filter through quantization noise by combining weights that have been rounded down with weights that have been rounded up. Moreover, by averaging weights to find a flat region of the loss surface, large perturbations of the weights will not affect the quality of the solution (Figures 9 and 10). Recent [work](https://arxiv.org/abs/1904.11943) shows that by adapting SWA to the low precision setting, in a method called SWALP, one can match the performance of full-precision SGD even with all training in", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "8 bits [5]. This is quite a practically important result, given that (1) SGD training in 8 bits performs notably worse than full precision SGD, and (2) low precision training is significantly harder than predictions in low precision after training (the usual setting). For example, a ResNet-164 trained on CIFAR-100 with float (16-bit) SGD achieves 22.2% error, while 8-bit SGD achieves 24.0% error. By contrast, SWALP with 8 bit training achieves 21.8% error.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n**Figure 9**. *Quantizing a solution leads to a perturbation of the weights which has a greater effect on the quality of the sharp solution (left) compared to wide solution (right)*. \n\n\n
\n \n
\n**Figure 10**. *The difference between standard low precision training and SWALP*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Another [work](https://arxiv.org/abs/2002.00343), SQWA, presents an approach for quantization and fine-tuning of neural networks in low precision [12]. In particular, SQWA achieved state-of-the-art results for DNNs quantized to 2 bits on CIFAR-100 and ImageNet.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Calibration and Uncertainty Estimates\n\nBy finding a centred solution in the loss, SWA can also improve calibration and uncertainty representation. Indeed, SWA can be viewed as an approximation to an ensemble, resembling a Bayesian model average, but with a single model [1].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "SWA can be viewed as taking the first moment of SGD iterates with a modified learning rate schedule. We can directly generalize SWA by also taking the second moment of iterates to form a Gaussian approximate posterior over the weights, further characterizing the loss geometry with SGD iterates. This approach,[SWA-Gaussian (SWAG)](https://arxiv.org/abs/1902.02476) is a simple, scalable and convenient approach to uncertainty estimation and calibration in Bayesian deep learning [4]. The SWAG distribution", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "approximates the shape of the true posterior: Figure 6 below shows the SWAG distribution and the posterior log-density for ResNet-20 on CIFAR-10.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "**Figure 6**. *SWAG posterior approximation and the loss surface for a ResNet-20 without skip-connections trained on CIFAR-10 in the subspace formed by the two largest eigenvalues of the SWAG covariance matrix. The shape of SWAG distribution is aligned with the posterior: the peaks of the two distributions coincide, and both distributions are wider in one direction than in the orthogonal direction. Visualization created in collaboration with* [Javier Ideami](https://losslandscape.com/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Empirically, SWAG performs on par or better than popular alternatives including MC dropout, KFAC Laplace, and temperature scaling on uncertainty quantification, out-of-distribution detection, calibration and transfer learning in computer vision tasks. Code for SWAG is available [here](https://github.com/wjmaddox/swa_gaussian).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n**Figure 7**. *MultiSWAG generalizes SWAG and deep ensembles, to perform Bayesian model averaging over multiple basins of attraction, leading to significantly improved performance. By contrast, as shown here, deep ensembles select different modes, while standard variational inference (VI) marginalizes (model averages) within a single basin*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "MultiSWAG [9] uses multiple independent SWAG models to form a mixture of Gaussians as an approximate posterior distribution. Different basins of attraction contain highly complementary explanations of the data. Accordingly, marginalizing over these multiple basins provides a significant boost in accuracy and uncertainty representation. MultiSWAG can be viewed as a generalization of deep ensembles, but with performance improvements.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Indeed, we see in Figure 8 that MultiSWAG entirely mitigates double descent -- more flexible models have monotonically improving performance -- and provides significantly improved generalization over SGD. For example, when the ResNet-18 has layers of width 20, Multi-SWAG achieves under 30% error whereas SGD achieves over 45%, more than a 15% gap!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n**Figure 8**. *SGD, SWAG, and Multi-SWAG on CIFAR-100 for a ResNet-18 with varying widths. We see Multi-SWAG in particular mitigates double descent and provides significant accuracy improvements over SGD*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Reference [10] also considers Multi-SWA, which uses multiple independently trained SWA solutions in an ensemble, providing performance improvements over deep ensembles without any additional computational cost. Code for MultiSWA and MultiSWAG is available [here](https://github.com/izmailovpavel/understandingbdl).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Another [method](https://arxiv.org/abs/1907.07504), Subspace Inference, constructs a low-dimensional subspace around the SWA solution and marginalizes the weights in this subspace to approximate the Bayesian model average [5]. Subspace Inference uses the statistics from the SGD iterates to construct both the SWA solution and the subspace. The method achieves strong performance in terms of prediction accuracy and uncertainty calibration both in classification and regression problems. Code is available", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[here](https://github.com/wjmaddox/drbayes).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "Try it Out!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "One of the greatest open questions in deep learning is why SGD manages to find good solutions, given that the training objectives are highly multimodal, and there are many settings of parameters that achieve no training loss but poor generalization. By understanding geometric features such as flatness, which relate to generalization, we can begin to resolve these questions and build optimizers that provide even better generalization, and many other useful features, such as uncertainty representation. We", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "have presented SWA, a simple drop-in replacement for standard optimizers such as SGD and Adam, which can in principle, benefit anyone training a deep neural network. SWA has been demonstrated to have a strong performance in several areas, including computer vision, semi-supervised learning, reinforcement learning, uncertainty representation, calibration, Bayesian model averaging, and low precision training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "We encourage you to try out SWA! SWA is now as easy as any standard training in PyTorch. And even if you have already trained your model, you can use SWA to significantly improve performance by running it for a small number of epochs from a pre-trained model. \n\n\n[1] Averaging Weights Leads to Wider Optima and Better Generalization; Pavel Izmailov, Dmitry Podoprikhin, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson; Uncertainty in Artificial Intelligence (UAI), 2018.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[2] There Are Many Consistent Explanations of Unlabeled Data: Why You Should Average; Ben Athiwaratkun, Marc Finzi, Pavel Izmailov, Andrew Gordon Wilson; \nInternational Conference on Learning Representations (ICLR), 2019.\n\n[3] Improving Stability in Deep Reinforcement Learning with Weight Averaging; Evgenii Nikishin, Pavel Izmailov, Ben Athiwaratkun, Dmitrii Podoprikhin, \nTimur Garipov, Pavel Shvechikov, Dmitry Vetrov, Andrew Gordon Wilson; UAI 2018 Workshop: Uncertainty in Deep Learning, 2018.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[4] A Simple Baseline for Bayesian Uncertainty in Deep Learning\nWesley Maddox, Timur Garipov, Pavel Izmailov, Andrew Gordon Wilson; Neural Information Processing Systems (NeurIPS), 2019.\n\n[5] Subspace Inference for Bayesian Deep Learning\nPavel Izmailov, Wesley Maddox, Polina Kirichenko, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson\nUncertainty in Artificial Intelligence (UAI), 2019.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[6] SWALP : Stochastic Weight Averaging in Low Precision Training\nGuandao Yang, Tianyi Zhang, Polina Kirichenko, Junwen Bai, \nAndrew Gordon Wilson, Christopher De Sa; International Conference on Machine Learning (ICML), 2019.\n\n[7] David Ruppert. Efficient estimations from a slowly convergent Robbins-Monro process; Technical report, Cornell University Operations Research and Industrial Engineering, 1988.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[8] Acceleration of stochastic approximation by averaging. Boris T Polyak and Anatoli B Juditsky; SIAM Journal on Control and Optimization, 30(4):838\u2013855, 1992.\n\n[9] Loss Surfaces, Mode Connectivity, and Fast Ensembling of DNNs\nTimur Garipov, Pavel Izmailov, Dmitrii Podoprikhin, Dmitry Vetrov, \nAndrew Gordon Wilson. Neural Information Processing Systems (NeurIPS), 2018.\n\n[10] Bayesian Deep Learning and a Probabilistic Perspective of Generalization\nAndrew Gordon Wilson, Pavel Izmailov. ArXiv preprint, 2020.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "[11] Stochastic Weight Averaging in Parallel: Large-Batch Training That Generalizes Well\nGupta, Vipul, Santiago Akle Serrano, and Dennis DeCoste; International Conference on Learning Representations (ICLR). 2019.\n\n[12] SQWA: Stochastic Quantized Weight Averaging for Improving the Generalization Capability of Low-Precision Deep Neural Networks\nShin, Sungho, Yoonho Boo, and Wonyong Sung; arXiv preprint 2020.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchRec, and other domain library updates in PyTorch 1.11\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We are introducing the beta release of TorchRec and a number of improvements to the current PyTorch domain libraries, alongside the [PyTorch 1.11 release](https://pytorch.org/blog/pytorch-1.11-released/). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **TorchRec**, a PyTorch domain library for Recommendation Systems, is available in beta. [View it on GitHub](https://github.com/pytorch/torchrec).\n- **TorchAudio** - Added Enformer- and RNN-T-based models and recipes to support the full development lifecycle of a streaming ASR model. See the release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **TorchText** - Added beta support for RoBERTa and XLM-R models, byte-level BPE tokenizer, and text datasets backed by TorchData. See the release notes [here](https://github.com/pytorch/text/releases).\n- **TorchVision** - Added 4 new model families and 14 new classification datasets such as CLEVR, GTSRB, FER2013. See the release notes [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchRec 0.1\n\nWe [announced TorchRec](https://pytorch.org/blog/introducing-torchrec/) a few weeks ago and we are excited to release the beta version today. To recap, TorchRec is a PyTorch domain library for Recommendation Systems. This new library provides common sparsity and parallelism primitives, enabling researchers to build state-of-the-art personalization models and deploy them in production. TorchRec was used to train a 1.25 trillion parameter model, pushed to production in January 2022.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "In particular, the library includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Modeling primitives, such as embedding bags and jagged tensors, that enable easy authoring of large, performant multi-device/multi-node models using hybrid data-parallelism and model-parallelism.\n- Optimized RecSys kernels powered by [FBGEMM](https://github.com/pytorch/FBGEMM), including support for sparse and quantized operations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- A sharder which can partition embedding tables with a variety of different strategies including data-parallel, table-wise, row-wise, table-wise-row-wise, and column-wise sharding.\n- A planner which can automatically generate optimized sharding plans for models.\n- Pipelining to overlap dataloading device transfer (copy to GPU), inter-device communications (input_dist), and computation (forward, backward) for increased performance.\n- GPU inference support.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Common modules for RecSys, such as models and public datasets (Criteo & Movielens).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please check the TorchRec announcement post [here](https://pytorch.org/blog/introducing-torchrec/), [video tutorial](https://www.youtube.com/watch?v=cjgj41dvSeQ), install instructions [here](https://github.com/pytorch/torchrec#readme), test drive the feature through this tutorial [here](https://pytorch.org/tutorials/intermediate/torchrec_tutorial.html), and refer to the reference document [here](https://pytorch.org/torchrec/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio 0.11\n\n#### TorchAudio: Building Blocks for Audio and Speech Processing\n\nWe published a paper, [TorchAudio: Building Blocks for Audio and Speech Processing](https://arxiv.org/abs/2110.15018), describing the overview of the TorchAudio library. If you find TorchAudio useful for your research, please help us share with the community by citing our paper.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) RNN-T & (Prototype) Emformer Models and Recipes\n\n

\n \n

\n\nEmformer is an efficient memory-transformer-based streaming acoustic model that has demonstrated state-of-the-art streaming automatic speech recognition (ASR) performance in low-latency, resource-constrained scenarios, such as on-device applications (citation: [https://arxiv.org/abs/2010.10759](https://arxiv.org/abs/2010.10759)).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The TorchAudio v0.11 release includes the following beta features:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Implementation of Emformer ([docs](https://pytorch.org/audio/main/models.html#emformer))\n- Recurrent neural network transducer (RNN-T) streaming ASR model that uses Emformer for its transcription network ([docs](https://pytorch.org/audio/main/models.html#rnn-t))\n- RNN-T beam search decoder with TorchScript support ([docs](https://pytorch.org/audio/main/models.html#rnntbeamsearch))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- LibriSpeech Emformer RNN-T training recipe ([GitHub](https://github.com/pytorch/audio/tree/release/0.11/examples/asr/librispeech_emformer_rnnt)) and corresponding pre-trained streaming ASR inference pipeline ([docs](https://pytorch.org/audio/main/pipelines.html#emformer-rnnt-base-librispeech))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Also there are prototype features that are available from nightly builds or the main branch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Training recipes trained on MuST-C and TED-LIUM3 datasets. ([GitHub](https://github.com/pytorch/audio/tree/main/examples/asr/emformer_rnnt))\n- Pre-trained pipelines corresponding to the recipes. ([docs](https://pytorch.org/audio/main/prototype.pipelines.html))\n- Tutorial that steps through performing online speech recognition with RNN-T Emformer model. ([docs](https://pytorch.org/audio/main/tutorials/online_asr_tutorial.html))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Collectively, these features cover the full development lifecycle of a streaming ASR model, from definition through training and inference, and enable users to easily develop their own Emformer- and RNN-T-based models.\n\nSpecial thanks to Yangyang Shi, Jay Mahadeokar, and Gil Keren for their code contributions and guidance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) HuBERT Pretrain Model", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The masked prediction training of HuBERT model requires the masked logits, unmasked logits, and feature norm as the outputs. The logits are for cross-entropy losses and the feature norm is for penalty loss. The release adds [HuBERTPretrainModel](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L120-L205) and corresponding factory functions ([hubert_pretrain_base](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L964-L1027),", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[hubert_pretrain_large](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L1030-L1090), and [hubert_pretrain_xlarge](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L1093-L1153)) to enable training from scratch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) CTC Beam Search Decoder\n\nIn recent releases, TorchAudio has added support for ASR models fine-tuned on CTC loss. The addition of an inference time CTC beam search decoder enables running end-to-end ASR evaluation using TorchAudio utils.\n\nThe CTC decoder in TorchAudio supports customizable beam search decoding with lexicon constraint. It also has optional KenLM language model support.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For more details, please check out the [API tutorial](https://pytorch.org/audio/main/tutorials/asr_inference_with_ctc_decoder_tutorial.html) and [documentation](https://pytorch.org/audio/main/prototype.ctc_decoder.html). This prototype feature is available through nightly builds.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Streaming API\n\nTorchAudio started as simple audio I/O APIs that supplement PyTorch. With the recent addition of ASR models and training recipes, the project has received requests to support high-level application development.\n\nStreaming API makes it easy to develop and test the model in online inference. It utilizes ffmpeg under the hood, and enables reading media from online services and hardware devices, decoding media in an incremental manner, and applying filters and preprocessing.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please checkout the [API tutorial](https://pytorch.org/audio/main/tutorials/streaming_api_tutorial.html) and [the documentation](https://pytorch.org/audio/main/prototype.io.html). There are also the [streaming ASR](https://pytorch.org/audio/main/tutorials/online_asr_tutorial.html) tutorial and the [device streaming ASR tutorial](https://pytorch.org/audio/main/tutorials/device_asr.html). This feature is available from nightly releases. Please refer to [pytorch.org](https://pytorch.org/get-started/locally/)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "for how to install nightly builds.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchText 0.12", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) RoBERTa and XLM-R Models\n\nTorchText has added support for pre-trained RoBERTa and XLM-R models. It would allow users to train end-2-end Transformer Encoder based models on standard NLP tasks using TorchText.\n\nMore specifically:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- The models are torchscriptable and hence can be employed for production use-cases.\n- The model APIs let users to easily attach custom task-specific heads with pre-trained encoders.\n- The API also comes equipped with data pre-processing transforms to match the pre-trained weights and model configuration.\n\nWe have added a [tutorial](https://pytorch.org/text/main/tutorials/sst2_classification_non_distributed.html) to demonstrate SST-2 binary text classification task with pre-trained XLM-R base architecture.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For additional details on model APIs and usage examples, please refer to the [documentation](https://pytorch.org/text/main/models.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) byte-level BPE tokenizer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchText has added support for a Byte-Level BPE tokenizer, as used in GPT-2. This tokenizer is also used for tokenizing inputs to the pre-trained RoBERTa models described previously. In addition to the RoBERTa vocab, users can also load their own custom BPE vocab to use the tokenizer. Furthermore, the tokenizer is fully torchscriptable and hence can be employed for production use-cases. For additional details on model APIs and usage examples, please refer to the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[documentation](https://pytorch.org/text/main/transforms.html#gpt2bpetokenizer).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Text datasets backed by TorchData\n\nTorchText has modernized its datasets by migrating from older-style Iterable Datasets to [TorchData\u2019s](https://github.com/pytorch/data#readme) DataPipes. TorchData is a library that provides modular/composable primitives, allowing users to load and transform data in performant data pipelines.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "These DataPipes work out-of-the-box with PyTorch DataLoader and would enable new functionalities like auto-sharding. Users can now easily do data manipulation and pre-processing using user-defined functions and transformations in a functional style programming. Datasets backed by DataPipes also enable standard flow-control like batching, collation, shuffling and bucketizing.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Collectively, DataPipes provides a comprehensive experience for data preprocessing and tensorization needs in a pythonic and flexible way for model training. We have added a [tutorial](https://pytorch.org/text/main/tutorials/sst2_classification_non_distributed.html) to demonstrate data-processing pipelining using the modernized dataset for binary text-classification.\n\nYou can learn more about TorchData DataPipe APIs in its [official documentation](https://pytorch.org/data).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchVision 0.12", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "New Models\n\nFour new model families have been released in the latest version along with pre-trained weights for their variants.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#1 Object Detection\n\n[FCOS](https://arxiv.org/pdf/1904.01355.pdf) is a popular, fully convolutional, anchor-free model for object detection. In this release we include a community-contributed model implementation as well as pre-trained weights. The model was trained on COCO train2017 and can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models\n\nx = [torch.rand(3, 224, 224)]\nfcos = models.detection.fcos_resnet50_fpn(pretrained=True).eval()\npredictions = fcos(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The box AP of the pre-trained model on COCO val2017 is 39.2 (see [#4961](https://github.com/pytorch/vision/pull/4961) for more details).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank [Hu Ye](https://github.com/xiaohu2015) and [Zhiqiang Wang](https://github.com/zhiqwang) for contributing to the model implementation and initial training. This was the first community-contributed model in a long while, and given its success, we decided to use the learnings from this process and create a new [model contribution guidelines](https://github.com/pytorch/vision/blob/main/CONTRIBUTING_MODELS.md).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#2 Optical Flow support and RAFT model\n\nTorchVision now supports optical flow! Optical Flow models try to predict movement in a video: given two consecutive frames, the model predicts where each pixel of the first frame ends up in the second frame. Check out our [new tutorial on Optical Flow](https://pytorch.org/vision/0.12/auto_examples/plot_optical_flow.html#sphx-glr-auto-examples-plot-optical-flow-py)!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We implemented a torchscript-compatible [RAFT](https://arxiv.org/abs/2003.12039) model with pre-trained weights (both normal and \u201csmall\u201d versions), and added support for [training and evaluating](https://github.com/pytorch/vision/tree/main/references/optical_flow) optical flow models. Our training scripts support distributed training across processes and nodes, leading to much faster training time than the original implementation. We also added 5 new [optical flow", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "datasets](https://pytorch.org/vision/0.12/datasets.html#optical-flow): Flying Chairs, Flying Things, Sintel, Kitti, and HD1K.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#3. Image Classification\n\n[Vision Transformer](https://arxiv.org/abs/2010.11929) (ViT) and [ConvNeXt](https://arxiv.org/abs/2201.03545) are two popular architectures which can be used as image classifiers or as backbones for downstream vision tasks. In this release we include 8 pre-trained weights for their classification variants. The models were trained on ImageNet and can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "x = torch.rand(1, 3, 224, 224)\nvit = models.vit_b_16(pretrained=True).eval()\nconvnext = models.convnext_tiny(pretrained=True).eval()\npredictions1 = vit(x)\npredictions2 = convnext(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The accuracies of the pre-trained models obtained on ImageNet val are seen below:\n\n| **Model** | **Acc@1** | **Acc@5** |\n| -------------- | --------: | --------: |\n| vit_b_16 | 81.072 | 95.318 |\n| vit_b_32 | 75.912 | 92.466 |\n| vit_l_16 | 79.662 | 94.638 |\n| vit_l_32 | 76.972 | 93.07 |\n| convnext_tiny | 82.52 | 96.146 |\n| convnext_small | 83.616 | 96.65 |\n| convnext_base | 84.062 | 96.87 |\n| convnext_large | 84.414 | 96.976 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The above models have been trained using an adjusted version of our new [training recipe](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and this allows us to offer models with accuracies significantly higher than the ones on the original papers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#4. GPU Video Decoding\n\nIn this release, we add support for GPU video decoding in the video reading API. To use hardware-accelerated decoding, we just need to pass a cuda device to the video reading API as shown below:\n\n```python\nimport torchvision\n\nreader = torchvision.io.VideoReader(file_name, device=\"cuda:0\")\nfor frame in reader:\n print(frame)\n```\n\nWe also support seeking to anyframe or a keyframe in the video before reading, as shown below:\n\n```python\nreader.seek(seek_time)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "New Datasets\n\nWe have implemented 14 new [classification datasets](https://pytorch.org/vision/0.12/datasets.html#image-classification): CLEVR, GTSRB, FER2013, SUN397, Country211, Flowers102, fvgc_aircraft, OxfordIIITPet, DTD, Food 101, Rendered SST2, Stanford cars, PCAM, and EuroSAT.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "As part of our work on Optical Flow support (see above for more details), we also added 5 new [optical flow datasets](https://pytorch.org/vision/0.12/datasets.html#optical-flow): Flying Chairs, Flying Things, Sintel, Kitti, and HD1K.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Other Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **New documentation layout**: Each function / class is now documented in a separate page, clearing up some space in the per-module pages, and easing the discovery of the proposed APIs. Compare e.g. our [previous docs](https://pytorch.org/vision/0.11/transforms.html) vs the [new ones](https://pytorch.org/vision/0.12/transforms.html). Please let us know if you have any [feedback](https://github.com/pytorch/vision/issues/5511)!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **New [model contribution guidelines](https://github.com/pytorch/vision/blob/main/CONTRIBUTING_MODELS.md)** have been published following the success of the [FCOS](https://github.com/pytorch/vision/pull/4961) model which was contributed by the community. These guidelines aim to be an overview of the model contribution process for anyone who would like to suggest, implement and train a new model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **Upcoming Prototype API** - We are currently working on a prototype API which adds Multi-weight support on all of our model builder methods. This will enable us to offer multiple pre-trained weights, associated with their meta-data and inference transforms. The API is still under review and thus was not included in the release but you can read more about it on our [blogpost](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) and provide your feedback on the dedicated [Github", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "issue](https://github.com/pytorch/vision/issues/5088).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **Changes in our deprecation policy** - Up until now, torchvision would almost never remove deprecated APIs. In order to be more aligned and consistent with pytorch core, we are updating our deprecation policy. We are now following a 2-release deprecation cycle: deprecated APIs will raise a warning for 2 versions, and will be removed after that. To reflect these changes and to smooth the transition, we have decided to:\n - Remove all APIs that had been deprecated before or on v0.8, released 1.5 years ago.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Update the removal timeline of all other deprecated APIs to v0.14, to reflect the new 2-cycle policy starting now in v0.12.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Captum 0.5\n\n[Captum](https://captum.ai/) is a PyTorch library for model interpretability. For this release, we expanded Captum with influential instances and added support for both similarity based influences and novel algorithms, [TracIn](https://arxiv.org/abs/2002.08484) and its variants. TracIn variants offer faster approximation of influence scores based on random projections for fully connected layers.\n\nMore specifically the new, influence, subsection of Captum includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **[SimilarityInfluence](https://captum.ai/api/influence.html#similarityinfluence)** computes similarity scores between test and training examples using default (cosine or euclidean) or custom user definite metrics w.r.t. given input model layers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **[TracInCP](https://captum.ai/api/influence.html#tracincp)** approximates the influential score of each training example on a given test example based on the dot-product similarity between loss gradients w.r.t. model parameters for test and training examples. Note that if we use training examples as test examples then we compute self influence. This method and its variants described below also return top-k proponents and opponents which are the top-k largest positive and negative influential examples", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "respectively.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **[TracInCPFast](https://captum.ai/api/influence.html#tracincpfast)** is an approximation of TracInCP that avoids computing the gradients w.r.t. large parameter matrices. It approximates influence score based on the dot products between last fully connected layer activations and loss gradients w.r.t. that layer for training and test examples.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **[TracInCPFastRandProj](https://captum.ai/api/influence.html#tracincpfastrandproj)** uses a nearest neighbor approximation library such as annoy to compute the dot product between the training and test quantities. In order to reduce the dimensionality of layer activations and corresponding gradients this method, in addition, allows to project those vectors into a lower dimensional space using random projection matrices.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "More about the implementation of influential instances can be found on our [GitHub](https://github.com/pytorch/captum/tree/master/captum/influence) page and [tutorials](https://captum.ai/tutorials/TracInCP_Tutorial).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "
\n
\n
\n \n
\n
\n
\n", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerating Hugging Face and TIMM models with PyTorch 2.0\"\nauthor: Mark Saroufim\nfeatured-img: \"assets/images/pytorch-2.0-feature-img.png\"\n---\n\n`torch.compile()` makes it easy to experiment with different compiler backends to make PyTorch code faster with a single line decorator `torch.compile()`. It works either directly over an nn.Module as a drop-in replacement for `torch.jit.script()` but without requiring you to make any source code changes. We expect this one line code change to provide you with between 30%-2x training time speedups on the vast majority of models that you\u2019re already running.\n\n```python\n\nopt_module = torch.compile(module)\n\n```\n\ntorch.compile supports arbitrary PyTorch code, control flow, mutation and comes with experimental support for dynamic shapes. We\u2019re so excited about this development that we call it PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "What makes this announcement different for us is we\u2019ve already benchmarked some of the most popular open source PyTorch models and gotten substantial speedups ranging from 30% to 2x [https://github.com/pytorch/torchdynamo/issues/681](https://github.com/pytorch/torchdynamo/issues/681).\n\nThere are no tricks here, we\u2019ve pip installed popular libraries like [https://github.com/huggingface/transformers](https://github.com/huggingface/transformers), [https://github.com/huggingface/accelerate](https://github.com/huggingface/accelerate) and [https://github.com/rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) and then ran torch.compile() on them and that\u2019s it.\n\nIt\u2019s rare to get both performance and convenience, but this is why the core team finds PyTorch 2.0 so exciting. The Hugging Face team is also excited, in their words:\n\nRoss Wightman the primary maintainer of TIMM: \u201cPT 2.0 works out of the box with majority of timm models for inference and train workloads and no code changes\u201d", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "Sylvain Gugger the primary maintainer of transformers and accelerate: \"With just one line of code to add, PyTorch 2.0 gives a speedup between 1.5x and 2.x in training Transformers models. This is the most exciting thing since mixed precision training was introduced!\"\n\nThis tutorial will show you exactly how to replicate those speedups so you can be as excited as to PyTorch 2.0 as we are.\n\n## Requirements and Setup\n\nFor GPU (newer generation GPUs will see drastically better performance)\n\n```\npip3 install numpy --pre torch --force-reinstall --extra-index-url https://download.pytorch.org/whl/nightly/cu117\n\n```\n\nFor CPU\n\n```\npip3 install --pre torch --extra-index-url https://download.pytorch.org/whl/nightly/cpu\n\n```\n\nOptional: Verify Installation\n\n```\ngit clone https://github.com/pytorch/pytorch\ncd tools/dynamo\npython verify_dynamo.py\n```\n\nOptional: Docker installation\n\nWe also provide all the required dependencies in the PyTorch nightly\nbinaries which you can download with", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "```\ndocker pull ghcr.io/pytorch/pytorch-nightly\n\n```\n\nAnd for ad hoc experiments just make sure that your container has access\nto all your GPUs\n\n```\ndocker run --gpus all -it ghcr.io/pytorch/pytorch-nightly:latest /bin/bash\n\n```\n\n## Getting started\n\n### a toy exmaple\n\nLet\u2019s start with a simple example and make things more complicated step\nby step. Please note that you\u2019re likely to see more significant speedups the newer your GPU is.\n\n```python\nimport torch\ndef fn(x, y):\n a = torch.sin(x).cuda()\n b = torch.sin(y).cuda()\n return a + b\nnew_fn = torch.compile(fn, backend=\"inductor\")\ninput_tensor = torch.randn(10000).to(device=\"cuda:0\")\na = new_fn(input_tensor, input_tensor)\n```\n\nThis example won\u2019t actually run faster but it\u2019s educational.\n\nexample that features `torch.cos()` and `torch.sin()` which are examples of pointwise ops as in they operate element by element on a vector. A more famous pointwise op you might actually want to use would be something like `torch.relu()`.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "Pointwise ops in eager mode are suboptimal because each one would need to read a tensor from memory, make some changes and then write back those changes.\n\nThe single most important optimization that PyTorch 2.0 does for you is fusion.\n\nSo back to our example we can turn 2 reads and 2 writes into 1 read and 1 write which is crucial especially for newer GPUs where the bottleneck is memory bandwidth (how quickly you can send data to a GPU) instead of compute (how quickly your GPU can crunch floating point operations)\n\nThe second most important optimization that PyTorch 2.0 does for you is CUDA graphs\n\nCUDA graphs help eliminate the overhead from launching individual kernels from a python program.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "torch.compile() supports many different backends but one that we\u2019re particularly excited about is Inductor which generates Triton kernels [https://github.com/openai/triton](https://github.com/openai/triton) which are written in Python yet outperform the vast majority of handwritten CUDA kernels. Suppose our example above was called trig.py we can actually inspect the code generated triton kernels by running.\n\n```\nTORCH_COMPILE_DEBUG=1 python trig.py\n```\n\n```python", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "```python\n\n@pointwise(size_hints=[16384], filename=__file__, meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': 0, 'constants': {}, 'configs': [instance_descriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]})\n@triton.jit\ndef kernel(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):\n xnumel = 10000\n xoffset = tl.program_id(0) * XBLOCK\n xindex = xoffset + tl.reshape(tl.arange(0, XBLOCK), [XBLOCK])\n xmask = xindex < xnumel\n x0 = xindex\n tmp0 = tl.load(in_ptr0 + (x0), xmask)\n tmp1 = tl.sin(tmp0)\n tmp2 = tl.sin(tmp1)\n tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp2, xmask)\n\n```\n\nAnd you can verify that fusing the two `sins` did actually occur because the two `sin` operations occur within a single Triton kernel and the temporary variables are held in registers with very fast access.\n\n### a real model\n\nAs a next step let\u2019s try a real model like resnet50 from the PyTorch hub.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "```python\nimport torch\nmodel = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)\nopt_model = torch.compile(model, backend=\"inductor\")\nmodel(torch.randn(1,3,64,64))\n\n```\n\nIf you actually run you may be surprised that the first run is slow and that\u2019s because the model is being compiled. Subsequent runs will be faster so it's common practice to warm up your model before you start benchmarking it.\n\nYou may have noticed how we also passed in the name of a compiler explicitly here with \u201cinductor\u201d but it\u2019s not the only available backend, you can run in a REPL `torch._dynamo.list_backends()` to see the full list of available backends. For fun you should try out `aot_cudagraphs` or `nvfuser`.\n\n### Hugging Face models", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "Let\u2019s do something a bit more interesting now, our community frequently\nuses pretrained models from transformers [https://github.com/huggingface/transformers](https://github.com/huggingface/transformers) or TIMM [https://github.com/rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) and one of our design goals for PyTorch 2.0 was that any new compiler stack needs to work out of the box with the vast majority of models people actually run.\n\nSo we\u2019re going to directly download a pretrained model from the Hugging Face hub and optimize it\n\n```python", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "```python\n\nimport torch\nfrom transformers import BertTokenizer, BertModel\n# Copy pasted from here https://huggingface.co/bert-base-uncased\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\nmodel = BertModel.from_pretrained(\"bert-base-uncased\").to(device=\"cuda:0\")\nmodel = torch.compile(model) # This is the only line of code that we changed\ntext = \"Replace me by any text you'd like.\"\nencoded_input = tokenizer(text, return_tensors='pt').to(device=\"cuda:0\")\noutput = model(**encoded_input)\n\n```\n\nIf you remove the `to(device=\"cuda:0\")` from the model and `encoded_input` then PyTorch 2.0 will generate C++ kernels that will be optimized for running on your CPU. You can inspect both Triton or C++ kernels for BERT, they\u2019re obviously more complex than the trigonometry example we had above but you can similarly skim it and understand if you understand PyTorch.\n\nThe same code also works just fine if used with [https://github.com/huggingface/accelerate](https://github.com/huggingface/accelerate) and DDP", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "Similarly let\u2019s try out a TIMM example\n\n```python\nimport timm\nimport torch\nmodel = timm.create_model('resnext101_32x8d', pretrained=True, num_classes=2)\nopt_model = torch.compile(model, backend=\"inductor\")\nopt_model(torch.randn(64,3,7,7))\n```\n\nOur goal with PyTorch was to build a breadth-first compiler that would speed up the vast majority of actual models people run in open source. The Hugging Face Hub ended up being an extremely valuable benchmarking tool for us, ensuring that any optimization we work on actually helps accelerate models people want to run.\n\nSo please try out PyTorch 2.0, enjoy the free perf and if you\u2019re not seeing it then please open an issue and we will make sure your model is supported [https://github.com/pytorch/torchdynamo/issues](https://github.com/pytorch/torchdynamo/issues)\n\nAfter all, we can\u2019t claim we\u2019re created a breadth-first unless YOUR models actually run faster.", "metadata": {"source": "https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.6 now includes Stochastic Weight Averaging'\nauthor: Pavel Izmailov, Andrew Gordon Wilson and Vincent Queneneville-Belair\n---\n\nDo you use stochastic gradient descent (SGD) or Adam? Regardless of the procedure you use to train your neural network, you can likely achieve significantly better generalization at virtually no additional cost with a simple new technique now natively supported in PyTorch 1.6, Stochastic Weight Averaging (SWA) [1]. Even if you have already trained your model, it\u2019s easy to realize the benefits of SWA by running SWA for a small number of epochs starting with a pre-trained model. [Again](https://twitter.com/MilesCranmer/status/1282140440892932096) and [again](https://twitter.com/leopd/status/1285969855062192129), researchers are discovering that SWA improves the performance of well-tuned models in a wide array of practical applications with little cost or effort!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "SWA has a wide range of applications and features:\n* SWA significantly improves performance compared to standard training techniques in computer vision (e.g., VGG, ResNets, Wide ResNets and DenseNets on ImageNet and CIFAR benchmarks [1, 2]).\n* SWA provides state-of-the-art performance on key benchmarks in semi-supervised learning and domain adaptation [2].\n* SWA was shown to improve performance in language modeling (e.g., AWD-LSTM on WikiText-2 [4]) and policy-gradient methods in deep reinforcement learning [3].\n* SWAG, an extension of SWA, can approximate Bayesian model averaging in Bayesian deep learning and achieves state-of-the-art uncertainty calibration results in various settings. Moreover, its recent generalization MultiSWAG provides significant additional performance gains and mitigates double-descent [4, 10]. Another approach, Subspace Inference, approximates the Bayesian posterior in a small subspace of the parameter space around the SWA solution [5].\n* SWA for low precision training, SWALP, can match the performance of full-precision SGD training, even with all numbers quantized down to 8 bits, including gradient accumulators [6].\n* SWA in parallel, SWAP, was shown to greatly speed up the training of neural networks by using large batch sizes and, in particular, set a record by training a neural network to 94% accuracy on CIFAR-10 in 27 seconds [11].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n**Figure 1**. *Illustrations of SWA and SGD with a Preactivation ResNet-164 on CIFAR-100 [1]. **Left**: test error surface for three FGE samples and the corresponding SWA solution (averaging in weight space). **Middle** and **Right**: test error and train loss surfaces showing the weights proposed by SGD (at convergence) and SWA, starting from the same initialization of SGD after 125 training epochs. Please see [1] for details on how these figures were constructed*.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "In short, SWA performs an equal average of the weights traversed by SGD (or any stochastic optimizer) with a modified learning rate schedule (see the left panel of Figure 1.). SWA solutions end up in the center of a wide flat region of loss, while SGD tends to converge to the boundary of the low-loss region, making it susceptible to the shift between train and test error surfaces (see the middle and right panels of Figure 1). We emphasize that SWA **can be used with any optimizer, such as Adam, and is not specific to SGD**.\n\nPreviously, SWA was in PyTorch contrib. In PyTorch 1.6, we provide a new convenient implementation of SWA in [torch.optim.swa_utils](https://pytorch.org/docs/stable/optim.html#stochastic-weight-averaging).\n\n## Is this just Averaged SGD?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "At a high level, averaging SGD iterates dates back several decades in convex optimization [7, 8], where it is sometimes referred to as Polyak-Ruppert averaging, or averaged SGD. **But the details matter**. Averaged SGD is often used in conjunction with a decaying learning rate, and an exponential moving average (EMA), typically for convex optimization. In convex optimization, the focus has been on improved rates of convergence. In deep learning, this form of averaged SGD smooths the trajectory of SGD iterates but does not perform very differently.\n\nBy contrast, SWA uses an **equal average** of SGD iterates with a modified **cyclical or high constant learning rate** and exploits the flatness of training objectives [8] specific to **deep learning** for **improved generalization**. \n\n## How does Stochastic Weight Averaging Work?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "There are two important ingredients that make SWA work. First, SWA uses a **modified learning rate** schedule so that SGD (or other optimizers such as Adam) continues to bounce around the optimum and explore diverse models instead of simply converging to a single solution. For example, we can use the standard decaying learning rate strategy for the first 75% of training time and then set the learning rate to a reasonably high constant value for the remaining 25% of the time (see Figure 2 below). The second ingredient is to take an average of the weights **(typically an equal average)** of the networks traversed by SGD. For example, we can maintain a running average of the weights obtained at the end of every epoch within the last 25% of training time (see Figure 2). After training is complete, we then set the weights of the network to the computed SWA averages.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "**Figure 2**. *Illustration of the learning rate schedule adopted by SWA. Standard decaying schedule is used for the first 75% of the training and then a high constant value is used for the remaining 25%. The SWA averages are formed during the last 25% of training*.\n\nOne important detail is the batch normalization. Batch normalization layers compute running statistics of activations during training. Note that the SWA averages of the weights are never used to make predictions during training. So the batch normalization layers do not have the activation statistics computed at the end of training. We can compute these statistics by doing a single forward pass on the train data with the SWA model.\n\nWhile we focus on SGD for simplicity in the description above, SWA can be combined with any optimizer. You can also use cyclical learning rates instead of a high constant value (see e.g., [2]).\n\n## How to use SWA in PyTorch?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "In `torch.optim.swa_utils` we implement all the SWA ingredients to make it convenient to use SWA with any model. In particular, we implement `AveragedModel` class for SWA models, `SWALR` learning rate scheduler, and `update_bn` utility function to update SWA batch normalization statistics at the end of training. \n\nIn the example below, `swa_model` is the SWA model that accumulates the averages of the weights. We train the model for a total of 300 epochs, and we switch to the SWA learning rate schedule and start to collect SWA averages of the parameters at epoch 160. \n\n```python\nfrom torch.optim.swa_utils import AveragedModel, SWALR\nfrom torch.optim.lr_scheduler import CosineAnnealingLR\n\nloader, optimizer, model, loss_fn = ...\nswa_model = AveragedModel(model)\nscheduler = CosineAnnealingLR(optimizer, T_max=100)\nswa_start = 5\nswa_scheduler = SWALR(optimizer, swa_lr=0.05)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "for epoch in range(100):\n for input, target in loader:\n optimizer.zero_grad()\n loss_fn(model(input), target).backward()\n optimizer.step()\n if epoch > swa_start:\n swa_model.update_parameters(model)\n swa_scheduler.step()\n else:\n scheduler.step()\n\n# Update bn statistics for the swa_model at the end\ntorch.optim.swa_utils.update_bn(loader, swa_model)\n# Use swa_model to make predictions on test data \npreds = swa_model(test_input)\n```\n\nNext, we explain each component of `torch.optim.swa_utils` in detail.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "`AveragedModel` class serves to compute the weights of the SWA model. You can create an averaged model by running `swa_model = AveragedModel(model)`. You can then update the parameters of the averaged model by `swa_model.update_parameters(model)`. By default, `AveragedModel` computes a running equal average of the parameters that you provide, but you can also use custom averaging functions with the `avg_fn` parameter. In the following example, `ema_model` computes an exponential moving average.\n\n```python\nema_avg = lambda averaged_model_parameter, model_parameter, num_averaged:\\\n0.1 * averaged_model_parameter + 0.9 * model_parameter\nema_model = torch.optim.swa_utils.AveragedModel(model, avg_fn=ema_avg)\n```\n\nIn practice, we find an equal average with the modified learning rate schedule in Figure 2 provides the best performance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "`SWALR` is a learning rate scheduler that anneals the learning rate to a fixed value, and then keeps it constant. For example, the following code creates a scheduler that linearly anneals the learning rate from its initial value to `0.05` in `5` epochs within each parameter group.\n\n```python\nswa_scheduler = torch.optim.swa_utils.SWALR(optimizer, \nanneal_strategy=\"linear\", anneal_epochs=5, swa_lr=0.05)\n\n```\nWe also implement cosine annealing to a fixed value (`anneal_strategy=\"cos\"`). In practice, we typically switch to `SWALR` at epoch `swa_start` (e.g. after 75% of the training epochs), and simultaneously start to compute the running averages of the weights:\n\n```python\nscheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=100)\nswa_start = 75\nfor epoch in range(100):\n # \n if i > swa_start:\n swa_model.update_parameters(model)\n swa_scheduler.step()\n else:\n scheduler.step()\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "Finally, `update_bn` is a utility function that computes the batchnorm statistics for the SWA model on a given dataloader `loader`:\n```\ntorch.optim.swa_utils.update_bn(loader, swa_model) \n```\n`update_bn` applies the `swa_model` to every element in the dataloader and computes the activation statistics for each batch normalization layer in the model.\n\nOnce you computed the SWA averages and updated the batch normalization layers, you can apply `swa_model` to make predictions on test data.\n\n## Why does it work?", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "## Why does it work?\n\nThere are large flat regions of the loss surface [9]. In Figure 3 below, we show a visualization of the loss surface in a subspace of the parameter space containing a path connecting two independently trained SGD solutions, such that the loss is similarly low at every point along the path. SGD converges near the boundary of these regions because there isn\u2019t much gradient signal to move inside, as the points in the region all have similarly low values of loss. By increasing the learning rate, SWA spins around this flat region, and then by averaging the iterates, moves towards the center of the flat region.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "**Figure 3**: *visualization of mode connectivity for ResNet-20 with no skip connections on CIFAR-10 dataset. The visualization is created in collaboration with Javier Ideami [(https://losslandscape.com/)](https://losslandscape.com/). For more details, see this [blogpost](https://izmailovpavel.github.io/curves_blogpost/)*.\n\nWe expect solutions that are centered in the flat region of the loss to generalize better than those near the boundary. Indeed, train and test error surfaces are not perfectly aligned in the weight space. Solutions that are centered in the flat region are not as susceptible to the shifts between train and test error surfaces as those near the boundary. In Figure 4 below, we show the train loss and test error surfaces along the direction connecting the SWA and SGD solutions. As you can see, while the SWA solution has a higher train loss compared to the SGD solution, it is centered in a region of low loss and has a substantially better test error.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n**Figure 4**. *Train loss and test error along the line connecting the SWA solution (circle) and SGD solution (square). The SWA solution is centered in a wide region of low train loss, while the SGD solution lies near the boundary. Because of the shift between train loss and test error surfaces, the SWA solution leads to much better generalization*.\n\n## What are the results achieved with SWA?\n\nWe release a GitHub [repo](https://github.com/izmailovpavel/torch_swa_examples) with examples using the PyTorch implementation of SWA for training DNNs. For example, these examples can be used to achieve the following results on CIFAR-100:\n\n\n {:.table.table-striped.table-bordered}\n | | VGG-16 | ResNet-164 | WideResNet-28x10 | \n| ------------- | ------------- | ------------- | ------------- |\n| SGD | 72.8 \u00b1 0.3 | 78.4 \u00b1 0.3 | 81.0 \u00b1 0.3 | \n| SWA | 74.4 \u00b1 0.3 | 79.8 \u00b1 0.4 | 82.5 \u00b1 0.2 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "## Semi-Supervised Learning\n\nIn a follow-up [paper](https://arxiv.org/abs/1806.05594) SWA was applied to semi-supervised learning, where it improved the best reported results in multiple settings [2]. For example, with SWA you can get 95% accuracy on CIFAR-10 if you only have the training labels for 4k training data points (the previous best reported result on this problem was 93.7%). This paper also explores averaging multiple times within epochs, which can accelerate convergence and find still flatter solutions in a given time.\n\n
\n \n
\n**Figure 5**. Performance of fast-SWA on semi-supervised learning with CIFAR-10. fast-SWA achieves record results in every setting considered.\n\n## Reinforcement Learning", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "In another follow-up [paper](http://www.gatsby.ucl.ac.uk/~balaji/udl-camera-ready/UDL-24.pdf) SWA was shown to improve the performance of policy gradient methods A2C and DDPG on several Atari games and MuJoCo environments [3]. This application is also an instance of where SWA is used with Adam. Recall that SWA is not specific to SGD and can benefit essentially any optimizer.\n\n\n{:.table.table-striped.table-bordered}\n | Environment Name | A2C | A2C + SWA | \n| ------------- | ------------- | ------------- | \n| Breakout | 522 \u00b1 34 | 703 \u00b1 60 |\n| Qbert | 18777 \u00b1 778 | 21272 \u00b1 655 |\n| SpaceInvaders | 7727 \u00b1 1121 | 21676 \u00b1 8897 |\n| Seaquest | 1779 \u00b1 4 | 1795 \u00b1 4 |\n| BeamRider | 9999 \u00b1 402 | 11321 \u00b1 1065 |\n| CrazyClimber | 147030 \u00b1 10239 | 139752 \u00b1 11618 |\n\n\n## Low Precision Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "We can filter through quantization noise by combining weights that have been rounded down with weights that have been rounded up. Moreover, by averaging weights to find a flat region of the loss surface, large perturbations of the weights will not affect the quality of the solution (Figures 9 and 10). Recent [work](https://arxiv.org/abs/1904.11943) shows that by adapting SWA to the low precision setting, in a method called SWALP, one can match the performance of full-precision SGD even with all training in 8 bits [5]. This is quite a practically important result, given that (1) SGD training in 8 bits performs notably worse than full precision SGD, and (2) low precision training is significantly harder than predictions in low precision after training (the usual setting). For example, a ResNet-164 trained on CIFAR-100 with float (16-bit) SGD achieves 22.2% error, while 8-bit SGD achieves 24.0% error. By contrast, SWALP with 8 bit training achieves 21.8% error.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n**Figure 9**. *Quantizing a solution leads to a perturbation of the weights which has a greater effect on the quality of the sharp solution (left) compared to wide solution (right)*. \n\n\n
\n \n
\n**Figure 10**. *The difference between standard low precision training and SWALP*.\n\nAnother [work](https://arxiv.org/abs/2002.00343), SQWA, presents an approach for quantization and fine-tuning of neural networks in low precision [12]. In particular, SQWA achieved state-of-the-art results for DNNs quantized to 2 bits on CIFAR-100 and ImageNet.\n\n## Calibration and Uncertainty Estimates\n\nBy finding a centred solution in the loss, SWA can also improve calibration and uncertainty representation. Indeed, SWA can be viewed as an approximation to an ensemble, resembling a Bayesian model average, but with a single model [1].", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "SWA can be viewed as taking the first moment of SGD iterates with a modified learning rate schedule. We can directly generalize SWA by also taking the second moment of iterates to form a Gaussian approximate posterior over the weights, further characterizing the loss geometry with SGD iterates. This approach,[SWA-Gaussian (SWAG)](https://arxiv.org/abs/1902.02476) is a simple, scalable and convenient approach to uncertainty estimation and calibration in Bayesian deep learning [4]. The SWAG distribution approximates the shape of the true posterior: Figure 6 below shows the SWAG distribution and the posterior log-density for ResNet-20 on CIFAR-10.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n**Figure 6**. *SWAG posterior approximation and the loss surface for a ResNet-20 without skip-connections trained on CIFAR-10 in the subspace formed by the two largest eigenvalues of the SWAG covariance matrix. The shape of SWAG distribution is aligned with the posterior: the peaks of the two distributions coincide, and both distributions are wider in one direction than in the orthogonal direction. Visualization created in collaboration with* [Javier Ideami](https://losslandscape.com/).\n\nEmpirically, SWAG performs on par or better than popular alternatives including MC dropout, KFAC Laplace, and temperature scaling on uncertainty quantification, out-of-distribution detection, calibration and transfer learning in computer vision tasks. Code for SWAG is available [here](https://github.com/wjmaddox/swa_gaussian).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n**Figure 7**. *MultiSWAG generalizes SWAG and deep ensembles, to perform Bayesian model averaging over multiple basins of attraction, leading to significantly improved performance. By contrast, as shown here, deep ensembles select different modes, while standard variational inference (VI) marginalizes (model averages) within a single basin*.\n\nMultiSWAG [9] uses multiple independent SWAG models to form a mixture of Gaussians as an approximate posterior distribution. Different basins of attraction contain highly complementary explanations of the data. Accordingly, marginalizing over these multiple basins provides a significant boost in accuracy and uncertainty representation. MultiSWAG can be viewed as a generalization of deep ensembles, but with performance improvements.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "Indeed, we see in Figure 8 that MultiSWAG entirely mitigates double descent -- more flexible models have monotonically improving performance -- and provides significantly improved generalization over SGD. For example, when the ResNet-18 has layers of width 20, Multi-SWAG achieves under 30% error whereas SGD achieves over 45%, more than a 15% gap! \n\n
\n \n
\n**Figure 8**. *SGD, SWAG, and Multi-SWAG on CIFAR-100 for a ResNet-18 with varying widths. We see Multi-SWAG in particular mitigates double descent and provides significant accuracy improvements over SGD*.\n\nReference [10] also considers Multi-SWA, which uses multiple independently trained SWA solutions in an ensemble, providing performance improvements over deep ensembles without any additional computational cost. Code for MultiSWA and MultiSWAG is available [here](https://github.com/izmailovpavel/understandingbdl).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "Another [method](https://arxiv.org/abs/1907.07504), Subspace Inference, constructs a low-dimensional subspace around the SWA solution and marginalizes the weights in this subspace to approximate the Bayesian model average [5]. Subspace Inference uses the statistics from the SGD iterates to construct both the SWA solution and the subspace. The method achieves strong performance in terms of prediction accuracy and uncertainty calibration both in classification and regression problems. Code is available [here](https://github.com/wjmaddox/drbayes).\n\n## Try it Out!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "## Try it Out!\n\nOne of the greatest open questions in deep learning is why SGD manages to find good solutions, given that the training objectives are highly multimodal, and there are many settings of parameters that achieve no training loss but poor generalization. By understanding geometric features such as flatness, which relate to generalization, we can begin to resolve these questions and build optimizers that provide even better generalization, and many other useful features, such as uncertainty representation. We have presented SWA, a simple drop-in replacement for standard optimizers such as SGD and Adam, which can in principle, benefit anyone training a deep neural network. SWA has been demonstrated to have a strong performance in several areas, including computer vision, semi-supervised learning, reinforcement learning, uncertainty representation, calibration, Bayesian model averaging, and low precision training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "We encourage you to try out SWA! SWA is now as easy as any standard training in PyTorch. And even if you have already trained your model, you can use SWA to significantly improve performance by running it for a small number of epochs from a pre-trained model. \n\n\n[1] Averaging Weights Leads to Wider Optima and Better Generalization; Pavel Izmailov, Dmitry Podoprikhin, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson; Uncertainty in Artificial Intelligence (UAI), 2018.\n\n[2] There Are Many Consistent Explanations of Unlabeled Data: Why You Should Average; Ben Athiwaratkun, Marc Finzi, Pavel Izmailov, Andrew Gordon Wilson; \nInternational Conference on Learning Representations (ICLR), 2019.\n\n[3] Improving Stability in Deep Reinforcement Learning with Weight Averaging; Evgenii Nikishin, Pavel Izmailov, Ben Athiwaratkun, Dmitrii Podoprikhin, \nTimur Garipov, Pavel Shvechikov, Dmitry Vetrov, Andrew Gordon Wilson; UAI 2018 Workshop: Uncertainty in Deep Learning, 2018.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "[4] A Simple Baseline for Bayesian Uncertainty in Deep Learning\nWesley Maddox, Timur Garipov, Pavel Izmailov, Andrew Gordon Wilson; Neural Information Processing Systems (NeurIPS), 2019.\n\n[5] Subspace Inference for Bayesian Deep Learning\nPavel Izmailov, Wesley Maddox, Polina Kirichenko, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson\nUncertainty in Artificial Intelligence (UAI), 2019.\n\n[6] SWALP : Stochastic Weight Averaging in Low Precision Training\nGuandao Yang, Tianyi Zhang, Polina Kirichenko, Junwen Bai, \nAndrew Gordon Wilson, Christopher De Sa; International Conference on Machine Learning (ICML), 2019.\n\n[7] David Ruppert. Efficient estimations from a slowly convergent Robbins-Monro process; Technical report, Cornell University Operations Research and Industrial Engineering, 1988.\n\n[8] Acceleration of stochastic approximation by averaging. Boris T Polyak and Anatoli B Juditsky; SIAM Journal on Control and Optimization, 30(4):838\u2013855, 1992.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "[9] Loss Surfaces, Mode Connectivity, and Fast Ensembling of DNNs\nTimur Garipov, Pavel Izmailov, Dmitrii Podoprikhin, Dmitry Vetrov, \nAndrew Gordon Wilson. Neural Information Processing Systems (NeurIPS), 2018.\n\n[10] Bayesian Deep Learning and a Probabilistic Perspective of Generalization\nAndrew Gordon Wilson, Pavel Izmailov. ArXiv preprint, 2020.\n\n[11] Stochastic Weight Averaging in Parallel: Large-Batch Training That Generalizes Well\nGupta, Vipul, Santiago Akle Serrano, and Dennis DeCoste; International Conference on Learning Representations (ICLR). 2019.\n\n[12] SQWA: Stochastic Quantized Weight Averaging for Improving the Generalization Capability of Low-Precision Deep Neural Networks\nShin, Sungho, Yoonho Boo, and Wonyong Sung; arXiv preprint 2020.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchRec, and other domain library updates in PyTorch 1.11\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---\n\nWe are introducing the beta release of TorchRec and a number of improvements to the current PyTorch domain libraries, alongside the [PyTorch 1.11 release](https://pytorch.org/blog/pytorch-1.11-released/). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- **TorchRec**, a PyTorch domain library for Recommendation Systems, is available in beta. [View it on GitHub](https://github.com/pytorch/torchrec).\n- **TorchAudio** - Added Enformer- and RNN-T-based models and recipes to support the full development lifecycle of a streaming ASR model. See the release notes [here](https://github.com/pytorch/audio/releases).\n- **TorchText** - Added beta support for RoBERTa and XLM-R models, byte-level BPE tokenizer, and text datasets backed by TorchData. See the release notes [here](https://github.com/pytorch/text/releases).\n- **TorchVision** - Added 4 new model families and 14 new classification datasets such as CLEVR, GTSRB, FER2013. See the release notes [here](https://github.com/pytorch/vision/releases).\n\n## TorchRec 0.1", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "## TorchRec 0.1\n\nWe [announced TorchRec](https://pytorch.org/blog/introducing-torchrec/) a few weeks ago and we are excited to release the beta version today. To recap, TorchRec is a PyTorch domain library for Recommendation Systems. This new library provides common sparsity and parallelism primitives, enabling researchers to build state-of-the-art personalization models and deploy them in production. TorchRec was used to train a 1.25 trillion parameter model, pushed to production in January 2022.\n\nIn particular, the library includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- Modeling primitives, such as embedding bags and jagged tensors, that enable easy authoring of large, performant multi-device/multi-node models using hybrid data-parallelism and model-parallelism.\n- Optimized RecSys kernels powered by [FBGEMM](https://github.com/pytorch/FBGEMM), including support for sparse and quantized operations.\n- A sharder which can partition embedding tables with a variety of different strategies including data-parallel, table-wise, row-wise, table-wise-row-wise, and column-wise sharding.\n- A planner which can automatically generate optimized sharding plans for models.\n- Pipelining to overlap dataloading device transfer (copy to GPU), inter-device communications (input_dist), and computation (forward, backward) for increased performance.\n- GPU inference support.\n- Common modules for RecSys, such as models and public datasets (Criteo & Movielens).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Please check the TorchRec announcement post [here](https://pytorch.org/blog/introducing-torchrec/), [video tutorial](https://www.youtube.com/watch?v=cjgj41dvSeQ), install instructions [here](https://github.com/pytorch/torchrec#readme), test drive the feature through this tutorial [here](https://pytorch.org/tutorials/intermediate/torchrec_tutorial.html), and refer to the reference document [here](https://pytorch.org/torchrec/).\n\n## TorchAudio 0.11\n\n#### TorchAudio: Building Blocks for Audio and Speech Processing\n\nWe published a paper, [TorchAudio: Building Blocks for Audio and Speech Processing](https://arxiv.org/abs/2110.15018), describing the overview of the TorchAudio library. If you find TorchAudio useful for your research, please help us share with the community by citing our paper.\n\n#### (Beta) RNN-T & (Prototype) Emformer Models and Recipes\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Emformer is an efficient memory-transformer-based streaming acoustic model that has demonstrated state-of-the-art streaming automatic speech recognition (ASR) performance in low-latency, resource-constrained scenarios, such as on-device applications (citation: [https://arxiv.org/abs/2010.10759](https://arxiv.org/abs/2010.10759)).\n\nThe TorchAudio v0.11 release includes the following beta features:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- Implementation of Emformer ([docs](https://pytorch.org/audio/main/models.html#emformer))\n- Recurrent neural network transducer (RNN-T) streaming ASR model that uses Emformer for its transcription network ([docs](https://pytorch.org/audio/main/models.html#rnn-t))\n- RNN-T beam search decoder with TorchScript support ([docs](https://pytorch.org/audio/main/models.html#rnntbeamsearch))\n- LibriSpeech Emformer RNN-T training recipe ([GitHub](https://github.com/pytorch/audio/tree/release/0.11/examples/asr/librispeech_emformer_rnnt)) and corresponding pre-trained streaming ASR inference pipeline ([docs](https://pytorch.org/audio/main/pipelines.html#emformer-rnnt-base-librispeech))\n\nAlso there are prototype features that are available from nightly builds or the main branch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- Training recipes trained on MuST-C and TED-LIUM3 datasets. ([GitHub](https://github.com/pytorch/audio/tree/main/examples/asr/emformer_rnnt))\n- Pre-trained pipelines corresponding to the recipes. ([docs](https://pytorch.org/audio/main/prototype.pipelines.html))\n- Tutorial that steps through performing online speech recognition with RNN-T Emformer model. ([docs](https://pytorch.org/audio/main/tutorials/online_asr_tutorial.html))\n\nCollectively, these features cover the full development lifecycle of a streaming ASR model, from definition through training and inference, and enable users to easily develop their own Emformer- and RNN-T-based models.\n\nSpecial thanks to Yangyang Shi, Jay Mahadeokar, and Gil Keren for their code contributions and guidance.\n\n#### (Beta) HuBERT Pretrain Model", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "The masked prediction training of HuBERT model requires the masked logits, unmasked logits, and feature norm as the outputs. The logits are for cross-entropy losses and the feature norm is for penalty loss. The release adds [HuBERTPretrainModel](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L120-L205) and corresponding factory functions ([hubert_pretrain_base](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L964-L1027), [hubert_pretrain_large](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L1030-L1090), and [hubert_pretrain_xlarge](https://github.com/pytorch/audio/blob/main/torchaudio/models/wav2vec2/model.py#L1093-L1153)) to enable training from scratch.\n\n#### (Prototype) CTC Beam Search Decoder\n\nIn recent releases, TorchAudio has added support for ASR models fine-tuned on CTC loss. The addition of an inference time CTC beam search decoder enables running end-to-end ASR evaluation using TorchAudio utils.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "The CTC decoder in TorchAudio supports customizable beam search decoding with lexicon constraint. It also has optional KenLM language model support.\n\nFor more details, please check out the [API tutorial](https://pytorch.org/audio/main/tutorials/asr_inference_with_ctc_decoder_tutorial.html) and [documentation](https://pytorch.org/audio/main/prototype.ctc_decoder.html). This prototype feature is available through nightly builds.\n\n#### (Prototype) Streaming API\n\nTorchAudio started as simple audio I/O APIs that supplement PyTorch. With the recent addition of ASR models and training recipes, the project has received requests to support high-level application development.\n\nStreaming API makes it easy to develop and test the model in online inference. It utilizes ffmpeg under the hood, and enables reading media from online services and hardware devices, decoding media in an incremental manner, and applying filters and preprocessing.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Please checkout the [API tutorial](https://pytorch.org/audio/main/tutorials/streaming_api_tutorial.html) and [the documentation](https://pytorch.org/audio/main/prototype.io.html). There are also the [streaming ASR](https://pytorch.org/audio/main/tutorials/online_asr_tutorial.html) tutorial and the [device streaming ASR tutorial](https://pytorch.org/audio/main/tutorials/device_asr.html). This feature is available from nightly releases. Please refer to [pytorch.org](https://pytorch.org/get-started/locally/) for how to install nightly builds.\n\n## TorchText 0.12\n\n#### (Beta) RoBERTa and XLM-R Models\n\nTorchText has added support for pre-trained RoBERTa and XLM-R models. It would allow users to train end-2-end Transformer Encoder based models on standard NLP tasks using TorchText.\n\nMore specifically:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "More specifically:\n\n- The models are torchscriptable and hence can be employed for production use-cases.\n- The model APIs let users to easily attach custom task-specific heads with pre-trained encoders.\n- The API also comes equipped with data pre-processing transforms to match the pre-trained weights and model configuration.\n\nWe have added a [tutorial](https://pytorch.org/text/main/tutorials/sst2_classification_non_distributed.html) to demonstrate SST-2 binary text classification task with pre-trained XLM-R base architecture.\n\nFor additional details on model APIs and usage examples, please refer to the [documentation](https://pytorch.org/text/main/models.html).\n\n#### (Beta) byte-level BPE tokenizer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "TorchText has added support for a Byte-Level BPE tokenizer, as used in GPT-2. This tokenizer is also used for tokenizing inputs to the pre-trained RoBERTa models described previously. In addition to the RoBERTa vocab, users can also load their own custom BPE vocab to use the tokenizer. Furthermore, the tokenizer is fully torchscriptable and hence can be employed for production use-cases. For additional details on model APIs and usage examples, please refer to the [documentation](https://pytorch.org/text/main/transforms.html#gpt2bpetokenizer).\n\n#### (Beta) Text datasets backed by TorchData\n\nTorchText has modernized its datasets by migrating from older-style Iterable Datasets to [TorchData\u2019s](https://github.com/pytorch/data#readme) DataPipes. TorchData is a library that provides modular/composable primitives, allowing users to load and transform data in performant data pipelines.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "These DataPipes work out-of-the-box with PyTorch DataLoader and would enable new functionalities like auto-sharding. Users can now easily do data manipulation and pre-processing using user-defined functions and transformations in a functional style programming. Datasets backed by DataPipes also enable standard flow-control like batching, collation, shuffling and bucketizing.\n\nCollectively, DataPipes provides a comprehensive experience for data preprocessing and tensorization needs in a pythonic and flexible way for model training. We have added a [tutorial](https://pytorch.org/text/main/tutorials/sst2_classification_non_distributed.html) to demonstrate data-processing pipelining using the modernized dataset for binary text-classification.\n\nYou can learn more about TorchData DataPipe APIs in its [official documentation](https://pytorch.org/data).\n\n## TorchVision 0.12\n\n### New Models\n\nFour new model families have been released in the latest version along with pre-trained weights for their variants.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "#### #1 Object Detection\n\n[FCOS](https://arxiv.org/pdf/1904.01355.pdf) is a popular, fully convolutional, anchor-free model for object detection. In this release we include a community-contributed model implementation as well as pre-trained weights. The model was trained on COCO train2017 and can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models\n\nx = [torch.rand(3, 224, 224)]\nfcos = models.detection.fcos_resnet50_fpn(pretrained=True).eval()\npredictions = fcos(x)\n```\n\nThe box AP of the pre-trained model on COCO val2017 is 39.2 (see [#4961](https://github.com/pytorch/vision/pull/4961) for more details).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We would like to thank [Hu Ye](https://github.com/xiaohu2015) and [Zhiqiang Wang](https://github.com/zhiqwang) for contributing to the model implementation and initial training. This was the first community-contributed model in a long while, and given its success, we decided to use the learnings from this process and create a new [model contribution guidelines](https://github.com/pytorch/vision/blob/main/CONTRIBUTING_MODELS.md).\n\n#### #2 Optical Flow support and RAFT model\n\nTorchVision now supports optical flow! Optical Flow models try to predict movement in a video: given two consecutive frames, the model predicts where each pixel of the first frame ends up in the second frame. Check out our [new tutorial on Optical Flow](https://pytorch.org/vision/0.12/auto_examples/plot_optical_flow.html#sphx-glr-auto-examples-plot-optical-flow-py)!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We implemented a torchscript-compatible [RAFT](https://arxiv.org/abs/2003.12039) model with pre-trained weights (both normal and \u201csmall\u201d versions), and added support for [training and evaluating](https://github.com/pytorch/vision/tree/main/references/optical_flow) optical flow models. Our training scripts support distributed training across processes and nodes, leading to much faster training time than the original implementation. We also added 5 new [optical flow datasets](https://pytorch.org/vision/0.12/datasets.html#optical-flow): Flying Chairs, Flying Things, Sintel, Kitti, and HD1K.\n\n

\n \n

\n\n#### #3. Image Classification", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "[Vision Transformer](https://arxiv.org/abs/2010.11929) (ViT) and [ConvNeXt](https://arxiv.org/abs/2201.03545) are two popular architectures which can be used as image classifiers or as backbones for downstream vision tasks. In this release we include 8 pre-trained weights for their classification variants. The models were trained on ImageNet and can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models\n\nx = torch.rand(1, 3, 224, 224)\nvit = models.vit_b_16(pretrained=True).eval()\nconvnext = models.convnext_tiny(pretrained=True).eval()\npredictions1 = vit(x)\npredictions2 = convnext(x)\n```\n\nThe accuracies of the pre-trained models obtained on ImageNet val are seen below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "| **Model** | **Acc@1** | **Acc@5** |\n| -------------- | --------: | --------: |\n| vit_b_16 | 81.072 | 95.318 |\n| vit_b_32 | 75.912 | 92.466 |\n| vit_l_16 | 79.662 | 94.638 |\n| vit_l_32 | 76.972 | 93.07 |\n| convnext_tiny | 82.52 | 96.146 |\n| convnext_small | 83.616 | 96.65 |\n| convnext_base | 84.062 | 96.87 |\n| convnext_large | 84.414 | 96.976 |\n\nThe above models have been trained using an adjusted version of our new [training recipe](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and this allows us to offer models with accuracies significantly higher than the ones on the original papers.\n\n#### #4. GPU Video Decoding\n\nIn this release, we add support for GPU video decoding in the video reading API. To use hardware-accelerated decoding, we just need to pass a cuda device to the video reading API as shown below:\n\n```python\nimport torchvision", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "reader = torchvision.io.VideoReader(file_name, device=\"cuda:0\")\nfor frame in reader:\n print(frame)\n```\n\nWe also support seeking to anyframe or a keyframe in the video before reading, as shown below:\n\n```python\nreader.seek(seek_time)\n```\n\n### New Datasets\n\nWe have implemented 14 new [classification datasets](https://pytorch.org/vision/0.12/datasets.html#image-classification): CLEVR, GTSRB, FER2013, SUN397, Country211, Flowers102, fvgc_aircraft, OxfordIIITPet, DTD, Food 101, Rendered SST2, Stanford cars, PCAM, and EuroSAT.\n\nAs part of our work on Optical Flow support (see above for more details), we also added 5 new [optical flow datasets](https://pytorch.org/vision/0.12/datasets.html#optical-flow): Flying Chairs, Flying Things, Sintel, Kitti, and HD1K.\n\n### Other Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- **New documentation layout**: Each function / class is now documented in a separate page, clearing up some space in the per-module pages, and easing the discovery of the proposed APIs. Compare e.g. our [previous docs](https://pytorch.org/vision/0.11/transforms.html) vs the [new ones](https://pytorch.org/vision/0.12/transforms.html). Please let us know if you have any [feedback](https://github.com/pytorch/vision/issues/5511)!\n- **New [model contribution guidelines](https://github.com/pytorch/vision/blob/main/CONTRIBUTING_MODELS.md)** have been published following the success of the [FCOS](https://github.com/pytorch/vision/pull/4961) model which was contributed by the community. These guidelines aim to be an overview of the model contribution process for anyone who would like to suggest, implement and train a new model.\n- **Upcoming Prototype API** - We are currently working on a prototype API which adds Multi-weight support on all of our model builder methods. This will enable us to offer multiple pre-trained weights, associated with their meta-data and inference transforms. The API is still under review and thus was not included in the release but you can read more about it on our [blogpost](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) and provide your feedback on the dedicated [Github issue](https://github.com/pytorch/vision/issues/5088).\n- **Changes in our deprecation policy** - Up until now, torchvision would almost never remove deprecated APIs. In order to be more aligned and consistent with pytorch core, we are updating our deprecation policy. We are now following a 2-release deprecation cycle: deprecated APIs will raise a warning for 2 versions, and will be removed after that. To reflect these changes and to smooth the transition, we have decided to:\n - Remove all APIs that had been deprecated before or on v0.8, released 1.5 years ago.\n - Update the removal timeline of all other deprecated APIs to v0.14, to reflect the new 2-cycle policy starting now in v0.12.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Captum 0.5\n\n[Captum](https://captum.ai/) is a PyTorch library for model interpretability. For this release, we expanded Captum with influential instances and added support for both similarity based influences and novel algorithms, [TracIn](https://arxiv.org/abs/2002.08484) and its variants. TracIn variants offer faster approximation of influence scores based on random projections for fully connected layers.\n\nMore specifically the new, influence, subsection of Captum includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "- **[SimilarityInfluence](https://captum.ai/api/influence.html#similarityinfluence)** computes similarity scores between test and training examples using default (cosine or euclidean) or custom user definite metrics w.r.t. given input model layers.\n- **[TracInCP](https://captum.ai/api/influence.html#tracincp)** approximates the influential score of each training example on a given test example based on the dot-product similarity between loss gradients w.r.t. model parameters for test and training examples. Note that if we use training examples as test examples then we compute self influence. This method and its variants described below also return top-k proponents and opponents which are the top-k largest positive and negative influential examples respectively.\n- **[TracInCPFast](https://captum.ai/api/influence.html#tracincpfast)** is an approximation of TracInCP that avoids computing the gradients w.r.t. large parameter matrices. It approximates influence score based on the dot products between last fully connected layer activations and loss gradients w.r.t. that layer for training and test examples.\n- **[TracInCPFastRandProj](https://captum.ai/api/influence.html#tracincpfastrandproj)** uses a nearest neighbor approximation library such as annoy to compute the dot product between the training and test quantities. In order to reduce the dimensionality of layer activations and corresponding gradients this method, in addition, allows to project those vectors into a lower dimensional space using random projection matrices.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "More about the implementation of influential instances can be found on our [GitHub](https://github.com/pytorch/captum/tree/master/captum/influence) page and [tutorials](https://captum.ai/tutorials/TracInCP_Tutorial).\n\nThanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "
\n
\n
\n \n
\n
\n
\n", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-new-library-releases/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 2.0 & XLA\u2014The Latest Cutting Edge Features\"\nauthor: Jack Cao, Milad Mohammadi, Alex Wertheim, Yeounoh Chung, Joe Spisak, Will Cromar, Shauheen Zahirazami\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Today, we are excited to share our latest work for [PyTorch/XLA 2.0](https://github.com/pytorch/xla/releases/tag/v2.0.0). The release of [PyTorch 2.0](https://pytorch.org/get-started/pytorch-2.0/) is yet another major milestone for this storied community and we are excited to continue to be part of it. When the [PyTorch/XLA](https://github.com/pytorch/xla) project started in 2018 between Google and Meta, the focus was on bringing cutting edge Cloud TPUs to help support the PyTorch community. Along the way,", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "others in the community such as Amazon joined the project and very quickly the community expanded. We are excited about XLA's [direction](https://opensource.googleblog.com/2023/03/openxla-is-ready-to-accelerate-and-simplify-ml-development.html) and the benefits this project continues to bring to the PyTorch community. In this blog we\u2019d like to showcase some key features that have been in development, show code snippets, and illustrate the benefit through some benchmarks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "TorchDynamo / torch.compile (Experimental)\n\n[TorchDynamo](https://github.com/pytorch/torchdynamo) (Dynamo) is a Python-level JIT compiler designed to make unmodified PyTorch programs faster. It provides a clean API for compiler backends to hook in; its biggest feature is to dynamically modify Python bytecode just before execution. In the PyTorch/XLA 2.0 release, an experimental backend for Dynamo is provided for both inference and training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Dynamo provides a [Torch FX](https://pytorch.org/docs/stable/fx.html) (FX) graph when it recognizes a model pattern and PyTorch/XLA uses a Lazy Tensor approach to compile the FX graph and return the compiled function. To get more insight regarding the technical details about PyTorch/XLA\u2019s dynamo implementation, check out [this](https://dev-discuss.pytorch.org/t/torchdynamo-update-10-integrating-with-pytorch-xla-for-inference-and-training/935) dev-discuss post and [dynamo", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "doc](https://github.com/pytorch/xla/blob/r2.0/docs/dynamo.md).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Here is a small code example of running ResNet18 with `torch.compile`:\n\n```\nimport torch\nimport torchvision\nimport torch_xla.core.xla_model as xm\n\ndef eval_model(loader):\n device = xm.xla_device()\n xla_resnet18 = torchvision.models.resnet18().to(device)\n xla_resnet18.eval()\n dynamo_resnet18 = torch.compile(\n xla_resnet18, backend='torchxla_trace_once')\n for data, _ in loader:\n output = dynamo_resnet18(data)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "With `torch.compile` PyTorch/XLA only traces the ResNet18 model once during the init time and executes the compiled binary everytime `dynamo_resnet18` is invoked, instead of tracing the model every step. To illustrate the benefits of Dynamo+XLA, below is an inference speedup analysis to compare Dynamo and LazyTensor (without Dynamo) using TorchBench on a Cloud TPU v4-8 where the y-axis is the speedup multiplier.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "![Inference Speedup - PyTorch/XLA Dynamo on TPU](/assets/images/2023-03-22-inferencespeedup.svg){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Dynamo for training is in the development stage with its implementation being at an earlier stage than inference. Developers are welcome to test this early feature, however, in the 2.0 release, PyTorch/XLA supports the forward and backward pass graphs and not the optimizer graph; the optimizer graph is available in the nightly builds and will land in the PyTorch/XLA 2.1 release. Below is an example of what training looks like using the ResNet18 example with `torch.compile`:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "```\nimport torch\nimport torchvision\nimport torch_xla.core.xla_model as xm\n\ndef train_model(model, data, target):\n loss_fn = torch.nn.CrossEntropyLoss()\n pred = model(data)\n loss = loss_fn(pred, target)\n loss.backward()\n return pred", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "def train_model_main(loader):\n device = xm.xla_device()\n xla_resnet18 = torchvision.models.resnet18().to(device)\n xla_resnet18.train()\n dynamo_train_model = torch.compile(\n train_model, backend='aot_torchxla_trace_once')\n for data, target in loader:\n output = dynamo_train_model(xla_resnet18, data, target)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Note that the backend for training is `aot_torchxla_trace_once` (API will be updated for stable release) whereas the inference backend is `torchxla_trace_once` (name subject to change). We expect to extract and execute 3 graphs per training step instead of 1 training step if you use the Lazy tensor. Below is a training speedup analysis to compare Dynamo and Lazy using the TorchBench on Cloud TPU v4-8.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "![Training Speedup - PyTorch/XLA Dynamo on TPU](/assets/images/2023-03-22-trainingspeedup.svg){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "PJRT Runtime (Beta)\n\nPyTorch/XLA is migrating from XRT to the new PJRT runtime. PJRT is a better-maintained stack, with demonstrated performance advantages, including, on average, a 35% performance for training on TorchBench 2.0 models. It also supports a richer set of features enabling technologies like SPMD. In the PyTorch/XLA 2.0 release, PJRT is the default runtime for TPU and CPU; GPU support is in experimental state. The PJRT features included in the PyTorch/XLA 2.0 release are:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "* TPU runtime implementation in `libtpu` using the [PJRT Plugin API](https://github.com/openxla/community/blob/main/rfcs/20230123-pjrt-plugin.md#rfc-openxla-pjrt-plugin) improves performance by up to 30%\n* `torch.distributed` support for TPU v2 and v3, including `pjrt://` `init_method` (Experimental)\n* Single-host GPU support. Multi-host support coming soon. (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Switching to PJRT requires no change (or minimal change for GPUs) to user code (see [pjrt.md](https://github.com/pytorch/xla/blob/master/docs/pjrt.md) for more details). Runtime configuration is as simple as setting the `PJRT_DEVICE` environment variable to the local device type (i.e. `TPU`, `GPU`, `CPU`). Below are examples of using PJRT runtimes on different devices. \n\n```\n# TPU Device\nPJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "```\n# TPU Pod Device\ngcloud alpha compute tpus tpu-vm ssh $USER-pjrt --zone=us-central2-b --project=$PROJECT --worker=all --command=\"git clone --depth=1 --branch r2.0 https://github.com/pytorch/xla.git\"\n\ngcloud alpha compute tpus tpu-vm ssh $USER-pjrt --zone=us-central2-b --project=$PROJECT --worker=all --command=\"PJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1\"", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "```\n# GPU Device (Experimental)\nPJRT_DEVICE=GPU GPU_NUM_DEVICES=4 python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=128 --num_epochs=1\n```\n\nBelow is a performance comparison between XRT and PJRT by task on TorchBench 2.0 on v4-8 TPU. To learn more about PJRT vs. XRT please review the [documentation](https://github.com/pytorch/xla/blob/r2.0/docs/pjrt.md#tpu).\n\n\n![TorchBench Training Time](/assets/images/2023-03-22-torchbenchtraining.svg){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Parallelization", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "GSPMD (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "We are delighted to introduce General and Scalable Parallelization for ML Computation Graphs ([GSPMD](https://arxiv.org/abs/2105.04663)) in PyTorch as a new experimental data & model sharding solution. [GSPMD](https://arxiv.org/abs/2105.04663) provides automatic parallelization for common ML workloads, allowing developers to write PyTorch programs as if on a single large device and without custom sharded computation ops and/or collective communication ops. The XLA compiler transforms the single device", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "program into a partitioned one with proper collectives, based on the user provided sharding hints. The API ([RFC](https://github.com/pytorch/xla/issues/3871)) will be available in the PyTorch/XLA 2.0 release as an experimental feature on a single TPU VM host.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Next Steps for GSPMD\n\nGSPMD is experimental in 2.0 release. To bring it to Stable status, we plan to address a number of feature gaps and known issues in the following releases, including multi-host support, DTensor integration, partial replication sharding, asynchronous data loading, and checkpointing.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "FSDP (Beta)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "PyTorch/XLA [introduced](https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/) fully sharded data parallel (FSDP) experimental support in version 1.12. This feature is a parallel representation of PyTorch FSDP and there are subtle differences in how XLA and upstream CUDA kernels are set up. `auto_wrap_policy` is a new argument that enables developers to automatically specify conditions for propagating partitioning specifications to neural network submodules. `auto_wrap_policy`s may be", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "simply passed in as an argument when wrapping a model with FSDP. Two `auto_wrap_policy` callables worth noting are: `size_based_auto_wrap_policy`, `transformer_auto_wrap_policy`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "`size_based_auto_wrap_policy` enables users to wrap submodules with a minimum number of parameters. The example below wraps model submodules having at least 10M parameters.\n\n```\nauto_wrap_policy = partial(size_based_auto_wrap_policy, min_num_params=1e7)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "`transformer_auto_wrap_policy` enables users to wrap all submodules that match a specific layer type. The example below wraps model submodules named `torch.nn.Conv2d`. To learn more, review [this ResNet example](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet_fsdp.py#L237-L255) by Ronghang Hu.\n\n```\nauto_wrap_policy = partial(transformer_auto_wrap_policy, transformer_layer_cls={torch.nn.Conv2d})", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "Today, we are excited to share our latest work for [PyTorch/XLA 2.0](https://github.com/pytorch/xla/releases/tag/v2.0.0). The release of [PyTorch 2.0](https://pytorch.org/get-started/pytorch-2.0/) is yet another major milestone for this storied community and we are excited to continue to be part of it. When the [PyTorch/XLA](https://github.com/pytorch/xla) project started in 2018 between Google and Meta, the focus was on bringing cutting edge Cloud TPUs to help support the PyTorch community. Along the way, others in the community such as Amazon joined the project and very quickly the community expanded. We are excited about XLA's [direction](https://opensource.googleblog.com/2023/03/openxla-is-ready-to-accelerate-and-simplify-ml-development.html) and the benefits this project continues to bring to the PyTorch community. In this blog we\u2019d like to showcase some key features that have been in development, show code snippets, and illustrate the benefit through some benchmarks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "## TorchDynamo / torch.compile (Experimental)\n\n[TorchDynamo](https://github.com/pytorch/torchdynamo) (Dynamo) is a Python-level JIT compiler designed to make unmodified PyTorch programs faster. It provides a clean API for compiler backends to hook in; its biggest feature is to dynamically modify Python bytecode just before execution. In the PyTorch/XLA 2.0 release, an experimental backend for Dynamo is provided for both inference and training. \n\nDynamo provides a [Torch FX](https://pytorch.org/docs/stable/fx.html) (FX) graph when it recognizes a model pattern and PyTorch/XLA uses a Lazy Tensor approach to compile the FX graph and return the compiled function. To get more insight regarding the technical details about PyTorch/XLA\u2019s dynamo implementation, check out [this](https://dev-discuss.pytorch.org/t/torchdynamo-update-10-integrating-with-pytorch-xla-for-inference-and-training/935) dev-discuss post and [dynamo doc](https://github.com/pytorch/xla/blob/r2.0/docs/dynamo.md).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "Here is a small code example of running ResNet18 with `torch.compile`:\n\n```\nimport torch\nimport torchvision\nimport torch_xla.core.xla_model as xm\n\ndef eval_model(loader):\n device = xm.xla_device()\n xla_resnet18 = torchvision.models.resnet18().to(device)\n xla_resnet18.eval()\n dynamo_resnet18 = torch.compile(\n xla_resnet18, backend='torchxla_trace_once')\n for data, _ in loader:\n output = dynamo_resnet18(data)\n```\n\nWith `torch.compile` PyTorch/XLA only traces the ResNet18 model once during the init time and executes the compiled binary everytime `dynamo_resnet18` is invoked, instead of tracing the model every step. To illustrate the benefits of Dynamo+XLA, below is an inference speedup analysis to compare Dynamo and LazyTensor (without Dynamo) using TorchBench on a Cloud TPU v4-8 where the y-axis is the speedup multiplier.\n\n\n![Inference Speedup - PyTorch/XLA Dynamo on TPU](/assets/images/2023-03-22-inferencespeedup.svg){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "Dynamo for training is in the development stage with its implementation being at an earlier stage than inference. Developers are welcome to test this early feature, however, in the 2.0 release, PyTorch/XLA supports the forward and backward pass graphs and not the optimizer graph; the optimizer graph is available in the nightly builds and will land in the PyTorch/XLA 2.1 release. Below is an example of what training looks like using the ResNet18 example with `torch.compile`:\n\n```\nimport torch\nimport torchvision\nimport torch_xla.core.xla_model as xm\n\ndef train_model(model, data, target):\n loss_fn = torch.nn.CrossEntropyLoss()\n pred = model(data)\n loss = loss_fn(pred, target)\n loss.backward()\n return pred", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "def train_model_main(loader):\n device = xm.xla_device()\n xla_resnet18 = torchvision.models.resnet18().to(device)\n xla_resnet18.train()\n dynamo_train_model = torch.compile(\n train_model, backend='aot_torchxla_trace_once')\n for data, target in loader:\n output = dynamo_train_model(xla_resnet18, data, target)\n```\n\nNote that the backend for training is `aot_torchxla_trace_once` (API will be updated for stable release) whereas the inference backend is `torchxla_trace_once` (name subject to change). We expect to extract and execute 3 graphs per training step instead of 1 training step if you use the Lazy tensor. Below is a training speedup analysis to compare Dynamo and Lazy using the TorchBench on Cloud TPU v4-8.\n\n\n![Training Speedup - PyTorch/XLA Dynamo on TPU](/assets/images/2023-03-22-trainingspeedup.svg){:width=\"100%\"}\n\n\n\n## PJRT Runtime (Beta)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "PyTorch/XLA is migrating from XRT to the new PJRT runtime. PJRT is a better-maintained stack, with demonstrated performance advantages, including, on average, a 35% performance for training on TorchBench 2.0 models. It also supports a richer set of features enabling technologies like SPMD. In the PyTorch/XLA 2.0 release, PJRT is the default runtime for TPU and CPU; GPU support is in experimental state. The PJRT features included in the PyTorch/XLA 2.0 release are:\n\n* TPU runtime implementation in `libtpu` using the [PJRT Plugin API](https://github.com/openxla/community/blob/main/rfcs/20230123-pjrt-plugin.md#rfc-openxla-pjrt-plugin) improves performance by up to 30%\n* `torch.distributed` support for TPU v2 and v3, including `pjrt://` `init_method` (Experimental)\n* Single-host GPU support. Multi-host support coming soon. (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "Switching to PJRT requires no change (or minimal change for GPUs) to user code (see [pjrt.md](https://github.com/pytorch/xla/blob/master/docs/pjrt.md) for more details). Runtime configuration is as simple as setting the `PJRT_DEVICE` environment variable to the local device type (i.e. `TPU`, `GPU`, `CPU`). Below are examples of using PJRT runtimes on different devices. \n\n```\n# TPU Device\nPJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1\n```\n\n```\n# TPU Pod Device\ngcloud alpha compute tpus tpu-vm ssh $USER-pjrt --zone=us-central2-b --project=$PROJECT --worker=all --command=\"git clone --depth=1 --branch r2.0 https://github.com/pytorch/xla.git\"\n\ngcloud alpha compute tpus tpu-vm ssh $USER-pjrt --zone=us-central2-b --project=$PROJECT --worker=all --command=\"PJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1\"\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "```\n# GPU Device (Experimental)\nPJRT_DEVICE=GPU GPU_NUM_DEVICES=4 python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=128 --num_epochs=1\n```\n\nBelow is a performance comparison between XRT and PJRT by task on TorchBench 2.0 on v4-8 TPU. To learn more about PJRT vs. XRT please review the [documentation](https://github.com/pytorch/xla/blob/r2.0/docs/pjrt.md#tpu).\n\n\n![TorchBench Training Time](/assets/images/2023-03-22-torchbenchtraining.svg){:width=\"100%\"}\n\n\n\n## Parallelization\n\n\n### GSPMD (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "We are delighted to introduce General and Scalable Parallelization for ML Computation Graphs ([GSPMD](https://arxiv.org/abs/2105.04663)) in PyTorch as a new experimental data & model sharding solution. [GSPMD](https://arxiv.org/abs/2105.04663) provides automatic parallelization for common ML workloads, allowing developers to write PyTorch programs as if on a single large device and without custom sharded computation ops and/or collective communication ops. The XLA compiler transforms the single device program into a partitioned one with proper collectives, based on the user provided sharding hints. The API ([RFC](https://github.com/pytorch/xla/issues/3871)) will be available in the PyTorch/XLA 2.0 release as an experimental feature on a single TPU VM host. \n\n\n#### Next Steps for GSPMD", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "GSPMD is experimental in 2.0 release. To bring it to Stable status, we plan to address a number of feature gaps and known issues in the following releases, including multi-host support, DTensor integration, partial replication sharding, asynchronous data loading, and checkpointing. \n\n\n### FSDP (Beta)\n\nPyTorch/XLA [introduced](https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/) fully sharded data parallel (FSDP) experimental support in version 1.12. This feature is a parallel representation of PyTorch FSDP and there are subtle differences in how XLA and upstream CUDA kernels are set up. `auto_wrap_policy` is a new argument that enables developers to automatically specify conditions for propagating partitioning specifications to neural network submodules. `auto_wrap_policy`s may be simply passed in as an argument when wrapping a model with FSDP. Two `auto_wrap_policy` callables worth noting are: `size_based_auto_wrap_policy`, `transformer_auto_wrap_policy`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "`size_based_auto_wrap_policy` enables users to wrap submodules with a minimum number of parameters. The example below wraps model submodules having at least 10M parameters.\n\n```\nauto_wrap_policy = partial(size_based_auto_wrap_policy, min_num_params=1e7)\n```\n\n`transformer_auto_wrap_policy` enables users to wrap all submodules that match a specific layer type. The example below wraps model submodules named `torch.nn.Conv2d`. To learn more, review [this ResNet example](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet_fsdp.py#L237-L255) by Ronghang Hu.\n\n```\nauto_wrap_policy = partial(transformer_auto_wrap_policy, transformer_layer_cls={torch.nn.Conv2d})\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} {"page_content": "PyTorch/XLA FSDP is now integrated in HuggingFace trainer class ([PR](https://github.com/huggingface/transformers/pull/21406)) enabling users to train much larger models on PyTorch/XLA ([official Hugging Face documentation](https://huggingface.co/docs/transformers/main/en/main_classes/trainer#pytorchxla-fully-sharded-data-parallel)). A 16B parameters GPT2 model trained on Cloud TPU v4-64 with this FSDP configuration achieved 39% hardware utilization.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n
TPU Accelerator - Num Devices\n v4-64\n
GPT2 Parameter Count\n 16B\n
Layers Wrapped with FSDP\n GPT2Block\n
TFLOPs / Chip\n 275\n
PFLOPs / Step\n 50\n
Hardware Utilization\n 39%\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Differences Between FSDP & GSPMD\n\nFSDP is a data parallelism technique that reduces device memory footprint by storing model parameters, optimizer states, and gradients all sharded. Note that the actual computation is still local to the device and requires all-gathering the sharded model parameters for both forward and backward passes, hence the name \u201cdata parallel\u201d. FSDP is one of the newest additions to PyTorch/XLA to scale large model training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "GSPMD on the other hand, is a general parallelization system that enables various types of parallelisms, including both data and model parallelisms. PyTorch/XLA provides a sharding annotation API and XLAShardedTensor abstraction, so a user can annotate any tensor with sharding specs in the PyTorch program. Developers don\u2019t need to manually implement sharded computations or inject collective communications ops to get it right. The XLA compiler does the work so that each computation can run in a distributed", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "manner on multiple devices.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Examples & Preliminary Results\n\nTo learn about PyTorch/XLA parallelism sharding API, visit our [RFC](https://github.com/pytorch/xla/issues/3871) and see the [Sample Code](https://github.com/pytorch/xla/tree/r2.0/test/spmd) references. Below is a simple example to enable data and model parallelism.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "```\nmodel = SimpleLinear().to(xm.xla_device())\n# Sharding annotate the linear layer weights.\nxs.mark_sharding(model.fc1.weight, mesh, partition_spec)\n# Training loop\nmodel.train()\nfor step, (data, target) in enumerate(loader):\n optimizer.zero_grad()\n data = data.to(xm.xla_device())\n target = target.to(xm.xla_device())\n # Sharding annotate input data, we can shard any input\n # dimensions. Sharidng the batch dimension enables \n # data parallelism, sharding the feature dimension enables", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "# spatial partitioning.\n xs.mark_sharding(data, mesh, partition_spec)\n ouput = model(data)\n loss = loss_fn(output, target)\n optimizer.step()\n xm.mark_step()", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "The following graph highlights the memory efficiency benefits of PyTorch/XLA FSDP and SPMD on Cloud TPU v4-8 running ResNet50.\n\n\n![Batch Size Scaling with Spatial Partitioning](/assets/images/2023-03-22-batchsizescaling.svg){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Closing Thoughts\u2026", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "We are excited to bring these features to the PyTorch community, and this is really just the beginning. Areas like dynamic shapes, deeper support for OpenXLA and many others are in development and we plan to put out more blogs to dive into the details. PyTorch/XLA is developed fully open source and we invite you to join the community of developers by filing issues, submitting pull requests, and sending RFCs on [GitHub](github.com/pytorch/xla). You can try PyTorch/XLA on a variety of XLA devices including", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "TPUs and GPUs. [Here](https://colab.sandbox.google.com/github/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb) is how to get started.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "Congratulations again to the PyTorch community on this milestone!\n\nCheers,\n\nThe PyTorch Team at Google", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Compromised PyTorch-nightly dependency chain between December 25th and December 30th, 2022.\"\nauthor: The PyTorch Team\n---\n\nIf you installed PyTorch-nightly on Linux via pip between December 25, 2022 and December 30, 2022, please uninstall it and torchtriton immediately, and use the latest nightly binaries (newer than Dec 30th 2022).\n\n```bash\n$ pip3 uninstall -y torch torchvision torchaudio torchtriton\n$ pip3 cache purge", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "PyTorch-nightly Linux packages installed via pip during that time installed a dependency, torchtriton, which was compromised on the Python Package Index (PyPI) code repository and ran a malicious binary. This is what is known as a supply chain attack and directly affects dependencies for packages that are hosted on public package indices.\n\n**NOTE:** Users of the PyTorch **stable** packages **are not** affected by this issue.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "How to check if your Python environment is affected\n\nThe following command searches for the malicious binary in the torchtriton package (`PYTHON_SITE_PACKAGES/triton/runtime/triton`) and prints out whether your current Python environment is affected or not.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "```bash\npython3 -c \"import pathlib;import importlib.util;s=importlib.util.find_spec('triton'); affected=any(x.name == 'triton' for x in (pathlib.Path(s.submodule_search_locations[0] if s is not None else '/' ) / 'runtime').glob('*'));print('You are {}affected'.format('' if affected else 'not '))\"", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "The malicious binary is executed when the triton package is imported, which requires explicit code to do and is not PyTorch\u2019s default behavior.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "The Background", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "At around 4:40pm GMT on December 30 (Friday), we learned about a malicious dependency package (`torchtriton`) that was uploaded to the Python Package Index (PyPI) code repository with the same package name as the one we ship on the [PyTorch nightly package index](https://download.pytorch.org/whl/nightly). Since the [PyPI index takes precedence](https://github.com/pypa/pip/issues/8606), this malicious package was being installed instead of the version from our official repository. This design enables", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "somebody to register a package by the same name as one that exists in a third party index, and pip will install their version by default.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "This malicious package has the same name `torchtriton` but added in code that uploads sensitive data from the machine.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "What we know\n\ntorchtriton on PyPI contains a malicious triton binary which is installed at `PYTHON_SITE_PACKAGES/triton/runtime/triton`. Its SHA256 hash is listed below.\n\n`SHA256(triton)= 2385b29489cd9e35f92c072780f903ae2e517ed422eae67246ae50a5cc738a0e`\n\nThe binary\u2019s main function does the following:", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "- Get system information\n - nameservers from `/etc/resolv.conf`\n - hostname from `gethostname()`\n - current username from `getlogin()`\n - current working directory name from `getcwd()`\n - environment variables\n- Read the following files\n - `/etc/hosts`\n - `/etc/passwd`\n - The first 1,000 files in `$HOME/*`\n - `$HOME/.gitconfig`\n - `$HOME/.ssh/*`\n- Upload all of this information, including file contents, via encrypted DNS queries to the domain *.h4ck[.]cfd, using the DNS server wheezy[.]io", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "The binary\u2019s file upload functionality is limited to files less than 99,999 bytes in size. It also uploads only the first 1,000 files in $HOME (but all files < 99,999 bytes in the .ssh directory).", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "Steps taken towards mitigation", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "- torchtriton has been removed as a dependency for our nightly packages and replaced with pytorch-triton ([pytorch/pytorch#91539](https://github.com/pytorch/pytorch/pull/91539)) and a dummy package registered on PyPI (so that this issue doesn\u2019t repeat)\n- All nightly packages that depend on torchtriton have been removed from our package indices at https://download.pytorch.org until further notice", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "- We have reached out to the PyPI security team to get proper ownership of the `torchtriton` package on PyPI and to delete the malicious version", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Running PyTorch Models on Jetson Nano'\nauthor: Jeff Tang, Hamid Shojanazeri, Geeta Chauhan\nfeatured-img: 'assets/images/pytorch-logo.jpg'\n---", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Overview\nNVIDIA [Jetson Nano](https://developer.nvidia.com/embedded/jetson-nano-developer-kit), part of the [Jetson family of products](https://developer.nvidia.com/embedded/jetson-modules) or Jetson modules, is a small yet powerful Linux (Ubuntu) based embedded computer with 2/4GB GPU. With it, you can run many PyTorch models efficiently. This document summarizes our experience of running different deep learning models using 3 different mechanisms on Jetson Nano:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "1. Jetson Inference the higher-level NVIDIA API that has built-in support for running most common computer vision models which can be transfer-learned with PyTorch on the Jetson platform.\n\n 2. TensorRT, an SDK for high-performance inference from NVIDIA that requires the conversion of a PyTorch model to ONNX, and then to the TensorRT engine file that the TensorRT runtime can run.\n\n 3. PyTorch with the direct PyTorch API `torch.nn` for inference.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Setting up Jetson Nano", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "After purchasing a Jetson Nano [here](https://developer.nvidia.com/buy-jetson?product=jetson_nano&location=US), simply follow the clear step-by-step [instructions](https://developer.nvidia.com/embedded/learn/get-started-jetson-nano-devkit) to download and write the Jetson Nano Developer Kit SD Card Image to a microSD card, and complete the setup. After the setup is done and the Nano is booted, you\u2019ll see the standard Linux prompt along with the username and the Nano name used in the setup.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "To check the GPU status on Nano, run the following commands:\n\n```\nsudo pip3 install jetson-stats\nsudo jtop", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "You\u2019ll see information, including:\n\n
\n \n
\n\nYou can also see the installed CUDA version:\n\n```\n$ ls -lt /usr/local\nlrwxrwxrwx 1 root root 22 Aug 2 01:47 cuda -> /etc/alternatives/cuda\nlrwxrwxrwx 1 root root 25 Aug 2 01:47 cuda-10 -> /etc/alternatives/cuda-10\ndrwxr-xr-x 12 root root 4096 Aug 2 01:47 cuda-10.2", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "To use a camera on Jetson Nano, for example, Arducam 8MP IMX219, follow the instructions [here](https://www.arducam.com/docs/camera-for-jetson-nano/mipi-camera-modules-for-jetson-nano/driver-installation/) or run the commands below after [installing a camera module](https://developer.nvidia.com/embedded/learn/jetson-nano-2gb-devkit-user-guide#id-.JetsonNano2GBDeveloperKitUserGuidevbatuu_v1.0-Camera):", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "```\ncd ~\nwget https://github.com/ArduCAM/MIPI_Camera/releases/download/v0.0.3/install_full.sh\nchmod +x install_full.sh\n./install_full.sh -m arducam", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Another way to do this is to use the original Jetson Nano camera driver:\n\n```\nsudo dpkg -r arducam-nvidia-l4t-kernel\nsudo shutdown -r now\n```\n\nThen, use ls /dev/video0 to confirm the camera is found:\n\n```\n$ ls /dev/video0\n/dev/video0\n```\n\nAnd finally, the following command to see the camera in action:\n\n```\nnvgstcapture-1.0 --orientation=2\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Using Jetson Inference\nNVIDIA [Jetson Inference](https://github.com/dusty-nv/jetson-inference) API offers the easiest way to run image recognition, object detection, semantic segmentation, and pose estimation models on Jetson Nano. Jetson Inference has TensorRT built-in, so it\u2019s very fast. \n\nTo test run Jetson Inference, first clone the repo and download the models:\n\n```\ngit clone --recursive https://github.com/dusty-nv/jetson-inference\ncd jetson-inference", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Then use the pre-built [Docker Container](https://github.com/dusty-nv/jetson-inference/blob/master/docs/jetpack-setup-2.md) that already has PyTorch installed to test run the models:\n\n```\ndocker/run.sh --volume ~/jetson_inference:/jetson_inference", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "To run image recognition, object detection, semantic segmentation, and pose estimation models on test images, use the following:\n\n```\ncd build/aarch64/bin\n./imagenet.py images/jellyfish.jpg /jetson_inference/jellyfish.jpg\n./segnet.py images/dog.jpg /jetson_inference/dog.jpeg\n./detectnet.py images/peds_0.jpg /jetson_inference/peds_0.jpg\n./posenet.py images/humans_0.jpg /jetson_inference/pose_humans_0.jpg", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Four result images from running the four different models will be generated. Exit the docker image to see them:\n\n```\n$ ls -lt ~/jetson_inference/\n-rw-r--r-- 1 root root 68834 Oct 15 21:30 pose_humans_0.jpg\n-rw-r--r-- 1 root root 914058 Oct 15 21:30 peds_0.jpg\n-rw-r--r-- 1 root root 666239 Oct 15 21:30 dog.jpeg\n-rw-r--r-- 1 root root 179760 Oct 15 21:29 jellyfish.jpg", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "
\n \"Using\n \"Using\n
", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "
\n \"Using\n \"Using\n
\n\nYou can also use the docker image to run PyTorch models because the image has PyTorch, torchvision and torchaudio installed:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "```\n# pip list|grep torch\ntorch (1.9.0)\ntorchaudio (0.9.0a0+33b2469)\ntorchvision (0.10.0a0+300a8a4)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Although Jetson Inference includes models already converted to the TensorRT engine file format, you can fine-tune the models by following the steps in Transfer Learning with PyTorch (for Jetson Inference) [here](https://github.com/dusty-nv/jetson-inference/blob/master/docs/pytorch-transfer-learning.md).", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Using TensorRT\n[TensorRT](https://docs.nvidia.com/deeplearning/tensorrt/) is an SDK for high-performance inference from NVIDIA. Jetson Nano supports TensorRT via the Jetpack SDK, included in the SD Card image used to set up Jetson Nano. To confirm that TensorRT is already installed in Nano, `run dpkg -l|grep -i tensorrt`:\n\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Theoretically, TensorRT can be used to \u201ctake a trained PyTorch model and optimize it to run more efficiently during inference on an NVIDIA GPU.\u201d Follow the instructions and code in the [notebook](https://github.com/NVIDIA/TensorRT/blob/master/quickstart/IntroNotebooks/4.%20Using%20PyTorch%20through%20ONNX.ipynb) to see how to use PyTorch with TensorRT through ONNX on a torchvision Resnet50 model:\n\n1. How to convert the model from PyTorch to ONNX;\n\n2. How to convert the ONNX model to a TensorRT engine file;", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "3. How to run the engine file with the TensorRT runtime for performance improvement: inference time improved from the original 31.5ms/19.4ms (FP32/FP16 precision) to 6.28ms (TensorRT).", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "You can replace the Resnet50 model in the notebook code with another PyTorch model, go through the conversion process above, and run the finally converted model TensorRT engine file with the TensorRT runtime to see the optimized performance. But be aware that due to the Nano GPU memory size, models larger than 100MB are likely to fail to run, with the following error information:\n\n`Error Code 1: Cuda Runtime (all CUDA-capable devices are busy or unavailable)`", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "You may also see an error when converting a PyTorch model to ONNX model, which may be fixed by replacing: \n\n`torch.onnx.export(resnet50, dummy_input, \"resnet50_pytorch.onnx\", verbose=False)`\n\nwith:\n\n`torch.onnx.export(model, dummy_input, \"deeplabv3_pytorch.onnx\", opset_version=11, verbose=False)`", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Using PyTorch \nFirst, to download and install PyTorch 1.9 on Nano, run the following commands (see [here](https://forums.developer.nvidia.com/t/pytorch-for-jetson-version-1-10-now-available/72048) for more information):", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "```\nwget https://nvidia.box.com/shared/static/p57jwntv436lfrd78inwl7iml6p13fzh.whl -O torch-1.8.0-cp36-cp36m-linux_aarch64.whl -O torch-1.9.0-cp36-cp36m-linux_aarch64.whl\nsudo apt-get install python3-pip libopenblas-base libopenmpi-dev \npip3 install Cython\npip3 install numpy torch-1.9.0-cp36-cp36m-linux_aarch64.whl", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "To download and install torchvision 0.10 on Nano, run the commands below:\n\n```\nhttps://drive.google.com/uc?id=1tU6YlPjrP605j4z8PMnqwCSoP6sSC91Z\npip3 install torchvision-0.10.0a0+300a8a4-cp36-cp36m-linux_aarch64.whl\n```\n\nAfter the steps above, run this to confirm:\n```\n$ pip3 list|grep torch\ntorch (1.9.0)\ntorchvision (0.10.0)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "You can also use the docker image described in the section *Using Jetson Inference* (which also has PyTorch and torchvision installed), to skip the manual steps above.\n\nThe official [YOLOv5](https://github.com/ultralytics/yolov5) repo is used to run the PyTorch YOLOv5 model on Jetson Nano. After logging in to Jetson Nano, follow the steps below:\n\n* Get the repo and install what\u2019s required:\n\n```\ngit clone https://github.com/ultralytics/yolov5\ncd yolov5\npip install -r requirements.txt", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "* Run `python3 detect.py`, which by default uses the PyTorch yolov5s.pt model. You should see something like:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "```\ndetect: weights=yolov5s.pt, source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False\nYOLOv5 \ud83d\ude80 v5.0-499-g48b00db torch 1.9.0 CUDA:0 (NVIDIA Tegra X1, 3956.1015625MB)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Fusing layers... \nModel Summary: 224 layers, 7266973 parameters, 0 gradients\nimage 1/5 /home/jeff/repos/yolov5-new/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, 1 fire hydrant, Done. (0.142s)\n...", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "**The inference time on Jetson Nano GPU is about 140ms, more than twice as fast as the inference time on iOS or Android (about 330ms).**\n\nIf you get an error `\u201cImportError: The _imagingft C module is not installed.\u201d` then you need to reinstall pillow:\n```\nsudo apt-get install libpng-dev\nsudo apt-get install libfreetype6-dev\npip3 uninstall pillow\npip3 install --no-cache-dir pillow", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "After successfully completing the `python3 detect.py` run, the object detection results of the test images located in `data/images` will be in the `runs/detect/exp` directory. To test the detection with a live webcam instead of local images, use the `--source 0` parameter when running `python3 detect.py`):", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "```\n~/repos/yolov5$ ls -lt runs/detect/exp10\ntotal 1456\n-rw-rw-r-- 1 jeff jeff 254895 Oct 15 16:12 zidane.jpg\n-rw-rw-r-- 1 jeff jeff 202674 Oct 15 16:12 test3.png\n-rw-rw-r-- 1 jeff jeff 217117 Oct 15 16:12 test2.jpg\n-rw-rw-r-- 1 jeff jeff 305826 Oct 15 16:12 test1.png\n-rw-rw-r-- 1 jeff jeff 495760 Oct 15 16:12 bus.jpg", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Using the same test files used in the PyTorch iOS YOLOv5 demo app or Android YOLOv5 demo app, you can compare the results generated with running the YOLOv5 PyTorch model on mobile devices and Jetson Nano:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "
\n \"PyTorch\n \"PyTorch\n
\nFigure 1. PyTorch YOLOv5 on Jetson Nano.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "
\n \"PyTorch\n \"PyTorch\n
\nFigure 2. PyTorch YOLOv5 on iOS.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "
\n \"PyTorch\n \"PyTorch\n
\nFigure 3. PyTorch YOLOv5 on Android.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Summary\nBased on our experience of running different PyTorch models for potential demo apps on Jetson Nano, we see that even Jetson Nano, a lower-end of the Jetson family of products, provides a powerful GPU and embedded system that can directly run some of the latest PyTorch models, pre-trained or transfer learned, efficiently.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "Building PyTorch demo apps on Jetson Nano can be similar to building PyTorch apps on Linux, but you can also choose to use TensorRT after converting the PyTorch models to the TensorRT engine file format.\n\nBut if you just need to run some common computer vision models on Jetson Nano using NVIDIA\u2019s Jetson Inference which supports image recognition, object detection, semantic segmentation, and pose estimation models, then this is the easiest way.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "References\nTorch-TensorRT, a compiler for PyTorch via TensorRT:\n[https://github.com/NVIDIA/Torch-TensorRT/](https://github.com/NVIDIA/Torch-TensorRT/)\n\nJetson Inference docker image details:\n[https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-docker.md](https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-docker.md)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "A guide to using TensorRT on the NVIDIA Jetson Nano:\n[https://docs.donkeycar.com/guide/robot_sbc/tensorrt_jetson_nano/](https://docs.donkeycar.com/guide/robot_sbc/tensorrt_jetson_nano/) \nincluding:\n\n1. Use Jetson as a portable GPU device to run an NN chess engine model: \n[https://medium.com/@ezchess/jetson-lc0-running-leela-chess-zero-on-nvidia-jetson-a-portable-gpu-device-a213afc9c018](https://medium.com/@ezchess/jetson-lc0-running-leela-chess-zero-on-nvidia-jetson-a-portable-gpu-device-a213afc9c018)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "2. A MaskEraser app using PyTorch and torchvision, installed directly with pip:\n[https://github.com/INTEC-ATI/MaskEraser#install-pytorch](https://github.com/INTEC-ATI/MaskEraser#install-pytorch)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.9 Release, including torch.linalg and Mobile Interpreter'\nauthor: Team PyTorch \n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the release of PyTorch 1.9. The release is composed of more than 3,400 commits since 1.8, made by 398 contributors. The release notes are available [here](https://github.com/pytorch/pytorch/releases). Highlights include:\n1. Major improvements to support scientific computing, including *torch.linalg*, *torch.special*, and Complex Autograd\n2. Major improvements in on-device binary size with Mobile Interpreter", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "3. Native support for elastic-fault tolerance training through the upstreaming of TorchElastic into PyTorch Core\n4. Major updates to the PyTorch RPC framework to support large scale distributed training with GPU support\n5. New APIs to optimize performance and packaging for model inference deployment \n6. Support for Distributed training, GPU utilization and SM efficiency in the PyTorch Profiler", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Along with 1.9, we are also releasing major updates to the PyTorch libraries, which you can read about in [this blog post](https://pytorch.org/blog/pytorch-1.9-new-library-releases/). \n\nWe\u2019d like to thank the community for their support and work on this latest release. We\u2019d especially like to thank Quansight and Microsoft for their contributions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Features in PyTorch releases are classified as Stable, Beta, and Prototype. You can learn more about the definitions in [this blog post](https://pytorch.org/blog/pytorch-feature-classification-changes/). \n\n# Frontend APIs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) *torch.linalg*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "In 1.9, the *torch.linalg* module is moving to a stable release. Linear algebra is essential to deep learning and scientific computing, and the *torch.linalg* module extends PyTorch\u2019s support for it with implementations of every function from [NumPy\u2019s linear algebra module](https://numpy.org/doc/stable/reference/routines.linalg.html) (now with support for accelerators and autograd) and more, like", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[*torch.linalg.matrix_norm*](https://pytorch.org/docs/1.9.0/generated/torch.linalg.matrix_norm.html?highlight=matrix_norm#torch.linalg.matrix_norm) and [*torch.linalg.householder_product*](https://pytorch.org/docs/1.9.0/generated/torch.linalg.householder_product.html?highlight=householder_product#torch.linalg.householder_product). This makes the module immediately familiar to users who have worked with NumPy. Refer to [the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "documentation](https://pytorch.org/docs/1.9.0/linalg.html?highlight=linalg#module-torch.linalg) here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "We plan to publish another blog post with more details on the *torch.linalg* module next week!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Complex Autograd \n\nThe Complex Autograd feature, released as a beta in PyTorch 1.8, is now stable. Since the beta release, we have extended support for Complex Autograd for over 98% operators in PyTorch 1.9, improved testing for complex operators by adding more OpInfos, and added greater validation through TorchAudio migration to native complex tensors (refer to [this issue](https://github.com/pytorch/audio/issues/1337)).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "This feature provides users the functionality to calculate complex gradients and optimize real valued loss functions with complex variables. This is a required feature for multiple current and downstream prospective users of complex numbers in PyTorch like TorchAudio, ESPNet, Asteroid, and FastMRI. Refer to [the documentation](https://pytorch.org/docs/1.9.0/notes/autograd.html#autograd-for-complex-numbers) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) torch.use_deterministic_algorithms() \n\nTo help with debugging and writing reproducible programs, PyTorch 1.9 includes a *torch.use_determinstic_algorithms* option. When this setting is enabled, operations will behave deterministically, if possible, or throw a runtime error if they might behave nondeterministically. Here are a couple examples:\n\n```python\n>>> a = torch.randn(100, 100, 100, device='cuda').to_sparse()\n>>> b = torch.randn(100, 100, 100, device='cuda')", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "# Sparse-dense CUDA bmm is usually nondeterministic\n>>> torch.bmm(a, b).eq(torch.bmm(a, b)).all().item()\nFalse\n\n>>> torch.use_deterministic_algorithms(True)\n\n# Now torch.bmm gives the same result each time, but with reduced performance\n>>> torch.bmm(a, b).eq(torch.bmm(a, b)).all().item()\nTrue\n\n# CUDA kthvalue has no deterministic algorithm, so it throws a runtime error\n>>> torch.zeros(10000, device='cuda').kthvalue(1)\nRuntimeError: kthvalue CUDA does not have a deterministic implementation...", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.9 adds deterministic implementations for a number of indexing operations, too, including *index_add*, *index_copy*, and *index_put with accum=False*. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.use_deterministic_algorithms.html?highlight=use_deterministic#torch.use_deterministic_algorithms) and [reproducibility note](https://pytorch.org/docs/1.9.0/notes/randomness.html?highlight=reproducibility).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) *torch.special*\n\nA *torch.special* module, analogous to [SciPy\u2019s special module](https://docs.scipy.org/doc/scipy/reference/special.html), is now available in beta. This module contains many functions useful for scientific computing and working with distributions such as *iv*, *ive*, *erfcx*, *logerfc*, and *logerfcx*. Refer to [the documentation](https://pytorch.org/docs/master/special.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) nn.Module parameterization \n\n```nn.Module``` parameterization allows users to parametrize any parameter or buffer of an ```nn.Module``` without modifying the ```nn.Module``` itself. It allows you to constrain the space in which your parameters live without the need for special optimization methods.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "This also contains a new implementation of the ```spectral_norm``` parametrization for PyTorch 1.9. More parametrization will be added to this feature (weight_norm, matrix constraints and part of pruning) for the feature to become stable in 1.10. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.nn.utils.parametrizations.spectral_norm.html?highlight=parametrize) and [tutorial](https://pytorch.org/tutorials/intermediate/parametrizations.html).\n\n# PyTorch Mobile", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Mobile Interpreter \n\nWe are releasing Mobile Interpreter, a streamlined version of the PyTorch runtime, in beta. The Interpreter will execute PyTorch programs in edge devices, with reduced binary size footprint.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Mobile Interpreter is one of the top requested features for PyTorch Mobile. This new release will significantly reduce binary size compared with the current on-device runtime. In order for you to get the binary size improvements with our interpreter (which can reduce the binary size up to ~75% for a typical application) follow these instructions. As an example, using Mobile Interpreter, we can reach 2.6 MB compressed with MobileNetV2 in arm64-v7a Android. With this latest release we are making it much", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "simpler to integrate the interpreter by providing pre-built libraries for iOS and Android.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "TorchVision Library", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Starting from 1.9, users can use the TorchVision library on their iOS/Android apps. The Torchvision library contains the C++ TorchVision ops and needs to be linked together with the main PyTorch library for iOS, for Android it can be added as a gradle dependency. This allows using TorchVision prebuilt MaskRCNN operators for object detections and segmentation. To learn more about the library, please refer to our tutorials and [demo apps](https://github.com/pytorch/android-demo-app/tree/master/D2Go).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Demo apps", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "We are releasing a new video app based on [PyTorch Video](https://pytorchvideo.org/) library and an updated speech recognition app based on the latest torchaudio, wave2vec model. Both are available on [iOS](https://github.com/pytorch/ios-demo-app) and [Android](https://github.com/pytorch/android-demo-app). In addition, we have updated the seven Computer Vision and three Natural Language Processing demo apps, including the HuggingFace DistilBERT, and the DeiT vision transformer models, with PyTorch Mobile", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "v1.9. With the addition of these two apps, we now offer a full suite of demo apps covering image, text, audio, and video. To get started check out our [iOS demo apps](https://github.com/pytorch/ios-demo-app) and [Android demo apps](https://github.com/pytorch/android-demo-app).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n# Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) TorchElastic is now part of core", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[TorchElastic](https://github.com/pytorch/pytorch/issues/50621), which was open sourced over a year ago in the [pytorch/elastic](https://github.com/pytorch/elastic) github repository, is a runner and coordinator for PyTorch worker processes. Since then, it has been adopted by various distributed torch use-cases: 1) [deepspeech.pytorch](https://medium.com/pytorch/training-deepspeech-using-torchelastic-ad013539682) 2)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[pytorch-lightning](https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#torchelastic) 3) [Kubernetes CRD](https://github.com/pytorch/elastic/blob/master/kubernetes/README.md). Now, it is part of PyTorch core.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "As its name suggests, the core function of TorcheElastic is to gracefully handle scaling events. A notable corollary of elasticity is that peer discovery and rank assignment are built into TorchElastic enabling users to run distributed training on preemptible instances without requiring a gang scheduler. As a side note, [etcd](https://etcd.io/) used to be a hard dependency of TorchElastic. With the upstream, this is no longer the case since we have added a \u201cstandalone\u201d rendezvous based on c10d::Store. For", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/distributed.elastic.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Distributed Training Updates\n\nIn addition to TorchElastic, there are a number of beta features available in the distributed package:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Beta) CUDA support is available in RPC**: Compared to CPU RPC and general-purpose RPC frameworks, CUDA RPC is a much more efficient way for P2P Tensor communication. It is built on top of TensorPipe which can automatically choose a communication channel for each Tensor based on Tensor device type and channel availability on both the caller and the callee. Existing TensorPipe channels cover NVLink, InfiniBand, SHM, CMA, TCP, etc. See [this recipe](https://pytorch.org/tutorials/recipes/cuda_rpc.html) for", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "how CUDA RPC helps to attain 34x speedup compared to CPU RPC.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Beta) ZeroRedundancyOptimizer**: ZeroRedundancyOptimizer can be used in conjunction with DistributedDataParallel to reduce the size of per-process optimizer states. The idea of ZeroRedundancyOptimizer comes from [DeepSpeed/ZeRO project](https://github.com/microsoft/DeepSpeed) and [Marian](https://github.com/marian-nmt/marian-dev), where the optimizer in each process owns a shard of model parameters and their corresponding optimizer states. When running `step()`, each optimizer only updates its own", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "parameters, and then uses collective communication to synchronize updated parameters across all processes. Refer to [this documentation](https://pytorch.org/docs/master/distributed.optim.html) and this [tutorial](https://pytorch.org/tutorials/recipes/zero_redundancy_optimizer.html) to learn more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Beta) Support for profiling distributed collectives**: PyTorch\u2019s profiler tools, *torch.profiler* and *torch.autograd.profiler*, are able to profile distributed collectives and point to point communication primitives including allreduce, alltoall, allgather, send/recv, etc. This is enabled for all backends supported natively by PyTorch: gloo, mpi, and nccl. This can be used to debug performance issues, analyze traces that contain distributed communication, and gain insight into performance of", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "applications that use distributed training. To learn more, refer to [this documentation](https://pytorch.org/docs/1.9.0/distributed.html#profiling-collective-communication).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "# Performance Optimization and Tooling", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Freezing API \n\nModule Freezing is the process of inlining module parameters and attributes values as constants into the TorchScript internal representation. This allows further optimization and specialization of your program, both for TorchScript optimizations and lowering to other backends. It is used by [optimize_for_mobile API](https://github.com/pytorch/pytorch/blob/master/torch/utils/mobile_optimizer.py), ONNX, and others.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Freezing is recommended for model deployment. It helps TorchScript JIT optimizations optimize away overhead and bookkeeping that is necessary for training, tuning, or debugging PyTorch models. It enables graph fusions that are not semantically valid on non-frozen graphs - such as fusing Conv-BN. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.jit.freeze.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) PyTorch Profiler \n\n
\n \n
\n\nThe new PyTorch Profiler graduates to beta and leverages [Kineto](https://github.com/pytorch/kineto/) for GPU profiling, TensorBoard for visualization and is now the standard across our tutorials and documentation.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.9 extends support for the new *torch.profiler* API to more builds, including Windows and Mac and is recommended in most cases instead of the previous *torch.autograd.profiler* API. The new API supports existing profiler features, integrates with CUPTI library (Linux-only) to trace on-device CUDA kernels and provides support for long-running jobs, e.g.:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "```python\ndef trace_handler(p):\n output = p.key_averages().table(sort_by=\"self_cuda_time_total\", row_limit=10)\n print(output)\n p.export_chrome_trace(\"/tmp/trace_\" + str(p.step_num) + \".json\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "with profile(\n activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],\n # schedule argument specifies the iterations on which the profiler is active\n schedule=torch.profiler.schedule(\n wait=1,\n warmup=1,\n active=2),\n # on_trace_ready argument specifies the handler for the traces\n on_trace_ready=trace_handler\n) as p:\n for idx in range(8):\n model(inputs)\n # profiler will trace iterations 2 and 3, and then 6 and 7 (counting from zero)\n p.step()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "More usage examples can be found on the [profiler recipe page](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html). \n\nThe PyTorch Profiler Tensorboard plugin has new features for:\n* Distributed Training summary view with communications overview for NCCL\n* GPU Utilization and SM Efficiency in Trace view and GPU operators view\n* Memory Profiling view\n* Jump to source when launched from Microsoft VSCode\n* Ability for load traces from cloud object storage systems", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Inference Mode API", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Inference Mode API allows significant speed-up for inference workloads while remaining safe and ensuring no incorrect gradients can ever be computed. It offers the best possible performance when no autograd is required. For more details, refer to [the documentation for inference mode itself](https://pytorch.org/docs/1.9.0/generated/torch.inference_mode.html?highlight=inference%20mode#torch.inference_mode) and [the documentation explaining when to use it and the difference with no_grad", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "mode](https://pytorch.org/docs/1.9.0/notes/autograd.html#locally-disabling-gradient-computation).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) *torch.package*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "*torch.package* is a new way to package PyTorch models in a self-contained, stable format. A package will include both the model\u2019s data (e.g. parameters, buffers) and its code (model architecture). Packaging a model with its full set of Python dependencies, combined with a description of a conda environment with pinned versions, can be used to easily reproduce training. Representing a model in a self-contained artifact will also allow it to be published and transferred throughout a production ML pipeline", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "while retaining the flexibility of a pure-Python representation. For more details, refer to [the documentation](https://pytorch.org/docs/1.9.0/package.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) prepare_for_inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "prepare_for_inference is a new prototype feature that takes in a module and performs graph-level optimizations to improve inference performance, depending on the device. It is meant to be a PyTorch-native option that requires minimal changes to user\u2019s workflows. For more details, see [the documentation](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) for the Torchscript version [here](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) or the FX version", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[here](https://github.com/pytorch/pytorch/blob/master/torch/fx/experimental/optimization.py#L234).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Profile-directed typing in TorchScript", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "TorchScript has a hard requirement for source code to have type annotations in order for compilation to be successful. For a long time, it was only possible to add missing or incorrect type annotations through trial and error (i.e., by fixing the type-checking errors generated by *torch.jit.script* one by one), which was inefficient and time consuming. Now, we have enabled profile directed typing for *torch.jit.script* by leveraging existing tools like MonkeyType, which makes the process much easier,", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "faster, and more efficient. For more details, refer to [the documentation](https://pytorch.org/docs/1.9.0/jit.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Facebook](https://www.facebook.com/pytorch/), [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), or", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[LinkedIn](https://www.linkedin.com/company/pytorch).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Cheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Developer Day 2020'\nauthor: Team PyTorch\n---\n\nStarting this year, we plan to host two separate events for PyTorch: one for developers and users to discuss core technical development, ideas and roadmaps called **\u201cDeveloper Day\u201d**, and another for the PyTorch ecosystem and industry communities to showcase their work and discover opportunities to collaborate called **\u201cEcosystem Day\u201d** (scheduled for early 2021).", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nThe **PyTorch Developer Day** (#PTD2) is kicking off on November 12, 2020, 8AM PST with a full day of technical talks on a variety of topics, including updates to the core framework, new tools and libraries to support development across a variety of domains. You'll also see talks covering the latest research around systems and tooling in ML.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} -{"page_content": "For Developer Day, we have an online networking event limited to people composed of PyTorch maintainers and contributors, long-time stakeholders and experts in areas relevant to PyTorch\u2019s future. Conversations from the networking event will strongly shape the future of PyTorch. Hence, invitations are required to attend the networking event.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} -{"page_content": "All talks will be livestreamed and available to the public.\n* [Livestream event page](https://www.facebook.com/events/802177440559164/)\n* [Apply for an invitation to the networking event](https://pytorchdeveloperday.fbreg.com/apply)\n\nVisit the [event website](https://pytorchdeveloperday.fbreg.com/) to learn more. We look forward to welcoming you to PyTorch Developer Day on November 12th! \n\nThank you,\n\nThe PyTorch team", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.7 released w/ CUDA 11, New APIs for FFTs, Windows support for Distributed training and more'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Today, we\u2019re announcing the availability of PyTorch 1.7, along with updated domain libraries. The PyTorch 1.7 release includes a number of new APIs including support for NumPy-Compatible FFT operations, profiling tools and major updates to both distributed data parallel (DDP) and remote procedure call (RPC) based distributed training. In addition, several features moved to [stable](https://pytorch.org/docs/stable/index.html#pytorch-documentation) including custom C++ Classes, the memory profiler, extensions", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "via custom tensor-like objects, user async functions in RPC and a number of other features in torch.distributed such as Per-RPC timeout, DDP dynamic bucketing and RRef helper.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "A few of the highlights include:\n* CUDA 11 is now officially supported with binaries available at [PyTorch.org](http://pytorch.org/)\n* Updates and additions to profiling and performance for RPC, TorchScript and Stack traces in the autograd profiler\n* (Beta) Support for NumPy compatible Fast Fourier transforms (FFT) via torch.fft\n* (Prototype) Support for Nvidia A100 generation GPUs and native TF32 format \n* (Prototype) Distributed training on Windows now supported\n* torchvision", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* (Stable) Transforms now support Tensor inputs, batch computation, GPU, and TorchScript\n * (Stable) Native image I/O for JPEG and PNG formats\n * (Beta) New Video Reader API\n* torchaudio\n * (Stable) Added support for speech rec (wav2letter), text to speech (WaveRNN) and source separation (ConvTasNet)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "To reiterate, starting PyTorch 1.6, features are now classified as stable, beta and prototype. You can see the detailed announcement [here](https://pytorch.org/blog/pytorch-feature-classification-changes/). Note that the prototype features listed in this blog are available as part of this release. \n\nFind the full release notes [here](https://github.com/pytorch/pytorch/releases). \n\n# Front End APIs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] NumPy Compatible torch.fft module\nFFT-related functionality is commonly used in a variety of scientific fields like signal processing. While PyTorch has historically supported a few FFT-related functions, the 1.7 release adds a new torch.fft module that implements FFT-related functions with the same API as NumPy.\n\nThis new module must be imported to be used in the 1.7 release, since its name conflicts with the historic (and now deprecated) torch.fft function.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "**Example usage:**\n```python\n>>> import torch.fft\n>>> t = torch.arange(4)\n>>> t\ntensor([0, 1, 2, 3])\n\n>>> torch.fft.fft(t)\ntensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])\n\n>>> t = tensor([0.+1.j, 2.+3.j, 4.+5.j, 6.+7.j])\n>>> torch.fft.fft(t)\ntensor([12.+16.j, -8.+0.j, -4.-4.j, 0.-8.j])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [Documentation](https://pytorch.org/docs/stable/fft.html#torch-fft)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] C++ Support for Transformer NN Modules\nSince [PyTorch 1.5](https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/), we\u2019ve continued to maintain parity between the python and C++ frontend APIs. This update allows developers to use the nn.transformer module abstraction from the C++ Frontend. And moreover, developers no longer need to save a module from python/JIT and load into C++ as it can now be used it in C++ directly.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [Documentation](https://pytorch.org/cppdocs/api/classtorch_1_1nn_1_1_transformer_impl.html#_CPPv4N5torch2nn15TransformerImplE)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] torch.set_deterministic", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Reproducibility (bit-for-bit determinism) may help identify errors when debugging or testing a program. To facilitate reproducibility, PyTorch 1.7 adds the ```torch.set_deterministic(bool)``` function that can direct PyTorch operators to select deterministic algorithms when available, and to throw a runtime error if an operation may result in nondeterministic behavior. By default, the flag this function controls is false and there is no change in behavior, meaning PyTorch may implement its operations", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "nondeterministically by default.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "More precisely, when this flag is true:\n* Operations known to not have a deterministic implementation throw a runtime error;\n* Operations with deterministic variants use those variants (usually with a performance penalty versus the non-deterministic version); and\n* ```torch.backends.cudnn.deterministic = True``` is set.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Note that this is necessary, **but not sufficient**, for determinism **within a single run of a PyTorch program**. Other sources of randomness like random number generators, unknown operations, or asynchronous or distributed computation may still cause nondeterministic behavior.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "See the documentation for ```torch.set_deterministic(bool)``` for the list of affected operations.\n* [RFC](https://github.com/pytorch/pytorch/issues/15359)\n* [Documentation](https://pytorch.org/docs/stable/generated/torch.set_deterministic.html)\n\n# Performance & Profiling", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Stack traces added to profiler", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Users can now see not only operator name/inputs in the profiler output table but also where the operator is in the code. The workflow requires very little change to take advantage of this capability. The user uses the [autograd profiler](https://pytorch.org/docs/stable/autograd.html#profiler) as before but with optional new parameters: ```with_stack``` and ```group_by_stack_n```. Caution: regular profiling runs should not use this feature as it adds significant overhead.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [Detail](https://github.com/pytorch/pytorch/pull/43898/)\n* [Documentation](https://pytorch.org/docs/stable/autograd.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# Distributed Training & RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] TorchElastic now bundled into PyTorch docker image\nTorchelastic offers a strict superset of the current ```torch.distributed.launch``` CLI with the added features for fault-tolerance and elasticity. If the user is not be interested in fault-tolerance, they can get the exact functionality/behavior parity by setting ```max_restarts=0``` with the added convenience of auto-assigned ```RANK``` and ```MASTER_ADDR|PORT``` (versus manually specified in ```torch.distributed.launch)```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "By bundling ```torchelastic``` in the same docker image as PyTorch, users can start experimenting with TorchElastic right-away without having to separately install ```torchelastic```. In addition to convenience, this work is a nice-to-have when adding support for elastic parameters in the existing Kubeflow\u2019s distributed PyTorch operators.\n* [Usage examples and how to get started](https://pytorch.org/elastic/0.2.0/examples.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Support for uneven dataset inputs in DDP", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.7 introduces a new context manager to be used in conjunction with models trained using ```torch.nn.parallel.DistributedDataParallel``` to enable training with uneven dataset size across different processes. This feature enables greater flexibility when using DDP and prevents the user from having to manually ensure dataset sizes are the same across different process. With this context manager, DDP will handle uneven dataset sizes automatically, which can prevent errors or hangs at the end of", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [RFC](https://github.com/pytorch/pytorch/issues/38174)\n* [Documentation](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel.join)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] NCCL Reliability - Async Error/Timeout Handling", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "In the past, NCCL training runs would hang indefinitely due to stuck collectives, leading to a very unpleasant experience for users. This feature will abort stuck collectives and throw an exception/crash the process if a potential hang is detected. When used with something like torchelastic (which can recover the training process from the last checkpoint), users can have much greater reliability for distributed training. This feature is completely opt-in and sits behind an environment variable that needs to", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "be explicitly set in order to enable this functionality (otherwise users will see the same behavior as before).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [RFC](https://github.com/pytorch/pytorch/issues/46874)\n* [Documentation](https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] TorchScript ```rpc_remote``` and ```rpc_sync```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "```torch.distributed.rpc.rpc_async``` has been available in TorchScript in prior releases. For PyTorch 1.7, this functionality will be extended the remaining two core RPC APIs, ```torch.distributed.rpc.rpc_sync``` and ```torch.distributed.rpc.remote```. This will complete the major RPC APIs targeted for support in TorchScript, it allows users to use the existing python RPC APIs within TorchScript (in a script function or script method, which releases the python Global Interpreter Lock) and could possibly", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "improve application performance in multithreaded environment.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* [Documentation](https://pytorch.org/docs/stable/rpc.html#rpc)\n* [Usage examples](https://github.com/pytorch/pytorch/blob/58ed60c259834e324e86f3e3118e4fcbbfea8dd1/torch/testing/_internal/distributed/rpc/jit/rpc_test.py#L505-L525)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Distributed optimizer with TorchScript support", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch provides a broad set of optimizers for training algorithms, and these have been used repeatedly as part of the python API. However, users often want to use multithreaded training instead of multiprocess training as it provides better resource utilization and efficiency in the context of large scale distributed training (e.g. Distributed Model Parallel) or any RPC-based training application). Users couldn\u2019t do this with with distributed optimizer before because we need to get rid of the python Global", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Interpreter Lock (GIL) limitation to achieve this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch 1.7, we are enabling the TorchScript support in distributed optimizer to remove the GIL, and make it possible to run optimizer in multithreaded applications. The new distributed optimizer has the exact same interface as before but it automatically converts optimizers within each worker into TorchScript to make each GIL free. This is done by leveraging a functional optimizer concept and allowing the distributed optimizer to convert the computational portion of the optimizer into TorchScript. This", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "will help use cases like distributed model parallel training and improve performance using multithreading.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Currently, the only optimizer that supports automatic conversion with TorchScript is ```Adagrad``` and all other optimizers will still work as before without TorchScript support. We are working on expanding the coverage to all PyTorch optimizers and expect more to come in future releases. The usage to enable TorchScript support is automatic and exactly the same with existing python APIs, here is an example of how to use this:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch.distributed.autograd as dist_autograd\nimport torch.distributed.rpc as rpc\nfrom torch import optim\nfrom torch.distributed.optim import DistributedOptimizer\n\nwith dist_autograd.context() as context_id:\n # Forward pass.\n rref1 = rpc.remote(\"worker1\", torch.add, args=(torch.ones(2), 3))\n rref2 = rpc.remote(\"worker1\", torch.add, args=(torch.ones(2), 1))\n loss = rref1.to_here() + rref2.to_here()\n\n # Backward pass.\n dist_autograd.backward(context_id, [loss.sum()])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# Optimizer, pass in optim.Adagrad, DistributedOptimizer will\n # automatically convert/compile it to TorchScript (GIL-free)\n dist_optim = DistributedOptimizer(\n optim.Adagrad,\n [rref1, rref2],\n lr=0.05,\n )\n dist_optim.step(context_id)\n ```\n* [RFC](https://github.com/pytorch/pytorch/issues/46883)\n* [Documentation](https://pytorch.org/docs/stable/rpc.html#module-torch.distributed.optim)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Enhancements to RPC-based Profiling\nSupport for using the PyTorch profiler in conjunction with the RPC framework was first introduced in PyTorch 1.6. In PyTorch 1.7, the following enhancements have been made:\n* Implemented better support for profiling TorchScript functions over RPC\n* Achieved parity in terms of profiler features that work with RPC\n* Added support for asynchronous RPC functions on the server-side (functions decorated with ```rpc.functions.async_execution)```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "Users are now able to use familiar profiling tools such as with ```torch.autograd.profiler.profile()``` and ```with torch.autograd.profiler.record_function```, and this works transparently with the RPC framework with full feature support, profiles asynchronous functions, and TorchScript functions.\n* [Design doc](https://github.com/pytorch/pytorch/issues/39675)\n* [Usage examples](https://pytorch.org/tutorials/recipes/distributed_rpc_profiling.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] Windows support for Distributed Training\nPyTorch 1.7 brings prototype support for ```DistributedDataParallel``` and collective communications on the Windows platform. In this release, the support only covers Gloo-based ```ProcessGroup``` and ```FileStore```.\n\nTo use this feature across multiple machines, please provide a file from a shared file system in ```init_process_group```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "```python\n# initialize the process group\ndist.init_process_group(\n \"gloo\",\n # multi-machine example:\n # init_method = \"file://////{machine}/{share_folder}/file\"\n init_method=\"file:///{your local file path}\",\n rank=rank,\n world_size=world_size\n)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "model = DistributedDataParallel(local_model, device_ids=[rank])\n```\n* [Design doc](https://github.com/pytorch/pytorch/issues/42095)\n* [Documentation](https://pytorch.org/docs/master/distributed.html#backends-that-come-with-pytorch)\n* Acknowledgement ([gunandrose4u](https://github.com/gunandrose4u))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# Mobile\nPyTorch Mobile supports both [iOS](https://pytorch.org/mobile/ios) and [Android](https://pytorch.org/mobile/android/) with binary packages available in [Cocoapods](https://cocoapods.org/) and [JCenter](https://mvnrepository.com/repos/jcenter) respectively. You can learn more about PyTorch Mobile [here](https://pytorch.org/mobile/home/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] PyTorch Mobile Caching allocator for performance improvements", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "On some mobile platforms, such as Pixel, we observed that memory is returned to the system more aggressively. This results in frequent page faults as PyTorch being a functional framework does not maintain state for the operators. Thus outputs are allocated dynamically on each execution of the op, for the most ops. To ameliorate performance penalties due to this, PyTorch 1.7 provides a simple caching allocator for CPU. The allocator caches allocations by tensor sizes and, is currently, available only via the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch C++ API. The caching allocator itself is owned by client and thus the lifetime of the allocator is also maintained by client code. Such a client owned caching allocator can then be used with scoped guard, ```c10::WithCPUCachingAllocatorGuard```, to enable the use of cached allocation within that scope.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "**Example usage:**", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "```python\n#include \n.....\nc10::CPUCachingAllocator caching_allocator;\n // Owned by client code. Can be a member of some client class so as to tie the\n // the lifetime of caching allocator to that of the class.\n.....\n{\n c10::optional caching_allocator_guard;\n if (FLAGS_use_caching_allocator) {\n caching_allocator_guard.emplace(&caching_allocator);\n }\n ....\n model.forward(..);\n}\n...\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "**NOTE**: Caching allocator is only available on mobile builds, thus the use of caching allocator outside of mobile builds won\u2019t be effective.\n* [Documentation](https://github.com/pytorch/pytorch/blob/master/c10/mobile/CPUCachingAllocator.h#L13-L43)\n* [Usage examples](https://github.com/pytorch/pytorch/blob/master/binaries/speed_benchmark_torch.cc#L207)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# torchvision", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Transforms now support Tensor inputs, batch computation, GPU, and TorchScript\ntorchvision transforms are now inherited from ```nn.Module``` and can be torchscripted and applied on torch Tensor inputs as well as on PIL images. They also support Tensors with batch dimensions and work seamlessly on CPU/GPU devices:\n```python\nimport torch\nimport torchvision.transforms as T\n\n# to fix random seed, use torch.manual_seed\n# instead of random.seed\ntorch.manual_seed(12)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "transforms = torch.nn.Sequential(\n T.RandomCrop(224),\n T.RandomHorizontalFlip(p=0.3),\n T.ConvertImageDtype(torch.float),\n T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n)\nscripted_transforms = torch.jit.script(transforms)\n# Note: we can similarly use T.Compose to define transforms\n# transforms = T.Compose([...]) and \n# scripted_transforms = torch.jit.script(torch.nn.Sequential(*transforms.transforms))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "tensor_image = torch.randint(0, 256, size=(3, 256, 256), dtype=torch.uint8)\n# works directly on Tensors\nout_image1 = transforms(tensor_image)\n# on the GPU\nout_image1_cuda = transforms(tensor_image.cuda())\n# with batches\nbatched_image = torch.randint(0, 256, size=(4, 3, 256, 256), dtype=torch.uint8)\nout_image_batched = transforms(batched_image)\n# and has torchscript support\nout_image2 = scripted_transforms(tensor_image)\n```\nThese improvements enable the following new features:\n* support for GPU acceleration", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* batched transformations e.g. as needed for videos\n* transform multi-band torch tensor images (with more than 3-4 channels)\n* torchscript transforms together with your model for deployment\n**Note:** Exceptions for TorchScript support includes ```Compose```, ```RandomChoice```, ```RandomOrder```, ```Lambda``` and those applied on PIL images, such as ```ToPILImage```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Native image IO for JPEG and PNG formats\ntorchvision 0.8.0 introduces native image reading and writing operations for JPEG and PNG formats. Those operators support TorchScript and return ```CxHxW``` tensors in ```uint8``` format, and can thus be now part of your model for deployment in C++ environments.\n```python\nfrom torchvision.io import read_image\n\n# tensor_image is a CxHxW uint8 Tensor\ntensor_image = read_image('path_to_image.jpeg')", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# or equivalently\nfrom torchvision.io import read_file, decode_image\n# raw_data is a 1d uint8 Tensor with the raw bytes\nraw_data = read_file('path_to_image.jpeg')\ntensor_image = decode_image(raw_data)\n\n# all operators are torchscriptable and can be\n# serialized together with your model torchscript code\nscripted_read_image = torch.jit.script(read_image)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] RetinaNet detection model\nThis release adds pretrained models for RetinaNet with a ResNet50 backbone from [Focal Loss for Dense Object Detection](https://arxiv.org/abs/1708.02002).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] New Video Reader API\nThis release introduces a new video reading abstraction, which gives more fine-grained control of iteration over videos. It supports image and audio, and implements an iterator interface so that it is interoperable with other the python libraries such as itertools.\n```python\nfrom torchvision.io import VideoReader", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# stream indicates if reading from audio or video\nreader = VideoReader('path_to_video.mp4', stream='video')\n# can change the stream after construction\n# via reader.set_current_stream\n\n# to read all frames in a video starting at 2 seconds\nfor frame in reader.seek(2):\n # frame is a dict with \"data\" and \"pts\" metadata\n print(frame[\"data\"], frame[\"pts\"])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# because reader is an iterator you can combine it with\n# itertools\nfrom itertools import takewhile, islice\n# read 10 frames starting from 2 seconds\nfor frame in islice(reader.seek(2), 10):\n pass\n \n# or to return all frames between 2 and 5 seconds\nfor frame in takewhile(lambda x: x[\"pts\"] < 5, reader):\n pass\n```\n**Notes:**\n* In order to use the Video Reader API beta, you must compile torchvision from source and have ffmpeg installed in your system.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "* The VideoReader API is currently released as beta and its API may change following user feedback.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "# torchaudio\nWith this release, torchaudio is expanding its support for models and [end-to-end applications](https://github.com/pytorch/audio/tree/master/examples), adding a wav2letter training pipeline and end-to-end text-to-speech and source separation pipelines. Please file an issue on [github](https://github.com/pytorch/audio/issues/new?template=questions-help-support.md) to provide feedback on them.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Speech Recognition\nBuilding on the addition of the wav2letter model for speech recognition in the last release, we\u2019ve now added an [example wav2letter training pipeline](https://github.com/pytorch/audio/tree/master/examples/pipeline_wav2letter) with the LibriSpeech dataset.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Text-to-speech\nWith the goal of supporting text-to-speech applications, we added a vocoder based on the WaveRNN model, based on the implementation from [this repository](https://github.com/fatchord/WaveRNN). The original implementation was introduced in \"Efficient Neural Audio Synthesis\". We also provide an [example WaveRNN training pipeline](https://github.com/pytorch/audio/tree/master/examples/pipeline_wavernn) that uses the LibriTTS dataset added to torchaudio in this release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Source Separation\nWith the addition of the ConvTasNet model, based on the paper \"Conv-TasNet: Surpassing Ideal Time-Frequency Magnitude Masking for Speech Separation,\" torchaudio now also supports source separation. An [example ConvTasNet training pipeline](https://github.com/pytorch/audio/tree/master/examples/source_separation) is provided with the wsj-mix dataset.\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Adding a Contributor License Agreement for PyTorch'\nauthor: Team PyTorch\n---\n\nTo ensure the ongoing growth and success of the framework, we're introducing the use of the Apache Contributor License Agreement (CLA) for PyTorch. We care deeply about the broad community of contributors who make PyTorch such a great framework, so we want to take a moment to explain why we are adding a CLA.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Why Does PyTorch Need a CLA?\n\nCLAs help clarify that users and maintainers have the relevant rights to use and maintain code contributed to an open source project, while allowing contributors to retain ownership rights to their code.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
TPU Accelerator - Num Devices\n v4-64\n
GPT2 Parameter Count\n 16B\n
Layers Wrapped with FSDP\n GPT2Block\n
TFLOPs / Chip\n 275\n
PFLOPs / Step\n 50\n
Hardware Utilization\n 39%\n
\n\n\n\n### Differences Between FSDP & GSPMD", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "FSDP is a data parallelism technique that reduces device memory footprint by storing model parameters, optimizer states, and gradients all sharded. Note that the actual computation is still local to the device and requires all-gathering the sharded model parameters for both forward and backward passes, hence the name \u201cdata parallel\u201d. FSDP is one of the newest additions to PyTorch/XLA to scale large model training.\n\nGSPMD on the other hand, is a general parallelization system that enables various types of parallelisms, including both data and model parallelisms. PyTorch/XLA provides a sharding annotation API and XLAShardedTensor abstraction, so a user can annotate any tensor with sharding specs in the PyTorch program. Developers don\u2019t need to manually implement sharded computations or inject collective communications ops to get it right. The XLA compiler does the work so that each computation can run in a distributed manner on multiple devices.\n\n\n### Examples & Preliminary Results", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "To learn about PyTorch/XLA parallelism sharding API, visit our [RFC](https://github.com/pytorch/xla/issues/3871) and see the [Sample Code](https://github.com/pytorch/xla/tree/r2.0/test/spmd) references. Below is a simple example to enable data and model parallelism.\n\n```\nmodel = SimpleLinear().to(xm.xla_device())\n# Sharding annotate the linear layer weights.\nxs.mark_sharding(model.fc1.weight, mesh, partition_spec)\n# Training loop\nmodel.train()\nfor step, (data, target) in enumerate(loader):\n optimizer.zero_grad()\n data = data.to(xm.xla_device())\n target = target.to(xm.xla_device())\n # Sharding annotate input data, we can shard any input\n # dimensions. Sharidng the batch dimension enables \n # data parallelism, sharding the feature dimension enables\n # spatial partitioning.\n xs.mark_sharding(data, mesh, partition_spec)\n ouput = model(data)\n loss = loss_fn(output, target)\n optimizer.step()\n xm.mark_step()\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "The following graph highlights the memory efficiency benefits of PyTorch/XLA FSDP and SPMD on Cloud TPU v4-8 running ResNet50.\n\n\n![Batch Size Scaling with Spatial Partitioning](/assets/images/2023-03-22-batchsizescaling.svg){:width=\"100%\"}\n\n\n\n## Closing Thoughts\u2026\n\nWe are excited to bring these features to the PyTorch community, and this is really just the beginning. Areas like dynamic shapes, deeper support for OpenXLA and many others are in development and we plan to put out more blogs to dive into the details. PyTorch/XLA is developed fully open source and we invite you to join the community of developers by filing issues, submitting pull requests, and sending RFCs on [GitHub](github.com/pytorch/xla). You can try PyTorch/XLA on a variety of XLA devices including TPUs and GPUs. [Here](https://colab.sandbox.google.com/github/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb) is how to get started.\n\nCongratulations again to the PyTorch community on this milestone!\n\nCheers,\n\nThe PyTorch Team at Google", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Compromised PyTorch-nightly dependency chain between December 25th and December 30th, 2022.\"\nauthor: The PyTorch Team\n---\n\nIf you installed PyTorch-nightly on Linux via pip between December 25, 2022 and December 30, 2022, please uninstall it and torchtriton immediately, and use the latest nightly binaries (newer than Dec 30th 2022).\n\n```bash\n$ pip3 uninstall -y torch torchvision torchaudio torchtriton\n$ pip3 cache purge\n```\n\nPyTorch-nightly Linux packages installed via pip during that time installed a dependency, torchtriton, which was compromised on the Python Package Index (PyPI) code repository and ran a malicious binary. This is what is known as a supply chain attack and directly affects dependencies for packages that are hosted on public package indices.\n\n**NOTE:** Users of the PyTorch **stable** packages **are not** affected by this issue.\n\n\n## How to check if your Python environment is affected", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} +{"page_content": "The following command searches for the malicious binary in the torchtriton package (`PYTHON_SITE_PACKAGES/triton/runtime/triton`) and prints out whether your current Python environment is affected or not.\n\n```bash\npython3 -c \"import pathlib;import importlib.util;s=importlib.util.find_spec('triton'); affected=any(x.name == 'triton' for x in (pathlib.Path(s.submodule_search_locations[0] if s is not None else '/' ) / 'runtime').glob('*'));print('You are {}affected'.format('' if affected else 'not '))\"\n```\n\nThe malicious binary is executed when the triton package is imported, which requires explicit code to do and is not PyTorch\u2019s default behavior.\n\n## The Background", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} +{"page_content": "## The Background\n\nAt around 4:40pm GMT on December 30 (Friday), we learned about a malicious dependency package (`torchtriton`) that was uploaded to the Python Package Index (PyPI) code repository with the same package name as the one we ship on the [PyTorch nightly package index](https://download.pytorch.org/whl/nightly). Since the [PyPI index takes precedence](https://github.com/pypa/pip/issues/8606), this malicious package was being installed instead of the version from our official repository. This design enables somebody to register a package by the same name as one that exists in a third party index, and pip will install their version by default.\n\nThis malicious package has the same name `torchtriton` but added in code that uploads sensitive data from the machine.\n\n\n## What we know\n\ntorchtriton on PyPI contains a malicious triton binary which is installed at `PYTHON_SITE_PACKAGES/triton/runtime/triton`. Its SHA256 hash is listed below.", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} +{"page_content": "`SHA256(triton)= 2385b29489cd9e35f92c072780f903ae2e517ed422eae67246ae50a5cc738a0e`\n\nThe binary\u2019s main function does the following:\n\n- Get system information\n - nameservers from `/etc/resolv.conf`\n - hostname from `gethostname()`\n - current username from `getlogin()`\n - current working directory name from `getcwd()`\n - environment variables\n- Read the following files\n - `/etc/hosts`\n - `/etc/passwd`\n - The first 1,000 files in `$HOME/*`\n - `$HOME/.gitconfig`\n - `$HOME/.ssh/*`\n- Upload all of this information, including file contents, via encrypted DNS queries to the domain *.h4ck[.]cfd, using the DNS server wheezy[.]io\n\nThe binary\u2019s file upload functionality is limited to files less than 99,999 bytes in size. It also uploads only the first 1,000 files in $HOME (but all files < 99,999 bytes in the .ssh directory).\n\n## Steps taken towards mitigation", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} +{"page_content": "- torchtriton has been removed as a dependency for our nightly packages and replaced with pytorch-triton ([pytorch/pytorch#91539](https://github.com/pytorch/pytorch/pull/91539)) and a dummy package registered on PyPI (so that this issue doesn\u2019t repeat)\n- All nightly packages that depend on torchtriton have been removed from our package indices at https://download.pytorch.org until further notice\n- We have reached out to the PyPI security team to get proper ownership of the `torchtriton` package on PyPI and to delete the malicious version", "metadata": {"source": "https://pytorch.org/blog/compromised-nightly-dependency/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Running PyTorch Models on Jetson Nano'\nauthor: Jeff Tang, Hamid Shojanazeri, Geeta Chauhan\nfeatured-img: 'assets/images/pytorch-logo.jpg'\n---\n\n### Overview\nNVIDIA [Jetson Nano](https://developer.nvidia.com/embedded/jetson-nano-developer-kit), part of the [Jetson family of products](https://developer.nvidia.com/embedded/jetson-modules) or Jetson modules, is a small yet powerful Linux (Ubuntu) based embedded computer with 2/4GB GPU. With it, you can run many PyTorch models efficiently. This document summarizes our experience of running different deep learning models using 3 different mechanisms on Jetson Nano:\n\n 1. Jetson Inference the higher-level NVIDIA API that has built-in support for running most common computer vision models which can be transfer-learned with PyTorch on the Jetson platform.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "2. TensorRT, an SDK for high-performance inference from NVIDIA that requires the conversion of a PyTorch model to ONNX, and then to the TensorRT engine file that the TensorRT runtime can run.\n\n 3. PyTorch with the direct PyTorch API `torch.nn` for inference.\n\n### Setting up Jetson Nano\nAfter purchasing a Jetson Nano [here](https://developer.nvidia.com/buy-jetson?product=jetson_nano&location=US), simply follow the clear step-by-step [instructions](https://developer.nvidia.com/embedded/learn/get-started-jetson-nano-devkit) to download and write the Jetson Nano Developer Kit SD Card Image to a microSD card, and complete the setup. After the setup is done and the Nano is booted, you\u2019ll see the standard Linux prompt along with the username and the Nano name used in the setup.\n\nTo check the GPU status on Nano, run the following commands:\n\n```\nsudo pip3 install jetson-stats\nsudo jtop\n```\n\nYou\u2019ll see information, including:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\nYou can also see the installed CUDA version:\n\n```\n$ ls -lt /usr/local\nlrwxrwxrwx 1 root root 22 Aug 2 01:47 cuda -> /etc/alternatives/cuda\nlrwxrwxrwx 1 root root 25 Aug 2 01:47 cuda-10 -> /etc/alternatives/cuda-10\ndrwxr-xr-x 12 root root 4096 Aug 2 01:47 cuda-10.2\n```\n\nTo use a camera on Jetson Nano, for example, Arducam 8MP IMX219, follow the instructions [here](https://www.arducam.com/docs/camera-for-jetson-nano/mipi-camera-modules-for-jetson-nano/driver-installation/) or run the commands below after [installing a camera module](https://developer.nvidia.com/embedded/learn/jetson-nano-2gb-devkit-user-guide#id-.JetsonNano2GBDeveloperKitUserGuidevbatuu_v1.0-Camera):\n\n```\ncd ~\nwget https://github.com/ArduCAM/MIPI_Camera/releases/download/v0.0.3/install_full.sh\nchmod +x install_full.sh\n./install_full.sh -m arducam\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "Another way to do this is to use the original Jetson Nano camera driver:\n\n```\nsudo dpkg -r arducam-nvidia-l4t-kernel\nsudo shutdown -r now\n```\n\nThen, use ls /dev/video0 to confirm the camera is found:\n\n```\n$ ls /dev/video0\n/dev/video0\n```\n\nAnd finally, the following command to see the camera in action:\n\n```\nnvgstcapture-1.0 --orientation=2\n```\n\n### Using Jetson Inference\nNVIDIA [Jetson Inference](https://github.com/dusty-nv/jetson-inference) API offers the easiest way to run image recognition, object detection, semantic segmentation, and pose estimation models on Jetson Nano. Jetson Inference has TensorRT built-in, so it\u2019s very fast. \n\nTo test run Jetson Inference, first clone the repo and download the models:\n\n```\ngit clone --recursive https://github.com/dusty-nv/jetson-inference\ncd jetson-inference\n```\n\nThen use the pre-built [Docker Container](https://github.com/dusty-nv/jetson-inference/blob/master/docs/jetpack-setup-2.md) that already has PyTorch installed to test run the models:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "```\ndocker/run.sh --volume ~/jetson_inference:/jetson_inference\n```\n\nTo run image recognition, object detection, semantic segmentation, and pose estimation models on test images, use the following:\n\n```\ncd build/aarch64/bin\n./imagenet.py images/jellyfish.jpg /jetson_inference/jellyfish.jpg\n./segnet.py images/dog.jpg /jetson_inference/dog.jpeg\n./detectnet.py images/peds_0.jpg /jetson_inference/peds_0.jpg\n./posenet.py images/humans_0.jpg /jetson_inference/pose_humans_0.jpg\n```\n\nFour result images from running the four different models will be generated. Exit the docker image to see them:\n\n```\n$ ls -lt ~/jetson_inference/\n-rw-r--r-- 1 root root 68834 Oct 15 21:30 pose_humans_0.jpg\n-rw-r--r-- 1 root root 914058 Oct 15 21:30 peds_0.jpg\n-rw-r--r-- 1 root root 666239 Oct 15 21:30 dog.jpeg\n-rw-r--r-- 1 root root 179760 Oct 15 21:29 jellyfish.jpg\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "
\n \"Using\n \"Using\n
\n\n\n
\n \"Using\n \"Using\n
\n\nYou can also use the docker image to run PyTorch models because the image has PyTorch, torchvision and torchaudio installed:\n\n```\n# pip list|grep torch\ntorch (1.9.0)\ntorchaudio (0.9.0a0+33b2469)\ntorchvision (0.10.0a0+300a8a4)\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "Although Jetson Inference includes models already converted to the TensorRT engine file format, you can fine-tune the models by following the steps in Transfer Learning with PyTorch (for Jetson Inference) [here](https://github.com/dusty-nv/jetson-inference/blob/master/docs/pytorch-transfer-learning.md).\n\n### Using TensorRT\n[TensorRT](https://docs.nvidia.com/deeplearning/tensorrt/) is an SDK for high-performance inference from NVIDIA. Jetson Nano supports TensorRT via the Jetpack SDK, included in the SD Card image used to set up Jetson Nano. To confirm that TensorRT is already installed in Nano, `run dpkg -l|grep -i tensorrt`:\n\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "Theoretically, TensorRT can be used to \u201ctake a trained PyTorch model and optimize it to run more efficiently during inference on an NVIDIA GPU.\u201d Follow the instructions and code in the [notebook](https://github.com/NVIDIA/TensorRT/blob/master/quickstart/IntroNotebooks/4.%20Using%20PyTorch%20through%20ONNX.ipynb) to see how to use PyTorch with TensorRT through ONNX on a torchvision Resnet50 model:\n\n1. How to convert the model from PyTorch to ONNX;\n\n2. How to convert the ONNX model to a TensorRT engine file; \n\n3. How to run the engine file with the TensorRT runtime for performance improvement: inference time improved from the original 31.5ms/19.4ms (FP32/FP16 precision) to 6.28ms (TensorRT).", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "You can replace the Resnet50 model in the notebook code with another PyTorch model, go through the conversion process above, and run the finally converted model TensorRT engine file with the TensorRT runtime to see the optimized performance. But be aware that due to the Nano GPU memory size, models larger than 100MB are likely to fail to run, with the following error information:\n\n`Error Code 1: Cuda Runtime (all CUDA-capable devices are busy or unavailable)`\n\nYou may also see an error when converting a PyTorch model to ONNX model, which may be fixed by replacing: \n\n`torch.onnx.export(resnet50, dummy_input, \"resnet50_pytorch.onnx\", verbose=False)`\n\nwith:\n\n`torch.onnx.export(model, dummy_input, \"deeplabv3_pytorch.onnx\", opset_version=11, verbose=False)`\n\n### Using PyTorch \nFirst, to download and install PyTorch 1.9 on Nano, run the following commands (see [here](https://forums.developer.nvidia.com/t/pytorch-for-jetson-version-1-10-now-available/72048) for more information):", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "```\nwget https://nvidia.box.com/shared/static/p57jwntv436lfrd78inwl7iml6p13fzh.whl -O torch-1.8.0-cp36-cp36m-linux_aarch64.whl -O torch-1.9.0-cp36-cp36m-linux_aarch64.whl\nsudo apt-get install python3-pip libopenblas-base libopenmpi-dev \npip3 install Cython\npip3 install numpy torch-1.9.0-cp36-cp36m-linux_aarch64.whl\n```\n\nTo download and install torchvision 0.10 on Nano, run the commands below:\n\n```\nhttps://drive.google.com/uc?id=1tU6YlPjrP605j4z8PMnqwCSoP6sSC91Z\npip3 install torchvision-0.10.0a0+300a8a4-cp36-cp36m-linux_aarch64.whl\n```\n\nAfter the steps above, run this to confirm:\n```\n$ pip3 list|grep torch\ntorch (1.9.0)\ntorchvision (0.10.0)\n```\n\nYou can also use the docker image described in the section *Using Jetson Inference* (which also has PyTorch and torchvision installed), to skip the manual steps above.\n\nThe official [YOLOv5](https://github.com/ultralytics/yolov5) repo is used to run the PyTorch YOLOv5 model on Jetson Nano. After logging in to Jetson Nano, follow the steps below:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "* Get the repo and install what\u2019s required:\n\n```\ngit clone https://github.com/ultralytics/yolov5\ncd yolov5\npip install -r requirements.txt\n```\n\n* Run `python3 detect.py`, which by default uses the PyTorch yolov5s.pt model. You should see something like:\n\n```\ndetect: weights=yolov5s.pt, source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False\nYOLOv5 \ud83d\ude80 v5.0-499-g48b00db torch 1.9.0 CUDA:0 (NVIDIA Tegra X1, 3956.1015625MB)\n\nFusing layers... \nModel Summary: 224 layers, 7266973 parameters, 0 gradients\nimage 1/5 /home/jeff/repos/yolov5-new/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, 1 fire hydrant, Done. (0.142s)\n...\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "**The inference time on Jetson Nano GPU is about 140ms, more than twice as fast as the inference time on iOS or Android (about 330ms).**\n\nIf you get an error `\u201cImportError: The _imagingft C module is not installed.\u201d` then you need to reinstall pillow:\n```\nsudo apt-get install libpng-dev\nsudo apt-get install libfreetype6-dev\npip3 uninstall pillow\npip3 install --no-cache-dir pillow\n```\n\nAfter successfully completing the `python3 detect.py` run, the object detection results of the test images located in `data/images` will be in the `runs/detect/exp` directory. To test the detection with a live webcam instead of local images, use the `--source 0` parameter when running `python3 detect.py`):\n\n```\n~/repos/yolov5$ ls -lt runs/detect/exp10\ntotal 1456\n-rw-rw-r-- 1 jeff jeff 254895 Oct 15 16:12 zidane.jpg\n-rw-rw-r-- 1 jeff jeff 202674 Oct 15 16:12 test3.png\n-rw-rw-r-- 1 jeff jeff 217117 Oct 15 16:12 test2.jpg\n-rw-rw-r-- 1 jeff jeff 305826 Oct 15 16:12 test1.png\n-rw-rw-r-- 1 jeff jeff 495760 Oct 15 16:12 bus.jpg\n```", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "Using the same test files used in the PyTorch iOS YOLOv5 demo app or Android YOLOv5 demo app, you can compare the results generated with running the YOLOv5 PyTorch model on mobile devices and Jetson Nano:\n\n
\n \"PyTorch\n \"PyTorch\n
\nFigure 1. PyTorch YOLOv5 on Jetson Nano. \n\n
\n \"PyTorch\n \"PyTorch\n
\nFigure 2. PyTorch YOLOv5 on iOS.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "
\n \"PyTorch\n \"PyTorch\n
\nFigure 3. PyTorch YOLOv5 on Android. \n\n### Summary\nBased on our experience of running different PyTorch models for potential demo apps on Jetson Nano, we see that even Jetson Nano, a lower-end of the Jetson family of products, provides a powerful GPU and embedded system that can directly run some of the latest PyTorch models, pre-trained or transfer learned, efficiently.\n\nBuilding PyTorch demo apps on Jetson Nano can be similar to building PyTorch apps on Linux, but you can also choose to use TensorRT after converting the PyTorch models to the TensorRT engine file format.", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "But if you just need to run some common computer vision models on Jetson Nano using NVIDIA\u2019s Jetson Inference which supports image recognition, object detection, semantic segmentation, and pose estimation models, then this is the easiest way.\n\n\n### References\nTorch-TensorRT, a compiler for PyTorch via TensorRT:\n[https://github.com/NVIDIA/Torch-TensorRT/](https://github.com/NVIDIA/Torch-TensorRT/)\n\nJetson Inference docker image details:\n[https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-docker.md](https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-docker.md)\n\nA guide to using TensorRT on the NVIDIA Jetson Nano:\n[https://docs.donkeycar.com/guide/robot_sbc/tensorrt_jetson_nano/](https://docs.donkeycar.com/guide/robot_sbc/tensorrt_jetson_nano/) \nincluding:", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "1. Use Jetson as a portable GPU device to run an NN chess engine model: \n[https://medium.com/@ezchess/jetson-lc0-running-leela-chess-zero-on-nvidia-jetson-a-portable-gpu-device-a213afc9c018](https://medium.com/@ezchess/jetson-lc0-running-leela-chess-zero-on-nvidia-jetson-a-portable-gpu-device-a213afc9c018)\n\n2. A MaskEraser app using PyTorch and torchvision, installed directly with pip:\n[https://github.com/INTEC-ATI/MaskEraser#install-pytorch](https://github.com/INTEC-ATI/MaskEraser#install-pytorch)", "metadata": {"source": "https://pytorch.org/blog/running-pytorch-models-on-jetson-nano/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.9 Release, including torch.linalg and Mobile Interpreter'\nauthor: Team PyTorch \n---\n\nWe are excited to announce the release of PyTorch 1.9. The release is composed of more than 3,400 commits since 1.8, made by 398 contributors. The release notes are available [here](https://github.com/pytorch/pytorch/releases). Highlights include:\n1. Major improvements to support scientific computing, including *torch.linalg*, *torch.special*, and Complex Autograd\n2. Major improvements in on-device binary size with Mobile Interpreter\n3. Native support for elastic-fault tolerance training through the upstreaming of TorchElastic into PyTorch Core\n4. Major updates to the PyTorch RPC framework to support large scale distributed training with GPU support\n5. New APIs to optimize performance and packaging for model inference deployment \n6. Support for Distributed training, GPU utilization and SM efficiency in the PyTorch Profiler", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Along with 1.9, we are also releasing major updates to the PyTorch libraries, which you can read about in [this blog post](https://pytorch.org/blog/pytorch-1.9-new-library-releases/). \n\nWe\u2019d like to thank the community for their support and work on this latest release. We\u2019d especially like to thank Quansight and Microsoft for their contributions.\n\nFeatures in PyTorch releases are classified as Stable, Beta, and Prototype. You can learn more about the definitions in [this blog post](https://pytorch.org/blog/pytorch-feature-classification-changes/). \n\n# Frontend APIs\n\n### (Stable) *torch.linalg*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "In 1.9, the *torch.linalg* module is moving to a stable release. Linear algebra is essential to deep learning and scientific computing, and the *torch.linalg* module extends PyTorch\u2019s support for it with implementations of every function from [NumPy\u2019s linear algebra module](https://numpy.org/doc/stable/reference/routines.linalg.html) (now with support for accelerators and autograd) and more, like [*torch.linalg.matrix_norm*](https://pytorch.org/docs/1.9.0/generated/torch.linalg.matrix_norm.html?highlight=matrix_norm#torch.linalg.matrix_norm) and [*torch.linalg.householder_product*](https://pytorch.org/docs/1.9.0/generated/torch.linalg.householder_product.html?highlight=householder_product#torch.linalg.householder_product). This makes the module immediately familiar to users who have worked with NumPy. Refer to [the documentation](https://pytorch.org/docs/1.9.0/linalg.html?highlight=linalg#module-torch.linalg) here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "We plan to publish another blog post with more details on the *torch.linalg* module next week!\n\n### (Stable) Complex Autograd \n\nThe Complex Autograd feature, released as a beta in PyTorch 1.8, is now stable. Since the beta release, we have extended support for Complex Autograd for over 98% operators in PyTorch 1.9, improved testing for complex operators by adding more OpInfos, and added greater validation through TorchAudio migration to native complex tensors (refer to [this issue](https://github.com/pytorch/audio/issues/1337)). \n\nThis feature provides users the functionality to calculate complex gradients and optimize real valued loss functions with complex variables. This is a required feature for multiple current and downstream prospective users of complex numbers in PyTorch like TorchAudio, ESPNet, Asteroid, and FastMRI. Refer to [the documentation](https://pytorch.org/docs/1.9.0/notes/autograd.html#autograd-for-complex-numbers) for more details. \n\n### (Stable) torch.use_deterministic_algorithms()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "To help with debugging and writing reproducible programs, PyTorch 1.9 includes a *torch.use_determinstic_algorithms* option. When this setting is enabled, operations will behave deterministically, if possible, or throw a runtime error if they might behave nondeterministically. Here are a couple examples:\n\n```python\n>>> a = torch.randn(100, 100, 100, device='cuda').to_sparse()\n>>> b = torch.randn(100, 100, 100, device='cuda')\n\n# Sparse-dense CUDA bmm is usually nondeterministic\n>>> torch.bmm(a, b).eq(torch.bmm(a, b)).all().item()\nFalse\n\n>>> torch.use_deterministic_algorithms(True)\n\n# Now torch.bmm gives the same result each time, but with reduced performance\n>>> torch.bmm(a, b).eq(torch.bmm(a, b)).all().item()\nTrue\n\n# CUDA kthvalue has no deterministic algorithm, so it throws a runtime error\n>>> torch.zeros(10000, device='cuda').kthvalue(1)\nRuntimeError: kthvalue CUDA does not have a deterministic implementation...\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 1.9 adds deterministic implementations for a number of indexing operations, too, including *index_add*, *index_copy*, and *index_put with accum=False*. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.use_deterministic_algorithms.html?highlight=use_deterministic#torch.use_deterministic_algorithms) and [reproducibility note](https://pytorch.org/docs/1.9.0/notes/randomness.html?highlight=reproducibility).\n\n### (Beta) *torch.special*\n\nA *torch.special* module, analogous to [SciPy\u2019s special module](https://docs.scipy.org/doc/scipy/reference/special.html), is now available in beta. This module contains many functions useful for scientific computing and working with distributions such as *iv*, *ive*, *erfcx*, *logerfc*, and *logerfcx*. Refer to [the documentation](https://pytorch.org/docs/master/special.html) for more details. \n\n### (Beta) nn.Module parameterization", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "```nn.Module``` parameterization allows users to parametrize any parameter or buffer of an ```nn.Module``` without modifying the ```nn.Module``` itself. It allows you to constrain the space in which your parameters live without the need for special optimization methods.\n\nThis also contains a new implementation of the ```spectral_norm``` parametrization for PyTorch 1.9. More parametrization will be added to this feature (weight_norm, matrix constraints and part of pruning) for the feature to become stable in 1.10. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.nn.utils.parametrizations.spectral_norm.html?highlight=parametrize) and [tutorial](https://pytorch.org/tutorials/intermediate/parametrizations.html).\n\n# PyTorch Mobile\n\n### (Beta) Mobile Interpreter \n\nWe are releasing Mobile Interpreter, a streamlined version of the PyTorch runtime, in beta. The Interpreter will execute PyTorch programs in edge devices, with reduced binary size footprint.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Mobile Interpreter is one of the top requested features for PyTorch Mobile. This new release will significantly reduce binary size compared with the current on-device runtime. In order for you to get the binary size improvements with our interpreter (which can reduce the binary size up to ~75% for a typical application) follow these instructions. As an example, using Mobile Interpreter, we can reach 2.6 MB compressed with MobileNetV2 in arm64-v7a Android. With this latest release we are making it much simpler to integrate the interpreter by providing pre-built libraries for iOS and Android.\n\n### TorchVision Library", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Starting from 1.9, users can use the TorchVision library on their iOS/Android apps. The Torchvision library contains the C++ TorchVision ops and needs to be linked together with the main PyTorch library for iOS, for Android it can be added as a gradle dependency. This allows using TorchVision prebuilt MaskRCNN operators for object detections and segmentation. To learn more about the library, please refer to our tutorials and [demo apps](https://github.com/pytorch/android-demo-app/tree/master/D2Go). \n\n### Demo apps", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "### Demo apps\n\nWe are releasing a new video app based on [PyTorch Video](https://pytorchvideo.org/) library and an updated speech recognition app based on the latest torchaudio, wave2vec model. Both are available on [iOS](https://github.com/pytorch/ios-demo-app) and [Android](https://github.com/pytorch/android-demo-app). In addition, we have updated the seven Computer Vision and three Natural Language Processing demo apps, including the HuggingFace DistilBERT, and the DeiT vision transformer models, with PyTorch Mobile v1.9. With the addition of these two apps, we now offer a full suite of demo apps covering image, text, audio, and video. To get started check out our [iOS demo apps](https://github.com/pytorch/ios-demo-app) and [Android demo apps](https://github.com/pytorch/android-demo-app).\n\n
\n \n
\n\n# Distributed Training\n\n### (Beta) TorchElastic is now part of core", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "[TorchElastic](https://github.com/pytorch/pytorch/issues/50621), which was open sourced over a year ago in the [pytorch/elastic](https://github.com/pytorch/elastic) github repository, is a runner and coordinator for PyTorch worker processes. Since then, it has been adopted by various distributed torch use-cases: 1) [deepspeech.pytorch](https://medium.com/pytorch/training-deepspeech-using-torchelastic-ad013539682) 2) [pytorch-lightning](https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#torchelastic) 3) [Kubernetes CRD](https://github.com/pytorch/elastic/blob/master/kubernetes/README.md). Now, it is part of PyTorch core.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "As its name suggests, the core function of TorcheElastic is to gracefully handle scaling events. A notable corollary of elasticity is that peer discovery and rank assignment are built into TorchElastic enabling users to run distributed training on preemptible instances without requiring a gang scheduler. As a side note, [etcd](https://etcd.io/) used to be a hard dependency of TorchElastic. With the upstream, this is no longer the case since we have added a \u201cstandalone\u201d rendezvous based on c10d::Store. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/distributed.elastic.html).\n\n### (Beta) Distributed Training Updates\n\nIn addition to TorchElastic, there are a number of beta features available in the distributed package:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "* **(Beta) CUDA support is available in RPC**: Compared to CPU RPC and general-purpose RPC frameworks, CUDA RPC is a much more efficient way for P2P Tensor communication. It is built on top of TensorPipe which can automatically choose a communication channel for each Tensor based on Tensor device type and channel availability on both the caller and the callee. Existing TensorPipe channels cover NVLink, InfiniBand, SHM, CMA, TCP, etc. See [this recipe](https://pytorch.org/tutorials/recipes/cuda_rpc.html) for how CUDA RPC helps to attain 34x speedup compared to CPU RPC.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "* **(Beta) ZeroRedundancyOptimizer**: ZeroRedundancyOptimizer can be used in conjunction with DistributedDataParallel to reduce the size of per-process optimizer states. The idea of ZeroRedundancyOptimizer comes from [DeepSpeed/ZeRO project](https://github.com/microsoft/DeepSpeed) and [Marian](https://github.com/marian-nmt/marian-dev), where the optimizer in each process owns a shard of model parameters and their corresponding optimizer states. When running `step()`, each optimizer only updates its own parameters, and then uses collective communication to synchronize updated parameters across all processes. Refer to [this documentation](https://pytorch.org/docs/master/distributed.optim.html) and this [tutorial](https://pytorch.org/tutorials/recipes/zero_redundancy_optimizer.html) to learn more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "* **(Beta) Support for profiling distributed collectives**: PyTorch\u2019s profiler tools, *torch.profiler* and *torch.autograd.profiler*, are able to profile distributed collectives and point to point communication primitives including allreduce, alltoall, allgather, send/recv, etc. This is enabled for all backends supported natively by PyTorch: gloo, mpi, and nccl. This can be used to debug performance issues, analyze traces that contain distributed communication, and gain insight into performance of applications that use distributed training. To learn more, refer to [this documentation](https://pytorch.org/docs/1.9.0/distributed.html#profiling-collective-communication). \n\n# Performance Optimization and Tooling\n\n### (Stable) Freezing API", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Module Freezing is the process of inlining module parameters and attributes values as constants into the TorchScript internal representation. This allows further optimization and specialization of your program, both for TorchScript optimizations and lowering to other backends. It is used by [optimize_for_mobile API](https://github.com/pytorch/pytorch/blob/master/torch/utils/mobile_optimizer.py), ONNX, and others. \n\nFreezing is recommended for model deployment. It helps TorchScript JIT optimizations optimize away overhead and bookkeeping that is necessary for training, tuning, or debugging PyTorch models. It enables graph fusions that are not semantically valid on non-frozen graphs - such as fusing Conv-BN. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/generated/torch.jit.freeze.html).\n\n### (Beta) PyTorch Profiler \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "The new PyTorch Profiler graduates to beta and leverages [Kineto](https://github.com/pytorch/kineto/) for GPU profiling, TensorBoard for visualization and is now the standard across our tutorials and documentation. \n\nPyTorch 1.9 extends support for the new *torch.profiler* API to more builds, including Windows and Mac and is recommended in most cases instead of the previous *torch.autograd.profiler* API. The new API supports existing profiler features, integrates with CUPTI library (Linux-only) to trace on-device CUDA kernels and provides support for long-running jobs, e.g.:\n\n```python\ndef trace_handler(p):\n output = p.key_averages().table(sort_by=\"self_cuda_time_total\", row_limit=10)\n print(output)\n p.export_chrome_trace(\"/tmp/trace_\" + str(p.step_num) + \".json\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "with profile(\n activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],\n # schedule argument specifies the iterations on which the profiler is active\n schedule=torch.profiler.schedule(\n wait=1,\n warmup=1,\n active=2),\n # on_trace_ready argument specifies the handler for the traces\n on_trace_ready=trace_handler\n) as p:\n for idx in range(8):\n model(inputs)\n # profiler will trace iterations 2 and 3, and then 6 and 7 (counting from zero)\n p.step()\n```\n\nMore usage examples can be found on the [profiler recipe page](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html). \n\nThe PyTorch Profiler Tensorboard plugin has new features for:\n* Distributed Training summary view with communications overview for NCCL\n* GPU Utilization and SM Efficiency in Trace view and GPU operators view\n* Memory Profiling view\n* Jump to source when launched from Microsoft VSCode\n* Ability for load traces from cloud object storage systems", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Inference Mode API \n\nInference Mode API allows significant speed-up for inference workloads while remaining safe and ensuring no incorrect gradients can ever be computed. It offers the best possible performance when no autograd is required. For more details, refer to [the documentation for inference mode itself](https://pytorch.org/docs/1.9.0/generated/torch.inference_mode.html?highlight=inference%20mode#torch.inference_mode) and [the documentation explaining when to use it and the difference with no_grad mode](https://pytorch.org/docs/1.9.0/notes/autograd.html#locally-disabling-gradient-computation).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) *torch.package* \n \n*torch.package* is a new way to package PyTorch models in a self-contained, stable format. A package will include both the model\u2019s data (e.g. parameters, buffers) and its code (model architecture). Packaging a model with its full set of Python dependencies, combined with a description of a conda environment with pinned versions, can be used to easily reproduce training. Representing a model in a self-contained artifact will also allow it to be published and transferred throughout a production ML pipeline while retaining the flexibility of a pure-Python representation. For more details, refer to [the documentation](https://pytorch.org/docs/1.9.0/package.html).\n\n### (Prototype) prepare_for_inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "prepare_for_inference is a new prototype feature that takes in a module and performs graph-level optimizations to improve inference performance, depending on the device. It is meant to be a PyTorch-native option that requires minimal changes to user\u2019s workflows. For more details, see [the documentation](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) for the Torchscript version [here](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) or the FX version [here](https://github.com/pytorch/pytorch/blob/master/torch/fx/experimental/optimization.py#L234).\n\n### (Prototype) Profile-directed typing in TorchScript", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "TorchScript has a hard requirement for source code to have type annotations in order for compilation to be successful. For a long time, it was only possible to add missing or incorrect type annotations through trial and error (i.e., by fixing the type-checking errors generated by *torch.jit.script* one by one), which was inefficient and time consuming. Now, we have enabled profile directed typing for *torch.jit.script* by leveraging existing tools like MonkeyType, which makes the process much easier, faster, and more efficient. For more details, refer to [the documentation](https://pytorch.org/docs/1.9.0/jit.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Thanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Facebook](https://www.facebook.com/pytorch/), [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), or [LinkedIn](https://www.linkedin.com/company/pytorch). \n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Developer Day 2020'\nauthor: Team PyTorch\n---\n\nStarting this year, we plan to host two separate events for PyTorch: one for developers and users to discuss core technical development, ideas and roadmaps called **\u201cDeveloper Day\u201d**, and another for the PyTorch ecosystem and industry communities to showcase their work and discover opportunities to collaborate called **\u201cEcosystem Day\u201d** (scheduled for early 2021).\n\n
\n \n
\n\nThe **PyTorch Developer Day** (#PTD2) is kicking off on November 12, 2020, 8AM PST with a full day of technical talks on a variety of topics, including updates to the core framework, new tools and libraries to support development across a variety of domains. You'll also see talks covering the latest research around systems and tooling in ML.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} +{"page_content": "For Developer Day, we have an online networking event limited to people composed of PyTorch maintainers and contributors, long-time stakeholders and experts in areas relevant to PyTorch\u2019s future. Conversations from the networking event will strongly shape the future of PyTorch. Hence, invitations are required to attend the networking event.\n\nAll talks will be livestreamed and available to the public.\n* [Livestream event page](https://www.facebook.com/events/802177440559164/)\n* [Apply for an invitation to the networking event](https://pytorchdeveloperday.fbreg.com/apply)\n\nVisit the [event website](https://pytorchdeveloperday.fbreg.com/) to learn more. We look forward to welcoming you to PyTorch Developer Day on November 12th! \n\nThank you,\n\nThe PyTorch team", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2020/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.7 released w/ CUDA 11, New APIs for FFTs, Windows support for Distributed training and more'\nauthor: Team PyTorch\n---\n\nToday, we\u2019re announcing the availability of PyTorch 1.7, along with updated domain libraries. The PyTorch 1.7 release includes a number of new APIs including support for NumPy-Compatible FFT operations, profiling tools and major updates to both distributed data parallel (DDP) and remote procedure call (RPC) based distributed training. In addition, several features moved to [stable](https://pytorch.org/docs/stable/index.html#pytorch-documentation) including custom C++ Classes, the memory profiler, extensions via custom tensor-like objects, user async functions in RPC and a number of other features in torch.distributed such as Per-RPC timeout, DDP dynamic bucketing and RRef helper.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "A few of the highlights include:\n* CUDA 11 is now officially supported with binaries available at [PyTorch.org](http://pytorch.org/)\n* Updates and additions to profiling and performance for RPC, TorchScript and Stack traces in the autograd profiler\n* (Beta) Support for NumPy compatible Fast Fourier transforms (FFT) via torch.fft\n* (Prototype) Support for Nvidia A100 generation GPUs and native TF32 format \n* (Prototype) Distributed training on Windows now supported\n* torchvision\n * (Stable) Transforms now support Tensor inputs, batch computation, GPU, and TorchScript\n * (Stable) Native image I/O for JPEG and PNG formats\n * (Beta) New Video Reader API\n* torchaudio\n * (Stable) Added support for speech rec (wav2letter), text to speech (WaveRNN) and source separation (ConvTasNet)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "To reiterate, starting PyTorch 1.6, features are now classified as stable, beta and prototype. You can see the detailed announcement [here](https://pytorch.org/blog/pytorch-feature-classification-changes/). Note that the prototype features listed in this blog are available as part of this release. \n\nFind the full release notes [here](https://github.com/pytorch/pytorch/releases). \n\n# Front End APIs\n## [Beta] NumPy Compatible torch.fft module\nFFT-related functionality is commonly used in a variety of scientific fields like signal processing. While PyTorch has historically supported a few FFT-related functions, the 1.7 release adds a new torch.fft module that implements FFT-related functions with the same API as NumPy.\n\nThis new module must be imported to be used in the 1.7 release, since its name conflicts with the historic (and now deprecated) torch.fft function.\n\n**Example usage:**\n```python\n>>> import torch.fft\n>>> t = torch.arange(4)\n>>> t\ntensor([0, 1, 2, 3])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": ">>> torch.fft.fft(t)\ntensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])\n\n>>> t = tensor([0.+1.j, 2.+3.j, 4.+5.j, 6.+7.j])\n>>> torch.fft.fft(t)\ntensor([12.+16.j, -8.+0.j, -4.-4.j, 0.-8.j])\n ```\n\n* [Documentation](https://pytorch.org/docs/stable/fft.html#torch-fft)\n\n## [Beta] C++ Support for Transformer NN Modules\nSince [PyTorch 1.5](https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/), we\u2019ve continued to maintain parity between the python and C++ frontend APIs. This update allows developers to use the nn.transformer module abstraction from the C++ Frontend. And moreover, developers no longer need to save a module from python/JIT and load into C++ as it can now be used it in C++ directly.\n* [Documentation](https://pytorch.org/cppdocs/api/classtorch_1_1nn_1_1_transformer_impl.html#_CPPv4N5torch2nn15TransformerImplE)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] torch.set_deterministic \nReproducibility (bit-for-bit determinism) may help identify errors when debugging or testing a program. To facilitate reproducibility, PyTorch 1.7 adds the ```torch.set_deterministic(bool)``` function that can direct PyTorch operators to select deterministic algorithms when available, and to throw a runtime error if an operation may result in nondeterministic behavior. By default, the flag this function controls is false and there is no change in behavior, meaning PyTorch may implement its operations nondeterministically by default. \n\nMore precisely, when this flag is true:\n* Operations known to not have a deterministic implementation throw a runtime error;\n* Operations with deterministic variants use those variants (usually with a performance penalty versus the non-deterministic version); and\n* ```torch.backends.cudnn.deterministic = True``` is set.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "Note that this is necessary, **but not sufficient**, for determinism **within a single run of a PyTorch program**. Other sources of randomness like random number generators, unknown operations, or asynchronous or distributed computation may still cause nondeterministic behavior.\n\nSee the documentation for ```torch.set_deterministic(bool)``` for the list of affected operations.\n* [RFC](https://github.com/pytorch/pytorch/issues/15359)\n* [Documentation](https://pytorch.org/docs/stable/generated/torch.set_deterministic.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "# Performance & Profiling\n## [Beta] Stack traces added to profiler\nUsers can now see not only operator name/inputs in the profiler output table but also where the operator is in the code. The workflow requires very little change to take advantage of this capability. The user uses the [autograd profiler](https://pytorch.org/docs/stable/autograd.html#profiler) as before but with optional new parameters: ```with_stack``` and ```group_by_stack_n```. Caution: regular profiling runs should not use this feature as it adds significant overhead.\n* [Detail](https://github.com/pytorch/pytorch/pull/43898/)\n* [Documentation](https://pytorch.org/docs/stable/autograd.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "# Distributed Training & RPC \n## [Stable] TorchElastic now bundled into PyTorch docker image\nTorchelastic offers a strict superset of the current ```torch.distributed.launch``` CLI with the added features for fault-tolerance and elasticity. If the user is not be interested in fault-tolerance, they can get the exact functionality/behavior parity by setting ```max_restarts=0``` with the added convenience of auto-assigned ```RANK``` and ```MASTER_ADDR|PORT``` (versus manually specified in ```torch.distributed.launch)```.\n\nBy bundling ```torchelastic``` in the same docker image as PyTorch, users can start experimenting with TorchElastic right-away without having to separately install ```torchelastic```. In addition to convenience, this work is a nice-to-have when adding support for elastic parameters in the existing Kubeflow\u2019s distributed PyTorch operators.\n* [Usage examples and how to get started](https://pytorch.org/elastic/0.2.0/examples.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] Support for uneven dataset inputs in DDP\nPyTorch 1.7 introduces a new context manager to be used in conjunction with models trained using ```torch.nn.parallel.DistributedDataParallel``` to enable training with uneven dataset size across different processes. This feature enables greater flexibility when using DDP and prevents the user from having to manually ensure dataset sizes are the same across different process. With this context manager, DDP will handle uneven dataset sizes automatically, which can prevent errors or hangs at the end of training.\n* [RFC](https://github.com/pytorch/pytorch/issues/38174)\n* [Documentation](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel.join)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] NCCL Reliability - Async Error/Timeout Handling\nIn the past, NCCL training runs would hang indefinitely due to stuck collectives, leading to a very unpleasant experience for users. This feature will abort stuck collectives and throw an exception/crash the process if a potential hang is detected. When used with something like torchelastic (which can recover the training process from the last checkpoint), users can have much greater reliability for distributed training. This feature is completely opt-in and sits behind an environment variable that needs to be explicitly set in order to enable this functionality (otherwise users will see the same behavior as before).\n* [RFC](https://github.com/pytorch/pytorch/issues/46874)\n* [Documentation](https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] TorchScript ```rpc_remote``` and ```rpc_sync```\n```torch.distributed.rpc.rpc_async``` has been available in TorchScript in prior releases. For PyTorch 1.7, this functionality will be extended the remaining two core RPC APIs, ```torch.distributed.rpc.rpc_sync``` and ```torch.distributed.rpc.remote```. This will complete the major RPC APIs targeted for support in TorchScript, it allows users to use the existing python RPC APIs within TorchScript (in a script function or script method, which releases the python Global Interpreter Lock) and could possibly improve application performance in multithreaded environment.\n* [Documentation](https://pytorch.org/docs/stable/rpc.html#rpc)\n* [Usage examples](https://github.com/pytorch/pytorch/blob/58ed60c259834e324e86f3e3118e4fcbbfea8dd1/torch/testing/_internal/distributed/rpc/jit/rpc_test.py#L505-L525)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] Distributed optimizer with TorchScript support\nPyTorch provides a broad set of optimizers for training algorithms, and these have been used repeatedly as part of the python API. However, users often want to use multithreaded training instead of multiprocess training as it provides better resource utilization and efficiency in the context of large scale distributed training (e.g. Distributed Model Parallel) or any RPC-based training application). Users couldn\u2019t do this with with distributed optimizer before because we need to get rid of the python Global Interpreter Lock (GIL) limitation to achieve this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "In PyTorch 1.7, we are enabling the TorchScript support in distributed optimizer to remove the GIL, and make it possible to run optimizer in multithreaded applications. The new distributed optimizer has the exact same interface as before but it automatically converts optimizers within each worker into TorchScript to make each GIL free. This is done by leveraging a functional optimizer concept and allowing the distributed optimizer to convert the computational portion of the optimizer into TorchScript. This will help use cases like distributed model parallel training and improve performance using multithreading.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "Currently, the only optimizer that supports automatic conversion with TorchScript is ```Adagrad``` and all other optimizers will still work as before without TorchScript support. We are working on expanding the coverage to all PyTorch optimizers and expect more to come in future releases. The usage to enable TorchScript support is automatic and exactly the same with existing python APIs, here is an example of how to use this:\n\n```python\nimport torch.distributed.autograd as dist_autograd\nimport torch.distributed.rpc as rpc\nfrom torch import optim\nfrom torch.distributed.optim import DistributedOptimizer\n\nwith dist_autograd.context() as context_id:\n # Forward pass.\n rref1 = rpc.remote(\"worker1\", torch.add, args=(torch.ones(2), 3))\n rref2 = rpc.remote(\"worker1\", torch.add, args=(torch.ones(2), 1))\n loss = rref1.to_here() + rref2.to_here()\n\n # Backward pass.\n dist_autograd.backward(context_id, [loss.sum()])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "# Optimizer, pass in optim.Adagrad, DistributedOptimizer will\n # automatically convert/compile it to TorchScript (GIL-free)\n dist_optim = DistributedOptimizer(\n optim.Adagrad,\n [rref1, rref2],\n lr=0.05,\n )\n dist_optim.step(context_id)\n ```\n* [RFC](https://github.com/pytorch/pytorch/issues/46883)\n* [Documentation](https://pytorch.org/docs/stable/rpc.html#module-torch.distributed.optim)\n\n## [Beta] Enhancements to RPC-based Profiling\nSupport for using the PyTorch profiler in conjunction with the RPC framework was first introduced in PyTorch 1.6. In PyTorch 1.7, the following enhancements have been made:\n* Implemented better support for profiling TorchScript functions over RPC\n* Achieved parity in terms of profiler features that work with RPC\n* Added support for asynchronous RPC functions on the server-side (functions decorated with ```rpc.functions.async_execution)```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "Users are now able to use familiar profiling tools such as with ```torch.autograd.profiler.profile()``` and ```with torch.autograd.profiler.record_function```, and this works transparently with the RPC framework with full feature support, profiles asynchronous functions, and TorchScript functions.\n* [Design doc](https://github.com/pytorch/pytorch/issues/39675)\n* [Usage examples](https://pytorch.org/tutorials/recipes/distributed_rpc_profiling.html)\n\n## [Prototype] Windows support for Distributed Training\nPyTorch 1.7 brings prototype support for ```DistributedDataParallel``` and collective communications on the Windows platform. In this release, the support only covers Gloo-based ```ProcessGroup``` and ```FileStore```.\n\nTo use this feature across multiple machines, please provide a file from a shared file system in ```init_process_group```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "```python\n# initialize the process group\ndist.init_process_group(\n \"gloo\",\n # multi-machine example:\n # init_method = \"file://////{machine}/{share_folder}/file\"\n init_method=\"file:///{your local file path}\",\n rank=rank,\n world_size=world_size\n)\n\nmodel = DistributedDataParallel(local_model, device_ids=[rank])\n```\n* [Design doc](https://github.com/pytorch/pytorch/issues/42095)\n* [Documentation](https://pytorch.org/docs/master/distributed.html#backends-that-come-with-pytorch)\n* Acknowledgement ([gunandrose4u](https://github.com/gunandrose4u))\n\n# Mobile\nPyTorch Mobile supports both [iOS](https://pytorch.org/mobile/ios) and [Android](https://pytorch.org/mobile/android/) with binary packages available in [Cocoapods](https://cocoapods.org/) and [JCenter](https://mvnrepository.com/repos/jcenter) respectively. You can learn more about PyTorch Mobile [here](https://pytorch.org/mobile/home/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] PyTorch Mobile Caching allocator for performance improvements\nOn some mobile platforms, such as Pixel, we observed that memory is returned to the system more aggressively. This results in frequent page faults as PyTorch being a functional framework does not maintain state for the operators. Thus outputs are allocated dynamically on each execution of the op, for the most ops. To ameliorate performance penalties due to this, PyTorch 1.7 provides a simple caching allocator for CPU. The allocator caches allocations by tensor sizes and, is currently, available only via the PyTorch C++ API. The caching allocator itself is owned by client and thus the lifetime of the allocator is also maintained by client code. Such a client owned caching allocator can then be used with scoped guard, ```c10::WithCPUCachingAllocatorGuard```, to enable the use of cached allocation within that scope.\n**Example usage:**", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "```python\n#include \n.....\nc10::CPUCachingAllocator caching_allocator;\n // Owned by client code. Can be a member of some client class so as to tie the\n // the lifetime of caching allocator to that of the class.\n.....\n{\n c10::optional caching_allocator_guard;\n if (FLAGS_use_caching_allocator) {\n caching_allocator_guard.emplace(&caching_allocator);\n }\n ....\n model.forward(..);\n}\n...\n```\n**NOTE**: Caching allocator is only available on mobile builds, thus the use of caching allocator outside of mobile builds won\u2019t be effective.\n* [Documentation](https://github.com/pytorch/pytorch/blob/master/c10/mobile/CPUCachingAllocator.h#L13-L43)\n* [Usage examples](https://github.com/pytorch/pytorch/blob/master/binaries/speed_benchmark_torch.cc#L207)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "# torchvision\n## [Stable] Transforms now support Tensor inputs, batch computation, GPU, and TorchScript\ntorchvision transforms are now inherited from ```nn.Module``` and can be torchscripted and applied on torch Tensor inputs as well as on PIL images. They also support Tensors with batch dimensions and work seamlessly on CPU/GPU devices:\n```python\nimport torch\nimport torchvision.transforms as T\n\n# to fix random seed, use torch.manual_seed\n# instead of random.seed\ntorch.manual_seed(12)\n\ntransforms = torch.nn.Sequential(\n T.RandomCrop(224),\n T.RandomHorizontalFlip(p=0.3),\n T.ConvertImageDtype(torch.float),\n T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n)\nscripted_transforms = torch.jit.script(transforms)\n# Note: we can similarly use T.Compose to define transforms\n# transforms = T.Compose([...]) and \n# scripted_transforms = torch.jit.script(torch.nn.Sequential(*transforms.transforms))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "tensor_image = torch.randint(0, 256, size=(3, 256, 256), dtype=torch.uint8)\n# works directly on Tensors\nout_image1 = transforms(tensor_image)\n# on the GPU\nout_image1_cuda = transforms(tensor_image.cuda())\n# with batches\nbatched_image = torch.randint(0, 256, size=(4, 3, 256, 256), dtype=torch.uint8)\nout_image_batched = transforms(batched_image)\n# and has torchscript support\nout_image2 = scripted_transforms(tensor_image)\n```\nThese improvements enable the following new features:\n* support for GPU acceleration\n* batched transformations e.g. as needed for videos\n* transform multi-band torch tensor images (with more than 3-4 channels)\n* torchscript transforms together with your model for deployment\n**Note:** Exceptions for TorchScript support includes ```Compose```, ```RandomChoice```, ```RandomOrder```, ```Lambda``` and those applied on PIL images, such as ```ToPILImage```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Stable] Native image IO for JPEG and PNG formats\ntorchvision 0.8.0 introduces native image reading and writing operations for JPEG and PNG formats. Those operators support TorchScript and return ```CxHxW``` tensors in ```uint8``` format, and can thus be now part of your model for deployment in C++ environments.\n```python\nfrom torchvision.io import read_image\n\n# tensor_image is a CxHxW uint8 Tensor\ntensor_image = read_image('path_to_image.jpeg')\n\n# or equivalently\nfrom torchvision.io import read_file, decode_image\n# raw_data is a 1d uint8 Tensor with the raw bytes\nraw_data = read_file('path_to_image.jpeg')\ntensor_image = decode_image(raw_data)\n\n# all operators are torchscriptable and can be\n# serialized together with your model torchscript code\nscripted_read_image = torch.jit.script(read_image)\n```\n## [Stable] RetinaNet detection model\nThis release adds pretrained models for RetinaNet with a ResNet50 backbone from [Focal Loss for Dense Object Detection](https://arxiv.org/abs/1708.02002).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] New Video Reader API\nThis release introduces a new video reading abstraction, which gives more fine-grained control of iteration over videos. It supports image and audio, and implements an iterator interface so that it is interoperable with other the python libraries such as itertools.\n```python\nfrom torchvision.io import VideoReader\n\n# stream indicates if reading from audio or video\nreader = VideoReader('path_to_video.mp4', stream='video')\n# can change the stream after construction\n# via reader.set_current_stream\n\n# to read all frames in a video starting at 2 seconds\nfor frame in reader.seek(2):\n # frame is a dict with \"data\" and \"pts\" metadata\n print(frame[\"data\"], frame[\"pts\"])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "# because reader is an iterator you can combine it with\n# itertools\nfrom itertools import takewhile, islice\n# read 10 frames starting from 2 seconds\nfor frame in islice(reader.seek(2), 10):\n pass\n \n# or to return all frames between 2 and 5 seconds\nfor frame in takewhile(lambda x: x[\"pts\"] < 5, reader):\n pass\n```\n**Notes:**\n* In order to use the Video Reader API beta, you must compile torchvision from source and have ffmpeg installed in your system.\n* The VideoReader API is currently released as beta and its API may change following user feedback.\n\n# torchaudio\nWith this release, torchaudio is expanding its support for models and [end-to-end applications](https://github.com/pytorch/audio/tree/master/examples), adding a wav2letter training pipeline and end-to-end text-to-speech and source separation pipelines. Please file an issue on [github](https://github.com/pytorch/audio/issues/new?template=questions-help-support.md) to provide feedback on them.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Stable] Speech Recognition\nBuilding on the addition of the wav2letter model for speech recognition in the last release, we\u2019ve now added an [example wav2letter training pipeline](https://github.com/pytorch/audio/tree/master/examples/pipeline_wav2letter) with the LibriSpeech dataset.\n\n## [Stable] Text-to-speech\nWith the goal of supporting text-to-speech applications, we added a vocoder based on the WaveRNN model, based on the implementation from [this repository](https://github.com/fatchord/WaveRNN). The original implementation was introduced in \"Efficient Neural Audio Synthesis\". We also provide an [example WaveRNN training pipeline](https://github.com/pytorch/audio/tree/master/examples/pipeline_wavernn) that uses the LibriTTS dataset added to torchaudio in this release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "## [Stable] Source Separation\nWith the addition of the ConvTasNet model, based on the paper \"Conv-TasNet: Surpassing Ideal Time-Frequency Magnitude Masking for Speech Separation,\" torchaudio now also supports source separation. An [example ConvTasNet training pipeline](https://github.com/pytorch/audio/tree/master/examples/source_separation) is provided with the wsj-mix dataset.\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.7-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Adding a Contributor License Agreement for PyTorch'\nauthor: Team PyTorch\n---\n\nTo ensure the ongoing growth and success of the framework, we're introducing the use of the Apache Contributor License Agreement (CLA) for PyTorch. We care deeply about the broad community of contributors who make PyTorch such a great framework, so we want to take a moment to explain why we are adding a CLA.\n\n#### Why Does PyTorch Need a CLA?\n\nCLAs help clarify that users and maintainers have the relevant rights to use and maintain code contributed to an open source project, while allowing contributors to retain ownership rights to their code.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} {"page_content": "PyTorch has grown from a small group of enthusiasts to a now global community with over 1,600 contributors from dozens of countries, each bringing their own diverse perspectives, values and approaches to collaboration. Looking forward, clarity about how this collaboration is happening is an important milestone for the framework as we continue to build a stronger, safer and more scalable community around PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The text of the Apache CLA can be found [here](https://www.apache.org/licenses/contributor-agreements.html), together with an accompanying [FAQ](https://www.apache.org/licenses/cla-faq.html). The language in the PyTorch CLA is identical to the Apache template. Although CLAs have been the subject of significant discussion in the open source community, we are seeing that using a CLA, and particularly the Apache CLA, is now standard practice when projects and communities reach a certain scale. Popular projects", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "that have adopted some type of CLA include: Visual Studio Code, Flutter, TensorFlow, kubernetes, Ubuntu, Django, Python, Go, Android and many others.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "What is Not Changing\n\nPyTorch\u2019s BSD license is **not** changing. There is no impact to PyTorch users. CLAs will only be required for new contributions to the project. For past contributions, no action is necessary. Everything else stays the same, whether it\u2019s IP ownership, workflows, contributor roles or anything else that you\u2019ve come to expect from PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "How the New CLA will Work\n\nMoving forward, all contributors to projects under the PyTorch GitHub organization will need to sign a CLA to merge their contributions. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you've contributed to other Facebook Open Source projects, you may have already signed the CLA, and no action is required. If you have not signed the CLA, a GitHub check will prompt you to sign it before your pull requests can be merged. You can reach the CLA from this [link](https://code.facebook.com/cla).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you're contributing as an individual, meaning the code is not something you worked on as part of your job, you should sign the individual contributor agreement. This agreement associates your GitHub username with future contributions and only needs to be signed once.", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you're contributing as part of your employment, you may need to sign the [corporate contributor agreement](https://code.facebook.com/cla/corporate). Check with your legal team on filling this out. Also you will include a list of github ids from your company.\n\nAs always, we continue to be humbled and grateful for all your support, and we look forward to scaling PyTorch together to even greater heights in the years to come.\n\nThank you!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Feature Extraction in TorchVision using Torch FX'\nauthor: Alexander Soare and Francisco Massa\nfeatured-img: 'assets/images/fx-image2.png'\n---\n\n\n\n# Introduction", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "[FX](https://pytorch.org/docs/stable/fx.html) based feature extraction is a new [TorchVision utility](https://pytorch.org/vision/stable/feature_extraction.html) that lets us access intermediate transformations of an input during the forward pass of a PyTorch Module. It does so by symbolically tracing the forward method to produce a graph where each node represents a single operation. Nodes are named in a human-readable manner such that one may easily specify which nodes they want to access.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Did that all sound a little complicated? Not to worry as there\u2019s a little in this article for everyone. Whether you\u2019re a beginner or an advanced deep-vision practitioner, chances are you will want to know about FX feature extraction. If you still want more background on feature extraction in general, read on. If you\u2019re already comfortable with that and want to know how to do it in PyTorch, skim ahead to Existing Methods in PyTorch: Pros and Cons. And if you already know about the challenges of doing feature", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "extraction in PyTorch, feel free to skim forward to FX to The Rescue.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "A Recap On Feature Extraction\n\nWe\u2019re all used to the idea of having a deep neural network (DNN) that takes inputs and produces outputs, and we don\u2019t necessarily think of what happens in between. Let\u2019s just consider a ResNet-50 classification model as an example:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"CResNet-50\n\t
\n\t\tFigure 1: ResNet-50 takes an image of a bird and transforms that into the abstract concept \"bird\". Source: Bird image from ImageNet.\n

", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "We know though, that there are many sequential \u201clayers\u201d within the ResNet-50 architecture that transform the input step-by-step. In Figure 2 below, we peek under the hood to show the layers within ResNet-50, and we also show the intermediate transformations of the input as it passes through those layers.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"ResNet-50\n\t
\n\t\tFigure 2: ResNet-50 transforms the input image in multiple steps. Conceptually, we may access the intermediate transformation of the image after each one of these steps. Source: Bird image from ImageNet.\n

", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Existing Methods In PyTorch: Pros and Cons\n\nThere were already a few ways of doing feature extraction in PyTorch prior to FX based feature extraction being introduced.\n\nTo illustrate these, let\u2019s consider a simple convolutional neural network that does the following\n\n* Applies several \u201cblocks\u201d each with several convolution layers within.\n* After several blocks, it uses a global average pool and flatten operation.\n* Finally it uses a single output classification layer.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nfrom torch import nn\n\n\nclass ConvBlock(nn.Module):\n \"\"\"\n Applies `num_layers` 3x3 convolutions each followed by ReLU then downsamples\n via 2x2 max pool.\n \"\"\"", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "def __init__(self, num_layers, in_channels, out_channels):\n super().__init__()\n self.convs = nn.ModuleList(\n [nn.Sequential(\n nn.Conv2d(in_channels if i==0 else out_channels, out_channels, 3, padding=1),\n nn.ReLU()\n )\n for i in range(num_layers)]\n )\n self.downsample = nn.MaxPool2d(kernel_size=2, stride=2)\n \n def forward(self, x):\n for conv in self.convs:\n x = conv(x)\n x = self.downsample(x)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "return x", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "class CNN(nn.Module):\n \"\"\"\n Applies several ConvBlocks each doubling the number of channels, and\n halving the feature map size, before taking a global average and classifying.\n \"\"\"", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "def __init__(self, in_channels, num_blocks, num_classes):\n super().__init__()\n first_channels = 64\n self.blocks = nn.ModuleList(\n [ConvBlock(\n 2 if i==0 else 3,\n in_channels=(in_channels if i == 0 else first_channels*(2**(i-1))),\n out_channels=first_channels*(2**i))\n for i in range(num_blocks)]\n )\n self.global_pool = nn.AdaptiveAvgPool2d((1, 1))", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "self.cls = nn.Linear(first_channels*(2**(num_blocks-1)), num_classes)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "def forward(self, x):\n for block in self.blocks:\n x = block(x)\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x\n\n\nmodel = CNN(3, 4, 10)\nout = model(torch.zeros(1, 3, 32, 32)) # This will be the final logits over classes", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Let\u2019s say we want to get the final feature map before global average pooling. We could do the following:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Modify the forward method\n\n```python\ndef forward(self, x):\n for block in self.blocks:\n x = block(x)\n self.final_feature_map = x\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Or return it directly:\n\n```python\ndef forward(self, x):\n for block in self.blocks:\n x = block(x)\n final_feature_map = x\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x, final_feature_map\n```\nThat looks pretty easy. But there are some downsides here which all stem from the same underlying issue: that is, modifying the source code is not ideal:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "* It\u2019s not always easy to access and change given the practical considerations of a project.\n* If we want flexibility (switching feature extraction on or off, or having variations on it), we need to further adapt the source code to support that.\n* It\u2019s not always just a question of inserting a single line of code. Think about how you would go about getting the feature map from one of the intermediate blocks with the way I\u2019ve written this module.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "* Overall, we\u2019d rather avoid the overhead of maintaining source code for a model, when we actually don\u2019t need to change anything about how it works.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "One can see how this downside can start to get a lot more thorny when dealing with larger, more complicated models, and trying to get at features from within nested submodules.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Write a new module using the parameters from the original one\n\nFollowing on the example from above, say we want to get a feature map from each block. We could write a new module like so:\n\n```python\nclass CNNFeatures(nn.Module):\n def __init__(self, backbone):\n super().__init__()\n self.blocks = backbone.blocks\n\n def forward(self, x):\n feature_maps = []\n for block in self.blocks:\n x = block(x)\n feature_maps.append(x)\n return feature_maps", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "backbone = CNN(3, 4, 10)\nmodel = CNNFeatures(backbone)\nout = model(torch.zeros(1, 3, 32, 32)) # This is now a list of Tensors, each representing a feature map", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "In fact, this is much like the method that TorchVision used internally to make many of its detection models. \n\nAlthough this approach solves some of the issues with modifying the source code directly, there are still some major downsides:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "* It\u2019s only really straight-forward to access the outputs of top-level submodules. Dealing with nested submodules rapidly becomes complicated.\n* We have to be careful not to miss any important operations in between the input and the output. We introduce potential for errors in transcribing the exact functionality of the original module to the new module.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Overall, this method and the last both have the complication of tying in feature extraction with the model\u2019s source code itself. Indeed, if we examine the source code for TorchVision models we might suspect that some of the design choices were influenced by the desire to use them in this way for downstream tasks.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Use hooks\n\nHooks move us away from the paradigm of writing source code, towards one of specifying outputs. Considering our toy CNN example above, and the goal of getting feature maps for each layer, we could use hooks like this:\n\n\n```python\nmodel = CNN(3, 4, 10)\nfeature_maps = [] # This will be a list of Tensors, each representing a feature map\n\ndef hook_feat_map(mod, inp, out):\n\tfeature_maps.append(out)\n\nfor block in model.blocks:\n\tblock.register_forward_hook(hook_feat_map)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "out = model(torch.zeros(1, 3, 32, 32)) # This will be the final logits over classes", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Now we have full flexibility in terms of accessing nested submodules, and we free ourselves of the responsibilities of fiddling with the source code. But this approach comes with its own downsides:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "* We can only apply hooks to modules. If we have functional operations (reshape, view, functional non-linearities, etc) for which we want the outputs, hooks won\u2019t work directly on them.\n* We have not modified anything about the source code, so the whole forward pass is executed, regardless of the hooks. If we only need to access early features without any need for the final output, this could result in a lot of useless computation.\n* Hooks are not TorchScript friendly.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "The text of the Apache CLA can be found [here](https://www.apache.org/licenses/contributor-agreements.html), together with an accompanying [FAQ](https://www.apache.org/licenses/cla-faq.html). The language in the PyTorch CLA is identical to the Apache template. Although CLAs have been the subject of significant discussion in the open source community, we are seeing that using a CLA, and particularly the Apache CLA, is now standard practice when projects and communities reach a certain scale. Popular projects that have adopted some type of CLA include: Visual Studio Code, Flutter, TensorFlow, kubernetes, Ubuntu, Django, Python, Go, Android and many others.\n\n#### What is Not Changing", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "PyTorch\u2019s BSD license is **not** changing. There is no impact to PyTorch users. CLAs will only be required for new contributions to the project. For past contributions, no action is necessary. Everything else stays the same, whether it\u2019s IP ownership, workflows, contributor roles or anything else that you\u2019ve come to expect from PyTorch. \n\n#### How the New CLA will Work\n\nMoving forward, all contributors to projects under the PyTorch GitHub organization will need to sign a CLA to merge their contributions. \n\n
\n \n
\n\nIf you've contributed to other Facebook Open Source projects, you may have already signed the CLA, and no action is required. If you have not signed the CLA, a GitHub check will prompt you to sign it before your pull requests can be merged. You can reach the CLA from this [link](https://code.facebook.com/cla).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "If you're contributing as an individual, meaning the code is not something you worked on as part of your job, you should sign the individual contributor agreement. This agreement associates your GitHub username with future contributions and only needs to be signed once.\n\nIf you're contributing as part of your employment, you may need to sign the [corporate contributor agreement](https://code.facebook.com/cla/corporate). Check with your legal team on filling this out. Also you will include a list of github ids from your company.\n\nAs always, we continue to be humbled and grateful for all your support, and we look forward to scaling PyTorch together to even greater heights in the years to come.\n\nThank you!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Feature Extraction in TorchVision using Torch FX'\nauthor: Alexander Soare and Francisco Massa\nfeatured-img: 'assets/images/fx-image2.png'\n---\n\n\n\n# Introduction\n\n[FX](https://pytorch.org/docs/stable/fx.html) based feature extraction is a new [TorchVision utility](https://pytorch.org/vision/stable/feature_extraction.html) that lets us access intermediate transformations of an input during the forward pass of a PyTorch Module. It does so by symbolically tracing the forward method to produce a graph where each node represents a single operation. Nodes are named in a human-readable manner such that one may easily specify which nodes they want to access.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "Did that all sound a little complicated? Not to worry as there\u2019s a little in this article for everyone. Whether you\u2019re a beginner or an advanced deep-vision practitioner, chances are you will want to know about FX feature extraction. If you still want more background on feature extraction in general, read on. If you\u2019re already comfortable with that and want to know how to do it in PyTorch, skim ahead to Existing Methods in PyTorch: Pros and Cons. And if you already know about the challenges of doing feature extraction in PyTorch, feel free to skim forward to FX to The Rescue.\n\n\n## A Recap On Feature Extraction\n\nWe\u2019re all used to the idea of having a deep neural network (DNN) that takes inputs and produces outputs, and we don\u2019t necessarily think of what happens in between. Let\u2019s just consider a ResNet-50 classification model as an example:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"CResNet-50\n\t
\n\t\tFigure 1: ResNet-50 takes an image of a bird and transforms that into the abstract concept \"bird\". Source: Bird image from ImageNet.\n

\n\nWe know though, that there are many sequential \u201clayers\u201d within the ResNet-50 architecture that transform the input step-by-step. In Figure 2 below, we peek under the hood to show the layers within ResNet-50, and we also show the intermediate transformations of the input as it passes through those layers.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"ResNet-50\n\t
\n\t\tFigure 2: ResNet-50 transforms the input image in multiple steps. Conceptually, we may access the intermediate transformation of the image after each one of these steps. Source: Bird image from ImageNet.\n

\n\n\n## Existing Methods In PyTorch: Pros and Cons\n\nThere were already a few ways of doing feature extraction in PyTorch prior to FX based feature extraction being introduced.\n\nTo illustrate these, let\u2019s consider a simple convolutional neural network that does the following\n\n* Applies several \u201cblocks\u201d each with several convolution layers within.\n* After several blocks, it uses a global average pool and flatten operation.\n* Finally it uses a single output classification layer.\n\n```python\nimport torch\nfrom torch import nn", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "class ConvBlock(nn.Module):\n \"\"\"\n Applies `num_layers` 3x3 convolutions each followed by ReLU then downsamples\n via 2x2 max pool.\n \"\"\"\n\n def __init__(self, num_layers, in_channels, out_channels):\n super().__init__()\n self.convs = nn.ModuleList(\n [nn.Sequential(\n nn.Conv2d(in_channels if i==0 else out_channels, out_channels, 3, padding=1),\n nn.ReLU()\n )\n for i in range(num_layers)]\n )\n self.downsample = nn.MaxPool2d(kernel_size=2, stride=2)\n \n def forward(self, x):\n for conv in self.convs:\n x = conv(x)\n x = self.downsample(x)\n return x\n \n\nclass CNN(nn.Module):\n \"\"\"\n Applies several ConvBlocks each doubling the number of channels, and\n halving the feature map size, before taking a global average and classifying.\n \"\"\"", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "def __init__(self, in_channels, num_blocks, num_classes):\n super().__init__()\n first_channels = 64\n self.blocks = nn.ModuleList(\n [ConvBlock(\n 2 if i==0 else 3,\n in_channels=(in_channels if i == 0 else first_channels*(2**(i-1))),\n out_channels=first_channels*(2**i))\n for i in range(num_blocks)]\n )\n self.global_pool = nn.AdaptiveAvgPool2d((1, 1))\n self.cls = nn.Linear(first_channels*(2**(num_blocks-1)), num_classes)\n\n def forward(self, x):\n for block in self.blocks:\n x = block(x)\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x\n\n\nmodel = CNN(3, 4, 10)\nout = model(torch.zeros(1, 3, 32, 32)) # This will be the final logits over classes\n\n```\n\nLet\u2019s say we want to get the final feature map before global average pooling. We could do the following:\n\n### Modify the forward method", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\ndef forward(self, x):\n for block in self.blocks:\n x = block(x)\n self.final_feature_map = x\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x\n```\n\nOr return it directly:\n\n```python\ndef forward(self, x):\n for block in self.blocks:\n x = block(x)\n final_feature_map = x\n x = self.global_pool(x)\n x = x.flatten(1)\n x = self.cls(x)\n return x, final_feature_map\n```\nThat looks pretty easy. But there are some downsides here which all stem from the same underlying issue: that is, modifying the source code is not ideal:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "* It\u2019s not always easy to access and change given the practical considerations of a project.\n* If we want flexibility (switching feature extraction on or off, or having variations on it), we need to further adapt the source code to support that.\n* It\u2019s not always just a question of inserting a single line of code. Think about how you would go about getting the feature map from one of the intermediate blocks with the way I\u2019ve written this module.\n* Overall, we\u2019d rather avoid the overhead of maintaining source code for a model, when we actually don\u2019t need to change anything about how it works.\n\nOne can see how this downside can start to get a lot more thorny when dealing with larger, more complicated models, and trying to get at features from within nested submodules.\n\n### Write a new module using the parameters from the original one\n\nFollowing on the example from above, say we want to get a feature map from each block. We could write a new module like so:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\nclass CNNFeatures(nn.Module):\n def __init__(self, backbone):\n super().__init__()\n self.blocks = backbone.blocks\n\n def forward(self, x):\n feature_maps = []\n for block in self.blocks:\n x = block(x)\n feature_maps.append(x)\n return feature_maps\n\n\nbackbone = CNN(3, 4, 10)\nmodel = CNNFeatures(backbone)\nout = model(torch.zeros(1, 3, 32, 32)) # This is now a list of Tensors, each representing a feature map\n```\n\nIn fact, this is much like the method that TorchVision used internally to make many of its detection models. \n\nAlthough this approach solves some of the issues with modifying the source code directly, there are still some major downsides:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "* It\u2019s only really straight-forward to access the outputs of top-level submodules. Dealing with nested submodules rapidly becomes complicated.\n* We have to be careful not to miss any important operations in between the input and the output. We introduce potential for errors in transcribing the exact functionality of the original module to the new module.\n\nOverall, this method and the last both have the complication of tying in feature extraction with the model\u2019s source code itself. Indeed, if we examine the source code for TorchVision models we might suspect that some of the design choices were influenced by the desire to use them in this way for downstream tasks.\n\n### Use hooks\n\nHooks move us away from the paradigm of writing source code, towards one of specifying outputs. Considering our toy CNN example above, and the goal of getting feature maps for each layer, we could use hooks like this:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\nmodel = CNN(3, 4, 10)\nfeature_maps = [] # This will be a list of Tensors, each representing a feature map\n\ndef hook_feat_map(mod, inp, out):\n\tfeature_maps.append(out)\n\nfor block in model.blocks:\n\tblock.register_forward_hook(hook_feat_map)\n\nout = model(torch.zeros(1, 3, 32, 32)) # This will be the final logits over classes\n```\n\nNow we have full flexibility in terms of accessing nested submodules, and we free ourselves of the responsibilities of fiddling with the source code. But this approach comes with its own downsides:\n\n* We can only apply hooks to modules. If we have functional operations (reshape, view, functional non-linearities, etc) for which we want the outputs, hooks won\u2019t work directly on them.\n* We have not modified anything about the source code, so the whole forward pass is executed, regardless of the hooks. If we only need to access early features without any need for the final output, this could result in a lot of useless computation.\n* Hooks are not TorchScript friendly.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} {"page_content": "Here\u2019s a summary of the different methods and their pros/cons:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| | Can use source code as is without any modifications or rewriting | Full flexibility in accessing features | Drops unnecessary computational steps | TorchScript friendly |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| Modify forward method | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| New module that reuses submodules / parameters of original module | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| Hooks | YES | Mostly YES. Only outputs of submodules | NO | NO |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Table 1: The pros (or cons) of some of the existing methods for feature extraction with PyTorch\n\nIn the next section of this article, let\u2019s see how we can get YES across the board.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "FX to The Rescue\n\nThe natural question for some new-starters in Python and coding at this point might be: *\u201cCan\u2019t we just point to a line of code and tell Python or PyTorch that we want the result of that line?\u201d* For those who have spent more time coding, the reason this can\u2019t be done is clear: multiple operations can happen in one line of code, whether they are explicitly written there, or they are implicit as sub-operations. Just take this simple module as an example:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "```python\nclass MyModule(torch.nn.Module):\n def __init__(self):\n super().__init__()\n self.param = torch.nn.Parameter(torch.rand(3, 4))\n self.submodule = MySubModule()\n\n def forward(self, x):\n return self.submodule(x + self.param).clamp(min=0.0, max=1.0)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "The forward method has a single line of code which we can unravel as:\n\n1. Add `self.param` to `x`\n2. Pass x through self.submodule. Here we would need to consider the steps happening in that submodule. I\u2019m just going to use dummy operation names for illustration:\n\tI. submodule.op_1\n\tII. submodule.op_2\n3. Apply the clamp operation\n\nSo even if we point at this one line, the question then is: \u201cFor which step do we want to extract the output?\u201d.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "[FX](https://pytorch.org/docs/stable/fx.html) is a core PyTorch toolkit that (oversimplifying) does the unravelling I just mentioned. It does something called \u201csymbolic tracing\u201d, which means the Python code is interpreted and stepped through, operation-by-operation, using some dummy proxy for a real input. Introducing some nomenclature, each step as described above is considered a **\u201cnode\u201d**, and consecutive nodes are connected to one another to form a **\u201cgraph\u201d** (not unlike the common mathematical notion", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "of a graph). Here are the \u201csteps\u201d above translated to this concept of a graph.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"Graphical\n\t
\n\t\tFigure 3: Graphical representation of the result of symbolically tracing our example of a simple forward method.\n

", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Note that we call this a graph, and not just a set of steps, because it\u2019s possible for the graph to branch off and recombine. Think of the skip connection in a residual block. This would look something like:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"Graphical\n\t
\n\t\tFigure 4: Graphical representation of a residual skip connection. The middle node is like the main branch of a residual block, and the final node represents the sum of the input and output of the main branch.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Now, TorchVision\u2019s **[get_graph_node_names](https://pytorch.org/vision/stable/feature_extraction.html#torchvision.models.feature_extraction.get_graph_node_names)** function applies FX as described above, and in the process of doing so, tags each node with a human readable name. Let\u2019s try this with our toy CNN model from the previous section:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "```python\nmodel = CNN(3, 4, 10)\nfrom torchvision.models.feature_extraction import get_graph_node_names\nnodes, _ = get_graph_node_names(model)\nprint(nodes)\n```\nwhich will result in:\n```python", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "['x', 'blocks.0.convs.0.0', 'blocks.0.convs.0.1', 'blocks.0.convs.1.0', 'blocks.0.convs.1.1', 'blocks.0.downsample', 'blocks.1.convs.0.0', 'blocks.1.convs.0.1', 'blocks.1.convs.1.0', 'blocks.1.convs.1.1', 'blocks.1.convs.2.0', 'blocks.1.convs.2.1', 'blocks.1.downsample', 'blocks.2.convs.0.0', 'blocks.2.convs.0.1', 'blocks.2.convs.1.0', 'blocks.2.convs.1.1', 'blocks.2.convs.2.0', 'blocks.2.convs.2.1', 'blocks.2.downsample', 'blocks.3.convs.0.0', 'blocks.3.convs.0.1', 'blocks.3.convs.1.0',", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "'blocks.3.convs.1.1', 'blocks.3.convs.2.0', 'blocks.3.convs.2.1', 'blocks.3.downsample', 'global_pool', 'flatten', 'cls']", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "We can read these node names as hierarchically organised \u201caddresses\u201d for the operations of interest. For example 'blocks.1.downsample' refers to the MaxPool2d layer in the second `ConvBlock`.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "[`create_feature_extractor`](https://pytorch.org/vision/stable/feature_extraction.html#torchvision.models.feature_extraction.create_feature_extractor), which is where all the magic happens, goes a few steps further than **`get_graph_node_names`**. It takes desired node names as one of the input arguments, and then uses more FX core functionality to:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "1. Assign the desired nodes as outputs.\n2. Prune unnecessary downstream nodes and their associated parameters.\n3. Translate the resulting graph back into Python code.\n4. Return another PyTorch Module to the user. This has the python code from step 3 as the forward method.\n\nAs a demonstration, here\u2019s how we would apply `create_feature_extractor` to get the 4 feature maps from our toy CNN model", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchvision.models.feature_extraction import create_feature_extractor\n# Confused about the node specification here?\n# We are allowed to provide truncated node names, and `create_feature_extractor`\n# will choose the last node with that prefix.\nfeature_extractor = create_feature_extractor(\n\tmodel, return_nodes=['blocks.0', 'blocks.1', 'blocks.2', 'blocks.3'])\n# `out` will be a dict of Tensors, each representing a feature map\nout = feature_extractor(torch.zeros(1, 3, 32, 32))", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "It\u2019s as simple as that. When it comes down to it, FX feature extraction is just a way of making it possible to do what some of us would have naively hoped for when we first started programming: *\u201cjust give me the output of this code (*points finger at screen)\u201d*.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "| | Can use source code as is without any modifications or rewriting | Full flexibility in accessing features | Drops unnecessary computational steps | TorchScript friendly |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| Modify forward method | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES | \n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| New module that reuses submodules / parameters of original module | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| Hooks | YES | Mostly YES. Only outputs of submodules | NO | NO |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "Table 1: The pros (or cons) of some of the existing methods for feature extraction with PyTorch\n\nIn the next section of this article, let\u2019s see how we can get YES across the board.\n\n\n## FX to The Rescue\n\nThe natural question for some new-starters in Python and coding at this point might be: *\u201cCan\u2019t we just point to a line of code and tell Python or PyTorch that we want the result of that line?\u201d* For those who have spent more time coding, the reason this can\u2019t be done is clear: multiple operations can happen in one line of code, whether they are explicitly written there, or they are implicit as sub-operations. Just take this simple module as an example:\n\n```python\nclass MyModule(torch.nn.Module):\n def __init__(self):\n super().__init__()\n self.param = torch.nn.Parameter(torch.rand(3, 4))\n self.submodule = MySubModule()\n\n def forward(self, x):\n return self.submodule(x + self.param).clamp(min=0.0, max=1.0)\n```\n\nThe forward method has a single line of code which we can unravel as:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "1. Add `self.param` to `x`\n2. Pass x through self.submodule. Here we would need to consider the steps happening in that submodule. I\u2019m just going to use dummy operation names for illustration:\n\tI. submodule.op_1\n\tII. submodule.op_2\n3. Apply the clamp operation\n\nSo even if we point at this one line, the question then is: \u201cFor which step do we want to extract the output?\u201d.\n\n[FX](https://pytorch.org/docs/stable/fx.html) is a core PyTorch toolkit that (oversimplifying) does the unravelling I just mentioned. It does something called \u201csymbolic tracing\u201d, which means the Python code is interpreted and stepped through, operation-by-operation, using some dummy proxy for a real input. Introducing some nomenclature, each step as described above is considered a **\u201cnode\u201d**, and consecutive nodes are connected to one another to form a **\u201cgraph\u201d** (not unlike the common mathematical notion of a graph). Here are the \u201csteps\u201d above translated to this concept of a graph.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"Graphical\n\t
\n\t\tFigure 3: Graphical representation of the result of symbolically tracing our example of a simple forward method.\n

\n\nNote that we call this a graph, and not just a set of steps, because it\u2019s possible for the graph to branch off and recombine. Think of the skip connection in a residual block. This would look something like:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"Graphical\n\t
\n\t\tFigure 4: Graphical representation of a residual skip connection. The middle node is like the main branch of a residual block, and the final node represents the sum of the input and output of the main branch.\n

\n\nNow, TorchVision\u2019s **[get_graph_node_names](https://pytorch.org/vision/stable/feature_extraction.html#torchvision.models.feature_extraction.get_graph_node_names)** function applies FX as described above, and in the process of doing so, tags each node with a human readable name. Let\u2019s try this with our toy CNN model from the previous section:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\nmodel = CNN(3, 4, 10)\nfrom torchvision.models.feature_extraction import get_graph_node_names\nnodes, _ = get_graph_node_names(model)\nprint(nodes)\n```\nwhich will result in:\n```python\n['x', 'blocks.0.convs.0.0', 'blocks.0.convs.0.1', 'blocks.0.convs.1.0', 'blocks.0.convs.1.1', 'blocks.0.downsample', 'blocks.1.convs.0.0', 'blocks.1.convs.0.1', 'blocks.1.convs.1.0', 'blocks.1.convs.1.1', 'blocks.1.convs.2.0', 'blocks.1.convs.2.1', 'blocks.1.downsample', 'blocks.2.convs.0.0', 'blocks.2.convs.0.1', 'blocks.2.convs.1.0', 'blocks.2.convs.1.1', 'blocks.2.convs.2.0', 'blocks.2.convs.2.1', 'blocks.2.downsample', 'blocks.3.convs.0.0', 'blocks.3.convs.0.1', 'blocks.3.convs.1.0', 'blocks.3.convs.1.1', 'blocks.3.convs.2.0', 'blocks.3.convs.2.1', 'blocks.3.downsample', 'global_pool', 'flatten', 'cls']\n```\n\nWe can read these node names as hierarchically organised \u201caddresses\u201d for the operations of interest. For example 'blocks.1.downsample' refers to the MaxPool2d layer in the second `ConvBlock`.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "[`create_feature_extractor`](https://pytorch.org/vision/stable/feature_extraction.html#torchvision.models.feature_extraction.create_feature_extractor), which is where all the magic happens, goes a few steps further than **`get_graph_node_names`**. It takes desired node names as one of the input arguments, and then uses more FX core functionality to:\n\n1. Assign the desired nodes as outputs.\n2. Prune unnecessary downstream nodes and their associated parameters.\n3. Translate the resulting graph back into Python code.\n4. Return another PyTorch Module to the user. This has the python code from step 3 as the forward method.\n\nAs a demonstration, here\u2019s how we would apply `create_feature_extractor` to get the 4 feature maps from our toy CNN model", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torchvision.models.feature_extraction import create_feature_extractor\n# Confused about the node specification here?\n# We are allowed to provide truncated node names, and `create_feature_extractor`\n# will choose the last node with that prefix.\nfeature_extractor = create_feature_extractor(\n\tmodel, return_nodes=['blocks.0', 'blocks.1', 'blocks.2', 'blocks.3'])\n# `out` will be a dict of Tensors, each representing a feature map\nout = feature_extractor(torch.zeros(1, 3, 32, 32))\n```\n\nIt\u2019s as simple as that. When it comes down to it, FX feature extraction is just a way of making it possible to do what some of us would have naively hoped for when we first started programming: *\u201cjust give me the output of this code (*points finger at screen)\u201d*.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} {"page_content": "- [ ] \u2026 does not require us to fiddle with source code.\n- [ ] \u2026 provides full flexibility in terms of accessing any intermediate transformation of our inputs, whether they are the results of a module or a functional operation\n- [ ] \u2026 does drop unnecessary computations steps once features have been extracted\n- [ ] \u2026 and I didn\u2019t mention this before, but it\u2019s also TorchScript friendly!\n\nHere\u2019s that table again with another row added for FX feature extraction", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| | Can use source code as is without any modifications or rewriting | Full flexibility in accessing features | Drops unnecessary computational steps | TorchScript friendly |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| Modify forward method | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| New module that reuses submodules / parameters of original module | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| Hooks | YES | Mostly YES. Only outputs of submodules | NO | NO |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "| FX | YES | YES | YES | YES |", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Table 2: A copy of Table 1 with an added row for FX feature extraction. FX feature extraction gets YES across the board!", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Current FX Limitations\n\nAlthough I would have loved to end the post there, FX does have some of its own limitations which boil down to:\n\n1. There may be some Python code that isn\u2019t yet handled by FX when it comes to the step of interpretation and translation into a graph.\n2. Dynamic control flow can\u2019t be represented in terms of a static graph.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "The easiest thing to do when these problems crop up is to bundle the underlying code into a \u201cleaf node\u201d. Recall the example graph from Figure 3? Conceptually, we may agree that the `submodule` should be treated as a node in itself rather than a set of nodes representing the underlying operations. If we do so, we can redraw the graph as:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"The\n\t
\n\t\tFigure 5: The individual operations within `submodule` may (left - within red box), may be consolidated into one node (right - node #2) if we consider the `submodule` as a \"leaf\" node.\n

", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "We would want to do so if there is some problematic code within the submodule, but we don\u2019t have any need for extracting any intermediate transformations from within it. In practice, this is easily achievable by providing a keyword argument to create_feature_extractor or get_graph_node_names.\n\n\n```python\nmodel = CNN(3, 4, 10)\nnodes, _ = get_graph_node_names(model, tracer_kwargs={'leaf_modules': [ConvBlock]})\nprint(nodes)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "for which the output will be:\n\n```python\n['x', 'blocks.0', 'blocks.1', 'blocks.2', 'blocks.3', 'global_pool', 'flatten', 'cls']", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Notice how, as compared to previously, all the nodes for any given `ConvBlock` are consolidated into a single node.\n\nWe could do something similar with functions. For example, Python\u2019s inbuilt `len` needs to be wrapped and the result should be treated as a leaf node. Here\u2019s how you can do that with core FX functionality:\n\n```python\ntorch.fx.wrap('len')\n\nclass MyModule(nn.Module):\n def forward(self, x):\n x += 1\n len(x)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "model = MyModule()\nfeature_extractor = create_feature_extractor(model, return_nodes=['add'])", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "For functions you define, you may instead use another keyword argument to `create_feature_extractor` (minor detail: here\u2019s[ why you might want to do it this way instead](https://github.com/pytorch/pytorch/issues/62021#issue-950458396)):\n\n\n```python\ndef myfunc(x):\n return len(x)\n\nclass MyModule(nn.Module):\n def forward(self, x):\n x += 1\n myfunc(x)\n\nmodel = MyModule()\nfeature_extractor = create_feature_extractor(\n model, return_nodes=['add'], tracer_kwargs={'autowrap_functions': [myfunc]})", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Notice that none of the fixes above involved modifying source code.\n\nOf course, there may be times when the very intermediate transformation one is trying to get access to is within the same forward method or function that is causing problems. Here, we can\u2019t just treat that module or function as a leaf node, because then we can\u2019t access the intermediate transformations within. In these cases, some rewriting of the source code will be needed. Here are some examples (not exhaustive)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "- FX will raise an error when trying to trace through code with an `assert` statement. In this case you may need to remove that assertion or switch it with [`torch._assert`](https://pytorch.org/docs/stable/generated/torch._assert.html) (this is not a public function - so consider it a bandaid and use with caution).", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "- Symbolically tracing in-place changes to slices of tensors is not supported. You will need to make a new variable for the slice, apply the operation, then reconstruct the original tensor using concatenation or stacking.\n- Representing dynamic control flow in a static graph is just not logically possible. See if you can distill the coded logic down to something that is not dynamic - see FX documentation for tips.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "In general, you may consult the FX documentation for more detail on the [limitations of symbolic tracing](https://pytorch.org/docs/stable/fx.html#limitations-of-symbolic-tracing) and the possible workarounds.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Conclusion", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "We did a quick recap on feature extraction and why one might want to do it. Although there are existing methods for doing feature extraction in PyTorch they all have rather significant shortcomings. We learned how TorchVision\u2019s FX feature extraction utility works and what makes it so versatile compared to the existing methods. While there are still some minor kinks to iron out for the latter, we understand the limitations, and can trade them off against the limitations of other methods depending on our use", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "case. Hopefully by adding this new utility to your PyTorch toolkit, you\u2019re now equipped to handle the vast majority of feature extraction requirements you may come across.", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Happy coding!", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch Ecosystem Day 2021 Recap and New Contributor Resources'\nauthor: Team PyTorch\n---\n\nThank you to our incredible community for making the first ever PyTorch Ecosystem Day a success! The day was filled with discussions on new developments, trends and challenges showcased through 71 posters, 32 breakout sessions and 6 keynote speakers. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "Special thanks to our keynote speakers: Piotr Bialecki, Ritchie Ng, Miquel Farr\u00e9, Joe Spisak, Geeta Chauhan, and Suraj Subramanian who shared updates from the latest release of PyTorch, exciting work being done with partners, use case example from Disney, the growth and development of the PyTorch community in Asia Pacific, and latest contributor highlights.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "If you missed the opening talks, you rewatch them here:\n* [Morning/EMEA Opening Talks](https://www.youtube.com/watch?v=MYE01-XaSZA)\n* [Evening/APAC Opening Talks](https://www.youtube.com/watch?v=CjU_6OaYKpw)", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "In addition to the talks, we had 71 posters covering various topics such as multimodal, NLP, compiler, distributed training, researcher productivity tools, AI accelerators, and more. From the event, it was clear that an underlying thread that ties all of these different projects together is the cross-collaboration of the PyTorch community. Thank you for continuing to push the state of the art with PyTorch!", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "To view the full catalogue of poster, please visit **[PyTorch Ecosystem Day 2021 Event Page](https://pytorch.org/ecosystem/pted/2021)**.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "New Contributor Resources \nToday, we are also sharing new contributor resources that we are trying out to give you the most access to up-to-date news, networking opportunities and more. \n* [Contributor Newsletter](https://pytorch.org/resources/contributors/) - Includes curated news including RFCs, feature roadmaps, notable PRs, editorials from developers, and more to support keeping track of everything that\u2019s happening in our community.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "* [Contributors Discussion Forum](https://dev-discuss.pytorch.org/) - Designed for contributors to learn and collaborate on the latest development across PyTorch. \n* [PyTorch Developer Podcast (Beta)](https://pytorch-dev-podcast.simplecast.com/) - Edward Yang, PyTorch Research Scientist, at Facebook AI shares bite-sized (10 to 20 mins) podcast episodes discussing topics about all sorts of internal development topics in PyTorch.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "Thank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Geospatial deep learning with TorchGeo\"\nauthor: Adam Stewart (University of Illinois at Urbana-Champaign), Caleb Robinson (Microsoft AI for Good Research Lab), Isaac Corley (University of Texas at San Antonio)\nfeatured-img: 'assets/images/torchgeo-hurricane.jpg'\n---\n\nTorchGeo is a PyTorch domain library providing datasets, samplers, transforms, and pre-trained models specific to geospatial data.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n https://github.com/microsoft/torchgeo\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "For decades, Earth observation satellites, aircraft, and more recently UAV platforms have been collecting increasing amounts of imagery of the Earth\u2019s surface. With information about seasonal and long-term trends, remotely sensed imagery can be invaluable for solving some of the greatest challenges to humanity, including climate change adaptation, natural disaster monitoring, water resource management, and food security for a growing global population. From a computer vision perspective, this includes", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "applications like land cover mapping (semantic segmentation), deforestation and flood monitoring (change detection), glacial flow (pixel tracking), hurricane tracking and intensity estimation (regression), and building and road detection (object detection, instance segmentation). By leveraging recent advancements in deep learning architectures, cheaper and more powerful GPUs, and petabytes of freely available satellite imagery datasets, we can come closer to solving these important problems.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "| | Can use source code as is without any modifications or rewriting | Full flexibility in accessing features | Drops unnecessary computational steps | TorchScript friendly |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| Modify forward method | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES | \n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| New module that reuses submodules / parameters of original module | NO | Technically yes. Depends on how much code you\u2019re willing to write. So in practice, NO. | YES | YES |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| Hooks | YES | Mostly YES. Only outputs of submodules | NO | NO |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|\n| FX | YES | YES | YES | YES |\n|-------------------------------------------------------------------|:-----------------------------------------------------------------:|:--------------------------------------------------------------------------------------:|:--------------------------------------:|:--------------------:|", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "Table 2: A copy of Table 1 with an added row for FX feature extraction. FX feature extraction gets YES across the board!\n\n\n## Current FX Limitations\n\nAlthough I would have loved to end the post there, FX does have some of its own limitations which boil down to:\n\n1. There may be some Python code that isn\u2019t yet handled by FX when it comes to the step of interpretation and translation into a graph.\n2. Dynamic control flow can\u2019t be represented in terms of a static graph.\n\nThe easiest thing to do when these problems crop up is to bundle the underlying code into a \u201cleaf node\u201d. Recall the example graph from Figure 3? Conceptually, we may agree that the `submodule` should be treated as a node in itself rather than a set of nodes representing the underlying operations. If we do so, we can redraw the graph as:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"The\n\t
\n\t\tFigure 5: The individual operations within `submodule` may (left - within red box), may be consolidated into one node (right - node #2) if we consider the `submodule` as a \"leaf\" node.\n

\n\n\nWe would want to do so if there is some problematic code within the submodule, but we don\u2019t have any need for extracting any intermediate transformations from within it. In practice, this is easily achievable by providing a keyword argument to create_feature_extractor or get_graph_node_names.\n\n\n```python\nmodel = CNN(3, 4, 10)\nnodes, _ = get_graph_node_names(model, tracer_kwargs={'leaf_modules': [ConvBlock]})\nprint(nodes)\n```\n\nfor which the output will be:", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\n['x', 'blocks.0', 'blocks.1', 'blocks.2', 'blocks.3', 'global_pool', 'flatten', 'cls']\n```\n\nNotice how, as compared to previously, all the nodes for any given `ConvBlock` are consolidated into a single node.\n\nWe could do something similar with functions. For example, Python\u2019s inbuilt `len` needs to be wrapped and the result should be treated as a leaf node. Here\u2019s how you can do that with core FX functionality:\n\n```python\ntorch.fx.wrap('len')\n\nclass MyModule(nn.Module):\n def forward(self, x):\n x += 1\n len(x)\n\nmodel = MyModule()\nfeature_extractor = create_feature_extractor(model, return_nodes=['add'])\n```\n\nFor functions you define, you may instead use another keyword argument to `create_feature_extractor` (minor detail: here\u2019s[ why you might want to do it this way instead](https://github.com/pytorch/pytorch/issues/62021#issue-950458396)):\n\n\n```python\ndef myfunc(x):\n return len(x)\n\nclass MyModule(nn.Module):\n def forward(self, x):\n x += 1\n myfunc(x)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "model = MyModule()\nfeature_extractor = create_feature_extractor(\n model, return_nodes=['add'], tracer_kwargs={'autowrap_functions': [myfunc]})\n```\n\nNotice that none of the fixes above involved modifying source code.\n\nOf course, there may be times when the very intermediate transformation one is trying to get access to is within the same forward method or function that is causing problems. Here, we can\u2019t just treat that module or function as a leaf node, because then we can\u2019t access the intermediate transformations within. In these cases, some rewriting of the source code will be needed. Here are some examples (not exhaustive)", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "- FX will raise an error when trying to trace through code with an `assert` statement. In this case you may need to remove that assertion or switch it with [`torch._assert`](https://pytorch.org/docs/stable/generated/torch._assert.html) (this is not a public function - so consider it a bandaid and use with caution).\n- Symbolically tracing in-place changes to slices of tensors is not supported. You will need to make a new variable for the slice, apply the operation, then reconstruct the original tensor using concatenation or stacking.\n- Representing dynamic control flow in a static graph is just not logically possible. See if you can distill the coded logic down to something that is not dynamic - see FX documentation for tips.\n\nIn general, you may consult the FX documentation for more detail on the [limitations of symbolic tracing](https://pytorch.org/docs/stable/fx.html#limitations-of-symbolic-tracing) and the possible workarounds.\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nWe did a quick recap on feature extraction and why one might want to do it. Although there are existing methods for doing feature extraction in PyTorch they all have rather significant shortcomings. We learned how TorchVision\u2019s FX feature extraction utility works and what makes it so versatile compared to the existing methods. While there are still some minor kinks to iron out for the latter, we understand the limitations, and can trade them off against the limitations of other methods depending on our use case. Hopefully by adding this new utility to your PyTorch toolkit, you\u2019re now equipped to handle the vast majority of feature extraction requirements you may come across.\n\nHappy coding!", "metadata": {"source": "https://pytorch.org/blog/FX-feature-extraction-torchvision/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch Ecosystem Day 2021 Recap and New Contributor Resources'\nauthor: Team PyTorch\n---\n\nThank you to our incredible community for making the first ever PyTorch Ecosystem Day a success! The day was filled with discussions on new developments, trends and challenges showcased through 71 posters, 32 breakout sessions and 6 keynote speakers. \n\n
\n \n
\n\nSpecial thanks to our keynote speakers: Piotr Bialecki, Ritchie Ng, Miquel Farr\u00e9, Joe Spisak, Geeta Chauhan, and Suraj Subramanian who shared updates from the latest release of PyTorch, exciting work being done with partners, use case example from Disney, the growth and development of the PyTorch community in Asia Pacific, and latest contributor highlights.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} +{"page_content": "If you missed the opening talks, you rewatch them here:\n* [Morning/EMEA Opening Talks](https://www.youtube.com/watch?v=MYE01-XaSZA)\n* [Evening/APAC Opening Talks](https://www.youtube.com/watch?v=CjU_6OaYKpw)\n\nIn addition to the talks, we had 71 posters covering various topics such as multimodal, NLP, compiler, distributed training, researcher productivity tools, AI accelerators, and more. From the event, it was clear that an underlying thread that ties all of these different projects together is the cross-collaboration of the PyTorch community. Thank you for continuing to push the state of the art with PyTorch! \n\nTo view the full catalogue of poster, please visit **[PyTorch Ecosystem Day 2021 Event Page](https://pytorch.org/ecosystem/pted/2021)**.", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} +{"page_content": "### New Contributor Resources \nToday, we are also sharing new contributor resources that we are trying out to give you the most access to up-to-date news, networking opportunities and more. \n* [Contributor Newsletter](https://pytorch.org/resources/contributors/) - Includes curated news including RFCs, feature roadmaps, notable PRs, editorials from developers, and more to support keeping track of everything that\u2019s happening in our community. \n* [Contributors Discussion Forum](https://dev-discuss.pytorch.org/) - Designed for contributors to learn and collaborate on the latest development across PyTorch. \n* [PyTorch Developer Podcast (Beta)](https://pytorch-dev-podcast.simplecast.com/) - Edward Yang, PyTorch Research Scientist, at Facebook AI shares bite-sized (10 to 20 mins) podcast episodes discussing topics about all sorts of internal development topics in PyTorch.\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/ecosystem-day-2021-recap/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Geospatial deep learning with TorchGeo\"\nauthor: Adam Stewart (University of Illinois at Urbana-Champaign), Caleb Robinson (Microsoft AI for Good Research Lab), Isaac Corley (University of Texas at San Antonio)\nfeatured-img: 'assets/images/torchgeo-hurricane.jpg'\n---\n\nTorchGeo is a PyTorch domain library providing datasets, samplers, transforms, and pre-trained models specific to geospatial data.\n\n

\n \n

\n\n

\n https://github.com/microsoft/torchgeo\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "For decades, Earth observation satellites, aircraft, and more recently UAV platforms have been collecting increasing amounts of imagery of the Earth\u2019s surface. With information about seasonal and long-term trends, remotely sensed imagery can be invaluable for solving some of the greatest challenges to humanity, including climate change adaptation, natural disaster monitoring, water resource management, and food security for a growing global population. From a computer vision perspective, this includes applications like land cover mapping (semantic segmentation), deforestation and flood monitoring (change detection), glacial flow (pixel tracking), hurricane tracking and intensity estimation (regression), and building and road detection (object detection, instance segmentation). By leveraging recent advancements in deep learning architectures, cheaper and more powerful GPUs, and petabytes of freely available satellite imagery datasets, we can come closer to solving these important problems.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} {"page_content": "

\n \n

\n\n

\nNational Oceanic and Atmospheric Administration satellite image of Hurricane Katrina, taken on August 28, 2005 (source). Geospatial machine learning libraries like TorchGeo can be used to detect, track, and predict future trajectories of hurricanes and other natural disasters.\n

\n\n# The challenges", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "In traditional computer vision datasets, such as ImageNet, the image files themselves tend to be rather simple and easy to work with. Most images have 3 spectral bands (RGB), are stored in common file formats like PNG or JPEG, and can be easily loaded with popular software libraries like [PIL](https://pillow.readthedocs.io/en/stable/) or [OpenCV](https://opencv.org/). Each image in these datasets is usually small enough to pass directly into a neural network. Furthermore, most of these datasets contain a", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "finite number of well-curated images that are assumed to be independent and identically distributed, making train-val-test splits straightforward. As a result of this relative homogeneity, the same pre-trained models (e.g., CNNs pretrained on ImageNet) have shown to be effective across a wide range of vision tasks using transfer learning methods. Existing libraries, such as [torchvision](https://github.com/pytorch/vision), handle these simple cases well, and have been used to make large advances in vision", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "tasks over the past decade.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Remote sensing imagery is not so uniform. Instead of simple RGB images, satellites tend to capture images that are multispectral ([Landsat 8](https://www.usgs.gov/landsat-missions) has 11 spectral bands) or even hyperspectral ([Hyperion](https://www.usgs.gov/centers/eros/science/usgs-eros-archive-earth-observing-one-eo-1-hyperion) has 242 spectral bands). These images capture information at a wider range of wavelengths (400 nm\u201315 \u00b5m), far outside of the visible spectrum. Different satellites also have very", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "different spatial resolutions\u2014[GOES](https://www.goes.noaa.gov/) has a resolution of 4 km/px, [Maxar](https://www.maxar.com/products/satellite-imagery) imagery is 30 cm/px, and drone imagery resolution can be as high as 7 mm/px. These datasets almost always have a temporal component, with satellite revisists that are daily, weekly, or biweekly. Images often have overlap with other images in the dataset, and need to be stitched together based on geographic metadata. These images tend to be very large (e.g.,", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "10K x 10K pixels), so it isn't possible to pass an entire image through a neural network. This data is distributed in hundreds of different raster and vector file formats like GeoTIFF and ESRI Shapefile, requiring specialty libraries like [GDAL](https://gdal.org/) to load.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\nFrom left to right: Mercator, Albers Equal Area, and Interrupted Goode Homolosine projections (source). Geospatial data is associated with one of many different types of reference systems that project the 3D Earth onto a 2D representation. Combining data from different sources often involves re-projecting to a common reference system in order to ensure that all layers are aligned.\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Although each image is 2D, the Earth itself is 3D. In order to stitch together images, they first need to be projected onto a 2D representation of the Earth, called a coordinate reference system (CRS). Most people are familiar with equal angle representations like Mercator that distort the size of regions (Greenland looks larger than Africa even though Africa is 15x larger), but there are many other CRSs that are commonly used. Each dataset may use a different CRS, and each image within a single dataset may", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "also be in a unique CRS. In order to use data from multiple layers, they must all share a common CRS, otherwise the data won't be properly aligned. For those who aren't familiar with remote sensing data, this can be a daunting task.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nEven if you correctly georeference images during indexing, if you don't project them to a common CRS, you'll end up with rotated images with nodata values around them, and the images won't be pixel-aligned.\n

\n\n# The solution", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "At the moment, it can be quite challenging to work with both deep learning models and geospatial data without having expertise in both of these very different fields. To address these challenges, we've built TorchGeo, a PyTorch domain library for working with geospatial data. TorchGeo is designed to make it simple:\n\n1. for machine learning experts to work with geospatial data, and\n2. for remote sensing experts to explore machine learning solutions.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "TorchGeo is not just a research project, but a production-quality library that uses continuous integration to test every commit with a range of Python versions on a range of platforms (Linux, macOS, Windows). It can be easily installed with any of your favorite package managers, including pip, conda, and [spack](https://spack.io):\n\n```\n$ pip install torchgeo", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "TorchGeo is designed to have the same API as other PyTorch domain libraries like torchvision, torchtext, and torchaudio. If you already use torchvision in your workflow for computer vision datasets, you can switch to TorchGeo by changing only a few lines of code. All TorchGeo datasets and samplers are compatible with the PyTorch ``DataLoader`` class, meaning that you can take advantage of wrapper libraries like [PyTorch Lightning](https://www.pytorchlightning.ai/) for distributed training. In the following", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "sections, we'll explore possible use cases for TorchGeo to show how simple it is to use.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "# Geospatial datasets and samplers\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\nExample application in which we combine A) a scene from Landsat 8 and B) Cropland Data Layer labels, even though these files are in different EPSG projections. We want to sample patches C) and D) from these datasets using a geospatial bounding box as an index.\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Many remote sensing applications involve working with [*geospatial datasets*](https://torchgeo.readthedocs.io/en/latest/api/datasets.html#geospatial-datasets) \u2014datasets with geographic metadata. In TorchGeo, we define a ``GeoDataset`` class to represent these kinds of datasets. Instead of being indexed by an integer, each ``GeoDataset`` is indexed by a spatiotemporal bounding box, meaning that two or more datasets covering a different geographic extent can be intelligently combined.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "In this example, we show how easy it is to work with geospatial data and to sample small image patches from a combination of Landsat and Cropland Data Layer (CDL) data using TorchGeo. First, we assume that the user has Landsat 7 and 8 imagery downloaded. Since Landsat 8 has more spectral bands than Landsat 7, we'll only use the bands that both satellites have in common. We'll create a single dataset including all images from both Landsat 7 and 8 data by taking the union between these two datasets.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "```c++\nfrom torch.utils.data import DataLoader\nfrom torchgeo.datasets import CDL, Landsat7, Landsat8, stack_samples\nfrom torchgeo.samplers import RandomGeoSampler\n\nlandsat7 = Landsat7(root=\"...\")\nlandsat8 = Landsat8(root=\"...\", bands=Landsat8.all_bands[1:-2])\nlandsat = landsat7 | landsat8", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Next, we take the intersection between this dataset and the CDL dataset. We want to take the intersection instead of the union to ensure that we only sample from regions where we have both Landsat and CDL data. Note that we can automatically download and checksum CDL data. Also note that each of these datasets may contain files in different CRSs or resolutions, but TorchGeo automatically ensures that a matching CRS and resolution is used.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "```c++\ncdl = CDL(root=\"...\", download=True, checksum=True)\ndataset = landsat & cdl", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "This dataset can now be used with a PyTorch data loader. Unlike benchmark datasets, geospatial datasets often include very large images. For example, the CDL dataset consists of a single image covering the entire contiguous United States. In order to sample from these datasets using geospatial coordinates, TorchGeo defines a number of [*samplers*](https://torchgeo.readthedocs.io/en/latest/api/samplers.html). In this example, we'll use a random sampler that returns 256 x 256 pixel images and 10,000 samples", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "per epoch. We'll also use a custom collation function to combine each sample dictionary into a mini-batch of samples.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "```c++\nsampler = RandomGeoSampler(dataset, size=256, length=10000)\ndataloader = DataLoader(dataset, batch_size=128, sampler=sampler, collate_fn=stack_samples)", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "This data loader can now be used in your normal training/evaluation pipeline.\n\n```c++\nfor batch in dataloader:\n image = batch[\"image\"]\n mask = batch[\"mask\"]\n\n # train a model, or make predictions using a pre-trained model", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Many applications involve intelligently composing datasets based on geospatial metadata like this. For example, users may want to:\n\n- Combine datasets for multiple image sources and treat them as equivalent (e.g., Landsat 7 and 8)\n- Combine datasets for disparate geospatial locations (e.g., Chesapeake NY and PA)\n\nThese combinations require that all queries are present in *at least one* dataset, and can be created using a ``UnionDataset``. Similarly, users may want to:", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "- Combine image and target labels and sample from both simultaneously (e.g., Landsat and CDL)\n- Combine datasets for multiple image sources for multimodal learning or data fusion (e.g., Landsat and Sentinel)\n\nThese combinations require that all queries are present in *both* datasets, and can be created using an ``IntersectionDataset``. TorchGeo automatically composes these datasets for you when you use the intersection (``&``) and union \\(``|``\\) operators.\n\n# Multispectral and geospatial transforms", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "In deep learning, it's common to augment and transform the data so that models are robust to variations in the input space. Geospatial data can have variations such as seasonal changes and warping effects, as well as image processing and capture issues like cloud cover and atmospheric distortion. TorchGeo utilizes augmentations and transforms from the [Kornia](https://kornia.github.io/) library, which supports GPU acceleration and supports multispectral imagery with more than 3 channels.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Traditional geospatial analyses compute and visualize spectral indices which are combinations of multispectral bands. Spectral indices are designed to highlight areas of interest in a multispectral image relevant to some application, such as vegetation health, areas of man-made change or increasing urbanization, or snow cover. TorchGeo supports numerous [*transforms*](https://torchgeo.readthedocs.io/en/latest/api/transforms.html), which can compute common spectral indices and append them as additional bands", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "to a multispectral image tensor.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Below, we show a simple example where we compute the Normalized Difference Vegetation Index (NDVI) on a Sentinel-2 image. NDVI measures the presence of vegetation and vegetation health and is computed as the normalized difference between the red and near-infrared (NIR) spectral bands. Spectral index transforms operate on sample dictionaries returned from TorchGeo datasets and append the resulting spectral index to the image channel dimension.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "First, we instantiate a Sentinel-2 dataset and load a sample image. Then, we plot the true color (RGB) representation of this data to see the region we are looking at.\n\n```c++\nimport matplotlib.pyplot as plt\nfrom torchgeo.datasets import Sentinel2\nfrom torchgeo.transforms import AppendNDVI\n\ndataset = Sentinel2(root=\"...\")\nsample = dataset[...]\nfig = dataset.plot(sample)\nplt.show()", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Next, we instantiate and compute an NDVI transform, appending this new channel to the end of the image. Sentinel-2 imagery uses index 0 for its red band and index 3 for its NIR band. In order to visualize the data, we also normalize the image. NDVI values can range from -1 to 1, but we want to use the range 0 to 1 for plotting.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "```c++\ntransform = AppendNDVI(index_red=0, index_nir=3)\nsample = transform(sample)\nsample[\"image\"][-1] = (sample[\"image\"][-1] + 1) / 2\nplt.imshow(sample[\"image\"][-1], cmap=\"RdYlGn_r\")\nplt.show()", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nTrue color (left) and NDVI (right) of the Texas Hill Region, taken on November 16, 2018 by the Sentinel-2 satellite. In the NDVI image, red indicates water bodies, yellow indicates barren soil, light green indicates unhealthy vegetation, and dark green indicates healthy vegetation.\n

\n\n# Benchmark datasets", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "One of the driving factors behind progress in computer vision is the existence of standardized benchmark datasets like ImageNet and MNIST. Using these datasets, researchers can directly compare the performance of different models and training procedures to determine which perform the best. In the remote sensing domain, there are many such datasets, but due to the aforementioned difficulties of working with this data and the lack of existing libraries for loading these datasets, many researchers opt to use", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "their own custom datasets.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "One of the goals of TorchGeo is to provide easy-to-use data loaders for these existing datasets. TorchGeo includes a number of [*benchmark datasets*](https://torchgeo.readthedocs.io/en/latest/api/datasets.html#non-geospatial-datasets) \u2014datasets that include both input images and target labels. This includes datasets for tasks like image classification, regression, semantic segmentation, object detection, instance segmentation, change detection, and more.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "If you've used torchvision before, these types of datasets should be familiar. In this example, we'll create a dataset for the Northwestern Polytechnical University (NWPU) very-high-resolution ten-class (VHR-10) geospatial object detection dataset. This dataset can be automatically downloaded, checksummed, and extracted, just like with torchvision.\n\n```c++\nfrom torch.utils.data import DataLoader\nfrom torchgeo.datasets import VHR10", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "dataset = VHR10(root=\"...\", download=True, checksum=True)\ndataloader = DataLoader(dataset, batch_size=128, shuffle=True, num_workers=4)\n\nfor batch in dataloader:\n image = batch[\"image\"]\n label = batch[\"label\"]\n\n # train a model, or make predictions using a pre-trained model", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "All TorchGeo datasets are compatible with PyTorch data loaders, making them easy to integrate into existing training workflows. The only difference between a benchmark dataset in TorchGeo and a similar dataset in torchvision is that each dataset returns a dictionary with keys for each PyTorch ``Tensor``.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\nExample predictions from a Mask R-CNN model trained on the NWPU VHR-10 dataset. The model predicts sharp bounding boxes and masks for all objects with high confidence scores.\n

\n\n# Reproducibility with PyTorch Lightning", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "In traditional computer vision datasets, such as ImageNet, the image files themselves tend to be rather simple and easy to work with. Most images have 3 spectral bands (RGB), are stored in common file formats like PNG or JPEG, and can be easily loaded with popular software libraries like [PIL](https://pillow.readthedocs.io/en/stable/) or [OpenCV](https://opencv.org/). Each image in these datasets is usually small enough to pass directly into a neural network. Furthermore, most of these datasets contain a finite number of well-curated images that are assumed to be independent and identically distributed, making train-val-test splits straightforward. As a result of this relative homogeneity, the same pre-trained models (e.g., CNNs pretrained on ImageNet) have shown to be effective across a wide range of vision tasks using transfer learning methods. Existing libraries, such as [torchvision](https://github.com/pytorch/vision), handle these simple cases well, and have been used to make large advances in vision tasks over the past decade.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Remote sensing imagery is not so uniform. Instead of simple RGB images, satellites tend to capture images that are multispectral ([Landsat 8](https://www.usgs.gov/landsat-missions) has 11 spectral bands) or even hyperspectral ([Hyperion](https://www.usgs.gov/centers/eros/science/usgs-eros-archive-earth-observing-one-eo-1-hyperion) has 242 spectral bands). These images capture information at a wider range of wavelengths (400 nm\u201315 \u00b5m), far outside of the visible spectrum. Different satellites also have very different spatial resolutions\u2014[GOES](https://www.goes.noaa.gov/) has a resolution of 4 km/px, [Maxar](https://www.maxar.com/products/satellite-imagery) imagery is 30 cm/px, and drone imagery resolution can be as high as 7 mm/px. These datasets almost always have a temporal component, with satellite revisists that are daily, weekly, or biweekly. Images often have overlap with other images in the dataset, and need to be stitched together based on geographic metadata. These images tend to be very large (e.g., 10K x 10K pixels), so it isn't possible to pass an entire image through a neural network. This data is distributed in hundreds of different raster and vector file formats like GeoTIFF and ESRI Shapefile, requiring specialty libraries like [GDAL](https://gdal.org/) to load.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n\n

\nFrom left to right: Mercator, Albers Equal Area, and Interrupted Goode Homolosine projections (source). Geospatial data is associated with one of many different types of reference systems that project the 3D Earth onto a 2D representation. Combining data from different sources often involves re-projecting to a common reference system in order to ensure that all layers are aligned.\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Although each image is 2D, the Earth itself is 3D. In order to stitch together images, they first need to be projected onto a 2D representation of the Earth, called a coordinate reference system (CRS). Most people are familiar with equal angle representations like Mercator that distort the size of regions (Greenland looks larger than Africa even though Africa is 15x larger), but there are many other CRSs that are commonly used. Each dataset may use a different CRS, and each image within a single dataset may also be in a unique CRS. In order to use data from multiple layers, they must all share a common CRS, otherwise the data won't be properly aligned. For those who aren't familiar with remote sensing data, this can be a daunting task.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "

\nEven if you correctly georeference images during indexing, if you don't project them to a common CRS, you'll end up with rotated images with nodata values around them, and the images won't be pixel-aligned.\n

\n\n# The solution\n\nAt the moment, it can be quite challenging to work with both deep learning models and geospatial data without having expertise in both of these very different fields. To address these challenges, we've built TorchGeo, a PyTorch domain library for working with geospatial data. TorchGeo is designed to make it simple:\n\n1. for machine learning experts to work with geospatial data, and\n2. for remote sensing experts to explore machine learning solutions.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "TorchGeo is not just a research project, but a production-quality library that uses continuous integration to test every commit with a range of Python versions on a range of platforms (Linux, macOS, Windows). It can be easily installed with any of your favorite package managers, including pip, conda, and [spack](https://spack.io):\n\n```\n$ pip install torchgeo\n```\n\nTorchGeo is designed to have the same API as other PyTorch domain libraries like torchvision, torchtext, and torchaudio. If you already use torchvision in your workflow for computer vision datasets, you can switch to TorchGeo by changing only a few lines of code. All TorchGeo datasets and samplers are compatible with the PyTorch ``DataLoader`` class, meaning that you can take advantage of wrapper libraries like [PyTorch Lightning](https://www.pytorchlightning.ai/) for distributed training. In the following sections, we'll explore possible use cases for TorchGeo to show how simple it is to use.\n\n# Geospatial datasets and samplers", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\nExample application in which we combine A) a scene from Landsat 8 and B) Cropland Data Layer labels, even though these files are in different EPSG projections. We want to sample patches C) and D) from these datasets using a geospatial bounding box as an index.\n

\n\nMany remote sensing applications involve working with [*geospatial datasets*](https://torchgeo.readthedocs.io/en/latest/api/datasets.html#geospatial-datasets) \u2014datasets with geographic metadata. In TorchGeo, we define a ``GeoDataset`` class to represent these kinds of datasets. Instead of being indexed by an integer, each ``GeoDataset`` is indexed by a spatiotemporal bounding box, meaning that two or more datasets covering a different geographic extent can be intelligently combined.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "In this example, we show how easy it is to work with geospatial data and to sample small image patches from a combination of Landsat and Cropland Data Layer (CDL) data using TorchGeo. First, we assume that the user has Landsat 7 and 8 imagery downloaded. Since Landsat 8 has more spectral bands than Landsat 7, we'll only use the bands that both satellites have in common. We'll create a single dataset including all images from both Landsat 7 and 8 data by taking the union between these two datasets.\n\n```c++\nfrom torch.utils.data import DataLoader\nfrom torchgeo.datasets import CDL, Landsat7, Landsat8, stack_samples\nfrom torchgeo.samplers import RandomGeoSampler\n\nlandsat7 = Landsat7(root=\"...\")\nlandsat8 = Landsat8(root=\"...\", bands=Landsat8.all_bands[1:-2])\nlandsat = landsat7 | landsat8\n```", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Next, we take the intersection between this dataset and the CDL dataset. We want to take the intersection instead of the union to ensure that we only sample from regions where we have both Landsat and CDL data. Note that we can automatically download and checksum CDL data. Also note that each of these datasets may contain files in different CRSs or resolutions, but TorchGeo automatically ensures that a matching CRS and resolution is used.\n\n```c++\ncdl = CDL(root=\"...\", download=True, checksum=True)\ndataset = landsat & cdl\n```", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "This dataset can now be used with a PyTorch data loader. Unlike benchmark datasets, geospatial datasets often include very large images. For example, the CDL dataset consists of a single image covering the entire contiguous United States. In order to sample from these datasets using geospatial coordinates, TorchGeo defines a number of [*samplers*](https://torchgeo.readthedocs.io/en/latest/api/samplers.html). In this example, we'll use a random sampler that returns 256 x 256 pixel images and 10,000 samples per epoch. We'll also use a custom collation function to combine each sample dictionary into a mini-batch of samples.\n\n```c++\nsampler = RandomGeoSampler(dataset, size=256, length=10000)\ndataloader = DataLoader(dataset, batch_size=128, sampler=sampler, collate_fn=stack_samples)\n```\n\nThis data loader can now be used in your normal training/evaluation pipeline.\n\n```c++\nfor batch in dataloader:\n image = batch[\"image\"]\n mask = batch[\"mask\"]", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "# train a model, or make predictions using a pre-trained model\n```\n\nMany applications involve intelligently composing datasets based on geospatial metadata like this. For example, users may want to:\n\n- Combine datasets for multiple image sources and treat them as equivalent (e.g., Landsat 7 and 8)\n- Combine datasets for disparate geospatial locations (e.g., Chesapeake NY and PA)\n\nThese combinations require that all queries are present in *at least one* dataset, and can be created using a ``UnionDataset``. Similarly, users may want to:\n\n- Combine image and target labels and sample from both simultaneously (e.g., Landsat and CDL)\n- Combine datasets for multiple image sources for multimodal learning or data fusion (e.g., Landsat and Sentinel)\n\nThese combinations require that all queries are present in *both* datasets, and can be created using an ``IntersectionDataset``. TorchGeo automatically composes these datasets for you when you use the intersection (``&``) and union \\(``|``\\) operators.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "# Multispectral and geospatial transforms\n\nIn deep learning, it's common to augment and transform the data so that models are robust to variations in the input space. Geospatial data can have variations such as seasonal changes and warping effects, as well as image processing and capture issues like cloud cover and atmospheric distortion. TorchGeo utilizes augmentations and transforms from the [Kornia](https://kornia.github.io/) library, which supports GPU acceleration and supports multispectral imagery with more than 3 channels.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Traditional geospatial analyses compute and visualize spectral indices which are combinations of multispectral bands. Spectral indices are designed to highlight areas of interest in a multispectral image relevant to some application, such as vegetation health, areas of man-made change or increasing urbanization, or snow cover. TorchGeo supports numerous [*transforms*](https://torchgeo.readthedocs.io/en/latest/api/transforms.html), which can compute common spectral indices and append them as additional bands to a multispectral image tensor.\n\nBelow, we show a simple example where we compute the Normalized Difference Vegetation Index (NDVI) on a Sentinel-2 image. NDVI measures the presence of vegetation and vegetation health and is computed as the normalized difference between the red and near-infrared (NIR) spectral bands. Spectral index transforms operate on sample dictionaries returned from TorchGeo datasets and append the resulting spectral index to the image channel dimension.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "First, we instantiate a Sentinel-2 dataset and load a sample image. Then, we plot the true color (RGB) representation of this data to see the region we are looking at.\n\n```c++\nimport matplotlib.pyplot as plt\nfrom torchgeo.datasets import Sentinel2\nfrom torchgeo.transforms import AppendNDVI\n\ndataset = Sentinel2(root=\"...\")\nsample = dataset[...]\nfig = dataset.plot(sample)\nplt.show()\n```\n\nNext, we instantiate and compute an NDVI transform, appending this new channel to the end of the image. Sentinel-2 imagery uses index 0 for its red band and index 3 for its NIR band. In order to visualize the data, we also normalize the image. NDVI values can range from -1 to 1, but we want to use the range 0 to 1 for plotting.\n\n```c++\ntransform = AppendNDVI(index_red=0, index_nir=3)\nsample = transform(sample)\nsample[\"image\"][-1] = (sample[\"image\"][-1] + 1) / 2\nplt.imshow(sample[\"image\"][-1], cmap=\"RdYlGn_r\")\nplt.show()\n```\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "

\nTrue color (left) and NDVI (right) of the Texas Hill Region, taken on November 16, 2018 by the Sentinel-2 satellite. In the NDVI image, red indicates water bodies, yellow indicates barren soil, light green indicates unhealthy vegetation, and dark green indicates healthy vegetation.\n

\n\n# Benchmark datasets\n\nOne of the driving factors behind progress in computer vision is the existence of standardized benchmark datasets like ImageNet and MNIST. Using these datasets, researchers can directly compare the performance of different models and training procedures to determine which perform the best. In the remote sensing domain, there are many such datasets, but due to the aforementioned difficulties of working with this data and the lack of existing libraries for loading these datasets, many researchers opt to use their own custom datasets.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "One of the goals of TorchGeo is to provide easy-to-use data loaders for these existing datasets. TorchGeo includes a number of [*benchmark datasets*](https://torchgeo.readthedocs.io/en/latest/api/datasets.html#non-geospatial-datasets) \u2014datasets that include both input images and target labels. This includes datasets for tasks like image classification, regression, semantic segmentation, object detection, instance segmentation, change detection, and more.\n\nIf you've used torchvision before, these types of datasets should be familiar. In this example, we'll create a dataset for the Northwestern Polytechnical University (NWPU) very-high-resolution ten-class (VHR-10) geospatial object detection dataset. This dataset can be automatically downloaded, checksummed, and extracted, just like with torchvision.\n\n```c++\nfrom torch.utils.data import DataLoader\nfrom torchgeo.datasets import VHR10", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "dataset = VHR10(root=\"...\", download=True, checksum=True)\ndataloader = DataLoader(dataset, batch_size=128, shuffle=True, num_workers=4)\n\nfor batch in dataloader:\n image = batch[\"image\"]\n label = batch[\"label\"]\n\n # train a model, or make predictions using a pre-trained model\n```\n\nAll TorchGeo datasets are compatible with PyTorch data loaders, making them easy to integrate into existing training workflows. The only difference between a benchmark dataset in TorchGeo and a similar dataset in torchvision is that each dataset returns a dictionary with keys for each PyTorch ``Tensor``.\n\n

\n \n

\n\n

\nExample predictions from a Mask R-CNN model trained on the NWPU VHR-10 dataset. The model predicts sharp bounding boxes and masks for all objects with high confidence scores.\n

\n\n# Reproducibility with PyTorch Lightning", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} {"page_content": "Another key goal of TorchGeo is reproducibility. For many of these benchmark datasets, there is no predefined train-val-test split, or the predefined split has issues with class imbalance or geographic distribution. As a result, the performance metrics reported in the literature either can't be reproduced, or aren't indicative of how well a pre-trained model would work in a different geographic location.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "In order to facilitate direct comparisons between results published in the literature and further reduce the boilerplate code needed to run experiments with datasets in TorchGeo, we have created PyTorch Lightning [*datamodules*](https://torchgeo.readthedocs.io/en/latest/api/datamodules.html) with well-defined train-val-test splits and [*trainers*](https://torchgeo.readthedocs.io/en/latest/api/trainers.html) for various tasks like classification, regression, and semantic segmentation. These datamodules show", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "how to incorporate augmentations from the kornia library, include preprocessing transforms (with pre-calculated channel statistics), and let users easily experiment with hyperparameters related to the data itself (as opposed to the modeling process). Training a semantic segmentation model on the Inria Aerial Image Labeling dataset is as easy as a few imports and four lines of code.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "```c++\nfrom pytorch_lightning import Trainer\nfrom torchgeo.datamodules import InriaAerialImageLabelingDataModule\nfrom torchgeo.trainers import SemanticSegmentationTask\n\ndatamodule = InriaAerialImageLabelingDataModule(root_dir=\"...\", batch_size=64, num_workers=6)\ntask = SemanticSegmentationTask(segmentation_model=\"unet\", encoder_weights=\"imagenet\", learning_rate=0.1)\ntrainer = Trainer(gpus=1, default_root_dir=\"...\")\n\ntrainer.fit(model=task, datamodule=datamodule)", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nBuilding segmentations produced by a U-Net model trained on the Inria Aerial Image Labeling dataset. Reproducing these results is as simple as a few imports and four lines of code, making comparison of different models and training techniques simple and easy.\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "In our [preprint](https://arxiv.org/abs/2111.08872) we show a set of results that use the aforementioned datamodules and trainers to benchmark simple modeling approaches for several of the datasets in TorchGeo. For example, we find that a simple ResNet-50 can achieve state-of-the-art performance on the [So2Sat](https://ieeexplore.ieee.org/document/9014553) dataset. These types of baseline results are important for evaluating the contribution of different modeling choices when tackling problems with remotely", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "sensed data.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "# Future work and contributing\n\nThere is still a lot of remaining work to be done in order to make TorchGeo as easy to use as possible, especially for users without prior deep learning experience. One of the ways in which we plan to achieve this is by expanding our tutorials to include subjects like \"writing a custom dataset\" and \"transfer learning\", or tasks like \"land cover mapping\" and \"object detection\".", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Another important project we are working on is pre-training models. Most remote sensing researchers work with very small labeled datasets, and could benefit from pre-trained models and transfer learning approaches. TorchGeo is the first deep learning library to provide models pre-trained on multispectral imagery. Our goal is to provide models for different image modalities (optical, SAR, multispectral) and specific platforms (Landsat, Sentinel, MODIS) as well as benchmark results showing their performance", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "with different amounts of training data. Self-supervised learning is a promising method for training such models. Satellite imagery datasets often contain petabytes of imagery, but accurately labeled datasets are much harder to come by. Self-supervised learning methods will allow us to train directly on the raw imagery without needing large labeled datasets.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Aside from these larger projects, we're always looking to add new datasets, data augmentation transforms, and sampling strategies. If you're Python savvy and interested in contributing to TorchGeo, we would love to see contributions! TorchGeo is open source under an MIT license, so you can use it in almost any project.\n\nExternal links:", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "- **Homepage**: [https://github.com/microsoft/torchgeo](https://github.com/microsoft/torchgeo)\n- **Documentation**: [https://torchgeo.readthedocs.io/](https://torchgeo.readthedocs.io/)\n- **PyPI**: [https://pypi.org/project/torchgeo/](https://pypi.org/project/torchgeo/)\n- **Paper**: [https://arxiv.org/abs/2111.08872](https://arxiv.org/abs/2111.08872)\n\nIf you like TorchGeo, give us a star on GitHub! And if you use TorchGeo in your work, please cite our paper.\n\n# Acknowledgments", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "*We would like to thank all TorchGeo contributors for their efforts in creating the library, the Microsoft AI for Good program for support, and the PyTorch Team for their guidance. This research is part of the Blue Waters sustained-petascale computing project, which is supported by the National Science Foundation (awards OCI-0725070 and ACI-1238993), the State of Illinois, and as of December, 2019, the National Geospatial-Intelligence Agency. Blue Waters is a joint effort of the University of Illinois at", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "Urbana-Champaign and its National Center for Supercomputing Applications. The research was supported in part by NSF grants IIS-1908104, OAC-1934634, and DBI-2021898.*", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'What\u2019s New in PyTorch Profiler 1.9?'\nauthor: Sabrina Smai, Program Manager on the AI Framework team at Microsoft\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Profiler v1.9 has been released! The goal of this new release (previous [PyTorch Profiler release](https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/)) is to provide you with new state-of-the-art tools to help diagnose and fix machine learning performance issues regardless of whether you are working on one or numerous machines. The objective is to target the execution steps that are the most costly in time and/or memory, and visualize the work load", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "distribution between GPUs and CPUs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Here is a summary of the five major features being released:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "1.\t**Distributed Training View**: This helps you understand how much time and memory is consumed in your distributed training job. Many issues occur when you take a training model and split the load into worker nodes to be run in parallel as it can be a black box. The overall model goal is to speed up model training. This distributed training view will help you diagnose and debug issues within individual nodes.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "2.\t**Memory View**: This view allows you to understand your memory usage better. This tool will help you avoid the famously pesky Out of Memory error by showing active memory allocations at various points of your program run. \n3.\t**GPU Utilization Visualization**: This tool helps you make sure that your GPU is being fully utilized. \n4.\t**Cloud Storage Support**: Tensorboard plugin can now read profiling data from Azure Blob Storage, Amazon S3, and Google Cloud Platform.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "5.\t**Jump to Source Code**: This feature allows you to visualize stack tracing information and jump directly into the source code. This helps you quickly optimize and iterate on your code based on your profiling results.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Getting Started with PyTorch Profiling Tool\nPyTorch includes a profiling functionality called \u00ab PyTorch Profiler \u00bb. The PyTorch Profiler tutorial can be found [here](https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html).\n\nTo instrument your PyTorch code for profiling, you must:\n\n$ pip install torch-tb-profiler\n\n```python\nimport torch.profiler as profiler\nWith profiler.profile(XXXX)", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "**Comments**:\n\n\u2022 For CUDA and CPU profiling, see [below](https://github.com/pytorch/kineto/blob/master/tb_plugin/examples/resnet50_profiler_api.py): \n```\nwith torch.profiler.profile( \nactivities=[ \ntorch.profiler.ProfilerActivity.CPU, \ntorch.profiler.ProfilerActivity.CUDA], \n```\n\n\u2022\tWith profiler.record_function(\u201c$NAME\u201d): allows putting a decorator (a tag associated to a name) for a block of function\n\n\u2022\tProfile_memory=True parameter under profiler.profile allows you to profile CPU and GPU memory footprint", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Visualizing PyTorch Model Performance using PyTorch Profiler", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Distributed Training \n\nRecent advances in deep learning argue for the value of large datasets and large models, which requires you to scale out model training to more computational resources. Distributed Data Parallel (DDP) and NVIDIA Collective Communications Library (NCCL) are the widely adopted paradigms in PyTorch for accelerating your deep learning training. \n\nIn this release of PyTorch Profiler, DDP with NCCL backend is now supported.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Computation/Communication Overview\n\nIn the Computation/Communication overview under the Distributed training view, you can observe the computation-to-communication ratio of each worker and [load balancer](https://en.wikipedia.org/wiki/Load_balancing_(computing) nodes between worker as measured by granularity. \n\n**Scenario 1**:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "If the computation and overlapping time of one worker is much larger than the others, this may suggest an issue in the workload balance or worker being a straggler. Computation is the sum of kernel time on GPU minus the overlapping time. The overlapping time is the time saved by interleaving communications during computation. The more overlapping time represents better parallelism between computation and communication. Ideally the computation and communication completely overlap with each other.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Communication is the total communication time minus the overlapping time. The example image below displays how this scenario appears on Tensorboard.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Figure: A straggler example

\n
\n\n**Scenario 2**:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "If there is a small batch size (i.e. less computation on each worker) or the data to be transferred is large, the computation-to-communication may also be small and be seen in the profiler with low GPU utilization and long waiting times. This computation/communication view will allow you to diagnose your code to reduce communication by adopting gradient accumulation, or to decrease the communication proportion by increasing batch size. DDP communication time depends on model size. Batch size has no", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "relationship with model size. So increasing batch size could make computation time longer and make computation-to-communication ratio bigger.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Synchronizing/Communication Overview", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "In the Synchronizing/Communication view, you can observe the efficiency of communication. This is done by taking the step time minus computation and communication time. Synchronizing time is part of the total communication time for waiting and synchronizing with other workers. The Synchronizing/Communication view includes initialization, data loader, CPU computation, and so on Insights like what is the ratio of total communication is really used for exchanging data and what is the idle time of waiting for", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "data from other workers can be drawn from this view.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nFor example, if there is an inefficient workload balance or straggler issue, you\u2019ll be able to identify it in this Synchronizing/Communication view. This view will show several workers\u2019 waiting time being longer than others. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "This table view above allows you to see the detailed statistics of all communication ops in each node. This allows you to see what operation types are being called, how many times each op is called, what is the size of the data being transferred by each op, etc.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Memory View:\n\nThis memory view tool helps you understand the hardware resource consumption of the operators in your model. Understanding the time and memory consumption on the operator-level allows you to resolve performance bottlenecks and in turn, allow your model to execute faster. Given limited GPU memory size, optimizing the memory usage can: \n\n1. Allow bigger model which can potentially generalize better on end level tasks.\n2. Allow bigger batch size. Bigger batch sizes increase the training speed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "The profiler records all the memory allocation during the profiler interval. Selecting the \u201cDevice\u201d will allow you to see each operator\u2019s memory usage on the GPU side or host side. You must enable ```profile_memory=True``` to generate the below memory data as shown [here](https://github.com/pytorch/kineto/blob/master/tb_plugin/examples/resnet50_profiler_api.py#L39). \n\n```\nWith torch.profiler.profile(\nProfiler_memory=True # this will take 1 \u2013 2 minutes to complete. \n)", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "**Important Definitions**:\n\n\u2022\t\u201cSize Increase\u201d displays the sum of all allocation bytes and minus all the memory release bytes.\n\n\u2022\t\u201cAllocation Size\u201d shows the sum of all allocation bytes without considering the memory release.\n\n\u2022\t\u201cSelf\u201d means the allocated memory is not from any child operators, instead by the operator itself.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "GPU Metric on Timeline:\n\nThis feature will help you debug performance issues when one or more GPU are underutilized. Ideally, your program should have high GPU utilization (aiming for 100% GPU utilization), minimal CPU to GPU communication, and no overhead.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "**Overview**:\nThe overview page highlights the results of three important GPU usage metrics at different levels (i.e. GPU Utilization, Est. SM Efficiency, and Est. Achieved Occupancy). Essentially, each GPU has a bunch of SM each with a bunch of warps that can execute a bunch of threads concurrently. Warps execute a bunch because the amount depends on the GPU. But at a high level, this GPU Metric on Timeline tool allows you can see the whole stack, which is useful.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "If the GPU utilization result is low, this suggests a potential bottleneck is present in your model. Common reasons: \n\n\u2022Insufficient parallelism in kernels (i.e., low batch size) \n\n\u2022Small kernels called in a loop. This is to say the launch overheads are not amortized \n\n\u2022CPU or I/O bottlenecks lead to the GPU not receiving enough work to keep busy", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Looking of the overview page where the performance recommendation section is where you\u2019ll find potential suggestions on how to increase that GPU utilization. In this example, GPU utilization is low so the performance recommendation was to increase batch size. Increasing batch size 4 to 32, as per the performance recommendation, increased the GPU Utilization by 60.68%.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "GPU Utilization: the step interval time in the profiler when a GPU engine was executing a workload. The high the utilization %, the better. The drawback of using GPU utilization solely to diagnose performance bottlenecks is it is too high-level and coarse. It won\u2019t be able to tell you how many Streaming Multiprocessors are in use. Note that while this metric is useful for detecting periods of idleness, a high value does not indicate efficient use of the GPU, only that it is doing anything at all. For", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "instance, a kernel with a single thread running continuously will get a GPU Utilization of 100%", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Estimated Stream Multiprocessor Efficiency (Est. SM Efficiency) is a finer grained metric, it indicates what percentage of SMs are in use at any point in the trace This metric reports the percentage of time where there is at least one active warp on a SM and those that are stalled (NVIDIA", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[doc](https://forums.developer.nvidia.com/t/nvprof-question-about-the-sm-efficiency-metric/72640#:~:text=My%20understanding%20from%20the%20profiler%20documentation%20is%20that,that%20%E2%80%9Cactive%20warps%E2%80%9D%20include%20warps%20that%20are%20stalled.)). Est. SM Efficiency also has it\u2019s limitation. For instance, a kernel with only one thread per block can\u2019t fully use each SM. SM Efficiency does not tell us how busy each SM is, only that they are doing anything at all, which can include stalling while", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "waiting on the result of a memory load. To keep an SM busy, it is necessary to have a sufficient number of ready warps that can be run whenever a stall occurs", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Estimated Achieved Occupancy (Est. Achieved Occupancy) is a layer deeper than Est. SM Efficiency and GPU Utilization for diagnosing performance issues. Estimated Achieved Occupancy indicates how many warps can be active at once per SMs. Having a sufficient number of active warps is usually key to achieving good throughput. Unlike GPU Utilization and SM Efficiency, it is not a goal to make this value as high as possible. As a rule of thumb, good throughput gains can be had by improving this metric to 15% and", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "above. But at some point you will hit diminishing returns. If the value is already at 30% for example, further gains will be uncertain. This metric reports the average values of all warp schedulers for the kernel execution period (NVIDIA [doc](https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/cudaexperiments/kernellevel/achievedoccupancy.htm)). The larger the Est. Achieve Occupancy value is the better.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Overview details: Resnet50_batchsize4

\n
\n\n
\n \n

Overview details: Resnet50_batchsize32

\n
\n\n_Kernel View_\nThe kernel has \u201cBlocks per SM\u201d and \u201cEst. Achieved Occupancy\u201d which is a great tool to compare model runs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nMean Blocks per SM: \nBlocks per SM = Blocks of this kernel / SM number of this GPU. If this number is less than 1, it indicates the GPU multiprocessors are not fully utilized. \u201cMean Blocks per SM\u201d is weighted average of all runs of this kernel name, using each run\u2019s duration as weight.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Mean Est. Achieved Occupancy: \nEst. Achieved Occupancy is defined as above in overview. \u201cMean Est. Achieved Occupancy\u201d is weighted average of all runs of this kernel name, using each run\u2019s duration as weight.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "_Trace View_\nThis trace view displays a timeline that shows the duration of operators in your model and which system executed the operation. This view can help you identify whether the high consumption and long execution is because of input or model training. Currently, this trace view shows GPU Utilization and Est. SM Efficiency on a timeline. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "GPU utilization is calculated independently and divided into multiple 10 millisecond buckets. The buckets\u2019 GPU utilization values are drawn alongside the timeline between 0 \u2013 100%. In the above example, the \u201cProfilerStep5\u201d GPU utilization during thread 28022\u2019s busy time is higher than the following the one during \u201cOptimizer.step\u201d. This is where you can zoom-in to investigate why that is. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "From above, we can see the former\u2019s kernels are longer than the later\u2019s kernels. The later\u2019s kernels are too short in execution, which results in lower GPU utilization. \n\nEst. SM Efficiency: Each kernel has a calculated est. SM efficiency between 0 \u2013 100%. For example, the below kernel has only 64 blocks, while the SMs in this GPU is 80. Then its \u201cEst. SM Efficiency\u201d is 64/80, which is 0.8.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Cloud Storage Support\n\nAfter running pip install tensorboard, to have data be read through these cloud providers, you can now run: \n\n``` sh \ntorch-tb-profiler[blob] \ntorch-tb-profiler[gs] \ntorch-tb-profiler[s3] \n``` \n```pip install torch-tb-profiler[blob]```, ```pip install torch-tb-profiler[gs]```, or ```pip install torch-tb-profiler[S3]``` to have data be read through these cloud providers. For more information, please refer to this [README](https://github.com/pytorch/kineto/tree/main/tb_plugin).", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Jump to Source Code:\n\nOne of the great benefits of having both TensorBoard and the PyTorch Profiler being integrated directly in Visual Studio Code (VS Code) is the ability to directly jump to the source code (file and line) from the profiler stack traces. VS Code Python Extension now [supports TensorBoard Integration](https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2021-release/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Jump to source is ONLY available when Tensorboard is launched within VS Code. Stack tracing will appear on the plugin UI if the profiling with_stack=True. When you click on a stack trace from the PyTorch Profiler, VS Code will automatically open the corresponding file side by side and jump directly to the line of code of interest for you to debug. This allows you to quickly make actionable optimizations and changes to your code based on the profiling results and suggestions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Gify: Jump to Source using Visual Studio Code Plug In UI

\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "For how to optimize batch size performance, check out the step-by-step tutorial [here](https://opendatascience.com/optimizing-pytorch-performance-batch-size-with-pytorch-profiler/). PyTorch Profiler is also integrated with [PyTorch Lightning](https://pytorch-lightning.readthedocs.io/en/stable/advanced/profiler.html#pytorch-profiling) and you can simply launch your lightning training jobs with --```trainer.profiler=pytorch``` flag to generate the traces. Check out an example", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "[here](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pl_examples/basic_examples/profiler_example.py).", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "What\u2019s Next for the PyTorch Profiler?\nYou just saw how PyTorch Profiler can help optimize a model. You can now try the Profiler by ```pip install torch-tb-profiler``` to optimize your PyTorch model. \n\nLook out for an advanced version of this tutorial in the future. We are also thrilled to continue to bring state-of-the-art tool to PyTorch users to improve ML performance. We'd love to hear from you. Feel free to open an issue [here](https://github.com/pytorch/kineto/issues).", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "For new and exciting features coming up with PyTorch Profiler, follow @PyTorch on Twitter and check us out on pytorch.org.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nThe author would like to thank the contributions of the following individuals to this piece. From the Facebook side: Geeta Chauhan, Gisle Dankel, Woo Kim, Sam Farahzad, and Mark Saroufim. On the Microsoft side: AI Framework engineers (Teng Gao, Mike Guo, and Yang Gu), Guoliang Hua, and Thuy Nguyen.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the Winners of the 2020 Global PyTorch Summer Hackathon'\nauthor: Team PyTorch\n---\n\nMore than 2,500 participants in this year\u2019s Global PyTorch Summer Hackathon pushed the envelope to create unique new tools and applications for PyTorch developers and researchers.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "***Notice**: None of the projects submitted to the hackathon are associated with or offered by Facebook, Inc.* \n\nThis year\u2019s projects fell into three categories:\n\n* **PyTorch Developer Tools:** a tool or library for improving productivity and efficiency for PyTorch researchers and developers.\n\n* **Web/Mobile Applications Powered by PyTorch:** a web or mobile interface and/or an embedded device built using PyTorch.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "* **PyTorch Responsible AI Development Tools:** a tool, library, or web/mobile app to support researchers and developers in creating responsible AI that factors in fairness, security, privacy, and more throughout its entire development process.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "The virtual hackathon ran from June 22 to August 25, with more than 2,500 registered participants, representing 114 countries from Republic of Azerbaijan, to Zimbabwe, to Japan, submitting a total of 106 projects. Entrants were judged on their idea\u2019s quality, originality, potential impact, and how well they implemented it.\n\nMeet the winners of each category below.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Developer Tools\n\n**1st place** - [DeMask](https://devpost.com/software/asteroid-the-pytorch-based-source-separation-toolkit)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "DeMask is an end-to-end model for enhancing speech while wearing face masks \u2014 offering a clear benefit during times when face masks are mandatory in many spaces and for workers who wear face masks on the job. Built with [Asteroid](https://github.com/mpariente/asteroid), a PyTorch-based audio source separation toolkit, DeMask is trained to recognize distortions in speech created by the muffling from face masks and to adjust the speech to make it sound clearer.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "This submission stood out in particular because it represents both a high-quality idea and an implementation that can be reproduced by other researchers.\n\nHere is an example on how to train a speech separation model in less than 20 lines:\n\n```python\nfrom torch import optim\nfrom pytorch_lightning import Trainer\n\nfrom asteroid import ConvTasNet\nfrom asteroid.losses import PITLossWrapper\nfrom asteroid.data import LibriMix\nfrom asteroid.engine import System", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "train_loader, val_loader = LibriMix.loaders_from_mini(task='sep_clean', batch_size=4)\nmodel = ConvTasNet(n_src=2)\noptimizer = optim.Adam(model.parameters(), lr=1e-3)\nloss = PITLossWrapper(\n lambda x, y: (x - y).pow(2).mean(-1), # MSE\n pit_from=\"pw_pt\", # Point in the pairwise matrix.\n)\n\nsystem = System(model, optimizer, loss, train_loader, val_loader)\n\ntrainer = Trainer(fast_dev_run=True)\ntrainer.fit(system)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**2nd place** - [carefree-learn](https://devpost.com/software/carefree-learn)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "A PyTorch-based automated machine learning (AutoML) solution, carefree-learn provides high-level APIs to make training models using tabular data sets simpler. It features an interface similar to [scikit-learn](https://scikit-learn.org/stable/) and functions as an end-to-end end pipeline for tabular data sets. It automatically detects feature column types and redundant feature columns, imputes missing values, encodes string columns and categorical columns, and preprocesses numerical columns, among other", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "features.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**3rd Place** - [TorchExpo](https://devpost.com/software/torchexpo)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "TorchExpo is a collection of models and extensions that simplifies taking PyTorch from research to production in mobile devices. This library is more than a web and mobile application, and also comes with a Python library. The Python library is available via pip install and it helps researchers convert a state-of-the-art model in TorchScript and ONNX format in just one line. Detailed docs are available [here](https://torchexpo.readthedocs.io/en/latest/).", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Web/Mobile Applications Powered by PyTorch\n\n**1st place** - [Q&Aid](https://devpost.com/software/pytorchxai)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Q&Aid is a conceptual health-care chatbot aimed at making health-care diagnoses and facilitating communication between patients and doctors. It relies on a series of machine learning models to filter, label, and answer medical questions, based on a medical image and/or questions in text provided by a patient. The transcripts from the chat app then can be forwarded to the local hospitals and the patient will be contacted by one of them to make an appointment to determine proper diagnosis and care. The team", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "hopes that this concept application helps hospitals to work with patients more efficiently and provide proper care.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n**2nd place** - [Rasoee](https://devpost.com/software/groundwav)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Rasoee is an application that can take images as input and output the name of the dish. It also lists the ingredients and recipe, along with the link to the original recipe online. Additionally, users can choose a cuisine from the list of cuisines in the drop menu, and describe the taste and/or method of preparation in text. Then the application will return matching dishes from the [list of 308 identifiable dishes](https://github.com/arijitgupta42/Rasoee/blob/master/Dishes.txt). The team has put a", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "significant amount of effort gathering and cleaning various datasets to build more accurate and comprehensive models. You can check out the application [here](https://rasoee.herokuapp.com).", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**3rd place** - [Rexana the Robot \u2014 PyTorch](https://devpost.com/software/rexana-the-robot)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Rexana is an AI voice assistant meant to lay the foundation for a physical robot that can complete basic tasks around the house. The system is capable of autonomous navigation (knowing its position around the house relative to landmarks), recognizing voice commands, and object detection and recognition \u2014 meaning it can be commanded to perform various household tasks (e.g., \"Rexana, water the potted plant in the lounge room.\u201d). Rexana can be controlled remotely via a mobile device, and the robot itself", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "features customizable hands (magnets, grippers, etc.) for taking on different jobs.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Responsible AI Development Tools\n\n**1st place**: [FairTorch](https://devpost.com/software/a-qeysp1)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "FairTorch is a fairness library for PyTorch. It lets developers add constraints to their models to equalize metrics across subgroups by simply adding a few lines of code. Model builders can choose a metric definition of fairness for their context, and enforce it at time of training. The library offers a suite of metrics that measure an AI system\u2019s performance among subgroups, and can apply to high-stakes examples where decision-making algorithms are deployed, such as hiring, school admissions, and banking.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "
\n \n \"FairTorch\"\n \n
\n\n**2nd place**: [Fluence](https://devpost.com/software/fluence-5g2s9m)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "In order to facilitate direct comparisons between results published in the literature and further reduce the boilerplate code needed to run experiments with datasets in TorchGeo, we have created PyTorch Lightning [*datamodules*](https://torchgeo.readthedocs.io/en/latest/api/datamodules.html) with well-defined train-val-test splits and [*trainers*](https://torchgeo.readthedocs.io/en/latest/api/trainers.html) for various tasks like classification, regression, and semantic segmentation. These datamodules show how to incorporate augmentations from the kornia library, include preprocessing transforms (with pre-calculated channel statistics), and let users easily experiment with hyperparameters related to the data itself (as opposed to the modeling process). Training a semantic segmentation model on the Inria Aerial Image Labeling dataset is as easy as a few imports and four lines of code.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "```c++\nfrom pytorch_lightning import Trainer\nfrom torchgeo.datamodules import InriaAerialImageLabelingDataModule\nfrom torchgeo.trainers import SemanticSegmentationTask\n\ndatamodule = InriaAerialImageLabelingDataModule(root_dir=\"...\", batch_size=64, num_workers=6)\ntask = SemanticSegmentationTask(segmentation_model=\"unet\", encoder_weights=\"imagenet\", learning_rate=0.1)\ntrainer = Trainer(gpus=1, default_root_dir=\"...\")\n\ntrainer.fit(model=task, datamodule=datamodule)\n```\n\n

\n \n

\n\n

\nBuilding segmentations produced by a U-Net model trained on the Inria Aerial Image Labeling dataset. Reproducing these results is as simple as a few imports and four lines of code, making comparison of different models and training techniques simple and easy.\n

", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "In our [preprint](https://arxiv.org/abs/2111.08872) we show a set of results that use the aforementioned datamodules and trainers to benchmark simple modeling approaches for several of the datasets in TorchGeo. For example, we find that a simple ResNet-50 can achieve state-of-the-art performance on the [So2Sat](https://ieeexplore.ieee.org/document/9014553) dataset. These types of baseline results are important for evaluating the contribution of different modeling choices when tackling problems with remotely sensed data.\n\n# Future work and contributing\n\nThere is still a lot of remaining work to be done in order to make TorchGeo as easy to use as possible, especially for users without prior deep learning experience. One of the ways in which we plan to achieve this is by expanding our tutorials to include subjects like \"writing a custom dataset\" and \"transfer learning\", or tasks like \"land cover mapping\" and \"object detection\".", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Another important project we are working on is pre-training models. Most remote sensing researchers work with very small labeled datasets, and could benefit from pre-trained models and transfer learning approaches. TorchGeo is the first deep learning library to provide models pre-trained on multispectral imagery. Our goal is to provide models for different image modalities (optical, SAR, multispectral) and specific platforms (Landsat, Sentinel, MODIS) as well as benchmark results showing their performance with different amounts of training data. Self-supervised learning is a promising method for training such models. Satellite imagery datasets often contain petabytes of imagery, but accurately labeled datasets are much harder to come by. Self-supervised learning methods will allow us to train directly on the raw imagery without needing large labeled datasets.", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "Aside from these larger projects, we're always looking to add new datasets, data augmentation transforms, and sampling strategies. If you're Python savvy and interested in contributing to TorchGeo, we would love to see contributions! TorchGeo is open source under an MIT license, so you can use it in almost any project.\n\nExternal links:\n\n- **Homepage**: [https://github.com/microsoft/torchgeo](https://github.com/microsoft/torchgeo)\n- **Documentation**: [https://torchgeo.readthedocs.io/](https://torchgeo.readthedocs.io/)\n- **PyPI**: [https://pypi.org/project/torchgeo/](https://pypi.org/project/torchgeo/)\n- **Paper**: [https://arxiv.org/abs/2111.08872](https://arxiv.org/abs/2111.08872)\n\nIf you like TorchGeo, give us a star on GitHub! And if you use TorchGeo in your work, please cite our paper.\n\n# Acknowledgments", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "# Acknowledgments\n\n*We would like to thank all TorchGeo contributors for their efforts in creating the library, the Microsoft AI for Good program for support, and the PyTorch Team for their guidance. This research is part of the Blue Waters sustained-petascale computing project, which is supported by the National Science Foundation (awards OCI-0725070 and ACI-1238993), the State of Illinois, and as of December, 2019, the National Geospatial-Intelligence Agency. Blue Waters is a joint effort of the University of Illinois at Urbana-Champaign and its National Center for Supercomputing Applications. The research was supported in part by NSF grants IIS-1908104, OAC-1934634, and DBI-2021898.*", "metadata": {"source": "https://pytorch.org/blog/geospatial-deep-learning-with-torchgeo/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'What\u2019s New in PyTorch Profiler 1.9?'\nauthor: Sabrina Smai, Program Manager on the AI Framework team at Microsoft\n---\n\nPyTorch Profiler v1.9 has been released! The goal of this new release (previous [PyTorch Profiler release](https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/)) is to provide you with new state-of-the-art tools to help diagnose and fix machine learning performance issues regardless of whether you are working on one or numerous machines. The objective is to target the execution steps that are the most costly in time and/or memory, and visualize the work load distribution between GPUs and CPUs. \n\nHere is a summary of the five major features being released:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "1.\t**Distributed Training View**: This helps you understand how much time and memory is consumed in your distributed training job. Many issues occur when you take a training model and split the load into worker nodes to be run in parallel as it can be a black box. The overall model goal is to speed up model training. This distributed training view will help you diagnose and debug issues within individual nodes. \n2.\t**Memory View**: This view allows you to understand your memory usage better. This tool will help you avoid the famously pesky Out of Memory error by showing active memory allocations at various points of your program run. \n3.\t**GPU Utilization Visualization**: This tool helps you make sure that your GPU is being fully utilized. \n4.\t**Cloud Storage Support**: Tensorboard plugin can now read profiling data from Azure Blob Storage, Amazon S3, and Google Cloud Platform. \n5.\t**Jump to Source Code**: This feature allows you to visualize stack tracing information and jump directly into the source code. This helps you quickly optimize and iterate on your code based on your profiling results.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "## Getting Started with PyTorch Profiling Tool\nPyTorch includes a profiling functionality called \u00ab PyTorch Profiler \u00bb. The PyTorch Profiler tutorial can be found [here](https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html).\n\nTo instrument your PyTorch code for profiling, you must:\n\n$ pip install torch-tb-profiler\n\n```python\nimport torch.profiler as profiler\nWith profiler.profile(XXXX)\n```\n\n**Comments**:\n\n\u2022 For CUDA and CPU profiling, see [below](https://github.com/pytorch/kineto/blob/master/tb_plugin/examples/resnet50_profiler_api.py): \n```\nwith torch.profiler.profile( \nactivities=[ \ntorch.profiler.ProfilerActivity.CPU, \ntorch.profiler.ProfilerActivity.CUDA], \n```\n\n\u2022\tWith profiler.record_function(\u201c$NAME\u201d): allows putting a decorator (a tag associated to a name) for a block of function\n\n\u2022\tProfile_memory=True parameter under profiler.profile allows you to profile CPU and GPU memory footprint\n\n## Visualizing PyTorch Model Performance using PyTorch Profiler\n\n### Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Recent advances in deep learning argue for the value of large datasets and large models, which requires you to scale out model training to more computational resources. Distributed Data Parallel (DDP) and NVIDIA Collective Communications Library (NCCL) are the widely adopted paradigms in PyTorch for accelerating your deep learning training. \n\nIn this release of PyTorch Profiler, DDP with NCCL backend is now supported.\n\n
\n \n
\n\n### Computation/Communication Overview\n\nIn the Computation/Communication overview under the Distributed training view, you can observe the computation-to-communication ratio of each worker and [load balancer](https://en.wikipedia.org/wiki/Load_balancing_(computing) nodes between worker as measured by granularity. \n\n**Scenario 1**:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "**Scenario 1**:\n\nIf the computation and overlapping time of one worker is much larger than the others, this may suggest an issue in the workload balance or worker being a straggler. Computation is the sum of kernel time on GPU minus the overlapping time. The overlapping time is the time saved by interleaving communications during computation. The more overlapping time represents better parallelism between computation and communication. Ideally the computation and communication completely overlap with each other. Communication is the total communication time minus the overlapping time. The example image below displays how this scenario appears on Tensorboard. \n\n
\n \n

Figure: A straggler example

\n
\n\n**Scenario 2**:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "**Scenario 2**:\n\nIf there is a small batch size (i.e. less computation on each worker) or the data to be transferred is large, the computation-to-communication may also be small and be seen in the profiler with low GPU utilization and long waiting times. This computation/communication view will allow you to diagnose your code to reduce communication by adopting gradient accumulation, or to decrease the communication proportion by increasing batch size. DDP communication time depends on model size. Batch size has no relationship with model size. So increasing batch size could make computation time longer and make computation-to-communication ratio bigger. \n\n### Synchronizing/Communication Overview", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "In the Synchronizing/Communication view, you can observe the efficiency of communication. This is done by taking the step time minus computation and communication time. Synchronizing time is part of the total communication time for waiting and synchronizing with other workers. The Synchronizing/Communication view includes initialization, data loader, CPU computation, and so on Insights like what is the ratio of total communication is really used for exchanging data and what is the idle time of waiting for data from other workers can be drawn from this view. \n\n
\n \n
\n\nFor example, if there is an inefficient workload balance or straggler issue, you\u2019ll be able to identify it in this Synchronizing/Communication view. This view will show several workers\u2019 waiting time being longer than others.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\nThis table view above allows you to see the detailed statistics of all communication ops in each node. This allows you to see what operation types are being called, how many times each op is called, what is the size of the data being transferred by each op, etc. \n\n### Memory View:\n\nThis memory view tool helps you understand the hardware resource consumption of the operators in your model. Understanding the time and memory consumption on the operator-level allows you to resolve performance bottlenecks and in turn, allow your model to execute faster. Given limited GPU memory size, optimizing the memory usage can: \n\n1. Allow bigger model which can potentially generalize better on end level tasks.\n2. Allow bigger batch size. Bigger batch sizes increase the training speed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "The profiler records all the memory allocation during the profiler interval. Selecting the \u201cDevice\u201d will allow you to see each operator\u2019s memory usage on the GPU side or host side. You must enable ```profile_memory=True``` to generate the below memory data as shown [here](https://github.com/pytorch/kineto/blob/master/tb_plugin/examples/resnet50_profiler_api.py#L39). \n\n```\nWith torch.profiler.profile(\nProfiler_memory=True # this will take 1 \u2013 2 minutes to complete. \n)\n```\n\n**Important Definitions**:\n\n\u2022\t\u201cSize Increase\u201d displays the sum of all allocation bytes and minus all the memory release bytes.\n\n\u2022\t\u201cAllocation Size\u201d shows the sum of all allocation bytes without considering the memory release.\n\n\u2022\t\u201cSelf\u201d means the allocated memory is not from any child operators, instead by the operator itself.\n\n
\n \n
\n\n\n### GPU Metric on Timeline:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "This feature will help you debug performance issues when one or more GPU are underutilized. Ideally, your program should have high GPU utilization (aiming for 100% GPU utilization), minimal CPU to GPU communication, and no overhead. \n\n**Overview**:\nThe overview page highlights the results of three important GPU usage metrics at different levels (i.e. GPU Utilization, Est. SM Efficiency, and Est. Achieved Occupancy). Essentially, each GPU has a bunch of SM each with a bunch of warps that can execute a bunch of threads concurrently. Warps execute a bunch because the amount depends on the GPU. But at a high level, this GPU Metric on Timeline tool allows you can see the whole stack, which is useful. \n\nIf the GPU utilization result is low, this suggests a potential bottleneck is present in your model. Common reasons: \n\n\u2022Insufficient parallelism in kernels (i.e., low batch size) \n\n\u2022Small kernels called in a loop. This is to say the launch overheads are not amortized", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "\u2022CPU or I/O bottlenecks lead to the GPU not receiving enough work to keep busy \n\nLooking of the overview page where the performance recommendation section is where you\u2019ll find potential suggestions on how to increase that GPU utilization. In this example, GPU utilization is low so the performance recommendation was to increase batch size. Increasing batch size 4 to 32, as per the performance recommendation, increased the GPU Utilization by 60.68%.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "GPU Utilization: the step interval time in the profiler when a GPU engine was executing a workload. The high the utilization %, the better. The drawback of using GPU utilization solely to diagnose performance bottlenecks is it is too high-level and coarse. It won\u2019t be able to tell you how many Streaming Multiprocessors are in use. Note that while this metric is useful for detecting periods of idleness, a high value does not indicate efficient use of the GPU, only that it is doing anything at all. For instance, a kernel with a single thread running continuously will get a GPU Utilization of 100%", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Estimated Stream Multiprocessor Efficiency (Est. SM Efficiency) is a finer grained metric, it indicates what percentage of SMs are in use at any point in the trace This metric reports the percentage of time where there is at least one active warp on a SM and those that are stalled (NVIDIA [doc](https://forums.developer.nvidia.com/t/nvprof-question-about-the-sm-efficiency-metric/72640#:~:text=My%20understanding%20from%20the%20profiler%20documentation%20is%20that,that%20%E2%80%9Cactive%20warps%E2%80%9D%20include%20warps%20that%20are%20stalled.)). Est. SM Efficiency also has it\u2019s limitation. For instance, a kernel with only one thread per block can\u2019t fully use each SM. SM Efficiency does not tell us how busy each SM is, only that they are doing anything at all, which can include stalling while waiting on the result of a memory load. To keep an SM busy, it is necessary to have a sufficient number of ready warps that can be run whenever a stall occurs", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Estimated Achieved Occupancy (Est. Achieved Occupancy) is a layer deeper than Est. SM Efficiency and GPU Utilization for diagnosing performance issues. Estimated Achieved Occupancy indicates how many warps can be active at once per SMs. Having a sufficient number of active warps is usually key to achieving good throughput. Unlike GPU Utilization and SM Efficiency, it is not a goal to make this value as high as possible. As a rule of thumb, good throughput gains can be had by improving this metric to 15% and above. But at some point you will hit diminishing returns. If the value is already at 30% for example, further gains will be uncertain. This metric reports the average values of all warp schedulers for the kernel execution period (NVIDIA [doc](https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/cudaexperiments/kernellevel/achievedoccupancy.htm)). The larger the Est. Achieve Occupancy value is the better.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "
\n \n

Overview details: Resnet50_batchsize4

\n
\n\n
\n \n

Overview details: Resnet50_batchsize32

\n
\n\n_Kernel View_\nThe kernel has \u201cBlocks per SM\u201d and \u201cEst. Achieved Occupancy\u201d which is a great tool to compare model runs. \n\n
\n \n
\n\nMean Blocks per SM: \nBlocks per SM = Blocks of this kernel / SM number of this GPU. If this number is less than 1, it indicates the GPU multiprocessors are not fully utilized. \u201cMean Blocks per SM\u201d is weighted average of all runs of this kernel name, using each run\u2019s duration as weight.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Mean Est. Achieved Occupancy: \nEst. Achieved Occupancy is defined as above in overview. \u201cMean Est. Achieved Occupancy\u201d is weighted average of all runs of this kernel name, using each run\u2019s duration as weight. \n\n_Trace View_\nThis trace view displays a timeline that shows the duration of operators in your model and which system executed the operation. This view can help you identify whether the high consumption and long execution is because of input or model training. Currently, this trace view shows GPU Utilization and Est. SM Efficiency on a timeline. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "GPU utilization is calculated independently and divided into multiple 10 millisecond buckets. The buckets\u2019 GPU utilization values are drawn alongside the timeline between 0 \u2013 100%. In the above example, the \u201cProfilerStep5\u201d GPU utilization during thread 28022\u2019s busy time is higher than the following the one during \u201cOptimizer.step\u201d. This is where you can zoom-in to investigate why that is. \n\n
\n \n
\n\nFrom above, we can see the former\u2019s kernels are longer than the later\u2019s kernels. The later\u2019s kernels are too short in execution, which results in lower GPU utilization. \n\nEst. SM Efficiency: Each kernel has a calculated est. SM efficiency between 0 \u2013 100%. For example, the below kernel has only 64 blocks, while the SMs in this GPU is 80. Then its \u201cEst. SM Efficiency\u201d is 64/80, which is 0.8.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n### Cloud Storage Support\n\nAfter running pip install tensorboard, to have data be read through these cloud providers, you can now run: \n\n``` sh \ntorch-tb-profiler[blob] \ntorch-tb-profiler[gs] \ntorch-tb-profiler[s3] \n``` \n```pip install torch-tb-profiler[blob]```, ```pip install torch-tb-profiler[gs]```, or ```pip install torch-tb-profiler[S3]``` to have data be read through these cloud providers. For more information, please refer to this [README](https://github.com/pytorch/kineto/tree/main/tb_plugin). \n\n### Jump to Source Code:", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "One of the great benefits of having both TensorBoard and the PyTorch Profiler being integrated directly in Visual Studio Code (VS Code) is the ability to directly jump to the source code (file and line) from the profiler stack traces. VS Code Python Extension now [supports TensorBoard Integration](https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2021-release/).\n\nJump to source is ONLY available when Tensorboard is launched within VS Code. Stack tracing will appear on the plugin UI if the profiling with_stack=True. When you click on a stack trace from the PyTorch Profiler, VS Code will automatically open the corresponding file side by side and jump directly to the line of code of interest for you to debug. This allows you to quickly make actionable optimizations and changes to your code based on the profiling results and suggestions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "
\n \n

Gify: Jump to Source using Visual Studio Code Plug In UI

\n
\n\nFor how to optimize batch size performance, check out the step-by-step tutorial [here](https://opendatascience.com/optimizing-pytorch-performance-batch-size-with-pytorch-profiler/). PyTorch Profiler is also integrated with [PyTorch Lightning](https://pytorch-lightning.readthedocs.io/en/stable/advanced/profiler.html#pytorch-profiling) and you can simply launch your lightning training jobs with --```trainer.profiler=pytorch``` flag to generate the traces. Check out an example [here](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pl_examples/basic_examples/profiler_example.py). \n\n## What\u2019s Next for the PyTorch Profiler?\nYou just saw how PyTorch Profiler can help optimize a model. You can now try the Profiler by ```pip install torch-tb-profiler``` to optimize your PyTorch model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "Look out for an advanced version of this tutorial in the future. We are also thrilled to continue to bring state-of-the-art tool to PyTorch users to improve ML performance. We'd love to hear from you. Feel free to open an issue [here](https://github.com/pytorch/kineto/issues). \n\nFor new and exciting features coming up with PyTorch Profiler, follow @PyTorch on Twitter and check us out on pytorch.org. \n\n## Acknowledgements\n\nThe author would like to thank the contributions of the following individuals to this piece. From the Facebook side: Geeta Chauhan, Gisle Dankel, Woo Kim, Sam Farahzad, and Mark Saroufim. On the Microsoft side: AI Framework engineers (Teng Gao, Mike Guo, and Yang Gu), Guoliang Hua, and Thuy Nguyen.", "metadata": {"source": "https://pytorch.org/blog/pytorch-profiler-1.9-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the Winners of the 2020 Global PyTorch Summer Hackathon'\nauthor: Team PyTorch\n---\n\nMore than 2,500 participants in this year\u2019s Global PyTorch Summer Hackathon pushed the envelope to create unique new tools and applications for PyTorch developers and researchers.\n\n
\n \n
\n\n***Notice**: None of the projects submitted to the hackathon are associated with or offered by Facebook, Inc.* \n\nThis year\u2019s projects fell into three categories:\n\n* **PyTorch Developer Tools:** a tool or library for improving productivity and efficiency for PyTorch researchers and developers.\n\n* **Web/Mobile Applications Powered by PyTorch:** a web or mobile interface and/or an embedded device built using PyTorch.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "* **PyTorch Responsible AI Development Tools:** a tool, library, or web/mobile app to support researchers and developers in creating responsible AI that factors in fairness, security, privacy, and more throughout its entire development process.\n\nThe virtual hackathon ran from June 22 to August 25, with more than 2,500 registered participants, representing 114 countries from Republic of Azerbaijan, to Zimbabwe, to Japan, submitting a total of 106 projects. Entrants were judged on their idea\u2019s quality, originality, potential impact, and how well they implemented it.\n\nMeet the winners of each category below. \n\n## PyTorch Developer Tools\n\n**1st place** - [DeMask](https://devpost.com/software/asteroid-the-pytorch-based-source-separation-toolkit)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "DeMask is an end-to-end model for enhancing speech while wearing face masks \u2014 offering a clear benefit during times when face masks are mandatory in many spaces and for workers who wear face masks on the job. Built with [Asteroid](https://github.com/mpariente/asteroid), a PyTorch-based audio source separation toolkit, DeMask is trained to recognize distortions in speech created by the muffling from face masks and to adjust the speech to make it sound clearer. \n\nThis submission stood out in particular because it represents both a high-quality idea and an implementation that can be reproduced by other researchers.\n\nHere is an example on how to train a speech separation model in less than 20 lines:\n\n```python\nfrom torch import optim\nfrom pytorch_lightning import Trainer\n\nfrom asteroid import ConvTasNet\nfrom asteroid.losses import PITLossWrapper\nfrom asteroid.data import LibriMix\nfrom asteroid.engine import System", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "train_loader, val_loader = LibriMix.loaders_from_mini(task='sep_clean', batch_size=4)\nmodel = ConvTasNet(n_src=2)\noptimizer = optim.Adam(model.parameters(), lr=1e-3)\nloss = PITLossWrapper(\n lambda x, y: (x - y).pow(2).mean(-1), # MSE\n pit_from=\"pw_pt\", # Point in the pairwise matrix.\n)\n\nsystem = System(model, optimizer, loss, train_loader, val_loader)\n\ntrainer = Trainer(fast_dev_run=True)\ntrainer.fit(system)\n```\n\n**2nd place** - [carefree-learn](https://devpost.com/software/carefree-learn)\n\nA PyTorch-based automated machine learning (AutoML) solution, carefree-learn provides high-level APIs to make training models using tabular data sets simpler. It features an interface similar to [scikit-learn](https://scikit-learn.org/stable/) and functions as an end-to-end end pipeline for tabular data sets. It automatically detects feature column types and redundant feature columns, imputes missing values, encodes string columns and categorical columns, and preprocesses numerical columns, among other features.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "**3rd Place** - [TorchExpo](https://devpost.com/software/torchexpo)\n\nTorchExpo is a collection of models and extensions that simplifies taking PyTorch from research to production in mobile devices. This library is more than a web and mobile application, and also comes with a Python library. The Python library is available via pip install and it helps researchers convert a state-of-the-art model in TorchScript and ONNX format in just one line. Detailed docs are available [here](https://torchexpo.readthedocs.io/en/latest/).\n\n## Web/Mobile Applications Powered by PyTorch\n\n**1st place** - [Q&Aid](https://devpost.com/software/pytorchxai)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "Q&Aid is a conceptual health-care chatbot aimed at making health-care diagnoses and facilitating communication between patients and doctors. It relies on a series of machine learning models to filter, label, and answer medical questions, based on a medical image and/or questions in text provided by a patient. The transcripts from the chat app then can be forwarded to the local hospitals and the patient will be contacted by one of them to make an appointment to determine proper diagnosis and care. The team hopes that this concept application helps hospitals to work with patients more efficiently and provide proper care. \n\n
\n \n
\n\n**2nd place** - [Rasoee](https://devpost.com/software/groundwav)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "Rasoee is an application that can take images as input and output the name of the dish. It also lists the ingredients and recipe, along with the link to the original recipe online. Additionally, users can choose a cuisine from the list of cuisines in the drop menu, and describe the taste and/or method of preparation in text. Then the application will return matching dishes from the [list of 308 identifiable dishes](https://github.com/arijitgupta42/Rasoee/blob/master/Dishes.txt). The team has put a significant amount of effort gathering and cleaning various datasets to build more accurate and comprehensive models. You can check out the application [here](https://rasoee.herokuapp.com).\n\n**3rd place** - [Rexana the Robot \u2014 PyTorch](https://devpost.com/software/rexana-the-robot)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "Rexana is an AI voice assistant meant to lay the foundation for a physical robot that can complete basic tasks around the house. The system is capable of autonomous navigation (knowing its position around the house relative to landmarks), recognizing voice commands, and object detection and recognition \u2014 meaning it can be commanded to perform various household tasks (e.g., \"Rexana, water the potted plant in the lounge room.\u201d). Rexana can be controlled remotely via a mobile device, and the robot itself features customizable hands (magnets, grippers, etc.) for taking on different jobs.\n\n## PyTorch Responsible AI Development Tools\n\n**1st place**: [FairTorch](https://devpost.com/software/a-qeysp1)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "FairTorch is a fairness library for PyTorch. It lets developers add constraints to their models to equalize metrics across subgroups by simply adding a few lines of code. Model builders can choose a metric definition of fairness for their context, and enforce it at time of training. The library offers a suite of metrics that measure an AI system\u2019s performance among subgroups, and can apply to high-stakes examples where decision-making algorithms are deployed, such as hiring, school admissions, and banking.\n\n
\n \n \"FairTorch\"\n \n
\n\n**2nd place**: [Fluence](https://devpost.com/software/fluence-5g2s9m)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} {"page_content": "Fluence is a PyTorch-based deep learning library for language research. It specifically addresses the large compute demands of natural language processing (NLP) research. Fluence aims to provide low-resource and computationally efficient algorithms for NLP, giving researchers algorithms that can enhance current NLP methods or help discover where current methods fall short.\n\n**3rd place**: [Causing: CAUSal INterpretation using Graphs](https://devpost.com/software/realrate-explainable-ai-for-company-ratings)", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Causing (CAUSal INterpretation using Graphs) is a multivariate graphic analysis tool for bringing transparency to neural networks. It explains causality and helps researchers and developers interpret the causal effects of a given equation system to ensure fairness. Developers can input data and a model describing the dependencies between the variables within the data set into Causing, and Causing will output a colored graph of quantified effects acting between the model\u2019s variables. In addition, it also", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "allows developers to estimate these effects to validate whether data fits a model.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "Thank you,\n\n**The PyTorch team**", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch\u2019s Tracing Based Selective Build\"\nauthor: Dhruv Matani, Suraj Subramanian\nfeatured-img: \"/assets/images/pytorchs-tracing-based-selective-build_Figure_4.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\n**TL;DR**: It can be challenging to run PyTorch on mobile devices, SBCs (Single Board Computers), and IOT devices. When compiled, the PyTorch library is huge and includes dependencies that might not be needed for the on-device use case.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "To run a specific set of models on-device, we actually require only a small subset of the features in the PyTorch library. We found that using a PyTorch runtime generated using **selective build** can achieve up to 90% reduction in binary size (for the CPU and QuantizedCPU backends on an x86-64 build on Linux). In this blog, we share our experience of generating model-specific minimal runtimes using Selective Build and show you how to do the same.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Why is this important for app developers?\n\nUsing a PyTorch runtime generated by **selective build** can reduce the size of AI-powered apps by 30+ MB - a significant reduction for a typical mobile app! Making mobile applications more lightweight has many benefits - they are runnable on a wider variety of devices, consume less cellular data, and can be downloaded and updated faster on user\u2019s devices.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "What does the Developer Experience look like?\n\nThis method can work seamlessly with any existing PyTorch Mobile deployment workflows. All you need to do is replace the general PyTorch runtime library with a runtime customized for the specific models you wish to use in your application. The general steps in this process are:", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "1. Build the PyTorch Runtime in **instrumentation mode** (this is called an **instrumentation build** of PyTorch). This will record the used operators, kernels and features.\n2. Run your models through this instrumentation build by using the provided **model_tracer** binary. This will generate a single YAML file that stores all the features used by your model. These features will be preserved in the minimal runtime.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "3. Build PyTorch using this YAML file as input. This is the **selective build** technique, and it greatly reduces the size of the final PyTorch binary.\n4. Use this selectively-built PyTorch library to reduce the size of your mobile application!", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Building the PyTorch Runtime in a special **\u201cinstrumentation\u201d mode** ( by passing the `TRACING_BASED=1` build option) generates an **instrumentation build** runtime of PyTorch, along with a **model_tracer** binary. Running a model with this build allows us to trace the parts of PyTorch used by the model.\n\n

\n \n

\n\n

\n Figure 1: Instrumentation build of PyTorch\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Clone the PyTorch repo\ngit clone https://github.com/pytorch/pytorch.git\ncd pytorch\n\n# Build the model_tracer\nUSE_NUMPY=0 USE_DISTRIBUTED=0 USE_CUDA=0 TRACING_BASED=1 \\\n python setup.py develop", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Now this instrumentation build is used to run a model inference with representative inputs. The **model_tracer** binary observes parts of the instrumentation build that were activated during the inference run, and dumps it to a YAML file.\n\n

\n \n

\n\n

\n Figure 2: YAML file generated by running model(s) on an instrumentation build\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Generate YAML file\n./build/bin/model_tracer \\\n --model_input_path /tmp/path_to_model.ptl \\\n --build_yaml_path /tmp/selected_ops.yaml", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Now we build the PyTorch Runtime again, but this time using the YAML file generated by the tracer. The runtime now only includes those parts that are needed for this model. This is called **\u201cSelectively built PyTorch runtime\u201d** in the diagram below.\n\n```python\n# Clean out cached configuration\nmake clean", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "# Build PyTorch using Selected Operators (from the YAML file)\n# using the host toolchain, and use this generated library\nBUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN=1 \\\nUSE_LIGHTWEIGHT_DISPATCH=0 \\\nBUILD_LITE_INTERPRETER=1 \\\nSELECTED_OP_LIST=/tmp/selected_ops.yaml \\\nTRACING_BASED=1 \\\n ./scripts/build_mobile.sh", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Figure 3: Selective Build of PyTorch and model execution on a selectively built PyTorch runtime\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Show me the code!\n\nWe\u2019ve put together a [notebook](https://gist.github.com/dhruvbird/65fd800983f362a72d78afe68031568c) to illustrate what the process above looks like in code using a simple PyTorch model. \n\nFor a more hands-on tutorial to deploy this on Android/iOS [this tutorial](https://pytorch.org/tutorials/prototype/tracing_based_selective_build.html) should be helpful.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Technical FAQs", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Why is Tracing needed for a Selective Build of PyTorch?", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch, CPU kernels can call other operators via the [PyTorch Dispatcher](http://blog.ezyang.com/2020/09/lets-talk-about-the-pytorch-dispatcher/). Simply including the set of root operators called directly by the model is not sufficient as there might be many more being called under-the-hood transitively. Running the model on representative inputs and observing the actual list of operators called (aka \u201ctracing\u201d) is the most accurate way of determining what parts of PyTorch are used.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Additionally, factors such as which dtypes a kernel should handle are also runtime features that depend on actual input provided to the model. Hence, the tracing mechanism is extremely suitable for this purpose.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Which features can be selected (in or out) by using Tracing Based Selective Build?\n\nThe following features can be selected for the PyTorch runtime during the tracing based selective build process:", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "1. [CPU/QuantizedCPU](https://codebrowser.bddppq.com/pytorch/pytorch/build/aten/src/ATen/) kernels for [PyTorch\u2019s ATen Operators](https://pytorch.org/cppdocs/): If a PyTorch Operator is not needed by a model targeted at a selectively built runtime, then the registration of that CPU kernel is omitted in the runtime. This is controlled via [Torchgen code-gen](https://github.com/pytorch/pytorch/blob/master/torchgen/gen.py).", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "2. [Primary Operators](https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/runtime/register_prim_ops.cpp): This is controlled by a macro named [TORCH_SELECTIVE_SCHEMA](https://codebrowser.bddppq.com/pytorch/pytorch/torch/library.h.html) (via templated selective build) that either selects a primary operator or de-selects it based on information in a generated header file.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "3. Code that handles [specific dtypes](https://codebrowser.bddppq.com/pytorch/pytorch/aten/src/ATen/Dispatch.h.html) in CPU kernels: This is performed by generating exception throws in specific case statements in the switch case generated by the macro [AT_PRIVATE_CHECK_SELECTIVE_BUILD](https://codebrowser.bddppq.com/pytorch/pytorch/aten/src/ATen/Dispatch.h.html#_M/AT_PRIVATE_CHECK_SELECTIVE_BUILD).", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "4. Registration of [Custom C++ Classes](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html) that extend PyTorch: This is controlled by the macro [TORCH_SELECTIVE_CLASS](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/fbgemm_utils.cpp#L385-L386), which can be used when registering Custom C++ Classes. The [torch::selective_class_<>](https://github.com/pytorch/pytorch/blob/master/torch/custom_class.h#L443-L460) helper is to be used in conjunction with the", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "macro [TORCH_SELECTIVE_CLASS](https://codebrowser.bddppq.com/pytorch/pytorch/torch/library.h.html#_M/TORCH_SELECTIVE_CLASS).", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "What is the structure of the YAML file used during the build?\n\nThe YAML file generated after tracing looks like the example below. It encodes all the elements of the \u201cselectable\u201d build feature as specified above.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "```python\ninclude_all_non_op_selectives: false\nbuild_features: []\noperators:\n aten::add.Tensor:\n is_used_for_training: false\n is_root_operator: true\n include_all_overloads: false\n aten::len.t:\n is_used_for_training: false\n is_root_operator: true\n include_all_overloads: false\nkernel_metadata:\n _local_scalar_dense_cpu:\n - Float\n add_stub:\n - Float\n copy_:\n - Bool\n - Byte\n mul_cpu:\n - Float\ncustom_classes: []\n```", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "How exactly is code eliminated from the generated binary?\n\nDepending on the specific scenario, there are 2 main techniques that are used to hint the compiler and linker about unused and unreachable code. This code is then cleaned up by the compiler or linker as unreachable code.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "[1] Unreferenced functions removed by the Linker\n\nWhen a function that isn\u2019t transitively referenced from any visible function is present in the compiled object files that are being linked together, the linker will remove it (if the right build flags are provided). This is leveraged in 2 scenarios by the selective build system.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Kernel Registration in the Dispatcher\n\nIf an operator\u2019s kernel isn\u2019t needed, then it isn\u2019t registered with the dispatcher. An unregistered kernel means that the function is unreachable, and it will be removed by the linker.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Templated Selective Build\n\nThe general idea here is that a class template specialization is used to select a class that either captures a reference to a function or not (depending on whether it\u2019s used) and the linker can come along and clean out the unreferenced function.\n\nFor example, in the code below, there\u2019s no reference to the function \u201c`fn2`\u201d, so it will be cleaned up by the linker since it\u2019s not referenced anywhere.\n\n```python\n#include \n#include ", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "template \nstruct FunctionSelector {\n T fn_;\n FunctionSelector(T fn): fn_(fn) {}\n T get() { return this->fn_; }\n};\n\n// The \"false\" specialization of this class does NOT retain the argument passed\n// to the class constructor, which means that the function pointer passed in\n// is considered to be unreferenced in the program (unless it is referenced\n// elsewhere).\ntemplate \nstruct FunctionSelector {\n FunctionSelector(T) {}\n};", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "template \nFunctionSelector make_function_selector_true(T fn) {\n return FunctionSelector(fn);\n}\n\ntemplate \nFunctionSelector make_function_selector_false(T fn) {\n return FunctionSelector(fn);\n}\n\ntypedef void(*fn_ptr_type)();\n\nstd::vector fns;\n\ntemplate \nvoid add_fn(FunctionSelector fs) {\n fns.push_back(fs.get());\n}\n\ntemplate \nvoid add_fn(FunctionSelector) {\n // Do nothing.\n}", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "// fn1 will be kept by the linker since it is added to the vector \"fns\" at\n// runtime.\nvoid fn1() {\n printf(\"fn1\\n\");\n}\n\n// fn2 will be removed by the linker since it isn't referenced at all.\nvoid fn2() {\n printf(\"fn2\\n\");\n}\n\nint main() {\n add_fn(make_function_selector_true(fn1));\n add_fn(make_function_selector_false(fn2));\n}\n```", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "[2] Dead Code Eliminated by the Compiler\n\nC++ Compilers can detect dead ([unreachable](https://en.wikipedia.org/wiki/Unreachable_code)) code by analyzing the code\u2019s control flow statically. For example, if there\u2019s a code-path that comes after an **unconditional exception throw**, then all the code after it will be marked as dead code and not converted to object code by the compiler. Typically, compilers require the use of the `-fdce` flag to eliminate dead code.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "In the example below, you can see that the C++ code on the left (in the red boxes) doesn\u2019t have any corresponding generated object code on the right.\n\n

\n \n

\n\n

\n Figure 4: Dead Code Elimination by C++ Compilers\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "This property is leveraged in the bodies of PyTorch kernel implementations that have a lot of repeated code to handle multiple dtypes of a Tensor. A [dtype](https://pytorch.org/docs/stable/tensor_attributes.html) is the underlying data-type that the Tensor stores elements of. This can be one of float, double, int64, bool, int8, etc\u2026", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Almost every PyTorch CPU kernel uses a macro of the form AT_DISPATCH_ALL_TYPES* that is used to substitute some code specialized for every dtype that the kernel needs to handle. For example:\n\n```python\nAT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(\n kBool, kHalf, kBFloat16, dtype, \"copy_kernel\", [&] {\n cpu_kernel_vec(\n iter,\n [=](scalar_t a) -> scalar_t { return a; },\n [=](Vectorized a) -> Vectorized { return a; });\n});", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "The macro `AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3` internally has a switch-case statement that looks like the code in Figure-4 above. The tracing process records the dtypes triggered for the kernel tag \"`copy_kernel`\" and the build process processes these tags and inserts `throw` statements in every `case` statement that is handling the dtype that isn\u2019t required for this kernel tag.\n\nThis is how dtype selectivity is implemented in PyTorch\u2019s Tracing Based Selective Build.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "Conclusion\n\nTracing Based Selective Build is a practical and scalable approach to selecting only the used parts of an application to retain code that static analysis can not detect. This code is usually extremely data/input dependent in nature.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "Causing (CAUSal INterpretation using Graphs) is a multivariate graphic analysis tool for bringing transparency to neural networks. It explains causality and helps researchers and developers interpret the causal effects of a given equation system to ensure fairness. Developers can input data and a model describing the dependencies between the variables within the data set into Causing, and Causing will output a colored graph of quantified effects acting between the model\u2019s variables. In addition, it also allows developers to estimate these effects to validate whether data fits a model.\n\nThank you,\n\n**The PyTorch team**", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2020-global-pytorch-summer-hackathon/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch\u2019s Tracing Based Selective Build\"\nauthor: Dhruv Matani, Suraj Subramanian\nfeatured-img: \"/assets/images/pytorchs-tracing-based-selective-build_Figure_4.png\"\n---\n\n## Introduction\n\n**TL;DR**: It can be challenging to run PyTorch on mobile devices, SBCs (Single Board Computers), and IOT devices. When compiled, the PyTorch library is huge and includes dependencies that might not be needed for the on-device use case. \n\nTo run a specific set of models on-device, we actually require only a small subset of the features in the PyTorch library. We found that using a PyTorch runtime generated using **selective build** can achieve up to 90% reduction in binary size (for the CPU and QuantizedCPU backends on an x86-64 build on Linux). In this blog, we share our experience of generating model-specific minimal runtimes using Selective Build and show you how to do the same.\n\n## Why is this important for app developers?", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "Using a PyTorch runtime generated by **selective build** can reduce the size of AI-powered apps by 30+ MB - a significant reduction for a typical mobile app! Making mobile applications more lightweight has many benefits - they are runnable on a wider variety of devices, consume less cellular data, and can be downloaded and updated faster on user\u2019s devices.\n\n## What does the Developer Experience look like?\n\nThis method can work seamlessly with any existing PyTorch Mobile deployment workflows. All you need to do is replace the general PyTorch runtime library with a runtime customized for the specific models you wish to use in your application. The general steps in this process are:", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "1. Build the PyTorch Runtime in **instrumentation mode** (this is called an **instrumentation build** of PyTorch). This will record the used operators, kernels and features.\n2. Run your models through this instrumentation build by using the provided **model_tracer** binary. This will generate a single YAML file that stores all the features used by your model. These features will be preserved in the minimal runtime.\n3. Build PyTorch using this YAML file as input. This is the **selective build** technique, and it greatly reduces the size of the final PyTorch binary.\n4. Use this selectively-built PyTorch library to reduce the size of your mobile application!\n\n\nBuilding the PyTorch Runtime in a special **\u201cinstrumentation\u201d mode** ( by passing the `TRACING_BASED=1` build option) generates an **instrumentation build** runtime of PyTorch, along with a **model_tracer** binary. Running a model with this build allows us to trace the parts of PyTorch used by the model.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\n Figure 1: Instrumentation build of PyTorch\n

\n\n```python\n# Clone the PyTorch repo\ngit clone https://github.com/pytorch/pytorch.git\ncd pytorch\n\n# Build the model_tracer\nUSE_NUMPY=0 USE_DISTRIBUTED=0 USE_CUDA=0 TRACING_BASED=1 \\\n python setup.py develop\n```\n\nNow this instrumentation build is used to run a model inference with representative inputs. The **model_tracer** binary observes parts of the instrumentation build that were activated during the inference run, and dumps it to a YAML file.\n\n

\n \n

\n\n

\n Figure 2: YAML file generated by running model(s) on an instrumentation build\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "```python\n# Generate YAML file\n./build/bin/model_tracer \\\n --model_input_path /tmp/path_to_model.ptl \\\n --build_yaml_path /tmp/selected_ops.yaml\n```\n\nNow we build the PyTorch Runtime again, but this time using the YAML file generated by the tracer. The runtime now only includes those parts that are needed for this model. This is called **\u201cSelectively built PyTorch runtime\u201d** in the diagram below.\n\n```python\n# Clean out cached configuration\nmake clean\n\n# Build PyTorch using Selected Operators (from the YAML file)\n# using the host toolchain, and use this generated library\nBUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN=1 \\\nUSE_LIGHTWEIGHT_DISPATCH=0 \\\nBUILD_LITE_INTERPRETER=1 \\\nSELECTED_OP_LIST=/tmp/selected_ops.yaml \\\nTRACING_BASED=1 \\\n ./scripts/build_mobile.sh\n```\n\n

\n \n

\n\n

\n Figure 3: Selective Build of PyTorch and model execution on a selectively built PyTorch runtime\n

", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "### Show me the code!\n\nWe\u2019ve put together a [notebook](https://gist.github.com/dhruvbird/65fd800983f362a72d78afe68031568c) to illustrate what the process above looks like in code using a simple PyTorch model. \n\nFor a more hands-on tutorial to deploy this on Android/iOS [this tutorial](https://pytorch.org/tutorials/prototype/tracing_based_selective_build.html) should be helpful.\n\n## Technical FAQs\n\n### Why is Tracing needed for a Selective Build of PyTorch?\n\nIn PyTorch, CPU kernels can call other operators via the [PyTorch Dispatcher](http://blog.ezyang.com/2020/09/lets-talk-about-the-pytorch-dispatcher/). Simply including the set of root operators called directly by the model is not sufficient as there might be many more being called under-the-hood transitively. Running the model on representative inputs and observing the actual list of operators called (aka \u201ctracing\u201d) is the most accurate way of determining what parts of PyTorch are used.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "Additionally, factors such as which dtypes a kernel should handle are also runtime features that depend on actual input provided to the model. Hence, the tracing mechanism is extremely suitable for this purpose.\n\n### Which features can be selected (in or out) by using Tracing Based Selective Build?\n\nThe following features can be selected for the PyTorch runtime during the tracing based selective build process:", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "1. [CPU/QuantizedCPU](https://codebrowser.bddppq.com/pytorch/pytorch/build/aten/src/ATen/) kernels for [PyTorch\u2019s ATen Operators](https://pytorch.org/cppdocs/): If a PyTorch Operator is not needed by a model targeted at a selectively built runtime, then the registration of that CPU kernel is omitted in the runtime. This is controlled via [Torchgen code-gen](https://github.com/pytorch/pytorch/blob/master/torchgen/gen.py).\n2. [Primary Operators](https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/runtime/register_prim_ops.cpp): This is controlled by a macro named [TORCH_SELECTIVE_SCHEMA](https://codebrowser.bddppq.com/pytorch/pytorch/torch/library.h.html) (via templated selective build) that either selects a primary operator or de-selects it based on information in a generated header file.\n3. Code that handles [specific dtypes](https://codebrowser.bddppq.com/pytorch/pytorch/aten/src/ATen/Dispatch.h.html) in CPU kernels: This is performed by generating exception throws in specific case statements in the switch case generated by the macro [AT_PRIVATE_CHECK_SELECTIVE_BUILD](https://codebrowser.bddppq.com/pytorch/pytorch/aten/src/ATen/Dispatch.h.html#_M/AT_PRIVATE_CHECK_SELECTIVE_BUILD).\n4. Registration of [Custom C++ Classes](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html) that extend PyTorch: This is controlled by the macro [TORCH_SELECTIVE_CLASS](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/fbgemm_utils.cpp#L385-L386), which can be used when registering Custom C++ Classes. The [torch::selective_class_<>](https://github.com/pytorch/pytorch/blob/master/torch/custom_class.h#L443-L460) helper is to be used in conjunction with the macro [TORCH_SELECTIVE_CLASS](https://codebrowser.bddppq.com/pytorch/pytorch/torch/library.h.html#_M/TORCH_SELECTIVE_CLASS).", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "### What is the structure of the YAML file used during the build?\n\nThe YAML file generated after tracing looks like the example below. It encodes all the elements of the \u201cselectable\u201d build feature as specified above.\n\n```python\ninclude_all_non_op_selectives: false\nbuild_features: []\noperators:\n aten::add.Tensor:\n is_used_for_training: false\n is_root_operator: true\n include_all_overloads: false\n aten::len.t:\n is_used_for_training: false\n is_root_operator: true\n include_all_overloads: false\nkernel_metadata:\n _local_scalar_dense_cpu:\n - Float\n add_stub:\n - Float\n copy_:\n - Bool\n - Byte\n mul_cpu:\n - Float\ncustom_classes: []\n```\n\n### How exactly is code eliminated from the generated binary?\n\nDepending on the specific scenario, there are 2 main techniques that are used to hint the compiler and linker about unused and unreachable code. This code is then cleaned up by the compiler or linker as unreachable code.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "#### [1] Unreferenced functions removed by the Linker\n\nWhen a function that isn\u2019t transitively referenced from any visible function is present in the compiled object files that are being linked together, the linker will remove it (if the right build flags are provided). This is leveraged in 2 scenarios by the selective build system.\n\n##### Kernel Registration in the Dispatcher\n\nIf an operator\u2019s kernel isn\u2019t needed, then it isn\u2019t registered with the dispatcher. An unregistered kernel means that the function is unreachable, and it will be removed by the linker.\n\n##### Templated Selective Build\n\nThe general idea here is that a class template specialization is used to select a class that either captures a reference to a function or not (depending on whether it\u2019s used) and the linker can come along and clean out the unreferenced function.\n\nFor example, in the code below, there\u2019s no reference to the function \u201c`fn2`\u201d, so it will be cleaned up by the linker since it\u2019s not referenced anywhere.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "```python\n#include \n#include \n\ntemplate \nstruct FunctionSelector {\n T fn_;\n FunctionSelector(T fn): fn_(fn) {}\n T get() { return this->fn_; }\n};\n\n// The \"false\" specialization of this class does NOT retain the argument passed\n// to the class constructor, which means that the function pointer passed in\n// is considered to be unreferenced in the program (unless it is referenced\n// elsewhere).\ntemplate \nstruct FunctionSelector {\n FunctionSelector(T) {}\n};\n\ntemplate \nFunctionSelector make_function_selector_true(T fn) {\n return FunctionSelector(fn);\n}\n\ntemplate \nFunctionSelector make_function_selector_false(T fn) {\n return FunctionSelector(fn);\n}\n\ntypedef void(*fn_ptr_type)();\n\nstd::vector fns;\n\ntemplate \nvoid add_fn(FunctionSelector fs) {\n fns.push_back(fs.get());\n}", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "template \nvoid add_fn(FunctionSelector) {\n // Do nothing.\n}\n\n// fn1 will be kept by the linker since it is added to the vector \"fns\" at\n// runtime.\nvoid fn1() {\n printf(\"fn1\\n\");\n}\n\n// fn2 will be removed by the linker since it isn't referenced at all.\nvoid fn2() {\n printf(\"fn2\\n\");\n}\n\nint main() {\n add_fn(make_function_selector_true(fn1));\n add_fn(make_function_selector_false(fn2));\n}\n```\n\n#### [2] Dead Code Eliminated by the Compiler\n\nC++ Compilers can detect dead ([unreachable](https://en.wikipedia.org/wiki/Unreachable_code)) code by analyzing the code\u2019s control flow statically. For example, if there\u2019s a code-path that comes after an **unconditional exception throw**, then all the code after it will be marked as dead code and not converted to object code by the compiler. Typically, compilers require the use of the `-fdce` flag to eliminate dead code.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "In the example below, you can see that the C++ code on the left (in the red boxes) doesn\u2019t have any corresponding generated object code on the right.\n\n

\n \n

\n\n

\n Figure 4: Dead Code Elimination by C++ Compilers\n

\n\nThis property is leveraged in the bodies of PyTorch kernel implementations that have a lot of repeated code to handle multiple dtypes of a Tensor. A [dtype](https://pytorch.org/docs/stable/tensor_attributes.html) is the underlying data-type that the Tensor stores elements of. This can be one of float, double, int64, bool, int8, etc\u2026\n\nAlmost every PyTorch CPU kernel uses a macro of the form AT_DISPATCH_ALL_TYPES* that is used to substitute some code specialized for every dtype that the kernel needs to handle. For example:", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} +{"page_content": "```python\nAT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(\n kBool, kHalf, kBFloat16, dtype, \"copy_kernel\", [&] {\n cpu_kernel_vec(\n iter,\n [=](scalar_t a) -> scalar_t { return a; },\n [=](Vectorized a) -> Vectorized { return a; });\n});\n```\n\nThe macro `AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3` internally has a switch-case statement that looks like the code in Figure-4 above. The tracing process records the dtypes triggered for the kernel tag \"`copy_kernel`\" and the build process processes these tags and inserts `throw` statements in every `case` statement that is handling the dtype that isn\u2019t required for this kernel tag.\n\nThis is how dtype selectivity is implemented in PyTorch\u2019s Tracing Based Selective Build.\n\n## Conclusion\n\nTracing Based Selective Build is a practical and scalable approach to selecting only the used parts of an application to retain code that static analysis can not detect. This code is usually extremely data/input dependent in nature.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} {"page_content": "This article provides detailed insights into how Tracing Based Selective Build works under the hood, and the technical details related to its implementation. These techniques can also be applied to other applications and situations that can benefit from reduced binary size.", "metadata": {"source": "https://pytorch.org/blog/pytorchs-tracing-based-selective-build/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch & OpenXLA: The Path Forward\"\nauthor: Milad Mohammadi, Jack Cao, Shauheen Zahirazami, Joe Spisak, and Jiewen Tan \n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "As we celebrate the release of [OpenXLA](https://opensource.googleblog.com/2023/03/openxla-is-ready-to-accelerate-and-simplify-ml-development.html), [PyTorch 2.0](https://pytorch.org/blog/pytorch-2.0-release/), and [PyTorch/XLA 2.0](https://pytorch.org/blog/pytorch-2.0-xla/), it\u2019s worth taking a step back and sharing where we see it all going in the short to medium term. With PyTorch adoption leading in the AI space and XLA supporting best-in-class compiler features, PyTorch/XLA is well positioned to", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "provide a cutting edge development stack for both model training and inference. To achieve this, we see investments in three main areas:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "* **Training Large Models** - Large language models (LLM) and diffusion models have quickly risen in popularity and many cutting edge applications today are built on them. Further to this, training these models requires scale and more specifically the ability to train across thousands of accelerators. To achieve this we are investing in features such as AMP for mixed precision training, PjRt for increased runtime performance, SPMD / FSDP for efficient model sharding, Dynamic Shapes to enable new research", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "approaches, faster data loading through Ray and tf.data, and a toolchain that packages all of these features together into a seamless workflow. Some of these features are already available in experimental or beta stages, and others are coming up this year with many heavily leveraging the underlying OpenXLA compiler stack.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "* **Model Inference** - With large models continuing to grow in size and computational cost, deployment becomes the next challenge as these models continue to find their way into applications. With the introduction of Dynamo in the PyTorch 2.0 release, PyTorch/XLA delivers performance competitive inference. We are, however, incorporating additional inference-oriented including model serving support, Dynamo for sharded large models, quantization via Torch.Export and StableHLO.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "* **Ecosystem integration** - We are expanding integration with [Hugging Face](https://huggingface.co/) and [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/) so users can take advantage of upcoming PyTorch/XLA cutting edge features (e.g. FSDP support in Hugging Face) and the downstream OpenXLA features (e.g. Quantization) through familiar APIs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "Additionally, PyTorch/XLA is set to migrate to the open source [OpenXLA](https://github.com/openxla) as its default downstream compiler; allowing the PyTorch community to gain access to a leading, framework-agnostic compiler stack that enjoys industry-wide contribution and innovation. To achieve this, we will begin supporting StableHLO. As a result, OpenXLA will replace the existing TF:XLA dependency, overall streamlining the dependencies and creating leverage from the broader compiler ecosystem.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "PyTorch/XLA will also sunset the XRT runtime after migration. You can see the resulting high level stack below with the TensorFlow dependency stricken out:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "![the upcoming PyTorch/XLA features and integrations](/assets/images/PyTorch_XLA Future Stack.svg){:style=\"max-height:800px; width:100%\"} \n\n**Figure:** the upcoming PyTorch/XLA features and integrations are illustrated here", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "We cannot be more excited about what\u2019s ahead for PyTorch/XLA and invite the community to join us. PyTorch/XLA is developed fully in open source so please file issues, submit pull requests, and send RFCs to [GitHub](https://github.com/pytorch/xla) such that we can openly collaborate. You can also [try out](https://colab.sandbox.google.com/github/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb) PyTorch/XLA for yourself on various XLA devices including TPUs and GPUs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "Cheers, \nThe PyTorch/XLA Team at Google", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling Multimodal Foundation Models in TorchMultimodal with Pytorch Distributed\"\nauthor: Ankita De, Edward Wang (EcoF), Rohan Varma, Anjali Sridhar, Kartikay Khandelwal\nfeatured-img: \"/assets/images/scaling-multimodal-image1-diagram-of-multimodal-flava-new.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Introduction", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "In recent years, scaling model sizes has become a promising area of research. In the field of NLP, language models have gone from hundreds of millions of parameters (BERT) to hundreds of billions of parameters (GPT-3) demonstrating significant improvements on downstream tasks. The [scaling laws](https://arxiv.org/pdf/2001.08361.pdf) for large scale language models have also been studied extensively in the industry. A similar trend can be observed in the vision field, with the community moving to transformer", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "based models (like [Vision Transformer](https://arxiv.org/pdf/2010.11929.pdf), [Masked Auto Encoders](https://arxiv.org/pdf/2111.06377.pdf)) as well. It is clear that individual modalities - text, image, video - have benefited massively from recent advancements in scale, and frameworks have quickly adapted to accommodate larger models.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch & OpenXLA: The Path Forward\"\nauthor: Milad Mohammadi, Jack Cao, Shauheen Zahirazami, Joe Spisak, and Jiewen Tan \n---\n\nAs we celebrate the release of [OpenXLA](https://opensource.googleblog.com/2023/03/openxla-is-ready-to-accelerate-and-simplify-ml-development.html), [PyTorch 2.0](https://pytorch.org/blog/pytorch-2.0-release/), and [PyTorch/XLA 2.0](https://pytorch.org/blog/pytorch-2.0-xla/), it\u2019s worth taking a step back and sharing where we see it all going in the short to medium term. With PyTorch adoption leading in the AI space and XLA supporting best-in-class compiler features, PyTorch/XLA is well positioned to provide a cutting edge development stack for both model training and inference. To achieve this, we see investments in three main areas:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} +{"page_content": "* **Training Large Models** - Large language models (LLM) and diffusion models have quickly risen in popularity and many cutting edge applications today are built on them. Further to this, training these models requires scale and more specifically the ability to train across thousands of accelerators. To achieve this we are investing in features such as AMP for mixed precision training, PjRt for increased runtime performance, SPMD / FSDP for efficient model sharding, Dynamic Shapes to enable new research approaches, faster data loading through Ray and tf.data, and a toolchain that packages all of these features together into a seamless workflow. Some of these features are already available in experimental or beta stages, and others are coming up this year with many heavily leveraging the underlying OpenXLA compiler stack.\n* **Model Inference** - With large models continuing to grow in size and computational cost, deployment becomes the next challenge as these models continue to find their way into applications. With the introduction of Dynamo in the PyTorch 2.0 release, PyTorch/XLA delivers performance competitive inference. We are, however, incorporating additional inference-oriented including model serving support, Dynamo for sharded large models, quantization via Torch.Export and StableHLO.\n* **Ecosystem integration** - We are expanding integration with [Hugging Face](https://huggingface.co/) and [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/) so users can take advantage of upcoming PyTorch/XLA cutting edge features (e.g. FSDP support in Hugging Face) and the downstream OpenXLA features (e.g. Quantization) through familiar APIs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} +{"page_content": "Additionally, PyTorch/XLA is set to migrate to the open source [OpenXLA](https://github.com/openxla) as its default downstream compiler; allowing the PyTorch community to gain access to a leading, framework-agnostic compiler stack that enjoys industry-wide contribution and innovation. To achieve this, we will begin supporting StableHLO. As a result, OpenXLA will replace the existing TF:XLA dependency, overall streamlining the dependencies and creating leverage from the broader compiler ecosystem. PyTorch/XLA will also sunset the XRT runtime after migration. You can see the resulting high level stack below with the TensorFlow dependency stricken out:\n\n![the upcoming PyTorch/XLA features and integrations](/assets/images/PyTorch_XLA Future Stack.svg){:style=\"max-height:800px; width:100%\"} \n\n**Figure:** the upcoming PyTorch/XLA features and integrations are illustrated here", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} +{"page_content": "We cannot be more excited about what\u2019s ahead for PyTorch/XLA and invite the community to join us. PyTorch/XLA is developed fully in open source so please file issues, submit pull requests, and send RFCs to [GitHub](https://github.com/pytorch/xla) such that we can openly collaborate. You can also [try out](https://colab.sandbox.google.com/github/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb) PyTorch/XLA for yourself on various XLA devices including TPUs and GPUs.\n\nCheers, \nThe PyTorch/XLA Team at Google", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-xla-path-forward/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling Multimodal Foundation Models in TorchMultimodal with Pytorch Distributed\"\nauthor: Ankita De, Edward Wang (EcoF), Rohan Varma, Anjali Sridhar, Kartikay Khandelwal\nfeatured-img: \"/assets/images/scaling-multimodal-image1-diagram-of-multimodal-flava-new.png\"\n---\n\n## Introduction", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "## Introduction\n\nIn recent years, scaling model sizes has become a promising area of research. In the field of NLP, language models have gone from hundreds of millions of parameters (BERT) to hundreds of billions of parameters (GPT-3) demonstrating significant improvements on downstream tasks. The [scaling laws](https://arxiv.org/pdf/2001.08361.pdf) for large scale language models have also been studied extensively in the industry. A similar trend can be observed in the vision field, with the community moving to transformer based models (like [Vision Transformer](https://arxiv.org/pdf/2010.11929.pdf), [Masked Auto Encoders](https://arxiv.org/pdf/2111.06377.pdf)) as well. It is clear that individual modalities - text, image, video - have benefited massively from recent advancements in scale, and frameworks have quickly adapted to accommodate larger models.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} {"page_content": "At the same time, multimodality is becoming increasingly important in research with tasks like image-text retrieval, visual question-answering, visual dialog and text to image generation gaining traction in real world applications. Training large scale multimodal models is the natural next step and we already see several efforts in this area like [CLIP](https://openai.com/blog/clip/) from OpenAI, [Parti](https://parti.research.google/) from Google and [CM3](https://arxiv.org/pdf/2201.07520.pdf) from Meta.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "In this blog, we present a case study demonstrating the scaling of [FLAVA](https://flava-model.github.io/) to 10B params using techniques from PyTorch Distributed. FLAVA is a vision and language foundation model, available in [TorchMultimodal](https://github.com/facebookresearch/multimodal/tree/main/torchmultimodal/models/flava), which has shown competitive performance on both unimodal and multimodal benchmarks. We also give the relevant code pointers in this blog. The instructions for running an example", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "script to scale FLAVA can be found [here](https://github.com/facebookresearch/multimodal/tree/main/examples/flava/native).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Scaling FLAVA Overview", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "FLAVA is a foundation multimodal model which consists of transformer based image and text encoders followed by a transformer-based multimodal fusion module. It is pretrained on both unimodal and multimodal data with a diverse set of losses. This includes masked language, image and multimodal modeling losses that require the model to reconstruct the original input from its context (self-supervised learning). It also uses image text matching loss over positive and negative examples of aligned image-text pairs", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "as well as CLIP style contrastive loss. In addition to multimodal tasks (like image-text retrieval), FLAVA demonstrated competitive performance on unimodal benchmarks as well (GLUE tasks for NLP and image classification for vision).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nThe original FLAVA model has ~350M parameters and uses ViT-B16 configurations (from the [Vision Transformer paper](https://arxiv.org/pdf/2010.11929.pdf)) for image and text encoders. The multimodal fusion transformer follows the unimodal encoders but with half the number of layers. We explore increasing the size of each encoder to larger ViT variants.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Another aspect of scaling is adding the ability to increase the batch size. FLAVA makes use of contrastive loss over in-batch negatives, which typically benefits from large batch size (as studied [here](https://openreview.net/pdf?id=U2exBrf_SJh)). The largest training efficiency or throughput is also generally achieved when operating near maximum possible batch sizes as determined by the amount of GPU memory available (also see the experiments section).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "The following table displays the different model configurations we experimented with. We also determine the maximum batch size that was able to fit in memory for each configuration in the experiments section.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "| Approx Model params | Hidden size | MLP size | Heads | Unimodal layers | Multimodal layers | Model size (fp32) |\n|-----------------------|---------------|----------|---------|-------------------|---------------------|---------------------|\n| 350M (original) | 768 | 3072 | 12 | 12 | 6 | 1.33GB |\n| 900M | 1024 | 4096 | 16 | 24 | 12 | 3.48GB |", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "| 1.8B | 1280 | 5120 | 16 | 32 | 16 | 6.66GB |\n| 2.7B | 1408 | 6144 | 16 | 40 | 20 | 10.3GB |\n| 4.8B | 1664 | 8192 | 16 | 48 | 24 | 18.1GB |\n| 10B | 2048 | 10240 | 16 | 64 | 40 | 38GB |", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Optimization overview\n\nPyTorch offers several native techniques to efficiently scale models. In the following sections, we go over some of these techniques and show how they can be applied to scale up a FLAVA model to 10 billion parameters.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Distributed Data Parallel\n\nA common starting point for distributed training is data parallelism. Data parallelism replicates the model across each worker (GPU), and partitions the dataset across the workers. Different workers process different data partitions in parallel and synchronize their gradients (via all reduce) before model weights are updated. The figure below showcases the flow (forward, backward, and weight update steps) for processing a single example for data parallelism:", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Source: https://engineering.fb.com/2021/07/15/open-source/fsdp/\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "PyTorch provides a native API, [DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html) (DDP) to enable data parallelism which can be used as a module wrapper as showcased below. Please see PyTorch Distributed [documentation](https://pytorch.org/docs/stable/distributed.html#) for more details.\n\n```Python\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nimport torch\nimport torch.distributed as dist", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "model = flava_model_for_pretraining().cuda()\n# Initialize PyTorch Distributed process groups\n# Please see https://pytorch.org/tutorials/intermediate/dist_tuto.html for details\ndist.init_process_group(backend=\u201dnccl\u201d)\n# Wrap model in DDP\nmodel = torch.nn.parallel.DistributedDataParallel(model, device_ids=[torch.cuda.current_device()])\n```", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Fully Sharded Data Parallel\n\nGPU memory usage of a training application can roughly be broken down into model inputs, intermediate activations (needed for gradient computation), model parameters, gradients, and optimizer states. Scaling a model will typically increase each of these elements. Scaling a model with DDP can eventually result in out-of-memory issues when a single GPU's memory becomes insufficient since it replicates the parameters, gradients, and optimizer states on all workers.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "To reduce this replication and save GPU memory, we can shard the model parameters, gradients, and optimizer states across all workers with each worker only managing a single shard. This technique was popularized by the [ZeRO-3](https://arxiv.org/abs/1910.02054) approach developed by Microsoft. A PyTorch-native implementation of this approach is available as [FullyShardedDataParallel](https://pytorch.org/docs/stable/fsdp.html) (FSDP) API, released as a beta feature in PyTorch 1.12. During a module\u2019s forward", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "and backward passes, FSDP unshards the model parameters as needed for computation (using all-gather) and reshards them after computation. It synchronizes gradients using the reduce-scatter collective to ensure sharded gradients are globally averaged. The forward and backward pass flow of a model wrapped in FSDP are detailed below:", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Source: https://engineering.fb.com/2021/07/15/open-source/fsdp/\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "To use FSDP, the submodules of a model need to be wrapped with the API to control when specific submodules are sharded or unsharded. FSDP provides an auto-wrapping API (see the [auto_wrap_policy](https://pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel) argument) that can be used out of the box as well as several [wrapping policies](https://github.com/pytorch/pytorch/blob/master/torch/distributed/fsdp/wrap.py) and the ability to [write your own", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "policy](https://github.com/pytorch/pytorch/blob/75c0e3a471c19b883feca15fd4ecfabedf746691/torch/distributed/fsdp/fully_sharded_data_parallel.py#L858).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "The following example demonstrates wrapping the FLAVA model with FSDP. We specify the auto-wrapping policy as `transformer_auto_wrap_policy`. This will wrap individual transformer layers (`TransformerEncoderLayer`), the image transformer (`ImageTransformer`), text encoder (`BERTTextEncoder`) and multimodal encoder (`FLAVATransformerWithoutEmbeddings`) as individual FSDP units. This uses a recursive wrapping approach for efficient memory management. For example, after an individual transformer layer\u2019s", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "forward or backward pass is finished, its parameters are discarded, freeing up memory thereby reducing peak memory usage.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "FSDP also provides a number of configurable options to tune the performance of applications. For example, in our use case, we illustrate the use of the new `limit_all_gathers` flag, which prevents all-gathering model parameters too early thereby alleviating memory pressure on the application. We encourage users to experiment with this flag which can potentially improve the performance of applications with high active memory usage.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "```Python\nimport torch\nfrom torch.distributed.fsdp import FullyShardedDataParallel as FSDP\nfrom torch.distributed.fsdp.wrap import transformer_auto_wrap_policy\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nfrom torchmultimodal.models.flava.text_encoder import BertTextEncoder\nfrom torchmultimodal.models.flava.image_encoder import ImageTransformer\nfrom torchmultimodal.models.flava.transformer import FLAVATransformerWithoutEmbeddings", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "from torchmultimodal.modules.layers.transformer import TransformerEncoderLayer", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "model = flava_model_for_pretraining().cuda()\ndist.init_process_group(backend=\u201dnccl\u201d)", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "model = FSDP(\n model,\n device_id=torch.cuda.current_device(),\n auto_wrap_policy=partial(\n transformer_auto_wrap_policy,\n transformer_layer_cls={\n TransformerEncoderLayer,\n ImageTransformer,\n BERTTextEncoder,\n FLAVATransformerWithoutEmbeddings\n },\n ),\n limit_all_gathers=True,\n )\n```", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Activation Checkpointing", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "As discussed above, intermediate activations, model parameters, gradients, and optimizer states contribute to the overall GPU memory usage. FSDP can reduce memory consumption due to the latter three but does not reduce memory consumed by activations. Memory used by activations increases with increase in batch size or number of hidden layers. Activation checkpointing is a technique to decrease this memory usage by recomputing the activations during the backward pass instead of holding them in memory for a", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "specific checkpointed module. For example, we observed ~4x reduction in the peak active memory after forward pass by applying activation checkpointing to the 2.7B parameter model.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "PyTorch offers a wrapper based activation checkpointing API. In particular, `checkpoint_wrapper` allows users to wrap an individual module with checkpointing, and `apply_activation_checkpointing` allows users to specify a policy with which to wrap modules within an overall module with checkpointing. Both these APIs can be applied to most models as they do not require any modifications to the model definition code. However, if more granular control over checkpointed segments, such as checkpointing specific", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "functions within a module, is required, the functional `torch.utils.checkpoint` [API](https://pytorch.org/docs/stable/checkpoint.html) can be leveraged, although this requires modification to the model code. The application of the activation checkpointing wrapper to individual FLAVA transformer layers (denoted by `TransformerEncoderLayer`) is shown below. For a thorough description of activation checkpointing, please see the description in the [PyTorch", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "documentation](https://pytorch.org/docs/stable/checkpoint.html).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "```Python\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nfrom torch.distributed.algorithms._checkpoint.checkpoint_wrapper import apply_activation_checkpointing, checkpoint_wrapper, CheckpointImpl\nfrom torchmultimodal.modules.layers.transformer import TransformerEncoderLayer\n\nmodel = flava_model_for_pretraining()\ncheckpoint_tformer_layers_policy = lambda submodule: isinstance(submodule, TransformerEncoderLayer)", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "apply_activation_checkpointing(\n model,\n checkpoint_wrapper_fn=checkpoint_wrapper,\n check_fn=checkpoint_tformer_layers_policy,\n )\n```\nUsed together, wrapping FLAVA transformer layers with activation checkpointing and wrapping the overall model with FSDP as demonstrated above, we are able to scale FLAVA to 10B parameters.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Experiments", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "We conduct an empirical study about the impact of the different optimizations from the previous section on system performance. For all our experiments, we use a single node with 8 A100 40GB GPUs and run the pretraining for 1000 iterations. All runs also used PyTorch\u2019s [automatic mixed precision](https://pytorch.org/docs/stable/amp.html) with the bfloat16 data type. [TensorFloat32](https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) format is also enabled to improve matmul", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "performance on the A100. We define throughput as the average number of items (text or image) processed per second (we ignore the first 100 iterations while measuring throughput to account for warmup). We leave training to convergence and its impact on downstream task metrics as an area for future study.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "In this blog, we present a case study demonstrating the scaling of [FLAVA](https://flava-model.github.io/) to 10B params using techniques from PyTorch Distributed. FLAVA is a vision and language foundation model, available in [TorchMultimodal](https://github.com/facebookresearch/multimodal/tree/main/torchmultimodal/models/flava), which has shown competitive performance on both unimodal and multimodal benchmarks. We also give the relevant code pointers in this blog. The instructions for running an example script to scale FLAVA can be found [here](https://github.com/facebookresearch/multimodal/tree/main/examples/flava/native).\n\n## Scaling FLAVA Overview", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "FLAVA is a foundation multimodal model which consists of transformer based image and text encoders followed by a transformer-based multimodal fusion module. It is pretrained on both unimodal and multimodal data with a diverse set of losses. This includes masked language, image and multimodal modeling losses that require the model to reconstruct the original input from its context (self-supervised learning). It also uses image text matching loss over positive and negative examples of aligned image-text pairs as well as CLIP style contrastive loss. In addition to multimodal tasks (like image-text retrieval), FLAVA demonstrated competitive performance on unimodal benchmarks as well (GLUE tasks for NLP and image classification for vision).\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "The original FLAVA model has ~350M parameters and uses ViT-B16 configurations (from the [Vision Transformer paper](https://arxiv.org/pdf/2010.11929.pdf)) for image and text encoders. The multimodal fusion transformer follows the unimodal encoders but with half the number of layers. We explore increasing the size of each encoder to larger ViT variants. \n\nAnother aspect of scaling is adding the ability to increase the batch size. FLAVA makes use of contrastive loss over in-batch negatives, which typically benefits from large batch size (as studied [here](https://openreview.net/pdf?id=U2exBrf_SJh)). The largest training efficiency or throughput is also generally achieved when operating near maximum possible batch sizes as determined by the amount of GPU memory available (also see the experiments section). \n\nThe following table displays the different model configurations we experimented with. We also determine the maximum batch size that was able to fit in memory for each configuration in the experiments section.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "| Approx Model params | Hidden size | MLP size | Heads | Unimodal layers | Multimodal layers | Model size (fp32) |\n|-----------------------|---------------|----------|---------|-------------------|---------------------|---------------------|\n| 350M (original) | 768 | 3072 | 12 | 12 | 6 | 1.33GB |\n| 900M | 1024 | 4096 | 16 | 24 | 12 | 3.48GB |\n| 1.8B | 1280 | 5120 | 16 | 32 | 16 | 6.66GB |\n| 2.7B | 1408 | 6144 | 16 | 40 | 20 | 10.3GB |\n| 4.8B | 1664 | 8192 | 16 | 48 | 24 | 18.1GB |\n| 10B | 2048 | 10240 | 16 | 64 | 40 | 38GB |", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "## Optimization overview\n\nPyTorch offers several native techniques to efficiently scale models. In the following sections, we go over some of these techniques and show how they can be applied to scale up a FLAVA model to 10 billion parameters.\n\n## Distributed Data Parallel\n\nA common starting point for distributed training is data parallelism. Data parallelism replicates the model across each worker (GPU), and partitions the dataset across the workers. Different workers process different data partitions in parallel and synchronize their gradients (via all reduce) before model weights are updated. The figure below showcases the flow (forward, backward, and weight update steps) for processing a single example for data parallelism:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "

\n Source: https://engineering.fb.com/2021/07/15/open-source/fsdp/\n

\n\nPyTorch provides a native API, [DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html) (DDP) to enable data parallelism which can be used as a module wrapper as showcased below. Please see PyTorch Distributed [documentation](https://pytorch.org/docs/stable/distributed.html#) for more details.\n\n```Python\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nimport torch\nimport torch.distributed as dist\n\nmodel = flava_model_for_pretraining().cuda()\n# Initialize PyTorch Distributed process groups\n# Please see https://pytorch.org/tutorials/intermediate/dist_tuto.html for details\ndist.init_process_group(backend=\u201dnccl\u201d)\n# Wrap model in DDP\nmodel = torch.nn.parallel.DistributedDataParallel(model, device_ids=[torch.cuda.current_device()])\n```\n\n## Fully Sharded Data Parallel", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "GPU memory usage of a training application can roughly be broken down into model inputs, intermediate activations (needed for gradient computation), model parameters, gradients, and optimizer states. Scaling a model will typically increase each of these elements. Scaling a model with DDP can eventually result in out-of-memory issues when a single GPU's memory becomes insufficient since it replicates the parameters, gradients, and optimizer states on all workers.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "To reduce this replication and save GPU memory, we can shard the model parameters, gradients, and optimizer states across all workers with each worker only managing a single shard. This technique was popularized by the [ZeRO-3](https://arxiv.org/abs/1910.02054) approach developed by Microsoft. A PyTorch-native implementation of this approach is available as [FullyShardedDataParallel](https://pytorch.org/docs/stable/fsdp.html) (FSDP) API, released as a beta feature in PyTorch 1.12. During a module\u2019s forward and backward passes, FSDP unshards the model parameters as needed for computation (using all-gather) and reshards them after computation. It synchronizes gradients using the reduce-scatter collective to ensure sharded gradients are globally averaged. The forward and backward pass flow of a model wrapped in FSDP are detailed below:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "

\n Source: https://engineering.fb.com/2021/07/15/open-source/fsdp/\n

\n\nTo use FSDP, the submodules of a model need to be wrapped with the API to control when specific submodules are sharded or unsharded. FSDP provides an auto-wrapping API (see the [auto_wrap_policy](https://pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel) argument) that can be used out of the box as well as several [wrapping policies](https://github.com/pytorch/pytorch/blob/master/torch/distributed/fsdp/wrap.py) and the ability to [write your own policy](https://github.com/pytorch/pytorch/blob/75c0e3a471c19b883feca15fd4ecfabedf746691/torch/distributed/fsdp/fully_sharded_data_parallel.py#L858).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "The following example demonstrates wrapping the FLAVA model with FSDP. We specify the auto-wrapping policy as `transformer_auto_wrap_policy`. This will wrap individual transformer layers (`TransformerEncoderLayer`), the image transformer (`ImageTransformer`), text encoder (`BERTTextEncoder`) and multimodal encoder (`FLAVATransformerWithoutEmbeddings`) as individual FSDP units. This uses a recursive wrapping approach for efficient memory management. For example, after an individual transformer layer\u2019s forward or backward pass is finished, its parameters are discarded, freeing up memory thereby reducing peak memory usage.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "FSDP also provides a number of configurable options to tune the performance of applications. For example, in our use case, we illustrate the use of the new `limit_all_gathers` flag, which prevents all-gathering model parameters too early thereby alleviating memory pressure on the application. We encourage users to experiment with this flag which can potentially improve the performance of applications with high active memory usage.\n\n```Python\nimport torch\nfrom torch.distributed.fsdp import FullyShardedDataParallel as FSDP\nfrom torch.distributed.fsdp.wrap import transformer_auto_wrap_policy\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nfrom torchmultimodal.models.flava.text_encoder import BertTextEncoder\nfrom torchmultimodal.models.flava.image_encoder import ImageTransformer\nfrom torchmultimodal.models.flava.transformer import FLAVATransformerWithoutEmbeddings\nfrom torchmultimodal.modules.layers.transformer import TransformerEncoderLayer", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "model = flava_model_for_pretraining().cuda()\ndist.init_process_group(backend=\u201dnccl\u201d)\n\nmodel = FSDP(\n model,\n device_id=torch.cuda.current_device(),\n auto_wrap_policy=partial(\n transformer_auto_wrap_policy,\n transformer_layer_cls={\n TransformerEncoderLayer,\n ImageTransformer,\n BERTTextEncoder,\n FLAVATransformerWithoutEmbeddings\n },\n ),\n limit_all_gathers=True,\n )\n```\n\n## Activation Checkpointing", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "As discussed above, intermediate activations, model parameters, gradients, and optimizer states contribute to the overall GPU memory usage. FSDP can reduce memory consumption due to the latter three but does not reduce memory consumed by activations. Memory used by activations increases with increase in batch size or number of hidden layers. Activation checkpointing is a technique to decrease this memory usage by recomputing the activations during the backward pass instead of holding them in memory for a specific checkpointed module. For example, we observed ~4x reduction in the peak active memory after forward pass by applying activation checkpointing to the 2.7B parameter model.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "PyTorch offers a wrapper based activation checkpointing API. In particular, `checkpoint_wrapper` allows users to wrap an individual module with checkpointing, and `apply_activation_checkpointing` allows users to specify a policy with which to wrap modules within an overall module with checkpointing. Both these APIs can be applied to most models as they do not require any modifications to the model definition code. However, if more granular control over checkpointed segments, such as checkpointing specific functions within a module, is required, the functional `torch.utils.checkpoint` [API](https://pytorch.org/docs/stable/checkpoint.html) can be leveraged, although this requires modification to the model code. The application of the activation checkpointing wrapper to individual FLAVA transformer layers (denoted by `TransformerEncoderLayer`) is shown below. For a thorough description of activation checkpointing, please see the description in the [PyTorch documentation](https://pytorch.org/docs/stable/checkpoint.html).", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "```Python\nfrom torchmultimodal.models.flava.model import flava_model_for_pretraining\nfrom torch.distributed.algorithms._checkpoint.checkpoint_wrapper import apply_activation_checkpointing, checkpoint_wrapper, CheckpointImpl\nfrom torchmultimodal.modules.layers.transformer import TransformerEncoderLayer\n\nmodel = flava_model_for_pretraining()\ncheckpoint_tformer_layers_policy = lambda submodule: isinstance(submodule, TransformerEncoderLayer)\n\napply_activation_checkpointing(\n model,\n checkpoint_wrapper_fn=checkpoint_wrapper,\n check_fn=checkpoint_tformer_layers_policy,\n )\n```\nUsed together, wrapping FLAVA transformer layers with activation checkpointing and wrapping the overall model with FSDP as demonstrated above, we are able to scale FLAVA to 10B parameters.\n\n## Experiments", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "## Experiments\n\nWe conduct an empirical study about the impact of the different optimizations from the previous section on system performance. For all our experiments, we use a single node with 8 A100 40GB GPUs and run the pretraining for 1000 iterations. All runs also used PyTorch\u2019s [automatic mixed precision](https://pytorch.org/docs/stable/amp.html) with the bfloat16 data type. [TensorFloat32](https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) format is also enabled to improve matmul performance on the A100. We define throughput as the average number of items (text or image) processed per second (we ignore the first 100 iterations while measuring throughput to account for warmup). We leave training to convergence and its impact on downstream task metrics as an area for future study.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} {"page_content": "Figure 1 plots the throughput for each model configuration and optimization, both with a local batch size of 8 and then with the maximum batch size possible on 1 node. Absence of a data point for a model variant for an optimization indicates that the model could not be trained on a single node.\n\nFigure 2 plots the maximum possible batch size per worker for each optimization. We observe a few things:", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "1. Scaling model size: DDP is only able to fit the 350M and 900M model on a node. With FSDP, due to memory savings, we are able to train ~3x bigger models compared to DDP (i.e. the 1.8B and 2.7B variants). Combining activation checkpointing (AC) with FSDP enables training even bigger models, on the order of ~10x compared to DDP (i.e. 4.8B and 10B variants)\n2. Throughput:", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "- For smaller model sizes, at a constant batch size of 8, the throughput for DDP is slightly higher than or equal to FSDP, explainable by the additional communication required by FSDP. It is lowest for FSDP and AC combined together. This is because AC re-runs checkpointed forward passes during the backwards pass, trading off additional computation for memory savings. However, in the case of the 2.7B model, FSDP + AC actually has higher throughput compared to FSDP alone. This is because the 2.7B model with", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "FSDP is operating close to the memory limit even at batch size 8 triggering CUDA malloc retries which tend to slow down training. AC helps with reducing the memory pressure and leads to no retries.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "- For DDP and FSDP + AC, the throughput increases with an increase in batch size for each model. For FSDP alone, this is true for smaller variants. However, with the 1.8B and 2.7B parameter models, we observe throughput degradation when increasing batch size. A potential reason for this, as noted above also, is that at the memory limit, PyTorch\u2019s CUDA memory management may have to retry cudaMalloc calls and/or run expensive defragmentation steps to find free memory blocks to handle the workload\u2019s memory", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "requirements which can result in training slowdown.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "- For larger models that can only be trained with FSDP (1.8B, 2.7B, 4.8B) the setting with highest throughput achieved is with FSDP + AC scaling to the maximum batch size. For 10B, we observe nearly equal throughput for smaller and maximum batch size. This might be counterintuitive as AC results in increased computation and maxing out batch size potentially leads to expensive defragmentation operations due to operating at CUDA memory limit. However, for these large models, the increase in batch size is", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "large enough to mask this overhead.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Figure 1: Training throughput for different configurations\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "
    \n
  1. Batch size: FSDP alone enables slightly higher batch sizes compared to DDP. Using FSDP + AC enables ~3x batch size compared to DDP for the 350M param model and ~5.5x for 900M param model. Even for 10B, a max batch size of ~20 which is fairly decent. This essentially enables larger global batch size using fewer GPUs which is especially useful for contrastive learning tasks.
  2. \n
", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Figure 2: Max local batchsize possible for different configurations\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "Conclusion", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "As the world moves towards multimodal foundation models, scaling model parameters and efficient training is becoming an area of focus. The PyTorch ecosystem aims to accelerate innovation in this field by providing different tools to the research community, both for training and scaling multimodal models. With FLAVA, we laid out an example of scaling a model for multimodal understanding. In the future, we plan to add support for other kinds of models like the ones for multimodal generation and demonstrate", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "their scaling factors. We also hope to automate many of these scaling and memory saving techniques (such as sharding and activation checkpointing) to reduce the amount of user experimentation needed to achieve the desired scale and maximum training throughput.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "References\n\n- [Introducing TorchMultimodal - a library for accelerating exploration in Multimodal AI](https://pytorch.org/blog/introducing-torchmultimodal/)\n- [FLAVA paper](https://deploy-preview-1186--pytorch-dot-org-preview.netlify.app/blog/introducing-torchmultimodal/)\n- [Introducing Pytorch FSDP](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/)", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"A BetterTransformer for Fast Transformer Inference\"\nauthor: Michael Gschwind, Eric Han, Scott Wolchok, Rui Zhu, Christian Puhrsch\nfeatured-img: \"/assets/images/2022-7-12-a-better-transformer-for-fast-transformer-encoder-inference-3.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "**tl;dr** Transformers achieve state-of-the-art performance for NLP, and are becoming popular for a myriad of other tasks. They are computationally expensive which has been a blocker to their widespread productionisation. Launching with PyTorch 1.12, BetterTransformer implements a backwards-compatible fast path of `torch.nn.TransformerEncoder` for Transformer Encoder Inference and does not require model authors to modify their models. BetterTransformer improvements can exceed 2x in speedup and throughput", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "for many common execution scenarios. To use BetterTransformer, [install](https://pytorch.org/get-started/locally/) PyTorch 1.12 and start using high-quality, high-performance Transformer models with the PyTorch API today.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nDiagram of the Transformer Encoder Architecture (from \"Attention Is All You Need\"). During Inference, the entire module will execute as a single PyTorch-native function.\n

", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "In this blog post, we share the following topics \u2014 Performance Improvements, Backwards compatibility, and Taking advantage of the FastPath. Learn more about these topics below.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Performance Improvements", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "BetterTransformer launches with accelerated native implementations of MultiHeadAttention and TransformerEncoderLayer for CPUs and GPUs. These fast paths are integrated in the standard PyTorch Transformer APIs, and will accelerate [TransformerEncoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html), [TransformerEncoderLayer](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html) and", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "[MultiHeadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) nn.modules. These new modules implement two types of optimizations: (1) fused kernels combine multiple individual operators normally used to implement Transformers to provide a more efficient implementation, and (2) take advantage of sparsity in the inputs to avoid performing unnecessary operations on padding tokens. Padding tokens frequently account for a large fraction of input batches in many Transformer", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "models used for Natural Language Processing.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Backwards compatibility", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Advantageously, **no model changes are necessary to benefit from the performance boost offered by BetterTransformer.** To benefit from fast path execution, inputs and operating conditions must satisfy some access conditions (see below). While the internal implementation of Transformer APIs has changed, PyTorch 1.12 maintains strict compatibility with Transformer modules shipped in previous versions, enabling PyTorch users to use models created and trained with previous PyTorch releases while benefiting from", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "BetterTransformer improvements.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "In addition to enabling the PyTorch nn.Modules, BetterTransformer provides improvements for PyTorch libraries. Performance benefits will become available through two different enablement paths:", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "1. Transparent acceleration: Current users of PyTorch nn.Modules such as [MultiHeadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) as well as higher-level Transformer components will benefit from the improved performance of the new nn.Modules automatically. An example of this is the [visual transformer (ViT)](https://arxiv.org/abs/2010.11929) implementation used in the torchvision library ([code", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "link](https://github.com/pytorch/vision/blob/87cde716b7f108f3db7b86047596ebfad1b88380/torchvision/models/vision_transformer.py#L103)).", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "2. Torchtext library acceleration: As part of this project, we have optimized Torchtext to build on the PyTorch core API to benefit from BetterTransformer enhancements while maintaining strict and transparent compatibility with previous library versions and models trained with previous Torchtext versions. Using PyTorch Transformers in Torchtext also ensures that Torchtext will benefit from expected future enhancements to the PyTorch Transformer implementation.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Taking advantage of the Fastpath\n\nBetterTransformer is a fastpath for the PyTorch Transformer API. The fastpath is a native, specialized implementation of key Transformer functions for CPU and GPU that applies to common Transformer use cases.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "1. Scaling model size: DDP is only able to fit the 350M and 900M model on a node. With FSDP, due to memory savings, we are able to train ~3x bigger models compared to DDP (i.e. the 1.8B and 2.7B variants). Combining activation checkpointing (AC) with FSDP enables training even bigger models, on the order of ~10x compared to DDP (i.e. 4.8B and 10B variants)\n2. Throughput:\n - For smaller model sizes, at a constant batch size of 8, the throughput for DDP is slightly higher than or equal to FSDP, explainable by the additional communication required by FSDP. It is lowest for FSDP and AC combined together. This is because AC re-runs checkpointed forward passes during the backwards pass, trading off additional computation for memory savings. However, in the case of the 2.7B model, FSDP + AC actually has higher throughput compared to FSDP alone. This is because the 2.7B model with FSDP is operating close to the memory limit even at batch size 8 triggering CUDA malloc retries which tend to slow down training. AC helps with reducing the memory pressure and leads to no retries.\n - For DDP and FSDP + AC, the throughput increases with an increase in batch size for each model. For FSDP alone, this is true for smaller variants. However, with the 1.8B and 2.7B parameter models, we observe throughput degradation when increasing batch size. A potential reason for this, as noted above also, is that at the memory limit, PyTorch\u2019s CUDA memory management may have to retry cudaMalloc calls and/or run expensive defragmentation steps to find free memory blocks to handle the workload\u2019s memory requirements which can result in training slowdown.\n - For larger models that can only be trained with FSDP (1.8B, 2.7B, 4.8B) the setting with highest throughput achieved is with FSDP + AC scaling to the maximum batch size. For 10B, we observe nearly equal throughput for smaller and maximum batch size. This might be counterintuitive as AC results in increased computation and maxing out batch size potentially leads to expensive defragmentation operations due to operating at CUDA memory limit. However, for these large models, the increase in batch size is large enough to mask this overhead.", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\n Figure 1: Training throughput for different configurations\n

\n\n
    \n
  1. Batch size: FSDP alone enables slightly higher batch sizes compared to DDP. Using FSDP + AC enables ~3x batch size compared to DDP for the 350M param model and ~5.5x for 900M param model. Even for 10B, a max batch size of ~20 which is fairly decent. This essentially enables larger global batch size using fewer GPUs which is especially useful for contrastive learning tasks.
  2. \n
\n\n

\n \n

\n\n

\n Figure 2: Max local batchsize possible for different configurations\n

\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nAs the world moves towards multimodal foundation models, scaling model parameters and efficient training is becoming an area of focus. The PyTorch ecosystem aims to accelerate innovation in this field by providing different tools to the research community, both for training and scaling multimodal models. With FLAVA, we laid out an example of scaling a model for multimodal understanding. In the future, we plan to add support for other kinds of models like the ones for multimodal generation and demonstrate their scaling factors. We also hope to automate many of these scaling and memory saving techniques (such as sharding and activation checkpointing) to reduce the amount of user experimentation needed to achieve the desired scale and maximum training throughput.\n\n## References", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "## References\n\n- [Introducing TorchMultimodal - a library for accelerating exploration in Multimodal AI](https://pytorch.org/blog/introducing-torchmultimodal/)\n- [FLAVA paper](https://deploy-preview-1186--pytorch-dot-org-preview.netlify.app/blog/introducing-torchmultimodal/)\n- [Introducing Pytorch FSDP](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/)", "metadata": {"source": "https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"A BetterTransformer for Fast Transformer Inference\"\nauthor: Michael Gschwind, Eric Han, Scott Wolchok, Rui Zhu, Christian Puhrsch\nfeatured-img: \"/assets/images/2022-7-12-a-better-transformer-for-fast-transformer-encoder-inference-3.png\"\n---\n\n**tl;dr** Transformers achieve state-of-the-art performance for NLP, and are becoming popular for a myriad of other tasks. They are computationally expensive which has been a blocker to their widespread productionisation. Launching with PyTorch 1.12, BetterTransformer implements a backwards-compatible fast path of `torch.nn.TransformerEncoder` for Transformer Encoder Inference and does not require model authors to modify their models. BetterTransformer improvements can exceed 2x in speedup and throughput for many common execution scenarios. To use BetterTransformer, [install](https://pytorch.org/get-started/locally/) PyTorch 1.12 and start using high-quality, high-performance Transformer models with the PyTorch API today.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\nDiagram of the Transformer Encoder Architecture (from \"Attention Is All You Need\"). During Inference, the entire module will execute as a single PyTorch-native function.\n

\n\nIn this blog post, we share the following topics \u2014 Performance Improvements, Backwards compatibility, and Taking advantage of the FastPath. Learn more about these topics below. \n\n## Performance Improvements", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "BetterTransformer launches with accelerated native implementations of MultiHeadAttention and TransformerEncoderLayer for CPUs and GPUs. These fast paths are integrated in the standard PyTorch Transformer APIs, and will accelerate [TransformerEncoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html), [TransformerEncoderLayer](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html) and [MultiHeadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) nn.modules. These new modules implement two types of optimizations: (1) fused kernels combine multiple individual operators normally used to implement Transformers to provide a more efficient implementation, and (2) take advantage of sparsity in the inputs to avoid performing unnecessary operations on padding tokens. Padding tokens frequently account for a large fraction of input batches in many Transformer models used for Natural Language Processing. \n\n## Backwards compatibility", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "Advantageously, **no model changes are necessary to benefit from the performance boost offered by BetterTransformer.** To benefit from fast path execution, inputs and operating conditions must satisfy some access conditions (see below). While the internal implementation of Transformer APIs has changed, PyTorch 1.12 maintains strict compatibility with Transformer modules shipped in previous versions, enabling PyTorch users to use models created and trained with previous PyTorch releases while benefiting from BetterTransformer improvements.\n\nIn addition to enabling the PyTorch nn.Modules, BetterTransformer provides improvements for PyTorch libraries. Performance benefits will become available through two different enablement paths:", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "1. Transparent acceleration: Current users of PyTorch nn.Modules such as [MultiHeadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) as well as higher-level Transformer components will benefit from the improved performance of the new nn.Modules automatically. An example of this is the [visual transformer (ViT)](https://arxiv.org/abs/2010.11929) implementation used in the torchvision library ([code link](https://github.com/pytorch/vision/blob/87cde716b7f108f3db7b86047596ebfad1b88380/torchvision/models/vision_transformer.py#L103)).", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "2. Torchtext library acceleration: As part of this project, we have optimized Torchtext to build on the PyTorch core API to benefit from BetterTransformer enhancements while maintaining strict and transparent compatibility with previous library versions and models trained with previous Torchtext versions. Using PyTorch Transformers in Torchtext also ensures that Torchtext will benefit from expected future enhancements to the PyTorch Transformer implementation.\n\n## Taking advantage of the Fastpath\n\nBetterTransformer is a fastpath for the PyTorch Transformer API. The fastpath is a native, specialized implementation of key Transformer functions for CPU and GPU that applies to common Transformer use cases.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} {"page_content": "To take advantage of input sparsity (i.e. padding) in accelerating your model (see Figure 2), set the keyword argument `enable_nested_tensor=True` when instantiating a [TransformerEncoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html) and pass in the `src_key_padding_mask` argument (which denotes padding tokens) during inference. This requires the padding mask to be contiguous, which is the typical case.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Currently, the BetterTransformer speedup only applies to transformer encoder models used in inference. To benefit from fastpath execution, models must be composed of any of the following components: [TransformerEncoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html), [TransformerEncoderLayer](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html) or [MultiheadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) (MHA).", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Fastpath execution is also subject to some criteria. Most importantly, the model must be executed in inference mode and operate on input tensors that do not collect gradient tape information (e.g., running with torch.no_grad). The full list of conditions can be found at these links for [nn.MultiHeadAttention](https://github.com/pytorch/pytorch/blob/29189d2ba8e583b2355cd0e9517a1ee742ba12cf/torch/nn/modules/activation.py#L1060) and", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "[nn.TransformerEncoder](https://github.com/pytorch/pytorch/blob/29189d2ba8e583b2355cd0e9517a1ee742ba12cf/torch/nn/modules/transformer.py#L206), respectively. If the criteria are not met, control flows to the legacy PyTorch 1.11 Transformer implementation which has the same API, but lacks the fastpath performance boost.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Other transformer models (such as decoder models) which use the PyTorch MultiheadAttention module will benefit from the BetterTransformer fastpath. Planned future work is to expand the end-to-end BetterTransformer fastpath to models based on [TransformerDecoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerDecoder.html) to support popular seq2seq and decoder-only (e.g., [OPT](https://ai.facebook.com/blog/democratizing-access-to-large-scale-language-models-with-opt-175b/)) model", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "architectures, and to training.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Speedups\n\nThe following graphs show the performance achieved for the [BERT](https://arxiv.org/abs/1810.04805)-base model with small and large-scale inputs:\n\n

\n \n

\n\n

\nFigure 1: PyTorch 1.12 Improvements with BetterTransformer fastpath execution\n

", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "Currently, the BetterTransformer speedup only applies to transformer encoder models used in inference. To benefit from fastpath execution, models must be composed of any of the following components: [TransformerEncoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html), [TransformerEncoderLayer](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html) or [MultiheadAttention](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) (MHA). Fastpath execution is also subject to some criteria. Most importantly, the model must be executed in inference mode and operate on input tensors that do not collect gradient tape information (e.g., running with torch.no_grad). The full list of conditions can be found at these links for [nn.MultiHeadAttention](https://github.com/pytorch/pytorch/blob/29189d2ba8e583b2355cd0e9517a1ee742ba12cf/torch/nn/modules/activation.py#L1060) and [nn.TransformerEncoder](https://github.com/pytorch/pytorch/blob/29189d2ba8e583b2355cd0e9517a1ee742ba12cf/torch/nn/modules/transformer.py#L206), respectively. If the criteria are not met, control flows to the legacy PyTorch 1.11 Transformer implementation which has the same API, but lacks the fastpath performance boost.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "Other transformer models (such as decoder models) which use the PyTorch MultiheadAttention module will benefit from the BetterTransformer fastpath. Planned future work is to expand the end-to-end BetterTransformer fastpath to models based on [TransformerDecoder](https://pytorch.org/docs/stable/generated/torch.nn.TransformerDecoder.html) to support popular seq2seq and decoder-only (e.g., [OPT](https://ai.facebook.com/blog/democratizing-access-to-large-scale-language-models-with-opt-175b/)) model architectures, and to training.\n\n## Speedups\n\nThe following graphs show the performance achieved for the [BERT](https://arxiv.org/abs/1810.04805)-base model with small and large-scale inputs:\n\n

\n \n

\n\n

\nFigure 1: PyTorch 1.12 Improvements with BetterTransformer fastpath execution\n

", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} {"page_content": "

\n \n

\n\n

\nFigure 2: PyTorch 1.12 Improvements with BetterTransformer fastpath execution
\nwith sparsity optimization enabled by enable_nested_tensor=True
\n

", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "BetterTransformer includes two types of optimization: (1) fused kernels implementing multiple operations more efficiently in a single kernel, and (2) exploiting sparsity by avoiding unnecessary processing on padding tokens. Enhanced performance for small input sizes benefits primarily from the fused kernel implementations, and shows a constant performance improvement regardless of padding amount. While large inputs still benefit from fused kernels, the computation heavy processing limits the benefits that", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "may be obtained by the fused kernels as baseline performance is already closer to the theoretical peak. However, as we increase the amount of padding, performance increases dramatically as increasingly large amounts of computation can be avoided by exploiting the sparsity introduced by padding in NLP workloads.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Future Work\n\nAs part of our ongoing work on PyTorch BetterTransformer, we are working on extending BetterTransformer improvements to Transformer Decoders. We aim to expand beyond inference to training as well.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "We are partnering to enable BetterTransformer on additional libraries such as FairSeq, MetaSeq, and HuggingFace to benefit all Transformer-based PyTorch models. We\u2019ll provide future updates on the progress of BetterTransformer accelerations for the larger PyTorch ecosystem as part of this blog series.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements: The authors would like to thank Lin Qiao, Ajit Mathews, Andrew Tulloch, Dmytro Dzhulgakov, Natalia Gimelshein, Emad El-Haraty, Mark Saroufim, Adnan Aziz, Geeta Chauhan, and Hamid Shojanazeri for their support, contributions and many helpful suggestions throughout the course of this project, and in the preparation of this blog.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Experience the power of PyTorch 2.0 on AMD Solutions\"\nauthor: AMD\n---", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 represents a significant step forward for the PyTorch machine learning framework. The stable release of PyTorch 2.0 brings new features that unlock even higher performance, while remaining backward compatible with prior releases and retaining the Pythonic focus which has helped to make PyTorch so enthusiastically adopted by the AI/ML community. AMD has long been a strong proponent of PyTorch, and we are delighted that the PyTorch 2.0 stable release includes support for AMD Instinct\u2122 and Radeon\u2122", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "GPUs that are supported by the ROCm\u2122 software platform.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "With the stable PyTorch 2.0 release, PyTorch 2.0 introduces torch.compile as a beta feature underpinned by TorchInductor with support for AMD Instinct and Radeon GPUs through OpenAI Triton deep learning compiler. Through TorchInductor, developers can now generate low level kernels using Triton that are portable and performant to hand-written kernels on native hardware centric kernel programming models.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "OpenAI Triton is a language and compiler for blocked algorithms, which aims to provide an abstraction layer between CUDA/HIP and Torch at which developers can write efficient kernels more productively. We have written a new backend which interfaces Triton's custom MLIR dialects with our ROCm compiler stack.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Triton can automatically optimize kernels generated by machine learning compilers such as TorchInductor for multiple AI accelerators including AMD Instinct GPU accelerator by leveraging hardware-specific features of the AMD CDNA\u2122 GPU architecture. This makes it easy for developers and users to switch seamlessly from any HW to AMD Instinct GPU accelerators and get great out of the box performance.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "In addition, compilers like Triton can also enable developers to use high-level programming languages, such as Python, to write machine learning code that can be efficiently compiled and executed on specialized hardware. This can help greatly improve the productivity of machine learning developers, as they can focus on the algorithmic aspects of their models and rely on the compiler to generate efficient code.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "By design, PyTorch 2.0 is backward compatible to earlier PyTorch releases. This holds true for the ROCm build of PyTorch 2.0 as well. Developers using PyTorch with AMD GPUs can migrate to PyTorch 2.0 with the confidence that their existing code will continue to work without any required changes, so there is no penalty to access the improvements that come with this release. On the other hand, using PyTorch 2.0 and TorchInductor can result in significant performance improvement over the default eager-mode as", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "shown below.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The initial results using AMD Instinct MI250 GPUs already shows strong performance improvement with minimal optimization on TorchInductor compared to the default eager-mode. We see an average performance increase of up to 1.54X on 44 out of the 45 models on HuggingFace benchmarks suite with CamemBert, DistillGPT2 and T5Small being a few of the standout models with up to 1.5X or more performance improvement over eager-mode. We are looking forward to continued engagement with members of the PyTorch team at", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Meta to enable further optimization on ROCm software stack and the additional performance improvement for future PyTorch releases.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "![Image 1: AMD MI250 GPU performance improvement for TorchInductor vs eager-mode using HuggingFace](/assets/images/t-vs-eager-mode.svg){:style=\"max-height:800px; width:100%\"} \n\nImage 1: AMD MI250 GPU performance improvement for TorchInductor vs eager-mode using HuggingFace MI200-89.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 follows the same set of install options as before to build and install for supporting AMD GPUs. These include an installable Python package hosted at [pytorch.org](https://pytorch.org/), AMD\u2019s public PyTorch docker image, and of course the option to build from source using the upstream PyTorch repository. As with PyTorch builds for other platforms, the specific command line to be run for pip-based install is provided by the configurator at", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/).", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The GPUs supported by the ROCm software platform which forms the basis for PyTorch support on AMD GPUs are documented at [https://docs.amd.com/bundle/Hardware_and_Software_Reference_Guide/page/Hardware_and_Software_Support.html](https://docs.amd.com/bundle/Hardware_and_Software_Reference_Guide/page/Hardware_and_Software_Support.html)", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Conclusion", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 represents a major step in continuing to broaden support for ML developers by increasing performance while maintaining a simple, Pythonic interface. This performance uplift is made possible in large part by the new TorchInductor infrastructure, which in turn harnesses the Triton ML programming language and just-in-time compiler. AMD\u2019s support for these technologies allows users to realize the full promise of the new PyTorch architecture. Our GPU support in PyTorch 2.0 is just one manifestation", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "of a larger vision around AI and machine learning. AI/ML plays an important role in multiple AMD product lines, including Instinct and Radeon GPUs, Alveo\u2122 data center accelerators, and both Ryzen\u2122 and EPYC processors. These hardware and software initiatives are all part of AMD\u2019s Pervasive AI vision, and we look forward to addressing the many new challenges and opportunities of this dynamic space.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "MI200-89 \u2013 PyTorch Inductor mode HuggingFace Transformers training speedup, running the standard PyTorch 2.0 test suite, over PyTorch eager-mode comparison based on AMD internal testing on a single GCD as of 3/10/2023 using a 2P AMD EPYC\u2122 7763 production server with 4x AMD Instinct\u2122 MI250 (128GB HBM2e) 560W GPUs with Infinity Fabric\u2122 technology; host ROCm\u2122 5.3, guest ROCm\u2122 5.4.4, PyTorch 2.0.0, Triton 2.0. Server manufacturers may vary configurations, yielding different results. Performance may vary based", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "on factors including use of latest drivers and optimizations.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "\u00a9 2023 Advanced Micro Devices, Inc. All rights reserved. AMD, the AMD Arrow logo, AMD CDNA, AMD Instinct, EPYC, Radeon, ROCm, Ryzen, and combinations thereof are trademarks of Advanced Micro Devices, Inc. Other product names used in this publication are for identification purposes only and may be trademarks of their respective owners.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Developer Day 2021'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/ptdevday21.gif'\n---\n\nWe are excited to announce PyTorch Developer Day (#PTD2), taking place virtually from December 1 & 2, 2021. Developer Day is designed for developers and users to discuss core technical developments, ideas, and roadmaps. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "Event Details \n**Technical Talks Live Stream - December 1, 2021**\n\nJoin us for technical talks on a variety of topics, including updates to the core framework, new tools and libraries to support development across a variety of domains, responsible AI and industry use cases. All talks will take place on December 1 and will be live streamed on PyTorch channels.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "Stay up to date by following us on our social channels: [Twitter](https://twitter.com/PyTorch), [Facebook](https://facebook.com/PyTorch), or [LinkedIn](https://www.linkedin.com/company/pytorch).\n\n**Poster Exhibition & Networking - December 2, 2021**", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "On the second day, we\u2019ll be hosting an online poster exhibition on Gather.Town. There will be opportunities to meet the authors and learn more about their PyTorch projects as well as network with the community. This poster and networking event is limited to people composed of PyTorch maintainers and contributors, long-time stakeholders and experts in areas relevant to PyTorch\u2019s future. Conversations from the networking event will strongly shape the future of PyTorch. As such, invitations are required to", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "attend the networking event.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "Apply for an invitation to the networking event by clicking [here](https://pytorchdeveloperday.fbreg.com/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "Call for Content Now Open\n\nSubmit your poster abstracts today! Please send us the title and brief summary of your project, tools and libraries that could benefit PyTorch researchers in academia and industry, application developers, and ML engineers for consideration. The focus must be on academic papers, machine learning research, or open-source projects related to PyTorch development, Responsible AI or Mobile. Please no sales pitches. **Deadline for submission is September 24, 2021**.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "You can submit your poster abstract during your application & registration process [here](https://pytorchdeveloperday.fbreg.com/apply).\n\nVisit the [event website](https://pytorchdeveloperday.fbreg.com/) for more information and we look forward to having you at PyTorch Developer Day. For any questions about the event, contact [pytorch@fbreg.com](mailto:pytorch@fbreg.com).", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Efficient PyTorch: Tensor Memory Format Matters'\nauthor: 'Dhruv Matani, Suraj Subramanian'\nfeatured-img: ''\n---\n\nEnsuring the right memory format for your inputs can significantly impact the running time of your PyTorch vision models. When in doubt, choose a Channels Last memory format.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "When dealing with vision models in PyTorch that accept multimedia (for example image Tensorts) as input, the Tensor\u2019s memory format can significantly impact **the inference execution speed of your model on mobile platforms when using the CPU backend along with XNNPACK**. This holds true for training and inference on server platforms as well, but latency is particularly critical for mobile devices and users.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Outline of this article\n1. Deep Dive into matrix storage/memory representation in C++. Introduction to [Row and Column major order](https://en.wikipedia.org/wiki/Row-_and_column-major_order).\n2. Impact of looping over a matrix in the same or different order as the storage representation, along with an example.\n3. Introduction to Cachegrind; a tool to inspect the cache friendliness of your code.\n4. Memory formats supported by PyTorch Operators.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "5. Best practices example to ensure efficient model execution with XNNPACK optimizations", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Matrix Storage Representation in C++\n\nImages are fed into PyTorch ML models as multi-dimensional Tensors. These Tensors have specific memory formats. To understand this concept better, let\u2019s take a look at how a 2-d matrix may be stored in memory.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Broadly speaking, there are 2 main ways of efficiently storing multi-dimensional data in memory.\n1. **Row Major Order:** In this format, the matrix is stored in row order, with each row stored before the next row in memory. I.e. row N comes before row N+1.\n2. **Column Major Order:** In this format, the matrix is stored in column-order, with each column stored before the next column in memory. I.e. column N comes before column N+1.\n\nYou can see the differences graphically below.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "

\n\"C++\n
\nC++ stores multi-dimensional data in row-major format.\n

", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Efficiently accessing elements of a 2d matrix\n\nSimilar to the storage format, there are 2 ways to access data in a 2d matrix.\n\n1. **Loop Over Rows first:** All elements of a row are processed before any element of the next row.\n2. **Loop Over Columns first:** All elements of a column are processed before any element of the next column.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "For maximum efficiency, one should always access data in the same format in which it is stored. I.e. if the data is stored in row-major order, then one should try to access it in that order.\n\nThe code below (main.cpp) shows [2 ways](https://stackoverflow.com/questions/9936132/why-does-the-order-of-the-loops-affect-performance-when-iterating-over-a-2d-arra) of accessing all the elements of a 2d 4000x4000 matrix.\n\n```python\n#include \n#include ", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "// loop1 accesses data in matrix 'a' in row major order,\n// since i is the outer loop variable, and j is the\n// inner loop variable.\nint loop1(int a[4000][4000]) {\n int s = 0;\n for (int i = 0; i < 4000; ++i) {\n for (int j = 0; j < 4000; ++j) {\n s += a[i][j];\n }\n }\n return s;\n}", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "// loop2 accesses data in matrix 'a' in column major order\n// since j is the outer loop variable, and i is the\n// inner loop variable.\nint loop2(int a[4000][4000]) {\n int s = 0;\n for (int j = 0; j < 4000; ++j) {\n for (int i = 0; i < 4000; ++i) {\n s += a[i][j];\n }\n }\n return s;\n}\n\nint main() {\n static int a[4000][4000] = {0};\n for (int i = 0; i < 100; ++i) {\n int x = rand() % 4000;\n int y = rand() % 4000;\n a[x][y] = rand() % 1000;\n }", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "auto start = std::chrono::high_resolution_clock::now();\n auto end = start;\n int s = 0;\n\n#if defined RUN_LOOP1\n start = std::chrono::high_resolution_clock::now();\n\n s = 0;\n for (int i = 0; i < 10; ++i) {\n s += loop1(a);\n s = s % 100;\n }\n end = std::chrono::high_resolution_clock::now();\n\n std::cout << \"s = \" << s << std::endl;\n std::cout << \"Time for loop1: \"\n << std::chrono::duration(end - start).count()\n << \"ms\" << std::endl;\n#endif", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "#if defined RUN_LOOP2\n start = std::chrono::high_resolution_clock::now();\n s = 0;\n for (int i = 0; i < 10; ++i) {\n s += loop2(a);\n s = s % 100;\n }\n end = std::chrono::high_resolution_clock::now();\n\n std::cout << \"s = \" << s << std::endl;\n std::cout << \"Time for loop2: \"\n << std::chrono::duration(end - start).count()\n << \"ms\" << std::endl;\n#endif\n}\n\n\nLet\u2019s build and run this program and see what it prints.\n\ng++ -O2 main.cpp -DRUN_LOOP1 -DRUN_LOOP2\n./a.out\n\n\nPrints the following:", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "s = 70\nTime for loop1: 77.0687ms\ns = 70\nTime for loop2: 1219.49ms", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "loop1() is **15x faster** than loop2(). Why is that? Let\u2019s find out below!", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Measure cache misses using Cachegrind\n\n[Cachegrind](https://courses.cs.washington.edu/courses/cse326/05wi/valgrind-doc/cg_main.html) is a cache profiling tool used to see how many I1 (first level instruction), D1 (first level data), and LL (last level) cache misses your program caused.\n\nLet\u2019s build our program with just loop1() and just loop2() to see how cache friendly each of these functions is.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Build and run/profile just loop1()\n\n```python\ng++ -O2 main.cpp -DRUN_LOOP1\nvalgrind --tool=cachegrind ./a.out\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Prints:", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "```python\n==3299700==\n==3299700== I refs: 643,156,721\n==3299700== I1 misses: 2,077\n==3299700== LLi misses: 2,021\n==3299700== I1 miss rate: 0.00%\n==3299700== LLi miss rate: 0.00%\n==3299700==\n==3299700== D refs: 160,952,192 (160,695,444 rd + 256,748 wr)\n==3299700== D1 misses: 10,021,300 ( 10,018,723 rd + 2,577 wr)\n==3299700== LLd misses: 10,010,916 ( 10,009,147 rd + 1,769 wr)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "==3299700== D1 miss rate: 6.2% ( 6.2% + 1.0% )\n==3299700== LLd miss rate: 6.2% ( 6.2% + 0.7% )\n==3299700==\n==3299700== LL refs: 10,023,377 ( 10,020,800 rd + 2,577 wr)\n==3299700== LL misses: 10,012,937 ( 10,011,168 rd + 1,769 wr)\n==3299700== LL miss rate: 1.2% ( 1.2% + 0.7% )\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Build and run/profile just loop2()\n\n\n```python\ng++ -O2 main.cpp -DRUN_LOOP2\nvalgrind --tool=cachegrind ./a.out\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Prints:", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "```python\n==3300389==\n==3300389== I refs: 643,156,726\n==3300389== I1 misses: 2,075\n==3300389== LLi misses: 2,018\n==3300389== I1 miss rate: 0.00%\n==3300389== LLi miss rate: 0.00%\n==3300389==\n==3300389== D refs: 160,952,196 (160,695,447 rd + 256,749 wr)\n==3300389== D1 misses: 160,021,290 (160,018,713 rd + 2,577 wr)\n==3300389== LLd misses: 10,014,907 ( 10,013,138 rd + 1,769 wr)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "==3300389== D1 miss rate: 99.4% ( 99.6% + 1.0% )\n==3300389== LLd miss rate: 6.2% ( 6.2% + 0.7% )\n==3300389==\n==3300389== LL refs: 160,023,365 (160,020,788 rd + 2,577 wr)\n==3300389== LL misses: 10,016,925 ( 10,015,156 rd + 1,769 wr)\n==3300389== LL miss rate: 1.2% ( 1.2% + 0.7% )", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "The main differences between the 2 runs are:\n1. **D1 misses:** 10M v/s 160M\n2. **D1 miss rate:** 6.2% v/s 99.4%\n\nAs you can see, `loop2()` causes many many more (**~16x more**) L1 data cache misses than loop1(). This is why `loop1()` is ~15x faster than loop2().", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Memory Formats supported by PyTorch Operators\n\nWhile PyTorch operators expect all tensors to be in [Channels First (NCHW) dimension format](https://discuss.pytorch.org/t/why-does-pytorch-prefer-using-nchw/83637/4), PyTorch operators support 3 output [memory formats](https://github.com/pytorch/pytorch/blob/master/c10/core/MemoryFormat.h).", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "1. **Contiguous:** Tensor memory is in the same order as the tensor\u2019s dimensions.\n2. **ChannelsLast:** Irrespective of the dimension order, the 2d (image) tensor is laid out as an HWC or [NHWC](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html) (N: batch, H: height, W: width, C: channels) tensor in memory. The dimensions could be permuted in any order.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "3. **ChannelsLast3d:** For 3d tensors (video tensors), the memory is laid out in THWC (Time, Height, Width, Channels) or NTHWC (N: batch, T: time, H: height, W: width, C: channels) format. The dimensions could be permuted in any order.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "The reason that ChannelsLast is preferred for vision models is because [XNNPACK](https://github.com/google/XNNPACK) (kernel acceleration library) used by PyTorch expects all inputs to be in **Channels Last** format, so if the input to the model isn\u2019t channels last, then it must first be converted to channels last, which is an additional operation.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Additionally, most PyTorch operators preserve the input tensor\u2019s memory format, so if the input is Channels First, then the operator needs to first convert to Channels Last, then perform the operation, and then convert back to Channels First.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "When you combine it with the fact that accelerated operators work better with a channels last memory format, you\u2019ll notice that having the operator return back a channels-last memory format is better for subsequent operator calls or you\u2019ll end up having every operator convert to channels-last (should it be more efficient for that specific operator).\n\nFrom the XNNPACK home page:\n\n> \u201cAll operators in XNNPACK support NHWC layout, but additionally allow custom stride along the Channel dimension\".", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Best Practice\n\nThe best way to get the most performance from your PyTorch vision models is to ensure that your input tensor is in a **Channels Last** [memory format](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html) before it is fed into the model.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "You can get even more speedups by optimizing your model to use the XNNPACK backend (by simply calling `optimize_for_mobile()` on your torchscripted model). Note that XNNPACK models will run slower if the inputs are contiguous, so definitely make sure it is in Channels-Last format.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Working example showing speedup\n\nRun this example on [Google Colab](https://colab.research.google.com/gist/suraj813/ad9aebcbffbdd6d02b23ca7231130a30/channels-last-with-xnnpack.ipynb#scrollTo=xvJN73YWXgDF) - note that runtimes on colab CPUs might not reflect accurate performance; it is recommended to run this code on your local machine.\n\n```python\nimport torch\nfrom torch.utils.mobile_optimizer import optimize_for_mobile\nimport torch.backends.xnnpack\nimport time", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "print(\"XNNPACK is enabled: \", torch.backends.xnnpack.enabled, \"\\n\")\n\nN, C, H, W = 1, 3, 200, 200\nx = torch.rand(N, C, H, W)\nprint(\"Contiguous shape: \", x.shape)\nprint(\"Contiguous stride: \", x.stride())\nprint()\n\nxcl = x.to(memory_format=torch.channels_last)\nprint(\"Channels-Last shape: \", xcl.shape)\nprint(\"Channels-Last stride: \", xcl.stride())", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Outputs:\n \n# XNNPACK is enabled: True\n \n# Contiguous shape: torch.Size([1, 3, 200, 200])\n# Contiguous stride: (120000, 40000, 200, 1)\n \n# Channels-Last shape: torch.Size([1, 3, 200, 200])\n# Channels-Last stride: (120000, 1, 600, 3)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "The input shape stays the same for contiguous and channels-last formats. Internally however, the tensor's layout has changed as you can see in the strides. Now, the number of jumps required to go across channels is only 1 (instead of 40000 in the contiguous tensor).\nThis better data locality means convolution layers can access all the channels for a given pixel much faster. Let's see now how the memory format affects runtime:\n\n```python\nfrom torchvision.models import resnet34, resnet50, resnet101", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "m = resnet34(pretrained=False)\n# m = resnet50(pretrained=False)\n# m = resnet101(pretrained=False)\n\ndef get_optimized_model(mm):\n mm = mm.eval()\n scripted = torch.jit.script(mm)\n optimized = optimize_for_mobile(scripted) # explicitly call the xnnpack rewrite \n return scripted, optimized\n\n\ndef compare_contiguous_CL(mm):\n # inference on contiguous\n start = time.perf_counter()\n for i in range(20):\n mm(x)\n end = time.perf_counter()\n print(\"Contiguous: \", end-start)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "# inference on channels-last\n start = time.perf_counter()\n for i in range(20):\n mm(xcl)\n end = time.perf_counter()\n print(\"Channels-Last: \", end-start)\n\nwith torch.inference_mode():\n scripted, optimized = get_optimized_model(m)\n\n print(\"Runtimes for torchscripted model: \")\n compare_contiguous_CL(scripted.eval())\n print()\n print(\"Runtimes for mobile-optimized model: \")\n compare_contiguous_CL(optimized.eval())", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "Outputs (on an Intel Core i9 CPU):\n \n# Runtimes for torchscripted model:\n# Contiguous: 1.6711160129999598\n# Channels-Last: 1.6678222839999535\n \n# Runtimes for mobile-optimized model:\n# Contiguous: 0.5712863490000473\n# Channels-Last: 0.46113000699995155\n\n```\n\n## Conclusion\n\nThe Memory Layout of an input tensor can significantly impact a model\u2019s running time. For Vision Models, prefer a **Channels Last** memory format to get the most out of your PyTorch models.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "References", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "- [Row/Column Major matrix storage order](https://en.wikipedia.org/wiki/Row-_and_column-major_order)\n- [Loop order impact on performance](https://stackoverflow.com/questions/9936132/why-does-the-order-of-the-loops-affect-performance-when-iterating-over-a-2d-arra)\n- [Cachegrind: a cache-miss profiler](https://courses.cs.washington.edu/courses/cse326/05wi/valgrind-doc/cg_main.html)\n- [NHWC format explained](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "- [Why does PyTorch prefer NCHW?](https://discuss.pytorch.org/t/why-does-pytorch-prefer-using-nchw/83637/4)\n- [XNNPACK](https://github.com/google/XNNPACK)\n- [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html)\n- [Supported operators](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch framework for cryptographically secure random number generation, torchcsprng, now available'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "One of the key components of modern cryptography is the pseudorandom number generator. Katz and Lindell stated, \"The use of badly designed or inappropriate random number generators can often leave a good cryptosystem vulnerable to attack. Particular care must be taken to use a random number generator that is designed for cryptographic use, rather than a 'general-purpose' random number generator which may be fine for some applications but not ones that are required to be cryptographically secure.\"[1]", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Additionally, most pseudorandom number generators scale poorly to massively parallel high-performance computation because of their sequential nature. Others don\u2019t satisfy cryptographically secure properties.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "[torchcsprng](https://github.com/pytorch/csprng) is a PyTorch [C++/CUDA extension](https://pytorch.org/tutorials/advanced/cpp_extension.html) that provides [cryptographically secure pseudorandom number generators](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) for PyTorch.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "torchcsprng overview", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Historically, PyTorch had only two pseudorandom number generator implementations: Mersenne Twister for CPU and Nvidia\u2019s cuRAND Philox for CUDA. Despite good performance properties, neither of them are suitable for cryptographic applications. Over the course of the past several months, the PyTorch team developed the torchcsprng extension API. Based on PyTorch dispatch mechanism and operator registration, it allows the users to extend c10::GeneratorImpl and implement their own custom pseudorandom number", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "generator.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "torchcsprng generates a random 128-bit key on the CPU using one of its generators and then runs AES128 in [CTR mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) either on CPU or GPU using CUDA. This then generates a random 128-bit state and applies a transformation function to map it to target tensor values. This approach is based on [Parallel Random Numbers: As Easy as 1, 2, 3 (John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, D. E. Shaw", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Research)](http://www.thesalmons.org/john/random123/papers/random123sc11.pdf). It makes torchcsprng both crypto-secure and parallel on both CPU and CUDA.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nSince torchcsprng is a PyTorch extension, it is available on the platforms where PyTorch is available (support for Windows-CUDA will be available in the coming months).", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Using torchcsprng\n\nThe torchcsprng API is very simple to use and is fully compatible with the PyTorch random infrastructure:\n\n**Step 1: Install via binary distribution**\n\nAnaconda:\n\n```python\nconda install torchcsprng -c pytorch\n ```\n\npip:\n\n```python\npip install torchcsprng\n ```\n\n**Step 2: import packages as usual but add csprng**\n\n```python\nimport torch\nimport torchcsprng as csprng", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "**Step 3: Create a cryptographically secure pseudorandom number generator from /dev/urandom:**\n\n```python\nurandom_gen = csprng.create_random_device_generator('/dev/urandom')\n ```\n \nand simply use it with the existing PyTorch methods:\n\n```python\ntorch.randn(10, device='cpu', generator=urandom_gen)\n ```\n\n**Step 4: Test with Cuda**\n\nOne of the advantages of torchcsprng generators is that they can be used with both CPU and CUDA tensors:\n\n```python\ntorch.randn(10, device='cuda', generator=urandom_gen)", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Another advantage of torchcsprng generators is that they are parallel on CPU unlike the default PyTorch CPU generator.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "Getting Started\n\nThe easiest way to get started with torchcsprng is by visiting the [GitHub page](https://github.com/pytorch/csprng) where you can find installation and build instructions, and more how-to examples. \n\nCheers,\n\nThe PyTorch Team\n\n[1] [Introduction to Modern Cryptography: Principles and Protocols (Chapman & Hall/CRC Cryptography and Network Security Series)](https://www.amazon.com/Introduction-Modern-Cryptography-Principles-Protocols/dp/1584885513) by Jonathan Katz and Yehuda Lindell", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Optimizing Production PyTorch Models\u2019 Performance with Graph Transformations\"\nauthor: Jade Nie, CK Luk, Xiaodong Wang, Jackie (Jiaqi) Xu\nfeatured-img: \"assets/images/blog1-3b.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "1. Introduction", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "PyTorch supports two execution modes [1]: eager mode and graph mode. In eager mode, operators in a model are immediately executed as they are encountered. In contrast, in graph mode, operators are first synthesized into a graph, which will then be compiled and executed as a whole. Eager mode is easier to use, more suitable for ML researchers, and hence is the default mode of execution. On the other hand, graph mode typically delivers higher performance and hence is heavily used in production.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "Specifically, graph mode enables operator fusion [2], wherein one operator is merged with another to reduce/localize memory reads as well as total kernel launch overhead. Fusion can be horizontal\u2014taking a single operation (e.g., BatchNorm) that is independently applied to many operands and merging those operands into an array; and vertical\u2014merging a kernel with another kernel that consumes the output of the first kernel (e.g., Convolution followed by ReLU).", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "Torch.FX [3, 4] (abbreviated as FX) is a publicly available toolkit as part of the PyTorch package that supports graph mode execution. In particular, it (1) captures the graph from a PyTorch program and (2) allows developers to write transformations on the captured graph. It is used inside Meta to optimize the training throughput of production models. By introducing a number of FX-based optimizations developed at Meta, we demonstrate the approach of using graph transformation to optimize PyTorch\u2019s", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "performance for production.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "2. Background\n\nEmbedding tables are ubiquitous in recommendation systems. Section 3 will discuss three FX transformations that optimize accesses to embedding tables. In this section, we provide some background on FX (Section 2.1) and embedding tables (Section 2.2).", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "2.1 FX\n\nFigure 1 is a simple example adopted from [3] which illustrates using FX to transform a PyTorch program. It contains three steps: (1) capturing the graph from a program, (2) modifying the graph (in this example, all uses of RELU are replaced by GELU), and (3) generating a new program from the modified graph.\n\n

\n\n

\n\n**Figure 1: A FX example which replaces all uses of RELU by GELU in a PyTorch module.**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "The FX API [4] provides many more functionalities for inspecting and transforming PyTorch program graphs.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "2.2 Embedding Tables\n\n

\n\n

\n\n**Figure 2: Illustration of an embedding table for a sparse feature with batch size = 1**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "In a recommendation system, sparse features (e.g., User ID, Story ID) are represented by embedding tables. An embedding table E is an HxD matrix, where H is the hash size, D is the embedding dimension. Each row of E is a vector of floats. Feature hashing [5] is used to map a sparse feature to a list of indices to E, say [S1,S2, \u2026, Sk], where 0<=Si<H. Its output value is computed as f(E[S1], E[S2], \u2026, E[Sk]), where", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "E[Si] is the vector at row Si, and f is called the pooling function, which is typically one of the following functions: sum, average, maximum. See Figure 2 for an illustration.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "To fully utilize the GPU, sparse features are usually processed in a batch. Each entity in a batch has its own list of indices. If a batch has B entities, a naive representation has B lists of indices. A more compact representation is to combine the B lists of indices into a single list of indices and add a list of the lengths of indices (one length for each entity in the batch). For example, if a batch has 3 entities whose lists of indices are as follows:", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "- Entity 1: indices = [10, 20]\n- Entity 2: indices = [5, 9, 77, 81]\n- Entity 3: indices = [15, 20, 45]\n\nThen the indices and lengths for the entire batch will be:\n\n- Indices = [10, 20, 5, 9, 77, 81, 15, 20, 45]\n- Lengths = [2, 4, 3]\n\nAnd the output of the embedding table lookup for the whole batch is a BxD matrix.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "3. Three FX Transformations\n\nWe have developed three FX transformations that accelerate accesses to embedding tables. Section 3.1 discusses a transformation that combines multiple small input tensors into a single big tensor; Section 3.2 a transformation that fuses multiple, parallel compute chains into a single compute chain; and Section 3.3 a transformation that overlaps communication with computation.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "3.1 Combining Input Sparse Features", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "Recall that an input sparse feature in a batch is represented by two lists: a list of indices and a list of B lengths, where B is the batch size. In PyTorch, these two lists are implemented as two tensors. When a PyTorch model is run on a GPU, embedding tables are commonly stored in the GPU memory (which is closer to the GPU and has much higher read/write bandwidth than the CPU memory). To use an input sparse feature, its two tensors need to be first copied from CPU to GPU. Nevertheless, per host-to-device", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "memory copying requires a kernel launch, which is relatively expensive compared to the actual data transfer time. If a model uses many input sparse features, this copying could become a performance bottleneck (e.g., 1000 input sparse features would require copying 2000 tensors from host to device).", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "An optimization that reduces the number of host-to-device memcpy is to combine multiple input sparse features before sending them to the device. For instance, given the following three input features:\n\n- Feature_A: indices = [106, 211, 7], lengths = [2, 1]\n- Feature_B: indices = [52, 498, 616, 870, 1013], lengths = [3, 2]\n- Feature_C: indices = [2011, 19, 351, 790], lengths = [1, 3]\n\nThe combined form is:", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "- Features_A_B_C: indices = [106, 211, 7, 52, 498, 616, 870, 1013, 2011, 19, 351, 790], lengths = [2, 1, 3, 2, 1, 3]\n\nSo, instead of copying 3x2=6 tensors from host to device, we only need to copy 2 tensors.\n\nFigure 3(b) describes an implementation of this optimization, which has two components:", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "- On the CPU side: The input pipeline is modified to combine all the indices of sparse features into a single tensor and similarly all the lengths into another tensor. Then the two tensors are copied to the GPU.\n- On the GPU side: Using FX, we insert a Permute_and_Split op into the model graph to recover the indices and lengths tensors of individual features from the combined tensors, and route them to the corresponding nodes downstream.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n(a). **Without the optimization**\n\n

\n\n

\n\n(b). **With the optimization**\n\n**Figure 3: Combining input sparse features**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "3.2 Horizontal fusion of computation chains started with accesses to embedding tables", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "In a production model, it is fairly common to have 10s of embedding tables residing on each GPU. For performance reasons, lookups to these tables are grouped together so that their outputs are concatenated in a single big tensor (see the red part in Figure 4(a)). To apply computations to individual feature outputs, a Split op is used to divide the big tensors into N smaller tensors (where N is the number of features) and then the desired computations are applied to each tensor. This is shown in Figure 4(a),", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "where the computation applied to each feature output O is Tanh(LayerNorm(O)). All the computation results are concatenated back to a big tensor, which is then passed to downstream ops (Op1 in Figure 4(a)).", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "BetterTransformer includes two types of optimization: (1) fused kernels implementing multiple operations more efficiently in a single kernel, and (2) exploiting sparsity by avoiding unnecessary processing on padding tokens. Enhanced performance for small input sizes benefits primarily from the fused kernel implementations, and shows a constant performance improvement regardless of padding amount. While large inputs still benefit from fused kernels, the computation heavy processing limits the benefits that may be obtained by the fused kernels as baseline performance is already closer to the theoretical peak. However, as we increase the amount of padding, performance increases dramatically as increasingly large amounts of computation can be avoided by exploiting the sparsity introduced by padding in NLP workloads.\n\n## Future Work", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "## Future Work\n\nAs part of our ongoing work on PyTorch BetterTransformer, we are working on extending BetterTransformer improvements to Transformer Decoders. We aim to expand beyond inference to training as well.\n\nWe are partnering to enable BetterTransformer on additional libraries such as FairSeq, MetaSeq, and HuggingFace to benefit all Transformer-based PyTorch models. We\u2019ll provide future updates on the progress of BetterTransformer accelerations for the larger PyTorch ecosystem as part of this blog series.\n\nAcknowledgements: The authors would like to thank Lin Qiao, Ajit Mathews, Andrew Tulloch, Dmytro Dzhulgakov, Natalia Gimelshein, Emad El-Haraty, Mark Saroufim, Adnan Aziz, Geeta Chauhan, and Hamid Shojanazeri for their support, contributions and many helpful suggestions throughout the course of this project, and in the preparation of this blog.", "metadata": {"source": "https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Experience the power of PyTorch 2.0 on AMD Solutions\"\nauthor: AMD\n---\n\nPyTorch 2.0 represents a significant step forward for the PyTorch machine learning framework. The stable release of PyTorch 2.0 brings new features that unlock even higher performance, while remaining backward compatible with prior releases and retaining the Pythonic focus which has helped to make PyTorch so enthusiastically adopted by the AI/ML community. AMD has long been a strong proponent of PyTorch, and we are delighted that the PyTorch 2.0 stable release includes support for AMD Instinct\u2122 and Radeon\u2122 GPUs that are supported by the ROCm\u2122 software platform.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "With the stable PyTorch 2.0 release, PyTorch 2.0 introduces torch.compile as a beta feature underpinned by TorchInductor with support for AMD Instinct and Radeon GPUs through OpenAI Triton deep learning compiler. Through TorchInductor, developers can now generate low level kernels using Triton that are portable and performant to hand-written kernels on native hardware centric kernel programming models.\n\nOpenAI Triton is a language and compiler for blocked algorithms, which aims to provide an abstraction layer between CUDA/HIP and Torch at which developers can write efficient kernels more productively. We have written a new backend which interfaces Triton's custom MLIR dialects with our ROCm compiler stack.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "Triton can automatically optimize kernels generated by machine learning compilers such as TorchInductor for multiple AI accelerators including AMD Instinct GPU accelerator by leveraging hardware-specific features of the AMD CDNA\u2122 GPU architecture. This makes it easy for developers and users to switch seamlessly from any HW to AMD Instinct GPU accelerators and get great out of the box performance. \n\nIn addition, compilers like Triton can also enable developers to use high-level programming languages, such as Python, to write machine learning code that can be efficiently compiled and executed on specialized hardware. This can help greatly improve the productivity of machine learning developers, as they can focus on the algorithmic aspects of their models and rely on the compiler to generate efficient code.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "By design, PyTorch 2.0 is backward compatible to earlier PyTorch releases. This holds true for the ROCm build of PyTorch 2.0 as well. Developers using PyTorch with AMD GPUs can migrate to PyTorch 2.0 with the confidence that their existing code will continue to work without any required changes, so there is no penalty to access the improvements that come with this release. On the other hand, using PyTorch 2.0 and TorchInductor can result in significant performance improvement over the default eager-mode as shown below.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "The initial results using AMD Instinct MI250 GPUs already shows strong performance improvement with minimal optimization on TorchInductor compared to the default eager-mode. We see an average performance increase of up to 1.54X on 44 out of the 45 models on HuggingFace benchmarks suite with CamemBert, DistillGPT2 and T5Small being a few of the standout models with up to 1.5X or more performance improvement over eager-mode. We are looking forward to continued engagement with members of the PyTorch team at Meta to enable further optimization on ROCm software stack and the additional performance improvement for future PyTorch releases. \n\n![Image 1: AMD MI250 GPU performance improvement for TorchInductor vs eager-mode using HuggingFace](/assets/images/t-vs-eager-mode.svg){:style=\"max-height:800px; width:100%\"} \n\nImage 1: AMD MI250 GPU performance improvement for TorchInductor vs eager-mode using HuggingFace MI200-89.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 2.0 follows the same set of install options as before to build and install for supporting AMD GPUs. These include an installable Python package hosted at [pytorch.org](https://pytorch.org/), AMD\u2019s public PyTorch docker image, and of course the option to build from source using the upstream PyTorch repository. As with PyTorch builds for other platforms, the specific command line to be run for pip-based install is provided by the configurator at [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/).\n\nThe GPUs supported by the ROCm software platform which forms the basis for PyTorch support on AMD GPUs are documented at [https://docs.amd.com/bundle/Hardware_and_Software_Reference_Guide/page/Hardware_and_Software_Support.html](https://docs.amd.com/bundle/Hardware_and_Software_Reference_Guide/page/Hardware_and_Software_Support.html)\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nPyTorch 2.0 represents a major step in continuing to broaden support for ML developers by increasing performance while maintaining a simple, Pythonic interface. This performance uplift is made possible in large part by the new TorchInductor infrastructure, which in turn harnesses the Triton ML programming language and just-in-time compiler. AMD\u2019s support for these technologies allows users to realize the full promise of the new PyTorch architecture. Our GPU support in PyTorch 2.0 is just one manifestation of a larger vision around AI and machine learning. AI/ML plays an important role in multiple AMD product lines, including Instinct and Radeon GPUs, Alveo\u2122 data center accelerators, and both Ryzen\u2122 and EPYC processors. These hardware and software initiatives are all part of AMD\u2019s Pervasive AI vision, and we look forward to addressing the many new challenges and opportunities of this dynamic space.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "MI200-89 \u2013 PyTorch Inductor mode HuggingFace Transformers training speedup, running the standard PyTorch 2.0 test suite, over PyTorch eager-mode comparison based on AMD internal testing on a single GCD as of 3/10/2023 using a 2P AMD EPYC\u2122 7763 production server with 4x AMD Instinct\u2122 MI250 (128GB HBM2e) 560W GPUs with Infinity Fabric\u2122 technology; host ROCm\u2122 5.3, guest ROCm\u2122 5.4.4, PyTorch 2.0.0, Triton 2.0. Server manufacturers may vary configurations, yielding different results. Performance may vary based on factors including use of latest drivers and optimizations. \n\n\u00a9 2023 Advanced Micro Devices, Inc. All rights reserved. AMD, the AMD Arrow logo, AMD CDNA, AMD Instinct, EPYC, Radeon, ROCm, Ryzen, and combinations thereof are trademarks of Advanced Micro Devices, Inc. Other product names used in this publication are for identification purposes only and may be trademarks of their respective owners.", "metadata": {"source": "https://pytorch.org/blog/experience-power-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Developer Day 2021'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/ptdevday21.gif'\n---\n\nWe are excited to announce PyTorch Developer Day (#PTD2), taking place virtually from December 1 & 2, 2021. Developer Day is designed for developers and users to discuss core technical developments, ideas, and roadmaps. \n\n
\n \n
\n\n## Event Details \n**Technical Talks Live Stream - December 1, 2021**\n\nJoin us for technical talks on a variety of topics, including updates to the core framework, new tools and libraries to support development across a variety of domains, responsible AI and industry use cases. All talks will take place on December 1 and will be live streamed on PyTorch channels.", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} +{"page_content": "Stay up to date by following us on our social channels: [Twitter](https://twitter.com/PyTorch), [Facebook](https://facebook.com/PyTorch), or [LinkedIn](https://www.linkedin.com/company/pytorch).\n\n**Poster Exhibition & Networking - December 2, 2021**\n\nOn the second day, we\u2019ll be hosting an online poster exhibition on Gather.Town. There will be opportunities to meet the authors and learn more about their PyTorch projects as well as network with the community. This poster and networking event is limited to people composed of PyTorch maintainers and contributors, long-time stakeholders and experts in areas relevant to PyTorch\u2019s future. Conversations from the networking event will strongly shape the future of PyTorch. As such, invitations are required to attend the networking event. \n\nApply for an invitation to the networking event by clicking [here](https://pytorchdeveloperday.fbreg.com/).\n\n## Call for Content Now Open", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} +{"page_content": "Submit your poster abstracts today! Please send us the title and brief summary of your project, tools and libraries that could benefit PyTorch researchers in academia and industry, application developers, and ML engineers for consideration. The focus must be on academic papers, machine learning research, or open-source projects related to PyTorch development, Responsible AI or Mobile. Please no sales pitches. **Deadline for submission is September 24, 2021**. \n\nYou can submit your poster abstract during your application & registration process [here](https://pytorchdeveloperday.fbreg.com/apply).\n\nVisit the [event website](https://pytorchdeveloperday.fbreg.com/) for more information and we look forward to having you at PyTorch Developer Day. For any questions about the event, contact [pytorch@fbreg.com](mailto:pytorch@fbreg.com).", "metadata": {"source": "https://pytorch.org/blog/pytorch-developer-day-2021/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Efficient PyTorch: Tensor Memory Format Matters'\nauthor: 'Dhruv Matani, Suraj Subramanian'\nfeatured-img: ''\n---\n\nEnsuring the right memory format for your inputs can significantly impact the running time of your PyTorch vision models. When in doubt, choose a Channels Last memory format.\n\nWhen dealing with vision models in PyTorch that accept multimedia (for example image Tensorts) as input, the Tensor\u2019s memory format can significantly impact **the inference execution speed of your model on mobile platforms when using the CPU backend along with XNNPACK**. This holds true for training and inference on server platforms as well, but latency is particularly critical for mobile devices and users.\n\n", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "## Outline of this article\n1. Deep Dive into matrix storage/memory representation in C++. Introduction to [Row and Column major order](https://en.wikipedia.org/wiki/Row-_and_column-major_order).\n2. Impact of looping over a matrix in the same or different order as the storage representation, along with an example.\n3. Introduction to Cachegrind; a tool to inspect the cache friendliness of your code.\n4. Memory formats supported by PyTorch Operators.\n5. Best practices example to ensure efficient model execution with XNNPACK optimizations\n\n## Matrix Storage Representation in C++\n\nImages are fed into PyTorch ML models as multi-dimensional Tensors. These Tensors have specific memory formats. To understand this concept better, let\u2019s take a look at how a 2-d matrix may be stored in memory.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "Broadly speaking, there are 2 main ways of efficiently storing multi-dimensional data in memory.\n1. **Row Major Order:** In this format, the matrix is stored in row order, with each row stored before the next row in memory. I.e. row N comes before row N+1.\n2. **Column Major Order:** In this format, the matrix is stored in column-order, with each column stored before the next column in memory. I.e. column N comes before column N+1.\n\nYou can see the differences graphically below.\n\n

\n\"C++\n
\nC++ stores multi-dimensional data in row-major format.\n

\n\n## Efficiently accessing elements of a 2d matrix\n\nSimilar to the storage format, there are 2 ways to access data in a 2d matrix.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "1. **Loop Over Rows first:** All elements of a row are processed before any element of the next row.\n2. **Loop Over Columns first:** All elements of a column are processed before any element of the next column.\n\nFor maximum efficiency, one should always access data in the same format in which it is stored. I.e. if the data is stored in row-major order, then one should try to access it in that order.\n\nThe code below (main.cpp) shows [2 ways](https://stackoverflow.com/questions/9936132/why-does-the-order-of-the-loops-affect-performance-when-iterating-over-a-2d-arra) of accessing all the elements of a 2d 4000x4000 matrix.\n\n```python\n#include \n#include \n\n// loop1 accesses data in matrix 'a' in row major order,\n// since i is the outer loop variable, and j is the\n// inner loop variable.\nint loop1(int a[4000][4000]) {\n int s = 0;\n for (int i = 0; i < 4000; ++i) {\n for (int j = 0; j < 4000; ++j) {\n s += a[i][j];\n }\n }\n return s;\n}", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "// loop2 accesses data in matrix 'a' in column major order\n// since j is the outer loop variable, and i is the\n// inner loop variable.\nint loop2(int a[4000][4000]) {\n int s = 0;\n for (int j = 0; j < 4000; ++j) {\n for (int i = 0; i < 4000; ++i) {\n s += a[i][j];\n }\n }\n return s;\n}\n\nint main() {\n static int a[4000][4000] = {0};\n for (int i = 0; i < 100; ++i) {\n int x = rand() % 4000;\n int y = rand() % 4000;\n a[x][y] = rand() % 1000;\n }\n\n auto start = std::chrono::high_resolution_clock::now();\n auto end = start;\n int s = 0;\n\n#if defined RUN_LOOP1\n start = std::chrono::high_resolution_clock::now();\n\n s = 0;\n for (int i = 0; i < 10; ++i) {\n s += loop1(a);\n s = s % 100;\n }\n end = std::chrono::high_resolution_clock::now();\n\n std::cout << \"s = \" << s << std::endl;\n std::cout << \"Time for loop1: \"\n << std::chrono::duration(end - start).count()\n << \"ms\" << std::endl;\n#endif", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "#if defined RUN_LOOP2\n start = std::chrono::high_resolution_clock::now();\n s = 0;\n for (int i = 0; i < 10; ++i) {\n s += loop2(a);\n s = s % 100;\n }\n end = std::chrono::high_resolution_clock::now();\n\n std::cout << \"s = \" << s << std::endl;\n std::cout << \"Time for loop2: \"\n << std::chrono::duration(end - start).count()\n << \"ms\" << std::endl;\n#endif\n}\n\n\nLet\u2019s build and run this program and see what it prints.\n\ng++ -O2 main.cpp -DRUN_LOOP1 -DRUN_LOOP2\n./a.out\n\n\nPrints the following:\n\ns = 70\nTime for loop1: 77.0687ms\ns = 70\nTime for loop2: 1219.49ms\n```\n\nloop1() is **15x faster** than loop2(). Why is that? Let\u2019s find out below!\n\n## Measure cache misses using Cachegrind\n\n[Cachegrind](https://courses.cs.washington.edu/courses/cse326/05wi/valgrind-doc/cg_main.html) is a cache profiling tool used to see how many I1 (first level instruction), D1 (first level data), and LL (last level) cache misses your program caused.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "Let\u2019s build our program with just loop1() and just loop2() to see how cache friendly each of these functions is.\n\n### Build and run/profile just loop1()\n\n```python\ng++ -O2 main.cpp -DRUN_LOOP1\nvalgrind --tool=cachegrind ./a.out\n```\n\n#### Prints:", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "#### Prints:\n\n```python\n==3299700==\n==3299700== I refs: 643,156,721\n==3299700== I1 misses: 2,077\n==3299700== LLi misses: 2,021\n==3299700== I1 miss rate: 0.00%\n==3299700== LLi miss rate: 0.00%\n==3299700==\n==3299700== D refs: 160,952,192 (160,695,444 rd + 256,748 wr)\n==3299700== D1 misses: 10,021,300 ( 10,018,723 rd + 2,577 wr)\n==3299700== LLd misses: 10,010,916 ( 10,009,147 rd + 1,769 wr)\n==3299700== D1 miss rate: 6.2% ( 6.2% + 1.0% )\n==3299700== LLd miss rate: 6.2% ( 6.2% + 0.7% )\n==3299700==\n==3299700== LL refs: 10,023,377 ( 10,020,800 rd + 2,577 wr)\n==3299700== LL misses: 10,012,937 ( 10,011,168 rd + 1,769 wr)\n==3299700== LL miss rate: 1.2% ( 1.2% + 0.7% )\n```\n\n### Build and run/profile just loop2()\n\n\n```python\ng++ -O2 main.cpp -DRUN_LOOP2\nvalgrind --tool=cachegrind ./a.out\n```\n\n#### Prints:", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "#### Prints:\n\n```python\n==3300389==\n==3300389== I refs: 643,156,726\n==3300389== I1 misses: 2,075\n==3300389== LLi misses: 2,018\n==3300389== I1 miss rate: 0.00%\n==3300389== LLi miss rate: 0.00%\n==3300389==\n==3300389== D refs: 160,952,196 (160,695,447 rd + 256,749 wr)\n==3300389== D1 misses: 160,021,290 (160,018,713 rd + 2,577 wr)\n==3300389== LLd misses: 10,014,907 ( 10,013,138 rd + 1,769 wr)\n==3300389== D1 miss rate: 99.4% ( 99.6% + 1.0% )\n==3300389== LLd miss rate: 6.2% ( 6.2% + 0.7% )\n==3300389==\n==3300389== LL refs: 160,023,365 (160,020,788 rd + 2,577 wr)\n==3300389== LL misses: 10,016,925 ( 10,015,156 rd + 1,769 wr)\n==3300389== LL miss rate: 1.2% ( 1.2% + 0.7% )\n```\n\nThe main differences between the 2 runs are:\n1. **D1 misses:** 10M v/s 160M\n2. **D1 miss rate:** 6.2% v/s 99.4%", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "As you can see, `loop2()` causes many many more (**~16x more**) L1 data cache misses than loop1(). This is why `loop1()` is ~15x faster than loop2().\n\n## Memory Formats supported by PyTorch Operators\n\nWhile PyTorch operators expect all tensors to be in [Channels First (NCHW) dimension format](https://discuss.pytorch.org/t/why-does-pytorch-prefer-using-nchw/83637/4), PyTorch operators support 3 output [memory formats](https://github.com/pytorch/pytorch/blob/master/c10/core/MemoryFormat.h).", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "1. **Contiguous:** Tensor memory is in the same order as the tensor\u2019s dimensions.\n2. **ChannelsLast:** Irrespective of the dimension order, the 2d (image) tensor is laid out as an HWC or [NHWC](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html) (N: batch, H: height, W: width, C: channels) tensor in memory. The dimensions could be permuted in any order.\n3. **ChannelsLast3d:** For 3d tensors (video tensors), the memory is laid out in THWC (Time, Height, Width, Channels) or NTHWC (N: batch, T: time, H: height, W: width, C: channels) format. The dimensions could be permuted in any order.\n\nThe reason that ChannelsLast is preferred for vision models is because [XNNPACK](https://github.com/google/XNNPACK) (kernel acceleration library) used by PyTorch expects all inputs to be in **Channels Last** format, so if the input to the model isn\u2019t channels last, then it must first be converted to channels last, which is an additional operation.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "Additionally, most PyTorch operators preserve the input tensor\u2019s memory format, so if the input is Channels First, then the operator needs to first convert to Channels Last, then perform the operation, and then convert back to Channels First.\n\nWhen you combine it with the fact that accelerated operators work better with a channels last memory format, you\u2019ll notice that having the operator return back a channels-last memory format is better for subsequent operator calls or you\u2019ll end up having every operator convert to channels-last (should it be more efficient for that specific operator).\n\nFrom the XNNPACK home page:\n\n> \u201cAll operators in XNNPACK support NHWC layout, but additionally allow custom stride along the Channel dimension\".\n\n## PyTorch Best Practice\n\nThe best way to get the most performance from your PyTorch vision models is to ensure that your input tensor is in a **Channels Last** [memory format](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html) before it is fed into the model.", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "You can get even more speedups by optimizing your model to use the XNNPACK backend (by simply calling `optimize_for_mobile()` on your torchscripted model). Note that XNNPACK models will run slower if the inputs are contiguous, so definitely make sure it is in Channels-Last format.\n\n## Working example showing speedup\n\nRun this example on [Google Colab](https://colab.research.google.com/gist/suraj813/ad9aebcbffbdd6d02b23ca7231130a30/channels-last-with-xnnpack.ipynb#scrollTo=xvJN73YWXgDF) - note that runtimes on colab CPUs might not reflect accurate performance; it is recommended to run this code on your local machine.\n\n```python\nimport torch\nfrom torch.utils.mobile_optimizer import optimize_for_mobile\nimport torch.backends.xnnpack\nimport time\n\nprint(\"XNNPACK is enabled: \", torch.backends.xnnpack.enabled, \"\\n\")\n\nN, C, H, W = 1, 3, 200, 200\nx = torch.rand(N, C, H, W)\nprint(\"Contiguous shape: \", x.shape)\nprint(\"Contiguous stride: \", x.stride())\nprint()", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "xcl = x.to(memory_format=torch.channels_last)\nprint(\"Channels-Last shape: \", xcl.shape)\nprint(\"Channels-Last stride: \", xcl.stride())\n\n## Outputs:\n \n# XNNPACK is enabled: True\n \n# Contiguous shape: torch.Size([1, 3, 200, 200])\n# Contiguous stride: (120000, 40000, 200, 1)\n \n# Channels-Last shape: torch.Size([1, 3, 200, 200])\n# Channels-Last stride: (120000, 1, 600, 3)\n\n```\n\nThe input shape stays the same for contiguous and channels-last formats. Internally however, the tensor's layout has changed as you can see in the strides. Now, the number of jumps required to go across channels is only 1 (instead of 40000 in the contiguous tensor).\nThis better data locality means convolution layers can access all the channels for a given pixel much faster. Let's see now how the memory format affects runtime:\n\n```python\nfrom torchvision.models import resnet34, resnet50, resnet101\n\nm = resnet34(pretrained=False)\n# m = resnet50(pretrained=False)\n# m = resnet101(pretrained=False)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "def get_optimized_model(mm):\n mm = mm.eval()\n scripted = torch.jit.script(mm)\n optimized = optimize_for_mobile(scripted) # explicitly call the xnnpack rewrite \n return scripted, optimized\n\n\ndef compare_contiguous_CL(mm):\n # inference on contiguous\n start = time.perf_counter()\n for i in range(20):\n mm(x)\n end = time.perf_counter()\n print(\"Contiguous: \", end-start)\n\n # inference on channels-last\n start = time.perf_counter()\n for i in range(20):\n mm(xcl)\n end = time.perf_counter()\n print(\"Channels-Last: \", end-start)\n\nwith torch.inference_mode():\n scripted, optimized = get_optimized_model(m)\n\n print(\"Runtimes for torchscripted model: \")\n compare_contiguous_CL(scripted.eval())\n print()\n print(\"Runtimes for mobile-optimized model: \")\n compare_contiguous_CL(optimized.eval())", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "## Outputs (on an Intel Core i9 CPU):\n \n# Runtimes for torchscripted model:\n# Contiguous: 1.6711160129999598\n# Channels-Last: 1.6678222839999535\n \n# Runtimes for mobile-optimized model:\n# Contiguous: 0.5712863490000473\n# Channels-Last: 0.46113000699995155\n\n```\n\n## Conclusion\n\nThe Memory Layout of an input tensor can significantly impact a model\u2019s running time. For Vision Models, prefer a **Channels Last** memory format to get the most out of your PyTorch models.\n\n## References", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "## References\n\n- [Row/Column Major matrix storage order](https://en.wikipedia.org/wiki/Row-_and_column-major_order)\n- [Loop order impact on performance](https://stackoverflow.com/questions/9936132/why-does-the-order-of-the-loops-affect-performance-when-iterating-over-a-2d-arra)\n- [Cachegrind: a cache-miss profiler](https://courses.cs.washington.edu/courses/cse326/05wi/valgrind-doc/cg_main.html)\n- [NHWC format explained](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html)\n- [Why does PyTorch prefer NCHW?](https://discuss.pytorch.org/t/why-does-pytorch-prefer-using-nchw/83637/4)\n- [XNNPACK](https://github.com/google/XNNPACK)\n- [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html)\n- [Supported operators](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support)", "metadata": {"source": "https://pytorch.org/blog/tensor-memory-format-matters/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch framework for cryptographically secure random number generation, torchcsprng, now available'\nauthor: Team PyTorch\n---\n\nOne of the key components of modern cryptography is the pseudorandom number generator. Katz and Lindell stated, \"The use of badly designed or inappropriate random number generators can often leave a good cryptosystem vulnerable to attack. Particular care must be taken to use a random number generator that is designed for cryptographic use, rather than a 'general-purpose' random number generator which may be fine for some applications but not ones that are required to be cryptographically secure.\"[1] Additionally, most pseudorandom number generators scale poorly to massively parallel high-performance computation because of their sequential nature. Others don\u2019t satisfy cryptographically secure properties.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} +{"page_content": "[torchcsprng](https://github.com/pytorch/csprng) is a PyTorch [C++/CUDA extension](https://pytorch.org/tutorials/advanced/cpp_extension.html) that provides [cryptographically secure pseudorandom number generators](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) for PyTorch.\n\n## torchcsprng overview \n\nHistorically, PyTorch had only two pseudorandom number generator implementations: Mersenne Twister for CPU and Nvidia\u2019s cuRAND Philox for CUDA. Despite good performance properties, neither of them are suitable for cryptographic applications. Over the course of the past several months, the PyTorch team developed the torchcsprng extension API. Based on PyTorch dispatch mechanism and operator registration, it allows the users to extend c10::GeneratorImpl and implement their own custom pseudorandom number generator.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} +{"page_content": "torchcsprng generates a random 128-bit key on the CPU using one of its generators and then runs AES128 in [CTR mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) either on CPU or GPU using CUDA. This then generates a random 128-bit state and applies a transformation function to map it to target tensor values. This approach is based on [Parallel Random Numbers: As Easy as 1, 2, 3 (John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, D. E. Shaw Research)](http://www.thesalmons.org/john/random123/papers/random123sc11.pdf). It makes torchcsprng both crypto-secure and parallel on both CPU and CUDA.\n\n
\n \n
\n\nSince torchcsprng is a PyTorch extension, it is available on the platforms where PyTorch is available (support for Windows-CUDA will be available in the coming months). \n\n## Using torchcsprng", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} +{"page_content": "## Using torchcsprng\n\nThe torchcsprng API is very simple to use and is fully compatible with the PyTorch random infrastructure:\n\n**Step 1: Install via binary distribution**\n\nAnaconda:\n\n```python\nconda install torchcsprng -c pytorch\n ```\n\npip:\n\n```python\npip install torchcsprng\n ```\n\n**Step 2: import packages as usual but add csprng**\n\n```python\nimport torch\nimport torchcsprng as csprng\n ```\n\n**Step 3: Create a cryptographically secure pseudorandom number generator from /dev/urandom:**\n\n```python\nurandom_gen = csprng.create_random_device_generator('/dev/urandom')\n ```\n \nand simply use it with the existing PyTorch methods:\n\n```python\ntorch.randn(10, device='cpu', generator=urandom_gen)\n ```\n\n**Step 4: Test with Cuda**\n\nOne of the advantages of torchcsprng generators is that they can be used with both CPU and CUDA tensors:\n\n```python\ntorch.randn(10, device='cuda', generator=urandom_gen)\n ```\n\nAnother advantage of torchcsprng generators is that they are parallel on CPU unlike the default PyTorch CPU generator.", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} +{"page_content": "## Getting Started\n\nThe easiest way to get started with torchcsprng is by visiting the [GitHub page](https://github.com/pytorch/csprng) where you can find installation and build instructions, and more how-to examples. \n\nCheers,\n\nThe PyTorch Team\n\n[1] [Introduction to Modern Cryptography: Principles and Protocols (Chapman & Hall/CRC Cryptography and Network Security Series)](https://www.amazon.com/Introduction-Modern-Cryptography-Principles-Protocols/dp/1584885513) by Jonathan Katz and Yehuda Lindell", "metadata": {"source": "https://pytorch.org/blog/torchcsprng-release-blog/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Optimizing Production PyTorch Models\u2019 Performance with Graph Transformations\"\nauthor: Jade Nie, CK Luk, Xiaodong Wang, Jackie (Jiaqi) Xu\nfeatured-img: \"assets/images/blog1-3b.png\"\n---\n\n## 1. Introduction\n\nPyTorch supports two execution modes [1]: eager mode and graph mode. In eager mode, operators in a model are immediately executed as they are encountered. In contrast, in graph mode, operators are first synthesized into a graph, which will then be compiled and executed as a whole. Eager mode is easier to use, more suitable for ML researchers, and hence is the default mode of execution. On the other hand, graph mode typically delivers higher performance and hence is heavily used in production.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "Specifically, graph mode enables operator fusion [2], wherein one operator is merged with another to reduce/localize memory reads as well as total kernel launch overhead. Fusion can be horizontal\u2014taking a single operation (e.g., BatchNorm) that is independently applied to many operands and merging those operands into an array; and vertical\u2014merging a kernel with another kernel that consumes the output of the first kernel (e.g., Convolution followed by ReLU).\n\nTorch.FX [3, 4] (abbreviated as FX) is a publicly available toolkit as part of the PyTorch package that supports graph mode execution. In particular, it (1) captures the graph from a PyTorch program and (2) allows developers to write transformations on the captured graph. It is used inside Meta to optimize the training throughput of production models. By introducing a number of FX-based optimizations developed at Meta, we demonstrate the approach of using graph transformation to optimize PyTorch\u2019s performance for production.\n\n## 2. Background", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "## 2. Background\n\nEmbedding tables are ubiquitous in recommendation systems. Section 3 will discuss three FX transformations that optimize accesses to embedding tables. In this section, we provide some background on FX (Section 2.1) and embedding tables (Section 2.2).\n\n### 2.1 FX\n\nFigure 1 is a simple example adopted from [3] which illustrates using FX to transform a PyTorch program. It contains three steps: (1) capturing the graph from a program, (2) modifying the graph (in this example, all uses of RELU are replaced by GELU), and (3) generating a new program from the modified graph.\n\n

\n\n

\n\n**Figure 1: A FX example which replaces all uses of RELU by GELU in a PyTorch module.**\n\nThe FX API [4] provides many more functionalities for inspecting and transforming PyTorch program graphs.\n\n### 2.2 Embedding Tables\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "**Figure 2: Illustration of an embedding table for a sparse feature with batch size = 1**\n\nIn a recommendation system, sparse features (e.g., User ID, Story ID) are represented by embedding tables. An embedding table E is an HxD matrix, where H is the hash size, D is the embedding dimension. Each row of E is a vector of floats. Feature hashing [5] is used to map a sparse feature to a list of indices to E, say [S1,S2, \u2026, Sk], where 0<=Si<H. Its output value is computed as f(E[S1], E[S2], \u2026, E[Sk]), where E[Si] is the vector at row Si, and f is called the pooling function, which is typically one of the following functions: sum, average, maximum. See Figure 2 for an illustration.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "To fully utilize the GPU, sparse features are usually processed in a batch. Each entity in a batch has its own list of indices. If a batch has B entities, a naive representation has B lists of indices. A more compact representation is to combine the B lists of indices into a single list of indices and add a list of the lengths of indices (one length for each entity in the batch). For example, if a batch has 3 entities whose lists of indices are as follows:\n\n- Entity 1: indices = [10, 20]\n- Entity 2: indices = [5, 9, 77, 81]\n- Entity 3: indices = [15, 20, 45]\n\nThen the indices and lengths for the entire batch will be:\n\n- Indices = [10, 20, 5, 9, 77, 81, 15, 20, 45]\n- Lengths = [2, 4, 3]\n\nAnd the output of the embedding table lookup for the whole batch is a BxD matrix.\n\n## 3. Three FX Transformations", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "We have developed three FX transformations that accelerate accesses to embedding tables. Section 3.1 discusses a transformation that combines multiple small input tensors into a single big tensor; Section 3.2 a transformation that fuses multiple, parallel compute chains into a single compute chain; and Section 3.3 a transformation that overlaps communication with computation.\n\n### 3.1 Combining Input Sparse Features", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "Recall that an input sparse feature in a batch is represented by two lists: a list of indices and a list of B lengths, where B is the batch size. In PyTorch, these two lists are implemented as two tensors. When a PyTorch model is run on a GPU, embedding tables are commonly stored in the GPU memory (which is closer to the GPU and has much higher read/write bandwidth than the CPU memory). To use an input sparse feature, its two tensors need to be first copied from CPU to GPU. Nevertheless, per host-to-device memory copying requires a kernel launch, which is relatively expensive compared to the actual data transfer time. If a model uses many input sparse features, this copying could become a performance bottleneck (e.g., 1000 input sparse features would require copying 2000 tensors from host to device).\n\nAn optimization that reduces the number of host-to-device memcpy is to combine multiple input sparse features before sending them to the device. For instance, given the following three input features:", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "- Feature_A: indices = [106, 211, 7], lengths = [2, 1]\n- Feature_B: indices = [52, 498, 616, 870, 1013], lengths = [3, 2]\n- Feature_C: indices = [2011, 19, 351, 790], lengths = [1, 3]\n\nThe combined form is:\n\n- Features_A_B_C: indices = [106, 211, 7, 52, 498, 616, 870, 1013, 2011, 19, 351, 790], lengths = [2, 1, 3, 2, 1, 3]\n\nSo, instead of copying 3x2=6 tensors from host to device, we only need to copy 2 tensors.\n\nFigure 3(b) describes an implementation of this optimization, which has two components:\n\n- On the CPU side: The input pipeline is modified to combine all the indices of sparse features into a single tensor and similarly all the lengths into another tensor. Then the two tensors are copied to the GPU.\n- On the GPU side: Using FX, we insert a Permute_and_Split op into the model graph to recover the indices and lengths tensors of individual features from the combined tensors, and route them to the corresponding nodes downstream.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "

\n\n

\n\n(a). **Without the optimization**\n\n

\n\n

\n\n(b). **With the optimization**\n\n**Figure 3: Combining input sparse features**\n\n### 3.2 Horizontal fusion of computation chains started with accesses to embedding tables", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "In a production model, it is fairly common to have 10s of embedding tables residing on each GPU. For performance reasons, lookups to these tables are grouped together so that their outputs are concatenated in a single big tensor (see the red part in Figure 4(a)). To apply computations to individual feature outputs, a Split op is used to divide the big tensors into N smaller tensors (where N is the number of features) and then the desired computations are applied to each tensor. This is shown in Figure 4(a), where the computation applied to each feature output O is Tanh(LayerNorm(O)). All the computation results are concatenated back to a big tensor, which is then passed to downstream ops (Op1 in Figure 4(a)).", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} {"page_content": "The main runtime cost here is the GPU kernel launch overhead. For instance, the number of GPU kernel launches in Figure 4(a) is 2\\*N + 3 (each oval in the figure is a GPU kernel). This could become a performance issue because execution times of LayerNorm and Tanh on the GPU are short compared to their kernel launch times. In addition, the Split op may create an extra copy of the embedding output tensor, consuming additional GPU memory.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "We use FX to implement an optimization called horizontal fusion which dramatically reduces the number of GPU kernel launches (in this example, the optimized number of GPU kernel launches is 5, see Figure 4(b)). Instead of doing an explicit Split, we use the Add_middle_dim op to reshape the 2D embedding tensor of shape (B, NxD) to a 3D tensor of shape (B, N, D). Then a single LayerNorm is applied to the last dimension of it. Then a single Tanh is applied to the result of the LayerNorm. At the end, we use the", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "Remove_middle_dim op to reshape the Tanh\u2019s result back to a 2D tensor. In addition, since Add_middle_dim and Remove_middle_dim only reshape the tensor without creating an extra copy, the amount of GPU memory consumption could be reduced as well.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n(a). **Without the optimization**\n\n

\n\n

\n\n(b). **With the optimization**\n\n**Figure 4: Horizontal fusion**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "3.3 Overlapping Computation with Communication\n\nTraining of a production recommendation model is typically done on a distributed GPU system. Since the capacity of the device memory per GPU is not big enough to hold all the embedding tables in the model, they need to be distributed among the GPUs.\n\nWithin a training step, a GPU needs to read/write feature values from/to the embedding tables on the other GPUs. This is known as all-to-all communication [6] and can be a major performance bottleneck.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "We use FX to implement a transformation that can overlap computation with all-to-all communication. Figure 5(a) shows the example of a model graph which has embedding table accesses (EmbeddingAllToAll) and other ops. Without any optimization, they are sequentially executed on a GPU stream, as shown in Figure 5(b). Using FX, we break EmbeddingAllToAll into EmbeddingAllToAll_Request and EmbeddingAllToAll_Wait, and schedule independent ops in between them.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n**(a) Model graph**\n\n

\n\n

\n\n**(b) Original execution order**\n\n

\n\n

\n\n**(c)Optimized execution order**\n\n**Figure 5: Overlapping Computation with Communication**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "3.4 Summary\n\nTable 1 summarizes the optimizations discussed in this section and the corresponding performance bottlenecks addressed.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Optimization\n Performance Bottleneck Addressed\n
Combining Input Sparse Features\n Host-to-device memory copy\n
Horizontal fusion\n GPU kernel launch overhead\n
Overlapping Computation with Communication\n Embedding all-to-all access time\n
", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "**Table 1: Summary of the optimizations and the performance bottlenecks addressed**\n\nWe have also developed other FX transformations which are not discussed in this section due to space limitations.\n\nTo discover which models would benefit from these transformations, we analyzed the performance data collected by MAIProf [7] from the models that run at Meta\u2019s data centers. Altogether, these transformations provide up to 2-3x of speedups compared to eager mode on a set of production models.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "4. Concluding Remarks\n\nThe graph mode in PyTorch is preferred over the eager mode for production use for performance reasons. FX is a powerful tool for capturing and optimizing the graph of a PyTorch program. We demonstrate three FX transformations that are used to optimize production recommendation models inside Meta. We hope that this blog can motivate other PyTorch model developers to use graph transformations to boost their models\u2019 performance.\n\nReferences", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "[1] [End-to-end Machine Learning Framework](https://pytorch.org/features/)\n\n[2] [DNNFusion: Accelerating Deep Neural Networks Execution with Advanced Operator Fusion](https://arxiv.org/abs/2108.13342)\n\n[3] [Torch.FX: Practical Program Capture and Transformation for Deep Learning In Python](https://arxiv.org/pdf/2112.08429.pdf), MLSys 2022.\n\n[4] [Torch.fx\u2014PyTorch 1.12 documentation](https://pytorch.org/docs/stable/fx.html)", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "[5] [Feature Hashing for Large Scale Multitask Learning](https://alex.smola.org/papers/2009/Weinbergeretal09.pdf)\n\n[6] [NVIDIA Collective Communication Library Documentation](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/)\n\n[7] [Performance Debugging of Production PyTorch Models at Meta](https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/)", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.3 adds mobile, privacy, quantization, and named tensors'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "PyTorch continues to gain momentum because of its focus on meeting the needs of researchers, its streamlined workflow for production use, and most of all because of the enthusiastic support it has received from the AI community. PyTorch citations in papers on ArXiv [grew 194 percent in the first half of 2019 alone, as noted by O\u2019Reilly](https://www.oreilly.com/ideas/one-simple-graphic-researchers-love-pytorch-and-tensorflow?fbclid=IwAR3kYmlyD7zky37IYFu0cafQn7yemhl8P-7MNyB30z0q5RDzxcTOrP8kxDk), and the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "number of contributors to the platform has grown more than 50 percent over the last year, to nearly 1,200. Facebook, Microsoft, Uber, and other organizations across industries are increasingly using it as the foundation for their most important machine learning (ML) research and production workloads.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "We use FX to implement an optimization called horizontal fusion which dramatically reduces the number of GPU kernel launches (in this example, the optimized number of GPU kernel launches is 5, see Figure 4(b)). Instead of doing an explicit Split, we use the Add_middle_dim op to reshape the 2D embedding tensor of shape (B, NxD) to a 3D tensor of shape (B, N, D). Then a single LayerNorm is applied to the last dimension of it. Then a single Tanh is applied to the result of the LayerNorm. At the end, we use the Remove_middle_dim op to reshape the Tanh\u2019s result back to a 2D tensor. In addition, since Add_middle_dim and Remove_middle_dim only reshape the tensor without creating an extra copy, the amount of GPU memory consumption could be reduced as well.\n\n

\n\n

\n\n(a). **Without the optimization**\n\n

\n\n

\n\n(b). **With the optimization**\n\n**Figure 4: Horizontal fusion**", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "### 3.3 Overlapping Computation with Communication\n\nTraining of a production recommendation model is typically done on a distributed GPU system. Since the capacity of the device memory per GPU is not big enough to hold all the embedding tables in the model, they need to be distributed among the GPUs.\n\nWithin a training step, a GPU needs to read/write feature values from/to the embedding tables on the other GPUs. This is known as all-to-all communication [6] and can be a major performance bottleneck.\n\nWe use FX to implement a transformation that can overlap computation with all-to-all communication. Figure 5(a) shows the example of a model graph which has embedding table accesses (EmbeddingAllToAll) and other ops. Without any optimization, they are sequentially executed on a GPU stream, as shown in Figure 5(b). Using FX, we break EmbeddingAllToAll into EmbeddingAllToAll_Request and EmbeddingAllToAll_Wait, and schedule independent ops in between them.", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "

\n\n

\n\n**(a) Model graph**\n\n

\n\n

\n\n**(b) Original execution order**\n\n

\n\n

\n\n**(c)Optimized execution order**\n\n**Figure 5: Overlapping Computation with Communication**\n\n### 3.4 Summary\n\nTable 1 summarizes the optimizations discussed in this section and the corresponding performance bottlenecks addressed.\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Optimization\n Performance Bottleneck Addressed\n
Combining Input Sparse Features\n Host-to-device memory copy\n
Horizontal fusion\n GPU kernel launch overhead\n
Overlapping Computation with Communication\n Embedding all-to-all access time\n
", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "**Table 1: Summary of the optimizations and the performance bottlenecks addressed**\n\nWe have also developed other FX transformations which are not discussed in this section due to space limitations.\n\nTo discover which models would benefit from these transformations, we analyzed the performance data collected by MAIProf [7] from the models that run at Meta\u2019s data centers. Altogether, these transformations provide up to 2-3x of speedups compared to eager mode on a set of production models.\n\n## 4. Concluding Remarks\n\nThe graph mode in PyTorch is preferred over the eager mode for production use for performance reasons. FX is a powerful tool for capturing and optimizing the graph of a PyTorch program. We demonstrate three FX transformations that are used to optimize production recommendation models inside Meta. We hope that this blog can motivate other PyTorch model developers to use graph transformations to boost their models\u2019 performance.\n\nReferences", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "References\n\n[1] [End-to-end Machine Learning Framework](https://pytorch.org/features/)\n\n[2] [DNNFusion: Accelerating Deep Neural Networks Execution with Advanced Operator Fusion](https://arxiv.org/abs/2108.13342)\n\n[3] [Torch.FX: Practical Program Capture and Transformation for Deep Learning In Python](https://arxiv.org/pdf/2112.08429.pdf), MLSys 2022.\n\n[4] [Torch.fx\u2014PyTorch 1.12 documentation](https://pytorch.org/docs/stable/fx.html)\n\n[5] [Feature Hashing for Large Scale Multitask Learning](https://alex.smola.org/papers/2009/Weinbergeretal09.pdf)\n\n[6] [NVIDIA Collective Communication Library Documentation](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/)\n\n[7] [Performance Debugging of Production PyTorch Models at Meta](https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/)", "metadata": {"source": "https://pytorch.org/blog/optimizing-production-pytorch-performance-with-graph-transformations/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.3 adds mobile, privacy, quantization, and named tensors'\nauthor: Team PyTorch\n---\n\nPyTorch continues to gain momentum because of its focus on meeting the needs of researchers, its streamlined workflow for production use, and most of all because of the enthusiastic support it has received from the AI community. PyTorch citations in papers on ArXiv [grew 194 percent in the first half of 2019 alone, as noted by O\u2019Reilly](https://www.oreilly.com/ideas/one-simple-graphic-researchers-love-pytorch-and-tensorflow?fbclid=IwAR3kYmlyD7zky37IYFu0cafQn7yemhl8P-7MNyB30z0q5RDzxcTOrP8kxDk), and the number of contributors to the platform has grown more than 50 percent over the last year, to nearly 1,200. Facebook, Microsoft, Uber, and other organizations across industries are increasingly using it as the foundation for their most important machine learning (ML) research and production workloads.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} {"page_content": "We are now advancing the platform further with the release of PyTorch 1.3, which includes experimental support for features such as seamless model deployment to mobile devices, model quantization for better performance at inference time, and front-end improvements, like the ability to name tensors and create clearer code with less need for inline comments. We\u2019re also launching a number of additional tools and libraries to support model interpretability and bringing multimodal research to production.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Additionally, we\u2019ve collaborated with Google and Salesforce to add broad support for Cloud Tensor Processing Units, providing a significantly accelerated option for training large-scale deep neural networks. [Alibaba Cloud](https://data.aliyun.com/bigdata/pai-pytorch?spm=5176.12825654.a9ylfrljh.d112.7b652c4ayuOO4M&scm=20140722.1068.1.1098&aly_as=-PvJ5e4c) also joins Amazon Web Services, Microsoft Azure, and Google Cloud as supported cloud platforms for PyTorch users. You can get started now at", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "[pytorch.org](https://pytorch.org/get-started/locally/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "# PyTorch 1.3\n\nThe 1.3 release of PyTorch brings significant new features, including experimental support for mobile device deployment, eager mode quantization at 8-bit integer, and the ability to name tensors. With each of these enhancements, we look forward to additional contributions and improvements from the PyTorch community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Named tensors (experimental)\n\nCornell University\u2019s [Sasha Rush has argued](http://nlp.seas.harvard.edu/NamedTensor) that, despite its ubiquity in deep learning, the traditional implementation of tensors has significant shortcomings, such as exposing private dimensions, broadcasting based on absolute position, and keeping type information in documentation. He proposed named tensors as an alternative approach.\n\nToday, we name and access dimensions by comment:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Tensor[N, C, H, W]\n images = torch.randn(32, 3, 56, 56)\n images.sum(dim=1)\n images.select(dim=1, index=0)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "But naming explicitly leads to more readable and maintainable code:\n\n```python\nNCHW = [\u2018N\u2019, \u2018C\u2019, \u2018H\u2019, \u2018W\u2019]\n images = torch.randn(32, 3, 56, 56, names=NCHW)\n images.sum('C')\n images.select('C', index=0)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Quantization (experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "It\u2019s important to make efficient use of both server-side and on-device compute resources when developing ML applications. To support more efficient deployment on servers and edge devices, PyTorch 1.3 now supports 8-bit model quantization using the familiar eager mode Python API. Quantization refers to techniques used to perform computation and storage at reduced precision, such as 8-bit integer. This currently experimental feature includes support for post-training quantization, dynamic quantization, and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "quantization-aware training. It leverages the [FBGEMM](https://github.com/pytorch/FBGEMM) and [QNNPACK](https://github.com/pytorch/QNNPACK) state-of-the-art quantized kernel back ends, for x86 and ARM CPUs, respectively, which are integrated with PyTorch and now share a common API.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "To learn more about the design and architecture, check out the API docs [here](https://pytorch.org/docs/master/quantization.html), and get started with any of the supported techniques using the tutorials available [here](https://pytorch.org/tutorials/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "PyTorch mobile (experimental)\n\nRunning ML on edge devices is growing in importance as applications continue to demand lower latency. It is also a foundational element for privacy-preserving techniques such as federated learning. To enable more efficient on-device ML, PyTorch 1.3 now supports an end-to-end workflow from Python to deployment on iOS and Android.\n\nThis is an early, experimental release, optimized for end-to-end development. Coming releases will focus on:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Optimization for size: Build level optimization and selective compilation depending on the operators needed for user applications (i.e., you pay binary size for only the operators you need)\n* Performance: Further improvements to performance and coverage on mobile CPUs and GPUs\n* High level API: Extend mobile native APIs to cover common preprocessing and integration tasks needed for incorporating ML in mobile applications. e.g. Computer vision and NLP", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Learn more or get started on Android or iOS [here](http://pytorch.org/mobile).\n\n# New tools for model interpretability and privacy", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Captum", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "As models become ever more complex, it is increasingly important to develop new methods for model interpretability. To help address this need, we\u2019re launching Captum, a tool to help developers working in PyTorch understand why their model generates a specific output. Captum provides state-of-the-art tools to understand how the importance of specific neurons and layers and affect predictions made by the models. Captum\u2019s algorithms include integrated gradients, conductance, SmoothGrad and VarGrad, and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "DeepLift.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "The example below shows how to apply model interpretability algorithms on a pretrained ResNet model and then visualize the attributions for each pixel by overlaying them on the image.\n\n```python\nnoise_tunnel = NoiseTunnel(integrated_gradients)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "attributions_ig_nt, delta = noise_tunnel.attribute(input, n_samples=10, nt_type='smoothgrad_sq', target=pred_label_idx)\n_ = viz.visualize_image_attr_multiple([\"original_image\", \"heat_map\"],\n [\"all\", \"positive\"],\n np.transpose(attributions_ig_nt.squeeze().cpu().detach().numpy(), (1,2,0)),\n np.transpose(transformed_img.squeeze().cpu().detach().numpy(), (1,2,0)),", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "cmap=default_cmap,\n show_colorbar=True)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n
\n \n
\n\nLearn more about Captum at [captum.ai](https://www.captum.ai/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "CrypTen", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Practical applications of ML via cloud-based or machine-learning-as-a-service (MLaaS) platforms pose a range of security and privacy challenges. In particular, users of these platforms may not want or be able to share unencrypted data, which prevents them from taking full advantage of ML tools. To address these challenges, the ML community is exploring a number of technical approaches, at various levels of maturity. These include homomorphic encryption, secure multiparty computation, trusted execution", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "environments, on-device computation, and differential privacy.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "To provide a better understanding of how some of these technologies can be applied, we are releasing CrypTen, a new community-based research platform for taking the field of privacy-preserving ML forward. Learn more about CrypTen [here](https://ai.facebook.com/blog/crypten-a-new-research-tool-for-secure-machine-learning-with-pytorch). It is available on GitHub [here](https://github.com/facebookresearch/CrypTen).\n\n# Tools for multimodal AI systems", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Digital content is often made up of several modalities, such as text, images, audio, and video. For example, a single public post might contain an image, body text, a title, a video, and a landing page. Even one particular component may have more than one modality, such as a video that contains both visual and audio signals, or a landing page that is composed of images, text, and HTML sources.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "The ecosystem of tools and libraries that work with PyTorch offer enhanced ways to address the challenges of building multimodal ML systems. Here are some of the latest libraries launching today:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Detectron2\n\nObject detection and segmentation are used for tasks ranging from autonomous vehicles to content understanding for platform integrity. To advance this work, Facebook AI Research (FAIR) is releasing Detectron2, an object detection library now implemented in PyTorch. Detectron2 provides support for the latest models and tasks, increased flexibility to aid computer vision research, and improvements in maintainability and scalability to support production use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Detectron2 is available [here](https://github.com/facebookresearch/detectron2) and you can learn more [here](https://ai.facebook.com/blog/-detectron2-a-pytorch-based-modular-object-detection-library-).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Speech extensions to fairseq", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Language translation and audio processing are critical components in systems and applications such as search, translation, speech, and assistants. There has been tremendous progress in these fields recently thanks to the development of new architectures like transformers, as well as large-scale pretraining methods. We\u2019ve extended fairseq, a framework for sequence-to-sequence applications such as language translation, to include support for end-to-end learning for speech and audio recognition tasks.These", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "extensions to fairseq enable faster exploration and prototyping of new speech research ideas while offering a clear path to production.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Get started with fairseq [here](https://github.com/pytorch/fairseq/tree/master/examples/speech_recognition).\n\n# Cloud provider and hardware ecosystem support\n\nCloud providers such as Amazon Web Services, Microsoft Azure, and Google Cloud provide extensive support for anyone looking to develop ML on PyTorch and deploy in production. We\u2019re excited to share the general availability of Google Cloud TPU support and a newly launched integration with Alibaba Cloud. We\u2019re also expanding hardware ecosystem support.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Google Cloud TPU support now broadly available. To accelerate the largest-scale machine learning (ML) applications deployed today and enable rapid development of the ML applications of tomorrow, Google created custom silicon chips called Tensor Processing Units ([TPUs](https://cloud.google.com/tpu/)). When assembled into multi-rack ML supercomputers called [Cloud TPU Pods](https://cloud.google.com/blog/products/ai-machine-learning/cloud-tpu-pods-break-ai-training-records), these TPUs can complete ML", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "workloads in minutes or hours that previously took days or weeks on other systems. Engineers from Facebook, Google, and Salesforce worked together to enable and pilot Cloud TPU support in PyTorch, including experimental support for Cloud TPU Pods. PyTorch support for Cloud TPUs is also available in Colab. Learn more about how to get started with PyTorch on Cloud TPUs [here](https://github.com/pytorch/xla).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Alibaba adds support for PyTorch in Alibaba Cloud. The initial integration involves a one-click solution for PyTorch 1.x, Data Science Workshop notebook service, distributed training with Gloo/NCCL, as well as seamless integration with Alibaba IaaS such as OSS, ODPS, and NAS. Together with the toolchain provided by Alibaba, we look forward to significantly reducing the overhead necessary for adoption, as well as helping Alibaba Cloud\u2019s global customer base leverage PyTorch to develop new AI applications.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* ML hardware ecosystem expands. In addition to key GPU and CPU partners, the PyTorch ecosystem has also enabled support for dedicated ML accelerators. Updates from [Intel](https://www.intel.ai/nnpi-glow-pytorch/) and [Habana](https://medium.com/@HabanaLabs/unlocking-ai-scaling-through-software-and-hardware-interface-standardization-77561cb7598b) showcase how PyTorch, connected to the Glow optimizing compiler, enables developers to utilize these market-specific solutions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "Additionally, we\u2019ve collaborated with Google and Salesforce to add broad support for Cloud Tensor Processing Units, providing a significantly accelerated option for training large-scale deep neural networks. [Alibaba Cloud](https://data.aliyun.com/bigdata/pai-pytorch?spm=5176.12825654.a9ylfrljh.d112.7b652c4ayuOO4M&scm=20140722.1068.1.1098&aly_as=-PvJ5e4c) also joins Amazon Web Services, Microsoft Azure, and Google Cloud as supported cloud platforms for PyTorch users. You can get started now at [pytorch.org](https://pytorch.org/get-started/locally/).\n\n# PyTorch 1.3\n\nThe 1.3 release of PyTorch brings significant new features, including experimental support for mobile device deployment, eager mode quantization at 8-bit integer, and the ability to name tensors. With each of these enhancements, we look forward to additional contributions and improvements from the PyTorch community.\n\n## Named tensors (experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "Cornell University\u2019s [Sasha Rush has argued](http://nlp.seas.harvard.edu/NamedTensor) that, despite its ubiquity in deep learning, the traditional implementation of tensors has significant shortcomings, such as exposing private dimensions, broadcasting based on absolute position, and keeping type information in documentation. He proposed named tensors as an alternative approach.\n\nToday, we name and access dimensions by comment:\n\n```python\n# Tensor[N, C, H, W]\n images = torch.randn(32, 3, 56, 56)\n images.sum(dim=1)\n images.select(dim=1, index=0)\n```\n\nBut naming explicitly leads to more readable and maintainable code:\n\n```python\nNCHW = [\u2018N\u2019, \u2018C\u2019, \u2018H\u2019, \u2018W\u2019]\n images = torch.randn(32, 3, 56, 56, names=NCHW)\n images.sum('C')\n images.select('C', index=0)\n```\n\n## Quantization (experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "It\u2019s important to make efficient use of both server-side and on-device compute resources when developing ML applications. To support more efficient deployment on servers and edge devices, PyTorch 1.3 now supports 8-bit model quantization using the familiar eager mode Python API. Quantization refers to techniques used to perform computation and storage at reduced precision, such as 8-bit integer. This currently experimental feature includes support for post-training quantization, dynamic quantization, and quantization-aware training. It leverages the [FBGEMM](https://github.com/pytorch/FBGEMM) and [QNNPACK](https://github.com/pytorch/QNNPACK) state-of-the-art quantized kernel back ends, for x86 and ARM CPUs, respectively, which are integrated with PyTorch and now share a common API.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "To learn more about the design and architecture, check out the API docs [here](https://pytorch.org/docs/master/quantization.html), and get started with any of the supported techniques using the tutorials available [here](https://pytorch.org/tutorials/).\n\n## PyTorch mobile (experimental)\n\nRunning ML on edge devices is growing in importance as applications continue to demand lower latency. It is also a foundational element for privacy-preserving techniques such as federated learning. To enable more efficient on-device ML, PyTorch 1.3 now supports an end-to-end workflow from Python to deployment on iOS and Android.\n\nThis is an early, experimental release, optimized for end-to-end development. Coming releases will focus on:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "* Optimization for size: Build level optimization and selective compilation depending on the operators needed for user applications (i.e., you pay binary size for only the operators you need)\n* Performance: Further improvements to performance and coverage on mobile CPUs and GPUs\n* High level API: Extend mobile native APIs to cover common preprocessing and integration tasks needed for incorporating ML in mobile applications. e.g. Computer vision and NLP\n\nLearn more or get started on Android or iOS [here](http://pytorch.org/mobile).\n\n# New tools for model interpretability and privacy\n\n## Captum", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "## Captum\n\nAs models become ever more complex, it is increasingly important to develop new methods for model interpretability. To help address this need, we\u2019re launching Captum, a tool to help developers working in PyTorch understand why their model generates a specific output. Captum provides state-of-the-art tools to understand how the importance of specific neurons and layers and affect predictions made by the models. Captum\u2019s algorithms include integrated gradients, conductance, SmoothGrad and VarGrad, and DeepLift.\n\nThe example below shows how to apply model interpretability algorithms on a pretrained ResNet model and then visualize the attributions for each pixel by overlaying them on the image.\n\n```python\nnoise_tunnel = NoiseTunnel(integrated_gradients)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "attributions_ig_nt, delta = noise_tunnel.attribute(input, n_samples=10, nt_type='smoothgrad_sq', target=pred_label_idx)\n_ = viz.visualize_image_attr_multiple([\"original_image\", \"heat_map\"],\n [\"all\", \"positive\"],\n np.transpose(attributions_ig_nt.squeeze().cpu().detach().numpy(), (1,2,0)),\n np.transpose(transformed_img.squeeze().cpu().detach().numpy(), (1,2,0)),\n cmap=default_cmap,\n show_colorbar=True)\n```\n\n
\n \n
\n
\n \n
\n\nLearn more about Captum at [captum.ai](https://www.captum.ai/).\n\n## CrypTen", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "## CrypTen\n\nPractical applications of ML via cloud-based or machine-learning-as-a-service (MLaaS) platforms pose a range of security and privacy challenges. In particular, users of these platforms may not want or be able to share unencrypted data, which prevents them from taking full advantage of ML tools. To address these challenges, the ML community is exploring a number of technical approaches, at various levels of maturity. These include homomorphic encryption, secure multiparty computation, trusted execution environments, on-device computation, and differential privacy.\n\nTo provide a better understanding of how some of these technologies can be applied, we are releasing CrypTen, a new community-based research platform for taking the field of privacy-preserving ML forward. Learn more about CrypTen [here](https://ai.facebook.com/blog/crypten-a-new-research-tool-for-secure-machine-learning-with-pytorch). It is available on GitHub [here](https://github.com/facebookresearch/CrypTen).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "# Tools for multimodal AI systems\n\nDigital content is often made up of several modalities, such as text, images, audio, and video. For example, a single public post might contain an image, body text, a title, a video, and a landing page. Even one particular component may have more than one modality, such as a video that contains both visual and audio signals, or a landing page that is composed of images, text, and HTML sources.\n\nThe ecosystem of tools and libraries that work with PyTorch offer enhanced ways to address the challenges of building multimodal ML systems. Here are some of the latest libraries launching today:\n\n## Detectron2", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "## Detectron2\n\nObject detection and segmentation are used for tasks ranging from autonomous vehicles to content understanding for platform integrity. To advance this work, Facebook AI Research (FAIR) is releasing Detectron2, an object detection library now implemented in PyTorch. Detectron2 provides support for the latest models and tasks, increased flexibility to aid computer vision research, and improvements in maintainability and scalability to support production use cases.\n\nDetectron2 is available [here](https://github.com/facebookresearch/detectron2) and you can learn more [here](https://ai.facebook.com/blog/-detectron2-a-pytorch-based-modular-object-detection-library-).\n\n## Speech extensions to fairseq", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "Language translation and audio processing are critical components in systems and applications such as search, translation, speech, and assistants. There has been tremendous progress in these fields recently thanks to the development of new architectures like transformers, as well as large-scale pretraining methods. We\u2019ve extended fairseq, a framework for sequence-to-sequence applications such as language translation, to include support for end-to-end learning for speech and audio recognition tasks.These extensions to fairseq enable faster exploration and prototyping of new speech research ideas while offering a clear path to production.\n\nGet started with fairseq [here](https://github.com/pytorch/fairseq/tree/master/examples/speech_recognition).\n\n# Cloud provider and hardware ecosystem support", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "Cloud providers such as Amazon Web Services, Microsoft Azure, and Google Cloud provide extensive support for anyone looking to develop ML on PyTorch and deploy in production. We\u2019re excited to share the general availability of Google Cloud TPU support and a newly launched integration with Alibaba Cloud. We\u2019re also expanding hardware ecosystem support.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "* Google Cloud TPU support now broadly available. To accelerate the largest-scale machine learning (ML) applications deployed today and enable rapid development of the ML applications of tomorrow, Google created custom silicon chips called Tensor Processing Units ([TPUs](https://cloud.google.com/tpu/)). When assembled into multi-rack ML supercomputers called [Cloud TPU Pods](https://cloud.google.com/blog/products/ai-machine-learning/cloud-tpu-pods-break-ai-training-records), these TPUs can complete ML workloads in minutes or hours that previously took days or weeks on other systems. Engineers from Facebook, Google, and Salesforce worked together to enable and pilot Cloud TPU support in PyTorch, including experimental support for Cloud TPU Pods. PyTorch support for Cloud TPUs is also available in Colab. Learn more about how to get started with PyTorch on Cloud TPUs [here](https://github.com/pytorch/xla).\n* Alibaba adds support for PyTorch in Alibaba Cloud. The initial integration involves a one-click solution for PyTorch 1.x, Data Science Workshop notebook service, distributed training with Gloo/NCCL, as well as seamless integration with Alibaba IaaS such as OSS, ODPS, and NAS. Together with the toolchain provided by Alibaba, we look forward to significantly reducing the overhead necessary for adoption, as well as helping Alibaba Cloud\u2019s global customer base leverage PyTorch to develop new AI applications.\n* ML hardware ecosystem expands. In addition to key GPU and CPU partners, the PyTorch ecosystem has also enabled support for dedicated ML accelerators. Updates from [Intel](https://www.intel.ai/nnpi-glow-pytorch/) and [Habana](https://medium.com/@HabanaLabs/unlocking-ai-scaling-through-software-and-hardware-interface-standardization-77561cb7598b) showcase how PyTorch, connected to the Glow optimizing compiler, enables developers to utilize these market-specific solutions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} {"page_content": "# Growth in the PyTorch community\n\nAs an open source, community-driven project, PyTorch benefits from wide range of contributors bringing new capabilities to the ecosystem. Here are some recent examples:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Mila SpeechBrain aims to provide an open source, all-in-one speech toolkit based on PyTorch. The goal is to develop a single, flexible, user-friendly toolkit that can be used to easily develop state-of-the-art systems for speech recognition (both end to end and HMM-DNN), speaker recognition, speech separation, multi-microphone signal processing (e.g., beamforming), self-supervised learning, and many others. [Learn more](https://speechbrain.github.io/)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* SpaCy is a new wrapping library with consistent and easy-to-use interfaces to several models, in order to extract features to power NLP pipelines. Support is provided for via spaCy\u2019s standard training API. The library also calculates an alignment so the transformer features can be related back to actual words instead of just wordpieces. [Learn more](https://explosion.ai/blog/spacy-pytorch-transformers)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* HuggingFace PyTorch-Transformers (formerly known as pytorch-pretrained-bert is a library of state-of-the-art pretrained models for Natural Language Processing (NLP). The library currently contains PyTorch implementations, pretrained model weights, usage scripts, and conversion utilities for models such as BERT, GPT-2, RoBERTa, and DistilBERT. It has also grown quickly, with more than 13,000 GitHub stars and a broad set of users. [Learn more](https://github.com/huggingface/transformers)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* PyTorch Lightning is a Keras-like ML library for PyTorch. It leaves core training and validation logic to you and automates the rest. Reproducibility is a crucial requirement for many fields of research, including those based on ML techniques. As the number of research papers submitted to arXiv and conferences skyrockets into the tens of thousands, scaling reproducibility becomes difficult. [Learn more](https://github.com/williamFalcon/pytorch-lightning).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "* Mila SpeechBrain aims to provide an open source, all-in-one speech toolkit based on PyTorch. The goal is to develop a single, flexible, user-friendly toolkit that can be used to easily develop state-of-the-art systems for speech recognition (both end to end and HMM-DNN), speaker recognition, speech separation, multi-microphone signal processing (e.g., beamforming), self-supervised learning, and many others. [Learn more](https://speechbrain.github.io/)\n* SpaCy is a new wrapping library with consistent and easy-to-use interfaces to several models, in order to extract features to power NLP pipelines. Support is provided for via spaCy\u2019s standard training API. The library also calculates an alignment so the transformer features can be related back to actual words instead of just wordpieces. [Learn more](https://explosion.ai/blog/spacy-pytorch-transformers)\n* HuggingFace PyTorch-Transformers (formerly known as pytorch-pretrained-bert is a library of state-of-the-art pretrained models for Natural Language Processing (NLP). The library currently contains PyTorch implementations, pretrained model weights, usage scripts, and conversion utilities for models such as BERT, GPT-2, RoBERTa, and DistilBERT. It has also grown quickly, with more than 13,000 GitHub stars and a broad set of users. [Learn more](https://github.com/huggingface/transformers)\n* PyTorch Lightning is a Keras-like ML library for PyTorch. It leaves core training and validation logic to you and automates the rest. Reproducibility is a crucial requirement for many fields of research, including those based on ML techniques. As the number of research papers submitted to arXiv and conferences skyrockets into the tens of thousands, scaling reproducibility becomes difficult. [Learn more](https://github.com/williamFalcon/pytorch-lightning).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} {"page_content": "We recently held the first online Global PyTorch Summer Hackathon, where researchers and developers around the world were invited to build innovative new projects with PyTorch. Nearly 1,500 developers participated, submitting projects ranging from livestock disease detection to AI-powered financial assistants. The winning projects were:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Torchmeta, which provides extensions for PyTorch to simplify the development of meta-learning algorithms in PyTorch. It features a unified interface inspired by TorchVision for both few-shot classification and regression problems, to allow easy benchmarking on multiple data sets to aid with reproducibility.\n* Open-Unmix, a system for end-to-end music demixing with PyTorch. Demixing separates the individual instruments or vocal track from any stereo recording.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "* Endless AI-Generated Tees, a store featuring AI-generated T-shirt designs that can be purchased and delivered worldwide. The system uses a state-of-the-art generative model (StyleGAN) that was built with PyTorch and then trained on modern art.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} -{"page_content": "Visit [pytorch.org](https://pytorch.org/) to learn more and get started with PyTorch 1.3 and the latest libraries and ecosystem projects. We look forward to the contributions, exciting research advancements, and real-world applications that the community builds with PyTorch.\n\n*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "* Torchmeta, which provides extensions for PyTorch to simplify the development of meta-learning algorithms in PyTorch. It features a unified interface inspired by TorchVision for both few-shot classification and regression problems, to allow easy benchmarking on multiple data sets to aid with reproducibility.\n* Open-Unmix, a system for end-to-end music demixing with PyTorch. Demixing separates the individual instruments or vocal track from any stereo recording.\n* Endless AI-Generated Tees, a store featuring AI-generated T-shirt designs that can be purchased and delivered worldwide. The system uses a state-of-the-art generative model (StyleGAN) that was built with PyTorch and then trained on modern art.\n\nVisit [pytorch.org](https://pytorch.org/) to learn more and get started with PyTorch 1.3 and the latest libraries and ecosystem projects. We look forward to the contributions, exciting research advancements, and real-world applications that the community builds with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} +{"page_content": "*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"New library updates in PyTorch 1.12\"\nauthor: Team PyTorch\nfeatured-img: ''\n---\n\nWe are bringing a number of improvements to the current PyTorch libraries, alongside the [PyTorch 1.12 release](https://github.com/pytorch/pytorch/releases/tag/v1.12.0). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Summary:\n- **TorchVision** - Added multi-weight support API, new architectures, model variants, and pretrained weight. See the release notes [here](https://github.com/pytorch/vision/releases).\n- **TorchAudio** - Introduced beta features including a streaming API, a CTC beam search decoder, and new beamforming modules and methods. See the release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **TorchText** - Extended support for scriptable BERT tokenizer and added datasets for GLUE benchmark. See the release notes [here](https://github.com/pytorch/text/releases).\n- **TorchRec** - Added EmbeddingModule benchmarks, examples for TwoTower Retrieval, inference and sequential embeddings, metrics, improved planner and demonstrated integration with production components. See the release notes [here](https://github.com/pytorch/torchrec/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- **TorchX** - Launch PyTorch trainers developed on local workspaces onto five different types of schedulers. See the release notes [here](https://github.com/pytorch/torchx/blob/main/CHANGELOG.md?plain=1#L3).\n- **FBGemm** - Added and improved kernels for Recommendation Systems inference workloads, including table batched embedding bag, jagged tensor operations, and other special-case optimizations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchVision v0.13", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Multi-weight support API\n\nTorchVision v0.13 offers a new [Multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) for loading different weights to the existing model builder methods:\n\n```python\nfrom torchvision.models import *\n\n# Old weights with accuracy 76.130%\nresnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\n\n# New weights with accuracy 80.858%\nresnet50(weights=ResNet50_Weights.IMAGENET1K_V2)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Best available weights (currently alias for IMAGENET1K_V2)\n# Note that these weights may change across versions\nresnet50(weights=ResNet50_Weights.DEFAULT)\n\n# Strings are also supported\nresnet50(weights=\"IMAGENET1K_V2\")\n\n# No weights - random initialization\nresnet50(weights=None)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The new API bundles along with the weights important details such as the preprocessing transforms and meta-data such as labels. Here is how to make the most out of it:\n\n```python\nfrom torchvision.io import read_image\nfrom torchvision.models import resnet50, ResNet50_Weights\n\nimg = read_image(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n\n# Step 1: Initialize model with the best available weights\nweights = ResNet50_Weights.DEFAULT\nmodel = resnet50(weights=weights)\nmodel.eval()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Step 2: Initialize the inference transforms\npreprocess = weights.transforms()\n\n# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\n\n# Step 4: Use the model and print the predicted category\nprediction = model(batch).squeeze(0).softmax(0)\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\ncategory_name = weights.meta[\"categories\"][class_id]\nprint(f\"{category_name}: {100 * score:.1f}%\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "You can read more about the new API in the [docs](https://pytorch.org/vision/0.13/models.html). To provide your feedback, please use this dedicated [Github issue](https://github.com/pytorch/vision/issues/5088).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "New architectures and model variants", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Classification\n\nThe [Swin Transformer](https://arxiv.org/abs/2103.14030) and [EfficienetNetV2](https://arxiv.org/abs/2104.00298) are two popular classification models which are often used for downstream vision tasks. This release includes 6 pre-trained weights for their classification variants. Here is how to use the new models:\n\n```python\nimport torch\nfrom torchvision.models import *\n\nimage = torch.rand(1, 3, 224, 224)\nmodel = swin_t(weights=\"DEFAULT\").eval()\nprediction = model(image)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "image = torch.rand(1, 3, 384, 384)\nmodel = efficientnet_v2_s(weights=\"DEFAULT\").eval()\nprediction = model(image)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "In addition to the above, we also provide new variants for existing architectures such as ShuffleNetV2, ResNeXt and MNASNet. The accuracies of all the new pre-trained models obtained on ImageNet-1K are seen below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| **Model** | **Acc@1** | **Acc@5** |\n|--------------------------------|-----------|-----------|\n| swin_t | 81.474 | 95.776 |\n| swin_s | 83.196 | 96.36 |\n| swin_b | 83.582 | 96.64 |\n| efficientnet_v2_s | 84.228 | 96.878 |\n| efficientnet_v2_m | 85.112 | 97.156 |\n| efficientnet_v2_l | 85.808 | 97.788 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| resnext101_64x4d | 83.246 | 96.454 |\n| resnext101_64x4d (quantized) | 82.898 | 96.326 |\n| shufflenet_v2_x1_5 | 72.996 | 91.086 |\n| shufflenet_v2_x1_5 (quantized) | 72.052 | 0.700 |\n| shufflenet_v2_x2_0 | 76.230 | 93.006 |\n| shufflenet_v2_x2_0 (quantized) | 75.354 | 92.488 |\n| mnasnet0_75 | 71.180 | 90.496 |\n| mnas1_3 | 76.506 | 93.522 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank Hu Ye for contributing to TorchVision the Swin Transformer implementation.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(BETA) Object Detection and Instance Segmentation\n\nWe have introduced 3 new model variants for RetinaNet, FasterRCNN and MaskRCNN that include several [post-paper architectural optimizations](https://github.com/pytorch/vision/pull/5444) and improved training recipes. All models can be used similarly:\n\n```python\nimport torch\nfrom torchvision.models.detection import *", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "images = [torch.rand(3, 800, 600)]\nmodel = retinanet_resnet50_fpn_v2(weights=\"DEFAULT\")\n# model = fasterrcnn_resnet50_fpn_v2(weights=\"DEFAULT\")\n# model = maskrcnn_resnet50_fpn_v2(weights=\"DEFAULT\")\nmodel.eval()\nprediction = model(images)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Below we present the metrics of the new variants on COCO val2017. In parenthesis we denote the improvement over the old variants:\n\n| **Model** | **Box mAP** | **Mask mAP** |\n|----------------------------|-------------|--------------|\n| retinanet_resnet50_fpn_v2 | 41.5 (+5.1) | - |\n| fasterrcnn_resnet50_fpn_v2 | 46.7 (+9.7) | - |\n| maskrcnn_resnet50_fpn_v2 | 47.4 (+9.5) | 41.8 (+7.2) |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank Ross Girshick, Piotr Dollar, Vaibhav Aggarwal, Francisco Massa and Hu Ye for their past research and contributions to this work.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "New pre-trained weights", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "SWAG weights", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The ViT and RegNet model variants offer new pre-trained [SWAG](https://arxiv.org/abs/2201.08371) (\u200b\u200bSupervised Weakly from hashtAGs) weights. One of the biggest of these models achieves a whopping 88.6% accuracy on ImageNet-1K. We currently offer two versions of the weights: 1) fine-tuned end-to-end weights on ImageNet-1K (highest accuracy) and 2) frozen trunk weights with a linear classifier fit on ImageNet-1K (great for transfer learning). Below we see the detailed accuracies of each model variant:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| **Model Weights** | **Acc@1** | **Acc@5** |\n|--------------------------------------------------|-----------|-----------|\n| RegNet_Y_16GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 86.012 | 98.054 |\n| RegNet_Y_16GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 83.976 | 97.244 |\n| RegNet_Y_32GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 86.838 | 98.362 |\n| RegNet_Y_32GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 84.622 | 97.48 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| RegNet_Y_128GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.228 | 98.682 |\n| RegNet_Y_128GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 86.068 | 97.844 |\n| ViT_B_16_Weights.IMAGENET1K_SWAG_E2E_V1 | 85.304 | 97.65 |\n| ViT_B_16_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 81.886 | 96.18 |\n| ViT_L_16_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.064 | 98.512 |\n| ViT_L_16_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 85.146 | 97.422 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| ViT_H_14_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.552 | 98.694 |\n| ViT_H_14_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 85.708 | 97.73 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The SWAG weights are released under the [Attribution-NonCommercial 4.0 International](https://github.com/facebookresearch/SWAG/blob/main/LICENSE) license. We would like to thank Laura Gustafson, Mannat Singh and Aaron Adcock for their work and support in making the weights available to TorchVision.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Model Refresh\n\nThe release of the Multi-weight support API enabled us to refresh the most popular models and offer more accurate weights. We improved on average each model by ~3 points. The new recipe used was learned on top of ResNet50 and its details were covered on a [previous blog post](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| **Model** | **Old weights** | **New weights** |\n|------------------------------|-----------------|-----------------|\n| efficientnet_b1 | 78.642 | 79.838 |\n| mobilenet_v2 | 71.878 | 72.154 |\n| mobilenet_v3_large | 74.042 | 75.274 |\n| regnet_y_400mf | 74.046 | 75.804 |\n| regnet_y_800mf | 76.42 | 78.828 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| regnet_y_1_6gf | 77.95 | 80.876 |\n| regnet_y_3_2gf | 78.948 | 81.982 |\n| regnet_y_8gf | 80.032 | 82.828 |\n| regnet_y_16gf | 80.424 | 82.886 |\n| regnet_y_32gf | 80.878 | 83.368 |\n| regnet_x_400mf | 72.834 | 74.864 |\n| regnet_x_800mf | 75.212 | 77.522 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| regnet_x_1_6gf | 77.04 | 79.668 |\n| regnet_x_3_2gf | 78.364 | 81.196 |\n| regnet_x_8gf | 79.344 | 81.682 |\n| regnet_x_16gf | 80.058 | 82.716 |\n| regnet_x_32gf | 80.622 | 83.014 |\n| resnet50 | 76.13 | 80.858 |\n| resnet50 (quantized) | 75.92 | 80.282 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "| resnet101 | 77.374 | 81.886 |\n| resnet152 | 78.312 | 82.284 |\n| resnext50_32x4d | 77.618 | 81.198 |\n| resnext101_32x8d | 79.312 | 82.834 |\n| resnext101_32x8d (quantized) | 78.986 | 82.574 |\n| wide_resnet50_2 | 78.468 | 81.602 |\n| wide_resnet101_2 | 78.848 | 82.51 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank Piotr Dollar, Mannat Singh and Hugo Touvron for their past research and contributions to this work.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "New Augmentations, Layers and Losses", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "This release brings a bunch of new primitives which can be used to produce SOTA models. Some highlights include the addition of [AugMix](https://arxiv.org/abs/1912.02781) data-augmentation method, the [DropBlock](https://arxiv.org/abs/1810.12890) layer, the [cIoU/dIoU](https://arxiv.org/abs/1911.08287) loss and [many more](https://github.com/pytorch/vision/issues/5410). We would like to thank Aditya Oke, Abhijit Deo, Yassine Alouini and Hu Ye for contributing to the project and for helping us maintain", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchVision relevant and fresh.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Documentation", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We completely revamped our models documentation to make them easier to browse, and added various key information such as supported image sizes, or image pre-processing steps of pre-trained weights. We now have a [main model page](https://pytorch.org/vision/main/models.html) with various [summary tables](https://pytorch.org/vision/main/models.html#table-of-all-available-classification-weights) of available weights, and each model has a [dedicated page](https://pytorch.org/vision/main/models/resnet.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Each model builder is also documented in their [own page](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html#torchvision.models.resnet50), with more details about the available weights, including accuracy, minimal image size, link to training recipes, and other valuable info. For comparison, our previous models docs are [here](https://pytorch.org/vision/0.12/models.html). To provide feedback on the new documentation, please use the dedicated [Github", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "issue](https://github.com/pytorch/vision/issues/5511).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio v0.12", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(BETA) Streaming API\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "StreamReader is TorchAudio\u2019s new I/O API. It is backed by FFmpeg\u2020, and allows users to:\n- Decode audio and video formats, including MP4 and AAC\n- Handle input forms, such as local files, network protocols, microphones, webcams, screen captures and file-like objects\n- Iterate over and decode chunk-by-chunk, while changing the sample rate or frame rate\n- Apply audio and video filters, such as low-pass filter and image scaling\n- Decode video with Nvidia's hardware-based decoder (NVDEC)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For usage details, please check out the [documentation](https://pytorch.org/audio/0.12.0/io.html#streamreader) and tutorials:\n- [Media Stream API - Pt.1](https://pytorch.org/audio/0.12.0/tutorials/streaming_api_tutorial.html)\n- [Media Stream API - Pt.2](https://pytorch.org/audio/0.12.0/tutorials/streaming_api2_tutorial.html)\n- [Online ASR with Emformer RNN-T](https://pytorch.org/audio/0.12.0/tutorials/online_asr_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [Device ASR with Emformer RNN-T](https://pytorch.org/audio/0.12.0/tutorials/device_asr.html)\n- [Accelerated Video Decoding with NVDEC](https://pytorch.org/audio/0.12.0/hw_acceleration_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "\u2020 To use StreamReader, FFmpeg libraries are required. Please install FFmpeg. The coverage of codecs depends on how these libraries are configured. TorchAudio official binaries are compiled to work with FFmpeg 4 libraries; FFmpeg 5 can be used if TorchAudio is built from source.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(BETA) CTC Beam Search Decoder\n\nTorchAudio integrates the wav2letter CTC beam search decoder from [Flashlight](https://arxiv.org/pdf/2201.12465.pdf) ([GitHub](https://github.com/flashlight/flashlight)). The addition of this inference time decoder enables running end-to-end CTC ASR evaluation using TorchAudio utils.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Customizable lexicon and lexicon-free decoders are supported, and both are compatible with KenLM n-gram language models or without using a language model. TorchAudio additionally supports downloading token, lexicon, and pretrained KenLM files for the LibriSpeech dataset.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For usage details, please check out the [documentation](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder) and [ASR inference tutorial](https://pytorch.org/audio/0.12.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(BETA) New Beamforming Modules and Methods", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "To improve flexibility in usage, the release adds two new beamforming modules under torchaudio.transforms: [SoudenMVDR](https://pytorch.org/audio/0.12.0/transforms.html#soudenmvdr) and [RTFMVDR](https://pytorch.org/audio/0.12.0/transforms.html#rtfmvdr). The main differences from [MVDR](https://pytorch.org/audio/0.11.0/transforms.html#mvdr) are:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- Use power spectral density (PSD) and relative transfer function (RTF) matrices as inputs instead of time-frequency masks. The module can be integrated with neural networks that directly predict complex-valued STFT coefficients of speech and noise\n- Add \\'reference_channel\\' as an input argument in the forward method, to allow users to select the reference channel in model training or dynamically change the reference channel in inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Besides the two modules, new function-level beamforming methods are added under torchaudio.functional. These include:\n- [psd](https://pytorch.org/audio/0.12.0/functional.html#psd)\n- [mvdr_weights_souden](https://pytorch.org/audio/0.12.0/functional.html#mvdr-weights-souden)\n- [mvdr_weights_rtf](https://pytorch.org/audio/0.12.0/functional.html#mvdr-weights-rtf)\n- [rtf_evd](https://pytorch.org/audio/0.12.0/functional.html#rtf-evd)\n- [rtf_power](https://pytorch.org/audio/0.12.0/functional.html#rtf-power)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [apply_beamforming](https://pytorch.org/audio/0.12.0/functional.html#apply-beamforming)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For usage details, please check out the documentation at [torchaudio.transforms](https://pytorch.org/audio/0.12.0/transforms.html#multi-channel) and [torchaudio.functional](https://pytorch.org/audio/0.12.0/functional.html#multi-channel) and the [Speech Enhancement with MVDR Beamforming tutorial](https://pytorch.org/audio/0.12.0/tutorials/mvdr_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchText v0.13", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Glue Datasets", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We increased the number of datasets in TorchText from 22 to 30 by adding the remaining 8 datasets from the GLUE benchmark (SST-2 was already supported). The complete list of GLUE datasets is as follows:\n- [CoLA](https://nyu-mll.github.io/CoLA/) ([paper](https://arxiv.org/pdf/1805.12471.pdf)): Single sentence binary classification acceptability task", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [SST-2](https://nlp.stanford.edu/sentiment/) ([paper](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf)): Single sentence binary classification sentiment task\n- [MRPC](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf) ([paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/I05-50025B15D.pdf)): Dual sentence binary classification paraphrase task\n- [QQP](https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs): Dual sentence binary classification paraphrase task", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [STS-B](https://ixa2.si.ehu.eus/stswiki/index.php/STSbenchmark) ([paper](https://aclanthology.org/S17-2001.pdf)): Single sentence to float regression sentence similarity task\n- [MNLI](https://cims.nyu.edu/~sbowman/multinli/) ([paper](https://cims.nyu.edu/~sbowman/multinli/paper.pdf)): Sentence ternary classification NLI task\n- [QNLI](https://gluebenchmark.com/) ([paper](https://arxiv.org/pdf/1804.07461.pdf)): Sentence binary classification QA and NLI tasks", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [RTE](https://aclweb.org/aclwiki/Recognizing_Textual_Entailment) ([paper](https://arxiv.org/pdf/2010.03061.pdf)): Dual sentence binary classification NLI task\n- [WNLI](https://cs.nyu.edu/~davise/papers/WinogradSchemas/WS.html) ([paper](http://commonsensereasoning.org/2011/papers/Levesque.pdf)): Dual sentence binary classification coreference and NLI tasks", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Scriptable BERT Tokenizer\n\nTorchText has extended support for scriptable tokenizer by adding the WordPiece tokenizer used in BERT. It is one of the commonly used algorithms for splitting input text into sub-words units and was introduced in [Japanese and Korean Voice Search (Schuster et al., 2012)](https://static.googleusercontent.com/media/research.google.com/ja//pubs/archive/37842.pdf).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchScriptabilty support would allow users to embed the BERT text-preprocessing natively in C++ without needing the support of python runtime. As TorchText now supports the CMAKE build system to natively link torchtext binaries with application code, users can easily integrate BERT tokenizers for deployment needs.\n\nFor usage details, please refer to the corresponding [documentation](https://pytorch.org/text/main/transforms.html#torchtext.transforms.BERTTokenizer).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchRec v0.2.0\n\n### EmbeddingModule + DLRM benchmarks\n\nA set of [benchmarking tests](https://github.com/pytorch/torchrec/tree/main/benchmarks), showing performance characteristics of TorchRec\u2019s base modules and research models built out of TorchRec.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TwoTower Retrieval Example, with FAISS\n\nWe provide an [example](https://github.com/pytorch/torchrec/tree/main/examples/retrieval) demonstrating training a distributed TwoTower (i.e. User-Item) Retrieval model that is sharded using TorchRec. The projected item embeddings are added to an IVFPQ FAISS index for candidate generation. The retrieval model and KNN lookup are bundled in a Pytorch model for efficient end-to-end retrieval.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Integrations", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We demonstrate that TorchRec works out of the box with many components commonly used alongside PyTorch models in production like systems, such as \n- [Training](https://github.com/pytorch/torchrec/tree/main/examples/ray) a TorchRec model on Ray Clusters utilizing the Torchx Ray scheduler\n- [Preprocessing](https://github.com/pytorch/torchrec/tree/main/torchrec/datasets/scripts/nvt) and DataLoading with NVTabular on DLRM", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "- [Training](https://github.com/pytorch/torchrec/tree/main/examples/torcharrow) a TorchRec model with on-the-fly preprocessing with TorchArrow showcasing RecSys domain UDFs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Sequential Embeddings Example: Bert4Rec\n\nWe provide an [example](https://github.com/pytorch/torchrec/tree/main/examples/bert4rec), using TorchRec, that reimplements the [BERT4REC](https://arxiv.org/abs/1904.06690) paper, showcasing EmbeddingCollection for non-pooled embeddings. Using DistributedModelParallel we see a 35% QPS gain over conventional data parallelism.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Planner", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The TorchRec library includes a built-in [planner](https://pytorch.org/torchrec/torchrec.distributed.planner.html) that selects near optimal sharding plan for a given model. The planner attempts to identify the best sharding plan by evaluating a series of proposals which are statically analyzed and fed into an integer partitioner. The planner is able to automatically adjust plans for a wide range of hardware setups, allowing users to scale performance seamlessly from local development environment to large", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "scale production hardware. See this [notebook](https://github.com/pytorch/torchrec/blob/main/torchrec/distributed/planner/Planner_Introduction.ipynb) for a more detailed tutorial.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[TorchRec Inference](https://github.com/pytorch/torchrec/tree/main/torchrec/inference) is a C++ library that supports multi-gpu inference. The TorchRec library is used to shard models written and packaged in Python via torch.package (an alternative to TorchScript). The torch.deploy library is used to serve inference from C++ by launching multiple Python interpreters carrying the packaged model, thus subverting the GIL. Two models are provided as examples: [DLRM", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "multi-GPU](https://github.com/pytorch/torchrec/blob/main/examples/inference/dlrm_predict.py) (sharded via TorchRec) and [DLRM single-GPU](https://github.com/pytorch/torchrec/blob/main/examples/inference/dlrm_predict_single_gpu.py).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) RecMetrics", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "RecMetrics is a [metrics](https://github.com/pytorch/torchrec/tree/main/torchrec/metrics) library that collects common utilities and optimizations for Recommendation models. It extends [torchmetrics](https://torchmetrics.readthedocs.io/en/stable/).\n- A centralized metrics module that allows users to add new metrics\n- Commonly used metrics, including AUC, Calibration, CTR, MSE/RMSE, NE & Throughput\n- Optimization for metrics related operations to reduce the overhead of metric computation\n- Checkpointing", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Single process Batched + Fused Embeddings", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Previously TorchRec\u2019s abstractions (EmbeddingBagCollection/EmbeddingCollection) over FBGEMM kernels, which provide benefits such as table batching, optimizer fusion, and UVM placement, could only be used in conjunction with DistributedModelParallel. We\u2019ve decoupled these notions from sharding, and introduced the [FusedEmbeddingBagCollection](https://github.com/pytorch/torchrec/blob/eb1247d8a2d16edc4952e5c2617e69acfe5477a5/torchrec/modules/fused_embedding_modules.py#L271), which can be used as a standalone", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "module, with all of the above features, and can also be sharded.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchX v0.2.0\n\nTorchX is a job launcher that makes it easier to run PyTorch in distributed training clusters with many scheduler integrations including Kubernetes and Slurm. We're excited to release TorchX 0.2.0 with a number of improvements. TorchX is currently being used in production in both on-premise and cloud environments.\n\nCheck out the [quickstart](https://pytorch.org/torchx/main/quickstart.html) to start launching local and remote jobs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Workspaces\n\nTorchX [now supports workspaces](https://pytorch.org/torchx/main/workspace.html) which allows users to easily launch training jobs using their local workspace. TorchX can automatically build a patch with your local training code on top of a base image to minimize iteration time and time to training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": ".torchxconfig\n\nSpecifying options in [.torchxconfig](https://pytorch.org/torchx/latest/runner.config.html) saves you from having to type long CLI commands each time you launch a job. You can also define project level generic configs and drop a config file in your home directory for user-level overrides.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Expanded Scheduler Support\n\nTorchX now supports [AWS Batch](https://pytorch.org/torchx/main/schedulers/aws_batch.html) and [Ray (experimental)](https://pytorch.org/torchx/main/schedulers/ray.html) schedulers in addition to our existing integrations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Distributed Training On All Schedulers\n\nThe TorchX dist.ddp component now works on all schedulers without any configuration. Distributed training workers will automatically discover each other when using [torchelastic](https://pytorch.org/docs/stable/distributed.elastic.html) via [the builtin dist.ddp component](https://pytorch.org/torchx/main/components/distributed.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Hyper Parameter Optimization\n\nTorchX [integrates with Ax](https://ax.dev/versions/latest/api/runners.html#module-ax.runners.torchx) to let you scale hyper-parameter optimizations (HPO) by launching the search trials onto remote clusters.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "File and Device Mounts\n\nTorchX now supports [remote filesystem mounts and custom devices](https://pytorch.org/torchx/main/specs.html#mounts). This enables your PyTorch jobs to efficiently access cloud storage such as NFS or Lustre. The device mounts enables usage of network accelerators like Infiniband and custom inference/training accelerators.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "FBGemm v0.2.0\n\nThe FBGEMM library contains optimized kernels meant to improve the performance of PyTorch workloads. We\u2019ve added a number of new features and optimizations over the last few months that we are excited to report.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Inference Table Batched Embedding (TBE)\n\nThe [table batched embedding bag](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L1541) (TBE) operator is an important base operation for embedding lookup for recommendation system inference on GPU. We added the following enhancements for performance and flexibility:\n\nAlignment restriction removed\n- Embedding dimension \\* data type size had to be multiple of 4B before and now, it is 1B.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Unified Virtual Memory (UVM) caching kernel optimizations\n- UVM caching kernels now scale linearly with # of tables using UVM caching. Previously, it was having similar overhead as all tables using UVM caching\n- UVM caching kernel overhead is much smaller than before", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Inference FP8 Table Batched Embedding (TBE)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The [table batched embedding bag](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L1541) (TBE) previously supported FP32, FP16, INT8, INT4, and INT2 embedding weight types. While these weight types work well in many models, we integrate FP8 weight types (in both GPU and CPU operations) to allow for numerical and performance evaluations of FP8 in our models. Compared to INT8, FP8 does not require the additional bias and scale storage and calculations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Additionally, the next generation of H100 GPUs has the FP8 support on Tensor Core (mainly matmul ops).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Jagged Tensor Kernels\n\nWe added optimized kernels to speed up [TorchRec JaggedTensor](https://pytorch.org/torchrec/torchrec.sparse.html). The purpose of JaggedTensor is to handle the case where one dimension of the input data is \u201cjagged\u201d, meaning that each consecutive row in a given dimension may be a different length, which is often the case with sparse feature inputs in recommendation systems. The internal representation is shown below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We added ops for [converting jagged tensors from sparse to dense formats](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L982) [and back](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L968), performing [matrix multiplications with jagged tensors](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L996), and [elementwise", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "ops](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L995).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Optimized permute102-baddbmm-permute102", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "It is difficult to fuse various matrix multiplications where the batch size is not the batch size of the model, switching the batch dimension is a quick solution. We created the [permute102_baddbmm_permute102](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/sparse_ops_cpu.cpp#L2401) operation that switches the first and the second dimension, performs the batched matrix multiplication and then switches back. Currently we only support forward pass with FP16 data type and will support FP32 type and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "backward pass in the future.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Optimized index_select for dim 0 index selection", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "index_select is normally used as part of a sparse operation. While PyTorch supports a generic index_select for an arbitrary-dimension index selection, its performance for a special case like the dim 0 index selection is suboptimal. For this reason, we implement a [specialized index_select for dim 0](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/sparse_ops_cpu.cpp#L2421). In some cases, we have observed 1.4x performance gain from FBGEMM\u2019s index_select compared to the one from PyTorch (using", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "uniform index distribution).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "More about the implementation of influential instances can be found on our [GitHub](https://github.com/pytorch/captum/tree/master/captum/influence) page and [tutorials](https://captum.ai/tutorials/TracInCP_Tutorial).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Summary:\n- **TorchVision** - Added multi-weight support API, new architectures, model variants, and pretrained weight. See the release notes [here](https://github.com/pytorch/vision/releases).\n- **TorchAudio** - Introduced beta features including a streaming API, a CTC beam search decoder, and new beamforming modules and methods. See the release notes [here](https://github.com/pytorch/audio/releases).\n- **TorchText** - Extended support for scriptable BERT tokenizer and added datasets for GLUE benchmark. See the release notes [here](https://github.com/pytorch/text/releases).\n- **TorchRec** - Added EmbeddingModule benchmarks, examples for TwoTower Retrieval, inference and sequential embeddings, metrics, improved planner and demonstrated integration with production components. See the release notes [here](https://github.com/pytorch/torchrec/releases).\n- **TorchX** - Launch PyTorch trainers developed on local workspaces onto five different types of schedulers. See the release notes [here](https://github.com/pytorch/torchx/blob/main/CHANGELOG.md?plain=1#L3).\n- **FBGemm** - Added and improved kernels for Recommendation Systems inference workloads, including table batched embedding bag, jagged tensor operations, and other special-case optimizations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "## TorchVision v0.13\n\n### Multi-weight support API\n\nTorchVision v0.13 offers a new [Multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) for loading different weights to the existing model builder methods:\n\n```python\nfrom torchvision.models import *\n\n# Old weights with accuracy 76.130%\nresnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\n\n# New weights with accuracy 80.858%\nresnet50(weights=ResNet50_Weights.IMAGENET1K_V2)\n\n# Best available weights (currently alias for IMAGENET1K_V2)\n# Note that these weights may change across versions\nresnet50(weights=ResNet50_Weights.DEFAULT)\n\n# Strings are also supported\nresnet50(weights=\"IMAGENET1K_V2\")\n\n# No weights - random initialization\nresnet50(weights=None)\n```\n\nThe new API bundles along with the weights important details such as the preprocessing transforms and meta-data such as labels. Here is how to make the most out of it:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torchvision.io import read_image\nfrom torchvision.models import resnet50, ResNet50_Weights\n\nimg = read_image(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n\n# Step 1: Initialize model with the best available weights\nweights = ResNet50_Weights.DEFAULT\nmodel = resnet50(weights=weights)\nmodel.eval()\n\n# Step 2: Initialize the inference transforms\npreprocess = weights.transforms()\n\n# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\n\n# Step 4: Use the model and print the predicted category\nprediction = model(batch).squeeze(0).softmax(0)\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\ncategory_name = weights.meta[\"categories\"][class_id]\nprint(f\"{category_name}: {100 * score:.1f}%\")\n```\n\nYou can read more about the new API in the [docs](https://pytorch.org/vision/0.13/models.html). To provide your feedback, please use this dedicated [Github issue](https://github.com/pytorch/vision/issues/5088).\n\n### New architectures and model variants", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "#### Classification\n\nThe [Swin Transformer](https://arxiv.org/abs/2103.14030) and [EfficienetNetV2](https://arxiv.org/abs/2104.00298) are two popular classification models which are often used for downstream vision tasks. This release includes 6 pre-trained weights for their classification variants. Here is how to use the new models:\n\n```python\nimport torch\nfrom torchvision.models import *\n\nimage = torch.rand(1, 3, 224, 224)\nmodel = swin_t(weights=\"DEFAULT\").eval()\nprediction = model(image)\n\nimage = torch.rand(1, 3, 384, 384)\nmodel = efficientnet_v2_s(weights=\"DEFAULT\").eval()\nprediction = model(image)\n```\n\nIn addition to the above, we also provide new variants for existing architectures such as ShuffleNetV2, ResNeXt and MNASNet. The accuracies of all the new pre-trained models obtained on ImageNet-1K are seen below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "| **Model** | **Acc@1** | **Acc@5** |\n|--------------------------------|-----------|-----------|\n| swin_t | 81.474 | 95.776 |\n| swin_s | 83.196 | 96.36 |\n| swin_b | 83.582 | 96.64 |\n| efficientnet_v2_s | 84.228 | 96.878 |\n| efficientnet_v2_m | 85.112 | 97.156 |\n| efficientnet_v2_l | 85.808 | 97.788 |\n| resnext101_64x4d | 83.246 | 96.454 |\n| resnext101_64x4d (quantized) | 82.898 | 96.326 |\n| shufflenet_v2_x1_5 | 72.996 | 91.086 |\n| shufflenet_v2_x1_5 (quantized) | 72.052 | 0.700 |\n| shufflenet_v2_x2_0 | 76.230 | 93.006 |\n| shufflenet_v2_x2_0 (quantized) | 75.354 | 92.488 |\n| mnasnet0_75 | 71.180 | 90.496 |\n| mnas1_3 | 76.506 | 93.522 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We would like to thank Hu Ye for contributing to TorchVision the Swin Transformer implementation.\n\n#### (BETA) Object Detection and Instance Segmentation\n\nWe have introduced 3 new model variants for RetinaNet, FasterRCNN and MaskRCNN that include several [post-paper architectural optimizations](https://github.com/pytorch/vision/pull/5444) and improved training recipes. All models can be used similarly:\n\n```python\nimport torch\nfrom torchvision.models.detection import *\n\nimages = [torch.rand(3, 800, 600)]\nmodel = retinanet_resnet50_fpn_v2(weights=\"DEFAULT\")\n# model = fasterrcnn_resnet50_fpn_v2(weights=\"DEFAULT\")\n# model = maskrcnn_resnet50_fpn_v2(weights=\"DEFAULT\")\nmodel.eval()\nprediction = model(images)\n```\n\nBelow we present the metrics of the new variants on COCO val2017. In parenthesis we denote the improvement over the old variants:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "| **Model** | **Box mAP** | **Mask mAP** |\n|----------------------------|-------------|--------------|\n| retinanet_resnet50_fpn_v2 | 41.5 (+5.1) | - |\n| fasterrcnn_resnet50_fpn_v2 | 46.7 (+9.7) | - |\n| maskrcnn_resnet50_fpn_v2 | 47.4 (+9.5) | 41.8 (+7.2) |\n\nWe would like to thank Ross Girshick, Piotr Dollar, Vaibhav Aggarwal, Francisco Massa and Hu Ye for their past research and contributions to this work.\n\n### New pre-trained weights \n\n#### SWAG weights\n\nThe ViT and RegNet model variants offer new pre-trained [SWAG](https://arxiv.org/abs/2201.08371) (\u200b\u200bSupervised Weakly from hashtAGs) weights. One of the biggest of these models achieves a whopping 88.6% accuracy on ImageNet-1K. We currently offer two versions of the weights: 1) fine-tuned end-to-end weights on ImageNet-1K (highest accuracy) and 2) frozen trunk weights with a linear classifier fit on ImageNet-1K (great for transfer learning). Below we see the detailed accuracies of each model variant:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "| **Model Weights** | **Acc@1** | **Acc@5** |\n|--------------------------------------------------|-----------|-----------|\n| RegNet_Y_16GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 86.012 | 98.054 |\n| RegNet_Y_16GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 83.976 | 97.244 |\n| RegNet_Y_32GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 86.838 | 98.362 |\n| RegNet_Y_32GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 84.622 | 97.48 |\n| RegNet_Y_128GF_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.228 | 98.682 |\n| RegNet_Y_128GF_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 86.068 | 97.844 |\n| ViT_B_16_Weights.IMAGENET1K_SWAG_E2E_V1 | 85.304 | 97.65 |\n| ViT_B_16_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 81.886 | 96.18 |\n| ViT_L_16_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.064 | 98.512 |\n| ViT_L_16_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 85.146 | 97.422 |\n| ViT_H_14_Weights.IMAGENET1K_SWAG_E2E_V1 | 88.552 | 98.694 |\n| ViT_H_14_Weights.IMAGENET1K_SWAG_LINEAR_V1 | 85.708 | 97.73 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "The SWAG weights are released under the [Attribution-NonCommercial 4.0 International](https://github.com/facebookresearch/SWAG/blob/main/LICENSE) license. We would like to thank Laura Gustafson, Mannat Singh and Aaron Adcock for their work and support in making the weights available to TorchVision.\n\n#### Model Refresh\n\nThe release of the Multi-weight support API enabled us to refresh the most popular models and offer more accurate weights. We improved on average each model by ~3 points. The new recipe used was learned on top of ResNet50 and its details were covered on a [previous blog post](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "| **Model** | **Old weights** | **New weights** |\n|------------------------------|-----------------|-----------------|\n| efficientnet_b1 | 78.642 | 79.838 |\n| mobilenet_v2 | 71.878 | 72.154 |\n| mobilenet_v3_large | 74.042 | 75.274 |\n| regnet_y_400mf | 74.046 | 75.804 |\n| regnet_y_800mf | 76.42 | 78.828 |\n| regnet_y_1_6gf | 77.95 | 80.876 |\n| regnet_y_3_2gf | 78.948 | 81.982 |\n| regnet_y_8gf | 80.032 | 82.828 |\n| regnet_y_16gf | 80.424 | 82.886 |\n| regnet_y_32gf | 80.878 | 83.368 |\n| regnet_x_400mf | 72.834 | 74.864 |\n| regnet_x_800mf | 75.212 | 77.522 |\n| regnet_x_1_6gf | 77.04 | 79.668 |\n| regnet_x_3_2gf | 78.364 | 81.196 |\n| regnet_x_8gf | 79.344 | 81.682 |\n| regnet_x_16gf | 80.058 | 82.716 |\n| regnet_x_32gf | 80.622 | 83.014 |\n| resnet50 | 76.13 | 80.858 |\n| resnet50 (quantized) | 75.92 | 80.282 |\n| resnet101 | 77.374 | 81.886 |\n| resnet152 | 78.312 | 82.284 |\n| resnext50_32x4d | 77.618 | 81.198 |\n| resnext101_32x8d | 79.312 | 82.834 |\n| resnext101_32x8d (quantized) | 78.986 | 82.574 |\n| wide_resnet50_2 | 78.468 | 81.602 |\n| wide_resnet101_2 | 78.848 | 82.51 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We would like to thank Piotr Dollar, Mannat Singh and Hugo Touvron for their past research and contributions to this work.\n\n### New Augmentations, Layers and Losses\n\nThis release brings a bunch of new primitives which can be used to produce SOTA models. Some highlights include the addition of [AugMix](https://arxiv.org/abs/1912.02781) data-augmentation method, the [DropBlock](https://arxiv.org/abs/1810.12890) layer, the [cIoU/dIoU](https://arxiv.org/abs/1911.08287) loss and [many more](https://github.com/pytorch/vision/issues/5410). We would like to thank Aditya Oke, Abhijit Deo, Yassine Alouini and Hu Ye for contributing to the project and for helping us maintain TorchVision relevant and fresh.\n\n### Documentation", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We completely revamped our models documentation to make them easier to browse, and added various key information such as supported image sizes, or image pre-processing steps of pre-trained weights. We now have a [main model page](https://pytorch.org/vision/main/models.html) with various [summary tables](https://pytorch.org/vision/main/models.html#table-of-all-available-classification-weights) of available weights, and each model has a [dedicated page](https://pytorch.org/vision/main/models/resnet.html). Each model builder is also documented in their [own page](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html#torchvision.models.resnet50), with more details about the available weights, including accuracy, minimal image size, link to training recipes, and other valuable info. For comparison, our previous models docs are [here](https://pytorch.org/vision/0.12/models.html). To provide feedback on the new documentation, please use the dedicated [Github issue](https://github.com/pytorch/vision/issues/5511).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "## TorchAudio v0.12\n\n### (BETA) Streaming API\n\n

\n \n

\n\n\nStreamReader is TorchAudio\u2019s new I/O API. It is backed by FFmpeg\u2020, and allows users to:\n- Decode audio and video formats, including MP4 and AAC\n- Handle input forms, such as local files, network protocols, microphones, webcams, screen captures and file-like objects\n- Iterate over and decode chunk-by-chunk, while changing the sample rate or frame rate\n- Apply audio and video filters, such as low-pass filter and image scaling\n- Decode video with Nvidia's hardware-based decoder (NVDEC)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "For usage details, please check out the [documentation](https://pytorch.org/audio/0.12.0/io.html#streamreader) and tutorials:\n- [Media Stream API - Pt.1](https://pytorch.org/audio/0.12.0/tutorials/streaming_api_tutorial.html)\n- [Media Stream API - Pt.2](https://pytorch.org/audio/0.12.0/tutorials/streaming_api2_tutorial.html)\n- [Online ASR with Emformer RNN-T](https://pytorch.org/audio/0.12.0/tutorials/online_asr_tutorial.html)\n- [Device ASR with Emformer RNN-T](https://pytorch.org/audio/0.12.0/tutorials/device_asr.html)\n- [Accelerated Video Decoding with NVDEC](https://pytorch.org/audio/0.12.0/hw_acceleration_tutorial.html)\n\n\u2020 To use StreamReader, FFmpeg libraries are required. Please install FFmpeg. The coverage of codecs depends on how these libraries are configured. TorchAudio official binaries are compiled to work with FFmpeg 4 libraries; FFmpeg 5 can be used if TorchAudio is built from source.\n\n### (BETA) CTC Beam Search Decoder", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "TorchAudio integrates the wav2letter CTC beam search decoder from [Flashlight](https://arxiv.org/pdf/2201.12465.pdf) ([GitHub](https://github.com/flashlight/flashlight)). The addition of this inference time decoder enables running end-to-end CTC ASR evaluation using TorchAudio utils.\n\nCustomizable lexicon and lexicon-free decoders are supported, and both are compatible with KenLM n-gram language models or without using a language model. TorchAudio additionally supports downloading token, lexicon, and pretrained KenLM files for the LibriSpeech dataset.\n\nFor usage details, please check out the [documentation](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder) and [ASR inference tutorial](https://pytorch.org/audio/0.12.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html).\n\n### (BETA) New Beamforming Modules and Methods", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "To improve flexibility in usage, the release adds two new beamforming modules under torchaudio.transforms: [SoudenMVDR](https://pytorch.org/audio/0.12.0/transforms.html#soudenmvdr) and [RTFMVDR](https://pytorch.org/audio/0.12.0/transforms.html#rtfmvdr). The main differences from [MVDR](https://pytorch.org/audio/0.11.0/transforms.html#mvdr) are:\n- Use power spectral density (PSD) and relative transfer function (RTF) matrices as inputs instead of time-frequency masks. The module can be integrated with neural networks that directly predict complex-valued STFT coefficients of speech and noise\n- Add \\'reference_channel\\' as an input argument in the forward method, to allow users to select the reference channel in model training or dynamically change the reference channel in inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Besides the two modules, new function-level beamforming methods are added under torchaudio.functional. These include:\n- [psd](https://pytorch.org/audio/0.12.0/functional.html#psd)\n- [mvdr_weights_souden](https://pytorch.org/audio/0.12.0/functional.html#mvdr-weights-souden)\n- [mvdr_weights_rtf](https://pytorch.org/audio/0.12.0/functional.html#mvdr-weights-rtf)\n- [rtf_evd](https://pytorch.org/audio/0.12.0/functional.html#rtf-evd)\n- [rtf_power](https://pytorch.org/audio/0.12.0/functional.html#rtf-power)\n- [apply_beamforming](https://pytorch.org/audio/0.12.0/functional.html#apply-beamforming)\n\nFor usage details, please check out the documentation at [torchaudio.transforms](https://pytorch.org/audio/0.12.0/transforms.html#multi-channel) and [torchaudio.functional](https://pytorch.org/audio/0.12.0/functional.html#multi-channel) and the [Speech Enhancement with MVDR Beamforming tutorial](https://pytorch.org/audio/0.12.0/tutorials/mvdr_tutorial.html).\n\n## TorchText v0.13\n\n### Glue Datasets", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We increased the number of datasets in TorchText from 22 to 30 by adding the remaining 8 datasets from the GLUE benchmark (SST-2 was already supported). The complete list of GLUE datasets is as follows:\n- [CoLA](https://nyu-mll.github.io/CoLA/) ([paper](https://arxiv.org/pdf/1805.12471.pdf)): Single sentence binary classification acceptability task\n- [SST-2](https://nlp.stanford.edu/sentiment/) ([paper](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf)): Single sentence binary classification sentiment task\n- [MRPC](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf) ([paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/I05-50025B15D.pdf)): Dual sentence binary classification paraphrase task\n- [QQP](https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs): Dual sentence binary classification paraphrase task\n- [STS-B](https://ixa2.si.ehu.eus/stswiki/index.php/STSbenchmark) ([paper](https://aclanthology.org/S17-2001.pdf)): Single sentence to float regression sentence similarity task\n- [MNLI](https://cims.nyu.edu/~sbowman/multinli/) ([paper](https://cims.nyu.edu/~sbowman/multinli/paper.pdf)): Sentence ternary classification NLI task\n- [QNLI](https://gluebenchmark.com/) ([paper](https://arxiv.org/pdf/1804.07461.pdf)): Sentence binary classification QA and NLI tasks\n- [RTE](https://aclweb.org/aclwiki/Recognizing_Textual_Entailment) ([paper](https://arxiv.org/pdf/2010.03061.pdf)): Dual sentence binary classification NLI task\n- [WNLI](https://cs.nyu.edu/~davise/papers/WinogradSchemas/WS.html) ([paper](http://commonsensereasoning.org/2011/papers/Levesque.pdf)): Dual sentence binary classification coreference and NLI tasks", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Scriptable BERT Tokenizer\n\nTorchText has extended support for scriptable tokenizer by adding the WordPiece tokenizer used in BERT. It is one of the commonly used algorithms for splitting input text into sub-words units and was introduced in [Japanese and Korean Voice Search (Schuster et al., 2012)](https://static.googleusercontent.com/media/research.google.com/ja//pubs/archive/37842.pdf). \n\nTorchScriptabilty support would allow users to embed the BERT text-preprocessing natively in C++ without needing the support of python runtime. As TorchText now supports the CMAKE build system to natively link torchtext binaries with application code, users can easily integrate BERT tokenizers for deployment needs.\n\nFor usage details, please refer to the corresponding [documentation](https://pytorch.org/text/main/transforms.html#torchtext.transforms.BERTTokenizer).\n\n## TorchRec v0.2.0\n\n### EmbeddingModule + DLRM benchmarks", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "A set of [benchmarking tests](https://github.com/pytorch/torchrec/tree/main/benchmarks), showing performance characteristics of TorchRec\u2019s base modules and research models built out of TorchRec.\n\n### TwoTower Retrieval Example, with FAISS\n\nWe provide an [example](https://github.com/pytorch/torchrec/tree/main/examples/retrieval) demonstrating training a distributed TwoTower (i.e. User-Item) Retrieval model that is sharded using TorchRec. The projected item embeddings are added to an IVFPQ FAISS index for candidate generation. The retrieval model and KNN lookup are bundled in a Pytorch model for efficient end-to-end retrieval.\n\n### Integrations", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Integrations\n\nWe demonstrate that TorchRec works out of the box with many components commonly used alongside PyTorch models in production like systems, such as \n- [Training](https://github.com/pytorch/torchrec/tree/main/examples/ray) a TorchRec model on Ray Clusters utilizing the Torchx Ray scheduler\n- [Preprocessing](https://github.com/pytorch/torchrec/tree/main/torchrec/datasets/scripts/nvt) and DataLoading with NVTabular on DLRM\n- [Training](https://github.com/pytorch/torchrec/tree/main/examples/torcharrow) a TorchRec model with on-the-fly preprocessing with TorchArrow showcasing RecSys domain UDFs\n\n### Sequential Embeddings Example: Bert4Rec\n\nWe provide an [example](https://github.com/pytorch/torchrec/tree/main/examples/bert4rec), using TorchRec, that reimplements the [BERT4REC](https://arxiv.org/abs/1904.06690) paper, showcasing EmbeddingCollection for non-pooled embeddings. Using DistributedModelParallel we see a 35% QPS gain over conventional data parallelism.\n\n### (Beta) Planner", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Planner\n\nThe TorchRec library includes a built-in [planner](https://pytorch.org/torchrec/torchrec.distributed.planner.html) that selects near optimal sharding plan for a given model. The planner attempts to identify the best sharding plan by evaluating a series of proposals which are statically analyzed and fed into an integer partitioner. The planner is able to automatically adjust plans for a wide range of hardware setups, allowing users to scale performance seamlessly from local development environment to large scale production hardware. See this [notebook](https://github.com/pytorch/torchrec/blob/main/torchrec/distributed/planner/Planner_Introduction.ipynb) for a more detailed tutorial.\n\n### (Beta) Inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Inference\n\n[TorchRec Inference](https://github.com/pytorch/torchrec/tree/main/torchrec/inference) is a C++ library that supports multi-gpu inference. The TorchRec library is used to shard models written and packaged in Python via torch.package (an alternative to TorchScript). The torch.deploy library is used to serve inference from C++ by launching multiple Python interpreters carrying the packaged model, thus subverting the GIL. Two models are provided as examples: [DLRM multi-GPU](https://github.com/pytorch/torchrec/blob/main/examples/inference/dlrm_predict.py) (sharded via TorchRec) and [DLRM single-GPU](https://github.com/pytorch/torchrec/blob/main/examples/inference/dlrm_predict_single_gpu.py).\n\n### (Beta) RecMetrics", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "RecMetrics is a [metrics](https://github.com/pytorch/torchrec/tree/main/torchrec/metrics) library that collects common utilities and optimizations for Recommendation models. It extends [torchmetrics](https://torchmetrics.readthedocs.io/en/stable/).\n- A centralized metrics module that allows users to add new metrics\n- Commonly used metrics, including AUC, Calibration, CTR, MSE/RMSE, NE & Throughput\n- Optimization for metrics related operations to reduce the overhead of metric computation\n- Checkpointing\n\n### (Prototype) Single process Batched + Fused Embeddings", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Previously TorchRec\u2019s abstractions (EmbeddingBagCollection/EmbeddingCollection) over FBGEMM kernels, which provide benefits such as table batching, optimizer fusion, and UVM placement, could only be used in conjunction with DistributedModelParallel. We\u2019ve decoupled these notions from sharding, and introduced the [FusedEmbeddingBagCollection](https://github.com/pytorch/torchrec/blob/eb1247d8a2d16edc4952e5c2617e69acfe5477a5/torchrec/modules/fused_embedding_modules.py#L271), which can be used as a standalone module, with all of the above features, and can also be sharded.\n\n## TorchX v0.2.0\n\nTorchX is a job launcher that makes it easier to run PyTorch in distributed training clusters with many scheduler integrations including Kubernetes and Slurm. We're excited to release TorchX 0.2.0 with a number of improvements. TorchX is currently being used in production in both on-premise and cloud environments.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Check out the [quickstart](https://pytorch.org/torchx/main/quickstart.html) to start launching local and remote jobs.\n\n### Workspaces\n\nTorchX [now supports workspaces](https://pytorch.org/torchx/main/workspace.html) which allows users to easily launch training jobs using their local workspace. TorchX can automatically build a patch with your local training code on top of a base image to minimize iteration time and time to training.\n\n### .torchxconfig\n\nSpecifying options in [.torchxconfig](https://pytorch.org/torchx/latest/runner.config.html) saves you from having to type long CLI commands each time you launch a job. You can also define project level generic configs and drop a config file in your home directory for user-level overrides.\n\n### Expanded Scheduler Support\n\nTorchX now supports [AWS Batch](https://pytorch.org/torchx/main/schedulers/aws_batch.html) and [Ray (experimental)](https://pytorch.org/torchx/main/schedulers/ray.html) schedulers in addition to our existing integrations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Distributed Training On All Schedulers\n\nThe TorchX dist.ddp component now works on all schedulers without any configuration. Distributed training workers will automatically discover each other when using [torchelastic](https://pytorch.org/docs/stable/distributed.elastic.html) via [the builtin dist.ddp component](https://pytorch.org/torchx/main/components/distributed.html).\n\n### Hyper Parameter Optimization\n\nTorchX [integrates with Ax](https://ax.dev/versions/latest/api/runners.html#module-ax.runners.torchx) to let you scale hyper-parameter optimizations (HPO) by launching the search trials onto remote clusters.\n\n### File and Device Mounts\n\nTorchX now supports [remote filesystem mounts and custom devices](https://pytorch.org/torchx/main/specs.html#mounts). This enables your PyTorch jobs to efficiently access cloud storage such as NFS or Lustre. The device mounts enables usage of network accelerators like Infiniband and custom inference/training accelerators.\n\n## FBGemm v0.2.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "## FBGemm v0.2.0\n\nThe FBGEMM library contains optimized kernels meant to improve the performance of PyTorch workloads. We\u2019ve added a number of new features and optimizations over the last few months that we are excited to report.\n\n### Inference Table Batched Embedding (TBE)\n\nThe [table batched embedding bag](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L1541) (TBE) operator is an important base operation for embedding lookup for recommendation system inference on GPU. We added the following enhancements for performance and flexibility:\n\nAlignment restriction removed\n- Embedding dimension \\* data type size had to be multiple of 4B before and now, it is 1B.\n\nUnified Virtual Memory (UVM) caching kernel optimizations\n- UVM caching kernels now scale linearly with # of tables using UVM caching. Previously, it was having similar overhead as all tables using UVM caching\n- UVM caching kernel overhead is much smaller than before", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Inference FP8 Table Batched Embedding (TBE) \n\nThe [table batched embedding bag](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L1541) (TBE) previously supported FP32, FP16, INT8, INT4, and INT2 embedding weight types. While these weight types work well in many models, we integrate FP8 weight types (in both GPU and CPU operations) to allow for numerical and performance evaluations of FP8 in our models. Compared to INT8, FP8 does not require the additional bias and scale storage and calculations. Additionally, the next generation of H100 GPUs has the FP8 support on Tensor Core (mainly matmul ops).\n\n### Jagged Tensor Kernels", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We added optimized kernels to speed up [TorchRec JaggedTensor](https://pytorch.org/torchrec/torchrec.sparse.html). The purpose of JaggedTensor is to handle the case where one dimension of the input data is \u201cjagged\u201d, meaning that each consecutive row in a given dimension may be a different length, which is often the case with sparse feature inputs in recommendation systems. The internal representation is shown below:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We added ops for [converting jagged tensors from sparse to dense formats](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L982) [and back](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L968), performing [matrix multiplications with jagged tensors](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L996), and [elementwise ops](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/jagged_tensor_ops_cpu.cpp#L995).\n \n### Optimized permute102-baddbmm-permute102", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "It is difficult to fuse various matrix multiplications where the batch size is not the batch size of the model, switching the batch dimension is a quick solution. We created the [permute102_baddbmm_permute102](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/sparse_ops_cpu.cpp#L2401) operation that switches the first and the second dimension, performs the batched matrix multiplication and then switches back. Currently we only support forward pass with FP16 data type and will support FP32 type and backward pass in the future.\n\n### Optimized index_select for dim 0 index selection", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "index_select is normally used as part of a sparse operation. While PyTorch supports a generic index_select for an arbitrary-dimension index selection, its performance for a special case like the dim 0 index selection is suboptimal. For this reason, we implement a [specialized index_select for dim 0](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/src/sparse_ops_cpu.cpp#L2421). In some cases, we have observed 1.4x performance gain from FBGEMM\u2019s index_select compared to the one from PyTorch (using uniform index distribution).\n\nMore about the implementation of influential instances can be found on our [GitHub](https://github.com/pytorch/captum/tree/master/captum/influence) page and [tutorials](https://captum.ai/tutorials/TracInCP_Tutorial).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} {"page_content": "Thanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'The torch.linalg module: Accelerated Linear Algebra with Autograd in PyTorch'\nauthor: Mike Ruberry, Ivan Yashchuk, Xiao Wang, Mario Lezcano and Natalia Gimelshein\nfeatured-img: 'assets/images/cholesky-decomposition.png'\n---", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "Linear algebra is essential to deep learning and scientific computing, and it\u2019s always been a core part of PyTorch. PyTorch 1.9 extends PyTorch\u2019s support for linear algebra operations with the ```torch.linalg``` module. This module, documented [here](https://pytorch.org/docs/master/linalg.html?highlight=linalg#module-torch.linalg), has 26 operators, including faster and easier to use versions of older PyTorch operators, every function from [NumPy\u2019s linear algebra", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "module](https://numpy.org/doc/stable/reference/routines.linalg.html) extended with accelerator and autograd support, and a few operators that are completely new. This makes the ```torch.linalg``` immediately familiar to NumPy users and an exciting update to PyTorch\u2019s linear algebra support.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "# NumPy-like linear algebra in PyTorch\n\nIf you\u2019re familiar with NumPy\u2019s linear algebra module then it\u2019ll be easy to start using ```torch.linalg```. In most cases it\u2019s a drop-in replacement. Let\u2019s looking at drawing samples from a [multivariate normal distribution](https://en.wikipedia.org/wiki/Multivariate_normal_distribution) using the [Cholesky decomposition](https://en.wikipedia.org/wiki/Cholesky_decomposition) as a motivating example to demonstrate this:\n\n```python\nimport numpy as np", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "# Creates inputs\nnp.random.seed(0)\nmu_np = np.random.rand(4)\nL = np.random.rand(4, 4)\n# Covariance matrix sigma is positive-definite\nsigma_np = L @ L.T + np.eye(4)\nnormal_noise_np = np.random.standard_normal(mu_np.size)\n\ndef multivariate_normal_sample_np(mu, sigma, normal_noise):\n return mu + np.linalg.cholesky(sigma) @ normal_noise\n\nprint(\"Random sample: \", \n multivariate_normal_sample_np(mu_np, sigma_np, normal_noise_np))\n: Random sample: [2.9502426 1.78518077 1.83168697 0.90798228]", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "Now let\u2019s see the same sampler implemented in PyTorch:\n\n```python\nimport torch\n\ndef multivariate_normal_sample_torch(mu, sigma, normal_noise):\n return mu + torch.linalg.cholesky(sigma) @ normal_noise", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "The two functions are identical, and we can validate their behavior by calling the function with the same arguments wrapped as PyTorch tensors:\n\n```python\n# NumPy arrays are wrapped as tensors and share their memory\nmu_torch = torch.from_numpy(mu_np)\nsigma_torch = torch.from_numpy(sigma_np)\nnormal_noise_torch = torch.from_numpy(normal_noise_np)\n\nmultivariate_normal_sample_torch(mu_torch, sigma_torch, normal_noise_torch)\n: tensor([2.9502, 1.7852, 1.8317, 0.9080], dtype=torch.float64)", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "The only difference is in how PyTorch prints tensors by default.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "The Cholesky decomposition can also help us quickly compute the probability density function of the non-degenerate multivariate normal distribution. One of the expensive terms in that computation is the square root of the determinant of the covariance matrix. Using [properties of the determinant](https://en.wikipedia.org/wiki/Determinant#Properties_of_the_determinant) and the Cholesky decomposition we can calculate the same result faster than the naive computation, however. Here\u2019s the NumPy program that", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "demonstrates this:", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "```python\nsqrt_sigma_det_np = np.sqrt(np.linalg.det(sigma_np))\nsqrt_L_det_np = np.prod(np.diag(np.linalg.cholesky(sigma_np)))\n\nprint(\"|sigma|^0.5 = \", sqrt_sigma_det_np)\n: |sigma|^0.5 = 4.237127491242027\n \nprint(\"|L| = \", sqrt_L_det_np)\n: |L| = 4.237127491242028", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "And here\u2019s the same validation in PyTorch:\n\n```python\nsqrt_sigma_det_torch = torch.sqrt(torch.linalg.det(sigma_torch))\nsqrt_L_det_torch = torch.prod(torch.diag(torch.linalg.cholesky(sigma_torch)))\n\nprint(\"|sigma|^0.5 = \", sqrt_sigma_det_torch)\n: |sigma|^0.5 = tensor(4.2371, dtype=torch.float64) \n\nprint(\"|L| = \", sqrt_L_det_torch)\n: |L| = tensor(4.2371, dtype=torch.float64)", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "We can measure the difference in run time using PyTorch\u2019s built-in benchmark utility:\n\n```python\nimport torch.utils.benchmark as benchmark\n\nt0 = benchmark.Timer(\n stmt='torch.sqrt(torch.linalg.det(sigma))',\n globals={'sigma': sigma_torch})\n\nt1 = benchmark.Timer(\n stmt='torch.prod(torch.diag(torch.linalg.cholesky(sigma)))',\n globals={'sigma': sigma_torch})\n\nprint(t0.timeit(100))\n: torch.sqrt(torch.linalg.det(sigma))\n 80.80 us\n 1 measurement, 100 runs , 1 thread", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "print(t1.timeit(100))\n: torch.prod(torch.diag(torch.linalg.cholesky(sigma)))\n 11.56 us\n 1 measurement, 100 runs , 1 thread\n ```\n \nDemonstrating that the approach using the Cholesky decomposition can be significantly faster. Behind the scenes, PyTorch\u2019s linear algebra module uses OpenBLAS or MKL implementations of the LAPACK standard to maximize its CPU performance.\n\n# Autograd Support", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "PyTorch\u2019s linear algebra module doesn\u2019t just implement the same functions as NumPy\u2019s linear algebra module (and a few more), it also extends them with autograd and CUDA support.\n\nLet\u2019s look at a very simple program that just computes an inverse and the gradient of that operation to show how autograd works:\n\n```python\nt = torch.tensor(((1, 2), (3, 4)), dtype=torch.float32, requires_grad=True)\n\ninv = torch.linalg.inv(t)\ninv.backward(torch.ones_like(inv))", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "print(t.grad)\n: tensor([[-0.5000, 0.5000],\n [ 0.5000, -0.5000]])", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "We can mimic the same computation in NumPy by defining the autograd formula ourselves:\n\n```python\na = np.array(((1, 2), (3, 4)), dtype=np.float32)\n\ninv_np = np.linalg.inv(a)\n\ndef inv_backward(result, grad):\n return -(result.transpose(-2, -1) @ (grad @ result.transpose(-2, -1)))\ngrad_np = inv_backward(inv_np, np.ones_like(inv_np))\n\nprint(grad_np)\n: [[-0.5 0.5]\n [ 0.5 -0.5]]", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "Of course, as programs become more complicated it\u2019s convenient to have builtin autograd support, and PyTorch\u2019s linear algebra module supports both real and complex autograd.\n\n# CUDA Support", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "Support for autograd and accelerators, like CUDA devices, is a core part of PyTorch. The ```torch.linalg``` module was developed with NVIDIA\u2019s PyTorch and cuSOLVER teams, who helped optimize its performance on CUDA devices with the cuSOLVER, cuBLAS, and MAGMA libraries. These improvements make PyTorch\u2019s CUDA linear algebra operations faster than ever. For example, let\u2019s look at the performance of PyTorch 1.9\u2019s ```torch.linalg.cholesky``` vs. PyTorch 1.8\u2019s (now deprecated) ```torch.cholesky```:", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n(The above charts were created using an Ampere A100 GPU with CUDA 11.3, cuSOLVER 11.1.1.58, and MAGMA 2.5.2. Matrices are in double precision.)", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "These charts show that performance has increased significantly on larger matrices, and that batched performance is better across the board. Other linear algebra operations, including ```torch.linalg.qr``` and ```torch.linalg.lstsq```, have also had their CUDA performance improved.\n\n# Beyond NumPy", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "In addition to offering all the functions in NumPy\u2019s linear algebra module with support for autograd and accelerators, ```torch.linalg``` has a few new functions of its own. NumPy\u2019s ```linalg.norm``` does not allow users to compute vector norms over arbitrary subsets of dimensions, so to enable this functionality we added ```torch.linalg.vector_norm```. We\u2019ve also started modernizing other linear algebra functionality in PyTorch, so we created ```torch.linalg.householder_product``` to replace the older", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "```torch.orgqr```, and we plan to continue adding more linear algebra functionality in the future, too.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "# The Future of Linear Algebra in PyTorch", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "The ```torch.linalg``` module is fast and familiar with great support for autograd and accelerators. It\u2019s already being used in libraries like [botorch](https://github.com/pytorch/botorch), too. But we\u2019re not stopping here. We plan to continue updating more of PyTorch\u2019s existing linear algebra functionality (like ```torch.lobpcg```) and offering more support for low rank and sparse linear algebra. We also want to hear your feedback on how we can improve, so start a conversation on the", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "[forum](https://discuss.pytorch.org/) or file an issue on our [Github](https://github.com/pytorch/pytorch) and share your thoughts.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "We look forward to hearing from you and seeing what the community does with PyTorch\u2019s new linear algebra functionality!", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Democratizing AI with PyTorch Foundation and ROCm\u2122 support for PyTorch\"\nauthor: AMD\n---\n\n![AMD Founding Member](/assets/images/2023-02-14-democratizing-ai-with-pytorch-1.png){:width=\"50%\" style=\"display:block; margin-left:auto; margin-right:auto\"}", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Last year, Meta announced that [PyTorch](https://pytorch.org/) joined the Linux Foundation as a neutral home for growing the machine learning project and community with AMD representation as a part of the founding membership and governing board.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[PyTorch Foundation\u2019s](https://pytorch.org/foundation) mission is to drive AI adoption by democratizing its software ecosystem through open source principles aligning with the AMD core principle of an Open software ecosystem. AMD strives to foster innovation through the support for latest generations of hardware, tools, libraries, and other components to simplify and accelerate adoption of AI across a broad range of scientific discoveries.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n
\n

", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "AMD, along with key PyTorch codebase developers (including those at Meta AI), delivered a set of updates to the ROCm\u2122 open software ecosystem that brings stable support for AMD Instinct\u2122 accelerators as well as many Radeon\u2122 GPUs. This now gives PyTorch developers the ability to build their next great AI solutions leveraging AMD GPU", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "accelerators & ROCm. The support from PyTorch community in identifying gaps, prioritizing key updates, providing feedback for performance optimizing and supporting our journey from \u201cBeta\u201d to \u201cStable\u201d was immensely helpful and we deeply appreciate the strong collaboration between the two teams at AMD and PyTorch. The move for ROCm support from \u201cBeta\u201d to \u201cStable\u201d came in the PyTorch 1.12 release (June 2022) brings the added support to easily run PyTorch on native environment without having to configure custom", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "dockers. This is a sign of confidence about the quality of support and performance of PyTorch using AMD Instinct and ROCm. The results of these collaborative efforts are evident in the performance measured on key industry benchmarks like Microsoft\u2019s SuperBench shown below in Graph 1.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n
\n
\n

", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\u201cWe are excited to see the significant impact of developers at AMD to contribute to and extend features within PyTorch to make AI models run in a more performant, efficient, and scalable way. A great example of this is the thought-leadership around unified memory approaches between the framework and future hardware systems, and we look forward to seeing that feature progress.\u201d
\n- Soumith Chintala, PyTorch lead-maintainer and Director of Engineering, Meta AI\n

\n
\n
", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The progressive improvements on both the AMD CDNA\u2122 architecture as well as ROCm and PyTorch shows single GPU model throughput increase from AMD Instinct MI100 to the latest generation AMD Instinct MI200 family GPUs going from ROCm 4.2 to ROCm 5.3 and from PyTorch 1.7 to PyTorch 1.12.\n\n![Graph 1: ML model performance over generation using Microsoft Superbench Suite](/assets/images/2023-02-14-democratizing-ai-with-pytorch-2.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Graph 1: ML model performance over generation using Microsoft Superbench Suite 1, 2, 3\n\n\nBelow are a few of the key updates for ROCm support since the PyTorch 1.12 release", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Full Continuous Integration (CI) for ROCm on PyTorch\n\nWith the ROCm support for PyTorch move from \u201cBeta\u201d to \u201cStable,\u201d all the functions and features commits are now verified through a full Continuous Integration (CI) process. The CI process helps ensure the proper build and test process ahead of an expected Docker and PIP wheel release with stable commits forthcoming.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Support for [Kineto Profiler](https://github.com/pytorch/kineto)\n\nThe addition of Kineto profiler support to ROCm now helps developers and users understand performance bottlenecks through effective diagnosis and profiling tools. The tool also provides recommendations to improve known issues and visualization through TensorBoard UI.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Key PyTorch Libraries support added\n\nPyTorch ecosystem libraries like [TorchText](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) (Text classification), [TorchRec](https://pytorch.org/torchrec/) (libraries for recommender systems - RecSys), [TorchVision](https://pytorch.org/vision/stable/index.html) (Computer Vision), [TorchAudio](https://pytorch.org/audio/stable/index.html) (audio and signal processing) are fully supported since ROCm 5.1 and upstreamed with PyTorch 1.12.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Key libraries provided with the ROCm software stack including [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) (Convolution models), [RCCL](https://github.com/ROCmSoftwarePlatform/rccl) (ROCm Collective Communications) and [rocBLAS](https://github.com/ROCmSoftwarePlatform/rocBLAS) (BLAS for transformers) were further optimized to offer new potential efficiencies and higher performance.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'The torch.linalg module: Accelerated Linear Algebra with Autograd in PyTorch'\nauthor: Mike Ruberry, Ivan Yashchuk, Xiao Wang, Mario Lezcano and Natalia Gimelshein\nfeatured-img: 'assets/images/cholesky-decomposition.png'\n---\n\nLinear algebra is essential to deep learning and scientific computing, and it\u2019s always been a core part of PyTorch. PyTorch 1.9 extends PyTorch\u2019s support for linear algebra operations with the ```torch.linalg``` module. This module, documented [here](https://pytorch.org/docs/master/linalg.html?highlight=linalg#module-torch.linalg), has 26 operators, including faster and easier to use versions of older PyTorch operators, every function from [NumPy\u2019s linear algebra module](https://numpy.org/doc/stable/reference/routines.linalg.html) extended with accelerator and autograd support, and a few operators that are completely new. This makes the ```torch.linalg``` immediately familiar to NumPy users and an exciting update to PyTorch\u2019s linear algebra support.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "# NumPy-like linear algebra in PyTorch\n\nIf you\u2019re familiar with NumPy\u2019s linear algebra module then it\u2019ll be easy to start using ```torch.linalg```. In most cases it\u2019s a drop-in replacement. Let\u2019s looking at drawing samples from a [multivariate normal distribution](https://en.wikipedia.org/wiki/Multivariate_normal_distribution) using the [Cholesky decomposition](https://en.wikipedia.org/wiki/Cholesky_decomposition) as a motivating example to demonstrate this:\n\n```python\nimport numpy as np\n\n# Creates inputs\nnp.random.seed(0)\nmu_np = np.random.rand(4)\nL = np.random.rand(4, 4)\n# Covariance matrix sigma is positive-definite\nsigma_np = L @ L.T + np.eye(4)\nnormal_noise_np = np.random.standard_normal(mu_np.size)\n\ndef multivariate_normal_sample_np(mu, sigma, normal_noise):\n return mu + np.linalg.cholesky(sigma) @ normal_noise\n\nprint(\"Random sample: \", \n multivariate_normal_sample_np(mu_np, sigma_np, normal_noise_np))\n: Random sample: [2.9502426 1.78518077 1.83168697 0.90798228]\n```", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "Now let\u2019s see the same sampler implemented in PyTorch:\n\n```python\nimport torch\n\ndef multivariate_normal_sample_torch(mu, sigma, normal_noise):\n return mu + torch.linalg.cholesky(sigma) @ normal_noise\n```\n\nThe two functions are identical, and we can validate their behavior by calling the function with the same arguments wrapped as PyTorch tensors:\n\n```python\n# NumPy arrays are wrapped as tensors and share their memory\nmu_torch = torch.from_numpy(mu_np)\nsigma_torch = torch.from_numpy(sigma_np)\nnormal_noise_torch = torch.from_numpy(normal_noise_np)\n\nmultivariate_normal_sample_torch(mu_torch, sigma_torch, normal_noise_torch)\n: tensor([2.9502, 1.7852, 1.8317, 0.9080], dtype=torch.float64)\n```\n\nThe only difference is in how PyTorch prints tensors by default.", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "The Cholesky decomposition can also help us quickly compute the probability density function of the non-degenerate multivariate normal distribution. One of the expensive terms in that computation is the square root of the determinant of the covariance matrix. Using [properties of the determinant](https://en.wikipedia.org/wiki/Determinant#Properties_of_the_determinant) and the Cholesky decomposition we can calculate the same result faster than the naive computation, however. Here\u2019s the NumPy program that demonstrates this:\n\n```python\nsqrt_sigma_det_np = np.sqrt(np.linalg.det(sigma_np))\nsqrt_L_det_np = np.prod(np.diag(np.linalg.cholesky(sigma_np)))\n\nprint(\"|sigma|^0.5 = \", sqrt_sigma_det_np)\n: |sigma|^0.5 = 4.237127491242027\n \nprint(\"|L| = \", sqrt_L_det_np)\n: |L| = 4.237127491242028\n```\n\nAnd here\u2019s the same validation in PyTorch:\n\n```python\nsqrt_sigma_det_torch = torch.sqrt(torch.linalg.det(sigma_torch))\nsqrt_L_det_torch = torch.prod(torch.diag(torch.linalg.cholesky(sigma_torch)))", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "print(\"|sigma|^0.5 = \", sqrt_sigma_det_torch)\n: |sigma|^0.5 = tensor(4.2371, dtype=torch.float64) \n\nprint(\"|L| = \", sqrt_L_det_torch)\n: |L| = tensor(4.2371, dtype=torch.float64)\n```\n\nWe can measure the difference in run time using PyTorch\u2019s built-in benchmark utility:\n\n```python\nimport torch.utils.benchmark as benchmark\n\nt0 = benchmark.Timer(\n stmt='torch.sqrt(torch.linalg.det(sigma))',\n globals={'sigma': sigma_torch})\n\nt1 = benchmark.Timer(\n stmt='torch.prod(torch.diag(torch.linalg.cholesky(sigma)))',\n globals={'sigma': sigma_torch})\n\nprint(t0.timeit(100))\n: torch.sqrt(torch.linalg.det(sigma))\n 80.80 us\n 1 measurement, 100 runs , 1 thread", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "print(t1.timeit(100))\n: torch.prod(torch.diag(torch.linalg.cholesky(sigma)))\n 11.56 us\n 1 measurement, 100 runs , 1 thread\n ```\n \nDemonstrating that the approach using the Cholesky decomposition can be significantly faster. Behind the scenes, PyTorch\u2019s linear algebra module uses OpenBLAS or MKL implementations of the LAPACK standard to maximize its CPU performance.\n\n# Autograd Support\n\nPyTorch\u2019s linear algebra module doesn\u2019t just implement the same functions as NumPy\u2019s linear algebra module (and a few more), it also extends them with autograd and CUDA support.\n\nLet\u2019s look at a very simple program that just computes an inverse and the gradient of that operation to show how autograd works:\n\n```python\nt = torch.tensor(((1, 2), (3, 4)), dtype=torch.float32, requires_grad=True)\n\ninv = torch.linalg.inv(t)\ninv.backward(torch.ones_like(inv))\n\nprint(t.grad)\n: tensor([[-0.5000, 0.5000],\n [ 0.5000, -0.5000]])\n```\n\nWe can mimic the same computation in NumPy by defining the autograd formula ourselves:", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "```python\na = np.array(((1, 2), (3, 4)), dtype=np.float32)\n\ninv_np = np.linalg.inv(a)\n\ndef inv_backward(result, grad):\n return -(result.transpose(-2, -1) @ (grad @ result.transpose(-2, -1)))\ngrad_np = inv_backward(inv_np, np.ones_like(inv_np))\n\nprint(grad_np)\n: [[-0.5 0.5]\n [ 0.5 -0.5]]\n```\n\nOf course, as programs become more complicated it\u2019s convenient to have builtin autograd support, and PyTorch\u2019s linear algebra module supports both real and complex autograd.\n\n# CUDA Support\n\nSupport for autograd and accelerators, like CUDA devices, is a core part of PyTorch. The ```torch.linalg``` module was developed with NVIDIA\u2019s PyTorch and cuSOLVER teams, who helped optimize its performance on CUDA devices with the cuSOLVER, cuBLAS, and MAGMA libraries. These improvements make PyTorch\u2019s CUDA linear algebra operations faster than ever. For example, let\u2019s look at the performance of PyTorch 1.9\u2019s ```torch.linalg.cholesky``` vs. PyTorch 1.8\u2019s (now deprecated) ```torch.cholesky```:", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n(The above charts were created using an Ampere A100 GPU with CUDA 11.3, cuSOLVER 11.1.1.58, and MAGMA 2.5.2. Matrices are in double precision.)\n\nThese charts show that performance has increased significantly on larger matrices, and that batched performance is better across the board. Other linear algebra operations, including ```torch.linalg.qr``` and ```torch.linalg.lstsq```, have also had their CUDA performance improved.\n\n# Beyond NumPy", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "# Beyond NumPy\n\nIn addition to offering all the functions in NumPy\u2019s linear algebra module with support for autograd and accelerators, ```torch.linalg``` has a few new functions of its own. NumPy\u2019s ```linalg.norm``` does not allow users to compute vector norms over arbitrary subsets of dimensions, so to enable this functionality we added ```torch.linalg.vector_norm```. We\u2019ve also started modernizing other linear algebra functionality in PyTorch, so we created ```torch.linalg.householder_product``` to replace the older ```torch.orgqr```, and we plan to continue adding more linear algebra functionality in the future, too.\n\n# The Future of Linear Algebra in PyTorch", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "The ```torch.linalg``` module is fast and familiar with great support for autograd and accelerators. It\u2019s already being used in libraries like [botorch](https://github.com/pytorch/botorch), too. But we\u2019re not stopping here. We plan to continue updating more of PyTorch\u2019s existing linear algebra functionality (like ```torch.lobpcg```) and offering more support for low rank and sparse linear algebra. We also want to hear your feedback on how we can improve, so start a conversation on the [forum](https://discuss.pytorch.org/) or file an issue on our [Github](https://github.com/pytorch/pytorch) and share your thoughts. \n\nWe look forward to hearing from you and seeing what the community does with PyTorch\u2019s new linear algebra functionality!", "metadata": {"source": "https://pytorch.org/blog/torch-linalg-autograd/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Democratizing AI with PyTorch Foundation and ROCm\u2122 support for PyTorch\"\nauthor: AMD\n---\n\n![AMD Founding Member](/assets/images/2023-02-14-democratizing-ai-with-pytorch-1.png){:width=\"50%\" style=\"display:block; margin-left:auto; margin-right:auto\"}\n\nLast year, Meta announced that [PyTorch](https://pytorch.org/) joined the Linux Foundation as a neutral home for growing the machine learning project and community with AMD representation as a part of the founding membership and governing board.\n\n[PyTorch Foundation\u2019s](https://pytorch.org/foundation) mission is to drive AI adoption by democratizing its software ecosystem through open source principles aligning with the AMD core principle of an Open software ecosystem. AMD strives to foster innovation through the support for latest generations of hardware, tools, libraries, and other components to simplify and accelerate adoption of AI across a broad range of scientific discoveries.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "
\n
\n

\nAMD, along with key PyTorch codebase developers (including those at Meta AI), delivered a set of updates to the ROCm\u2122 open software ecosystem that brings stable support for AMD Instinct\u2122 accelerators as well as many Radeon\u2122 GPUs. This now gives PyTorch developers the ability to build their next great AI solutions leveraging AMD GPU accelerators & ROCm. The support from PyTorch community in identifying gaps, prioritizing key updates, providing feedback for performance optimizing and supporting our journey from \u201cBeta\u201d to \u201cStable\u201d was immensely helpful and we deeply appreciate the strong collaboration between the two teams at AMD and PyTorch. The move for ROCm support from \u201cBeta\u201d to \u201cStable\u201d came in the PyTorch 1.12 release (June 2022) brings the added support to easily run PyTorch on native environment without having to configure custom dockers. This is a sign of confidence about the quality of support and performance of PyTorch using AMD Instinct and ROCm. The results of these collaborative efforts are evident in the performance measured on key industry benchmarks like Microsoft\u2019s SuperBench shown below in Graph 1.\n

\n
\n
\n

\n\u201cWe are excited to see the significant impact of developers at AMD to contribute to and extend features within PyTorch to make AI models run in a more performant, efficient, and scalable way. A great example of this is the thought-leadership around unified memory approaches between the framework and future hardware systems, and we look forward to seeing that feature progress.\u201d
\n- Soumith Chintala, PyTorch lead-maintainer and Director of Engineering, Meta AI\n

\n
\n
", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The progressive improvements on both the AMD CDNA\u2122 architecture as well as ROCm and PyTorch shows single GPU model throughput increase from AMD Instinct MI100 to the latest generation AMD Instinct MI200 family GPUs going from ROCm 4.2 to ROCm 5.3 and from PyTorch 1.7 to PyTorch 1.12.\n\n![Graph 1: ML model performance over generation using Microsoft Superbench Suite](/assets/images/2023-02-14-democratizing-ai-with-pytorch-2.png){:width=\"100%\"}\n\nGraph 1: ML model performance over generation using Microsoft Superbench Suite 1, 2, 3\n\n\nBelow are a few of the key updates for ROCm support since the PyTorch 1.12 release\n\n \n\n## Full Continuous Integration (CI) for ROCm on PyTorch", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "With the ROCm support for PyTorch move from \u201cBeta\u201d to \u201cStable,\u201d all the functions and features commits are now verified through a full Continuous Integration (CI) process. The CI process helps ensure the proper build and test process ahead of an expected Docker and PIP wheel release with stable commits forthcoming.\n\n\n## Support for [Kineto Profiler](https://github.com/pytorch/kineto)\n\nThe addition of Kineto profiler support to ROCm now helps developers and users understand performance bottlenecks through effective diagnosis and profiling tools. The tool also provides recommendations to improve known issues and visualization through TensorBoard UI.\n\n## Key PyTorch Libraries support added", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "PyTorch ecosystem libraries like [TorchText](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) (Text classification), [TorchRec](https://pytorch.org/torchrec/) (libraries for recommender systems - RecSys), [TorchVision](https://pytorch.org/vision/stable/index.html) (Computer Vision), [TorchAudio](https://pytorch.org/audio/stable/index.html) (audio and signal processing) are fully supported since ROCm 5.1 and upstreamed with PyTorch 1.12.\n\nKey libraries provided with the ROCm software stack including [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) (Convolution models), [RCCL](https://github.com/ROCmSoftwarePlatform/rccl) (ROCm Collective Communications) and [rocBLAS](https://github.com/ROCmSoftwarePlatform/rocBLAS) (BLAS for transformers) were further optimized to offer new potential efficiencies and higher performance.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} {"page_content": "MIOpen innovates on several fronts, such as implementing fusion to optimize for memory bandwidth and GPU launch overheads, providing an auto-tuning infrastructure to overcome the large design space of problem configurations, and implementing different algorithms to optimize convolutions for different filter and input sizes. MIOpen is one of the first libraries to publicly support the bfloat16 data-type for convolutions, allowing efficient training at lower precision maintaining expected accuracy.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "RCCL (pronounced \"Rickle\") is a stand-alone library of standard collective communication routines for GPUs, implementing all-reduce, all-gather, reduce, broadcast, reduce-scatter, gather, scatter, and all-to-all. There is support for direct GPU-to-GPU send and receive operations. It has been optimized to achieve high bandwidth on platforms using PCIe\u00ae, Infinity Fabric\u2122 (GPU to GPU) as well as networking using InfiniBand Verbs or TCP/IP sockets. RCCL supports an arbitrary number of GPUs installed in single", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "or multiple nodes and can be used in either single- or multi-process (e.g., MPI) applications.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Along with the above key highlights, over 50 features and functionality improvements were completed jointly between AMD and PyTorch to add stable support for ROCm. These include improvements to tools, compilers, runtime, graph optimizations through TorchScript, INT8 quant path usage, and [ONNX runtime integration](https://onnxruntime.ai/) including support for Navi 21 based Radeon\u2122 PRO datacenter graphics card to name a few.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[AITemplate](https://github.com/facebookincubator/AITemplate) Inference Engine", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "MetaAI recently published a blog announcing the release of its open source AITemplate ([link](https://ai.facebook.com/blog/gpu-inference-engine-nvidia-amd-open-source/)) for a unified inference system supporting AMD Instinct GPU accelerators using the AMD ROCm stack. This Python based framework can help significantly improve performance through increased utilization of AMD matrix cores for transformer blocks. This is achieved through the AMD [Composable Kernel (CK)", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "library](https://github.com/ROCmSoftwarePlatform/composable_kernel) which provides performance critical Kernels for ML AI workloads across multiple architectures including GPUs and CPUs through HIP & C++.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Moreover, the AITemplate also provides out-of-the-box support for widely used AI models like BERT, ResNET, Vision Transformer, Stable Diffusion etc. simplifying deployment process through these pretrained models.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "What\u2019s coming with future ROCm releases?", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Unified memory models for CPU + GPU", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "As system architecture evolves to address the complexity of large problem sizes and data sets, memory management becomes a key performance bottle neck that needs a cohesive strategy to be addressed through innovations at both hardware and software levels. AMD is uniquely positioned to address this problem with its effective data center solutions integrating AMD EPYC\u2122 CPU cores with its AMD Instinct GPU compute units in a truly unified datacenter APU (Accelerated Processing Unit) form factor set to be", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "launched in 2H 2023.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The software work to leverage the unified CPU + GPU memory has already started in collaboration with the PyTorch team, to enable the usage of a fast, low latency, synchronized memory model that enables not only AMD but also other AI accelerators to address the complex memory management problem of today. We are looking forward to this joint effort and announcement soon.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgement\n\nThe content in this blog highlights the joint work between AMD and key PyTorch contributors including Meta, working on many of the core features, as well as Microsoft enabling ONNX Runtime support. We are looking forward to working with the other founding members at the PyTorch Foundation on the next steps and improvements to democratize and grow adoption of PyTorch across the industry.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "CAUTIONARY STATEMENT", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This blog contains forward-looking statements concerning Advanced Micro Devices, Inc. (AMD) such as the availability, timing and expected benefits of an AMD datacenter APU form factor, which are made pursuant to the Safe Harbor provisions of the Private Securities Litigation Reform Act of 1995. Forward-looking statements are commonly identified by words such as \"would,\" \"may,\" \"expects,\" \"believes,\" \"plans,\" \"intends,\" \"projects\" and other terms with similar meaning. Investors are cautioned that the", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "forward-looking statements in this blog are based on current beliefs, assumptions and expectations, speak only as of the date of this blog and involve risks and uncertainties that could cause actual results to differ materially from current expectations. Such statements are subject to certain known and unknown risks and uncertainties, many of which are difficult to predict and generally beyond AMD's control, that could cause actual results and other future events to differ materially from those expressed", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "in, or implied or projected by, the forward-looking information and statements. Investors are urged to review in detail the risks and uncertainties in AMD\u2019s Securities and Exchange Commission filings, including but not limited to AMD\u2019s most recent reports on Forms 10-K and 10-Q. AMD does not assume, and hereby disclaims, any obligation to update forward-looking statements made in this blog, except as may be required by law.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Endnotes", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. MI100D-01 SuperBench v0.5 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122 7763 CPU server tested with 1x AMD Instinct\u2122 MI100 (32GB HBM2e) 300W GPU, SBIOS 2.2, Ubuntu\u00ae 20.04.5 LTS, host ROCm\u2122 5.2.0, guest ROCm 4.2, PyTorch 1.7.0. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. MI200D-01 SuperBench v0.6 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122 7763 CPU server tested with 1x AMD Instinct\u2122 MI210 (64GB HBM2e) 300W GPU, SBIOS 2.2, Ubuntu 20.04.5 LTS, host ROCm 5.3.0, guest ROCm 5.3, PyTorch 1.12. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. MI200D-02: SuperBench v0.6 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122\ufe0f 7763 CPU server tested with 1x AMD Instinct\u2122\ufe0f MI250 (128GB HBM2e) 560W GPU, SBIOS M12, Ubuntu 20.04 LTS, host ROCm 5.3.0, guest ROCm 5.3, PyTorch 1.12. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing PyTorch Fully Sharded Data Parallel (FSDP) API\"\nauthor: Yanli Zhao, Rohan Varma, Chien-Chin Huang, Shen Li, Min Xu, Alban Desmaison\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Recent studies have shown that large model training will be beneficial for improving model quality. During the last 3 years, model size grew 10,000 times from [BERT](https://arxiv.org/abs/1810.04805) with 110M parameters to [Megatron-2](https://arxiv.org/abs/2104.04473) with one trillion. However, training large AI models is not easy\u2014aside from the need for large amounts of computing resources, software engineering complexity is also challenging. PyTorch has been working on building tools and infrastructure", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "to make it easier.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Distributed data parallelism is a staple of scalable deep learning because of its robustness and simplicity. It however requires the model to fit on one GPU. Recent approaches like DeepSpeed ZeRO and FairScale\u2019s Fully Sharded Data Parallel allow us to break this barrier by sharding a model\u2019s parameters, gradients and optimizer states across data parallel workers while still maintaining the simplicity of data parallelism.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "With PyTorch 1.11 we\u2019re adding native support for Fully Sharded Data Parallel (FSDP), currently available as a prototype feature. Its implementation heavily borrows from FairScale\u2019s version while bringing more streamlined APIs and additional performance improvements.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Scaling tests of PyTorch FSDP on AWS show it can scale up to train dense models with 1T parameters. Realized performance in our experiments reached 84 TFLOPS per A100 GPU for GPT 1T model and 159 TFLOPS per A100 GPU for GPT 175B model on AWS cluster. Native FSDP implementation also dramatically improved model initialization time compared to FairScale\u2019s original when CPU offloading was enabled.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "In future PyTorch versions, we\u2019re going to enable users to seamlessly switch between DDP, ZeRO-1, ZeRO-2 and FSDP flavors of data parallelism, so that users can train different scales of models with simple configurations in the unified API.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "How FSDP Works\n\nFSDP is a type of data-parallel training, but unlike traditional data-parallel, which maintains a per-GPU copy of a model\u2019s parameters, gradients and optimizer states, it shards all of these states across data-parallel workers and can optionally offload the sharded model parameters to CPUs. \n\nThe figure below shows how FSDP works for 2 data-parallel processes:\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure 1. FSDP workflow\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Usually, model layers are wrapped with FSDP in a nested way, so that only layers in a single FSDP instance need to gather the full parameters to a single device during forward or backward computations. The gathered full parameters will be freed immediately after computation, and the freed memory can be used for the next layer\u2019s computation. In this way, peak GPU memory could be saved and thus training can be scaled to use a larger model size or larger batch size. To further maximize memory efficiency, FSDP", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "can offload the parameters, gradients and optimizer states to CPUs when the instance is not active in the computation.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Using FSDP in PyTorch\n\nThere are two ways to wrap a model with PyTorch FSDP. Auto wrapping is a drop-in replacement for DDP; manual wrapping needs minimal changes of model definition code with the ability to explore complex sharding strategies.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Auto Wrapping\n\nModel layers should be wrapped in FSDP in a nested way to save peak memory and enable communication and computation overlapping. The simplest way to do it is auto wrapping, which can serve as a drop-in replacement for DDP without changing the rest of the code.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "fsdp_auto_wrap_policy argument allows specifying a callable function to recursively wrap layers with FSDP. default_auto_wrap_policy function provided by the PyTorch FSDP recursively wraps layers with the number of parameters larger than 100M. You can supply your own wrapping policy as needed. The example of writing a customized wrapping policy is shown in the [FSDP API doc](https://pytorch.org/docs/stable/fsdp.html).", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "In addition, cpu_offload could be configured optionally to offload wrapped parameters to CPUs when these parameters are not used in computation. This can further improve memory efficiency at the cost of data transfer overhead between host and device.\n\nThe example below shows how FSDP is wrapped using auto wrapping.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.distributed.fsdp import (\n FullyShardedDataParallel,\n CPUOffload,\n)\nfrom torch.distributed.fsdp.wrap import (\n default_auto_wrap_policy,\n)\nimport torch.nn as nn\n \nclass model(nn.Module):\n def __init__(self):\n super().__init__()\n self.layer1 = nn.Linear(8, 4)\n self.layer2 = nn.Linear(4, 16)\n self.layer3 = nn.Linear(16, 4)\n \nmodel = DistributedDataParallel(model())\nfsdp_model = FullyShardedDataParallel(\n model(),", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "fsdp_auto_wrap_policy=default_auto_wrap_policy,\n cpu_offload=CPUOffload(offload_params=True),\n)\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Manual Wrapping\n\nManual wrapping can be useful to explore complex sharding strategies by applying `wrap` selectively to some parts of the model. Overall settings can be passed to the enable_wrap() context manager.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.distributed.fsdp import (\n FullyShardedDataParallel,\n CPUOffload,\n)\nfrom torch.distributed.fsdp.wrap import (\n enable_wrap,\n wrap,\n)\nimport torch.nn as nn\nfrom typing import Dict\n \n \nclass model(nn.Module):\n def __init__(self):\n super().__init__()\n self.layer1 = wrap(nn.Linear(8, 4))\n self.layer2 = nn.Linear(4, 16)\n self.layer3 = wrap(nn.Linear(16, 4))\n \nwrapper_kwargs = Dict(cpu_offload=CPUOffload(offload_params=True))", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "with enable_wrap(wrapper_cls=FullyShardedDataParallel, **wrapper_kwargs):\n fsdp_model = wrap(model())", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "After wrapping the model with FSDP using one of the two above approaches, the model can be trained in a similar way as local training, like this:\n\n```python\noptim = torch.optim.Adam(fsdp_model.parameters(), lr=0.0001)\nfor sample, label in next_batch():\n out = fsdp_model(input)\n loss = criterion(out, label)\n loss.backward()\n optim.step()\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Benchmark Results\n\nWe ran extensive scaling tests for 175B and 1T GPT models on AWS clusters using PyTorch FSDP. Each cluster node is an instance with 8 [NVIDIA A100-SXM4-40GB](https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Center/a100/pdf/nvidia-a100-datasheet-us-nvidia-1758950-r4-web.pdf) GPUs, and inter-nodes are connected via AWS Elastic Fabric Adapter (EFA) with 400 Gbps network bandwidth.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "GPT models are implemented using [minGPT](https://github.com/karpathy/minGPT). A randomly generated input dataset is used for benchmarking purposes. All experiments ran with 50K vocabulary size, fp16 precision and [SGD](https://pytorch.org/docs/stable/generated/torch.optim.SGD.html) optimizer.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "| Model | Number of layers | Hidden size | Attention heads | Model size, billions of parameters |\n|----------|------------------|-------------|-----------------|------------------------------------|\n| GPT 175B | 96 | 12288 | 96 | 175 |\n| GPT 1T | 128 | 25600 | 160 | 1008 |", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "In addition to using FSDP with parameters CPU offloading in the experiments, the [activation checkpointing feature](https://pytorch.org/docs/stable/checkpoint.html) in PyTorch is also applied in the tests.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "The maximum per-GPU throughput of 159 teraFLOP/s (51% of NVIDIA A100 peak theoretical performance 312 teraFLOP/s/GPU) is achieved with batch size 20 and sequence length 512 on 128 GPUs for the GPT 175B model; further increase of the number of GPUs leads to per-GPU throughput degradation because of growing communication between the nodes.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "For the GPT 1T model, the maximum per-GPU throughput of 84 teraFLOP/s (27% of the peak teraFLOP/s) is achieved with batch size 4 and sequence length 2048 on 128 GPUs. However, further increase of the number of GPUs doesn\u2019t affect the per-GPU throughput too much because we observed that the largest bottleneck in the 1T model training is not from communication but from the slow CUDA cache allocator when peak GPU memory is reaching the limit. The use of A100 80G GPUs with larger memory capacity will mostly", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "resolve this issue and also help scale the batch size to achieve much larger throughput.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Future Work", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "In the next beta release, we are planning to add efficient distributed model/states checkpointing APIs, meta device support for large model materialization, and mixed-precision support inside FSDP computation and communication. We\u2019re also going to make it easier to switch between [DDP](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html), [ZeRO1, ZeRO2](https://arxiv.org/abs/1910.02054) and FSDP flavors of data parallelism in the new API. To further improve FSDP performance, memory fragmentation", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "reduction and communication efficiency improvements are also planned.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "A Bit of History of 2 Versions of FSDP\n\n[FairScale FSDP](https://engineering.fb.com/2021/07/15/open-source/fsdp/) was released in early 2021 as part of the FairScale library. And then we started the effort to upstream FairScale FSDP to PyTorch in PT 1.11, making it production-ready. We have selectively upstreamed and refactored key features from FairScale FSDP, redesigned user interfaces and made performance improvements.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "In the near future, FairScale FSDP will stay in the FairScale repository for research projects, while generic and widely adopted features will be upstreamed to PyTorch incrementally and hardened accordingly.\n\nMeanwhile, PyTorch FSDP will focus more on production readiness and long-term support. This includes better integration with ecosystems and improvements on performance, usability, reliability, debuggability and composability.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgments", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank the authors of FairScale FSDP: Myle Ott, Sam Shleifer, Min Xu, Priya Goyal, Quentin Duval, Vittorio Caggiano, Tingting Markstrum, Anjali Sridhar. Thanks to the Microsoft DeepSpeed ZeRO team for developing and popularizing sharded data parallel techniques. Thanks to Pavel Belevich, Jessica Choi, Sisil Mehta for running experiments using PyTorch FSDP on different clusters. Thanks to Geeta Chauhan, Mahesh Yadav, Pritam Damania, Dmytro Dzhulgakov for supporting this effort and insightful", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "discussions.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch library updates including new model serving library '\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "Along with the PyTorch 1.5 release, we are announcing new libraries for high-performance PyTorch model serving and tight integration with TorchElastic and Kubernetes. Additionally, we are releasing updated packages for torch_xla (Google Cloud TPUs), torchaudio, torchvision, and torchtext. All of these new libraries and enhanced capabilities are available today and accompany all of the core features [released in PyTorch 1.5](https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "TorchServe (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "RCCL (pronounced \"Rickle\") is a stand-alone library of standard collective communication routines for GPUs, implementing all-reduce, all-gather, reduce, broadcast, reduce-scatter, gather, scatter, and all-to-all. There is support for direct GPU-to-GPU send and receive operations. It has been optimized to achieve high bandwidth on platforms using PCIe\u00ae, Infinity Fabric\u2122 (GPU to GPU) as well as networking using InfiniBand Verbs or TCP/IP sockets. RCCL supports an arbitrary number of GPUs installed in single or multiple nodes and can be used in either single- or multi-process (e.g., MPI) applications.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Along with the above key highlights, over 50 features and functionality improvements were completed jointly between AMD and PyTorch to add stable support for ROCm. These include improvements to tools, compilers, runtime, graph optimizations through TorchScript, INT8 quant path usage, and [ONNX runtime integration](https://onnxruntime.ai/) including support for Navi 21 based Radeon\u2122 PRO datacenter graphics card to name a few.\n\n## [AITemplate](https://github.com/facebookincubator/AITemplate) Inference Engine", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "MetaAI recently published a blog announcing the release of its open source AITemplate ([link](https://ai.facebook.com/blog/gpu-inference-engine-nvidia-amd-open-source/)) for a unified inference system supporting AMD Instinct GPU accelerators using the AMD ROCm stack. This Python based framework can help significantly improve performance through increased utilization of AMD matrix cores for transformer blocks. This is achieved through the AMD [Composable Kernel (CK) library](https://github.com/ROCmSoftwarePlatform/composable_kernel) which provides performance critical Kernels for ML AI workloads across multiple architectures including GPUs and CPUs through HIP & C++.\n\nMoreover, the AITemplate also provides out-of-the-box support for widely used AI models like BERT, ResNET, Vision Transformer, Stable Diffusion etc. simplifying deployment process through these pretrained models.\n\n \n## What\u2019s coming with future ROCm releases?\n\n### Unified memory models for CPU + GPU", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "As system architecture evolves to address the complexity of large problem sizes and data sets, memory management becomes a key performance bottle neck that needs a cohesive strategy to be addressed through innovations at both hardware and software levels. AMD is uniquely positioned to address this problem with its effective data center solutions integrating AMD EPYC\u2122 CPU cores with its AMD Instinct GPU compute units in a truly unified datacenter APU (Accelerated Processing Unit) form factor set to be launched in 2H 2023.\n\nThe software work to leverage the unified CPU + GPU memory has already started in collaboration with the PyTorch team, to enable the usage of a fast, low latency, synchronized memory model that enables not only AMD but also other AI accelerators to address the complex memory management problem of today. We are looking forward to this joint effort and announcement soon.\n\n## Acknowledgement", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Acknowledgement\n\nThe content in this blog highlights the joint work between AMD and key PyTorch contributors including Meta, working on many of the core features, as well as Microsoft enabling ONNX Runtime support. We are looking forward to working with the other founding members at the PyTorch Foundation on the next steps and improvements to democratize and grow adoption of PyTorch across the industry.\n\n## CAUTIONARY STATEMENT", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "\nThis blog contains forward-looking statements concerning Advanced Micro Devices, Inc. (AMD) such as the availability, timing and expected benefits of an AMD datacenter APU form factor, which are made pursuant to the Safe Harbor provisions of the Private Securities Litigation Reform Act of 1995. Forward-looking statements are commonly identified by words such as \"would,\" \"may,\" \"expects,\" \"believes,\" \"plans,\" \"intends,\" \"projects\" and other terms with similar meaning. Investors are cautioned that the forward-looking statements in this blog are based on current beliefs, assumptions and expectations, speak only as of the date of this blog and involve risks and uncertainties that could cause actual results to differ materially from current expectations. Such statements are subject to certain known and unknown risks and uncertainties, many of which are difficult to predict and generally beyond AMD's control, that could cause actual results and other future events to differ materially from those expressed in, or implied or projected by, the forward-looking information and statements. Investors are urged to review in detail the risks and uncertainties in AMD\u2019s Securities and Exchange Commission filings, including but not limited to AMD\u2019s most recent reports on Forms 10-K and 10-Q. AMD does not assume, and hereby disclaims, any obligation to update forward-looking statements made in this blog, except as may be required by law. \n", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Endnotes", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "1. MI100D-01 SuperBench v0.5 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122 7763 CPU server tested with 1x AMD Instinct\u2122 MI100 (32GB HBM2e) 300W GPU, SBIOS 2.2, Ubuntu\u00ae 20.04.5 LTS, host ROCm\u2122 5.2.0, guest ROCm 4.2, PyTorch 1.7.0. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.\n2. MI200D-01 SuperBench v0.6 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122 7763 CPU server tested with 1x AMD Instinct\u2122 MI210 (64GB HBM2e) 300W GPU, SBIOS 2.2, Ubuntu 20.04.5 LTS, host ROCm 5.3.0, guest ROCm 5.3, PyTorch 1.12. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.\n3. MI200D-02: SuperBench v0.6 model training results based on AMD internal testing as of 11/09/2022 measuring the total training throughput, at half precision, using a 2P AMD EPYC\u2122\ufe0f 7763 CPU server tested with 1x AMD Instinct\u2122\ufe0f MI250 (128GB HBM2e) 560W GPU, SBIOS M12, Ubuntu 20.04 LTS, host ROCm 5.3.0, guest ROCm 5.3, PyTorch 1.12. Server manufacturers may vary configurations, yielding different results. Performance may vary based factors including use of latest drivers and optimizations.", "metadata": {"source": "https://pytorch.org/blog/democratizing-ai-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing PyTorch Fully Sharded Data Parallel (FSDP) API\"\nauthor: Yanli Zhao, Rohan Varma, Chien-Chin Huang, Shen Li, Min Xu, Alban Desmaison\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---\n\nRecent studies have shown that large model training will be beneficial for improving model quality. During the last 3 years, model size grew 10,000 times from [BERT](https://arxiv.org/abs/1810.04805) with 110M parameters to [Megatron-2](https://arxiv.org/abs/2104.04473) with one trillion. However, training large AI models is not easy\u2014aside from the need for large amounts of computing resources, software engineering complexity is also challenging. PyTorch has been working on building tools and infrastructure to make it easier.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "PyTorch Distributed data parallelism is a staple of scalable deep learning because of its robustness and simplicity. It however requires the model to fit on one GPU. Recent approaches like DeepSpeed ZeRO and FairScale\u2019s Fully Sharded Data Parallel allow us to break this barrier by sharding a model\u2019s parameters, gradients and optimizer states across data parallel workers while still maintaining the simplicity of data parallelism.\n\nWith PyTorch 1.11 we\u2019re adding native support for Fully Sharded Data Parallel (FSDP), currently available as a prototype feature. Its implementation heavily borrows from FairScale\u2019s version while bringing more streamlined APIs and additional performance improvements.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "Scaling tests of PyTorch FSDP on AWS show it can scale up to train dense models with 1T parameters. Realized performance in our experiments reached 84 TFLOPS per A100 GPU for GPT 1T model and 159 TFLOPS per A100 GPU for GPT 175B model on AWS cluster. Native FSDP implementation also dramatically improved model initialization time compared to FairScale\u2019s original when CPU offloading was enabled.\n\nIn future PyTorch versions, we\u2019re going to enable users to seamlessly switch between DDP, ZeRO-1, ZeRO-2 and FSDP flavors of data parallelism, so that users can train different scales of models with simple configurations in the unified API.\n\n### How FSDP Works\n\nFSDP is a type of data-parallel training, but unlike traditional data-parallel, which maintains a per-GPU copy of a model\u2019s parameters, gradients and optimizer states, it shards all of these states across data-parallel workers and can optionally offload the sharded model parameters to CPUs. \n\nThe figure below shows how FSDP works for 2 data-parallel processes:", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\nFigure 1. FSDP workflow\n

\n\nUsually, model layers are wrapped with FSDP in a nested way, so that only layers in a single FSDP instance need to gather the full parameters to a single device during forward or backward computations. The gathered full parameters will be freed immediately after computation, and the freed memory can be used for the next layer\u2019s computation. In this way, peak GPU memory could be saved and thus training can be scaled to use a larger model size or larger batch size. To further maximize memory efficiency, FSDP can offload the parameters, gradients and optimizer states to CPUs when the instance is not active in the computation.\n\n### Using FSDP in PyTorch\n\nThere are two ways to wrap a model with PyTorch FSDP. Auto wrapping is a drop-in replacement for DDP; manual wrapping needs minimal changes of model definition code with the ability to explore complex sharding strategies.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "#### Auto Wrapping\n\nModel layers should be wrapped in FSDP in a nested way to save peak memory and enable communication and computation overlapping. The simplest way to do it is auto wrapping, which can serve as a drop-in replacement for DDP without changing the rest of the code.\n\nfsdp_auto_wrap_policy argument allows specifying a callable function to recursively wrap layers with FSDP. default_auto_wrap_policy function provided by the PyTorch FSDP recursively wraps layers with the number of parameters larger than 100M. You can supply your own wrapping policy as needed. The example of writing a customized wrapping policy is shown in the [FSDP API doc](https://pytorch.org/docs/stable/fsdp.html).\n\nIn addition, cpu_offload could be configured optionally to offload wrapped parameters to CPUs when these parameters are not used in computation. This can further improve memory efficiency at the cost of data transfer overhead between host and device.\n\nThe example below shows how FSDP is wrapped using auto wrapping.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torch.distributed.fsdp import (\n FullyShardedDataParallel,\n CPUOffload,\n)\nfrom torch.distributed.fsdp.wrap import (\n default_auto_wrap_policy,\n)\nimport torch.nn as nn\n \nclass model(nn.Module):\n def __init__(self):\n super().__init__()\n self.layer1 = nn.Linear(8, 4)\n self.layer2 = nn.Linear(4, 16)\n self.layer3 = nn.Linear(16, 4)\n \nmodel = DistributedDataParallel(model())\nfsdp_model = FullyShardedDataParallel(\n model(),\n fsdp_auto_wrap_policy=default_auto_wrap_policy,\n cpu_offload=CPUOffload(offload_params=True),\n)\n```\n\n#### Manual Wrapping\n\nManual wrapping can be useful to explore complex sharding strategies by applying `wrap` selectively to some parts of the model. Overall settings can be passed to the enable_wrap() context manager.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torch.distributed.fsdp import (\n FullyShardedDataParallel,\n CPUOffload,\n)\nfrom torch.distributed.fsdp.wrap import (\n enable_wrap,\n wrap,\n)\nimport torch.nn as nn\nfrom typing import Dict\n \n \nclass model(nn.Module):\n def __init__(self):\n super().__init__()\n self.layer1 = wrap(nn.Linear(8, 4))\n self.layer2 = nn.Linear(4, 16)\n self.layer3 = wrap(nn.Linear(16, 4))\n \nwrapper_kwargs = Dict(cpu_offload=CPUOffload(offload_params=True))\nwith enable_wrap(wrapper_cls=FullyShardedDataParallel, **wrapper_kwargs):\n fsdp_model = wrap(model())\n```\n\nAfter wrapping the model with FSDP using one of the two above approaches, the model can be trained in a similar way as local training, like this:\n\n```python\noptim = torch.optim.Adam(fsdp_model.parameters(), lr=0.0001)\nfor sample, label in next_batch():\n out = fsdp_model(input)\n loss = criterion(out, label)\n loss.backward()\n optim.step()\n```\n\n### Benchmark Results", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "We ran extensive scaling tests for 175B and 1T GPT models on AWS clusters using PyTorch FSDP. Each cluster node is an instance with 8 [NVIDIA A100-SXM4-40GB](https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Center/a100/pdf/nvidia-a100-datasheet-us-nvidia-1758950-r4-web.pdf) GPUs, and inter-nodes are connected via AWS Elastic Fabric Adapter (EFA) with 400 Gbps network bandwidth.\n\nGPT models are implemented using [minGPT](https://github.com/karpathy/minGPT). A randomly generated input dataset is used for benchmarking purposes. All experiments ran with 50K vocabulary size, fp16 precision and [SGD](https://pytorch.org/docs/stable/generated/torch.optim.SGD.html) optimizer.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "| Model | Number of layers | Hidden size | Attention heads | Model size, billions of parameters |\n|----------|------------------|-------------|-----------------|------------------------------------|\n| GPT 175B | 96 | 12288 | 96 | 175 |\n| GPT 1T | 128 | 25600 | 160 | 1008 |\n\nIn addition to using FSDP with parameters CPU offloading in the experiments, the [activation checkpointing feature](https://pytorch.org/docs/stable/checkpoint.html) in PyTorch is also applied in the tests.\n\nThe maximum per-GPU throughput of 159 teraFLOP/s (51% of NVIDIA A100 peak theoretical performance 312 teraFLOP/s/GPU) is achieved with batch size 20 and sequence length 512 on 128 GPUs for the GPT 175B model; further increase of the number of GPUs leads to per-GPU throughput degradation because of growing communication between the nodes.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "For the GPT 1T model, the maximum per-GPU throughput of 84 teraFLOP/s (27% of the peak teraFLOP/s) is achieved with batch size 4 and sequence length 2048 on 128 GPUs. However, further increase of the number of GPUs doesn\u2019t affect the per-GPU throughput too much because we observed that the largest bottleneck in the 1T model training is not from communication but from the slow CUDA cache allocator when peak GPU memory is reaching the limit. The use of A100 80G GPUs with larger memory capacity will mostly resolve this issue and also help scale the batch size to achieve much larger throughput.\n\n

\n \n

\n\n

\n \n

\n\n### Future Work", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "### Future Work\n\nIn the next beta release, we are planning to add efficient distributed model/states checkpointing APIs, meta device support for large model materialization, and mixed-precision support inside FSDP computation and communication. We\u2019re also going to make it easier to switch between [DDP](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html), [ZeRO1, ZeRO2](https://arxiv.org/abs/1910.02054) and FSDP flavors of data parallelism in the new API. To further improve FSDP performance, memory fragmentation reduction and communication efficiency improvements are also planned.\n\n### A Bit of History of 2 Versions of FSDP", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "[FairScale FSDP](https://engineering.fb.com/2021/07/15/open-source/fsdp/) was released in early 2021 as part of the FairScale library. And then we started the effort to upstream FairScale FSDP to PyTorch in PT 1.11, making it production-ready. We have selectively upstreamed and refactored key features from FairScale FSDP, redesigned user interfaces and made performance improvements.\n\nIn the near future, FairScale FSDP will stay in the FairScale repository for research projects, while generic and widely adopted features will be upstreamed to PyTorch incrementally and hardened accordingly.\n\nMeanwhile, PyTorch FSDP will focus more on production readiness and long-term support. This includes better integration with ecosystems and improvements on performance, usability, reliability, debuggability and composability.\n\n### Acknowledgments", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "### Acknowledgments\n\nWe would like to thank the authors of FairScale FSDP: Myle Ott, Sam Shleifer, Min Xu, Priya Goyal, Quentin Duval, Vittorio Caggiano, Tingting Markstrum, Anjali Sridhar. Thanks to the Microsoft DeepSpeed ZeRO team for developing and popularizing sharded data parallel techniques. Thanks to Pavel Belevich, Jessica Choi, Sisil Mehta for running experiments using PyTorch FSDP on different clusters. Thanks to Geeta Chauhan, Mahesh Yadav, Pritam Damania, Dmytro Dzhulgakov for supporting this effort and insightful discussions.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch library updates including new model serving library '\nauthor: Team PyTorch\n---\n\n\nAlong with the PyTorch 1.5 release, we are announcing new libraries for high-performance PyTorch model serving and tight integration with TorchElastic and Kubernetes. Additionally, we are releasing updated packages for torch_xla (Google Cloud TPUs), torchaudio, torchvision, and torchtext. All of these new libraries and enhanced capabilities are available today and accompany all of the core features [released in PyTorch 1.5](https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis). \n\n## TorchServe (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} {"page_content": "TorchServe is a flexible and easy to use library for serving PyTorch models in production performantly at scale. It is cloud and environment agnostic and supports features such as multi-model serving, logging, metrics, and the creation of RESTful endpoints for application integration. TorchServe was jointly developed by engineers from Facebook and AWS with feedback and engagement from the broader PyTorch community. The experimental release of TorchServe is available today. Some of the highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "* Support for both Python-based and TorchScript-based models\n* Default handlers for common use cases (e.g., image segmentation, text classification) as well as the ability to write custom handlers for other use cases\n* Model versioning, the ability to run multiple versions of a model at the same time, and the ability to roll back to an earlier version", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "* The ability to package a model, learning weights, and supporting files (e.g., class mappings, vocabularies) into a single, persistent artifact (a.k.a. the \u201cmodel archive\u201d)\n* Robust management capability, allowing full configuration of models, versions, and individual worker threads via command line, config file, or run-time API\n* Automatic batching of individual inferences across HTTP requests\n* Logging including common metrics, and the ability to incorporate custom metrics", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "* Ready-made Dockerfile for easy deployment\n* HTTPS support for secure deployment", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "To learn more about the APIs and the design of this feature, see the links below:\n* See for a full multi-node deployment reference architecture.\n* The full documentation can be found [here](https://pytorch.org/serve).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "TorchElastic integration with Kubernetes (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "[TorchElastic](https://github.com/pytorch/elastic) is a proven library for training large scale deep neural networks at scale within companies like Facebook, where having the ability to dynamically adapt to server availability and scale as new compute resources come online is critical. Kubernetes enables customers using machine learning frameworks like PyTorch to run training jobs distributed across fleets of powerful GPU instances like the Amazon EC2 P3. Distributed training jobs, however, are not", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "fault-tolerant, and a job cannot continue if a node failure or reclamation interrupts training. Further, jobs cannot start without acquiring all required resources, or scale up and down without being restarted. This lack of resiliency and flexibility results in increased training time and costs from idle resources. TorchElastic addresses these limitations by enabling distributed training jobs to be executed in a fault-tolerant and elastic manner. Until today, Kubernetes users needed to manage Pods and", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "Services required for TorchElastic training jobs manually.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "Through the joint collaboration of engineers at Facebook and AWS, TorchElastic, adding elasticity and fault tolerance, is now supported using vanilla Kubernetes and through the managed EKS service from AWS.\n\nTo learn more see the [TorchElastic repo](http://pytorch.org/elastic/0.2.0rc0/kubernetes.html) for the controller implementation and docs on how to use it.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "torch_xla 1.5 now available", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "[torch_xla](http://pytorch.org/xla/) is a Python package that uses the [XLA linear algebra compiler](https://www.tensorflow.org/xla) to accelerate the [PyTorch deep learning framework](https://pytorch.org/) on [Cloud TPUs](https://cloud.google.com/tpu/) and [Cloud TPU Pods](https://cloud.google.com/tpu/docs/tutorials/pytorch-pod). torch_xla aims to give PyTorch users the ability to do everything they can do on GPUs on Cloud TPUs as well while minimizing changes to the user experience. The project began with", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "a conversation at NeurIPS 2017 and gathered momentum in 2018 when teams from Facebook and Google came together to create a proof of concept. We announced this collaboration at PTDC 2018 and made the PyTorch/XLA integration broadly available at PTDC 2019. The project already has 28 contributors, nearly 2k commits, and a repo that has been forked more than 100 times.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "This release of [torch_xla](http://pytorch.org/xla/) is aligned and tested with PyTorch 1.5 to reduce friction for developers and to provide a stable and mature PyTorch/XLA stack for training models using Cloud TPU hardware. You can [try it for free](https://medium.com/pytorch/get-started-with-pytorch-cloud-tpus-and-colab-a24757b8f7fc) in your browser on an 8-core Cloud TPU device with [Google Colab](https://colab.research.google.com/), and you can use it at a much larger scaleon [Google", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "Cloud](https://cloud.google.com/gcp).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "See the full torch_xla release notes [here](https://github.com/pytorch/xla/releases). Full docs and tutorials can be found [here](https://pytorch.org/xla/) and [here](https://cloud.google.com/tpu/docs/tutorials).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Domain Libraries\n\ntorchaudio, torchvision, and torchtext complement PyTorch with common datasets, models, and transforms in each domain area. We\u2019re excited to share new releases for all three domain libraries alongside PyTorch 1.5 and the rest of the library updates. For this release, all three domain libraries are removing support for Python2 and will support Python3 only.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "torchaudio 0.5\nThe torchaudio 0.5 release includes new transforms, functionals, and datasets. Highlights for the release include:\n\n* Added the Griffin-Lim functional and transform, `InverseMelScale` and `Vol` transforms, and `DB_to_amplitude`. \n* Added support for `allpass`, `fade`, `bandpass`, `bandreject`, `band`, `treble`, `deemph`, and `riaa` filters and transformations.\n* New datasets added including `LJSpeech` and `SpeechCommands` datasets.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "See the release full notes [here](https://github.com/pytorch/audio/releases) and full docs can be found [here](https://pytorch.org/audio/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "torchvision 0.6\nThe torchvision 0.6 release includes updates to datasets, models and a significant number of bug fixes. Highlights include:\n\n* Faster R-CNN now supports negative samples which allows the feeding of images without annotations at training time.\n* Added `aligned` flag to `RoIAlign` to match Detectron2. \n* Refactored abstractions for C++ video decoder", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "See the release full notes [here](https://github.com/pytorch/vision/releases) and full docs can be found [here](https://pytorch.org/docs/stable/torchvision/index.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "torchtext 0.6\nThe torchtext 0.6 release includes a number of bug fixes and improvements to documentation. Based on user's feedback, dataset abstractions are currently being redesigned also. Highlights for the release include:\n\n* Fixed an issue related to the SentencePiece dependency in conda package.\n* Added support for the experimental IMDB dataset to allow a custom vocab.\n* A number of documentation updates including adding a code of conduct and a deduplication of the docs on the torchtext site.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "Your feedback and discussions on the experimental datasets API are welcomed. You can send them to [issue #664](https://github.com/pytorch/text/issues/664). We would also like to highlight the pull request [here](https://github.com/pytorch/text/pull/701) where the latest dataset abstraction is applied to the text classification datasets. The feedback can be beneficial to finalizing this abstraction.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "See the release full notes [here](https://github.com/pytorch/text/releases) and full docs can be found [here](https://pytorch.org/text/).\n\n\n*We\u2019d like to thank the entire PyTorch team, the Amazon team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Understanding LazyTensor System Performance with PyTorch/XLA on Cloud TPU\"\nauthor: Vaibhav Singh\nfeatured-img: \"\"\n---", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\nEase of use, expressivity, and debuggability are among the core principles of PyTorch. One of the key drivers for the ease of use is that PyTorch execution is by default \u201ceager, i.e. op by op execution preserves the imperative nature of the program. However, eager execution does not offer the compiler based optimization, for example, the optimizations when the computation can be expressed as a graph.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "LazyTensor [[1]], first introduced with PyTorch/XLA, helps combine these seemingly disparate approaches. While PyTorch eager execution is widely used, intuitive, and well understood, lazy execution is not as prevalent yet.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "In this post we will explore some of the basic concepts of the LazyTensor System with the goal of applying these concepts to understand and debug performance of LazyTensor based implementations in PyTorch. Although we will use PyTorch/XLA on Cloud TPU as the vehicle for exploring these concepts, we hope that these ideas will be useful to understand other system(s) built on LazyTensors.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "LazyTensor\n\nAny operation performed on a PyTorch tensor is by default dispatched as a kernel or a composition of kernels to the underlying hardware. These kernels are executed asynchronously on the underlying hardware. The program execution is not blocked until the value of a tensor is fetched. This approach scales extremely well with massively parallel programmed hardware such as GPUs.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "The starting point of a LazyTensor system is a custom tensor type. In PyTorch/XLA, this type is called XLA tensor. In contrast to PyTorch\u2019s native tensor type, operations performed on XLA tensors are recorded into an IR graph. Let\u2019s examine an example that sums the product of two tensors:\n\n```python\nimport torch\nimport torch_xla\nimport torch_xla.core.xla_model as xm\n\ndev = xm.xla_device()\n\nx1 = torch.rand((3, 3)).to(dev)\nx2 = torch.rand((3, 8)).to(dev)", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "y1 = torch.einsum('bs,st->bt', x1, x2)\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "You can execute [this](https://github.com/ultrons/xla/blob/lazy-tensor-post/contrib/colab/LazyTensor_Basics.ipynb) colab notebook to examine the resulting graph for y1. Notice that no computation has been performed yet.\n\n```python\ny1 = y1 + x2\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "The operations will continue until PyTorch/XLA encounters a barrier. This barrier can either be a [mark step()](https://github.com/pytorch/xla/blob/ff079bb48744e5aa6696201ccf34057f15fc7cac/torch_xla/core/xla_model.py#L751) api call or any other event which forces the execution of the graph recorded so far.\n\n```python\nxm.mark_step()\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Once the mark_step() is called, the graph is compiled and then executed on TPU, i.e. the tensors have been materialized. Therefore, the graph is now reduced to a single line y1 tensor which holds the result of the computation.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Compile Once, Execute Often", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "XLA compilation passes offer optimizations (e.g. op-fusion, which reduces HBM pressure by using scratch-pad memory for multiple ops, [ref](https://arxiv.org/pdf/2004.13336.pdf) ) and leverages lower level XLA infrastructure to optimally use the underlying hardware. However, there is one caveat, compilation passes are expensive, i.e. can add to the training step time. Therefore, this approach scales well if and only if we can **compile once and execute often** (compilation cache helps, such that the same", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "graph is not compiled more than once).", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "In the following example, we create a small computation graph and time the execution:\n\n```python\ny1 = torch.rand((3, 8)).to(dev)\ndef dummy_step() :\n y1 = torch.einsum('bs,st->bt', y1, x)\n xm.mark_step()\n return y1", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "```python\n%timeit dummy_step\n```\n\n```python\nThe slowest run took 29.74 times longer than the fastest. This could mean that an intermediate result is being cached.\n10000000 loops, best of 5: 34.2 ns per loop", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "You notice that the slowest step is quite longer than the fastest. This is because of the graph compilation overhead which is incurred only once for a given shape of graph, input shape, and output shape. Subsequent steps are faster because no graph compilation is necessary.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "This also implies that we expect to see performance cliffs when the \u201ccompile once and execute often\u201d assumption breaks. Understanding when this assumption breaks is the key to understanding and optimizing the performance of a LazyTensor system. Let\u2019s examine what triggers the compilation.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Graph Compilation and Execution and LazyTensor Barrier", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "We saw that the computation graph is compiled and executed when a LazyTensor barrier is encountered. There are three scenarios when the LazyTensor barrier is automatically or manually introduced. The first is the explicit call of mark_step() api as shown in the preceding example. mark_step() is also called implicitly at every step when you wrap your dataloader with MpDeviceLoader (highly recommended to overlap compute and data upload to TPU device). The [Optimizer", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "step](https://github.com/pytorch/xla/blob/master/torch_xla/core/xla_model.py#L804) method of xla_model also allows to implicitly call mark_step (when you set barrier=True).", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "The second scenario where a barrier is introduced is when PyTorch/XLA finds an op with no mapping (lowering) to equivalent XLA HLO ops. PyTorch has [2000+](https://dev-discuss.pytorch.org/t/where-do-the-2000-pytorch-operators-come-from-more-than-you-wanted-to-know/373) operations. Although most of these operations are composite (i.e. can be expressed in terms of other fundamental operations), some of these operations do not have corresponding lowering in XLA.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "What happens when an op with no XLA lowering is used? PyTorch XLA stops the operation recording and cuts the graph(s) leading to the input(s) of the unlowered op. This cut graph is then compiled and dispatched for execution. The results (materialized tensor) of execution are sent back from device to host, the unlowered op is then executed on the host (cpu), and then downstream LazyTensor operations creating a new graph(s) until a barrier is encountered again.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "The third and final scenario which results in a LazyTensor barrier is when there is a control structure/statement or another method which requires the value of a tensor. This statement would at the minimum cause the execution of the computation graph leading to the tensor (if the graph has already been seen) or cause compilation and execution of both.\n\nOther examples of such methods include .item(), isEqual(). In general, any operation that maps Tensor -> Scalar will cause this behavior.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Dynamic Graph\n\nAs illustrated in the preceding section, graph compilation cost is amortized if the same shape of the graph is executed many times. It\u2019s because the compiled graph is cached with a hash derived from the graph shape, input shape, and the output shape. If these shapes change it will trigger compilation, and too frequent compilation will result in training time degradation.\n\nLet\u2019s consider the following example:", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "```python\ndef dummy_step(x, y, loss, acc=False):\n z = torch.einsum('bs,st->bt', y, x)\n step_loss = z.sum().view(1,)\n if acc:\n loss = torch.cat((loss, step_loss))\n else:\n loss = step_loss\n xm.mark_step()\n return loss", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "import time\ndef measure_time(acc=False):\n exec_times = []\n iter_count = 100\n x = torch.rand((512, 8)).to(dev)\n y = torch.rand((512, 512)).to(dev)\n loss = torch.zeros(1).to(dev)\n for i in range(iter_count):\n tic = time.time()\n loss = dummy_step(x, y, loss, acc=acc)\n toc = time.time()\n exec_times.append(toc - tic)\n return exec_times", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "dyn = measure_time(acc=True) # acc= True Results in dynamic graph\nst = measure_time(acc=False) # Static graph, computation shape, inputs and output shapes don't change\n\nimport matplotlib.pyplot as plt\nplt.plot(st, label = 'static graph')\nplt.plot(dyn, label = 'dynamic graph')\nplt.legend()\nplt.title('Execution time in seconds')", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nNote that static and dynamic cases have the same computation but dynamic graph compiles every time, leading to the higher overall run-time. In practice, the training step with recompilation can sometimes be an order of magnitude or slower. In the next section we discuss some of the PyTorch/XLA tools to debug training degradation.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Profiling Training Performance with PyTorch/XLA", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "PyTorch/XLA profiling consists of two major components. First is the client side profiling. This feature is turned on by simply setting the environment variable PT_XLA_DEBUG to 1. Client side profiling points to unlowered ops or device-to-host transfer in your source code. Client side profiling also reports if there are too frequent compilations happening during the training. You can explore some metrics and counters provided by PyTorch/XLA in conjunction with the profiler in", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "[this](https://github.com/ultrons/xla/blob/lazy-tensor-post/contrib/colab/Exploring_LazyTensor_with_Debug_Metrics.ipynb) notebook.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "The second component offered by PyTorch/XLA profiler is the inline trace annotation. For example:\n\n```python\nimport torch_xla.debug.profiler as xp", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "def train_imagenet():\n print('==> Preparing data..')\n img_dim = get_model_property('img_dim')\n ....\n server = xp.start_server(3294)\n def train_loop_fn(loader, epoch):\n ....\n model.train()\n for step, (data, target) in enumerate(loader):\n with xp.StepTrace('Train_Step', step_num=step):\n ....\n if FLAGS.amp:\n ....\n else:\n with xp.Trace('build_graph'):\n output = model(data)\n loss = loss_fn(output, target)\n loss.backward()", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "xm.optimizer_step(optimizer)", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Notice the start_server api call. The port number that you have used here is the same port number you will use with the tensorboard profiler in order to view the op trace similar to:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Op trace along with the client-side debugging function is a powerful set of tools to debug and optimize your training performance with PyTorch/XLA. For more detailed instructions on the profiler usage, the reader is encouraged to explore blogs [part-1](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-tpu-vm-part-1), [part-2](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-cloud-tpu-vm-part-ii), and", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "[part-3](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-cloud-tpu-vm-part-iii) of the blog series on PyTorch/XLA performance debugging.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Summary\n\nIn this article we have reviewed the fundamentals of the LazyTensor system. We built on those fundamentals with PyTorch/XLA to understand the potential causes of training performance degradation. We discussed why \u201ccompile once and execute often\u201d helps to get the best performance on LazyTensor systems, and why training slows down when this assumption breaks.\n\nWe hope that PyTorch users will find these insights helpful for their novel works with LazyTensor systems.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "A big thank you to my outstanding colleagues Jack Cao, Milad Mohammedi, Karl Weinmeister, Rajesh Thallam, Jordan Tottan (Google) and Geeta Chauhan (Meta) for their meticulous reviews and feedback. And thanks to the extended PyTorch/XLA development team from Google, Meta, and the open source community to make PyTorch possible on TPUs. And finally, thanks to the authors of the [LazyTensor paper](https://arxiv.org/pdf/2102.13267.pdf) not only for developing LazyTensor but also for writing such an accessible", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "paper.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "Refrences\n\n[[1]] LazyTensor: combining eager execution with domain-specific compilers\n\n[1]: https://arxiv.org/pdf/2102.13267.pdf", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Extending TorchVision\u2019s Transforms to Object Detection, Segmentation & Video tasks\"\nauthor: Philip Meier, Victor Fomin, Vasilis Vryniotis, Nicolas Hug\nfeatured-img: \"assets/images/Transforms-v2-feature-image.png\"\n---\n\n**Note**: A previous version of this post was published in November 2022. We have updated this post with the most up-to-date info, in view of the upcoming 0.15 release of torchvision in March 2023, jointly with PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "TorchVision is extending its Transforms API! Here is what\u2019s new:\n\n- You can use them not only for Image Classification but also for Object Detection, Instance & Semantic Segmentation and Video Classification.\n- You can use new functional transforms for transforming Videos, Bounding Boxes and Segmentation Masks.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "The API is completely backward compatible with the previous one, and remains the same to assist the migration and adoption. We are now releasing this new API as Beta in the torchvision.transforms.v2 namespace, and we would love to get early feedback from you to improve its functionality. Please [_reach out to us_](https://github.com/pytorch/vision/issues/6753) if you have any questions or suggestions.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "Limitations of current Transforms\n\nThe existing Transforms API of TorchVision (aka V1) only supports single images. As a result it can only be used for classification tasks:\n\n```python\nfrom torchvision import transforms\ntrans = transforms.Compose([\n transforms.ColorJitter(contrast=0.5),\n transforms.RandomRotation(30),\n transforms.CenterCrop(480),\n])\nimgs = trans(imgs)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "* Support for both Python-based and TorchScript-based models\n* Default handlers for common use cases (e.g., image segmentation, text classification) as well as the ability to write custom handlers for other use cases\n* Model versioning, the ability to run multiple versions of a model at the same time, and the ability to roll back to an earlier version\n* The ability to package a model, learning weights, and supporting files (e.g., class mappings, vocabularies) into a single, persistent artifact (a.k.a. the \u201cmodel archive\u201d)\n* Robust management capability, allowing full configuration of models, versions, and individual worker threads via command line, config file, or run-time API\n* Automatic batching of individual inferences across HTTP requests\n* Logging including common metrics, and the ability to incorporate custom metrics\n* Ready-made Dockerfile for easy deployment\n* HTTPS support for secure deployment", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "To learn more about the APIs and the design of this feature, see the links below:\n* See for a full multi-node deployment reference architecture.\n* The full documentation can be found [here](https://pytorch.org/serve).\n\n## TorchElastic integration with Kubernetes (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "[TorchElastic](https://github.com/pytorch/elastic) is a proven library for training large scale deep neural networks at scale within companies like Facebook, where having the ability to dynamically adapt to server availability and scale as new compute resources come online is critical. Kubernetes enables customers using machine learning frameworks like PyTorch to run training jobs distributed across fleets of powerful GPU instances like the Amazon EC2 P3. Distributed training jobs, however, are not fault-tolerant, and a job cannot continue if a node failure or reclamation interrupts training. Further, jobs cannot start without acquiring all required resources, or scale up and down without being restarted. This lack of resiliency and flexibility results in increased training time and costs from idle resources. TorchElastic addresses these limitations by enabling distributed training jobs to be executed in a fault-tolerant and elastic manner. Until today, Kubernetes users needed to manage Pods and Services required for TorchElastic training jobs manually.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "Through the joint collaboration of engineers at Facebook and AWS, TorchElastic, adding elasticity and fault tolerance, is now supported using vanilla Kubernetes and through the managed EKS service from AWS.\n\nTo learn more see the [TorchElastic repo](http://pytorch.org/elastic/0.2.0rc0/kubernetes.html) for the controller implementation and docs on how to use it.\n\n## torch_xla 1.5 now available", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "[torch_xla](http://pytorch.org/xla/) is a Python package that uses the [XLA linear algebra compiler](https://www.tensorflow.org/xla) to accelerate the [PyTorch deep learning framework](https://pytorch.org/) on [Cloud TPUs](https://cloud.google.com/tpu/) and [Cloud TPU Pods](https://cloud.google.com/tpu/docs/tutorials/pytorch-pod). torch_xla aims to give PyTorch users the ability to do everything they can do on GPUs on Cloud TPUs as well while minimizing changes to the user experience. The project began with a conversation at NeurIPS 2017 and gathered momentum in 2018 when teams from Facebook and Google came together to create a proof of concept. We announced this collaboration at PTDC 2018 and made the PyTorch/XLA integration broadly available at PTDC 2019. The project already has 28 contributors, nearly 2k commits, and a repo that has been forked more than 100 times.", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "This release of [torch_xla](http://pytorch.org/xla/) is aligned and tested with PyTorch 1.5 to reduce friction for developers and to provide a stable and mature PyTorch/XLA stack for training models using Cloud TPU hardware. You can [try it for free](https://medium.com/pytorch/get-started-with-pytorch-cloud-tpus-and-colab-a24757b8f7fc) in your browser on an 8-core Cloud TPU device with [Google Colab](https://colab.research.google.com/), and you can use it at a much larger scaleon [Google Cloud](https://cloud.google.com/gcp).\n\nSee the full torch_xla release notes [here](https://github.com/pytorch/xla/releases). Full docs and tutorials can be found [here](https://pytorch.org/xla/) and [here](https://cloud.google.com/tpu/docs/tutorials).\n\n## PyTorch Domain Libraries", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "torchaudio, torchvision, and torchtext complement PyTorch with common datasets, models, and transforms in each domain area. We\u2019re excited to share new releases for all three domain libraries alongside PyTorch 1.5 and the rest of the library updates. For this release, all three domain libraries are removing support for Python2 and will support Python3 only.\n\n### torchaudio 0.5\nThe torchaudio 0.5 release includes new transforms, functionals, and datasets. Highlights for the release include:\n\n* Added the Griffin-Lim functional and transform, `InverseMelScale` and `Vol` transforms, and `DB_to_amplitude`. \n* Added support for `allpass`, `fade`, `bandpass`, `bandreject`, `band`, `treble`, `deemph`, and `riaa` filters and transformations.\n* New datasets added including `LJSpeech` and `SpeechCommands` datasets. \n\nSee the release full notes [here](https://github.com/pytorch/audio/releases) and full docs can be found [here](https://pytorch.org/audio/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "### torchvision 0.6\nThe torchvision 0.6 release includes updates to datasets, models and a significant number of bug fixes. Highlights include:\n\n* Faster R-CNN now supports negative samples which allows the feeding of images without annotations at training time.\n* Added `aligned` flag to `RoIAlign` to match Detectron2. \n* Refactored abstractions for C++ video decoder\n\nSee the release full notes [here](https://github.com/pytorch/vision/releases) and full docs can be found [here](https://pytorch.org/docs/stable/torchvision/index.html).\n\n### torchtext 0.6\nThe torchtext 0.6 release includes a number of bug fixes and improvements to documentation. Based on user's feedback, dataset abstractions are currently being redesigned also. Highlights for the release include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "* Fixed an issue related to the SentencePiece dependency in conda package.\n* Added support for the experimental IMDB dataset to allow a custom vocab.\n* A number of documentation updates including adding a code of conduct and a deduplication of the docs on the torchtext site. \n\nYour feedback and discussions on the experimental datasets API are welcomed. You can send them to [issue #664](https://github.com/pytorch/text/issues/664). We would also like to highlight the pull request [here](https://github.com/pytorch/text/pull/701) where the latest dataset abstraction is applied to the text classification datasets. The feedback can be beneficial to finalizing this abstraction. \n\nSee the release full notes [here](https://github.com/pytorch/text/releases) and full docs can be found [here](https://pytorch.org/text/).\n\n\n*We\u2019d like to thank the entire PyTorch team, the Amazon team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-library-updates-new-model-serving-library/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Understanding LazyTensor System Performance with PyTorch/XLA on Cloud TPU\"\nauthor: Vaibhav Singh\nfeatured-img: \"\"\n---\n\n## Introduction\n\nEase of use, expressivity, and debuggability are among the core principles of PyTorch. One of the key drivers for the ease of use is that PyTorch execution is by default \u201ceager, i.e. op by op execution preserves the imperative nature of the program. However, eager execution does not offer the compiler based optimization, for example, the optimizations when the computation can be expressed as a graph.\n\nLazyTensor [[1]], first introduced with PyTorch/XLA, helps combine these seemingly disparate approaches. While PyTorch eager execution is widely used, intuitive, and well understood, lazy execution is not as prevalent yet.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "In this post we will explore some of the basic concepts of the LazyTensor System with the goal of applying these concepts to understand and debug performance of LazyTensor based implementations in PyTorch. Although we will use PyTorch/XLA on Cloud TPU as the vehicle for exploring these concepts, we hope that these ideas will be useful to understand other system(s) built on LazyTensors.\n\n## LazyTensor\n\nAny operation performed on a PyTorch tensor is by default dispatched as a kernel or a composition of kernels to the underlying hardware. These kernels are executed asynchronously on the underlying hardware. The program execution is not blocked until the value of a tensor is fetched. This approach scales extremely well with massively parallel programmed hardware such as GPUs.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "The starting point of a LazyTensor system is a custom tensor type. In PyTorch/XLA, this type is called XLA tensor. In contrast to PyTorch\u2019s native tensor type, operations performed on XLA tensors are recorded into an IR graph. Let\u2019s examine an example that sums the product of two tensors:\n\n```python\nimport torch\nimport torch_xla\nimport torch_xla.core.xla_model as xm\n\ndev = xm.xla_device()\n\nx1 = torch.rand((3, 3)).to(dev)\nx2 = torch.rand((3, 8)).to(dev)\n\ny1 = torch.einsum('bs,st->bt', x1, x2)\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))\n```\n\nYou can execute [this](https://github.com/ultrons/xla/blob/lazy-tensor-post/contrib/colab/LazyTensor_Basics.ipynb) colab notebook to examine the resulting graph for y1. Notice that no computation has been performed yet.\n\n```python\ny1 = y1 + x2\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))\n```", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "The operations will continue until PyTorch/XLA encounters a barrier. This barrier can either be a [mark step()](https://github.com/pytorch/xla/blob/ff079bb48744e5aa6696201ccf34057f15fc7cac/torch_xla/core/xla_model.py#L751) api call or any other event which forces the execution of the graph recorded so far.\n\n```python\nxm.mark_step()\nprint(torch_xla._XLAC._get_xla_tensors_text([y1]))\n```\n\nOnce the mark_step() is called, the graph is compiled and then executed on TPU, i.e. the tensors have been materialized. Therefore, the graph is now reduced to a single line y1 tensor which holds the result of the computation.\n\n### Compile Once, Execute Often", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "XLA compilation passes offer optimizations (e.g. op-fusion, which reduces HBM pressure by using scratch-pad memory for multiple ops, [ref](https://arxiv.org/pdf/2004.13336.pdf) ) and leverages lower level XLA infrastructure to optimally use the underlying hardware. However, there is one caveat, compilation passes are expensive, i.e. can add to the training step time. Therefore, this approach scales well if and only if we can **compile once and execute often** (compilation cache helps, such that the same graph is not compiled more than once).\n\nIn the following example, we create a small computation graph and time the execution:\n\n```python\ny1 = torch.rand((3, 8)).to(dev)\ndef dummy_step() :\n y1 = torch.einsum('bs,st->bt', y1, x)\n xm.mark_step()\n return y1\n```\n\n```python\n%timeit dummy_step\n```\n\n```python\nThe slowest run took 29.74 times longer than the fastest. This could mean that an intermediate result is being cached.\n10000000 loops, best of 5: 34.2 ns per loop\n```", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "You notice that the slowest step is quite longer than the fastest. This is because of the graph compilation overhead which is incurred only once for a given shape of graph, input shape, and output shape. Subsequent steps are faster because no graph compilation is necessary.\n\nThis also implies that we expect to see performance cliffs when the \u201ccompile once and execute often\u201d assumption breaks. Understanding when this assumption breaks is the key to understanding and optimizing the performance of a LazyTensor system. Let\u2019s examine what triggers the compilation.\n\n### Graph Compilation and Execution and LazyTensor Barrier", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "We saw that the computation graph is compiled and executed when a LazyTensor barrier is encountered. There are three scenarios when the LazyTensor barrier is automatically or manually introduced. The first is the explicit call of mark_step() api as shown in the preceding example. mark_step() is also called implicitly at every step when you wrap your dataloader with MpDeviceLoader (highly recommended to overlap compute and data upload to TPU device). The [Optimizer step](https://github.com/pytorch/xla/blob/master/torch_xla/core/xla_model.py#L804) method of xla_model also allows to implicitly call mark_step (when you set barrier=True).", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "The second scenario where a barrier is introduced is when PyTorch/XLA finds an op with no mapping (lowering) to equivalent XLA HLO ops. PyTorch has [2000+](https://dev-discuss.pytorch.org/t/where-do-the-2000-pytorch-operators-come-from-more-than-you-wanted-to-know/373) operations. Although most of these operations are composite (i.e. can be expressed in terms of other fundamental operations), some of these operations do not have corresponding lowering in XLA.\n\n

\n \n

\n\nWhat happens when an op with no XLA lowering is used? PyTorch XLA stops the operation recording and cuts the graph(s) leading to the input(s) of the unlowered op. This cut graph is then compiled and dispatched for execution. The results (materialized tensor) of execution are sent back from device to host, the unlowered op is then executed on the host (cpu), and then downstream LazyTensor operations creating a new graph(s) until a barrier is encountered again.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "The third and final scenario which results in a LazyTensor barrier is when there is a control structure/statement or another method which requires the value of a tensor. This statement would at the minimum cause the execution of the computation graph leading to the tensor (if the graph has already been seen) or cause compilation and execution of both.\n\nOther examples of such methods include .item(), isEqual(). In general, any operation that maps Tensor -> Scalar will cause this behavior.\n\n### Dynamic Graph\n\nAs illustrated in the preceding section, graph compilation cost is amortized if the same shape of the graph is executed many times. It\u2019s because the compiled graph is cached with a hash derived from the graph shape, input shape, and the output shape. If these shapes change it will trigger compilation, and too frequent compilation will result in training time degradation.\n\nLet\u2019s consider the following example:", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "```python\ndef dummy_step(x, y, loss, acc=False):\n z = torch.einsum('bs,st->bt', y, x)\n step_loss = z.sum().view(1,)\n if acc:\n loss = torch.cat((loss, step_loss))\n else:\n loss = step_loss\n xm.mark_step()\n return loss\n\n\nimport time\ndef measure_time(acc=False):\n exec_times = []\n iter_count = 100\n x = torch.rand((512, 8)).to(dev)\n y = torch.rand((512, 512)).to(dev)\n loss = torch.zeros(1).to(dev)\n for i in range(iter_count):\n tic = time.time()\n loss = dummy_step(x, y, loss, acc=acc)\n toc = time.time()\n exec_times.append(toc - tic)\n return exec_times\n\ndyn = measure_time(acc=True) # acc= True Results in dynamic graph\nst = measure_time(acc=False) # Static graph, computation shape, inputs and output shapes don't change\n\nimport matplotlib.pyplot as plt\nplt.plot(st, label = 'static graph')\nplt.plot(dyn, label = 'dynamic graph')\nplt.legend()\nplt.title('Execution time in seconds')\n```\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "Note that static and dynamic cases have the same computation but dynamic graph compiles every time, leading to the higher overall run-time. In practice, the training step with recompilation can sometimes be an order of magnitude or slower. In the next section we discuss some of the PyTorch/XLA tools to debug training degradation.\n\n### Profiling Training Performance with PyTorch/XLA\n\nPyTorch/XLA profiling consists of two major components. First is the client side profiling. This feature is turned on by simply setting the environment variable PT_XLA_DEBUG to 1. Client side profiling points to unlowered ops or device-to-host transfer in your source code. Client side profiling also reports if there are too frequent compilations happening during the training. You can explore some metrics and counters provided by PyTorch/XLA in conjunction with the profiler in [this](https://github.com/ultrons/xla/blob/lazy-tensor-post/contrib/colab/Exploring_LazyTensor_with_Debug_Metrics.ipynb) notebook.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "The second component offered by PyTorch/XLA profiler is the inline trace annotation. For example:\n\n```python\nimport torch_xla.debug.profiler as xp\n\ndef train_imagenet():\n print('==> Preparing data..')\n img_dim = get_model_property('img_dim')\n ....\n server = xp.start_server(3294)\n def train_loop_fn(loader, epoch):\n ....\n model.train()\n for step, (data, target) in enumerate(loader):\n with xp.StepTrace('Train_Step', step_num=step):\n ....\n if FLAGS.amp:\n ....\n else:\n with xp.Trace('build_graph'):\n output = model(data)\n loss = loss_fn(output, target)\n loss.backward()\n xm.optimizer_step(optimizer)\n```\n\nNotice the start_server api call. The port number that you have used here is the same port number you will use with the tensorboard profiler in order to view the op trace similar to:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "Op trace along with the client-side debugging function is a powerful set of tools to debug and optimize your training performance with PyTorch/XLA. For more detailed instructions on the profiler usage, the reader is encouraged to explore blogs [part-1](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-tpu-vm-part-1), [part-2](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-cloud-tpu-vm-part-ii), and [part-3](https://cloud.google.com/blog/topics/developers-practitioners/pytorchxla-performance-debugging-cloud-tpu-vm-part-iii) of the blog series on PyTorch/XLA performance debugging.\n\n### Summary", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "### Summary\n\nIn this article we have reviewed the fundamentals of the LazyTensor system. We built on those fundamentals with PyTorch/XLA to understand the potential causes of training performance degradation. We discussed why \u201ccompile once and execute often\u201d helps to get the best performance on LazyTensor systems, and why training slows down when this assumption breaks.\n\nWe hope that PyTorch users will find these insights helpful for their novel works with LazyTensor systems.\n\n### Acknowledgements\n\nA big thank you to my outstanding colleagues Jack Cao, Milad Mohammedi, Karl Weinmeister, Rajesh Thallam, Jordan Tottan (Google) and Geeta Chauhan (Meta) for their meticulous reviews and feedback. And thanks to the extended PyTorch/XLA development team from Google, Meta, and the open source community to make PyTorch possible on TPUs. And finally, thanks to the authors of the [LazyTensor paper](https://arxiv.org/pdf/2102.13267.pdf) not only for developing LazyTensor but also for writing such an accessible paper.", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "## Refrences\n\n[[1]] LazyTensor: combining eager execution with domain-specific compilers\n\n[1]: https://arxiv.org/pdf/2102.13267.pdf", "metadata": {"source": "https://pytorch.org/blog/understanding-lazytensor-system-performance-with-pytorch-xla-on-cloud-tpu/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Extending TorchVision\u2019s Transforms to Object Detection, Segmentation & Video tasks\"\nauthor: Philip Meier, Victor Fomin, Vasilis Vryniotis, Nicolas Hug\nfeatured-img: \"assets/images/Transforms-v2-feature-image.png\"\n---\n\n**Note**: A previous version of this post was published in November 2022. We have updated this post with the most up-to-date info, in view of the upcoming 0.15 release of torchvision in March 2023, jointly with PyTorch 2.0.\n\nTorchVision is extending its Transforms API! Here is what\u2019s new:\n\n- You can use them not only for Image Classification but also for Object Detection, Instance & Semantic Segmentation and Video Classification.\n- You can use new functional transforms for transforming Videos, Bounding Boxes and Segmentation Masks.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "The API is completely backward compatible with the previous one, and remains the same to assist the migration and adoption. We are now releasing this new API as Beta in the torchvision.transforms.v2 namespace, and we would love to get early feedback from you to improve its functionality. Please [_reach out to us_](https://github.com/pytorch/vision/issues/6753) if you have any questions or suggestions.\n\n## Limitations of current Transforms\n\nThe existing Transforms API of TorchVision (aka V1) only supports single images. As a result it can only be used for classification tasks:\n\n```python\nfrom torchvision import transforms\ntrans = transforms.Compose([\n transforms.ColorJitter(contrast=0.5),\n transforms.RandomRotation(30),\n transforms.CenterCrop(480),\n])\nimgs = trans(imgs)\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} {"page_content": "The above approach doesn\u2019t support Object Detection nor Segmentation. This limitation made any non-classification Computer Vision tasks second-class citizens as one couldn\u2019t use the Transforms API to perform the necessary augmentations. Historically this made it difficult to train high-accuracy models using TorchVision\u2019s primitives and thus our Model Zoo lagged by several points from SoTA.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "To circumvent this limitation, TorchVision offered [_custom implementations_](https://github.com/pytorch/vision/blob/main/references/detection/transforms.py) in its reference scripts that show-cased how one could perform augmentations in each task. Though this practice enabled us to train high accuracy [_classification_](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/), [_object detection &", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "segmentation_](https://pytorch.org/blog/pytorch-1.12-new-library-releases/#beta-object-detection-and-instance-segmentation) models, it was a hacky approach which made those transforms impossible to import from the TorchVision binary.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "The new Transforms API\n\nThe Transforms V2 API supports videos, bounding boxes, and segmentation masks meaning that it offers native support for many Computer Vision tasks. The new solution is a drop-in replacement:\n\n```python\nimport torchvision.transforms.v2 as transforms\n\n# Exactly the same interface as V1:\ntrans = transforms.Compose([\n transforms.ColorJitter(contrast=0.5),\n transforms.RandomRotation(30),\n transforms.CenterCrop(480),\n])\nimgs, bboxes, labels = trans(imgs, bboxes, labels)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "The new Transform Classes can receive any arbitrary number of inputs without enforcing specific order or structure:\n\n```python\n# Already supported:\ntrans(imgs) # Image Classification\ntrans(videos) # Video Tasks\ntrans(imgs, bboxes, labels) # Object Detection\ntrans(imgs, bboxes, masks, labels) # Instance Segmentation\ntrans(imgs, masks) # Semantic Segmentation\ntrans({\"image\": imgs, \"box\": bboxes, \"tag\": labels}) # Arbitrary Structure", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "# Future support:\ntrans(imgs, bboxes, labels, keypoints) # Keypoint Detection\ntrans(stereo_images, disparities, masks) # Depth Perception\ntrans(image1, image2, optical_flows, masks) # Optical Flow\ntrans(imgs_or_videos, labels) # MixUp/CutMix-style Transforms", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "The Transform Classes make sure that they apply the same random transforms to all the inputs to ensure consistent results.\n\nThe functional API has been updated to support all necessary signal processing kernels (resizing, cropping, affine transforms, padding etc) for all inputs:\n\n```python\nfrom torchvision.transforms.v2 import functional as F", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "# High-level dispatcher, accepts any supported input type, fully BC\nF.resize(inpt, size=[224, 224])\n# Image tensor kernel\nF.resize_image_tensor(img_tensor, size=[224, 224], antialias=True) \n# PIL image kernel\nF.resize_image_pil(img_pil, size=[224, 224], interpolation=BILINEAR)\n# Video kernel\nF.resize_video(video, size=[224, 224], antialias=True) \n# Mask kernel\nF.resize_mask(mask, size=[224, 224])\n# Bounding box kernel\nF.resize_bounding_box(bbox, size=[224, 224], spatial_size=[256, 256])", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "Under the hood, the API uses Tensor subclassing to wrap the input, attach useful meta-data and dispatch to the right kernel. For your data to be compatible with these new transforms, you can either use the provided dataset wrapper which should work with most of torchvision built-in datasets, or your can wrap your data manually into Datapoints:", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchvision.datasets import wrap_dataset_for_transforms_v2\nds = CocoDetection(..., transforms=v2_transforms)\nds = wrap_dataset_for_transforms_v2(ds) # data is now compatible with transforms v2!\n\n# Or wrap your data manually using the lower-level Datapoint classes:\nfrom torchvision import datapoints\n\nimgs = datapoints.Image(images)\nvids = datapoints.Video(videos)\nmasks = datapoints.Mask(target[\"masks\u201c])\nbboxes = datapoints.BoundingBox(target[\"boxes\u201c], format=\u201dXYXY\u201d, spatial_size=imgs.shape)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "In addition to the new API, we now provide importable implementations for several data augmentations that are used in SoTA research such as [_Large Scale Jitter_](https://github.com/pytorch/vision/blob/928b05cad36eadb13e169f03028767c8bcd1f21d/torchvision/transforms/v2/_geometry.py#L1109), [_AutoAugmentation_](https://github.com/pytorch/vision/blob/main/torchvision/transforms/v2/_auto_augment.py) methods and [_several_](https://github.com/pytorch/vision/blob/main/torchvision/transforms/v2/__init__.py) new", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "Geometric, Color and Type Conversion transforms.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "The API continues to support both PIL and Tensor backends for Images, single or batched input and maintains JIT-scriptability on both the functional and class APIs.. The new API has been [_verified_](https://github.com/pytorch/vision/pull/6433#issuecomment-1256741233) to achieve the same accuracy as the previous implementation.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "An end-to-end example\n\n Here is an example of the new API using the following [_image_](https://user-images.githubusercontent.com/5347466/195350223-8683ef25-1367-4292-9174-c15f85c7358e.jpg). It works both with PIL images and Tensors. For more examples and tutorials, [_take a look at our gallery!_](https://pytorch.org/vision/0.15/auto_examples/index.html)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchvision import io, utils\nfrom torchvision import datapoints\nfrom torchvision.transforms import v2 as T\nfrom torchvision.transforms.v2 import functional as F", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "# Defining and wrapping input to appropriate Tensor Subclasses\npath = \"COCO_val2014_000000418825.jpg\"\nimg = datapoints.Image(io.read_image(path))\n# img = PIL.Image.open(path)\nbboxes = datapoints.BoundingBox(\n [[2, 0, 206, 253], [396, 92, 479, 241], [328, 253, 417, 332],\n [148, 68, 256, 182], [93, 158, 170, 260], [432, 0, 438, 26],\n [422, 0, 480, 25], [419, 39, 424, 52], [448, 37, 456, 62],\n [435, 43, 437, 50], [461, 36, 469, 63], [461, 75, 469, 94],", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "[469, 36, 480, 64], [440, 37, 446, 56], [398, 233, 480, 304],\n [452, 39, 463, 63], [424, 38, 429, 50]],\n format=datapoints.BoundingBoxFormat.XYXY,\n spatial_size=F.get_spatial_size(img),\n)\nlabels = [59, 58, 50, 64, 76, 74, 74, 74, 74, 74, 74, 74, 74, 74, 50, 74, 74]\n# Defining and applying Transforms V2\ntrans = T.Compose(\n [\n T.ColorJitter(contrast=0.5),\n T.RandomRotation(30),\n T.CenterCrop(480),\n ]\n)\nimg, bboxes, labels = trans(img, bboxes, labels)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "# Visualizing results\nviz = utils.draw_bounding_boxes(F.to_image_tensor(img), boxes=bboxes)\nF.to_pil_image(viz).show()\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "Development milestones and future work\n\nHere is where we are in development:", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "- [x] Design API\n- [x] Write Kernels for transforming Videos, Bounding Boxes, Masks and Labels\n- [x] Rewrite all existing Transform Classes (stable + references) on the new API:\n - [x] Image Classification\n - [x] Video Classification\n - [x] Object Detection\n - [x] Instance Segmentation\n - [x] Semantic Segmentation\n- [x] Verify the accuracy of the new API for all supported Tasks and Backends\n- [x] Speed Benchmarks and Performance Optimizations (in progress - planned for Dec)", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "- [x] Graduate from Prototype (planned for Q1)\n- [ ] Add support of Depth Perception, Keypoint Detection, Optical Flow and more (future)\n- [ ] Add smooth support for batch-wise transforms like MixUp and CutMix", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "We would love to get [_feedback_](https://github.com/pytorch/vision/issues/6753) from you to improve its functionality. Please reach out to us if you have any questions or suggestions.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"New Library Updates in PyTorch 1.13\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/new-library-updates-in-pytorch-1.13-2.jpg\"\n---", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Summary\n\nWe are bringing a number of improvements to the current PyTorch libraries, alongside the PyTorch 1.13 [release](https://github.com/pytorch/pytorch/releases). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch.\n\nAlong with **1.13**, we are releasing updates to the PyTorch Libraries, please find them below.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Hybrid Demucs Model and Pipeline\n\nHybrid Demucs is a music source separation model that uses both spectrogram and time domain features. It has demonstrated state-of-the-art performance in the Sony\u00ae Music DeMixing Challenge. (citation: [https://arxiv.org/abs/2111.03600](https://arxiv.org/abs/2111.03600))\n\nThe TorchAudio v0.13 release includes the following features", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- MUSDB_HQ Dataset, which is used in Hybrid Demucs training ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.MUSDB_HQ.html#torchaudio.datasets.MUSDB_HQ))\n- Hybrid Demucs model architecture ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.models.HDemucs.html#torchaudio.models.HDemucs))\n- Three factory functions suitable for different sample rate ranges\n- Pre-trained pipelines ([docs](https://pytorch.org/audio/0.13.0/pipelines.html#id46))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- SDR Results of pre-trained pipelines on MUSDB_HQ test set\n- Tutorial that steps through music source separation using the pretrained pipeline ([docs](https://pytorch.org/audio/0.13.0/tutorials/hybrid_demucs_tutorial.html))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "| Pipeline | All | Drums | Bass | Other | Vocals |\n|----------------------------------------|-------|-------|--------|-------|--------|\n| HDEMUCS_HIGH_MUSDB* | 6.42 | 7.76 | 6.51 | 4.47 | 6.93 |\n| HDEMUCS_HIGH_MUSDB_PLUS** | 9.37 | 11.38 | 10.53 | 7.24 | 8.32 |", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "

* Trained on the training data of MUSDB-HQ dataset.
** Trained on both training and test sets of MUSDB-HQ and 150 extra songs from an internal database that were specifically produced for Meta.

\n\n```python\nfrom torchaudio.pipelines import HDEMUCS_HIGH_MUSDB_PLUS\n\nbundle = HDEMUCS_HIGH_MUSDB_PLUS\nmodel = bundle.get_model()\nsources_list = model.sources\n\nmixture, samplerate = torchaudio.load(\"song.wav\")\nsources = model(mixture)\naudios = dict(zip(sources_list, sources)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Special thanks to Alexandre Defossez for the guidance.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Datasets and Metadata Mode for SUPERB Benchmark\n\nTorchAudio adds support for various audio-related datasets used in downstream tasks for benchmarking self-supervised learning models. With the addition of several new datasets, there is now support for the downstream tasks in version 1 of the [SUPERB benchmark](https://superbbenchmark.org/), which can be found in the [s3prl repository](https://github.com/s3prl/s3prl/blob/master/s3prl/downstream/docs/superb.md).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "For these datasets, we also add metadata support through a `get_metadata` function, enabling faster dataset iteration or preprocessing without the need to load waveforms. The function returns the same features as `__getitem__`, except it returns the relative waveform path rather than the loaded waveform.\n\nDatasets with metadata functionality", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- LIBRISPEECH ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.LIBRISPEECH.html#torchaudio.datasets.LIBRISPEECH))\n- LibriMix ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.LibriMix.html#torchaudio.datasets.LibriMix))\n- QUESST14 ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.QUESST14.html#torchaudio.datasets.QUESST14))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- SPEECHCOMMANDS ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.SPEECHCOMMANDS.html#torchaudio.datasets.SPEECHCOMMANDS))\n- (new) FluentSpeechCommands ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.FluentSpeechCommands.html#torchaudio.datasets.FluentSpeechCommands))\n- (new) Snips ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.Snips.html#torchaudio.datasets.Snips))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- (new) IEMOCAP ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.IEMOCAP.html#torchaudio.datasets.IEMOCAP))\n- (new) VoxCeleb1 ([Identification](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.VoxCeleb1Identification.html#torchaudio.datasets.VoxCeleb1Identification), [Verification](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.VoxCeleb1Verification.html#torchaudio.datasets.VoxCeleb1Verification))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Custom Language Model support in CTC Beam Search Decoding\n\nTorchAudio released a CTC beam search decoder in release 0.12, with KenLM language model support. This release, there is added functionality for creating custom Python language models that are compatible with the decoder, using the `torchaudio.models.decoder.CTCDecoderLM` wrapper.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "For more information on using a custom language model, please refer to the [documentation](https://pytorch.org/audio/0.13.0/generated/torchaudio.models.decoder.CTCDecoder.html#ctcdecoderlm) and [tutorial](https://pytorch.org/audio/0.13.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html#custom-language-model).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) StreamWriter\n\ntorchaudio.io.StreamWriter is a class for encoding media including audio and video. This can handle a wide variety of codecs, chunk-by-chunk encoding and GPU encoding.\n\n```python\nwriter = StreamWriter(\"example.mp4\")\nwriter.add_audio_stream(\n sample_rate=16_000,\n num_channels=2,\n)\nwriter.add_video_stream(\n frame_rate=30,\n height=96,\n width=128,\n format=\"rgb24\",\n)\nwith writer.open():\n writer.write_audio_chunk(0, audio)\n writer.write_video_chunk(1, video)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "For more information, refer to [the documentation](https://pytorch.org/audio/0.13.0/generated/torchaudio.io.StreamWriter.html) and the following tutorials\n- [StreamWriter Basic Usage](https://pytorch.org/audio/0.13.0/tutorials/streamwriter_basic_tutorial.html)\n- [StreamWriter Advanced Usage](https://pytorch.org/audio/0.13.0/tutorials/streamwriter_advanced.html)\n- [Hardware-Accelerated Video Decoding and Encoding](https://pytorch.org/audio/0.13.0/hw_acceleration_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchData\n\nFor a complete list of changes and new features, please visit [our repository\u2019s 0.5.0 release note](https://github.com/pytorch/data/releases).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) DataLoader2\n\n`DataLoader2` was introduced in the last release to execute `DataPipe` graph, with support for dynamic sharding for multi-process/distributed data loading, multiple backend ReadingServices, and `DataPipe` graph in-place modification (e.g. shuffle control).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "In this release, we further consolidated the API for `DataLoader2` and a [detailed documentation is now available here](https://pytorch.org/data/0.5/dataloader2.html). We continue to welcome early adopters and feedback, as well as potential contributors. If you are interested in trying it out, we encourage you to install the nightly version of TorchData.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Data Loading from Cloud Service Providers\n\nWe extended our support to load data from additional cloud storage providers via DataPipes, now covering AWS, Google Cloud Storage, and Azure. A [tutorial is also available](https://pytorch.org/data/0.5/tutorial.html#working-with-cloud-storage-providers). We are open to feedback and feature requests.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We also performed a simple benchmark, comparing the performance of data loading from AWS S3 and attached volume on an AWS EC2 instance. The results are [visible here](https://github.com/pytorch/data/blob/gh/NivekT/100/head/benchmarks/cloud/aws_s3_results.md).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "torch::deploy (Beta)\n\ntorch::deploy is now in Beta! torch::deploy is a C++ library for Linux based operating systems that allows you to run multiple Python interpreters in a single process. You can run your existing eager PyTorch models without any changes for production inference use cases. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- Existing models work out of the box\u2013no need to modify your python code to support tracing.\n- Full support for your existing Python environment including C extensions.\n- No need to cross process boundaries to load balance in multi-GPU serving environments.\n- Model weight can be shared between multiple Python interpreters.\n- A vastly improved installation and setup process.\n\n```Python\ntorch::deploy::InterpreterManager manager(4);\n\n// access one of the 4 interpreters\nauto I = manager.acquireOne();", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "// run infer from your_model.py\nI.global(\"your_model\", \"infer\")({at::randn({10, 240, 320})});", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Learn more [here](https://github.com/pytorch/multipy).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) CUDA/ROCm/CPU Backends\n\ntorch::deploy now links against standard PyTorch Python distributions so all accelerators that PyTorch core supports such as CUDA and AMD/HIP work out of the box.\n\n- Can install any device variant of PyTorch via pip/conda like normal.\n- [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) aarch64/arm64 support\n\ntorch::deploy now has basic support for aarch64 Linux systems.\n\n- We're looking to gather feedback on it and learn more about arm use cases for eager PyTorch models.\n- Learn more / share your use case at [https://github.com/pytorch/multipy/issues/64](https://github.com/pytorch/multipy/issues/64)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchEval", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Introducing Native Metrics Support for PyTorch\n\nTorchEval is a library built for users who want highly performant implementations of common metrics to evaluate machine learning models. It also provides an easy to use interface for building custom metrics with the same toolkit. Building your metrics with TorchEval makes running distributed training loops with [torch.distributed](https://pytorch.org/docs/stable/distributed.html) a breeze.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Learn more with our [docs](https://pytorch.org/torcheval), see our [examples](https://pytorch.org/torcheval/metric_example.html), or check out our [GitHub repo](http://github.com/pytorch/torcheval).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchMultimodal Release (Beta)\n\nPlease watch for upcoming blogs in early November that will introduce TorchMultimodal, a PyTorch domain library for training SoTA multi-task multimodal models at scale, in more details; in the meantime, play around with the library and models through our [tutorial](https://pytorch.org/tutorials/beginner/flava_finetuning_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchRec", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Simplified Optimizer Fusion APIs", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We\u2019ve provided a simplified and more intuitive API for setting fused optimizer settings via apply_optimizer_in_backward. This new approach enables the ability to specify optimizer settings on a per-parameter basis and sharded modules will configure [FBGEMM\u2019s TableBatchedEmbedding modules accordingly](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L181). Additionally, this now let's TorchRec\u2019s planner account for optimizer memory usage. This should", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "alleviate reports of sharding jobs OOMing after using Adam using a plan generated from planner.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Simplified Sharding APIs", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re introducing the shard API, which now allows you to shard only the embedding modules within a model, and provides an alternative to the current main entry point - DistributedModelParallel. This lets you have a finer grained control over the rest of the model, which can be useful for customized parallelization logic, and inference use cases (which may not require any parallelization on the dense layers). We\u2019re also introducing construct_module_sharding_plan, providing a simpler interface to the TorchRec", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "sharder.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Quantized Comms", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Applying [quantization or mixed precision](https://dlp-kdd.github.io/assets/pdf/a11-yang.pdf) to tensors in a collective call during model parallel training greatly improves training efficiency, with little to no effect on model quality. TorchRec now integrates with the [quantized comms library provided by FBGEMM GPU](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/quantize_comm.py) and provides an interface to construct encoders and decoders (codecs) that surround the all_to_all, and", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "reduce_scatter collective calls in the output_dist of a sharded module. We also allow you to construct your own codecs to apply to your sharded module. The codces provided by FBGEMM allow FP16, BF16, FP8, and INT8 compressions, and you may use different quantizations for the forward pass and backward pass.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchSnapshot (Beta)\n\nAlong with PyTorch 1.13, we are releasing the beta version of TorchSnapshot, which is a performant, memory-efficient checkpointing library for PyTorch applications, designed with large, complex distributed workloads in mind. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- Performance: TorchSnapshot provides a fast checkpointing implementation employing various optimizations, including zero-copy serialization for most tensor types, overlapped device-to-host copy and storage I/O, parallelized storage I/O\n- Memory Use: TorchSnapshot's memory usage adapts to the host's available resources, greatly reducing the chance of out-of-memory issues when saving and loading checkpoints\n- Usability: Simple APIs that are consistent between distributed and non-distributed workloads", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Learn more with our [tutorial](https://pytorch.org/torchsnapshot/main/getting_started.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchVision", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We are happy to introduce torchvision v0.14 [(release note)](https://github.com/pytorch/vision/releases). This version introduces a new [model registration API](https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/) to help users retrieving and listing models and weights. It also includes new image and video classification models such as MViT, S3D, Swin Transformer V2, and MaxViT. Last but not least, we also have new primitives and augmentation such as PolynomicalLR", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "scheduler and SimpleCopyPaste.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Model Registration API", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Following up on the [multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) that was released on the previous version, we have added a new [model registration API](https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/) to help users retrieve models and weights. There are now 4 new methods under the torchvision.models module: get_model, get_model_weights, get_weight, and list_models. Here are examples of how we can use", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "them:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "```Python\nimport torchvision\nfrom torchvision.models import get_model, get_model_weights, list_models\n\n\nmax_params = 5000000\n\ntiny_models = []\nfor model_name in list_models(module=torchvision.models):\n weights_enum = get_model_weights(model_name)\n if len([w for w in weights_enum if w.meta[\"num_params\"] <= max_params]) > 0:\n tiny_models.append(model_name)\n\nprint(tiny_models)\n# ['mnasnet0_5', 'mnasnet0_75', 'mnasnet1_0', 'mobilenet_v2', ...]", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "model = get_model(tiny_models[0], weights=\"DEFAULT\")\nprint(sum(x.numel() for x in model.state_dict().values()))\n# 2239188\n```", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) New Video Classification Models\n\nWe added two new video classification models, MViT and S3D. MViT is a state of the art video classification transformer model which has 80.757% accuracy on the Kinetics400 dataset, while S3D is a relatively small model with good accuracy for its size. These models can be used as follows:\n\n```Python\nimport torch\nfrom torchvision.models.video import *", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "video = torch.rand(3, 32, 800, 600)\nmodel = mvit_v2_s(weights=\"DEFAULT\")\n# model = s3d(weights=\"DEFAULT\")\nmodel.eval()\nprediction = model(images)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Here is the table showing the accuracy of the new video classification models tested in the Kinetics400 dataset.\n\n| **Model** | **Acc@1** | **Acc@5** |\n|--------------------------------|-----------|-----------|\n| mvit_v1_b | 81.474 | 95.776 |\n| mvit_v2_s | 83.196 | 96.36 |\n| s3d | 83.582 | 96.64 |", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank Haoqi Fan, Yanghao Li, Christoph Feichtenhofer and Wan-Yen Lo for their work on [PyTorchVideo](https://github.com/facebookresearch/pytorchvideo/) and their support during the development of the MViT model. We would like to thank Sophia Zhi for her contribution implementing the S3D model in torchvision.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Stable) New Architecture and Model Variants\n\nFor Classification Models, we\u2019ve added the Swin Transformer V2 architecture along with pre-trained weights for its tiny/small/base variants. In addition, we have added support for the MaxViT transformer. Here is an example on how to use the models:\n\n```Python\nimport torch\nfrom torchvision.models import *\n\nimage = torch.rand(1, 3, 224, 224)\nmodel = swin_v2_t(weights=\"DEFAULT\").eval()\n# model = maxvit_t(weights=\"DEFAULT\").eval()\nprediction = model(image)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Here is the table showing the accuracy of the models tested on ImageNet1K dataset.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "| **Model** | **Acc@1** | **Acc@1 change over V1** | **Acc@5** | **Acc@5 change over V1** |\n|---------------|-----------|--------------------------|-----------|--------------------------|\n| swin_v2_t | 82.072 | + 0.598 | 96.132 | + 0.356 |\n| swin_v2_s | 83.712 | + 0.516 | 96.816 | + 0.456 |\n| swin_v2_b | 84.112 | + 0.530 | 96.864 | + 0.224 |", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "| maxvit_t | 83.700 | - | 96.722 | - |", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank [Ren Pang](https://github.com/ain-soph) and [Teodor Poncu](https://github.com/TeodorPoncu) for contributing the 2 models to torchvision.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Stable) New Primitives & Augmentations", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "In this release we\u2019ve added the [SimpleCopyPaste](https://arxiv.org/abs/2012.07177) augmentation in our reference scripts and we up-streamed the PolynomialLR scheduler to PyTorch Core. We would like to thank [Lezwon Castelino](https://github.com/lezwon) and [Federico Pozzi](https://github.com/federicopozzi33) for their contributions. We are continuing our efforts to modernize TorchVision by adding more SoTA primitives, Augmentations and architectures with the help of our community. If you are interested in", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "contributing, have a look at the following [issue](https://github.com/pytorch/vision/issues/6323).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Torch-TensorRT", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) TensorRT with FX2TRT frontend\n\nTorch-TensorRT is the PyTorch integration for TensorRT, providing high performance inference on NVIDIA GPUs. Torch-TRT allows for optimizing models directly in PyTorch for deployment providing up to 6x performance improvement.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Torch-TRT is an AoT compiler which ingests an nn.Module or TorchScript module, optimizes compatible subgraphs in TensorRT & leaves the rest to run in PyTorch. This gives users the performance of TensorRT, but the usability and familiarity of Torch.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Torch-TensorRT is part of the PyTorch ecosystem, and was released as v1.0 in November \u201821. There are currently two distinct front-ends: Torchscript & FX. Each provides the same value proposition and underlying operation with the primary difference being the input & output formats (TS vs FX / Python).\n\nThe Torchscript front-end was included in v1.0 and should be considered stable. The FX front-end is first released in v1.2 and should be considered a Beta.\n\nRelevant Links:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "- [Github](https://github.com/pytorch/TensorRT)\n- [Documentation](https://pytorch.org/TensorRT/)\n- [Generic (TS) getting started guide](https://pytorch.org/TensorRT/getting_started/getting_started_with_python_api.html)\n- [FX getting started guide](https://pytorch.org/TensorRT/tutorials/getting_started_with_fx_path.html)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Introducing Torch-TensorRT", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Torch-TensorRT is an integration for PyTorch that leverages inference optimizations of TensorRT on NVIDIA GPUs. It takes advantage of TensorRT optimizations, such as FP16 and INT8 reduced precision, graph optimization, operation fusion, etc. while offering a fallback to native PyTorch when TensorRT does not support the model subgraphs. Currently, there are two frontend paths existing in the library that help to convert a PyTorch model to tensorRT engine. One path is through Torch Script (TS) and the other", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "is through FX frontend. That being said, the models are traced by either TS or FX into their IR graph and then converted to TensorRT from it.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Learn more with our [tutorial](https://pytorch.org/TensorRT/).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "TorchX\n\nTorchX 0.3 updates include a new list API, experiment tracking, elastic training and improved scheduler support. There\u2019s also a new Multi-Objective NAS [tutorial](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html) using TorchX + Ax.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) List\n\nThe newly added list command and API allows you to list recently launched jobs and their statuses for a given scheduler directly from within TorchX.\n\n- This removes the need for using secondary tools to list the jobs.\n- Full programmatic access to recent jobs for integration with custom tools.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "```Python\n$ torchx list -s kubernetes\nAPP HANDLE APP STATUS\n----------------------------------------------- -----------------\nkubernetes://torchx/default:train-f2nx4459p5crr SUCCEEDED", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Learn more with our [documentation](https://pytorch.org/torchx/main/schedulers.html#torchx.schedulers.Scheduler.list).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Tracker\n\nTorchX Tracker is a new prototype library that provides a flexible and customizable experiment and artifact tracking interface. This allows you to track inputs and outputs for jobs across multiple steps to make it easier to use TorchX with pipelines and other external systems.\n\n```Python\nfrom torchx import tracker", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "app_run = tracker.app_run_from_env()\napp_run.add_metadata(lr=lr, gamma=gamma) # hyper parameters\napp_run.add_artifact(\"model\", \"storage://path/mnist_cnn.pt\") # logs / checkpoints\napp_run.add_source(parent_run_id, \"model\") # lineage", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Example:\n\n- [https://github.com/pytorch/torchx/tree/main/torchx/examples/apps/tracker](https://github.com/pytorch/torchx/tree/main/torchx/examples/apps/tracker)\n- [https://pytorch.org/torchx/main/tracker.html](https://pytorch.org/torchx/main/tracker.html)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Elastic Training and Autoscaling\n\nElasticity on Ray and Kubernetes \u2013 automatic scale up of distributed training jobs when using a supported scheduler. Learn more with our [documentation](https://pytorch.org/torchx/main/components/distributed.html).\n\n#### (Prototype) Scheduler Improvements: IBM\u00ae Spectrum LSF\n\nAdded prototype support for the IBM Spectrum LSF scheduler.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Beta) AWS Batch Scheduler\n\nThe AWS Batch scheduler integration is now in beta.\n\n- log fetching and listing jobs is now supported.\n- Added configs for job priorities and queue policies\n- Easily access job UI via ui_url\n[https://pytorch.org/torchx/main/schedulers/aws_batch.html](https://pytorch.org/torchx/main/schedulers/aws_batch.html)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) AnyPrecision Optimizer \n\nDrop in replacement for AdamW optimizer that reduces GPU memory, enables two main features:\n\n- Ability to successfully train the entire model pipeline in full BFloat16.\nKahan summation ensures precision. This can improve training throughput, especially on huge models, by reduced memory and increased computation speed.\n- Ability to change the variance state to BFloat16. This can reduce overall memory required for model training with additional speed improvements.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "Find more information [here](https://github.com/pytorch/torchdistx/pull/52).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.11, TorchData, and functorch are now available\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---\n\nWe are excited to announce the release of PyTorch 1.11 ([release notes](https://github.com/pytorch/pytorch/releases/tag/v1.11.0)). This release is composed of over 3,300 commits since 1.10, made by 434 contributors. Along with 1.11, we are releasing beta versions of TorchData and functorch.\n\nSummary:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "* **TorchData** is a new library for common modular data loading primitives for easily constructing flexible and performant data pipelines. [View it on GitHub](https://github.com/pytorch/data).\n* **functorch**, a library that adds composable function transforms to PyTorch, is now available in beta. [View it on GitHub](https://github.com/pytorch/functorch).\n* Distributed Data Parallel (DDP) static graph optimizations available in stable.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Introducing TorchData", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "We are delighted to present the Beta release of [TorchData](https://github.com/pytorch/data). This is a library of common modular data loading primitives for easily constructing flexible and performant data pipelines. Based on community feedback, we have found that the existing DataLoader bundled too many features together and can be difficult to extend. Moreover, different use cases often have to rewrite the same data loading utilities over and over again. The goal here is to enable composable data loading", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "through Iterable-style and Map-style building blocks called \u201c[DataPipes](https://github.com/pytorch/data#what-are-datapipes)\u201d that work well out of the box with the [PyTorch\u2019s DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "A `DataPipe` takes in some access function over Python data structures, `__iter__` for `IterDataPipe` and `__getitem__` for `MapDataPipe`, and returns a new access function with a slight transformation applied. You can chain multiple DataPipes together to form a data pipeline that performs all the necessary data transformation.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "We have implemented over 50 DataPipes that provide different core functionalities, such as opening files, parsing texts, transforming samples, caching, shuffling, and batching. For users who are interested in connecting to cloud providers (such as Google Drive or AWS S3), the [fsspec](https://pytorch.org/data/0.3.0/torchdata.datapipes.iter.html#io-datapipes) and iopath DataPipes will allow you to do so. The documentation provides detailed explanations and usage examples of each", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "[IterDataPipe](https://pytorch.org/data/0.3.0/torchdata.datapipes.iter.html) and [MapDataPipe](https://pytorch.org/data/0.3.0/torchdata.datapipes.map.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "In this release, some of the PyTorch domain libraries have migrated their datasets to use DataPipes. In TorchText, the [popular datasets provided by the library](https://github.com/pytorch/text/tree/release/0.12/torchtext/datasets) are implemented using DataPipes and a [section of its SST-2 binary text classification tutorial](https://pytorch.org/text/0.12.0/tutorials/sst2_classification_non_distributed.html#dataset) demonstrates how you can use DataPipes to preprocess data for your model. There also are", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "other prototype implementations of datasets with DataPipes in [TorchVision (available in nightly releases)](https://github.com/pytorch/vision/tree/main/torchvision/prototype/datasets/_builtin) and in [TorchRec](https://pytorch.org/torchrec/torchrec.datasets.html). You can find more [specific examples here](https://pytorch.org/data/0.3.0/examples.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "The [documentation for TorchData](https://pytorch.org/data) is now live. It contains a tutorial that covers [how to use DataPipes](https://pytorch.org/data/0.3.0/tutorial.html#using-datapipes), [use them with DataLoader](https://pytorch.org/data/0.3.0/tutorial.html#working-with-dataloader), and [implement custom ones](https://pytorch.org/data/0.3.0/tutorial.html#implementing-a-custom-datapipe). FAQs and future plans related to DataLoader are described in [our project\u2019s README", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "file](https://github.com/pytorch/data#readme).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Introducing functorch\n\nWe\u2019re excited to announce the first beta release of [functorch](https://github.com/pytorch/functorch). Heavily inspired by [Google JAX](https://github.com/google/jax), functorch is a library that adds composable function transforms to PyTorch. It aims to provide composable vmap (vectorization) and autodiff transforms that work with PyTorch modules and PyTorch autograd with good eager-mode performance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Composable function transforms can help with a number of use cases that are tricky to do in PyTorch today:\n\n* computing per-sample-gradients (or other per-sample quantities)\n* running ensembles of models on a single machine\n* efficiently batching together tasks in the inner-loop of MAML\n* efficiently computing Jacobians and Hessians as well as batched ones", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Composing vmap (vectorization), vjp (reverse-mode AD), and jvp (forward-mode AD) transforms allows us to effortlessly express the above without designing a separate library for each.\n\nFor more details, please see our [documentation](https://pytorch.org/functorch/), [tutorials](https://pytorch.org/functorch), and [installation instructions](https://pytorch.org/functorch/stable/install.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) DDP static graph", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "DDP static graph assumes that your model employs the same set of used/unused parameters in every iteration, so that it can deterministically know states like which hooks will fire, how many times the hooks will fire and gradients computation ready order after the first iteration. Static graph caches these states in the first iteration, and thus it could support features that DDP can not support in previous releases, e.g., support multiple activation checkpoints on the same parameters regardless of whether", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "there are unused parameters or not. The static graph feature also applies performance optimizations when there are unused parameters, e.g., it avoids traversing graphs to search unused parameters every iteration, and enables dynamic bucketing order. These optimizations in the DDP static graph brought 10% QPS gain for some recommendation models.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "To enable static graph, just simply set static_graph=True in the DDP API like this:\n\n```\nddp_model = DistributedDataParallel(model, static_graph=True)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "For more details, please see our [documentation](https://pytorch.org/docs/master/generated/torch.nn.parallel.DistributedDataParallel.html) and [tutorials](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "To circumvent this limitation, TorchVision offered [_custom implementations_](https://github.com/pytorch/vision/blob/main/references/detection/transforms.py) in its reference scripts that show-cased how one could perform augmentations in each task. Though this practice enabled us to train high accuracy [_classification_](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/), [_object detection & segmentation_](https://pytorch.org/blog/pytorch-1.12-new-library-releases/#beta-object-detection-and-instance-segmentation) models, it was a hacky approach which made those transforms impossible to import from the TorchVision binary.\n\n## The new Transforms API\n\nThe Transforms V2 API supports videos, bounding boxes, and segmentation masks meaning that it offers native support for many Computer Vision tasks. The new solution is a drop-in replacement:\n\n```python\nimport torchvision.transforms.v2 as transforms", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "# Exactly the same interface as V1:\ntrans = transforms.Compose([\n transforms.ColorJitter(contrast=0.5),\n transforms.RandomRotation(30),\n transforms.CenterCrop(480),\n])\nimgs, bboxes, labels = trans(imgs, bboxes, labels)\n```\n\nThe new Transform Classes can receive any arbitrary number of inputs without enforcing specific order or structure:\n\n```python\n# Already supported:\ntrans(imgs) # Image Classification\ntrans(videos) # Video Tasks\ntrans(imgs, bboxes, labels) # Object Detection\ntrans(imgs, bboxes, masks, labels) # Instance Segmentation\ntrans(imgs, masks) # Semantic Segmentation\ntrans({\"image\": imgs, \"box\": bboxes, \"tag\": labels}) # Arbitrary Structure\n\n# Future support:\ntrans(imgs, bboxes, labels, keypoints) # Keypoint Detection\ntrans(stereo_images, disparities, masks) # Depth Perception\ntrans(image1, image2, optical_flows, masks) # Optical Flow\ntrans(imgs_or_videos, labels) # MixUp/CutMix-style Transforms\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "The Transform Classes make sure that they apply the same random transforms to all the inputs to ensure consistent results.\n\nThe functional API has been updated to support all necessary signal processing kernels (resizing, cropping, affine transforms, padding etc) for all inputs:\n\n```python\nfrom torchvision.transforms.v2 import functional as F\n\n\n# High-level dispatcher, accepts any supported input type, fully BC\nF.resize(inpt, size=[224, 224])\n# Image tensor kernel\nF.resize_image_tensor(img_tensor, size=[224, 224], antialias=True) \n# PIL image kernel\nF.resize_image_pil(img_pil, size=[224, 224], interpolation=BILINEAR)\n# Video kernel\nF.resize_video(video, size=[224, 224], antialias=True) \n# Mask kernel\nF.resize_mask(mask, size=[224, 224])\n# Bounding box kernel\nF.resize_bounding_box(bbox, size=[224, 224], spatial_size=[256, 256])\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "Under the hood, the API uses Tensor subclassing to wrap the input, attach useful meta-data and dispatch to the right kernel. For your data to be compatible with these new transforms, you can either use the provided dataset wrapper which should work with most of torchvision built-in datasets, or your can wrap your data manually into Datapoints:\n\n```python\nfrom torchvision.datasets import wrap_dataset_for_transforms_v2\nds = CocoDetection(..., transforms=v2_transforms)\nds = wrap_dataset_for_transforms_v2(ds) # data is now compatible with transforms v2!\n\n# Or wrap your data manually using the lower-level Datapoint classes:\nfrom torchvision import datapoints\n\nimgs = datapoints.Image(images)\nvids = datapoints.Video(videos)\nmasks = datapoints.Mask(target[\"masks\u201c])\nbboxes = datapoints.BoundingBox(target[\"boxes\u201c], format=\u201dXYXY\u201d, spatial_size=imgs.shape)\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "In addition to the new API, we now provide importable implementations for several data augmentations that are used in SoTA research such as [_Large Scale Jitter_](https://github.com/pytorch/vision/blob/928b05cad36eadb13e169f03028767c8bcd1f21d/torchvision/transforms/v2/_geometry.py#L1109), [_AutoAugmentation_](https://github.com/pytorch/vision/blob/main/torchvision/transforms/v2/_auto_augment.py) methods and [_several_](https://github.com/pytorch/vision/blob/main/torchvision/transforms/v2/__init__.py) new Geometric, Color and Type Conversion transforms.\n\nThe API continues to support both PIL and Tensor backends for Images, single or batched input and maintains JIT-scriptability on both the functional and class APIs.. The new API has been [_verified_](https://github.com/pytorch/vision/pull/6433#issuecomment-1256741233) to achieve the same accuracy as the previous implementation.\n\n## An end-to-end example", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "Here is an example of the new API using the following [_image_](https://user-images.githubusercontent.com/5347466/195350223-8683ef25-1367-4292-9174-c15f85c7358e.jpg). It works both with PIL images and Tensors. For more examples and tutorials, [_take a look at our gallery!_](https://pytorch.org/vision/0.15/auto_examples/index.html)\n\n\n```python\nfrom torchvision import io, utils\nfrom torchvision import datapoints\nfrom torchvision.transforms import v2 as T\nfrom torchvision.transforms.v2 import functional as F", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "# Defining and wrapping input to appropriate Tensor Subclasses\npath = \"COCO_val2014_000000418825.jpg\"\nimg = datapoints.Image(io.read_image(path))\n# img = PIL.Image.open(path)\nbboxes = datapoints.BoundingBox(\n [[2, 0, 206, 253], [396, 92, 479, 241], [328, 253, 417, 332],\n [148, 68, 256, 182], [93, 158, 170, 260], [432, 0, 438, 26],\n [422, 0, 480, 25], [419, 39, 424, 52], [448, 37, 456, 62],\n [435, 43, 437, 50], [461, 36, 469, 63], [461, 75, 469, 94],\n [469, 36, 480, 64], [440, 37, 446, 56], [398, 233, 480, 304],\n [452, 39, 463, 63], [424, 38, 429, 50]],\n format=datapoints.BoundingBoxFormat.XYXY,\n spatial_size=F.get_spatial_size(img),\n)\nlabels = [59, 58, 50, 64, 76, 74, 74, 74, 74, 74, 74, 74, 74, 74, 50, 74, 74]\n# Defining and applying Transforms V2\ntrans = T.Compose(\n [\n T.ColorJitter(contrast=0.5),\n T.RandomRotation(30),\n T.CenterCrop(480),\n ]\n)\nimg, bboxes, labels = trans(img, bboxes, labels)\n# Visualizing results\nviz = utils.draw_bounding_boxes(F.to_image_tensor(img), boxes=bboxes)\nF.to_pil_image(viz).show()\n```", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "## Development milestones and future work\n\nHere is where we are in development:\n\n- [x] Design API\n- [x] Write Kernels for transforming Videos, Bounding Boxes, Masks and Labels\n- [x] Rewrite all existing Transform Classes (stable + references) on the new API:\n - [x] Image Classification\n - [x] Video Classification\n - [x] Object Detection\n - [x] Instance Segmentation\n - [x] Semantic Segmentation\n- [x] Verify the accuracy of the new API for all supported Tasks and Backends\n- [x] Speed Benchmarks and Performance Optimizations (in progress - planned for Dec)\n- [x] Graduate from Prototype (planned for Q1)\n- [ ] Add support of Depth Perception, Keypoint Detection, Optical Flow and more (future)\n- [ ] Add smooth support for batch-wise transforms like MixUp and CutMix\n\n\nWe would love to get [_feedback_](https://github.com/pytorch/vision/issues/6753) from you to improve its functionality. Please reach out to us if you have any questions or suggestions.", "metadata": {"source": "https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"New Library Updates in PyTorch 1.13\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/new-library-updates-in-pytorch-1.13-2.jpg\"\n---\n\n## Summary\n\nWe are bringing a number of improvements to the current PyTorch libraries, alongside the PyTorch 1.13 [release](https://github.com/pytorch/pytorch/releases). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch.\n\nAlong with **1.13**, we are releasing updates to the PyTorch Libraries, please find them below.\n\n### TorchAudio \n\n#### (Beta) Hybrid Demucs Model and Pipeline\n\nHybrid Demucs is a music source separation model that uses both spectrogram and time domain features. It has demonstrated state-of-the-art performance in the Sony\u00ae Music DeMixing Challenge. (citation: [https://arxiv.org/abs/2111.03600](https://arxiv.org/abs/2111.03600))\n\nThe TorchAudio v0.13 release includes the following features", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "- MUSDB_HQ Dataset, which is used in Hybrid Demucs training ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.MUSDB_HQ.html#torchaudio.datasets.MUSDB_HQ))\n- Hybrid Demucs model architecture ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.models.HDemucs.html#torchaudio.models.HDemucs))\n- Three factory functions suitable for different sample rate ranges\n- Pre-trained pipelines ([docs](https://pytorch.org/audio/0.13.0/pipelines.html#id46))\n- SDR Results of pre-trained pipelines on MUSDB_HQ test set\n- Tutorial that steps through music source separation using the pretrained pipeline ([docs](https://pytorch.org/audio/0.13.0/tutorials/hybrid_demucs_tutorial.html))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "| Pipeline | All | Drums | Bass | Other | Vocals |\n|----------------------------------------|-------|-------|--------|-------|--------|\n| HDEMUCS_HIGH_MUSDB* | 6.42 | 7.76 | 6.51 | 4.47 | 6.93 |\n| HDEMUCS_HIGH_MUSDB_PLUS** | 9.37 | 11.38 | 10.53 | 7.24 | 8.32 |\n\n

* Trained on the training data of MUSDB-HQ dataset.
** Trained on both training and test sets of MUSDB-HQ and 150 extra songs from an internal database that were specifically produced for Meta.

\n\n```python\nfrom torchaudio.pipelines import HDEMUCS_HIGH_MUSDB_PLUS\n\nbundle = HDEMUCS_HIGH_MUSDB_PLUS\nmodel = bundle.get_model()\nsources_list = model.sources\n\nmixture, samplerate = torchaudio.load(\"song.wav\")\nsources = model(mixture)\naudios = dict(zip(sources_list, sources)\n```\n\nSpecial thanks to Alexandre Defossez for the guidance.\n\n#### (Beta) Datasets and Metadata Mode for SUPERB Benchmark", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "TorchAudio adds support for various audio-related datasets used in downstream tasks for benchmarking self-supervised learning models. With the addition of several new datasets, there is now support for the downstream tasks in version 1 of the [SUPERB benchmark](https://superbbenchmark.org/), which can be found in the [s3prl repository](https://github.com/s3prl/s3prl/blob/master/s3prl/downstream/docs/superb.md).\n\nFor these datasets, we also add metadata support through a `get_metadata` function, enabling faster dataset iteration or preprocessing without the need to load waveforms. The function returns the same features as `__getitem__`, except it returns the relative waveform path rather than the loaded waveform.\n\nDatasets with metadata functionality", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "- LIBRISPEECH ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.LIBRISPEECH.html#torchaudio.datasets.LIBRISPEECH))\n- LibriMix ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.LibriMix.html#torchaudio.datasets.LibriMix))\n- QUESST14 ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.QUESST14.html#torchaudio.datasets.QUESST14))\n- SPEECHCOMMANDS ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.SPEECHCOMMANDS.html#torchaudio.datasets.SPEECHCOMMANDS))\n- (new) FluentSpeechCommands ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.FluentSpeechCommands.html#torchaudio.datasets.FluentSpeechCommands))\n- (new) Snips ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.Snips.html#torchaudio.datasets.Snips))\n- (new) IEMOCAP ([docs](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.IEMOCAP.html#torchaudio.datasets.IEMOCAP))\n- (new) VoxCeleb1 ([Identification](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.VoxCeleb1Identification.html#torchaudio.datasets.VoxCeleb1Identification), [Verification](https://pytorch.org/audio/0.13.0/generated/torchaudio.datasets.VoxCeleb1Verification.html#torchaudio.datasets.VoxCeleb1Verification))", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "#### (Beta) Custom Language Model support in CTC Beam Search Decoding\n\nTorchAudio released a CTC beam search decoder in release 0.12, with KenLM language model support. This release, there is added functionality for creating custom Python language models that are compatible with the decoder, using the `torchaudio.models.decoder.CTCDecoderLM` wrapper.\n\nFor more information on using a custom language model, please refer to the [documentation](https://pytorch.org/audio/0.13.0/generated/torchaudio.models.decoder.CTCDecoder.html#ctcdecoderlm) and [tutorial](https://pytorch.org/audio/0.13.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html#custom-language-model).\n\n#### (Beta) StreamWriter\n\ntorchaudio.io.StreamWriter is a class for encoding media including audio and video. This can handle a wide variety of codecs, chunk-by-chunk encoding and GPU encoding.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "```python\nwriter = StreamWriter(\"example.mp4\")\nwriter.add_audio_stream(\n sample_rate=16_000,\n num_channels=2,\n)\nwriter.add_video_stream(\n frame_rate=30,\n height=96,\n width=128,\n format=\"rgb24\",\n)\nwith writer.open():\n writer.write_audio_chunk(0, audio)\n writer.write_video_chunk(1, video)\n```\n\nFor more information, refer to [the documentation](https://pytorch.org/audio/0.13.0/generated/torchaudio.io.StreamWriter.html) and the following tutorials\n- [StreamWriter Basic Usage](https://pytorch.org/audio/0.13.0/tutorials/streamwriter_basic_tutorial.html)\n- [StreamWriter Advanced Usage](https://pytorch.org/audio/0.13.0/tutorials/streamwriter_advanced.html)\n- [Hardware-Accelerated Video Decoding and Encoding](https://pytorch.org/audio/0.13.0/hw_acceleration_tutorial.html)\n\n### TorchData\n\nFor a complete list of changes and new features, please visit [our repository\u2019s 0.5.0 release note](https://github.com/pytorch/data/releases).\n\n#### (Prototype) DataLoader2", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "`DataLoader2` was introduced in the last release to execute `DataPipe` graph, with support for dynamic sharding for multi-process/distributed data loading, multiple backend ReadingServices, and `DataPipe` graph in-place modification (e.g. shuffle control).\n\nIn this release, we further consolidated the API for `DataLoader2` and a [detailed documentation is now available here](https://pytorch.org/data/0.5/dataloader2.html). We continue to welcome early adopters and feedback, as well as potential contributors. If you are interested in trying it out, we encourage you to install the nightly version of TorchData.\n\n#### (Beta) Data Loading from Cloud Service Providers\n\nWe extended our support to load data from additional cloud storage providers via DataPipes, now covering AWS, Google Cloud Storage, and Azure. A [tutorial is also available](https://pytorch.org/data/0.5/tutorial.html#working-with-cloud-storage-providers). We are open to feedback and feature requests.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "We also performed a simple benchmark, comparing the performance of data loading from AWS S3 and attached volume on an AWS EC2 instance. The results are [visible here](https://github.com/pytorch/data/blob/gh/NivekT/100/head/benchmarks/cloud/aws_s3_results.md).\n\n### torch::deploy (Beta)\n\ntorch::deploy is now in Beta! torch::deploy is a C++ library for Linux based operating systems that allows you to run multiple Python interpreters in a single process. You can run your existing eager PyTorch models without any changes for production inference use cases. Highlights include: \n\n- Existing models work out of the box\u2013no need to modify your python code to support tracing.\n- Full support for your existing Python environment including C extensions.\n- No need to cross process boundaries to load balance in multi-GPU serving environments.\n- Model weight can be shared between multiple Python interpreters.\n- A vastly improved installation and setup process.\n\n```Python\ntorch::deploy::InterpreterManager manager(4);", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "// access one of the 4 interpreters\nauto I = manager.acquireOne();\n\n// run infer from your_model.py\nI.global(\"your_model\", \"infer\")({at::randn({10, 240, 320})});\n```\n\nLearn more [here](https://github.com/pytorch/multipy).\n\n#### (Beta) CUDA/ROCm/CPU Backends\n\ntorch::deploy now links against standard PyTorch Python distributions so all accelerators that PyTorch core supports such as CUDA and AMD/HIP work out of the box.\n\n- Can install any device variant of PyTorch via pip/conda like normal.\n- [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/)\n\n#### (Prototype) aarch64/arm64 support\n\ntorch::deploy now has basic support for aarch64 Linux systems.\n\n- We're looking to gather feedback on it and learn more about arm use cases for eager PyTorch models.\n- Learn more / share your use case at [https://github.com/pytorch/multipy/issues/64](https://github.com/pytorch/multipy/issues/64)\n\n### TorchEval\n\n#### (Prototype) Introducing Native Metrics Support for PyTorch", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "TorchEval is a library built for users who want highly performant implementations of common metrics to evaluate machine learning models. It also provides an easy to use interface for building custom metrics with the same toolkit. Building your metrics with TorchEval makes running distributed training loops with [torch.distributed](https://pytorch.org/docs/stable/distributed.html) a breeze.\n\nLearn more with our [docs](https://pytorch.org/torcheval), see our [examples](https://pytorch.org/torcheval/metric_example.html), or check out our [GitHub repo](http://github.com/pytorch/torcheval).\n\n### TorchMultimodal Release (Beta)\n\nPlease watch for upcoming blogs in early November that will introduce TorchMultimodal, a PyTorch domain library for training SoTA multi-task multimodal models at scale, in more details; in the meantime, play around with the library and models through our [tutorial](https://pytorch.org/tutorials/beginner/flava_finetuning_tutorial.html).\n\n### TorchRec", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "### TorchRec\n\n#### (Prototype) Simplified Optimizer Fusion APIs\n\nWe\u2019ve provided a simplified and more intuitive API for setting fused optimizer settings via apply_optimizer_in_backward. This new approach enables the ability to specify optimizer settings on a per-parameter basis and sharded modules will configure [FBGEMM\u2019s TableBatchedEmbedding modules accordingly](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py#L181). Additionally, this now let's TorchRec\u2019s planner account for optimizer memory usage. This should alleviate reports of sharding jobs OOMing after using Adam using a plan generated from planner.\n\n#### (Prototype) Simplified Sharding APIs", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "We\u2019re introducing the shard API, which now allows you to shard only the embedding modules within a model, and provides an alternative to the current main entry point - DistributedModelParallel. This lets you have a finer grained control over the rest of the model, which can be useful for customized parallelization logic, and inference use cases (which may not require any parallelization on the dense layers). We\u2019re also introducing construct_module_sharding_plan, providing a simpler interface to the TorchRec sharder.\n\n#### (Beta) Quantized Comms", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Applying [quantization or mixed precision](https://dlp-kdd.github.io/assets/pdf/a11-yang.pdf) to tensors in a collective call during model parallel training greatly improves training efficiency, with little to no effect on model quality. TorchRec now integrates with the [quantized comms library provided by FBGEMM GPU](https://github.com/pytorch/FBGEMM/blob/main/fbgemm_gpu/fbgemm_gpu/quantize_comm.py) and provides an interface to construct encoders and decoders (codecs) that surround the all_to_all, and reduce_scatter collective calls in the output_dist of a sharded module. We also allow you to construct your own codecs to apply to your sharded module. The codces provided by FBGEMM allow FP16, BF16, FP8, and INT8 compressions, and you may use different quantizations for the forward pass and backward pass.\n\n### TorchSnapshot (Beta)", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Along with PyTorch 1.13, we are releasing the beta version of TorchSnapshot, which is a performant, memory-efficient checkpointing library for PyTorch applications, designed with large, complex distributed workloads in mind. Highlights include:\n\n- Performance: TorchSnapshot provides a fast checkpointing implementation employing various optimizations, including zero-copy serialization for most tensor types, overlapped device-to-host copy and storage I/O, parallelized storage I/O\n- Memory Use: TorchSnapshot's memory usage adapts to the host's available resources, greatly reducing the chance of out-of-memory issues when saving and loading checkpoints\n- Usability: Simple APIs that are consistent between distributed and non-distributed workloads\n\nLearn more with our [tutorial](https://pytorch.org/torchsnapshot/main/getting_started.html).\n\n### TorchVision", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "### TorchVision \n\nWe are happy to introduce torchvision v0.14 [(release note)](https://github.com/pytorch/vision/releases). This version introduces a new [model registration API](https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/) to help users retrieving and listing models and weights. It also includes new image and video classification models such as MViT, S3D, Swin Transformer V2, and MaxViT. Last but not least, we also have new primitives and augmentation such as PolynomicalLR scheduler and SimpleCopyPaste.\n\n#### (Beta) Model Registration API", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Following up on the [multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/) that was released on the previous version, we have added a new [model registration API](https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/) to help users retrieve models and weights. There are now 4 new methods under the torchvision.models module: get_model, get_model_weights, get_weight, and list_models. Here are examples of how we can use them:\n\n```Python\nimport torchvision\nfrom torchvision.models import get_model, get_model_weights, list_models\n\n\nmax_params = 5000000\n\ntiny_models = []\nfor model_name in list_models(module=torchvision.models):\n weights_enum = get_model_weights(model_name)\n if len([w for w in weights_enum if w.meta[\"num_params\"] <= max_params]) > 0:\n tiny_models.append(model_name)\n\nprint(tiny_models)\n# ['mnasnet0_5', 'mnasnet0_75', 'mnasnet1_0', 'mobilenet_v2', ...]", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "model = get_model(tiny_models[0], weights=\"DEFAULT\")\nprint(sum(x.numel() for x in model.state_dict().values()))\n# 2239188\n```\n\n#### (Beta) New Video Classification Models\n\nWe added two new video classification models, MViT and S3D. MViT is a state of the art video classification transformer model which has 80.757% accuracy on the Kinetics400 dataset, while S3D is a relatively small model with good accuracy for its size. These models can be used as follows:\n\n```Python\nimport torch\nfrom torchvision.models.video import *\n\nvideo = torch.rand(3, 32, 800, 600)\nmodel = mvit_v2_s(weights=\"DEFAULT\")\n# model = s3d(weights=\"DEFAULT\")\nmodel.eval()\nprediction = model(images)\n```\n\nHere is the table showing the accuracy of the new video classification models tested in the Kinetics400 dataset.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "| **Model** | **Acc@1** | **Acc@5** |\n|--------------------------------|-----------|-----------|\n| mvit_v1_b | 81.474 | 95.776 |\n| mvit_v2_s | 83.196 | 96.36 |\n| s3d | 83.582 | 96.64 |\n\nWe would like to thank Haoqi Fan, Yanghao Li, Christoph Feichtenhofer and Wan-Yen Lo for their work on [PyTorchVideo](https://github.com/facebookresearch/pytorchvideo/) and their support during the development of the MViT model. We would like to thank Sophia Zhi for her contribution implementing the S3D model in torchvision.\n\n#### (Stable) New Architecture and Model Variants\n\nFor Classification Models, we\u2019ve added the Swin Transformer V2 architecture along with pre-trained weights for its tiny/small/base variants. In addition, we have added support for the MaxViT transformer. Here is an example on how to use the models:\n\n```Python\nimport torch\nfrom torchvision.models import *", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "image = torch.rand(1, 3, 224, 224)\nmodel = swin_v2_t(weights=\"DEFAULT\").eval()\n# model = maxvit_t(weights=\"DEFAULT\").eval()\nprediction = model(image)\n```\n\nHere is the table showing the accuracy of the models tested on ImageNet1K dataset.\n\n| **Model** | **Acc@1** | **Acc@1 change over V1** | **Acc@5** | **Acc@5 change over V1** |\n|---------------|-----------|--------------------------|-----------|--------------------------|\n| swin_v2_t | 82.072 | + 0.598 | 96.132 | + 0.356 |\n| swin_v2_s | 83.712 | + 0.516 | 96.816 | + 0.456 |\n| swin_v2_b | 84.112 | + 0.530 | 96.864 | + 0.224 |\n| maxvit_t | 83.700 | - | 96.722 | - |\n\nWe would like to thank [Ren Pang](https://github.com/ain-soph) and [Teodor Poncu](https://github.com/TeodorPoncu) for contributing the 2 models to torchvision.\n\n### (Stable) New Primitives & Augmentations", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "In this release we\u2019ve added the [SimpleCopyPaste](https://arxiv.org/abs/2012.07177) augmentation in our reference scripts and we up-streamed the PolynomialLR scheduler to PyTorch Core. We would like to thank [Lezwon Castelino](https://github.com/lezwon) and [Federico Pozzi](https://github.com/federicopozzi33) for their contributions. We are continuing our efforts to modernize TorchVision by adding more SoTA primitives, Augmentations and architectures with the help of our community. If you are interested in contributing, have a look at the following [issue](https://github.com/pytorch/vision/issues/6323).\n\n### Torch-TensorRT\n\n#### (Prototype) TensorRT with FX2TRT frontend\n\nTorch-TensorRT is the PyTorch integration for TensorRT, providing high performance inference on NVIDIA GPUs. Torch-TRT allows for optimizing models directly in PyTorch for deployment providing up to 6x performance improvement.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Torch-TRT is an AoT compiler which ingests an nn.Module or TorchScript module, optimizes compatible subgraphs in TensorRT & leaves the rest to run in PyTorch. This gives users the performance of TensorRT, but the usability and familiarity of Torch.\n\nTorch-TensorRT is part of the PyTorch ecosystem, and was released as v1.0 in November \u201821. There are currently two distinct front-ends: Torchscript & FX. Each provides the same value proposition and underlying operation with the primary difference being the input & output formats (TS vs FX / Python).\n\nThe Torchscript front-end was included in v1.0 and should be considered stable. The FX front-end is first released in v1.2 and should be considered a Beta.\n\nRelevant Links:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Relevant Links:\n\n- [Github](https://github.com/pytorch/TensorRT)\n- [Documentation](https://pytorch.org/TensorRT/)\n- [Generic (TS) getting started guide](https://pytorch.org/TensorRT/getting_started/getting_started_with_python_api.html)\n- [FX getting started guide](https://pytorch.org/TensorRT/tutorials/getting_started_with_fx_path.html)\n\n#### (Stable) Introducing Torch-TensorRT", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "Torch-TensorRT is an integration for PyTorch that leverages inference optimizations of TensorRT on NVIDIA GPUs. It takes advantage of TensorRT optimizations, such as FP16 and INT8 reduced precision, graph optimization, operation fusion, etc. while offering a fallback to native PyTorch when TensorRT does not support the model subgraphs. Currently, there are two frontend paths existing in the library that help to convert a PyTorch model to tensorRT engine. One path is through Torch Script (TS) and the other is through FX frontend. That being said, the models are traced by either TS or FX into their IR graph and then converted to TensorRT from it.\n\nLearn more with our [tutorial](https://pytorch.org/TensorRT/).\n\n### TorchX\n\nTorchX 0.3 updates include a new list API, experiment tracking, elastic training and improved scheduler support. There\u2019s also a new Multi-Objective NAS [tutorial](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html) using TorchX + Ax.\n\n#### (Prototype) List", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "The newly added list command and API allows you to list recently launched jobs and their statuses for a given scheduler directly from within TorchX.\n\n- This removes the need for using secondary tools to list the jobs.\n- Full programmatic access to recent jobs for integration with custom tools.\n\n```Python\n$ torchx list -s kubernetes\nAPP HANDLE APP STATUS\n----------------------------------------------- -----------------\nkubernetes://torchx/default:train-f2nx4459p5crr SUCCEEDED\n```\n\nLearn more with our [documentation](https://pytorch.org/torchx/main/schedulers.html#torchx.schedulers.Scheduler.list).\n\n#### (Prototype) Tracker\n\nTorchX Tracker is a new prototype library that provides a flexible and customizable experiment and artifact tracking interface. This allows you to track inputs and outputs for jobs across multiple steps to make it easier to use TorchX with pipelines and other external systems.\n\n```Python\nfrom torchx import tracker", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "app_run = tracker.app_run_from_env()\napp_run.add_metadata(lr=lr, gamma=gamma) # hyper parameters\napp_run.add_artifact(\"model\", \"storage://path/mnist_cnn.pt\") # logs / checkpoints\napp_run.add_source(parent_run_id, \"model\") # lineage\n```\n\nExample:\n\n- [https://github.com/pytorch/torchx/tree/main/torchx/examples/apps/tracker](https://github.com/pytorch/torchx/tree/main/torchx/examples/apps/tracker)\n- [https://pytorch.org/torchx/main/tracker.html](https://pytorch.org/torchx/main/tracker.html)\n\n#### (Prototype) Elastic Training and Autoscaling\n\nElasticity on Ray and Kubernetes \u2013 automatic scale up of distributed training jobs when using a supported scheduler. Learn more with our [documentation](https://pytorch.org/torchx/main/components/distributed.html).\n\n#### (Prototype) Scheduler Improvements: IBM\u00ae Spectrum LSF\n\nAdded prototype support for the IBM Spectrum LSF scheduler.\n\n#### (Beta) AWS Batch Scheduler\n\nThe AWS Batch scheduler integration is now in beta.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "- log fetching and listing jobs is now supported.\n- Added configs for job priorities and queue policies\n- Easily access job UI via ui_url\n[https://pytorch.org/torchx/main/schedulers/aws_batch.html](https://pytorch.org/torchx/main/schedulers/aws_batch.html)\n\n#### (Prototype) AnyPrecision Optimizer \n\nDrop in replacement for AdamW optimizer that reduces GPU memory, enables two main features:\n\n- Ability to successfully train the entire model pipeline in full BFloat16.\nKahan summation ensures precision. This can improve training throughput, especially on huge models, by reduced memory and increased computation speed.\n- Ability to change the variance state to BFloat16. This can reduce overall memory required for model training with additional speed improvements.\n\nFind more information [here](https://github.com/pytorch/torchdistx/pull/52).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.11, TorchData, and functorch are now available\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/pytorch-logo.jpg\"\n---\n\nWe are excited to announce the release of PyTorch 1.11 ([release notes](https://github.com/pytorch/pytorch/releases/tag/v1.11.0)). This release is composed of over 3,300 commits since 1.10, made by 434 contributors. Along with 1.11, we are releasing beta versions of TorchData and functorch.\n\nSummary:\n\n* **TorchData** is a new library for common modular data loading primitives for easily constructing flexible and performant data pipelines. [View it on GitHub](https://github.com/pytorch/data).\n* **functorch**, a library that adds composable function transforms to PyTorch, is now available in beta. [View it on GitHub](https://github.com/pytorch/functorch).\n* Distributed Data Parallel (DDP) static graph optimizations available in stable.\n\n## Introducing TorchData", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "We are delighted to present the Beta release of [TorchData](https://github.com/pytorch/data). This is a library of common modular data loading primitives for easily constructing flexible and performant data pipelines. Based on community feedback, we have found that the existing DataLoader bundled too many features together and can be difficult to extend. Moreover, different use cases often have to rewrite the same data loading utilities over and over again. The goal here is to enable composable data loading through Iterable-style and Map-style building blocks called \u201c[DataPipes](https://github.com/pytorch/data#what-are-datapipes)\u201d that work well out of the box with the [PyTorch\u2019s DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "A `DataPipe` takes in some access function over Python data structures, `__iter__` for `IterDataPipe` and `__getitem__` for `MapDataPipe`, and returns a new access function with a slight transformation applied. You can chain multiple DataPipes together to form a data pipeline that performs all the necessary data transformation.\n\nWe have implemented over 50 DataPipes that provide different core functionalities, such as opening files, parsing texts, transforming samples, caching, shuffling, and batching. For users who are interested in connecting to cloud providers (such as Google Drive or AWS S3), the [fsspec](https://pytorch.org/data/0.3.0/torchdata.datapipes.iter.html#io-datapipes) and iopath DataPipes will allow you to do so. The documentation provides detailed explanations and usage examples of each [IterDataPipe](https://pytorch.org/data/0.3.0/torchdata.datapipes.iter.html) and [MapDataPipe](https://pytorch.org/data/0.3.0/torchdata.datapipes.map.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "In this release, some of the PyTorch domain libraries have migrated their datasets to use DataPipes. In TorchText, the [popular datasets provided by the library](https://github.com/pytorch/text/tree/release/0.12/torchtext/datasets) are implemented using DataPipes and a [section of its SST-2 binary text classification tutorial](https://pytorch.org/text/0.12.0/tutorials/sst2_classification_non_distributed.html#dataset) demonstrates how you can use DataPipes to preprocess data for your model. There also are other prototype implementations of datasets with DataPipes in [TorchVision (available in nightly releases)](https://github.com/pytorch/vision/tree/main/torchvision/prototype/datasets/_builtin) and in [TorchRec](https://pytorch.org/torchrec/torchrec.datasets.html). You can find more [specific examples here](https://pytorch.org/data/0.3.0/examples.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "The [documentation for TorchData](https://pytorch.org/data) is now live. It contains a tutorial that covers [how to use DataPipes](https://pytorch.org/data/0.3.0/tutorial.html#using-datapipes), [use them with DataLoader](https://pytorch.org/data/0.3.0/tutorial.html#working-with-dataloader), and [implement custom ones](https://pytorch.org/data/0.3.0/tutorial.html#implementing-a-custom-datapipe). FAQs and future plans related to DataLoader are described in [our project\u2019s README file](https://github.com/pytorch/data#readme).\n\n## Introducing functorch\n\nWe\u2019re excited to announce the first beta release of [functorch](https://github.com/pytorch/functorch). Heavily inspired by [Google JAX](https://github.com/google/jax), functorch is a library that adds composable function transforms to PyTorch. It aims to provide composable vmap (vectorization) and autodiff transforms that work with PyTorch modules and PyTorch autograd with good eager-mode performance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "Composable function transforms can help with a number of use cases that are tricky to do in PyTorch today:\n\n* computing per-sample-gradients (or other per-sample quantities)\n* running ensembles of models on a single machine\n* efficiently batching together tasks in the inner-loop of MAML\n* efficiently computing Jacobians and Hessians as well as batched ones\n\nComposing vmap (vectorization), vjp (reverse-mode AD), and jvp (forward-mode AD) transforms allows us to effortlessly express the above without designing a separate library for each.\n\nFor more details, please see our [documentation](https://pytorch.org/functorch/), [tutorials](https://pytorch.org/functorch), and [installation instructions](https://pytorch.org/functorch/stable/install.html).\n\n## Distributed Training\n\n### (Stable) DDP static graph", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "DDP static graph assumes that your model employs the same set of used/unused parameters in every iteration, so that it can deterministically know states like which hooks will fire, how many times the hooks will fire and gradients computation ready order after the first iteration. Static graph caches these states in the first iteration, and thus it could support features that DDP can not support in previous releases, e.g., support multiple activation checkpoints on the same parameters regardless of whether there are unused parameters or not. The static graph feature also applies performance optimizations when there are unused parameters, e.g., it avoids traversing graphs to search unused parameters every iteration, and enables dynamic bucketing order. These optimizations in the DDP static graph brought 10% QPS gain for some recommendation models.\n\nTo enable static graph, just simply set static_graph=True in the DDP API like this:\n\n```\nddp_model = DistributedDataParallel(model, static_graph=True)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} +{"page_content": "For more details, please see our [documentation](https://pytorch.org/docs/master/generated/torch.nn.parallel.DistributedDataParallel.html) and [tutorials](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html).\n\nThanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.11-released/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"Torchserve Performance Tuning, Animated Drawings Case-Study\"\nauthor: Hamid Shojanazeri, Geeta Chauhan, Mark Saroufim, Jesse Smith\nfeatured-img: \"assets/images/sketch_animator.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "In this post we discuss performance tuning of Torchserve for serving your models in production. One of the biggest challenges in the life cycle of a ML project is deploying models in production. This requires a reliable serving solution along with solutions that address the MLOps needs. A robust serving solution needs to provide support for multi model serving, model versioning, metric logging, monitoring and scaling to serve the peak traffic. In this post, we will have an overview of Torchserve and how to", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "tune its performance for production use-cases. We discuss the [Animated Drawings app](https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/) from Meta that can turn your human figure sketches to animations and how it could serve the peak traffic with Torchserve. The Animated Drawing\u2019s workflow is below.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n[https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/](https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/)", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Many AI systems and tools are designed to handle realistic images of humans, children's drawings add a level of complexity and unpredictability as they are often constructed in abstract, fanciful ways. These types of morphological and stylistic variations can confuse even state-of-the-art AI systems that excel at spotting objects in photorealistic images and drawings.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Meta AI researchers are working to overcome this challenge so that AI systems will be better able to recognize drawings of human figures in the wildly varied ways that children create them. This great blog post provides more details about the Animated Drawings and the approach taken.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve\n\n

\n\n

Fig1. Overall flow of Torchserve performance tuning
\n

\n\nOnce you have trained your model, it needs to be integrated into a larger system to have a full-fledged application, we use the term \u201cmodel serving\u201d to refer to this integration. Basically model serving is making your trained model available to run inferences and subsequent use of the model.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve is the Pytorch preferred solution for serving models in production. It is a performant and scalable tool that wraps your model in a HTTP or HTTPS API. It has a frontend implemented in Java that handles multiple tasks from assigning workers for serving models to handling the connection between client and server. Torchserve has a Python backend that is responsible for handling the inference service.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve supports multi model serving and versioning for AB test, dynamic batching, logging and metrics. It exposes four APIs for [inference](https://github.com/pytorch/serve/blob/master/docs/inference_api.md), [explanations](https://github.com/pytorch/serve/blob/master/docs/inference_api.md#explanations-api), [management](https://github.com/pytorch/serve/blob/master/docs/management_api.md) and [metrics](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md).", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "[Inference](https://github.com/pytorch/serve/blob/master/docs/inference_api.md) API is listening on port 8080 and accessible through localhost by default, this can be configured in [Torchserve configuration](https://github.com/pytorch/serve/blob/master/docs/configuration.md) and enable getting predictions from the model.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "[Explanation](https://github.com/pytorch/serve/blob/master/docs/inference_api.md#explanations-api) API uses Captum under the hood to provide explanations of the model that is being served and listens to the port 8080 as well.\n\n[Management](https://github.com/pytorch/serve/blob/master/docs/management_api.md#management-api) API allows to register or unregister and describe a model. It also enables users to scale up or down the number of workers that serve the model.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "[Metric](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md) API by default listens to port 8082 and enables us to monitor the model that is being served.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve let you scale your model serving and handle the peak traffic by supporting [batch inference](https://github.com/pytorch/serve/blob/master/docs/batch_inference_with_ts.md) and multiple workers that serve your model. Scaling can be done through [management](https://github.com/pytorch/serve/blob/master/docs/management_api.md) API and settings through a [configuration](https://github.com/pytorch/serve/blob/master/docs/configuration.md) file. Also, metric API helps you to monitor your model serving", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "through default and customizable metrics.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Other advanced settings such as the length of the queue for the received requests, maximum wait time for a batch of inputs and many other properties are configurable through a[ config file](https://github.com/pytorch/serve/blob/master/docs/configuration.md) that can be passed to Torchserve when it is started.\n\n\n**Steps to serve your model with Torchserve**", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "1. [Install Torchserve, model archiver](https://github.com/pytorch/serve/blob/master/docs/getting_started.md#install-torchserve-and-torch-model-archiver) and its requirements.\n2. Choose a default handler that fits your task (e.g image classification, etc) or author a [custom handler](https://github.com/pytorch/serve/blob/master/docs/custom_service.md#custom-handlers).", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "3. [Package your model](https://github.com/pytorch/serve/tree/master/examples/Huggingface_Transformers#create-model-archive-eager-mode) artifacts (trained model checkpoint and all other necessary files for loading and running your model) and the handler into a \u201c.mar\u201d file using [Torcharchive](https://github.com/pytorch/serve/blob/master/model-archiver/README.md) and place it in the model store.\n4. [Start serving your model](https://github.com/pytorch/serve/blob/master/docs/getting_started.md).", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "5. [Run inference](https://github.com/pytorch/serve/blob/master/docs/getting_started.md#get-predictions-from-a-model).\nWe will discuss model handlers and metrics in more detail here.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Model handlers\n\nTorchserve uses a handler in the backend to load the models, preprocess the received data, run inference and post-process the response. Handler in torchserve is a **python script** that all the model initialization, preprocessing, inference and post processing logic goes into.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve provides an out of the box handler for a number of applications like image classification, segmentation, object detection and text classification. It also supports custom handlers, in case your use case is not supported in default handlers.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "It provides a great flexibility in custom handlers, this potentially make Torchserve as **multi-framework** serving tool. Custom handlers let you define your custom logic to initialize a model that can be used also to load models from other frameworks such as ONNX.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve **handler** is made of four main **functions**, **initialize**, **preprocess**, **inference** and **postprocess** that each return a list. The code snippet below shows an example of a custom handler.**Custom handlers inherit** from **BaseHandler** in Torchserve and can **overwrite** any of the **main** **functions**. Here is an example of the handler used for loading the [Detectron2](https://github.com/facebookresearch/detectron2) model for figure detection, this model has been exported to", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchscript and uses model.half() to run the inference with FP16, details are explained in another [section]() in this post.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "```python\n\nclass MyModelHandler(BaseHandler):\n def initialize(self, context):\n self.manifest = ctx.manifest\n properties = ctx.system_properties\n model_dir = properties.get(\"model_dir\")\n serialized_file = self.manifest[\"model\"][\"serializedFile\"]\n model_pt_path = os.path.join(model_dir, serialized_file)", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "self.device = torch.device(\n \"cuda:\" + str(properties.get(\"gpu_id\"))\n if torch.cuda.is_available() and properties.get(\"gpu_id\") is not None\n else \"cpu\"\n )\n self.model = torch.jit.load(model_pt_path, map_location=self.device)\n\n self.model = self.model.half()\n\n def preprocess(self, data):\n\n inputs = []\n for request in batch:\n\n request_body = request.get(\"body\")", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "input_ = io.BytesIO(request_body)\n image = cv2.imdecode(np.fromstring(input_.read(), np.uint8), 1)\n input = torch.Tensor(image).permute(2, 0, 1)\n input = input.to(self.device)\n input = input.half()\n inputs.append({\"image\": input})\n\n return inputs\n\n def inference(self,inputs):\n predictions = self.model(**inputs)\n return predictions", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "def postprocess(self, output):\n responses = []\n for inference_output in inference_outputs:\n responses_json = {\n 'classes': inference_output['pred_classes'].tolist(),\n 'scores': inference_output['scores'].tolist(),\n \"boxes\": inference_output['pred_boxes'].tolist()\n }\n responses.append(json.dumps(responses_json))\n\n return responses\n```", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Metrics\n\nAn essential component in serving models in production is the ability to monitor them. **Torchserve** **collects** **system level** [metrics](https://github.com/pytorch/serve/blob/master/docs/metrics.md) regularly and **allows** adding **custom metrics** as well.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "**[System level metrics](https://github.com/pytorch/serve/blob/master/docs/metrics.md#system-metrics)** consist of CPU utilization, available and used disk space and memory on the host machine along with number of requests with different response codes (e.g 200-300, 400-500 and above 500). **Custom metrics** can be **added** to the metrics as explained [here](https://github.com/pytorch/serve/blob/master/docs/metrics.md#custom-metrics-api). TorchServe logs these two sets of metrics to different log files.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Metrics are collected by default at:", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* System metrics - log_directory/ts_metrics.log\n* Custom metrics - log directory/model_metrics.log", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "As mentioned before, Torchserve also exposes [metric API](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md), that by default listens to port 8082 and enables users to query and monitor the collected metrics. The default metrics endpoint returns Prometheus formatted metrics. You can query metrics using curl requests or point a [Prometheus Server](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md#prometheus-server) to the endpoint and use", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "[Grafana](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md#grafana) for dashboards.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "While serving a model you can query metrics using curl request as follows:\n\n```\ncurl http://127.0.0.1:8082/metrics", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "In case you are looking into exporting the logged metrics, please refer to this [example](https://github.com/google/mtail) that uses mtail to export metrics to Prometheus. Tracking these metrics in a dashboard allows you to monitor performance regressions that may have been sporadic or hard to spot during an offline benchmark run.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "What to consider for tuning performance of a model in production\n\nThe workflow suggested in Fig 1, is the general idea on how to approach model deployment in production with Torchserve.\n\nIn many cases serving models in production is **optimized** **based** on **throughput** or **latency** service level agreement (**SLA)s**. Usually **real-time** **applications** are more concerned about **latency** whereas **off-line applications** may care more about higher **throughput**.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "In this post we discuss performance tuning of Torchserve for serving your models in production. One of the biggest challenges in the life cycle of a ML project is deploying models in production. This requires a reliable serving solution along with solutions that address the MLOps needs. A robust serving solution needs to provide support for multi model serving, model versioning, metric logging, monitoring and scaling to serve the peak traffic. In this post, we will have an overview of Torchserve and how to tune its performance for production use-cases. We discuss the [Animated Drawings app](https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/) from Meta that can turn your human figure sketches to animations and how it could serve the peak traffic with Torchserve. The Animated Drawing\u2019s workflow is below.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "[https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/](https://ai.facebook.com/blog/using-ai-to-bring-childrens-drawings-to-life/)\n\nMany AI systems and tools are designed to handle realistic images of humans, children's drawings add a level of complexity and unpredictability as they are often constructed in abstract, fanciful ways. These types of morphological and stylistic variations can confuse even state-of-the-art AI systems that excel at spotting objects in photorealistic images and drawings.\nMeta AI researchers are working to overcome this challenge so that AI systems will be better able to recognize drawings of human figures in the wildly varied ways that children create them. This great blog post provides more details about the Animated Drawings and the approach taken.\n\n## Torchserve\n\n

\n\n

Fig1. Overall flow of Torchserve performance tuning
\n

", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Once you have trained your model, it needs to be integrated into a larger system to have a full-fledged application, we use the term \u201cmodel serving\u201d to refer to this integration. Basically model serving is making your trained model available to run inferences and subsequent use of the model. \n\nTorchserve is the Pytorch preferred solution for serving models in production. It is a performant and scalable tool that wraps your model in a HTTP or HTTPS API. It has a frontend implemented in Java that handles multiple tasks from assigning workers for serving models to handling the connection between client and server. Torchserve has a Python backend that is responsible for handling the inference service.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Torchserve supports multi model serving and versioning for AB test, dynamic batching, logging and metrics. It exposes four APIs for [inference](https://github.com/pytorch/serve/blob/master/docs/inference_api.md), [explanations](https://github.com/pytorch/serve/blob/master/docs/inference_api.md#explanations-api), [management](https://github.com/pytorch/serve/blob/master/docs/management_api.md) and [metrics](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md). \n\n[Inference](https://github.com/pytorch/serve/blob/master/docs/inference_api.md) API is listening on port 8080 and accessible through localhost by default, this can be configured in [Torchserve configuration](https://github.com/pytorch/serve/blob/master/docs/configuration.md) and enable getting predictions from the model.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "[Explanation](https://github.com/pytorch/serve/blob/master/docs/inference_api.md#explanations-api) API uses Captum under the hood to provide explanations of the model that is being served and listens to the port 8080 as well.\n\n[Management](https://github.com/pytorch/serve/blob/master/docs/management_api.md#management-api) API allows to register or unregister and describe a model. It also enables users to scale up or down the number of workers that serve the model. \n\n[Metric](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md) API by default listens to port 8082 and enables us to monitor the model that is being served.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Torchserve let you scale your model serving and handle the peak traffic by supporting [batch inference](https://github.com/pytorch/serve/blob/master/docs/batch_inference_with_ts.md) and multiple workers that serve your model. Scaling can be done through [management](https://github.com/pytorch/serve/blob/master/docs/management_api.md) API and settings through a [configuration](https://github.com/pytorch/serve/blob/master/docs/configuration.md) file. Also, metric API helps you to monitor your model serving through default and customizable metrics.\n\nOther advanced settings such as the length of the queue for the received requests, maximum wait time for a batch of inputs and many other properties are configurable through a[ config file](https://github.com/pytorch/serve/blob/master/docs/configuration.md) that can be passed to Torchserve when it is started.\n\n\n**Steps to serve your model with Torchserve**", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "1. [Install Torchserve, model archiver](https://github.com/pytorch/serve/blob/master/docs/getting_started.md#install-torchserve-and-torch-model-archiver) and its requirements.\n2. Choose a default handler that fits your task (e.g image classification, etc) or author a [custom handler](https://github.com/pytorch/serve/blob/master/docs/custom_service.md#custom-handlers).\n3. [Package your model](https://github.com/pytorch/serve/tree/master/examples/Huggingface_Transformers#create-model-archive-eager-mode) artifacts (trained model checkpoint and all other necessary files for loading and running your model) and the handler into a \u201c.mar\u201d file using [Torcharchive](https://github.com/pytorch/serve/blob/master/model-archiver/README.md) and place it in the model store.\n4. [Start serving your model](https://github.com/pytorch/serve/blob/master/docs/getting_started.md).\n5. [Run inference](https://github.com/pytorch/serve/blob/master/docs/getting_started.md#get-predictions-from-a-model).\nWe will discuss model handlers and metrics in more detail here.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "## Model handlers\n\nTorchserve uses a handler in the backend to load the models, preprocess the received data, run inference and post-process the response. Handler in torchserve is a **python script** that all the model initialization, preprocessing, inference and post processing logic goes into.\n\nTorchserve provides an out of the box handler for a number of applications like image classification, segmentation, object detection and text classification. It also supports custom handlers, in case your use case is not supported in default handlers. \n\nIt provides a great flexibility in custom handlers, this potentially make Torchserve as **multi-framework** serving tool. Custom handlers let you define your custom logic to initialize a model that can be used also to load models from other frameworks such as ONNX.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Torchserve **handler** is made of four main **functions**, **initialize**, **preprocess**, **inference** and **postprocess** that each return a list. The code snippet below shows an example of a custom handler.**Custom handlers inherit** from **BaseHandler** in Torchserve and can **overwrite** any of the **main** **functions**. Here is an example of the handler used for loading the [Detectron2](https://github.com/facebookresearch/detectron2) model for figure detection, this model has been exported to Torchscript and uses model.half() to run the inference with FP16, details are explained in another [section]() in this post.\n\n```python\n\nclass MyModelHandler(BaseHandler):\n def initialize(self, context):\n self.manifest = ctx.manifest\n properties = ctx.system_properties\n model_dir = properties.get(\"model_dir\")\n serialized_file = self.manifest[\"model\"][\"serializedFile\"]\n model_pt_path = os.path.join(model_dir, serialized_file)", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "self.device = torch.device(\n \"cuda:\" + str(properties.get(\"gpu_id\"))\n if torch.cuda.is_available() and properties.get(\"gpu_id\") is not None\n else \"cpu\"\n )\n self.model = torch.jit.load(model_pt_path, map_location=self.device)\n\n self.model = self.model.half()\n\n def preprocess(self, data):\n\n inputs = []\n for request in batch:\n\n request_body = request.get(\"body\")\n\n input_ = io.BytesIO(request_body)\n image = cv2.imdecode(np.fromstring(input_.read(), np.uint8), 1)\n input = torch.Tensor(image).permute(2, 0, 1)\n input = input.to(self.device)\n input = input.half()\n inputs.append({\"image\": input})\n\n return inputs\n\n def inference(self,inputs):\n predictions = self.model(**inputs)\n return predictions", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "def postprocess(self, output):\n responses = []\n for inference_output in inference_outputs:\n responses_json = {\n 'classes': inference_output['pred_classes'].tolist(),\n 'scores': inference_output['scores'].tolist(),\n \"boxes\": inference_output['pred_boxes'].tolist()\n }\n responses.append(json.dumps(responses_json))\n\n return responses\n```\n\n## Metrics\n\nAn essential component in serving models in production is the ability to monitor them. **Torchserve** **collects** **system level** [metrics](https://github.com/pytorch/serve/blob/master/docs/metrics.md) regularly and **allows** adding **custom metrics** as well.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "**[System level metrics](https://github.com/pytorch/serve/blob/master/docs/metrics.md#system-metrics)** consist of CPU utilization, available and used disk space and memory on the host machine along with number of requests with different response codes (e.g 200-300, 400-500 and above 500). **Custom metrics** can be **added** to the metrics as explained [here](https://github.com/pytorch/serve/blob/master/docs/metrics.md#custom-metrics-api). TorchServe logs these two sets of metrics to different log files. Metrics are collected by default at:\n\n* System metrics - log_directory/ts_metrics.log\n* Custom metrics - log directory/model_metrics.log", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "As mentioned before, Torchserve also exposes [metric API](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md), that by default listens to port 8082 and enables users to query and monitor the collected metrics. The default metrics endpoint returns Prometheus formatted metrics. You can query metrics using curl requests or point a [Prometheus Server](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md#prometheus-server) to the endpoint and use [Grafana](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md#grafana) for dashboards. \n\nWhile serving a model you can query metrics using curl request as follows:\n\n```\ncurl http://127.0.0.1:8082/metrics\n```", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "In case you are looking into exporting the logged metrics, please refer to this [example](https://github.com/google/mtail) that uses mtail to export metrics to Prometheus. Tracking these metrics in a dashboard allows you to monitor performance regressions that may have been sporadic or hard to spot during an offline benchmark run.\n\n## What to consider for tuning performance of a model in production\n\nThe workflow suggested in Fig 1, is the general idea on how to approach model deployment in production with Torchserve.\n\nIn many cases serving models in production is **optimized** **based** on **throughput** or **latency** service level agreement (**SLA)s**. Usually **real-time** **applications** are more concerned about **latency** whereas **off-line applications** may care more about higher **throughput**.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} {"page_content": "There are a number of main factors contributing to the performance of a serving model in production. In particular, we are focusing on serving Pytorch models with Torchserve here, however most of these factors generalize to all models from other frameworks as well.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Model optimizations**: this is a pre-step for deploying models into production. This is a very broad discussion that we will get into in a series of future blogs. This includes techniques like quantization, pruning to decrease the size of the model, using Intermediate representations (IR graphs) such as Torchscript in Pytorch, fusing kernels and many others. Currently [torchprep](https://github.com/msaroufim/torchprep) provides many of these techniques as a CLI tool.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Batch inference:** it refers to feeding multiple inputs into a model, while it is essential during training, it can be very helpful to manage the cost at inference time as well. Hardware accelerators are optimized for parallelism and batching helps to saturate the compute capacity and often leads to higher throughput. The main difference in inference is you can\u2019t wait too long to get a batch filled from clients, something we call dynamic batching", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Number of Workers :** Torchserve uses workers to serve models. Torchserve workers are Python processes that hold a copy of the model weights for running inference. Too few workers means you\u2019re not benefitting from enough parallelism but too many can cause worker contention and degrade end to end performance.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "- **Hardware :** choosing the appropriate hardware based on the model, application and latency, throughput budget. This could be one of the **supported** hardwares in Torchserve, **CPU, GPU, AWS Inferentia**. Some hardware configurations are intended for best in class performance and others are better suited for cost effective inference. From our experiments we\u2019ve found that GPUs shine best at larger batch sizes whereas the right CPUs and AWS Inferentia can be far more cost effective for lower batch sizes", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "and low latency.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Best Practices for Performance tuning on Torchserve\n\nTo get the best performance out of your model while serving it with Torchserve, we are sharing some of the best practices here. Torchserve provides a [benchmark](https://github.com/pytorch/serve/tree/c87bfec8916d340de5de5810b14a016049b0e395/benchmarks#benchmarking-with-apache-bench) suite that provides helpful insight to make informed decisions on different choices as detailed below.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Optimize your model** as the first step, Pytorch model optimization [tutorials](https://pytorch.org/tutorials/). **Model optimization** choices are also closely **tied** to the **hardware** of choice. We will discuss it in more detail in another blog post.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Deciding** the **hardware** for model deployment can be closely related to the latency and throughput budget and cost per inference. Depending on the size of model and application it can vary, for some models like computer vision models it has been historically not affordable to run in production on CPU. However, by having optimizations such [IPEX](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/examples/intel_extension_for_pytorch/README.md) as recently added to", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve this has been much more affordable and cost beneficial and you can learn more in this investigative [case study](https://pytorch.org/tutorials/intermediate/torchserve_with_ipex.html)", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Workers** in Torchserve are Python processes that provide parallelism, setting the number of workers should be done carefully. By default Torchserve launch number of workers equal to VCPUs or available GPUs on the host, this can add a considerable amount of time to the Torchserve start.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Torchserve exposes a [config property](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/configuration.md#config-model) to set the number of workers. To provide an **efficient parallelism** through **multiple workers** and avoiding them to compete over resources, as a baseline we **recommend** following setting on CPU and GPU:", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "**CPU** : In the handler, `torch.set_num_threads(1) `then set the number of workers to `num physical cores / 2. `But the the best threading configurations can be achieved by leveraging the Intel CPU launcher script.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "* **Model optimizations**: this is a pre-step for deploying models into production. This is a very broad discussion that we will get into in a series of future blogs. This includes techniques like quantization, pruning to decrease the size of the model, using Intermediate representations (IR graphs) such as Torchscript in Pytorch, fusing kernels and many others. Currently [torchprep](https://github.com/msaroufim/torchprep) provides many of these techniques as a CLI tool. \n* **Batch inference:** it refers to feeding multiple inputs into a model, while it is essential during training, it can be very helpful to manage the cost at inference time as well. Hardware accelerators are optimized for parallelism and batching helps to saturate the compute capacity and often leads to higher throughput. The main difference in inference is you can\u2019t wait too long to get a batch filled from clients, something we call dynamic batching\n* **Number of Workers :** Torchserve uses workers to serve models. Torchserve workers are Python processes that hold a copy of the model weights for running inference. Too few workers means you\u2019re not benefitting from enough parallelism but too many can cause worker contention and degrade end to end performance.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "- **Hardware :** choosing the appropriate hardware based on the model, application and latency, throughput budget. This could be one of the **supported** hardwares in Torchserve, **CPU, GPU, AWS Inferentia**. Some hardware configurations are intended for best in class performance and others are better suited for cost effective inference. From our experiments we\u2019ve found that GPUs shine best at larger batch sizes whereas the right CPUs and AWS Inferentia can be far more cost effective for lower batch sizes and low latency.\n\n## Best Practices for Performance tuning on Torchserve\n\nTo get the best performance out of your model while serving it with Torchserve, we are sharing some of the best practices here. Torchserve provides a [benchmark](https://github.com/pytorch/serve/tree/c87bfec8916d340de5de5810b14a016049b0e395/benchmarks#benchmarking-with-apache-bench) suite that provides helpful insight to make informed decisions on different choices as detailed below.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "* **Optimize your model** as the first step, Pytorch model optimization [tutorials](https://pytorch.org/tutorials/). **Model optimization** choices are also closely **tied** to the **hardware** of choice. We will discuss it in more detail in another blog post.\n* **Deciding** the **hardware** for model deployment can be closely related to the latency and throughput budget and cost per inference. Depending on the size of model and application it can vary, for some models like computer vision models it has been historically not affordable to run in production on CPU. However, by having optimizations such [IPEX](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/examples/intel_extension_for_pytorch/README.md) as recently added to Torchserve this has been much more affordable and cost beneficial and you can learn more in this investigative [case study](https://pytorch.org/tutorials/intermediate/torchserve_with_ipex.html) \n* **Workers** in Torchserve are Python processes that provide parallelism, setting the number of workers should be done carefully. By default Torchserve launch number of workers equal to VCPUs or available GPUs on the host, this can add a considerable amount of time to the Torchserve start.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Torchserve exposes a [config property](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/configuration.md#config-model) to set the number of workers. To provide an **efficient parallelism** through **multiple workers** and avoiding them to compete over resources, as a baseline we **recommend** following setting on CPU and GPU:\n\n\n **CPU** : In the handler, `torch.set_num_threads(1) `then set the number of workers to `num physical cores / 2. `But the the best threading configurations can be achieved by leveraging the Intel CPU launcher script.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} {"page_content": "**GPU**: number of available GPUs can be set through[ number_gpus](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/configuration.md#limit-gpu-usage) in config.properties. Torchserve uses round robin to assign workers to GPUs. We recommend setting the number of workers as follows. `Number of worker = (Number of available GPUs) / (Number of Unique Models). `Note that GPUs that are pre-Ampere do not provide any resource isolation with Multi Instance GPUs.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* **Batch size** can directly affect the latency and the throughput. To better utilize the compute resources batch size needs to be increased. However, there is a tradeoff between latency and throughput. **Larger batch sizes** can **increase** the **throughput but results in a higher latency** as well. Batch size can be set in Torchserve in two ways, either through[ model config](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/configuration.md#config-model) in", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "config.properties or while registering the model using [Management API](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/management_api.md#scale-workers).", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "In the next section, we are going to use Torchserve benchmark suite to decide the best combination of model optimization, hardware, workers, and batch size.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Animated Drawings Performance Tuning \n\nTo use the Torchserve benchmark suite, first we need to have an archived file, \u201c.mar\u201d file as discussed above, that contains the model, handler and all other artifacts to load and run inference. Animated Drawings uses Detectron2\u2019s implementation of Mask-RCNN for an object detection model.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "How to run benchmark suite \n\nThe [Automated benchmark suite](https://github.com/pytorch/serve/tree/master/benchmarks#auto-benchmarking-with-apache-bench) in Torchserve let you benchmark multiple models with different setting including batch size and number of worker and finally generate a report for you. To get started:\n\n```\ngit clone https://github.com/pytorch/serve.git\n\ncd serve/benchmarks\n\npip install -r requirements-ab.txt\n\napt-get install apache2-utils", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Model level settings can be configured in a yaml file similar to \n\n```yaml", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Model_name:\n eager_mode:\n benchmark_engine: \"ab\"\n url: \"Path to .mar file\"\n workers:\n - 1\n - 4\n batch_delay: 100\n batch_size:\n - 1\n - 2\n - 4\n - 8\n requests: 10000\n concurrency: 10\n input: \"Path to model input\"\n backend_profiling: False\n exec_env: \"local\"\n processors:\n - \"cpu\"\n - \"gpus\": \"all\"", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "This yaml file will be referenced in the [benchmark_config_template](https://github.com/pytorch/serve/blob/master/benchmarks/benchmark_config_template.yaml#L12).yaml file that includes other settings for generating reports, this can optionally work with AWS cloud watch for logs as well.\n\n```\npython benchmarks/auto_benchmark.py --input benchmark_config_template.yaml", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Running the **benchmarks**, results will be written in \u201ccsv\u201d file that can be found in \u201c_ /tmp/benchmark/ab_report.csv_\u201d and full report \u201c/tmp/ts_benchmark/report.md\". It will include items such as Torchserve average latency, model P99 latency, throughput, number of concurrency, number of requests, handler time, and some other metrics. Here we focus on some of the important ones that we track to tune the performance which are, **concurrency**, **model P99** latency, **throughput**. We look at these numbers", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "specifically in **combination** with **batch size**, the used **device, number of workers** and if any **model optimization** has been done.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "The **latency SLA** for this model has been set to **100 ms,** this is real-time application and as we discussed earlier, latency is more of a concern and **throughput** ideally should be as high as possible while it does **not violate** the **latency SLA.**\n\nThrough searching the space, over different batch sizes (1-32), number of workers (1-16) and devices (CPU,GPU), we have run a set of experiments that summarized the best ones in the table below.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n
Device \n Concurrency \n # Requests\n #workers\n Batch size\n Payload/image\n Optimization \n Throughput \n Latency P99\n
CPU\n 10\n 1000\n 1\n 1\n small\n N/A\n 3.45\n 305.3 ms\n
CPU\n 1", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "1000\n 1\n 1\n small\n N/A\n 3.45\n 291.8 ms\n
GPU\n 10\n 1000\n 1\n 1\n small\n N/A\n 41.05\n 25.48 ms\n
GPU\n 1\n 1000\n 1\n 1\n small\n N/A\n 42.21\n 23.6 ms\n
GPU\n 10\n 1000\n 1\n 4\n small\n N/A\n 54.78\n 73.62 ms\n
GPU\n 10\n 1000\n 1\n 4\n small\n model.half()\n 78.62\n 50.69 ms\n
GPU\n 10\n 1000\n 1\n 8\n small\n model.half()\n 85.29\n 94.4 ms\n
", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "The latency of this model on CPU with all of the tried settings in terms of batch size, concurrency and number of workers did not meet the SLA, in fact ~13x higher.\n\n**Moving** the model serving **to GPU**, immediately could **improve** the **latency** ~**13x **from 305 ms down to 23.6 ms.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "One of the **simplest** **optimizations** that we could do for the model was lowering its precision to **fp16**, it is one liner (**model.half()**) and could reduce the **model P99 latency **by **32%** and increase the throughput by almost the same amount.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "There could be other optimization done by Torchscripting the model and using [optimize_for_inference](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) or other tricks including onnx or tensorrt runtime optimizations which leverage aggressive fusions are out of the scope of this post. We will discuss model optimizations in a separate post.\n\nWe found both on CPU and GPU , setting **number of workers=1 **worked the best in this case.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "* Moving the model to GPU, using **number of workers = 1**, and **batch size = 1** increased the **Throughput ~12x compared** to **CPU and latency ~13x.**\n* Moving the model to GPU, using **model.half()**, **number of workers = 1**, and **batch size = 8** yielded **best** results in terms of **Throughput** and tolerable latency. **Throughput** increased **~25x compared** to **CPU with latency still meeting the SLA (94.4ms).**", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "_Note: if you are running the benchmark suite, make sure you are setting a proper `batch_delay` and set the concurrency of the request to a number proportional to your batch size. Concurrency here means the number of concurrent requests being sent to the server._", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Conclusion\n\nIn this post, we have discussed the considerations and knobs that Torchserve expose to tune the performance in production. We have discussed the Torchserve benchmark suite as a means to tune the performance and get insights on possible choices for model optimizations, hardware choice and cost in general. We used Animated Drawings app which uses Detectron2\u2019s Mask-RCNN model as a case-study to showcase the performance tuning with benchmark suite.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "For more details on Performance tuning in Torchserve please refer to our documentation [here](https://github.com/pytorch/serve/blob/master/docs/performance_guide.md).\nAlso feel free to open a ticket on [Torchserve repo](https://github.com/pytorch/serve/issues) for any further questions and feedback.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgement\n\nWe would like to thank Somya Jain (Meta), Christopher Gustave (Meta) for their great support and guidance throughout many steps of this blog and providing insights to Sketch Animator workflow. Also, special thanks to[ Li Ning](https://www.linkedin.com/in/li-ning-7274604/) from AWS for the great efforts to make performance tuning much easier on Torchserve with automated benchmark suite.\n\n\n", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling Vision Model Training Platforms with PyTorch\"\nauthor: Vaibhav Aggarwal, Mannat Singh, Anjali Sridhar, Yanghao Li, Shoubhik Debnath, Ronghang Hu, Will Feng, Xinlei Chen, Tingting Markstrum, Diana Liskovich, Anupam Bhatnagar, Chay Ryali, Haoqi Fan, Tete Xiao, Min Xu, Rahul Iyer, Christoph Feichtenhofer, Ross Girshick, Piotr Dollar, Aaron Adcock, Wan-Yen Lo, CK Luk\nfeatured-img: \"/assets/images/scaling-vision-figure_1-solutions-to-the-challenges.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "*TL;DR: We demonstrate the use of PyTorch with FairScale\u2019s FullyShardedDataParallel (FSDP) API in writing large vision transformer models. We discuss our techniques for scaling and optimizing these models on a GPU cluster. The goal of this platform scaling effort is to enable research at scale. This blog does not discuss model accuracy, new model architectures, or new training recipes.*", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. Introduction\n\nLatest vision research [1, 2] demonstrates model scaling as a promising research direction. In this project, we aim to enable our platforms to train massive vision transformer (ViT) [3] models. We present our work on scaling the largest trainable ViT from 1B to 120B parameters in FAIR vision platforms. We wrote ViT in PyTorch and leveraged its support for large-scale, distributed training on a GPU cluster.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the rest of this blog, we will first discuss the main challenges, namely *scalability*, *optimization*, and *numerical stability*. Then we will discuss how we tackle them with techniques including *data and model parallelism*, *automatic mixed precision*, *kernel fusion*, and *bfloat16*. Finally, we present our results and conclude.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. Main Challenges", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2.1 Scalability\n\nThe key scalability challenge is to efficiently shard a model\u2019s operations and state across multiple GPUs. A 100B parameter model requires ~200GB of RAM just for parameters, assuming fp16 representation. So, it is impossible to fit the model on a single GPU (A100 has at most 80GB RAM). Therefore, we need some way to efficiently shard a model\u2019s data (input, parameters, activations, and optimizer state) across multiple GPUs.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Another aspect of this problem is to scale without significantly changing the training recipe. E.g. Certain representation learning recipes use a global batch size of up to 4096 beyond which we start to see accuracy degradation. We cannot scale to more than 4096 GPUs without using some form of tensor or pipeline parallelism.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2.2 Optimization", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The key optimization challenge is to maintain high GPU utilization even as we scale the number of model parameters and flops. When we scale models to teraflops and beyond, we start to hit major bottlenecks in our software stack that super-linearly increase training time and reduce accelerator utilization. We require hundreds or thousands of GPUs to run just a single experiment. Improvements in accelerator utilization can lead to significant reductions in cost and improve fleet utilization. It enables us to", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "fund more projects and run more experiments in parallel.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2.3 Numerical Stability", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The key stability challenge is to avoid numerical instability and divergence at large scale. We empirically observed in our experiments that the training instability gets severe and hard to deal with when we scale up model sizes, data, batch sizes, learning rate, etc. Vision Transformers particularly face training instability even at a lower parameter threshold. E.g., we find it challenging to train even ViT-H (with just 630M parameters) in mixed-precision mode without using strong data augmentation. We", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "need to study the model properties and training recipes to make sure that the models train stably and converge.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. Our Solutions\n\n**Figure 1** depicts our solutions to each of the challenges.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3.1 Addressing scaling challenges with data parallelism and model parallelism\n\nWe apply various forms of data and model parallelism to enable fitting very large models in GPU memory.\n\nWe use FairScale\u2019s *FullyShardedDataParallel (FSDP)* API [4], based on PyTorch, to shard parameters, gradients, and optimizer state across multiple GPUs, thereby reducing the memory footprint per GPU. This process consists of the following three steps:", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Step 1: We wrapped the entire model in a single FSDP instance. This shards the model parameters at the end of a forward pass and gathers parameters at the beginning of a forward pass. This enabled us to scale ~3x from 1.5B to 4.5B parameters.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Step 2: We experimented with wrapping individual model layers in separate FSDP instances. This nested wrapping further reduced the memory footprint by sharding and gathering parameters of individual model layers instead of an entire model. The peak memory is then determined by an individually wrapped transformer block in GPU memory in this mode instead of the entire model.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Step 3: We used *activation-checkpoint* to reduce the memory consumption by activations. It saves the input tensors and discards the intermediate activation tensors during the forward pass. These are recomputed during the backward pass.\n\nIn addition, we experimented with model-parallelism techniques such as pipeline parallelism [5], which allow us to scale to more GPUs without increasing the batch size.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3.2 Addressing optimization challenges with advanced AMP and kernel fusion", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Advanced AMP\n\nAutomatic Mixed Precision (AMP) [6] training refers to training models using a lower precision of bits than FP32 or the default but still maintaining accuracy. We experimented with three levels of AMP as described below:\n\n- AMP O1: This refers to training in mixed precision where weights are in FP32 and some operations are in FP16. With AMP O1, the ops that might impact accuracy remain in FP32 and are not autocasted to FP16.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- AMP O2: This refers to training in mixed precision but with more weights and ops in FP16 than in O1. Weights do not implicitly remain in FP32 and are cast to FP16. A copy of the master weights is maintained in the FP32 precision that is used by the optimizer. If we want the normalization layer weights in FP32 then we need to explicitly use layer wrapping to ensure that.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Full FP16: This refers to training in full FP16 where weights and operations are in FP16. FP16 is challenging to enable for training due to convergence issues.\n\nWe found that AMP O2 with LayerNorm wrapping in FP32 leads to the best performance without sacrificing accuracy.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Kernel Fusion\n\n- To reduce GPU kernel launch overhead and increase GPU work granularity, we experimented with kernel fusions, including fused dropout and fused layer-norm, using the [xformers library](https://github.com/facebookresearch/xformers) [7].", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3.3 Addressing stability challenges by studying ops numerical stability and training recipes", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "BFloat16 in general but with LayerNorm in FP32\n\nThe [bfloat16](https://cloud.google.com/tpu/docs/bfloat16) (BF16) [8] floating-point format provides the same dynamic range as FP32 with a memory footprint identical to FP16. We found that we could train models in the BF16 format using the same set of hyperparameters as in FP32, without special parameter tuning. Nevertheless, we found that we need to keep LayerNorm in FP32 mode in order for the training to converge.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3.4 Final training recipe\n\nA summary of the final training recipe.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. Wrap the outer model in an FSDP instance. Enable parameter sharding after the forward pass.\n2. Wrap individual ViT blocks with activation checkpointing, nested FSDP wrapping, and parameter flattening.\n3. Enable mixed precision mode (AMP O2) with bfloat16 representation. Maintain the optimizer state in FP32 precision to enhance numerical stability.\n4. Wrap normalization layers like LayerNorm in FP32 for better numerical stability.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "5. Maximize the Nvidia TensorCore utilization by keeping matrix dimensions to be multiple of 8. For More details check [Nvidia Tensor Core Performance Guide](https://developer.download.nvidia.com/video/gputechconf/gtc/2019/presentation/s9926-tensor-core-performance-the-ultimate-guide.pdf).", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "4. Results", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this section, we show the scaling results of ViT on three types of tasks: (1) image classification, (2) object detection (3) video understanding. **Our key result is that we are able to train massive ViT backbones across these vision tasks after applying the discussed scaling and optimization techniques. This enables vision research at a much larger scale.** We trained the models to convergence to verify that we maintain the current baselines even with all the optimizations. A common trend in Figures 2,", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3, 4 is that we are able to train up to 25B-param models with an epoch time of less than 4 hours on 128 A100 GPUs. The 60B and 120B models are relatively slower to train.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 2** shows the *image-classification* scaling result. It plots the epoch time for training ViTs on ImageNet using 128 A100-80GB GPUs with different model sizes.\n\n

\n\n

\n\n

\nFigure 2: Image-classification scaling result.\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 3** shows the *object-detection* scaling result. It plots the epoch time for training [ViTDet](https://arxiv.org/abs/2203.16527) [9] with different ViT backbones on COCO using 128 A100-80GB GPUs.\n\n

\n\n

\n\n

\nFigure 3: Object-detection scaling result.\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 4** shows the *video-understanding* scaling result. It plots the epoch time for training [MViTv2](https://arxiv.org/abs/2112.01526) [10] models on [Kinetics 400](https://www.deepmind.com/open-source/kinetics) [11] using 128 V100 (32 GB) GPUs in FP32.\n\n

\n\n

\n\n

\nFigure 4: Video-understanding scaling result.\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 5** shows the optimization result with the ViT-H model in Figure 2 on 8 A100-40GB GPUs.\nThree versions are used: (1) the baseline uses PyTorch\u2019s DDP [12] with AMP O1, (2) FSDP + AMP-O2 + other optimizations, and (3) FSDP + FP16 + other optimizations. These optimizations altogether speed up the training by up to 2.2x.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure 5: Training speedups from various optimizations.\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "5. Concluding Remarks\n\nWe have demonstrated the use of PyTorch with FairScale\u2019s FullyShardedDataParallel (FSDP) API in writing large vision transformer models. We discuss our techniques for scaling and optimizing these models on a GPU cluster. We hope that this article can motivate others to develop large-scale ML models with PyTorch and its ecosystem.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "References\n\n[1] [Masked Autoencoders Are Scalable Vision Learners](https://arxiv.org/abs/2111.06377)\n\n[2] [Revisiting Weakly Supervised Pre-Training of Visual Perception Models](https://arxiv.org/abs/2201.08371)\n\n[3] [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929v2)\n\n[4] [fairscale.nn.FullyShardedDataParallel](https://fairscale.readthedocs.io/en/stable/api/nn/fsdp.html)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[5] [Pipeline parallelism in PyTorch](https://pytorch.org/docs/stable/pipeline.html)\n\n[6] [Automatic Mixed Precision (AMP) in PyTorch](https://pytorch.org/docs/stable/amp.html#module-torch.amp)\n\n[7] [xformers](https://github.com/facebookresearch/xformers)\n\n[8] [The bfloat16 numerical format](https://cloud.google.com/tpu/docs/bfloat16)\n\n[9] [Exploring Plain Vision Transformer Backbones for Object Detection](https://arxiv.org/abs/2203.16527)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[10] [MViTv2: Improved Multiscale Vision Transformers for Classification and Detection](https://arxiv.org/abs/2112.01526)\n\n[11] [https://www.deepmind.com/open-source/kinetics](https://www.deepmind.com/open-source/kinetics)\n\n[12] [Getting Started with Distributed Data Parallel (DDP)](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Ecosystem Day'\nauthor: Team PyTorch\n---\n\nWe\u2019re proud to announce our first PyTorch Ecosystem Day. The virtual, one-day event will focus completely on our Ecosystem and Industry PyTorch communities!\n\n\nPyTorch is a deep learning framework of choice for academics and companies, all thanks to its rich ecosystem of tools and strong community. As with our developers, our ecosystem partners play a pivotal role in the development and growth of the community.", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "We will be hosting our first PyTorch Ecosystem Day, a virtual event designed for our ecosystem and industry communities to showcase their work and discover new opportunities to collaborate.", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Ecosystem Day will be held on April 21, with both a morning and evening session, to ensure we reach our global community. Join us virtually for a day filled with discussions on new developments, trends, challenges, and best practices through keynotes, breakout sessions, and a unique networking opportunity hosted through Gather.Town .", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "Event Details\nApril 21, 2021 (Pacific Time)\nFully digital experience \n \n* Morning Session: (EMEA)\nOpening Talks - 8:00 am-9:00 am PT\nPoster Exhibition & Breakout Sessions - 9:00 am-12:00 pm PT \n\n* Evening Session (APAC/US)\nOpening Talks - 3:00 pm-4:00 pm PT\nPoster Exhibition & Breakout Sessions - 3:00 pm-6:00 pm PT \n\n* Networking - 9:00 am-7:00 pm PT", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "There are two ways to participate in PyTorch Ecosystem Day:\n \n1. **Poster Exhibition** from the PyTorch ecosystem and industry communities covering a variety of topics. Posters are available for viewing throughout the duration of the event. To be part of the poster exhibition, please see below for submission details. If your poster is accepted, we highly recommend tending your poster during one of the morning or evening sessions or both!", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "2. **Breakout Sessions** are 40-min sessions freely designed by the community. The breakouts can be talks, demos, tutorials or discussions. Note: you must have an accepted poster to apply for the breakout sessions.", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "Call for posters now open! [Submit your proposal](https://pytorchecosystemday.fbreg.com/posters) today! Please send us the **title** and **summary** of your projects, tools, and libraries that could benefit PyTorch researchers in academia and industry, application developers, and ML engineers for consideration. The focus must be on academic papers, machine learning research, or open-source projects. Please no sales pitches. **Deadline for submission is March 18, 2021.**", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "Visit [pytorchecosystemday.fbreg.com](http://pytorchecosystemday.fbreg.com) for more information and we look forward to welcoming you to PyTorch Ecosystem Day on April 21st!", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.5 released, new and updated APIs including C++ frontend API parity with Python'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Today, we\u2019re announcing the availability of PyTorch 1.5, along with new and updated libraries. This release includes several major new API additions and improvements. PyTorch now includes a significant update to the C++ frontend, \u2018channels last\u2019 memory format for computer vision models, and a stable release of the distributed RPC framework used for model-parallel training. The release also has new APIs for autograd for hessians and jacobians, and an API that allows the creation of Custom C++ Classes that", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "was inspired by pybind.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "You can find the detailed release notes [here](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "C++ Frontend API (Stable)\n\nThe C++ frontend API is now at parity with Python, and the features overall have been moved to \u2018stable\u2019 (previously tagged as experimental). Some of the major highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "* Now with ~100% coverage and docs for C++ torch::nn module/functional, users can easily translate their model from Python API to C++ API, making the model authoring experience much smoother.\n* Optimizers in C++ had deviated from the Python equivalent: C++ optimizers can\u2019t take parameter groups as input while the Python ones can. Additionally, step function implementations were not exactly the same. With the 1.5 release, C++ optimizers will always behave the same as the Python equivalent.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "* The lack of tensor multi-dim indexing API in C++ is a well-known issue and had resulted in many posts in PyTorch Github issue tracker and forum. The previous workaround was to use a combination of `narrow` / `select` / `index_select` / `masked_select`, which was clunky and error-prone compared to the Python API\u2019s elegant `tensor[:, 0, ..., mask]` syntax. With the 1.5 release, users can use `tensor.index({Slice(), 0, \"...\", mask})` to achieve the same purpose.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "\u2018Channels last\u2019 memory format for Computer Vision models (Experimental)\n\n\u2018Channels last\u2019 memory layout unlocks ability to use performance efficient convolution algorithms and hardware (NVIDIA\u2019s Tensor Cores, FBGEMM, QNNPACK). Additionally, it is designed to automatically propagate through the operators, which allows easy switching between memory layouts.\n\nLearn more [here](https://github.com/pytorch/pytorch/wiki/Writing-memory-format-aware-operators) on how to write memory format aware operators.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Custom C++ Classes (Experimental)\n\nThis release adds a new API, `torch::class_`, for binding custom C++ classes into TorchScript and Python simultaneously. This API is almost identical in syntax to [pybind11](https://pybind11.readthedocs.io/en/stable/). It allows users to expose their C++ class and its methods to the TorchScript type system and runtime system such that they can instantiate and manipulate arbitrary C++ objects from TorchScript and Python. An example C++ binding:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "```python\ntemplate \nstruct MyStackClass : torch::CustomClassHolder {\n std::vector stack_;\n MyStackClass(std::vector init) : stack_(std::move(init)) {}\n\n void push(T x) {\n stack_.push_back(x);\n }\n T pop() {\n auto val = stack_.back();\n stack_.pop_back();\n return val;\n }\n};", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "static auto testStack =\n torch::class_>(\"myclasses\", \"MyStackClass\")\n .def(torch::init>())\n .def(\"push\", &MyStackClass::push)\n .def(\"pop\", &MyStackClass::pop)\n .def(\"size\", [](const c10::intrusive_ptr& self) {\n return self->stack_.size();\n });", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Which exposes a class you can use in Python and TorchScript like so:\n\n```python\n@torch.jit.script\ndef do_stacks(s : torch.classes.myclasses.MyStackClass):\n s2 = torch.classes.myclasses.MyStackClass([\"hi\", \"mom\"])\n print(s2.pop()) # \"mom\"\n s2.push(\"foobar\")\n return s2 # [\"hi\", \"foobar\"]\n```\n\nYou can try it out in the tutorial [here](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Distributed RPC framework APIs (Now Stable)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "The Distributed [RPC framework](https://pytorch.org/docs/stable/rpc.html) was launched as experimental in the 1.4 release and the proposal is to mark Distributed RPC framework as stable and no longer experimental. This work involves a lot of enhancements and bug fixes to make the distributed RPC framework more reliable and robust overall, as well as adding a couple of new features, including profiling support, using TorchScript functions in RPC, and several enhancements for ease of use. Below is an overview", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "of the various APIs within the framework:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "RPC API\nThe RPC API allows users to specify functions to run and objects to be instantiated on remote nodes. These functions are transparently recorded so that gradients can backpropagate through remote nodes using Distributed Autograd.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Distributed Autograd", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Distributed Autograd connects the autograd graph across several nodes and allows gradients to flow through during the backwards pass. Gradients are accumulated into a context (as opposed to the .grad field as with Autograd) and users must specify their model\u2019s forward pass under a with `dist_autograd.context()` manager in order to ensure that all RPC communication is recorded properly. Currently, only FAST mode is implemented (see", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "[here](https://pytorch.org/docs/stable/rpc/distributed_autograd.html#distributed-autograd-design) for the difference between FAST and SMART modes).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Distributed Optimizer\nThe distributed optimizer creates RRefs to optimizers on each worker with parameters that require gradients, and then uses the RPC API to run the optimizer remotely. The user must collect all remote parameters and wrap them in an `RRef`, as this is required input to the distributed optimizer. The user must also specify the distributed autograd `context_id` so that the optimizer knows in which context to look for gradients.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Learn more about distributed RPC framework APIs [here](https://pytorch.org/docs/stable/rpc.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "New High level autograd API (Experimental)\n\nPyTorch 1.5 brings new functions including jacobian, hessian, jvp, vjp, hvp and vhp to the `torch.autograd.functional` submodule. This feature builds on the current API and allows the user to easily perform these functions.\n\nDetailed design discussion on GitHub can be found [here](https://github.com/pytorch/pytorch/issues/30632).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "Python 2 no longer supported\n\nStarting PyTorch 1.5.0, we will no longer support Python 2, specifically version 2.7. Going forward support for Python will be limited to Python 3, specifically Python 3.5, 3.6, 3.7 and 3.8 (first enabled in PyTorch 1.4.0).\n\n\n*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'OpenMined and PyTorch partner to launch fellowship funding for privacy-preserving ML community'\nauthor: Andrew Trask (OpenMined/U.Oxford), Shubho Sengupta, Laurens van der Maaten, Joe Spisak\nexcerpt: Many applications of machine learning (ML) pose a range of security and privacy challenges.\n---\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Many applications of machine learning (ML) pose a range of security and privacy challenges. In particular, users may not be willing or allowed to share their data, which prevents them from taking full advantage of ML platforms like PyTorch. To take the field of privacy-preserving ML (PPML) forward, OpenMined and PyTorch are announcing plans to jointly develop a combined platform to accelerate PPML research as well as new funding for fellowships.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "There are many techniques attempting to solve the problem of privacy in ML, each at various levels of maturity. These include (1) homomorphic encryption, (2) secure multi-party computation, (3) trusted execution environments, (4) on-device computation, (5) federated learning with secure aggregation, and (6) differential privacy. Additionally, a number of open source projects implementing these techniques were created with the goal of enabling research at the intersection of privacy, security, and ML. Among", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "them, PySyft and CrypTen have taken an \u201cML-first\u201d approach by presenting an API that is familiar to the ML community, while masking the complexities of privacy and security protocols. We are excited to announce that these two projects are now collaborating closely to build a mature PPML ecosystem around PyTorch.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Additionally, to bolster this ecosystem and take the field of privacy preserving ML forward, we are also calling for contributions and supporting research efforts on this combined platform by providing funding to support the OpenMined community and the researchers that contribute, build proofs of concepts and desire to be on the cutting edge of how privacy-preserving technology is applied. We will provide funding through the [RAAIS Foundation](https://www.raais.org/), a non-profit organization with a", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "mission to advance education and research in artificial intelligence for the common good. We encourage interested parties to apply to one or more of the fellowships listed below.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Tools Powering the Future of Privacy-Preserving ML\n\nThe next generation of privacy-preserving open source tools enable ML researchers to easily experiment with ML models using secure computing techniques without needing to be cryptography experts. By integrating with PyTorch, PySyft and CrypTen offer familiar environments for ML developers to research and apply these techniques as part of their work.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "**PySyft** is a Python library for secure and private ML developed by the OpenMined community. It is a flexible, easy-to-use library that makes secure computation techniques like [multi-party computation (MPC)](https://en.wikipedia.org/wiki/Secure_multi-party_computation) and privacy-preserving techniques like [differential privacy](https://en.wikipedia.org/wiki/Differential_privacy) accessible to the ML community. It prioritizes ease of use and focuses on integrating these techniques into end-user use", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "cases like federated learning with mobile phones and other edge devices, encrypted ML as a service, and privacy-preserving data science.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "**CrypTen** is a framework built on PyTorch that enables private and secure ML for the PyTorch community. It is the first step along the journey towards a privacy-preserving mode in PyTorch that will make secure computing techniques accessible beyond cryptography researchers. It currently implements [secure multiparty computation](https://en.wikipedia.org/wiki/Secure_multi-party_computation) with the goal of offering other secure computing backends in the near future. Other benefits to ML researchers", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "include:", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "* It is **ML first** and presents secure computing techniques via a CrypTensor object that looks and feels exactly like a PyTorch Tensor. This allows the user to use automatic differentiation and neural network modules akin to those in PyTorch.\n* The framework focuses on **scalability and performance** and is built with real-world challenges in mind.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "The focus areas for CrypTen and PySyft are naturally aligned and complement each other. The former focuses on building support for various secure and privacy preserving techniques on PyTorch through an encrypted tensor abstraction, while the latter focuses on end user use cases like deployment on edge devices and a user friendly data science platform.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Working together will enable PySyft to use CrypTen as a backend for encrypted tensors. This can lead to an increase in performance for PySyft and the adoption of CrypTen as a runtime by PySyft\u2019s userbase. In addition to this, PyTorch is also adding cryptography friendly features such as support for cryptographically secure random number generation. Over the long run, this allows each library to focus exclusively on its core competencies while enjoying the benefits of the synergistic relationship.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "New Funding for OpenMined Contributors\n\nWe are especially excited to announce that the PyTorch team has invested $250,000 to support OpenMined in furthering the development and proliferation of privacy-preserving ML. This gift will be facilitated via the [RAAIS Foundation](https://www.raais.org/) and will be available immediately to support paid fellowship grants for the OpenMined community.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "How to get involved\n\nThanks to the support from the PyTorch team, OpenMined is able to offer three different opportunities for you to participate in the project\u2019s development. Each of these fellowships furthers our shared mission to lower the barrier-to-entry for privacy-preserving ML and to create a more privacy-preserving world.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Core PySyft CrypTen Integration Fellowships", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "During these fellowships, we will integrate CrypTen as a supported backend for encrypted computation in PySyft. This will allow for the high-performance, secure multi-party computation capabilities of CrypTen to be used alongside other important tools in PySyft such as differential privacy and federated learning. For more information on the roadmap and how to apply for a paid fellowship, check out the project\u2019s [call for contributors](https://blog.openmined.org/openmined-pytorch-fellowship-crypten-project).", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Federated Learning on Mobile, Web, and IoT Devices", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "During these fellowships, we will be extending PyTorch with the ability to perform federated learning across mobile, web, and IoT devices. To this end, a PyTorch front-end will be able to coordinate across federated learning backends that run in Javascript, Kotlin, Swift, and Python. Furthermore, we will also extend PySyft with the ability to coordinate these backends using peer-to-peer connections, providing low latency and the ability to run secure aggregation as a part of the protocol. For more", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "information on the roadmap and how to apply for a paid fellowship, check out the project\u2019s [call for contributors](https://blog.openmined.org/announcing-the-pytorch-openmined-federated-learning-fellowships).", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Development Challenges", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "Over the coming months, we will issue regular open competitions for increasing the performance and security of the PySyft and PyGrid codebases. For performance-related challenges, contestants will compete (for a cash prize) to make a specific PySyft demo (such as federated learning) as fast as possible. For security-related challenges, contestants will compete to hack into a PyGrid server. The first to demonstrate their ability will win the cash bounty! For more information on the challenges and to sign up", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "to receive emails when each challenge is opened, [sign up here](http://blog.openmined.org/announcing-the-openmined-pytorch-development-challenges).", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "To apply, select one of the above projects and identify a role that matches your strengths!\n\nCheers,\n\nAndrew, Laurens, Joe, and Shubho", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'How Computational Graphs are Constructed in PyTorch'\nauthor: Preferred Networks\nfeatured-img: 'assets/images/augmented_computational_graph.png'\n---", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the previous [post](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/) we went over the theoretical foundations of automatic differentiation and reviewed the implementation in PyTorch. In this post, we will be showing the parts of PyTorch involved in creating the graph and executing it. In order to understand the following contents, please read @ezyang\u2019s wonderful [blog post](http://blog.ezyang.com/2019/05/pytorch-internals/) about PyTorch internals.\n\n# Autograd components", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "First of all, let\u2019s look at where the different components of autograd live:", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[tools/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/tools/autograd): Here we can find the definition of the derivatives as we saw in the previous post [derivatives.yaml](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/derivatives.yaml), several python scripts and a folder called [templates](https://github.com/pytorch/pytorch/tree/release/1.9/tools/autograd/templates). These scripts and the templates are used at building time to generate the C++ code for the derivatives as", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "specified in the yaml file. Also, the scripts here generate wrappers for the regular ATen functions so that the computational graph can be constructed.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[torch/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/torch/autograd): This folder is where the autograd components that can be used directly from python are located. In [function.py](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/function.py) we find the actual definition of `torch.autograd.Function`, a class used by users to write their own differentiable functions in python as per the documentation.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[functional.py](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/functional.py) holds components for functionally computing the jacobian vector product, hessian, and other gradient related computations of a given function.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The rest of the files have additional components such as gradient checkers, anomaly detection, and the autograd profiler.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[torch/csrc/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/torch/csrc/autograd): This is where the graph creation and execution-related code lives.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "All this code is written in C++, since it is a critical part that is required to be extremely performant. Here we have several files that implement the engine, metadata storage, and all the needed components. Alongside this, we have several files whose names start with `python_`, and their main responsibility is to allow python objects to be used in the autograd engine.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Graph Creation\n\n[Previously](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/), we described the creation of a computational graph. Now, we will see how PyTorch creates these graphs with references to the actual codebase.\n\n

\n\n
\nFigure 1: Example of an augmented computational graph\n

\n\nIt all starts when in our python code, where we request a tensor to require the gradient.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```py\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When the `required_grad` flag is set in tensor creation, c10 will [allocate](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/c10/core/TensorImpl.cpp#L382-L406) an `AutogradMeta` object that is used to hold the graph information.\n\n```c++\n\nvoid TensorImpl::set_requires_grad(bool requires_grad) {\n ...\n if (!autograd_meta_)\n autograd_meta_ = impl::GetAutogradMetaFactory()->make();\n autograd_meta_->set_requires_grad(requires_grad, this);\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The `AutogradMeta` object is defined in [torch/csrc/autograd/variable.h](https://github.com/pytorch/pytorch/blob/release/1.9/torch/csrc/autograd/variable.h#L190-L286) as follows:\n\n```c++\n\nstruct TORCH_API AutogradMeta : public c10::AutogradMetaInterface {\n std::string name_;\n\n Variable grad_;\n std::shared_ptr grad_fn_;\n std::weak_ptr grad_accumulator_;\n // other fields and methods\n ...\n};", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The most important fields in this structure are the computed gradient in `grad_` and a pointer to the function `grad_fn` that will be called by the engine to produce the actual gradient. Also, there is a gradient accumulator object that is used to add together all the different gradients where this tensor is involved as we will see in the graph execution.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Graphs, Nodes and Edges.\n\nNow, when we call a differentiable function that takes this tensor as an argument, the associated metadata will be populated. Let\u2019s suppose that we call a regular torch function that is implemented in ATen. Let it be the multiplication as in our previous blog post example. The resulting tensor has a field called `grad_fn` that is essentially a pointer to the function that will be used to compute the gradient of that operation.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```py\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> v = x[0] * x[1]\n>>> v\ntensor(0.3750, grad_fn=)", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here we see that the tensors\u2019 `grad_fn` has a `MulBackward0` value. This function is the same that was written in the [derivatives.yaml](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/tools/autograd/derivatives.yaml#L840-L843) file, and its C++ code was generated automatically by all the scripts in `tools/autograd`. It\u2019s auto-generated source code can be seen in `torch/csrc/autograd/generated/Functions.cpp`.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nvariable_list MulBackward0::apply(variable_list&& grads) {\n std::lock_guard lock(mutex_);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "IndexRangeGenerator gen;\n auto self_ix = gen.range(1);\n auto other_ix = gen.range(1);\n variable_list grad_inputs(gen.size());\n auto& grad = grads[0];\n auto self = self_.unpack();\n auto other = other_.unpack();\n bool any_grad_defined = any_variable_defined(grads);\n if (should_compute_output({ other_ix })) {\n auto grad_result = any_grad_defined ? (mul_tensor_backward(grad, self, other_scalar_type)) : Tensor();\n copy_range(grad_inputs, other_ix, grad_result);\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (should_compute_output({ self_ix })) {\n auto grad_result = any_grad_defined ? (mul_tensor_backward(grad, other, self_scalar_type)) : Tensor();\n copy_range(grad_inputs, self_ix, grad_result);\n }\n return grad_inputs;\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The `grad_fn` objects inherit from the [`TraceableFunction`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L535-L541) class, a descendant of `Node` with just a property set to enable tracing for debugging and optimization purposes. A graph by definition has nodes and edges, so these functions are indeed the nodes of the computational graph that are linked together by using `Edge` objects to enable the graph traversal later on.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The `Node` definition can be found in the [torch/csrc/autograd/function.h](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L50-L533) file.\n\n```c++\nstruct TORCH_API Node : std::enable_shared_from_this {\n ...\n /// Evaluates the function on the given inputs and returns the result of the\n /// function call.\n variable_list operator()(variable_list&& inputs) {\n ...\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "protected:\n /// Performs the `Node`'s actual operation.\n virtual variable_list apply(variable_list&& inputs) = 0;\n \u2026\n edge_list next_edges_;", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Essentially we see that it has an override of the `operator ()` that performs the call to the actual function, and a pure virtual function called `apply`. The automatically generated functions override this `apply` method as we saw in the `MulBackward0` example above. Finally, the node also has a list of edges to enable graph connectivity.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The [Edge](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/edge.h#L14-L39) object is used to link `Node`s together and its implementation is straightforward.\n\n```c++\nstruct Edge {\n ...\n /// The function this `Edge` points to.\n std::shared_ptr function;\n /// The identifier of a particular input to the function.\n uint32_t input_nr;\n};", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "It only requires a function pointer (the actual `grad_fn` objects that the edges link together), and an input number that acts as an id for the edge.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Linking nodes together\n\nWhen we invoke the product operation of two tensors, we enter into the realm of autogenerated code. All the scripts that we saw in `tools/autograd` fill a series of templates that wrap the differentiable functions in ATen. These functions have code to construct the backward graph during the forward pass.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The [gen_variable_type.py](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/gen_variable_type.py) script is in charge of writing all this wrapping code. This script is called from the [tools/autograd/gen_autograd.py](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/gen_autograd.py) during the pytorch build process and it will output the automatically generated function wrappers to `torch/csrc/autograd/generated/`.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Let\u2019s take a look at how the tensor multiplication generated function looks like. The code has been simplified, but it can be found in the `torch/csrc/autograd/generated/VariableType_4.cpp` file when compiling pytorch from source.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nat::Tensor mul_Tensor(c10::DispatchKeySet ks, const at::Tensor & self, const at::Tensor & other) {\n ...\n auto _any_requires_grad = compute_requires_grad( self, other );\n std::shared_ptr grad_fn;\n if (_any_requires_grad) {\n // Creates the link to the actual grad_fn and links the graph for backward traversal\n grad_fn = std::shared_ptr(new MulBackward0(), deleteNode);\n grad_fn->set_next_edges(collect_next_edges( self, other ));\n ...\n }\n \u2026", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Does the actual function call to ATen\n auto _tmp = ([&]() {\n at::AutoDispatchBelowADInplaceOrView guard;\n return at::redispatch::mul(ks & c10::after_autograd_keyset, self_, other_);\n })();", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "auto result = std::move(_tmp);\n if (grad_fn) {\n // Connects the result to the graph\n set_history(flatten_tensor_args( result ), grad_fn);\n }\n ...\n return result;\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Let\u2019s walk through the most important lines of this code.\nFirst of all, the `grad_fn` object is created with: ` grad_fn = std::shared_ptr(new MulBackward0(), deleteNode);`.\n\nAfter the `grad_fn` object is created, the edges used to link the nodes together are created by using the `grad_fn->set_next_edges(collect_next_edges( self, other ));` calls.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nstruct MakeNextFunctionList : IterArgs {\n edge_list next_edges;\n using IterArgs::operator();\n void operator()(const Variable& variable) {\n if (variable.defined()) {\n next_edges.push_back(impl::gradient_edge(variable));\n } else {\n next_edges.emplace_back();\n }\n }\n void operator()(const c10::optional& variable) {\n if (variable.has_value() && variable->defined()) {\n next_edges.push_back(impl::gradient_edge(*variable));", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "} else {\n next_edges.emplace_back();\n }\n }\n};", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "template \nedge_list collect_next_edges(Variables&&... variables) {\n detail::MakeNextFunctionList make;\n make.apply(std::forward(variables)...);\n return std::move(make.next_edges);\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* **Batch size** can directly affect the latency and the throughput. To better utilize the compute resources batch size needs to be increased. However, there is a tradeoff between latency and throughput. **Larger batch sizes** can **increase** the **throughput but results in a higher latency** as well. Batch size can be set in Torchserve in two ways, either through[ model config](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/configuration.md#config-model) in config.properties or while registering the model using [Management API](https://github.com/pytorch/serve/blob/c87bfec8916d340de5de5810b14a016049b0e395/docs/management_api.md#scale-workers). \n\nIn the next section, we are going to use Torchserve benchmark suite to decide the best combination of model optimization, hardware, workers, and batch size. \n\n## Animated Drawings Performance Tuning", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "To use the Torchserve benchmark suite, first we need to have an archived file, \u201c.mar\u201d file as discussed above, that contains the model, handler and all other artifacts to load and run inference. Animated Drawings uses Detectron2\u2019s implementation of Mask-RCNN for an object detection model. \n\n### How to run benchmark suite \n\nThe [Automated benchmark suite](https://github.com/pytorch/serve/tree/master/benchmarks#auto-benchmarking-with-apache-bench) in Torchserve let you benchmark multiple models with different setting including batch size and number of worker and finally generate a report for you. To get started:\n\n```\ngit clone https://github.com/pytorch/serve.git\n\ncd serve/benchmarks\n\npip install -r requirements-ab.txt\n\napt-get install apache2-utils\n```\n\nModel level settings can be configured in a yaml file similar to \n\n```yaml", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "```yaml\n\nModel_name:\n eager_mode:\n benchmark_engine: \"ab\"\n url: \"Path to .mar file\"\n workers:\n - 1\n - 4\n batch_delay: 100\n batch_size:\n - 1\n - 2\n - 4\n - 8\n requests: 10000\n concurrency: 10\n input: \"Path to model input\"\n backend_profiling: False\n exec_env: \"local\"\n processors:\n - \"cpu\"\n - \"gpus\": \"all\"\n\n```\n\nThis yaml file will be referenced in the [benchmark_config_template](https://github.com/pytorch/serve/blob/master/benchmarks/benchmark_config_template.yaml#L12).yaml file that includes other settings for generating reports, this can optionally work with AWS cloud watch for logs as well.\n\n```\npython benchmarks/auto_benchmark.py --input benchmark_config_template.yaml\n```", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Running the **benchmarks**, results will be written in \u201ccsv\u201d file that can be found in \u201c_ /tmp/benchmark/ab_report.csv_\u201d and full report \u201c/tmp/ts_benchmark/report.md\". It will include items such as Torchserve average latency, model P99 latency, throughput, number of concurrency, number of requests, handler time, and some other metrics. Here we focus on some of the important ones that we track to tune the performance which are, **concurrency**, **model P99** latency, **throughput**. We look at these numbers specifically in **combination** with **batch size**, the used **device, number of workers** and if any **model optimization** has been done.\n\n\nThe **latency SLA** for this model has been set to **100 ms,** this is real-time application and as we discussed earlier, latency is more of a concern and **throughput** ideally should be as high as possible while it does **not violate** the **latency SLA.**", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "Through searching the space, over different batch sizes (1-32), number of workers (1-16) and devices (CPU,GPU), we have run a set of experiments that summarized the best ones in the table below.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Device \n Concurrency \n # Requests\n #workers\n Batch size\n Payload/image\n Optimization \n Throughput \n Latency P99\n
CPU\n 10\n 1000\n 1\n 1\n small\n N/A\n 3.45\n 305.3 ms\n
CPU\n 1\n 1000\n 1\n 1\n small\n N/A\n 3.45\n 291.8 ms\n
GPU\n 10\n 1000\n 1\n 1\n small\n N/A\n 41.05\n 25.48 ms\n
GPU\n 1\n 1000\n 1\n 1\n small\n N/A\n 42.21\n 23.6 ms\n
GPU\n 10\n 1000\n 1\n 4\n small\n N/A\n 54.78\n 73.62 ms\n
GPU\n 10\n 1000\n 1\n 4\n small\n model.half()\n 78.62\n 50.69 ms\n
GPU\n 10\n 1000\n 1\n 8\n small\n model.half()\n 85.29\n 94.4 ms\n
", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "The latency of this model on CPU with all of the tried settings in terms of batch size, concurrency and number of workers did not meet the SLA, in fact ~13x higher.\n\n**Moving** the model serving **to GPU**, immediately could **improve** the **latency** ~**13x **from 305 ms down to 23.6 ms. \n\nOne of the **simplest** **optimizations** that we could do for the model was lowering its precision to **fp16**, it is one liner (**model.half()**) and could reduce the **model P99 latency **by **32%** and increase the throughput by almost the same amount.\n\nThere could be other optimization done by Torchscripting the model and using [optimize_for_inference](https://github.com/pytorch/pytorch/blob/master/torch/jit/_freeze.py#L168) or other tricks including onnx or tensorrt runtime optimizations which leverage aggressive fusions are out of the scope of this post. We will discuss model optimizations in a separate post.\n\nWe found both on CPU and GPU , setting **number of workers=1 **worked the best in this case.", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "* Moving the model to GPU, using **number of workers = 1**, and **batch size = 1** increased the **Throughput ~12x compared** to **CPU and latency ~13x.**\n* Moving the model to GPU, using **model.half()**, **number of workers = 1**, and **batch size = 8** yielded **best** results in terms of **Throughput** and tolerable latency. **Throughput** increased **~25x compared** to **CPU with latency still meeting the SLA (94.4ms).**\n \n_Note: if you are running the benchmark suite, make sure you are setting a proper `batch_delay` and set the concurrency of the request to a number proportional to your batch size. Concurrency here means the number of concurrent requests being sent to the server._\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nIn this post, we have discussed the considerations and knobs that Torchserve expose to tune the performance in production. We have discussed the Torchserve benchmark suite as a means to tune the performance and get insights on possible choices for model optimizations, hardware choice and cost in general. We used Animated Drawings app which uses Detectron2\u2019s Mask-RCNN model as a case-study to showcase the performance tuning with benchmark suite. \n\nFor more details on Performance tuning in Torchserve please refer to our documentation [here](https://github.com/pytorch/serve/blob/master/docs/performance_guide.md).\nAlso feel free to open a ticket on [Torchserve repo](https://github.com/pytorch/serve/issues) for any further questions and feedback. \n\n### Acknowledgement", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "### Acknowledgement\n\nWe would like to thank Somya Jain (Meta), Christopher Gustave (Meta) for their great support and guidance throughout many steps of this blog and providing insights to Sketch Animator workflow. Also, special thanks to[ Li Ning](https://www.linkedin.com/in/li-ning-7274604/) from AWS for the great efforts to make performance tuning much easier on Torchserve with automated benchmark suite.\n\n\n", "metadata": {"source": "https://pytorch.org/blog/torchserve-performance-tuning/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling Vision Model Training Platforms with PyTorch\"\nauthor: Vaibhav Aggarwal, Mannat Singh, Anjali Sridhar, Yanghao Li, Shoubhik Debnath, Ronghang Hu, Will Feng, Xinlei Chen, Tingting Markstrum, Diana Liskovich, Anupam Bhatnagar, Chay Ryali, Haoqi Fan, Tete Xiao, Min Xu, Rahul Iyer, Christoph Feichtenhofer, Ross Girshick, Piotr Dollar, Aaron Adcock, Wan-Yen Lo, CK Luk\nfeatured-img: \"/assets/images/scaling-vision-figure_1-solutions-to-the-challenges.png\"\n---\n\n*TL;DR: We demonstrate the use of PyTorch with FairScale\u2019s FullyShardedDataParallel (FSDP) API in writing large vision transformer models. We discuss our techniques for scaling and optimizing these models on a GPU cluster. The goal of this platform scaling effort is to enable research at scale. This blog does not discuss model accuracy, new model architectures, or new training recipes.*\n\n## 1. Introduction", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## 1. Introduction\n\nLatest vision research [1, 2] demonstrates model scaling as a promising research direction. In this project, we aim to enable our platforms to train massive vision transformer (ViT) [3] models. We present our work on scaling the largest trainable ViT from 1B to 120B parameters in FAIR vision platforms. We wrote ViT in PyTorch and leveraged its support for large-scale, distributed training on a GPU cluster.\n\nIn the rest of this blog, we will first discuss the main challenges, namely *scalability*, *optimization*, and *numerical stability*. Then we will discuss how we tackle them with techniques including *data and model parallelism*, *automatic mixed precision*, *kernel fusion*, and *bfloat16*. Finally, we present our results and conclude.\n\n## 2. Main Challenges\n\n### 2.1 Scalability", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### 2.1 Scalability\n\nThe key scalability challenge is to efficiently shard a model\u2019s operations and state across multiple GPUs. A 100B parameter model requires ~200GB of RAM just for parameters, assuming fp16 representation. So, it is impossible to fit the model on a single GPU (A100 has at most 80GB RAM). Therefore, we need some way to efficiently shard a model\u2019s data (input, parameters, activations, and optimizer state) across multiple GPUs.\n\nAnother aspect of this problem is to scale without significantly changing the training recipe. E.g. Certain representation learning recipes use a global batch size of up to 4096 beyond which we start to see accuracy degradation. We cannot scale to more than 4096 GPUs without using some form of tensor or pipeline parallelism.\n\n### 2.2 Optimization", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### 2.2 Optimization\n\nThe key optimization challenge is to maintain high GPU utilization even as we scale the number of model parameters and flops. When we scale models to teraflops and beyond, we start to hit major bottlenecks in our software stack that super-linearly increase training time and reduce accelerator utilization. We require hundreds or thousands of GPUs to run just a single experiment. Improvements in accelerator utilization can lead to significant reductions in cost and improve fleet utilization. It enables us to fund more projects and run more experiments in parallel.\n\n### 2.3 Numerical Stability", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The key stability challenge is to avoid numerical instability and divergence at large scale. We empirically observed in our experiments that the training instability gets severe and hard to deal with when we scale up model sizes, data, batch sizes, learning rate, etc. Vision Transformers particularly face training instability even at a lower parameter threshold. E.g., we find it challenging to train even ViT-H (with just 630M parameters) in mixed-precision mode without using strong data augmentation. We need to study the model properties and training recipes to make sure that the models train stably and converge.\n\n## 3. Our Solutions\n\n**Figure 1** depicts our solutions to each of the challenges.\n\n

\n\n

\n\n### 3.1 Addressing scaling challenges with data parallelism and model parallelism\n\nWe apply various forms of data and model parallelism to enable fitting very large models in GPU memory.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We use FairScale\u2019s *FullyShardedDataParallel (FSDP)* API [4], based on PyTorch, to shard parameters, gradients, and optimizer state across multiple GPUs, thereby reducing the memory footprint per GPU. This process consists of the following three steps:\n\n- Step 1: We wrapped the entire model in a single FSDP instance. This shards the model parameters at the end of a forward pass and gathers parameters at the beginning of a forward pass. This enabled us to scale ~3x from 1.5B to 4.5B parameters. \n\n- Step 2: We experimented with wrapping individual model layers in separate FSDP instances. This nested wrapping further reduced the memory footprint by sharding and gathering parameters of individual model layers instead of an entire model. The peak memory is then determined by an individually wrapped transformer block in GPU memory in this mode instead of the entire model.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "- Step 3: We used *activation-checkpoint* to reduce the memory consumption by activations. It saves the input tensors and discards the intermediate activation tensors during the forward pass. These are recomputed during the backward pass.\n\nIn addition, we experimented with model-parallelism techniques such as pipeline parallelism [5], which allow us to scale to more GPUs without increasing the batch size.\n\n### 3.2 Addressing optimization challenges with advanced AMP and kernel fusion\n\n#### Advanced AMP\n\nAutomatic Mixed Precision (AMP) [6] training refers to training models using a lower precision of bits than FP32 or the default but still maintaining accuracy. We experimented with three levels of AMP as described below:\n\n- AMP O1: This refers to training in mixed precision where weights are in FP32 and some operations are in FP16. With AMP O1, the ops that might impact accuracy remain in FP32 and are not autocasted to FP16.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "- AMP O2: This refers to training in mixed precision but with more weights and ops in FP16 than in O1. Weights do not implicitly remain in FP32 and are cast to FP16. A copy of the master weights is maintained in the FP32 precision that is used by the optimizer. If we want the normalization layer weights in FP32 then we need to explicitly use layer wrapping to ensure that.\n\n- Full FP16: This refers to training in full FP16 where weights and operations are in FP16. FP16 is challenging to enable for training due to convergence issues.\n\nWe found that AMP O2 with LayerNorm wrapping in FP32 leads to the best performance without sacrificing accuracy.\n\n#### Kernel Fusion\n\n- To reduce GPU kernel launch overhead and increase GPU work granularity, we experimented with kernel fusions, including fused dropout and fused layer-norm, using the [xformers library](https://github.com/facebookresearch/xformers) [7].\n\n### 3.3 Addressing stability challenges by studying ops numerical stability and training recipes", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "#### BFloat16 in general but with LayerNorm in FP32\n\nThe [bfloat16](https://cloud.google.com/tpu/docs/bfloat16) (BF16) [8] floating-point format provides the same dynamic range as FP32 with a memory footprint identical to FP16. We found that we could train models in the BF16 format using the same set of hyperparameters as in FP32, without special parameter tuning. Nevertheless, we found that we need to keep LayerNorm in FP32 mode in order for the training to converge.\n\n### 3.4 Final training recipe\n\nA summary of the final training recipe.", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "1. Wrap the outer model in an FSDP instance. Enable parameter sharding after the forward pass.\n2. Wrap individual ViT blocks with activation checkpointing, nested FSDP wrapping, and parameter flattening.\n3. Enable mixed precision mode (AMP O2) with bfloat16 representation. Maintain the optimizer state in FP32 precision to enhance numerical stability.\n4. Wrap normalization layers like LayerNorm in FP32 for better numerical stability.\n5. Maximize the Nvidia TensorCore utilization by keeping matrix dimensions to be multiple of 8. For More details check [Nvidia Tensor Core Performance Guide](https://developer.download.nvidia.com/video/gputechconf/gtc/2019/presentation/s9926-tensor-core-performance-the-ultimate-guide.pdf).\n\n## 4. Results", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## 4. Results\n\nIn this section, we show the scaling results of ViT on three types of tasks: (1) image classification, (2) object detection (3) video understanding. **Our key result is that we are able to train massive ViT backbones across these vision tasks after applying the discussed scaling and optimization techniques. This enables vision research at a much larger scale.** We trained the models to convergence to verify that we maintain the current baselines even with all the optimizations. A common trend in Figures 2, 3, 4 is that we are able to train up to 25B-param models with an epoch time of less than 4 hours on 128 A100 GPUs. The 60B and 120B models are relatively slower to train.\n\n**Figure 2** shows the *image-classification* scaling result. It plots the epoch time for training ViTs on ImageNet using 128 A100-80GB GPUs with different model sizes.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 2: Image-classification scaling result.\n

\n\n**Figure 3** shows the *object-detection* scaling result. It plots the epoch time for training [ViTDet](https://arxiv.org/abs/2203.16527) [9] with different ViT backbones on COCO using 128 A100-80GB GPUs.\n\n

\n\n

\n\n

\nFigure 3: Object-detection scaling result.\n

\n\n**Figure 4** shows the *video-understanding* scaling result. It plots the epoch time for training [MViTv2](https://arxiv.org/abs/2112.01526) [10] models on [Kinetics 400](https://www.deepmind.com/open-source/kinetics) [11] using 128 V100 (32 GB) GPUs in FP32.\n\n

\n\n

\n\n

\nFigure 4: Video-understanding scaling result.\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Figure 5** shows the optimization result with the ViT-H model in Figure 2 on 8 A100-40GB GPUs.\nThree versions are used: (1) the baseline uses PyTorch\u2019s DDP [12] with AMP O1, (2) FSDP + AMP-O2 + other optimizations, and (3) FSDP + FP16 + other optimizations. These optimizations altogether speed up the training by up to 2.2x.\n\n

\n\n

\n\n

\nFigure 5: Training speedups from various optimizations.\n

\n\n## 5. Concluding Remarks\n\nWe have demonstrated the use of PyTorch with FairScale\u2019s FullyShardedDataParallel (FSDP) API in writing large vision transformer models. We discuss our techniques for scaling and optimizing these models on a GPU cluster. We hope that this article can motivate others to develop large-scale ML models with PyTorch and its ecosystem.\n\n## References\n\n[1] [Masked Autoencoders Are Scalable Vision Learners](https://arxiv.org/abs/2111.06377)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[2] [Revisiting Weakly Supervised Pre-Training of Visual Perception Models](https://arxiv.org/abs/2201.08371)\n\n[3] [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929v2)\n\n[4] [fairscale.nn.FullyShardedDataParallel](https://fairscale.readthedocs.io/en/stable/api/nn/fsdp.html)\n\n[5] [Pipeline parallelism in PyTorch](https://pytorch.org/docs/stable/pipeline.html)\n\n[6] [Automatic Mixed Precision (AMP) in PyTorch](https://pytorch.org/docs/stable/amp.html#module-torch.amp)\n\n[7] [xformers](https://github.com/facebookresearch/xformers)\n\n[8] [The bfloat16 numerical format](https://cloud.google.com/tpu/docs/bfloat16)\n\n[9] [Exploring Plain Vision Transformer Backbones for Object Detection](https://arxiv.org/abs/2203.16527)\n\n[10] [MViTv2: Improved Multiscale Vision Transformers for Classification and Detection](https://arxiv.org/abs/2112.01526)\n\n[11] [https://www.deepmind.com/open-source/kinetics](https://www.deepmind.com/open-source/kinetics)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[12] [Getting Started with Distributed Data Parallel (DDP)](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/scaling-vision-model-training-platforms-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Ecosystem Day'\nauthor: Team PyTorch\n---\n\nWe\u2019re proud to announce our first PyTorch Ecosystem Day. The virtual, one-day event will focus completely on our Ecosystem and Industry PyTorch communities!\n\n\nPyTorch is a deep learning framework of choice for academics and companies, all thanks to its rich ecosystem of tools and strong community. As with our developers, our ecosystem partners play a pivotal role in the development and growth of the community.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} +{"page_content": "We will be hosting our first PyTorch Ecosystem Day, a virtual event designed for our ecosystem and industry communities to showcase their work and discover new opportunities to collaborate. \n \nPyTorch Ecosystem Day will be held on April 21, with both a morning and evening session, to ensure we reach our global community. Join us virtually for a day filled with discussions on new developments, trends, challenges, and best practices through keynotes, breakout sessions, and a unique networking opportunity hosted through Gather.Town . \n\n## Event Details\nApril 21, 2021 (Pacific Time)\nFully digital experience \n \n* Morning Session: (EMEA)\nOpening Talks - 8:00 am-9:00 am PT\nPoster Exhibition & Breakout Sessions - 9:00 am-12:00 pm PT \n\n* Evening Session (APAC/US)\nOpening Talks - 3:00 pm-4:00 pm PT\nPoster Exhibition & Breakout Sessions - 3:00 pm-6:00 pm PT \n\n* Networking - 9:00 am-7:00 pm PT", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} +{"page_content": "### There are two ways to participate in PyTorch Ecosystem Day:\n \n1. **Poster Exhibition** from the PyTorch ecosystem and industry communities covering a variety of topics. Posters are available for viewing throughout the duration of the event. To be part of the poster exhibition, please see below for submission details. If your poster is accepted, we highly recommend tending your poster during one of the morning or evening sessions or both!\n \n2. **Breakout Sessions** are 40-min sessions freely designed by the community. The breakouts can be talks, demos, tutorials or discussions. Note: you must have an accepted poster to apply for the breakout sessions.", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} +{"page_content": "Call for posters now open! [Submit your proposal](https://pytorchecosystemday.fbreg.com/posters) today! Please send us the **title** and **summary** of your projects, tools, and libraries that could benefit PyTorch researchers in academia and industry, application developers, and ML engineers for consideration. The focus must be on academic papers, machine learning research, or open-source projects. Please no sales pitches. **Deadline for submission is March 18, 2021.** \n\nVisit [pytorchecosystemday.fbreg.com](http://pytorchecosystemday.fbreg.com) for more information and we look forward to welcoming you to PyTorch Ecosystem Day on April 21st!", "metadata": {"source": "https://pytorch.org/blog/ecosystem_day_2021/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.5 released, new and updated APIs including C++ frontend API parity with Python'\nauthor: Team PyTorch\n---\n\n\nToday, we\u2019re announcing the availability of PyTorch 1.5, along with new and updated libraries. This release includes several major new API additions and improvements. PyTorch now includes a significant update to the C++ frontend, \u2018channels last\u2019 memory format for computer vision models, and a stable release of the distributed RPC framework used for model-parallel training. The release also has new APIs for autograd for hessians and jacobians, and an API that allows the creation of Custom C++ Classes that was inspired by pybind.\n\nYou can find the detailed release notes [here](https://github.com/pytorch/pytorch/releases).\n\n## C++ Frontend API (Stable)\n\nThe C++ frontend API is now at parity with Python, and the features overall have been moved to \u2018stable\u2019 (previously tagged as experimental). Some of the major highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "* Now with ~100% coverage and docs for C++ torch::nn module/functional, users can easily translate their model from Python API to C++ API, making the model authoring experience much smoother.\n* Optimizers in C++ had deviated from the Python equivalent: C++ optimizers can\u2019t take parameter groups as input while the Python ones can. Additionally, step function implementations were not exactly the same. With the 1.5 release, C++ optimizers will always behave the same as the Python equivalent.\n* The lack of tensor multi-dim indexing API in C++ is a well-known issue and had resulted in many posts in PyTorch Github issue tracker and forum. The previous workaround was to use a combination of `narrow` / `select` / `index_select` / `masked_select`, which was clunky and error-prone compared to the Python API\u2019s elegant `tensor[:, 0, ..., mask]` syntax. With the 1.5 release, users can use `tensor.index({Slice(), 0, \"...\", mask})` to achieve the same purpose.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "## \u2018Channels last\u2019 memory format for Computer Vision models (Experimental)\n\n\u2018Channels last\u2019 memory layout unlocks ability to use performance efficient convolution algorithms and hardware (NVIDIA\u2019s Tensor Cores, FBGEMM, QNNPACK). Additionally, it is designed to automatically propagate through the operators, which allows easy switching between memory layouts.\n\nLearn more [here](https://github.com/pytorch/pytorch/wiki/Writing-memory-format-aware-operators) on how to write memory format aware operators.\n\n## Custom C++ Classes (Experimental)\n\nThis release adds a new API, `torch::class_`, for binding custom C++ classes into TorchScript and Python simultaneously. This API is almost identical in syntax to [pybind11](https://pybind11.readthedocs.io/en/stable/). It allows users to expose their C++ class and its methods to the TorchScript type system and runtime system such that they can instantiate and manipulate arbitrary C++ objects from TorchScript and Python. An example C++ binding:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "```python\ntemplate \nstruct MyStackClass : torch::CustomClassHolder {\n std::vector stack_;\n MyStackClass(std::vector init) : stack_(std::move(init)) {}\n\n void push(T x) {\n stack_.push_back(x);\n }\n T pop() {\n auto val = stack_.back();\n stack_.pop_back();\n return val;\n }\n};\n\nstatic auto testStack =\n torch::class_>(\"myclasses\", \"MyStackClass\")\n .def(torch::init>())\n .def(\"push\", &MyStackClass::push)\n .def(\"pop\", &MyStackClass::pop)\n .def(\"size\", [](const c10::intrusive_ptr& self) {\n return self->stack_.size();\n });\n```\n\n Which exposes a class you can use in Python and TorchScript like so:\n\n```python\n@torch.jit.script\ndef do_stacks(s : torch.classes.myclasses.MyStackClass):\n s2 = torch.classes.myclasses.MyStackClass([\"hi\", \"mom\"])\n print(s2.pop()) # \"mom\"\n s2.push(\"foobar\")\n return s2 # [\"hi\", \"foobar\"]\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "You can try it out in the tutorial [here](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html).\n\n\n## Distributed RPC framework APIs (Now Stable)\n\nThe Distributed [RPC framework](https://pytorch.org/docs/stable/rpc.html) was launched as experimental in the 1.4 release and the proposal is to mark Distributed RPC framework as stable and no longer experimental. This work involves a lot of enhancements and bug fixes to make the distributed RPC framework more reliable and robust overall, as well as adding a couple of new features, including profiling support, using TorchScript functions in RPC, and several enhancements for ease of use. Below is an overview of the various APIs within the framework:\n\n### RPC API\nThe RPC API allows users to specify functions to run and objects to be instantiated on remote nodes. These functions are transparently recorded so that gradients can backpropagate through remote nodes using Distributed Autograd.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "### Distributed Autograd\nDistributed Autograd connects the autograd graph across several nodes and allows gradients to flow through during the backwards pass. Gradients are accumulated into a context (as opposed to the .grad field as with Autograd) and users must specify their model\u2019s forward pass under a with `dist_autograd.context()` manager in order to ensure that all RPC communication is recorded properly. Currently, only FAST mode is implemented (see [here](https://pytorch.org/docs/stable/rpc/distributed_autograd.html#distributed-autograd-design) for the difference between FAST and SMART modes).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "### Distributed Optimizer\nThe distributed optimizer creates RRefs to optimizers on each worker with parameters that require gradients, and then uses the RPC API to run the optimizer remotely. The user must collect all remote parameters and wrap them in an `RRef`, as this is required input to the distributed optimizer. The user must also specify the distributed autograd `context_id` so that the optimizer knows in which context to look for gradients.\n\nLearn more about distributed RPC framework APIs [here](https://pytorch.org/docs/stable/rpc.html).\n\n## New High level autograd API (Experimental)\n\nPyTorch 1.5 brings new functions including jacobian, hessian, jvp, vjp, hvp and vhp to the `torch.autograd.functional` submodule. This feature builds on the current API and allows the user to easily perform these functions.\n\nDetailed design discussion on GitHub can be found [here](https://github.com/pytorch/pytorch/issues/30632).\n\n## Python 2 no longer supported", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "Starting PyTorch 1.5.0, we will no longer support Python 2, specifically version 2.7. Going forward support for Python will be limited to Python 3, specifically Python 3.5, 3.6, 3.7 and 3.8 (first enabled in PyTorch 1.4.0).\n\n\n*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-5-released-with-new-and-updated-apis/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'OpenMined and PyTorch partner to launch fellowship funding for privacy-preserving ML community'\nauthor: Andrew Trask (OpenMined/U.Oxford), Shubho Sengupta, Laurens van der Maaten, Joe Spisak\nexcerpt: Many applications of machine learning (ML) pose a range of security and privacy challenges.\n---\n\n
\n \n
\n\nMany applications of machine learning (ML) pose a range of security and privacy challenges. In particular, users may not be willing or allowed to share their data, which prevents them from taking full advantage of ML platforms like PyTorch. To take the field of privacy-preserving ML (PPML) forward, OpenMined and PyTorch are announcing plans to jointly develop a combined platform to accelerate PPML research as well as new funding for fellowships.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "There are many techniques attempting to solve the problem of privacy in ML, each at various levels of maturity. These include (1) homomorphic encryption, (2) secure multi-party computation, (3) trusted execution environments, (4) on-device computation, (5) federated learning with secure aggregation, and (6) differential privacy. Additionally, a number of open source projects implementing these techniques were created with the goal of enabling research at the intersection of privacy, security, and ML. Among them, PySyft and CrypTen have taken an \u201cML-first\u201d approach by presenting an API that is familiar to the ML community, while masking the complexities of privacy and security protocols. We are excited to announce that these two projects are now collaborating closely to build a mature PPML ecosystem around PyTorch.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "Additionally, to bolster this ecosystem and take the field of privacy preserving ML forward, we are also calling for contributions and supporting research efforts on this combined platform by providing funding to support the OpenMined community and the researchers that contribute, build proofs of concepts and desire to be on the cutting edge of how privacy-preserving technology is applied. We will provide funding through the [RAAIS Foundation](https://www.raais.org/), a non-profit organization with a mission to advance education and research in artificial intelligence for the common good. We encourage interested parties to apply to one or more of the fellowships listed below.\n\n## Tools Powering the Future of Privacy-Preserving ML", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "The next generation of privacy-preserving open source tools enable ML researchers to easily experiment with ML models using secure computing techniques without needing to be cryptography experts. By integrating with PyTorch, PySyft and CrypTen offer familiar environments for ML developers to research and apply these techniques as part of their work.\n\n**PySyft** is a Python library for secure and private ML developed by the OpenMined community. It is a flexible, easy-to-use library that makes secure computation techniques like [multi-party computation (MPC)](https://en.wikipedia.org/wiki/Secure_multi-party_computation) and privacy-preserving techniques like [differential privacy](https://en.wikipedia.org/wiki/Differential_privacy) accessible to the ML community. It prioritizes ease of use and focuses on integrating these techniques into end-user use cases like federated learning with mobile phones and other edge devices, encrypted ML as a service, and privacy-preserving data science.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "**CrypTen** is a framework built on PyTorch that enables private and secure ML for the PyTorch community. It is the first step along the journey towards a privacy-preserving mode in PyTorch that will make secure computing techniques accessible beyond cryptography researchers. It currently implements [secure multiparty computation](https://en.wikipedia.org/wiki/Secure_multi-party_computation) with the goal of offering other secure computing backends in the near future. Other benefits to ML researchers include:\n\n* It is **ML first** and presents secure computing techniques via a CrypTensor object that looks and feels exactly like a PyTorch Tensor. This allows the user to use automatic differentiation and neural network modules akin to those in PyTorch.\n* The framework focuses on **scalability and performance** and is built with real-world challenges in mind.", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "The focus areas for CrypTen and PySyft are naturally aligned and complement each other. The former focuses on building support for various secure and privacy preserving techniques on PyTorch through an encrypted tensor abstraction, while the latter focuses on end user use cases like deployment on edge devices and a user friendly data science platform.\n\nWorking together will enable PySyft to use CrypTen as a backend for encrypted tensors. This can lead to an increase in performance for PySyft and the adoption of CrypTen as a runtime by PySyft\u2019s userbase. In addition to this, PyTorch is also adding cryptography friendly features such as support for cryptographically secure random number generation. Over the long run, this allows each library to focus exclusively on its core competencies while enjoying the benefits of the synergistic relationship.\n\n## New Funding for OpenMined Contributors", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "We are especially excited to announce that the PyTorch team has invested $250,000 to support OpenMined in furthering the development and proliferation of privacy-preserving ML. This gift will be facilitated via the [RAAIS Foundation](https://www.raais.org/) and will be available immediately to support paid fellowship grants for the OpenMined community.\n\n## How to get involved\n\nThanks to the support from the PyTorch team, OpenMined is able to offer three different opportunities for you to participate in the project\u2019s development. Each of these fellowships furthers our shared mission to lower the barrier-to-entry for privacy-preserving ML and to create a more privacy-preserving world.\n\n### Core PySyft CrypTen Integration Fellowships", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "During these fellowships, we will integrate CrypTen as a supported backend for encrypted computation in PySyft. This will allow for the high-performance, secure multi-party computation capabilities of CrypTen to be used alongside other important tools in PySyft such as differential privacy and federated learning. For more information on the roadmap and how to apply for a paid fellowship, check out the project\u2019s [call for contributors](https://blog.openmined.org/openmined-pytorch-fellowship-crypten-project).\n\n### Federated Learning on Mobile, Web, and IoT Devices", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "During these fellowships, we will be extending PyTorch with the ability to perform federated learning across mobile, web, and IoT devices. To this end, a PyTorch front-end will be able to coordinate across federated learning backends that run in Javascript, Kotlin, Swift, and Python. Furthermore, we will also extend PySyft with the ability to coordinate these backends using peer-to-peer connections, providing low latency and the ability to run secure aggregation as a part of the protocol. For more information on the roadmap and how to apply for a paid fellowship, check out the project\u2019s [call for contributors](https://blog.openmined.org/announcing-the-pytorch-openmined-federated-learning-fellowships).\n\n### Development Challenges", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "Over the coming months, we will issue regular open competitions for increasing the performance and security of the PySyft and PyGrid codebases. For performance-related challenges, contestants will compete (for a cash prize) to make a specific PySyft demo (such as federated learning) as fast as possible. For security-related challenges, contestants will compete to hack into a PyGrid server. The first to demonstrate their ability will win the cash bounty! For more information on the challenges and to sign up to receive emails when each challenge is opened, [sign up here](http://blog.openmined.org/announcing-the-openmined-pytorch-development-challenges).\n\nTo apply, select one of the above projects and identify a role that matches your strengths!\n\nCheers,\n\nAndrew, Laurens, Joe, and Shubho", "metadata": {"source": "https://pytorch.org/blog/openmined-and-pytorch-launch-fellowship-funding-for-privacy-preserving-ml/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'How Computational Graphs are Constructed in PyTorch'\nauthor: Preferred Networks\nfeatured-img: 'assets/images/augmented_computational_graph.png'\n---\n\nIn the previous [post](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/) we went over the theoretical foundations of automatic differentiation and reviewed the implementation in PyTorch. In this post, we will be showing the parts of PyTorch involved in creating the graph and executing it. In order to understand the following contents, please read @ezyang\u2019s wonderful [blog post](http://blog.ezyang.com/2019/05/pytorch-internals/) about PyTorch internals.\n\n# Autograd components\n\nFirst of all, let\u2019s look at where the different components of autograd live:", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[tools/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/tools/autograd): Here we can find the definition of the derivatives as we saw in the previous post [derivatives.yaml](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/derivatives.yaml), several python scripts and a folder called [templates](https://github.com/pytorch/pytorch/tree/release/1.9/tools/autograd/templates). These scripts and the templates are used at building time to generate the C++ code for the derivatives as specified in the yaml file. Also, the scripts here generate wrappers for the regular ATen functions so that the computational graph can be constructed.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[torch/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/torch/autograd): This folder is where the autograd components that can be used directly from python are located. In [function.py](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/function.py) we find the actual definition of `torch.autograd.Function`, a class used by users to write their own differentiable functions in python as per the documentation. [functional.py](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/functional.py) holds components for functionally computing the jacobian vector product, hessian, and other gradient related computations of a given function.\nThe rest of the files have additional components such as gradient checkers, anomaly detection, and the autograd profiler.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[torch/csrc/autograd](https://github.com/pytorch/pytorch/tree/release/1.9/torch/csrc/autograd): This is where the graph creation and execution-related code lives.\nAll this code is written in C++, since it is a critical part that is required to be extremely performant. Here we have several files that implement the engine, metadata storage, and all the needed components. Alongside this, we have several files whose names start with `python_`, and their main responsibility is to allow python objects to be used in the autograd engine.\n\n# Graph Creation\n\n[Previously](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/), we described the creation of a computational graph. Now, we will see how PyTorch creates these graphs with references to the actual codebase.\n\n

\n\n
\nFigure 1: Example of an augmented computational graph\n

", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "It all starts when in our python code, where we request a tensor to require the gradient.\n\n```py\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n```\n\nWhen the `required_grad` flag is set in tensor creation, c10 will [allocate](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/c10/core/TensorImpl.cpp#L382-L406) an `AutogradMeta` object that is used to hold the graph information.\n\n```c++\n\nvoid TensorImpl::set_requires_grad(bool requires_grad) {\n ...\n if (!autograd_meta_)\n autograd_meta_ = impl::GetAutogradMetaFactory()->make();\n autograd_meta_->set_requires_grad(requires_grad, this);\n}\n```\n\n\nThe `AutogradMeta` object is defined in [torch/csrc/autograd/variable.h](https://github.com/pytorch/pytorch/blob/release/1.9/torch/csrc/autograd/variable.h#L190-L286) as follows:\n\n```c++\n\nstruct TORCH_API AutogradMeta : public c10::AutogradMetaInterface {\n std::string name_;", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Variable grad_;\n std::shared_ptr grad_fn_;\n std::weak_ptr grad_accumulator_;\n // other fields and methods\n ...\n};\n```\n\nThe most important fields in this structure are the computed gradient in `grad_` and a pointer to the function `grad_fn` that will be called by the engine to produce the actual gradient. Also, there is a gradient accumulator object that is used to add together all the different gradients where this tensor is involved as we will see in the graph execution.\n\n### Graphs, Nodes and Edges.\n\nNow, when we call a differentiable function that takes this tensor as an argument, the associated metadata will be populated. Let\u2019s suppose that we call a regular torch function that is implemented in ATen. Let it be the multiplication as in our previous blog post example. The resulting tensor has a field called `grad_fn` that is essentially a pointer to the function that will be used to compute the gradient of that operation.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```py\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> v = x[0] * x[1]\n>>> v\ntensor(0.3750, grad_fn=)\n```\n\nHere we see that the tensors\u2019 `grad_fn` has a `MulBackward0` value. This function is the same that was written in the [derivatives.yaml](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/tools/autograd/derivatives.yaml#L840-L843) file, and its C++ code was generated automatically by all the scripts in `tools/autograd`. It\u2019s auto-generated source code can be seen in `torch/csrc/autograd/generated/Functions.cpp`.\n\n```c++\nvariable_list MulBackward0::apply(variable_list&& grads) {\n std::lock_guard lock(mutex_);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "IndexRangeGenerator gen;\n auto self_ix = gen.range(1);\n auto other_ix = gen.range(1);\n variable_list grad_inputs(gen.size());\n auto& grad = grads[0];\n auto self = self_.unpack();\n auto other = other_.unpack();\n bool any_grad_defined = any_variable_defined(grads);\n if (should_compute_output({ other_ix })) {\n auto grad_result = any_grad_defined ? (mul_tensor_backward(grad, self, other_scalar_type)) : Tensor();\n copy_range(grad_inputs, other_ix, grad_result);\n }\n if (should_compute_output({ self_ix })) {\n auto grad_result = any_grad_defined ? (mul_tensor_backward(grad, other, self_scalar_type)) : Tensor();\n copy_range(grad_inputs, self_ix, grad_result);\n }\n return grad_inputs;\n}\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The `grad_fn` objects inherit from the [`TraceableFunction`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L535-L541) class, a descendant of `Node` with just a property set to enable tracing for debugging and optimization purposes. A graph by definition has nodes and edges, so these functions are indeed the nodes of the computational graph that are linked together by using `Edge` objects to enable the graph traversal later on.\n\nThe `Node` definition can be found in the [torch/csrc/autograd/function.h](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L50-L533) file.\n\n```c++\nstruct TORCH_API Node : std::enable_shared_from_this {\n ...\n /// Evaluates the function on the given inputs and returns the result of the\n /// function call.\n variable_list operator()(variable_list&& inputs) {\n ...\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "protected:\n /// Performs the `Node`'s actual operation.\n virtual variable_list apply(variable_list&& inputs) = 0;\n \u2026\n edge_list next_edges_;\n```\n\nEssentially we see that it has an override of the `operator ()` that performs the call to the actual function, and a pure virtual function called `apply`. The automatically generated functions override this `apply` method as we saw in the `MulBackward0` example above. Finally, the node also has a list of edges to enable graph connectivity.\n\nThe [Edge](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/edge.h#L14-L39) object is used to link `Node`s together and its implementation is straightforward.\n\n```c++\nstruct Edge {\n ...\n /// The function this `Edge` points to.\n std::shared_ptr function;\n /// The identifier of a particular input to the function.\n uint32_t input_nr;\n};\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "It only requires a function pointer (the actual `grad_fn` objects that the edges link together), and an input number that acts as an id for the edge.\n\n### Linking nodes together\n\nWhen we invoke the product operation of two tensors, we enter into the realm of autogenerated code. All the scripts that we saw in `tools/autograd` fill a series of templates that wrap the differentiable functions in ATen. These functions have code to construct the backward graph during the forward pass.\n\nThe [gen_variable_type.py](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/gen_variable_type.py) script is in charge of writing all this wrapping code. This script is called from the [tools/autograd/gen_autograd.py](https://github.com/pytorch/pytorch/blob/release/1.9/tools/autograd/gen_autograd.py) during the pytorch build process and it will output the automatically generated function wrappers to `torch/csrc/autograd/generated/`.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Let\u2019s take a look at how the tensor multiplication generated function looks like. The code has been simplified, but it can be found in the `torch/csrc/autograd/generated/VariableType_4.cpp` file when compiling pytorch from source.\n\n```c++\nat::Tensor mul_Tensor(c10::DispatchKeySet ks, const at::Tensor & self, const at::Tensor & other) {\n ...\n auto _any_requires_grad = compute_requires_grad( self, other );\n std::shared_ptr grad_fn;\n if (_any_requires_grad) {\n // Creates the link to the actual grad_fn and links the graph for backward traversal\n grad_fn = std::shared_ptr(new MulBackward0(), deleteNode);\n grad_fn->set_next_edges(collect_next_edges( self, other ));\n ...\n }\n \u2026\n // Does the actual function call to ATen\n auto _tmp = ([&]() {\n at::AutoDispatchBelowADInplaceOrView guard;\n return at::redispatch::mul(ks & c10::after_autograd_keyset, self_, other_);\n })();", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "auto result = std::move(_tmp);\n if (grad_fn) {\n // Connects the result to the graph\n set_history(flatten_tensor_args( result ), grad_fn);\n }\n ...\n return result;\n}\n```\n\nLet\u2019s walk through the most important lines of this code.\nFirst of all, the `grad_fn` object is created with: ` grad_fn = std::shared_ptr(new MulBackward0(), deleteNode);`.\n\nAfter the `grad_fn` object is created, the edges used to link the nodes together are created by using the `grad_fn->set_next_edges(collect_next_edges( self, other ));` calls.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nstruct MakeNextFunctionList : IterArgs {\n edge_list next_edges;\n using IterArgs::operator();\n void operator()(const Variable& variable) {\n if (variable.defined()) {\n next_edges.push_back(impl::gradient_edge(variable));\n } else {\n next_edges.emplace_back();\n }\n }\n void operator()(const c10::optional& variable) {\n if (variable.has_value() && variable->defined()) {\n next_edges.push_back(impl::gradient_edge(*variable));\n } else {\n next_edges.emplace_back();\n }\n }\n};\n\ntemplate \nedge_list collect_next_edges(Variables&&... variables) {\n detail::MakeNextFunctionList make;\n make.apply(std::forward(variables)...);\n return std::move(make.next_edges);\n}\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} {"page_content": "Given an input variable (it\u2019s just a regular tensor), [`collect_next_edges`](\nhttps://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L597-L603)\n will create an `Edge` object by calling [`impl::gradient_edge`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/variable.cpp#L228-L240.)", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n Edge gradient_edge(const Variable& self) {\n // If grad_fn is null (as is the case for a leaf node), we instead\n // interpret the gradient function to be a gradient accumulator, which will\n // accumulate its inputs into the grad property of the variable. These\n // nodes get suppressed in some situations, see \"suppress gradient\n // accumulation\" below. Note that only variables which have `requires_grad =\n // True` can have gradient accumulators.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (const auto& gradient = self.grad_fn()) {\n return Edge(gradient, self.output_nr());\n } else {\n return Edge(grad_accumulator(self), 0);\n }\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "To understand how edges work, let\u2019s assume that an early executed function produced two output tensors, both with their `grad_fn` set, each tensor also has an `output_nr` property with the order in which they were returned. When creating the edges for the current `grad_fn`, an `Edge` object per input variable will be created. The edges will point to the variable\u2019s grad_fn and will also track the `output_nr` to establish ids used when traversing the graph. In the case that the input variables are \u201cleaf\u201d,", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "i.e. they were not produced by any differentiable function, they don\u2019t have a `grad_fn` attribute set. A special function called a gradient accumulator is set by default as seen in the above code snippet.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "After the edges are created, the `grad_fn` graph Node object that is being currently created will hold them using the [`set_next_edges`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L258-L263) function. This is what connects `grad_fn`s together, producing the computational graph.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n void set_next_edges(edge_list&& next_edges) {\n next_edges_ = std::move(next_edges);\n for(const auto& next_edge : next_edges_) {\n update_topological_nr(next_edge);\n }\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Now, the forward pass of the function will execute, and after the execution `set_history` will connect the output tensors to the `grad_fn` Node.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\ninline void set_history(\n at::Tensor& variable,\n const std::shared_ptr& grad_fn) {\n AT_ASSERT(grad_fn);\n if (variable.defined()) {\n // If the codegen triggers this, you most likely want to add your newly added function\n // to the DONT_REQUIRE_DERIVATIVE list in tools/autograd/gen_variable_type.py\n TORCH_INTERNAL_ASSERT(isDifferentiableType(variable.scalar_type()));\n auto output_nr =\n grad_fn->add_input_metadata(variable);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "impl::set_gradient_edge(variable, {grad_fn, output_nr});\n } else {\n grad_fn->add_input_metadata(Node::undefined_input());\n }\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`set_history`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/functions/utils.h#L58-L72) calls [`set_gradient_edge`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/variable.cpp#L242-L255), which just copies the grad_fn and the `output_nr` to the `AutogradMeta` object that the tensor has.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n void set_gradient_edge(const Variable& self, Edge edge) {\n auto* meta = materialize_autograd_meta(self);\n meta->grad_fn_ = std::move(edge.function);\n meta->output_nr_ = edge.input_nr;\n // For views, make sure this new grad_fn_ is not overwritten unless it is necessary\n // in the VariableHooks::grad_fn below.\n // This logic is only relevant for custom autograd Functions for which multiple\n // operations can happen on a given Tensor before its gradient edge is set when", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// exiting the custom Function.\n auto diff_view_meta = get_view_autograd_meta(self);\n if (diff_view_meta && diff_view_meta->has_bw_view()) {\n diff_view_meta->set_attr_version(self._version());\n }\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This tensor now will be the input to another function and the above steps will be all repeated. Check the animation below to see how the graph is created.\n\n

\n\n
\nFigure 2: Animation that shows the graph creation\n

", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Registering Python Functions in the graph\n\nWe have seen how autograd creates the graph for the functions included in ATen. However, when we define our differentiable functions in Python, they are also included in the graph!\n\nAn autograd python defined function looks like the following:\n\n```python\nclass Exp(torch.autograd.Function):\n @staticmethod\n def forward(ctx, i):\n result = i.exp()\n ctx.save_for_backward(result)\n return result", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "@staticmethod\n def backward(ctx, grad_output):\n result, = ctx.saved_tensors\n return grad_output * result\n\n# Call the function\nExp.apply(torch.tensor(0.5, requires_grad=True))\n# Outputs: tensor(1.6487, grad_fn=)", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the above snippet autograd detected our python function when creating the graph. All of this is possible thanks to the [`Function`](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/function.py#L106) class. Let\u2019s take a look at what happens when we call `apply`.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`apply` is defined in the [`torch._C._FunctionBase`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L859-L908) class, but this class is not present in the python source. `_FunctionBase` is defined in C++ by using the python C API to hook C functions together into a single python class. We are looking for a function named", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`THPFunction_apply`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L577-L633).", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n\nPyObject *THPFunction_apply(PyObject *cls, PyObject *inputs)\n{\n \n // Generates the graph node\n THPObjectPtr backward_cls(PyObject_GetAttrString(cls, \"_backward_cls\"));\n if (!backward_cls) return nullptr;\n THPObjectPtr ctx_obj(PyObject_CallFunctionObjArgs(backward_cls, nullptr));\n if (!ctx_obj) return nullptr;\n THPFunction* ctx = (THPFunction*)ctx_obj.get();\n\n auto cdata = std::shared_ptr(new PyNode(std::move(ctx_obj)), deleteNode);\n ctx->cdata = cdata;", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Prepare inputs and allocate context (grad fn)\n // Unpack inputs will collect the edges\n auto info_pair = unpack_input(inputs);\n UnpackedInput& unpacked_input = info_pair.first;\n InputFlags& input_info = info_pair.second;", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Initialize backward function (and ctx)\n bool is_executable = input_info.is_executable;\n cdata->set_next_edges(std::move(input_info.next_edges));\n ctx->needs_input_grad = input_info.needs_input_grad.release();\n ctx->is_variable_input = std::move(input_info.is_variable_input);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Prepend ctx to input_tuple, in preparation for static method call\n auto num_args = PyTuple_GET_SIZE(inputs);\n THPObjectPtr ctx_input_tuple(PyTuple_New(num_args + 1));\n if (!ctx_input_tuple) return nullptr;\n Py_INCREF(ctx);\n PyTuple_SET_ITEM(ctx_input_tuple.get(), 0, (PyObject*)ctx);\n for (int i = 0; i < num_args; ++i) {\n PyObject *arg = PyTuple_GET_ITEM(unpacked_input.input_tuple.get(), i);\n Py_INCREF(arg);\n PyTuple_SET_ITEM(ctx_input_tuple.get(), i + 1, arg);\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Call forward\n THPObjectPtr tensor_outputs;\n {\n AutoGradMode grad_mode(false);\n THPObjectPtr forward_fn(PyObject_GetAttrString(cls, \"forward\"));\n if (!forward_fn) return nullptr;\n tensor_outputs = PyObject_CallObject(forward_fn, ctx_input_tuple);\n if (!tensor_outputs) return nullptr;\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Here is where the outputs gets the tensors tracked\n return process_outputs(cls, cdata, ctx, unpacked_input, inputs, std::move(tensor_outputs),\n is_executable, node);\n END_HANDLE_TH_ERRORS\n}\n```\n \nAlthough this code is hard to read at first due to all the python API calls, it essentially does the same thing as the auto-generated forward functions that we saw for ATen:", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Create a `grad_fn` object.\nCollect the edges to link the current `grad_fn` with the input tensors one.\nExecute the function `forward`.\nAssign the created `grad_fn` to the output tensors metadata.\n\nThe `grad_fn` object is created in:", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n // Generates the graph node\n THPObjectPtr backward_cls(PyObject_GetAttrString(cls, \"_backward_cls\"));\n if (!backward_cls) return nullptr;\n THPObjectPtr ctx_obj(PyObject_CallFunctionObjArgs(backward_cls, nullptr));\n if (!ctx_obj) return nullptr;\n THPFunction* ctx = (THPFunction*)ctx_obj.get();\n\n auto cdata = std::shared_ptr(new PyNode(std::move(ctx_obj)), deleteNode);\n ctx->cdata = cdata;", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Basically, it asks the python API to get a pointer to the Python object that can execute the user-written function. Then it wraps it into a [`PyNode`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.h#L24-L58) object that is a specialized `Node` object that calls the python interpreter with the provided python function when `apply` is executed during the forward pass. Note that in the code `cdata` is the actual `Node` object that is part", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "of the graph. `ctx` is the object that is passed to the python `forward`/`backward` functions and it is used to store autograd related information by both, the user\u2019s function and PyTorch.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "As in the regular C++ functions we also call `collect_next_edges` to track the inputs `grad_fn` objects, but this is done in [`unpack_input`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L413-L448):", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\ntemplate\nstd::pair unpack_input(PyObject *args) {\n ...\n flags.next_edges = (flags.is_executable ? collect_next_edges(unpacked.input_vars) : edge_list());\n return std::make_pair(std::move(unpacked), std::move(flags));\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "After this, the edges are assigned to the `grad_fn` by just doing `cdata->set_next_edges(std::move(input_info.next_edges));` and the forward function is called through the python interpreter C API.\n\nOnce the output tensors are returned from the forward pass, they are processed and converted to variables inside the [`process_outputs`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L519-L562) function.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nPyObject* process_outputs(PyObject *op_obj, const std::shared_ptr& cdata,\n THPFunction* grad_fn, const UnpackedInput& unpacked,\n PyObject *inputs, THPObjectPtr&& raw_output, bool is_executable,\n torch::jit::Node* node) {\n ...\n _wrap_outputs(cdata, grad_fn, unpacked.input_vars, raw_output, outputs, is_executable);\n _trace_post_record(node, op_obj, unpacked.input_vars, outputs, is_inplace, unpack_output);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (is_executable) {\n _save_variables(cdata, grad_fn);\n } ...\n return outputs.release();\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here, [`_wrap_outputs`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L302-L346) is in charge of setting the forward outputs `grad_fn` to the newly created one. For this, it calls another `_wrap_outputs` function defined in a different [file](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/custom_function.cpp#L28-L105), so the process here gets a little confusing.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nstatic void _wrap_outputs(const std::shared_ptr& cdata, THPFunction *self,\n const variable_list &input_vars, PyObject *raw_output, PyObject *outputs, bool is_executable)\n{\n auto cdata_if_executable = is_executable ? cdata : nullptr;\n ...\n\n // Wrap only the tensor outputs.\n // This calls csrc/autograd/custom_function.cpp\n auto wrapped_outputs = _wrap_outputs(input_vars, non_differentiable, dirty_inputs, raw_output_vars, cdata_if_executable);\n...\n}", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n Edge gradient_edge(const Variable& self) {\n // If grad_fn is null (as is the case for a leaf node), we instead\n // interpret the gradient function to be a gradient accumulator, which will\n // accumulate its inputs into the grad property of the variable. These\n // nodes get suppressed in some situations, see \"suppress gradient\n // accumulation\" below. Note that only variables which have `requires_grad =\n // True` can have gradient accumulators.\n if (const auto& gradient = self.grad_fn()) {\n return Edge(gradient, self.output_nr());\n } else {\n return Edge(grad_accumulator(self), 0);\n }\n }\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "To understand how edges work, let\u2019s assume that an early executed function produced two output tensors, both with their `grad_fn` set, each tensor also has an `output_nr` property with the order in which they were returned. When creating the edges for the current `grad_fn`, an `Edge` object per input variable will be created. The edges will point to the variable\u2019s grad_fn and will also track the `output_nr` to establish ids used when traversing the graph. In the case that the input variables are \u201cleaf\u201d, i.e. they were not produced by any differentiable function, they don\u2019t have a `grad_fn` attribute set. A special function called a gradient accumulator is set by default as seen in the above code snippet.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "After the edges are created, the `grad_fn` graph Node object that is being currently created will hold them using the [`set_next_edges`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/function.h#L258-L263) function. This is what connects `grad_fn`s together, producing the computational graph.\n\n```c++\n void set_next_edges(edge_list&& next_edges) {\n next_edges_ = std::move(next_edges);\n for(const auto& next_edge : next_edges_) {\n update_topological_nr(next_edge);\n }\n }\n```\n\nNow, the forward pass of the function will execute, and after the execution `set_history` will connect the output tensors to the `grad_fn` Node.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\ninline void set_history(\n at::Tensor& variable,\n const std::shared_ptr& grad_fn) {\n AT_ASSERT(grad_fn);\n if (variable.defined()) {\n // If the codegen triggers this, you most likely want to add your newly added function\n // to the DONT_REQUIRE_DERIVATIVE list in tools/autograd/gen_variable_type.py\n TORCH_INTERNAL_ASSERT(isDifferentiableType(variable.scalar_type()));\n auto output_nr =\n grad_fn->add_input_metadata(variable);\n impl::set_gradient_edge(variable, {grad_fn, output_nr});\n } else {\n grad_fn->add_input_metadata(Node::undefined_input());\n }\n}\n```\n\n[`set_history`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/functions/utils.h#L58-L72) calls [`set_gradient_edge`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/variable.cpp#L242-L255), which just copies the grad_fn and the `output_nr` to the `AutogradMeta` object that the tensor has.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n void set_gradient_edge(const Variable& self, Edge edge) {\n auto* meta = materialize_autograd_meta(self);\n meta->grad_fn_ = std::move(edge.function);\n meta->output_nr_ = edge.input_nr;\n // For views, make sure this new grad_fn_ is not overwritten unless it is necessary\n // in the VariableHooks::grad_fn below.\n // This logic is only relevant for custom autograd Functions for which multiple\n // operations can happen on a given Tensor before its gradient edge is set when\n // exiting the custom Function.\n auto diff_view_meta = get_view_autograd_meta(self);\n if (diff_view_meta && diff_view_meta->has_bw_view()) {\n diff_view_meta->set_attr_version(self._version());\n }\n }\n```\n\nThis tensor now will be the input to another function and the above steps will be all repeated. Check the animation below to see how the graph is created.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n\n
\nFigure 2: Animation that shows the graph creation\n

\n\n### Registering Python Functions in the graph\n\nWe have seen how autograd creates the graph for the functions included in ATen. However, when we define our differentiable functions in Python, they are also included in the graph!\n\nAn autograd python defined function looks like the following:\n\n```python\nclass Exp(torch.autograd.Function):\n @staticmethod\n def forward(ctx, i):\n result = i.exp()\n ctx.save_for_backward(result)\n return result\n\n @staticmethod\n def backward(ctx, grad_output):\n result, = ctx.saved_tensors\n return grad_output * result\n\n# Call the function\nExp.apply(torch.tensor(0.5, requires_grad=True))\n# Outputs: tensor(1.6487, grad_fn=)\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In the above snippet autograd detected our python function when creating the graph. All of this is possible thanks to the [`Function`](https://github.com/pytorch/pytorch/blob/release/1.9/torch/autograd/function.py#L106) class. Let\u2019s take a look at what happens when we call `apply`.\n\n`apply` is defined in the [`torch._C._FunctionBase`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L859-L908) class, but this class is not present in the python source. `_FunctionBase` is defined in C++ by using the python C API to hook C functions together into a single python class. We are looking for a function named [`THPFunction_apply`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L577-L633). \n\n```c++", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n\nPyObject *THPFunction_apply(PyObject *cls, PyObject *inputs)\n{\n \n // Generates the graph node\n THPObjectPtr backward_cls(PyObject_GetAttrString(cls, \"_backward_cls\"));\n if (!backward_cls) return nullptr;\n THPObjectPtr ctx_obj(PyObject_CallFunctionObjArgs(backward_cls, nullptr));\n if (!ctx_obj) return nullptr;\n THPFunction* ctx = (THPFunction*)ctx_obj.get();\n\n auto cdata = std::shared_ptr(new PyNode(std::move(ctx_obj)), deleteNode);\n ctx->cdata = cdata;\n\n // Prepare inputs and allocate context (grad fn)\n // Unpack inputs will collect the edges\n auto info_pair = unpack_input(inputs);\n UnpackedInput& unpacked_input = info_pair.first;\n InputFlags& input_info = info_pair.second;\n\n // Initialize backward function (and ctx)\n bool is_executable = input_info.is_executable;\n cdata->set_next_edges(std::move(input_info.next_edges));\n ctx->needs_input_grad = input_info.needs_input_grad.release();\n ctx->is_variable_input = std::move(input_info.is_variable_input);", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "// Prepend ctx to input_tuple, in preparation for static method call\n auto num_args = PyTuple_GET_SIZE(inputs);\n THPObjectPtr ctx_input_tuple(PyTuple_New(num_args + 1));\n if (!ctx_input_tuple) return nullptr;\n Py_INCREF(ctx);\n PyTuple_SET_ITEM(ctx_input_tuple.get(), 0, (PyObject*)ctx);\n for (int i = 0; i < num_args; ++i) {\n PyObject *arg = PyTuple_GET_ITEM(unpacked_input.input_tuple.get(), i);\n Py_INCREF(arg);\n PyTuple_SET_ITEM(ctx_input_tuple.get(), i + 1, arg);\n }\n\n // Call forward\n THPObjectPtr tensor_outputs;\n {\n AutoGradMode grad_mode(false);\n THPObjectPtr forward_fn(PyObject_GetAttrString(cls, \"forward\"));\n if (!forward_fn) return nullptr;\n tensor_outputs = PyObject_CallObject(forward_fn, ctx_input_tuple);\n if (!tensor_outputs) return nullptr;\n }", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "// Here is where the outputs gets the tensors tracked\n return process_outputs(cls, cdata, ctx, unpacked_input, inputs, std::move(tensor_outputs),\n is_executable, node);\n END_HANDLE_TH_ERRORS\n}\n```\n \nAlthough this code is hard to read at first due to all the python API calls, it essentially does the same thing as the auto-generated forward functions that we saw for ATen:\n\nCreate a `grad_fn` object.\nCollect the edges to link the current `grad_fn` with the input tensors one.\nExecute the function `forward`.\nAssign the created `grad_fn` to the output tensors metadata.\n\nThe `grad_fn` object is created in:\n\n```c++\n // Generates the graph node\n THPObjectPtr backward_cls(PyObject_GetAttrString(cls, \"_backward_cls\"));\n if (!backward_cls) return nullptr;\n THPObjectPtr ctx_obj(PyObject_CallFunctionObjArgs(backward_cls, nullptr));\n if (!ctx_obj) return nullptr;\n THPFunction* ctx = (THPFunction*)ctx_obj.get();", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "auto cdata = std::shared_ptr(new PyNode(std::move(ctx_obj)), deleteNode);\n ctx->cdata = cdata;\n```\n\nBasically, it asks the python API to get a pointer to the Python object that can execute the user-written function. Then it wraps it into a [`PyNode`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.h#L24-L58) object that is a specialized `Node` object that calls the python interpreter with the provided python function when `apply` is executed during the forward pass. Note that in the code `cdata` is the actual `Node` object that is part of the graph. `ctx` is the object that is passed to the python `forward`/`backward` functions and it is used to store autograd related information by both, the user\u2019s function and PyTorch.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "As in the regular C++ functions we also call `collect_next_edges` to track the inputs `grad_fn` objects, but this is done in [`unpack_input`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L413-L448):\n\n```c++\ntemplate\nstd::pair unpack_input(PyObject *args) {\n ...\n flags.next_edges = (flags.is_executable ? collect_next_edges(unpacked.input_vars) : edge_list());\n return std::make_pair(std::move(unpacked), std::move(flags));\n}\n```\n\nAfter this, the edges are assigned to the `grad_fn` by just doing `cdata->set_next_edges(std::move(input_info.next_edges));` and the forward function is called through the python interpreter C API.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Once the output tensors are returned from the forward pass, they are processed and converted to variables inside the [`process_outputs`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L519-L562) function.\n\n```c++\nPyObject* process_outputs(PyObject *op_obj, const std::shared_ptr& cdata,\n THPFunction* grad_fn, const UnpackedInput& unpacked,\n PyObject *inputs, THPObjectPtr&& raw_output, bool is_executable,\n torch::jit::Node* node) {\n ...\n _wrap_outputs(cdata, grad_fn, unpacked.input_vars, raw_output, outputs, is_executable);\n _trace_post_record(node, op_obj, unpacked.input_vars, outputs, is_inplace, unpack_output);\n if (is_executable) {\n _save_variables(cdata, grad_fn);\n } ...\n return outputs.release();\n}\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Here, [`_wrap_outputs`](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/python_function.cpp#L302-L346) is in charge of setting the forward outputs `grad_fn` to the newly created one. For this, it calls another `_wrap_outputs` function defined in a different [file](https://github.com/pytorch/pytorch/blob/e7cd59c7a061c78d8d0265e4308b5933e44f9176/torch/csrc/autograd/custom_function.cpp#L28-L105), so the process here gets a little confusing.\n\n```c++\nstatic void _wrap_outputs(const std::shared_ptr& cdata, THPFunction *self,\n const variable_list &input_vars, PyObject *raw_output, PyObject *outputs, bool is_executable)\n{\n auto cdata_if_executable = is_executable ? cdata : nullptr;\n ...\n\n // Wrap only the tensor outputs.\n // This calls csrc/autograd/custom_function.cpp\n auto wrapped_outputs = _wrap_outputs(input_vars, non_differentiable, dirty_inputs, raw_output_vars, cdata_if_executable);\n...\n}\n```", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} {"page_content": "The called `_wrap_outputs` is the one in charge of setting the autograd metadata in the output tensors:\n\n```c++\nstd::vector> _wrap_outputs(const variable_list &input_vars,\n const std::unordered_set &non_differentiable,\n const std::unordered_set &dirty_inputs,\n const at::ArrayRef> raw_outputs,\n const std::shared_ptr &cdata) {", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "std::unordered_set inputs;\n \u2026\n // Sets the grad_fn and output_nr of an output Variable.\n auto set_history = [&](Variable& var, uint32_t output_nr, bool is_input, bool is_modified,\n bool is_differentiable) {\n // Lots of checks\n if (!is_differentiable) {\n ...\n } else if (is_input) {\n // An input has been returned, but it wasn't modified. Return it as a view\n // so that we can attach a new grad_fn to the Variable.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Run in no_grad mode to mimic the behavior of the forward.\n {\n AutoGradMode grad_mode(false);\n var = var.view_as(var);\n }\n impl::set_gradient_edge(var, {cdata, output_nr});\n } else if (cdata) {\n impl::set_gradient_edge(var, {cdata, output_nr});\n }\n };", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "And this is where `set_gradient_edge` was called and this is how a user-written python function gets included in the computational graph with its associated backward function!\n\n# Closing remarks\n\nThis blog post is intended to be a code overview on how PyTorch constructs the actual computational graphs that we discussed in the previous post. The next entry will deal with how the autograd engine executes these graphs.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Efficient Multi-Objective Neural Architecture Search with Ax\"\nauthor: David Eriksson, Max Balandat\nfeatured-img: \"/assets/images/MOO-NAS-blog-img2-pareto_frontier_plot.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "tl;dr\n\nMulti-Objective Optimization in Ax enables efficient exploration of tradeoffs (e.g. between model performance and model size or latency) in Neural Architecture Search. This method has been successfully applied at Meta for a variety of products such as On-Device AI. In this post, we provide an [end-to-end](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html) tutorial that allows you to try it out yourself.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\nNeural networks continue to grow in both size and complexity. Developing state-of-the-art architectures is often a cumbersome and time-consuming process that requires both domain expertise and large engineering efforts. In an attempt to overcome these challenges, several Neural Architecture Search (NAS) approaches have been proposed to automatically design well-performing architectures without requiring a human in-the-loop.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Despite being very sample-inefficient, na\u00efve approaches like random search and grid search are still popular for both hyperparameter optimization and NAS (a [study](https://hal.archives-ouvertes.fr/hal-02447823/document) conducted at NeurIPS 2019 and ICLR 2020 found that 80% of NeurIPS papers and 88% of ICLR papers tuned their ML model hyperparameters using manual tuning, random search, or grid search). But as models are often time-consuming to train and may require large amounts of computational resources,", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "minimizing the number of configurations that are evaluated is important.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "std::unordered_set inputs;\n \u2026\n // Sets the grad_fn and output_nr of an output Variable.\n auto set_history = [&](Variable& var, uint32_t output_nr, bool is_input, bool is_modified,\n bool is_differentiable) {\n // Lots of checks\n if (!is_differentiable) {\n ...\n } else if (is_input) {\n // An input has been returned, but it wasn't modified. Return it as a view\n // so that we can attach a new grad_fn to the Variable.\n // Run in no_grad mode to mimic the behavior of the forward.\n {\n AutoGradMode grad_mode(false);\n var = var.view_as(var);\n }\n impl::set_gradient_edge(var, {cdata, output_nr});\n } else if (cdata) {\n impl::set_gradient_edge(var, {cdata, output_nr});\n }\n };\n```\n\nAnd this is where `set_gradient_edge` was called and this is how a user-written python function gets included in the computational graph with its associated backward function!\n\n# Closing remarks", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "# Closing remarks\n\nThis blog post is intended to be a code overview on how PyTorch constructs the actual computational graphs that we discussed in the previous post. The next entry will deal with how the autograd engine executes these graphs.", "metadata": {"source": "https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Efficient Multi-Objective Neural Architecture Search with Ax\"\nauthor: David Eriksson, Max Balandat\nfeatured-img: \"/assets/images/MOO-NAS-blog-img2-pareto_frontier_plot.png\"\n---\n\n## tl;dr\n\nMulti-Objective Optimization in Ax enables efficient exploration of tradeoffs (e.g. between model performance and model size or latency) in Neural Architecture Search. This method has been successfully applied at Meta for a variety of products such as On-Device AI. In this post, we provide an [end-to-end](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html) tutorial that allows you to try it out yourself.\n\n## Introduction", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "## Introduction\n\nNeural networks continue to grow in both size and complexity. Developing state-of-the-art architectures is often a cumbersome and time-consuming process that requires both domain expertise and large engineering efforts. In an attempt to overcome these challenges, several Neural Architecture Search (NAS) approaches have been proposed to automatically design well-performing architectures without requiring a human in-the-loop.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "Despite being very sample-inefficient, na\u00efve approaches like random search and grid search are still popular for both hyperparameter optimization and NAS (a [study](https://hal.archives-ouvertes.fr/hal-02447823/document) conducted at NeurIPS 2019 and ICLR 2020 found that 80% of NeurIPS papers and 88% of ICLR papers tuned their ML model hyperparameters using manual tuning, random search, or grid search). But as models are often time-consuming to train and may require large amounts of computational resources, minimizing the number of configurations that are evaluated is important.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} {"page_content": "[Ax](https://ax.dev/) is a general tool for black-box optimization that allows users to explore large search spaces in a sample-efficient manner using [state-of-the art algorithms such as Bayesian Optimization](http://proceedings.mlr.press/v133/turner21a/turner21a.pdf). At Meta, Ax is used in a variety of domains, including hyperparameter tuning, NAS, identifying optimal product settings through large-scale A/B testing, infrastructure optimization, and designing cutting-edge AR/VR hardware.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "In many NAS applications, there is a natural tradeoff between multiple metrics of interest. For instance, when deploying models on-device we may want to maximize model performance (e.g., accuracy), while simultaneously minimizing competing metrics such as power consumption, inference latency, or model size, in order to satisfy deployment constraints. In many cases, we have been able to reduce computational requirements or latency of predictions substantially by accepting a small degradation in model", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "performance (in some cases we were able to both increase accuracy and reduce latency!). Principled methods for exploring such tradeoffs efficiently are key enablers of [Sustainable AI](https://arxiv.org/abs/2111.00364).", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "At Meta, we have successfully used [multi-objective Bayesian NAS](https://research.facebook.com/blog/2021/07/optimizing-model-accuracy-and-latency-using-bayesian-multi-objective-neural-architecture-search/) in Ax to explore such tradeoffs. Our methodology is being used routinely for optimizing AR/VR on-device ML models. Beyond NAS applications, we have also developed [MORBO](https://arxiv.org/pdf/2109.10964.pdf) which is a method for high-dimensional multi-objective optimization that can be used to optimize", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "optical systems for augmented reality (AR).", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Fully automated Multi-Objective NAS with Ax\n\nAx\u2019s Scheduler allows running experiments asynchronously in a closed-loop fashion by continuously deploying trials to an external system, polling for results, leveraging the fetched data to generate more trials, and repeating the process until a stopping condition is met. No human intervention or oversight is required. Features of the Scheduler include:\n\n- Customizability of parallelism, failure tolerance, and many other settings;", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "- A large selection of state-of-the-art optimization algorithms;\n\n- Saving in-progress experiments (to a SQL DB or json) and resuming an experiment from storage;\n\n- Easy extensibility to new backends for running trial evaluations remotely.\n\nThe following illustration from the [Ax scheduler tutorial](https://ax.dev/tutorials/scheduler.html) summarizes how the scheduler interacts with any external system used to run trial evaluations:\n\n", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\nTo run automated NAS with the Scheduler, the main things we need to do are:", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "- Define a [Runner](https://github.com/facebook/Ax/blob/main/ax/core/runner.py#L21), which is responsible for sending off a model with a particular architecture to be trained on a platform of our choice (like Kubernetes, or maybe just a Docker image on our local machine). In the tutorial below, we use TorchX for handling deployment of training jobs.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "- Define a [Metric](https://github.com/facebook/Ax/blob/main/ax/core/metric.py#L21), which is responsible for fetching the objective metrics (such as accuracy, model size, latency) from the training job. In our tutorial, we use Tensorboard to log data, and so can use the Tensorboard metrics that come bundled with Ax.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Tutorial", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "In our tutorial we show how to use Ax to run multi-objective NAS for a simple neural network model on the popular MNIST dataset. While the underlying methodology can be used for more complicated models and larger datasets, we opt for a tutorial that is easily runnable end-to-end on a laptop in less than an hour. In our example, we will tune the widths of two hidden layers, the learning rate, the dropout probability, the batch size, and the number of training epochs. The goal is to trade off performance", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "(accuracy on the validation set) and model size (the number of model parameters) using [multi-objective Bayesian optimization](https://proceedings.neurips.cc/paper/2021/file/11704817e347269b7254e744b5e22dac-Paper.pdf).", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "The tutorial makes use of the following PyTorch libraries:\n\n- [PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning) (specifying the model and training loop)\n\n- [TorchX](https://github.com/pytorch/torchx) (for running training jobs remotely / asynchronously)\n\n- [BoTorch](https://github.com/pytorch/botorch) (the Bayesian optimization library that powers Ax\u2019s algorithms)", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "The complete runnable example is available as a **[PyTorch Tutorial](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html)**.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Results", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "The final results from the NAS optimization performed in the tutorial can be seen in the tradeoff plot below. Here, each point corresponds to the result of a trial, with the color representing its iteration number, and the star indicating the reference point defined by the thresholds we imposed on the objectives. We see that our method was able to successfully explore the trade-offs between validation accuracy and number of parameters and found both large models with high validation accuracy as well as", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "small models with lower validation accuracy. Depending on the performance requirements and model size constraints, the decision maker can now choose which model to use or analyze further.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Visualizations", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Ax provides a number of visualizations that make it possible to analyze and understand the results of an experiment. Here, we will focus on the performance of the Gaussian process models that model the unknown objectives, which are used to help us discover promising configurations faster. Ax makes it easy to better understand how accurate these models are and how they perform on unseen data via leave-one-out cross-validation. In the figures below, we see that the model fits look quite good - predictions are", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "close to the actual outcomes, and predictive 95% confidence intervals cover the actual outcomes well. Additionally, we observe that the model size `(num_params)` metric is much easier to model than the validation accuracy `(val_acc)` metric.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "\n\n\n\n
\n

\n\n

\n\n

\n\n

\n
", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Takeaways\n\n- We showed how to run a fully automated multi-objective Neural Architecture Search using Ax.\n\n- Using the Ax Scheduler, we were able to run the optimization automatically in a fully asynchronous fashion - this can be done locally (as done in the tutorial) or by deploying trials remotely to a cluster (simply by changing the TorchX scheduler configuration).", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "- The state-of-the-art multi-objective Bayesian optimization algorithms available in Ax allowed us to efficiently explore the tradeoffs between validation accuracy and model size.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Advanced Functionality\n\nAx has a number of other advanced capabilities that we did not discuss in our tutorial. Among these are the following:", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Early Stopping", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "When evaluating a new candidate configuration, partial learning curves are typically available while the NN training job is running. We can use the information contained in the partial curves to identify under-performing trials to stop early in order to free up computational resources for more promising candidates. While not demonstrated in the above tutorial, Ax supports early stopping out-of-the-box - see our [early stopping", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "tutorial](https://ax.dev/versions/latest/tutorials/early_stopping/early_stopping.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "High-dimensional search spaces", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "In our tutorial, we used Bayesian optimization with a standard Gaussian process in order to keep the runtime low. However, these models typically scale to only about 10-20 tunable parameters. Our new SAASBO method ([paper](https://proceedings.mlr.press/v161/eriksson21a/eriksson21a.pdf), [Ax tutorial](https://ax.dev/tutorials/saasbo.html), [BoTorch tutorial](https://botorch.org/tutorials/saasbo)) is very sample-efficient and enables tuning hundreds of parameters. SAASBO can easily be enabled by passing", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "`use_saasbo=True` to `choose_generation_strategy`.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nWe thank the TorchX team (in particular Kiuk Chung and Tristan Rice) for their help with integrating TorchX with Ax, and the Adaptive Experimentation team @ Meta for their contributions to Ax and BoTorch.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "References\n\n[D. Eriksson, P. Chuang, S. Daulton, M. Balandat. Optimizing model accuracy and latency using Bayesian multi-objective neural architecture search. Meta Research blog, July 2021.](https://research.facebook.com/blog/2021/07/optimizing-model-accuracy-and-latency-using-bayesian-multi-objective-neural-architecture-search/)", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch Adds New Ecosystem Projects for Encrypted AI and Quantum Computing, Expands PyTorch Hub'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch ecosystem includes projects, tools, models and libraries from a broad community of researchers in academia and industry, application developers, and ML engineers. The goal of this ecosystem is to support, accelerate, and aid in your exploration with PyTorch and help you push the state of the art, no matter what field you are exploring. Similarly, we are expanding the recently launched PyTorch Hub to further help you discover and reproduce the latest research.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "In this post, we\u2019ll highlight some of the projects that have been added to the PyTorch ecosystem this year and provide some context on the criteria we use to evaluate community projects. We\u2019ll also provide an update on the fast-growing PyTorch Hub and share details on our upcoming PyTorch Summer Hackathon.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "Recently added ecosystem projects\n\nFrom private AI to quantum computing, we\u2019ve seen the community continue to expand into new and interesting areas. The latest projects include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "- [Advertorch](https://github.com/BorealisAI/advertorch): A Python toolbox for adversarial robustness research. The primary functionalities are implemented in PyTorch. Specifically, AdverTorch contains modules for generating adversarial perturbations and defending against adversarial examples, as well as scripts for adversarial training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "- [botorch](https://botorch.org/): A modular and easily extensible interface for composing Bayesian optimization primitives, including probabilistic models, acquisition functions, and optimizers.\n\n- [Skorch](https://github.com/skorch-dev/skorch): A high-level library for PyTorch that provides full scikit-learn compatibility.\n\n- [PyTorch Geometric](https://github.com/rusty1s/pytorch_geometric): A library for deep learning on irregular input data such as graphs, point clouds, and manifolds.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "- [PySyft](https://github.com/OpenMined/PySyft): A Python library for encrypted, privacy preserving deep learning.\n\n- [PennyLane](https://pennylane.ai/): A library for quantum ML, automatic differentiation, and optimization of hybrid quantum-classical computations.\n\n- [Flair](https://github.com/zalandoresearch/flair): A very simple framework for state-of-the-art natural language processing (NLP).", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "What makes a great project?\n\nWhen we review project submissions for the PyTorch ecosystem, we take into account a number of factors that we feel are important and that we would want in the projects we use ourselves. Some of these criteria include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "1. *Well-tested:* Users should be confident that ecosystem projects will work well with PyTorch, and include support for CI to ensure that testing is occurring on a continuous basis and the project can run on the latest version of PyTorch.\n2. *Clear utility:* Users should understand where each project fits within the PyTorch ecosystem and the value it brings.\n3. *Permissive licensing:* Users must be able to utilize ecosystem projects without licensing concerns. e.g. BSD-3, Apache-2 and MIT licenses", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "4. *Easy onboarding:* Projects need to have support for binary installation options (pip/Conda), clear documentation and a rich set of tutorials (ideally built into Jupyter notebooks).\n5. *Ongoing maintenance:* Project authors need to be committed to supporting and maintaining their projects.\n6. *Community:* Projects should have (or be on track to building) an active, broad-based community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "If you would like to have your project included in the PyTorch ecosystem and featured on [pytorch.org/ecosystem](http://pytorch.org/ecosystem), please complete the form [here](https://pytorch.org/ecosystem/join). If you've previously submitted a project for consideration and haven't heard back, we promise to get back to you as soon as we can - we've received a lot of submissions!", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Hub for reproducible research | New models", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "Since [launching](https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/) the PyTorch Hub in beta, we\u2019ve received a lot of interest from the community including the contribution of many new models. Some of the latest include [U-Net for Brain MRI](https://pytorch.org/hub/mateuszbuda_brain-segmentation-pytorch_unet/) contributed by researchers at Duke University, [Single Shot Detection](https://pytorch.org/hub/nvidia_deeplearningexamples_ssd/) from NVIDIA and", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "[Transformer-XL](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_transformerXL/) from HuggingFace.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "We\u2019ve seen organic integration of the PyTorch Hub by folks like [paperswithcode](https://paperswithcode.com/), making it even easier for you to try out the state of the art in AI research. In addition, companies like [Seldon](https://github.com/axsaucedo/seldon-core/tree/pytorch_hub/examples/models/pytorchhub) provide production-level support for PyTorch Hub models on top of Kubernetes.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "What are the benefits of contributing a model in the PyTorch Hub?\n\n- *Compatibility:* PyTorch Hub models are prioritized first for testing by the TorchScript and Cloud TPU teams, and used as baselines for researchers across a number of fields.\n\n- *Visibility:* Models in the Hub will be promoted on [pytorch.org](http://pytorch.org/) as well as on [paperswithcode](https://paperswithcode.com/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "- *Ease of testing and reproducibility:* Each model comes with code, clear preprocessing requirements, and methods/dependencies to run. There is also tight integration with [Google Colab](https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/facebookresearch_WSL-Images_resnext.ipynb#scrollTo=LM_l7vXJvnDM), making it a true single click to get started.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Hub contributions welcome!\n\nWe are actively looking to grow the PyTorch Hub and welcome contributions. You don\u2019t need to be an original paper author to contribute, and we\u2019d love to see the number of domains and fields broaden. So what types of contributions are we looking for?\n\n- Artifacts of a published or an arXiv paper (or something of a similar nature that serves a different audience \u2014 such as ULMFit) that a large audience would need.\n\n AND\n\n- Reproduces the published results (or better)", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "Overall these models are aimed at researchers either trying to reproduce a baseline, or trying to build downstream research on top of the model (such as feature-extraction or fine-tuning) as well as researchers looking for a demo of the paper for subjective evaluation. Please keep this audience in mind when contributing.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "If you are short on inspiration or would just like to find out what the SOTA is an any given field or domain, checkout the Paperswithcode [state-of-the-art gallery](https://paperswithcode.com/sota).", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Summer Hackathon\n\nWe\u2019ll be hosting the first PyTorch Summer Hackathon next month. We invite you to apply to participate in the in-person hackathon on August 8th to 9th at Facebook's Menlo Park campus. We'll be bringing the community together to work on innovative ML projects that can solve a broad range of complex challenges.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "Applications will be reviewed and accepted on a rolling basis until spaces are filled. For those who cannot join this Hackathon in person, we\u2019ll be following up soon with other ways to participate.\n\nPlease visit [this link to apply](https://www.eventbrite.com/e/pytorch-summer-hackathon-in-menlo-park-registration-63756668913).\n\nThank you for being part of the PyTorch community!\n\n-Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PipeTransformer: Automated Elastic Pipelining for Distributed Training of Large-scale Models'\nauthor: Chaoyang He, Shen Li, Mahdi Soltanolkotabi, and Salman Avestimehr\nfeatured-img: 'assets/images/pipetransformer_overview.png'\n---", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "In this blog post, we describe the first peer-reviewed research paper that explores accelerating the hybrid of PyTorch DDP (`torch.nn.parallel.DistributedDataParallel`) [1] and Pipeline (`torch.distributed.pipeline`) - [PipeTransformer: Automated Elastic Pipelining for Distributed Training of Large-scale Models](http://proceedings.mlr.press/v139/he21a.html) (Transformers such as BERT [2] and ViT [3]), published at ICML 2021.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "PipeTransformer leverages automated elastic pipelining for efficient distributed training of Transformer models. In PipeTransformer, we designed an adaptive on-the-fly freeze algorithm that can identify and freeze some layers gradually during training and an elastic pipelining system that can dynamically allocate resources to train the remaining active layers. More specifically, PipeTransformer automatically excludes frozen layers from the pipeline, packs active layers into fewer GPUs, and forks more", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "replicas to increase data-parallel width. We evaluate PipeTransformer using Vision Transformer (ViT) on ImageNet and BERT on SQuAD and GLUE datasets. Our results show that compared to the state-of-the-art baseline, PipeTransformer attains up to 2.83-fold speedup without losing accuracy. We also provide various performance analyses for a more comprehensive understanding of our algorithmic and system-wise design.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Next, we will introduce the background, motivation, our idea, design, and how we implement the algorithm and system with PyTorch Distributed APIs.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "* Paper: [http://proceedings.mlr.press/v139/he21a.html](http://proceedings.mlr.press/v139/he21a.html)\n* Source Code: [https://DistML.ai](https://distml.ai).\n* Slides: [https://docs.google.com/presentation/d/1t6HWL33KIQo2as0nSHeBpXYtTBcy0nXCoLiKd0EashY/edit?usp=sharing](https://docs.google.com/presentation/d/1t6HWL33KIQo2as0nSHeBpXYtTBcy0nXCoLiKd0EashY/edit?usp=sharing)", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Introduction\n

\n\"Model\n
\nFigure 1: the Parameter Number of Transformer Models Increases Dramatically.\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Large Transformer models [4][5] have powered accuracy breakthroughs in both natural language processing and computer vision. GPT-3 [4] hit a new record high accuracy for nearly all NLP tasks. Vision Transformer (ViT) [3] also achieved 89\\% top-1 accuracy in ImageNet, outperforming state-of-the-art convolutional networks ResNet-152 and EfficientNet. To tackle the growth in model sizes, researchers have proposed various distributed training techniques, including parameter servers [6][7][8], pipeline", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "parallelism [9][10][11][12], intra-layer parallelism [13][14][15], and zero redundancy data-parallel [16].", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Existing distributed training solutions, however, only study scenarios where all model weights are required to be optimized throughout the training (i.e., computation and communication overhead remains relatively static over different iterations). Recent works on progressive training suggest that parameters in neural networks can be trained dynamically:", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "* Freeze Training: Singular Vector Canonical Correlation Analysis for Deep Learning Dynamics and Interpretability. NeurIPS 2017\n* Efficient Training of BERT by Progressively Stacking. ICML 2019\n* Accelerating Training of Transformer-Based Language Models with Progressive Layer Dropping. NeurIPS 2020.\n* On the Transformer Growth for Progressive BERT Training. NACCL 2021", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\"Freeze\n
\n

\nFigure 2. Interpretable Freeze Training: DNNs converge bottom-up (Results on CIFAR10 using ResNet). Each pane shows layer-by-layer similarity using SVCCA [17][18]", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "For example, in freeze training [17][18], neural networks usually converge from the bottom-up (i.e., not all layers need to be trained all the way through training). Figure 2 shows an example of how weights gradually stabilize during training in this approach. This observation motivates us to utilize freeze training for distributed training of Transformer models to accelerate training by dynamically allocating resources to focus on a shrinking set of active layers. Such a layer freezing strategy is", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "especially pertinent to pipeline parallelism, as excluding consecutive bottom layers from the pipeline can reduce computation, memory, and communication overhead.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\n
\nFigure 3. The process of PipeTransformer\u2019s automated and elastic pipelining to accelerate distributed training of Transformer models\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "We propose PipeTransformer, an elastic pipelining training acceleration framework that automatically reacts to frozen layers by dynamically transforming the scope of the pipelined model and the number of pipeline replicas. To the best of our knowledge, this is the first paper that studies layer freezing in the context of both pipeline and data-parallel training. Figure 3 demonstrates the benefits of such a combination. First, by excluding frozen layers from the pipeline, the same model can be packed into", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "fewer GPUs, leading to both fewer cross-GPU communications and smaller pipeline bubbles. Second, after packing the model into fewer GPUs, the same cluster can accommodate more pipeline replicas, increasing the width of data parallelism. More importantly, the speedups acquired from these two benefits are multiplicative rather than additive, further accelerating the training.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "The design of PipeTransformer faces four major challenges. First, the freeze algorithm must make on-the-fly and adaptive freezing decisions; however, existing work [17][18] only provides a posterior analysis tool. Second, the efficiency of pipeline re-partitioning results is influenced by multiple factors, including partition granularity, cross-partition activation size, and the chunking (the number of micro-batches) in mini-batches, which require reasoning and searching in a large solution space. Third, to", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "dynamically introduce additional pipeline replicas, PipeTransformer must overcome the static nature of collective communications and avoid potentially complex cross-process messaging protocols when onboarding new processes (one pipeline is handled by one process). Finally, caching can save time for repeated forward propagation of frozen layers, but it must be shared between existing pipelines and newly added ones, as the system cannot afford to create and warm up a dedicated cache for each replica.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "In many NAS applications, there is a natural tradeoff between multiple metrics of interest. For instance, when deploying models on-device we may want to maximize model performance (e.g., accuracy), while simultaneously minimizing competing metrics such as power consumption, inference latency, or model size, in order to satisfy deployment constraints. In many cases, we have been able to reduce computational requirements or latency of predictions substantially by accepting a small degradation in model performance (in some cases we were able to both increase accuracy and reduce latency!). Principled methods for exploring such tradeoffs efficiently are key enablers of [Sustainable AI](https://arxiv.org/abs/2111.00364).", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "At Meta, we have successfully used [multi-objective Bayesian NAS](https://research.facebook.com/blog/2021/07/optimizing-model-accuracy-and-latency-using-bayesian-multi-objective-neural-architecture-search/) in Ax to explore such tradeoffs. Our methodology is being used routinely for optimizing AR/VR on-device ML models. Beyond NAS applications, we have also developed [MORBO](https://arxiv.org/pdf/2109.10964.pdf) which is a method for high-dimensional multi-objective optimization that can be used to optimize optical systems for augmented reality (AR).\n\n## Fully automated Multi-Objective NAS with Ax\n\nAx\u2019s Scheduler allows running experiments asynchronously in a closed-loop fashion by continuously deploying trials to an external system, polling for results, leveraging the fetched data to generate more trials, and repeating the process until a stopping condition is met. No human intervention or oversight is required. Features of the Scheduler include:", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "- Customizability of parallelism, failure tolerance, and many other settings;\n\n- A large selection of state-of-the-art optimization algorithms;\n\n- Saving in-progress experiments (to a SQL DB or json) and resuming an experiment from storage;\n\n- Easy extensibility to new backends for running trial evaluations remotely.\n\nThe following illustration from the [Ax scheduler tutorial](https://ax.dev/tutorials/scheduler.html) summarizes how the scheduler interacts with any external system used to run trial evaluations:\n\n\n\n

\n\n

\n\nTo run automated NAS with the Scheduler, the main things we need to do are:", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "- Define a [Runner](https://github.com/facebook/Ax/blob/main/ax/core/runner.py#L21), which is responsible for sending off a model with a particular architecture to be trained on a platform of our choice (like Kubernetes, or maybe just a Docker image on our local machine). In the tutorial below, we use TorchX for handling deployment of training jobs.\n\n- Define a [Metric](https://github.com/facebook/Ax/blob/main/ax/core/metric.py#L21), which is responsible for fetching the objective metrics (such as accuracy, model size, latency) from the training job. In our tutorial, we use Tensorboard to log data, and so can use the Tensorboard metrics that come bundled with Ax.\n\n## Tutorial", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "## Tutorial\n\nIn our tutorial we show how to use Ax to run multi-objective NAS for a simple neural network model on the popular MNIST dataset. While the underlying methodology can be used for more complicated models and larger datasets, we opt for a tutorial that is easily runnable end-to-end on a laptop in less than an hour. In our example, we will tune the widths of two hidden layers, the learning rate, the dropout probability, the batch size, and the number of training epochs. The goal is to trade off performance (accuracy on the validation set) and model size (the number of model parameters) using [multi-objective Bayesian optimization](https://proceedings.neurips.cc/paper/2021/file/11704817e347269b7254e744b5e22dac-Paper.pdf).\n\nThe tutorial makes use of the following PyTorch libraries:\n\n- [PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning) (specifying the model and training loop)\n\n- [TorchX](https://github.com/pytorch/torchx) (for running training jobs remotely / asynchronously)", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "- [BoTorch](https://github.com/pytorch/botorch) (the Bayesian optimization library that powers Ax\u2019s algorithms)\n\nThe complete runnable example is available as a **[PyTorch Tutorial](https://pytorch.org/tutorials/intermediate/ax_multiobjective_nas_tutorial.html)**.\n\n### Results\n\nThe final results from the NAS optimization performed in the tutorial can be seen in the tradeoff plot below. Here, each point corresponds to the result of a trial, with the color representing its iteration number, and the star indicating the reference point defined by the thresholds we imposed on the objectives. We see that our method was able to successfully explore the trade-offs between validation accuracy and number of parameters and found both large models with high validation accuracy as well as small models with lower validation accuracy. Depending on the performance requirements and model size constraints, the decision maker can now choose which model to use or analyze further.", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "

\n\n

\n\n### Visualizations\n\nAx provides a number of visualizations that make it possible to analyze and understand the results of an experiment. Here, we will focus on the performance of the Gaussian process models that model the unknown objectives, which are used to help us discover promising configurations faster. Ax makes it easy to better understand how accurate these models are and how they perform on unseen data via leave-one-out cross-validation. In the figures below, we see that the model fits look quite good - predictions are close to the actual outcomes, and predictive 95% confidence intervals cover the actual outcomes well. Additionally, we observe that the model size `(num_params)` metric is much easier to model than the validation accuracy `(val_acc)` metric.\n\n\n\n", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "\n\n
\n

\n\n

\n\n

\n\n

\n
\n\n## Takeaways\n\n- We showed how to run a fully automated multi-objective Neural Architecture Search using Ax.\n\n- Using the Ax Scheduler, we were able to run the optimization automatically in a fully asynchronous fashion - this can be done locally (as done in the tutorial) or by deploying trials remotely to a cluster (simply by changing the TorchX scheduler configuration).\n\n- The state-of-the-art multi-objective Bayesian optimization algorithms available in Ax allowed us to efficiently explore the tradeoffs between validation accuracy and model size.\n\n## Advanced Functionality\n\nAx has a number of other advanced capabilities that we did not discuss in our tutorial. Among these are the following:\n\n### Early Stopping", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "### Early Stopping\n\nWhen evaluating a new candidate configuration, partial learning curves are typically available while the NN training job is running. We can use the information contained in the partial curves to identify under-performing trials to stop early in order to free up computational resources for more promising candidates. While not demonstrated in the above tutorial, Ax supports early stopping out-of-the-box - see our [early stopping tutorial](https://ax.dev/versions/latest/tutorials/early_stopping/early_stopping.html) for more details.\n\n### High-dimensional search spaces", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "In our tutorial, we used Bayesian optimization with a standard Gaussian process in order to keep the runtime low. However, these models typically scale to only about 10-20 tunable parameters. Our new SAASBO method ([paper](https://proceedings.mlr.press/v161/eriksson21a/eriksson21a.pdf), [Ax tutorial](https://ax.dev/tutorials/saasbo.html), [BoTorch tutorial](https://botorch.org/tutorials/saasbo)) is very sample-efficient and enables tuning hundreds of parameters. SAASBO can easily be enabled by passing `use_saasbo=True` to `choose_generation_strategy`.\n\n## Acknowledgements\n\nWe thank the TorchX team (in particular Kiuk Chung and Tristan Rice) for their help with integrating TorchX with Ax, and the Adaptive Experimentation team @ Meta for their contributions to Ax and BoTorch.\n\n## References", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "## References\n\n[D. Eriksson, P. Chuang, S. Daulton, M. Balandat. Optimizing model accuracy and latency using Bayesian multi-objective neural architecture search. Meta Research blog, July 2021.](https://research.facebook.com/blog/2021/07/optimizing-model-accuracy-and-latency-using-bayesian-multi-objective-neural-architecture-search/)", "metadata": {"source": "https://pytorch.org/blog/effective-multi-objective-nueral-architecture/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch Adds New Ecosystem Projects for Encrypted AI and Quantum Computing, Expands PyTorch Hub'\nauthor: Team PyTorch\n---\n\nThe PyTorch ecosystem includes projects, tools, models and libraries from a broad community of researchers in academia and industry, application developers, and ML engineers. The goal of this ecosystem is to support, accelerate, and aid in your exploration with PyTorch and help you push the state of the art, no matter what field you are exploring. Similarly, we are expanding the recently launched PyTorch Hub to further help you discover and reproduce the latest research.\n\nIn this post, we\u2019ll highlight some of the projects that have been added to the PyTorch ecosystem this year and provide some context on the criteria we use to evaluate community projects. We\u2019ll also provide an update on the fast-growing PyTorch Hub and share details on our upcoming PyTorch Summer Hackathon.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n## Recently added ecosystem projects\n\nFrom private AI to quantum computing, we\u2019ve seen the community continue to expand into new and interesting areas. The latest projects include:\n\n- [Advertorch](https://github.com/BorealisAI/advertorch): A Python toolbox for adversarial robustness research. The primary functionalities are implemented in PyTorch. Specifically, AdverTorch contains modules for generating adversarial perturbations and defending against adversarial examples, as well as scripts for adversarial training.\n\n- [botorch](https://botorch.org/): A modular and easily extensible interface for composing Bayesian optimization primitives, including probabilistic models, acquisition functions, and optimizers.\n\n- [Skorch](https://github.com/skorch-dev/skorch): A high-level library for PyTorch that provides full scikit-learn compatibility.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "- [PyTorch Geometric](https://github.com/rusty1s/pytorch_geometric): A library for deep learning on irregular input data such as graphs, point clouds, and manifolds.\n\n- [PySyft](https://github.com/OpenMined/PySyft): A Python library for encrypted, privacy preserving deep learning.\n\n- [PennyLane](https://pennylane.ai/): A library for quantum ML, automatic differentiation, and optimization of hybrid quantum-classical computations.\n\n- [Flair](https://github.com/zalandoresearch/flair): A very simple framework for state-of-the-art natural language processing (NLP).\n\n### What makes a great project?\n\nWhen we review project submissions for the PyTorch ecosystem, we take into account a number of factors that we feel are important and that we would want in the projects we use ourselves. Some of these criteria include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "1. *Well-tested:* Users should be confident that ecosystem projects will work well with PyTorch, and include support for CI to ensure that testing is occurring on a continuous basis and the project can run on the latest version of PyTorch.\n2. *Clear utility:* Users should understand where each project fits within the PyTorch ecosystem and the value it brings.\n3. *Permissive licensing:* Users must be able to utilize ecosystem projects without licensing concerns. e.g. BSD-3, Apache-2 and MIT licenses\n4. *Easy onboarding:* Projects need to have support for binary installation options (pip/Conda), clear documentation and a rich set of tutorials (ideally built into Jupyter notebooks).\n5. *Ongoing maintenance:* Project authors need to be committed to supporting and maintaining their projects.\n6. *Community:* Projects should have (or be on track to building) an active, broad-based community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "If you would like to have your project included in the PyTorch ecosystem and featured on [pytorch.org/ecosystem](http://pytorch.org/ecosystem), please complete the form [here](https://pytorch.org/ecosystem/join). If you've previously submitted a project for consideration and haven't heard back, we promise to get back to you as soon as we can - we've received a lot of submissions!\n\n## PyTorch Hub for reproducible research | New models", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "Since [launching](https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/) the PyTorch Hub in beta, we\u2019ve received a lot of interest from the community including the contribution of many new models. Some of the latest include [U-Net for Brain MRI](https://pytorch.org/hub/mateuszbuda_brain-segmentation-pytorch_unet/) contributed by researchers at Duke University, [Single Shot Detection](https://pytorch.org/hub/nvidia_deeplearningexamples_ssd/) from NVIDIA and [Transformer-XL](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_transformerXL/) from HuggingFace.\n\nWe\u2019ve seen organic integration of the PyTorch Hub by folks like [paperswithcode](https://paperswithcode.com/), making it even easier for you to try out the state of the art in AI research. In addition, companies like [Seldon](https://github.com/axsaucedo/seldon-core/tree/pytorch_hub/examples/models/pytorchhub) provide production-level support for PyTorch Hub models on top of Kubernetes.", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "### What are the benefits of contributing a model in the PyTorch Hub?\n\n- *Compatibility:* PyTorch Hub models are prioritized first for testing by the TorchScript and Cloud TPU teams, and used as baselines for researchers across a number of fields.\n\n- *Visibility:* Models in the Hub will be promoted on [pytorch.org](http://pytorch.org/) as well as on [paperswithcode](https://paperswithcode.com/).\n\n- *Ease of testing and reproducibility:* Each model comes with code, clear preprocessing requirements, and methods/dependencies to run. There is also tight integration with [Google Colab](https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/facebookresearch_WSL-Images_resnext.ipynb#scrollTo=LM_l7vXJvnDM), making it a true single click to get started.\n\n### PyTorch Hub contributions welcome!", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "We are actively looking to grow the PyTorch Hub and welcome contributions. You don\u2019t need to be an original paper author to contribute, and we\u2019d love to see the number of domains and fields broaden. So what types of contributions are we looking for?\n\n- Artifacts of a published or an arXiv paper (or something of a similar nature that serves a different audience \u2014 such as ULMFit) that a large audience would need.\n\n AND\n\n- Reproduces the published results (or better)\n\nOverall these models are aimed at researchers either trying to reproduce a baseline, or trying to build downstream research on top of the model (such as feature-extraction or fine-tuning) as well as researchers looking for a demo of the paper for subjective evaluation. Please keep this audience in mind when contributing.\n\nIf you are short on inspiration or would just like to find out what the SOTA is an any given field or domain, checkout the Paperswithcode [state-of-the-art gallery](https://paperswithcode.com/sota).\n\n## PyTorch Summer Hackathon", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "We\u2019ll be hosting the first PyTorch Summer Hackathon next month. We invite you to apply to participate in the in-person hackathon on August 8th to 9th at Facebook's Menlo Park campus. We'll be bringing the community together to work on innovative ML projects that can solve a broad range of complex challenges.\n\nApplications will be reviewed and accepted on a rolling basis until spaces are filled. For those who cannot join this Hackathon in person, we\u2019ll be following up soon with other ways to participate.\n\nPlease visit [this link to apply](https://www.eventbrite.com/e/pytorch-summer-hackathon-in-menlo-park-registration-63756668913).\n\nThank you for being part of the PyTorch community!\n\n-Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-ecosystem/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PipeTransformer: Automated Elastic Pipelining for Distributed Training of Large-scale Models'\nauthor: Chaoyang He, Shen Li, Mahdi Soltanolkotabi, and Salman Avestimehr\nfeatured-img: 'assets/images/pipetransformer_overview.png'\n---\n\nIn this blog post, we describe the first peer-reviewed research paper that explores accelerating the hybrid of PyTorch DDP (`torch.nn.parallel.DistributedDataParallel`) [1] and Pipeline (`torch.distributed.pipeline`) - [PipeTransformer: Automated Elastic Pipelining for Distributed Training of Large-scale Models](http://proceedings.mlr.press/v139/he21a.html) (Transformers such as BERT [2] and ViT [3]), published at ICML 2021.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "PipeTransformer leverages automated elastic pipelining for efficient distributed training of Transformer models. In PipeTransformer, we designed an adaptive on-the-fly freeze algorithm that can identify and freeze some layers gradually during training and an elastic pipelining system that can dynamically allocate resources to train the remaining active layers. More specifically, PipeTransformer automatically excludes frozen layers from the pipeline, packs active layers into fewer GPUs, and forks more replicas to increase data-parallel width. We evaluate PipeTransformer using Vision Transformer (ViT) on ImageNet and BERT on SQuAD and GLUE datasets. Our results show that compared to the state-of-the-art baseline, PipeTransformer attains up to 2.83-fold speedup without losing accuracy. We also provide various performance analyses for a more comprehensive understanding of our algorithmic and system-wise design.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Next, we will introduce the background, motivation, our idea, design, and how we implement the algorithm and system with PyTorch Distributed APIs.\n\n* Paper: [http://proceedings.mlr.press/v139/he21a.html](http://proceedings.mlr.press/v139/he21a.html)\n* Source Code: [https://DistML.ai](https://distml.ai).\n* Slides: [https://docs.google.com/presentation/d/1t6HWL33KIQo2as0nSHeBpXYtTBcy0nXCoLiKd0EashY/edit?usp=sharing](https://docs.google.com/presentation/d/1t6HWL33KIQo2as0nSHeBpXYtTBcy0nXCoLiKd0EashY/edit?usp=sharing)\n\n# Introduction\n

\n\"Model\n
\nFigure 1: the Parameter Number of Transformer Models Increases Dramatically.\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Large Transformer models [4][5] have powered accuracy breakthroughs in both natural language processing and computer vision. GPT-3 [4] hit a new record high accuracy for nearly all NLP tasks. Vision Transformer (ViT) [3] also achieved 89\\% top-1 accuracy in ImageNet, outperforming state-of-the-art convolutional networks ResNet-152 and EfficientNet. To tackle the growth in model sizes, researchers have proposed various distributed training techniques, including parameter servers [6][7][8], pipeline parallelism [9][10][11][12], intra-layer parallelism [13][14][15], and zero redundancy data-parallel [16].\n\n\nExisting distributed training solutions, however, only study scenarios where all model weights are required to be optimized throughout the training (i.e., computation and communication overhead remains relatively static over different iterations). Recent works on progressive training suggest that parameters in neural networks can be trained dynamically:", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "* Freeze Training: Singular Vector Canonical Correlation Analysis for Deep Learning Dynamics and Interpretability. NeurIPS 2017\n* Efficient Training of BERT by Progressively Stacking. ICML 2019\n* Accelerating Training of Transformer-Based Language Models with Progressive Layer Dropping. NeurIPS 2020.\n* On the Transformer Growth for Progressive BERT Training. NACCL 2021\n\n\n

\n\"Freeze\n
\n

\nFigure 2. Interpretable Freeze Training: DNNs converge bottom-up (Results on CIFAR10 using ResNet). Each pane shows layer-by-layer similarity using SVCCA [17][18]", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "For example, in freeze training [17][18], neural networks usually converge from the bottom-up (i.e., not all layers need to be trained all the way through training). Figure 2 shows an example of how weights gradually stabilize during training in this approach. This observation motivates us to utilize freeze training for distributed training of Transformer models to accelerate training by dynamically allocating resources to focus on a shrinking set of active layers. Such a layer freezing strategy is especially pertinent to pipeline parallelism, as excluding consecutive bottom layers from the pipeline can reduce computation, memory, and communication overhead.\n\n

\n\n
\nFigure 3. The process of PipeTransformer\u2019s automated and elastic pipelining to accelerate distributed training of Transformer models\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "We propose PipeTransformer, an elastic pipelining training acceleration framework that automatically reacts to frozen layers by dynamically transforming the scope of the pipelined model and the number of pipeline replicas. To the best of our knowledge, this is the first paper that studies layer freezing in the context of both pipeline and data-parallel training. Figure 3 demonstrates the benefits of such a combination. First, by excluding frozen layers from the pipeline, the same model can be packed into fewer GPUs, leading to both fewer cross-GPU communications and smaller pipeline bubbles. Second, after packing the model into fewer GPUs, the same cluster can accommodate more pipeline replicas, increasing the width of data parallelism. More importantly, the speedups acquired from these two benefits are multiplicative rather than additive, further accelerating the training.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "The design of PipeTransformer faces four major challenges. First, the freeze algorithm must make on-the-fly and adaptive freezing decisions; however, existing work [17][18] only provides a posterior analysis tool. Second, the efficiency of pipeline re-partitioning results is influenced by multiple factors, including partition granularity, cross-partition activation size, and the chunking (the number of micro-batches) in mini-batches, which require reasoning and searching in a large solution space. Third, to dynamically introduce additional pipeline replicas, PipeTransformer must overcome the static nature of collective communications and avoid potentially complex cross-process messaging protocols when onboarding new processes (one pipeline is handled by one process). Finally, caching can save time for repeated forward propagation of frozen layers, but it must be shared between existing pipelines and newly added ones, as the system cannot afford to create and warm up a dedicated cache for each replica.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} {"page_content": "

\n\"Freeze\n
\nFigure 4: An Animation to Show the Dynamics of PipeTransformer\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "As shown in the animation (Figure 4), PipeTransformer is designed with four core building blocks to address the aforementioned challenges. First, we design a tunable and adaptive algorithm to generate signals that guide the selection of layers to freeze over different iterations (Freeze Algorithm). Once triggered by these signals, our elastic pipelining module (AutoPipe), then packs the remaining active layers into fewer GPUs by taking both activation sizes and variances of workloads across heterogeneous", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "partitions (frozen layers and active layers) into account. It then splits a mini-batch into an optimal number of micro-batches based on prior profiling results for different pipeline lengths. Our next module, AutoDP, spawns additional pipeline replicas to occupy freed-up GPUs and maintains hierarchical communication process groups to attain dynamic membership for collective communications. Our final module, AutoCache, efficiently shares activations across existing and new data-parallel processes and", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "automatically replaces stale caches during transitions.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Overall, PipeTransformer combines the Freeze Algorithm, AutoPipe, AutoDP, and AutoCache modules to provide a significant training speedup.\nWe evaluate PipeTransformer using Vision Transformer (ViT) on ImageNet and BERT on GLUE and SQuAD datasets. Our results show that PipeTransformer attains up to 2.83-fold speedup without losing accuracy. We also provide various performance analyses for a more comprehensive understanding of our algorithmic and system-wise design.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Finally, we have also developed open-source flexible APIs for PipeTransformer, which offer a clean separation among the freeze algorithm, model definitions, and training accelerations, allowing for transferability to other algorithms that require similar freezing strategies.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Overall Design\n\nSuppose we aim to train a massive model in a distributed training system where the hybrid of pipelined model parallelism and data parallelism is used to target scenarios where either the memory of a single GPU device cannot hold the model, or if loaded, the batch size is small enough to avoid running out of memory. More specifically, we define our settings as follows:", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Training task and model definition. We train Transformer models (e.g., Vision Transformer, BERT on large-scale image or text datasets. The Transformer model has layers, in which the th layer is composed of a forward computation function and a corresponding set of parameters.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Training infrastructure. Assume the training infrastructure contains a GPU cluster that has GPU servers (i.e. nodes). Each node has GPUs. Our cluster is homogeneous, meaning that each GPU and server have the same hardware configuration. Each GPU's memory capacity is . Servers are", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "connected by a high bandwidth network interface such as InfiniBand interconnect.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Pipeline parallelism. In each machine, we load a model into a pipeline which has partitions ( also represents the pipeline length). The th", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "partition consists of consecutive layers. We assume each partition is handled by a single GPU device. , meaning that we can build multiple pipelines for multiple model replicas in a single machine. We assume all GPU devices in a pipeline belonging to the same machine. Our pipeline is a synchronous pipeline, which does not involve stale gradients, and the", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "number of micro-batches is . In the Linux OS, each pipeline is handled by a single process. We refer the reader to GPipe [10] for more details.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Data parallelism. DDP is a cross-machine distributed data-parallel process group within parallel workers. Each worker is a pipeline replica (a single process). The th worker's index (ID) is rank . For any two pipelines in DDP, they can belong to either the same GPU server or different GPU", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "servers, and they can exchange gradients with the AllReduce algorithm.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Under these settings, our goal is to accelerate training by leveraging freeze training, which does not require all layers to be trained throughout the duration of the training. Additionally, it may help save computation, communication, memory cost, and potentially prevent overfitting by consecutively freezing layers. However, these benefits can only be achieved by overcoming the four challenges of designing an adaptive freezing algorithm, dynamical pipeline re-partitioning, efficient resource reallocation,", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "and cross-process caching, as discussed in the introduction.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\"Overview\"\n
\nFigure 5. Overview of PipeTransformer Training System\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "PipeTransformer co-designs an on-the-fly freeze algorithm and an automated elastic pipelining training system that can dynamically transform the scope of the pipelined model and the number of pipeline replicas. The overall system architecture is illustrated in Figure 5. To support PipeTransformer\u2019s elastic pipelining, we maintain a customized version of PyTorch Pipeline. For data parallelism, we use PyTorch DDP as a baseline. Other libraries are standard mechanisms of an operating system", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "(e.g.,multi-processing) and thus avoid specialized software or hardware customization requirements. To ensure the generality of our framework, we have decoupled the training system into four core components: freeze algorithm, AutoPipe, AutoDP, and AutoCache. The freeze algorithm (grey) samples indicators from the training loop and makes layer-wise freezing decisions, which will be shared with AutoPipe", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "(green). AutoPipe is an elastic pipeline module that speeds up training by excluding frozen layers from the pipeline and packing the active layers into fewer GPUs (pink), leading to both fewer cross-GPU communications and smaller pipeline bubbles. Subsequently, AutoPipe passes pipeline length information to AutoDP (purple), which then spawns more pipeline replicas to increase data-parallel width, if possible. The illustration also includes an example in which AutoDP", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "introduces a new replica (purple). AutoCache (orange edges) is a cross-pipeline caching module, as illustrated by connections between pipelines. The source code architecture is aligned with Figure 5 for readability and generality.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Implementation Using PyTorch APIs\n\nAs can be seen from Figure 5, PipeTransformers contain four components: Freeze Algorithm, AutoPipe, AutoDP, and AutoCache. Among them, AutoPipe and AutoDP relies on PyTorch DDP (`torch.nn.parallel.DistributedDataParallel`) [1] and Pipeline (`torch.distributed.pipeline`), respectively. In this blog, we only highlight the key implementation details of AutoPipe and AutoDP. For details of Freeze Algorithm and AutoCache, please refer to our paper.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "AutoPipe: Elastic Pipelining\n\nAutoPipe can accelerate training by excluding frozen layers from the pipeline and packing the active layers into fewer GPUs. This section elaborates on the key components of AutoPipe that dynamically 1) partition pipelines, 2) minimize the number of pipeline devices, and 3) optimize mini-batch chunk size accordingly.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Basic Usage of PyTorch Pipeline\n\nBefore diving into details of AutoPipe, let us warm up the basic usage of PyTorch Pipeline (`torch.distributed.pipeline.sync.Pipe`, see [this tutorial](https://pytorch.org/docs/stable/pipeline.html)). More specially, we present a simple example to understand the design of Pipeline in practice:\n\n```python\n# Step 1: build a model including two linear layers\nfc1 = nn.Linear(16, 8).cuda(0)\nfc2 = nn.Linear(8, 4).cuda(1)", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Step 2: wrap the two layers with nn.Sequential\nmodel = nn.Sequential(fc1, fc2)\n\n# Step 3: build Pipe (torch.distributed.pipeline.sync.Pipe)\nmodel = Pipe(model, chunks=8)\n\n# do training/inference\ninput = torch.rand(16, 16).cuda(0)\noutput_rref = model(input)", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "In this basic example, we can see that before initializing `Pipe`, we need to partition the model `nn.Sequential` into multiple GPU devices and set optimal chunk number (`chunks`). Balancing computation time across partitions is critical to pipeline training speed, as skewed workload distributions across stages can lead to stragglers and forcing devices with lighter workloads to wait. The chunk number may also have a non-trivial influence on the throughput of the pipeline.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Balanced Pipeline Partitioning\n\nIn dynamic training system such as PipeTransformer, maintaining optimally balanced partitions in terms of parameter numbers does not guarantee the fastest training speed because other factors also play a crucial role:\n\n

\n\n
\nFigure 6. The partition boundary is in the middle of a skip connection\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "1. Cross-partition communication overhead. Placing a partition boundary in the middle of a skip connection leads to additional communications since tensors in the skip connection must now be copied to a different GPU. For example, with BERT partitions in Figure 6, partition must take intermediate outputs from both partition and partition . In contrast, if the boundary is placed after the addition layer, the communication overhead between partition and is visibly smaller. Our measurements show that having cross-device communication is more expensive than having slightly imbalanced partitions (see the Appendix in our paper). Therefore, we do", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "not consider breaking skip connections (highlighted separately as an entire attention layer and MLP layer in green color at line 7 in Algorithm 1.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "2. Frozen layer memory footprint. During training, AutoPipe must recompute partition boundaries several times to balance two distinct types of layers: frozen layers and active layers. The frozen layer's memory cost is a fraction of that inactive layer, given that the frozen layer does not need backward activation maps, optimizer states, and gradients. Instead of launching intrusive profilers to obtain thorough metrics on memory and computational cost, we define a tunable cost factor to estimate the memory footprint ratio of a frozen layer over the same active layer. Based on empirical measurements in our experimental hardware, we set it to .", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Based on the above two considerations, AutoPipe balances pipeline partitions based on parameter sizes. More specifically, AutoPipe uses a greedy algorithm to allocate all frozen and active layers to evenly distribute partitioned sublayers into GPU devices. Pseudocode is described as the `load\\_balance()` function in Algorithm 1. The frozen layers are extracted from the original model and kept in a separate model instance in the first device of a pipeline.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Note that the partition algorithm employed in this paper is not the only option; PipeTransformer is modularized to work with any alternatives.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Pipeline Compression", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Pipeline compression helps to free up GPUs to accommodate more pipeline replicas and reduce the number of cross-device communications between partitions. To determine the timing of compression, we can estimate the memory cost of the largest partition after compression, and then compare it with that of the largest partition of a pipeline at timestep . To avoid extensive memory profiling, the compression algorithm uses the parameter size as", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "a proxy for the training memory footprint. Based on this simplification, the criterion of pipeline compression is as follows:", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Once the freeze notification is received, AutoPipe will always attempt to divide the pipeline length by 2 (e.g., from 8 to 4, then 2). By using as the input, the compression algorithm can verify if the result satisfies the criterion in Equation (1). Pseudocode is shown in lines 25-33 in Algorithm 1. Note that this compression makes the acceleration ratio", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "exponentially increase during training, meaning that if a GPU server has a larger number of GPUs (e.g., more than 8), the acceleration ratio will be further amplified.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\n
", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Figure 7. Pipeline Bubble: , and denote forward, backward, and the optimizer update of micro-batch on device , respectively. The total bubble size in each iteration is times per micro-batch forward and backward cost.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Additionally, such a technique can also speed up training by shrinking the size of pipeline bubbles. To explain bubble sizes in a pipeline, Figure 7 depicts how 4 micro-batches run through a 4-device pipeline . In general, the total bubble size is times per micro-batch forward and backward cost. Therefore, it is clear that shorter pipelines have smaller bubble sizes.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Dynamic Number of Micro-Batches", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Prior pipeline parallel systems use a fixed number of micro-batches per mini-batch ( ). GPipe suggests , where is the number of partitions (pipeline length). However, given that PipeTransformer dynamically configures , we find", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "it to be sub-optimal to maintain a static during training. Moreover, when integrated with DDP, the value of also has an impact on the efficiency of DDP gradient synchronizations. Since DDP must wait for the last micro-batch to finish its backward computation on a parameter before launching its gradient synchronization, finer micro-batches lead to a smaller overlap between", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "computation and communication. Hence, instead of using a static value, PipeTransformer searches for optimal on the fly in the hybrid of DDP environment by enumerating values ranging from to . For a specific training environment, the", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "profiling needs only to be done once (see Algorithm 1 line 35).", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "For the complete source code, please refer to `https://github.com/Distributed-AI/PipeTransformer/blob/master/pipe_transformer/pipe/auto_pipe.py`.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "AutoDP: Spawning More Pipeline Replicas\nAs AutoPipe compresses the same pipeline into fewer GPUs, AutoDP can automatically spawn new pipeline replicas to increase data-parallel width.\n\nDespite the conceptual simplicity, subtle dependencies on communications and states require careful design. The challenges are threefold:\n\n1. DDP Communication: Collective communications in PyTorch DDP requires static membership, which prevents new pipelines from connecting with existing ones;", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "2. State Synchronization: newly activated processes must be consistent with existing pipelines in the training progress (e.g., epoch number and learning rate), weights and optimizer states, the boundary of frozen layers, and pipeline GPU range;\n\n3. Dataset Redistribution: the dataset should be re-balanced to match a dynamic number of pipelines. This not only avoids stragglers but also ensures that gradients from all DDP processes are equally weighted.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "

\n\n
\nFigure 8. AutoDP: handling dynamical data-parallel with messaging between double process groups (Process 0-7 belong to machine 0, while process 8-15 belong to machine 1)\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "To tackle these challenges, we create double communication process groups for DDP. As in the example shown in Figure 8, the message process group (purple) is responsible for light-weight control messages and covers all processes, while the active training process group (yellow) only contains active processes and serves as a vehicle for heavy-weight tensor communications during training. The message group remains static, whereas the training group is dismantled and reconstructed to match active processes.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "In T0, only processes 0 and 8 are active. During the transition to T1, process 0 activates processes 1 and 9 (newly added pipeline replicas) and synchronizes necessary information mentioned above using the message group. The four active processes then form a new training group, allowing static collective communications adaptive to dynamic memberships.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "To redistribute the dataset, we implement a variant of DistributedSampler that can seamlessly adjust data samples to match the number of active pipeline replicas.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "The above design also naturally helps to reduce DDP communication overhead. More specifically, when transitioning from T0 to T1, processes 0 and 1 destroy the existing DDP instances, and active processes construct a new DDP training group using a cached pipelined model (AutoPipe stores frozen model and cached model separately).\n\nWe use the following APIs to implement the design above.\n\n```python\nimport torch.distributed as dist\nfrom torch.nn.parallel import DistributedDataParallel as DDP", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# initialize the process group (this must be called in the initialization of PyTorch DDP)\ndist.init_process_group(init_method='tcp://' + str(self.config.master_addr) + ':' +\nstr(self.config.master_port), backend=Backend.GLOO, rank=self.global_rank, world_size=self.world_size)\n...\n\n# create active process group (yellow color)\nself.active_process_group = dist.new_group(ranks=self.active_ranks, backend=Backend.NCCL, timeout=timedelta(days=365))\n...", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# create message process group (yellow color)\nself.comm_broadcast_group = dist.new_group(ranks=[i for i in range(self.world_size)], backend=Backend.GLOO, timeout=timedelta(days=365))\n...", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# create DDP-enabled model when the number of data-parallel workers is changed. Note:\n# 1. The process group to be used for distributed data all-reduction.\nIf None, the default process group, which is created by torch.distributed.init_process_group, will be used.\nIn our case, we set it as self.active_process_group\n# 2. device_ids should be set when the pipeline length = 1 (the model resides on a single CUDA device).", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "self.pipe_len = gpu_num_per_process\nif gpu_num_per_process > 1:\n model = DDP(model, process_group=self.active_process_group, find_unused_parameters=True)\nelse:\n model = DDP(model, device_ids=[self.local_rank], process_group=self.active_process_group, find_unused_parameters=True)", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# to broadcast message among processes, we use dist.broadcast_object_list\ndef dist_broadcast(object_list, src, group):\n \"\"\"Broadcasts a given object to all parties.\"\"\"\n dist.broadcast_object_list(object_list, src, group=group)\n return object_list\n```\nFor the complete source code, please refer to `https://github.com/Distributed-AI/PipeTransformer/blob/master/pipe_transformer/dp/auto_dp.py`.\n\n# Experiments", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "This section first summarizes experiment setups and then evaluates PipeTransformer using computer vision and natural language processing tasks.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Hardware. Experiments were conducted on 2 identical machines connected by InfiniBand CX353A (GB/s), where each machine is equipped with 8 NVIDIA Quadro RTX 5000 (16GB GPU memory). GPU-to-GPU bandwidth within a machine (PCI 3.0, 16 lanes) is GB/s.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Implementation. We used PyTorch Pipe as a building block. The BERT model definition, configuration, and related tokenizer are from HuggingFace 3.5.0. We implemented Vision Transformer using PyTorch by following its TensorFlow implementation. More details can be found in our source code.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Models and Datasets. Experiments employ two representative Transformers in CV and NLP: Vision Transformer (ViT) and BERT. ViT was run on an image classification task, initialized with pre-trained weights on ImageNet21K and fine-tuned on ImageNet and CIFAR-100. BERT was run on two tasks, text classification on the SST-2 dataset from the General Language Understanding Evaluation (GLUE) benchmark, and question answering on the SQuAD v1.1 Dataset (Stanford Question Answering), which is a", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "collection of 100k crowdsourced question/answer pairs.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Training Schemes. Given that large models normally would require thousands of GPU-days {\\emph{e.g.}, GPT-3) if trained from scratch, fine-tuning downstream tasks using pre-trained models has become a trend in CV and NLP communities. Moreover, PipeTransformer is a complex training system that involves multiple core components. Thus, for the first version of PipeTransformer system development and algorithmic research, it is not cost-efficient to develop and evaluate from scratch using", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "large-scale pre-training. Therefore, the experiments presented in this section focuses on pre-trained models. Note that since the model architectures in pre-training and fine-tuning are the same, PipeTransformer can serve both. We discussed pre-training results in the Appendix.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Baseline. Experiments in this section compare PipeTransformer to the state-of-the-art framework, a hybrid scheme of PyTorch Pipeline (PyTorch\u2019s implementation of GPipe) and PyTorch DDP. Since this is the first paper that studies accelerating distributed training by freezing layers, there are no perfectly aligned counterpart solutions yet.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Hyper-parameters. Experiments use ViT-B/16 (12 transformer layers, input patch size) for ImageNet and CIFAR-100, BERT-large-uncased (24 layers) for SQuAD 1.1, and BERT-base-uncased (12 layers) for SST-2. With PipeTransformer, ViT and BERT training can set the per-pipeline batch size to around 400 and 64, respectively. Other hyperparameters (e.g., epoch, learning rate) for all experiments are presented in", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Appendix.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Overall Training Acceleration\n

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "We summarize the overall experimental results in the table above. Note that the speedup we report is based on a conservative value that can obtain comparable or even higher accuracy. A more aggressive (, ) can obtain a higher speedup but may lead to a slight loss in accuracy. Note that the model size of BERT (24 layers) is larger than ViT-B/16 (12 layers), thus it takes more time for communication.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Performance Analysis", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Speedup Breakdown\n\nThis section presents evaluation results and analyzes the performance of different components in \\autopipe. More experimental results can be found in the Appendix.\n\n

\n\n
\nFigure 9. Speedup Breakdown (ViT on ImageNet)\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "To understand the efficacy of all four components and their impacts on training speed, we experimented with different combinations and used their training sample throughput (samples/second) and speedup ratio as metrics. Results are illustrated in Figure 9. Key takeaways from these experimental results are:", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "1. the main speedup is the result of elastic pipelining which is achieved through the joint use of AutoPipe and AutoDP;\n2. AutoCache's contribution is amplified by AutoDP;\n3. freeze training alone without system-wise adjustment even downgrades the training speed.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Tuning in Freezing Algorithm\n\n

\n\n
\nFigure 10. Tuning in Freezing Algorithm\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "We ran experiments to show how the in the freeze algorithms influences training speed. The result clearly demonstrates that a larger (excessive freeze) leads to a greater speedup but suffers from a slight performance degradation. In the case shown in Figure 10, where , freeze training", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "outperforms normal training and obtains a -fold speedup. We provide more results in the Appendix.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Optimal Chunks in the elastic pipeline\n\n

\n\n
\nFigure 11. Optimal chunk number in the elastic pipeline\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "We profiled the optimal number of micro-batches for different pipeline lengths . Results are summarized in Figure 11. As we can see, different values lead to different optimal , and the throughput gaps across different M values are large (as", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "shown when ), which confirms the necessity of an anterior profiler in elastic pipelining.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "Understanding the Timing of Caching\n\n

\n\n
\nFigure 12. the timing of caching\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "To evaluate AutoCache, we compared the sample throughput of training that activates AutoCache from epoch (blue) with the training job without AutoCache (red). Figure 12 shows that enabling caching too early can slow down training, as caching can be more expensive than the forward propagation on a small number of frozen layers. After more layers are frozen, caching activations clearly outperform the corresponding forward propagation. As a", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "result, AutoCache uses a profiler to determine the proper timing to enable caching. In our system, for ViT (12 layers), caching starts from 3 frozen layers, while for BERT (24 layers), caching starts from 5 frozen layers.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "For more detailed experimental analysis, please refer to our paper.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Summarization", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "This blog introduces PipeTransformer, a holistic solution that combines elastic pipeline-parallel and data-parallel for distributed training using PyTorch Distributed APIs. More specifically, PipeTransformer incrementally freezes layers in the pipeline, packs remaining active layers into fewer GPUs, and forks more pipeline replicas to increase the data-parallel width. Evaluations on ViT and BERT models show that compared to the state-of-the-art baseline, PipeTransformer attains up to 2.83\u00d7 speedups without", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "accuracy loss.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "# Reference\n\n[1] Li, S., Zhao, Y., Varma, R., Salpekar, O., Noordhuis, P., Li,T., Paszke, A., Smith, J., Vaughan, B., Damania, P., et al. Pytorch Distributed: Experiences on Accelerating Dataparallel Training. Proceedings of the VLDB Endowment,13(12), 2020\n\n[2] Devlin, J., Chang, M. W., Lee, K., and Toutanova, K. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. In NAACL-HLT, 2019", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[3] Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., Dehghani, M., Minderer, M., Heigold, G., Gelly, S., et al. An image is Worth 16x16 words: Transformers for Image Recognition at Scale.\n\n[4] Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., et al. Language Models are Few-shot Learners.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[5] Lepikhin, D., Lee, H., Xu, Y., Chen, D., Firat, O., Huang, Y., Krikun, M., Shazeer, N., and Chen, Z. Gshard: Scaling Giant Models with Conditional Computation and Automatic Sharding.\n\n[6] Li, M., Andersen, D. G., Park, J. W., Smola, A. J., Ahmed, A., Josifovski, V., Long, J., Shekita, E. J., and Su, B. Y. Scaling Distributed Machine Learning with the Parameter Server. In 11th {USENIX} Symposium on Operating Systems Design and Implementation ({OSDI} 14), pp. 583\u2013598, 2014.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[7] Jiang, Y., Zhu, Y., Lan, C., Yi, B., Cui, Y., and Guo, C. A Unified Architecture for Accelerating Distributed DNN Training in Heterogeneous GPU/CPU Clusters. In 14th USENIX Symposium on Operating Systems Design and Implementation (OSDI 20), pp. 463\u2013479. USENIX Association, November 2020. ISBN 978-1-939133-19- 9.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[8] Kim, S., Yu, G. I., Park, H., Cho, S., Jeong, E., Ha, H., Lee, S., Jeong, J. S., and Chun, B. G. Parallax: Sparsity-aware Data Parallel Training of Deep Neural Networks. In Proceedings of the Fourteenth EuroSys Conference 2019, pp. 1\u201315, 2019.\n\n[9] Kim, C., Lee, H., Jeong, M., Baek, W., Yoon, B., Kim, I., Lim, S., and Kim, S. TorchGPipe: On-the-fly Pipeline Parallelism for Training Giant Models.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[10] Huang, Y., Cheng, Y., Bapna, A., Firat, O., Chen, M. X., Chen, D., Lee, H., Ngiam, J., Le, Q. V., Wu, Y., et al. Gpipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[11] Park, J. H., Yun, G., Yi, C. M., Nguyen, N. T., Lee, S., Choi, J., Noh, S. H., and ri Choi, Y. Hetpipe: Enabling Large DNN Training on (whimpy) Heterogeneous GPU Clusters through Integration of Pipelined Model Parallelism and Data Parallelism. In 2020 USENIX Annual Technical Conference (USENIX ATC 20), pp. 307\u2013321. USENIX Association, July 2020. ISBN 978-1-939133- 14-4.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[12] Narayanan, D., Harlap, A., Phanishayee, A., Seshadri, V., Devanur, N. R., Ganger, G. R., Gibbons, P. B., and Zaharia, M. Pipedream: Generalized Pipeline Parallelism for DNN Training. In Proceedings of the 27th ACM Symposium on Operating Systems Principles, SOSP \u201919, pp. 1\u201315, New York, NY, USA, 2019. Association for Computing Machinery. ISBN 9781450368735. doi: 10.1145/3341301.3359646.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[13] Lepikhin, D., Lee, H., Xu, Y., Chen, D., Firat, O., Huang, Y., Krikun, M., Shazeer, N., and Chen, Z. Gshard: Scaling Giant Models with Conditional Computation and Automatic Sharding.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[14] Shazeer, N., Cheng, Y., Parmar, N., Tran, D., Vaswani, A., Koanantakool, P., Hawkins, P., Lee, H., Hong, M., Young, C., Sepassi, R., and Hechtman, B. Mesh-Tensorflow: Deep Learning for Supercomputers. In Bengio, S., Wallach, H., Larochelle, H., Grauman, K., Cesa-Bianchi, N., and Garnett, R. (eds.), Advances in Neural Information Processing Systems, volume 31, pp. 10414\u201310423. Curran Associates, Inc., 2018.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "[15] Shoeybi, M., Patwary, M., Puri, R., LeGresley, P., Casper, J., and Catanzaro, B. Megatron-LM: Training Multi-billion Parameter Language Models using Model Parallelism.\n\n[16] Rajbhandari, S., Rasley, J., Ruwase, O., and He, Y. ZERO: Memory Optimization towards Training a Trillion Parameter Models.\n\n[17] Raghu, M., Gilmer, J., Yosinski, J., and Sohl Dickstein, J. Svcca: Singular Vector Canonical Correlation Analysis for Deep Learning Dynamics and Interpretability. In NIPS, 2017.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "As shown in the animation (Figure 4), PipeTransformer is designed with four core building blocks to address the aforementioned challenges. First, we design a tunable and adaptive algorithm to generate signals that guide the selection of layers to freeze over different iterations (Freeze Algorithm). Once triggered by these signals, our elastic pipelining module (AutoPipe), then packs the remaining active layers into fewer GPUs by taking both activation sizes and variances of workloads across heterogeneous partitions (frozen layers and active layers) into account. It then splits a mini-batch into an optimal number of micro-batches based on prior profiling results for different pipeline lengths. Our next module, AutoDP, spawns additional pipeline replicas to occupy freed-up GPUs and maintains hierarchical communication process groups to attain dynamic membership for collective communications. Our final module, AutoCache, efficiently shares activations across existing and new data-parallel processes and automatically replaces stale caches during transitions.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Overall, PipeTransformer combines the Freeze Algorithm, AutoPipe, AutoDP, and AutoCache modules to provide a significant training speedup.\nWe evaluate PipeTransformer using Vision Transformer (ViT) on ImageNet and BERT on GLUE and SQuAD datasets. Our results show that PipeTransformer attains up to 2.83-fold speedup without losing accuracy. We also provide various performance analyses for a more comprehensive understanding of our algorithmic and system-wise design.\nFinally, we have also developed open-source flexible APIs for PipeTransformer, which offer a clean separation among the freeze algorithm, model definitions, and training accelerations, allowing for transferability to other algorithms that require similar freezing strategies.\n\n# Overall Design", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "# Overall Design\n\nSuppose we aim to train a massive model in a distributed training system where the hybrid of pipelined model parallelism and data parallelism is used to target scenarios where either the memory of a single GPU device cannot hold the model, or if loaded, the batch size is small enough to avoid running out of memory. More specifically, we define our settings as follows:\n\nTraining task and model definition. We train Transformer models (e.g., Vision Transformer, BERT on large-scale image or text datasets. The Transformer model has layers, in which the th layer is composed of a forward computation function and a corresponding set of parameters.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Training infrastructure. Assume the training infrastructure contains a GPU cluster that has GPU servers (i.e. nodes). Each node has GPUs. Our cluster is homogeneous, meaning that each GPU and server have the same hardware configuration. Each GPU's memory capacity is . Servers are connected by a high bandwidth network interface such as InfiniBand interconnect.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Pipeline parallelism. In each machine, we load a model into a pipeline which has partitions ( also represents the pipeline length). The th partition consists of consecutive layers. We assume each partition is handled by a single GPU device. , meaning that we can build multiple pipelines for multiple model replicas in a single machine. We assume all GPU devices in a pipeline belonging to the same machine. Our pipeline is a synchronous pipeline, which does not involve stale gradients, and the number of micro-batches is . In the Linux OS, each pipeline is handled by a single process. We refer the reader to GPipe [10] for more details.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Data parallelism. DDP is a cross-machine distributed data-parallel process group within parallel workers. Each worker is a pipeline replica (a single process). The th worker's index (ID) is rank . For any two pipelines in DDP, they can belong to either the same GPU server or different GPU servers, and they can exchange gradients with the AllReduce algorithm.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Under these settings, our goal is to accelerate training by leveraging freeze training, which does not require all layers to be trained throughout the duration of the training. Additionally, it may help save computation, communication, memory cost, and potentially prevent overfitting by consecutively freezing layers. However, these benefits can only be achieved by overcoming the four challenges of designing an adaptive freezing algorithm, dynamical pipeline re-partitioning, efficient resource reallocation, and cross-process caching, as discussed in the introduction.\n\n\n

\n\"Overview\"\n
\nFigure 5. Overview of PipeTransformer Training System\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "PipeTransformer co-designs an on-the-fly freeze algorithm and an automated elastic pipelining training system that can dynamically transform the scope of the pipelined model and the number of pipeline replicas. The overall system architecture is illustrated in Figure 5. To support PipeTransformer\u2019s elastic pipelining, we maintain a customized version of PyTorch Pipeline. For data parallelism, we use PyTorch DDP as a baseline. Other libraries are standard mechanisms of an operating system (e.g.,multi-processing) and thus avoid specialized software or hardware customization requirements. To ensure the generality of our framework, we have decoupled the training system into four core components: freeze algorithm, AutoPipe, AutoDP, and AutoCache. The freeze algorithm (grey) samples indicators from the training loop and makes layer-wise freezing decisions, which will be shared with AutoPipe (green). AutoPipe is an elastic pipeline module that speeds up training by excluding frozen layers from the pipeline and packing the active layers into fewer GPUs (pink), leading to both fewer cross-GPU communications and smaller pipeline bubbles. Subsequently, AutoPipe passes pipeline length information to AutoDP (purple), which then spawns more pipeline replicas to increase data-parallel width, if possible. The illustration also includes an example in which AutoDP introduces a new replica (purple). AutoCache (orange edges) is a cross-pipeline caching module, as illustrated by connections between pipelines. The source code architecture is aligned with Figure 5 for readability and generality.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "# Implementation Using PyTorch APIs\n\nAs can be seen from Figure 5, PipeTransformers contain four components: Freeze Algorithm, AutoPipe, AutoDP, and AutoCache. Among them, AutoPipe and AutoDP relies on PyTorch DDP (`torch.nn.parallel.DistributedDataParallel`) [1] and Pipeline (`torch.distributed.pipeline`), respectively. In this blog, we only highlight the key implementation details of AutoPipe and AutoDP. For details of Freeze Algorithm and AutoCache, please refer to our paper.\n\n## AutoPipe: Elastic Pipelining\n\nAutoPipe can accelerate training by excluding frozen layers from the pipeline and packing the active layers into fewer GPUs. This section elaborates on the key components of AutoPipe that dynamically 1) partition pipelines, 2) minimize the number of pipeline devices, and 3) optimize mini-batch chunk size accordingly.\n\n### Basic Usage of PyTorch Pipeline", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Before diving into details of AutoPipe, let us warm up the basic usage of PyTorch Pipeline (`torch.distributed.pipeline.sync.Pipe`, see [this tutorial](https://pytorch.org/docs/stable/pipeline.html)). More specially, we present a simple example to understand the design of Pipeline in practice:\n\n```python\n# Step 1: build a model including two linear layers\nfc1 = nn.Linear(16, 8).cuda(0)\nfc2 = nn.Linear(8, 4).cuda(1)\n\n# Step 2: wrap the two layers with nn.Sequential\nmodel = nn.Sequential(fc1, fc2)\n\n# Step 3: build Pipe (torch.distributed.pipeline.sync.Pipe)\nmodel = Pipe(model, chunks=8)\n\n# do training/inference\ninput = torch.rand(16, 16).cuda(0)\noutput_rref = model(input)\n```", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "In this basic example, we can see that before initializing `Pipe`, we need to partition the model `nn.Sequential` into multiple GPU devices and set optimal chunk number (`chunks`). Balancing computation time across partitions is critical to pipeline training speed, as skewed workload distributions across stages can lead to stragglers and forcing devices with lighter workloads to wait. The chunk number may also have a non-trivial influence on the throughput of the pipeline.\n\n\n### Balanced Pipeline Partitioning\n\nIn dynamic training system such as PipeTransformer, maintaining optimally balanced partitions in terms of parameter numbers does not guarantee the fastest training speed because other factors also play a crucial role:\n\n

\n\n
\nFigure 6. The partition boundary is in the middle of a skip connection\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "1. Cross-partition communication overhead. Placing a partition boundary in the middle of a skip connection leads to additional communications since tensors in the skip connection must now be copied to a different GPU. For example, with BERT partitions in Figure 6, partition must take intermediate outputs from both partition and partition . In contrast, if the boundary is placed after the addition layer, the communication overhead between partition and is visibly smaller. Our measurements show that having cross-device communication is more expensive than having slightly imbalanced partitions (see the Appendix in our paper). Therefore, we do not consider breaking skip connections (highlighted separately as an entire attention layer and MLP layer in green color at line 7 in Algorithm 1.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "2. Frozen layer memory footprint. During training, AutoPipe must recompute partition boundaries several times to balance two distinct types of layers: frozen layers and active layers. The frozen layer's memory cost is a fraction of that inactive layer, given that the frozen layer does not need backward activation maps, optimizer states, and gradients. Instead of launching intrusive profilers to obtain thorough metrics on memory and computational cost, we define a tunable cost factor to estimate the memory footprint ratio of a frozen layer over the same active layer. Based on empirical measurements in our experimental hardware, we set it to .\n\n\n\n

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Based on the above two considerations, AutoPipe balances pipeline partitions based on parameter sizes. More specifically, AutoPipe uses a greedy algorithm to allocate all frozen and active layers to evenly distribute partitioned sublayers into GPU devices. Pseudocode is described as the `load\\_balance()` function in Algorithm 1. The frozen layers are extracted from the original model and kept in a separate model instance in the first device of a pipeline.\n\nNote that the partition algorithm employed in this paper is not the only option; PipeTransformer is modularized to work with any alternatives.\n\n\n### Pipeline Compression", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Pipeline compression helps to free up GPUs to accommodate more pipeline replicas and reduce the number of cross-device communications between partitions. To determine the timing of compression, we can estimate the memory cost of the largest partition after compression, and then compare it with that of the largest partition of a pipeline at timestep . To avoid extensive memory profiling, the compression algorithm uses the parameter size as a proxy for the training memory footprint. Based on this simplification, the criterion of pipeline compression is as follows:\n\n

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Once the freeze notification is received, AutoPipe will always attempt to divide the pipeline length by 2 (e.g., from 8 to 4, then 2). By using as the input, the compression algorithm can verify if the result satisfies the criterion in Equation (1). Pseudocode is shown in lines 25-33 in Algorithm 1. Note that this compression makes the acceleration ratio exponentially increase during training, meaning that if a GPU server has a larger number of GPUs (e.g., more than 8), the acceleration ratio will be further amplified.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "

\n\n
\nFigure 7. Pipeline Bubble: , and denote forward, backward, and the optimizer update of micro-batch on device , respectively. The total bubble size in each iteration is times per micro-batch forward and backward cost.\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Additionally, such a technique can also speed up training by shrinking the size of pipeline bubbles. To explain bubble sizes in a pipeline, Figure 7 depicts how 4 micro-batches run through a 4-device pipeline . In general, the total bubble size is times per micro-batch forward and backward cost. Therefore, it is clear that shorter pipelines have smaller bubble sizes.\n\n### Dynamic Number of Micro-Batches", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Prior pipeline parallel systems use a fixed number of micro-batches per mini-batch ( ). GPipe suggests , where is the number of partitions (pipeline length). However, given that PipeTransformer dynamically configures , we find it to be sub-optimal to maintain a static during training. Moreover, when integrated with DDP, the value of also has an impact on the efficiency of DDP gradient synchronizations. Since DDP must wait for the last micro-batch to finish its backward computation on a parameter before launching its gradient synchronization, finer micro-batches lead to a smaller overlap between computation and communication. Hence, instead of using a static value, PipeTransformer searches for optimal on the fly in the hybrid of DDP environment by enumerating values ranging from to . For a specific training environment, the profiling needs only to be done once (see Algorithm 1 line 35).", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "For the complete source code, please refer to `https://github.com/Distributed-AI/PipeTransformer/blob/master/pipe_transformer/pipe/auto_pipe.py`.\n\n## AutoDP: Spawning More Pipeline Replicas\nAs AutoPipe compresses the same pipeline into fewer GPUs, AutoDP can automatically spawn new pipeline replicas to increase data-parallel width.\n\nDespite the conceptual simplicity, subtle dependencies on communications and states require careful design. The challenges are threefold:\n\n1. DDP Communication: Collective communications in PyTorch DDP requires static membership, which prevents new pipelines from connecting with existing ones;\n\n2. State Synchronization: newly activated processes must be consistent with existing pipelines in the training progress (e.g., epoch number and learning rate), weights and optimizer states, the boundary of frozen layers, and pipeline GPU range;", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "3. Dataset Redistribution: the dataset should be re-balanced to match a dynamic number of pipelines. This not only avoids stragglers but also ensures that gradients from all DDP processes are equally weighted.\n\n

\n\n
\nFigure 8. AutoDP: handling dynamical data-parallel with messaging between double process groups (Process 0-7 belong to machine 0, while process 8-15 belong to machine 1)\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "To tackle these challenges, we create double communication process groups for DDP. As in the example shown in Figure 8, the message process group (purple) is responsible for light-weight control messages and covers all processes, while the active training process group (yellow) only contains active processes and serves as a vehicle for heavy-weight tensor communications during training. The message group remains static, whereas the training group is dismantled and reconstructed to match active processes.\nIn T0, only processes 0 and 8 are active. During the transition to T1, process 0 activates processes 1 and 9 (newly added pipeline replicas) and synchronizes necessary information mentioned above using the message group. The four active processes then form a new training group, allowing static collective communications adaptive to dynamic memberships.\nTo redistribute the dataset, we implement a variant of DistributedSampler that can seamlessly adjust data samples to match the number of active pipeline replicas.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "The above design also naturally helps to reduce DDP communication overhead. More specifically, when transitioning from T0 to T1, processes 0 and 1 destroy the existing DDP instances, and active processes construct a new DDP training group using a cached pipelined model (AutoPipe stores frozen model and cached model separately).\n\nWe use the following APIs to implement the design above.\n\n```python\nimport torch.distributed as dist\nfrom torch.nn.parallel import DistributedDataParallel as DDP\n\n# initialize the process group (this must be called in the initialization of PyTorch DDP)\ndist.init_process_group(init_method='tcp://' + str(self.config.master_addr) + ':' +\nstr(self.config.master_port), backend=Backend.GLOO, rank=self.global_rank, world_size=self.world_size)\n...\n\n# create active process group (yellow color)\nself.active_process_group = dist.new_group(ranks=self.active_ranks, backend=Backend.NCCL, timeout=timedelta(days=365))\n...", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "# create message process group (yellow color)\nself.comm_broadcast_group = dist.new_group(ranks=[i for i in range(self.world_size)], backend=Backend.GLOO, timeout=timedelta(days=365))\n...\n\n# create DDP-enabled model when the number of data-parallel workers is changed. Note:\n# 1. The process group to be used for distributed data all-reduction.\nIf None, the default process group, which is created by torch.distributed.init_process_group, will be used.\nIn our case, we set it as self.active_process_group\n# 2. device_ids should be set when the pipeline length = 1 (the model resides on a single CUDA device).\n\nself.pipe_len = gpu_num_per_process\nif gpu_num_per_process > 1:\n model = DDP(model, process_group=self.active_process_group, find_unused_parameters=True)\nelse:\n model = DDP(model, device_ids=[self.local_rank], process_group=self.active_process_group, find_unused_parameters=True)", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "# to broadcast message among processes, we use dist.broadcast_object_list\ndef dist_broadcast(object_list, src, group):\n \"\"\"Broadcasts a given object to all parties.\"\"\"\n dist.broadcast_object_list(object_list, src, group=group)\n return object_list\n```\nFor the complete source code, please refer to `https://github.com/Distributed-AI/PipeTransformer/blob/master/pipe_transformer/dp/auto_dp.py`.\n\n# Experiments\n\nThis section first summarizes experiment setups and then evaluates PipeTransformer using computer vision and natural language processing tasks.\n\nHardware. Experiments were conducted on 2 identical machines connected by InfiniBand CX353A (GB/s), where each machine is equipped with 8 NVIDIA Quadro RTX 5000 (16GB GPU memory). GPU-to-GPU bandwidth within a machine (PCI 3.0, 16 lanes) is GB/s.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Implementation. We used PyTorch Pipe as a building block. The BERT model definition, configuration, and related tokenizer are from HuggingFace 3.5.0. We implemented Vision Transformer using PyTorch by following its TensorFlow implementation. More details can be found in our source code.\n\nModels and Datasets. Experiments employ two representative Transformers in CV and NLP: Vision Transformer (ViT) and BERT. ViT was run on an image classification task, initialized with pre-trained weights on ImageNet21K and fine-tuned on ImageNet and CIFAR-100. BERT was run on two tasks, text classification on the SST-2 dataset from the General Language Understanding Evaluation (GLUE) benchmark, and question answering on the SQuAD v1.1 Dataset (Stanford Question Answering), which is a collection of 100k crowdsourced question/answer pairs.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Training Schemes. Given that large models normally would require thousands of GPU-days {\\emph{e.g.}, GPT-3) if trained from scratch, fine-tuning downstream tasks using pre-trained models has become a trend in CV and NLP communities. Moreover, PipeTransformer is a complex training system that involves multiple core components. Thus, for the first version of PipeTransformer system development and algorithmic research, it is not cost-efficient to develop and evaluate from scratch using large-scale pre-training. Therefore, the experiments presented in this section focuses on pre-trained models. Note that since the model architectures in pre-training and fine-tuning are the same, PipeTransformer can serve both. We discussed pre-training results in the Appendix.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "Baseline. Experiments in this section compare PipeTransformer to the state-of-the-art framework, a hybrid scheme of PyTorch Pipeline (PyTorch\u2019s implementation of GPipe) and PyTorch DDP. Since this is the first paper that studies accelerating distributed training by freezing layers, there are no perfectly aligned counterpart solutions yet.\n\nHyper-parameters. Experiments use ViT-B/16 (12 transformer layers, input patch size) for ImageNet and CIFAR-100, BERT-large-uncased (24 layers) for SQuAD 1.1, and BERT-base-uncased (12 layers) for SST-2. With PipeTransformer, ViT and BERT training can set the per-pipeline batch size to around 400 and 64, respectively. Other hyperparameters (e.g., epoch, learning rate) for all experiments are presented in Appendix.\n\n## Overall Training Acceleration\n

\n\n
\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "We summarize the overall experimental results in the table above. Note that the speedup we report is based on a conservative value that can obtain comparable or even higher accuracy. A more aggressive (, ) can obtain a higher speedup but may lead to a slight loss in accuracy. Note that the model size of BERT (24 layers) is larger than ViT-B/16 (12 layers), thus it takes more time for communication.\n\n## Performance Analysis\n\n### Speedup Breakdown\n\nThis section presents evaluation results and analyzes the performance of different components in \\autopipe. More experimental results can be found in the Appendix.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "

\n\n
\nFigure 9. Speedup Breakdown (ViT on ImageNet)\n

\n\nTo understand the efficacy of all four components and their impacts on training speed, we experimented with different combinations and used their training sample throughput (samples/second) and speedup ratio as metrics. Results are illustrated in Figure 9. Key takeaways from these experimental results are:\n\n1. the main speedup is the result of elastic pipelining which is achieved through the joint use of AutoPipe and AutoDP;\n2. AutoCache's contribution is amplified by AutoDP;\n3. freeze training alone without system-wise adjustment even downgrades the training speed.\n\n### Tuning in Freezing Algorithm", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "

\n\n
\nFigure 10. Tuning in Freezing Algorithm\n

\n\nWe ran experiments to show how the in the freeze algorithms influences training speed. The result clearly demonstrates that a larger (excessive freeze) leads to a greater speedup but suffers from a slight performance degradation. In the case shown in Figure 10, where , freeze training outperforms normal training and obtains a -fold speedup. We provide more results in the Appendix.\n\n### Optimal Chunks in the elastic pipeline", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "

\n\n
\nFigure 11. Optimal chunk number in the elastic pipeline\n

\n\nWe profiled the optimal number of micro-batches for different pipeline lengths . Results are summarized in Figure 11. As we can see, different values lead to different optimal , and the throughput gaps across different M values are large (as shown when ), which confirms the necessity of an anterior profiler in elastic pipelining.\n\n### Understanding the Timing of Caching\n\n

\n\n
\nFigure 12. the timing of caching\n

", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "To evaluate AutoCache, we compared the sample throughput of training that activates AutoCache from epoch (blue) with the training job without AutoCache (red). Figure 12 shows that enabling caching too early can slow down training, as caching can be more expensive than the forward propagation on a small number of frozen layers. After more layers are frozen, caching activations clearly outperform the corresponding forward propagation. As a result, AutoCache uses a profiler to determine the proper timing to enable caching. In our system, for ViT (12 layers), caching starts from 3 frozen layers, while for BERT (24 layers), caching starts from 5 frozen layers.\n\nFor more detailed experimental analysis, please refer to our paper.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "# Summarization\nThis blog introduces PipeTransformer, a holistic solution that combines elastic pipeline-parallel and data-parallel for distributed training using PyTorch Distributed APIs. More specifically, PipeTransformer incrementally freezes layers in the pipeline, packs remaining active layers into fewer GPUs, and forks more pipeline replicas to increase the data-parallel width. Evaluations on ViT and BERT models show that compared to the state-of-the-art baseline, PipeTransformer attains up to 2.83\u00d7 speedups without accuracy loss.\n\n\n# Reference\n\n[1] Li, S., Zhao, Y., Varma, R., Salpekar, O., Noordhuis, P., Li,T., Paszke, A., Smith, J., Vaughan, B., Damania, P., et al. Pytorch Distributed: Experiences on Accelerating Dataparallel Training. Proceedings of the VLDB Endowment,13(12), 2020\n\n[2] Devlin, J., Chang, M. W., Lee, K., and Toutanova, K. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. In NAACL-HLT, 2019", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "[3] Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., Dehghani, M., Minderer, M., Heigold, G., Gelly, S., et al. An image is Worth 16x16 words: Transformers for Image Recognition at Scale.\n\n[4] Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., et al. Language Models are Few-shot Learners.\n\n[5] Lepikhin, D., Lee, H., Xu, Y., Chen, D., Firat, O., Huang, Y., Krikun, M., Shazeer, N., and Chen, Z. Gshard: Scaling Giant Models with Conditional Computation and Automatic Sharding.\n\n[6] Li, M., Andersen, D. G., Park, J. W., Smola, A. J., Ahmed, A., Josifovski, V., Long, J., Shekita, E. J., and Su, B. Y. Scaling Distributed Machine Learning with the Parameter Server. In 11th {USENIX} Symposium on Operating Systems Design and Implementation ({OSDI} 14), pp. 583\u2013598, 2014.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "[7] Jiang, Y., Zhu, Y., Lan, C., Yi, B., Cui, Y., and Guo, C. A Unified Architecture for Accelerating Distributed DNN Training in Heterogeneous GPU/CPU Clusters. In 14th USENIX Symposium on Operating Systems Design and Implementation (OSDI 20), pp. 463\u2013479. USENIX Association, November 2020. ISBN 978-1-939133-19- 9.\n\n[8] Kim, S., Yu, G. I., Park, H., Cho, S., Jeong, E., Ha, H., Lee, S., Jeong, J. S., and Chun, B. G. Parallax: Sparsity-aware Data Parallel Training of Deep Neural Networks. In Proceedings of the Fourteenth EuroSys Conference 2019, pp. 1\u201315, 2019.\n\n[9] Kim, C., Lee, H., Jeong, M., Baek, W., Yoon, B., Kim, I., Lim, S., and Kim, S. TorchGPipe: On-the-fly Pipeline Parallelism for Training Giant Models.\n\n[10] Huang, Y., Cheng, Y., Bapna, A., Firat, O., Chen, M. X., Chen, D., Lee, H., Ngiam, J., Le, Q. V., Wu, Y., et al. Gpipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "[11] Park, J. H., Yun, G., Yi, C. M., Nguyen, N. T., Lee, S., Choi, J., Noh, S. H., and ri Choi, Y. Hetpipe: Enabling Large DNN Training on (whimpy) Heterogeneous GPU Clusters through Integration of Pipelined Model Parallelism and Data Parallelism. In 2020 USENIX Annual Technical Conference (USENIX ATC 20), pp. 307\u2013321. USENIX Association, July 2020. ISBN 978-1-939133- 14-4.\n\n[12] Narayanan, D., Harlap, A., Phanishayee, A., Seshadri, V., Devanur, N. R., Ganger, G. R., Gibbons, P. B., and Zaharia, M. Pipedream: Generalized Pipeline Parallelism for DNN Training. In Proceedings of the 27th ACM Symposium on Operating Systems Principles, SOSP \u201919, pp. 1\u201315, New York, NY, USA, 2019. Association for Computing Machinery. ISBN 9781450368735. doi: 10.1145/3341301.3359646.\n\n[13] Lepikhin, D., Lee, H., Xu, Y., Chen, D., Firat, O., Huang, Y., Krikun, M., Shazeer, N., and Chen, Z. Gshard: Scaling Giant Models with Conditional Computation and Automatic Sharding.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} +{"page_content": "[14] Shazeer, N., Cheng, Y., Parmar, N., Tran, D., Vaswani, A., Koanantakool, P., Hawkins, P., Lee, H., Hong, M., Young, C., Sepassi, R., and Hechtman, B. Mesh-Tensorflow: Deep Learning for Supercomputers. In Bengio, S., Wallach, H., Larochelle, H., Grauman, K., Cesa-Bianchi, N., and Garnett, R. (eds.), Advances in Neural Information Processing Systems, volume 31, pp. 10414\u201310423. Curran Associates, Inc., 2018.\n\n[15] Shoeybi, M., Patwary, M., Puri, R., LeGresley, P., Casper, J., and Catanzaro, B. Megatron-LM: Training Multi-billion Parameter Language Models using Model Parallelism.\n\n[16] Rajbhandari, S., Rasley, J., Ruwase, O., and He, Y. ZERO: Memory Optimization towards Training a Trillion Parameter Models.\n\n[17] Raghu, M., Gilmer, J., Yosinski, J., and Sohl Dickstein, J. Svcca: Singular Vector Canonical Correlation Analysis for Deep Learning Dynamics and Interpretability. In NIPS, 2017.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} {"page_content": "[18] Morcos, A., Raghu, M., and Bengio, S. Insights on Representational Similarity in Neural Networks with Canonical Correlation. In Bengio, S., Wallach, H., Larochelle, H., Grauman, K., Cesa-Bianchi, N., and Garnett, R. (eds.), Advances in Neural Information Processing Systems 31, pp. 5732\u20135741. Curran Associates, Inc., 2018.", "metadata": {"source": "https://pytorch.org/blog/pipetransformer-automated-elastic-pipelining/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Model Serving in PyTorch'\nauthor: Jeff Smith\nredirect_from: /2019/05/08/model-serving-in-pyorch.html\n---", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "PyTorch has seen a lot of adoption in research, but people can get confused about how well PyTorch models can be taken into production. This blog post is meant to clear up any confusion people might have about the road to production in PyTorch.\nUsually when people talk about taking a model \u201cto production,\u201d they usually mean performing **inference**, sometimes called model evaluation or prediction or serving. At the level of a function call, in PyTorch, inference looks something like this:", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "* In Python\n * `module(input)`\n* In traced modules\n * `module(input)`\n* In C++\n * `at::Tensor output = module->forward(inputs).toTensor();`\n\nSince we at Facebook perform inference operations using PyTorch hundreds of trillions of times per day, we've done a lot to make sure that inference runs as efficiently as possible.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Serving Strategies\n\nThat zoomed-in view of how you use models in inference isn't usually the whole story, though. In a real world machine learning system, you often need to do more than just run a single inference operation in the REPL or Jupyter notebook. Instead, you usually need to integrate your model into a larger application in some way. Depending on what you need to do, you can usually take one of the following approaches.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Direct embedding", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "In application settings like mobile, we often just directly call the model as part of a larger program. This isn't just for apps; usually this is how robotics and dedicated devices work as well. At a code-level, the call to the model is exactly the same as what is shown above in the section about inference shown above. A key concern is often that a Python interpreter is not present in such environments, which is why PyTorch allows you to call your models from C++ and ship a model without the need for a", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Python runtime.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Model microservices", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "If you're using your model in a server side context and you're managing multiple models, you might choose to treat each individual model (or each individual model version) as a separate service, usually using some sort of packaging mechanism like a Docker container. Then that service is often made network accessible via some sort of service, either using JSON over HTTP or an RPC technology like gRPC. The key characteristic of this approach is that you're defining a service with a single endpoint that just", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "calls your model. Then you do do all of your model management (promotion, rollback, etc.) via whatever system you already use to manage your services (e.g. kubernetes, ECS).", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Model servers", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "An additional possible solution is to use a model server. This is an application built to manage and serve models. It allows you to upload multiple models and get distinct prediction endpoints for each of them. Typically such systems include a number of other features to help solve more of the whole problem of managing and serving models. This can include things like metrics, visualization, data pre-processing, and more. Even something as simple as having a system for automatically versioning models can", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "make building important features like model rollbacks much easier.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Evolving Patterns", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "The above is a somewhat arbitrary breakdown of different approaches based on a snapshot in time. Design patterns are still evolving. Recently, model server designs have started to adopt more of the technologies of general service infrastructure such as Docker containers and kubernetes, so many model servers have started to share properties of the model microservice design discussed above. For a deeper dive into the general concepts of model server designs, you can check out my [book on machine learning", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "systems](https://www.manning.com/books/machine-learning-systems).", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Serving PyTorch Models\n\nSo, if you're a PyTorch user, what should you use if you want to take your models to production?", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "If you're on mobile or working on an embedded system like a robot, direct embedding in your application is often the right choice. \nFor mobile specifically, your use case might be served by the ONNX export functionality.\nNote that ONNX, by its very nature, has limitations and doesn't support all of the functionality provided by the larger PyTorch project.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "You can check out [this tutorial](https://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html) on deploying PyTorch models to mobile using ONNX to see if this path might suit your use case. \nThat said, we've heard that there's a lot more that PyTorch users want to do on mobile, so look for more mobile-specific functionality in PyTorch in the future.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "For other embedded systems, like robots, running [inference on a PyTorch model from the C++ API](https://pytorch.org/tutorials/advanced/cpp_export.html) could be the right solution.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "If you can't use the cloud or prefer to manage all services using the same technology, you can follow [this example](https://medium.com/datadriveninvestor/deploy-your-pytorch-model-to-production-f69460192217) to build a simple model microservice using the Flask web framework.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "If you want to manage multiple models within a non-cloud service solution, there are teams developing PyTorch support in model servers like [MLFlow](https://mlflow.org/), [Kubeflow](https://www.kubeflow.org/), and [RedisAI.](https://oss.redislabs.com/redisai/) We're excited to see innovation from multiple teams building OSS model servers, and we'll continue to highlight innovation in the PyTorch ecosystem in the future.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "If you can use the cloud for your application, there are several great choices for working with models in the cloud. For AWS Sagemaker, you can start find a guide to [all of the resources from AWS for working with PyTorch](https://docs.aws.amazon.com/sagemaker/latest/dg/pytorch.html), including docs on how to use the [Sagemaker Python SDK](https://sagemaker.readthedocs.io/en/stable/using_pytorch.html). You can also see [some](https://youtu.be/5h1Ot2dPi2E) [talks](https://youtu.be/qc5ZikKw9_w) we've given on", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "using PyTorch on Sagemaker. Finally, if you happen to be using PyTorch via FastAI, then they've written a [really simple guide](https://course.fast.ai/deployment_amzn_sagemaker.html) to getting up and running on Sagemaker.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "The story is similar across other major clouds. On Google Cloud, you can follow [these instructions](https://cloud.google.com/deep-learning-vm/docs/pytorch_start_instance) to get access to a Deep Learning VM with PyTorch pre-installed. On Microsoft Azure, you have a number of ways to get started from [Azure Machine Learning Service](https://azure.microsoft.com/en-us/services/machine-learning-service/) to [Azure Notebooks](https://notebooks.azure.com/pytorch/projects/tutorials) showing how to use PyTorch.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} -{"page_content": "Your Models\n\nWhichever approach you take to bringing your PyTorch models to production, we want to support you and enable your success. Do you love one of the options above? Are you having difficulty with that one crucial feature you can't find support for? We'd love to discuss more on the [deployment category](https://discuss.pytorch.org/c/deployment) on the PyTorch Discuss forums. We'd love to help, and where you're seeing success, amplify your story.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Model Serving in PyTorch'\nauthor: Jeff Smith\nredirect_from: /2019/05/08/model-serving-in-pyorch.html\n---\n\nPyTorch has seen a lot of adoption in research, but people can get confused about how well PyTorch models can be taken into production. This blog post is meant to clear up any confusion people might have about the road to production in PyTorch.\nUsually when people talk about taking a model \u201cto production,\u201d they usually mean performing **inference**, sometimes called model evaluation or prediction or serving. At the level of a function call, in PyTorch, inference looks something like this:\n\n* In Python\n * `module(input)`\n* In traced modules\n * `module(input)`\n* In C++\n * `at::Tensor output = module->forward(inputs).toTensor();`\n\nSince we at Facebook perform inference operations using PyTorch hundreds of trillions of times per day, we've done a lot to make sure that inference runs as efficiently as possible.\n\n## Serving Strategies", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "That zoomed-in view of how you use models in inference isn't usually the whole story, though. In a real world machine learning system, you often need to do more than just run a single inference operation in the REPL or Jupyter notebook. Instead, you usually need to integrate your model into a larger application in some way. Depending on what you need to do, you can usually take one of the following approaches.\n\n### Direct embedding\n\nIn application settings like mobile, we often just directly call the model as part of a larger program. This isn't just for apps; usually this is how robotics and dedicated devices work as well. At a code-level, the call to the model is exactly the same as what is shown above in the section about inference shown above. A key concern is often that a Python interpreter is not present in such environments, which is why PyTorch allows you to call your models from C++ and ship a model without the need for a Python runtime.\n\n### Model microservices", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "If you're using your model in a server side context and you're managing multiple models, you might choose to treat each individual model (or each individual model version) as a separate service, usually using some sort of packaging mechanism like a Docker container. Then that service is often made network accessible via some sort of service, either using JSON over HTTP or an RPC technology like gRPC. The key characteristic of this approach is that you're defining a service with a single endpoint that just calls your model. Then you do do all of your model management (promotion, rollback, etc.) via whatever system you already use to manage your services (e.g. kubernetes, ECS).\n\n### Model servers", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "### Model servers\n\nAn additional possible solution is to use a model server. This is an application built to manage and serve models. It allows you to upload multiple models and get distinct prediction endpoints for each of them. Typically such systems include a number of other features to help solve more of the whole problem of managing and serving models. This can include things like metrics, visualization, data pre-processing, and more. Even something as simple as having a system for automatically versioning models can make building important features like model rollbacks much easier.\n\n### Evolving Patterns", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "The above is a somewhat arbitrary breakdown of different approaches based on a snapshot in time. Design patterns are still evolving. Recently, model server designs have started to adopt more of the technologies of general service infrastructure such as Docker containers and kubernetes, so many model servers have started to share properties of the model microservice design discussed above. For a deeper dive into the general concepts of model server designs, you can check out my [book on machine learning systems](https://www.manning.com/books/machine-learning-systems).\n\n## Serving PyTorch Models\n\nSo, if you're a PyTorch user, what should you use if you want to take your models to production?", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "If you're on mobile or working on an embedded system like a robot, direct embedding in your application is often the right choice. \nFor mobile specifically, your use case might be served by the ONNX export functionality.\nNote that ONNX, by its very nature, has limitations and doesn't support all of the functionality provided by the larger PyTorch project.\nYou can check out [this tutorial](https://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html) on deploying PyTorch models to mobile using ONNX to see if this path might suit your use case. \nThat said, we've heard that there's a lot more that PyTorch users want to do on mobile, so look for more mobile-specific functionality in PyTorch in the future.\nFor other embedded systems, like robots, running [inference on a PyTorch model from the C++ API](https://pytorch.org/tutorials/advanced/cpp_export.html) could be the right solution.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "If you can't use the cloud or prefer to manage all services using the same technology, you can follow [this example](https://medium.com/datadriveninvestor/deploy-your-pytorch-model-to-production-f69460192217) to build a simple model microservice using the Flask web framework.\n\nIf you want to manage multiple models within a non-cloud service solution, there are teams developing PyTorch support in model servers like [MLFlow](https://mlflow.org/), [Kubeflow](https://www.kubeflow.org/), and [RedisAI.](https://oss.redislabs.com/redisai/) We're excited to see innovation from multiple teams building OSS model servers, and we'll continue to highlight innovation in the PyTorch ecosystem in the future.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "If you can use the cloud for your application, there are several great choices for working with models in the cloud. For AWS Sagemaker, you can start find a guide to [all of the resources from AWS for working with PyTorch](https://docs.aws.amazon.com/sagemaker/latest/dg/pytorch.html), including docs on how to use the [Sagemaker Python SDK](https://sagemaker.readthedocs.io/en/stable/using_pytorch.html). You can also see [some](https://youtu.be/5h1Ot2dPi2E) [talks](https://youtu.be/qc5ZikKw9_w) we've given on using PyTorch on Sagemaker. Finally, if you happen to be using PyTorch via FastAI, then they've written a [really simple guide](https://course.fast.ai/deployment_amzn_sagemaker.html) to getting up and running on Sagemaker.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} +{"page_content": "The story is similar across other major clouds. On Google Cloud, you can follow [these instructions](https://cloud.google.com/deep-learning-vm/docs/pytorch_start_instance) to get access to a Deep Learning VM with PyTorch pre-installed. On Microsoft Azure, you have a number of ways to get started from [Azure Machine Learning Service](https://azure.microsoft.com/en-us/services/machine-learning-service/) to [Azure Notebooks](https://notebooks.azure.com/pytorch/projects/tutorials) showing how to use PyTorch.\n\n## Your Models\n\nWhichever approach you take to bringing your PyTorch models to production, we want to support you and enable your success. Do you love one of the options above? Are you having difficulty with that one crucial feature you can't find support for? We'd love to discuss more on the [deployment category](https://discuss.pytorch.org/c/deployment) on the PyTorch Discuss forums. We'd love to help, and where you're seeing success, amplify your story.", "metadata": {"source": "https://pytorch.org/blog/model-serving-in-pyorch/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'Accelerating PyTorch with CUDA Graphs'\nauthor: Vinh Nguyen, Michael Carilli, Sukru Burc Eryilmaz, Vartika Singh, Michelle Lin, Natalia Gimelshein, Alban Desmaison, Edward Yang\nfeatured-img: 'assets/images/cudagraphs-pytorch.png'\n---", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Today, we are pleased to announce a new advanced CUDA feature, CUDA Graphs, has been brought to PyTorch. Modern DL frameworks have complicated software stacks that incur significant overheads associated with the submission of each operation to the GPU. When DL workloads are strong-scaled to many GPUs for performance, the time taken by each GPU operation diminishes to just a few microseconds and, in these cases, the high work submission latencies of frameworks often lead to low utilization of the GPU. As", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "GPUs get faster and workloads are scaled to more devices, the likelihood of workloads suffering from these launch-induced stalls increases. To overcome these performance overheads, NVIDIA engineers worked with PyTorch developers to enable CUDA graph execution natively in PyTorch. This design was instrumental in scaling NVIDIA\u2019s MLPerf workloads (implemented in PyTorch) to over 4000 GPUs in order to achieve [record-breaking performance](https://blogs.nvidia.com/blog/2021/06/30/mlperf-ai-training-partners/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Today, we are pleased to announce a new advanced CUDA feature, CUDA Graphs, has been brought to PyTorch. Modern DL frameworks have complicated software stacks that incur significant overheads associated with the submission of each operation to the GPU. When DL workloads are strong-scaled to many GPUs for performance, the time taken by each GPU operation diminishes to just a few microseconds and, in these cases, the high work submission latencies of frameworks often lead to low utilization of the GPU. As GPUs get faster and workloads are scaled to more devices, the likelihood of workloads suffering from these launch-induced stalls increases. To overcome these performance overheads, NVIDIA engineers worked with PyTorch developers to enable CUDA graph execution natively in PyTorch. This design was instrumental in scaling NVIDIA\u2019s MLPerf workloads (implemented in PyTorch) to over 4000 GPUs in order to achieve [record-breaking performance](https://blogs.nvidia.com/blog/2021/06/30/mlperf-ai-training-partners/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} {"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs support in PyTorch is just one more example of a long collaboration between NVIDIA and Facebook engineers. [torch.cuda.amp](https://pytorch.org/docs/stable/amp.html), for example, trains with half precision while maintaining the network accuracy achieved with single precision and automatically utilizing tensor cores wherever possible. AMP delivers up to 3X higher performance than FP32 with just a few lines of code change. Similarly, NVIDIA\u2019s [Megatron-LM](https://github.com/NVIDIA/Megatron-LM)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "was trained using PyTorch on up to 3072 GPUs. In PyTorch, one of the most performant methods to scale-out GPU training is with [torch.nn.parallel.DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel) coupled with the NVIDIA Collective Communications Library ([NCCL](https://developer.nvidia.com/nccl)) backend.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "CUDA graphs support in PyTorch is just one more example of a long collaboration between NVIDIA and Facebook engineers. [torch.cuda.amp](https://pytorch.org/docs/stable/amp.html), for example, trains with half precision while maintaining the network accuracy achieved with single precision and automatically utilizing tensor cores wherever possible. AMP delivers up to 3X higher performance than FP32 with just a few lines of code change. Similarly, NVIDIA\u2019s [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) was trained using PyTorch on up to 3072 GPUs. In PyTorch, one of the most performant methods to scale-out GPU training is with [torch.nn.parallel.DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel) coupled with the NVIDIA Collective Communications Library ([NCCL](https://developer.nvidia.com/nccl)) backend.\n\n\n# CUDA Graphs", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} {"page_content": "# CUDA Graphs\n\n\n[CUDA Graphs](https://developer.nvidia.com/blog/cuda-10-features-revealed/), which made its debut in CUDA 10, let a series of CUDA kernels to be defined and encapsulated as a single unit, i.e., a graph of operations, rather than a sequence of individually-launched operations. It provides a mechanism to launch multiple GPU operations through a single CPU operation, and hence reduces the launching overheads.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "The benefits of CUDA graphs can be demonstrated with the simple example in Figure 1. On the top, a sequence of short kernels is launched one-by-one by the CPU. The CPU launching overhead creates a significant gap in between the kernels. If we replace this sequence of kernels with a CUDA graph, initially we will need to spend a little extra time on building the graph and launching the whole graph in one go on the first occasion, but subsequent executions will be very fast, as there will be very little gap", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "between the kernels. The difference is more pronounced when the same sequence of operations is repeated many times, for example, overy many training steps. In that case, the initial costs of building and launching the graph will be amortized over the entire number of training iterations. For a more comprehensive introduction on the topic, see our blog", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "[Getting Started with CUDA Graphs](https://developer.nvidia.com/blog/cuda-graphs) and GTC talk [Effortless CUDA Graphs](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s32082/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\"Cuda\n
\n\tFigure 1. Benefits of using CUDA graphs\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "NCCL support for CUDA graphs\n\n\nThe previously mentioned benefits of reducing launch overheads also extend to NCCL kernel launches. NCCL enables GPU-based collective and P2P communications. With [NCCL support for CUDA graphs](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/usage/cudagraph.html), we can eliminate the NCCL kernel launch overhead.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Additionally, kernel launch timing can be unpredictable due to various CPU load and operating system factors. Such time skews can be harmful to the performance of NCCL collective operations. With CUDA graphs, kernels are clustered together so that performance is consistent across ranks in a distributed workload. This is especially useful in large clusters where even a single slow node can bring down overall cluster level performance.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "For distributed multi-GPU workloads, NCCL is used for collective communications. If we look at training a neural network that leverages data parallelism, without NCCL support for CUDA graphs, we\u2019ll need a separate launch for each of forward/back propagation and NCCL AllReduce. By contrast, with NCCL support for CUDA graphs, we can reduce launch overhead by lumping together the forward/backward propagation and NCCL AllReduce all in a single graph launch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\"With\n
\n Figure 2. Looking at a typical neural network, all the kernel launches for NCCL AllReduce can be bundled into a graph to reduce overhead launch time.\n

\n\n\n# PyTorch CUDA Graphs", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "From PyTorch v1.10, the CUDA graphs functionality is made available as a set of beta APIs.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "API overview", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "PyTorch supports the construction of CUDA graphs using [stream capture](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#creating-a-graph-using-stream-capture), which puts a CUDA stream in capture mode. CUDA work issued to a capturing stream doesn\u2019t actually run on the GPU. Instead, the work is recorded in a graph. After capture, the graph can be launched to run the GPU work as many times as needed. Each replay runs the same kernels with the same arguments. For pointer arguments this means", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "the same memory addresses are used. By filling input memory with new data (e.g., from a new batch) before each replay, you can rerun the same work on new data.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Replaying a graph sacrifices the dynamic flexibility of typical eager execution in exchange for greatly reduced CPU overhead. A graph\u2019s arguments and kernels are fixed, so a graph replay skips all layers of argument setup and kernel dispatch, including Python, C++, and CUDA driver overheads. Under the hood, a replay submits the entire graph\u2019s work to the GPU with a single call to", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "[cudaGraphLaunch](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__GRAPH.html#group__CUDART__GRAPH_1g1accfe1da0c605a577c22d9751a09597). Kernels in a replay also execute slightly faster on the GPU, but eliding CPU overhead is the main benefit.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "You should try CUDA graphs if all or part of your network is graph-safe (usually this means static shapes and static control flow, but see the other [constraints](https://pytorch.org/docs/master/notes/cuda.html#constraints)) and you suspect its runtime is at least somewhat CPU-limited.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "API example\n\nPyTorch exposes graphs via a raw [`torch.cuda.CUDAGraph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph)class and two convenience wrappers, [`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph) and [`torch.cuda.make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "[`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph) is a simple, versatile context manager that captures CUDA work in its context. Before capture, warm up the workload to be captured by running a few eager iterations. Warmup must occur on a side stream. Because the graph reads from and writes to the same memory addresses in every replay, you must maintain long-lived references to tensors that hold input and output data during capture. To run the graph on", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "new input data, copy new data to the capture\u2019s input tensor(s), replay the graph, then read the new output from the capture\u2019s output tensor(s).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "If the entire network is capture safe, one can capture and replay the whole network as in the following example. \n\n```python\nN, D_in, H, D_out = 640, 4096, 2048, 1024\nmodel = torch.nn.Sequential(torch.nn.Linear(D_in, H),\n torch.nn.Dropout(p=0.2),\n torch.nn.Linear(H, D_out),\n torch.nn.Dropout(p=0.1)).cuda()\nloss_fn = torch.nn.MSELoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=0.1)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "# Placeholders used for capture\nstatic_input = torch.randn(N, D_in, device='cuda')\nstatic_target = torch.randn(N, D_out, device='cuda')", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "# warmup\n# Uses static_input and static_target here for convenience,\n# but in a real setting, because the warmup includes optimizer.step()\n# you must use a few batches of real data.\ns = torch.cuda.Stream()\ns.wait_stream(torch.cuda.current_stream())\nwith torch.cuda.stream(s):\n for i in range(3):\n optimizer.zero_grad(set_to_none=True)\n y_pred = model(static_input)\n loss = loss_fn(y_pred, static_target)\n loss.backward()\n optimizer.step()", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "torch.cuda.current_stream().wait_stream(s)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "The benefits of CUDA graphs can be demonstrated with the simple example in Figure 1. On the top, a sequence of short kernels is launched one-by-one by the CPU. The CPU launching overhead creates a significant gap in between the kernels. If we replace this sequence of kernels with a CUDA graph, initially we will need to spend a little extra time on building the graph and launching the whole graph in one go on the first occasion, but subsequent executions will be very fast, as there will be very little gap between the kernels. The difference is more pronounced when the same sequence of operations is repeated many times, for example, overy many training steps. In that case, the initial costs of building and launching the graph will be amortized over the entire number of training iterations. For a more comprehensive introduction on the topic, see our blog \n [Getting Started with CUDA Graphs](https://developer.nvidia.com/blog/cuda-graphs) and GTC talk [Effortless CUDA Graphs](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s32082/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "

\n\"Cuda\n
\n\tFigure 1. Benefits of using CUDA graphs\n

\n\n\n## NCCL support for CUDA graphs\n\n\nThe previously mentioned benefits of reducing launch overheads also extend to NCCL kernel launches. NCCL enables GPU-based collective and P2P communications. With [NCCL support for CUDA graphs](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/usage/cudagraph.html), we can eliminate the NCCL kernel launch overhead.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Additionally, kernel launch timing can be unpredictable due to various CPU load and operating system factors. Such time skews can be harmful to the performance of NCCL collective operations. With CUDA graphs, kernels are clustered together so that performance is consistent across ranks in a distributed workload. This is especially useful in large clusters where even a single slow node can bring down overall cluster level performance.\n\nFor distributed multi-GPU workloads, NCCL is used for collective communications. If we look at training a neural network that leverages data parallelism, without NCCL support for CUDA graphs, we\u2019ll need a separate launch for each of forward/back propagation and NCCL AllReduce. By contrast, with NCCL support for CUDA graphs, we can reduce launch overhead by lumping together the forward/backward propagation and NCCL AllReduce all in a single graph launch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "

\n\"With\n
\n Figure 2. Looking at a typical neural network, all the kernel launches for NCCL AllReduce can be bundled into a graph to reduce overhead launch time.\n

\n\n\n# PyTorch CUDA Graphs\n\n\nFrom PyTorch v1.10, the CUDA graphs functionality is made available as a set of beta APIs. \n\n### API overview", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "### API overview\n\nPyTorch supports the construction of CUDA graphs using [stream capture](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#creating-a-graph-using-stream-capture), which puts a CUDA stream in capture mode. CUDA work issued to a capturing stream doesn\u2019t actually run on the GPU. Instead, the work is recorded in a graph. After capture, the graph can be launched to run the GPU work as many times as needed. Each replay runs the same kernels with the same arguments. For pointer arguments this means the same memory addresses are used. By filling input memory with new data (e.g., from a new batch) before each replay, you can rerun the same work on new data.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Replaying a graph sacrifices the dynamic flexibility of typical eager execution in exchange for greatly reduced CPU overhead. A graph\u2019s arguments and kernels are fixed, so a graph replay skips all layers of argument setup and kernel dispatch, including Python, C++, and CUDA driver overheads. Under the hood, a replay submits the entire graph\u2019s work to the GPU with a single call to [cudaGraphLaunch](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__GRAPH.html#group__CUDART__GRAPH_1g1accfe1da0c605a577c22d9751a09597). Kernels in a replay also execute slightly faster on the GPU, but eliding CPU overhead is the main benefit.\n\nYou should try CUDA graphs if all or part of your network is graph-safe (usually this means static shapes and static control flow, but see the other [constraints](https://pytorch.org/docs/master/notes/cuda.html#constraints)) and you suspect its runtime is at least somewhat CPU-limited.\n\n### API example", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "### API example\n\nPyTorch exposes graphs via a raw [`torch.cuda.CUDAGraph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph)class and two convenience wrappers, [`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph) and [`torch.cuda.make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables).", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "[`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph) is a simple, versatile context manager that captures CUDA work in its context. Before capture, warm up the workload to be captured by running a few eager iterations. Warmup must occur on a side stream. Because the graph reads from and writes to the same memory addresses in every replay, you must maintain long-lived references to tensors that hold input and output data during capture. To run the graph on new input data, copy new data to the capture\u2019s input tensor(s), replay the graph, then read the new output from the capture\u2019s output tensor(s).\n\nIf the entire network is capture safe, one can capture and replay the whole network as in the following example.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "```python\nN, D_in, H, D_out = 640, 4096, 2048, 1024\nmodel = torch.nn.Sequential(torch.nn.Linear(D_in, H),\n torch.nn.Dropout(p=0.2),\n torch.nn.Linear(H, D_out),\n torch.nn.Dropout(p=0.1)).cuda()\nloss_fn = torch.nn.MSELoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=0.1)\n\n# Placeholders used for capture\nstatic_input = torch.randn(N, D_in, device='cuda')\nstatic_target = torch.randn(N, D_out, device='cuda')\n\n# warmup\n# Uses static_input and static_target here for convenience,\n# but in a real setting, because the warmup includes optimizer.step()\n# you must use a few batches of real data.\ns = torch.cuda.Stream()\ns.wait_stream(torch.cuda.current_stream())\nwith torch.cuda.stream(s):\n for i in range(3):\n optimizer.zero_grad(set_to_none=True)\n y_pred = model(static_input)\n loss = loss_fn(y_pred, static_target)\n loss.backward()\n optimizer.step()\ntorch.cuda.current_stream().wait_stream(s)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} {"page_content": "# capture\ng = torch.cuda.CUDAGraph()\n# Sets grads to None before capture, so backward() will create\n# .grad attributes with allocations from the graph's private pool\noptimizer.zero_grad(set_to_none=True)\nwith torch.cuda.graph(g):\n static_y_pred = model(static_input)\n static_loss = loss_fn(static_y_pred, static_target)\n static_loss.backward()\n optimizer.step()\n\nreal_inputs = [torch.rand_like(static_input) for _ in range(10)]\nreal_targets = [torch.rand_like(static_target) for _ in range(10)]", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "for data, target in zip(real_inputs, real_targets):\n # Fills the graph's input memory with new data to compute on\n static_input.copy_(data)\n static_target.copy_(target)\n # replay() includes forward, backward, and step.\n # You don't even need to call optimizer.zero_grad() between iterations\n # because the captured backward refills static .grad tensors in place.\n g.replay()\n # Params have been updated. static_y_pred, static_loss, and .grad", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "# attributes hold values from computing on this iteration's data.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "If some of your network is unsafe to capture (e.g., due to dynamic control flow, dynamic shapes, CPU syncs, or essential CPU-side logic), you can run the unsafe part(s) eagerly and use [`torch.cuda.make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) to graph only the capture-safe part(s). This is demonstrated next.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "[`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) accepts callables (functions or [`nn.Module`](https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module) and returns graphed versions. By default, callables returned by [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) are autograd-aware, and can be used", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "in the training loop as direct replacements for the functions or [`nn.Module`](https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module) you passed. [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) internally creates [`CUDAGraph`](https://pytorch.org/docs/master/generated/torch.cuda.CUDAGraph.html#torch.cuda.CUDAGraph) objects, runs warm up iterations, and maintains static inputs and outputs", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "as needed. Therefore, (unlike with [`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph)) you don\u2019t need to handle those manually.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "In the following example, data-dependent dynamic control flow means the network isn\u2019t capturable end-to-end, but [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables)() lets us capture and run graph-safe sections as graphs regardless:\n\n\n```python\nN, D_in, H, D_out = 640, 4096, 2048, 1024\n\nmodule1 = torch.nn.Linear(D_in, H).cuda()\nmodule2 = torch.nn.Linear(H, D_out).cuda()\nmodule3 = torch.nn.Linear(H, D_out).cuda()", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "loss_fn = torch.nn.MSELoss()\noptimizer = torch.optim.SGD(chain(module1.parameters(),\n module2.parameters(),\n module3.parameters()),\n lr=0.1)\n\n# Sample inputs used for capture\n# requires_grad state of sample inputs must match\n# requires_grad state of real inputs each callable will see.\nx = torch.randn(N, D_in, device='cuda')\nh = torch.randn(N, H, device='cuda', requires_grad=True)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "module1 = torch.cuda.make_graphed_callables(module1, (x,))\nmodule2 = torch.cuda.make_graphed_callables(module2, (h,))\nmodule3 = torch.cuda.make_graphed_callables(module3, (h,))\n\nreal_inputs = [torch.rand_like(x) for _ in range(10)]\nreal_targets = [torch.randn(N, D_out, device=\"cuda\") for _ in range(10)]\n\nfor data, target in zip(real_inputs, real_targets):\n optimizer.zero_grad(set_to_none=True)\n\n tmp = module1(data) # forward ops run as a graph", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "if tmp.sum().item() > 0:\n tmp = module2(tmp) # forward ops run as a graph\n else:\n tmp = module3(tmp) # forward ops run as a graph\n\n loss = loss_fn(tmp, target)\n # module2's or module3's (whichever was chosen) backward ops,\n # as well as module1's backward ops, run as graphs\n loss.backward()\n optimizer.step()", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "# Example use cases", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "MLPerf v1.0 training workloads\n\nThe PyTorch CUDA graphs functionality was instrumental in scaling NVIDIA\u2019s MLPerf training v1.0 workloads (implemented in PyTorch) to over 4000 GPUs, setting new [records across the board](https://blogs.nvidia.com/blog/2021/06/30/mlperf-ai-training-partners/). We illustrate below two MLPerf workloads where the most significant gains were observed with the use of CUDA graphs, yielding up to ~1.7x speedup.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "| | Number of GPUs | Speedup from CUDA-graphs |\n|-----------------------------|----------------:|-------------------------:|\n| Mask R-CNN | 272 | 1.70\u00d7 |\n| BERT | 4096 | 1.12\u00d7 |\n\nTable 1. MLPerf training v1.0 performance improvement with PyTorch CUDA graph.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Mask R-CNN", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Deep learning frameworks use GPUs to accelerate computations, but a significant amount of code still runs on CPU cores. CPU cores process meta-data like tensor shapes in order to prepare arguments needed to launch GPU kernels. Processing meta-data is a fixed cost while the cost of the computational work done by the GPUs is positively correlated with batch size. For large batch sizes, CPU overhead is a negligible percentage of total run time cost, but at small batch sizes CPU overhead can become larger than", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "GPU run time. When that happens, GPUs go idle between kernel calls. This issue can be identified on an NSight timeline plot in Figure 3. The plot below shows the \u201cbackbone\u201d portion of Mask R-CNN with per-gpu batch size of 1 before graphing. The green portion shows CPU load while the blue portion shows GPU load. In this profile we see that the CPU is maxed out at 100% load while GPU is idle most of the time, there is a lot of empty space between GPU kernels.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\"NSight\n
\n Figure 3: NSight timeline plot of Mask R-CNN\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs can automatically eliminate CPU overhead when tensor shapes are static. A complete graph of all the kernel calls is captured during the first step, in subsequent steps the entire graph is launched with a single op, eliminating all the CPU overhead, as observed in Figure 4..", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\"With\n
\n Figure 4: CUDA graphs optimization\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "With graphing, we see that the GPU kernels are tightly packed and GPU utilization remains high. The graphed portion now runs in 6 ms instead of 31ms, a speedup of 5x. We did not graph the entire model, mostly just the resnet backbone, which resulted in an overall speedup of ~1.7x.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "In order to increase the scope of the graph, we made some changes in the software stack to eliminate some of the CPU-GPU synchronization points. In MLPerf v1.0, this work included changing the implementation of torch.randperm function to use CUB instead of Thrust because the latter is a synchronous C++ template library. These improvements are available in the latest NGC container.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "BERT", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Similarly, by graph capturing the model, we eliminate CPU overhead and accompanying synchronization overhead. CUDA graphs implementation results in a 1.12x performance boost for our max-scale BERT configuration. To maximize the benefits from CUDA graphs, it is important to keep the scope of the graph as large as possible. To achieve this, we modified the model script to remove CPU-GPU synchronizations during the execution such that the full model can be graph captured. Furthermore, we also made sure that", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "the tensor sizes during the execution are static within the scope of the graph. For instance, in BERT, only a specific subset of total tokens contribute to loss function, determined by a pre-generated mask tensor. Extracting the indices of valid tokens from this mask, and using these indices to gather the tokens that contribute to the loss, results in a tensor with a dynamic shape, i.e. with shape that is not constant across iterations. In order to make sure tensor sizes are static, instead of using the", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "dynamic-shape tensors in the loss computation, we used static shape tensors where a mask is used to indicate which elements are valid. As a result, all tensor shapes are static. Dynamic shapes also require CPU-GPU synchronization since it has to involve the framework\u2019s memory management on the CPU side. With static-only shapes, no CPU-GPU synchronizations are necessary. This is shown in Figure 5.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"Synchronization\n\t
\n\tFigure 5. By using a fixed size tensor and a boolean mask as described in the text, we are able to eliminate CPU synchronizations needed for dynamic sized tensors \n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs in NVIDIA DL examples collection", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Single GPU use cases can also benefit from using CUDA Graphs. This is particularly true for workloads launching many short kernels with small batches. A good example is training and inference for recommender systems. Below we present preliminary benchmark results for NVIDIA's implementation of the Deep Learning Recommendation Model (DLRM) from our Deep Learning Examples collection. Using CUDA graphs for this workload provides significant speedups for both training and inference. The effect is particularly", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "visible when using very small batch sizes, where CPU overheads are more pronounced.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs are being actively integrated into other PyTorch NGC model scripts and the NVIDIA Github deep learning examples. Stay tuned for more examples on how to use it.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "

\n\t\"CUDA\n

\n

\n\t\"CUDA\n
", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Figure 6: CUDA graphs optimization for the DLRM model.\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "# Call to action: CUDA Graphs in PyTorch v1.10", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "CUDA graphs can provide substantial benefits for workloads that comprise many small GPU kernels and hence bogged down by CPU launch overheads. This has been demonstrated in our MLPerf efforts, optimizing PyTorch models. Many of these optimizations, including CUDA graphs, have or will eventually be integrated into our PyTorch NGC model scripts [collection](https://ngc.nvidia.com/catalog/collections?orderBy=scoreDESC&pageNumber=0&query=pytorch&quickFilter=&filters=) and the NVIDIA [Github deep learning", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "examples](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/). For now, check out our open-source MLPerf training v1.0 [implementation](https://github.com/mlcommons/training_results_v1.0/tree/master/NVIDIA) which could serve as a good starting point to see CUDA graph in action. Alternatively, try the PyTorch CUDA graphs API on your own workloads.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "We thank many NVIDIAN\u2019s and Facebook engineers for their discussions and suggestions: \n[Karthik Mandakolathur US](mailto:karthik@nvidia.com),\n[Tomasz Grel](mailto:tgrel@nvidia.com), \n[PLJoey Conway](mailto:jconway@nvidia.com), \n[Arslan Zulfiqar US](mailto:azulfiqar@nvidia.com)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Authors bios\n\n[**Vinh Nguyen**](mailto:vinhn@nvidia.com)\n*DL Engineer, NVIDIA*\n\nVinh is a Deep learning engineer and data scientist, having published more than 50 scientific articles attracting more than 2500 citations. At NVIDIA, his work spans a wide range of deep learning and AI applications, including speech, language and vision processing, and recommender systems.\n\n[**Michael Carilli**](mailto:mcarilli@nvidia.com)\n*Senior Developer Technology Engineer, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Michael worked at the Air Force Research Laboratory optimizing CFD code for modern parallel architectures. He holds a PhD in computational physics from the University of California, Santa Barbara. A member of the PyTorch team, he focuses on making GPU training fast, numerically stable, and easy(er) for internal teams, external customers, and Pytorch community users.\n\n[**Sukru Burc Eryilmaz**](mailto:seryilmaz@nvidia.com)\n*Senior Architect in Dev Arch, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Sukru received his PhD from Stanford University, and B.S from Bilkent University. He currently works on improving the end-to-end performance of neural network training both at single-node scale and supercomputer scale. \n\n[**Vartika Singh**](mailto:vartikas@nvidia.com)\n*Tech Partner Lead for DL Frameworks and Libraries, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Vartika has led teams working in confluence of cloud and distributed computing, scaling and AI, influencing the design and strategy of major corporations. She currently works with the major frameworks and compiler organizations and developers within and outside NVIDIA, to help the design to work efficiently and optimally on NVIDIA hardware.\n\n[**Michelle Lin**](mailto:miclin@nvidia.com)\n*Product Intern, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Michelle is currently pursuing an undergraduate degree in Computer Science and Business Administration at UC Berkeley. She is currently managing execution of projects such as conducting market research and creating marketing assets for Magnum IO.\n\n[**Natalia Gimelshein**](mailto:ngimel@fb.com)\n*Applied Research Scientist, Facebook*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Natalia Gimelshein worked on GPU performance optimization for deep learning workloads at NVIDIA and Facebook. She is currently a member of the PyTorch core team, working with partners to seamlessly support new software and hardware features.\n\n[**Alban Desmaison**](mailto:albandes@fb.com)\n*Research Engineer, Facebook*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Alban studied engineering and did a PhD in Machine Learning and Optimization, during which he was an OSS contributor to PyTorch prior to joining Facebook. His main responsibilities are maintaining some core library and features (autograd, optim, nn) and working on making PyTorch better in general.\n\n[**Edward Yang**](mailto:ezyang@fb.com)\n*Research Engineer, Facebook*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "Edward studied CS at MIT and then Stanford before starting at Facebook. He is a part of the PyTorch core team and is one of the leading contributors to PyTorch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.10 Release, including CUDA Graphs APIs, Frontend and Compiler Improvements'\nauthor: Team PyTorch\n---\n\nWe are excited to announce the release of PyTorch 1.10. This release is composed of over 3,400 commits since 1.9, made by 426 contributors. We want to sincerely thank our community for continuously improving PyTorch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.10 updates are focused on improving training and performance of PyTorch, and developer usability. The full release notes are available [here](https://github.com/pytorch/pytorch/releases/tag/v1.10.0). Highlights include:\n1. CUDA Graphs APIs are integrated to reduce CPU overheads for CUDA workloads.\n2. Several frontend APIs such as FX, torch.special, and nn.Module Parametrization, have moved from beta to stable. \n3. Support for automatic fusion in JIT Compiler expands to CPUs in addition to GPUs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "4. Android NNAPI support is now available in beta.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Along with 1.10, we are also releasing major updates to the PyTorch libraries, which you can read about in [this blog post](https://pytorch.org/blog/pytorch-1.10-new-library-releases/).\n\n\n\n# Frontend APIs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Python code transformations with FX\n\nFX provides a Pythonic platform for transforming and lowering PyTorch programs. It is a toolkit for pass writers to facilitate Python-to-Python transformation of functions and nn.Module instances. This toolkit aims to support a subset of Python language semantics\u2014rather than the whole Python language\u2014to facilitate ease of implementation of transforms. With 1.10, FX is moving to stable.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "You can learn more about FX in the [official documentation](https://pytorch.org/docs/master/fx.html) and [GitHub examples](https://github.com/pytorch/examples/tree/master/fx) of program transformations implemented using ```torch.fx```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) *torch.special* \nA ```torch.special module```, analogous to [SciPy\u2019s special module](https://docs.scipy.org/doc/scipy/reference/special.html), is now available in stable. The module has 30 operations, including gamma, Bessel, and (Gauss) error functions. \n\nRefer to this [documentation](https://pytorch.org/docs/master/special.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Stable) nn.Module Parametrization \n```nn.Module``` parametrizaton, a feature that allows users to parametrize any parameter or buffer of an ```nn.Module``` without modifying the ```nn.Module``` itself, is available in stable. This release adds weight normalization (```weight_norm```), orthogonal parametrization (matrix constraints and part of pruning) and more flexibility when creating your own parametrization.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Refer to this [tutorial](https://pytorch.org/tutorials/intermediate/parametrizations.html) and the general [documentation](https://pytorch.org/docs/master/generated/torch.nn.utils.parametrizations.spectral_norm.html?highlight=parametrize) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) CUDA Graphs APIs Integration\nPyTorch now integrates CUDA Graphs APIs to reduce CPU overheads for CUDA workloads.\n\nCUDA Graphs greatly reduce the CPU overhead for CPU-bound cuda workloads and thus improve performance by increasing GPU utilization. For distributed workloads, CUDA Graphs also reduce jitter, and since parallel workloads have to wait for the slowest worker, reducing jitter improves overall parallel efficiency.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "for data, target in zip(real_inputs, real_targets):\n # Fills the graph's input memory with new data to compute on\n static_input.copy_(data)\n static_target.copy_(target)\n # replay() includes forward, backward, and step.\n # You don't even need to call optimizer.zero_grad() between iterations\n # because the captured backward refills static .grad tensors in place.\n g.replay()\n # Params have been updated. static_y_pred, static_loss, and .grad\n # attributes hold values from computing on this iteration's data.\n```\n\nIf some of your network is unsafe to capture (e.g., due to dynamic control flow, dynamic shapes, CPU syncs, or essential CPU-side logic), you can run the unsafe part(s) eagerly and use [`torch.cuda.make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) to graph only the capture-safe part(s). This is demonstrated next.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "[`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) accepts callables (functions or [`nn.Module`](https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module) and returns graphed versions. By default, callables returned by [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) are autograd-aware, and can be used in the training loop as direct replacements for the functions or [`nn.Module`](https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module) you passed. [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables) internally creates [`CUDAGraph`](https://pytorch.org/docs/master/generated/torch.cuda.CUDAGraph.html#torch.cuda.CUDAGraph) objects, runs warm up iterations, and maintains static inputs and outputs as needed. Therefore, (unlike with [`torch.cuda.graph`](https://pytorch.org/docs/master/generated/torch.cuda.graph.html#torch.cuda.graph)) you don\u2019t need to handle those manually.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "In the following example, data-dependent dynamic control flow means the network isn\u2019t capturable end-to-end, but [`make_graphed_callables`](https://pytorch.org/docs/master/generated/torch.cuda.make_graphed_callables.html#torch.cuda.make_graphed_callables)() lets us capture and run graph-safe sections as graphs regardless:\n\n\n```python\nN, D_in, H, D_out = 640, 4096, 2048, 1024\n\nmodule1 = torch.nn.Linear(D_in, H).cuda()\nmodule2 = torch.nn.Linear(H, D_out).cuda()\nmodule3 = torch.nn.Linear(H, D_out).cuda()\n\nloss_fn = torch.nn.MSELoss()\noptimizer = torch.optim.SGD(chain(module1.parameters(),\n module2.parameters(),\n module3.parameters()),\n lr=0.1)\n\n# Sample inputs used for capture\n# requires_grad state of sample inputs must match\n# requires_grad state of real inputs each callable will see.\nx = torch.randn(N, D_in, device='cuda')\nh = torch.randn(N, H, device='cuda', requires_grad=True)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "module1 = torch.cuda.make_graphed_callables(module1, (x,))\nmodule2 = torch.cuda.make_graphed_callables(module2, (h,))\nmodule3 = torch.cuda.make_graphed_callables(module3, (h,))\n\nreal_inputs = [torch.rand_like(x) for _ in range(10)]\nreal_targets = [torch.randn(N, D_out, device=\"cuda\") for _ in range(10)]\n\nfor data, target in zip(real_inputs, real_targets):\n optimizer.zero_grad(set_to_none=True)\n\n tmp = module1(data) # forward ops run as a graph\n\n if tmp.sum().item() > 0:\n tmp = module2(tmp) # forward ops run as a graph\n else:\n tmp = module3(tmp) # forward ops run as a graph\n\n loss = loss_fn(tmp, target)\n # module2's or module3's (whichever was chosen) backward ops,\n # as well as module1's backward ops, run as graphs\n loss.backward()\n optimizer.step()\n```\n\n# Example use cases\n## MLPerf v1.0 training workloads", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "The PyTorch CUDA graphs functionality was instrumental in scaling NVIDIA\u2019s MLPerf training v1.0 workloads (implemented in PyTorch) to over 4000 GPUs, setting new [records across the board](https://blogs.nvidia.com/blog/2021/06/30/mlperf-ai-training-partners/). We illustrate below two MLPerf workloads where the most significant gains were observed with the use of CUDA graphs, yielding up to ~1.7x speedup.\n\n| | Number of GPUs | Speedup from CUDA-graphs |\n|-----------------------------|----------------:|-------------------------:|\n| Mask R-CNN | 272 | 1.70\u00d7 |\n| BERT | 4096 | 1.12\u00d7 |\n\nTable 1. MLPerf training v1.0 performance improvement with PyTorch CUDA graph.\n\n### Mask R-CNN", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "### Mask R-CNN\n\nDeep learning frameworks use GPUs to accelerate computations, but a significant amount of code still runs on CPU cores. CPU cores process meta-data like tensor shapes in order to prepare arguments needed to launch GPU kernels. Processing meta-data is a fixed cost while the cost of the computational work done by the GPUs is positively correlated with batch size. For large batch sizes, CPU overhead is a negligible percentage of total run time cost, but at small batch sizes CPU overhead can become larger than GPU run time. When that happens, GPUs go idle between kernel calls. This issue can be identified on an NSight timeline plot in Figure 3. The plot below shows the \u201cbackbone\u201d portion of Mask R-CNN with per-gpu batch size of 1 before graphing. The green portion shows CPU load while the blue portion shows GPU load. In this profile we see that the CPU is maxed out at 100% load while GPU is idle most of the time, there is a lot of empty space between GPU kernels.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "

\n\"NSight\n
\n Figure 3: NSight timeline plot of Mask R-CNN\n

\n\nCUDA graphs can automatically eliminate CPU overhead when tensor shapes are static. A complete graph of all the kernel calls is captured during the first step, in subsequent steps the entire graph is launched with a single op, eliminating all the CPU overhead, as observed in Figure 4.. \n\n

\n\"With\n
\n Figure 4: CUDA graphs optimization\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "With graphing, we see that the GPU kernels are tightly packed and GPU utilization remains high. The graphed portion now runs in 6 ms instead of 31ms, a speedup of 5x. We did not graph the entire model, mostly just the resnet backbone, which resulted in an overall speedup of ~1.7x.\nIn order to increase the scope of the graph, we made some changes in the software stack to eliminate some of the CPU-GPU synchronization points. In MLPerf v1.0, this work included changing the implementation of torch.randperm function to use CUB instead of Thrust because the latter is a synchronous C++ template library. These improvements are available in the latest NGC container.\n\n\n### BERT", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Similarly, by graph capturing the model, we eliminate CPU overhead and accompanying synchronization overhead. CUDA graphs implementation results in a 1.12x performance boost for our max-scale BERT configuration. To maximize the benefits from CUDA graphs, it is important to keep the scope of the graph as large as possible. To achieve this, we modified the model script to remove CPU-GPU synchronizations during the execution such that the full model can be graph captured. Furthermore, we also made sure that the tensor sizes during the execution are static within the scope of the graph. For instance, in BERT, only a specific subset of total tokens contribute to loss function, determined by a pre-generated mask tensor. Extracting the indices of valid tokens from this mask, and using these indices to gather the tokens that contribute to the loss, results in a tensor with a dynamic shape, i.e. with shape that is not constant across iterations. In order to make sure tensor sizes are static, instead of using the dynamic-shape tensors in the loss computation, we used static shape tensors where a mask is used to indicate which elements are valid. As a result, all tensor shapes are static. Dynamic shapes also require CPU-GPU synchronization since it has to involve the framework\u2019s memory management on the CPU side. With static-only shapes, no CPU-GPU synchronizations are necessary. This is shown in Figure 5.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "

\n\t\"Synchronization\n\t
\n\tFigure 5. By using a fixed size tensor and a boolean mask as described in the text, we are able to eliminate CPU synchronizations needed for dynamic sized tensors \n

\n\n\n## CUDA graphs in NVIDIA DL examples collection\n\nSingle GPU use cases can also benefit from using CUDA Graphs. This is particularly true for workloads launching many short kernels with small batches. A good example is training and inference for recommender systems. Below we present preliminary benchmark results for NVIDIA's implementation of the Deep Learning Recommendation Model (DLRM) from our Deep Learning Examples collection. Using CUDA graphs for this workload provides significant speedups for both training and inference. The effect is particularly visible when using very small batch sizes, where CPU overheads are more pronounced.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "CUDA graphs are being actively integrated into other PyTorch NGC model scripts and the NVIDIA Github deep learning examples. Stay tuned for more examples on how to use it.\n\n\n

\n\t\"CUDA\n

\n

\n\t\"CUDA\n
\n\tFigure 6: CUDA graphs optimization for the DLRM model.\n

\n\n\n# Call to action: CUDA Graphs in PyTorch v1.10", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "CUDA graphs can provide substantial benefits for workloads that comprise many small GPU kernels and hence bogged down by CPU launch overheads. This has been demonstrated in our MLPerf efforts, optimizing PyTorch models. Many of these optimizations, including CUDA graphs, have or will eventually be integrated into our PyTorch NGC model scripts [collection](https://ngc.nvidia.com/catalog/collections?orderBy=scoreDESC&pageNumber=0&query=pytorch&quickFilter=&filters=) and the NVIDIA [Github deep learning examples](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/). For now, check out our open-source MLPerf training v1.0 [implementation](https://github.com/mlcommons/training_results_v1.0/tree/master/NVIDIA) which could serve as a good starting point to see CUDA graph in action. Alternatively, try the PyTorch CUDA graphs API on your own workloads.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "We thank many NVIDIAN\u2019s and Facebook engineers for their discussions and suggestions: \n[Karthik Mandakolathur US](mailto:karthik@nvidia.com),\n[Tomasz Grel](mailto:tgrel@nvidia.com), \n[PLJoey Conway](mailto:jconway@nvidia.com), \n[Arslan Zulfiqar US](mailto:azulfiqar@nvidia.com)\n\n## Authors bios\n\n[**Vinh Nguyen**](mailto:vinhn@nvidia.com)\n*DL Engineer, NVIDIA*\n\nVinh is a Deep learning engineer and data scientist, having published more than 50 scientific articles attracting more than 2500 citations. At NVIDIA, his work spans a wide range of deep learning and AI applications, including speech, language and vision processing, and recommender systems.\n\n[**Michael Carilli**](mailto:mcarilli@nvidia.com)\n*Senior Developer Technology Engineer, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Michael worked at the Air Force Research Laboratory optimizing CFD code for modern parallel architectures. He holds a PhD in computational physics from the University of California, Santa Barbara. A member of the PyTorch team, he focuses on making GPU training fast, numerically stable, and easy(er) for internal teams, external customers, and Pytorch community users.\n\n[**Sukru Burc Eryilmaz**](mailto:seryilmaz@nvidia.com)\n*Senior Architect in Dev Arch, NVIDIA*\n\nSukru received his PhD from Stanford University, and B.S from Bilkent University. He currently works on improving the end-to-end performance of neural network training both at single-node scale and supercomputer scale. \n\n[**Vartika Singh**](mailto:vartikas@nvidia.com)\n*Tech Partner Lead for DL Frameworks and Libraries, NVIDIA*", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "Vartika has led teams working in confluence of cloud and distributed computing, scaling and AI, influencing the design and strategy of major corporations. She currently works with the major frameworks and compiler organizations and developers within and outside NVIDIA, to help the design to work efficiently and optimally on NVIDIA hardware.\n\n[**Michelle Lin**](mailto:miclin@nvidia.com)\n*Product Intern, NVIDIA*\n\nMichelle is currently pursuing an undergraduate degree in Computer Science and Business Administration at UC Berkeley. She is currently managing execution of projects such as conducting market research and creating marketing assets for Magnum IO.\n\n[**Natalia Gimelshein**](mailto:ngimel@fb.com)\n*Applied Research Scientist, Facebook*\n\nNatalia Gimelshein worked on GPU performance optimization for deep learning workloads at NVIDIA and Facebook. She is currently a member of the PyTorch core team, working with partners to seamlessly support new software and hardware features.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "[**Alban Desmaison**](mailto:albandes@fb.com)\n*Research Engineer, Facebook*\n\nAlban studied engineering and did a PhD in Machine Learning and Optimization, during which he was an OSS contributor to PyTorch prior to joining Facebook. His main responsibilities are maintaining some core library and features (autograd, optim, nn) and working on making PyTorch better in general.\n\n[**Edward Yang**](mailto:ezyang@fb.com)\n*Research Engineer, Facebook*\n\nEdward studied CS at MIT and then Stanford before starting at Facebook. He is a part of the PyTorch core team and is one of the leading contributors to PyTorch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.10 Release, including CUDA Graphs APIs, Frontend and Compiler Improvements'\nauthor: Team PyTorch\n---\n\nWe are excited to announce the release of PyTorch 1.10. This release is composed of over 3,400 commits since 1.9, made by 426 contributors. We want to sincerely thank our community for continuously improving PyTorch. \n\nPyTorch 1.10 updates are focused on improving training and performance of PyTorch, and developer usability. The full release notes are available [here](https://github.com/pytorch/pytorch/releases/tag/v1.10.0). Highlights include:\n1. CUDA Graphs APIs are integrated to reduce CPU overheads for CUDA workloads.\n2. Several frontend APIs such as FX, torch.special, and nn.Module Parametrization, have moved from beta to stable. \n3. Support for automatic fusion in JIT Compiler expands to CPUs in addition to GPUs.\n4. Android NNAPI support is now available in beta.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "Along with 1.10, we are also releasing major updates to the PyTorch libraries, which you can read about in [this blog post](https://pytorch.org/blog/pytorch-1.10-new-library-releases/).\n\n\n\n# Frontend APIs\n\n### (Stable) Python code transformations with FX\n\nFX provides a Pythonic platform for transforming and lowering PyTorch programs. It is a toolkit for pass writers to facilitate Python-to-Python transformation of functions and nn.Module instances. This toolkit aims to support a subset of Python language semantics\u2014rather than the whole Python language\u2014to facilitate ease of implementation of transforms. With 1.10, FX is moving to stable. \n\nYou can learn more about FX in the [official documentation](https://pytorch.org/docs/master/fx.html) and [GitHub examples](https://github.com/pytorch/examples/tree/master/fx) of program transformations implemented using ```torch.fx```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "### (Stable) *torch.special* \nA ```torch.special module```, analogous to [SciPy\u2019s special module](https://docs.scipy.org/doc/scipy/reference/special.html), is now available in stable. The module has 30 operations, including gamma, Bessel, and (Gauss) error functions. \n\nRefer to this [documentation](https://pytorch.org/docs/master/special.html) for more details.\n\n### (Stable) nn.Module Parametrization \n```nn.Module``` parametrizaton, a feature that allows users to parametrize any parameter or buffer of an ```nn.Module``` without modifying the ```nn.Module``` itself, is available in stable. This release adds weight normalization (```weight_norm```), orthogonal parametrization (matrix constraints and part of pruning) and more flexibility when creating your own parametrization.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "Refer to this [tutorial](https://pytorch.org/tutorials/intermediate/parametrizations.html) and the general [documentation](https://pytorch.org/docs/master/generated/torch.nn.utils.parametrizations.spectral_norm.html?highlight=parametrize) for more details.\n\n### (Beta) CUDA Graphs APIs Integration\nPyTorch now integrates CUDA Graphs APIs to reduce CPU overheads for CUDA workloads.\n\nCUDA Graphs greatly reduce the CPU overhead for CPU-bound cuda workloads and thus improve performance by increasing GPU utilization. For distributed workloads, CUDA Graphs also reduce jitter, and since parallel workloads have to wait for the slowest worker, reducing jitter improves overall parallel efficiency.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} {"page_content": "Integration allows seamless interop between the parts of the network captured by cuda graphs, and parts of the network that cannot be captured due to graph limitations. \n \nRead the [note](https://pytorch.org/docs/master/notes/cuda.html#cuda-graphs) for more details and examples, and refer to the general [documentation](https://pytorch.org/docs/master/generated/torch.cuda.CUDAGraph.html#torch.cuda.CUDAGraph) for additional information.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Conjugate View", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch\u2019s conjugation for complex tensors ([torch.conj()](https://pytorch.org/docs/1.10.0/generated/torch.conj.html?highlight=conj#torch.conj)) is now a constant time operation, and returns a view of the input tensor with a conjugate bit set as can be seen by calling [torch.is_conj()](https://pytorch.org/docs/1.10.0/generated/torch.is_conj.html?highlight=is_conj#torch.is_conj) . This has already been leveraged in various other PyTorch operations like matrix multiplication, dot product etc., to fuse", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "conjugation with the operation leading to significant performance gain and memory savings on both CPU and CUDA.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "# Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Distributed Training Releases Now in Stable \nIn 1.10, there are a number of features that are moving from beta to stable in the distributed package:\n* **(Stable) Remote Module**: This feature allows users to operate a module on a remote worker like using a local module, where the RPCs are transparent to the user. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Stable) DDP Communication Hook**: This feature allows users to override how DDP synchronizes gradients across processes. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Stable) ZeroRedundancyOptimizer**: This feature can be used in conjunction with DistributedDataParallel to reduce the size of per-process optimizer states. With this stable release, it now can handle uneven inputs to different data-parallel workers. Check out this [tutorial](https://pytorch.org/tutorials/advanced/generic_join.html). We also improved the parameter partition algorithm to better balance memory and computation overhead across processes. Refer to this", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "[documentation](https://pytorch.org/docs/master/distributed.optim.html) and this [tutorial](https://pytorch.org/tutorials/recipes/zero_redundancy_optimizer.html) to learn more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "# Performance Optimization and Tooling", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Profile-directed typing in TorchScript \nTorchScript has a hard requirement for source code to have type annotations in order for compilation to be successful. For a long time, it was only possible to add missing or incorrect type annotations through trial and error (i.e., by fixing the type-checking errors generated by torch.jit.script one by one), which was inefficient and time consuming.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Now, we have enabled profile directed typing for torch.jit.script by leveraging existing tools like MonkeyType, which makes the process much easier, faster, and more efficient. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/jit.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) CPU Fusion \nIn PyTorch 1.10, we've added an LLVM-based JIT compiler for CPUs that can fuse together sequences of `torch` library calls to improve performance. While we've had this capability for some time on GPUs, this release is the first time we've brought compilation to the CPU. \nYou can check out a few performance results for yourself in this [Colab notebook](https://colab.research.google.com/drive/1xaH-L0XjsxUcS15GG220mtyrvIgDoZl6?usp=sharing).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) PyTorch Profiler \nThe objective of PyTorch Profiler is to target the execution steps that are the most costly in time and/or memory, and visualize the workload distribution between GPUs and CPUs. PyTorch 1.10 includes the following key features:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "* **Enhanced Memory View**: This helps you understand your memory usage better. This tool will help you avoid Out of Memory errors by showing active memory allocations at various points of your program run.\n* **Enhanced Automated Recommendations**: This helps provide automated performance recommendations to help optimize your model. The tools recommend changes to batch size, TensorCore, memory reduction technologies, etc.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "* **Enhanced Kernel View**: Additional columns show grid and block sizes as well as shared memory usage and registers per thread.\n* **Distributed Training**: Gloo is now supported for distributed training jobs.\n* **Correlate Operators in the Forward & Backward Pass**: This helps map the operators found in the forward pass to the backward pass, and vice versa, in a trace view.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "* **TensorCore**: This tool shows the Tensor Core (TC) usage and provides recommendations for data scientists and framework developers.\n* **NVTX**: Support for NVTX markers was ported from the legacy autograd profiler.\n* **Support for profiling on mobile devices**: The PyTorch profiler now has better integration with TorchScript and mobile backends, enabling trace collection for mobile workloads.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Refer to this [documentation](https://pytorch.org/docs/stable/profiler.html) for details. Check out this [tutorial](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html) to learn how to get started with this feature. \n\n# PyTorch Mobile", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Android NNAPI Support in Beta \nLast year we [released prototype support](https://medium.com/pytorch/pytorch-mobile-now-supports-android-nnapi-e2a2aeb74534) for Android\u2019s Neural Networks API (NNAPI). NNAPI allows Android apps to run computationally intensive neural networks on the most powerful and efficient parts of the chips that power mobile phones, including GPUs (Graphics Processing Units) and NPUs (specialized Neural Processing Units).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Since the prototype we\u2019ve added more op coverage, added support for load-time flexible shapes and ability to run the model on the host for testing. Try out this feature using the [tutorial](https://pytorch.org/tutorials/prototype/nnapi_mobilenetv2.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Additionally, Transfer Learning steps have been added to Object Detection examples. Check out this [GitHub page](https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection#transfer-learning) to learn more. Please provide your feedback or ask questions on the [forum](https://discuss.pytorch.org/c/mobile/18). You can also check out [this presentation](https://www.youtube.com/watch?v=B-2spa3UCTU) to get an overview.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch). \n\nCheers!\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Ambient Clinical Intelligence: Generating Medical Reports with PyTorch\"\nauthor: Miguel Del-Agua, Principal Research Scientist, Nuance and Jeremy Jancsary, Senior Principal Research Scientist, Nuance\nfeatured-img: \"\"\n---", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\nComplete and accurate clinical documentation is an essential tool for tracking patient care. It allows for treatment plans to be shared among care teams to aid in continuity of care and ensures a transparent and effective process for reimbursement.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Physicians are responsible for documenting patient care. Traditional clinical documentation methods have resulted in a sub-par patient-provider experience, less time interacting with patients, and decreased work-life balance. A significant amount of physicians\u2019 time is spent in front of the computer doing administrative tasks. As a result, patients are less satisfied with the overall experience, and physicians, who prepare for years studying medicine, cannot practice at the top of their license and are", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "burned out. Every hour physicians provide direct clinical face time to patients results in nearly two additional hours spent on EHR and desk work within the clinic day. Outside office hours, physicians [spend another 1 to 2 hours of personal](https://www.acpjournals.org/doi/10.7326/m16-0961) time each night doing additional computer and other clerical work.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* [42% of all physicians reported having burnout. \u2013 Medscape](https://www.medscape.com/slideshow/2020-lifestyle-burnout-6012460)\n* [The problem has grown worse due to the pandemic with 64% of U.S. physicians now reporting burnout. - AAFP](https://www.aafp.org/journals/fpm/blogs/inpractice/entry/covid_burnout_survey.html#:~:text=Physician%20burnout%20was%20already%20a,5%2C000%20%E2%80%94%20practice%20in%20the%20U.S.)", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* [\"Too many bureaucratic tasks e.g., charting and paperwork\" is the leading contribution to burnout, increased computerization ranks 4th.](https://login.medscape.com/login/sso/getlogin?urlCache=aHR0cHM6Ly93d3cubWVkc2NhcGUuY29tL3NsaWRlc2hvdy8yMDIwLWxpZmVzdHlsZS1idXJub3V0LTYwMTI0NjA%3D&ac=401) - Medscape", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* [75% of U.S. Consumers Wish Their Healthcare Experiences Were More Personalized,](https://www.businesswire.com/news/home/20200218005006/en/75-of-U.S.-Consumers-Wish-Their-Healthcare-Experiences-Were-More-Personalized-Redpoint-Global-Survey-Reveals)- Business Wire", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* [61% of patients would visit their healthcare provider more often if the communication experience felt more personalized.](https://www.businesswire.com/news/home/20200218005006/en/75-of-U.S.-Consumers-Wish-Their-Healthcare-Experiences-Were-More-Personalized-Redpoint-Global-Survey-Reveals) \u2013 Business Wire", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Physician burnout is one of the primary causes for increased [medical errors](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6175626/), malpractice suits, turnover, and decreased access to care. Burnout leads to an increase in healthcare costs and a decrease in overall patient satisfaction. [Burnout costs the United States $4.6 billion a year.](https://www.nejm.org/doi/full/10.1056/NEJMp2003149)", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "What can we do to bring back trust, joy, and humanity to the delivery of healthcare? A significant portion of the administrative work consists of entering patient data into Electronic Health Records (EHRs) and creating clinical documentation. Clinical documentation is created from information already in the EHR as well as from the patient-provider encounter conversation.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This article will showcase how the Nuance Dragon Ambient eXperience (DAX), an AI-powered, voice-enabled, ambient clinical intelligence solution, automatically documents patient encounters accurately and efficiently at the point of care and the technologies that enable it.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Nuance DAX enhances the quality of care and patient experience, increases provider efficiency and satisfaction, and improves financial outcomes. It can be used in office and telehealth settings in all ambulatory specialties, including primary and urgent care.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Natural Language Processing", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Natural Language Processing (NLP) is one of the most challenging fields in Artificial Intelligence (AI). It comprehends a set of algorithms that allow computers to understand or generate the language used by humans. These algorithms can process and analyze vast amounts of natural language data from different sources (either sound or text) to build models that can understand, classify, or even generate natural language as humans would. Like other fields in AI, NLP has significantly progressed thanks to the", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "advent of Deep Learning (DL), which has resulted in models that can obtain results on par with humans in some tasks.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "These advanced NLP techniques are being applied in healthcare. During a typical patient-provider encounter, a conversation ensues where the doctor constructs, through questions and answers, a chronological description of the development of the patient's presenting illness or symptoms. A physician examines the patient and makes clinical decisions to establish a diagnosis and determine a treatment plan. This conversation, and data in the EHR, provide the required information for physicians to generate the", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "clinical documentation, referred to as medical reports.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Two main NLP components play a role in automating the creation of clinical documentation. The first component, Automatic Speech Recognition (ASR), is used to translate speech into text. It takes the audio recording of the encounter and generates a conversation transcription (cf. Figure 2). The second component, Automatic Text Summarization, helps generate summaries from large text documents. This component is responsible for understanding and capturing the nuances and most essential aspects from the", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "transcribed conversation into a final report in narrative form (cf. Figure 3), structured form, or a combination of both.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We will focus on this second component, Automatic Text Summarization, which is a difficult task with many challenges:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* Its performance is tied to the ASR quality from multiple speakers (noisy input).\n* The input is conversational in nature and contains layman's terms.\n* Protected Health Information (PHI) regulations limit medical data access.\n* The information for one output sentence is potentially spread across multiple conversation turns.\n* There is no explicit sentence alignment between input and output.\n* Various medical specialties, encounter types, and EHR systems constitute a broad and complex output space.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* Physicians have different styles of conducting encounters and have their preferences for medical reports; there is no standard. \n* Standard summarization metrics might differ from human judgment of quality.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFigure 2: Transcript of a patient-doctor conversation\n

\n\n

\n \n

\n\n

\nFigure 3: Excerpt of an AI-generated medical report. HPI stands for History of present illness.\n

", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Text Summarization with PyTorch and Fairseq", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[PyTorch](https://pytorch.org/) is an open-source machine learning framework developed by Facebook that helps researchers prototype Deep Learning models. The [Fairseq](https://github.com/pytorch/fairseq) toolkit is built on top of PyTorch and focuses on sequence generation tasks, such as Neural Machine Translation (NMT) or Text Summarization. Fairseq features an active community that is continuously providing reference implementations of state-of-the-art models. It contains many built-in components (model", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "architectures, modules, loss functions, and optimizers) and is easily extendable with plugins.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Text summarization constitutes a significant challenge in NLP. We need models capable of generating a short version of a document while retaining the key points and avoiding uninformative content. These challenges can be addressed with different approaches. 1). Abstractive text summarization aimed at training models that can generate a summary in narrative form. 2). Extractive methods where the models are trained to select the most important parts from the input text. 3). A combination of the two, where", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "the essential parts from the input are selected and then summarized in an abstractive fashion. Hence, summarization can be accomplished via a single end-to-end network or as a pipeline of extractive and abstractive components. To that end, Fairseq provides all the necessary tools to be successful in our endeavor. It features either end-to-end models such as the classical Transformer, different types of Language Models and pre-trained versions that enable researchers to focus on what matters most\u2014to build", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "state-of-the-art models that generate valuable reports.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "However, we are not just summarizing the transcribed conversation; we generate high-quality medical reports, which have many considerations.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* Every section of a medical report is different in terms of content, structure, fluency, etc.\n* All medical facts mentioned in the conversation should be present in the report, for example, a particular treatment or dosage.\n* In the healthcare domain, the vocabulary is extensive, and models need to deal with medical terminology.\n* Patient-doctor conversations are usually much longer than the final report.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### [Beta] Conjugate View\nPyTorch\u2019s conjugation for complex tensors ([torch.conj()](https://pytorch.org/docs/1.10.0/generated/torch.conj.html?highlight=conj#torch.conj)) is now a constant time operation, and returns a view of the input tensor with a conjugate bit set as can be seen by calling [torch.is_conj()](https://pytorch.org/docs/1.10.0/generated/torch.is_conj.html?highlight=is_conj#torch.is_conj) . This has already been leveraged in various other PyTorch operations like matrix multiplication, dot product etc., to fuse conjugation with the operation leading to significant performance gain and memory savings on both CPU and CUDA.\n\n# Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "### Distributed Training Releases Now in Stable \nIn 1.10, there are a number of features that are moving from beta to stable in the distributed package:\n* **(Stable) Remote Module**: This feature allows users to operate a module on a remote worker like using a local module, where the RPCs are transparent to the user. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details.\n* **(Stable) DDP Communication Hook**: This feature allows users to override how DDP synchronizes gradients across processes. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details. \n* **(Stable) ZeroRedundancyOptimizer**: This feature can be used in conjunction with DistributedDataParallel to reduce the size of per-process optimizer states. With this stable release, it now can handle uneven inputs to different data-parallel workers. Check out this [tutorial](https://pytorch.org/tutorials/advanced/generic_join.html). We also improved the parameter partition algorithm to better balance memory and computation overhead across processes. Refer to this [documentation](https://pytorch.org/docs/master/distributed.optim.html) and this [tutorial](https://pytorch.org/tutorials/recipes/zero_redundancy_optimizer.html) to learn more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "# Performance Optimization and Tooling\n\n### [Beta] Profile-directed typing in TorchScript \nTorchScript has a hard requirement for source code to have type annotations in order for compilation to be successful. For a long time, it was only possible to add missing or incorrect type annotations through trial and error (i.e., by fixing the type-checking errors generated by torch.jit.script one by one), which was inefficient and time consuming. \n\nNow, we have enabled profile directed typing for torch.jit.script by leveraging existing tools like MonkeyType, which makes the process much easier, faster, and more efficient. For more details, refer to the [documentation](https://pytorch.org/docs/1.9.0/jit.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) CPU Fusion \nIn PyTorch 1.10, we've added an LLVM-based JIT compiler for CPUs that can fuse together sequences of `torch` library calls to improve performance. While we've had this capability for some time on GPUs, this release is the first time we've brought compilation to the CPU. \nYou can check out a few performance results for yourself in this [Colab notebook](https://colab.research.google.com/drive/1xaH-L0XjsxUcS15GG220mtyrvIgDoZl6?usp=sharing).\n\n### (Beta) PyTorch Profiler \nThe objective of PyTorch Profiler is to target the execution steps that are the most costly in time and/or memory, and visualize the workload distribution between GPUs and CPUs. PyTorch 1.10 includes the following key features:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "* **Enhanced Memory View**: This helps you understand your memory usage better. This tool will help you avoid Out of Memory errors by showing active memory allocations at various points of your program run.\n* **Enhanced Automated Recommendations**: This helps provide automated performance recommendations to help optimize your model. The tools recommend changes to batch size, TensorCore, memory reduction technologies, etc.\n* **Enhanced Kernel View**: Additional columns show grid and block sizes as well as shared memory usage and registers per thread.\n* **Distributed Training**: Gloo is now supported for distributed training jobs.\n* **Correlate Operators in the Forward & Backward Pass**: This helps map the operators found in the forward pass to the backward pass, and vice versa, in a trace view.\n* **TensorCore**: This tool shows the Tensor Core (TC) usage and provides recommendations for data scientists and framework developers.\n* **NVTX**: Support for NVTX markers was ported from the legacy autograd profiler.\n* **Support for profiling on mobile devices**: The PyTorch profiler now has better integration with TorchScript and mobile backends, enabling trace collection for mobile workloads.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "Refer to this [documentation](https://pytorch.org/docs/stable/profiler.html) for details. Check out this [tutorial](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html) to learn how to get started with this feature. \n\n# PyTorch Mobile\n\n### (Beta) Android NNAPI Support in Beta \nLast year we [released prototype support](https://medium.com/pytorch/pytorch-mobile-now-supports-android-nnapi-e2a2aeb74534) for Android\u2019s Neural Networks API (NNAPI). NNAPI allows Android apps to run computationally intensive neural networks on the most powerful and efficient parts of the chips that power mobile phones, including GPUs (Graphics Processing Units) and NPUs (specialized Neural Processing Units). \n\nSince the prototype we\u2019ve added more op coverage, added support for load-time flexible shapes and ability to run the model on the host for testing. Try out this feature using the [tutorial](https://pytorch.org/tutorials/prototype/nnapi_mobilenetv2.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "Additionally, Transfer Learning steps have been added to Object Detection examples. Check out this [GitHub page](https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection#transfer-learning) to learn more. Please provide your feedback or ask questions on the [forum](https://discuss.pytorch.org/c/mobile/18). You can also check out [this presentation](https://www.youtube.com/watch?v=B-2spa3UCTU) to get an overview. \n\nThanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch). \n\nCheers!\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Ambient Clinical Intelligence: Generating Medical Reports with PyTorch\"\nauthor: Miguel Del-Agua, Principal Research Scientist, Nuance and Jeremy Jancsary, Senior Principal Research Scientist, Nuance\nfeatured-img: \"\"\n---\n\n## Introduction\n\nComplete and accurate clinical documentation is an essential tool for tracking patient care. It allows for treatment plans to be shared among care teams to aid in continuity of care and ensures a transparent and effective process for reimbursement.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Physicians are responsible for documenting patient care. Traditional clinical documentation methods have resulted in a sub-par patient-provider experience, less time interacting with patients, and decreased work-life balance. A significant amount of physicians\u2019 time is spent in front of the computer doing administrative tasks. As a result, patients are less satisfied with the overall experience, and physicians, who prepare for years studying medicine, cannot practice at the top of their license and are burned out. Every hour physicians provide direct clinical face time to patients results in nearly two additional hours spent on EHR and desk work within the clinic day. Outside office hours, physicians [spend another 1 to 2 hours of personal](https://www.acpjournals.org/doi/10.7326/m16-0961) time each night doing additional computer and other clerical work.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* [42% of all physicians reported having burnout. \u2013 Medscape](https://www.medscape.com/slideshow/2020-lifestyle-burnout-6012460)\n* [The problem has grown worse due to the pandemic with 64% of U.S. physicians now reporting burnout. - AAFP](https://www.aafp.org/journals/fpm/blogs/inpractice/entry/covid_burnout_survey.html#:~:text=Physician%20burnout%20was%20already%20a,5%2C000%20%E2%80%94%20practice%20in%20the%20U.S.)\n* [\"Too many bureaucratic tasks e.g., charting and paperwork\" is the leading contribution to burnout, increased computerization ranks 4th.](https://login.medscape.com/login/sso/getlogin?urlCache=aHR0cHM6Ly93d3cubWVkc2NhcGUuY29tL3NsaWRlc2hvdy8yMDIwLWxpZmVzdHlsZS1idXJub3V0LTYwMTI0NjA%3D&ac=401) - Medscape\n* [75% of U.S. Consumers Wish Their Healthcare Experiences Were More Personalized,](https://www.businesswire.com/news/home/20200218005006/en/75-of-U.S.-Consumers-Wish-Their-Healthcare-Experiences-Were-More-Personalized-Redpoint-Global-Survey-Reveals)- Business Wire\n* [61% of patients would visit their healthcare provider more often if the communication experience felt more personalized.](https://www.businesswire.com/news/home/20200218005006/en/75-of-U.S.-Consumers-Wish-Their-Healthcare-Experiences-Were-More-Personalized-Redpoint-Global-Survey-Reveals) \u2013 Business Wire", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Physician burnout is one of the primary causes for increased [medical errors](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6175626/), malpractice suits, turnover, and decreased access to care. Burnout leads to an increase in healthcare costs and a decrease in overall patient satisfaction. [Burnout costs the United States $4.6 billion a year.](https://www.nejm.org/doi/full/10.1056/NEJMp2003149)\n\nWhat can we do to bring back trust, joy, and humanity to the delivery of healthcare? A significant portion of the administrative work consists of entering patient data into Electronic Health Records (EHRs) and creating clinical documentation. Clinical documentation is created from information already in the EHR as well as from the patient-provider encounter conversation.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "This article will showcase how the Nuance Dragon Ambient eXperience (DAX), an AI-powered, voice-enabled, ambient clinical intelligence solution, automatically documents patient encounters accurately and efficiently at the point of care and the technologies that enable it.\n\nNuance DAX enhances the quality of care and patient experience, increases provider efficiency and satisfaction, and improves financial outcomes. It can be used in office and telehealth settings in all ambulatory specialties, including primary and urgent care.\n\n

\n \n

\n\n## Natural Language Processing", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Natural Language Processing (NLP) is one of the most challenging fields in Artificial Intelligence (AI). It comprehends a set of algorithms that allow computers to understand or generate the language used by humans. These algorithms can process and analyze vast amounts of natural language data from different sources (either sound or text) to build models that can understand, classify, or even generate natural language as humans would. Like other fields in AI, NLP has significantly progressed thanks to the advent of Deep Learning (DL), which has resulted in models that can obtain results on par with humans in some tasks.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "These advanced NLP techniques are being applied in healthcare. During a typical patient-provider encounter, a conversation ensues where the doctor constructs, through questions and answers, a chronological description of the development of the patient's presenting illness or symptoms. A physician examines the patient and makes clinical decisions to establish a diagnosis and determine a treatment plan. This conversation, and data in the EHR, provide the required information for physicians to generate the clinical documentation, referred to as medical reports.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Two main NLP components play a role in automating the creation of clinical documentation. The first component, Automatic Speech Recognition (ASR), is used to translate speech into text. It takes the audio recording of the encounter and generates a conversation transcription (cf. Figure 2). The second component, Automatic Text Summarization, helps generate summaries from large text documents. This component is responsible for understanding and capturing the nuances and most essential aspects from the transcribed conversation into a final report in narrative form (cf. Figure 3), structured form, or a combination of both.\n\nWe will focus on this second component, Automatic Text Summarization, which is a difficult task with many challenges:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* Its performance is tied to the ASR quality from multiple speakers (noisy input).\n* The input is conversational in nature and contains layman's terms.\n* Protected Health Information (PHI) regulations limit medical data access.\n* The information for one output sentence is potentially spread across multiple conversation turns.\n* There is no explicit sentence alignment between input and output.\n* Various medical specialties, encounter types, and EHR systems constitute a broad and complex output space. \n* Physicians have different styles of conducting encounters and have their preferences for medical reports; there is no standard. \n* Standard summarization metrics might differ from human judgment of quality.\n\n

\n \n

\n\n

\nFigure 2: Transcript of a patient-doctor conversation\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 3: Excerpt of an AI-generated medical report. HPI stands for History of present illness.\n

\n\n## Text Summarization with PyTorch and Fairseq\n\n[PyTorch](https://pytorch.org/) is an open-source machine learning framework developed by Facebook that helps researchers prototype Deep Learning models. The [Fairseq](https://github.com/pytorch/fairseq) toolkit is built on top of PyTorch and focuses on sequence generation tasks, such as Neural Machine Translation (NMT) or Text Summarization. Fairseq features an active community that is continuously providing reference implementations of state-of-the-art models. It contains many built-in components (model architectures, modules, loss functions, and optimizers) and is easily extendable with plugins.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Text summarization constitutes a significant challenge in NLP. We need models capable of generating a short version of a document while retaining the key points and avoiding uninformative content. These challenges can be addressed with different approaches. 1). Abstractive text summarization aimed at training models that can generate a summary in narrative form. 2). Extractive methods where the models are trained to select the most important parts from the input text. 3). A combination of the two, where the essential parts from the input are selected and then summarized in an abstractive fashion. Hence, summarization can be accomplished via a single end-to-end network or as a pipeline of extractive and abstractive components. To that end, Fairseq provides all the necessary tools to be successful in our endeavor. It features either end-to-end models such as the classical Transformer, different types of Language Models and pre-trained versions that enable researchers to focus on what matters most\u2014to build state-of-the-art models that generate valuable reports.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "However, we are not just summarizing the transcribed conversation; we generate high-quality medical reports, which have many considerations.\n\n* Every section of a medical report is different in terms of content, structure, fluency, etc.\n* All medical facts mentioned in the conversation should be present in the report, for example, a particular treatment or dosage.\n* In the healthcare domain, the vocabulary is extensive, and models need to deal with medical terminology.\n* Patient-doctor conversations are usually much longer than the final report.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} {"page_content": "All these challenges require our researchers to run a battery of extensive experiments. Thanks to the flexibility of PyTorch and Fairseq, their productivity has greatly increased. Further, the ecosystem offers an easy path from ideation, implementation, experimentation, and final roll-out to production. Using multiple GPUs or CPUs is as simple as providing an additional argument to the tools, and because of the tight Python integration, PyTorch code can be easily debugged.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In our continuous effort to contribute to the open-source community, features have been developed at Nuance and pushed to the Fairseq GitHub repository. These try to overcome some of the challenges mentioned such as, facilitating copying of, especially rare or unseen, words from the input to summary, training speedups by improving Tensor Core utilization, and ensuring TorchScript compatibility of different Transformer configurations. Following, we will show an example of how to train a Transformer model", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "with a Pointer Generator mechanism (Transformer-PG), which can copy words from the input.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "How to build a Transformer model with a Pointer Generator mechanism\n\nIn this step-by-step guide, it is assumed the user has already installed PyTorch and Fairseq.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. Create a vocabulary and extend it with source position markers:\n\nThese markers will allow the model to point to any word in the input sequence.\n\n```python\nvocab_size=\nposition_markers=512\nexport LC_ALL=C\ncat train.src train.tgt |\n tr -s '[:space:]' '\\n' |\n sort |\n uniq -c |\n sort -k1,1bnr -k2 |\n head -n \"$((vocab_size - 4))\" |\n awk '{ print $2 \" \" $1 }' > dict.pg.txt\npython3 -c \"[print(' 0'.format(n)) for n in range($position_markers)]\" >> dict.pg.txt", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This will create a file \"dict.pg.txt\" that contains the \\ most frequent words followed by 512 position markers named from \"\\\" to \"\\\".\n\nIn case we have an input like\n\n```python\nsrc = \"Hello, I'm The Dogtor\"\n```\n\nit could happen that our model has been trained without the word \"Dogtor\" in its vocabulary. Therefore, when we feed this sequence into the model, it should be converted to:\n\n```python\nsrc = \"Hello, I'm The \"", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Now, \"\\\" is part of our vocabulary and could be predicted by the model (this is where the pointer-generator comes in). In such a case, we will only need to post-process the output to replace \"\\\" by the word at input position 3.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. Preprocess the text data to replace unknown words by its positional markers:\n\nWe can use the scripts from [https://github.com/pytorch/fairseq/tree/master/examples/pointer_generator](https://github.com/pytorch/fairseq/tree/master/examples/pointer_generator).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Considering we have our data in:\n# train_src = /path/to/train.src\n# train_tgt = /path/to/train.tgt\n# valid_src = /path/to/valid.src\n# valid_tgt = /path/to/valid.tgt\n./preprocess.py --source /path/to/train.src \\\n --target /path/to/train.tgt \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/train.pg.src \\\n --target-out /path/to/train.pg.tgt", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "./preprocess.py --source /path/to/valid.src \\\n --target /path/to/valid.tgt \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/valid.pg.src \\\n --target-out /path/to/valid.pg.tgt\n\n./preprocess.py --source /path/to/test.src \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/test.pg.src\n```", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. Now let's binarize the data, so that it can be processed faster:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nfairseq-preprocess --task \"translation\" \\\n --source-lang \"pg.src\" \\\n --target-lang \"pg.tgt\" \\\n --trainpref /path/to/train \\\n --validpref /path/to/valid \\\n --srcdict dict.pg.txt \\\n --cpu \\\n --joined-dictionary \\\n --destdir \n```", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "You might notice the type of task is \"translation\". This is because there is no \"summarization\" task available; we could understand it as a kind of NMT task where the input and output languages are shared and the output (summary) is shorter than the input.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "4. Now we can train the model:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nfairseq-train \\\n --save-dir \\\n --task \"translation\" \\\n --source-lang \"src\" \\\n --target-lang \"tgt\" \\\n --arch \"transformer_pointer_generator\" \\\n --max-source-positions 512 \\\n --max-target-positions 128 \\\n --truncate-source \\\n --max-tokens 2048 \\\n --required-batch-size-multiple 1 \\\n --required-seq-len-multiple 8 \\", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "--share-all-embeddings \\\n --dropout 0.1 \\\n --criterion \"cross_entropy\" \\\n --optimizer adam \\\n --adam-betas '(0.9, 0.98)' \\\n --adam-eps 1e-9 \\\n --update-freq 4 \\\n --lr 0.004 \\\n # Pointer Generator\n --alignment-layer -1 \\\n --alignment-heads 1 \\\n --source-position-markers 512", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This configuration makes use of features Nuance has contributed back to Fairseq:\n\n* Transformer with a Pointer Generator mechanism to facilitate copying of words from the input.\n* Sequence length padded to a multiple of 8 to better use tensor cores and reduce training time.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "5. Now let's take a look at how to generate a summary with our new medical report generation system:\n\n```python\nimport torch\nfrom examples.pointer_generator.pointer_generator_src.transformer_pg import TransformerPointerGeneratorModel\n\n# Patient-Doctor conversation\ninput = \"[doctor] Lisa Simpson, thirty six year old female, presents to the clinic today because \" \\\n \"she has severe right wrist pain\"", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Load the model\nmodel = TransformerPointerGeneratorModel.from_pretrained(data_name_or_path=,\n model_name_or_path=,\n checkpoint_file=\"checkpoint_best.pt\")\n\nresult = model.translate([input], beam=2)\n\nprint(result[0])\nMs. is a 36-year-old female who presents to the clinic today for evaluation of her right wrist.\n```", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "6. Alternatively, we can use fairseq-interactive and a postprocessing tool to substitute positional unknown tokens by its words from the input:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nfairseq-interactive \\\n --batch-size \\\n --task translation \\\n --source-lang src \\\n --target-lang tgt \\\n --path /checkpoint_last.pt \\\n --input /path/to/test.pg.src \\\n --buffer-size 20 \\\n --max-len-a 0 \\\n --max-len-b 128 \\\n --beam 2 \\\n --skip-invalid-size-inputs-valid-test | tee generate.out", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "grep \"^H-\" generate.out | cut -f 3- > generate.hyp\n\n./postprocess.py \\\n\t--source <(awk 'NF<512' /path/to/test.pg.src) \\\n\t--target generate.hyp \\\n\t--target-out generate.hyp.processed", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Now we have the final set of reports in \"generate.hyp.processed\", with \"\\\" replaced by the original word from the input sequence.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Model Deployment\n\nPyTorch offers great flexibility in modeling and a rich surrounding ecosystem. However, while several recent articles have suggested that the use of PyTorch in research and academia may be close to surpassing TensorFlow, there seems to be an overall sense of TensorFlow being the preferred platform for deployment to production. Is this still the case in 2021? Teams looking to serve their PyTorch models in production have a few options.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Before describing our journey, let's take a brief detour and define the term model.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Models as computation graphs", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "A few years back, it was still common for machine learning toolkits to support only particular classes of models of a rather fixed and rigid structure, with only a few degrees of freedom (like the kernel of a support vector machine or the number of hidden layers of a neural network). Inspired by foundational work in Theano, toolkits like Microsoft's CNTK or Google's TensorFlow were among the first to popularize a more flexible view on models, as computation graphs with associated parameters that can be", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "estimated from data. This view blurred the boundaries between popular types of models (such as DNNs or SVMs), as it became easy to blend the characteristics of each into your type of graph. Still, such a graph had to be defined upfront before estimating its parameters, and it was pretty static. This made it easy to save models to a self-contained bundle, like a TensorFlow SavedModel (such a bundle simply contains the structure of the graph, as well as the concrete values of the estimated parameters).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "However, debugging such models can be difficult because the statements in the Python code that build the graph are logically separate from the lines that execute it. Researchers also long for easier ways of expressing dynamic behavior, such as the computation steps of the forward pass of a model being conditionally dependent on its input data (or its previous output).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Most recently, the above limitations have led to a second revolution spearheaded by PyTorch and TensorFlow 2. The computation graph is no longer defined explicitly. Instead, it will be populated implicitly as the Python code executes operations on tensor arguments. An essential technique that powers this development is automatic differentiation. As the computation graph is being built implicitly while executing the steps of the forward pass, all the necessary data will be tracked for later computation of", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "the gradient concerning the model parameters. This allows for great flexibility in training a model, but it raises an important question. If the computation happening inside a model is only implicitly defined through our Python code's steps as it executes concrete data, what is it that we want to save as a model? The answer \u2013 at least initially \u2013 was the Python code with all its dependencies, along with the estimated parameters. This is undesirable for practical reasons. For instance, there is a danger that", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "the team working on model deployment does not exactly reproduce the Python code dependencies used during training, leading to subtly divergent behavior. The solution typically consists of combining two techniques, scripting and tracing, that is, extra annotations in your Python code and execution of your code on exemplary input data, allowing PyTorch to define and save the graph that should be executed during later inference on new, unseen data. This requires some discipline by whoever creates the model", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "code (arguably voiding some of the original flexibility of eager execution), but it results in a self-contained model bundle in TorchScript format. The solution in TensorFlow 2 is remarkably similar.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Serving our report generation models", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Our journey in deploying the report generation models reflects the above discussion. We started out serving our models by deploying the model code and its dependencies along with the parameter checkpoints in a custom Docker image exposing a gRPC service interface. However, we soon noticed that it became error-prone to replicate the exact code and environment used by the modeling team while estimating the parameters. Moreover, this approach prevented us from leveraging high-performance model serving", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "frameworks like NVIDIA's Triton, which is written in C++ and requires self-contained models that can be used without a Python interpreter. At this stage, we were facing a choice between attempting to export our PyTorch models to ONNX or TorchScript format. ONNX is an open specification for representing machine learning models that increasingly finds adoption. It is powered by a high-performance runtime developed by Microsoft (ONNX Runtime). While we were able to achieve performance acceleration for our", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "TensorFlow BERT-based model using ONNX Runtime, at the time one of our PyTorch model required some operators that weren\u2019t yet supported in ONNX. Rather than implement these using custom operators, we decided to look into TorchScript for the time being.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "A maturing ecosystem", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Is it all roses? No, it has been a rockier journey than we expected. We encountered what seems to be a memory leak in the MKL libraries used by PyTorch while serving the PyTorch code directly. We encountered deadlocks in trying to load multiple models from multiple threads. We had difficulties exporting our models to ONNX and TorchScript formats. Models would not work out-of-the-box on hardware with multiple GPUs, they always accessed the particular GPU device on which they were exported. We encountered", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "excessive memory usage in the Triton inference server while serving TorchScript models, which we found out was due to automatic differentiation accidentally being enabled during the forward pass. However, the ecosystem keeps improving, and there is a helpful and vibrant open-source community eager to work with us to mitigate such issues.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Where to go from here? For those that require the flexibility of serving PyTorch code directly, without going through the extra step of exporting self-contained models, it is worth pointing out that the TorchServe project now provides a way of bundling the code together with parameter checkpoints into a single servable archive, greatly reducing the risk of code and parameters running apart. To us, however, exporting models to TorchScript has proven beneficial. It provides a clear interface between modeling", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "and deployment teams, and TorchScript further reduces the latency when serving models on GPU via its just-in-time compilation engine.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Scaling at large and the future", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Finally, efficient deployment to the cloud is about more than just computing the response of a single model instance efficiently. Flexibility is needed in managing, versioning and updating models. High-level scalability must be achieved via techniques such as load-balancing, horizontal scaling and vertical scaling. If many models are involved, scale-to-zero quickly becomes a topic as it is unacceptable to pay for serving models that do not answer any requests. Providing such extra functionality on top of a", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "low-level inference server like Triton is the job of an orchestration framework. After gaining some first experience with KubeFlow, to that end, we decided to turn our attention to Azure ML, which provides similar functionality but integrates more deeply with the Azure platform, on which we crucially rely for large parts of our technology stack already. This part of our journey has just begun.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Conclusion", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Academia has long recognized that we are \"standing on the shoulders of giants.\" As Artificial Intelligence is maturing from a scientific discipline into technology, the same spirit of collaboration that originally fueled its scientific foundation has carried over into the world of software engineering. Open-source enthusiasts join technology companies worldwide to build open software ecosystems that allow for new angles at solving some of the most pressing challenges of modern society. In this article,", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "we've taken a look at Nuance's [Dragon Ambient eXperience](http://www.nuance.com/ambient), an AI-powered, voice-enabled solution that automatically documents patient care, reducing healthcare providers' administrative burdens. Nuance DAX improves the patient-provider experience, reduces physician burnout, and improves financial outcomes. It brings back trust, joy, and humanity to the delivery of healthcare. Fairseq and PyTorch have proven to be an incredible platform for powering this AI technology, and in", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "turn, Nuance has contributed back some of its innovations in this space. For further reading, we invite you to take a look at our recent [ACL publication](https://www.aclweb.org/anthology/2020.nlpmc-1.4/) and the [Nuance \"What's Next\" blog](https://whatsnext.nuance.com/rd/using-deep-learning-to-generate-medical-reports/).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Microsoft becomes maintainer of the Windows version of PyTorch'\nauthor: Maxim Lukiyanov - Principal PM at Microsoft, Emad Barsoum - Group EM at Microsoft, Guoliang Hua - Principal EM at Microsoft, Nikita Shulga - Tech Lead at Facebook, Geeta Chauhan - PE Lead at Facebook, Chris Gottbrath - Technical PM at Facebook, Jiachen Pu - Engineer at Facebook", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Along with the PyTorch 1.6 release, we are excited to announce that Microsoft has expanded its participation in the PyTorch community and will be responsible for the development and maintenance of the PyTorch build for Windows.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "According to the latest [Stack Overflow developer survey](https://insights.stackoverflow.com/survey/2020#technology-developers-primary-operating-systems), Windows remains the primary operating system for the developer community (46% Windows vs 28% MacOS). [Jiachen Pu](https://github.com/peterjc123) initially made a heroic effort to add support for PyTorch on Windows, but due to limited resources, Windows support for PyTorch has lagged behind other platforms. Lack of test coverage resulted in unexpected", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "issues popping up every now and then. Some of the core tutorials, meant for new users to learn and adopt PyTorch, would fail to run. The installation experience was also not as smooth, with the lack of official PyPI support for PyTorch on Windows. Lastly, some of the PyTorch functionality was simply not available on the Windows platform, such as the TorchAudio domain library and distributed training support. To help alleviate this pain, Microsoft is happy to bring its Windows expertise to the table and", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "bring PyTorch on Windows to its best possible self.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the PyTorch 1.6 release, we have improved the core quality of the Windows build by bringing test coverage up to par with Linux for core PyTorch and its domain libraries and by automating tutorial testing. Thanks to the broader PyTorch community, which contributed TorchAudio support to Windows, we were able to add test coverage to all three domain libraries: TorchVision, TorchText and TorchAudio. In subsequent releases of PyTorch, we will continue improving the Windows experience based on community", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "feedback and requests. So far, the feedback we received from the community points to distributed training support and a better installation experience using pip as the next areas of improvement.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In addition to the native Windows experience, Microsoft released a preview adding [GPU compute support to Windows Subsystem for Linux (WSL) 2](https://blogs.windows.com/windowsdeveloper/2020/06/17/gpu-accelerated-ml-training-inside-the-windows-subsystem-for-linux/) distros, with a focus on enabling AI and ML developer workflows. WSL is designed for developers that want to run any Linux based tools directly on Windows. This preview enables valuable scenarios for a variety of frameworks and Python packages", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "that utilize [NVIDIA CUDA](https://developer.nvidia.com/cuda/wsl) for acceleration and only support Linux. This means WSL customers using the preview can run native Linux based PyTorch applications on Windows unmodified without the need for a traditional virtual machine or a dual boot setup.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Getting started with PyTorch on Windows\nIt's easy to get started with PyTorch on Windows. To install PyTorch using Anaconda with the latest GPU support, run the command below. To install different supported configurations of PyTorch, refer to the installation instructions on [pytorch.org](https://pytorch.org).\n\n`conda install pytorch torchvision cudatoolkit=10.2 -c pytorch`", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Once you install PyTorch, learn more by visiting the [PyTorch Tutorials](https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html) and [documentation](https://pytorch.org/docs/stable/index.html).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Getting started with PyTorch on Windows Subsystem for Linux\nThe [preview of NVIDIA CUDA support in WSL](https://docs.microsoft.com/en-us/windows/win32/direct3d12/gpu-cuda-in-wsl) is now available to Windows Insiders running Build 20150 or higher. In WSL, the command to install PyTorch using Anaconda is the same as the above command for native Windows. If you prefer pip, use the command below.\n\n`pip install torch torchvision`", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "You can use the same tutorials and documentation inside your WSL environment as on native Windows. This functionality is still in preview so if you run into issues with WSL please share feedback via the [WSL GitHub repo](https://github.com/microsoft/WSL) or with NVIDIA CUDA support share via NVIDIA\u2019s [Community Forum for CUDA on WSL](https://forums.developer.nvidia.com/c/accelerated-computing/cuda/cuda-on-windows-subsystem-for-linux/303).", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Feedback\nIf you find gaps in the PyTorch experience on Windows, please let us know on the [PyTorch discussion forum](https://discuss.pytorch.org/c/windows/26) or file an issue on [GitHub](https://github.com/pytorch/pytorch) using the #module: windows label.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated PyTorch 2 Transformers\"\nauthor: Michael Gschwind, Driss Guessous, Christian Puhrsch\n---", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch 2.0 release includes a new high-performance implementation of the PyTorch Transformer API with the goal of making training and deployment of state-of-the-art Transformer models affordable. Following the successful release of \u201cfastpath\u201d inference execution (\u201cBetter Transformer\u201d), this release introduces high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA).", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "You can take advantage of the new fused SDPA kernels either by calling the new SDPA operator directly (as described in the [SDPA tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html#beta-implementing-high-performance-transformers-with-scaled-dot-product-attention-sdpa)), or transparently via integration into the pre-existing PyTorch Transformer API. All features of the PyTorch Transformer API will continue to work compatibly, with many features mapped to", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "high-performance SDPA kernels, while other features are impossible to support with higher performance (e.g., need_weights, as per below) while expanded high-performance support for other features may still be under active development. \\", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "\\", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "Similar to the \u201cfastpath\u201d architecture, custom kernels are fully integrated into the PyTorch Transformer API \u2013 thus, using the native Transformer and MultiHeadAttention API will enable users to transparently see significant speed improvements. Unlike the \u201cfastpath\u201d architecture, the newly introduced \u201ccustom kernels\u201d support many more use cases including models using Cross-Attention, Transformer Decoders, and for training models, in addition to the existing fastpath inference for fixed and variable sequence", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "length Transformer Encoder and Self Attention use cases.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "To take full advantage of different hardware models and Transformer use cases, multiple SDPA custom kernels are supported, with custom kernel selection logic that will pick the highest-performance kernel for a given model and hardware type. In particular, the first custom kernels included with the PyTorch 2.0 release are the [Flash Attention](https://arxiv.org/abs/2205.14135) kernel (sdpa_flash, for 16-bit floating point training and inference on Nvidia GPUs with SM80+ architecture level) and the [xFormers", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "memory-efficient attention](https://github.com/facebookresearch/xformers) kernel (sdpa_mem_eff, for 16-bit and 32-bit floating point training and inference on a broad range of Nvidia GPUs). A general-purpose kernel sdpa_math provides an implementation when the custom kernels are not applicable.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "As mentioned, custom kernels provide a wider range of support for execution scenarios To ensure efficient execution (e,g., to use GPU tensor cores), model configurations need to meet a small number of requirements. This list of requirements will evolve over time, prospectively relaxing constraints limiting the usage of currently supported custom kernels, or providing additional kernels in the future.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "For the most up to date list of custom kernels and dispatch constraints, you can refer to [sdp_utils.h](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/transformers/cuda/sdp_utils.h). As of PyTorch 2.0, the existing fused SDPA kernels have the following constraints:", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* Flash Attention only supports 16 bit floating point data types (float16 and bfloat16).\n* The head dimension must be a multiple of 8 for 16-bit floating point numbers and a multiple of 4 for 32-bit floating point numbers. At present, the maximum head_dim support for the Flash Attention custom kernel is 128.\n* The CUDA architecture level must be sm5x or better for the mem_efficient kernel, and sm80 for Flash Attention.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* Flash Attention supports arbitrary dropout, in PyTorch 2.0 the mem_efficient kernel does not support dropout (i.e., dropout must be set to zero for this kernel to be selected in PyTorch 2.0).", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* To support variable-sequence length batches, all SDPA kernels support Nested Tensor inputs that combine input data and padding information using variable sequence length tensors for forward. (You can find more information about Nested Tensors in the [Nested Tensor tutorial](https://pytorch.org/tutorials/prototype/nestedtensor.html).)", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* You can specify both a _key_padding_mask_ and an _attn_mask_ by combining them before passing them to the SDPA operator. In particular, you can use the per-batch-element key padding mask of the nn.Transformer API to implement training for variable-sequence length inputs in a batch.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* At present, the only attention mask supported by fused kernel implementation is the causal mask commonly used for training. To specify the causal mask in custom kernels, it must be specified with the _is_causal_ boolean and _attn_mask_ must be None. \n* Support for Nested Tensors is still under development. Specifically, in PyTorch 2.0, only the sdpa_math kernel supports training with Nested Tensors. Also, PyTorch 2.0 does not support Nested Tensors as part of code being compiled with torch.compile().", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "* The SDPA operator does not support returning averaged attention weights because computing them defeats the optimizations that enabled fused kernels to execute more efficiently. The argument _need_weights_ for torch.nn.MultiheadAttention's forward function defaults to True. In order to use the fused kernels, _need_weights_ needs to be set to _need_weights=False_.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "We find that an attention mask is rarely used in real-world applications, except for the causal mask during training. Consequently, we reduce kernel complexity and compute cost by building in the option to use a causal mask as attention mask, and select this new capability with the _is_causal_ parameter introduced in conjunction with the new SDPA operator.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "Providing the _is_causal_ Boolean flag for the frequently used causal mask also obviates the expensive and memory-intensive allocation of a causal mask, increasing training memory efficiency by allowing more memory to be used for large batch sizes, and reduce memory bandwidth and cache contention \u2013 which are both at a premium in GPU accelerators \u2013 by not needing to load an attention mask tensor.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "If the constraints of none of the available custom kernels are met, then training falls back to using the default sdpa_math kernel, implementing the mathematical equations for scaled dot product attention using a sequence of PyTorch operator to implement SDPA. This is the most general \u201ccatch-all\u201d fallback kernel to ensure successful training for all models.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "In addition to the existing Transformer API, model developers may also use the scaled dot product attention kernels directly by calling the new `scaled_dot_product_attention()` operator. This operator may be used to efficiently implement multi-head attention by combining it with in-projection and outprojection, as described in the [SDPA tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "In addition to adding custom kernels, Accelerated PyTorch 2 Transformers are integrated with PyTorch 2.0 compilation. To use your model while benefiting from the additional acceleration of PT2-compilation (for inference or training), pre-process the model with\n\n\n```\nmodel = torch.compile(model)", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "We have achieved major speedups for training transformer models and in particular large language models with Accelerated PyTorch 2 Transformers using a combination of custom kernels and torch.compile().", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "![Better Transformer chart](/assets/images/pytorch_better_transformer_chart1.png){:width=\"100%\"}\nFigure: Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "Finally, because the custom kernels are much more memory efficient, try to increase the size of training batches to achieve faster training with increased batch size.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "In addition to automatic kernel selection, a context manager enables developers to override the kernel selection algorithm \u2013 this is not required for day to day operation, but enables developers to debug their code as well as enable performance engineers to override kernel selection. The SDPA tutorial provides additional information on using the SDPA context manager.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "In addition to availability as part of the nn.Transformer API, Accelerated PyTorch 2 Transformer custom kernels are also available in conjunction with the torchtext, torchvision, and fairseq domain libraries with the launch of PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Mapillary Research: Seamless Scene Segmentation and In-Place Activated BatchNorm'\nauthor: Lorenzo Porzi, Mapillary\nredirect_from: /2019/07/23/mapillary-research.html\n---", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "With roads in developed countries like the US changing up to 15% annually, Mapillary addresses a growing demand for keeping maps updated by combining images from any camera into a 3D visualization of the world. Mapillary's independent and collaborative approach enables anyone to collect, share, and use street-level images for improving maps, developing cities, and advancing the automotive industry.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Today, people and organizations all over the world have contributed more than 600 million images toward Mapillary's mission of helping people understand the world's places through images and making this data available, with clients and partners including the World Bank, HERE, and Toyota Research Institute.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Mapillary\u2019s computer vision technology brings intelligence to maps in an unprecedented way, increasing our overall understanding of the world. [Mapillary](https://www.mapillary.com/) runs state-of-the-art semantic image analysis and image-based 3d modeling at scale and on all its images. In this post we discuss two recent works from Mapillary Research and their implementations in PyTorch - Seamless Scene Segmentation [1] and In-Place Activated BatchNorm [2] - generating Panoptic segmentation results and", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "saving up to 50% of GPU memory during training, respectively.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Seamless Scene Segmentation\n\n_Github project page: [https://github.com/mapillary/seamseg/](https://github.com/mapillary/seamseg/)_\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "The objective of Seamless Scene Segmentation is to predict a \u201cpanoptic\u201d segmentation [3] from an image, that is a complete labeling where each pixel is assigned with a class id and, where possible, an instance id. Like many modern CNNs dealing with instance detection and segmentation, we adopt the Mask R-CNN framework [4], using ResNet50 + FPN [5] as a backbone. This architecture works in two stages: first, the \u201cProposal Head\u201d selects a set of candidate bounding boxes on the image (i.e. the proposals) that", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "could contain an object; then, the \u201cMask Head\u201d focuses on each proposal, predicting its class and segmentation mask. The output of this process is a \u201csparse\u201d instance segmentation, covering only the parts of the image that contain countable objects (e.g. cars and pedestrians).", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "To complete our panoptic approach coined Seamless Scene Segmentation, we add a third stage to Mask R-CNN. Stemming from the same backbone, the \u201cSemantic Head\u201d predicts a dense semantic segmentation over the whole image, also accounting for the uncountable or amorphous classes (e.g. road and sky). The outputs of the Mask and Semantic heads are finally fused using a simple non-maximum suppression algorithm to generate the final panoptic prediction. All details about the actual network architecture, used", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "losses and underlying math can be found at the [project website](https://research.mapillary.com/publication/cvpr19a) for our CVPR 2019 paper [1].", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "While several versions of Mask R-CNN are publicly available, including an [official implementation](https://github.com/facebookresearch/Detectron) written in Caffe2, at Mapillary we decided to build Seamless Scene Segmentation from scratch using PyTorch, in order to have full control and understanding of the whole pipeline. While doing so we encountered a couple of main stumbling blocks, and had to come up with some creative workarounds we are going to describe next.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Dealing with variable-sized tensors", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Something that sets aside panoptic segmentation networks from traditional CNNs is the prevalence of variable-sized data. In fact, many of the quantities we are dealing with cannot be easily represented with fixed sized tensors: each image contains a different number of objects, the Proposal head can produce a different number of proposals for each image, and the images themselves can have different sizes. While this is not a problem per-se -- one could just process images one at a time -- we would still", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "like to exploit batch-level parallelism as much as possible. Furthermore, when performing distributed training with multiple GPUs, `DistributedDataParallel` expects its inputs to be batched, uniformly-sized tensors.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Our solution to these issues is to wrap each batch of variable-sized tensors in a `PackedSequence`. `PackedSequence` is little more than a glorified list class for tensors, tagging its contents as \u201crelated\u201d, ensuring that they all share the same type, and providing useful methods like moving all the tensors to a particular device, etc. When performing light-weight operations that wouldn\u2019t be much faster with batch-level parallelism, we simply iterate over the contents of the `PackedSequence` in a for loop.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "When performance is crucial, e.g. in the body of the network, we simply concatenate the contents of the PackedSequence, adding zero padding as required (like in RNNs with variable-length inputs), and keeping track of the original dimensions of each tensor.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "`PackedSequence`s also help us deal with the second problem highlighted above. We slightly modify `DistributedDataParallel` to recognize `PackedSequence` inputs, splitting them in equally sized chunks and distributing their contents across the GPUs.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Asymmetric computational graphs with Distributed Data Parallel", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Another, perhaps more subtle, peculiarity of our network is that it can generate asymmetric computational graphs across GPUs. In fact, some of the modules that compose the network are \u201coptional\u201d, in the sense that they are not always computed for all images. As an example, when the Proposal head doesn\u2019t output any proposal, the Mask head is not traversed at all. If we are training on multiple GPUs with `DistributedDataParallel`, this results in one of the replicas not computing gradients for the Mask head", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "parameters.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Prior to PyTorch 1.1, this resulted in a crash, so we had to develop a workaround. Our simple but effective solution was to compute a \u201cfake forward pass\u201d when no actual forward is required, i.e. something like this:\n\n```python\ndef fake_forward():\n fake_input = get_correctly_shaped_fake_input()\n fake_output = mask_head(fake_input)\n fake_loss = fake_output.sum() * 0\n return fake_loss", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Here, we generate a batch of bogus data, pass it through the Mask head, and return a loss that always back-progates zeros to all parameters.\n\nStarting from PyTorch 1.1 this workaround is no longer required: by setting `find_unused_parameters=True` in the constructor, `DistributedDataParallel` is told to identify parameters whose gradients have not been computed by all replicas and correctly handle them. This leads to some substantial simplifications in our code base!", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "In-place Activated BatchNorm\n\n_Github project page: [https://github.com/mapillary/inplace_abn/](https://github.com/mapillary/inplace_abn/)_", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Most researchers would probably agree that there are always constraints in terms of available GPU resources, regardless if their research lab has access to only a few or multiple thousands of GPUs. In a time where at Mapillary we still worked at rather few and mostly 12GB Titan X - style prosumer GPUs, we were searching for a solution that virtually enhances the usable memory during training, so we would be able to obtain and push state-of-the-art results on dense labeling tasks like semantic segmentation.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "In-place activated BatchNorm is enabling us to use up to 50% more memory (at little computational overhead) and is therefore deeply integrated in all our current projects (including Seamless Scene Segmentation described above).", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "When processing a BN-Activation-Convolution sequence in the forward pass, most deep learning frameworks (including PyTorch) need to store two big buffers, i.e. the input x of BN and the input z of Conv. This is necessary because the standard implementations of the backward passes of BN and Conv depend on their inputs to calculate the gradients. Using InPlace-ABN to replace the BN-Activation sequence, we can safely discard x, thus saving up to 50% GPU memory at training time. To achieve this, we rewrite the", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "backward pass of BN in terms of its output y, which is in turn reconstructed from z by inverting the activation function.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "The only limitation of InPlace-ABN is that it requires using an invertible activation function, such as leaky relu or elu. Except for this, it can be used as a direct, drop-in replacement for BN+activation modules in any network. Our native CUDA implementation offers minimal computational overhead compared to PyTorch\u2019s standard BN, and is available for anyone to use from here: [https://github.com/mapillary/inplace_abn/](https://github.com/mapillary/inplace_abn/).", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Synchronized BN with asymmetric graphs and unbalanced batches", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "When training networks with synchronized SGD over multiple GPUs and/or multiple nodes, it\u2019s common practice to compute BatchNorm statistics separately on each device. However, in our experience working with semantic and panoptic segmentation networks, we found that accumulating mean and variance across all workers can bring a substantial boost in accuracy. This is particularly true when dealing with small batches, like in Seamless Scene Segmentation where we train with a single, super-high resolution image", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "per GPU.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "InPlace-ABN supports synchronized operation over multiple GPUs and multiple nodes, and, since version 1.1, this can also be achieved in the standard PyTorch library using [SyncBatchNorm](https://pytorch.org/docs/stable/nn.html#syncbatchnorm). Compared to SyncBatchNorm, however, we support some additional functionality which is particularly important for Seamless Scene Segmentation: unbalanced batches and asymmetric graphs.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "As mentioned before, Mask R-CNN-like networks naturally give rise to variable-sized tensors. Thus, in InPlace-ABN we calculate synchronized statistics using a variant of the parallel algorithm described [here](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm), which properly takes into account the fact that each GPU can hold a different number of samples. PyTorch\u2019s SyncBatchNorm is currently being revised to support this, and the improved functionality will be available", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "in a future release.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "Asymmetric graphs (in the sense mentioned above) are another complicating factor one has to deal with when creating a synchronized BatchNorm implementation. Luckily, PyTorch\u2019s distributed group functionality allows us to restrict distributed communication to a subset of workers, easily excluding those that are currently inactive. The only missing piece is that, in order to create a distributed group, each process needs to know the ids of all processes that will participate in the group, and even processes", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "that are not part of the group need to call the `new_group()` function. In InPlace-ABN we handle it with a function like this:", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nimport torch.distributed as distributed\n\ndef active_group(active):\n \"\"\"Initialize a distributed group where each process can independently decide whether to participate or not\"\"\"\n world_size = distributed.get_world_size()\n rank = distributed.get_rank()", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "# Gather active status from all workers\n active = torch.tensor(rank if active else -1, dtype=torch.long, device=torch.cuda.current_device())\n active_workers = torch.empty(world_size, dtype=torch.long, device=torch.cuda.current_device())\n distributed.all_gather(list(active_workers.unbind(0)), active)\n\n # Create group\n active_workers = [int(i) for i in active_workers.tolist() if i != -1]\n group = distributed.new_group(active_workers)\n return group", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "First each process, including inactive ones, communicates its status to all others through an `all_gather` call, then it creates the distributed group with the shared information. In the actual implementation we also include a caching mechanism for groups, since `new_group()` is usually too expensive to call at each batch.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "References\n\n[1] Seamless Scene Segmentation; Lorenzo Porzi, Samuel Rota Bul\u00f2, Aleksander Colovic, Peter Kontschieder; Computer Vision and Pattern Recognition (CVPR), 2019\n\n[2] In-place Activated BatchNorm for Memory-Optimized Training of DNNs; Samuel Rota Bul\u00f2, Lorenzo Porzi, Peter Kontschieder; Computer Vision and Pattern Recognition (CVPR), 2018\n\n[3] Panoptic Segmentation; Alexander Kirillov, Kaiming He, Ross Girshick, Carsten Rother, Piotr Dollar; Computer Vision and Pattern Recognition (CVPR), 2019", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} -{"page_content": "[4] Mask R-CNN; Kaiming He, Georgia Gkioxari, Piotr Dollar, Ross Girshick; International Conference on Computer Vision (ICCV), 2017\n\n[5] Feature Pyramid Networks for Object Detection; Tsung-Yi Lin, Piotr Dollar, Ross Girshick, Kaiming He, Bharath Hariharan, Serge Belongie; Computer Vision and Pattern Recognition (CVPR), 2017", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "In our continuous effort to contribute to the open-source community, features have been developed at Nuance and pushed to the Fairseq GitHub repository. These try to overcome some of the challenges mentioned such as, facilitating copying of, especially rare or unseen, words from the input to summary, training speedups by improving Tensor Core utilization, and ensuring TorchScript compatibility of different Transformer configurations. Following, we will show an example of how to train a Transformer model with a Pointer Generator mechanism (Transformer-PG), which can copy words from the input.\n\n## How to build a Transformer model with a Pointer Generator mechanism\n\nIn this step-by-step guide, it is assumed the user has already installed PyTorch and Fairseq.\n\n### 1. Create a vocabulary and extend it with source position markers:\n\nThese markers will allow the model to point to any word in the input sequence.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nvocab_size=\nposition_markers=512\nexport LC_ALL=C\ncat train.src train.tgt |\n tr -s '[:space:]' '\\n' |\n sort |\n uniq -c |\n sort -k1,1bnr -k2 |\n head -n \"$((vocab_size - 4))\" |\n awk '{ print $2 \" \" $1 }' > dict.pg.txt\npython3 -c \"[print(' 0'.format(n)) for n in range($position_markers)]\" >> dict.pg.txt\n```\n\nThis will create a file \"dict.pg.txt\" that contains the \\ most frequent words followed by 512 position markers named from \"\\\" to \"\\\".\n\nIn case we have an input like\n\n```python\nsrc = \"Hello, I'm The Dogtor\"\n```\n\nit could happen that our model has been trained without the word \"Dogtor\" in its vocabulary. Therefore, when we feed this sequence into the model, it should be converted to:\n\n```python\nsrc = \"Hello, I'm The \"\n```", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Now, \"\\\" is part of our vocabulary and could be predicted by the model (this is where the pointer-generator comes in). In such a case, we will only need to post-process the output to replace \"\\\" by the word at input position 3.\n\n### 2. Preprocess the text data to replace unknown words by its positional markers:\n\nWe can use the scripts from [https://github.com/pytorch/fairseq/tree/master/examples/pointer_generator](https://github.com/pytorch/fairseq/tree/master/examples/pointer_generator).\n\n```python\n# Considering we have our data in:\n# train_src = /path/to/train.src\n# train_tgt = /path/to/train.tgt\n# valid_src = /path/to/valid.src\n# valid_tgt = /path/to/valid.tgt\n./preprocess.py --source /path/to/train.src \\\n --target /path/to/train.tgt \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/train.pg.src \\\n --target-out /path/to/train.pg.tgt", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "./preprocess.py --source /path/to/valid.src \\\n --target /path/to/valid.tgt \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/valid.pg.src \\\n --target-out /path/to/valid.pg.tgt\n\n./preprocess.py --source /path/to/test.src \\\n --vocab <(cut -d' ' -f1 dict.pg.txt) \\\n --source-out /path/to/test.pg.src\n```\n\n### 3. Now let's binarize the data, so that it can be processed faster:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nfairseq-preprocess --task \"translation\" \\\n --source-lang \"pg.src\" \\\n --target-lang \"pg.tgt\" \\\n --trainpref /path/to/train \\\n --validpref /path/to/valid \\\n --srcdict dict.pg.txt \\\n --cpu \\\n --joined-dictionary \\\n --destdir \n```\t\t \n\t\t\t\t \nYou might notice the type of task is \"translation\". This is because there is no \"summarization\" task available; we could understand it as a kind of NMT task where the input and output languages are shared and the output (summary) is shorter than the input.\n\n### 4. Now we can train the model:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nfairseq-train \\\n --save-dir \\\n --task \"translation\" \\\n --source-lang \"src\" \\\n --target-lang \"tgt\" \\\n --arch \"transformer_pointer_generator\" \\\n --max-source-positions 512 \\\n --max-target-positions 128 \\\n --truncate-source \\\n --max-tokens 2048 \\\n --required-batch-size-multiple 1 \\\n --required-seq-len-multiple 8 \\\n --share-all-embeddings \\\n --dropout 0.1 \\\n --criterion \"cross_entropy\" \\\n --optimizer adam \\\n --adam-betas '(0.9, 0.98)' \\\n --adam-eps 1e-9 \\\n --update-freq 4 \\\n --lr 0.004 \\\n # Pointer Generator\n --alignment-layer -1 \\\n --alignment-heads 1 \\\n --source-position-markers 512\n```\n\nThis configuration makes use of features Nuance has contributed back to Fairseq:", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* Transformer with a Pointer Generator mechanism to facilitate copying of words from the input.\n* Sequence length padded to a multiple of 8 to better use tensor cores and reduce training time.\n\n### 5. Now let's take a look at how to generate a summary with our new medical report generation system:\n\n```python\nimport torch\nfrom examples.pointer_generator.pointer_generator_src.transformer_pg import TransformerPointerGeneratorModel\n\n# Patient-Doctor conversation\ninput = \"[doctor] Lisa Simpson, thirty six year old female, presents to the clinic today because \" \\\n \"she has severe right wrist pain\"\n\n# Load the model\nmodel = TransformerPointerGeneratorModel.from_pretrained(data_name_or_path=,\n model_name_or_path=,\n checkpoint_file=\"checkpoint_best.pt\")\n\nresult = model.translate([input], beam=2)", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "print(result[0])\nMs. is a 36-year-old female who presents to the clinic today for evaluation of her right wrist.\n```\n\n### 6. Alternatively, we can use fairseq-interactive and a postprocessing tool to substitute positional unknown tokens by its words from the input:\n\n```python\nfairseq-interactive \\\n --batch-size \\\n --task translation \\\n --source-lang src \\\n --target-lang tgt \\\n --path /checkpoint_last.pt \\\n --input /path/to/test.pg.src \\\n --buffer-size 20 \\\n --max-len-a 0 \\\n --max-len-b 128 \\\n --beam 2 \\\n --skip-invalid-size-inputs-valid-test | tee generate.out\n\ngrep \"^H-\" generate.out | cut -f 3- > generate.hyp\n\n./postprocess.py \\\n\t--source <(awk 'NF<512' /path/to/test.pg.src) \\\n\t--target generate.hyp \\\n\t--target-out generate.hyp.processed\n```", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Now we have the final set of reports in \"generate.hyp.processed\", with \"\\\" replaced by the original word from the input sequence.\n\n## Model Deployment\n\nPyTorch offers great flexibility in modeling and a rich surrounding ecosystem. However, while several recent articles have suggested that the use of PyTorch in research and academia may be close to surpassing TensorFlow, there seems to be an overall sense of TensorFlow being the preferred platform for deployment to production. Is this still the case in 2021? Teams looking to serve their PyTorch models in production have a few options.\n\nBefore describing our journey, let's take a brief detour and define the term model.\n\n### Models as computation graphs", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "A few years back, it was still common for machine learning toolkits to support only particular classes of models of a rather fixed and rigid structure, with only a few degrees of freedom (like the kernel of a support vector machine or the number of hidden layers of a neural network). Inspired by foundational work in Theano, toolkits like Microsoft's CNTK or Google's TensorFlow were among the first to popularize a more flexible view on models, as computation graphs with associated parameters that can be estimated from data. This view blurred the boundaries between popular types of models (such as DNNs or SVMs), as it became easy to blend the characteristics of each into your type of graph. Still, such a graph had to be defined upfront before estimating its parameters, and it was pretty static. This made it easy to save models to a self-contained bundle, like a TensorFlow SavedModel (such a bundle simply contains the structure of the graph, as well as the concrete values of the estimated parameters). However, debugging such models can be difficult because the statements in the Python code that build the graph are logically separate from the lines that execute it. Researchers also long for easier ways of expressing dynamic behavior, such as the computation steps of the forward pass of a model being conditionally dependent on its input data (or its previous output).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Most recently, the above limitations have led to a second revolution spearheaded by PyTorch and TensorFlow 2. The computation graph is no longer defined explicitly. Instead, it will be populated implicitly as the Python code executes operations on tensor arguments. An essential technique that powers this development is automatic differentiation. As the computation graph is being built implicitly while executing the steps of the forward pass, all the necessary data will be tracked for later computation of the gradient concerning the model parameters. This allows for great flexibility in training a model, but it raises an important question. If the computation happening inside a model is only implicitly defined through our Python code's steps as it executes concrete data, what is it that we want to save as a model? The answer \u2013 at least initially \u2013 was the Python code with all its dependencies, along with the estimated parameters. This is undesirable for practical reasons. For instance, there is a danger that the team working on model deployment does not exactly reproduce the Python code dependencies used during training, leading to subtly divergent behavior. The solution typically consists of combining two techniques, scripting and tracing, that is, extra annotations in your Python code and execution of your code on exemplary input data, allowing PyTorch to define and save the graph that should be executed during later inference on new, unseen data. This requires some discipline by whoever creates the model code (arguably voiding some of the original flexibility of eager execution), but it results in a self-contained model bundle in TorchScript format. The solution in TensorFlow 2 is remarkably similar.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### Serving our report generation models", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Our journey in deploying the report generation models reflects the above discussion. We started out serving our models by deploying the model code and its dependencies along with the parameter checkpoints in a custom Docker image exposing a gRPC service interface. However, we soon noticed that it became error-prone to replicate the exact code and environment used by the modeling team while estimating the parameters. Moreover, this approach prevented us from leveraging high-performance model serving frameworks like NVIDIA's Triton, which is written in C++ and requires self-contained models that can be used without a Python interpreter. At this stage, we were facing a choice between attempting to export our PyTorch models to ONNX or TorchScript format. ONNX is an open specification for representing machine learning models that increasingly finds adoption. It is powered by a high-performance runtime developed by Microsoft (ONNX Runtime). While we were able to achieve performance acceleration for our TensorFlow BERT-based model using ONNX Runtime, at the time one of our PyTorch model required some operators that weren\u2019t yet supported in ONNX. Rather than implement these using custom operators, we decided to look into TorchScript for the time being.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### A maturing ecosystem\n\nIs it all roses? No, it has been a rockier journey than we expected. We encountered what seems to be a memory leak in the MKL libraries used by PyTorch while serving the PyTorch code directly. We encountered deadlocks in trying to load multiple models from multiple threads. We had difficulties exporting our models to ONNX and TorchScript formats. Models would not work out-of-the-box on hardware with multiple GPUs, they always accessed the particular GPU device on which they were exported. We encountered excessive memory usage in the Triton inference server while serving TorchScript models, which we found out was due to automatic differentiation accidentally being enabled during the forward pass. However, the ecosystem keeps improving, and there is a helpful and vibrant open-source community eager to work with us to mitigate such issues.", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Where to go from here? For those that require the flexibility of serving PyTorch code directly, without going through the extra step of exporting self-contained models, it is worth pointing out that the TorchServe project now provides a way of bundling the code together with parameter checkpoints into a single servable archive, greatly reducing the risk of code and parameters running apart. To us, however, exporting models to TorchScript has proven beneficial. It provides a clear interface between modeling and deployment teams, and TorchScript further reduces the latency when serving models on GPU via its just-in-time compilation engine.\n\n### Scaling at large and the future", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Finally, efficient deployment to the cloud is about more than just computing the response of a single model instance efficiently. Flexibility is needed in managing, versioning and updating models. High-level scalability must be achieved via techniques such as load-balancing, horizontal scaling and vertical scaling. If many models are involved, scale-to-zero quickly becomes a topic as it is unacceptable to pay for serving models that do not answer any requests. Providing such extra functionality on top of a low-level inference server like Triton is the job of an orchestration framework. After gaining some first experience with KubeFlow, to that end, we decided to turn our attention to Azure ML, which provides similar functionality but integrates more deeply with the Azure platform, on which we crucially rely for large parts of our technology stack already. This part of our journey has just begun.\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Academia has long recognized that we are \"standing on the shoulders of giants.\" As Artificial Intelligence is maturing from a scientific discipline into technology, the same spirit of collaboration that originally fueled its scientific foundation has carried over into the world of software engineering. Open-source enthusiasts join technology companies worldwide to build open software ecosystems that allow for new angles at solving some of the most pressing challenges of modern society. In this article, we've taken a look at Nuance's [Dragon Ambient eXperience](http://www.nuance.com/ambient), an AI-powered, voice-enabled solution that automatically documents patient care, reducing healthcare providers' administrative burdens. Nuance DAX improves the patient-provider experience, reduces physician burnout, and improves financial outcomes. It brings back trust, joy, and humanity to the delivery of healthcare. Fairseq and PyTorch have proven to be an incredible platform for powering this AI technology, and in turn, Nuance has contributed back some of its innovations in this space. For further reading, we invite you to take a look at our recent [ACL publication](https://www.aclweb.org/anthology/2020.nlpmc-1.4/) and the [Nuance \"What's Next\" blog](https://whatsnext.nuance.com/rd/using-deep-learning-to-generate-medical-reports/).", "metadata": {"source": "https://pytorch.org/blog/ambient-clinical-intelligence-generating-medical-reports-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Microsoft becomes maintainer of the Windows version of PyTorch'\nauthor: Maxim Lukiyanov - Principal PM at Microsoft, Emad Barsoum - Group EM at Microsoft, Guoliang Hua - Principal EM at Microsoft, Nikita Shulga - Tech Lead at Facebook, Geeta Chauhan - PE Lead at Facebook, Chris Gottbrath - Technical PM at Facebook, Jiachen Pu - Engineer at Facebook\n\n---\n\nAlong with the PyTorch 1.6 release, we are excited to announce that Microsoft has expanded its participation in the PyTorch community and will be responsible for the development and maintenance of the PyTorch build for Windows.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "According to the latest [Stack Overflow developer survey](https://insights.stackoverflow.com/survey/2020#technology-developers-primary-operating-systems), Windows remains the primary operating system for the developer community (46% Windows vs 28% MacOS). [Jiachen Pu](https://github.com/peterjc123) initially made a heroic effort to add support for PyTorch on Windows, but due to limited resources, Windows support for PyTorch has lagged behind other platforms. Lack of test coverage resulted in unexpected issues popping up every now and then. Some of the core tutorials, meant for new users to learn and adopt PyTorch, would fail to run. The installation experience was also not as smooth, with the lack of official PyPI support for PyTorch on Windows. Lastly, some of the PyTorch functionality was simply not available on the Windows platform, such as the TorchAudio domain library and distributed training support. To help alleviate this pain, Microsoft is happy to bring its Windows expertise to the table and bring PyTorch on Windows to its best possible self.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In the PyTorch 1.6 release, we have improved the core quality of the Windows build by bringing test coverage up to par with Linux for core PyTorch and its domain libraries and by automating tutorial testing. Thanks to the broader PyTorch community, which contributed TorchAudio support to Windows, we were able to add test coverage to all three domain libraries: TorchVision, TorchText and TorchAudio. In subsequent releases of PyTorch, we will continue improving the Windows experience based on community feedback and requests. So far, the feedback we received from the community points to distributed training support and a better installation experience using pip as the next areas of improvement.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In addition to the native Windows experience, Microsoft released a preview adding [GPU compute support to Windows Subsystem for Linux (WSL) 2](https://blogs.windows.com/windowsdeveloper/2020/06/17/gpu-accelerated-ml-training-inside-the-windows-subsystem-for-linux/) distros, with a focus on enabling AI and ML developer workflows. WSL is designed for developers that want to run any Linux based tools directly on Windows. This preview enables valuable scenarios for a variety of frameworks and Python packages that utilize [NVIDIA CUDA](https://developer.nvidia.com/cuda/wsl) for acceleration and only support Linux. This means WSL customers using the preview can run native Linux based PyTorch applications on Windows unmodified without the need for a traditional virtual machine or a dual boot setup.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Getting started with PyTorch on Windows\nIt's easy to get started with PyTorch on Windows. To install PyTorch using Anaconda with the latest GPU support, run the command below. To install different supported configurations of PyTorch, refer to the installation instructions on [pytorch.org](https://pytorch.org).\n\n`conda install pytorch torchvision cudatoolkit=10.2 -c pytorch`\n\nOnce you install PyTorch, learn more by visiting the [PyTorch Tutorials](https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html) and [documentation](https://pytorch.org/docs/stable/index.html).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Getting started with PyTorch on Windows Subsystem for Linux\nThe [preview of NVIDIA CUDA support in WSL](https://docs.microsoft.com/en-us/windows/win32/direct3d12/gpu-cuda-in-wsl) is now available to Windows Insiders running Build 20150 or higher. In WSL, the command to install PyTorch using Anaconda is the same as the above command for native Windows. If you prefer pip, use the command below.\n\n`pip install torch torchvision`\n\nYou can use the same tutorials and documentation inside your WSL environment as on native Windows. This functionality is still in preview so if you run into issues with WSL please share feedback via the [WSL GitHub repo](https://github.com/microsoft/WSL) or with NVIDIA CUDA support share via NVIDIA\u2019s [Community Forum for CUDA on WSL](https://forums.developer.nvidia.com/c/accelerated-computing/cuda/cuda-on-windows-subsystem-for-linux/303).", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Feedback\nIf you find gaps in the PyTorch experience on Windows, please let us know on the [PyTorch discussion forum](https://discuss.pytorch.org/c/windows/26) or file an issue on [GitHub](https://github.com/pytorch/pytorch) using the #module: windows label.", "metadata": {"source": "https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated PyTorch 2 Transformers\"\nauthor: Michael Gschwind, Driss Guessous, Christian Puhrsch\n---\n\nThe PyTorch 2.0 release includes a new high-performance implementation of the PyTorch Transformer API with the goal of making training and deployment of state-of-the-art Transformer models affordable. Following the successful release of \u201cfastpath\u201d inference execution (\u201cBetter Transformer\u201d), this release introduces high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA).", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "You can take advantage of the new fused SDPA kernels either by calling the new SDPA operator directly (as described in the [SDPA tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html#beta-implementing-high-performance-transformers-with-scaled-dot-product-attention-sdpa)), or transparently via integration into the pre-existing PyTorch Transformer API. All features of the PyTorch Transformer API will continue to work compatibly, with many features mapped to high-performance SDPA kernels, while other features are impossible to support with higher performance (e.g., need_weights, as per below) while expanded high-performance support for other features may still be under active development. \\\n \\\nSimilar to the \u201cfastpath\u201d architecture, custom kernels are fully integrated into the PyTorch Transformer API \u2013 thus, using the native Transformer and MultiHeadAttention API will enable users to transparently see significant speed improvements. Unlike the \u201cfastpath\u201d architecture, the newly introduced \u201ccustom kernels\u201d support many more use cases including models using Cross-Attention, Transformer Decoders, and for training models, in addition to the existing fastpath inference for fixed and variable sequence length Transformer Encoder and Self Attention use cases.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "To take full advantage of different hardware models and Transformer use cases, multiple SDPA custom kernels are supported, with custom kernel selection logic that will pick the highest-performance kernel for a given model and hardware type. In particular, the first custom kernels included with the PyTorch 2.0 release are the [Flash Attention](https://arxiv.org/abs/2205.14135) kernel (sdpa_flash, for 16-bit floating point training and inference on Nvidia GPUs with SM80+ architecture level) and the [xFormers memory-efficient attention](https://github.com/facebookresearch/xformers) kernel (sdpa_mem_eff, for 16-bit and 32-bit floating point training and inference on a broad range of Nvidia GPUs). A general-purpose kernel sdpa_math provides an implementation when the custom kernels are not applicable.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "As mentioned, custom kernels provide a wider range of support for execution scenarios To ensure efficient execution (e,g., to use GPU tensor cores), model configurations need to meet a small number of requirements. This list of requirements will evolve over time, prospectively relaxing constraints limiting the usage of currently supported custom kernels, or providing additional kernels in the future.\n\nFor the most up to date list of custom kernels and dispatch constraints, you can refer to [sdp_utils.h](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/transformers/cuda/sdp_utils.h). As of PyTorch 2.0, the existing fused SDPA kernels have the following constraints:", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "* Flash Attention only supports 16 bit floating point data types (float16 and bfloat16).\n* The head dimension must be a multiple of 8 for 16-bit floating point numbers and a multiple of 4 for 32-bit floating point numbers. At present, the maximum head_dim support for the Flash Attention custom kernel is 128.\n* The CUDA architecture level must be sm5x or better for the mem_efficient kernel, and sm80 for Flash Attention.\n* Flash Attention supports arbitrary dropout, in PyTorch 2.0 the mem_efficient kernel does not support dropout (i.e., dropout must be set to zero for this kernel to be selected in PyTorch 2.0). \n* To support variable-sequence length batches, all SDPA kernels support Nested Tensor inputs that combine input data and padding information using variable sequence length tensors for forward. (You can find more information about Nested Tensors in the [Nested Tensor tutorial](https://pytorch.org/tutorials/prototype/nestedtensor.html).)\n* You can specify both a _key_padding_mask_ and an _attn_mask_ by combining them before passing them to the SDPA operator. In particular, you can use the per-batch-element key padding mask of the nn.Transformer API to implement training for variable-sequence length inputs in a batch.\n* At present, the only attention mask supported by fused kernel implementation is the causal mask commonly used for training. To specify the causal mask in custom kernels, it must be specified with the _is_causal_ boolean and _attn_mask_ must be None. \n* Support for Nested Tensors is still under development. Specifically, in PyTorch 2.0, only the sdpa_math kernel supports training with Nested Tensors. Also, PyTorch 2.0 does not support Nested Tensors as part of code being compiled with torch.compile(). \n* The SDPA operator does not support returning averaged attention weights because computing them defeats the optimizations that enabled fused kernels to execute more efficiently. The argument _need_weights_ for torch.nn.MultiheadAttention's forward function defaults to True. In order to use the fused kernels, _need_weights_ needs to be set to _need_weights=False_.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "We find that an attention mask is rarely used in real-world applications, except for the causal mask during training. Consequently, we reduce kernel complexity and compute cost by building in the option to use a causal mask as attention mask, and select this new capability with the _is_causal_ parameter introduced in conjunction with the new SDPA operator. \n\nProviding the _is_causal_ Boolean flag for the frequently used causal mask also obviates the expensive and memory-intensive allocation of a causal mask, increasing training memory efficiency by allowing more memory to be used for large batch sizes, and reduce memory bandwidth and cache contention \u2013 which are both at a premium in GPU accelerators \u2013 by not needing to load an attention mask tensor.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "If the constraints of none of the available custom kernels are met, then training falls back to using the default sdpa_math kernel, implementing the mathematical equations for scaled dot product attention using a sequence of PyTorch operator to implement SDPA. This is the most general \u201ccatch-all\u201d fallback kernel to ensure successful training for all models.\n\nIn addition to the existing Transformer API, model developers may also use the scaled dot product attention kernels directly by calling the new `scaled_dot_product_attention()` operator. This operator may be used to efficiently implement multi-head attention by combining it with in-projection and outprojection, as described in the [SDPA tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "In addition to adding custom kernels, Accelerated PyTorch 2 Transformers are integrated with PyTorch 2.0 compilation. To use your model while benefiting from the additional acceleration of PT2-compilation (for inference or training), pre-process the model with\n\n\n```\nmodel = torch.compile(model)\n```\n\n\nWe have achieved major speedups for training transformer models and in particular large language models with Accelerated PyTorch 2 Transformers using a combination of custom kernels and torch.compile(). \n\n\n![Better Transformer chart](/assets/images/pytorch_better_transformer_chart1.png){:width=\"100%\"}\nFigure: Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "Finally, because the custom kernels are much more memory efficient, try to increase the size of training batches to achieve faster training with increased batch size.\n\nIn addition to automatic kernel selection, a context manager enables developers to override the kernel selection algorithm \u2013 this is not required for day to day operation, but enables developers to debug their code as well as enable performance engineers to override kernel selection. The SDPA tutorial provides additional information on using the SDPA context manager.\n\nIn addition to availability as part of the nn.Transformer API, Accelerated PyTorch 2 Transformer custom kernels are also available in conjunction with the torchtext, torchvision, and fairseq domain libraries with the launch of PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/accelerated-pytorch-2/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Mapillary Research: Seamless Scene Segmentation and In-Place Activated BatchNorm'\nauthor: Lorenzo Porzi, Mapillary\nredirect_from: /2019/07/23/mapillary-research.html\n---\n\nWith roads in developed countries like the US changing up to 15% annually, Mapillary addresses a growing demand for keeping maps updated by combining images from any camera into a 3D visualization of the world. Mapillary's independent and collaborative approach enables anyone to collect, share, and use street-level images for improving maps, developing cities, and advancing the automotive industry.\n\nToday, people and organizations all over the world have contributed more than 600 million images toward Mapillary's mission of helping people understand the world's places through images and making this data available, with clients and partners including the World Bank, HERE, and Toyota Research Institute.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Mapillary\u2019s computer vision technology brings intelligence to maps in an unprecedented way, increasing our overall understanding of the world. [Mapillary](https://www.mapillary.com/) runs state-of-the-art semantic image analysis and image-based 3d modeling at scale and on all its images. In this post we discuss two recent works from Mapillary Research and their implementations in PyTorch - Seamless Scene Segmentation [1] and In-Place Activated BatchNorm [2] - generating Panoptic segmentation results and saving up to 50% of GPU memory during training, respectively.\n\n## Seamless Scene Segmentation\n\n_Github project page: [https://github.com/mapillary/seamseg/](https://github.com/mapillary/seamseg/)_\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "The objective of Seamless Scene Segmentation is to predict a \u201cpanoptic\u201d segmentation [3] from an image, that is a complete labeling where each pixel is assigned with a class id and, where possible, an instance id. Like many modern CNNs dealing with instance detection and segmentation, we adopt the Mask R-CNN framework [4], using ResNet50 + FPN [5] as a backbone. This architecture works in two stages: first, the \u201cProposal Head\u201d selects a set of candidate bounding boxes on the image (i.e. the proposals) that could contain an object; then, the \u201cMask Head\u201d focuses on each proposal, predicting its class and segmentation mask. The output of this process is a \u201csparse\u201d instance segmentation, covering only the parts of the image that contain countable objects (e.g. cars and pedestrians).", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "To complete our panoptic approach coined Seamless Scene Segmentation, we add a third stage to Mask R-CNN. Stemming from the same backbone, the \u201cSemantic Head\u201d predicts a dense semantic segmentation over the whole image, also accounting for the uncountable or amorphous classes (e.g. road and sky). The outputs of the Mask and Semantic heads are finally fused using a simple non-maximum suppression algorithm to generate the final panoptic prediction. All details about the actual network architecture, used losses and underlying math can be found at the [project website](https://research.mapillary.com/publication/cvpr19a) for our CVPR 2019 paper [1].", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "While several versions of Mask R-CNN are publicly available, including an [official implementation](https://github.com/facebookresearch/Detectron) written in Caffe2, at Mapillary we decided to build Seamless Scene Segmentation from scratch using PyTorch, in order to have full control and understanding of the whole pipeline. While doing so we encountered a couple of main stumbling blocks, and had to come up with some creative workarounds we are going to describe next.\n\n## Dealing with variable-sized tensors", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Something that sets aside panoptic segmentation networks from traditional CNNs is the prevalence of variable-sized data. In fact, many of the quantities we are dealing with cannot be easily represented with fixed sized tensors: each image contains a different number of objects, the Proposal head can produce a different number of proposals for each image, and the images themselves can have different sizes. While this is not a problem per-se -- one could just process images one at a time -- we would still like to exploit batch-level parallelism as much as possible. Furthermore, when performing distributed training with multiple GPUs, `DistributedDataParallel` expects its inputs to be batched, uniformly-sized tensors.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Our solution to these issues is to wrap each batch of variable-sized tensors in a `PackedSequence`. `PackedSequence` is little more than a glorified list class for tensors, tagging its contents as \u201crelated\u201d, ensuring that they all share the same type, and providing useful methods like moving all the tensors to a particular device, etc. When performing light-weight operations that wouldn\u2019t be much faster with batch-level parallelism, we simply iterate over the contents of the `PackedSequence` in a for loop. When performance is crucial, e.g. in the body of the network, we simply concatenate the contents of the PackedSequence, adding zero padding as required (like in RNNs with variable-length inputs), and keeping track of the original dimensions of each tensor.\n\n`PackedSequence`s also help us deal with the second problem highlighted above. We slightly modify `DistributedDataParallel` to recognize `PackedSequence` inputs, splitting them in equally sized chunks and distributing their contents across the GPUs.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "## Asymmetric computational graphs with Distributed Data Parallel\n\nAnother, perhaps more subtle, peculiarity of our network is that it can generate asymmetric computational graphs across GPUs. In fact, some of the modules that compose the network are \u201coptional\u201d, in the sense that they are not always computed for all images. As an example, when the Proposal head doesn\u2019t output any proposal, the Mask head is not traversed at all. If we are training on multiple GPUs with `DistributedDataParallel`, this results in one of the replicas not computing gradients for the Mask head parameters.\n\nPrior to PyTorch 1.1, this resulted in a crash, so we had to develop a workaround. Our simple but effective solution was to compute a \u201cfake forward pass\u201d when no actual forward is required, i.e. something like this:\n\n```python\ndef fake_forward():\n fake_input = get_correctly_shaped_fake_input()\n fake_output = mask_head(fake_input)\n fake_loss = fake_output.sum() * 0\n return fake_loss\n```", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Here, we generate a batch of bogus data, pass it through the Mask head, and return a loss that always back-progates zeros to all parameters.\n\nStarting from PyTorch 1.1 this workaround is no longer required: by setting `find_unused_parameters=True` in the constructor, `DistributedDataParallel` is told to identify parameters whose gradients have not been computed by all replicas and correctly handle them. This leads to some substantial simplifications in our code base!\n\n## In-place Activated BatchNorm\n\n_Github project page: [https://github.com/mapillary/inplace_abn/](https://github.com/mapillary/inplace_abn/)_", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Most researchers would probably agree that there are always constraints in terms of available GPU resources, regardless if their research lab has access to only a few or multiple thousands of GPUs. In a time where at Mapillary we still worked at rather few and mostly 12GB Titan X - style prosumer GPUs, we were searching for a solution that virtually enhances the usable memory during training, so we would be able to obtain and push state-of-the-art results on dense labeling tasks like semantic segmentation. In-place activated BatchNorm is enabling us to use up to 50% more memory (at little computational overhead) and is therefore deeply integrated in all our current projects (including Seamless Scene Segmentation described above).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "When processing a BN-Activation-Convolution sequence in the forward pass, most deep learning frameworks (including PyTorch) need to store two big buffers, i.e. the input x of BN and the input z of Conv. This is necessary because the standard implementations of the backward passes of BN and Conv depend on their inputs to calculate the gradients. Using InPlace-ABN to replace the BN-Activation sequence, we can safely discard x, thus saving up to 50% GPU memory at training time. To achieve this, we rewrite the backward pass of BN in terms of its output y, which is in turn reconstructed from z by inverting the activation function.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "The only limitation of InPlace-ABN is that it requires using an invertible activation function, such as leaky relu or elu. Except for this, it can be used as a direct, drop-in replacement for BN+activation modules in any network. Our native CUDA implementation offers minimal computational overhead compared to PyTorch\u2019s standard BN, and is available for anyone to use from here: [https://github.com/mapillary/inplace_abn/](https://github.com/mapillary/inplace_abn/).\n\n## Synchronized BN with asymmetric graphs and unbalanced batches", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "When training networks with synchronized SGD over multiple GPUs and/or multiple nodes, it\u2019s common practice to compute BatchNorm statistics separately on each device. However, in our experience working with semantic and panoptic segmentation networks, we found that accumulating mean and variance across all workers can bring a substantial boost in accuracy. This is particularly true when dealing with small batches, like in Seamless Scene Segmentation where we train with a single, super-high resolution image per GPU.\n\nInPlace-ABN supports synchronized operation over multiple GPUs and multiple nodes, and, since version 1.1, this can also be achieved in the standard PyTorch library using [SyncBatchNorm](https://pytorch.org/docs/stable/nn.html#syncbatchnorm). Compared to SyncBatchNorm, however, we support some additional functionality which is particularly important for Seamless Scene Segmentation: unbalanced batches and asymmetric graphs.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "As mentioned before, Mask R-CNN-like networks naturally give rise to variable-sized tensors. Thus, in InPlace-ABN we calculate synchronized statistics using a variant of the parallel algorithm described [here](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm), which properly takes into account the fact that each GPU can hold a different number of samples. PyTorch\u2019s SyncBatchNorm is currently being revised to support this, and the improved functionality will be available in a future release.", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "Asymmetric graphs (in the sense mentioned above) are another complicating factor one has to deal with when creating a synchronized BatchNorm implementation. Luckily, PyTorch\u2019s distributed group functionality allows us to restrict distributed communication to a subset of workers, easily excluding those that are currently inactive. The only missing piece is that, in order to create a distributed group, each process needs to know the ids of all processes that will participate in the group, and even processes that are not part of the group need to call the `new_group()` function. In InPlace-ABN we handle it with a function like this:\n\n```python\nimport torch\nimport torch.distributed as distributed\n\ndef active_group(active):\n \"\"\"Initialize a distributed group where each process can independently decide whether to participate or not\"\"\"\n world_size = distributed.get_world_size()\n rank = distributed.get_rank()", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "# Gather active status from all workers\n active = torch.tensor(rank if active else -1, dtype=torch.long, device=torch.cuda.current_device())\n active_workers = torch.empty(world_size, dtype=torch.long, device=torch.cuda.current_device())\n distributed.all_gather(list(active_workers.unbind(0)), active)\n\n # Create group\n active_workers = [int(i) for i in active_workers.tolist() if i != -1]\n group = distributed.new_group(active_workers)\n return group\n```\n\nFirst each process, including inactive ones, communicates its status to all others through an `all_gather` call, then it creates the distributed group with the shared information. In the actual implementation we also include a caching mechanism for groups, since `new_group()` is usually too expensive to call at each batch.\n\n## References\n\n[1] Seamless Scene Segmentation; Lorenzo Porzi, Samuel Rota Bul\u00f2, Aleksander Colovic, Peter Kontschieder; Computer Vision and Pattern Recognition (CVPR), 2019", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} +{"page_content": "[2] In-place Activated BatchNorm for Memory-Optimized Training of DNNs; Samuel Rota Bul\u00f2, Lorenzo Porzi, Peter Kontschieder; Computer Vision and Pattern Recognition (CVPR), 2018\n\n[3] Panoptic Segmentation; Alexander Kirillov, Kaiming He, Ross Girshick, Carsten Rother, Piotr Dollar; Computer Vision and Pattern Recognition (CVPR), 2019\n\n[4] Mask R-CNN; Kaiming He, Georgia Gkioxari, Piotr Dollar, Ross Girshick; International Conference on Computer Vision (ICCV), 2017\n\n[5] Feature Pyramid Networks for Object Detection; Tsung-Yi Lin, Piotr Dollar, Ross Girshick, Kaiming He, Bharath Hariharan, Serge Belongie; Computer Vision and Pattern Recognition (CVPR), 2017", "metadata": {"source": "https://pytorch.org/blog/mapillary-research/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'Introduction to Quantization on PyTorch'\nauthor: Raghuraman Krishnamoorthi, James Reed, Min Ni, Chris Gottbrath, and Seth Weidman\n---\n\nIt\u2019s important to make efficient use of both server-side and on-device compute resources when developing machine learning applications. To support more efficient deployment on servers and edge devices, PyTorch added a support for model quantization using the familiar eager mode Python API.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Quantization leverages 8bit integer (int8) instructions to reduce the model size and run the inference faster (reduced latency) and can be the difference between a model achieving quality of service goals or even fitting into the resources available on a mobile device. Even when resources aren\u2019t quite so constrained it may enable you to deploy a larger and more accurate model. Quantization is available in PyTorch starting in version 1.3 and with the release of PyTorch 1.4 we published quantized models for", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "ResNet, ResNext, MobileNetV2, GoogleNet, InceptionV3 and ShuffleNetV2 in the PyTorch torchvision 0.5 library.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This blog post provides an overview of the quantization support on PyTorch and its incorporation with the TorchVision domain library.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**What is Quantization?**\n\nQuantization refers to techniques for doing both computations and memory accesses with lower precision data, usually int8 compared to floating point implementations. This enables performance gains in several important areas:\n* 4x reduction in model size;\n* 2-4x reduction in memory bandwidth;\n* 2-4x faster inference due to savings in memory bandwidth and faster compute with int8 arithmetic (the exact speed up varies depending on the hardware, the runtime, and the model).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Quantization does not however come without additional cost. Fundamentally quantization means introducing approximations and the resulting networks have slightly less accuracy. These techniques attempt to minimize the gap between the full floating point accuracy and the quantized accuracy.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We designed quantization to fit into the PyTorch framework. The means that:\n1. PyTorch has data types corresponding to [quantized tensors](https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor), which share many of the features of tensors.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. One can write kernels with quantized tensors, much like kernels for floating point tensors to customize their implementation. PyTorch supports quantized modules for common operations as part of the `torch.nn.quantized` and `torch.nn.quantized.dynamic` name-space.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. Quantization is compatible with the rest of PyTorch: quantized models are traceable and scriptable. The quantization method is virtually identical for both server and mobile backends. One can easily mix quantized and floating point operations in a model.\n4. Mapping of floating point tensors to quantized tensors is customizable with user defined observer/fake-quantization blocks. PyTorch provides default implementations that should work for most use cases.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nWe developed three techniques for quantizing neural networks in PyTorch as part of quantization tooling in the `torch.quantization` name-space.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**The Three Modes of Quantization Supported in PyTorch starting version 1.3**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. ### **Dynamic Quantization**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The easiest method of quantization PyTorch supports is called **dynamic quantization**. This involves not just converting the weights to int8 - as happens in all quantization variants - but also converting the activations to int8 on the fly, just before doing the computation (hence \u201cdynamic\u201d). The computations will thus be performed using efficient int8 matrix multiplication and convolution implementations, resulting in faster compute. However, the activations are read and written to memory in floating", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "point format.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* **PyTorch API**: we have a simple API for dynamic quantization in PyTorch. `torch.quantization.quantize_dynamic` takes in a model, as well as a couple other arguments, and produces a quantized model! Our [end-to-end tutorial](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html) illustrates this for a BERT model; while the tutorial is long and contains sections on loading pre-trained models and other concepts unrelated to quantization, the part the quantizes the BERT model", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "is simply:", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\n import torch.quantization\n quantized_model = torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=torch.qint8)\n ```\n * See the documentation for the function [here](https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic) an end-to-end example in our tutorials [here](https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html) and [here](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. ### **Post-Training Static Quantization**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "One can further improve the performance (latency) by converting networks to use both integer arithmetic and int8 memory accesses. Static quantization performs the additional step of first feeding batches of data through the network and computing the resulting distributions of the different activations (specifically, this is done by inserting \u201cobserver\u201d modules at different points that record these distributions). This information is used to determine how specifically the different activations should be", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "quantized at inference time (a simple technique would be to simply divide the entire range of activations into 256 levels, but we support more sophisticated methods as well). Importantly, this additional step allows us to pass quantized values between operations instead of converting these values to floats - and then back to ints - between every operation, resulting in a significant speed-up.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "With this release, we\u2019re supporting several features that allow users to optimize their static quantization:\n 1. Observers: you can customize observer modules which specify how statistics are collected prior to quantization to try out more advanced methods to quantize your data.\n 2. Operator fusion: you can fuse multiple operations into a single operation, saving on memory access while also improving the operation\u2019s numerical accuracy.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. Per-channel quantization: we can independently quantize weights for each output channel in a convolution/linear layer, which can lead to higher accuracy with almost the same speed.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* ### **PyTorch API**:\n * To fuse modules, we have `torch.quantization.fuse_modules`\n * Observers are inserted using `torch.quantization.prepare`\n * Finally, quantization itself is done using `torch.quantization.convert`", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We have a tutorial with an end-to-end example of quantization (this same tutorial also covers our third quantization method, quantization-aware training), but because of our simple API, the three lines that perform post-training static quantization on the pre-trained model `myModel` are:\n ```python\n # set quantization config for server (x86)\n deploymentmyModel.qconfig = torch.quantization.get_default_config('fbgemm')", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# insert observers\n torch.quantization.prepare(myModel, inplace=True)\n # Calibrate the model and collect statistics\n\n # convert to quantized version\n torch.quantization.convert(myModel, inplace=True)", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. ### **Quantization Aware Training**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Quantization-aware training(QAT)** is the third method, and the one that typically results in highest accuracy of these three. With QAT, all weights and activations are \u201cfake quantized\u201d during both the forward and backward passes of training: that is, float values are rounded to mimic int8 values, but all computations are still done with floating point numbers. Thus, all the weight adjustments during training are made while \u201caware\u201d of the fact that the model will ultimately be quantized; after", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "quantizing, therefore, this method usually yields higher accuracy than the other two methods.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "* ### **PyTorch API**:\n * `torch.quantization.prepare_qat` inserts fake quantization modules to model quantization.\n * Mimicking the static quantization API, `torch.quantization.convert` actually quantizes the model once training is complete.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For example, in [the end-to-end example](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html), we load in a pre-trained model as `qat_model`, then we simply perform quantization-aware training using:\n\n ```python\n # specify quantization config for QAT\n qat_model.qconfig=torch.quantization.get_default_qat_qconfig('fbgemm')\n\n # prepare QAT\n torch.quantization.prepare_qat(qat_model, inplace=True)", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# convert to quantized version, removing dropout, to check for accuracy on each\n epochquantized_model=torch.quantization.convert(qat_model.eval(), inplace=False)\n ```", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Device and Operator Support**\nQuantization support is restricted to a subset of available operators, depending on the method being used, for a list of supported operators, please see the documentation at [https://pytorch.org/docs/stable/quantization.html](https://pytorch.org/docs/stable/quantization.html).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The set of available operators and the quantization numerics also depend on the backend being used to run quantized models. Currently quantized operators are supported only for CPU inference in the following backends: x86 and ARM. Both the quantization configuration (how tensors should be quantized and the quantized kernels (arithmetic with quantized tensors) are backend dependent. One can specify the backend by doing:", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torchbackend='fbgemm'\n# 'fbgemm' for server, 'qnnpack' for mobile\nmy_model.qconfig = torch.quantization.get_default_qconfig(backend)\n# prepare and convert model\n# Set the backend on which the quantized kernels need to be run\ntorch.backends.quantized.engine=backend", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Quantization leverages 8bit integer (int8) instructions to reduce the model size and run the inference faster (reduced latency) and can be the difference between a model achieving quality of service goals or even fitting into the resources available on a mobile device. Even when resources aren\u2019t quite so constrained it may enable you to deploy a larger and more accurate model. Quantization is available in PyTorch starting in version 1.3 and with the release of PyTorch 1.4 we published quantized models for ResNet, ResNext, MobileNetV2, GoogleNet, InceptionV3 and ShuffleNetV2 in the PyTorch torchvision 0.5 library.\n\nThis blog post provides an overview of the quantization support on PyTorch and its incorporation with the TorchVision domain library.\n\n## **What is Quantization?**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Quantization refers to techniques for doing both computations and memory accesses with lower precision data, usually int8 compared to floating point implementations. This enables performance gains in several important areas:\n* 4x reduction in model size;\n* 2-4x reduction in memory bandwidth;\n* 2-4x faster inference due to savings in memory bandwidth and faster compute with int8 arithmetic (the exact speed up varies depending on the hardware, the runtime, and the model).\n\nQuantization does not however come without additional cost. Fundamentally quantization means introducing approximations and the resulting networks have slightly less accuracy. These techniques attempt to minimize the gap between the full floating point accuracy and the quantized accuracy.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We designed quantization to fit into the PyTorch framework. The means that:\n1. PyTorch has data types corresponding to [quantized tensors](https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor), which share many of the features of tensors.\n2. One can write kernels with quantized tensors, much like kernels for floating point tensors to customize their implementation. PyTorch supports quantized modules for common operations as part of the `torch.nn.quantized` and `torch.nn.quantized.dynamic` name-space.\n3. Quantization is compatible with the rest of PyTorch: quantized models are traceable and scriptable. The quantization method is virtually identical for both server and mobile backends. One can easily mix quantized and floating point operations in a model.\n4. Mapping of floating point tensors to quantized tensors is customizable with user defined observer/fake-quantization blocks. PyTorch provides default implementations that should work for most use cases.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\nWe developed three techniques for quantizing neural networks in PyTorch as part of quantization tooling in the `torch.quantization` name-space.\n\n## **The Three Modes of Quantization Supported in PyTorch starting version 1.3**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "1. ### **Dynamic Quantization**\n The easiest method of quantization PyTorch supports is called **dynamic quantization**. This involves not just converting the weights to int8 - as happens in all quantization variants - but also converting the activations to int8 on the fly, just before doing the computation (hence \u201cdynamic\u201d). The computations will thus be performed using efficient int8 matrix multiplication and convolution implementations, resulting in faster compute. However, the activations are read and written to memory in floating point format.\n * **PyTorch API**: we have a simple API for dynamic quantization in PyTorch. `torch.quantization.quantize_dynamic` takes in a model, as well as a couple other arguments, and produces a quantized model! Our [end-to-end tutorial](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html) illustrates this for a BERT model; while the tutorial is long and contains sections on loading pre-trained models and other concepts unrelated to quantization, the part the quantizes the BERT model is simply:", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\n import torch.quantization\n quantized_model = torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=torch.qint8)\n ```\n * See the documentation for the function [here](https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic) an end-to-end example in our tutorials [here](https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html) and [here](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html).\n\n2. ### **Post-Training Static Quantization**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "One can further improve the performance (latency) by converting networks to use both integer arithmetic and int8 memory accesses. Static quantization performs the additional step of first feeding batches of data through the network and computing the resulting distributions of the different activations (specifically, this is done by inserting \u201cobserver\u201d modules at different points that record these distributions). This information is used to determine how specifically the different activations should be quantized at inference time (a simple technique would be to simply divide the entire range of activations into 256 levels, but we support more sophisticated methods as well). Importantly, this additional step allows us to pass quantized values between operations instead of converting these values to floats - and then back to ints - between every operation, resulting in a significant speed-up.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "With this release, we\u2019re supporting several features that allow users to optimize their static quantization:\n 1. Observers: you can customize observer modules which specify how statistics are collected prior to quantization to try out more advanced methods to quantize your data.\n 2. Operator fusion: you can fuse multiple operations into a single operation, saving on memory access while also improving the operation\u2019s numerical accuracy.\n 3. Per-channel quantization: we can independently quantize weights for each output channel in a convolution/linear layer, which can lead to higher accuracy with almost the same speed.\n\n * ### **PyTorch API**:\n * To fuse modules, we have `torch.quantization.fuse_modules`\n * Observers are inserted using `torch.quantization.prepare`\n * Finally, quantization itself is done using `torch.quantization.convert`", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We have a tutorial with an end-to-end example of quantization (this same tutorial also covers our third quantization method, quantization-aware training), but because of our simple API, the three lines that perform post-training static quantization on the pre-trained model `myModel` are:\n ```python\n # set quantization config for server (x86)\n deploymentmyModel.qconfig = torch.quantization.get_default_config('fbgemm')\n\n # insert observers\n torch.quantization.prepare(myModel, inplace=True)\n # Calibrate the model and collect statistics\n\n # convert to quantized version\n torch.quantization.convert(myModel, inplace=True)\n ```", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "3. ### **Quantization Aware Training**\n **Quantization-aware training(QAT)** is the third method, and the one that typically results in highest accuracy of these three. With QAT, all weights and activations are \u201cfake quantized\u201d during both the forward and backward passes of training: that is, float values are rounded to mimic int8 values, but all computations are still done with floating point numbers. Thus, all the weight adjustments during training are made while \u201caware\u201d of the fact that the model will ultimately be quantized; after quantizing, therefore, this method usually yields higher accuracy than the other two methods.\n* ### **PyTorch API**:\n * `torch.quantization.prepare_qat` inserts fake quantization modules to model quantization.\n * Mimicking the static quantization API, `torch.quantization.convert` actually quantizes the model once training is complete.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "For example, in [the end-to-end example](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html), we load in a pre-trained model as `qat_model`, then we simply perform quantization-aware training using:\n\n ```python\n # specify quantization config for QAT\n qat_model.qconfig=torch.quantization.get_default_qat_qconfig('fbgemm')\n\n # prepare QAT\n torch.quantization.prepare_qat(qat_model, inplace=True)\n\n # convert to quantized version, removing dropout, to check for accuracy on each\n epochquantized_model=torch.quantization.convert(qat_model.eval(), inplace=False)\n ```\n\n### **Device and Operator Support**\nQuantization support is restricted to a subset of available operators, depending on the method being used, for a list of supported operators, please see the documentation at [https://pytorch.org/docs/stable/quantization.html](https://pytorch.org/docs/stable/quantization.html).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The set of available operators and the quantization numerics also depend on the backend being used to run quantized models. Currently quantized operators are supported only for CPU inference in the following backends: x86 and ARM. Both the quantization configuration (how tensors should be quantized and the quantized kernels (arithmetic with quantized tensors) are backend dependent. One can specify the backend by doing:\n\n```python\nimport torchbackend='fbgemm'\n# 'fbgemm' for server, 'qnnpack' for mobile\nmy_model.qconfig = torch.quantization.get_default_qconfig(backend)\n# prepare and convert model\n# Set the backend on which the quantized kernels need to be run\ntorch.backends.quantized.engine=backend\n```", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} {"page_content": "However, quantization aware training occurs in full floating point and can run on either GPU or CPU. Quantization aware training is typically only used in CNN models when post training static or dynamic quantization doesn\u2019t yield sufficient accuracy. This can occur with models that are highly optimized to achieve small size (such as Mobilenet).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Integration in torchvision**\nWe\u2019ve also enabled quantization for some of the most popular models in [torchvision](https://github.com/pytorch/vision/tree/master/torchvision/models/quantization): Googlenet, Inception, Resnet, ResNeXt, Mobilenet and Shufflenet. We have upstreamed these changes to torchvision in three forms:\n1. Pre-trained quantized weights so that you can use them right away.\n2. Quantization ready model definitions so that you can do post-training quantization or quantization aware training.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. A script for doing quantization aware training \u2014 which is available for any of these model though, as you will learn below, we only found it necessary for achieving accuracy with Mobilenet.\n4. We also have a [tutorial](https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html) showing how you can do transfer learning with quantization using one of the torchvision models.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Choosing an approach**\nThe choice of which scheme to use depends on multiple factors:\n1. Model/Target requirements: Some models might be sensitive to quantization, requiring quantization aware training.\n2. Operator/Backend support: Some backends require fully quantized operators.\n\nCurrently, operator coverage is limited and may restrict the choices listed in the table below:\nThe table below provides a guideline.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n
Model TypePreferred schemeWhy
LSTM/RNNDynamic QuantizationThroughput dominated by compute/memory bandwidth for weights
BERT/TransformerDynamic QuantizationThroughput dominated by compute/memory bandwidth for weights
CNNStatic QuantizationThroughput limited by memory bandwidth for activations
CNNQuantization Aware TrainingIn the case where accuracy can't be achieved with static quantization
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Performance Results**\nQuantization provides a 4x reduction in the model size and a speedup of 2x to 3x compared to floating point implementations depending on the hardware platform and the model being benchmarked. Some sample results are:", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n
ModelFloat Latency (ms)Quantized Latency (ms)Inference Performance GainDeviceNotes
BERT5813131.8xXeon-D2191 (1.6GHz)Batch size = 1, Maximum sequence length= 128, Single thread, x86-64, Dynamic quantization
Resnet-502141032xXeon-D2191 (1.6GHz)Single thread, x86-64, Static quantization
Mobilenet-v297175.7xSamsung S9Static quantization, Floating point numbers are based on Caffe2 run-time and are not optimized
\n
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Accuracy results**\nWe also compared the accuracy of static quantized models with the floating point models on Imagenet. For dynamic quantization, we [compared](https://github.com/huggingface/transformers/blob/master/examples/run_glue.py) the F1 score of BERT on the GLUE benchmark for MRPC.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Computer Vision Model accuracy**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n
ModelTop-1 Accuracy (Float)Top-1 Accuracy (Quantized)Quantization scheme
Googlenet69.869.7Static post training quantization
Inception-v377.577.1Static post training quantization
ResNet-1869.869.4Static post training quantization
Resnet-5076.175.9Static post training quantization
ResNext-101 32x8d79.379Static post training quantization
Mobilenet-v271.971.6Quantization Aware Training
Shufflenet-v269.468.4Static post training quantization
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Speech and NLP Model accuracy**\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n
ModelF1 (GLUEMRPC) FloatF1 (GLUEMRPC) QuantizedQuantization scheme
BERT0.9020.895Dynamic quantization
\n
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Conclusion**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "To get started on quantizing your models in PyTorch, start with [the tutorials on the PyTorch website](https://pytorch.org/tutorials/#model-optimization). If you are working with sequence data start with [dynamic quantization for LSTM](https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html), or [BERT](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html). If you are working with image data then we recommend starting with the [transfer learning with", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "quantization](https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html) tutorial. Then you can explore [static post training quantization](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html). If you find that the accuracy drop with post training quantization is too high, then try [quantization aware training](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you run into issues you can get community help by posting in at [discuss.pytorch.org](https://discuss.pytorch.org/), use the quantization category for quantization related issues.\n\n_This post is authored by Raghuraman Krishnamoorthi, James Reed, Min Ni, Chris Gottbrath and Seth Weidman. Special thanks to Jianyu Huang, Lingyi Liu and Haixin Liu for producing quantization metrics included in this post._", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Further reading**:\n1. PyTorch quantization presentation at Neurips: [(https://research.fb.com/wp-content/uploads/2019/12/2.-Quantization.pptx)](https://research.fb.com/wp-content/uploads/2019/12/2.-Quantization.pptx)\n2. Quantized Tensors [(https://github.com/pytorch/pytorch/wiki/\nIntroducing-Quantized-Tensor)](https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor)\n3. Quantization RFC on Github [(https://github.com/pytorch/pytorch/", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "issues/18318)](https://github.com/pytorch/pytorch/issues/18318)", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling PyTorch FSDP for Training Foundation Models on IBM Cloud\"\nauthor: Linsong Chu, Less Wright, Hamid Shojanazeri, Sophia Wen, Raghu Ganti, Geeta Chauhan\nfeatured-img: \"/assets/images/scaling-pytorch-fsdp-image1-IBM_scaling_FSDP_visual_new.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Large model training using a cloud native approach is of growing interest for many enterprises given the emergence and success of [foundation models](https://research.ibm.com/blog/what-are-foundation-models). Some AI practitioners may assume that the only way they can achieve high GPU utilization for distributed training jobs is to run them on HPC systems, such as those inter-connected with Infiniband and may not consider Ethernet connected systems. We demonstrate how the latest distributed training", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "technique, Fully Sharded Data Parallel (FSDP) from PyTorch, successfully scales to models of size 10B+ parameters using commodity Ethernet networking in IBM Cloud.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "PyTorch FSDP Scaling", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "As models get larger, the standard techniques for data parallel training work only if the GPU can hold a full replica of the model, along with its training state (optimizer, activations, etc.). However, GPU memory increases have not kept up with the model size increases and new techniques for training such models have emerged (e.g., Fully Sharded Data Parallel, [DeepSpeed](https://www.deepspeed.ai/)), which allow us to efficiently distribute the model and data over multiple GPUs during training. In this", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "blog post, we demonstrate a path to achieve remarkable scaling of model training to 64 nodes (512 GPUs) using PyTorch native FSDP APIs as we increase model sizes to 11B.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "What is Fully Sharded Data Parallel?\n\nFSDP extends the distributed data parallel training (DDP) approach by sharding model parameters, gradient and optimizer states into K FSDP units, determined by using a wrapping policy. FSDP achieves large model training efficiency in terms of resources and performance by significantly reducing the memory footprint on each GPU and overlapping computation and communication.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Resource efficiency is achieved with memory footprint reduction by having all GPUs own a portion of each FSDP unit. To process a given FSDP unit, all GPUs share their locally owned portion via all_gather communication calls.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "#### **Integration in torchvision**\nWe\u2019ve also enabled quantization for some of the most popular models in [torchvision](https://github.com/pytorch/vision/tree/master/torchvision/models/quantization): Googlenet, Inception, Resnet, ResNeXt, Mobilenet and Shufflenet. We have upstreamed these changes to torchvision in three forms:\n1. Pre-trained quantized weights so that you can use them right away.\n2. Quantization ready model definitions so that you can do post-training quantization or quantization aware training.\n3. A script for doing quantization aware training \u2014 which is available for any of these model though, as you will learn below, we only found it necessary for achieving accuracy with Mobilenet.\n4. We also have a [tutorial](https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html) showing how you can do transfer learning with quantization using one of the torchvision models.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### **Choosing an approach**\nThe choice of which scheme to use depends on multiple factors:\n1. Model/Target requirements: Some models might be sensitive to quantization, requiring quantization aware training.\n2. Operator/Backend support: Some backends require fully quantized operators.\n\nCurrently, operator coverage is limited and may restrict the choices listed in the table below:\nThe table below provides a guideline.", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Model TypePreferred schemeWhy
LSTM/RNNDynamic QuantizationThroughput dominated by compute/memory bandwidth for weights
BERT/TransformerDynamic QuantizationThroughput dominated by compute/memory bandwidth for weights
CNNStatic QuantizationThroughput limited by memory bandwidth for activations
CNNQuantization Aware TrainingIn the case where accuracy can't be achieved with static quantization
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### **Performance Results**\nQuantization provides a 4x reduction in the model size and a speedup of 2x to 3x compared to floating point implementations depending on the hardware platform and the model being benchmarked. Some sample results are:", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ModelFloat Latency (ms)Quantized Latency (ms)Inference Performance GainDeviceNotes
BERT5813131.8xXeon-D2191 (1.6GHz)Batch size = 1, Maximum sequence length= 128, Single thread, x86-64, Dynamic quantization
Resnet-502141032xXeon-D2191 (1.6GHz)Single thread, x86-64, Static quantization
Mobilenet-v297175.7xSamsung S9Static quantization, Floating point numbers are based on Caffe2 run-time and are not optimized
\n
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### **Accuracy results**\nWe also compared the accuracy of static quantized models with the floating point models on Imagenet. For dynamic quantization, we [compared](https://github.com/huggingface/transformers/blob/master/examples/run_glue.py) the F1 score of BERT on the GLUE benchmark for MRPC.\n\n#### **Computer Vision Model accuracy**", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ModelTop-1 Accuracy (Float)Top-1 Accuracy (Quantized)Quantization scheme
Googlenet69.869.7Static post training quantization
Inception-v377.577.1Static post training quantization
ResNet-1869.869.4Static post training quantization
Resnet-5076.175.9Static post training quantization
ResNext-101 32x8d79.379Static post training quantization
Mobilenet-v271.971.6Quantization Aware Training
Shufflenet-v269.468.4Static post training quantization
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "#### **Speech and NLP Model accuracy**\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n
ModelF1 (GLUEMRPC) FloatF1 (GLUEMRPC) QuantizedQuantization scheme
BERT0.9020.895Dynamic quantization
\n
", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### **Conclusion**\nTo get started on quantizing your models in PyTorch, start with [the tutorials on the PyTorch website](https://pytorch.org/tutorials/#model-optimization). If you are working with sequence data start with [dynamic quantization for LSTM](https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html), or [BERT](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html). If you are working with image data then we recommend starting with the [transfer learning with quantization](https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html) tutorial. Then you can explore [static post training quantization](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html). If you find that the accuracy drop with post training quantization is too high, then try [quantization aware training](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "If you run into issues you can get community help by posting in at [discuss.pytorch.org](https://discuss.pytorch.org/), use the quantization category for quantization related issues.\n\n_This post is authored by Raghuraman Krishnamoorthi, James Reed, Min Ni, Chris Gottbrath and Seth Weidman. Special thanks to Jianyu Huang, Lingyi Liu and Haixin Liu for producing quantization metrics included in this post._\n\n### **Further reading**:\n1. PyTorch quantization presentation at Neurips: [(https://research.fb.com/wp-content/uploads/2019/12/2.-Quantization.pptx)](https://research.fb.com/wp-content/uploads/2019/12/2.-Quantization.pptx)\n2. Quantized Tensors [(https://github.com/pytorch/pytorch/wiki/\nIntroducing-Quantized-Tensor)](https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor)\n3. Quantization RFC on Github [(https://github.com/pytorch/pytorch/\nissues/18318)](https://github.com/pytorch/pytorch/issues/18318)", "metadata": {"source": "https://pytorch.org/blog/introduction-to-quantization-on-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling PyTorch FSDP for Training Foundation Models on IBM Cloud\"\nauthor: Linsong Chu, Less Wright, Hamid Shojanazeri, Sophia Wen, Raghu Ganti, Geeta Chauhan\nfeatured-img: \"/assets/images/scaling-pytorch-fsdp-image1-IBM_scaling_FSDP_visual_new.png\"\n---\n\nLarge model training using a cloud native approach is of growing interest for many enterprises given the emergence and success of [foundation models](https://research.ibm.com/blog/what-are-foundation-models). Some AI practitioners may assume that the only way they can achieve high GPU utilization for distributed training jobs is to run them on HPC systems, such as those inter-connected with Infiniband and may not consider Ethernet connected systems. We demonstrate how the latest distributed training technique, Fully Sharded Data Parallel (FSDP) from PyTorch, successfully scales to models of size 10B+ parameters using commodity Ethernet networking in IBM Cloud.\n\n## PyTorch FSDP Scaling", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "As models get larger, the standard techniques for data parallel training work only if the GPU can hold a full replica of the model, along with its training state (optimizer, activations, etc.). However, GPU memory increases have not kept up with the model size increases and new techniques for training such models have emerged (e.g., Fully Sharded Data Parallel, [DeepSpeed](https://www.deepspeed.ai/)), which allow us to efficiently distribute the model and data over multiple GPUs during training. In this blog post, we demonstrate a path to achieve remarkable scaling of model training to 64 nodes (512 GPUs) using PyTorch native FSDP APIs as we increase model sizes to 11B.\n\n### What is Fully Sharded Data Parallel?", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "FSDP extends the distributed data parallel training (DDP) approach by sharding model parameters, gradient and optimizer states into K FSDP units, determined by using a wrapping policy. FSDP achieves large model training efficiency in terms of resources and performance by significantly reducing the memory footprint on each GPU and overlapping computation and communication.\n\nResource efficiency is achieved with memory footprint reduction by having all GPUs own a portion of each FSDP unit. To process a given FSDP unit, all GPUs share their locally owned portion via all_gather communication calls.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} {"page_content": "Performance efficiency is accomplished by overlapping all_gather communication calls for upcoming FSDP units with computation of the current FSDP unit. Once the current FSDP unit has been processed, the non-locally owned parameters are dropped, freeing memory for the upcoming FSDP units. This process achieves training efficiency by the overlap of computation and communication, while also reducing the peak memory needed by each GPU.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "In what follows, we demonstrate how FSDP allows us to keep hundreds of GPUs highly utilized throughout a distributed training job, while running over standard Ethernet networking (system description towards the end of the blog). We chose the T5 architecture for our experiments and leveraged the code from the [FSDP workshop](https://github.com/pytorch/workshops/tree/master/FSDP_Workshop). In each of our experiments, we start with a single node experiment to create a baseline and report the metric", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "seconds/iteration normalized by the batch size as well as compute the teraflops based on the [Megatron-LM paper](https://cs.stanford.edu/~matei/papers/2021/sc_megatron_lm.pdf) (see Appendix for details of teraflop computation for T5). Our experiments aim to maximize the batch size (while avoiding cudaMalloc retries) to take full advantage of overlap in computation and communications, as discussed below. Scaling is defined as the ratio of the seconds/iteration normalized by batch size for N nodes versus a", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "single node, representing how well we can utilize the additional GPUs as more nodes are added.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Experimental Results", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Our first set of experiments using the T5-3B configuration (mixed precision with BF16, activation checkpointing, and transformer wrapping policy) demonstrated scaling efficiency of 95% as we increased the number of GPUs from 8 to 512 (1 to 64 nodes, respectively). We achieved these results without any modifications to the existing FSDP APIs. We observed that, for this scale, over Ethernet based network, there is sufficient bandwidth to enable continuous overlap of communication and computation.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "However, when we increased the T5 model size to 11B, the scaling efficiency declined substantially to 20%. The PyTorch profiler shows that overlap of communication and computation was very limited. Further investigation into the network bandwidth usage revealed that the poor overlap is being caused by latency in the communication of individual packets and not the bandwidth required (in fact, our peak bandwidth utilization is 1/4th of that available). This led us to hypothesize that if we can increase the", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "compute time by increasing the batch size, we can better overlap communication and computation. However, given we are already at maximum GPU memory allocation, we must identify opportunities to rebalance the memory allocation to allow for increase in batch size. We identified that the model state was being allocated a lot more memory than was needed. The primary function of these reservations is to have pre-reserved memory ready to aggressively send/receive tensors during the communication periods and too", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "few buffers can result in increased wait times, whereas too many buffers result in smaller batch sizes.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "To achieve better efficiency, the PyTorch distributed team introduced a new control knob, the rate_limiter which controls how much memory is allocated for send/receive of tensors, alleviating the memory pressure and providing room for higher batch sizes. In our case, the rate_limiter could increase the batch size from 20 to 50, thus increasing compute time by 2.5x and allowing for much greater overlap of communication and computation. With this fix, we increased the scaling efficiency to >75% (at 32", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "nodes)!", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Continued investigation into the factors limiting scaling efficiency uncovered that the rate limiter was creating a recurring pipeline bubble of GPU idle time. This was due to the rate limiter using a block and flush approach for the allocation and release of each set of memory buffers. By waiting for the entire block to complete before initiating a new all_gather, the GPU was idling at the start of each block, while waiting for the new set of all_gather parameters to arrive. This bubble was alleviated by", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "moving to a sliding window approach. Upon the completion of a single all_gather step and its computation (rather than a block of them), the memory is freed and the next all_gather is immediately issued in a much more uniform manner. This improvement eliminated the pipeline bubble and boosted the scaling efficiencies to >90% (at 32 nodes).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n

\nFigure 1: Scaling of T5-XL (3B) and T5-XXL (11B) from 1 node to 64 nodes\n

\n\n

\n\n

\n\n

\nFigure 2: TFLOPs/sec usage for T5-XL(3B) and T5-XXL (11B) as we increase number of nodes\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "IBM Cloud AI System and Middleware\n\nThe AI infrastructure used for this work is a large-scale AI system on IBM Cloud consisting of nearly 200 nodes, each node with 8 NVIDIA A100 80GB cards, 96 vCPUs, and 1.2TB CPU RAM. The GPU cards within a node are connected via NVLink with a card-to-card bandwidth of 600GBps. Nodes are connected by 2 x 100Gbps Ethernet links with SRIOV based TCP/IP stack, providing a usable bandwidth of 120Gbps.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "The IBM Cloud AI System has been production-ready since May of 2022 and is configured with the OpenShift container platform to run AI workloads. We also built a software stack for production AI workloads that provide end-to-end tools for training workloads. The middleware leverages Ray for pre and post processing workloads and PyTorch for training of models. We also integrate a Kubernetes native scheduler, MCAD, that manages multiple jobs with job queuing, gang scheduling, prioritization, and quota", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "management. A multi-NIC CNI discovers all available network interfaces and handles them as a single NIC pool enabling optimized use of the network interfaces in Kubernetes. Finally, CodeFlare CLI supports a single pane for observability of the full stack using a desktop CLI (e.g., GPU utilization, application metrics like loss, gradient norm).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFigure 3: Foundation Model Middleware Stack\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Conclusion and Future Work", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "In conclusion, we demonstrated how we can achieve remarkable scaling of FSDP APIs over non-InfiniBand networks. We identified the bottleneck that had limited scaling to less than 20% efficiency for 11B parameter model training. After identifying the issue, we were able to correct this with a new rate limiter control to ensure a more optimal balance of reserved memory and communication overlap relative to compute time. With this improvement, we were able to achieve 90% scaling efficiency (a 4.5x", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "improvement), at 256 GPUs and 80% at 512 GPUs for training of the 11B parameter model. In addition, the 3B parameter model scales extremely well with 95% efficiency even as we increase the number of GPUs to 512.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "This is a first in the industry to achieve such scaling efficiencies for up to 11B parameter models using Kubernetes with vanilla Ethernet and PyTorch native FSDP API\u2019s. This improvement enables users to train huge models on a Hybrid Cloud platform in a cost efficient and sustainable manner.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "We plan on continuing to investigate scaling with decoder only models and increasing the size of these models to 100B+ parameters. From a system design perspective, we are exploring capabilities such as RoCE and GDR that can improve latencies of communications over Ethernet networks.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nThis blog was possible because of contributions from both PyTorch Distributed and IBM Research teams.\n\nFrom the PyTorch Distributed team, we would like to thank Less Wright, Hamid Shojanazeri, Geeta Chauhan, Shen Li, Rohan Varma, Yanli Zhao, Andrew Gu, Anjali Sridhar, Chien-Chin Huang, and Bernard Nguyen.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "From the IBM Research team, we would like to thank Linsong Chu, Sophia Wen, Lixiang (Eric) Luo, Marquita Ellis, Davis Wertheimer, Supriyo Chakraborty, Raghu Ganti, Mudhakar Srivatsa, Seetharami Seelam, Carlos Costa, Abhishek Malvankar, Diana Arroyo, Alaa Youssef, Nick Mitchell.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Appendix", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "Teraflop computation\n\nThe T5-XXL (11B) architecture has two types of T5 blocks, one is an encoder and the second is a decoder. Following the approach of Megatron-LM, where each matrix multiplication requires 2m\u00d7k\u00d7n FLOPs, where the first matrix is of size m\u00d7k and the second is k\u00d7n. The encoder block consists of self-attention and feed forward layers, whereas the decoder block consists of self-attention, cross-attention, and feed forward layers.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "The attention (both self and cross) block consists of a QKV projection, which requires 6Bsh2 operations, an attention matrix computation requiring 2Bs2h operations, an attention over values which needs 2Bs2h computations, and the post-attention linear projection requires 2Bsh2 operations. Finally, the feed forward layer requires 15Bsh2 operations.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "The total for an encoder block is 23Bsh2+4Bs2h, whereas for a decoder block, it comes to 31Bsh2+8Bs2h. With a total of 24 encoder and 24 decoder blocks and 2 forward passes (as we discard the activations) and one backward pass (equivalent to two forward passes), the final FLOPs computation comes to be 96\u00d7(54Bsh2+ 12Bs2h) + 6BshV. Here, B is the batch size per GPU, s is sequence length, h is hidden state size, and V is vocabulary size.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "We repeat a similar computation for T5-XL (3B) architecture, which is slightly different.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16\"\nauthor: Mingfei Ma (Intel), Vitaly Fedyunin (Meta), Wei Wei (Meta)\nfeatured-img: '\\assets\\images\\empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16.png'\n---", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Overview", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Recent years, the growing complexity of AI models have been posing requirements on hardware for more and more compute capability. Reduced precision numeric format has been proposed to address this problem. Bfloat16 is a custom 16-bit floating point format for AI which consists of one sign bit, eight exponent bits, and seven mantissa bits. With the same dynamic range as float32, bfloat16 doesn\u2019t require a special handling such as loss scaling. Therefore, bfloat16 is a drop-in replacement for float32 when", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "running deep neural networks for both inference and training.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "The 3rd Gen Intel\u00ae Xeon\u00ae Scalable processor (codenamed Cooper Lake), is the first general purpose x86 CPU with native bfloat16 support. Three new bfloat16 instructions were introduced in Intel\u00ae Advanced Vector Extensions-512 (Intel\u00ae AVX-512): VCVTNE2PS2BF16, VCVTNEPS2BF16, and VDPBF16PS. The first two instructions perform conversion from float32 to bfloat16, and the last one performs a dot product of bfloat16 pairs. Bfloat16 theoretical compute throughput is", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "doubled over float32 on Cooper Lake. On the next generation of Intel\u00ae Xeon\u00ae Scalable Processors, bfloat16 compute throughput will be further enhanced through Advanced Matrix Extensions (Intel\u00ae AMX) instruction set extension.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Intel and Meta previously collaborated to enable bfloat16 on PyTorch, and the related work was published in an earlier [blog](https://community.intel.com/t5/Blogs/Tech-Innovation/Artificial-Intelligence-AI/Intel-and-Facebook-Accelerate-PyTorch-Performance-with-3rd-Gen/post/1335659) during launch of Cooper Lake. In that blog, we introduced the hardware advancement for native bfloat16 support and showcased a performance boost of 1.4x to 1.6x of bfloat16 over float32 from DLRM, ResNet-50 and ResNext-101-32x4d.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "In this blog, we will introduce the latest software enhancement on bfloat16 in PyTorch 1.12, which would apply to much broader scope of user scenarios and showcase even higher performance boost.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Native Level Optimization on Bfloat16", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "On PyTorch CPU bfloat16 path, the compute intensive operators, e.g., convolution, linear and bmm, use oneDNN (oneAPI Deep Neural Network Library) to achieve optimal performance on Intel CPUs with AVX512_BF16 or AMX support. The other operators, such as tensor operators and neural network operators, are optimized at PyTorch native level. We have enlarged bfloat16 kernel level optimizations to majority of operators on dense tensors, both inference and training applicable (sparse tensor bfloat16 support will", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "be covered in future work), specifically:", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- **Bfloat16 vectorization**: Bfloat16 is stored as unsigned 16-bit integer, which requires it to be casted to float32 for arithmetic operations such as add, mul, etc. Specifically, each bfloat16 vector will be converted to two float32 vectors, processed accordingly and then converted back. While for non-arithmetic operations such as cat, copy, etc., it is a straight memory copy and no data type conversion will be involved.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- **Bfloat16 reduction**: Reduction on bfloat16 data uses float32 as accumulation type to guarantee numerical stability, e.g., sum, BatchNorm2d, MaxPool2d, etc.\n- **Channels Last optimization**: For vision models, Channels Last is the preferable memory format over Channels First from performance perspective. We have implemented fully optimized CPU kernels for all the commonly used CV modules on channels last memory format, taking care of both float32 and bfloat16.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Run Bfloat16 with Auto Mixed Precision\n\nTo run model on bfloat16, typically user can either explicitly convert the data and model to bfloat16, for example:\n\n```console\n# with explicit conversion\ninput = input.to(dtype=torch.bfloat16)\nmodel = model.to(dtype=torch.bfloat16)", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "or utilize torch.amp (Automatic Mixed Precision) package. The autocast instance serves as context managers or decorators that allow regions of your script to run in mixed precision, for example:\n\n```console\n# with AMP\nwith torch.autocast(device_type=\"cpu\", dtype=torch.bfloat16):\n output = model(input)", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Generally, the explicit conversion approach and AMP approach have similar performance. Even though, we recommend run bfloat16 models with AMP, because:\n\n- **Better user experience with automatic fallback**: If your script includes operators that don\u2019t have bfloat16 support, autocast will implicitly convert them back to float32 while the explicit converted model will give a runtime error.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- **Mixed data type for activation and parameters**: Unlike the explicit conversion which converts all the model parameters to bfloat16, AMP mode will run in mixed data type. To be specific, input/output will be kept in bfloat16 while parameters, e.g., weight/bias, will be kept in float32. The mixed data type of activation and parameters will help improve performance while maintaining the accuracy.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Performance Gains\n\nWe benchmarked inference performance of TorchVision models on Intel\u00ae Xeon\u00ae Platinum 8380H CPU @ 2.90GHz (codenamed Cooper Lake), single instance per socket (batch size = 2 x number of physical cores). Results show that bfloat16 has 1.4x to 2.2x performance gain over float32.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "The performance boost of bfloat16 over float32 primarily comes from 3 aspects:", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- The compute intensive operators take advantage of the new bfloat16 native instruction VDPBF16PS which doubles the hardware compute throughput.\n- Bfloat16 have only half the memory footprint of float32, so theoretically the memory bandwidth intensive operators will be twice faster.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- On Channels Last, we intentionally keep the same parallelization scheme for all the memory format aware operators (can\u2019t do this on Channels First though), which increases the data locality when passing each layer\u2019s output to the next. Basically, it keeps the data closer to CPU cores while data would reside in cache anyway. And bfloat16 will have a higher cache hit rate compared with float32 in such scenarios due to smaller memory footprint.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Conclusion & Future Work", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "In this blog, we introduced recent software optimizations on bfloat16 introduced in PyTorch 1.12. Results on the 3rd Gen Intel\u00ae Xeon\u00ae Scalable processor show that bfloat16 has 1.4x to 2.2x performance gain over float32 on the TorchVision models. Further improvement is expected on the next generation of Intel\u00ae Xeon\u00ae Scalable Processors with AMX instruction support. Though the performance number for this blog is collected with TorchVision models, the benefit is", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "broad across all topologies. And we will continue to extend the bfloat16 optimization effort to a broader scope in the future!", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgement\n\nThe results presented in this blog is a joint effort of Meta and Intel PyTorch team. Special thanks to Vitaly Fedyunin and Wei Wei from Meta who spent precious time and gave substantial assistance! Together we made one more step on the path of improving the PyTorch CPU eco system.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "Reference", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "- [The bfloat16 numerical format](https://cloud.google.com/tpu/docs/bfloat16?hl=en)\n- [https://pytorch.org/docs/master/amp.html#torch.autocast](https://pytorch.org/docs/master/amp.html#torch.autocast)\n- [Intel and Facebook Accelerate PyTorch Performance with 3rd Gen Intel\u00ae Xeon\u00ae Processors and Intel\u00ae Deep Learning Boost\u2019s new BFloat16 capability](https://community.intel.com/t5/Blogs/Tech-Innovation/Artificial-Intelligence-AI/Intel-and-Facebook-Accelerate-PyTorch-Performance-with-3rd-Gen/post/1335659)", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Announcing PyTorch Conference 2022\"\nauthor:\nfeatured-img: \"/assets/images/pytorch-conference-2022.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce that the PyTorch Conference returns in-person as a satellite event to", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "[NeurlPS](https://l.workplace.com/l.php?u=https%3A%2F%2Fnips.cc%2F&h=AT3cdRwSEhyuNXpH2ptWjk-KxMxcceaYeTfflT6PEezDQ_zeUxRv1gjX7GhTQBgvZxFAR0wlSBwuhpipdMjUknMnhY5oJ5C4HjLNO40-12UnoeYALriwrvdxGfgigo8KYlWu_gRIQwlO-2r0wTnNft0whoSaOdVAxw&__tn__=-UK-R&c[0]=AT3z6QRLu8Uw48lKQ_P6FFq7ncHfjsfI16OGZvWO9kALatCY4sZcMjNzR7a4OiOG25RKVHpDX0TGutZHyM_R8Kl2s71Y3DEbq5QccmUVaSzCbcMUSc5Ms2zXHoeGxUlw1XirihAydPsX4Y1OmF6GRjqH8YFTNTFQRN3I8j2SFhR8LEUDxDmfnZ8Q7c2hXi0HeGc) (Neural Information Processing Systems) in New Orleans on Dec.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "2nd.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "We changed the name from PyTorch Developer Day to PyTorch Conference to signify the turning of a new chapter as we look to the future of PyTorch, encompassing the entire PyTorch Community. This conference will bring together leading researchers, academics and developers from the Machine Learning (ML) and Deep Learning (DL) communities to join a multiple set of talks and a poster session; covering new software releases on [PyTorch](https://pytorch.org/), use cases in academia and industry, as well as ML/DL", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "development and production trends.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "EVENT OVERVIEW\n\nWhen: Dec 2nd, 2022 (In-Person and Virtual)\n\nWhere: New Orleans, Louisiana (USA) | *Virtual option as well*", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "SCHEDULE\n\nAll times in Central Standard.\n\n8:00-9:00 am   Registration/Check in\n\n9:00-11:20 am   Keynote & Technical Talks\n\n11:30-1:00 pm   Lunch\n\n1:00-3:00 pm   Poster Session & Breakouts\n\n3:00-4:00 pm   Community/Partner Talks\n\n4:00-5:00 pm   Panel Discussion\n\nAgenda subject to change.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "All talks will be livestreamed and available to the public. The in-person event will be by invitation only as space is limited. If you\u2019d like to apply to attend in person, please submit all requests [here](https://pytorchconference22.splashthat.com/).", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "LINKS\n\n- [Submit Content for Consideration by Sept. 30th](https://docs.google.com/forms/d/121ptOuhqhmcPev9g5Zt2Ffl-NtB_oeyFk5CWjumUVLQ/edit)\n- [Livestream event page](https://www.facebook.com/events/1562940847455759)\n- [Apply for an invitation to the in-person event](https://pytorchconference22.splashthat.com/)", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'New PyTorch library releases including TorchVision Mobile, TorchAudio I/O, and more'\nauthor: Team PyTorch \n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Today, we are announcing updates to a number of PyTorch libraries, alongside the [PyTorch 1.8 release](https://pytorch.org/blog/pytorch-1.8-released). The updates include new releases for the domain libraries including TorchVision, TorchText and TorchAudio as well as new version of TorchCSPRNG. These releases include a number of new features and improvements and, along with the PyTorch 1.8 release, provide a broad set of updates for the PyTorch community to build on and leverage.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Some highlights include:\n* **TorchVision** - Added support for PyTorch Mobile including [Detectron2Go](https://ai.facebook.com/blog/d2go-brings-detectron2-to-mobile) (D2Go), auto-augmentation of data during training, on the fly type conversion, and [AMP autocasting](https://pytorch.org/docs/stable/amp.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchAudio** - Major improvements to I/O, including defaulting to sox_io backend and file-like object support. Added Kaldi Pitch feature and support for CMake based build allowing TorchAudio to better support no-Python environments.\n* **TorchText** - Updated the dataset loading API to be compatible with standard PyTorch data loading utilities.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchCSPRNG** - Support for cryptographically secure pseudorandom number generators for PyTorch is now stable with new APIs for AES128 ECB/CTR and CUDA support on Windows.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please note that, starting in PyTorch 1.6, features are classified as Stable, Beta, and Prototype. Prototype features are not included as part of the binary distribution and are instead available through either building from source, using nightlies or via compiler flag. You can see the detailed announcement [here](https://pytorch.org/blog/pytorch-feature-classification-changes/).\n\n\n# TorchVision 0.9.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Stable] TorchVision Mobile: Operators, Android Binaries, and Tutorial", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the first on-device support and binaries for a PyTorch domain library. We have seen significant appetite in both research and industry for on-device vision support to allow low latency, privacy friendly, and resource efficient mobile vision experiences. You can follow this [new tutorial](https://github.com/pytorch/android-demo-app/tree/master/D2Go) to build your own Android object detection app using TorchVision operators, D2Go, or your own custom operators and model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Stable] New Mobile models for Classification, Object Detection and Semantic Segmentation\nWe have added support for the MobileNetV3 architecture and provided pre-trained weights for Classification, Object Detection and Segmentation. It is easy to get up and running with these models, just import and load them as you would any ```torchvision``` model:\n```python\nimport torch\nimport torchvision", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Classification\nx = torch.rand(1, 3, 224, 224)\nm_classifier = torchvision.models.mobilenet_v3_large(pretrained=True)\nm_classifier.eval()\npredictions = m_classifier(x)\n\n# Quantized Classification\nx = torch.rand(1, 3, 224, 224)\nm_classifier = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\nm_classifier.eval()\npredictions = m_classifier(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Object Detection: Highly Accurate High Resolution Mobile Model\nx = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Semantic Segmentation: Highly Accurate Mobile Model\nx = torch.rand(1, 3, 520, 520)\nm_segmenter = torchvision.models.segmentation.deeplabv3_mobilenet_v3_large(pretrained=True)\nm_segmenter.eval()\npredictions = m_segmenter(x)\n```\nThese models are highly competitive with TorchVision\u2019s existing models on resource efficiency, speed, and accuracy. See our [release notes](https://github.com/pytorch/vision/releases) for detailed performance metrics.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Stable] AutoAugment", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[AutoAugment](https://arxiv.org/pdf/1805.09501.pdf) is a common Data Augmentation technique that can increase the accuracy of Scene Classification models. Though the data augmentation policies are directly linked to their trained dataset, empirical studies show that ImageNet policies provide significant improvements when applied to other datasets. We\u2019ve implemented 3 policies learned on the following datasets: ImageNet, CIFA10 and SVHN. These can be used standalone or mixed-and-matched with existing", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "transforms:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchvision import transforms", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "t = transforms.AutoAugment()\ntransformed = t(image)\n\n\ntransform=transforms.Compose([\n transforms.Resize(256),\n transforms.AutoAugment(),\n transforms.ToTensor()])\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Other New Features for TorchVision\n* [Stable] All read and decode methods in the io.image package now support:\n * Palette, Grayscale Alpha and RBG Alpha image types during PNG decoding\n * On-the-fly conversion of image from one type to the other during read\n* [Stable] WiderFace dataset\n* [Stable] Improved FasterRCNN speed and accuracy by introducing a score threshold on RPN\n* [Stable] Modulation input for DeformConv2D\n* [Stable] Option to write audio to a video file", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* [Stable] Utility to draw bounding boxes\n* [Beta] Autocast support in all Operators\nFind the full TorchVision release notes [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# TorchAudio 0.8.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "I/O Improvements\nWe have continued our work from the [previous release](https://github.com/pytorch/audio/releases/tag/v0.7.0) to improve TorchAudio\u2019s I/O support, including:\n* [Stable] Changing the default backend to \u201csox_io\u201d (for Linux/macOS), and updating the \u201csoundfile\u201d backend\u2019s interface to align with that of \u201csox_io\u201d. The legacy backend and interface are still accessible, though it is strongly discouraged to use them.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* [Stable] File-like object support in both \"sox_io\" backend, \u201csoundfile\u201d backend and sox_effects.\n* [Stable] New options to change the format, encoding, and bits_per_sample when saving.\n* [Stable] Added GSM, HTK, AMB, AMR-NB and AMR-WB format support to the \u201csox_io\u201d backend.\n* [Beta] A new ```functional.apply_codec``` function which can degrade audio data by applying audio codecs supported by \u201csox_io\u201d backend in an in-memory fashion.\nHere are some examples of features landed in this release:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Load audio over HTTP\nwith requests.get(URL, stream=True) as response:\n waveform, sample_rate = torchaudio.load(response.raw)\n \n# Saving to Bytes buffer as 32-bit floating-point PCM\nbuffer_ = io.BytesIO()\ntorchaudio.save(\n buffer_, waveform, sample_rate,\n format=\"wav\", encoding=\"PCM_S\", bits_per_sample=16)\n \n# Apply effects while loading audio from S3\nclient = boto3.client('s3')\nresponse = client.get_object(Bucket=S3_BUCKET, Key=S3_KEY)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "waveform, sample_rate = torchaudio.sox_effects.apply_effect_file(\n response['Body'],\n [[\"lowpass\", \"-1\", \"300\"], [\"rate\", \"8000\"]])\n \n# Apply GSM codec to Tensor\nencoded = torchaudio.functional.apply_codec(\n waveform, sample_rate, format=\"gsm\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Check out the revamped audio preprocessing tutorial, [Audio Manipulation with TorchAudio](https://pytorch.org/tutorials/beginner/audio_preprocessing_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Switch to CMake-based build\nIn the previous version of TorchAudio, it was utilizing CMake to build third party dependencies. Starting in 0.8.0, TorchaAudio uses CMake to build its C++ extension. This will open the door to integrate TorchAudio in non-Python environments (such as C++ applications and mobile). We will continue working on adding example applications and mobile integrations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Improved and New Audio Transforms", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We have added two widely requested operators in this release: the SpectralCentroid transform and the Kaldi Pitch feature extraction (detailed in [\"A pitch extraction algorithm tuned for automatic speech recognition\"](https://ieeexplore.ieee.org/document/6854049)). We\u2019ve also exposed a normalization method to Mel transforms, and additional STFT arguments to Spectrogram. We would like to ask our community to continue to [raise feature", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "requests](https://github.com/pytorch/audio/issues/new?assignees=&labels=&template=feature-request.md) for core audio processing features like these!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Community Contributions", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We had more contributions from the open source community in this release than ever before, including several completely new features. We would like to extend our sincere thanks to the community. Please check out the newly added [CONTRIBUTING.md](https://github.com/pytorch/audio/blob/master/CONTRIBUTING.md) for ways to contribute code, and remember that reporting bugs and requesting features are just as valuable. We will continue posting well-scoped work items as issues labeled \u201chelp-wanted\u201d and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "\u201ccontributions-welcome\u201d for anyone who would like to contribute code, and are happy to coach new contributors through the contribution process.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Find the full TorchAudio release notes [here](https://github.com/pytorch/audio/releases).\n\n# TorchText 0.9.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Dataset API Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "In this release, we are updating TorchText\u2019s dataset API to be compatible with PyTorch data utilities, such as DataLoader, and are deprecating TorchText\u2019s custom data abstractions such as ```Field```. The updated datasets are simple string-by-string iterators over the data. For guidance about migrating from the legacy abstractions to use modern PyTorch data utilities, please refer to our [migration guide](https://github.com/pytorch/text/blob/master/examples/legacy_tutorial/migration_tutorial.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The text datasets listed below have been updated as part of this work. For examples of how to use these datasets, please refer to our [end-to-end text classification tutorial](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html).\n* **Language modeling:** WikiText2, WikiText103, PennTreebank, EnWik9\n* **Text classification:** AG_NEWS, SogouNews, DBpedia, YelpReviewPolarity, YelpReviewFull, YahooAnswers, AmazonReviewPolarity, AmazonReviewFull, IMDB", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **Sequence tagging:** UDPOS, CoNLL2000Chunking\n* **Translation:** IWSLT2016, IWSLT2017\n* **Question answer:** SQuAD1, SQuAD2", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Find the full TorchText release notes [here](https://github.com/pytorch/text/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# [Stable] TorchCSPRNG 0.2.0\nWe [released TorchCSPRNG in August 2020](https://pytorch.org/blog/torchcsprng-release-blog/), a PyTorch C++/CUDA extension that provides cryptographically secure pseudorandom number generators for PyTorch. Today, we are releasing the 0.2.0 version and designating the library as stable. This release includes a new API for encrypt/decrypt with AES128 ECB/CTR as well as CUDA 11 and Windows CUDA support.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Find the full TorchCSPRNG release notes [here](https://github.com/pytorch/csprng/releases/).\n\n\n\n\n\n\n\nThanks for reading, and if you are excited about these updates and want to participate in the future of PyTorch, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch).\n\nCheers!\n\n***Team PyTorch***", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "In what follows, we demonstrate how FSDP allows us to keep hundreds of GPUs highly utilized throughout a distributed training job, while running over standard Ethernet networking (system description towards the end of the blog). We chose the T5 architecture for our experiments and leveraged the code from the [FSDP workshop](https://github.com/pytorch/workshops/tree/master/FSDP_Workshop). In each of our experiments, we start with a single node experiment to create a baseline and report the metric seconds/iteration normalized by the batch size as well as compute the teraflops based on the [Megatron-LM paper](https://cs.stanford.edu/~matei/papers/2021/sc_megatron_lm.pdf) (see Appendix for details of teraflop computation for T5). Our experiments aim to maximize the batch size (while avoiding cudaMalloc retries) to take full advantage of overlap in computation and communications, as discussed below. Scaling is defined as the ratio of the seconds/iteration normalized by batch size for N nodes versus a single node, representing how well we can utilize the additional GPUs as more nodes are added.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "### Experimental Results\n\nOur first set of experiments using the T5-3B configuration (mixed precision with BF16, activation checkpointing, and transformer wrapping policy) demonstrated scaling efficiency of 95% as we increased the number of GPUs from 8 to 512 (1 to 64 nodes, respectively). We achieved these results without any modifications to the existing FSDP APIs. We observed that, for this scale, over Ethernet based network, there is sufficient bandwidth to enable continuous overlap of communication and computation.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "However, when we increased the T5 model size to 11B, the scaling efficiency declined substantially to 20%. The PyTorch profiler shows that overlap of communication and computation was very limited. Further investigation into the network bandwidth usage revealed that the poor overlap is being caused by latency in the communication of individual packets and not the bandwidth required (in fact, our peak bandwidth utilization is 1/4th of that available). This led us to hypothesize that if we can increase the compute time by increasing the batch size, we can better overlap communication and computation. However, given we are already at maximum GPU memory allocation, we must identify opportunities to rebalance the memory allocation to allow for increase in batch size. We identified that the model state was being allocated a lot more memory than was needed. The primary function of these reservations is to have pre-reserved memory ready to aggressively send/receive tensors during the communication periods and too few buffers can result in increased wait times, whereas too many buffers result in smaller batch sizes.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "To achieve better efficiency, the PyTorch distributed team introduced a new control knob, the rate_limiter which controls how much memory is allocated for send/receive of tensors, alleviating the memory pressure and providing room for higher batch sizes. In our case, the rate_limiter could increase the batch size from 20 to 50, thus increasing compute time by 2.5x and allowing for much greater overlap of communication and computation. With this fix, we increased the scaling efficiency to >75% (at 32 nodes)!", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "Continued investigation into the factors limiting scaling efficiency uncovered that the rate limiter was creating a recurring pipeline bubble of GPU idle time. This was due to the rate limiter using a block and flush approach for the allocation and release of each set of memory buffers. By waiting for the entire block to complete before initiating a new all_gather, the GPU was idling at the start of each block, while waiting for the new set of all_gather parameters to arrive. This bubble was alleviated by moving to a sliding window approach. Upon the completion of a single all_gather step and its computation (rather than a block of them), the memory is freed and the next all_gather is immediately issued in a much more uniform manner. This improvement eliminated the pipeline bubble and boosted the scaling efficiencies to >90% (at 32 nodes).\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 1: Scaling of T5-XL (3B) and T5-XXL (11B) from 1 node to 64 nodes\n

\n\n

\n\n

\n\n

\nFigure 2: TFLOPs/sec usage for T5-XL(3B) and T5-XXL (11B) as we increase number of nodes\n

\n\n## IBM Cloud AI System and Middleware\n\nThe AI infrastructure used for this work is a large-scale AI system on IBM Cloud consisting of nearly 200 nodes, each node with 8 NVIDIA A100 80GB cards, 96 vCPUs, and 1.2TB CPU RAM. The GPU cards within a node are connected via NVLink with a card-to-card bandwidth of 600GBps. Nodes are connected by 2 x 100Gbps Ethernet links with SRIOV based TCP/IP stack, providing a usable bandwidth of 120Gbps.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "The IBM Cloud AI System has been production-ready since May of 2022 and is configured with the OpenShift container platform to run AI workloads. We also built a software stack for production AI workloads that provide end-to-end tools for training workloads. The middleware leverages Ray for pre and post processing workloads and PyTorch for training of models. We also integrate a Kubernetes native scheduler, MCAD, that manages multiple jobs with job queuing, gang scheduling, prioritization, and quota management. A multi-NIC CNI discovers all available network interfaces and handles them as a single NIC pool enabling optimized use of the network interfaces in Kubernetes. Finally, CodeFlare CLI supports a single pane for observability of the full stack using a desktop CLI (e.g., GPU utilization, application metrics like loss, gradient norm).\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 3: Foundation Model Middleware Stack\n

\n\n### Conclusion and Future Work\n\nIn conclusion, we demonstrated how we can achieve remarkable scaling of FSDP APIs over non-InfiniBand networks. We identified the bottleneck that had limited scaling to less than 20% efficiency for 11B parameter model training. After identifying the issue, we were able to correct this with a new rate limiter control to ensure a more optimal balance of reserved memory and communication overlap relative to compute time. With this improvement, we were able to achieve 90% scaling efficiency (a 4.5x improvement), at 256 GPUs and 80% at 512 GPUs for training of the 11B parameter model. In addition, the 3B parameter model scales extremely well with 95% efficiency even as we increase the number of GPUs to 512.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "This is a first in the industry to achieve such scaling efficiencies for up to 11B parameter models using Kubernetes with vanilla Ethernet and PyTorch native FSDP API\u2019s. This improvement enables users to train huge models on a Hybrid Cloud platform in a cost efficient and sustainable manner.\n\nWe plan on continuing to investigate scaling with decoder only models and increasing the size of these models to 100B+ parameters. From a system design perspective, we are exploring capabilities such as RoCE and GDR that can improve latencies of communications over Ethernet networks.\n\n## Acknowledgements\n\nThis blog was possible because of contributions from both PyTorch Distributed and IBM Research teams.\n\nFrom the PyTorch Distributed team, we would like to thank Less Wright, Hamid Shojanazeri, Geeta Chauhan, Shen Li, Rohan Varma, Yanli Zhao, Andrew Gu, Anjali Sridhar, Chien-Chin Huang, and Bernard Nguyen.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "From the IBM Research team, we would like to thank Linsong Chu, Sophia Wen, Lixiang (Eric) Luo, Marquita Ellis, Davis Wertheimer, Supriyo Chakraborty, Raghu Ganti, Mudhakar Srivatsa, Seetharami Seelam, Carlos Costa, Abhishek Malvankar, Diana Arroyo, Alaa Youssef, Nick Mitchell.\n\n## Appendix\n\n#### Teraflop computation\n\nThe T5-XXL (11B) architecture has two types of T5 blocks, one is an encoder and the second is a decoder. Following the approach of Megatron-LM, where each matrix multiplication requires 2m\u00d7k\u00d7n FLOPs, where the first matrix is of size m\u00d7k and the second is k\u00d7n. The encoder block consists of self-attention and feed forward layers, whereas the decoder block consists of self-attention, cross-attention, and feed forward layers.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "The attention (both self and cross) block consists of a QKV projection, which requires 6Bsh2 operations, an attention matrix computation requiring 2Bs2h operations, an attention over values which needs 2Bs2h computations, and the post-attention linear projection requires 2Bsh2 operations. Finally, the feed forward layer requires 15Bsh2 operations. \n\nThe total for an encoder block is 23Bsh2+4Bs2h, whereas for a decoder block, it comes to 31Bsh2+8Bs2h. With a total of 24 encoder and 24 decoder blocks and 2 forward passes (as we discard the activations) and one backward pass (equivalent to two forward passes), the final FLOPs computation comes to be 96\u00d7(54Bsh2+ 12Bs2h) + 6BshV. Here, B is the batch size per GPU, s is sequence length, h is hidden state size, and V is vocabulary size. \nWe repeat a similar computation for T5-XL (3B) architecture, which is slightly different.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-fsdp-for-training-foundation-models-on-ibm-cloud/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16\"\nauthor: Mingfei Ma (Intel), Vitaly Fedyunin (Meta), Wei Wei (Meta)\nfeatured-img: '\\assets\\images\\empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16.png'\n---\n\n## Overview\n\nRecent years, the growing complexity of AI models have been posing requirements on hardware for more and more compute capability. Reduced precision numeric format has been proposed to address this problem. Bfloat16 is a custom 16-bit floating point format for AI which consists of one sign bit, eight exponent bits, and seven mantissa bits. With the same dynamic range as float32, bfloat16 doesn\u2019t require a special handling such as loss scaling. Therefore, bfloat16 is a drop-in replacement for float32 when running deep neural networks for both inference and training.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "The 3rd Gen Intel\u00ae Xeon\u00ae Scalable processor (codenamed Cooper Lake), is the first general purpose x86 CPU with native bfloat16 support. Three new bfloat16 instructions were introduced in Intel\u00ae Advanced Vector Extensions-512 (Intel\u00ae AVX-512): VCVTNE2PS2BF16, VCVTNEPS2BF16, and VDPBF16PS. The first two instructions perform conversion from float32 to bfloat16, and the last one performs a dot product of bfloat16 pairs. Bfloat16 theoretical compute throughput is doubled over float32 on Cooper Lake. On the next generation of Intel\u00ae Xeon\u00ae Scalable Processors, bfloat16 compute throughput will be further enhanced through Advanced Matrix Extensions (Intel\u00ae AMX) instruction set extension.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "Intel and Meta previously collaborated to enable bfloat16 on PyTorch, and the related work was published in an earlier [blog](https://community.intel.com/t5/Blogs/Tech-Innovation/Artificial-Intelligence-AI/Intel-and-Facebook-Accelerate-PyTorch-Performance-with-3rd-Gen/post/1335659) during launch of Cooper Lake. In that blog, we introduced the hardware advancement for native bfloat16 support and showcased a performance boost of 1.4x to 1.6x of bfloat16 over float32 from DLRM, ResNet-50 and ResNext-101-32x4d.\n\nIn this blog, we will introduce the latest software enhancement on bfloat16 in PyTorch 1.12, which would apply to much broader scope of user scenarios and showcase even higher performance boost.\n\n## Native Level Optimization on Bfloat16", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "On PyTorch CPU bfloat16 path, the compute intensive operators, e.g., convolution, linear and bmm, use oneDNN (oneAPI Deep Neural Network Library) to achieve optimal performance on Intel CPUs with AVX512_BF16 or AMX support. The other operators, such as tensor operators and neural network operators, are optimized at PyTorch native level. We have enlarged bfloat16 kernel level optimizations to majority of operators on dense tensors, both inference and training applicable (sparse tensor bfloat16 support will be covered in future work), specifically:", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "- **Bfloat16 vectorization**: Bfloat16 is stored as unsigned 16-bit integer, which requires it to be casted to float32 for arithmetic operations such as add, mul, etc. Specifically, each bfloat16 vector will be converted to two float32 vectors, processed accordingly and then converted back. While for non-arithmetic operations such as cat, copy, etc., it is a straight memory copy and no data type conversion will be involved.\n- **Bfloat16 reduction**: Reduction on bfloat16 data uses float32 as accumulation type to guarantee numerical stability, e.g., sum, BatchNorm2d, MaxPool2d, etc.\n- **Channels Last optimization**: For vision models, Channels Last is the preferable memory format over Channels First from performance perspective. We have implemented fully optimized CPU kernels for all the commonly used CV modules on channels last memory format, taking care of both float32 and bfloat16.\n\n## Run Bfloat16 with Auto Mixed Precision", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "To run model on bfloat16, typically user can either explicitly convert the data and model to bfloat16, for example:\n\n```console\n# with explicit conversion\ninput = input.to(dtype=torch.bfloat16)\nmodel = model.to(dtype=torch.bfloat16)\n```\n\nor utilize torch.amp (Automatic Mixed Precision) package. The autocast instance serves as context managers or decorators that allow regions of your script to run in mixed precision, for example:\n\n```console\n# with AMP\nwith torch.autocast(device_type=\"cpu\", dtype=torch.bfloat16):\n output = model(input)\n```\n\nGenerally, the explicit conversion approach and AMP approach have similar performance. Even though, we recommend run bfloat16 models with AMP, because:\n\n- **Better user experience with automatic fallback**: If your script includes operators that don\u2019t have bfloat16 support, autocast will implicitly convert them back to float32 while the explicit converted model will give a runtime error.", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "- **Mixed data type for activation and parameters**: Unlike the explicit conversion which converts all the model parameters to bfloat16, AMP mode will run in mixed data type. To be specific, input/output will be kept in bfloat16 while parameters, e.g., weight/bias, will be kept in float32. The mixed data type of activation and parameters will help improve performance while maintaining the accuracy.\n\n## Performance Gains\n\nWe benchmarked inference performance of TorchVision models on Intel\u00ae Xeon\u00ae Platinum 8380H CPU @ 2.90GHz (codenamed Cooper Lake), single instance per socket (batch size = 2 x number of physical cores). Results show that bfloat16 has 1.4x to 2.2x performance gain over float32.\n\n

\n \n

\n\n## The performance boost of bfloat16 over float32 primarily comes from 3 aspects:", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "- The compute intensive operators take advantage of the new bfloat16 native instruction VDPBF16PS which doubles the hardware compute throughput.\n- Bfloat16 have only half the memory footprint of float32, so theoretically the memory bandwidth intensive operators will be twice faster.\n- On Channels Last, we intentionally keep the same parallelization scheme for all the memory format aware operators (can\u2019t do this on Channels First though), which increases the data locality when passing each layer\u2019s output to the next. Basically, it keeps the data closer to CPU cores while data would reside in cache anyway. And bfloat16 will have a higher cache hit rate compared with float32 in such scenarios due to smaller memory footprint.\n\n## Conclusion & Future Work", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "In this blog, we introduced recent software optimizations on bfloat16 introduced in PyTorch 1.12. Results on the 3rd Gen Intel\u00ae Xeon\u00ae Scalable processor show that bfloat16 has 1.4x to 2.2x performance gain over float32 on the TorchVision models. Further improvement is expected on the next generation of Intel\u00ae Xeon\u00ae Scalable Processors with AMX instruction support. Though the performance number for this blog is collected with TorchVision models, the benefit is broad across all topologies. And we will continue to extend the bfloat16 optimization effort to a broader scope in the future!\n\n## Acknowledgement\n\nThe results presented in this blog is a joint effort of Meta and Intel PyTorch team. Special thanks to Vitaly Fedyunin and Wei Wei from Meta who spent precious time and gave substantial assistance! Together we made one more step on the path of improving the PyTorch CPU eco system.\n\n## Reference", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "## Reference\n\n- [The bfloat16 numerical format](https://cloud.google.com/tpu/docs/bfloat16?hl=en)\n- [https://pytorch.org/docs/master/amp.html#torch.autocast](https://pytorch.org/docs/master/amp.html#torch.autocast)\n- [Intel and Facebook Accelerate PyTorch Performance with 3rd Gen Intel\u00ae Xeon\u00ae Processors and Intel\u00ae Deep Learning Boost\u2019s new BFloat16 capability](https://community.intel.com/t5/Blogs/Tech-Innovation/Artificial-Intelligence-AI/Intel-and-Facebook-Accelerate-PyTorch-Performance-with-3rd-Gen/post/1335659)", "metadata": {"source": "https://pytorch.org/blog/empowering-pytorch-on-intel-xeon-scalable-processors-with-bfloat16/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Announcing PyTorch Conference 2022\"\nauthor:\nfeatured-img: \"/assets/images/pytorch-conference-2022.png\"\n---\n\nWe are excited to announce that the PyTorch Conference returns in-person as a satellite event to [NeurlPS](https://l.workplace.com/l.php?u=https%3A%2F%2Fnips.cc%2F&h=AT3cdRwSEhyuNXpH2ptWjk-KxMxcceaYeTfflT6PEezDQ_zeUxRv1gjX7GhTQBgvZxFAR0wlSBwuhpipdMjUknMnhY5oJ5C4HjLNO40-12UnoeYALriwrvdxGfgigo8KYlWu_gRIQwlO-2r0wTnNft0whoSaOdVAxw&__tn__=-UK-R&c[0]=AT3z6QRLu8Uw48lKQ_P6FFq7ncHfjsfI16OGZvWO9kALatCY4sZcMjNzR7a4OiOG25RKVHpDX0TGutZHyM_R8Kl2s71Y3DEbq5QccmUVaSzCbcMUSc5Ms2zXHoeGxUlw1XirihAydPsX4Y1OmF6GRjqH8YFTNTFQRN3I8j2SFhR8LEUDxDmfnZ8Q7c2hXi0HeGc) (Neural Information Processing Systems) in New Orleans on Dec. 2nd.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} +{"page_content": "We changed the name from PyTorch Developer Day to PyTorch Conference to signify the turning of a new chapter as we look to the future of PyTorch, encompassing the entire PyTorch Community. This conference will bring together leading researchers, academics and developers from the Machine Learning (ML) and Deep Learning (DL) communities to join a multiple set of talks and a poster session; covering new software releases on [PyTorch](https://pytorch.org/), use cases in academia and industry, as well as ML/DL development and production trends.\n\n### EVENT OVERVIEW\n\nWhen: Dec 2nd, 2022 (In-Person and Virtual)\n\nWhere: New Orleans, Louisiana (USA) | *Virtual option as well*\n\n### SCHEDULE\n\nAll times in Central Standard.\n\n8:00-9:00 am   Registration/Check in\n\n9:00-11:20 am   Keynote & Technical Talks\n\n11:30-1:00 pm   Lunch\n\n1:00-3:00 pm   Poster Session & Breakouts\n\n3:00-4:00 pm   Community/Partner Talks\n\n4:00-5:00 pm   Panel Discussion", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} +{"page_content": "Agenda subject to change.\n\nAll talks will be livestreamed and available to the public. The in-person event will be by invitation only as space is limited. If you\u2019d like to apply to attend in person, please submit all requests [here](https://pytorchconference22.splashthat.com/).\n\n### LINKS\n\n- [Submit Content for Consideration by Sept. 30th](https://docs.google.com/forms/d/121ptOuhqhmcPev9g5Zt2Ffl-NtB_oeyFk5CWjumUVLQ/edit)\n- [Livestream event page](https://www.facebook.com/events/1562940847455759)\n- [Apply for an invitation to the in-person event](https://pytorchconference22.splashthat.com/)", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-conference-2022/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'New PyTorch library releases including TorchVision Mobile, TorchAudio I/O, and more'\nauthor: Team PyTorch \n---\n\nToday, we are announcing updates to a number of PyTorch libraries, alongside the [PyTorch 1.8 release](https://pytorch.org/blog/pytorch-1.8-released). The updates include new releases for the domain libraries including TorchVision, TorchText and TorchAudio as well as new version of TorchCSPRNG. These releases include a number of new features and improvements and, along with the PyTorch 1.8 release, provide a broad set of updates for the PyTorch community to build on and leverage.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Some highlights include:\n* **TorchVision** - Added support for PyTorch Mobile including [Detectron2Go](https://ai.facebook.com/blog/d2go-brings-detectron2-to-mobile) (D2Go), auto-augmentation of data during training, on the fly type conversion, and [AMP autocasting](https://pytorch.org/docs/stable/amp.html). \n* **TorchAudio** - Major improvements to I/O, including defaulting to sox_io backend and file-like object support. Added Kaldi Pitch feature and support for CMake based build allowing TorchAudio to better support no-Python environments.\n* **TorchText** - Updated the dataset loading API to be compatible with standard PyTorch data loading utilities.\n* **TorchCSPRNG** - Support for cryptographically secure pseudorandom number generators for PyTorch is now stable with new APIs for AES128 ECB/CTR and CUDA support on Windows.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Please note that, starting in PyTorch 1.6, features are classified as Stable, Beta, and Prototype. Prototype features are not included as part of the binary distribution and are instead available through either building from source, using nightlies or via compiler flag. You can see the detailed announcement [here](https://pytorch.org/blog/pytorch-feature-classification-changes/).\n\n\n# TorchVision 0.9.0\n### [Stable] TorchVision Mobile: Operators, Android Binaries, and Tutorial\nWe are excited to announce the first on-device support and binaries for a PyTorch domain library. We have seen significant appetite in both research and industry for on-device vision support to allow low latency, privacy friendly, and resource efficient mobile vision experiences. You can follow this [new tutorial](https://github.com/pytorch/android-demo-app/tree/master/D2Go) to build your own Android object detection app using TorchVision operators, D2Go, or your own custom operators and model.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n### [Stable] New Mobile models for Classification, Object Detection and Semantic Segmentation\nWe have added support for the MobileNetV3 architecture and provided pre-trained weights for Classification, Object Detection and Segmentation. It is easy to get up and running with these models, just import and load them as you would any ```torchvision``` model:\n```python\nimport torch\nimport torchvision\n\n# Classification\nx = torch.rand(1, 3, 224, 224)\nm_classifier = torchvision.models.mobilenet_v3_large(pretrained=True)\nm_classifier.eval()\npredictions = m_classifier(x)\n\n# Quantized Classification\nx = torch.rand(1, 3, 224, 224)\nm_classifier = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\nm_classifier.eval()\npredictions = m_classifier(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# Object Detection: Highly Accurate High Resolution Mobile Model\nx = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)\n\n# Semantic Segmentation: Highly Accurate Mobile Model\nx = torch.rand(1, 3, 520, 520)\nm_segmenter = torchvision.models.segmentation.deeplabv3_mobilenet_v3_large(pretrained=True)\nm_segmenter.eval()\npredictions = m_segmenter(x)\n```\nThese models are highly competitive with TorchVision\u2019s existing models on resource efficiency, speed, and accuracy. See our [release notes](https://github.com/pytorch/vision/releases) for detailed performance metrics.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### [Stable] AutoAugment\n[AutoAugment](https://arxiv.org/pdf/1805.09501.pdf) is a common Data Augmentation technique that can increase the accuracy of Scene Classification models. Though the data augmentation policies are directly linked to their trained dataset, empirical studies show that ImageNet policies provide significant improvements when applied to other datasets. We\u2019ve implemented 3 policies learned on the following datasets: ImageNet, CIFA10 and SVHN. These can be used standalone or mixed-and-matched with existing transforms:\n```python\nfrom torchvision import transforms\n\nt = transforms.AutoAugment()\ntransformed = t(image)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "transform=transforms.Compose([\n transforms.Resize(256),\n transforms.AutoAugment(),\n transforms.ToTensor()])\n```\n### Other New Features for TorchVision\n* [Stable] All read and decode methods in the io.image package now support:\n * Palette, Grayscale Alpha and RBG Alpha image types during PNG decoding\n * On-the-fly conversion of image from one type to the other during read\n* [Stable] WiderFace dataset\n* [Stable] Improved FasterRCNN speed and accuracy by introducing a score threshold on RPN\n* [Stable] Modulation input for DeformConv2D\n* [Stable] Option to write audio to a video file\n* [Stable] Utility to draw bounding boxes\n* [Beta] Autocast support in all Operators\nFind the full TorchVision release notes [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# TorchAudio 0.8.0\n### I/O Improvements\nWe have continued our work from the [previous release](https://github.com/pytorch/audio/releases/tag/v0.7.0) to improve TorchAudio\u2019s I/O support, including:\n* [Stable] Changing the default backend to \u201csox_io\u201d (for Linux/macOS), and updating the \u201csoundfile\u201d backend\u2019s interface to align with that of \u201csox_io\u201d. The legacy backend and interface are still accessible, though it is strongly discouraged to use them.\n* [Stable] File-like object support in both \"sox_io\" backend, \u201csoundfile\u201d backend and sox_effects.\n* [Stable] New options to change the format, encoding, and bits_per_sample when saving.\n* [Stable] Added GSM, HTK, AMB, AMR-NB and AMR-WB format support to the \u201csox_io\u201d backend.\n* [Beta] A new ```functional.apply_codec``` function which can degrade audio data by applying audio codecs supported by \u201csox_io\u201d backend in an in-memory fashion.\nHere are some examples of features landed in this release:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n# Load audio over HTTP\nwith requests.get(URL, stream=True) as response:\n waveform, sample_rate = torchaudio.load(response.raw)\n \n# Saving to Bytes buffer as 32-bit floating-point PCM\nbuffer_ = io.BytesIO()\ntorchaudio.save(\n buffer_, waveform, sample_rate,\n format=\"wav\", encoding=\"PCM_S\", bits_per_sample=16)\n \n# Apply effects while loading audio from S3\nclient = boto3.client('s3')\nresponse = client.get_object(Bucket=S3_BUCKET, Key=S3_KEY)\nwaveform, sample_rate = torchaudio.sox_effects.apply_effect_file(\n response['Body'],\n [[\"lowpass\", \"-1\", \"300\"], [\"rate\", \"8000\"]])\n \n# Apply GSM codec to Tensor\nencoded = torchaudio.functional.apply_codec(\n waveform, sample_rate, format=\"gsm\")\n```\n\nCheck out the revamped audio preprocessing tutorial, [Audio Manipulation with TorchAudio](https://pytorch.org/tutorials/beginner/audio_preprocessing_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### [Stable] Switch to CMake-based build\nIn the previous version of TorchAudio, it was utilizing CMake to build third party dependencies. Starting in 0.8.0, TorchaAudio uses CMake to build its C++ extension. This will open the door to integrate TorchAudio in non-Python environments (such as C++ applications and mobile). We will continue working on adding example applications and mobile integrations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### [Beta] Improved and New Audio Transforms\nWe have added two widely requested operators in this release: the SpectralCentroid transform and the Kaldi Pitch feature extraction (detailed in [\"A pitch extraction algorithm tuned for automatic speech recognition\"](https://ieeexplore.ieee.org/document/6854049)). We\u2019ve also exposed a normalization method to Mel transforms, and additional STFT arguments to Spectrogram. We would like to ask our community to continue to [raise feature requests](https://github.com/pytorch/audio/issues/new?assignees=&labels=&template=feature-request.md) for core audio processing features like these!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### Community Contributions\nWe had more contributions from the open source community in this release than ever before, including several completely new features. We would like to extend our sincere thanks to the community. Please check out the newly added [CONTRIBUTING.md](https://github.com/pytorch/audio/blob/master/CONTRIBUTING.md) for ways to contribute code, and remember that reporting bugs and requesting features are just as valuable. We will continue posting well-scoped work items as issues labeled \u201chelp-wanted\u201d and \u201ccontributions-welcome\u201d for anyone who would like to contribute code, and are happy to coach new contributors through the contribution process.\n\nFind the full TorchAudio release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# TorchText 0.9.0\n### [Beta] Dataset API Updates\nIn this release, we are updating TorchText\u2019s dataset API to be compatible with PyTorch data utilities, such as DataLoader, and are deprecating TorchText\u2019s custom data abstractions such as ```Field```. The updated datasets are simple string-by-string iterators over the data. For guidance about migrating from the legacy abstractions to use modern PyTorch data utilities, please refer to our [migration guide](https://github.com/pytorch/text/blob/master/examples/legacy_tutorial/migration_tutorial.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "The text datasets listed below have been updated as part of this work. For examples of how to use these datasets, please refer to our [end-to-end text classification tutorial](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html).\n* **Language modeling:** WikiText2, WikiText103, PennTreebank, EnWik9\n* **Text classification:** AG_NEWS, SogouNews, DBpedia, YelpReviewPolarity, YelpReviewFull, YahooAnswers, AmazonReviewPolarity, AmazonReviewFull, IMDB\n* **Sequence tagging:** UDPOS, CoNLL2000Chunking\n* **Translation:** IWSLT2016, IWSLT2017\n* **Question answer:** SQuAD1, SQuAD2\n\nFind the full TorchText release notes [here](https://github.com/pytorch/text/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# [Stable] TorchCSPRNG 0.2.0\nWe [released TorchCSPRNG in August 2020](https://pytorch.org/blog/torchcsprng-release-blog/), a PyTorch C++/CUDA extension that provides cryptographically secure pseudorandom number generators for PyTorch. Today, we are releasing the 0.2.0 version and designating the library as stable. This release includes a new API for encrypt/decrypt with AES128 ECB/CTR as well as CUDA 11 and Windows CUDA support.\n\nFind the full TorchCSPRNG release notes [here](https://github.com/pytorch/csprng/releases/).\n\n\n\n\n\n\n\nThanks for reading, and if you are excited about these updates and want to participate in the future of PyTorch, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch).\n\nCheers!\n\n***Team PyTorch***", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-new-library-releases/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'An overview of the ML models introduced in TorchVision v0.9'\nauthor: Team PyTorch \n---\n\nTorchVision v0.9 has been [released](https://github.com/pytorch/vision/releases) and it is packed with numerous new Machine Learning models and features, speed improvements and bug fixes. In this blog post, we provide a quick overview of the newly introduced ML models and discuss their key features and characteristics.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "Classification", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **MobileNetV3 Large & Small:** These two classification models are optimized for Mobile use-cases and are used as backbones on other Computer Vision tasks. The implementation of the new [MobileNetV3 architecture](https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenetv3.py) supports the Large & Small variants and the depth multiplier parameter as described in the [original paper](https://arxiv.org/pdf/1905.02244.pdf). We offer pre-trained weights on ImageNet for both Large and Small", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "networks with depth multiplier 1.0 and resolution 224x224. Our previous [training recipes](https://github.com/pytorch/vision/tree/master/references/classification#mobilenetv3-large--small) have been updated and can be used to easily train the models from scratch (shoutout to Ross Wightman for inspiring some of our [training configuration](https://rwightman.github.io/pytorch-image-models/training_hparam_examples/#mobilenetv3-large-100-75766-top-1-92542-top-5)). The Large variant offers a [competitive", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#classification) comparing to ResNet50 while being over 6x faster on CPU, meaning that it is a good candidate for applications where speed is important. For applications where speed is critical, one can sacrifice further accuracy for speed and use the Small variant which is 15x faster than ResNet50.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **Quantized MobileNetV3 Large:** The quantized version of MobilNetV3 Large reduces the number of parameters by 45% and it is roughly 2.5x faster than the non-quantized version while remaining competitive in [terms of accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#quantized-models). It was fitted on ImageNet using Quantization Aware Training by iterating on the non-quantized version and it can be trained from scratch using the existing [reference", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "scripts](https://github.com/pytorch/vision/tree/master/references/classification#quantized).", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "**Usage:**\n```\nmodel = torchvision.models.mobilenet_v3_large(pretrained=True)\n# model = torchvision.models.mobilenet_v3_small(pretrained=True)\n# model = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\nmodel.eval()\npredictions = model(img)\n```", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "Object Detection", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **Faster R-CNN MobileNetV3-Large FPN:** Combining the MobileNetV3 Large backbone with a Faster R-CNN detector and a Feature Pyramid Network leads to a highly accurate and fast object detector. The pre-trained weights are fitted on COCO 2017 using the provided reference [scripts](https://github.com/pytorch/vision/tree/master/references/detection#faster-r-cnn-mobilenetv3-large-fpn) and the model is 5x faster on CPU than the equivalent ResNet50 detector while remaining competitive in [terms of", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#object-detection-instance-segmentation-and-person-keypoint-detection).", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **Faster R-CNN MobileNetV3-Large 320 FPN:** This is an iteration of the previous model that uses reduced resolution (min_size=320 pixel) and sacrifices accuracy for speed. It is 25x faster on CPU than the equivalent ResNet50 detector and thus it is good for real mobile use-cases.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "**Usage:**\n```\nmodel = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True)\n# model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True)\nmodel.eval()\npredictions = model(img)\n```", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "Semantic Segmentation", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **DeepLabV3 with Dilated MobileNetV3 Large Backbone:** A dilated version of the MobileNetV3 Large backbone combined with DeepLabV3 helps us build a highly accurate and fast semantic segmentation model. The pre-trained weights are fitted on COCO 2017 using our [standard training recipes](https://github.com/pytorch/vision/tree/master/references/segmentation#deeplabv3_mobilenet_v3_large). The final model has the [same", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#semantic-segmentation) as the FCN ResNet50 but it is 8.5x faster on CPU and thus making it an excellent replacement for the majority of applications.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} -{"page_content": "* **Lite R-ASPP with Dilated MobileNetV3 Large Backbone:** We introduce the implementation of a new segmentation head called Lite R-ASPP and combine it with the dilated MobileNetV3 Large backbone to build a very fast segmentation model. The new model sacrifices some accuracy to achieve a 15x speed improvement comparing to the previously most lightweight segmentation model which was the FCN ResNet50.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} +{"page_content": "### Classification\n* **MobileNetV3 Large & Small:** These two classification models are optimized for Mobile use-cases and are used as backbones on other Computer Vision tasks. The implementation of the new [MobileNetV3 architecture](https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenetv3.py) supports the Large & Small variants and the depth multiplier parameter as described in the [original paper](https://arxiv.org/pdf/1905.02244.pdf). We offer pre-trained weights on ImageNet for both Large and Small networks with depth multiplier 1.0 and resolution 224x224. Our previous [training recipes](https://github.com/pytorch/vision/tree/master/references/classification#mobilenetv3-large--small) have been updated and can be used to easily train the models from scratch (shoutout to Ross Wightman for inspiring some of our [training configuration](https://rwightman.github.io/pytorch-image-models/training_hparam_examples/#mobilenetv3-large-100-75766-top-1-92542-top-5)). The Large variant offers a [competitive accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#classification) comparing to ResNet50 while being over 6x faster on CPU, meaning that it is a good candidate for applications where speed is important. For applications where speed is critical, one can sacrifice further accuracy for speed and use the Small variant which is 15x faster than ResNet50.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} +{"page_content": "* **Quantized MobileNetV3 Large:** The quantized version of MobilNetV3 Large reduces the number of parameters by 45% and it is roughly 2.5x faster than the non-quantized version while remaining competitive in [terms of accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#quantized-models). It was fitted on ImageNet using Quantization Aware Training by iterating on the non-quantized version and it can be trained from scratch using the existing [reference scripts](https://github.com/pytorch/vision/tree/master/references/classification#quantized).", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} +{"page_content": "**Usage:**\n```\nmodel = torchvision.models.mobilenet_v3_large(pretrained=True)\n# model = torchvision.models.mobilenet_v3_small(pretrained=True)\n# model = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\nmodel.eval()\npredictions = model(img)\n```\n### Object Detection\n* **Faster R-CNN MobileNetV3-Large FPN:** Combining the MobileNetV3 Large backbone with a Faster R-CNN detector and a Feature Pyramid Network leads to a highly accurate and fast object detector. The pre-trained weights are fitted on COCO 2017 using the provided reference [scripts](https://github.com/pytorch/vision/tree/master/references/detection#faster-r-cnn-mobilenetv3-large-fpn) and the model is 5x faster on CPU than the equivalent ResNet50 detector while remaining competitive in [terms of accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#object-detection-instance-segmentation-and-person-keypoint-detection). \n* **Faster R-CNN MobileNetV3-Large 320 FPN:** This is an iteration of the previous model that uses reduced resolution (min_size=320 pixel) and sacrifices accuracy for speed. It is 25x faster on CPU than the equivalent ResNet50 detector and thus it is good for real mobile use-cases.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} +{"page_content": "**Usage:**\n```\nmodel = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True)\n# model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True)\nmodel.eval()\npredictions = model(img)\n```\n### Semantic Segmentation\n* **DeepLabV3 with Dilated MobileNetV3 Large Backbone:** A dilated version of the MobileNetV3 Large backbone combined with DeepLabV3 helps us build a highly accurate and fast semantic segmentation model. The pre-trained weights are fitted on COCO 2017 using our [standard training recipes](https://github.com/pytorch/vision/tree/master/references/segmentation#deeplabv3_mobilenet_v3_large). The final model has the [same accuracy](https://github.com/pytorch/vision/blob/master/docs/source/models.rst#semantic-segmentation) as the FCN ResNet50 but it is 8.5x faster on CPU and thus making it an excellent replacement for the majority of applications.\n* **Lite R-ASPP with Dilated MobileNetV3 Large Backbone:** We introduce the implementation of a new segmentation head called Lite R-ASPP and combine it with the dilated MobileNetV3 Large backbone to build a very fast segmentation model. The new model sacrifices some accuracy to achieve a 15x speed improvement comparing to the previously most lightweight segmentation model which was the FCN ResNet50.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} {"page_content": "**Usage:**\n```\nmodel = torchvision.models.segmentation.deeplabv3_mobilenet_v3_large(pretrained=True)\n# model = torchvision.models.segmentation.lraspp_mobilenet_v3_large(pretrained=True)\nmodel.eval()\npredictions = model(img)\n```\nIn the near future we plan to publish an article that covers the details of how the above models were trained and discuss their tradeoffs and design choices. Until then we encourage you to try out the new models and provide your feedback.", "metadata": {"source": "https://pytorch.org/blog/ml-models-torchvision-v0.9/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing nvFuser, a deep learning compiler for PyTorch'\nauthor: Christian Sarofeen, Piotr Bialecki, Jie Jiang, Kevin Stephano, Masaki Kozuki, Neal Vaidya, Stas Bekman\nfeatured-img: \"/assets/images/introducing-nvfuser-a-deep-learning-compiler-for-pytorch-1.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser is a Deep Learning Compiler for NVIDIA GPUs that automatically just-in-time compiles fast and flexible kernels to reliably accelerate users' networks. It provides significant speedups for deep learning networks running on Volta and later CUDA accelerators by generating fast custom \u201cfusion\u201d kernels at runtime. nvFuser is specifically designed to meet the unique requirements of the PyTorch community, and it supports diverse network architectures and programs with dynamic inputs of varying shapes and", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "strides.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this blog post we\u2019ll describe nvFuser and how it\u2019s used today, show the significant performance improvements it can obtain on models from HuggingFace and TIMM, and look ahead to nvFuser in PyTorch 1.13 and beyond. If you would like to know more about how and why fusion improves the speed of training for Deep Learning networks, please see our previous talks on nvFuser from [GTC 2022](https://www.nvidia.com/en-us/on-demand/session/gtcspring22-s41958/) and [GTC", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2021](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31952/).", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser relies on a graph representation of PyTorch operations to optimize and accelerate. Since PyTorch has an eager execution model, the PyTorch operations users are running are not directly accessible as a whole program that can be optimized by a system like nvFuser. Therefore users must utilize systems built on top of nvFuser which are capable of capturing users programs and translating them into a form that is optimizable by nvFuser. These higher level systems then pass these captured operations to", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser, so that nvFuser can optimize the execution of the user\u2019s script for NVIDIA GPUs. There are three systems that capture, translate, and pass user programs to nvFuser for optimization:", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [TorchScript jit.script](https://pytorch.org/docs/stable/generated/torch.jit.script.html#torch.jit.script)\n - This system directly parses sections of an annotated python script to translate into its own representation what the user is doing. This system then applies its own version of auto differentiation to the graph, and passes sections of the subsequent forward and backwards graphs to nvFuser for optimization.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [FuncTorch](https://pytorch.org/functorch/stable/generated/functorch.compile.memory_efficient_fusion.html#functorch.compile.memory_efficient_fusion)", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- This system doesn\u2019t directly look at the user python script, instead inserting a mechanism that captures PyTorch operations as they\u2019re being run. We refer to this type of capture system as \u201ctrace program acquisition\u201d, since we\u2019re tracing what has been performed. FuncTorch doesn\u2019t perform its own auto differentiation \u2013 it simply traces PyTorch\u2019s autograd directly to get backward graphs.\n- [TorchDynamo](https://github.com/pytorch/torchdynamo)", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- TorchDynamo is another program acquisition mechanism built on top of FuncTorch. TorchDynamo parses the Python bytecode produced from the user script in order to select portions to trace with FuncTorch. The benefit of TorchDynamo is that it\u2019s able to apply decorators to a user\u2019s script, effectively isolating what should be sent to FuncTorch, making it easier for FuncTorch to successfully trace complex Python scripts.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "These systems are available for users to interact with directly while nvFuser automatically and seamlessly optimizes performance critical regions of the user\u2019s code. These systems automatically send parsed user programs to nvFuser so nvFuser can:", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. Analyze the operations being run on GPUs\n2. Plan parallelization and optimization strategies for those operations\n3. Apply those strategies in generated GPU code\n4. Runtime-compile the generated optimized GPU functions\n5. Execute those CUDA kernels on subsequent iterations", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "It is important to note nvFuser does not yet support all PyTorch operations, and there are still some scenarios that are actively being improved in nvFuser that are discussed herein. However, nvFuser does support many DL performance critical operations today, and the number of supported operations will grow in subsequent PyTorch releases. nvFuser is capable of generating highly specialized and optimized GPU functions for the operations it does have support for. This means nvFuser is able to power new", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "PyTorch systems like TorchDynamo and FuncTorch to combine the flexibility PyTorch is known for with unbeatable performance.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser Performance", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Before getting into how to use nvFuser, in this section we\u2019ll show the improvements in training speed nvFuser provides for a variety of models from the [HuggingFace Transformers](https://github.com/huggingface/transformers) and [PyTorch Image Models (TIMM)](https://github.com/rwightman/pytorch-image-models) repositories and we will discuss current gaps in nvFuser performance that are under development today. All performance numbers in this section were taken using an NVIDIA A100 40GB GPU, and used either", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "FuncTorch alone or Functorch with TorchDynamo.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "HuggingFace Transformer Benchmarks\n\nnvFuser can dramatically accelerate training of HuggingFace Transformers when combined with another important optimization (more on that in a moment). Performance improvements can be seen in Figure 1 to range between 1.12x and 1.50x across a subset of popular HuggingFace Transformer networks.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Figure 1: Performance gains of 8 training scenarios from HuggingFace\u2019s Transformer repository. First performance boost in the dark green is due to replacing the optimizer with an NVIDIA Apex fused AdamW optimizer. The light green is due to adding nvFuser. Models were run with batch size and sequence lengths of [64, 128], [8, 512], [2, 1024], [64, 128], [8, 512], [8, src_seql=512, tgt_seql=128], [8, src_seql=1024, tgt_seql=128], and [8, 512] respectively. All networks were run with Automatic Mixed Precision", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "(AMP) enabled with dtype=float16.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "While these speedups are significant, it\u2019s important to understand that nvFuser doesn\u2019t (yet) automate everything about running networks quickly. For HuggingFace Transformers, for example, it was important to use the AdamW fused optimizer from [NVIDIA\u2019s Apex repository](https://github.com/NVIDIA/apex) as the optimizer otherwise consumed a large portion of runtime. Using the fused AdamW optimizer to make the network faster exposes the next major performance bottleneck \u2014 memory bound operations. These", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "operations are optimized by nvFuser, providing another large performance boost. With the fused optimizer and nvFuser enabled, the training speed of these networks improved between 1.12x to 1.5x.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "HuggingFace Transformer models were run with [the torch.amp module](https://pytorch.org/docs/stable/amp.html). (\u201camp\u201d stands for Automated Mixed Precision, see the [\u201cWhat Every User Should Know about Mixed Precision in PyTorch\u201d](https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/) blog post for details.) An option to use nvFuser was added to HuggingFace\u2019sTrainer. If you have [TorchDynamo installed](https://github.com/pytorch/torchdynamo#requirements-and-setup) you", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "can activate it to enable nvFuser in HuggingFace by passing *torchdynamo = \u2018nvfuser\u2019* to the Trainer class.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser has great support for normalization kernels and related fusions frequently found in Natural Language Processing (NLP) models, and it is recommended users try nvFuser in their NLP workloads.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Image Models (TIMM) Benchmarks", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "nvFuser, can also significantly reduce the training time of TIMM networks, up to over 1.3x vs. eager PyTorch, and up to 1.44x vs. eager PyTorch when combined with the torch.amp module. Figure 1 shows nvFuser\u2019s speedup without torch.amp, and when torch.amp is used with the NHWC (\u201cchannels last\u201d) and NCHW (\u201cchannels first\u201d) formats. nvFuser is integrated in TIMM through FuncTorch tracing directly (without TorchDynamo) and can be used by adding the [--aot-autograd command line", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "argument](https://github.com/rwightman/pytorch-image-models/commit/ca991c1fa57373286b9876aa63370fd19f5d6032) when running the TIMM benchmark or training script.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Figure 1: The Y-axis is the performance gain nvFuser provides over not using nvFuser. A value of 1.0 means no change in perf, 2.0 would mean nvFuser is twice as fast, 0.5 would mean nvFuser takes twice the time to run. Square markers are with float16 Automatic Mixed Precision (AMP) and channels first contiguous inputs, circle markers are float32 inputs, and triangles are with float16 AMP and channels last contiguous inputs. Missing data points are due to an error being encountered when tracing.\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When running with float32 precision nvFuser provides a 1.12x geometric mean (\u201cgeomean\u201d) speedup on TIMM networks, and when running with torch.amp and \u201cchannels first\u201d it provides a 1.14x geomean speedup. However, nvFuser currently doesn\u2019t speedup torch.amp and \u201cchannels last\u201d training (a .9x geomean regression), so we recommend not using it in those cases. We are actively working on improving \u201cchannels last\u201d performance now, and soon we will have two additional optimization strategies (grid persistent", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "optimizations for channels-last normalizations and fast transposes) which we expect will provide speedups comparable to \u201cchannels first\u201d in PyTorch version 1.13 and later. Many of nvFuser\u2019s optimizations can also help in inference cases. However, in PyTorch when running inference on small batch sizes, the performance is typically limited by CPU overhead, which nvFuser can\u2019t completely remove or fix. Therefore, typically the most important optimization for inference is to enable [CUDA", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Graphs](https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/) when possible. Once CUDA Graphs is enabled, then it can also be beneficial to also enable fusion through nvFuser. Performance of inference is shown in Figure 2 and Figure 3. Inference is only run with float16 AMP as it is uncommon to run inference workloads in full float32 precision.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Figure 2: Performance gains of enabling CUDA Graphs, and CUDA Graphs with nvFuser compared to the performance of native PyTorch without CUDA Graphs and nvFuser across TIMM models with float16 AMP, channels first inputs, and a batch size of 1 and 8 respectively. There is a geomean speedup of 2.74x with CUDA Graphs and 2.71x with CUDA Graphs + nvFuser respectively. nvFuser provides a maximum regression of 0.68x and a maximum performance gain of 2.74x (relative to CUDA Graphs without nvFuser).", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Performance gain is measured relative to the average time per iteration PyTorch takes without CUDA Graphs and without nvFuser. Models are sorted by how much additional performance nvFuser is providing.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Figure 3: Performance gains of enabling CUDA Graphs, and CUDA Graphs with nvFuser compared to the performance of native PyTorch without CUDA Graphs and nvFuser across TIMM models with AMP, channels last inputs, and a batch size of 1 and 8 respectively. There is a geomean speedup of 2.29x with CUDA Graphs and 2.95x with CUDA Graphs + nvFuser respectively. nvFuser provides a maximum regression of 0.86x and a maximum performance gain of 3.82x (relative to CUDA Graphs without nvFuser). Performance gain", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "is measured relative to the average time per iteration PyTorch takes without CUDA Graphs and without nvFuser. Models are sorted by how much additional performance nvFuser is providing.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "So far nvFuser performance has not been tuned for inference workloads so its performance benefit is not consistent across all cases. However, there are still many models that benefit significantly from nvFuser during inference and we encourage users to try nvFuser in inference workloads to see if you would benefit today. Performance of nvFuser in inference workloads will improve in the future and if you\u2019re interested in nvFuser in inference workloads please reach out to us on the PyTorch forums.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Getting Started - Accelerate Your Scripts with nvFuser", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We\u2019ve created [a tutorial](https://pytorch.org/tutorials/intermediate/nvfuser_intro_tutorial.html) demonstrating how to take advantage of nvFuser to accelerate part of a standard transformer block, and how nvFuser can be used to define fast and novel operations. There are still some rough edges in nvFuser that we\u2019re working hard on improving as we\u2019ve outlined in this blog post. However we\u2019ve also demonstrated some great improvements for training speed on multiple networks in HuggingFace and TIMM and we", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "expect there are opportunities in your networks where nvFuser can help today, and many more opportunities it will help in the future.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you would like to learn more about nvFuser we recommend watching our presentations from NVIDIA\u2019s GTC conference [GTC 2022](https://www.nvidia.com/en-us/on-demand/session/gtcspring22-s41958/) and [GTC 2021](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31952/).", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing PyTorch Profiler - the new and improved performance tool'\nauthor: Maxim Lukiyanov - Principal PM at Microsoft, Guoliang Hua - Principal Engineering Manager at Microsoft, Geeta Chauhan - Partner Engineering Lead at Facebook, Gisle Dankel - Tech Lead at Facebook\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "Along with [PyTorch 1.8.1 release](https://github.com/pytorch/pytorch/releases/tag/v1.8.1), we are excited to announce PyTorch Profiler \u2013 the new and improved performance debugging profiler for PyTorch. Developed as part of a collaboration between Microsoft and Facebook, the PyTorch Profiler is an open-source tool that enables accurate and efficient performance analysis and troubleshooting for large-scale deep learning models.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "Analyzing and improving large-scale deep learning model performance is an ongoing challenge that grows in importance as the model sizes increase. For a long time, PyTorch users had a hard time solving this challenge due to the lack of available tools. There were standard performance debugging tools that provide GPU hardware level information but missed PyTorch-specific context of operations. In order to recover missed information, users needed to combine multiple tools together or manually add minimum", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "correlation information to make sense of the data. There was also the autograd profiler (```torch.autograd.profiler```) which can capture information about PyTorch operations but does not capture detailed GPU hardware-level information and cannot provide support for visualization.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "The new PyTorch Profiler (```torch.profiler```) is a tool that brings both types of information together and then builds experience that realizes the full potential of that information. This new profiler collects both GPU hardware and PyTorch related information, correlates them, performs automatic detection of bottlenecks in the model, and generates recommendations on how to resolve these bottlenecks. All of this information from the profiler is visualized for the user in TensorBoard. The new Profiler API", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "is natively supported in PyTorch and delivers the simplest experience available to date where users can profile their models without installing any additional packages and see results immediately in TensorBoard with the new PyTorch Profiler plugin. Below is the screenshot of PyTorch Profiler - automatic bottleneck detection.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "Getting started\n\nPyTorch Profiler is the next version of the PyTorch autograd profiler. It has a new module namespace ```torch.profiler``` but maintains compatibility with autograd profiler APIs. The Profiler uses a new GPU profiling engine, built using Nvidia CUPTI APIs, and is able to capture GPU kernel events with high fidelity. To profile your model training loop, wrap the code in the profiler context manager as shown below.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "```python\n with torch.profiler.profile(\n schedule=torch.profiler.schedule(\n wait=2,\n warmup=2,\n active=6,\n repeat=1),\n on_trace_ready=tensorboard_trace_handler,\n with_stack=True\n) as profiler:\n for step, data in enumerate(trainloader, 0):\n print(\"step:{}\".format(step))\n inputs, labels = data[0].to(device=device), data[1].to(device=device)\n\n outputs = model(inputs)\n loss = criterion(outputs, labels)", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "optimizer.zero_grad()\n loss.backward()\n optimizer.step()\n profiler.step()\n```\nThe ```schedule``` parameter allows you to limit the number of training steps included in the profile to reduce the amount of data collected and simplify visual analysis by focusing on what\u2019s important. The ```tensorboard_trace_handler``` automatically saves profiling results to disk for analysis in TensorBoard.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "To view results of the profiling session in TensorBoard, install PyTorch Profiler TensorBoard Plugin package.\n\n```python\npip install torch_tb_profiler\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "Visual Studio Code Integration", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "[Microsoft Visual Studio Code](https://code.visualstudio.com/) is one of the most popular code editors for Python developers and data scientists. The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) for VS Code recently added the integration of TensorBoard into the code editor, including support for the PyTorch Profiler. Once you have VS Code and the Python extension installed, you can quickly open the TensorBoard Profiler plugin by launching the Command Palette", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "using the keyboard shortcut CTRL + SHIFT + P (CMD + SHIFT + P on a Mac) and typing the \u201cLaunch TensorBoard\u201d command.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "This integration comes with a built-in lifecycle management feature. VS Code will install the TensorBoard package and the PyTorch Profiler plugin package (coming in mid-April) automatically if you don\u2019t have them on your system. VS Code will also launch TensorBoard process for you and automatically look for any TensorBoard log files within your current directory. When you\u2019re done, just close the tab and VS Code will automatically close the process. No more Terminal windows running on your system to", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "provide a backend for the TensorBoard UI! Below is PyTorch Profiler Trace View running in TensorBoard.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nLearn more about TensorBoard support in VS Code in [this blog](https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2021-release/).", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "Feedback\n\nReview [PyTorch Profiler documentation](https://pytorch.org/docs/stable/profiler.html), give Profiler a try and let us know about your experience. Provide your feedback on [PyTorch Discussion Forum](https://discuss.pytorch.org/) or file issues on [PyTorch GitHub](https://github.com/pytorch/pytorch).", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"How Disney Improved Activity Recognition Through Multimodal Approaches with PyTorch\"\nauthor: Monica Alfaro, Albert Aparicio, Francesc Guitart, Marc Junyent, Pablo Pernias, Marcel Porta, and Miquel \u00c0ngel Farr\u00e9 (former Senior Technology Manager)\nfeatured-img: 'assets/images/disney_media_logo.jpg'\n---\n\n# Introduction", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Among the many things Disney Media & Entertainment Distribution (DMED) is responsible for, is the management and distribution of a huge array of media assets including news, sports, entertainment and features, episodic programs, marketing and advertising and more.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Our team focuses on media annotation as part of DMED Technology\u2019s content platforms group. In our day-to-day work, we automatically analyze a variety of content that constantly challenges the efficiency of our machine learning workflow and the accuracy of our models.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Several of our colleagues recently discussed the workflow efficiencies that we achieved by switching to an end-to-end video analysis pipeline using PyTorch, as well as how we approach animated character recognition. We invite you to read more about both in this previous post.\n\nWhile the conversion to an end-to-end PyTorch pipeline is a solution that any company might benefit from, animated character recognition was a uniquely-Disney concept and solution.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this article we will focus on activity recognition, which is a general challenge across industries \u2014 but with some specific opportunities when leveraged in the media production field, because we can combine audio, video, and subtitles to provide a solution.\n\n# Experimenting with Multimodality", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Working on a multimodal problem adds more complexity to the usual training pipelines. Having multiple information modes for each example means that the multimodal pipeline has to have specific implementations to process each mode in the dataset. Usually after this processing step, the pipeline has to merge or fuse the outputs.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Our initial experiments in multimodality were completed using the [MMF framework](https://github.com/facebookresearch/mmf). MMF is a modular framework for vision and language multimodal research. MMF contains reference implementations of state-of-the-art vision and language models and has also powered multiple research projects at Meta AI Research (as seen in this [poster](https://assets.pytorch.org/pted2021/posters/A3.png) presented in PyTorch Ecosystem Day 2020). Along with the recent release of", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "TorchMultimodal, a PyTorch library for training state-of-the-art multimodal models at scale, MMF highlights the growing interest in Multimodal understanding.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "MMF tackles this complexity with modular management of all the elements of the pipeline through a wide set of different implementations for specific modules, ranging from the processing of the modalities to the fusion of the processed information.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In our scenario, MMF was a great entry point to experiment with multimodality. It allowed us to iterate quickly by combining audio, video and closed captioning and experiment at different levels of scale with certain multimodal models, shifting from a single GPU to TPU Pods.\n\n# Multimodal Transformers", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "With a workbench based on MMF, our initial model was based on a concatenation of features from each modality evolving to a pipeline that included a Transformer-based fusion module to combine the different input modes.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Specifically, we made use of the fusion module called MMFTransformer, developed in collaboration with the Meta AI Research team. This is an implementation based on [VisualBERT](https://arxiv.org/abs/1908.03557) for which the necessary modifications were added to be able to work with text, audio and video.\n\nDespite having decent results with the out-of-box implementation MMFTransformer, we were still far from our goal, and the Transformers-based models required more data than we had available.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Searching for less data-hungry solutions\n\nSearching for less data-hungry solutions, our team started studying [MLP-Mixer](https://arxiv.org/abs/2105.01601). This new architecture has been proposed by the Google Brain team and it provides an alternative to well established de facto architectures like convolutions or self-attention for computer vision tasks.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "MLP-Mixer\n\nThe core idea behind mixed variations consists of replacing the convolutions or self-attention mechanisms used in transformers with Multilayer Perceptrons. This change in architecture favors the performance of the model in high data regimes (especially with respect to the Transformers), while also opening some questions regarding the inductive biases hidden in the convolutions and the self-attention layers.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Those proposals perform great in solving image classification tasks by splitting the image in chunks, flattening those chunks into 1D vectors and passing them through a sequence of Mixer Layers.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Inspired by the advantages of Mixer based architectures, our team searched for parallelisms with the type of problems we try to solve in video classification: specifically, instead of a single image, we have a set of frames that need to be classified, along with audio and closed captioning in the shape of new modalities.\n\n# Activity Recognition reinterpreting the MLP-Mixer", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Our proposal takes the core idea of the [MLP-Mixer](https://arxiv.org/abs/2105.01601) \u2014 using multiple multi-layer perceptrons on a sequence and transposed sequence and extends it into a Multi Modal framework that allows us to process video, audio & text with the same architecture.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For each of the modalities, we use different extractors that will provide embeddings describing the content. Given the embeddings of each modality, the MLP-Mixer architecture solves the problem of deciding which of the modalities might be the most important, while also weighing how much each modality contributes to the final labeling.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For example, when it comes to detecting laughs, sometimes the key information is in audio or in the frames, and in some of the cases we have a strong signal in the closed caption.\n\nWe tried processing each frame separately with a ResNet34 and getting a sequence of embeddings and by using a video-specific model called R3D, both pre-trained on ImageNet and Kinetics400 respectively.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "To process the audio, we use the pretrained ResNet34, and we remove the final layers to be able to extract 2D embeddings from the audio spectrograms (for 224x224 images we end up with 7x7 embeddings).\n\n\n\n

\n \n

\n\n\n\nFor closed captioning, we are using a pre-trained BERT-large, with all layers frozen, except for the Embeddings & LayerNorms.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n\n\nOnce we have extracted the embedding from each modality, we concatenate them into a single sequence and pass it through a set of MLP-Mixer blocks; next we use average pooling & a classification head to get predictions.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Our experiments have been performed on a custom, manually labeled dataset for activity recognition with 15 classes, which we know from experiments are hard and cannot all be predicted accurately using a single modality.\n\nThese experiments have shown a significant increase in performance using our approach, especially in a low/mid-data regime (75K training samples).", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When it comes to using only Text and Audio, our experiments showed a 15 percent improvement in accuracy over using a classifier on top of the features extracted by state-of-the-art backbones.\n\nUsing Text, Audio and Video we have seen a 17 percent improvement in accuracy over using Meta AIFacebook\u2019s MMF Framework, which uses a VisualBERT-like model to combine modalities using more powerful state of the art backbones.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Currently, we extended the initial model to cover up to 55 activity classes and 45 event classes. One of the challenges we expect to improve upon in the future is to include all activities and events, even those that are less frequent.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Interpreting the MLP-Mixer mode combinations \n\nAn MLP-Mixer is a concatenation of MultiLayer Perceptrons. This can be, very roughly, approximated to a linear operation, in the sense that, once trained, the weights are fixed and the input will directly affect the output.\n\nOnce we assume that approximation, we also assume that for an input consisting of NxM numbers, we could find a NxM matrix that (when multiplied elementwise) could approximate the predictions of the MLP-Mixer for a class.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n\n\nWe will call this matrix a stencil, and if we have access to it, we can find what parts of the input embeddings are responsible for a specific prediction.\n\nYou can think of it as a punch card with holes in specific positions. Only information in those positions will pass and contribute to a specific prediction. So we can measure the intensity of the input at those positions.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n\n\nOf course, this is an oversimplification, and there won't exist a unique stencil that perfectly represents all of the contributions of the input to a class (otherwise that would mean that the problem could be solved linearly). So this should be used for visualization purposes only, not as an accurate predictor.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Once we have a set of stencils for each class, we can effortlessly measure input contribution without relying on any external visualization techniques.\n\nTo find a stencil, we can start from a \"random noise\" stencil and optimize it to maximize the activations for a specific class by just back-propagating through the MLP-Mixer.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "By doing this we can end up with many valid stencils, and we can reduce them to a few by using K-means to cluster them into similar stencils and averaging each cluster.\n\n# Using the Mixer to get the best of each world\n\nMLP-Mixer, used as an image classification model without convolutional layers, requires a lot of data, since the lack of inductive bias \u2013 one of the model's good points overall \u2013 is a weakness when it comes to working in low data domains.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When used as a way to combine information previously extracted by large pretrained backbones (as opposed to being used as a full end-to-end solution), they shine. The Mixer\u2019s strength lies in finding temporal or structural coherence between different inputs. For example, in video-related tasks we could extract embeddings from the frames using a powerful, pretrained model that understands what is going on at frame level and use the mixer to make sense of it in a sequential manner.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This way of using the Mixer allows us to work with limited amounts of data and still get better results than what was achieved with Transformers. This is because Mixers seem to be more stable during training and seem to pay attention to all the inputs, while Transformers tend to collapse and pay attention only to some modalities/parts of the sequence.\n\nAcknowledgements: We would like to thank the Meta AI Research and Partner Engineering teams for this collaboration.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Practical Quantization in PyTorch'\nauthor: Suraj Subramanian, Mark Saroufim, Jerry Zhang\nfeatured-img: ''\n---", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Quantization is a cheap and easy way to make your DNN run faster and with lower memory requirements. PyTorch offers a few different approaches to quantize your model. In this blog post, we'll lay a (quick) foundation of quantization in deep learning, and then take a look at how each technique looks like in practice. Finally we'll end with recommendations from the literature for using quantization in your workflows.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "

\n \n
\n Fig 1. PyTorch <3 Quantization\n

\n\n**Contents**\n* TOC\n{:toc}", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Fundamentals of Quantization\n\n> If someone asks you what time it is, you don't respond \"10:14:34:430705\", but you might say \"a quarter past 10\".\n\nQuantization has roots in information compression; in deep networks it refers to reducing the numerical precision of its weights and/or activations.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Overparameterized DNNs have more degrees of freedom and this makes them good candidates for information compression [[1]]. When you quantize a model, two things generally happen - the model gets smaller and runs with better efficiency. Hardware vendors explicitly allow for faster processing of 8-bit data (than 32-bit data) resulting in higher throughput. A smaller model has lower memory footprint and power consumption [[2]], crucial for deployment at the edge.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Mapping function\nThe mapping function is what you might guess - a function that maps values from floating-point to integer space. A commonly used mapping function is a linear transformation given by , where is the input and are **quantization parameters**.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "To reconvert to floating point space, the inverse function is given by . \n\n, and their difference constitutes the *quantization error*.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Quantization Parameters\nThe mapping function is parameterized by the **scaling factor** and **zero-point** . \n\n is simply the ratio of the input range to the output range \n", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "where [] is the clipping range of the input, i.e. the boundaries of permissible inputs. [] is the range in quantized output space that it is mapped to. For 8-bit quantization, the output range .", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": " acts as a bias to ensure that a 0 in the input space maps perfectly to a 0 in the quantized space. ", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Calibration", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "The process of choosing the input clipping range is known as **calibration**. The simplest technique (also the default in PyTorch) is to record the running mininmum and maximum values and assign them to and . [TensorRT](https://docs.nvidia.com/deeplearning/tensorrt/pytorch-quantization-toolkit/docs/calib.html) also uses entropy minimization (KL divergence), mean-square-error minimization, or", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "percentiles of the input range.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch, `Observer` modules ([docs](https://PyTorch.org/docs/stable/torch.quantization.html?highlight=observer#observers), [code](https://github.com/PyTorch/PyTorch/blob/748d9d24940cd17938df963456c90fa1a13f3932/torch/ao/quantization/observer.py#L88)) collect statistics on the input values and calculate the qparams . Different calibration schemes result in different quantized outputs, and it's best to empirically verify which scheme works best for your", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "application and architecture (more on that later).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.quantization.observer import MinMaxObserver, MovingAverageMinMaxObserver, HistogramObserver\nC, L = 3, 4\nnormal = torch.distributions.normal.Normal(0,1)\ninputs = [normal.sample((C, L)), normal.sample((C, L))]\nprint(inputs)\n\n# >>>>>\n# [tensor([[-0.0590, 1.1674, 0.7119, -1.1270],\n# [-1.3974, 0.5077, -0.5601, 0.0683],\n# [-0.0929, 0.9473, 0.7159, -0.4574]]]),", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "# tensor([[-0.0236, -0.7599, 1.0290, 0.8914],\n# [-1.1727, -1.2556, -0.2271, 0.9568],\n# [-0.2500, 1.4579, 1.4707, 0.4043]])]\n\nobservers = [MinMaxObserver(), MovingAverageMinMaxObserver(), HistogramObserver()]\nfor obs in observers:\n for x in inputs: obs(x) \n print(obs.__class__.__name__, obs.calculate_qparams())", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "# >>>>>\n# MinMaxObserver (tensor([0.0112]), tensor([124], dtype=torch.int32))\n# MovingAverageMinMaxObserver (tensor([0.0101]), tensor([139], dtype=torch.int32))\n# HistogramObserver (tensor([0.0100]), tensor([106], dtype=torch.int32))\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Affine and Symmetric Quantization Schemes\n**Affine or asymmetric quantization** schemes assign the input range to the min and max observed values. Affine schemes generally offer tighter clipping ranges and are useful for quantizing non-negative activations (you don't need the input range to contain negative values if your input tensors are never negative). The range is calculated as", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": ". Affine quantization leads to more computationally expensive inference when used for weight tensors [[3]].", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "**Symmetric quantization** schemes center the input range around 0, eliminating the need to calculate a zero-point offset. The range is calculated as \n. For skewed signals (like non-negative activations) this can result in bad quantization resolution because the clipping range includes values that never show up in the input (see the pyplot below).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\nact = torch.distributions.pareto.Pareto(1, 10).sample((1,1024))\nweights = torch.distributions.normal.Normal(0, 0.12).sample((3, 64, 7, 7)).flatten()\n\ndef get_symmetric_range(x):\n beta = torch.max(x.max(), x.min().abs())\n return -beta.item(), beta.item()\n\ndef get_affine_range(x):\n return x.min().item(), x.max().item()", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "def plot(plt, data, scheme):\n boundaries = get_affine_range(data) if scheme == 'affine' else get_symmetric_range(data)\n a, _, _ = plt.hist(data, density=True, bins=100)\n ymin, ymax = np.quantile(a[a>0], [0.25, 0.95])\n plt.vlines(x=boundaries, ls='--', colors='purple', ymin=ymin, ymax=ymax)\n\nfig, axs = plt.subplots(2,2)\nplot(axs[0, 0], act, 'affine')\naxs[0, 0].set_title(\"Activation, Affine-Quantized\")\n\nplot(axs[0, 1], act, 'symmetric')\naxs[0, 1].set_title(\"Activation, Symmetric-Quantized\")", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "plot(axs[1, 0], weights, 'affine')\naxs[1, 0].set_title(\"Weights, Affine-Quantized\")\n\nplot(axs[1, 1], weights, 'symmetric')\naxs[1, 1].set_title(\"Weights, Symmetric-Quantized\")\nplt.show()", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "

\n \n
Fig 2. Clipping ranges (in purple) for affine and symmetric schemes\n

\n\n\nIn PyTorch, you can specify affine or symmetric schemes while initializing the Observer. Note that not all observers support both schemes.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\nfor qscheme in [torch.per_tensor_affine, torch.per_tensor_symmetric]:\n obs = MovingAverageMinMaxObserver(qscheme=qscheme)\n for x in inputs: obs(x)\n print(f\"Qscheme: {qscheme} | {obs.calculate_qparams()}\")\n\n# >>>>>\n# Qscheme: torch.per_tensor_affine | (tensor([0.0101]), tensor([139], dtype=torch.int32))\n# Qscheme: torch.per_tensor_symmetric | (tensor([0.0109]), tensor([128]))\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Per-Tensor and Per-Channel Quantization Schemes\nQuantization parameters can be calculated for the layer's entire weight tensor as a whole, or separately for each channel. In per-tensor, the same clipping range is applied to all the channels in a layer\n\n

\n \n
Fig 3. Per-Channel uses one set of qparams for each channel. Per-tensor uses the same qparams for the entire tensor.\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "For weights quantization, symmetric-per-channel quantization provides better accuracies; per-tensor quantization performs poorly, possibly due to high variance in conv weights across channels from batchnorm folding [[3]].\n\n```python\nfrom torch.quantization.observer import MovingAveragePerChannelMinMaxObserver\nobs = MovingAveragePerChannelMinMaxObserver(ch_axis=0) # calculate qparams for all `C` channels separately\nfor x in inputs: obs(x)\nprint(obs.calculate_qparams())", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "# >>>>>\n# (tensor([0.0090, 0.0075, 0.0055]), tensor([125, 187, 82], dtype=torch.int32))\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Backend Engine\nCurrently, quantized operators run on x86 machines via the [FBGEMM backend](https://github.com/pytorch/FBGEMM), or use [QNNPACK](https://github.com/pytorch/QNNPACK) primitives on ARM machines. Backend support for server GPUs (via TensorRT and cuDNN) is coming soon. Learn more about extending quantization to custom backends: [RFC-0019](https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\nbackend = 'fbgemm' if x86 else 'qnnpack'\nqconfig = torch.quantization.get_default_qconfig(backend) \ntorch.backends.quantized.engine = backend", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "QConfig\n\nThe `QConfig` ([code](https://github.com/PyTorch/PyTorch/blob/d6b15bfcbdaff8eb73fa750ee47cef4ccee1cd92/torch/ao/quantization/qconfig.py#L165), [docs](https://pytorch.org/docs/stable/torch.quantization.html?highlight=qconfig#torch.quantization.QConfig)) NamedTuple stores the Observers and the quantization schemes used to quantize activations and weights.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Be sure to pass the Observer class (not the instance), or a callable that can return Observer instances. Use `with_args()` to override the default arguments.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\nmy_qconfig = torch.quantization.QConfig(\n activation=MovingAverageMinMaxObserver.with_args(qscheme=torch.per_tensor_affine),\n weight=MovingAveragePerChannelMinMaxObserver.with_args(qscheme=torch.qint8)\n)\n# >>>>>\n# QConfig(activation=functools.partial(, qscheme=torch.per_tensor_affine){}, weight=functools.partial(, qscheme=torch.qint8){})", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch\n\nPyTorch allows you a few different ways to quantize your model depending on\n- if you prefer a flexible but manual, or a restricted automagic process (*Eager Mode* v/s *FX Graph Mode*)\n- if qparams for quantizing activations (layer outputs) are precomputed for all inputs, or calculated afresh with each input (*static* v/s *dynamic*),\n- if qparams are computed with or without retraining (*quantization-aware training* v/s *post-training quantization*)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "FX Graph Mode automatically fuses eligible modules, inserts Quant/DeQuant stubs, calibrates the model and returns a quantized module - all in two method calls - but only for networks that are [symbolic traceable](https://PyTorch.org/docs/stable/fx.html#torch.fx.symbolic_trace). The examples below contain the calls using Eager Mode and FX Graph Mode for comparison.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "In DNNs, eligible candidates for quantization are the FP32 weights (layer parameters) and activations (layer outputs). Quantizing weights reduces the model size. Quantized activations typically result in faster inference.\n\nAs an example, the 50-layer ResNet network has ~26 million weight parameters and computes ~16 million activations in the forward pass.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Post-Training Dynamic/Weight-only Quantization \nHere the model's weights are pre-quantized; the activations are quantized on-the-fly (\"dynamic\") during inference. The simplest of all approaches, it has a one line API call in `torch.quantization.quantize_dynamic`. Currently only Linear and Recurrent (`LSTM`, `GRU`, `RNN`) layers are supported for dynamic quantization.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "**(+)** Can result in higher accuracies since the clipping range is exactly calibrated for each input [[1]].\n \n **(+)** Dynamic quantization is preferred for models like LSTMs and Transformers where writing/retrieving the model's weights from memory dominate bandwidths [[4]]. \n \n **(-)** Calibrating and quantizing the activations at each layer during runtime can add to the compute overhead. \n\n```python\nimport torch\nfrom torch import nn", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "# toy model\nm = nn.Sequential(\n nn.Conv2d(2, 64, (8,)),\n nn.ReLU(),\n nn.Linear(16,10),\n nn.LSTM(10, 10))\n\nm.eval()", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "EAGER MODE\nfrom torch.quantization import quantize_dynamic\nmodel_quantized = quantize_dynamic(\n model=m, qconfig_spec={nn.LSTM, nn.Linear}, dtype=torch.qint8, inplace=False\n)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "FX MODE\nfrom torch.quantization import quantize_fx\nqconfig_dict = {\"\": torch.quantization.default_dynamic_qconfig} # An empty key denotes the default applied to all modules\nmodel_prepared = quantize_fx.prepare_fx(m, qconfig_dict)\nmodel_quantized = quantize_fx.convert_fx(model_prepared)\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Post-Training Static Quantization (PTQ)\nPTQ also pre-quantizes model weights but instead of calibrating activations on-the-fly, the clipping range is pre-calibrated and fixed (\"static\") using validation data. Activations stay in quantized precision between operations during inference. About 100 mini-batches of representative data are sufficient to calibrate the observers [[2]]. The examples below use random data in calibration for convenience - using that in your application will result in bad qparams.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "

\n \"PTQ\n
\n Fig 4. Steps in Post-Training Static Quantization\n

\n\n\n[Module fusion](https://pytorch.org/tutorials/recipes/fuse.html) combines multiple sequential modules (eg: `[Conv2d, BatchNorm, ReLU]`) into one. Fusing modules means the compiler needs to only run one kernel instead of many; this speeds things up and improves accuracy by reducing quantization error.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "**(+)** Static quantization has faster inference than dynamic quantization because it eliminates the float<->int conversion costs between layers. \n\n**(-)** Static quantized models may need regular re-calibration to stay robust against distribution-drift.\n\n\n```python\n# Static quantization of a model consists of the following steps:", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "# Fuse modules\n# Insert Quant/DeQuant Stubs\n# Prepare the fused module (insert observers before and after layers)\n# Calibrate the prepared module (pass it representative data)\n# Convert the calibrated module (replace with quantized version)\n\nimport torch\nfrom torch import nn\nimport copy\n\nbackend = \"fbgemm\" # running on a x86 CPU. Use \"qnnpack\" if running on ARM.\n\nmodel = nn.Sequential(\n nn.Conv2d(2,64,3),\n nn.ReLU(),\n nn.Conv2d(64, 128, 3),\n nn.ReLU()\n)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "EAGER MODE\nm = copy.deepcopy(model)\nm.eval()\n\"\"\"Fuse\n- Inplace fusion replaces the first module in the sequence with the fused module, and the rest with identity modules\n\"\"\"\ntorch.quantization.fuse_modules(m, ['0','1'], inplace=True) # fuse first Conv-ReLU pair\ntorch.quantization.fuse_modules(m, ['2','3'], inplace=True) # fuse second Conv-ReLU pair\n\n\"\"\"Insert stubs\"\"\"\nm = nn.Sequential(torch.quantization.QuantStub(), \n *m, \n torch.quantization.DeQuantStub())", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "\"\"\"Prepare\"\"\"\nm.qconfig = torch.quantization.get_default_qconfig(backend)\ntorch.quantization.prepare(m, inplace=True)\n\n\"\"\"Calibrate\n- This example uses random data for convenience. Use representative (validation) data instead.\n\"\"\"\nwith torch.inference_mode():\n for _ in range(10):\n x = torch.rand(1,2, 28, 28)\n m(x)\n \n\"\"\"Convert\"\"\"\ntorch.quantization.convert(m, inplace=True)\n\n\"\"\"Check\"\"\"\nprint(m[[1]].weight().element_size()) # 1 byte instead of 4 bytes for FP32", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "FX GRAPH\nfrom torch.quantization import quantize_fx\nm = copy.deepcopy(model)\nm.eval()\nqconfig_dict = {\"\": torch.quantization.get_default_qconfig(backend)}\n# Prepare\nmodel_prepared = quantize_fx.prepare_fx(m, qconfig_dict)\n# Calibrate - Use representative (validation) data.\nwith torch.inference_mode():\n for _ in range(10):\n x = torch.rand(1,2,28, 28)\n model_prepared(x)\n# quantize\nmodel_quantized = quantize_fx.convert_fx(model_prepared)\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Quantization-aware Training (QAT)\n

\n \"QAT\n
\n Fig 5. Steps in Quantization-Aware Training\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "The PTQ approach is great for large models, but accuracy suffers in smaller models [[6]]. This is of course due to the loss in numerical precision when adapting a model from FP32 to the INT8 realm *(Figure 6(a))*. QAT tackles this by including this quantization error in the training loss, thereby training an INT8-first model.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "

\n \"Fig.\n
\n Fig 6. Comparison of PTQ and QAT convergence [3]\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "All weights and biases are stored in FP32, and backpropagation happens as usual. However in the forward pass, quantization is internally simulated via `FakeQuantize` modules. They are called fake because they quantize and immediately dequantize the data, adding quantization noise similar to what might be encountered during quantized inference. The final loss thus accounts for any expected quantization errors. Optimizing on this allows the model to identify a wider region in the loss function *(Figure", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "6(b))*, and identify FP32 parameters such that quantizing them to INT8 does not significantly affect accuracy.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "

\n \"Fake\n
Fig 7. Fake Quantization in the forward and backward pass \n
Image source: https://developer.nvidia.com/blog/achieving-fp32-accuracy-for-int8-inference-using-quantization-aware-training-with-tensorrt\n

\n\n**(+)** QAT yields higher accuracies than PTQ.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "**(+)** Qparams can be learned during model training for more fine-grained accuracy (see [LearnableFakeQuantize](https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/_learnable_fake_quantize.py))\n\n**(-)** Computational cost of retraining a model in QAT can be several hundred epochs [[1]]\n\n\n```python\n# QAT follows the same steps as PTQ, with the exception of the training loop before you actually convert the model to its quantized version\n\nimport torch\nfrom torch import nn", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "backend = \"fbgemm\" # running on a x86 CPU. Use \"qnnpack\" if running on ARM.\n\nm = nn.Sequential(\n nn.Conv2d(2,64,8),\n nn.ReLU(),\n nn.Conv2d(64, 128, 8),\n nn.ReLU()\n)\n\n\"\"\"Fuse\"\"\"\ntorch.quantization.fuse_modules(m, ['0','1'], inplace=True) # fuse first Conv-ReLU pair\ntorch.quantization.fuse_modules(m, ['2','3'], inplace=True) # fuse second Conv-ReLU pair", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "\"\"\"Insert stubs\"\"\"\nm = nn.Sequential(torch.quantization.QuantStub(), \n *m, \n torch.quantization.DeQuantStub())\n\n\"\"\"Prepare\"\"\"\nm.train()\nm.qconfig = torch.quantization.get_default_qconfig(backend)\ntorch.quantization.prepare_qat(m, inplace=True)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "\"\"\"Training Loop\"\"\"\nn_epochs = 10\nopt = torch.optim.SGD(m.parameters(), lr=0.1)\nloss_fn = lambda out, tgt: torch.pow(tgt-out, 2).mean()\nfor epoch in range(n_epochs):\n x = torch.rand(10,2,24,24)\n out = m(x)\n loss = loss_fn(out, torch.rand_like(out))\n opt.zero_grad()\n loss.backward()\n opt.step()\n\n\"\"\"Convert\"\"\"\nm.eval()\ntorch.quantization.convert(m, inplace=True)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Sensitivity Analysis", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Not all layers respond to quantization equally, some are more sensitive to precision drops than others. Identifying the optimal combination of layers that minimizes accuracy drop is time-consuming, so [[3]] suggest a one-at-a-time sensitivity analysis to identify which layers are most sensitive, and retaining FP32 precision on those. In their experiments, skipping just 2 conv layers (out of a total 28 in MobileNet v1) give them near-FP32 accuracy. Using FX Graph Mode, we can create custom qconfigs to do", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "this easily:", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "```python\n# ONE-AT-A-TIME SENSITIVITY ANALYSIS \n\nfor quantized_layer, _ in model.named_modules():\n print(\"Only quantizing layer: \", quantized_layer)\n\n # The module_name key allows module-specific qconfigs. \n qconfig_dict = {\"\": None, \n \"module_name\":[(quantized_layer, torch.quantization.get_default_qconfig(backend))]}\n\n model_prepared = quantize_fx.prepare_fx(model, qconfig_dict)\n # calibrate\n model_quantized = quantize_fx.convert_fx(model_prepared)\n # evaluate(model)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Another approach is to compare statistics of the FP32 and INT8 layers; commonly used metrics for these are SQNR (Signal to Quantized Noise Ratio) and Mean-Squre-Error. Such a comparative analysis may also help in guiding further optimizations. \n\n

\n \"Fig\n
\n Fig 8. Comparing model weights and activations\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "PyTorch provides tools to help with this analysis under the Numeric Suite. Learn more about using Numeric Suite from the [full tutorial](https://pytorch.org/tutorials/prototype/numeric_suite_tutorial.html).\n\n```python\n# extract from https://pytorch.org/tutorials/prototype/numeric_suite_tutorial.html\nimport torch.quantization._numeric_suite as ns\n\ndef SQNR(x, y):\n # Higher is better\n Ps = torch.norm(x)\n Pn = torch.norm(x-y)\n return 20*torch.log10(Ps/Pn)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "wt_compare_dict = ns.compare_weights(fp32_model.state_dict(), int8_model.state_dict())\nfor key in wt_compare_dict:\n print(key, compute_error(wt_compare_dict[key]['float'], wt_compare_dict[key]['quantized'].dequantize()))\n\nact_compare_dict = ns.compare_model_outputs(fp32_model, int8_model, input_data)\nfor key in act_compare_dict:\n print(key, compute_error(act_compare_dict[key]['float'][0], act_compare_dict[key]['quantized'][0].dequantize()))", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Recommendations for your workflow\n

\n \"Suggested\n
\n Fig 9. Suggested quantization workflow\n

\n Click for larger image ", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "Points to note\n - Large (10M+ parameters) models are more robust to quantization error. [[2]]\n - Quantizing a model from a FP32 checkpoint provides better accuracy than training an INT8 model from scratch.[[2]]\n - Profiling the model runtime is optional but it can help identify layers that bottleneck inference.\n - Dynamic Quantization is an easy first step, especially if your model has many Linear or Recurrent layers.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "- Use symmetric-per-channel quantization with `MinMax` observers for quantizing weights. Use affine-per-tensor quantization with `MovingAverageMinMax` observers for quantizing activations[[2], [3]]\n - Use metrics like SQNR to identify which layers are most suscpetible to quantization error. Turn off quantization on these layers.\n - Use QAT to fine-tune for around 10% of the original training schedule with an annealing learning rate schedule starting at 1% of the initial training learning rate. [[3]]", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "- If the above workflow didn't work for you, we want to know more. Post a thread with details of your code (model architecture, accuracy metric, techniques tried). Feel free to cc me [@suraj.pt](https://discuss.pytorch.org/u/suraj.pt/).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "That was a lot to digest, congratulations for sticking with it! Next, we'll take a look at quantizing a \"real-world\" model that uses dynamic control structures (if-else, loops). These elements disallow symbolic tracing a model, which makes it a bit tricky to directly quantize the model out of the box. In the next post of this series, we'll get our hands dirty on a model that is chock full of loops and if-else blocks, and even uses third-party libraries in the `forward` call.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "We'll also cover a cool new feature in PyTorch Quantization called Define-by-Run, that tries to ease this constraint by needing only subsets of the model's computational graph to be free of dynamic flow. Check out the [Define-by-Run poster at PTDD'21](https://s3.amazonaws.com/assets.pytorch.org/ptdd2021/posters/C8.png) for a preview.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "References\n[[1]] Gholami, A., Kim, S., Dong, Z., Yao, Z., Mahoney, M. W., & Keutzer, K. (2021). A survey of quantization methods for efficient neural network inference. arXiv preprint arXiv:2103.13630.\n\n[[2]] Krishnamoorthi, R. (2018). Quantizing deep convolutional networks for efficient inference: A whitepaper. arXiv preprint arXiv:1806.08342.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "[[3]] Wu, H., Judd, P., Zhang, X., Isaev, M., & Micikevicius, P. (2020). Integer quantization for deep learning inference: Principles and empirical evaluation. arXiv preprint arXiv:2004.09602.\n\n[[4]] PyTorch Quantization Docs\n\n\n[1]: https://arxiv.org/pdf/2103.13630.pdf\n[2]: https://arxiv.org/pdf/1806.08342.pdf\n[3]: https://arxiv.org/abs/2004.09602\n[4]: https://pytorch.org/docs/stable/quantization.html#prototype-fx-graph-mode-quantization", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Towards Reproducible Research with PyTorch Hub'\nauthor: Team PyTorch\nredirect_from: /2019/06/10/pytorch_hub.html\n---", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Reproducibility is an essential requirement for many fields of research including those based on machine learning techniques. However, many machine learning publications are either not reproducible or are difficult to reproduce. With the continued growth in the number of research publications, including tens of thousands of papers now hosted on arXiv and submissions to conferences at an all time high, research reproducibility is more important than ever. While many of these publications are accompanied by", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "code as well as trained models which is helpful but still leaves a number of steps for users to figure out for themselves.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the availability of PyTorch Hub, a simple API and workflow that provides the basic building blocks for improving machine learning research reproducibility. PyTorch Hub consists of a pre-trained model repository designed specifically to facilitate research reproducibility and enable new research. It also has built-in support for [Colab](https://colab.research.google.com/), integration with [*Papers With Code*](https://paperswithcode.com/) and currently contains a broad set of", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "models that include Classification and Segmentation, Generative, Transformers, etc.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "[Owner] Publishing models", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Hub supports the publication of pre-trained models (model definitions and pre-trained weights) to a GitHub repository by adding a simple ```hubconf.py``` file.\nThis provides an enumeration of which models are to be supported and a list of dependencies needed to run the models.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Examples can be found in the [torchvision](https://github.com/pytorch/vision/blob/master/hubconf.py), [huggingface-bert](https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/hubconf.py) and [gan-model-zoo](https://github.com/facebookresearch/pytorch_GAN_zoo) repositories.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Let us look at the simplest case: `torchvision`'s `hubconf.py`:\n\n```python\n# Optional list of dependencies required by the package\ndependencies = ['torch']", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "from torchvision.models.alexnet import alexnet\nfrom torchvision.models.densenet import densenet121, densenet169, densenet201, densenet161\nfrom torchvision.models.inception import inception_v3\nfrom torchvision.models.resnet import resnet18, resnet34, resnet50, resnet101, resnet152,\\\nresnext50_32x4d, resnext101_32x8d\nfrom torchvision.models.squeezenet import squeezenet1_0, squeezenet1_1\nfrom torchvision.models.vgg import vgg11, vgg13, vgg16, vgg19, vgg11_bn, vgg13_bn, vgg16_bn, vgg19_bn", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "from torchvision.models.segmentation import fcn_resnet101, deeplabv3_resnet101\nfrom torchvision.models.googlenet import googlenet\nfrom torchvision.models.shufflenetv2 import shufflenet_v2_x0_5, shufflenet_v2_x1_0\nfrom torchvision.models.mobilenet import mobilenet_v2", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "In `torchvision`, the models have the following properties:\n- Each model file can function and be executed independently\n- They dont require any package other than PyTorch (encoded in `hubconf.py` as `dependencies['torch']`)\n- They dont need separate entry-points, because the models when created, work seamlessly out of the box\n\nMinimizing package dependencies reduces the friction for users to load your model for immediate experimentation.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "A more involved example is HuggingFace's BERT models. Here is their `hubconf.py`\n\n```python\ndependencies = ['torch', 'tqdm', 'boto3', 'requests', 'regex']\n\nfrom hubconfs.bert_hubconf import (\n bertTokenizer,\n bertModel,\n bertForNextSentencePrediction,\n bertForPreTraining,\n bertForMaskedLM,\n bertForSequenceClassification,\n bertForMultipleChoice,\n bertForQuestionAnswering,\n bertForTokenClassification\n)", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Each model then requires an entrypoint to be created. Here is a code snippet to specify an entrypoint of the ```bertForMaskedLM``` model, which returns the pre-trained model weights.\n\n```python\ndef bertForMaskedLM(*args, **kwargs):\n \"\"\"\n BertForMaskedLM includes the BertModel Transformer followed by the\n pre-trained masked language modeling head.\n Example:\n ...\n \"\"\"\n model = BertForMaskedLM.from_pretrained(*args, **kwargs)\n return model", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "These entry-points can serve as wrappers around complex model factories. They can give a clean and consistent help docstring, have logic to support downloading of pretrained weights (for example via `pretrained=True`) or have additional hub-specific functionality such as visualization.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "With a `hubconf.py` in place, you can send a pull request based on the template [here](https://github.com/pytorch/hub/blob/master/docs/template.md).\nOur goal is to curate high-quality, easily-reproducible, maximally-beneficial models for research reproducibility.\nHence, we may work with you to refine your pull request and in some cases reject some low-quality models to be published.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Once we accept your pull request, your model will soon appear on [Pytorch hub webpage](https://pytorch.org/hub) for all users to explore.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "[User] Workflow\n\nAs a user, PyTorch Hub allows you to follow a few simple steps and do things like: 1) explore available models; 2) load a model; and 3) understand what methods are available for any given model. Let's walk through some examples of each.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Explore available entrypoints.\n\nUsers can list all available entrypoints in a repo using the ```torch.hub.list()``` API.\n\n```python\n>>> torch.hub.list('pytorch/vision')\n>>>\n['alexnet',\n'deeplabv3_resnet101',\n'densenet121',\n...\n'vgg16',\n'vgg16_bn',\n'vgg19',\n 'vgg19_bn']", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Note that PyTorch Hub also allows auxillary entrypoints (other than pretrained models), e.g. ```bertTokenizer``` for preprocessing in the [BERT](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_bert/) models, to make the user workflow smoother.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Load a model\n\nNow that we know which models are available in the Hub, users can load a model entrypoint using the ```torch.hub.load()``` API. This only requires a single command without the need to install a wheel. In addition the ```torch.hub.help()``` API can provide useful information about how to instantiate the model.\n\n```python\nprint(torch.hub.help('pytorch/vision', 'deeplabv3_resnet101'))\nmodel = torch.hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "It is also common that repo owners will want to continually add bug fixes or performance improvements. PyTorch Hub makes it super simple for users to get the latest update by calling:\n\n```python\nmodel = torch.hub.load(..., force_reload=True)", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "We believe this will help to alleviate the burden of repetitive package releases by repo owners and instead allow them to focus more on their research.\nIt also ensures that, as a user, you are getting the freshest available models.\n\nOn the contrary, stability is important for users. Hence, some model owners serve them from a specificed branch or tag, rather than the `master` branch, to ensure stability of the code.\nFor example, `pytorch_GAN_zoo` serves them from the `hub` branch:", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "```python\nmodel = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub', 'DCGAN', pretrained=True, useGPU=False)", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Note that the ```*args```, ```**kwargs``` passed to `hub.load()` are used to *instantiate* a model. In the above example, `pretrained=True` and `useGPU=False` are given to the model's entrypoint.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Explore a loaded model\n\nOnce you have a model from PyTorch Hub loaded, you can use the following workflow to find out the available methods that are supported as well as understand better what arguments are requires to run it.\n\n\n```dir(model)``` to see all available methods of the model. Let's take a look at `bertForMaskedLM`'s available methods.\n\n```python\n>>> dir(model)\n>>>\n['forward'\n...\n'to'\n'state_dict',\n]", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "```help(model.forward)``` provides a view into what arguments are required to make your loaded model run\n\n```python\n>>> help(model.forward)\n>>>\nHelp on method forward in module pytorch_pretrained_bert.modeling:\nforward(input_ids, token_type_ids=None, attention_mask=None, masked_lm_labels=None)\n...", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Have a closer look at the [BERT](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_bert/) and [DeepLabV3](https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/) pages, where you can see how these models can be used once loaded.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Other ways to explore\n\nModels available in PyTorch Hub also support both [Colab](https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/facebookresearch_pytorch-gan-zoo_pgan.ipynb) and are directly linked on [Papers With Code](https://paperswithcode.com/) and you can get started with a single click. [Here](https://paperswithcode.com/paper/densely-connected-convolutional-networks) is a good example to get started with (shown below).", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "Additional resources:\n\n* PyTorch Hub API documentation can be found [here](https://pytorch.org/docs/stable/hub.html).\n* Submit a model [here](https://github.com/pytorch/hub) for publication in PyTorch Hub.\n* Go to [https://pytorch.org/hub](https://pytorch.org/hub) to learn more about the available models.\n* Look for more models to come on [paperswithcode.com](https://paperswithcode.com/).", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "A BIG thanks to the folks at HuggingFace, the PapersWithCode team, fast.ai and Nvidia as well as Morgane Riviere (FAIR Paris) and lots of others for helping bootstrap this effort!!\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "FAQ:\n\n**Q: If we would like to contribute a model that is already in the Hub but perhaps mine has better accuracy, should I still contribute?**\n\n\nA: Yes!! A next step for Hub is to implement an upvote/downvote system to surface the best models.\n\n**Q: Who hosts the model weights for PyTorch Hub?**", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "A: You, as the contributor, are responsible to host the model weights. You can host your model in your favorite cloud storage or, if it fits within the limits, on GitHub. If it is not within your means to host the weights, check with us via opening an issue on the hub repository.\n\n**Q: What if my model is trained on private data? Should I still contribute this model?**", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "A: No! PyTorch Hub is centered around open research and that extends to the usage of open datasets to train these models on. If a pull request for a proprietary model is submitted, we will kindly ask that you resubmit a model trained on something open and available.\n\n**Q: Where are my downloaded models saved?**", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "A: We follow the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) and adhere to common standards around cached files and directories.\n\nThe locations are used in the order of:\n\n* Calling ```hub.set_dir()```\n* ```$TORCH_HOME/hub```, if environment variable ```TORCH_HOME``` is set.\n* ```$XDG_CACHE_HOME/torch/hub```, if environment variable ```XDG_CACHE_HOME``` is set.\n* ```~/.cache/torch/hub```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'The road to 1.0: production ready PyTorch'\nauthor: The PyTorch Team\nredirect_from: /2018/05/02/road-to-1.0.html\n---", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "We would like to give you a preview of the roadmap for PyTorch 1.0 , the next release of PyTorch. Over the last year, we've had 0.2, 0.3 and 0.4 transform PyTorch from a [Torch+Chainer]-like interface into something cleaner, adding double-backwards, numpy-like functions, advanced indexing and removing Variable boilerplate. At this time, we're confident that the API is in a reasonable and stable state to confidently release a 1.0.\n\nHowever, 1.0 isn't just about stability of the interface.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "One of PyTorch's biggest strengths is its first-class Python integration, imperative style, simplicity of the API and options. These are aspects that make PyTorch good for research and hackability.\n\nOne of its biggest downsides has been production-support. What we mean by production-support is the countless things one has to do to models to run them efficiently at massive scale:", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "- exporting to C++-only runtimes for use in larger projects\n- optimizing mobile systems on iPhone, Android, Qualcomm and other systems\n- using more efficient data layouts and performing kernel fusion to do faster inference (saving 10% of speed or memory at scale is a big win)\n- quantized inference (such as 8-bit inference)", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Startups, large companies and anyone who wants to build a product around PyTorch have asked for production support. At Facebook (the largest stakeholder for PyTorch) we have Caffe2, which has been the production-ready platform, running in our datacenters and shipping to more than 1 billion phones spanning eight generations of iPhones and six generations of Android CPU architectures. It has server-optimized inference on Intel / ARM, TensorRT support, and all the necessary bits for production. Considering all", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "this value locked-in to a platform that the PyTorch team works quite closely with, **we decided to marry PyTorch and Caffe2 which gives the production-level readiness for PyTorch**.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Supporting production features without adding usability issues for our researchers and end-users needs creative solutions.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Production != Pain for researchers\n\nAdding production capabilities involves increasing the API complexity and number of configurable options for models. One configures memory-layouts (NCHW vs NHWC vs N,C/32,H,W,32, each providing different performance characteristics), quantization (8-bit? 3-bit?), fusion of low-level kernels (you used a Conv + BatchNorm + ReLU, let's fuse them into a single kernel), separate backend options (MKLDNN backend for a few layers and NNPACK backend for other layers), etc.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch's central goal is to provide a great platform for research and hackability. So, while we add all these optimizations, we've been working with a hard design constraint to never trade these off against usability.\n\nTo pull this off, we are introducing `torch.jit`, a just-in-time (JIT) compiler that at runtime takes your PyTorch models and rewrites them to run at production-efficiency. The JIT compiler can also export your model to run in a C++-only runtime based on Caffe2 bits.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "> In 1.0, your code continues to work as-is, we're not making any big changes to the existing API.\n\nMaking your model production-ready is an opt-in annotation, which uses the `torch.jit` compiler to export your model to a Python-less environment, and improving its performance. Let's walk through the JIT compiler in detail.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "`torch.jit`: A JIT-compiler for your models", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "We strongly believe that it's hard to match the productivity you get from specifying your models directly as idiomatic Python code. This is what makes PyTorch so flexible, but it also means that PyTorch pretty much never knows the operation you'll run next. This however is a big blocker for export/productionization and heavyweight automatic performance optimizations because they need full upfront knowledge of how the computation will look before it even gets executed.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "We provide two opt-in ways of recovering this information from your code, one based on tracing native python code and one based on compiling a subset of the python language annotated into a python-free intermediate representation. After thorough discussions we concluded that they're both going to be useful in different contexts, and as such you will be able to mix and match them freely.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Tracing Mode", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch tracer, `torch.jit.trace`, is a function that records all the native PyTorch operations performed in a code region, along with the data dependencies between them. In fact, PyTorch has had a tracer since 0.3, which has been used for exporting models through ONNX. What changes now, is that you no longer necessarily need to take the trace and run it elsewhere - PyTorch can re-execute it for you, using a carefully designed high-performance C++ runtime. As we develop PyTorch 1.0 this runtime will", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "integrate all the optimizations and hardware integrations that Caffe2 provides.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "The biggest benefit of this approach is that it doesn't really care how your Python code is structured \u2014 you can trace through generators or coroutines, modules or pure functions. Since we only record native PyTorch operators, these details have no effect on the trace recorded. This behavior, however, is a double-edged sword. For example, if you have a loop in your model, it will get unrolled in the trace, inserting a copy of the loop body for as many times as the loop ran. This opens up opportunities for", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "zero-cost abstraction (e.g. you can loop over modules, and the actual trace will be loop-overhead free!), but on the other hand this will also affect data dependent loops (think of e.g. processing sequences of varying lengths), effectively hard-coding a single length into the trace.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "For networks that do not contain loops and if statements, tracing is non-invasive and is robust enough to handle a wide variety of coding styles. This code example illustrates what tracing looks like:", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "```python\n# This will run your nn.Module or regular Python function with the example\n# input that you provided. The returned callable can be used to re-execute\n# all operations that happened during the example run, but it will no longer\n# use the Python interpreter.\nfrom torch.jit import trace\ntraced_model = trace(model, example_input=input)\ntraced_fn = trace(fn, example_input=input)", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "# The training loop doesn't change. Traced model behaves exactly like an\n# nn.Module, except that you can't edit what it does or change its attributes.\n# Think of it as a \"frozen module\".\nfor input, target in data_loader:\n loss = loss_fn(traced_model(input), target)\n```", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Script Mode\n\nTracing mode is a great way to minimize the impact on your code, but we're also very excited about the models that fundamentally make use of control flow such as RNNs. Our solution to this is a scripting mode.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "In this case you write out a regular Python function, except that you can no longer use certain more complicated language features. Once you isolated the desired functionality, you let us know that you'd like the function to get compiled by decorating it with an `@script` decorator. This annotation will transform your python function directly into our high-performance C++ runtime. This lets us recover all the PyTorch operations along with loops and conditionals. They will be embedded into our internal", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "representation of this function, and will be accounted for every time this function is run.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.jit import script\n\n@script\ndef rnn_loop(x):\n hidden = None\n for x_t in x.split(1):\n x, hidden = model(x, hidden)\n return x\n```", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Optimization and Export\n\nRegardless of whether you use tracing or `@script`, the result is a python-free representation of your model, which can be used to optimize the model or to export the model from python for use in production environments.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Extracting bigger segments of the model into an intermediate representation makes it possible to do sophisticated whole-program optimizations and to offload computation to specialized AI accelerators which operate on graphs of computation. We have already been developing the beginnings of these optimizations, including passes that fuse GPU operations together to improve the performance of smaller RNN models.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "It also allows us to use existing high-performance backends available in Caffe2 today to run the model efficiently. Additionally, @script functions (and modules!) can be fully exported to ONNX in a way that retains their dynamic nature, such that you can easily run them in a Python-free environment using the model executors from Caffe2 or by transferring the model to any other framework supporting ONNX.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Usability\n\n**We care deeply about maintaining our current level of usability and we know that execution of the code not directly in Python leads to harder debugging, but this is something that we think about a lot, and we're making sure that you're not getting locked in to a completely different programming language.**", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "First, we follow the principle of pay for what you use \u2014 if you don't need to optimize or export your model, you do not have to use these new features and won't see any downsides. Furthermore, use of traced or @script modules/functions can be done incrementally. For instance, all of these behaviors are allowed: You can trace part of your model and use the trace in a larger non-traced model. You can use tracing for 90% of your model, and use @script for the one sub-module that actually has some control flow", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "in it. You can write a function using @script and have it call a native python function. If something appears incorrect in an @script function, you can remove the annotation and the code will execute in native python where it is easy to debug using your favorite tools and methods. Think of tracing and @script like type annotations using MyPy or TypeScript \u2014 each additional annotation can be tested incrementally, and none are required until you want to optimize or productionize.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Most importantly, these modes will be built into the core of PyTorch so that mixing and matching them with your existing code can happen seamlessly.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "_Note: The name JIT for these components is a bit of a misnomer and comes from historical reasons. The tracing/function execution in PyTorch started out as an optimizing JIT compiler that generated fused CUDA kernels but then grew to encompass optimization, @script, and export. When it is ready for release we will likely rename this functionality to the hybrid frontend, but we wanted to present it here as it is named in the code so that you can follow along as we develop it._", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Other changes and improvements\n\nProduction support is the big feature for 1.0, but we will continue optimizing and fixing other parts of PyTorch as course of the standard release process.\n\nOn the backend side of things, PyTorch will see some changes, which might affect user-written C and C++ extensions. We are replacing (or refactoring) the backend ATen library to incorporate features and optimizations from Caffe2.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "Last Words\n\nWe aim to release 1.0 some time during the summer. You can follow-along our progress on the [Pull Requests](https://github.com/pytorch/pytorch/pulls) page.\n\nYou can read this from the perspective of the Caffe2 project at: [https://caffe2.ai/blog/2018/05/02/Caffe2_PyTorch_1_0.html](https://caffe2.ai/blog/2018/05/02/Caffe2_PyTorch_1_0.html)", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch adds new tools and libraries, welcomes Preferred Networks to its community'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "PyTorch continues to be used for the latest state-of-the-art research on display at the NeurIPS conference next week, making up nearly [70% of papers](https://chillee.github.io/pytorch-vs-tensorflow/) that cite a framework. In addition, we\u2019re excited to welcome Preferred Networks, the maintainers of the Chainer framework, to the PyTorch community. Their teams are moving fully over to PyTorch for developing their ML capabilities and services.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "This growth underpins PyTorch\u2019s focus on building for the needs of the research community, and increasingly, supporting the full workflow from research to production deployment. To further support researchers and developers, we\u2019re launching a number of new tools and libraries for large scale computer vision and elastic fault tolerant training. Learn more on GitHub and at our NeurIPS booth.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "Preferred Networks joins the PyTorch community\n\nPreferred Networks, Inc. (PFN) announced plans to move its deep learning framework from Chainer to PyTorch. As part of this change, PFN will collaborate with the PyTorch community and contributors, including people from Facebook, Microsoft, CMU, and NYU, to participate in the development of PyTorch.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "PFN developed Chainer, a deep learning framework that introduced the concept of define-by-run (also referred to as eager execution), to support and speed up its deep learning development. Chainer has been used at PFN since 2015 to rapidly solve real-world problems with the latest, cutting-edge technology. Chainer was also one of the inspirations for PyTorch\u2019s initial design, as outlined in the [PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "NeurIPS](https://papers.nips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library) paper.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "PFN has driven innovative work with [CuPy](https://cupy.chainer.org/), ImageNet in 15 minutes, [Optuna](https://optuna.org/), and other projects that have pushed the boundaries of design and engineering. As part of the PyTorch community, PFN brings with them creative engineering capabilities and experience to help take the framework forward. In addition, PFN\u2019s migration to PyTorch will allow it to efficiently incorporate the latest research results to accelerate its R&D activities, [given PyTorch\u2019s broad", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "adoption with researchers](https://thegradient.pub/state-of-ml-frameworks-2019-pytorch-dominates-research-tensorflow-dominates-industry/), and to collaborate with the community to add support for PyTorch on MN-Core, a deep learning processor currently in development.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "We are excited to welcome PFN to the PyTorch community, and to jointly work towards the common goal of furthering advances in deep learning technology. Learn more about the PFN\u2019s migration to PyTorch [here](https://preferred.jp/en/news/pr20191205/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "Tools for elastic training and large scale computer vision", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Elastic (Experimental)\n\nLarge scale model training is becoming commonplace with architectures like BERT and the growth of model parameter counts into the billions or even tens of billions. To achieve convergence at this scale in a reasonable amount of time, the use of distributed training is needed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "The current PyTorch Distributed Data Parallel (DDP) module enables data parallel training where each process trains the same model but on different shards of data. It enables bulk synchronous, multi-host, multi-GPU/CPU execution of ML training. However, DDP has several shortcomings; e.g. jobs cannot start without acquiring all the requested nodes; jobs cannot continue after a node fails due to error or transient issue; jobs cannot incorporate a node that joined later; and lastly; progress cannot be made", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "with the presence of a slow/stuck node.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "The focus of [PyTorch Elastic](https://github.com/pytorch/elastic), which uses Elastic Distributed Data Parallelism, is to address these issues and build a generic framework/APIs for PyTorch to enable reliable and elastic execution of these data parallel training workloads. It will provide better programmability, higher resilience to failures of all kinds, higher-efficiency and larger-scale training compared with pure DDP.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "Elasticity, in this case, means both: 1) the ability for a job to continue after node failure (by running with fewer nodes and/or by incorporating a new host and transferring state to it); and 2) the ability to add/remove nodes dynamically due to resource availability changes or bottlenecks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "While this feature is still experimental, you can try it out on AWS EC2, with the instructions [here](https://github.com/pytorch/elastic/tree/master/aws). Additionally, the PyTorch distributed team is working closely with teams across AWS to support PyTorch Elastic training within services such as Amazon Sagemaker and Elastic Kubernetes Service (EKS). Look for additional updates in the near future.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "New Classification Framework\n\nImage and video classification are at the core of content understanding. To that end, you can now leverage a new end-to-end framework for large-scale training of state-of-the-art image and video classification models. It allows researchers to quickly prototype and iterate on large distributed training jobs at the scale of billions of images. Advantages include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Ease of use - This framework features a modular, flexible design that allows anyone to train machine learning models on top of PyTorch using very simple abstractions. The system also has out-of-the-box integration with AWS on PyTorch Elastic, facilitating research at scale and making it simple to move between research and production.\n* High performance - Researchers can use the framework to train models such as Resnet50 on ImageNet in as little as 15 minutes.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "You can learn more at the [NeurIPS Expo workshop](https://nips.cc/ExpoConferences/2019/schedule?workshop_id=16) on Multi-Modal research to production or get started with the PyTorch Elastic Imagenet example [here](https://github.com/pytorch/elastic/blob/master/examples/imagenet/main.py).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "Come see us at NeurIPS\n\nThe PyTorch team will be hosting workshops at NeurIPS during the industry expo on 12/8. Join the sessions below to learn more, and visit the team at the PyTorch booth on the show floor and during the Poster Session. At the booth, we\u2019ll be walking through an interactive demo of PyTorch running fast neural style transfer on a Cloud TPU - here\u2019s a [sneak peek](https://colab.research.google.com/github/pytorch/xla/blob/master/contrib/colab/style_transfer_inference-xrt-1-15.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re also publishing a [paper that details the principles that drove the implementation of PyTorch](https://papers.nips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library) and how they\u2019re reflected in its architecture.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "*[Multi-modal Research to Production](https://nips.cc/ExpoConferences/2019/schedule?workshop_id=16)* - This workshop will dive into a number of modalities such as computer vision (large scale image classification and instance segmentation) and Translation and Speech (seq-to-seq Transformers) from the lens of taking cutting edge research to production. Lastly, we will also walk through how to use the latest APIs in PyTorch to take eager mode developed models into graph mode via Torchscript and quantize them", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "for scale production deployment on servers or mobile devices. Libraries used include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Classification Framework - a newly open sourced PyTorch framework developed by Facebook AI for research on large-scale image and video classification. It allows researchers to quickly prototype and iterate on large distributed training jobs. Models built on the framework can be seamlessly deployed to production.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Detectron2 - the recently released object detection library built by the Facebook AI Research computer vision team. We will articulate the improvements over the previous version including: 1) Support for latest models and new tasks; 2) Increased flexibility, to enable new computer vision research; 3) Maintainable and scalable, to support production use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Fairseq - general purpose sequence-to-sequence library, can be used in many applications, including (unsupervised) translation, summarization, dialog and speech recognition.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "nvFuser is a Deep Learning Compiler for NVIDIA GPUs that automatically just-in-time compiles fast and flexible kernels to reliably accelerate users' networks. It provides significant speedups for deep learning networks running on Volta and later CUDA accelerators by generating fast custom \u201cfusion\u201d kernels at runtime. nvFuser is specifically designed to meet the unique requirements of the PyTorch community, and it supports diverse network architectures and programs with dynamic inputs of varying shapes and strides.\nIn this blog post we\u2019ll describe nvFuser and how it\u2019s used today, show the significant performance improvements it can obtain on models from HuggingFace and TIMM, and look ahead to nvFuser in PyTorch 1.13 and beyond. If you would like to know more about how and why fusion improves the speed of training for Deep Learning networks, please see our previous talks on nvFuser from [GTC 2022](https://www.nvidia.com/en-us/on-demand/session/gtcspring22-s41958/) and [GTC 2021](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31952/).\nnvFuser relies on a graph representation of PyTorch operations to optimize and accelerate. Since PyTorch has an eager execution model, the PyTorch operations users are running are not directly accessible as a whole program that can be optimized by a system like nvFuser. Therefore users must utilize systems built on top of nvFuser which are capable of capturing users programs and translating them into a form that is optimizable by nvFuser. These higher level systems then pass these captured operations to nvFuser, so that nvFuser can optimize the execution of the user\u2019s script for NVIDIA GPUs. There are three systems that capture, translate, and pass user programs to nvFuser for optimization:", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "- [TorchScript jit.script](https://pytorch.org/docs/stable/generated/torch.jit.script.html#torch.jit.script)\n - This system directly parses sections of an annotated python script to translate into its own representation what the user is doing. This system then applies its own version of auto differentiation to the graph, and passes sections of the subsequent forward and backwards graphs to nvFuser for optimization.\n- [FuncTorch](https://pytorch.org/functorch/stable/generated/functorch.compile.memory_efficient_fusion.html#functorch.compile.memory_efficient_fusion)\n - This system doesn\u2019t directly look at the user python script, instead inserting a mechanism that captures PyTorch operations as they\u2019re being run. We refer to this type of capture system as \u201ctrace program acquisition\u201d, since we\u2019re tracing what has been performed. FuncTorch doesn\u2019t perform its own auto differentiation \u2013 it simply traces PyTorch\u2019s autograd directly to get backward graphs.\n- [TorchDynamo](https://github.com/pytorch/torchdynamo)\n - TorchDynamo is another program acquisition mechanism built on top of FuncTorch. TorchDynamo parses the Python bytecode produced from the user script in order to select portions to trace with FuncTorch. The benefit of TorchDynamo is that it\u2019s able to apply decorators to a user\u2019s script, effectively isolating what should be sent to FuncTorch, making it easier for FuncTorch to successfully trace complex Python scripts.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "These systems are available for users to interact with directly while nvFuser automatically and seamlessly optimizes performance critical regions of the user\u2019s code. These systems automatically send parsed user programs to nvFuser so nvFuser can:\n\n1. Analyze the operations being run on GPUs\n2. Plan parallelization and optimization strategies for those operations\n3. Apply those strategies in generated GPU code\n4. Runtime-compile the generated optimized GPU functions\n5. Execute those CUDA kernels on subsequent iterations", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "It is important to note nvFuser does not yet support all PyTorch operations, and there are still some scenarios that are actively being improved in nvFuser that are discussed herein. However, nvFuser does support many DL performance critical operations today, and the number of supported operations will grow in subsequent PyTorch releases. nvFuser is capable of generating highly specialized and optimized GPU functions for the operations it does have support for. This means nvFuser is able to power new PyTorch systems like TorchDynamo and FuncTorch to combine the flexibility PyTorch is known for with unbeatable performance.\n\n## nvFuser Performance", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Before getting into how to use nvFuser, in this section we\u2019ll show the improvements in training speed nvFuser provides for a variety of models from the [HuggingFace Transformers](https://github.com/huggingface/transformers) and [PyTorch Image Models (TIMM)](https://github.com/rwightman/pytorch-image-models) repositories and we will discuss current gaps in nvFuser performance that are under development today. All performance numbers in this section were taken using an NVIDIA A100 40GB GPU, and used either FuncTorch alone or Functorch with TorchDynamo.\n\n## HuggingFace Transformer Benchmarks\n\nnvFuser can dramatically accelerate training of HuggingFace Transformers when combined with another important optimization (more on that in a moment). Performance improvements can be seen in Figure 1 to range between 1.12x and 1.50x across a subset of popular HuggingFace Transformer networks.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 1: Performance gains of 8 training scenarios from HuggingFace\u2019s Transformer repository. First performance boost in the dark green is due to replacing the optimizer with an NVIDIA Apex fused AdamW optimizer. The light green is due to adding nvFuser. Models were run with batch size and sequence lengths of [64, 128], [8, 512], [2, 1024], [64, 128], [8, 512], [8, src_seql=512, tgt_seql=128], [8, src_seql=1024, tgt_seql=128], and [8, 512] respectively. All networks were run with Automatic Mixed Precision (AMP) enabled with dtype=float16.\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "While these speedups are significant, it\u2019s important to understand that nvFuser doesn\u2019t (yet) automate everything about running networks quickly. For HuggingFace Transformers, for example, it was important to use the AdamW fused optimizer from [NVIDIA\u2019s Apex repository](https://github.com/NVIDIA/apex) as the optimizer otherwise consumed a large portion of runtime. Using the fused AdamW optimizer to make the network faster exposes the next major performance bottleneck \u2014 memory bound operations. These operations are optimized by nvFuser, providing another large performance boost. With the fused optimizer and nvFuser enabled, the training speed of these networks improved between 1.12x to 1.5x.\nHuggingFace Transformer models were run with [the torch.amp module](https://pytorch.org/docs/stable/amp.html). (\u201camp\u201d stands for Automated Mixed Precision, see the [\u201cWhat Every User Should Know about Mixed Precision in PyTorch\u201d](https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/) blog post for details.) An option to use nvFuser was added to HuggingFace\u2019sTrainer. If you have [TorchDynamo installed](https://github.com/pytorch/torchdynamo#requirements-and-setup) you can activate it to enable nvFuser in HuggingFace by passing *torchdynamo = \u2018nvfuser\u2019* to the Trainer class.\nnvFuser has great support for normalization kernels and related fusions frequently found in Natural Language Processing (NLP) models, and it is recommended users try nvFuser in their NLP workloads.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## PyTorch Image Models (TIMM) Benchmarks\nnvFuser, can also significantly reduce the training time of TIMM networks, up to over 1.3x vs. eager PyTorch, and up to 1.44x vs. eager PyTorch when combined with the torch.amp module. Figure 1 shows nvFuser\u2019s speedup without torch.amp, and when torch.amp is used with the NHWC (\u201cchannels last\u201d) and NCHW (\u201cchannels first\u201d) formats. nvFuser is integrated in TIMM through FuncTorch tracing directly (without TorchDynamo) and can be used by adding the [--aot-autograd command line argument](https://github.com/rwightman/pytorch-image-models/commit/ca991c1fa57373286b9876aa63370fd19f5d6032) when running the TIMM benchmark or training script.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 1: The Y-axis is the performance gain nvFuser provides over not using nvFuser. A value of 1.0 means no change in perf, 2.0 would mean nvFuser is twice as fast, 0.5 would mean nvFuser takes twice the time to run. Square markers are with float16 Automatic Mixed Precision (AMP) and channels first contiguous inputs, circle markers are float32 inputs, and triangles are with float16 AMP and channels last contiguous inputs. Missing data points are due to an error being encountered when tracing.\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "When running with float32 precision nvFuser provides a 1.12x geometric mean (\u201cgeomean\u201d) speedup on TIMM networks, and when running with torch.amp and \u201cchannels first\u201d it provides a 1.14x geomean speedup. However, nvFuser currently doesn\u2019t speedup torch.amp and \u201cchannels last\u201d training (a .9x geomean regression), so we recommend not using it in those cases. We are actively working on improving \u201cchannels last\u201d performance now, and soon we will have two additional optimization strategies (grid persistent optimizations for channels-last normalizations and fast transposes) which we expect will provide speedups comparable to \u201cchannels first\u201d in PyTorch version 1.13 and later. Many of nvFuser\u2019s optimizations can also help in inference cases. However, in PyTorch when running inference on small batch sizes, the performance is typically limited by CPU overhead, which nvFuser can\u2019t completely remove or fix. Therefore, typically the most important optimization for inference is to enable [CUDA Graphs](https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/) when possible. Once CUDA Graphs is enabled, then it can also be beneficial to also enable fusion through nvFuser. Performance of inference is shown in Figure 2 and Figure 3. Inference is only run with float16 AMP as it is uncommon to run inference workloads in full float32 precision.", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\n \n

\n\n

\nFigure 2: Performance gains of enabling CUDA Graphs, and CUDA Graphs with nvFuser compared to the performance of native PyTorch without CUDA Graphs and nvFuser across TIMM models with float16 AMP, channels first inputs, and a batch size of 1 and 8 respectively. There is a geomean speedup of 2.74x with CUDA Graphs and 2.71x with CUDA Graphs + nvFuser respectively. nvFuser provides a maximum regression of 0.68x and a maximum performance gain of 2.74x (relative to CUDA Graphs without nvFuser). Performance gain is measured relative to the average time per iteration PyTorch takes without CUDA Graphs and without nvFuser. Models are sorted by how much additional performance nvFuser is providing.\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\n \n

\n\n

\nFigure 3: Performance gains of enabling CUDA Graphs, and CUDA Graphs with nvFuser compared to the performance of native PyTorch without CUDA Graphs and nvFuser across TIMM models with AMP, channels last inputs, and a batch size of 1 and 8 respectively. There is a geomean speedup of 2.29x with CUDA Graphs and 2.95x with CUDA Graphs + nvFuser respectively. nvFuser provides a maximum regression of 0.86x and a maximum performance gain of 3.82x (relative to CUDA Graphs without nvFuser). Performance gain is measured relative to the average time per iteration PyTorch takes without CUDA Graphs and without nvFuser. Models are sorted by how much additional performance nvFuser is providing.\n

", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "So far nvFuser performance has not been tuned for inference workloads so its performance benefit is not consistent across all cases. However, there are still many models that benefit significantly from nvFuser during inference and we encourage users to try nvFuser in inference workloads to see if you would benefit today. Performance of nvFuser in inference workloads will improve in the future and if you\u2019re interested in nvFuser in inference workloads please reach out to us on the PyTorch forums.\n\n## Getting Started - Accelerate Your Scripts with nvFuser", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We\u2019ve created [a tutorial](https://pytorch.org/tutorials/intermediate/nvfuser_intro_tutorial.html) demonstrating how to take advantage of nvFuser to accelerate part of a standard transformer block, and how nvFuser can be used to define fast and novel operations. There are still some rough edges in nvFuser that we\u2019re working hard on improving as we\u2019ve outlined in this blog post. However we\u2019ve also demonstrated some great improvements for training speed on multiple networks in HuggingFace and TIMM and we expect there are opportunities in your networks where nvFuser can help today, and many more opportunities it will help in the future.\nIf you would like to learn more about nvFuser we recommend watching our presentations from NVIDIA\u2019s GTC conference [GTC 2022](https://www.nvidia.com/en-us/on-demand/session/gtcspring22-s41958/) and [GTC 2021](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31952/).", "metadata": {"source": "https://pytorch.org/blog/introducing-nvfuser-a-deep-learning-compiler-for-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing PyTorch Profiler - the new and improved performance tool'\nauthor: Maxim Lukiyanov - Principal PM at Microsoft, Guoliang Hua - Principal Engineering Manager at Microsoft, Geeta Chauhan - Partner Engineering Lead at Facebook, Gisle Dankel - Tech Lead at Facebook\n---\n\nAlong with [PyTorch 1.8.1 release](https://github.com/pytorch/pytorch/releases/tag/v1.8.1), we are excited to announce PyTorch Profiler \u2013 the new and improved performance debugging profiler for PyTorch. Developed as part of a collaboration between Microsoft and Facebook, the PyTorch Profiler is an open-source tool that enables accurate and efficient performance analysis and troubleshooting for large-scale deep learning models.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "Analyzing and improving large-scale deep learning model performance is an ongoing challenge that grows in importance as the model sizes increase. For a long time, PyTorch users had a hard time solving this challenge due to the lack of available tools. There were standard performance debugging tools that provide GPU hardware level information but missed PyTorch-specific context of operations. In order to recover missed information, users needed to combine multiple tools together or manually add minimum correlation information to make sense of the data. There was also the autograd profiler (```torch.autograd.profiler```) which can capture information about PyTorch operations but does not capture detailed GPU hardware-level information and cannot provide support for visualization.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "The new PyTorch Profiler (```torch.profiler```) is a tool that brings both types of information together and then builds experience that realizes the full potential of that information. This new profiler collects both GPU hardware and PyTorch related information, correlates them, performs automatic detection of bottlenecks in the model, and generates recommendations on how to resolve these bottlenecks. All of this information from the profiler is visualized for the user in TensorBoard. The new Profiler API is natively supported in PyTorch and delivers the simplest experience available to date where users can profile their models without installing any additional packages and see results immediately in TensorBoard with the new PyTorch Profiler plugin. Below is the screenshot of PyTorch Profiler - automatic bottleneck detection. \n\n
\n \n
\n\n## Getting started", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "## Getting started\n\nPyTorch Profiler is the next version of the PyTorch autograd profiler. It has a new module namespace ```torch.profiler``` but maintains compatibility with autograd profiler APIs. The Profiler uses a new GPU profiling engine, built using Nvidia CUPTI APIs, and is able to capture GPU kernel events with high fidelity. To profile your model training loop, wrap the code in the profiler context manager as shown below.\n\n```python\n with torch.profiler.profile(\n schedule=torch.profiler.schedule(\n wait=2,\n warmup=2,\n active=6,\n repeat=1),\n on_trace_ready=tensorboard_trace_handler,\n with_stack=True\n) as profiler:\n for step, data in enumerate(trainloader, 0):\n print(\"step:{}\".format(step))\n inputs, labels = data[0].to(device=device), data[1].to(device=device)\n\n outputs = model(inputs)\n loss = criterion(outputs, labels)", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "optimizer.zero_grad()\n loss.backward()\n optimizer.step()\n profiler.step()\n```\nThe ```schedule``` parameter allows you to limit the number of training steps included in the profile to reduce the amount of data collected and simplify visual analysis by focusing on what\u2019s important. The ```tensorboard_trace_handler``` automatically saves profiling results to disk for analysis in TensorBoard.\n\nTo view results of the profiling session in TensorBoard, install PyTorch Profiler TensorBoard Plugin package.", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "```python\npip install torch_tb_profiler\n```\n## Visual Studio Code Integration\n[Microsoft Visual Studio Code](https://code.visualstudio.com/) is one of the most popular code editors for Python developers and data scientists. The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) for VS Code recently added the integration of TensorBoard into the code editor, including support for the PyTorch Profiler. Once you have VS Code and the Python extension installed, you can quickly open the TensorBoard Profiler plugin by launching the Command Palette using the keyboard shortcut CTRL + SHIFT + P (CMD + SHIFT + P on a Mac) and typing the \u201cLaunch TensorBoard\u201d command.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "This integration comes with a built-in lifecycle management feature. VS Code will install the TensorBoard package and the PyTorch Profiler plugin package (coming in mid-April) automatically if you don\u2019t have them on your system. VS Code will also launch TensorBoard process for you and automatically look for any TensorBoard log files within your current directory. When you\u2019re done, just close the tab and VS Code will automatically close the process. No more Terminal windows running on your system to provide a backend for the TensorBoard UI! Below is PyTorch Profiler Trace View running in TensorBoard.\n\n
\n \n
\n\nLearn more about TensorBoard support in VS Code in [this blog](https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2021-release/).\n\n## Feedback", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "## Feedback\n\nReview [PyTorch Profiler documentation](https://pytorch.org/docs/stable/profiler.html), give Profiler a try and let us know about your experience. Provide your feedback on [PyTorch Discussion Forum](https://discuss.pytorch.org/) or file issues on [PyTorch GitHub](https://github.com/pytorch/pytorch).", "metadata": {"source": "https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"How Disney Improved Activity Recognition Through Multimodal Approaches with PyTorch\"\nauthor: Monica Alfaro, Albert Aparicio, Francesc Guitart, Marc Junyent, Pablo Pernias, Marcel Porta, and Miquel \u00c0ngel Farr\u00e9 (former Senior Technology Manager)\nfeatured-img: 'assets/images/disney_media_logo.jpg'\n---\n\n# Introduction\n\nAmong the many things Disney Media & Entertainment Distribution (DMED) is responsible for, is the management and distribution of a huge array of media assets including news, sports, entertainment and features, episodic programs, marketing and advertising and more.\n\n\n\n

\n \n

\n\n\n\nOur team focuses on media annotation as part of DMED Technology\u2019s content platforms group. In our day-to-day work, we automatically analyze a variety of content that constantly challenges the efficiency of our machine learning workflow and the accuracy of our models.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Several of our colleagues recently discussed the workflow efficiencies that we achieved by switching to an end-to-end video analysis pipeline using PyTorch, as well as how we approach animated character recognition. We invite you to read more about both in this previous post.\n\nWhile the conversion to an end-to-end PyTorch pipeline is a solution that any company might benefit from, animated character recognition was a uniquely-Disney concept and solution.\n\nIn this article we will focus on activity recognition, which is a general challenge across industries \u2014 but with some specific opportunities when leveraged in the media production field, because we can combine audio, video, and subtitles to provide a solution.\n\n# Experimenting with Multimodality", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Working on a multimodal problem adds more complexity to the usual training pipelines. Having multiple information modes for each example means that the multimodal pipeline has to have specific implementations to process each mode in the dataset. Usually after this processing step, the pipeline has to merge or fuse the outputs.\n\nOur initial experiments in multimodality were completed using the [MMF framework](https://github.com/facebookresearch/mmf). MMF is a modular framework for vision and language multimodal research. MMF contains reference implementations of state-of-the-art vision and language models and has also powered multiple research projects at Meta AI Research (as seen in this [poster](https://assets.pytorch.org/pted2021/posters/A3.png) presented in PyTorch Ecosystem Day 2020). Along with the recent release of TorchMultimodal, a PyTorch library for training state-of-the-art multimodal models at scale, MMF highlights the growing interest in Multimodal understanding.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "MMF tackles this complexity with modular management of all the elements of the pipeline through a wide set of different implementations for specific modules, ranging from the processing of the modalities to the fusion of the processed information.\n\nIn our scenario, MMF was a great entry point to experiment with multimodality. It allowed us to iterate quickly by combining audio, video and closed captioning and experiment at different levels of scale with certain multimodal models, shifting from a single GPU to TPU Pods.\n\n# Multimodal Transformers\n\nWith a workbench based on MMF, our initial model was based on a concatenation of features from each modality evolving to a pipeline that included a Transformer-based fusion module to combine the different input modes.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Specifically, we made use of the fusion module called MMFTransformer, developed in collaboration with the Meta AI Research team. This is an implementation based on [VisualBERT](https://arxiv.org/abs/1908.03557) for which the necessary modifications were added to be able to work with text, audio and video.\n\nDespite having decent results with the out-of-box implementation MMFTransformer, we were still far from our goal, and the Transformers-based models required more data than we had available.\n\n# Searching for less data-hungry solutions\n\nSearching for less data-hungry solutions, our team started studying [MLP-Mixer](https://arxiv.org/abs/2105.01601). This new architecture has been proposed by the Google Brain team and it provides an alternative to well established de facto architectures like convolutions or self-attention for computer vision tasks.\n\n## MLP-Mixer", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## MLP-Mixer\n\nThe core idea behind mixed variations consists of replacing the convolutions or self-attention mechanisms used in transformers with Multilayer Perceptrons. This change in architecture favors the performance of the model in high data regimes (especially with respect to the Transformers), while also opening some questions regarding the inductive biases hidden in the convolutions and the self-attention layers.\n\nThose proposals perform great in solving image classification tasks by splitting the image in chunks, flattening those chunks into 1D vectors and passing them through a sequence of Mixer Layers.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Inspired by the advantages of Mixer based architectures, our team searched for parallelisms with the type of problems we try to solve in video classification: specifically, instead of a single image, we have a set of frames that need to be classified, along with audio and closed captioning in the shape of new modalities.\n\n# Activity Recognition reinterpreting the MLP-Mixer\n\nOur proposal takes the core idea of the [MLP-Mixer](https://arxiv.org/abs/2105.01601) \u2014 using multiple multi-layer perceptrons on a sequence and transposed sequence and extends it into a Multi Modal framework that allows us to process video, audio & text with the same architecture.\n\nFor each of the modalities, we use different extractors that will provide embeddings describing the content. Given the embeddings of each modality, the MLP-Mixer architecture solves the problem of deciding which of the modalities might be the most important, while also weighing how much each modality contributes to the final labeling.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "For example, when it comes to detecting laughs, sometimes the key information is in audio or in the frames, and in some of the cases we have a strong signal in the closed caption.\n\nWe tried processing each frame separately with a ResNet34 and getting a sequence of embeddings and by using a video-specific model called R3D, both pre-trained on ImageNet and Kinetics400 respectively.\n\n\n\n

\n \n

\n\n\n\nTo process the audio, we use the pretrained ResNet34, and we remove the final layers to be able to extract 2D embeddings from the audio spectrograms (for 224x224 images we end up with 7x7 embeddings).\n\n\n\n

\n \n

\n\n\n\nFor closed captioning, we are using a pre-trained BERT-large, with all layers frozen, except for the Embeddings & LayerNorms.\n\n\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Once we have extracted the embedding from each modality, we concatenate them into a single sequence and pass it through a set of MLP-Mixer blocks; next we use average pooling & a classification head to get predictions.\n\n\n\n

\n \n

\n\n\n\nOur experiments have been performed on a custom, manually labeled dataset for activity recognition with 15 classes, which we know from experiments are hard and cannot all be predicted accurately using a single modality.\n\nThese experiments have shown a significant increase in performance using our approach, especially in a low/mid-data regime (75K training samples).\n\nWhen it comes to using only Text and Audio, our experiments showed a 15 percent improvement in accuracy over using a classifier on top of the features extracted by state-of-the-art backbones.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Using Text, Audio and Video we have seen a 17 percent improvement in accuracy over using Meta AIFacebook\u2019s MMF Framework, which uses a VisualBERT-like model to combine modalities using more powerful state of the art backbones.\n\nCurrently, we extended the initial model to cover up to 55 activity classes and 45 event classes. One of the challenges we expect to improve upon in the future is to include all activities and events, even those that are less frequent.\n\n## Interpreting the MLP-Mixer mode combinations \n\nAn MLP-Mixer is a concatenation of MultiLayer Perceptrons. This can be, very roughly, approximated to a linear operation, in the sense that, once trained, the weights are fixed and the input will directly affect the output.\n\nOnce we assume that approximation, we also assume that for an input consisting of NxM numbers, we could find a NxM matrix that (when multiplied elementwise) could approximate the predictions of the MLP-Mixer for a class.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n\n\nWe will call this matrix a stencil, and if we have access to it, we can find what parts of the input embeddings are responsible for a specific prediction.\n\nYou can think of it as a punch card with holes in specific positions. Only information in those positions will pass and contribute to a specific prediction. So we can measure the intensity of the input at those positions.\n\n\n\n

\n \n

\n\n\n\nOf course, this is an oversimplification, and there won't exist a unique stencil that perfectly represents all of the contributions of the input to a class (otherwise that would mean that the problem could be solved linearly). So this should be used for visualization purposes only, not as an accurate predictor.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Once we have a set of stencils for each class, we can effortlessly measure input contribution without relying on any external visualization techniques.\n\nTo find a stencil, we can start from a \"random noise\" stencil and optimize it to maximize the activations for a specific class by just back-propagating through the MLP-Mixer.\n\n\n\n

\n \n

\n\n\n\nBy doing this we can end up with many valid stencils, and we can reduce them to a few by using K-means to cluster them into similar stencils and averaging each cluster.\n\n# Using the Mixer to get the best of each world\n\nMLP-Mixer, used as an image classification model without convolutional layers, requires a lot of data, since the lack of inductive bias \u2013 one of the model's good points overall \u2013 is a weakness when it comes to working in low data domains.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "When used as a way to combine information previously extracted by large pretrained backbones (as opposed to being used as a full end-to-end solution), they shine. The Mixer\u2019s strength lies in finding temporal or structural coherence between different inputs. For example, in video-related tasks we could extract embeddings from the frames using a powerful, pretrained model that understands what is going on at frame level and use the mixer to make sense of it in a sequential manner.\n\nThis way of using the Mixer allows us to work with limited amounts of data and still get better results than what was achieved with Transformers. This is because Mixers seem to be more stable during training and seem to pay attention to all the inputs, while Transformers tend to collapse and pay attention only to some modalities/parts of the sequence.\n\nAcknowledgements: We would like to thank the Meta AI Research and Partner Engineering teams for this collaboration.", "metadata": {"source": "https://pytorch.org/blog/how-disney-improved-activity-recognition-with-multimodal-approaches-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Practical Quantization in PyTorch'\nauthor: Suraj Subramanian, Mark Saroufim, Jerry Zhang\nfeatured-img: ''\n---\n\nQuantization is a cheap and easy way to make your DNN run faster and with lower memory requirements. PyTorch offers a few different approaches to quantize your model. In this blog post, we'll lay a (quick) foundation of quantization in deep learning, and then take a look at how each technique looks like in practice. Finally we'll end with recommendations from the literature for using quantization in your workflows.\n\n

\n \n
\n Fig 1. PyTorch <3 Quantization\n

\n\n**Contents**\n* TOC\n{:toc}\n## Fundamentals of Quantization\n\n> If someone asks you what time it is, you don't respond \"10:14:34:430705\", but you might say \"a quarter past 10\".\n\nQuantization has roots in information compression; in deep networks it refers to reducing the numerical precision of its weights and/or activations.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "Overparameterized DNNs have more degrees of freedom and this makes them good candidates for information compression [[1]]. When you quantize a model, two things generally happen - the model gets smaller and runs with better efficiency. Hardware vendors explicitly allow for faster processing of 8-bit data (than 32-bit data) resulting in higher throughput. A smaller model has lower memory footprint and power consumption [[2]], crucial for deployment at the edge.\n\n### Mapping function\nThe mapping function is what you might guess - a function that maps values from floating-point to integer space. A commonly used mapping function is a linear transformation given by , where is the input and are **quantization parameters**.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "To reconvert to floating point space, the inverse function is given by . \n\n, and their difference constitutes the *quantization error*.\n\n### Quantization Parameters\nThe mapping function is parameterized by the **scaling factor** and **zero-point** . \n\n is simply the ratio of the input range to the output range \n", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "where [] is the clipping range of the input, i.e. the boundaries of permissible inputs. [] is the range in quantized output space that it is mapped to. For 8-bit quantization, the output range .\n\n\n acts as a bias to ensure that a 0 in the input space maps perfectly to a 0 in the quantized space. ", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "### Calibration\nThe process of choosing the input clipping range is known as **calibration**. The simplest technique (also the default in PyTorch) is to record the running mininmum and maximum values and assign them to and . [TensorRT](https://docs.nvidia.com/deeplearning/tensorrt/pytorch-quantization-toolkit/docs/calib.html) also uses entropy minimization (KL divergence), mean-square-error minimization, or percentiles of the input range.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "In PyTorch, `Observer` modules ([docs](https://PyTorch.org/docs/stable/torch.quantization.html?highlight=observer#observers), [code](https://github.com/PyTorch/PyTorch/blob/748d9d24940cd17938df963456c90fa1a13f3932/torch/ao/quantization/observer.py#L88)) collect statistics on the input values and calculate the qparams . Different calibration schemes result in different quantized outputs, and it's best to empirically verify which scheme works best for your application and architecture (more on that later).\n\n```python\nfrom torch.quantization.observer import MinMaxObserver, MovingAverageMinMaxObserver, HistogramObserver\nC, L = 3, 4\nnormal = torch.distributions.normal.Normal(0,1)\ninputs = [normal.sample((C, L)), normal.sample((C, L))]\nprint(inputs)\n\n# >>>>>\n# [tensor([[-0.0590, 1.1674, 0.7119, -1.1270],\n# [-1.3974, 0.5077, -0.5601, 0.0683],\n# [-0.0929, 0.9473, 0.7159, -0.4574]]]),", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "# tensor([[-0.0236, -0.7599, 1.0290, 0.8914],\n# [-1.1727, -1.2556, -0.2271, 0.9568],\n# [-0.2500, 1.4579, 1.4707, 0.4043]])]\n\nobservers = [MinMaxObserver(), MovingAverageMinMaxObserver(), HistogramObserver()]\nfor obs in observers:\n for x in inputs: obs(x) \n print(obs.__class__.__name__, obs.calculate_qparams())\n\n# >>>>>\n# MinMaxObserver (tensor([0.0112]), tensor([124], dtype=torch.int32))\n# MovingAverageMinMaxObserver (tensor([0.0101]), tensor([139], dtype=torch.int32))\n# HistogramObserver (tensor([0.0100]), tensor([106], dtype=torch.int32))\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "### Affine and Symmetric Quantization Schemes\n**Affine or asymmetric quantization** schemes assign the input range to the min and max observed values. Affine schemes generally offer tighter clipping ranges and are useful for quantizing non-negative activations (you don't need the input range to contain negative values if your input tensors are never negative). The range is calculated as \n. Affine quantization leads to more computationally expensive inference when used for weight tensors [[3]].\n\n**Symmetric quantization** schemes center the input range around 0, eliminating the need to calculate a zero-point offset. The range is calculated as \n. For skewed signals (like non-negative activations) this can result in bad quantization resolution because the clipping range includes values that never show up in the input (see the pyplot below).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "```python\nact = torch.distributions.pareto.Pareto(1, 10).sample((1,1024))\nweights = torch.distributions.normal.Normal(0, 0.12).sample((3, 64, 7, 7)).flatten()\n\ndef get_symmetric_range(x):\n beta = torch.max(x.max(), x.min().abs())\n return -beta.item(), beta.item()\n\ndef get_affine_range(x):\n return x.min().item(), x.max().item()\n\ndef plot(plt, data, scheme):\n boundaries = get_affine_range(data) if scheme == 'affine' else get_symmetric_range(data)\n a, _, _ = plt.hist(data, density=True, bins=100)\n ymin, ymax = np.quantile(a[a>0], [0.25, 0.95])\n plt.vlines(x=boundaries, ls='--', colors='purple', ymin=ymin, ymax=ymax)\n\nfig, axs = plt.subplots(2,2)\nplot(axs[0, 0], act, 'affine')\naxs[0, 0].set_title(\"Activation, Affine-Quantized\")\n\nplot(axs[0, 1], act, 'symmetric')\naxs[0, 1].set_title(\"Activation, Symmetric-Quantized\")\n\nplot(axs[1, 0], weights, 'affine')\naxs[1, 0].set_title(\"Weights, Affine-Quantized\")\n\nplot(axs[1, 1], weights, 'symmetric')\naxs[1, 1].set_title(\"Weights, Symmetric-Quantized\")\nplt.show()\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "

\n \n
Fig 2. Clipping ranges (in purple) for affine and symmetric schemes\n

\n\n\nIn PyTorch, you can specify affine or symmetric schemes while initializing the Observer. Note that not all observers support both schemes.\n\n```python\nfor qscheme in [torch.per_tensor_affine, torch.per_tensor_symmetric]:\n obs = MovingAverageMinMaxObserver(qscheme=qscheme)\n for x in inputs: obs(x)\n print(f\"Qscheme: {qscheme} | {obs.calculate_qparams()}\")\n\n# >>>>>\n# Qscheme: torch.per_tensor_affine | (tensor([0.0101]), tensor([139], dtype=torch.int32))\n# Qscheme: torch.per_tensor_symmetric | (tensor([0.0109]), tensor([128]))\n```\n\n### Per-Tensor and Per-Channel Quantization Schemes\nQuantization parameters can be calculated for the layer's entire weight tensor as a whole, or separately for each channel. In per-tensor, the same clipping range is applied to all the channels in a layer", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "

\n \n
Fig 3. Per-Channel uses one set of qparams for each channel. Per-tensor uses the same qparams for the entire tensor.\n

\n\nFor weights quantization, symmetric-per-channel quantization provides better accuracies; per-tensor quantization performs poorly, possibly due to high variance in conv weights across channels from batchnorm folding [[3]].\n\n```python\nfrom torch.quantization.observer import MovingAveragePerChannelMinMaxObserver\nobs = MovingAveragePerChannelMinMaxObserver(ch_axis=0) # calculate qparams for all `C` channels separately\nfor x in inputs: obs(x)\nprint(obs.calculate_qparams())\n\n# >>>>>\n# (tensor([0.0090, 0.0075, 0.0055]), tensor([125, 187, 82], dtype=torch.int32))\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "### Backend Engine\nCurrently, quantized operators run on x86 machines via the [FBGEMM backend](https://github.com/pytorch/FBGEMM), or use [QNNPACK](https://github.com/pytorch/QNNPACK) primitives on ARM machines. Backend support for server GPUs (via TensorRT and cuDNN) is coming soon. Learn more about extending quantization to custom backends: [RFC-0019](https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md).\n\n```python\nbackend = 'fbgemm' if x86 else 'qnnpack'\nqconfig = torch.quantization.get_default_qconfig(backend) \ntorch.backends.quantized.engine = backend\n```\n\n\n### QConfig\n\nThe `QConfig` ([code](https://github.com/PyTorch/PyTorch/blob/d6b15bfcbdaff8eb73fa750ee47cef4ccee1cd92/torch/ao/quantization/qconfig.py#L165), [docs](https://pytorch.org/docs/stable/torch.quantization.html?highlight=qconfig#torch.quantization.QConfig)) NamedTuple stores the Observers and the quantization schemes used to quantize activations and weights.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "Be sure to pass the Observer class (not the instance), or a callable that can return Observer instances. Use `with_args()` to override the default arguments.\n\n```python\nmy_qconfig = torch.quantization.QConfig(\n activation=MovingAverageMinMaxObserver.with_args(qscheme=torch.per_tensor_affine),\n weight=MovingAveragePerChannelMinMaxObserver.with_args(qscheme=torch.qint8)\n)\n# >>>>>\n# QConfig(activation=functools.partial(, qscheme=torch.per_tensor_affine){}, weight=functools.partial(, qscheme=torch.qint8){})\n```\n\n\n## In PyTorch", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "## In PyTorch\n\nPyTorch allows you a few different ways to quantize your model depending on\n- if you prefer a flexible but manual, or a restricted automagic process (*Eager Mode* v/s *FX Graph Mode*)\n- if qparams for quantizing activations (layer outputs) are precomputed for all inputs, or calculated afresh with each input (*static* v/s *dynamic*),\n- if qparams are computed with or without retraining (*quantization-aware training* v/s *post-training quantization*)\n\nFX Graph Mode automatically fuses eligible modules, inserts Quant/DeQuant stubs, calibrates the model and returns a quantized module - all in two method calls - but only for networks that are [symbolic traceable](https://PyTorch.org/docs/stable/fx.html#torch.fx.symbolic_trace). The examples below contain the calls using Eager Mode and FX Graph Mode for comparison.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "In DNNs, eligible candidates for quantization are the FP32 weights (layer parameters) and activations (layer outputs). Quantizing weights reduces the model size. Quantized activations typically result in faster inference.\n\nAs an example, the 50-layer ResNet network has ~26 million weight parameters and computes ~16 million activations in the forward pass.\n\n### Post-Training Dynamic/Weight-only Quantization \nHere the model's weights are pre-quantized; the activations are quantized on-the-fly (\"dynamic\") during inference. The simplest of all approaches, it has a one line API call in `torch.quantization.quantize_dynamic`. Currently only Linear and Recurrent (`LSTM`, `GRU`, `RNN`) layers are supported for dynamic quantization.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "**(+)** Can result in higher accuracies since the clipping range is exactly calibrated for each input [[1]].\n \n **(+)** Dynamic quantization is preferred for models like LSTMs and Transformers where writing/retrieving the model's weights from memory dominate bandwidths [[4]]. \n \n **(-)** Calibrating and quantizing the activations at each layer during runtime can add to the compute overhead. \n\n```python\nimport torch\nfrom torch import nn\n\n# toy model\nm = nn.Sequential(\n nn.Conv2d(2, 64, (8,)),\n nn.ReLU(),\n nn.Linear(16,10),\n nn.LSTM(10, 10))\n\nm.eval()\n\n## EAGER MODE\nfrom torch.quantization import quantize_dynamic\nmodel_quantized = quantize_dynamic(\n model=m, qconfig_spec={nn.LSTM, nn.Linear}, dtype=torch.qint8, inplace=False\n)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "## FX MODE\nfrom torch.quantization import quantize_fx\nqconfig_dict = {\"\": torch.quantization.default_dynamic_qconfig} # An empty key denotes the default applied to all modules\nmodel_prepared = quantize_fx.prepare_fx(m, qconfig_dict)\nmodel_quantized = quantize_fx.convert_fx(model_prepared)\n```\n\n### Post-Training Static Quantization (PTQ)\nPTQ also pre-quantizes model weights but instead of calibrating activations on-the-fly, the clipping range is pre-calibrated and fixed (\"static\") using validation data. Activations stay in quantized precision between operations during inference. About 100 mini-batches of representative data are sufficient to calibrate the observers [[2]]. The examples below use random data in calibration for convenience - using that in your application will result in bad qparams.\n\n\n

\n \"PTQ\n
\n Fig 4. Steps in Post-Training Static Quantization\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "[Module fusion](https://pytorch.org/tutorials/recipes/fuse.html) combines multiple sequential modules (eg: `[Conv2d, BatchNorm, ReLU]`) into one. Fusing modules means the compiler needs to only run one kernel instead of many; this speeds things up and improves accuracy by reducing quantization error.\n\n**(+)** Static quantization has faster inference than dynamic quantization because it eliminates the float<->int conversion costs between layers. \n\n**(-)** Static quantized models may need regular re-calibration to stay robust against distribution-drift.\n\n\n```python\n# Static quantization of a model consists of the following steps:\n\n# Fuse modules\n# Insert Quant/DeQuant Stubs\n# Prepare the fused module (insert observers before and after layers)\n# Calibrate the prepared module (pass it representative data)\n# Convert the calibrated module (replace with quantized version)\n\nimport torch\nfrom torch import nn\nimport copy\n\nbackend = \"fbgemm\" # running on a x86 CPU. Use \"qnnpack\" if running on ARM.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "model = nn.Sequential(\n nn.Conv2d(2,64,3),\n nn.ReLU(),\n nn.Conv2d(64, 128, 3),\n nn.ReLU()\n)\n\n## EAGER MODE\nm = copy.deepcopy(model)\nm.eval()\n\"\"\"Fuse\n- Inplace fusion replaces the first module in the sequence with the fused module, and the rest with identity modules\n\"\"\"\ntorch.quantization.fuse_modules(m, ['0','1'], inplace=True) # fuse first Conv-ReLU pair\ntorch.quantization.fuse_modules(m, ['2','3'], inplace=True) # fuse second Conv-ReLU pair\n\n\"\"\"Insert stubs\"\"\"\nm = nn.Sequential(torch.quantization.QuantStub(), \n *m, \n torch.quantization.DeQuantStub())\n\n\"\"\"Prepare\"\"\"\nm.qconfig = torch.quantization.get_default_qconfig(backend)\ntorch.quantization.prepare(m, inplace=True)\n\n\"\"\"Calibrate\n- This example uses random data for convenience. Use representative (validation) data instead.\n\"\"\"\nwith torch.inference_mode():\n for _ in range(10):\n x = torch.rand(1,2, 28, 28)\n m(x)\n \n\"\"\"Convert\"\"\"\ntorch.quantization.convert(m, inplace=True)", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "\"\"\"Check\"\"\"\nprint(m[[1]].weight().element_size()) # 1 byte instead of 4 bytes for FP32\n\n\n## FX GRAPH\nfrom torch.quantization import quantize_fx\nm = copy.deepcopy(model)\nm.eval()\nqconfig_dict = {\"\": torch.quantization.get_default_qconfig(backend)}\n# Prepare\nmodel_prepared = quantize_fx.prepare_fx(m, qconfig_dict)\n# Calibrate - Use representative (validation) data.\nwith torch.inference_mode():\n for _ in range(10):\n x = torch.rand(1,2,28, 28)\n model_prepared(x)\n# quantize\nmodel_quantized = quantize_fx.convert_fx(model_prepared)\n```\n\n### Quantization-aware Training (QAT)\n

\n \"QAT\n
\n Fig 5. Steps in Quantization-Aware Training\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "The PTQ approach is great for large models, but accuracy suffers in smaller models [[6]]. This is of course due to the loss in numerical precision when adapting a model from FP32 to the INT8 realm *(Figure 6(a))*. QAT tackles this by including this quantization error in the training loss, thereby training an INT8-first model.\n\n

\n \"Fig.\n
\n Fig 6. Comparison of PTQ and QAT convergence [3]\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "All weights and biases are stored in FP32, and backpropagation happens as usual. However in the forward pass, quantization is internally simulated via `FakeQuantize` modules. They are called fake because they quantize and immediately dequantize the data, adding quantization noise similar to what might be encountered during quantized inference. The final loss thus accounts for any expected quantization errors. Optimizing on this allows the model to identify a wider region in the loss function *(Figure 6(b))*, and identify FP32 parameters such that quantizing them to INT8 does not significantly affect accuracy.\n\n

\n \"Fake\n
Fig 7. Fake Quantization in the forward and backward pass \n
Image source: https://developer.nvidia.com/blog/achieving-fp32-accuracy-for-int8-inference-using-quantization-aware-training-with-tensorrt\n

", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "**(+)** QAT yields higher accuracies than PTQ.\n\n**(+)** Qparams can be learned during model training for more fine-grained accuracy (see [LearnableFakeQuantize](https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/_learnable_fake_quantize.py))\n\n**(-)** Computational cost of retraining a model in QAT can be several hundred epochs [[1]]\n\n\n```python\n# QAT follows the same steps as PTQ, with the exception of the training loop before you actually convert the model to its quantized version\n\nimport torch\nfrom torch import nn\n\nbackend = \"fbgemm\" # running on a x86 CPU. Use \"qnnpack\" if running on ARM.\n\nm = nn.Sequential(\n nn.Conv2d(2,64,8),\n nn.ReLU(),\n nn.Conv2d(64, 128, 8),\n nn.ReLU()\n)\n\n\"\"\"Fuse\"\"\"\ntorch.quantization.fuse_modules(m, ['0','1'], inplace=True) # fuse first Conv-ReLU pair\ntorch.quantization.fuse_modules(m, ['2','3'], inplace=True) # fuse second Conv-ReLU pair", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "\"\"\"Insert stubs\"\"\"\nm = nn.Sequential(torch.quantization.QuantStub(), \n *m, \n torch.quantization.DeQuantStub())\n\n\"\"\"Prepare\"\"\"\nm.train()\nm.qconfig = torch.quantization.get_default_qconfig(backend)\ntorch.quantization.prepare_qat(m, inplace=True)\n\n\"\"\"Training Loop\"\"\"\nn_epochs = 10\nopt = torch.optim.SGD(m.parameters(), lr=0.1)\nloss_fn = lambda out, tgt: torch.pow(tgt-out, 2).mean()\nfor epoch in range(n_epochs):\n x = torch.rand(10,2,24,24)\n out = m(x)\n loss = loss_fn(out, torch.rand_like(out))\n opt.zero_grad()\n loss.backward()\n opt.step()\n\n\"\"\"Convert\"\"\"\nm.eval()\ntorch.quantization.convert(m, inplace=True)\n```", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "## Sensitivity Analysis\nNot all layers respond to quantization equally, some are more sensitive to precision drops than others. Identifying the optimal combination of layers that minimizes accuracy drop is time-consuming, so [[3]] suggest a one-at-a-time sensitivity analysis to identify which layers are most sensitive, and retaining FP32 precision on those. In their experiments, skipping just 2 conv layers (out of a total 28 in MobileNet v1) give them near-FP32 accuracy. Using FX Graph Mode, we can create custom qconfigs to do this easily:\n\n```python\n# ONE-AT-A-TIME SENSITIVITY ANALYSIS \n\nfor quantized_layer, _ in model.named_modules():\n print(\"Only quantizing layer: \", quantized_layer)\n\n # The module_name key allows module-specific qconfigs. \n qconfig_dict = {\"\": None, \n \"module_name\":[(quantized_layer, torch.quantization.get_default_qconfig(backend))]}", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "model_prepared = quantize_fx.prepare_fx(model, qconfig_dict)\n # calibrate\n model_quantized = quantize_fx.convert_fx(model_prepared)\n # evaluate(model)\n```\n\nAnother approach is to compare statistics of the FP32 and INT8 layers; commonly used metrics for these are SQNR (Signal to Quantized Noise Ratio) and Mean-Squre-Error. Such a comparative analysis may also help in guiding further optimizations. \n\n

\n \"Fig\n
\n Fig 8. Comparing model weights and activations\n

\n\nPyTorch provides tools to help with this analysis under the Numeric Suite. Learn more about using Numeric Suite from the [full tutorial](https://pytorch.org/tutorials/prototype/numeric_suite_tutorial.html).\n\n```python\n# extract from https://pytorch.org/tutorials/prototype/numeric_suite_tutorial.html\nimport torch.quantization._numeric_suite as ns", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "def SQNR(x, y):\n # Higher is better\n Ps = torch.norm(x)\n Pn = torch.norm(x-y)\n return 20*torch.log10(Ps/Pn)\n\nwt_compare_dict = ns.compare_weights(fp32_model.state_dict(), int8_model.state_dict())\nfor key in wt_compare_dict:\n print(key, compute_error(wt_compare_dict[key]['float'], wt_compare_dict[key]['quantized'].dequantize()))\n\nact_compare_dict = ns.compare_model_outputs(fp32_model, int8_model, input_data)\nfor key in act_compare_dict:\n print(key, compute_error(act_compare_dict[key]['float'][0], act_compare_dict[key]['quantized'][0].dequantize()))\n\n```\n\n\n## Recommendations for your workflow\n

\n \"Suggested\n
\n Fig 9. Suggested quantization workflow\n

\n Click for larger image ", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "### Points to note\n - Large (10M+ parameters) models are more robust to quantization error. [[2]]\n - Quantizing a model from a FP32 checkpoint provides better accuracy than training an INT8 model from scratch.[[2]]\n - Profiling the model runtime is optional but it can help identify layers that bottleneck inference.\n - Dynamic Quantization is an easy first step, especially if your model has many Linear or Recurrent layers.\n - Use symmetric-per-channel quantization with `MinMax` observers for quantizing weights. Use affine-per-tensor quantization with `MovingAverageMinMax` observers for quantizing activations[[2], [3]]\n - Use metrics like SQNR to identify which layers are most suscpetible to quantization error. Turn off quantization on these layers.\n - Use QAT to fine-tune for around 10% of the original training schedule with an annealing learning rate schedule starting at 1% of the initial training learning rate. [[3]]\n - If the above workflow didn't work for you, we want to know more. Post a thread with details of your code (model architecture, accuracy metric, techniques tried). Feel free to cc me [@suraj.pt](https://discuss.pytorch.org/u/suraj.pt/).", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "That was a lot to digest, congratulations for sticking with it! Next, we'll take a look at quantizing a \"real-world\" model that uses dynamic control structures (if-else, loops). These elements disallow symbolic tracing a model, which makes it a bit tricky to directly quantize the model out of the box. In the next post of this series, we'll get our hands dirty on a model that is chock full of loops and if-else blocks, and even uses third-party libraries in the `forward` call. \n\nWe'll also cover a cool new feature in PyTorch Quantization called Define-by-Run, that tries to ease this constraint by needing only subsets of the model's computational graph to be free of dynamic flow. Check out the [Define-by-Run poster at PTDD'21](https://s3.amazonaws.com/assets.pytorch.org/ptdd2021/posters/C8.png) for a preview.", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "## References\n[[1]] Gholami, A., Kim, S., Dong, Z., Yao, Z., Mahoney, M. W., & Keutzer, K. (2021). A survey of quantization methods for efficient neural network inference. arXiv preprint arXiv:2103.13630.\n\n[[2]] Krishnamoorthi, R. (2018). Quantizing deep convolutional networks for efficient inference: A whitepaper. arXiv preprint arXiv:1806.08342.\n\n[[3]] Wu, H., Judd, P., Zhang, X., Isaev, M., & Micikevicius, P. (2020). Integer quantization for deep learning inference: Principles and empirical evaluation. arXiv preprint arXiv:2004.09602.\n\n[[4]] PyTorch Quantization Docs\n\n\n[1]: https://arxiv.org/pdf/2103.13630.pdf\n[2]: https://arxiv.org/pdf/1806.08342.pdf\n[3]: https://arxiv.org/abs/2004.09602\n[4]: https://pytorch.org/docs/stable/quantization.html#prototype-fx-graph-mode-quantization", "metadata": {"source": "https://pytorch.org/blog/quantization-in-practice/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Towards Reproducible Research with PyTorch Hub'\nauthor: Team PyTorch\nredirect_from: /2019/06/10/pytorch_hub.html\n---\n\nReproducibility is an essential requirement for many fields of research including those based on machine learning techniques. However, many machine learning publications are either not reproducible or are difficult to reproduce. With the continued growth in the number of research publications, including tens of thousands of papers now hosted on arXiv and submissions to conferences at an all time high, research reproducibility is more important than ever. While many of these publications are accompanied by code as well as trained models which is helpful but still leaves a number of steps for users to figure out for themselves.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "We are excited to announce the availability of PyTorch Hub, a simple API and workflow that provides the basic building blocks for improving machine learning research reproducibility. PyTorch Hub consists of a pre-trained model repository designed specifically to facilitate research reproducibility and enable new research. It also has built-in support for [Colab](https://colab.research.google.com/), integration with [*Papers With Code*](https://paperswithcode.com/) and currently contains a broad set of models that include Classification and Segmentation, Generative, Transformers, etc.\n\n
\n \n
\n\n## [Owner] Publishing models", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "PyTorch Hub supports the publication of pre-trained models (model definitions and pre-trained weights) to a GitHub repository by adding a simple ```hubconf.py``` file.\nThis provides an enumeration of which models are to be supported and a list of dependencies needed to run the models.\nExamples can be found in the [torchvision](https://github.com/pytorch/vision/blob/master/hubconf.py), [huggingface-bert](https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/hubconf.py) and [gan-model-zoo](https://github.com/facebookresearch/pytorch_GAN_zoo) repositories.\n\nLet us look at the simplest case: `torchvision`'s `hubconf.py`:\n\n```python\n# Optional list of dependencies required by the package\ndependencies = ['torch']", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "from torchvision.models.alexnet import alexnet\nfrom torchvision.models.densenet import densenet121, densenet169, densenet201, densenet161\nfrom torchvision.models.inception import inception_v3\nfrom torchvision.models.resnet import resnet18, resnet34, resnet50, resnet101, resnet152,\\\nresnext50_32x4d, resnext101_32x8d\nfrom torchvision.models.squeezenet import squeezenet1_0, squeezenet1_1\nfrom torchvision.models.vgg import vgg11, vgg13, vgg16, vgg19, vgg11_bn, vgg13_bn, vgg16_bn, vgg19_bn\nfrom torchvision.models.segmentation import fcn_resnet101, deeplabv3_resnet101\nfrom torchvision.models.googlenet import googlenet\nfrom torchvision.models.shufflenetv2 import shufflenet_v2_x0_5, shufflenet_v2_x1_0\nfrom torchvision.models.mobilenet import mobilenet_v2\n```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "In `torchvision`, the models have the following properties:\n- Each model file can function and be executed independently\n- They dont require any package other than PyTorch (encoded in `hubconf.py` as `dependencies['torch']`)\n- They dont need separate entry-points, because the models when created, work seamlessly out of the box\n\nMinimizing package dependencies reduces the friction for users to load your model for immediate experimentation.\n\nA more involved example is HuggingFace's BERT models. Here is their `hubconf.py`\n\n```python\ndependencies = ['torch', 'tqdm', 'boto3', 'requests', 'regex']\n\nfrom hubconfs.bert_hubconf import (\n bertTokenizer,\n bertModel,\n bertForNextSentencePrediction,\n bertForPreTraining,\n bertForMaskedLM,\n bertForSequenceClassification,\n bertForMultipleChoice,\n bertForQuestionAnswering,\n bertForTokenClassification\n)\n```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "Each model then requires an entrypoint to be created. Here is a code snippet to specify an entrypoint of the ```bertForMaskedLM``` model, which returns the pre-trained model weights.\n\n```python\ndef bertForMaskedLM(*args, **kwargs):\n \"\"\"\n BertForMaskedLM includes the BertModel Transformer followed by the\n pre-trained masked language modeling head.\n Example:\n ...\n \"\"\"\n model = BertForMaskedLM.from_pretrained(*args, **kwargs)\n return model\n```\n\nThese entry-points can serve as wrappers around complex model factories. They can give a clean and consistent help docstring, have logic to support downloading of pretrained weights (for example via `pretrained=True`) or have additional hub-specific functionality such as visualization.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "With a `hubconf.py` in place, you can send a pull request based on the template [here](https://github.com/pytorch/hub/blob/master/docs/template.md).\nOur goal is to curate high-quality, easily-reproducible, maximally-beneficial models for research reproducibility.\nHence, we may work with you to refine your pull request and in some cases reject some low-quality models to be published.\nOnce we accept your pull request, your model will soon appear on [Pytorch hub webpage](https://pytorch.org/hub) for all users to explore.\n\n\n## [User] Workflow\n\nAs a user, PyTorch Hub allows you to follow a few simple steps and do things like: 1) explore available models; 2) load a model; and 3) understand what methods are available for any given model. Let's walk through some examples of each.\n\n### Explore available entrypoints.\n\nUsers can list all available entrypoints in a repo using the ```torch.hub.list()``` API.", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> torch.hub.list('pytorch/vision')\n>>>\n['alexnet',\n'deeplabv3_resnet101',\n'densenet121',\n...\n'vgg16',\n'vgg16_bn',\n'vgg19',\n 'vgg19_bn']\n ```\n\nNote that PyTorch Hub also allows auxillary entrypoints (other than pretrained models), e.g. ```bertTokenizer``` for preprocessing in the [BERT](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_bert/) models, to make the user workflow smoother.\n\n\n### Load a model\n\nNow that we know which models are available in the Hub, users can load a model entrypoint using the ```torch.hub.load()``` API. This only requires a single command without the need to install a wheel. In addition the ```torch.hub.help()``` API can provide useful information about how to instantiate the model.\n\n```python\nprint(torch.hub.help('pytorch/vision', 'deeplabv3_resnet101'))\nmodel = torch.hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)\n```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "It is also common that repo owners will want to continually add bug fixes or performance improvements. PyTorch Hub makes it super simple for users to get the latest update by calling:\n\n```python\nmodel = torch.hub.load(..., force_reload=True)\n```\n\nWe believe this will help to alleviate the burden of repetitive package releases by repo owners and instead allow them to focus more on their research.\nIt also ensures that, as a user, you are getting the freshest available models.\n\nOn the contrary, stability is important for users. Hence, some model owners serve them from a specificed branch or tag, rather than the `master` branch, to ensure stability of the code.\nFor example, `pytorch_GAN_zoo` serves them from the `hub` branch:\n\n```python\nmodel = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub', 'DCGAN', pretrained=True, useGPU=False)\n```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "Note that the ```*args```, ```**kwargs``` passed to `hub.load()` are used to *instantiate* a model. In the above example, `pretrained=True` and `useGPU=False` are given to the model's entrypoint.\n\n\n### Explore a loaded model\n\nOnce you have a model from PyTorch Hub loaded, you can use the following workflow to find out the available methods that are supported as well as understand better what arguments are requires to run it.\n\n\n```dir(model)``` to see all available methods of the model. Let's take a look at `bertForMaskedLM`'s available methods.\n\n```python\n>>> dir(model)\n>>>\n['forward'\n...\n'to'\n'state_dict',\n]\n```\n\n```help(model.forward)``` provides a view into what arguments are required to make your loaded model run\n\n```python\n>>> help(model.forward)\n>>>\nHelp on method forward in module pytorch_pretrained_bert.modeling:\nforward(input_ids, token_type_ids=None, attention_mask=None, masked_lm_labels=None)\n...\n```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "Have a closer look at the [BERT](https://pytorch.org/hub/huggingface_pytorch-pretrained-bert_bert/) and [DeepLabV3](https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/) pages, where you can see how these models can be used once loaded.\n\n### Other ways to explore\n\nModels available in PyTorch Hub also support both [Colab](https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/facebookresearch_pytorch-gan-zoo_pgan.ipynb) and are directly linked on [Papers With Code](https://paperswithcode.com/) and you can get started with a single click. [Here](https://paperswithcode.com/paper/densely-connected-convolutional-networks) is a good example to get started with (shown below).\n\n
\n \n
\n\n## Additional resources:", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "* PyTorch Hub API documentation can be found [here](https://pytorch.org/docs/stable/hub.html).\n* Submit a model [here](https://github.com/pytorch/hub) for publication in PyTorch Hub.\n* Go to [https://pytorch.org/hub](https://pytorch.org/hub) to learn more about the available models.\n* Look for more models to come on [paperswithcode.com](https://paperswithcode.com/).\n\n\nA BIG thanks to the folks at HuggingFace, the PapersWithCode team, fast.ai and Nvidia as well as Morgane Riviere (FAIR Paris) and lots of others for helping bootstrap this effort!!\n\nCheers!\n\nTeam PyTorch\n\n\n\n\n## FAQ:\n\n**Q: If we would like to contribute a model that is already in the Hub but perhaps mine has better accuracy, should I still contribute?**\n\n\nA: Yes!! A next step for Hub is to implement an upvote/downvote system to surface the best models.\n\n**Q: Who hosts the model weights for PyTorch Hub?**", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "A: You, as the contributor, are responsible to host the model weights. You can host your model in your favorite cloud storage or, if it fits within the limits, on GitHub. If it is not within your means to host the weights, check with us via opening an issue on the hub repository.\n\n**Q: What if my model is trained on private data? Should I still contribute this model?**\n\n\nA: No! PyTorch Hub is centered around open research and that extends to the usage of open datasets to train these models on. If a pull request for a proprietary model is submitted, we will kindly ask that you resubmit a model trained on something open and available.\n\n**Q: Where are my downloaded models saved?**\n\n\nA: We follow the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) and adhere to common standards around cached files and directories.\n\nThe locations are used in the order of:", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "* Calling ```hub.set_dir()```\n* ```$TORCH_HOME/hub```, if environment variable ```TORCH_HOME``` is set.\n* ```$XDG_CACHE_HOME/torch/hub```, if environment variable ```XDG_CACHE_HOME``` is set.\n* ```~/.cache/torch/hub```", "metadata": {"source": "https://pytorch.org/blog/towards-reproducible-research-with-pytorch-hub/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'The road to 1.0: production ready PyTorch'\nauthor: The PyTorch Team\nredirect_from: /2018/05/02/road-to-1.0.html\n---\n\nWe would like to give you a preview of the roadmap for PyTorch 1.0 , the next release of PyTorch. Over the last year, we've had 0.2, 0.3 and 0.4 transform PyTorch from a [Torch+Chainer]-like interface into something cleaner, adding double-backwards, numpy-like functions, advanced indexing and removing Variable boilerplate. At this time, we're confident that the API is in a reasonable and stable state to confidently release a 1.0.\n\nHowever, 1.0 isn't just about stability of the interface.\n\nOne of PyTorch's biggest strengths is its first-class Python integration, imperative style, simplicity of the API and options. These are aspects that make PyTorch good for research and hackability.\n\nOne of its biggest downsides has been production-support. What we mean by production-support is the countless things one has to do to models to run them efficiently at massive scale:", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "- exporting to C++-only runtimes for use in larger projects\n- optimizing mobile systems on iPhone, Android, Qualcomm and other systems\n- using more efficient data layouts and performing kernel fusion to do faster inference (saving 10% of speed or memory at scale is a big win)\n- quantized inference (such as 8-bit inference)\n\nStartups, large companies and anyone who wants to build a product around PyTorch have asked for production support. At Facebook (the largest stakeholder for PyTorch) we have Caffe2, which has been the production-ready platform, running in our datacenters and shipping to more than 1 billion phones spanning eight generations of iPhones and six generations of Android CPU architectures. It has server-optimized inference on Intel / ARM, TensorRT support, and all the necessary bits for production. Considering all this value locked-in to a platform that the PyTorch team works quite closely with, **we decided to marry PyTorch and Caffe2 which gives the production-level readiness for PyTorch**.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "Supporting production features without adding usability issues for our researchers and end-users needs creative solutions.\n\n## Production != Pain for researchers\n\nAdding production capabilities involves increasing the API complexity and number of configurable options for models. One configures memory-layouts (NCHW vs NHWC vs N,C/32,H,W,32, each providing different performance characteristics), quantization (8-bit? 3-bit?), fusion of low-level kernels (you used a Conv + BatchNorm + ReLU, let's fuse them into a single kernel), separate backend options (MKLDNN backend for a few layers and NNPACK backend for other layers), etc.\n\nPyTorch's central goal is to provide a great platform for research and hackability. So, while we add all these optimizations, we've been working with a hard design constraint to never trade these off against usability.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "To pull this off, we are introducing `torch.jit`, a just-in-time (JIT) compiler that at runtime takes your PyTorch models and rewrites them to run at production-efficiency. The JIT compiler can also export your model to run in a C++-only runtime based on Caffe2 bits.\n\n> In 1.0, your code continues to work as-is, we're not making any big changes to the existing API.\n\nMaking your model production-ready is an opt-in annotation, which uses the `torch.jit` compiler to export your model to a Python-less environment, and improving its performance. Let's walk through the JIT compiler in detail.\n\n## `torch.jit`: A JIT-compiler for your models", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "We strongly believe that it's hard to match the productivity you get from specifying your models directly as idiomatic Python code. This is what makes PyTorch so flexible, but it also means that PyTorch pretty much never knows the operation you'll run next. This however is a big blocker for export/productionization and heavyweight automatic performance optimizations because they need full upfront knowledge of how the computation will look before it even gets executed.\n\nWe provide two opt-in ways of recovering this information from your code, one based on tracing native python code and one based on compiling a subset of the python language annotated into a python-free intermediate representation. After thorough discussions we concluded that they're both going to be useful in different contexts, and as such you will be able to mix and match them freely.\n\n## Tracing Mode", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "## Tracing Mode\n\nThe PyTorch tracer, `torch.jit.trace`, is a function that records all the native PyTorch operations performed in a code region, along with the data dependencies between them. In fact, PyTorch has had a tracer since 0.3, which has been used for exporting models through ONNX. What changes now, is that you no longer necessarily need to take the trace and run it elsewhere - PyTorch can re-execute it for you, using a carefully designed high-performance C++ runtime. As we develop PyTorch 1.0 this runtime will integrate all the optimizations and hardware integrations that Caffe2 provides.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "The biggest benefit of this approach is that it doesn't really care how your Python code is structured \u2014 you can trace through generators or coroutines, modules or pure functions. Since we only record native PyTorch operators, these details have no effect on the trace recorded. This behavior, however, is a double-edged sword. For example, if you have a loop in your model, it will get unrolled in the trace, inserting a copy of the loop body for as many times as the loop ran. This opens up opportunities for zero-cost abstraction (e.g. you can loop over modules, and the actual trace will be loop-overhead free!), but on the other hand this will also affect data dependent loops (think of e.g. processing sequences of varying lengths), effectively hard-coding a single length into the trace.\n\nFor networks that do not contain loops and if statements, tracing is non-invasive and is robust enough to handle a wide variety of coding styles. This code example illustrates what tracing looks like:", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "```python\n# This will run your nn.Module or regular Python function with the example\n# input that you provided. The returned callable can be used to re-execute\n# all operations that happened during the example run, but it will no longer\n# use the Python interpreter.\nfrom torch.jit import trace\ntraced_model = trace(model, example_input=input)\ntraced_fn = trace(fn, example_input=input)\n\n# The training loop doesn't change. Traced model behaves exactly like an\n# nn.Module, except that you can't edit what it does or change its attributes.\n# Think of it as a \"frozen module\".\nfor input, target in data_loader:\n loss = loss_fn(traced_model(input), target)\n```\n\n## Script Mode\n\nTracing mode is a great way to minimize the impact on your code, but we're also very excited about the models that fundamentally make use of control flow such as RNNs. Our solution to this is a scripting mode.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "In this case you write out a regular Python function, except that you can no longer use certain more complicated language features. Once you isolated the desired functionality, you let us know that you'd like the function to get compiled by decorating it with an `@script` decorator. This annotation will transform your python function directly into our high-performance C++ runtime. This lets us recover all the PyTorch operations along with loops and conditionals. They will be embedded into our internal representation of this function, and will be accounted for every time this function is run.\n\n```python\nfrom torch.jit import script\n\n@script\ndef rnn_loop(x):\n hidden = None\n for x_t in x.split(1):\n x, hidden = model(x, hidden)\n return x\n```\n\n## Optimization and Export\n\nRegardless of whether you use tracing or `@script`, the result is a python-free representation of your model, which can be used to optimize the model or to export the model from python for use in production environments.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "Extracting bigger segments of the model into an intermediate representation makes it possible to do sophisticated whole-program optimizations and to offload computation to specialized AI accelerators which operate on graphs of computation. We have already been developing the beginnings of these optimizations, including passes that fuse GPU operations together to improve the performance of smaller RNN models.\n\nIt also allows us to use existing high-performance backends available in Caffe2 today to run the model efficiently. Additionally, @script functions (and modules!) can be fully exported to ONNX in a way that retains their dynamic nature, such that you can easily run them in a Python-free environment using the model executors from Caffe2 or by transferring the model to any other framework supporting ONNX.\n\n## Usability", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "## Usability\n\n**We care deeply about maintaining our current level of usability and we know that execution of the code not directly in Python leads to harder debugging, but this is something that we think about a lot, and we're making sure that you're not getting locked in to a completely different programming language.**", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "First, we follow the principle of pay for what you use \u2014 if you don't need to optimize or export your model, you do not have to use these new features and won't see any downsides. Furthermore, use of traced or @script modules/functions can be done incrementally. For instance, all of these behaviors are allowed: You can trace part of your model and use the trace in a larger non-traced model. You can use tracing for 90% of your model, and use @script for the one sub-module that actually has some control flow in it. You can write a function using @script and have it call a native python function. If something appears incorrect in an @script function, you can remove the annotation and the code will execute in native python where it is easy to debug using your favorite tools and methods. Think of tracing and @script like type annotations using MyPy or TypeScript \u2014 each additional annotation can be tested incrementally, and none are required until you want to optimize or productionize.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "Most importantly, these modes will be built into the core of PyTorch so that mixing and matching them with your existing code can happen seamlessly.\n\n_Note: The name JIT for these components is a bit of a misnomer and comes from historical reasons. The tracing/function execution in PyTorch started out as an optimizing JIT compiler that generated fused CUDA kernels but then grew to encompass optimization, @script, and export. When it is ready for release we will likely rename this functionality to the hybrid frontend, but we wanted to present it here as it is named in the code so that you can follow along as we develop it._\n\n## Other changes and improvements\n\nProduction support is the big feature for 1.0, but we will continue optimizing and fixing other parts of PyTorch as course of the standard release process.", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "On the backend side of things, PyTorch will see some changes, which might affect user-written C and C++ extensions. We are replacing (or refactoring) the backend ATen library to incorporate features and optimizations from Caffe2.\n\n## Last Words\n\nWe aim to release 1.0 some time during the summer. You can follow-along our progress on the [Pull Requests](https://github.com/pytorch/pytorch/pulls) page.\n\nYou can read this from the perspective of the Caffe2 project at: [https://caffe2.ai/blog/2018/05/02/Caffe2_PyTorch_1_0.html](https://caffe2.ai/blog/2018/05/02/Caffe2_PyTorch_1_0.html)", "metadata": {"source": "https://pytorch.org/blog/the-road-to-1_0/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch adds new tools and libraries, welcomes Preferred Networks to its community'\nauthor: Team PyTorch\n---\n\nPyTorch continues to be used for the latest state-of-the-art research on display at the NeurIPS conference next week, making up nearly [70% of papers](https://chillee.github.io/pytorch-vs-tensorflow/) that cite a framework. In addition, we\u2019re excited to welcome Preferred Networks, the maintainers of the Chainer framework, to the PyTorch community. Their teams are moving fully over to PyTorch for developing their ML capabilities and services.\n\nThis growth underpins PyTorch\u2019s focus on building for the needs of the research community, and increasingly, supporting the full workflow from research to production deployment. To further support researchers and developers, we\u2019re launching a number of new tools and libraries for large scale computer vision and elastic fault tolerant training. Learn more on GitHub and at our NeurIPS booth.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "## Preferred Networks joins the PyTorch community\n\nPreferred Networks, Inc. (PFN) announced plans to move its deep learning framework from Chainer to PyTorch. As part of this change, PFN will collaborate with the PyTorch community and contributors, including people from Facebook, Microsoft, CMU, and NYU, to participate in the development of PyTorch.\n\nPFN developed Chainer, a deep learning framework that introduced the concept of define-by-run (also referred to as eager execution), to support and speed up its deep learning development. Chainer has been used at PFN since 2015 to rapidly solve real-world problems with the latest, cutting-edge technology. Chainer was also one of the inspirations for PyTorch\u2019s initial design, as outlined in the [PyTorch NeurIPS](https://papers.nips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library) paper.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "PFN has driven innovative work with [CuPy](https://cupy.chainer.org/), ImageNet in 15 minutes, [Optuna](https://optuna.org/), and other projects that have pushed the boundaries of design and engineering. As part of the PyTorch community, PFN brings with them creative engineering capabilities and experience to help take the framework forward. In addition, PFN\u2019s migration to PyTorch will allow it to efficiently incorporate the latest research results to accelerate its R&D activities, [given PyTorch\u2019s broad adoption with researchers](https://thegradient.pub/state-of-ml-frameworks-2019-pytorch-dominates-research-tensorflow-dominates-industry/), and to collaborate with the community to add support for PyTorch on MN-Core, a deep learning processor currently in development.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "We are excited to welcome PFN to the PyTorch community, and to jointly work towards the common goal of furthering advances in deep learning technology. Learn more about the PFN\u2019s migration to PyTorch [here](https://preferred.jp/en/news/pr20191205/).\n\n## Tools for elastic training and large scale computer vision\n\n### PyTorch Elastic (Experimental)\n\nLarge scale model training is becoming commonplace with architectures like BERT and the growth of model parameter counts into the billions or even tens of billions. To achieve convergence at this scale in a reasonable amount of time, the use of distributed training is needed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "The current PyTorch Distributed Data Parallel (DDP) module enables data parallel training where each process trains the same model but on different shards of data. It enables bulk synchronous, multi-host, multi-GPU/CPU execution of ML training. However, DDP has several shortcomings; e.g. jobs cannot start without acquiring all the requested nodes; jobs cannot continue after a node fails due to error or transient issue; jobs cannot incorporate a node that joined later; and lastly; progress cannot be made with the presence of a slow/stuck node.\n\nThe focus of [PyTorch Elastic](https://github.com/pytorch/elastic), which uses Elastic Distributed Data Parallelism, is to address these issues and build a generic framework/APIs for PyTorch to enable reliable and elastic execution of these data parallel training workloads. It will provide better programmability, higher resilience to failures of all kinds, higher-efficiency and larger-scale training compared with pure DDP.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "Elasticity, in this case, means both: 1) the ability for a job to continue after node failure (by running with fewer nodes and/or by incorporating a new host and transferring state to it); and 2) the ability to add/remove nodes dynamically due to resource availability changes or bottlenecks.\n\nWhile this feature is still experimental, you can try it out on AWS EC2, with the instructions [here](https://github.com/pytorch/elastic/tree/master/aws). Additionally, the PyTorch distributed team is working closely with teams across AWS to support PyTorch Elastic training within services such as Amazon Sagemaker and Elastic Kubernetes Service (EKS). Look for additional updates in the near future.\n\n### New Classification Framework", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "Image and video classification are at the core of content understanding. To that end, you can now leverage a new end-to-end framework for large-scale training of state-of-the-art image and video classification models. It allows researchers to quickly prototype and iterate on large distributed training jobs at the scale of billions of images. Advantages include:\n\n* Ease of use - This framework features a modular, flexible design that allows anyone to train machine learning models on top of PyTorch using very simple abstractions. The system also has out-of-the-box integration with AWS on PyTorch Elastic, facilitating research at scale and making it simple to move between research and production.\n* High performance - Researchers can use the framework to train models such as Resnet50 on ImageNet in as little as 15 minutes.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "You can learn more at the [NeurIPS Expo workshop](https://nips.cc/ExpoConferences/2019/schedule?workshop_id=16) on Multi-Modal research to production or get started with the PyTorch Elastic Imagenet example [here](https://github.com/pytorch/elastic/blob/master/examples/imagenet/main.py).\n\n## Come see us at NeurIPS\n\nThe PyTorch team will be hosting workshops at NeurIPS during the industry expo on 12/8. Join the sessions below to learn more, and visit the team at the PyTorch booth on the show floor and during the Poster Session. At the booth, we\u2019ll be walking through an interactive demo of PyTorch running fast neural style transfer on a Cloud TPU - here\u2019s a [sneak peek](https://colab.research.google.com/github/pytorch/xla/blob/master/contrib/colab/style_transfer_inference-xrt-1-15.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "We\u2019re also publishing a [paper that details the principles that drove the implementation of PyTorch](https://papers.nips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library) and how they\u2019re reflected in its architecture.\n\n*[Multi-modal Research to Production](https://nips.cc/ExpoConferences/2019/schedule?workshop_id=16)* - This workshop will dive into a number of modalities such as computer vision (large scale image classification and instance segmentation) and Translation and Speech (seq-to-seq Transformers) from the lens of taking cutting edge research to production. Lastly, we will also walk through how to use the latest APIs in PyTorch to take eager mode developed models into graph mode via Torchscript and quantize them for scale production deployment on servers or mobile devices. Libraries used include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "* Classification Framework - a newly open sourced PyTorch framework developed by Facebook AI for research on large-scale image and video classification. It allows researchers to quickly prototype and iterate on large distributed training jobs. Models built on the framework can be seamlessly deployed to production.\n* Detectron2 - the recently released object detection library built by the Facebook AI Research computer vision team. We will articulate the improvements over the previous version including: 1) Support for latest models and new tasks; 2) Increased flexibility, to enable new computer vision research; 3) Maintainable and scalable, to support production use cases.\n* Fairseq - general purpose sequence-to-sequence library, can be used in many applications, including (unsupervised) translation, summarization, dialog and speech recognition.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} {"page_content": "*[Responsible and Reproducible AI](https://nips.cc/ExpoConferences/2019/schedule?workshop_id=14)* - This workshop on Responsible and Reproducible AI will dive into important areas that are shaping the future of how we interpret, reproduce research, and build AI with privacy in mind. We will cover major challenges, walk through solutions, and finish each talk with a hands-on tutorial.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Reproducibility: As the number of research papers submitted to arXiv and conferences skyrockets, scaling reproducibility becomes difficult. We must address the following challenges: aid extensibility by standardizing code bases, democratize paper implementation by writing hardware agnostic code, facilitate results validation by documenting \u201ctricks\u201d authors use to make their complex systems function. To offer solutions, we will dive into tool like PyTorch Hub and PyTorch Lightning which are used by some of", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "the top researchers in the world to reproduce the state of the art.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Interpretability: With the increase in model complexity and the resulting lack of transparency, model interpretability methods have become increasingly important. Model understanding is both an active area of research as well as an area of focus for practical applications across industries using machine learning. To get hands on, we will use the recently released Captum library that provides state-of-the-art algorithms to provide researchers and developers with an easy way to understand the importance of", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "neurons/layers and the predictions made by our models.`", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "* Private AI: Practical applications of ML via cloud-based or machine-learning-as-a-service platforms pose a range of security and privacy challenges. There are a number of technical approaches being studied including: homomorphic encryption, secure multi-party computation, trusted execution environments, on-device computation, and differential privacy. To provide an immersive understanding of how some of these technologies are applied, we will use the CrypTen project which provides a community based", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "research platform to take the field of Private AI forward.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} +{"page_content": "* Reproducibility: As the number of research papers submitted to arXiv and conferences skyrockets, scaling reproducibility becomes difficult. We must address the following challenges: aid extensibility by standardizing code bases, democratize paper implementation by writing hardware agnostic code, facilitate results validation by documenting \u201ctricks\u201d authors use to make their complex systems function. To offer solutions, we will dive into tool like PyTorch Hub and PyTorch Lightning which are used by some of the top researchers in the world to reproduce the state of the art.\n* Interpretability: With the increase in model complexity and the resulting lack of transparency, model interpretability methods have become increasingly important. Model understanding is both an active area of research as well as an area of focus for practical applications across industries using machine learning. To get hands on, we will use the recently released Captum library that provides state-of-the-art algorithms to provide researchers and developers with an easy way to understand the importance of neurons/layers and the predictions made by our models.`\n* Private AI: Practical applications of ML via cloud-based or machine-learning-as-a-service platforms pose a range of security and privacy challenges. There are a number of technical approaches being studied including: homomorphic encryption, secure multi-party computation, trusted execution environments, on-device computation, and differential privacy. To provide an immersive understanding of how some of these technologies are applied, we will use the CrypTen project which provides a community based research platform to take the field of Private AI forward.", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} {"page_content": "*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"How Computational Graphs are Executed in PyTorch\"\nauthor: Preferred Networks\nfeatured-img: \"\"\n---\n\nWelcome to the last entry into understanding the autograd engine of PyTorch series!\nIf you haven\u2019t read parts [1](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/) & [2](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/) check them now to understand how PyTorch creates the computational graph for the backward pass!", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This post is based on PyTorch v1.11, so some highlighted parts may differ across versions.\n\n# PyTorch autograd graph execution\n\nThe last post showed how PyTorch constructs the graph to calculate the outputs' derivatives w.r.t. the inputs when executing the forward pass. Now we will see how the execution of the backward pass is coordinated and done by looking at the whole process, starting from Python down to the lower C++ level internals.\n\n# What Happens when Calling `backward()`/`grad()` from Python", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Using `variable.backward()`\n\nAfter doing all our calculations with an input set to require the gradient, we call `.backward()` on the result to initiate the backward pass execution.\n\n```python\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> y = torch.exp(x).sum()\n>>> y.backward()", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Calling [`.backward()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/_tensor.py#L307-L363) on a tensor results in a call to [`torch.autograd.backward()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L85-L175).\n```python\n# torch/_tensor.py", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "def backward(self, gradient=None, retain_graph=None, create_graph=False, inputs=None):\n \u2026\n torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)\n\n```\n`torch.autograd.backward()` checks the arguments and calls the autograd engine in the C++ layer.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "``` python\ndef backward(\n tensors: _TensorOrTensors,\n grad_tensors: Optional[_TensorOrTensors] = None,\n retain_graph: Optional[bool] = None,\n create_graph: bool = False,\n grad_variables: Optional[_TensorOrTensors] = None,\n inputs: Optional[_TensorOrTensors] = None,\n) -> None:\n \u2026\n\n if inputs is not None and len(inputs) == 0:\n raise RuntimeError(\"'inputs' argument to backward() cannot be empty.\")", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "tensors = (tensors,) if isinstance(tensors, torch.Tensor) else tuple(tensors)\n inputs = (inputs,) if isinstance(inputs, torch.Tensor) else \\\n tuple(inputs) if inputs is not None else tuple()\n\n grad_tensors_ = _tensor_or_tensors_to_tuple(grad_tensors, len(tensors))\n grad_tensors_ = _make_grads(tensors, grad_tensors_)\n if retain_graph is None:\n retain_graph = create_graph", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Variable._execution_engine.run_backward(\n tensors, grad_tensors_, retain_graph, create_graph, inputs,\n allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "First, whether the `grad_tensors` argument was specified or not, there is a call to the [`_make_grads`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L30-L74) function. This is used to check the provided `grad_tensors` or to specify the default value for them by looking at the `tensors` argument values\u2019 shapes. Check the first blog post for details on the default value for the `grad_tensors` of the backward pass. This function just provides the", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "vector of the vector jacobian product if it was not initially specified.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the above code, `Variable` has an `_execution_engine` attribute that is defined in [`torch.autograd.variable`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/variable.py#L14) to be of type `ImperativeEngine`; the C++ engine exported to python and declared in [`torch/csrc/autograd/python_engine.cpp`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/python_engine.cpp#L384). In the following sections, we", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "explain in detail how this object executes the backward pass.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Note that the `torch.autograd.backward` function has an `inputs` optional argument. This argument is used when we want to calculate the `.grad` field of only a subset of input tensors in the forward pass.\n\n```python\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> y = torch.tensor([0.1, 0.90], requires_grad=True)\n>>> z = torch.exp(x * y).sum()\n>>> torch.autograd.backward([z], inputs=[x])\n>>> x.grad\ntensor([0.1051, 1.7676])\n>>> y.grad # None\n>>>\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Using `torch.autograd.grad`\n\nAn alternative to `backward()` is to use [`torch.autograd.grad()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L177-L277). The main difference to `backward()` is that `grad()` returns a tuple of tensors with the gradients of the `outputs` w.r.t. the `inputs` kwargs instead of storing them in the `.grad` field of the tensors. As you can see, the `grad()` code shown below is very similar to backward.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\ndef grad(\n outputs: _TensorOrTensors,\n inputs: _TensorOrTensors,\n grad_outputs: Optional[_TensorOrTensors] = None,\n retain_graph: Optional[bool] = None,\n create_graph: bool = False,\n only_inputs: bool = True,\n allow_unused: bool = False,\n is_grads_batched: bool = False\n) -> Tuple[torch.Tensor, ...]:\n \n outputs = (outputs,) if isinstance(outputs, torch.Tensor) else tuple(outputs)\n inputs = (inputs,) if isinstance(inputs, torch.Tensor) else tuple(inputs)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "overridable_args = outputs + inputs\n if has_torch_function(overridable_args):\n return handle_torch_function(\n grad,\n overridable_args,\n outputs,\n inputs,\n grad_outputs=grad_outputs,\n retain_graph=retain_graph,\n create_graph=create_graph,\n only_inputs=only_inputs,\n allow_unused=allow_unused,\n )", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "grad_outputs_ = _tensor_or_tensors_to_tuple(grad_outputs, len(outputs))\n grad_outputs_ = _make_grads(outputs, grad_outputs_)\n\n if retain_graph is None:\n retain_graph = create_graph\n\n if is_grads_batched:\n # \u2026. It will not be covered here\n else:\n return Variable._execution_engine.run_backward(\n outputs, grad_outputs_, retain_graph, create_graph, inputs,\n allow_unused, accumulate_grad=False) # Calls into the C++ engine to run the backward pass", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Figure 1 shows the computational graph with the `backward()` and `grad()` arguments highlighted in red and blue, respectively:\n\n

\n \n

\n\n

\nFgiure 1: Correspondence of `backward`/`grad` arguments in the graphs.\n

\n\n# Going Inside the Autograd Engine", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Refreshing Concepts: Nodes and Edges\n\nAs we saw in [2](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/)\nThe computational graph comprises `Node` and `Edge` objects. Please read that post if you haven\u2019t done it yet.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Nodes", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`Node` objects are defined in [`torch/csrc/autograd/function.h`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L105-L176), and they provide an overload of `operator()` for the associated function and a list of edges to do the graph traversal. Note that `Node` is a base class that autograd functions inherit from and override the `apply` method to execute the backward function.\n```c++\nstruct TORCH_API Node : std::enable_shared_from_this {", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "...\n /// Evaluates the function on the given inputs and returns the result of the\n /// function call.\n variable_list operator()(variable_list&& inputs) {\n ...\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "protected:\n /// Performs the `Node`'s actual operation.\n virtual variable_list apply(variable_list&& inputs) = 0;\n \u2026\n edge_list next_edges_;\n uint64_t topological_nr_ = 0;\n \u2026", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "There is an attribute called [`topological_nr_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L481) in every node object. This number is used to optimize the graph execution as it allows to discard of graph branches under certain conditions. The topological number is the longest distance between this node and any leaf node and it is shown in Figure 2. Its main property is that for any pair of nodes `x`, `y` in a directed graph `topo_nr(x) <", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "topo_nr(y)` means that there is no path from `x` to `y`. So this allows for reducing the number of paths in the graph in need of traversal. Check the [topological_nr](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L314-L343)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": ") method comment for further details.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFigure 2: Example of the Topological Number calculation\n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Edges\n\nThe [`Edge`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/edge.h#L14-L39) object links `Node`s together, and its implementation is straightforward.\n\n```c++\nstruct Edge {\n ...\n /// The function this `Edge` points to.\n std::shared_ptr function;\n /// The identifier of a particular input to the function.\n uint32_t input_nr;\n};", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "It only requires a function pointer to the `Node` and an input number that is the index of the output from the forward function this edge points to. When preparing the set of gradients before calling \"function\", we know that what is flowing from this edge should be accumulated in the \"input_nr\"th argument. Note that the input/output name is flipped here and this is the input to the backward function.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`Edge` objects are constructed using the [`gradient_edge`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/variable.cpp#L221-L233) function method.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n Edge gradient_edge(const Variable& self) {\n if (const auto& gradient = self.grad_fn()) {\n return Edge(gradient, self.output_nr());\n } else {\n return Edge(grad_accumulator(self), 0);\n }\n }\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Entering the C++ Realm", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Once that `torch.autograd.backward()` has been invoked, the\n[`THPEngine_run_backward`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/python_engine.cpp#L152-L286) routine starts the graph traversal. Following is a schema of the function body:\n```c++\nPyObject *THPEngine_run_backward(PyObject *self, PyObject *args, PyObject *kwargs)\n{\n HANDLE_TH_ERRORS\n PyObject *tensors = nullptr;\n PyObject *grad_tensors = nullptr;\n unsigned char keep_graph = 0;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "unsigned char create_graph = 0;\n PyObject *inputs = nullptr;\n \n // Convert the python arguments to C++ objects\n const char *accepted_kwargs[] = { // NOLINT\n \"tensors\", \"grad_tensors\", \"keep_graph\", \"create_graph\", \"inputs\",\n \"allow_unreachable\", \"accumulate_grad\", nullptr\n };\n if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OObb|Obb\", (char**)accepted_kwargs,\n &tensors, &grad_tensors, &keep_graph, &create_graph, &inputs, &allow_unreachable, &accumulate_grad))", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Prepare arguments\n for(const auto i : c10::irange(num_tensors)) {\n // Check that the tensors require gradients\n }\n\n std::vector output_edges;\n if (inputs != nullptr) {\n // Prepare outputs\n }\n\n {\n // Calls the actual autograd engine\n pybind11::gil_scoped_release no_gil;\n outputs = engine.execute(roots, grads, keep_graph, create_graph, accumulate_grad, output_edges);\n }\n // Clean up and finish\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "First, we prepare the input arguments after converting the `PyObject` arguments to actual C++ objects. The `tensors` list contains the tensors from which we start the backward pass. These tensors are converted to edges using `torch::autograd::impl::gradient_edge` and added to a list called `roots` where the graph traversal starts.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n edge_list roots;\n roots.reserve(num_tensors);\n variable_list grads;\n grads.reserve(num_tensors);\n for(const auto i : c10::irange(num_tensors)) {\n PyObject *_tensor = PyTuple_GET_ITEM(tensors, i);\n const auto& variable = THPVariable_Unpack(_tensor);\n auto gradient_edge = torch::autograd::impl::gradient_edge(variable);\n roots.push_back(std::move(gradient_edge));", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "PyObject *grad = PyTuple_GET_ITEM(grad_tensors, i);\n if (THPVariable_Check(grad)) {\n const Variable& grad_var = THPVariable_Unpack(grad);\n grads.push_back(grad_var);\n } \n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Now, if the `inputs` argument was specified in `backward` or we used the `torch.autograd.grad` api, the following code creates a list of edges to accumulate the gradients in the specified tensors at the end of the computation. The engine uses this later to optimize the execution as it doesn\u2019t add the gradients in all the leaf nodes, just the specified ones.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n std::vector output_edges;\n if (inputs != nullptr) {\n int num_inputs = PyTuple_GET_SIZE(inputs);\n output_edges.reserve(num_inputs);\n for (const auto i : c10::irange(num_inputs)) {\n PyObject *input = PyTuple_GET_ITEM(inputs, i);\n const auto& tensor = THPVariable_Unpack(input);\n const auto output_nr = tensor.output_nr();\n auto grad_fn = tensor.grad_fn();\n if (!grad_fn) {\n grad_fn = torch::autograd::impl::try_get_grad_accumulator(tensor);\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (accumulate_grad) {\n tensor.retain_grad();\n }\n if (!grad_fn) {\n output_edges.emplace_back(std::make_shared(), 0);\n } else {\n output_edges.emplace_back(grad_fn, output_nr);\n }\n }\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The next step is the actual graph traversal and node function execution, and finally, the cleanup and return.\n\n```c++\n {\n // Calls the actual autograd engine\n pybind11::gil_scoped_release no_gil;\n auto& engine = python::PythonEngine::get_python_engine();\n outputs = engine.execute(roots, grads, keep_graph, create_graph, accumulate_grad, output_edges);\n }\n // Clean up and finish\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Starting the Real Execution\n\n`engine.execute`is present in [torch/csrc/autograd/engine.cpp](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L969-L1044) \n\nThere are two differentiated steps here:\n\nAnalyze the graph to find dependencies between functions\nCreate worker threads that traverse the graph", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Data Structures Used for the Execution", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "GraphTask\n\nAll the execution metadata is managed by the [`GraphTask`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L51-L196) class in [torch/csrc/autograd/engine.h](https://github.com/pytorch/pytorch/blob/release/1.11/torch/csrc/autograd/engine.h)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nstruct GraphTask: std::enable_shared_from_this {\n std::atomic outstanding_tasks_{0};\n // \u2026 \n std::unordered_map not_ready_;\n std::unordered_map dependencies_;\n\n struct ExecInfo {\n // \u2026\n };\n std::unordered_map exec_info_;\n std::vector captured_vars_;\n // \u2026\n std::shared_ptr cpu_ready_queue_;\n};", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here we see a series of variables dedicated to maintaining the execution state.\n`outstanding_tasks_` tracks the number of tasks left to be executed for the backward pass to complete. `not_ready_` holds the input arguments for the `Node`s that are not ready to be executed. `dependencies_` track the number of predecessors that a `Node` has. As the count reaches `0`, the `Node` is ready for execution; it is placed in a ready queue to be retrieved and executed later.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`exec_info_` and the associated `ExecInfo` struct are used only when the `inputs` argument is specified or it is a call to `autograd.grad()`. They allow filter paths on the graph that are not needeed since only the gradients are calculated only for the variables in the `inputs` list.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`captured_vars_` is where the results of the graph execution are temporarily stored if we used the `torch.autograd.grad()` api instead of `torch.autograd.backward()` since `grad()` returns the gradients as tensors instead of just filling the `.grad` field of the inputs.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "NodeTask", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The [`NodeTask`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L210-L242) struct is a basic class that holds an `fn_` pointer to the node to execute, and an `inputs_` buffer to store the input arguments to this function. Note that the functions executed by the backward pass are the derivatives specified in the `derivatives.yaml` file. or the user provided backward function when using custom functions as described in the second blog post.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The `inputs_` buffer is also where the output gradients of the previously executed functions are aggregated, and it is defined as a [`std::vector` container](https://github.com/pytorch/pytorch/blob/release/1.10/torch/csrc/autograd/input_buffer.h) with facilities to accumulate values at a given position.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nstruct NodeTask {\n std::weak_ptr base_;\n std::shared_ptr fn_;\n // This buffer serves as an implicit \"addition\" node for all of the\n // gradients flowing here. Once all the dependencies are finished, we\n // use the contents of this buffer to run the function.\n InputBuffer inputs_;\n};\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "GraphRoot\n\nThe [`GraphRoot`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/basic_ops.h#L72-L89) is a special function used to hold multiple input variables in a single place. The code is pretty simple as it only acts as a container of variables.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nstruct TORCH_API GraphRoot : public Node {\n GraphRoot(edge_list functions, variable_list inputs)\n : Node(std::move(functions)),\n outputs(std::move(inputs)) {\n for (const auto& t : outputs) {\n add_input_metadata(t);\n }\n }\n\n variable_list apply(variable_list&& inputs) override {\n return outputs;\n }\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "AccumulateGrad\n\nThis function is set during the graph creation in `gradient_edge` when the `Variable` object doesn\u2019t have a `grad_fn`. This is, it is a leaf node.\n\n```c++\n if (const auto& gradient = self.grad_fn()) {\n // \u2026\n } else {\n return Edge(grad_accumulator(self), 0);\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The function body is defined in [`torch/csrc/autograd/functions/accumulate_grad.cpp`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/accumulate_grad.cpp#L25-L63) and it essentially accumulates the input grads in the object\u2019s `.grad` attribute.\n\n```c++\nauto AccumulateGrad::apply(variable_list&& grads) -> variable_list {\n check_input_variables(\"AccumulateGrad\", grads, 1, 0);\n \u2026", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "at::Tensor new_grad = callHooks(variable, std::move(grads[0]));\n std::lock_guard lock(mutex_);\n\n at::Tensor& grad = variable.mutable_grad();\n accumulateGrad(\n variable,\n grad,\n new_grad,\n 1 + !post_hooks().empty() /* num_expected_refs */,\n [&grad](at::Tensor&& grad_update) { grad = std::move(grad_update); });\n return variable_list();\n}\n}} // namespace torch::autograd", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`accumulateGrad`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/accumulate_grad.h#L100)\ndoes several checks on the tensors format and eventually performs the `variable_grad += new_grad;` accumulation.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Preparing the graph for execution\n\nNow, let\u2019s walk through [`Engine::execute`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L969-L1126). The first thing to do besides arguments consistency checks is to create the actual `GraphTask` object we described above. This object keeps all the metadata of the graph execution.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nauto Engine::execute(const edge_list& roots,\n const variable_list& inputs,\n bool keep_graph,\n bool create_graph,\n bool accumulate_grad,\n const edge_list& outputs) -> variable_list {\n\n validate_outputs(roots, const_cast(inputs), [](const std::string& msg) {\n return msg;\n });\n\n // Checks", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "auto graph_task = std::make_shared(\n /* keep_graph */ keep_graph,\n /* create_graph */ create_graph,\n /* depth */ not_reentrant_backward_call ? 0 : total_depth + 1,\n /* cpu_ready_queue */ local_ready_queue);\n\n // If we receive a single root, skip creating extra root node\n // \u2026\n // Prepare graph by computing dependencies\n // \u2026\n // Queue the root \n // \u2026\n // launch execution\n // \u2026\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "After creating the `GraphTask`, we use its associated function if we only have one root node. If we have multiple root nodes, we create a special `GraphRoot` object as described before.\n\n```c++\n bool skip_dummy_node = roots.size() == 1;\n auto graph_root = skip_dummy_node ?\n roots.at(0).function :\n std::make_shared(roots, inputs);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The next step is to fill the `dependencies_` map in the `GraphTask` object since the engine must know when it can execute a task. The `outputs` here is the `inputs` argument passed to the `torch.autograd.backward()` call in Python. But here, we have reversed the names since the gradients w.r.t. the inputs of the forward pass are now the outputs of the backward pass. And from now on, there is no concept of forward/backward, but only graph traversal and execution.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n auto min_topo_nr = compute_min_topological_nr(outputs);\n // Now compute the dependencies for all executable functions\n compute_dependencies(graph_root.get(), *graph_task, min_topo_nr);\n\n if (!outputs.empty()) {\n graph_task->init_to_execute(*graph_root, outputs, accumulate_grad, min_topo_nr);\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here we preprocess the graph for the execution of the nodes. First, [`compute_min_topological_nr`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L922-L933) is called to to obtain the minimum topological number of the tensors specified in `outputs` (0 if no `inputs` kwarg was supplied to `.backward` or `input` for `.grad`). This computation prunes paths in the graph that lead to input variables of which we don\u2019t want/need to calculate the", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "grads.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Second, is the [`compute_dependencies`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L935-L967) call. This function is a very simple graph traversal that starts with the root `Node`, and for each of the edges in `node.next_edges()` it increments the counter in `dependencies_`. Figure 3 shows the result of the dependencies calculation for the example graph. Note that the number of dependencies of any node is just the number of edges arriving", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "at it.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFigure 3: Number of dependencies for each node\n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Finally, the [`init_to_execute`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1281-L1383) call, this is the one that populates the `GraphTask::exec_info_` map in case that `inputs` were specified in the python `backward` call. It iterates the graph again, starting from the root, and records in the `exec_info_` map the intermediate nodes needed to calculate only the given `inputs` gradients.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n // Queue the root\n if (skip_dummy_node) {\n InputBuffer input_buffer(roots.at(0).function->num_inputs());\n auto input = inputs.at(0);\n\n\n input_buffer.add(roots.at(0).input_nr,\n std::move(input),\n input_stream,\n opt_next_stream);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "execute_with_graph_task(graph_task, graph_root, std::move(input_buffer));\n } else {\n execute_with_graph_task(graph_task, graph_root, InputBuffer(variable_list()));\n }\n // Avoid a refcount bump for the Future, since we check for refcount in\n // DistEngine (see TORCH_INTERNAL_ASSERT(futureGrads.use_count() == 1)\n // in dist_engine.cpp).\n auto& fut = graph_task->future_result_;\n fut->wait();\n return fut->value().toTensorVector();\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "And now, we are ready to start the actual execution by creating the `InputBuffer`. In case we only have one root variable, we begin by copying the value of the `inputs` tensor (this is the `gradients` passed to python `backward`) in position 0 of the input_buffer. This is a small optimization that avoids running the `RootNode` for no reason. Also, if the rest of the graph is not on the cpu, we directly start on that worker while the `RootNode` is always placed on the cpu ready queue. Details of the workers", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "and ready queues are explained in the section below.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "On the other hand, if we have multiple roots, the `GraphRoot` object also holds the inputs, so it is enough to pass it an empty `InputBuffer`.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Graph Traversal and Node Execution", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Devices, Threads and Queues\n\nBefore diving into the actual execution, we need to see how the engine is structured.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "First of all, the engine is multithreaded with one thread per device. For example, the caller thread is associated with the CPU while additional threads are created and associated with each GPU or other devices available in the system. Each thread tracks its device using thread-local storage in the [`worker_device`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L69) variable. In addition, the threads have a queue of tasks to be executed also", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "located in thread-local storage, the [`local_ready_queue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L103-L104). This is where work is queued for this thread to execute in the `thread_main` function that is explained later.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "You will wonder how the device where a task should be executed is decided. The `InputBuffer` class has a [`device()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/input_buffer.cpp#L173-L189) function that returns the first non-cpu device of all its tensors.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This function is used together with [`Engine::ready_queue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1181-L1190) to select the queue to queue a task.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nauto Engine::ready_queue(std::shared_ptr cpu_ready_queue, at::Device device) -> std::shared_ptr{\n if (device.type() == at::kCPU || device.type() == at::DeviceType::Meta) {\n return cpu_ready_queue;\n } else {\n // See Note [Allocating GPUs to autograd threads]\n return device_ready_queues_.at(device.index());\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The [`ReadyQueue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L245-L283) object is defined in `torch/csrc/autograd/engine.h` and it is a simple wrapper over `std::priority_queue` that allows a thread to [wait for a task](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L219) if it\u2019s empty. One interesting property of the `ReadyQueue` is that it increases the", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`GraphTask::outstanding_tasks_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L195) value used to determine if the execution has completed or not.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nauto ReadyQueue::push(NodeTask item, bool incrementOutstandingTasks) -> void {\n {\n std::lock_guard lock(mutex_);\n if (incrementOutstandingTasks) {\n std::shared_ptr graph_task = item.base_.lock();\n ++graph_task->outstanding_tasks_;\n }\n heap_.push(std::move(item));\n }\n not_empty_.notify_one();\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "auto ReadyQueue::pop() -> NodeTask {\n std::unique_lock lock(mutex_);\n not_empty_.wait(lock, [this]{ return !heap_.empty(); });\n auto task = std::move(const_cast(heap_.top())); heap_.pop();\n return task;\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Reentrant Backward\n\nA reentrant backward happens when one of the tasks in a backward pass calls again `backward`. It is not a very common case, but it can be used to reduce memory utilization as it could potentially avoid saving intermediate results. For more information, check this [PyTorch forum post](https://discuss.pytorch.org/t/what-is-the-scenario-of-reentrant-backwards-in-pytorch-source-code/19330/2).", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nclass ReentrantBackward(torch.autograd.Function):\n @staticmethod\n def forward(ctx, input):\n return input.sum()\n\n @staticmethod\n def backward(ctx, input):\n # Let's compute the backward by using autograd\n input = input.detach().requires_grad_()\n with torch.enable_grad():\n out = input.sum()\n out.backward() # REENTRANT CALL!!\n return out.detach()", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here, we call `backward()` inside `backward()` for a user custom-defined autograd function.\nThis situation can lead to deadlocks because the first backward needs to wait for the second one to complete. But some internal implementation details can prevent the second backward from completing as it is explained in the dedicated subsection.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Thread Initialization\n\n[`execute_with_graph_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1054-L1126) is in charge of initializing the threads taking care of the computation and placing the `root` node in the queue of the device that produced it.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nc10::intrusive_ptr Engine::execute_with_graph_task(\n const std::shared_ptr& graph_task,\n std::shared_ptr graph_root,\n InputBuffer&& input_buffer) {\n\n initialize_device_threads_pool();\n // Lock mutex for GraphTask.\n std::unique_lock lock(graph_task->mutex_);\n\n auto queue = ready_queue(graph_task->cpu_ready_queue_, input_buffer.device());", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (worker_device == NO_DEVICE) {\n set_device(CPU_DEVICE);\n graph_task->owner_ = worker_device;\n queue->push(NodeTask(graph_task, std::move(graph_root), std::move(input_buffer)));\n lock.unlock();\n thread_main(graph_task);\n worker_device = NO_DEVICE;\n } else {\n // This deals with reentrant backwards, we will see it later.\n }\n return graph_task->future_result_;\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "First, this function initializes several threads (one per device) calling [` initialize_device_threads_pool()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1046-L1052) where several things happen:\nOne `ReadyQueue` per device is created.\nOne thread per non-cpu device is created.\nA thread local `worker_device` variable is set to track the current device associated with the thread.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`thread_main` function is called, and threads wait for tasks to be put in their queues.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Then it retrieves the queue to place the root node based on the device that holds the tensors present in the `input_buffer` using the `ready_queue` function. Now, the main thread (the one also executing the Python interpreter) has its `worker_device` set to `NO_DEVICE`, and it is in charge of executing functions with all its tensors living in the cpu. If `worker_device` is set to any other value, the graph execution is already started, and `.backward()` was called inside a running `Node`, creating a", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "reentrant backward call. This is explained later. For now,", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "the main thread places the task in the queue and call `thread_main`.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Where the Magic Happens\n\nIt\u2019s been a long way, but finally, we are ready to traverse the graph and execute the nodes. Each of the spawned threads, and the main thread call [`thread_main`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L377-L464).\n\n```c++\nauto Engine::thread_main(const std::shared_ptr& graph_task) -> void {", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "while (graph_task == nullptr || !graph_task->future_result_->completed()) {\n std::shared_ptr local_graph_task;\n {\n NodeTask task = local_ready_queue->pop();\n\n if (task.isShutdownTask_) {\n break;\n }\n\n if (!(local_graph_task = task.base_.lock())) {\n // GraphTask for function is no longer valid, skipping further\n // execution.\n continue;\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (task.fn_ && !local_graph_task->has_error_.load()) {\n at::ThreadLocalStateGuard tls_guard(local_graph_task->thread_locals_);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "try {\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n local_graph_task,\n task.fn_.get(),\n task.inputs_,\n local_graph_task->cpu_ready_queue_);\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Decrement the outstanding tasks.\n --local_graph_task->outstanding_tasks_;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Check if we've completed execution.\n if (local_graph_task->completed()) {\n local_graph_task->mark_as_completed_and_run_post_processing();\n auto base_owner = local_graph_task->owner_;\n if (worker_device != base_owner) {\n std::atomic_thread_fence(std::memory_order_release);\n ready_queue_by_index(local_graph_task->cpu_ready_queue_, base_owner)\n ->push(NodeTask(local_graph_task, nullptr, InputBuffer(0)));\n }\n }\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The code here is simple, given the `local_ready_queue` assigned to each thread in thread-local storage. The threads loop until there are no tasks left to execute in the graph. Note that for device-associated threads, the passed `graph_task` argument is [`nullptr`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L326-L327), and they block in `local_ready_queue->pop()` until a task is pushed in their queue. After some consistency checks (the task", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "type is shutdown, or the graph is still valid). We get to the actual function invocation in `evaluate_function`.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n try {\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n local_graph_task,\n task.fn_.get(),\n task.inputs_,\n local_graph_task->cpu_ready_queue_);\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "After calling `evaluate_function`, we check if the `graph_task` execution is complete by looking the `outstanding_tasks_` number. This number increases when a task is pushed to a queue and is decreased in `local_graph_task->completed()` when a task is executed. When the execution is done, we return the results that are be in the `captured_vars_` in case we called `torch.autograd.grad()` instead of `torch.autograd.backward()` as this function returns tensors instead of storing them in the `.grad` attribute", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "of the inputs. Finally we wake up the main thread if it\u2019s waiting by sending a dummy task.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n // Decrement the outstanding tasks.\n --local_graph_task->outstanding_tasks_;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Check if we've completed execution.\n if (local_graph_task->completed()) {\n local_graph_task->mark_as_completed_and_run_post_processing();\n auto base_owner = local_graph_task->owner_;\n if (worker_device != base_owner) {\n std::atomic_thread_fence(std::memory_order_release);\n ready_queue_by_index(local_graph_task->cpu_ready_queue_, base_owner)\n ->push(NodeTask(local_graph_task, nullptr, InputBuffer(0)));\n }\n }\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Calling the Function and Unlocking New Tasks\n\n[`evaluate_function`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L786-L920) serves three purposes:\n\nRun the function.\nAccumulate its results in the next node `InputBuffers`.\nDecrease the dependencies counter of the next nodes and enqueues the tasks reaching 0 to be executed.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nvoid Engine::evaluate_function(\n std::shared_ptr& graph_task,\n Node* func,\n InputBuffer& inputs,\n const std::shared_ptr& cpu_ready_queue) {\n\n // If exec_info_ is not empty, we have to instrument the execution\n auto& exec_info_ = graph_task->exec_info_;\n if (!exec_info_.empty()) {\n // Checks if the function needs to be executed \n if (!fn_info.needed_) {\n // Skip execution if we don't need to execute the function.\n return;\n }\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "auto outputs = call_function(graph_task, func, inputs);\n\n auto& fn = *func;\n if (!graph_task->keep_graph_) {\n fn.release_variables();\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Initially, we check the `exec_info_` map of the `GraphTask` structure to determine if the current node needs to be executed. Remember that if this map is empty, all the nodes are executed because we are calculating the grads for all the inputs of the forward pass.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "After this check, the function is executed by running [`call_function`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L735-L784). Its implementation is very straightforward and calls the actual derivative function and registered hooks if any.\n\n```c++\n int num_outputs = outputs.size();\n if (num_outputs == 0) {\n // Records leaf stream (if applicable)\n return;\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (AnomalyMode::is_enabled()) {\n // check for nan values in result\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Next, we check the outputs of the function after `call_function` is done. If the number of outputs is 0, there are no following nodes to be executed so we can safely return. This is the case of the `AccumulateGrad` node associated with the leaf nodes.\n\n Also, the check for `NaN` values in the gradients is done here if requested.\n```c++", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "std::lock_guard lock(graph_task->mutex_);\n for (const auto i : c10::irange(num_outputs)) {\n auto& output = outputs[i];\n const auto& next = fn.next_edge(i);\n\n if (!next.is_valid()) continue;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We have now executed a `grad_fn` that has returned one gradient per each of the associated forward pass function inputs. As we saw in the [previous blog post](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/#linking-nodes-together), we have an `Edge` object per each of these input tensors, and the `grad_fn` of the function producing them in the forward pass. Essentially, Output[0] of the node in the backward pass, corresponds to the first argument of the forward pass associated", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "function. Figure 4 shows how the outputs of a backward function are related to the inputs of the forward function. See that the outputs of `grad_fn C` are the gradients of `z` w.r.t. the inputs of `Function C`", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFigure 4: Correspondence between forward and backward functions inputs and outputs\n

\n\nWe now iterate through these edges and check if the associated functions are ready to be executed.\n\n```c++\n // Check if the next function is ready to be computed\n bool is_ready = false;\n auto& dependencies = graph_task->dependencies_;\n auto it = dependencies.find(next.function.get());", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (it == dependencies.end()) {\n auto name = next.function->name();\n throw std::runtime_error(std::string(\"dependency not found for \") + name);\n } else if (--it->second == 0) {\n dependencies.erase(it);\n is_ready = true;\n }\n\n auto& not_ready = graph_task->not_ready_;\n auto not_ready_it = not_ready.find(next.function.get());", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For this, we check the `graph_task->dependencies_` map. We decrement the counter, and if it reaches 0, we mark the function pointed by the edge ready to be executed. Following, we prepare the input buffers of the tasks indicated by the next edges.\n\n```c++\n if (not_ready_it == not_ready.end()) {\n if (!exec_info_.empty()) {\n // Skip functions that aren't supposed to be executed\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// Creates an InputBuffer and moves the output to the corresponding input position\n InputBuffer input_buffer(next.function->num_inputs());\n input_buffer.add(next.input_nr,\n std::move(output),\n opt_parent_stream,\n opt_next_stream);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (is_ready) {\n auto queue = ready_queue(cpu_ready_queue, input_buffer.device());\n queue->push(\n NodeTask(graph_task, next.function, std::move(input_buffer)));\n } else {\n not_ready.emplace(next.function.get(), std::move(input_buffer));\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here, we look for the task in the `graph_task->not_ready_` map. If it is not present, we create a new `InputBuffer` object and set the current output in the `input_nr` position of the buffer associated with the edge. If the task is ready to be executed, we enqueue it in the appropriate device `ready_queue` and complete the execution. However, if the task is not ready and we have seen it before, it is present in the `not_ready_map_`.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n } else {\n // The function already has a buffer\n auto &input_buffer = not_ready_it->second;\n // Accumulates into buffer\n input_buffer.add(next.input_nr,\n std::move(output),\n opt_parent_stream,\n opt_next_stream);\n if (is_ready) {\n auto queue = ready_queue(cpu_ready_queue, input_buffer.device());\n queue->push(NodeTask(graph_task, next.function, std::move(input_buffer)));", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "not_ready.erase(not_ready_it);\n }\n }\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this case, we accumulate the output in the existing `input_buffer` instead of creating a new one. Once all the tasks are processed, the worker thread exits the loop and complete.\nAll this process is summarized in the animation in Figure 5. We see how a thread peeks at the tasks in the ready queue and decrements the next nodes' dependencies, unlocking them for execution.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure 5: Animation of the execution of the computational graph\n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Flow with Reentrant Backward\n\nAs we saw above, the reentrant backward problem is when the currently executed function does a nested call to `backward`. When this happens, the thread running this function goes all the way down to `execute_with_graph_task` as in the non-reentrant case, but here is when things are different.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nc10::intrusive_ptr Engine::execute_with_graph_task(\n const std::shared_ptr& graph_task,\n std::shared_ptr graph_root,\n InputBuffer&& input_buffer) {\n\n initialize_device_threads_pool();\n // Lock mutex for GraphTask.\n std::unique_lock lock(graph_task->mutex_);\n\n auto queue = ready_queue(graph_task->cpu_ready_queue_, input_buffer.device());", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (worker_device == NO_DEVICE) {\n //Regular case\n } else {\n // If worker_device is any devices (i.e. CPU, CUDA): this is a re-entrant\n // backward call from that device.\n graph_task->owner_ = worker_device;\n\n // Now that all the non-thread safe fields of the graph_task have been populated,\n // we can enqueue it.\n queue->push(NodeTask(graph_task, std::move(graph_root), std::move(input_buffer)));", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (current_depth >= max_recursion_depth_) {\n // If reached the max depth, switch to a different thread\n add_thread_pool_task(graph_task);\n } else {\n ++total_depth;\n ++current_depth;\n lock.unlock();\n thread_main(graph_task);\n --current_depth;\n --total_depth;\n }\n }\n return graph_task->future_result_;\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Here, `execute_with_graph_task` detects this as a reentrant call and then looks for the current number of nested calls. If it exceeds the limit, we create a new thread to take care of the execution of this graph, and if not, we execute this reentrant call regularly.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The limit of nested calls was originally set to avoid stack overflow due to reentrant calls creating very large call stacks. However, the number was further reduced when sanitizer tests were added because of the maximum amount of locks a thread can hold at a given moment. This can be seen in [`torch/csrc/autograd/engine.h`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L36-L42).", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When this maximum depth is exceeded, a new thread is created with the [`add_thread_pool_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1239-L1255) function.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nvoid Engine::add_thread_pool_task(const std::weak_ptr& graph_task) {\n std::unique_lock lck(thread_pool_shared_->mutex_);\n // if we have pending graph_task objects to be processed, create a worker.\n bool create_thread = (thread_pool_shared_->num_workers_ <= thread_pool_shared_->graphtasks_queue_.size());\n thread_pool_shared_->graphtasks_queue_.push(graph_task);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "lck.unlock();\n if (create_thread) {\n std::thread t(&Engine::reentrant_thread_init, this);\n t.detach();\n }\n\n thread_pool_shared_->work_.notify_one();\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Before going in-depth, let's look at the `thread_pool_shared_` object in the [`Engine`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L421) which manages all the information related to the threads associated to the reentrant backward calls.\n\n```c++\n struct ThreadPoolShared {\n unsigned int num_workers_;\n std::condition_variable work_;\n std::mutex mutex_;\n std::queue> graphtasks_queue_;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)\n ThreadPoolShared() : num_workers_(0) {}\n };", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`ThreadPoolShared`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L398-L414) is a simple container holding a queue of `GraphTask` objects with synchronization mechanisms and the number of current workers.\n\nNow it is easy to understand how `add_thread_pool_task` creates a thread when there are `graph_task` objects enqueued and insufficient workers to process them.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "`add_thread_pool_task` initializes a thread by executing [`reentrant_thread_init`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L471-L493)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nvoid Engine::reentrant_thread_init() {\n at::init_num_threads();\n auto tp_shared = thread_pool_shared_;\n while(true) {\n std::unique_lock lk(tp_shared->mutex_);\n ++thread_pool_shared_->num_workers_;\n tp_shared->work_.wait(lk, [&tp_shared]{ return !tp_shared->graphtasks_queue_.empty();});\n --thread_pool_shared_->num_workers_;\n auto task = tp_shared->graphtasks_queue_.front();\n tp_shared->graphtasks_queue_.pop();\n lk.unlock();\n std::shared_ptr graph_task;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "if (!(graph_task = task.lock())) {\n continue;\n }\n set_device(graph_task->owner_);\n // set the local_ready_queue to the ready queue on the graph_task->owner_ device\n local_ready_queue = ready_queue_by_index(graph_task->cpu_ready_queue_, graph_task->owner_);\n total_depth = graph_task->reentrant_depth_;\n thread_main(graph_task);\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The code is straightforward. The newly created thread waits on the `thread_pool_shared->graphtasks_queue_` for reentrant backward graphs to be available and executes them. Notice that this thread uses the task-ready queue associated with the device of the thread that started this call by accessing the `graph_task->owner_` field set in the [`execute_with_graph_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1092) function.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Error Handling\n\nWhenever an error happens in one of the worker threads. It will be propagated to the `backward` calling thread.\n\nTo achieve this, there is a try/catch block in the [`thread_main`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L415-L438) that catches any exception in the `Node` function call and sets it to the associated `GraphTask` object.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n try {\n \u2026\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n \u2026\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[`thread_on_exception`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L495-L500) and the [functions it calls](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L605-L621) end up setting the exception in the `local_graph_task` object.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\nvoid Engine::thread_on_exception(\n std::shared_ptr graph_task,\n const std::shared_ptr& fn,\n std::exception& e) {\n graph_task->set_exception(std::current_exception(), fn);\n}\n\nvoid GraphTask::set_exception_without_signal(const std::shared_ptr& fn) {\n if (!has_error_.exchange(true)) {\n if (AnomalyMode::is_enabled() && fn) {\n fn->metadata()->print_stack(fn->name());\n }\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "void GraphTask::set_exception(\n std::exception_ptr eptr,\n const std::shared_ptr& fn) {\n set_exception_without_signal(fn);\n if (!future_completed_.exchange(true)) {\n // NOLINTNEXTLINE(performance-move-const-arg)\n future_result_->setError(std::move(eptr));\n }\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In `set_exception` it sets the `has_error_` flag to `true` and it calls the [`setError`]()\nfunction of the [`future_result_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/aten/src/ATen/core/ivalue_inl.h#L770-L1322) object. This will make the error to be re-thrown at the caller thread when `future_result_->value()` is accessed.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```c++\n IValue value() {\n std::unique_lock lock(mutex_);\n AT_ASSERT(completed());\n if (eptr_) {\n std::rethrow_exception(eptr_);\n }\n return value_;\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Closing Remarks\n\nThis has been the last post of this series covering how PyTorch does the auto differentiation. We hope you enjoyed reading it and that now you are familiar enough with PyTorch internals to start contributing in PyTorch development!", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.6 released w/ Native AMP Support, Microsoft joins as maintainers for Windows'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Today, we\u2019re announcing the availability of PyTorch 1.6, along with updated domain libraries. We are also excited to announce the team at [Microsoft is now maintaining Windows builds and binaries](https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch) and will also be supporting the community on GitHub as well as the PyTorch Windows discussion forums.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch 1.6 release includes a number of new APIs, tools for performance improvement and profiling, as well as major updates to both distributed data parallel (DDP) and remote procedure call (RPC) based distributed training. \nA few of the highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "1. Automatic mixed precision (AMP) training is now natively supported and a stable feature (See [here](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/) for more details) - thanks for NVIDIA\u2019s contributions; \n2. Native TensorPipe support now added for tensor-aware, point-to-point communication primitives built specifically for machine learning; \n3. Added support for complex tensors to the frontend API surface;", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "4. New profiling tools providing tensor-level memory consumption information;\n5. Numerous improvements and new features for both distributed data parallel (DDP) training and the remote procedural call (RPC) packages.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Additionally, from this release onward, features will be classified as Stable, Beta and Prototype. Prototype features are not included as part of the binary distribution and are instead available through either building from source, using nightlies or via compiler flag. You can learn more about what this change means in the post [here](https://pytorch.org/blog/pytorch-feature-classification-changes/). You can also find the full release notes [here](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "# Performance & Profiling", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Automatic Mixed Precision (AMP) Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "AMP allows users to easily enable automatic mixed precision training enabling higher performance and memory savings of up to 50% on Tensor Core GPUs. Using the natively supported `torch.cuda.amp` API, AMP provides convenience methods for mixed precision, where some operations use the `torch.float32 (float)` datatype and other operations use `torch.float16 (half)`. Some ops, like linear layers and convolutions, are much faster in `float16`. Other ops, like reductions, often require the dynamic range of", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "`float32`. Mixed precision tries to match each op to its appropriate datatype.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* Design doc ([Link](https://github.com/pytorch/pytorch/issues/25081))\n* Documentation ([Link](https://pytorch.org/docs/stable/amp.html))\n* Usage examples ([Link](https://pytorch.org/docs/stable/notes/amp_examples.html))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Fork/Join Parallelism \n\nThis release adds support for a language-level construct as well as runtime support for coarse-grained parallelism in TorchScript code. This support is useful for situations such as running models in an ensemble in parallel, or running bidirectional components of recurrent nets in parallel, and allows the ability to unlock the computational power of parallel architectures (e.g. many-core CPUs) for task level parallelism.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Parallel execution of TorchScript programs is enabled through two primitives: `torch.jit.fork` and `torch.jit.wait`. In the below example, we parallelize execution of `foo`:\n\n```python\nimport torch\nfrom typing import List\n\ndef foo(x):\n return torch.neg(x)\n\n@torch.jit.script\ndef example(x):\n futures = [torch.jit.fork(foo, x) for _ in range(100)]\n results = [torch.jit.wait(future) for future in futures]\n return torch.sum(torch.stack(results))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "print(example(torch.ones([])))\n ```\n \n* Documentation ([Link](https://pytorch.org/docs/stable/jit.html))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Memory Profiler \n\nThe `torch.autograd.profiler` API now includes a memory profiler that lets you inspect the tensor memory cost of different operators inside your CPU and GPU models.\n\nHere is an example usage of the API:\n\n```python\nimport torch\nimport torchvision.models as models\nimport torch.autograd.profiler as profiler\n\nmodel = models.resnet18()\ninputs = torch.randn(5, 3, 224, 224)\nwith profiler.profile(profile_memory=True, record_shapes=True) as prof:\n model(inputs)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "# NOTE: some columns were removed for brevity\nprint(prof.key_averages().table(sort_by=\"self_cpu_memory_usage\", row_limit=10))\n# --------------------------- --------------- --------------- ---------------\n# Name CPU Mem Self CPU Mem Number of Calls\n# --------------------------- --------------- --------------- ---------------\n# empty 94.79 Mb 94.79 Mb 123\n# resize_ 11.48 Mb 11.48 Mb 2", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "# addmm 19.53 Kb 19.53 Kb 1\n# empty_strided 4 b 4 b 1\n# conv2d 47.37 Mb 0 b 20\n# --------------------------- --------------- --------------- ---------------", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* PR ([Link](https://github.com/pytorch/pytorch/pull/37775))\n* Documentation ([Link](https://pytorch.org/docs/stable/autograd.html#profiler))\n\n# Distributed Training & RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] TensorPipe backend for RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.6 introduces a new backend for the RPC module which leverages the TensorPipe library, a tensor-aware point-to-point communication primitive targeted at machine learning, intended to complement the current primitives for distributed training in PyTorch (Gloo, MPI, ...) which are collective and blocking. The pairwise and asynchronous nature of TensorPipe lends itself to new networking paradigms that go beyond data parallel: client-server approaches (e.g., parameter server for embeddings,", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "actor-learner separation in Impala-style RL, ...) and model and pipeline parallel training (think GPipe), gossip SGD, etc.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "```python\n# One-line change needed to opt in\ntorch.distributed.rpc.init_rpc(\n ...\n backend=torch.distributed.rpc.BackendType.TENSORPIPE,\n)\n\n# No changes to the rest of the RPC API\ntorch.distributed.rpc.rpc_sync(...)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* Design doc ([Link](https://github.com/pytorch/pytorch/issues/35251))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc/index.html))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] DDP+RPC \n\nPyTorch Distributed supports two powerful paradigms: DDP for full sync data parallel training of models and the RPC framework which allows for distributed model parallelism. Previously, these two features worked independently and users couldn\u2019t mix and match these to try out hybrid parallelism paradigms.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Starting in PyTorch 1.6, we\u2019ve enabled DDP and RPC to work together seamlessly so that users can combine these two techniques to achieve both data parallelism and model parallelism. An example is where users would like to place large embedding tables on parameter servers and use the RPC framework for embedding lookups, but store smaller dense parameters on trainers and use DDP to synchronize the dense parameters. Below is a simple code snippet. \n\n```python\n// On each trainer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "remote_emb = create_emb(on=\"ps\", ...)\nddp_model = DDP(dense_model)\n\nfor data in batch:\n with torch.distributed.autograd.context():\n res = remote_emb(data)\n loss = ddp_model(res)\n torch.distributed.autograd.backward([loss])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* DDP+RPC Tutorial ([Link](https://pytorch.org/tutorials/advanced/rpc_ddp_tutorial.html))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc/index.html))\n* Usage Examples ([Link](https://github.com/pytorch/examples/pull/800))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] RPC - Asynchronous User Functions", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "RPC Asynchronous User Functions supports the ability to yield and resume on the server side when executing a user-defined function. Prior to this feature, when a callee processes a request, one RPC thread waits until the user function returns. If the user function contains IO (e.g., nested RPC) or signaling (e.g., waiting for another request to unblock), the corresponding RPC thread would sit idle waiting for these events. As a result, some applications have to use a very large number of threads and send", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "additional RPC requests, which can potentially lead to performance degradation. To make a user function yield on such events, applications need to: 1) Decorate the function with the `@rpc.functions.async_execution` decorator; and 2) Let the function return a `torch.futures.Future` and install the resume logic as callbacks on the `Future` object. See below for an example:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "```python\n@rpc.functions.async_execution\ndef async_add_chained(to, x, y, z):\n return rpc.rpc_async(to, torch.add, args=(x, y)).then(\n lambda fut: fut.wait() + z\n )\n\nret = rpc.rpc_sync(\n \"worker1\", \n async_add_chained, \n args=(\"worker2\", torch.ones(2), 1, 1)\n)\n \nprint(ret) # prints tensor([3., 3.])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* Tutorial for performant batch RPC using Asynchronous User Functions ([Link](https://github.com/pytorch/tutorials/blob/release/1.6/intermediate_source/rpc_async_execution.rst))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc.html#torch.distributed.rpc.functions.async_execution))\n* Usage examples ([Link](https://github.com/pytorch/examples/tree/master/distributed/rpc/batch))\n\n# Frontend API Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Complex Numbers", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch 1.6 release brings beta level support for complex tensors including torch.complex64 and torch.complex128 dtypes. A complex number is a number that can be expressed in the form a + bj, where a and b are real numbers, and j is a solution of the equation x^2 = \u22121. Complex numbers frequently occur in mathematics and engineering, especially in signal processing and the area of complex neural networks is an active area of research. The beta release of complex tensors will support common PyTorch and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "complex tensor functionality, plus functions needed by Torchaudio, ESPnet and others. While this is an early version of this feature, and we expect it to improve over time, the overall goal is provide a NumPy compatible user experience that leverages PyTorch\u2019s ability to run on accelerators and work with autograd to better support the scientific community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "# Mobile Updates\n\nPyTorch 1.6 brings increased performance and general stability for mobile on-device inference. We squashed a few bugs, continued maintenance and added few new features while improving fp32 and int8 performance on a large variety of ML model inference on CPU backend.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Mobile Features and Performance \n\n* Stateless and stateful XNNPACK Conv and Linear operators\n* Stateless MaxPool2d + JIT optimization passes\n* JIT pass optimizations: Conv + BatchNorm fusion, graph rewrite to replace conv2d/linear with xnnpack ops, relu/hardtanh fusion, dropout removal\n* QNNPACK integration removes requantization scale constraint\n* Per-channel quantization for conv, linear and dynamic linear\n* Disable tracing for mobile client to save ~600 KB on full-jit builds", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "# Updated Domain Libraries", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "torchvision 0.7", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "torchvision 0.7 introduces two new pretrained semantic segmentation models, [FCN ResNet50](https://arxiv.org/abs/1411.4038) and [DeepLabV3 ResNet50](https://arxiv.org/abs/1706.05587), both trained on COCO and using smaller memory footprints than the ResNet101 backbone. We also introduced support for AMP (Automatic Mixed Precision) autocasting for torchvision models and operators, which automatically selects the floating point precision for different GPU operations to improve performance while maintaining", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "accuracy.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* Release notes ([Link](https://github.com/pytorch/vision/releases))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "torchaudio 0.6\n\ntorchaudio now officially supports Windows. This release also introduces a new model module (with wav2letter included), new functionals (contrast, cvm, dcshift, overdrive, vad, phaser, flanger, biquad), datasets (GTZAN, CMU), and a new optional sox backend with support for TorchScript.\n\n* Release notes ([Link](https://github.com/pytorch/audio/releases))\n\n# Additional updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "HACKATHON\n\nThe Global PyTorch Summer Hackathon is back! This year, teams can compete in three categories virtually:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "1. **PyTorch Developer Tools:** Tools or libraries designed to improve productivity and efficiency of PyTorch for researchers and developers\n 2. **Web/Mobile Applications powered by PyTorch:** Applications with web/mobile interfaces and/or embedded devices powered by PyTorch \n 3. **PyTorch Responsible AI Development Tools:** Tools, libraries, or web/mobile apps for responsible AI development\n\nThis is a great opportunity to connect with the community and practice your machine learning skills.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "* [Join the hackathon](http://pytorch2020.devpost.com/)\n* [Watch educational videos](https://www.youtube.com/pytorch)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "LPCV Challenge\n\nThe [2020 CVPR Low-Power Vision Challenge (LPCV) - Online Track for UAV video](https://lpcv.ai/2020CVPR/video-track) submission deadline is coming up shortly. You have until July 31, 2020 to build a system that can discover and recognize characters in video captured by an unmanned aerial vehicle (UAV) accurately using PyTorch and Raspberry Pi 3B+.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Prototype Features\n\nTo reiterate, Prototype features in PyTorch are early features that we are looking to gather feedback on, gauge the usefulness of and improve ahead of graduating them to Beta or Stable. The following features are not part of the PyTorch 1.6 release and instead are available in nightlies with separate docs/tutorials to help facilitate early usage and feedback.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Distributed RPC/Profiler\nAllow users to profile training jobs that use `torch.distributed.rpc` using the autograd profiler, and remotely invoke the profiler in order to collect profiling information across different nodes. The RFC can be found [here](https://github.com/pytorch/pytorch/issues/39675) and a short recipe on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "TorchScript Module Freezing\nModule Freezing is the process of inlining module parameters and attributes values into the TorchScript internal representation. Parameter and attribute values are treated as final value and they cannot be modified in the frozen module. The PR for this feature can be found [here](https://github.com/pytorch/pytorch/pull/32178) and a short tutorial on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Graph Mode Quantization", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Eager mode quantization requires users to make changes to their model, including explicitly quantizing activations, module fusion, rewriting use of torch ops with Functional Modules and quantization of functionals are not supported. If we can trace or script the model, then the quantization can be done automatically with graph mode quantization without any of the complexities in eager mode, and it is configurable through a `qconfig_dict`. A tutorial on how to use this feature can be found", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "[here](https://github.com/pytorch/tutorials/tree/master/prototype_source).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Quantization Numerical Suite\nQuantization is good when it works, but it\u2019s difficult to know what's wrong when it doesn't satisfy the expected accuracy. A prototype is now available for a Numerical Suite that measures comparison statistics between quantized modules and float modules. This is available to test using eager mode and on CPU only with more support coming. A tutorial on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).\n\n\nCheers!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} -{"page_content": "Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"How Computational Graphs are Executed in PyTorch\"\nauthor: Preferred Networks\nfeatured-img: \"\"\n---\n\nWelcome to the last entry into understanding the autograd engine of PyTorch series!\nIf you haven\u2019t read parts [1](https://pytorch.org/blog/overview-of-pytorch-autograd-engine/) & [2](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/) check them now to understand how PyTorch creates the computational graph for the backward pass!\n\nThis post is based on PyTorch v1.11, so some highlighted parts may differ across versions.\n\n# PyTorch autograd graph execution\n\nThe last post showed how PyTorch constructs the graph to calculate the outputs' derivatives w.r.t. the inputs when executing the forward pass. Now we will see how the execution of the backward pass is coordinated and done by looking at the whole process, starting from Python down to the lower C++ level internals.\n\n# What Happens when Calling `backward()`/`grad()` from Python\n## Using `variable.backward()`", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "After doing all our calculations with an input set to require the gradient, we call `.backward()` on the result to initiate the backward pass execution.\n\n```python\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> y = torch.exp(x).sum()\n>>> y.backward()\n```\n\nCalling [`.backward()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/_tensor.py#L307-L363) on a tensor results in a call to [`torch.autograd.backward()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L85-L175).\n```python\n# torch/_tensor.py\n\ndef backward(self, gradient=None, retain_graph=None, create_graph=False, inputs=None):\n \u2026\n torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)\n\n```\n`torch.autograd.backward()` checks the arguments and calls the autograd engine in the C++ layer.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "``` python\ndef backward(\n tensors: _TensorOrTensors,\n grad_tensors: Optional[_TensorOrTensors] = None,\n retain_graph: Optional[bool] = None,\n create_graph: bool = False,\n grad_variables: Optional[_TensorOrTensors] = None,\n inputs: Optional[_TensorOrTensors] = None,\n) -> None:\n \u2026\n\n if inputs is not None and len(inputs) == 0:\n raise RuntimeError(\"'inputs' argument to backward() cannot be empty.\")\n\n tensors = (tensors,) if isinstance(tensors, torch.Tensor) else tuple(tensors)\n inputs = (inputs,) if isinstance(inputs, torch.Tensor) else \\\n tuple(inputs) if inputs is not None else tuple()\n\n grad_tensors_ = _tensor_or_tensors_to_tuple(grad_tensors, len(tensors))\n grad_tensors_ = _make_grads(tensors, grad_tensors_)\n if retain_graph is None:\n retain_graph = create_graph", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Variable._execution_engine.run_backward(\n tensors, grad_tensors_, retain_graph, create_graph, inputs,\n allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag\n\n```\nFirst, whether the `grad_tensors` argument was specified or not, there is a call to the [`_make_grads`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L30-L74) function. This is used to check the provided `grad_tensors` or to specify the default value for them by looking at the `tensors` argument values\u2019 shapes. Check the first blog post for details on the default value for the `grad_tensors` of the backward pass. This function just provides the vector of the vector jacobian product if it was not initially specified.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In the above code, `Variable` has an `_execution_engine` attribute that is defined in [`torch.autograd.variable`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/variable.py#L14) to be of type `ImperativeEngine`; the C++ engine exported to python and declared in [`torch/csrc/autograd/python_engine.cpp`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/python_engine.cpp#L384). In the following sections, we explain in detail how this object executes the backward pass.\n\nNote that the `torch.autograd.backward` function has an `inputs` optional argument. This argument is used when we want to calculate the `.grad` field of only a subset of input tensors in the forward pass.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> x = torch.tensor([0.5, 0.75], requires_grad=True)\n>>> y = torch.tensor([0.1, 0.90], requires_grad=True)\n>>> z = torch.exp(x * y).sum()\n>>> torch.autograd.backward([z], inputs=[x])\n>>> x.grad\ntensor([0.1051, 1.7676])\n>>> y.grad # None\n>>>\n\n```\n## Using `torch.autograd.grad`\n\nAn alternative to `backward()` is to use [`torch.autograd.grad()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/autograd/__init__.py#L177-L277). The main difference to `backward()` is that `grad()` returns a tuple of tensors with the gradients of the `outputs` w.r.t. the `inputs` kwargs instead of storing them in the `.grad` field of the tensors. As you can see, the `grad()` code shown below is very similar to backward.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\ndef grad(\n outputs: _TensorOrTensors,\n inputs: _TensorOrTensors,\n grad_outputs: Optional[_TensorOrTensors] = None,\n retain_graph: Optional[bool] = None,\n create_graph: bool = False,\n only_inputs: bool = True,\n allow_unused: bool = False,\n is_grads_batched: bool = False\n) -> Tuple[torch.Tensor, ...]:\n \n outputs = (outputs,) if isinstance(outputs, torch.Tensor) else tuple(outputs)\n inputs = (inputs,) if isinstance(inputs, torch.Tensor) else tuple(inputs)\n overridable_args = outputs + inputs\n if has_torch_function(overridable_args):\n return handle_torch_function(\n grad,\n overridable_args,\n outputs,\n inputs,\n grad_outputs=grad_outputs,\n retain_graph=retain_graph,\n create_graph=create_graph,\n only_inputs=only_inputs,\n allow_unused=allow_unused,\n )", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "grad_outputs_ = _tensor_or_tensors_to_tuple(grad_outputs, len(outputs))\n grad_outputs_ = _make_grads(outputs, grad_outputs_)\n\n if retain_graph is None:\n retain_graph = create_graph\n\n if is_grads_batched:\n # \u2026. It will not be covered here\n else:\n return Variable._execution_engine.run_backward(\n outputs, grad_outputs_, retain_graph, create_graph, inputs,\n allow_unused, accumulate_grad=False) # Calls into the C++ engine to run the backward pass\n\n```\n\nFigure 1 shows the computational graph with the `backward()` and `grad()` arguments highlighted in red and blue, respectively:\n\n

\n \n

\n\n

\nFgiure 1: Correspondence of `backward`/`grad` arguments in the graphs.\n

\n\n# Going Inside the Autograd Engine\n\n## Refreshing Concepts: Nodes and Edges", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "As we saw in [2](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/)\nThe computational graph comprises `Node` and `Edge` objects. Please read that post if you haven\u2019t done it yet.\n\n### Nodes\n\n`Node` objects are defined in [`torch/csrc/autograd/function.h`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L105-L176), and they provide an overload of `operator()` for the associated function and a list of edges to do the graph traversal. Note that `Node` is a base class that autograd functions inherit from and override the `apply` method to execute the backward function.\n```c++\nstruct TORCH_API Node : std::enable_shared_from_this {\n ...\n /// Evaluates the function on the given inputs and returns the result of the\n /// function call.\n variable_list operator()(variable_list&& inputs) {\n ...\n }", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "protected:\n /// Performs the `Node`'s actual operation.\n virtual variable_list apply(variable_list&& inputs) = 0;\n \u2026\n edge_list next_edges_;\n uint64_t topological_nr_ = 0;\n \u2026\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nThere is an attribute called [`topological_nr_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L481) in every node object. This number is used to optimize the graph execution as it allows to discard of graph branches under certain conditions. The topological number is the longest distance between this node and any leaf node and it is shown in Figure 2. Its main property is that for any pair of nodes `x`, `y` in a directed graph `topo_nr(x) < topo_nr(y)` means that there is no path from `x` to `y`. So this allows for reducing the number of paths in the graph in need of traversal. Check the [topological_nr](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/function.h#L314-L343)\n) method comment for further details.\n\n

\n \n

\n\n

\nFigure 2: Example of the Topological Number calculation\n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### Edges\n\nThe [`Edge`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/edge.h#L14-L39) object links `Node`s together, and its implementation is straightforward.\n\n```c++\nstruct Edge {\n ...\n /// The function this `Edge` points to.\n std::shared_ptr function;\n /// The identifier of a particular input to the function.\n uint32_t input_nr;\n};\n\n```\n\nIt only requires a function pointer to the `Node` and an input number that is the index of the output from the forward function this edge points to. When preparing the set of gradients before calling \"function\", we know that what is flowing from this edge should be accumulated in the \"input_nr\"th argument. Note that the input/output name is flipped here and this is the input to the backward function.\n `Edge` objects are constructed using the [`gradient_edge`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/variable.cpp#L221-L233) function method.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n Edge gradient_edge(const Variable& self) {\n if (const auto& gradient = self.grad_fn()) {\n return Edge(gradient, self.output_nr());\n } else {\n return Edge(grad_accumulator(self), 0);\n }\n }\n\n```\n## Entering the C++ Realm", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Once that `torch.autograd.backward()` has been invoked, the\n[`THPEngine_run_backward`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/python_engine.cpp#L152-L286) routine starts the graph traversal. Following is a schema of the function body:\n```c++\nPyObject *THPEngine_run_backward(PyObject *self, PyObject *args, PyObject *kwargs)\n{\n HANDLE_TH_ERRORS\n PyObject *tensors = nullptr;\n PyObject *grad_tensors = nullptr;\n unsigned char keep_graph = 0;\n unsigned char create_graph = 0;\n PyObject *inputs = nullptr;\n \n // Convert the python arguments to C++ objects\n const char *accepted_kwargs[] = { // NOLINT\n \"tensors\", \"grad_tensors\", \"keep_graph\", \"create_graph\", \"inputs\",\n \"allow_unreachable\", \"accumulate_grad\", nullptr\n };\n if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OObb|Obb\", (char**)accepted_kwargs,\n &tensors, &grad_tensors, &keep_graph, &create_graph, &inputs, &allow_unreachable, &accumulate_grad))", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "// Prepare arguments\n for(const auto i : c10::irange(num_tensors)) {\n // Check that the tensors require gradients\n }\n\n std::vector output_edges;\n if (inputs != nullptr) {\n // Prepare outputs\n }\n\n {\n // Calls the actual autograd engine\n pybind11::gil_scoped_release no_gil;\n outputs = engine.execute(roots, grads, keep_graph, create_graph, accumulate_grad, output_edges);\n }\n // Clean up and finish\n}\n\n```\n\nFirst, we prepare the input arguments after converting the `PyObject` arguments to actual C++ objects. The `tensors` list contains the tensors from which we start the backward pass. These tensors are converted to edges using `torch::autograd::impl::gradient_edge` and added to a list called `roots` where the graph traversal starts.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n edge_list roots;\n roots.reserve(num_tensors);\n variable_list grads;\n grads.reserve(num_tensors);\n for(const auto i : c10::irange(num_tensors)) {\n PyObject *_tensor = PyTuple_GET_ITEM(tensors, i);\n const auto& variable = THPVariable_Unpack(_tensor);\n auto gradient_edge = torch::autograd::impl::gradient_edge(variable);\n roots.push_back(std::move(gradient_edge));\n\n PyObject *grad = PyTuple_GET_ITEM(grad_tensors, i);\n if (THPVariable_Check(grad)) {\n const Variable& grad_var = THPVariable_Unpack(grad);\n grads.push_back(grad_var);\n } \n }\n\n```\n\nNow, if the `inputs` argument was specified in `backward` or we used the `torch.autograd.grad` api, the following code creates a list of edges to accumulate the gradients in the specified tensors at the end of the computation. The engine uses this later to optimize the execution as it doesn\u2019t add the gradients in all the leaf nodes, just the specified ones.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n std::vector output_edges;\n if (inputs != nullptr) {\n int num_inputs = PyTuple_GET_SIZE(inputs);\n output_edges.reserve(num_inputs);\n for (const auto i : c10::irange(num_inputs)) {\n PyObject *input = PyTuple_GET_ITEM(inputs, i);\n const auto& tensor = THPVariable_Unpack(input);\n const auto output_nr = tensor.output_nr();\n auto grad_fn = tensor.grad_fn();\n if (!grad_fn) {\n grad_fn = torch::autograd::impl::try_get_grad_accumulator(tensor);\n }\n if (accumulate_grad) {\n tensor.retain_grad();\n }\n if (!grad_fn) {\n output_edges.emplace_back(std::make_shared(), 0);\n } else {\n output_edges.emplace_back(grad_fn, output_nr);\n }\n }\n }\n\n```\n\nThe next step is the actual graph traversal and node function execution, and finally, the cleanup and return.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n {\n // Calls the actual autograd engine\n pybind11::gil_scoped_release no_gil;\n auto& engine = python::PythonEngine::get_python_engine();\n outputs = engine.execute(roots, grads, keep_graph, create_graph, accumulate_grad, output_edges);\n }\n // Clean up and finish\n}\n\n```\n\n# Starting the Real Execution\n\n`engine.execute`is present in [torch/csrc/autograd/engine.cpp](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L969-L1044) \n\nThere are two differentiated steps here:\n\nAnalyze the graph to find dependencies between functions\nCreate worker threads that traverse the graph\n\n## Data Structures Used for the Execution\n\n### GraphTask\n\nAll the execution metadata is managed by the [`GraphTask`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L51-L196) class in [torch/csrc/autograd/engine.h](https://github.com/pytorch/pytorch/blob/release/1.11/torch/csrc/autograd/engine.h)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nstruct GraphTask: std::enable_shared_from_this {\n std::atomic outstanding_tasks_{0};\n // \u2026 \n std::unordered_map not_ready_;\n std::unordered_map dependencies_;\n\n struct ExecInfo {\n // \u2026\n };\n std::unordered_map exec_info_;\n std::vector captured_vars_;\n // \u2026\n std::shared_ptr cpu_ready_queue_;\n};\n\n```\n\nHere we see a series of variables dedicated to maintaining the execution state.\n`outstanding_tasks_` tracks the number of tasks left to be executed for the backward pass to complete. `not_ready_` holds the input arguments for the `Node`s that are not ready to be executed. `dependencies_` track the number of predecessors that a `Node` has. As the count reaches `0`, the `Node` is ready for execution; it is placed in a ready queue to be retrieved and executed later.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "`exec_info_` and the associated `ExecInfo` struct are used only when the `inputs` argument is specified or it is a call to `autograd.grad()`. They allow filter paths on the graph that are not needeed since only the gradients are calculated only for the variables in the `inputs` list.\n\n `captured_vars_` is where the results of the graph execution are temporarily stored if we used the `torch.autograd.grad()` api instead of `torch.autograd.backward()` since `grad()` returns the gradients as tensors instead of just filling the `.grad` field of the inputs.\n\n\n### NodeTask", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### NodeTask\n\nThe [`NodeTask`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L210-L242) struct is a basic class that holds an `fn_` pointer to the node to execute, and an `inputs_` buffer to store the input arguments to this function. Note that the functions executed by the backward pass are the derivatives specified in the `derivatives.yaml` file. or the user provided backward function when using custom functions as described in the second blog post.\n\nThe `inputs_` buffer is also where the output gradients of the previously executed functions are aggregated, and it is defined as a [`std::vector` container](https://github.com/pytorch/pytorch/blob/release/1.10/torch/csrc/autograd/input_buffer.h) with facilities to accumulate values at a given position.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nstruct NodeTask {\n std::weak_ptr base_;\n std::shared_ptr fn_;\n // This buffer serves as an implicit \"addition\" node for all of the\n // gradients flowing here. Once all the dependencies are finished, we\n // use the contents of this buffer to run the function.\n InputBuffer inputs_;\n};\n\n```\n### GraphRoot\n\nThe [`GraphRoot`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/basic_ops.h#L72-L89) is a special function used to hold multiple input variables in a single place. The code is pretty simple as it only acts as a container of variables.\n\n```c++\nstruct TORCH_API GraphRoot : public Node {\n GraphRoot(edge_list functions, variable_list inputs)\n : Node(std::move(functions)),\n outputs(std::move(inputs)) {\n for (const auto& t : outputs) {\n add_input_metadata(t);\n }\n }\n\n variable_list apply(variable_list&& inputs) override {\n return outputs;\n }\n\n```\n\n### AccumulateGrad", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "### AccumulateGrad\n\nThis function is set during the graph creation in `gradient_edge` when the `Variable` object doesn\u2019t have a `grad_fn`. This is, it is a leaf node.\n\n```c++\n if (const auto& gradient = self.grad_fn()) {\n // \u2026\n } else {\n return Edge(grad_accumulator(self), 0);\n }\n\n```\n\nThe function body is defined in [`torch/csrc/autograd/functions/accumulate_grad.cpp`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/accumulate_grad.cpp#L25-L63) and it essentially accumulates the input grads in the object\u2019s `.grad` attribute.\n\n```c++\nauto AccumulateGrad::apply(variable_list&& grads) -> variable_list {\n check_input_variables(\"AccumulateGrad\", grads, 1, 0);\n \u2026\n\n at::Tensor new_grad = callHooks(variable, std::move(grads[0]));\n std::lock_guard lock(mutex_);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "at::Tensor& grad = variable.mutable_grad();\n accumulateGrad(\n variable,\n grad,\n new_grad,\n 1 + !post_hooks().empty() /* num_expected_refs */,\n [&grad](at::Tensor&& grad_update) { grad = std::move(grad_update); });\n return variable_list();\n}\n}} // namespace torch::autograd\n\n\n\n```\n\n[`accumulateGrad`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/functions/accumulate_grad.h#L100)\ndoes several checks on the tensors format and eventually performs the `variable_grad += new_grad;` accumulation.\n\n## Preparing the graph for execution\n\nNow, let\u2019s walk through [`Engine::execute`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L969-L1126). The first thing to do besides arguments consistency checks is to create the actual `GraphTask` object we described above. This object keeps all the metadata of the graph execution.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nauto Engine::execute(const edge_list& roots,\n const variable_list& inputs,\n bool keep_graph,\n bool create_graph,\n bool accumulate_grad,\n const edge_list& outputs) -> variable_list {\n\n validate_outputs(roots, const_cast(inputs), [](const std::string& msg) {\n return msg;\n });\n\n // Checks\n\n auto graph_task = std::make_shared(\n /* keep_graph */ keep_graph,\n /* create_graph */ create_graph,\n /* depth */ not_reentrant_backward_call ? 0 : total_depth + 1,\n /* cpu_ready_queue */ local_ready_queue);\n\n // If we receive a single root, skip creating extra root node\n // \u2026\n // Prepare graph by computing dependencies\n // \u2026\n // Queue the root \n // \u2026\n // launch execution\n // \u2026\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nAfter creating the `GraphTask`, we use its associated function if we only have one root node. If we have multiple root nodes, we create a special `GraphRoot` object as described before.\n\n```c++\n bool skip_dummy_node = roots.size() == 1;\n auto graph_root = skip_dummy_node ?\n roots.at(0).function :\n std::make_shared(roots, inputs);\n\n```\n\nThe next step is to fill the `dependencies_` map in the `GraphTask` object since the engine must know when it can execute a task. The `outputs` here is the `inputs` argument passed to the `torch.autograd.backward()` call in Python. But here, we have reversed the names since the gradients w.r.t. the inputs of the forward pass are now the outputs of the backward pass. And from now on, there is no concept of forward/backward, but only graph traversal and execution.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n auto min_topo_nr = compute_min_topological_nr(outputs);\n // Now compute the dependencies for all executable functions\n compute_dependencies(graph_root.get(), *graph_task, min_topo_nr);\n\n if (!outputs.empty()) {\n graph_task->init_to_execute(*graph_root, outputs, accumulate_grad, min_topo_nr);\n }\n\n```\n\nHere we preprocess the graph for the execution of the nodes. First, [`compute_min_topological_nr`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L922-L933) is called to to obtain the minimum topological number of the tensors specified in `outputs` (0 if no `inputs` kwarg was supplied to `.backward` or `input` for `.grad`). This computation prunes paths in the graph that lead to input variables of which we don\u2019t want/need to calculate the grads.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Second, is the [`compute_dependencies`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L935-L967) call. This function is a very simple graph traversal that starts with the root `Node`, and for each of the edges in `node.next_edges()` it increments the counter in `dependencies_`. Figure 3 shows the result of the dependencies calculation for the example graph. Note that the number of dependencies of any node is just the number of edges arriving at it.\n\n

\n \n

\n\n

\nFigure 3: Number of dependencies for each node\n

", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Finally, the [`init_to_execute`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1281-L1383) call, this is the one that populates the `GraphTask::exec_info_` map in case that `inputs` were specified in the python `backward` call. It iterates the graph again, starting from the root, and records in the `exec_info_` map the intermediate nodes needed to calculate only the given `inputs` gradients.\n\n```c++\n // Queue the root\n if (skip_dummy_node) {\n InputBuffer input_buffer(roots.at(0).function->num_inputs());\n auto input = inputs.at(0);\n\n\n input_buffer.add(roots.at(0).input_nr,\n std::move(input),\n input_stream,\n opt_next_stream);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "execute_with_graph_task(graph_task, graph_root, std::move(input_buffer));\n } else {\n execute_with_graph_task(graph_task, graph_root, InputBuffer(variable_list()));\n }\n // Avoid a refcount bump for the Future, since we check for refcount in\n // DistEngine (see TORCH_INTERNAL_ASSERT(futureGrads.use_count() == 1)\n // in dist_engine.cpp).\n auto& fut = graph_task->future_result_;\n fut->wait();\n return fut->value().toTensorVector();\n}\n\n```\n\nAnd now, we are ready to start the actual execution by creating the `InputBuffer`. In case we only have one root variable, we begin by copying the value of the `inputs` tensor (this is the `gradients` passed to python `backward`) in position 0 of the input_buffer. This is a small optimization that avoids running the `RootNode` for no reason. Also, if the rest of the graph is not on the cpu, we directly start on that worker while the `RootNode` is always placed on the cpu ready queue. Details of the workers and ready queues are explained in the section below.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "On the other hand, if we have multiple roots, the `GraphRoot` object also holds the inputs, so it is enough to pass it an empty `InputBuffer`.\n\n## Graph Traversal and Node Execution\n### Devices, Threads and Queues\n\nBefore diving into the actual execution, we need to see how the engine is structured.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "First of all, the engine is multithreaded with one thread per device. For example, the caller thread is associated with the CPU while additional threads are created and associated with each GPU or other devices available in the system. Each thread tracks its device using thread-local storage in the [`worker_device`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L69) variable. In addition, the threads have a queue of tasks to be executed also located in thread-local storage, the [`local_ready_queue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L103-L104). This is where work is queued for this thread to execute in the `thread_main` function that is explained later.\nYou will wonder how the device where a task should be executed is decided. The `InputBuffer` class has a [`device()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/input_buffer.cpp#L173-L189) function that returns the first non-cpu device of all its tensors.\nThis function is used together with [`Engine::ready_queue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1181-L1190) to select the queue to queue a task.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nauto Engine::ready_queue(std::shared_ptr cpu_ready_queue, at::Device device) -> std::shared_ptr{\n if (device.type() == at::kCPU || device.type() == at::DeviceType::Meta) {\n return cpu_ready_queue;\n } else {\n // See Note [Allocating GPUs to autograd threads]\n return device_ready_queues_.at(device.index());\n }\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nThe [`ReadyQueue`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L245-L283) object is defined in `torch/csrc/autograd/engine.h` and it is a simple wrapper over `std::priority_queue` that allows a thread to [wait for a task](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L219) if it\u2019s empty. One interesting property of the `ReadyQueue` is that it increases the [`GraphTask::outstanding_tasks_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L195) value used to determine if the execution has completed or not.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nauto ReadyQueue::push(NodeTask item, bool incrementOutstandingTasks) -> void {\n {\n std::lock_guard lock(mutex_);\n if (incrementOutstandingTasks) {\n std::shared_ptr graph_task = item.base_.lock();\n ++graph_task->outstanding_tasks_;\n }\n heap_.push(std::move(item));\n }\n not_empty_.notify_one();\n}\n\nauto ReadyQueue::pop() -> NodeTask {\n std::unique_lock lock(mutex_);\n not_empty_.wait(lock, [this]{ return !heap_.empty(); });\n auto task = std::move(const_cast(heap_.top())); heap_.pop();\n return task;\n}\n\n```\n\n### Reentrant Backward\n\nA reentrant backward happens when one of the tasks in a backward pass calls again `backward`. It is not a very common case, but it can be used to reduce memory utilization as it could potentially avoid saving intermediate results. For more information, check this [PyTorch forum post](https://discuss.pytorch.org/t/what-is-the-scenario-of-reentrant-backwards-in-pytorch-source-code/19330/2).", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nclass ReentrantBackward(torch.autograd.Function):\n @staticmethod\n def forward(ctx, input):\n return input.sum()\n\n @staticmethod\n def backward(ctx, input):\n # Let's compute the backward by using autograd\n input = input.detach().requires_grad_()\n with torch.enable_grad():\n out = input.sum()\n out.backward() # REENTRANT CALL!!\n return out.detach()\n\n```\n\nHere, we call `backward()` inside `backward()` for a user custom-defined autograd function.\nThis situation can lead to deadlocks because the first backward needs to wait for the second one to complete. But some internal implementation details can prevent the second backward from completing as it is explained in the dedicated subsection.\n## Thread Initialization", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[`execute_with_graph_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1054-L1126) is in charge of initializing the threads taking care of the computation and placing the `root` node in the queue of the device that produced it.\n\n```c++\nc10::intrusive_ptr Engine::execute_with_graph_task(\n const std::shared_ptr& graph_task,\n std::shared_ptr graph_root,\n InputBuffer&& input_buffer) {\n\n initialize_device_threads_pool();\n // Lock mutex for GraphTask.\n std::unique_lock lock(graph_task->mutex_);\n\n auto queue = ready_queue(graph_task->cpu_ready_queue_, input_buffer.device());", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "if (worker_device == NO_DEVICE) {\n set_device(CPU_DEVICE);\n graph_task->owner_ = worker_device;\n queue->push(NodeTask(graph_task, std::move(graph_root), std::move(input_buffer)));\n lock.unlock();\n thread_main(graph_task);\n worker_device = NO_DEVICE;\n } else {\n // This deals with reentrant backwards, we will see it later.\n }\n return graph_task->future_result_;\n}\n\n```\n\nFirst, this function initializes several threads (one per device) calling [` initialize_device_threads_pool()`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1046-L1052) where several things happen:\nOne `ReadyQueue` per device is created.\nOne thread per non-cpu device is created.\nA thread local `worker_device` variable is set to track the current device associated with the thread.\n`thread_main` function is called, and threads wait for tasks to be put in their queues.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Then it retrieves the queue to place the root node based on the device that holds the tensors present in the `input_buffer` using the `ready_queue` function. Now, the main thread (the one also executing the Python interpreter) has its `worker_device` set to `NO_DEVICE`, and it is in charge of executing functions with all its tensors living in the cpu. If `worker_device` is set to any other value, the graph execution is already started, and `.backward()` was called inside a running `Node`, creating a reentrant backward call. This is explained later. For now, \nthe main thread places the task in the queue and call `thread_main`.\n## Where the Magic Happens\n\nIt\u2019s been a long way, but finally, we are ready to traverse the graph and execute the nodes. Each of the spawned threads, and the main thread call [`thread_main`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L377-L464).", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nauto Engine::thread_main(const std::shared_ptr& graph_task) -> void {\n\n while (graph_task == nullptr || !graph_task->future_result_->completed()) {\n std::shared_ptr local_graph_task;\n {\n NodeTask task = local_ready_queue->pop();\n\n if (task.isShutdownTask_) {\n break;\n }\n\n if (!(local_graph_task = task.base_.lock())) {\n // GraphTask for function is no longer valid, skipping further\n // execution.\n continue;\n }\n\n if (task.fn_ && !local_graph_task->has_error_.load()) {\n at::ThreadLocalStateGuard tls_guard(local_graph_task->thread_locals_);", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "try {\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n local_graph_task,\n task.fn_.get(),\n task.inputs_,\n local_graph_task->cpu_ready_queue_);\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }\n }\n\n // Decrement the outstanding tasks.\n --local_graph_task->outstanding_tasks_;\n\n // Check if we've completed execution.\n if (local_graph_task->completed()) {\n local_graph_task->mark_as_completed_and_run_post_processing();\n auto base_owner = local_graph_task->owner_;\n if (worker_device != base_owner) {\n std::atomic_thread_fence(std::memory_order_release);\n ready_queue_by_index(local_graph_task->cpu_ready_queue_, base_owner)\n ->push(NodeTask(local_graph_task, nullptr, InputBuffer(0)));\n }\n }\n }\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nThe code here is simple, given the `local_ready_queue` assigned to each thread in thread-local storage. The threads loop until there are no tasks left to execute in the graph. Note that for device-associated threads, the passed `graph_task` argument is [`nullptr`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L326-L327), and they block in `local_ready_queue->pop()` until a task is pushed in their queue. After some consistency checks (the task type is shutdown, or the graph is still valid). We get to the actual function invocation in `evaluate_function`.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n try {\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n local_graph_task,\n task.fn_.get(),\n task.inputs_,\n local_graph_task->cpu_ready_queue_);\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nAfter calling `evaluate_function`, we check if the `graph_task` execution is complete by looking the `outstanding_tasks_` number. This number increases when a task is pushed to a queue and is decreased in `local_graph_task->completed()` when a task is executed. When the execution is done, we return the results that are be in the `captured_vars_` in case we called `torch.autograd.grad()` instead of `torch.autograd.backward()` as this function returns tensors instead of storing them in the `.grad` attribute of the inputs. Finally we wake up the main thread if it\u2019s waiting by sending a dummy task.\n\n```c++\n // Decrement the outstanding tasks.\n --local_graph_task->outstanding_tasks_;", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "// Check if we've completed execution.\n if (local_graph_task->completed()) {\n local_graph_task->mark_as_completed_and_run_post_processing();\n auto base_owner = local_graph_task->owner_;\n if (worker_device != base_owner) {\n std::atomic_thread_fence(std::memory_order_release);\n ready_queue_by_index(local_graph_task->cpu_ready_queue_, base_owner)\n ->push(NodeTask(local_graph_task, nullptr, InputBuffer(0)));\n }\n }\n\n```\n\n## Calling the Function and Unlocking New Tasks\n\n[`evaluate_function`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L786-L920) serves three purposes:\n\nRun the function.\nAccumulate its results in the next node `InputBuffers`.\nDecrease the dependencies counter of the next nodes and enqueues the tasks reaching 0 to be executed.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nvoid Engine::evaluate_function(\n std::shared_ptr& graph_task,\n Node* func,\n InputBuffer& inputs,\n const std::shared_ptr& cpu_ready_queue) {\n\n // If exec_info_ is not empty, we have to instrument the execution\n auto& exec_info_ = graph_task->exec_info_;\n if (!exec_info_.empty()) {\n // Checks if the function needs to be executed \n if (!fn_info.needed_) {\n // Skip execution if we don't need to execute the function.\n return;\n }\n }\n\n auto outputs = call_function(graph_task, func, inputs);\n\n auto& fn = *func;\n if (!graph_task->keep_graph_) {\n fn.release_variables();\n }\n\n```\n\nInitially, we check the `exec_info_` map of the `GraphTask` structure to determine if the current node needs to be executed. Remember that if this map is empty, all the nodes are executed because we are calculating the grads for all the inputs of the forward pass.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "After this check, the function is executed by running [`call_function`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L735-L784). Its implementation is very straightforward and calls the actual derivative function and registered hooks if any.\n\n```c++\n int num_outputs = outputs.size();\n if (num_outputs == 0) {\n // Records leaf stream (if applicable)\n return;\n }\n\n if (AnomalyMode::is_enabled()) {\n // check for nan values in result\n }\n\n```\n\nNext, we check the outputs of the function after `call_function` is done. If the number of outputs is 0, there are no following nodes to be executed so we can safely return. This is the case of the `AccumulateGrad` node associated with the leaf nodes.\n\n Also, the check for `NaN` values in the gradients is done here if requested.\n```c++", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "std::lock_guard lock(graph_task->mutex_);\n for (const auto i : c10::irange(num_outputs)) {\n auto& output = outputs[i];\n const auto& next = fn.next_edge(i);\n\n if (!next.is_valid()) continue;\n\n \n\n```\n\nWe have now executed a `grad_fn` that has returned one gradient per each of the associated forward pass function inputs. As we saw in the [previous blog post](https://pytorch.org/blog/computational-graphs-constructed-in-pytorch/#linking-nodes-together), we have an `Edge` object per each of these input tensors, and the `grad_fn` of the function producing them in the forward pass. Essentially, Output[0] of the node in the backward pass, corresponds to the first argument of the forward pass associated function. Figure 4 shows how the outputs of a backward function are related to the inputs of the forward function. See that the outputs of `grad_fn C` are the gradients of `z` w.r.t. the inputs of `Function C`", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n

\nFigure 4: Correspondence between forward and backward functions inputs and outputs\n

\n\nWe now iterate through these edges and check if the associated functions are ready to be executed.\n\n```c++\n // Check if the next function is ready to be computed\n bool is_ready = false;\n auto& dependencies = graph_task->dependencies_;\n auto it = dependencies.find(next.function.get());\n\n if (it == dependencies.end()) {\n auto name = next.function->name();\n throw std::runtime_error(std::string(\"dependency not found for \") + name);\n } else if (--it->second == 0) {\n dependencies.erase(it);\n is_ready = true;\n }\n\n auto& not_ready = graph_task->not_ready_;\n auto not_ready_it = not_ready.find(next.function.get());\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nFor this, we check the `graph_task->dependencies_` map. We decrement the counter, and if it reaches 0, we mark the function pointed by the edge ready to be executed. Following, we prepare the input buffers of the tasks indicated by the next edges.\n\n```c++\n if (not_ready_it == not_ready.end()) {\n if (!exec_info_.empty()) {\n // Skip functions that aren't supposed to be executed\n }\n\n // Creates an InputBuffer and moves the output to the corresponding input position\n InputBuffer input_buffer(next.function->num_inputs());\n input_buffer.add(next.input_nr,\n std::move(output),\n opt_parent_stream,\n opt_next_stream);\n\n if (is_ready) {\n auto queue = ready_queue(cpu_ready_queue, input_buffer.device());\n queue->push(\n NodeTask(graph_task, next.function, std::move(input_buffer)));\n } else {\n not_ready.emplace(next.function.get(), std::move(input_buffer));\n }\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nHere, we look for the task in the `graph_task->not_ready_` map. If it is not present, we create a new `InputBuffer` object and set the current output in the `input_nr` position of the buffer associated with the edge. If the task is ready to be executed, we enqueue it in the appropriate device `ready_queue` and complete the execution. However, if the task is not ready and we have seen it before, it is present in the `not_ready_map_`.\n\n```c++\n } else {\n // The function already has a buffer\n auto &input_buffer = not_ready_it->second;\n // Accumulates into buffer\n input_buffer.add(next.input_nr,\n std::move(output),\n opt_parent_stream,\n opt_next_stream);\n if (is_ready) {\n auto queue = ready_queue(cpu_ready_queue, input_buffer.device());\n queue->push(NodeTask(graph_task, next.function, std::move(input_buffer)));\n not_ready.erase(not_ready_it);\n }\n }\n }\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nIn this case, we accumulate the output in the existing `input_buffer` instead of creating a new one. Once all the tasks are processed, the worker thread exits the loop and complete.\nAll this process is summarized in the animation in Figure 5. We see how a thread peeks at the tasks in the ready queue and decrements the next nodes' dependencies, unlocking them for execution.\n\n

\n \n

\n\n

\nFigure 5: Animation of the execution of the computational graph\n

\n\n## Flow with Reentrant Backward\n\nAs we saw above, the reentrant backward problem is when the currently executed function does a nested call to `backward`. When this happens, the thread running this function goes all the way down to `execute_with_graph_task` as in the non-reentrant case, but here is when things are different.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nc10::intrusive_ptr Engine::execute_with_graph_task(\n const std::shared_ptr& graph_task,\n std::shared_ptr graph_root,\n InputBuffer&& input_buffer) {\n\n initialize_device_threads_pool();\n // Lock mutex for GraphTask.\n std::unique_lock lock(graph_task->mutex_);\n\n auto queue = ready_queue(graph_task->cpu_ready_queue_, input_buffer.device());\n\n if (worker_device == NO_DEVICE) {\n //Regular case\n } else {\n // If worker_device is any devices (i.e. CPU, CUDA): this is a re-entrant\n // backward call from that device.\n graph_task->owner_ = worker_device;\n\n // Now that all the non-thread safe fields of the graph_task have been populated,\n // we can enqueue it.\n queue->push(NodeTask(graph_task, std::move(graph_root), std::move(input_buffer)));", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "if (current_depth >= max_recursion_depth_) {\n // If reached the max depth, switch to a different thread\n add_thread_pool_task(graph_task);\n } else {\n ++total_depth;\n ++current_depth;\n lock.unlock();\n thread_main(graph_task);\n --current_depth;\n --total_depth;\n }\n }\n return graph_task->future_result_;\n}\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nHere, `execute_with_graph_task` detects this as a reentrant call and then looks for the current number of nested calls. If it exceeds the limit, we create a new thread to take care of the execution of this graph, and if not, we execute this reentrant call regularly.\nThe limit of nested calls was originally set to avoid stack overflow due to reentrant calls creating very large call stacks. However, the number was further reduced when sanitizer tests were added because of the maximum amount of locks a thread can hold at a given moment. This can be seen in [`torch/csrc/autograd/engine.h`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L36-L42).\n\n\nWhen this maximum depth is exceeded, a new thread is created with the [`add_thread_pool_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1239-L1255) function.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nvoid Engine::add_thread_pool_task(const std::weak_ptr& graph_task) {\n std::unique_lock lck(thread_pool_shared_->mutex_);\n // if we have pending graph_task objects to be processed, create a worker.\n bool create_thread = (thread_pool_shared_->num_workers_ <= thread_pool_shared_->graphtasks_queue_.size());\n thread_pool_shared_->graphtasks_queue_.push(graph_task);\n\n\n lck.unlock();\n if (create_thread) {\n std::thread t(&Engine::reentrant_thread_init, this);\n t.detach();\n }\n\n thread_pool_shared_->work_.notify_one();\n}\n\n\n\n```\n\nBefore going in-depth, let's look at the `thread_pool_shared_` object in the [`Engine`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L421) which manages all the information related to the threads associated to the reentrant backward calls.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n struct ThreadPoolShared {\n unsigned int num_workers_;\n std::condition_variable work_;\n std::mutex mutex_;\n std::queue> graphtasks_queue_;\n\n // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)\n ThreadPoolShared() : num_workers_(0) {}\n };\n\n\n\n ```\n\n[`ThreadPoolShared`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.h#L398-L414) is a simple container holding a queue of `GraphTask` objects with synchronization mechanisms and the number of current workers.\n\nNow it is easy to understand how `add_thread_pool_task` creates a thread when there are `graph_task` objects enqueued and insufficient workers to process them.\n\n`add_thread_pool_task` initializes a thread by executing [`reentrant_thread_init`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L471-L493)", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\nvoid Engine::reentrant_thread_init() {\n at::init_num_threads();\n auto tp_shared = thread_pool_shared_;\n while(true) {\n std::unique_lock lk(tp_shared->mutex_);\n ++thread_pool_shared_->num_workers_;\n tp_shared->work_.wait(lk, [&tp_shared]{ return !tp_shared->graphtasks_queue_.empty();});\n --thread_pool_shared_->num_workers_;\n auto task = tp_shared->graphtasks_queue_.front();\n tp_shared->graphtasks_queue_.pop();\n lk.unlock();\n std::shared_ptr graph_task;\n if (!(graph_task = task.lock())) {\n continue;\n }\n set_device(graph_task->owner_);\n // set the local_ready_queue to the ready queue on the graph_task->owner_ device\n local_ready_queue = ready_queue_by_index(graph_task->cpu_ready_queue_, graph_task->owner_);\n total_depth = graph_task->reentrant_depth_;\n thread_main(graph_task);\n }\n}\n\n\n\n```", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```\n\nThe code is straightforward. The newly created thread waits on the `thread_pool_shared->graphtasks_queue_` for reentrant backward graphs to be available and executes them. Notice that this thread uses the task-ready queue associated with the device of the thread that started this call by accessing the `graph_task->owner_` field set in the [`execute_with_graph_task`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L1092) function. \n\n## Error Handling\n\nWhenever an error happens in one of the worker threads. It will be propagated to the `backward` calling thread.\n\nTo achieve this, there is a try/catch block in the [`thread_main`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L415-L438) that catches any exception in the `Node` function call and sets it to the associated `GraphTask` object.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n try {\n \u2026\n GraphTaskGuard guard(local_graph_task);\n NodeGuard ndguard(task.fn_);\n {\n evaluate_function(\n \u2026\n }\n } catch (std::exception& e) {\n thread_on_exception(local_graph_task, task.fn_, e);\n }\n }\n }\n\n```\n\n[`thread_on_exception`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L495-L500) and the [functions it calls](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/autograd/engine.cpp#L605-L621) end up setting the exception in the `local_graph_task` object.\n\n```c++\nvoid Engine::thread_on_exception(\n std::shared_ptr graph_task,\n const std::shared_ptr& fn,\n std::exception& e) {\n graph_task->set_exception(std::current_exception(), fn);\n}", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "void GraphTask::set_exception_without_signal(const std::shared_ptr& fn) {\n if (!has_error_.exchange(true)) {\n if (AnomalyMode::is_enabled() && fn) {\n fn->metadata()->print_stack(fn->name());\n }\n }\n}\n\nvoid GraphTask::set_exception(\n std::exception_ptr eptr,\n const std::shared_ptr& fn) {\n set_exception_without_signal(fn);\n if (!future_completed_.exchange(true)) {\n // NOLINTNEXTLINE(performance-move-const-arg)\n future_result_->setError(std::move(eptr));\n }\n}\n\n```\n\nIn `set_exception` it sets the `has_error_` flag to `true` and it calls the [`setError`]()\nfunction of the [`future_result_`](https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/aten/src/ATen/core/ivalue_inl.h#L770-L1322) object. This will make the error to be re-thrown at the caller thread when `future_result_->value()` is accessed.", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```c++\n IValue value() {\n std::unique_lock lock(mutex_);\n AT_ASSERT(completed());\n if (eptr_) {\n std::rethrow_exception(eptr_);\n }\n return value_;\n }\n\n```\n\n# Closing Remarks\n\nThis has been the last post of this series covering how PyTorch does the auto differentiation. We hope you enjoyed reading it and that now you are familiar enough with PyTorch internals to start contributing in PyTorch development!", "metadata": {"source": "https://pytorch.org/blog/how-computational-graphs-are-executed-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.6 released w/ Native AMP Support, Microsoft joins as maintainers for Windows'\nauthor: Team PyTorch\n---\n\nToday, we\u2019re announcing the availability of PyTorch 1.6, along with updated domain libraries. We are also excited to announce the team at [Microsoft is now maintaining Windows builds and binaries](https://pytorch.org/blog/microsoft-becomes-maintainer-of-the-windows-version-of-pytorch) and will also be supporting the community on GitHub as well as the PyTorch Windows discussion forums.\n\nThe PyTorch 1.6 release includes a number of new APIs, tools for performance improvement and profiling, as well as major updates to both distributed data parallel (DDP) and remote procedure call (RPC) based distributed training. \nA few of the highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "1. Automatic mixed precision (AMP) training is now natively supported and a stable feature (See [here](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/) for more details) - thanks for NVIDIA\u2019s contributions; \n2. Native TensorPipe support now added for tensor-aware, point-to-point communication primitives built specifically for machine learning; \n3. Added support for complex tensors to the frontend API surface;\n4. New profiling tools providing tensor-level memory consumption information;\n5. Numerous improvements and new features for both distributed data parallel (DDP) training and the remote procedural call (RPC) packages.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "Additionally, from this release onward, features will be classified as Stable, Beta and Prototype. Prototype features are not included as part of the binary distribution and are instead available through either building from source, using nightlies or via compiler flag. You can learn more about what this change means in the post [here](https://pytorch.org/blog/pytorch-feature-classification-changes/). You can also find the full release notes [here](https://github.com/pytorch/pytorch/releases). \n\n# Performance & Profiling\n\n## [Stable] Automatic Mixed Precision (AMP) Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "AMP allows users to easily enable automatic mixed precision training enabling higher performance and memory savings of up to 50% on Tensor Core GPUs. Using the natively supported `torch.cuda.amp` API, AMP provides convenience methods for mixed precision, where some operations use the `torch.float32 (float)` datatype and other operations use `torch.float16 (half)`. Some ops, like linear layers and convolutions, are much faster in `float16`. Other ops, like reductions, often require the dynamic range of `float32`. Mixed precision tries to match each op to its appropriate datatype.\n\n* Design doc ([Link](https://github.com/pytorch/pytorch/issues/25081))\n* Documentation ([Link](https://pytorch.org/docs/stable/amp.html))\n* Usage examples ([Link](https://pytorch.org/docs/stable/notes/amp_examples.html))\n\n## [Beta] Fork/Join Parallelism", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "This release adds support for a language-level construct as well as runtime support for coarse-grained parallelism in TorchScript code. This support is useful for situations such as running models in an ensemble in parallel, or running bidirectional components of recurrent nets in parallel, and allows the ability to unlock the computational power of parallel architectures (e.g. many-core CPUs) for task level parallelism.\n\nParallel execution of TorchScript programs is enabled through two primitives: `torch.jit.fork` and `torch.jit.wait`. In the below example, we parallelize execution of `foo`:\n\n```python\nimport torch\nfrom typing import List\n\ndef foo(x):\n return torch.neg(x)\n\n@torch.jit.script\ndef example(x):\n futures = [torch.jit.fork(foo, x) for _ in range(100)]\n results = [torch.jit.wait(future) for future in futures]\n return torch.sum(torch.stack(results))\n\nprint(example(torch.ones([])))\n ```\n \n* Documentation ([Link](https://pytorch.org/docs/stable/jit.html))\n\n## [Beta] Memory Profiler", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "The `torch.autograd.profiler` API now includes a memory profiler that lets you inspect the tensor memory cost of different operators inside your CPU and GPU models.\n\nHere is an example usage of the API:\n\n```python\nimport torch\nimport torchvision.models as models\nimport torch.autograd.profiler as profiler\n\nmodel = models.resnet18()\ninputs = torch.randn(5, 3, 224, 224)\nwith profiler.profile(profile_memory=True, record_shapes=True) as prof:\n model(inputs)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "# NOTE: some columns were removed for brevity\nprint(prof.key_averages().table(sort_by=\"self_cpu_memory_usage\", row_limit=10))\n# --------------------------- --------------- --------------- ---------------\n# Name CPU Mem Self CPU Mem Number of Calls\n# --------------------------- --------------- --------------- ---------------\n# empty 94.79 Mb 94.79 Mb 123\n# resize_ 11.48 Mb 11.48 Mb 2\n# addmm 19.53 Kb 19.53 Kb 1\n# empty_strided 4 b 4 b 1\n# conv2d 47.37 Mb 0 b 20\n# --------------------------- --------------- --------------- ---------------\n ```\n\n* PR ([Link](https://github.com/pytorch/pytorch/pull/37775))\n* Documentation ([Link](https://pytorch.org/docs/stable/autograd.html#profiler))\n\n# Distributed Training & RPC \n\n## [Beta] TensorPipe backend for RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 1.6 introduces a new backend for the RPC module which leverages the TensorPipe library, a tensor-aware point-to-point communication primitive targeted at machine learning, intended to complement the current primitives for distributed training in PyTorch (Gloo, MPI, ...) which are collective and blocking. The pairwise and asynchronous nature of TensorPipe lends itself to new networking paradigms that go beyond data parallel: client-server approaches (e.g., parameter server for embeddings, actor-learner separation in Impala-style RL, ...) and model and pipeline parallel training (think GPipe), gossip SGD, etc.\n\n```python\n# One-line change needed to opt in\ntorch.distributed.rpc.init_rpc(\n ...\n backend=torch.distributed.rpc.BackendType.TENSORPIPE,\n)\n\n# No changes to the rest of the RPC API\ntorch.distributed.rpc.rpc_sync(...)\n```\n\n* Design doc ([Link](https://github.com/pytorch/pytorch/issues/35251))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc/index.html))\n\n## [Beta] DDP+RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "## [Beta] DDP+RPC \n\nPyTorch Distributed supports two powerful paradigms: DDP for full sync data parallel training of models and the RPC framework which allows for distributed model parallelism. Previously, these two features worked independently and users couldn\u2019t mix and match these to try out hybrid parallelism paradigms.\n\nStarting in PyTorch 1.6, we\u2019ve enabled DDP and RPC to work together seamlessly so that users can combine these two techniques to achieve both data parallelism and model parallelism. An example is where users would like to place large embedding tables on parameter servers and use the RPC framework for embedding lookups, but store smaller dense parameters on trainers and use DDP to synchronize the dense parameters. Below is a simple code snippet. \n\n```python\n// On each trainer\n\nremote_emb = create_emb(on=\"ps\", ...)\nddp_model = DDP(dense_model)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "for data in batch:\n with torch.distributed.autograd.context():\n res = remote_emb(data)\n loss = ddp_model(res)\n torch.distributed.autograd.backward([loss])\n```\n\n* DDP+RPC Tutorial ([Link](https://pytorch.org/tutorials/advanced/rpc_ddp_tutorial.html))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc/index.html))\n* Usage Examples ([Link](https://github.com/pytorch/examples/pull/800))\n\n## [Beta] RPC - Asynchronous User Functions", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "RPC Asynchronous User Functions supports the ability to yield and resume on the server side when executing a user-defined function. Prior to this feature, when a callee processes a request, one RPC thread waits until the user function returns. If the user function contains IO (e.g., nested RPC) or signaling (e.g., waiting for another request to unblock), the corresponding RPC thread would sit idle waiting for these events. As a result, some applications have to use a very large number of threads and send additional RPC requests, which can potentially lead to performance degradation. To make a user function yield on such events, applications need to: 1) Decorate the function with the `@rpc.functions.async_execution` decorator; and 2) Let the function return a `torch.futures.Future` and install the resume logic as callbacks on the `Future` object. See below for an example:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "```python\n@rpc.functions.async_execution\ndef async_add_chained(to, x, y, z):\n return rpc.rpc_async(to, torch.add, args=(x, y)).then(\n lambda fut: fut.wait() + z\n )\n\nret = rpc.rpc_sync(\n \"worker1\", \n async_add_chained, \n args=(\"worker2\", torch.ones(2), 1, 1)\n)\n \nprint(ret) # prints tensor([3., 3.])\n```\n\n* Tutorial for performant batch RPC using Asynchronous User Functions ([Link](https://github.com/pytorch/tutorials/blob/release/1.6/intermediate_source/rpc_async_execution.rst))\n* Documentation ([Link](https://pytorch.org/docs/stable/rpc.html#torch.distributed.rpc.functions.async_execution))\n* Usage examples ([Link](https://github.com/pytorch/examples/tree/master/distributed/rpc/batch))\n\n# Frontend API Updates\n\n## [Beta] Complex Numbers", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "The PyTorch 1.6 release brings beta level support for complex tensors including torch.complex64 and torch.complex128 dtypes. A complex number is a number that can be expressed in the form a + bj, where a and b are real numbers, and j is a solution of the equation x^2 = \u22121. Complex numbers frequently occur in mathematics and engineering, especially in signal processing and the area of complex neural networks is an active area of research. The beta release of complex tensors will support common PyTorch and complex tensor functionality, plus functions needed by Torchaudio, ESPnet and others. While this is an early version of this feature, and we expect it to improve over time, the overall goal is provide a NumPy compatible user experience that leverages PyTorch\u2019s ability to run on accelerators and work with autograd to better support the scientific community. \n\n# Mobile Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "# Mobile Updates\n\nPyTorch 1.6 brings increased performance and general stability for mobile on-device inference. We squashed a few bugs, continued maintenance and added few new features while improving fp32 and int8 performance on a large variety of ML model inference on CPU backend.\n\n## [Beta] Mobile Features and Performance \n\n* Stateless and stateful XNNPACK Conv and Linear operators\n* Stateless MaxPool2d + JIT optimization passes\n* JIT pass optimizations: Conv + BatchNorm fusion, graph rewrite to replace conv2d/linear with xnnpack ops, relu/hardtanh fusion, dropout removal\n* QNNPACK integration removes requantization scale constraint\n* Per-channel quantization for conv, linear and dynamic linear\n* Disable tracing for mobile client to save ~600 KB on full-jit builds\n\n# Updated Domain Libraries\n\n## torchvision 0.7", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "## torchvision 0.7 \n\ntorchvision 0.7 introduces two new pretrained semantic segmentation models, [FCN ResNet50](https://arxiv.org/abs/1411.4038) and [DeepLabV3 ResNet50](https://arxiv.org/abs/1706.05587), both trained on COCO and using smaller memory footprints than the ResNet101 backbone. We also introduced support for AMP (Automatic Mixed Precision) autocasting for torchvision models and operators, which automatically selects the floating point precision for different GPU operations to improve performance while maintaining accuracy. \n\n* Release notes ([Link](https://github.com/pytorch/vision/releases))\n\n## torchaudio 0.6\n\ntorchaudio now officially supports Windows. This release also introduces a new model module (with wav2letter included), new functionals (contrast, cvm, dcshift, overdrive, vad, phaser, flanger, biquad), datasets (GTZAN, CMU), and a new optional sox backend with support for TorchScript.\n\n* Release notes ([Link](https://github.com/pytorch/audio/releases))\n\n# Additional updates\n\n## HACKATHON", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "## HACKATHON\n\nThe Global PyTorch Summer Hackathon is back! This year, teams can compete in three categories virtually:\n\n 1. **PyTorch Developer Tools:** Tools or libraries designed to improve productivity and efficiency of PyTorch for researchers and developers\n 2. **Web/Mobile Applications powered by PyTorch:** Applications with web/mobile interfaces and/or embedded devices powered by PyTorch \n 3. **PyTorch Responsible AI Development Tools:** Tools, libraries, or web/mobile apps for responsible AI development\n\nThis is a great opportunity to connect with the community and practice your machine learning skills. \n\n* [Join the hackathon](http://pytorch2020.devpost.com/)\n* [Watch educational videos](https://www.youtube.com/pytorch)\n\n\n## LPCV Challenge", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "## LPCV Challenge\n\nThe [2020 CVPR Low-Power Vision Challenge (LPCV) - Online Track for UAV video](https://lpcv.ai/2020CVPR/video-track) submission deadline is coming up shortly. You have until July 31, 2020 to build a system that can discover and recognize characters in video captured by an unmanned aerial vehicle (UAV) accurately using PyTorch and Raspberry Pi 3B+. \n\n## Prototype Features\n\nTo reiterate, Prototype features in PyTorch are early features that we are looking to gather feedback on, gauge the usefulness of and improve ahead of graduating them to Beta or Stable. The following features are not part of the PyTorch 1.6 release and instead are available in nightlies with separate docs/tutorials to help facilitate early usage and feedback.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "#### Distributed RPC/Profiler\nAllow users to profile training jobs that use `torch.distributed.rpc` using the autograd profiler, and remotely invoke the profiler in order to collect profiling information across different nodes. The RFC can be found [here](https://github.com/pytorch/pytorch/issues/39675) and a short recipe on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).\n\n#### TorchScript Module Freezing\nModule Freezing is the process of inlining module parameters and attributes values into the TorchScript internal representation. Parameter and attribute values are treated as final value and they cannot be modified in the frozen module. The PR for this feature can be found [here](https://github.com/pytorch/pytorch/pull/32178) and a short tutorial on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "#### Graph Mode Quantization\nEager mode quantization requires users to make changes to their model, including explicitly quantizing activations, module fusion, rewriting use of torch ops with Functional Modules and quantization of functionals are not supported. If we can trace or script the model, then the quantization can be done automatically with graph mode quantization without any of the complexities in eager mode, and it is configurable through a `qconfig_dict`. A tutorial on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} +{"page_content": "#### Quantization Numerical Suite\nQuantization is good when it works, but it\u2019s difficult to know what's wrong when it doesn't satisfy the expected accuracy. A prototype is now available for a Numerical Suite that measures comparison statistics between quantized modules and float modules. This is available to test using eager mode and on CPU only with more support coming. A tutorial on how to use this feature can be found [here](https://github.com/pytorch/tutorials/tree/master/prototype_source).\n\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.6-released/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerating Large Language Models with Accelerated Transformers\"\nauthor: Lucas Pasqualin, Driss Guessous, Christian Puhrsch, Bertrand Maher, Michael Gschwind\n---", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "**TL;DR.** We show how to use Accelerated PyTorch 2.0 Transformers and the newly introduced `torch.compile()` method to accelerate Large Language Models on the example of [nanoGPT](https://github.com/karpathy/nanoGPT), a compact open-source implementation of the GPT model from Andrej Karpathy. Using the new [scaled dot product attention operator](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) introduced with Accelerated PT2 Transformers, we select the", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "flash_attention custom kernel and achieve faster training time per batch (measured with Nvidia A100 GPUs), going from a ~143ms/batch baseline to ~113 ms/batch. In addition, the enhanced implementation using the SDPA operator offers better numerical stability. Finally, further optimizations are achieved using padded inputs, which when combined with flash attention lead to ~87ms/batch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Recent times have seen exponential adoption of large language models (LLMs) and Generative AI in everyday life. Tightly coupled with these ever-growing models is the ever-growing training cost - in terms of both time and hardware utilization. The PyTorch team has tackled these challenges head on with [Accelerated PyTorch 2 Transformers](https://pytorch.org/blog/accelerated-pytorch-2/) (previously known as \u201cBetter Transformer\u201d) and JIT Compilation in [PyTorch", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "2.0](https://pytorch.org/blog/pytorch-2.0-release/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "In this blog post, we explore training optimizations gained by utilizing custom kernel implementations of SDPA - also known as scaled dot product attention - a critical layer in transformer models. The custom kernel for SDPA replaces several discrete sequential operations with one globally optimized kernel which avoids allocating a large amount of intermediate CUDA memory. This approach offers a number of advantages, including but not limited to: higher performance computation of SDPA by reducing memory", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "bandwidth bottleneck, reduced memory footprint to support larger batch sizes, and finally added numerical stability by prescaling input tensors. These optimizations are demonstrated on nanoGPT, an open-source implementation of GPT from Andrej Karpathy.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Background \n\nScaled dot product attention is the fundamental building block of multihead attention, as introduced in [\u201cAttention is All You Need\u201d](https://arxiv.org/abs/1706.03762), and has a wide range of applications in LLM and Generative AI models.\n\n![The Transformer model architecture](/assets/images/2023-04-18-accelerating-large-language-models/PyTorch_Better-Transformer_Figure-1.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "**Figure 1:** The Transformer model architecture based on [\u201cAttention is All You Need\u201d](https://arxiv.org/abs/1706.03762). With the new PyTorch SDPA operator, Multi-Head Attention is efficiently implemented by a linear layer for the in-projection, the SDPA operator, and a linear layer for the out-projection.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "With the new scaled_dot_product_attention operator, multihead attention can be implemented in just 3 steps: in projection with a linear layer, SDPA, and out projection with a linear layer.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "```\n# In Projection\n# variable descriptions:\n# q,k,v = Query, Key, Value tensors\n# bsz = batch size\n# num_heads = Numner of heads for Multihead Attention\n# tgt_len = Target length\n# src_len = Source Length\n# head_dim: Head Dimension\n q, k, v = _in_projection(query, key, value, q_proj_weight, k_proj_weight, v_proj_weight, b_q, b_k, b_v)\n q = q.view(bsz, num_heads, tgt_len, head_dim)\n k = k.view(bsz, num_heads, src_len, head_dim)\n v = v.view(bsz, num_heads, src_len, head_dim)", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "# Scaled Dot Product Attention\n attn_output = scaled_dot_product_attention(q, k, v, attn_mask, dropout_p, is_causal)\n\n # Out Projection\n attn_output = attn_output.permute(2, 0, 1, 3).contiguous().view(bsz * tgt_len, embed_dim)\n attn_output = linear(attn_output, out_proj_weight, out_proj_bias)\n attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "**TL;DR.** We show how to use Accelerated PyTorch 2.0 Transformers and the newly introduced `torch.compile()` method to accelerate Large Language Models on the example of [nanoGPT](https://github.com/karpathy/nanoGPT), a compact open-source implementation of the GPT model from Andrej Karpathy. Using the new [scaled dot product attention operator](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) introduced with Accelerated PT2 Transformers, we select the flash_attention custom kernel and achieve faster training time per batch (measured with Nvidia A100 GPUs), going from a ~143ms/batch baseline to ~113 ms/batch. In addition, the enhanced implementation using the SDPA operator offers better numerical stability. Finally, further optimizations are achieved using padded inputs, which when combined with flash attention lead to ~87ms/batch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "Recent times have seen exponential adoption of large language models (LLMs) and Generative AI in everyday life. Tightly coupled with these ever-growing models is the ever-growing training cost - in terms of both time and hardware utilization. The PyTorch team has tackled these challenges head on with [Accelerated PyTorch 2 Transformers](https://pytorch.org/blog/accelerated-pytorch-2/) (previously known as \u201cBetter Transformer\u201d) and JIT Compilation in [PyTorch 2.0](https://pytorch.org/blog/pytorch-2.0-release/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "In this blog post, we explore training optimizations gained by utilizing custom kernel implementations of SDPA - also known as scaled dot product attention - a critical layer in transformer models. The custom kernel for SDPA replaces several discrete sequential operations with one globally optimized kernel which avoids allocating a large amount of intermediate CUDA memory. This approach offers a number of advantages, including but not limited to: higher performance computation of SDPA by reducing memory bandwidth bottleneck, reduced memory footprint to support larger batch sizes, and finally added numerical stability by prescaling input tensors. These optimizations are demonstrated on nanoGPT, an open-source implementation of GPT from Andrej Karpathy.\n\n\n## Background \n\nScaled dot product attention is the fundamental building block of multihead attention, as introduced in [\u201cAttention is All You Need\u201d](https://arxiv.org/abs/1706.03762), and has a wide range of applications in LLM and Generative AI models.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "![The Transformer model architecture](/assets/images/2023-04-18-accelerating-large-language-models/PyTorch_Better-Transformer_Figure-1.png){:style=\"max-height:800px; width:100%\"} \n\n**Figure 1:** The Transformer model architecture based on [\u201cAttention is All You Need\u201d](https://arxiv.org/abs/1706.03762). With the new PyTorch SDPA operator, Multi-Head Attention is efficiently implemented by a linear layer for the in-projection, the SDPA operator, and a linear layer for the out-projection.\n\n\nWith the new scaled_dot_product_attention operator, multihead attention can be implemented in just 3 steps: in projection with a linear layer, SDPA, and out projection with a linear layer.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "```\n# In Projection\n# variable descriptions:\n# q,k,v = Query, Key, Value tensors\n# bsz = batch size\n# num_heads = Numner of heads for Multihead Attention\n# tgt_len = Target length\n# src_len = Source Length\n# head_dim: Head Dimension\n q, k, v = _in_projection(query, key, value, q_proj_weight, k_proj_weight, v_proj_weight, b_q, b_k, b_v)\n q = q.view(bsz, num_heads, tgt_len, head_dim)\n k = k.view(bsz, num_heads, src_len, head_dim)\n v = v.view(bsz, num_heads, src_len, head_dim)\n\n # Scaled Dot Product Attention\n attn_output = scaled_dot_product_attention(q, k, v, attn_mask, dropout_p, is_causal)\n\n # Out Projection\n attn_output = attn_output.permute(2, 0, 1, 3).contiguous().view(bsz * tgt_len, embed_dim)\n attn_output = linear(attn_output, out_proj_weight, out_proj_bias)\n attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))\n```", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} {"page_content": "PyTorch 2. supports multiple different kernels optimized for specific use cases, with specific requirements. A kernel picker picks the best kernel for a particular combination of input parameters. If no optimized \"custom kernel\" for a particular combination of input parameters can be identified, the kernel picker selects a general kernel that can handle all input combinations. \n\nWhile future releases may extend this set of operators, PyTorch 2.0 launches with 3 implementations for the SDPA operator:", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "1. A generic kernel which implements the mathematical equation of SDPA in the function `sdpa_math()`\n2. An optimized kernel based on the paper \u201c[Flash Attention](https://arxiv.org/abs/2205.14135)\u201d, which supports evaluation of SDPA with 16 bit floating point data types on compute architecture SM80 (A100).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "3. An optimized kernel based on the paper \u201c[Self-Attention Does Not Need O(n^2) Memory](https://arxiv.org/abs/2112.0568)\" and implemented in [xFormer](https://github.com/facebookresearch/xformers), which supports both 32 and 16 bit floating data types on a wider range of architectures (SM40 and later). This blog post refers to this kernel as the `mem_efficient` kernel.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Note that both optimized kernels (two and three listed above), support a key padding mask and limit the supported attention mask to causal attention. Accelerated PyTorch 2.0 Transformers today only support the causal mask when it is specified using the `is_causal` boolean. When a mask is specified, the general-purpose kernel will be selected because it is too expensive to analyze the contents of a provided mask to determine if it is the causal mask. Additional explanations on the constraints for each kernel", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "can be found in the [Accelerated PT2 Transformer blog](https://pytorch.org/blog/accelerated-pytorch-2/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Enabling Accelerated Transformers with nanoGPT\n\nThe SDPA operator being a critical component of the GPT model, we identified the open source nanoGPT model as an excellent candidate for both demonstrating the ease of implementation and benefits of PyTorch 2.0\u2019s Accelerated Transformers. The following demonstrates the exact process by which Accelerated Transformers was enabled on nanoGPT.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "This process largely revolves around replacing the existing SDPA implementation with the newly added F.scaled_dot_product_attention operator from [functional.py](https://github.com/pytorch/pytorch/blob/df14650f0b14b80db132b0c1797dc595fbee1054/torch/nn/functional.py#L4834). This process can be easily adapted to enable the operator in many other LLMs. Alternatively, users can instead choose to call F.multi_head_attention_forward() or utilize the nn.MultiHeadAttention module directly where applicable. The", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "following code snippets are adapted from Karpathy\u2019s nanoGPT repository.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Step 1: Identify the existing SDPA implementation\n\nIn the case of nanoGPT, SDPA is implemented in the model\u2019s [CausalSelfAttention](https://github.com/karpathy/nanoGPT/blob/master/model.py#L37) class. The original implementation at time of writing is adapted below for this post.\n\n![The original implementation at time of writing](/assets/images/2023-04-18-accelerating-large-language-models/causal_attention_step_1.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Step 2: Replace with Torch\u2019s _scaled_dot_product_attention_\n\nAt this point we can note the following:\n\n* Lines 36 - 42 define the mathematical implementation of SDPA which we are replacing\n* The mask applied on line 39 is no longer relevant since we are using scaled_dot_product_attention\u2019s `is_causal` flag.\n* The dropout layer used in line 41 is also now unnecessary.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Swapping out the SDPA implementation for torch\u2019s scaled_dot_product_attention and removing the now redundant code yields the following implementation.\n\n![Swapping out the SDPA implementation for torch\u2019s scaled_dot_product_attention and removing the now redundant code yields the following implementation.](/assets/images/2023-04-18-accelerating-large-language-models/causal_attention_step_2.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Alternatively, the original mask can be passed into the `attn_mask` field however due to the mentioned kernel constraints that would limit the implementation to only support the generic `sdpa_math` kernel.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Step 3 (Bonus): Faster matmuls with padding\n\nOn top of the performance improvements from SDPA, our analysis yielded a nice ancillary win. In Andrej's words \"The most dramatic optimization to nanoGPT so far (~25% speedup) is to simply increase the vocab size from 50257 to 50304 (nearest multiple of 64).\"\n\n\n![Tweet by Andrej Karpathy](/assets/images/2023-04-18-accelerating-large-language-models/tweet.png){:style=\"max-height:800px; width:100%; max-width:600px\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "The vocab size determines the dimensions of matmuls in the output layer of GPT, and these are so large that they were taking a _majority_ of the time for the entire training loop! We discovered that they were achieving performance significantly below the peak throughput achievable on the A100 GPU, and guessed from [NVIDIA's matmul documentation](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html) that 64-element alignment would yield better results. Indeed,", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "padding these matmuls achieves nearly a 3x speedup! The underlying cause is that unaligned memory accesses significantly reduce efficiency. A deeper analysis can be found in [this Twitter thread](https://twitter.com/cHHillee/status/1630274804795445248).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "With this optimization we were able to further reduce training time from ~113 ms (using flash attention) to ~87 ms per batch.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Results\n\nThe figure below demonstrates the performance gained using Pytorch custom kernels. Here are the exact figures:\n\n* baseline (nanoGPT implementation): ~143ms\n* sdpa_math (generic): ~134ms (6.71% faster)\n* `mem_efficient` kernel: ~119ms (20.16% faster)\n* `flash_attention` kernel: ~113ms (26.54% faster)\n* flash_attention + padded vocab: ~87ms (64.37% faster)", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "All code was run on an 8 x NVIDIA Corporation A100 server with 80 GB HBM [A100 SXM4 80GB], and for the purpose of this experiment dropout was set to 0.\n\n\n![Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models](/assets/images/2023-04-18-accelerating-large-language-models/PyTorch_Better-Transformer_Chart-2.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "**Figure 2:** Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Enhancing Numerical Model Stability", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "In addition to being faster, PyTorch's implementation offers increased numerical stability by avoiding loss of precision in many execution scenarios. There is a great explanation [here](https://github.com/bigscience-workshop/Megatron-DeepSpeed/pull/118), but essentially the PyTorch implementation scales the Query and Key matrices _before_ multiplication, which is said to be more stable and avoid loss of precision. Because of the merged custom kernel architecture of SDPA, this scaling does not introduce", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "additional overhead in the computation of the attention result. In comparison, an implementation from the individual computational components would require separate pre-scaling at additional cost. For an additional explanation, see Appendix A.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Improved Memory Consumption", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Yet another large advantage of using the torch SDPA kernels is the reduced memory footprint, which allows for the utilization of larger batch sizes. The following chart compares the best validation loss after one hour of training for both flash attention and the baseline implementations of causal attention. As can be seen, the maximum batch size achieved with the baseline causal attention implementation (on 8 x NVIDIA Corporation A100 server with 80 GB HBM) was 24, significantly less then the maximum", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "achieved with flash attention, which was 39.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "![Using Flash Attention enables the usage of larger batch sizes](/assets/images/2023-04-18-accelerating-large-language-models/chart.png){:style=\"max-height:800px; width:100%\"} \n\n\n**Figure 3:** Using Flash Attention enables the usage of larger batch sizes, allowing users to achieve lower validation loss after one hour of training (smaller is better).", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Conclusion\n\nAccelerated PyTorch 2 Transformers were designed to make the training and production deployment of state-of-the-art transformer models affordable and integrated with PyTorch 2.0 model JIT compilation. The newly introduced PyTorch SDPA operator provides improved performance for training Transformer models and is particularly valuable for the expensive Large Language Model training. In this post we demonstrate a number of optimizations on the exemplary nanoGPT model including:", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "* Over 26% training speedup, when compared against the baseline with constant batch size\n* An additional speedup achieved with padded vocabulary, bringing the total optimization to approximately 64% compared to the baseline\n* Additional numerical stability", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Appendix A: Analyzing Attention Numeric Stability\n\nIn this section we provide a more in depth explanation of the previously mentioned enhanced numerical stability which is gained by prescaling SDPA\u2019s input vectors. The following is a simplified version of nanoGPT\u2019s mathematical implementation of SDPA. The important thing to note here is that the query undergoes matrix multiplication without being scaled.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "```\n# nanoGPT implementation of SDPA\n# notice q (our query vector) is not scaled !\natt = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))\natt = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))\natt = F.softmax(att, dim=-1)\n\n# Dropout is set to 0, so we can safely ignore this line in the implementation# att = self.attn_dropout(att) \n\ny_nanogpt = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "The following is the equivalent mathematical implementation in torch\u2019s `scaled_dot_product_attention`.\n\n```\n# PyTorch implementation of SDPA\nembed_size = q.size(-1)\nscaling_factor = math.sqrt(math.sqrt(embed_size))\nq = q / scaling_factor \t# notice q _is_ scaled here !\n\n# same as above, but with scaling factor\natt = q @ (k.transpose(-2, -1) / scaling_factor)\natt = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))\natt = F.softmax(att0, dim=-1)", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "# Dropout is set to 0, so we can safely ignore this line in the implementation# att = self.attn_dropout(att) \n\ny_scale_before = att @ v", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Mathematically both approaches should be equivalent, however our experimentation shows that in practice we receive different results from each approach. \n\nUsing the approach above, we verified `y_scale_before` matches the expected output from using the `scaled_dot_product_attention `method while `y_nanogpt` does not.\n\nThe `torch.allclose` method was used to test equivalence. Specifically, we showed that:", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "```\ny_sdpa = torch.nn.functional._scaled_dot_product_attention(\n\tq,\n\tk,\n\tv,\n\tattn_mask=self.bias[:,:,:T,:T] != 0,\n\tdropout_p=0.0,\n\tneed_attn_weights=False,\n\tis_causal=False,\n)\n\ntorch.allclose(y_sdpa, y_nanogpt) # False, indicating fp issues\ntorch.allclose(y_sdpa, y_scale_before) # True, as expected\n```", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Appendix B: Reproducing Experiment Results", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "Researchers seeking to reproduce these results should start with the following commit from Andrej\u2019s nanoGPT repository - **b3c17c6c6a363357623f223aaa4a8b1e89d0a465**. This commit was used as the baseline when measuring the per batch speed improvements. For results which include padded vocabulary optimizations (which yielded the most significant improvements to batch speed), use the following commit - **77e7e04c2657846ddf30c1ca2dd9f7cbb93ddeab**. From either checkout, selecting kernels for experimentation is made trivial with the use of the [torch.backends](https://pytorch.org/docs/stable/backends.html) API.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "The desired kernel can be selected via a context manager:\n\n```\nwith torch.backends.cuda.sdp_kernel (\n enable_math = False,\n enable_flash = False,\n enable_mem_efficient = True\n):\n train(model)\n```", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Straggler Mitigation On PyTorch DDP By Hierarchical SGD\"\nauthor: Yi Wang (Cruise AI), Rohan Varma (Meta AI)\n---", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "[PyTorch DDP](https://pytorch.org/docs/stable/notes/ddp.html) has been widely adopted across the industry for distributed training, which by default runs synchronous SGD to synchronize gradients across model replicas at every step. The performance of this technique is critical for fast iteration during model exploration as well as resource and cost saving. The performance is critical for fast iteration and cost saving of model development and exploration. To resolve a ubiquitous performance bottleneck", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "introduced by slow nodes in large-scale training, Cruise and Meta co-developed a solution based on the [Hierarchical SGD](https://arxiv.org/abs/2007.13819) algorithm to significantly accelerate training in the presence of these stragglers.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The Need For Straggler Mitigation\n\nIn DDP setup, a straggler problem can occur when one or more processes run much slower (\"stragglers\") than other processes. When this happens, all the processes have to wait for the stragglers before synchronizing gradients and completing the communication, which essentially bottlenecks distributed performance to the slowest worker.As a result, even for the cases of training relatively small models, the communication cost can still be a major performance bottleneck.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Potential Causes of Stragglers\n\nSevere straggler issues are usually caused by workload imbalance before synchronization, and many factors can contribute to this imbalance. For instance, some data loader workers in the distributed environment can become stragglers, because some input examples can be outliers in terms of the data size, or the data transfer of some examples can be drastically slowed down due to unstable network I/O, or the on-the-fly data transformation costs can have a high variance.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Besides data loading, other phases before gradient synchronization can also cause stragglers, such as unbalanced workloads of embedding table lookup during the forward pass in recommendation systems.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The Appearance of Stragglers", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "If we profile DDP training jobs that have stragglers, we can find that some processes may have much higher gradient synchronization costs (a.k.a., allreducing gradients) than other processes at a certain step. As a result, the distributed performance can be dominated by the communication cost even if the model size is very small. In this case, some processes run faster than the straggler(s) at a step, and hence they have to wait for the stragglers and spend a much longer time on allreduce.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The below shows screenshots of two trace files output by PyTorch profiler in a use case. Each screenshot profiles 3 steps.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "* The first screenshot shows that a process has a very high allreduce cost in both the first and the third steps, because this process reaches the synchronization phase earlier than the straggler(s), and it spends more time on waiting. On the other hand, the allreduce cost is relatively small in the second step, this suggests that 1) there is no straggler at this step; or 2) this process is the straggler among all the processes, so it does not need to wait for any other process.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "![chart showing allreduce cost](/assets/images/straggler-mitigation/straggler-mitigation-1.png){:style=\"max-height:800px; width:100%\"} \n\nBoth the 1st and the 3rd Steps Are Slowed Down by Stragglers\n\n\n* The second screenshot shows a normal case without stragglers. In this case, all the gradient synchronizations are relatively short.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "![chart showing normal case without stragglers](/assets/images/straggler-mitigation/straggler-mitigation-2.png){:style=\"max-height:800px; width:100%\"} \n\nNormal Case Without Stragglers", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Hierarchical SGD in PyTorch", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Recently hierarchical SGD has been proposed to optimize the communication costs by mainly reducing the total amount of data transfer in large-scale distributed training, and multiple convergence analyses have been provided ([example](https://arxiv.org/pdf/2010.12998.pdf)). As a main novelty of this post, at Cruise we could leverage hierarchical SGD to mitigate stragglers, which may also occur on training relatively small models. Our implementation has been upstreamed by Cruise to PyTorch in early 2022.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "How Does Hierarchical SGD Work?\n\nAs the name implies, hierarchical SGD organizes all the processes into groups at different levels as a hierarchy, and runs synchronization by following the rules below:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "* All the groups at the same level have the same number of processes, and the processes in these groups synchronize at the same frequency concurrently, where the synchronization period is pre-defined by the user.\n* The higher level a group is, the larger synchronization period is used, as the synchronization becomes more expensive.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "* When multiple overlapping groups are supposed to synchronize according to their periods, to reduce redundant synchronization and avoid data race across groups, only the highest-level group runs synchronization.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The following figure illustrates an example of 4-level hierarchy SGD among 16 processes on 8 machines, each of which has 2 GPUs:\n\n1. **Level 1:** Each process runs mini-batch SGD locally;\n2. **Level 2:** Each 4-process group across 2 machines runs synchronization every 2 steps;\n3. **Level 3:** Each 8-process group across 4 machines runs synchronization every 4 steps;\n4. **Level 4:** The global process group of all 16 processes over 8 machines runs synchronization every 8 steps.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Particularly, when the step number can be divided by 8, only the synchronization at 3) is executed, and when the step number can be divided by 4 but not 8, only the synchronization at 2) is executed.\n\n\n![An example of 4-level hierarchy SGD among 16 processes on 8 machines, each of which has 2 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-3.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Intuitively, hierarchical SGD can be viewed as an extension of [local SGD](https://core.ac.uk/download/pdf/211998087.pdf), which only has a two-level hierarchy \u2013 every process runs mini-batch SGD locally and then synchronizes globally at a certain frequency. This can also help explain that, just like local SGD, hierarchical SGD synchronizes model parameters instead of gradients. Otherwise the gradient descent will be mathematically incorrect when the frequency is greater than 1.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Why Can Hierarchical SGD Mitigate Stragglers?\n\nThe key insight here is that, when there is a random straggler, it only directly slows down a relatively small group of processes instead of all the processes. Next time another random straggler is very likely to slow down a different small group, and hence a hierarchy can help smooth out the straggler effect.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The example below assumes that there is a random straggler among totally 8 processes at every step. After 4 steps, vanilla DDP that runs synchronous SGD will be slowed down by straggler 4 times, because it runs global synchronization at every step. In contrast, hierarchical SGD runs synchronization with the groups of 4 processes after the first two steps, and then a global synchronization after another two steps. We can see that both the first two and the last two stragglers have a large overlap, and hence", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "the performance loss can be mitigated.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "![flow diagram](/assets/images/straggler-mitigation/straggler-mitigation-4.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Essentially, the mitigation effect of this hierarchical SGD example actually is between local SGD at a frequency of every 2 steps and every 4 steps. The main advantage of hierarchical SGD over local SGD is a better convergence efficiency of the same global synchronization frequency, because hierarchical SGD allows more low-level synchronization. Moreover, it is possible for hierarchical SGD to provide a global synchronization frequency lower than local SGD with model parity, leading to a higher training", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "performance, especially in a large-scale distributed training.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Ease of Use", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Straggler mitigation is not a novel study in distributed training. Multiple approaches have been proposed, such as [gossip SGD](https://arxiv.org/pdf/1705.09056.pdf), [data encoding](https://proceedings.neurips.cc/paper/2017/file/663772ea088360f95bac3dc7ffb841be-Paper.pdf), [gradient coding](http://proceedings.mlr.press/v70/tandon17a/tandon17a.pdf), as well as some particularly designed for parameter-server architecture, including [backup", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "workers](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45187.pdf) and [stale synchronous parallel](http://www.cs.cmu.edu/~seunghak/SSPTable_NIPS2013.pdf). However, to the best of our knowledge, before this effort we have not found a good open-source PyTorch implementation of straggler mitigation that can work like a plugin to our training system at Cruise. In contrast, our implementation only requires the minimal changes \u2013 no need to modify the existing code or tune any", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "existing hyperparameters. This is a very appealing advantage for industry users.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "As the code example below shows, only a few lines need to be added to the setup of DDP model, and the training loop code can keep untouched. As explained previously, hierarchical SGD is an extended form of local SGD, so the enablement can be quite similar to local SGD (see PyTorch docs of [PostLocalSGDOptimizer](https://pytorch.org/docs/stable/distributed.optim.html#torch.distributed.optim.PostLocalSGDOptimizer)):", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "1. Register a post-local SGD communication hook to run a warmup stage of fully synchronous SGD and defer hierarchical SGD.\n2. Create a post-local SGD optimizer that wraps an existing local optimizer and a hierarchical SGD configuration.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "```\nimport torch.distributed.algorithms.model_averaging.hierarchical_model_averager as hierarchicalSGD\nfrom torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook import (\n PostLocalSGDState,\n post_localSGD_hook,\n)\nfrom torch.distributed.optim import PostLocalSGDOptimizer\n\nddp_model = nn.parallel.DistributedDataParallel(\n module=model,\n device_ids=[rank],\n)", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "# Register a post-local SGD communication hook for the warmup.\nsubgroup, _ = torch.distributed.new_subgroups()\nstate = PostLocalSGDState(subgroup=subgroup, start_localSGD_iter=1_000)\nddp_model.register_comm_hook(state, post_localSGD_hook)", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "# Wraps the existing (local) optimizer to run hierarchical model averaging.\noptim = PostLocalSGDOptimizer(\n optim=optim,\n averager=hierarchicalSGD.HierarchicalModelAverager(\n # The config runs a 4-level hierarchy SGD among 128 processes:\n # 1) Each process runs mini-batch SGD locally;\n # 2) Each 8-process group synchronize every 2 steps;\n # 3) Each 32-process group synchronize every 4 steps;\n # 4) All 128 processes synchronize every 8 steps.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "period_group_size_dict=OrderedDict([(2, 8), (4, 32), (8, 128)]),\n # Do not run hierarchical SGD until 1K steps for model parity.\n warmup_steps=1_000)\n)\n```", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Algorithm Hyperparameters\n\nHierarchical SGD has two major hyperparameters: _period_group_size_dict_ and _warmup_steps_.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "* **period_group_size_dict** is an ordered dictionary mapping from synchronization period to process group size, used for initializing process groups of different sizes in a hierarchy to synchronize parameters concurrently. A larger group is expected to use a larger synchronization period.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "* **warmup_steps** specifies a number of steps as the warmup stage to run synchronous SGD before hierarchical SGD. Similar to [post-local SGD](https://arxiv.org/pdf/1808.07217.pdf) algorithm, a warmup stage is usually recommended to achieve a higher accuracy. The value should be the same as _start_localSGD_iter_ arg used in _PostLocalSGDState_ when post_localSGD_hook is registered. Typically the warmup stage should at least cover the beginning of training when the loss is decreased drastically.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "A subtle difference between the PyTorch implementation and the initial design proposed by relevant papers is that, after the warmup stage, by default the processes within each host still run intra-host gradient synchronization at every step. This is because that:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "1. The intra-host communication is relatively cheap, and it can usually significantly accelerate the convergence;", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "2. The intra-host group (of size 4 or 8 for most industry users) can usually be a good choice of the smallest group of processes that synchronize most frequently in hierarchical SGD. If the synchronization period is 1, then gradient synchronization is faster than model parameter synchronization (a.k.a., model averaging), because DDP automatically overlaps gradient synchronization and the backward pass.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Such intra-host gradient synchronization can be disabled by unsetting _post_local_gradient_allreduce_ arg in _PostLocalSGDState_.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Demonstration\n\nNow we demonstrate that hierarchical SGD can accelerate distributed training by mitigating stragglers.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Experimental Setup", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "We compared the performance of hierarchical SGD against local SGD and synchronous SGD on [ResNet18](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet18.html) (model size: 45MB). Since the model is so small, the training is not bottlenecked by data transfer cost during synchronization. To avoid the noises incurred by data loading from remote storage, the input data was randomly simulated from memory. We varied the number of GPUs used by training from 64 to 256. The batch size per", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "worker is 32, and the number of iterations of training is 1,000. Since we don\u2019t evaluate convergence efficiency in this set of experiments, warmup is not enabled.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "We also emulated stragglers at a rate of 1% on 128 and 256 GPUs, and 2% on 64 GPUs, to make sure at least one stragglers at every step on average. These stragglers randomly appear on different CUDA devices. Each straggler stalls for 1 second besides the normal per-step training time (~55ms in our setup). This can be perceived as a practical scenario where 1% or 2% of input data are outliers in terms of the data pre-processing cost (I/O and/or data transformation on the fly) during training, and such cost is", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "20X+ larger than the average.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The code snippet below shows how a straggler can be emulated in the training loop. We applied it to a ResNet model, and it can be easily applied to the other models as well.\n\n```\n loss = loss_fn(y_pred, y)\n # Emulate a straggler that lags for 1 second at a rate of 1%.\n if random.randint(1, 100) == 1:\n time.sleep(1)\n loss.backward()\n optimizer.step()", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "The experiments are conducted on us-central1 GCP cluster. Each machine has 4 NVIDIA Tesla T4 GPUs with 16 GB memory per GPU, connected through a 32 Gbit/s ethernet network. Each instance also features 96 vCPUs, 360 GB RAM.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n
Architecture\n ResNet18 (45MB)\n
Workers\n 64, 128, 256\n
Backend\n NCCL\n
GPU\n Tesla T4, 16 GB memory\n
Batch size\n 32 x ## of workers\n
Straggler Duration\n 1 sec\n
Straggler Rate\n 1% on 128 and 256 GPUs, 2% on 64 GPUs\n
", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "We used multiple configurations for both local SGD and hierarchical SGD. Local SGD runs global synchronization every 2, 4, and 8 steps, respectively.\n\nWe ran hierarchical SGD with the following configurations:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "1. On 64 GPUs:\n 1. Each 8-process group, 32-process, and the global 64-process group synchronizes every 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 2-8,4-32,8-64**_\".\n 2. Each 32-process group and the global 64-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-32,8-64**_\".\n2. On 128 GPUs:\n 3. Each 8-process group, 32-process group, and the global 128-process group synchronizes every 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 2-8,4-32,8-128**_\".", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "4. Each 32-process group and the global 128-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-32,8-128**_\".\n3. On 256 GPUs:\n 5. Each 4-process group, 16-process group, 64-process group, and the global 256-process group synchronizes every 1, 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 1-4,2-16,4-64,8-256**_\".", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "6. Each 8-process group, 64-process group, and the global 256-process group synchronizes every 2, 4, and 8 steps. Denoted as \"_**HSGD 2-8,4-64,8-256**_\".\n 7. Each 16-process group and the global 256-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-16,8-256**_\".", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Experimental Results\n\nThe figures below show the speedups of different communication schemes against the baseline of synchronous SGD, with the emulated stragglers. We can make the following observations:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "1. As expected, we can see that both hierarchical SGD and local SGD can achieve a higher speedup with a lower synchronization frequency.\n2. The speedups of the hierarchical SGD schemes are **2.08X-2.45X** on 64 GPUs, **2.57X-2.68X** on 128 GPUs, and **2.63X-3.25X** on 256 GPUs, respectively. This shows that hierarchical SGD can significantly mitigate stragglers, and such mitigation can be more effective at a larger scale.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "3. The performance of local SGD with the synchronization period of 2 steps and 8 steps can be perceived as the lower bound and upper bound of the experimented hierarchical SGD schemes, respectively. This is because the hierarchical SGD schemes synchronize less frequently than every 2 steps globally, but their low-level synchronization at small groups are the extra overheads in comparison with the global synchronization every 8 steps.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Overall, hierarchical SGD can provide a finer-grained trade-off between communication cost and model quality than local SGD. Therefore, when local SGD at a relatively large synchronization period like 8 or 4 cannot give a satisfactory convergence efficiency, hierarchical SGD can have a much better chance to achieve both a good speedup and a model parity.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Since only simulated data is used in the experiments, we did not demonstrate the model parity here, which in practice can be achieved in two ways:\n1. Tuning the hyperparameters including both hierarchy and warmup steps;", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "2. For some cases, hierarchical SGD could lead to a slightly lower quality than the original model for the same number of training steps (i.e., lower convergence rate), but with a speedup like 2X+ per training step, it is still possible to achieve model parity with more steps but still less total training time.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "![Speedups on 64 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-5.png){:style=\"max-height:800px; width:100%\"} \n\n![Speedups on 128 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-6.png){:style=\"max-height:800px; width:100%\"} \n\n![Speedups on 256 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-7.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Limitations\n\nBefore applying hierarchical SGD to straggler mitigation, the user should be aware of a few limitations of this approach:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "1. This approach can only mitigate non-persistent stragglers, which occur to different workers at different times. However, for the case of persistent stragglers, which can be caused by hardware degradation or a network issue on a specific host, these stragglers will slow down the same low-level subgroup at every time, leading to nearly no straggler mitigation.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "2. This approach can only mitigate low-frequency stragglers. E.g., if 30% workers can randomly become stragglers at every step, then most low-level synchronizations will still be slowed down by stragglers. As a result, hierarchical SGD may not show an obvious performance advantage over synchronous SGD.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "3. Since hierarchical SGD applies model averaging that does not overlap with backward like gradient averaging used by vanilla DDP, its performance gain of straggler mitigation must outweigh the performance loss of no overlap between communication and backward pass. Therefore, if stragglers only slow down training by less than 10%, hierarchical SGD may not be able to bring much speedup. This limitation can be addressed by [overlapping optimizer step and backward", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "pass](https://github.com/pytorch/pytorch/blob/release/1.13/torch/distributed/algorithms/ddp_comm_hooks/optimizer_overlap_hooks.py) in the future.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "4. Since hierarchical SGD is less well-studied than local SGD, there is no guarantee that hierarchical SGD with a finer-grained synchronization granularity can converge faster than certain advanced forms of local SGD, such as [SlowMo](https://openreview.net/pdf?id=SkxJ8REYPH), which can improve convergence efficiency with slow momentum. However, to the best of our knowledge, these advanced algorithms cannot be natively supported as a PyTorch DDP plugin like hierarchical SGD yet.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nWe would like to thank Cruise teammates **Bo Tian**, **Sergei Vorobev**, **Eugene Selivonchyk, Tsugn-Hsien Lee**, **Dan Ring**, **Ian Ackerman**, **Lei Chen**, **Maegan Chew**, **Viet Anh To**, **Xiaohui Long**, **Zeyu Chen**, **Alexander Sidorov**, **Igor Tsvetkov**, **Xin Hu**, **Manav Kataria**, **Marina Rubtsova**, and **Mohamed Fawzy**, as well as Meta teammates **Shen Li, Yanli Zhao, Suraj Subramanian, Hamid Shojanzeri, Anjali Sridhar** and **Bernard Nguyen** for the support.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Easily list and initialize models with new APIs in TorchVision\"\nauthor: Vasilis Vryniotis and Laurence Rouesnel\nfeatured-img: \"/assets/images/easily-list-and-initialize-models-with-new-apis-in-torchvision-1.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "TorchVision now supports listing and initializing all available built-in models and weights by name. This new API builds upon the recently introduced [Multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/), is currently in Beta, and it addresses a long-standing [request](https://github.com/pytorch/vision/issues/1143) from the community.", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nYou can try out the new API in the [latest nightly](https://pytorch.org/get-started/locally/) release of TorchVision. We\u2019re looking to collect feedback ahead of finalizing the feature in TorchVision v0.14. We have created a dedicated [Github Issue](https://github.com/pytorch/vision/issues/6365) where you can post your comments, questions and suggestions!", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Querying and initializing available models\n\nBefore the new model registration API, developers had to query the ``__dict__`` attribute of the modules in order to list all available models or to fetch a specific model builder method by its name:\n\n```python\n# Initialize a model by its name:\nmodel = torchvision.models.__dict__[model_name]()\n\n# List available models:\navailable_models = [\n k for k, v in torchvision.models.__dict__.items()\n if callable(v) and k[0].islower() and k[0] != \"_\"\n]", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "The above approach does not always produce the expected results and is hard to discover. For example, since the [``get_weight()``](https://pytorch.org/vision/main/models.html#using-models-from-hub) method is exposed publicly under the same module, it will be included in the list despite not being a model. In general, reducing the verbosity (less imports, shorter names etc) and being able to initialize models and weights directly from their names (better support of configs, TorchHub etc) was", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "[feedback](https://github.com/pytorch/vision/issues/5088) provided previously by the community. To solve this problem, we have developed a model registration API.", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "A new approach\n\nWe\u2019ve added 4 new methods under the torchvision.models module:\n\n```python\nfrom torchvision.models import get_model, get_model_weights, get_weight, list_models", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "The styles and naming conventions align closely with a prototype mechanism proposed by Philip Meier for the [Datasets V2](https://github.com/pytorch/vision/blob/main/torchvision/prototype/datasets/_api.py) API, aiming to offer a similar user experience. The model registration methods are kept private on purpose as we currently focus only on supporting the built-in models of TorchVision.", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "List models\n\nListing all available models in TorchVision can be done with a single function call:\n\n```python\n>>> list_models()\n['alexnet', 'mobilenet_v3_large', 'mobilenet_v3_small', 'quantized_mobilenet_v3_large', ...]\n```\n\nTo list the available models of specific submodules:\n\n```python\n>>> list_models(module=torchvision.models)\n['alexnet', 'mobilenet_v3_large', 'mobilenet_v3_small', ...]\n>>> list_models(module=torchvision.models.quantization)\n['quantized_mobilenet_v3_large', ...]\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Initialize models\n\nNow that you know which models are available, you can easily initialize a model with pre-trained weights:\n\n```python\n>>> get_model(\"quantized_mobilenet_v3_large\", weights=\"DEFAULT\")\nQuantizableMobileNetV3(\n (features): Sequential(\n ....\n )\n)\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Get weights\nSometimes, while working with config files or using TorchHub, you might have the name of a specific weight entry and wish to get its instance. This can be easily done with the following method:\n\n```python\n>>> get_weight(\"ResNet50_Weights.IMAGENET1K_V2\")\nResNet50_Weights.IMAGENET1K_V2\n```\n\nTo get the enum class with all available weights of a specific model you can use either its name:\n\n```python\n>>> get_model_weights(\"quantized_mobilenet_v3_large\")\n", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Or its model builder method:\n\n```python\n>>> get_model_weights(torchvision.models.quantization.mobilenet_v3_large)\n\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "TorchHub support\nThe new methods are also available via TorchHub:\n\n```python\nimport torch\n\n# Fetching a specific weight entry by its name:\nweights = torch.hub.load(\"pytorch/vision\", \"get_weight\", weights=\"ResNet50_Weights.IMAGENET1K_V2\")\n\n# Fetching the weights enum class to list all available entries:\nweight_enum = torch.hub.load(\"pytorch/vision\", \"get_model_weights\", name=\"resnet50\")\nprint([weight for weight in weight_enum])\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "Putting it all together\n\nFor example, if you wanted to retrieve all the small-sized models with pre-trained weights and initialize one of them, it\u2019s a matter of using the above APIs:\n\n```python\nimport torchvision\nfrom torchvision.models import get_model, get_model_weights, list_models\n\n\nmax_params = 5000000", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "tiny_models = []\nfor model_name in list_models(module=torchvision.models):\n weights_enum = get_model_weights(model_name)\n if len([w for w in weights_enum if w.meta[\"num_params\"] <= max_params]) > 0:\n tiny_models.append(model_name)\n\nprint(tiny_models)\n# ['mnasnet0_5', 'mnasnet0_75', 'mnasnet1_0', 'mobilenet_v2', ...]\n\nmodel = get_model(tiny_models[0], weights=\"DEFAULT\")\nprint(sum(x.numel() for x in model.state_dict().values()))\n# 2239188", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "For more technical details please see the original [RFC](https://github.com/pytorch/vision/pull/6330). Please spare a few minutes to provide your feedback on the new API, as this is crucial for graduating it from beta and including it in the next release. You can do this on the dedicated [Github Issue](https://github.com/pytorch/vision/issues/6365). We are looking forward to reading your comments!", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing native PyTorch automatic mixed precision for faster training on NVIDIA GPUs'\nauthor: Mengdi Huang, Chetan Tekur, Michael Carilli\n---", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "Most deep learning frameworks, including PyTorch, train with 32-bit floating point (FP32) arithmetic by default. However this is not essential to achieve full accuracy for many deep learning models. In 2017, NVIDIA researchers developed a methodology for [mixed-precision training](https://developer.nvidia.com/blog/mixed-precision-training-deep-neural-networks/), which combined", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "[single-precision](https://blogs.nvidia.com/blog/2019/11/15/whats-the-difference-between-single-double-multi-and-mixed-precision-computing/) (FP32) with half-precision (e.g. FP16) format when training a network, and achieved the same accuracy as FP32 training using the same hyperparameters, with additional performance benefits on NVIDIA GPUs:", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "* Shorter training time;\n* Lower memory requirements, enabling larger batch sizes, larger models, or larger inputs.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "In order to streamline the user experience of training in mixed precision for researchers and practitioners, NVIDIA developed [Apex](https://developer.nvidia.com/blog/apex-pytorch-easy-mixed-precision-training/) in 2018, which is a lightweight PyTorch extension with [Automatic Mixed Precision](https://developer.nvidia.com/automatic-mixed-precision) (AMP) feature. This feature enables automatic conversion of certain GPU operations from FP32 precision to mixed precision, thus improving performance while", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "maintaining accuracy.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "For the PyTorch 1.6 release, developers at NVIDIA and Facebook moved mixed precision functionality into PyTorch core as the AMP package, [torch.cuda.amp](https://pytorch.org/docs/stable/amp.html). `torch.cuda.amp` is more flexible and intuitive compared to `apex.amp`. Some of `apex.amp`'s known pain points that `torch.cuda.amp` has been able to fix:", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "* Guaranteed PyTorch version compatibility, because it's part of PyTorch\n* No need to build extensions\n* Windows support\n* Bitwise accurate [saving/restoring](https://pytorch.org/docs/master/amp.html#torch.cuda.amp.GradScaler.load_state_dict) of checkpoints", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "* [DataParallel](https://pytorch.org/docs/master/notes/amp_examples.html#dataparallel-in-a-single-process) and intra-process model parallelism (although we still recommend [torch.nn.DistributedDataParallel](https://pytorch.org/docs/master/notes/amp_examples.html#distributeddataparallel-one-gpu-per-process) with one GPU per process as the most performant approach)\n* [Gradient penalty](https://pytorch.org/docs/master/notes/amp_examples.html#gradient-penalty) (double backward)", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "* torch.cuda.amp.autocast() has no effect outside regions where it's enabled, so it should serve cases that formerly struggled with multiple calls to [apex.amp.initialize()](https://github.com/NVIDIA/apex/issues/439) (including [cross-validation)](https://github.com/NVIDIA/apex/issues/392#issuecomment-610038073) without difficulty. Multiple convergence runs in the same script should each use a fresh [GradScaler instance](https://github.com/NVIDIA/apex/issues/439#issuecomment-610028282), but GradScalers are", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "lightweight and self-contained so that's not a problem.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "* Sparse gradient support", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "With AMP being added to PyTorch core, we have started the process of deprecating `apex.amp.` We have moved `apex.amp` to maintenance mode and will support customers using `apex.amp.` However, we highly encourage `apex.amp` customers to transition to using `torch.cuda.amp` from PyTorch Core.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "# Example Walkthrough\nPlease see official docs for usage:\n* [https://pytorch.org/docs/stable/amp.html](https://pytorch.org/docs/stable/amp.html )\n* [https://pytorch.org/docs/stable/notes/amp_examples.html](https://pytorch.org/docs/stable/notes/amp_examples.html)\n\nExample:\n\n```python\nimport torch\n# Creates once at the beginning of training\nscaler = torch.cuda.amp.GradScaler()", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "for data, label in data_iter:\n optimizer.zero_grad()\n # Casts operations to mixed precision\n with torch.cuda.amp.autocast():\n loss = model(data)\n\n # Scales the loss, and calls backward()\n # to create scaled gradients\n scaler.scale(loss).backward()\n\n # Unscales gradients and calls\n # or skips optimizer.step()\n scaler.step(optimizer)\n\n # Updates the scale for next iteration\n scaler.update()", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "# Performance Benchmarks\nIn this section, we discuss the accuracy and performance of mixed precision training with AMP on the latest NVIDIA GPU A100 and also previous generation V100 GPU. The mixed precision performance is compared to FP32 performance, when running Deep Learning workloads in the [NVIDIA pytorch:20.06-py3 container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch?ncid=partn-52193#cid=ngc01_partn_en-us) from NGC.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "Accuracy: AMP (FP16), FP32", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "The advantage of using AMP for Deep Learning training is that the models converge to the similar final accuracy while providing improved training performance. To illustrate this point, for [Resnet 50 v1.5 training](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnet50v1.5#training-accuracy-nvidia-dgx-a100-8x-a100-40gb), we see the following accuracy results where higher is better. Please note that the below accuracy numbers are sample numbers that are subject to", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "run to run variance of up to 0.4%. Accuracy numbers for other models including BERT, Transformer, ResNeXt-101, Mask-RCNN, DLRM can be found at [NVIDIA Deep Learning Examples Github](https://github.com/NVIDIA/DeepLearningExamples).", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "Training accuracy: NVIDIA DGX A100 (8x A100 40GB)\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n
 epochs Mixed Precision Top 1(%) TF32 Top1(%)
 90 76.93 76.85
\n\nTraining accuracy: NVIDIA DGX-1 (8x V100 16GB)", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n \n \n \n \n \n
 epochs Mixed Precision Top 1(%) FP32 Top1(%)
5076.2576.26
9077.0977.01
25078.4278.30
", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "Speedup Performance:", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "FP16 on NVIDIA V100 vs. FP32 on V100\nAMP with FP16 is the most performant option for DL training on the V100. In Table 1, we can observe that for various models, AMP on V100 provides a speedup of 1.5x to 5.5x over FP32 on V100 while converging to the same final accuracy.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n*Figure 2. Performance of mixed precision training on NVIDIA 8xV100 vs. FP32 training on 8xV100 GPU. Bars represent the speedup factor of V100 AMP over V100 FP32. The higher the better.*", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "FP16 on NVIDIA A100 vs. FP16 on V100\n\nAMP with FP16 remains the most performant option for DL training on the A100. In Figure 3, we can observe that for various models, AMP on A100 provides a speedup of 1.3x to 2.5x over AMP on V100 while converging to the same final accuracy.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "1. A generic kernel which implements the mathematical equation of SDPA in the function `sdpa_math()`\n2. An optimized kernel based on the paper \u201c[Flash Attention](https://arxiv.org/abs/2205.14135)\u201d, which supports evaluation of SDPA with 16 bit floating point data types on compute architecture SM80 (A100).\n3. An optimized kernel based on the paper \u201c[Self-Attention Does Not Need O(n^2) Memory](https://arxiv.org/abs/2112.0568)\" and implemented in [xFormer](https://github.com/facebookresearch/xformers), which supports both 32 and 16 bit floating data types on a wider range of architectures (SM40 and later). This blog post refers to this kernel as the `mem_efficient` kernel.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "Note that both optimized kernels (two and three listed above), support a key padding mask and limit the supported attention mask to causal attention. Accelerated PyTorch 2.0 Transformers today only support the causal mask when it is specified using the `is_causal` boolean. When a mask is specified, the general-purpose kernel will be selected because it is too expensive to analyze the contents of a provided mask to determine if it is the causal mask. Additional explanations on the constraints for each kernel can be found in the [Accelerated PT2 Transformer blog](https://pytorch.org/blog/accelerated-pytorch-2/).\n\n\n## Enabling Accelerated Transformers with nanoGPT\n\nThe SDPA operator being a critical component of the GPT model, we identified the open source nanoGPT model as an excellent candidate for both demonstrating the ease of implementation and benefits of PyTorch 2.0\u2019s Accelerated Transformers. The following demonstrates the exact process by which Accelerated Transformers was enabled on nanoGPT.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "This process largely revolves around replacing the existing SDPA implementation with the newly added F.scaled_dot_product_attention operator from [functional.py](https://github.com/pytorch/pytorch/blob/df14650f0b14b80db132b0c1797dc595fbee1054/torch/nn/functional.py#L4834). This process can be easily adapted to enable the operator in many other LLMs. Alternatively, users can instead choose to call F.multi_head_attention_forward() or utilize the nn.MultiHeadAttention module directly where applicable. The following code snippets are adapted from Karpathy\u2019s nanoGPT repository.\n\n\n### Step 1: Identify the existing SDPA implementation\n\nIn the case of nanoGPT, SDPA is implemented in the model\u2019s [CausalSelfAttention](https://github.com/karpathy/nanoGPT/blob/master/model.py#L37) class. The original implementation at time of writing is adapted below for this post.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "![The original implementation at time of writing](/assets/images/2023-04-18-accelerating-large-language-models/causal_attention_step_1.png){:style=\"max-height:800px; width:100%\"} \n\n\n### Step 2: Replace with Torch\u2019s _scaled_dot_product_attention_\n\nAt this point we can note the following:\n\n* Lines 36 - 42 define the mathematical implementation of SDPA which we are replacing\n* The mask applied on line 39 is no longer relevant since we are using scaled_dot_product_attention\u2019s `is_causal` flag.\n* The dropout layer used in line 41 is also now unnecessary. \n\nSwapping out the SDPA implementation for torch\u2019s scaled_dot_product_attention and removing the now redundant code yields the following implementation.\n\n![Swapping out the SDPA implementation for torch\u2019s scaled_dot_product_attention and removing the now redundant code yields the following implementation.](/assets/images/2023-04-18-accelerating-large-language-models/causal_attention_step_2.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "Alternatively, the original mask can be passed into the `attn_mask` field however due to the mentioned kernel constraints that would limit the implementation to only support the generic `sdpa_math` kernel.\n\n\n### Step 3 (Bonus): Faster matmuls with padding\n\nOn top of the performance improvements from SDPA, our analysis yielded a nice ancillary win. In Andrej's words \"The most dramatic optimization to nanoGPT so far (~25% speedup) is to simply increase the vocab size from 50257 to 50304 (nearest multiple of 64).\"\n\n\n![Tweet by Andrej Karpathy](/assets/images/2023-04-18-accelerating-large-language-models/tweet.png){:style=\"max-height:800px; width:100%; max-width:600px\"}", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "The vocab size determines the dimensions of matmuls in the output layer of GPT, and these are so large that they were taking a _majority_ of the time for the entire training loop! We discovered that they were achieving performance significantly below the peak throughput achievable on the A100 GPU, and guessed from [NVIDIA's matmul documentation](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html) that 64-element alignment would yield better results. Indeed, padding these matmuls achieves nearly a 3x speedup! The underlying cause is that unaligned memory accesses significantly reduce efficiency. A deeper analysis can be found in [this Twitter thread](https://twitter.com/cHHillee/status/1630274804795445248).\n\nWith this optimization we were able to further reduce training time from ~113 ms (using flash attention) to ~87 ms per batch.\n\n\n## Results\n\nThe figure below demonstrates the performance gained using Pytorch custom kernels. Here are the exact figures:", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "* baseline (nanoGPT implementation): ~143ms\n* sdpa_math (generic): ~134ms (6.71% faster)\n* `mem_efficient` kernel: ~119ms (20.16% faster)\n* `flash_attention` kernel: ~113ms (26.54% faster)\n* flash_attention + padded vocab: ~87ms (64.37% faster)\n\nAll code was run on an 8 x NVIDIA Corporation A100 server with 80 GB HBM [A100 SXM4 80GB], and for the purpose of this experiment dropout was set to 0.\n\n\n![Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models](/assets/images/2023-04-18-accelerating-large-language-models/PyTorch_Better-Transformer_Chart-2.png){:style=\"max-height:800px; width:100%\"} \n\n\n**Figure 2:** Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.\n\n\n## Enhancing Numerical Model Stability", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "In addition to being faster, PyTorch's implementation offers increased numerical stability by avoiding loss of precision in many execution scenarios. There is a great explanation [here](https://github.com/bigscience-workshop/Megatron-DeepSpeed/pull/118), but essentially the PyTorch implementation scales the Query and Key matrices _before_ multiplication, which is said to be more stable and avoid loss of precision. Because of the merged custom kernel architecture of SDPA, this scaling does not introduce additional overhead in the computation of the attention result. In comparison, an implementation from the individual computational components would require separate pre-scaling at additional cost. For an additional explanation, see Appendix A.\n\n\n### Improved Memory Consumption", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "Yet another large advantage of using the torch SDPA kernels is the reduced memory footprint, which allows for the utilization of larger batch sizes. The following chart compares the best validation loss after one hour of training for both flash attention and the baseline implementations of causal attention. As can be seen, the maximum batch size achieved with the baseline causal attention implementation (on 8 x NVIDIA Corporation A100 server with 80 GB HBM) was 24, significantly less then the maximum achieved with flash attention, which was 39.\n\n![Using Flash Attention enables the usage of larger batch sizes](/assets/images/2023-04-18-accelerating-large-language-models/chart.png){:style=\"max-height:800px; width:100%\"} \n\n\n**Figure 3:** Using Flash Attention enables the usage of larger batch sizes, allowing users to achieve lower validation loss after one hour of training (smaller is better).\n\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nAccelerated PyTorch 2 Transformers were designed to make the training and production deployment of state-of-the-art transformer models affordable and integrated with PyTorch 2.0 model JIT compilation. The newly introduced PyTorch SDPA operator provides improved performance for training Transformer models and is particularly valuable for the expensive Large Language Model training. In this post we demonstrate a number of optimizations on the exemplary nanoGPT model including:\n\n\n\n* Over 26% training speedup, when compared against the baseline with constant batch size\n* An additional speedup achieved with padded vocabulary, bringing the total optimization to approximately 64% compared to the baseline\n* Additional numerical stability\n\n\n## Appendix A: Analyzing Attention Numeric Stability", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "In this section we provide a more in depth explanation of the previously mentioned enhanced numerical stability which is gained by prescaling SDPA\u2019s input vectors. The following is a simplified version of nanoGPT\u2019s mathematical implementation of SDPA. The important thing to note here is that the query undergoes matrix multiplication without being scaled.\n\n```\n# nanoGPT implementation of SDPA\n# notice q (our query vector) is not scaled !\natt = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))\natt = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))\natt = F.softmax(att, dim=-1)\n\n# Dropout is set to 0, so we can safely ignore this line in the implementation# att = self.attn_dropout(att) \n\ny_nanogpt = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)\n```\n\nThe following is the equivalent mathematical implementation in torch\u2019s `scaled_dot_product_attention`.", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "```\n# PyTorch implementation of SDPA\nembed_size = q.size(-1)\nscaling_factor = math.sqrt(math.sqrt(embed_size))\nq = q / scaling_factor \t# notice q _is_ scaled here !\n\n# same as above, but with scaling factor\natt = q @ (k.transpose(-2, -1) / scaling_factor)\natt = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))\natt = F.softmax(att0, dim=-1)\n\n# Dropout is set to 0, so we can safely ignore this line in the implementation# att = self.attn_dropout(att) \n\ny_scale_before = att @ v\n```\n\nMathematically both approaches should be equivalent, however our experimentation shows that in practice we receive different results from each approach. \n\nUsing the approach above, we verified `y_scale_before` matches the expected output from using the `scaled_dot_product_attention `method while `y_nanogpt` does not.\n\nThe `torch.allclose` method was used to test equivalence. Specifically, we showed that:", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "```\ny_sdpa = torch.nn.functional._scaled_dot_product_attention(\n\tq,\n\tk,\n\tv,\n\tattn_mask=self.bias[:,:,:T,:T] != 0,\n\tdropout_p=0.0,\n\tneed_attn_weights=False,\n\tis_causal=False,\n)\n\ntorch.allclose(y_sdpa, y_nanogpt) # False, indicating fp issues\ntorch.allclose(y_sdpa, y_scale_before) # True, as expected\n```\n\n## Appendix B: Reproducing Experiment Results", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "Researchers seeking to reproduce these results should start with the following commit from Andrej\u2019s nanoGPT repository - **b3c17c6c6a363357623f223aaa4a8b1e89d0a465**. This commit was used as the baseline when measuring the per batch speed improvements. For results which include padded vocabulary optimizations (which yielded the most significant improvements to batch speed), use the following commit - **77e7e04c2657846ddf30c1ca2dd9f7cbb93ddeab**. From either checkout, selecting kernels for experimentation is made trivial with the use of the [torch.backends](https://pytorch.org/docs/stable/backends.html) API. \n\nThe desired kernel can be selected via a context manager:\n\n```\nwith torch.backends.cuda.sdp_kernel (\n enable_math = False,\n enable_flash = False,\n enable_mem_efficient = True\n):\n train(model)\n```", "metadata": {"source": "https://pytorch.org/blog/accelerating-large-language-models/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Straggler Mitigation On PyTorch DDP By Hierarchical SGD\"\nauthor: Yi Wang (Cruise AI), Rohan Varma (Meta AI)\n---\n\n[PyTorch DDP](https://pytorch.org/docs/stable/notes/ddp.html) has been widely adopted across the industry for distributed training, which by default runs synchronous SGD to synchronize gradients across model replicas at every step. The performance of this technique is critical for fast iteration during model exploration as well as resource and cost saving. The performance is critical for fast iteration and cost saving of model development and exploration. To resolve a ubiquitous performance bottleneck introduced by slow nodes in large-scale training, Cruise and Meta co-developed a solution based on the [Hierarchical SGD](https://arxiv.org/abs/2007.13819) algorithm to significantly accelerate training in the presence of these stragglers.\n\n\n## The Need For Straggler Mitigation", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "In DDP setup, a straggler problem can occur when one or more processes run much slower (\"stragglers\") than other processes. When this happens, all the processes have to wait for the stragglers before synchronizing gradients and completing the communication, which essentially bottlenecks distributed performance to the slowest worker.As a result, even for the cases of training relatively small models, the communication cost can still be a major performance bottleneck.\n\n\n### Potential Causes of Stragglers\n\nSevere straggler issues are usually caused by workload imbalance before synchronization, and many factors can contribute to this imbalance. For instance, some data loader workers in the distributed environment can become stragglers, because some input examples can be outliers in terms of the data size, or the data transfer of some examples can be drastically slowed down due to unstable network I/O, or the on-the-fly data transformation costs can have a high variance.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Besides data loading, other phases before gradient synchronization can also cause stragglers, such as unbalanced workloads of embedding table lookup during the forward pass in recommendation systems.\n\n\n### The Appearance of Stragglers\n\nIf we profile DDP training jobs that have stragglers, we can find that some processes may have much higher gradient synchronization costs (a.k.a., allreducing gradients) than other processes at a certain step. As a result, the distributed performance can be dominated by the communication cost even if the model size is very small. In this case, some processes run faster than the straggler(s) at a step, and hence they have to wait for the stragglers and spend a much longer time on allreduce.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "The below shows screenshots of two trace files output by PyTorch profiler in a use case. Each screenshot profiles 3 steps.\n* The first screenshot shows that a process has a very high allreduce cost in both the first and the third steps, because this process reaches the synchronization phase earlier than the straggler(s), and it spends more time on waiting. On the other hand, the allreduce cost is relatively small in the second step, this suggests that 1) there is no straggler at this step; or 2) this process is the straggler among all the processes, so it does not need to wait for any other process.\n\n\n![chart showing allreduce cost](/assets/images/straggler-mitigation/straggler-mitigation-1.png){:style=\"max-height:800px; width:100%\"} \n\nBoth the 1st and the 3rd Steps Are Slowed Down by Stragglers\n\n\n* The second screenshot shows a normal case without stragglers. In this case, all the gradient synchronizations are relatively short.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "![chart showing normal case without stragglers](/assets/images/straggler-mitigation/straggler-mitigation-2.png){:style=\"max-height:800px; width:100%\"} \n\nNormal Case Without Stragglers\n\n\n## Hierarchical SGD in PyTorch\n\nRecently hierarchical SGD has been proposed to optimize the communication costs by mainly reducing the total amount of data transfer in large-scale distributed training, and multiple convergence analyses have been provided ([example](https://arxiv.org/pdf/2010.12998.pdf)). As a main novelty of this post, at Cruise we could leverage hierarchical SGD to mitigate stragglers, which may also occur on training relatively small models. Our implementation has been upstreamed by Cruise to PyTorch in early 2022.\n\n\n### How Does Hierarchical SGD Work?\n\nAs the name implies, hierarchical SGD organizes all the processes into groups at different levels as a hierarchy, and runs synchronization by following the rules below:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "* All the groups at the same level have the same number of processes, and the processes in these groups synchronize at the same frequency concurrently, where the synchronization period is pre-defined by the user.\n* The higher level a group is, the larger synchronization period is used, as the synchronization becomes more expensive.\n* When multiple overlapping groups are supposed to synchronize according to their periods, to reduce redundant synchronization and avoid data race across groups, only the highest-level group runs synchronization.\n\nThe following figure illustrates an example of 4-level hierarchy SGD among 16 processes on 8 machines, each of which has 2 GPUs:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "1. **Level 1:** Each process runs mini-batch SGD locally;\n2. **Level 2:** Each 4-process group across 2 machines runs synchronization every 2 steps;\n3. **Level 3:** Each 8-process group across 4 machines runs synchronization every 4 steps;\n4. **Level 4:** The global process group of all 16 processes over 8 machines runs synchronization every 8 steps.\n\nParticularly, when the step number can be divided by 8, only the synchronization at 3) is executed, and when the step number can be divided by 4 but not 8, only the synchronization at 2) is executed.\n\n\n![An example of 4-level hierarchy SGD among 16 processes on 8 machines, each of which has 2 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-3.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Intuitively, hierarchical SGD can be viewed as an extension of [local SGD](https://core.ac.uk/download/pdf/211998087.pdf), which only has a two-level hierarchy \u2013 every process runs mini-batch SGD locally and then synchronizes globally at a certain frequency. This can also help explain that, just like local SGD, hierarchical SGD synchronizes model parameters instead of gradients. Otherwise the gradient descent will be mathematically incorrect when the frequency is greater than 1.\n\n\n### Why Can Hierarchical SGD Mitigate Stragglers?\n\nThe key insight here is that, when there is a random straggler, it only directly slows down a relatively small group of processes instead of all the processes. Next time another random straggler is very likely to slow down a different small group, and hence a hierarchy can help smooth out the straggler effect.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "The example below assumes that there is a random straggler among totally 8 processes at every step. After 4 steps, vanilla DDP that runs synchronous SGD will be slowed down by straggler 4 times, because it runs global synchronization at every step. In contrast, hierarchical SGD runs synchronization with the groups of 4 processes after the first two steps, and then a global synchronization after another two steps. We can see that both the first two and the last two stragglers have a large overlap, and hence the performance loss can be mitigated.\n\n\n![flow diagram](/assets/images/straggler-mitigation/straggler-mitigation-4.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Essentially, the mitigation effect of this hierarchical SGD example actually is between local SGD at a frequency of every 2 steps and every 4 steps. The main advantage of hierarchical SGD over local SGD is a better convergence efficiency of the same global synchronization frequency, because hierarchical SGD allows more low-level synchronization. Moreover, it is possible for hierarchical SGD to provide a global synchronization frequency lower than local SGD with model parity, leading to a higher training performance, especially in a large-scale distributed training.\n\n\n### Ease of Use", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Straggler mitigation is not a novel study in distributed training. Multiple approaches have been proposed, such as [gossip SGD](https://arxiv.org/pdf/1705.09056.pdf), [data encoding](https://proceedings.neurips.cc/paper/2017/file/663772ea088360f95bac3dc7ffb841be-Paper.pdf), [gradient coding](http://proceedings.mlr.press/v70/tandon17a/tandon17a.pdf), as well as some particularly designed for parameter-server architecture, including [backup workers](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45187.pdf) and [stale synchronous parallel](http://www.cs.cmu.edu/~seunghak/SSPTable_NIPS2013.pdf). However, to the best of our knowledge, before this effort we have not found a good open-source PyTorch implementation of straggler mitigation that can work like a plugin to our training system at Cruise. In contrast, our implementation only requires the minimal changes \u2013 no need to modify the existing code or tune any existing hyperparameters. This is a very appealing advantage for industry users.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "As the code example below shows, only a few lines need to be added to the setup of DDP model, and the training loop code can keep untouched. As explained previously, hierarchical SGD is an extended form of local SGD, so the enablement can be quite similar to local SGD (see PyTorch docs of [PostLocalSGDOptimizer](https://pytorch.org/docs/stable/distributed.optim.html#torch.distributed.optim.PostLocalSGDOptimizer)):\n\n1. Register a post-local SGD communication hook to run a warmup stage of fully synchronous SGD and defer hierarchical SGD.\n2. Create a post-local SGD optimizer that wraps an existing local optimizer and a hierarchical SGD configuration.\n\n```\nimport torch.distributed.algorithms.model_averaging.hierarchical_model_averager as hierarchicalSGD\nfrom torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook import (\n PostLocalSGDState,\n post_localSGD_hook,\n)\nfrom torch.distributed.optim import PostLocalSGDOptimizer", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "ddp_model = nn.parallel.DistributedDataParallel(\n module=model,\n device_ids=[rank],\n)\n\n# Register a post-local SGD communication hook for the warmup.\nsubgroup, _ = torch.distributed.new_subgroups()\nstate = PostLocalSGDState(subgroup=subgroup, start_localSGD_iter=1_000)\nddp_model.register_comm_hook(state, post_localSGD_hook)\n\n# Wraps the existing (local) optimizer to run hierarchical model averaging.\noptim = PostLocalSGDOptimizer(\n optim=optim,\n averager=hierarchicalSGD.HierarchicalModelAverager(\n # The config runs a 4-level hierarchy SGD among 128 processes:\n # 1) Each process runs mini-batch SGD locally;\n # 2) Each 8-process group synchronize every 2 steps;\n # 3) Each 32-process group synchronize every 4 steps;\n # 4) All 128 processes synchronize every 8 steps.\n period_group_size_dict=OrderedDict([(2, 8), (4, 32), (8, 128)]),\n # Do not run hierarchical SGD until 1K steps for model parity.\n warmup_steps=1_000)\n)\n```\n\n### Algorithm Hyperparameters", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Hierarchical SGD has two major hyperparameters: _period_group_size_dict_ and _warmup_steps_.\n\n* **period_group_size_dict** is an ordered dictionary mapping from synchronization period to process group size, used for initializing process groups of different sizes in a hierarchy to synchronize parameters concurrently. A larger group is expected to use a larger synchronization period.\n* **warmup_steps** specifies a number of steps as the warmup stage to run synchronous SGD before hierarchical SGD. Similar to [post-local SGD](https://arxiv.org/pdf/1808.07217.pdf) algorithm, a warmup stage is usually recommended to achieve a higher accuracy. The value should be the same as _start_localSGD_iter_ arg used in _PostLocalSGDState_ when post_localSGD_hook is registered. Typically the warmup stage should at least cover the beginning of training when the loss is decreased drastically.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "A subtle difference between the PyTorch implementation and the initial design proposed by relevant papers is that, after the warmup stage, by default the processes within each host still run intra-host gradient synchronization at every step. This is because that:\n\n1. The intra-host communication is relatively cheap, and it can usually significantly accelerate the convergence;\n2. The intra-host group (of size 4 or 8 for most industry users) can usually be a good choice of the smallest group of processes that synchronize most frequently in hierarchical SGD. If the synchronization period is 1, then gradient synchronization is faster than model parameter synchronization (a.k.a., model averaging), because DDP automatically overlaps gradient synchronization and the backward pass.\n\nSuch intra-host gradient synchronization can be disabled by unsetting _post_local_gradient_allreduce_ arg in _PostLocalSGDState_.\n\n\n## Demonstration", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "## Demonstration\n\nNow we demonstrate that hierarchical SGD can accelerate distributed training by mitigating stragglers.\n\n\n### Experimental Setup\n\nWe compared the performance of hierarchical SGD against local SGD and synchronous SGD on [ResNet18](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet18.html) (model size: 45MB). Since the model is so small, the training is not bottlenecked by data transfer cost during synchronization. To avoid the noises incurred by data loading from remote storage, the input data was randomly simulated from memory. We varied the number of GPUs used by training from 64 to 256. The batch size per worker is 32, and the number of iterations of training is 1,000. Since we don\u2019t evaluate convergence efficiency in this set of experiments, warmup is not enabled.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "We also emulated stragglers at a rate of 1% on 128 and 256 GPUs, and 2% on 64 GPUs, to make sure at least one stragglers at every step on average. These stragglers randomly appear on different CUDA devices. Each straggler stalls for 1 second besides the normal per-step training time (~55ms in our setup). This can be perceived as a practical scenario where 1% or 2% of input data are outliers in terms of the data pre-processing cost (I/O and/or data transformation on the fly) during training, and such cost is 20X+ larger than the average.\n\nThe code snippet below shows how a straggler can be emulated in the training loop. We applied it to a ResNet model, and it can be easily applied to the other models as well.\n\n```\n loss = loss_fn(y_pred, y)\n # Emulate a straggler that lags for 1 second at a rate of 1%.\n if random.randint(1, 100) == 1:\n time.sleep(1)\n loss.backward()\n optimizer.step()\n```", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "The experiments are conducted on us-central1 GCP cluster. Each machine has 4 NVIDIA Tesla T4 GPUs with 16 GB memory per GPU, connected through a 32 Gbit/s ethernet network. Each instance also features 96 vCPUs, 360 GB RAM.\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Architecture\n ResNet18 (45MB)\n
Workers\n 64, 128, 256\n
Backend\n NCCL\n
GPU\n Tesla T4, 16 GB memory\n
Batch size\n 32 x ## of workers\n
Straggler Duration\n 1 sec\n
Straggler Rate\n 1% on 128 and 256 GPUs, 2% on 64 GPUs\n
\n\n\nWe used multiple configurations for both local SGD and hierarchical SGD. Local SGD runs global synchronization every 2, 4, and 8 steps, respectively.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "We ran hierarchical SGD with the following configurations:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "1. On 64 GPUs:\n 1. Each 8-process group, 32-process, and the global 64-process group synchronizes every 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 2-8,4-32,8-64**_\".\n 2. Each 32-process group and the global 64-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-32,8-64**_\".\n2. On 128 GPUs:\n 3. Each 8-process group, 32-process group, and the global 128-process group synchronizes every 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 2-8,4-32,8-128**_\".\n 4. Each 32-process group and the global 128-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-32,8-128**_\".\n3. On 256 GPUs:\n 5. Each 4-process group, 16-process group, 64-process group, and the global 256-process group synchronizes every 1, 2, 4, and 8 steps, respectively. Denoted as \"_**HSGD 1-4,2-16,4-64,8-256**_\".\n 6. Each 8-process group, 64-process group, and the global 256-process group synchronizes every 2, 4, and 8 steps. Denoted as \"_**HSGD 2-8,4-64,8-256**_\".\n 7. Each 16-process group and the global 256-process group synchronizes every 4 and 8 steps, respectively. Denoted as \"_**HSGD 4-16,8-256**_\".", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "### Experimental Results\n\nThe figures below show the speedups of different communication schemes against the baseline of synchronous SGD, with the emulated stragglers. We can make the following observations:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "1. As expected, we can see that both hierarchical SGD and local SGD can achieve a higher speedup with a lower synchronization frequency.\n2. The speedups of the hierarchical SGD schemes are **2.08X-2.45X** on 64 GPUs, **2.57X-2.68X** on 128 GPUs, and **2.63X-3.25X** on 256 GPUs, respectively. This shows that hierarchical SGD can significantly mitigate stragglers, and such mitigation can be more effective at a larger scale.\n3. The performance of local SGD with the synchronization period of 2 steps and 8 steps can be perceived as the lower bound and upper bound of the experimented hierarchical SGD schemes, respectively. This is because the hierarchical SGD schemes synchronize less frequently than every 2 steps globally, but their low-level synchronization at small groups are the extra overheads in comparison with the global synchronization every 8 steps.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "Overall, hierarchical SGD can provide a finer-grained trade-off between communication cost and model quality than local SGD. Therefore, when local SGD at a relatively large synchronization period like 8 or 4 cannot give a satisfactory convergence efficiency, hierarchical SGD can have a much better chance to achieve both a good speedup and a model parity.\n\nSince only simulated data is used in the experiments, we did not demonstrate the model parity here, which in practice can be achieved in two ways:\n1. Tuning the hyperparameters including both hierarchy and warmup steps;\n2. For some cases, hierarchical SGD could lead to a slightly lower quality than the original model for the same number of training steps (i.e., lower convergence rate), but with a speedup like 2X+ per training step, it is still possible to achieve model parity with more steps but still less total training time.\n\n\n![Speedups on 64 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-5.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "![Speedups on 128 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-6.png){:style=\"max-height:800px; width:100%\"} \n\n![Speedups on 256 GPUs](/assets/images/straggler-mitigation/straggler-mitigation-7.png){:style=\"max-height:800px; width:100%\"} \n\n\n## Limitations\n\nBefore applying hierarchical SGD to straggler mitigation, the user should be aware of a few limitations of this approach:", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "1. This approach can only mitigate non-persistent stragglers, which occur to different workers at different times. However, for the case of persistent stragglers, which can be caused by hardware degradation or a network issue on a specific host, these stragglers will slow down the same low-level subgroup at every time, leading to nearly no straggler mitigation.\n2. This approach can only mitigate low-frequency stragglers. E.g., if 30% workers can randomly become stragglers at every step, then most low-level synchronizations will still be slowed down by stragglers. As a result, hierarchical SGD may not show an obvious performance advantage over synchronous SGD.\n3. Since hierarchical SGD applies model averaging that does not overlap with backward like gradient averaging used by vanilla DDP, its performance gain of straggler mitigation must outweigh the performance loss of no overlap between communication and backward pass. Therefore, if stragglers only slow down training by less than 10%, hierarchical SGD may not be able to bring much speedup. This limitation can be addressed by [overlapping optimizer step and backward pass](https://github.com/pytorch/pytorch/blob/release/1.13/torch/distributed/algorithms/ddp_comm_hooks/optimizer_overlap_hooks.py) in the future.\n4. Since hierarchical SGD is less well-studied than local SGD, there is no guarantee that hierarchical SGD with a finer-grained synchronization granularity can converge faster than certain advanced forms of local SGD, such as [SlowMo](https://openreview.net/pdf?id=SkxJ8REYPH), which can improve convergence efficiency with slow momentum. However, to the best of our knowledge, these advanced algorithms cannot be natively supported as a PyTorch DDP plugin like hierarchical SGD yet.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "## Acknowledgements\n\nWe would like to thank Cruise teammates **Bo Tian**, **Sergei Vorobev**, **Eugene Selivonchyk, Tsugn-Hsien Lee**, **Dan Ring**, **Ian Ackerman**, **Lei Chen**, **Maegan Chew**, **Viet Anh To**, **Xiaohui Long**, **Zeyu Chen**, **Alexander Sidorov**, **Igor Tsvetkov**, **Xin Hu**, **Manav Kataria**, **Marina Rubtsova**, and **Mohamed Fawzy**, as well as Meta teammates **Shen Li, Yanli Zhao, Suraj Subramanian, Hamid Shojanzeri, Anjali Sridhar** and **Bernard Nguyen** for the support.", "metadata": {"source": "https://pytorch.org/blog/straggler-mitigation/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Easily list and initialize models with new APIs in TorchVision\"\nauthor: Vasilis Vryniotis and Laurence Rouesnel\nfeatured-img: \"/assets/images/easily-list-and-initialize-models-with-new-apis-in-torchvision-1.png\"\n---\n\nTorchVision now supports listing and initializing all available built-in models and weights by name. This new API builds upon the recently introduced [Multi-weight support API](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/), is currently in Beta, and it addresses a long-standing [request](https://github.com/pytorch/vision/issues/1143) from the community.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "You can try out the new API in the [latest nightly](https://pytorch.org/get-started/locally/) release of TorchVision. We\u2019re looking to collect feedback ahead of finalizing the feature in TorchVision v0.14. We have created a dedicated [Github Issue](https://github.com/pytorch/vision/issues/6365) where you can post your comments, questions and suggestions!\n\n## Querying and initializing available models\n\nBefore the new model registration API, developers had to query the ``__dict__`` attribute of the modules in order to list all available models or to fetch a specific model builder method by its name:\n\n```python\n# Initialize a model by its name:\nmodel = torchvision.models.__dict__[model_name]()\n\n# List available models:\navailable_models = [\n k for k, v in torchvision.models.__dict__.items()\n if callable(v) and k[0].islower() and k[0] != \"_\"\n]\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "The above approach does not always produce the expected results and is hard to discover. For example, since the [``get_weight()``](https://pytorch.org/vision/main/models.html#using-models-from-hub) method is exposed publicly under the same module, it will be included in the list despite not being a model. In general, reducing the verbosity (less imports, shorter names etc) and being able to initialize models and weights directly from their names (better support of configs, TorchHub etc) was [feedback](https://github.com/pytorch/vision/issues/5088) provided previously by the community. To solve this problem, we have developed a model registration API.\n\n## A new approach\n\nWe\u2019ve added 4 new methods under the torchvision.models module:\n\n```python\nfrom torchvision.models import get_model, get_model_weights, get_weight, list_models\n```", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "The styles and naming conventions align closely with a prototype mechanism proposed by Philip Meier for the [Datasets V2](https://github.com/pytorch/vision/blob/main/torchvision/prototype/datasets/_api.py) API, aiming to offer a similar user experience. The model registration methods are kept private on purpose as we currently focus only on supporting the built-in models of TorchVision.\n\n### List models\n\nListing all available models in TorchVision can be done with a single function call:\n\n```python\n>>> list_models()\n['alexnet', 'mobilenet_v3_large', 'mobilenet_v3_small', 'quantized_mobilenet_v3_large', ...]\n```\n\nTo list the available models of specific submodules:\n\n```python\n>>> list_models(module=torchvision.models)\n['alexnet', 'mobilenet_v3_large', 'mobilenet_v3_small', ...]\n>>> list_models(module=torchvision.models.quantization)\n['quantized_mobilenet_v3_large', ...]\n```\n\n### Initialize models\n\nNow that you know which models are available, you can easily initialize a model with pre-trained weights:", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> get_model(\"quantized_mobilenet_v3_large\", weights=\"DEFAULT\")\nQuantizableMobileNetV3(\n (features): Sequential(\n ....\n )\n)\n```\n\n### Get weights\nSometimes, while working with config files or using TorchHub, you might have the name of a specific weight entry and wish to get its instance. This can be easily done with the following method:\n\n```python\n>>> get_weight(\"ResNet50_Weights.IMAGENET1K_V2\")\nResNet50_Weights.IMAGENET1K_V2\n```\n\nTo get the enum class with all available weights of a specific model you can use either its name:\n\n```python\n>>> get_model_weights(\"quantized_mobilenet_v3_large\")\n\n```\n\nOr its model builder method:\n\n```python\n>>> get_model_weights(torchvision.models.quantization.mobilenet_v3_large)\n\n```\n\n### TorchHub support\nThe new methods are also available via TorchHub:\n\n```python\nimport torch", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "# Fetching a specific weight entry by its name:\nweights = torch.hub.load(\"pytorch/vision\", \"get_weight\", weights=\"ResNet50_Weights.IMAGENET1K_V2\")\n\n# Fetching the weights enum class to list all available entries:\nweight_enum = torch.hub.load(\"pytorch/vision\", \"get_model_weights\", name=\"resnet50\")\nprint([weight for weight in weight_enum])\n```\n\n## Putting it all together\n\nFor example, if you wanted to retrieve all the small-sized models with pre-trained weights and initialize one of them, it\u2019s a matter of using the above APIs:\n\n```python\nimport torchvision\nfrom torchvision.models import get_model, get_model_weights, list_models\n\n\nmax_params = 5000000\n\ntiny_models = []\nfor model_name in list_models(module=torchvision.models):\n weights_enum = get_model_weights(model_name)\n if len([w for w in weights_enum if w.meta[\"num_params\"] <= max_params]) > 0:\n tiny_models.append(model_name)\n\nprint(tiny_models)\n# ['mnasnet0_5', 'mnasnet0_75', 'mnasnet1_0', 'mobilenet_v2', ...]", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "model = get_model(tiny_models[0], weights=\"DEFAULT\")\nprint(sum(x.numel() for x in model.state_dict().values()))\n# 2239188\n```\n\nFor more technical details please see the original [RFC](https://github.com/pytorch/vision/pull/6330). Please spare a few minutes to provide your feedback on the new API, as this is crucial for graduating it from beta and including it in the next release. You can do this on the dedicated [Github Issue](https://github.com/pytorch/vision/issues/6365). We are looking forward to reading your comments!", "metadata": {"source": "https://pytorch.org/blog/easily-list-and-initialize-models-with-new-apis-in-torchvision/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing native PyTorch automatic mixed precision for faster training on NVIDIA GPUs'\nauthor: Mengdi Huang, Chetan Tekur, Michael Carilli\n---\n\nMost deep learning frameworks, including PyTorch, train with 32-bit floating point (FP32) arithmetic by default. However this is not essential to achieve full accuracy for many deep learning models. In 2017, NVIDIA researchers developed a methodology for [mixed-precision training](https://developer.nvidia.com/blog/mixed-precision-training-deep-neural-networks/), which combined [single-precision](https://blogs.nvidia.com/blog/2019/11/15/whats-the-difference-between-single-double-multi-and-mixed-precision-computing/) (FP32) with half-precision (e.g. FP16) format when training a network, and achieved the same accuracy as FP32 training using the same hyperparameters, with additional performance benefits on NVIDIA GPUs:\n\n* Shorter training time;\n* Lower memory requirements, enabling larger batch sizes, larger models, or larger inputs.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "In order to streamline the user experience of training in mixed precision for researchers and practitioners, NVIDIA developed [Apex](https://developer.nvidia.com/blog/apex-pytorch-easy-mixed-precision-training/) in 2018, which is a lightweight PyTorch extension with [Automatic Mixed Precision](https://developer.nvidia.com/automatic-mixed-precision) (AMP) feature. This feature enables automatic conversion of certain GPU operations from FP32 precision to mixed precision, thus improving performance while maintaining accuracy.\n\nFor the PyTorch 1.6 release, developers at NVIDIA and Facebook moved mixed precision functionality into PyTorch core as the AMP package, [torch.cuda.amp](https://pytorch.org/docs/stable/amp.html). `torch.cuda.amp` is more flexible and intuitive compared to `apex.amp`. Some of `apex.amp`'s known pain points that `torch.cuda.amp` has been able to fix:", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "* Guaranteed PyTorch version compatibility, because it's part of PyTorch\n* No need to build extensions\n* Windows support\n* Bitwise accurate [saving/restoring](https://pytorch.org/docs/master/amp.html#torch.cuda.amp.GradScaler.load_state_dict) of checkpoints\n* [DataParallel](https://pytorch.org/docs/master/notes/amp_examples.html#dataparallel-in-a-single-process) and intra-process model parallelism (although we still recommend [torch.nn.DistributedDataParallel](https://pytorch.org/docs/master/notes/amp_examples.html#distributeddataparallel-one-gpu-per-process) with one GPU per process as the most performant approach)\n* [Gradient penalty](https://pytorch.org/docs/master/notes/amp_examples.html#gradient-penalty) (double backward)\n* torch.cuda.amp.autocast() has no effect outside regions where it's enabled, so it should serve cases that formerly struggled with multiple calls to [apex.amp.initialize()](https://github.com/NVIDIA/apex/issues/439) (including [cross-validation)](https://github.com/NVIDIA/apex/issues/392#issuecomment-610038073) without difficulty. Multiple convergence runs in the same script should each use a fresh [GradScaler instance](https://github.com/NVIDIA/apex/issues/439#issuecomment-610028282), but GradScalers are lightweight and self-contained so that's not a problem.\n* Sparse gradient support", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "With AMP being added to PyTorch core, we have started the process of deprecating `apex.amp.` We have moved `apex.amp` to maintenance mode and will support customers using `apex.amp.` However, we highly encourage `apex.amp` customers to transition to using `torch.cuda.amp` from PyTorch Core.\n\n# Example Walkthrough\nPlease see official docs for usage:\n* [https://pytorch.org/docs/stable/amp.html](https://pytorch.org/docs/stable/amp.html )\n* [https://pytorch.org/docs/stable/notes/amp_examples.html](https://pytorch.org/docs/stable/notes/amp_examples.html)\n\nExample:\n\n```python\nimport torch\n# Creates once at the beginning of training\nscaler = torch.cuda.amp.GradScaler()\n\nfor data, label in data_iter:\n optimizer.zero_grad()\n # Casts operations to mixed precision\n with torch.cuda.amp.autocast():\n loss = model(data)\n\n # Scales the loss, and calls backward()\n # to create scaled gradients\n scaler.scale(loss).backward()", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "# Unscales gradients and calls\n # or skips optimizer.step()\n scaler.step(optimizer)\n\n # Updates the scale for next iteration\n scaler.update()\n```\n\n# Performance Benchmarks\nIn this section, we discuss the accuracy and performance of mixed precision training with AMP on the latest NVIDIA GPU A100 and also previous generation V100 GPU. The mixed precision performance is compared to FP32 performance, when running Deep Learning workloads in the [NVIDIA pytorch:20.06-py3 container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch?ncid=partn-52193#cid=ngc01_partn_en-us) from NGC.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "## Accuracy: AMP (FP16), FP32\nThe advantage of using AMP for Deep Learning training is that the models converge to the similar final accuracy while providing improved training performance. To illustrate this point, for [Resnet 50 v1.5 training](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnet50v1.5#training-accuracy-nvidia-dgx-a100-8x-a100-40gb), we see the following accuracy results where higher is better. Please note that the below accuracy numbers are sample numbers that are subject to run to run variance of up to 0.4%. Accuracy numbers for other models including BERT, Transformer, ResNeXt-101, Mask-RCNN, DLRM can be found at [NVIDIA Deep Learning Examples Github](https://github.com/NVIDIA/DeepLearningExamples).\n\nTraining accuracy: NVIDIA DGX A100 (8x A100 40GB)", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n
 epochs Mixed Precision Top 1(%) TF32 Top1(%)
 90 76.93 76.85
\n\nTraining accuracy: NVIDIA DGX-1 (8x V100 16GB)\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n \n \n \n \n \n
 epochs Mixed Precision Top 1(%) FP32 Top1(%)
5076.2576.26
9077.0977.01
25078.4278.30
\n\n## Speedup Performance:", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "### FP16 on NVIDIA V100 vs. FP32 on V100\nAMP with FP16 is the most performant option for DL training on the V100. In Table 1, we can observe that for various models, AMP on V100 provides a speedup of 1.5x to 5.5x over FP32 on V100 while converging to the same final accuracy.\n\n
\n \n
\n*Figure 2. Performance of mixed precision training on NVIDIA 8xV100 vs. FP32 training on 8xV100 GPU. Bars represent the speedup factor of V100 AMP over V100 FP32. The higher the better.*\n\n## FP16 on NVIDIA A100 vs. FP16 on V100\n\nAMP with FP16 remains the most performant option for DL training on the A100. In Figure 3, we can observe that for various models, AMP on A100 provides a speedup of 1.3x to 2.5x over AMP on V100 while converging to the same final accuracy.", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} {"page_content": "
\n \n
\n*Figure 3. Performance of mixed precision training on NVIDIA 8xA100 vs. 8xV100 GPU. Bars represent the speedup factor of A100 over V100. The higher the better.*", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "# Call to action", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "AMP provides a healthy speedup for Deep Learning training workloads on Nvidia Tensor Core GPUs, especially on the latest Ampere generation A100 GPUs. You can start experimenting with AMP enabled models and model scripts for A100, V100, T4 and other GPUs available at NVIDIA deep learning [examples](https://github.com/NVIDIA/DeepLearningExamples). NVIDIA PyTorch with native AMP support is available from the [PyTorch NGC", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch?ncid=partn-52193#cid=ngc01_partn_en-us) version 20.06. We highly encourage existing `apex.amp` customers to transition to using `torch.cuda.amp` from PyTorch Core available in the latest [PyTorch 1.6 release](https://pytorch.org/blog/pytorch-1.6-released/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Tensor Comprehensions in PyTorch'\nauthor: Priya Goyal (FAIR), Nicolas Vasilache (FAIR), Oleksandr Zinenko (Inria & DI ENS), Theodoros Theodoridis (ETH Z\u00fcrich), Zachary DeVito (FAIR), William S. Moses (MIT CSAIL), Sven Verdoolaege (FAIR), Andrew Adams (FAIR), Albert Cohen (Inria & DI ENS & FAIR)\nredirect_from: /2018/03/05/tensor-comprehensions.html\n---", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Tensor Comprehensions (TC) is a tool that lowers the barrier for writing high-performance code. It generates GPU code from a simple high-level language and autotunes the code for specific input sizes.\n\n**We highly recommend reading the [Tensor Comprehensions blogpost](https://research.fb.com/announcing-tensor-comprehensions/) first.**\n\nIf you ran into any of the following scenarios, TC is a useful tool for you.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "- Your PyTorch layer is large and slow, and you contemplated writing a dedicated C++ or CUDA code for it. But you don't know how to program in CUDA or write low-level code.\n\n- You wrote a CUDA layer, but it took a week to write, debug, optimize for speed. You wished you could do this in an hour.\n\n- You want to fuse multiple layers like Conv-ReLU-BatchNorm or Linear-ReLU-Linear-ReLU in your network for speed, but it was quite difficult to comprehend", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "- Your research involves weird Tensor shapes that CuDNN and MKL are not optimized for. For example, you do convolutions of 13 x 24 with an input image of 143 x 55. You tried running it with CuDNN and it was slower than you wished.\n\n- Your code is slowed-down by transposing Tensors constantly to fit a particular memory layout. You wish it was easy to write custom code that operates efficiently on your input layout.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Tensor Comprehensions are seamless to use in PyTorch, interoperating with PyTorch Tensors and `nn` Variables.\n\nLet us run through using TC with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "1. Install the package\n\n```bash\nconda install -c pytorch -c tensorcomp tensor_comprehensions\n```\n\nAt this time we only provide Linux-64 binaries which have been tested on Ubuntu 16.04 and CentOS7.\n\nTC depends on heavyweight C++ projects such as [Halide](http://halide-lang.org/), [Tapir-LLVM](https://github.com/wsmoses/Tapir-LLVM) and [ISL](http://isl.gforge.inria.fr/). Hence, we rely on Anaconda to distribute these dependencies reliably. For the same reason, TC is not available via PyPI.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "2. Import the python package\n\n```python\nimport tensor_comprehensions as tc\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "3. Define the TC expression and create a python function\n\n```python\nlang = \"\"\"\ndef fcrelu(float(B,M) I, float(N,M) W1, float(N) B1) -> (O1) {\n O1(b, n) +=! I(b, m) * W1(n, m)\n O1(b, n) = O1(b, n) + B1(n)\n O1(b, n) = fmax(O1(b, n), 0)\n}\n\"\"\"\nfcrelu = tc.define(lang, name=\"fcrelu\")\n```\n\nThis `fcrelu` function takes PyTorch Tensors as input and returns a PyTorch Tensor. It takes input `I`, weight `W1`, bias `B1` and returns output `O1`.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "4. Let's create some dummy input tensors\n\n```python\nB, M, N = 100, 128, 100\nI, W1, B1 = torch.randn(B, M).cuda(), torch.randn(N, M).cuda(), torch.randn(N).cuda()\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "5. Now autotune the function for your input sizes\n\n```python\nfcrelu.autotune(I, W1, B1, cache=\"fcrelu_100_128_100.tc\")", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "The autotuner is your biggest friend. You generally do not want to use a `tc` function without autotuning it first.\n\nWhen the autotuning is running, the current best performance is displayed. If you are satisfied with the current result or you are out of time, stop the tuning procedure by pressing `Ctrl+C`.\n\n![tc-autotuner](https://pytorch.org/static/img/tc_autotuner.gif)", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "`cache` saves the results of the autotuned kernel search and saves it to the file `fcrelu_100_128_100.tc`. The next time you call the same line of code, it loads the results of the autotuning without recomputing it.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "The autotuner has a few hyperparameters (just like your ConvNet has learning rate, number of layers, etc.). We pick reasonable defaults, but you can read about using advanced options [here](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/writing_layers.html#specifying-mapping-options).", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "6. Call the function with the inputs, to get your result\n\n```python\nout = fcrelu(I, W1, B1)\n```\n\nNow, let's look at how to write TC expressions.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "A quick primer on the TC language\n\nThe TC notation focuses on the mathematical nature of the layer, leaving performance considerations to it's backend code that uses Halide and polyhedral compilation techniques which accumulate decades of cutting edge Loop Nest Optimization (LNO) research.\n\nTC is close to [np.einsum](https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html). We shall quickly learn TC by example", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "```python\nlang = \"\"\"\ndef matmul(float(M,N) A, float(N,K) B) -> (output) {\n output(i, j) +=! A(i, kk) * B(kk, j)\n}\n\"\"\"", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "In this example, we define a function `matmul` which takes two input `A` and `B` of shapes `M x N` and `N x K` and returns a single `output`. The shape of `output` is automatically inferred by the TC language (discussed below).\n\nLet's look at this line:\n\n```python\noutput(i, j) +=! A(i, kk) * B(kk, j)", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "It says:\n\n- `output(i, j)` means output is 2D.\n- for each location `output(i, j)`, we add (`+=`) `A(i, kk) * B(kk, j)`.\n- `i` is well-defined as all locations in `A` dim=0, i.e. `i in range(0, M)`\n- `j` is well-defined as all locations in `B` dim=1, i.e. `j in range(0, K)`\n- `kk` is inferred as all locations from `0` to `N`\n\nThe shape of output is inferred from the maximum values `i` and `j` can take, which is `M` and `K`, so output is of size `M x K`.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "The `!` symbol initializes output with `0.0`. It is equivalent to:\n\n```python\noutput(i, j) = 0\noutput(i, j) += A(i, kk) * B(kk, j)", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "**Scalar inputs and range constraints: implement AvgPool2d**\n\n```python\n\"\"\"\n\n{% raw %}def avgpool(float(B, C, H, W) input) -> (output) {{{% endraw %}\n output(b, c, h, w) += input(b, c, h * {sH} + kh, w * {sW} + kw) where kh in 0:{kH}, kw in 0:{kW}\n{% raw %}}}{% endraw %}\n\n\"\"\"\navgpool = tc.define(LANG, name=\"avgpool\", constants={\"sH\":1, \"sW\":1, \"kH\":2, \"kW\":2})", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "here the `where` keyword can take ranges of values to operate on. `0:{kH}` is equivalent `range(kH)` in Python.\n\nNote: the syntax for passing in scalars is subject to change in the next release.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "torch.nn layers\n\nWe added some sugar-coating around the basic PyTorch integration of TC to make it easy to integrate TC into larger `torch.nn` models by defining the forward and backward TC expressions and taking `Variable` inputs / outputs. Here is an [example](https://github.com/facebookresearch/TensorComprehensions/blob/master/test_python/layers/test_convolution_train.py) of defining a convolution layer with TC.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Some essentials that you will miss (we're working on them)", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Autotuning for variable-length sequences\n\nThe TC auto-tuner requires all input sizes to be specified before-hand. For example, if you have input `I1` which is an image batch, the autotuner wants to know the exact shape of `I1` to generate an optimized kernel. You cannot specify: `image with height between 200 and 300`. This is more essential in sequence data such as NLP, where each sentence can have a different length.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "The reason why the autotuner is non-parametric is because it's harder and harder to auto-tune parametric constraints, this is active research. Hence, for the first release, we made a conscious decision to give you the tool in a form where we know it works well.\n\nAs a work-around, if you know that you have a few specific shapes of interest, you can run the autotuner with these multiple shapes.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "```python\nrelu = tc.define(LANG, name=\"relu\")\nbatch, channels = 16, 3\ntc.autotune((batch, channels, 32, 32)) # image of size 32 x 32\ntc.autotune((batch, channels, 48, 48)) # image of size 48 x 48\ntc.autotune((batch, channels, 64, 64)) # image of size 64 x 64", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Now the autotuner is tuned for these three specific image sizes `32x32`, `48x48` and `64x64`.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Lack of loops\n\nIf you want to write an RNN, it's easy to see it as a `for` loop over time. However, the TC language does not have loops yet. If you really want to write RNNs, you can write unrolled loops.\n\n### Strided-Tensors\n\nThe TC backend does not support non-contiguous Tensors yet. If the inputs you give are not contiguous, they are made contiguous before passing to the TC backend.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Reshaping Tensors within a TC expression\n\nYou cannot write this operation in TC: `torch.matmul(...).view(...).mean(...)`. Whenever there is need for a `view` to change the shape of an input, you have to get the output, `view` it at the PyTorch level.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Getting Started", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "- [Walk through Tutorial](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/writing_layers.html) to quickly get started with understanding and using Tensor Comprehensions PyTorch package.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "- [Over 20 examples](https://github.com/facebookresearch/TensorComprehensions/tree/master/test_python/layers) of various ML layers with TC, including `avgpool`, `maxpool`, `matmul`, matmul - give output buffers and `batch-matmul`, `convolution`, `strided-convolution`, `batchnorm`, `copy`, `cosine similarity`, `Linear`, `Linear + ReLU`, `group-convolutions`, strided `group-convolutions`, `indexing`, `Embedding` (lookup table), small-mobilenet, `softmax`, `tensordot`, `transpose`", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "- [Detailed docs](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/getting_started.html) on Tensor Comprehensions and integration with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Communication\n\n- [Slack](https://tensorcomprehensions.herokuapp.com/): For discussion around framework integration, build support, collaboration, etc. join our slack channel.\n- Email: tensorcomp@fb.com\n- [GitHub](https://github.com/facebookresearch/TensorComprehensions): bug reports, feature requests, install issues, RFCs, thoughts, etc.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nWe would like to thank Soumith Chintala, [Edward Yang](https://github.com/ezyang) and [Sam Gross](https://github.com/colesbury) for their immense guidance and help in making the integration API nice and smooth. We would also like to thank rest of the PyTorch team and our pre-release users for their helpful feedback that guided us in making the integration better.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchVision\u2019s New Multi-Weight Support API\"\nauthor: Vasilis Vryniotis\nfeatured-img: \"assets/images/torchvision_featured.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "TorchVision has a new backwards compatible API for building models with multi-weight support. The new API allows loading different pre-trained weights on the same model variant, keeps track of vital meta-data such as the classification labels and includes the preprocessing transforms necessary for using the models. In this blog post, we plan to review the prototype API, show-case its features and highlight key differences with the existing one.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nWe are hoping to get your thoughts about the API prior finalizing it. To collect your feedback, we have created a [Github issue](https://github.com/pytorch/vision/issues/5088) where you can post your thoughts, questions and comments.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Limitations of the current API\n\nTorchVision currently provides pre-trained models which could be a starting point for transfer learning or used as-is in Computer Vision applications. The typical way to instantiate a pre-trained model and make a prediction is:\n\n```Python\nimport torch\n\nfrom PIL import Image\nfrom torchvision import models as M\nfrom torchvision.transforms import transforms as T\n\n\nimg = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# Step 1: Initialize model\nmodel = M.resnet50(pretrained=True)\nmodel.eval()\n\n# Step 2: Define and initialize the inference transforms\npreprocess = T.Compose([\n T.Resize([256, ]),\n T.CenterCrop(224),\n T.PILToTensor(),\n T.ConvertImageDtype(torch.float),\n T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\n# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\nprediction = model(batch).squeeze(0).softmax(0)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# Step 4: Use the model and print the predicted category\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\nwith open(\"imagenet_classes.txt\", \"r\") as f:\n categories = [s.strip() for s in f.readlines()]\n category_name = categories[class_id]\nprint(f\"{category_name}: {100 * score}%\")", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "There are a few limitations with the above approach:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "1. **Inability to support multiple pre-trained weights:** Since the `pretrained` variable is boolean, we can only offer one set of weights. This poses a severe limitation when we significantly [improve the accuracy of existing models](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and we want to make those improvements available to the community. It also stops us from offering pre-trained weights of the same model variant on different datasets.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "2. **Missing inference/preprocessing transforms:** The user is forced to define the necessary transforms prior using the model. The inference transforms are usually linked to the training process and dataset used to estimate the weights. Any minor discrepancies in these transforms (such as interpolation value, resize/crop sizes etc) can lead to major reductions in accuracy or unusable models.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "3. **Lack of meta-data:** Critical pieces of information in relation to the weights are unavailable to the users. For example, one needs to look into external sources and the documentation to find things like the [category labels](https://github.com/pytorch/vision/issues/1946), the training recipe, the accuracy metrics etc.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "The new API addresses the above limitations and reduces the amount of boilerplate code needed for standard tasks.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Overview of the prototype API\n\nLet\u2019s see how we can achieve exactly the same results as above using the new API:\n\n```Python\nfrom PIL import Image\nfrom torchvision.prototype import models as PM\n\n\nimg = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n\n# Step 1: Initialize model\nweights = PM.ResNet50_Weights.IMAGENET1K_V1\nmodel = PM.resnet50(weights=weights)\nmodel.eval()\n\n# Step 2: Initialize the inference transforms\npreprocess = weights.transforms()", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\nprediction = model(batch).squeeze(0).softmax(0)\n\n# Step 4: Use the model and print the predicted category\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\ncategory_name = weights.meta[\"categories\"][class_id]\nprint(f\"{category_name}: {100 * score}*%*\")", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "As we can see the new API eliminates the aforementioned limitations. Let\u2019s explore the new features in detail.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Multi-weight support", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "At the heart of the new API, we have the ability to define multiple different weights for the same model variant. Each model building method (eg `resnet50`) has an associated Enum class (eg `ResNet50_Weights`) which has as many entries as the number of pre-trained weights available. Additionally, each Enum class has a `DEFAULT` alias which points to the best available weights for the specific model. This allows the users who want to always use the best available weights to do so without modifying their", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "code.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Here is an example of initializing models with different weights:\n\n```python\nfrom torchvision.prototype.models import resnet50, ResNet50_Weights\n\n# Legacy weights with accuracy 76.130%\nmodel = resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\n\n# New weights with accuracy 80.858%\nmodel = resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)\n\n# Best available weights (currently alias for IMAGENET1K_V2)\nmodel = resnet50(weights=ResNet50_Weights.DEFAULT)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# No weights - random initialization\nmodel = resnet50(weights=None)\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Associated meta-data & preprocessing transforms\n\nThe weights of each model are associated with meta-data. The type of information we store depends on the task of the model (Classification, Detection, Segmentation etc). Typical information includes a link to the training recipe, the interpolation mode, information such as the categories and validation metrics. These values are programmatically accessible via the `meta` attribute:\n\n```Python\nfrom torchvision.prototype.models import ResNet50_Weights", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# Accessing a single record\nsize = ResNet50_Weights.IMAGENET1K_V2.meta[\"size\"]\n\n# Iterating the items of the meta-data dictionary\nfor k, v in ResNet50_Weights.IMAGENET1K_V2.meta.items():\n print(k, v)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Additionally, each weights entry is associated with the necessary preprocessing transforms. All current preprocessing transforms are JIT-scriptable and can be accessed via the `transforms` attribute. Prior using them with the data, the transforms need to be initialized/constructed. This lazy initialization scheme is done to ensure the solution is memory efficient. The input of the transforms can be either a `PIL.Image` or a `Tensor` read using `torchvision.io`.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "```Python\nfrom torchvision.prototype.models import ResNet50_Weights\n\n# Initializing preprocessing at standard 224x224 resolution\npreprocess = ResNet50_Weights.IMAGENET1K_V2.transforms()\n\n# Initializing preprocessing at 400x400 resolution\npreprocess = ResNet50_Weights.IMAGENET1K_V2.transforms(crop_size=400, resize_size=400)\n\n# Once initialized the callable can accept the image data:\n# img_preprocessed = preprocess(img)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Associating the weights with their meta-data and preprocessing will boost transparency, improve reproducibility and make it easier to document how a set of weights was produced.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Get weights by name\n\nThe ability to link directly the weights with their properties (meta data, preprocessing callables etc) is the reason why our implementation uses Enums instead of Strings. Nevertheless for cases when only the name of the weights is available, we offer a method capable of linking Weight names to their Enums:\n\n```Python\nfrom torchvision.prototype.models import get_weight", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "# Weights can be retrieved by name:\nassert get_weight(\"ResNet50_Weights.IMAGENET1K_V1\") == ResNet50_Weights.IMAGENET1K_V1\nassert get_weight(\"ResNet50_Weights.IMAGENET1K_V2\") == ResNet50_Weights.IMAGENET1K_V2\n\n# Including using the DEFAULT alias:\nassert get_weight(\"ResNet50_Weights.DEFAULT\") == ResNet50_Weights.IMAGENET1K_V2\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Deprecations\n\nIn the new API the boolean `pretrained` and `pretrained_backbone` parameters, which were previously used to load weights to the full model or to its backbone, are deprecated. The current implementation is fully backwards compatible as it seamlessly maps the old parameters to the new ones. Using the old parameters to the new builders emits the following deprecation warnings:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "```Python\n>>> model = torchvision.prototype.models.resnet50(pretrained=True)\n UserWarning: The parameter 'pretrained' is deprecated, please use 'weights' instead.\nUserWarning:\nArguments other than a weight enum or `None` for 'weights' are deprecated.\nThe current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`.\nYou can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Additionally the builder methods require using keyword parameters. The use of positional parameter is deprecated and using them emits the following warning:\n\n```Python\n>>> model = torchvision.prototype.models.resnet50(None)\nUserWarning:\nUsing 'weights' as positional parameter(s) is deprecated.\nPlease use keyword parameter(s) instead.\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Testing the new API\n\nMigrating to the new API is very straightforward. The following method calls between the 2 APIs are all equivalent:\n\n```\n# Using pretrained weights:\ntorchvision.prototype.models.resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\ntorchvision.models.resnet50(pretrained=True)\ntorchvision.models.resnet50(True)\n\n# Using no weights:\ntorchvision.prototype.models.resnet50(weights=None)\ntorchvision.models.resnet50(pretrained=False)\ntorchvision.models.resnet50(False)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Note that the prototype features are available only on the nightly versions of TorchVision, so to use it you need to install it as follows:\n\n```\nconda install torchvision -c pytorch-nightly\n```\n\nFor alternative ways to install the nightly have a look on the PyTorch [download page](https://pytorch.org/get-started/locally/). You can also install TorchVision from source from the latest main; for more information have a look on our [repo](https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "Accessing state-of-the-art model weights with the new API\n\nIf you are still unconvinced about giving a try to the new API, here is one more reason to do so. We\u2019ve recently refreshed our [training recipe](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and achieved SOTA accuracy from many of our models. The improved weights can easily be accessed via the new API. Here is a quick overview of the model improvements:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "| Model | Old Acc@1 | New Acc@1 |\n| -------------------------- | --------- | --------- |\n| EfficientNet B1 | 78.642 | 79.838 |\n| MobileNetV3 Large | 74.042 | 75.274 |\n| Quantized ResNet50 | 75.92 | 80.282 |\n| Quantized ResNeXt101 32x8d | 78.986 | 82.574 |\n| RegNet X 400mf | 72.834 | 74.864 |\n| RegNet X 800mf | 75.212 | 77.522 |\n| RegNet X 1 6gf | 77.04 | 79.668 |", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "| RegNet X 3 2gf | 78.364 | 81.198 |\n| RegNet X 8gf | 79.344 | 81.682 |\n| RegNet X 16gf | 80.058 | 82.72 |\n| RegNet X 32gf | 80.622 | 83.018 |\n| RegNet Y 400mf | 74.046 | 75.806 |\n| RegNet Y 800mf | 76.42 | 78.838 |\n| RegNet Y 1 6gf | 77.95 | 80.882 |\n| RegNet Y 3 2gf | 78.948 | 81.984 |\n| RegNet Y 8gf | 80.032 | 82.828 |", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "| RegNet Y 16gf | 80.424 | 82.89 |\n| RegNet Y 32gf | 80.878 | 83.366 |\n| ResNet50 | 76.13 | 80.858 |\n| ResNet101 | 77.374 | 81.886 |\n| ResNet152 | 78.312 | 82.284 |\n| ResNeXt50 32x4d | 77.618 | 81.198 |\n| ResNeXt101 32x8d | 79.312 | 82.834 |\n| Wide ResNet50 2 | 78.468 | 81.602 |\n| Wide ResNet101 2 | 78.848 | 82.51 |", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "# Call to action\nAMP provides a healthy speedup for Deep Learning training workloads on Nvidia Tensor Core GPUs, especially on the latest Ampere generation A100 GPUs. You can start experimenting with AMP enabled models and model scripts for A100, V100, T4 and other GPUs available at NVIDIA deep learning [examples](https://github.com/NVIDIA/DeepLearningExamples). NVIDIA PyTorch with native AMP support is available from the [PyTorch NGC container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch?ncid=partn-52193#cid=ngc01_partn_en-us) version 20.06. We highly encourage existing `apex.amp` customers to transition to using `torch.cuda.amp` from PyTorch Core available in the latest [PyTorch 1.6 release](https://pytorch.org/blog/pytorch-1.6-released/).", "metadata": {"source": "https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Tensor Comprehensions in PyTorch'\nauthor: Priya Goyal (FAIR), Nicolas Vasilache (FAIR), Oleksandr Zinenko (Inria & DI ENS), Theodoros Theodoridis (ETH Z\u00fcrich), Zachary DeVito (FAIR), William S. Moses (MIT CSAIL), Sven Verdoolaege (FAIR), Andrew Adams (FAIR), Albert Cohen (Inria & DI ENS & FAIR)\nredirect_from: /2018/03/05/tensor-comprehensions.html\n---\n\nTensor Comprehensions (TC) is a tool that lowers the barrier for writing high-performance code. It generates GPU code from a simple high-level language and autotunes the code for specific input sizes.\n\n**We highly recommend reading the [Tensor Comprehensions blogpost](https://research.fb.com/announcing-tensor-comprehensions/) first.**\n\nIf you ran into any of the following scenarios, TC is a useful tool for you.\n\n- Your PyTorch layer is large and slow, and you contemplated writing a dedicated C++ or CUDA code for it. But you don't know how to program in CUDA or write low-level code.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "- You wrote a CUDA layer, but it took a week to write, debug, optimize for speed. You wished you could do this in an hour.\n\n- You want to fuse multiple layers like Conv-ReLU-BatchNorm or Linear-ReLU-Linear-ReLU in your network for speed, but it was quite difficult to comprehend\n\n- Your research involves weird Tensor shapes that CuDNN and MKL are not optimized for. For example, you do convolutions of 13 x 24 with an input image of 143 x 55. You tried running it with CuDNN and it was slower than you wished.\n\n- Your code is slowed-down by transposing Tensors constantly to fit a particular memory layout. You wish it was easy to write custom code that operates efficiently on your input layout.\n\n\nTensor Comprehensions are seamless to use in PyTorch, interoperating with PyTorch Tensors and `nn` Variables.\n\nLet us run through using TC with PyTorch.\n\n#### 1. Install the package\n\n```bash\nconda install -c pytorch -c tensorcomp tensor_comprehensions\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "At this time we only provide Linux-64 binaries which have been tested on Ubuntu 16.04 and CentOS7.\n\nTC depends on heavyweight C++ projects such as [Halide](http://halide-lang.org/), [Tapir-LLVM](https://github.com/wsmoses/Tapir-LLVM) and [ISL](http://isl.gforge.inria.fr/). Hence, we rely on Anaconda to distribute these dependencies reliably. For the same reason, TC is not available via PyPI.\n\n#### 2. Import the python package\n\n```python\nimport tensor_comprehensions as tc\n```\n\n#### 3. Define the TC expression and create a python function\n\n```python\nlang = \"\"\"\ndef fcrelu(float(B,M) I, float(N,M) W1, float(N) B1) -> (O1) {\n O1(b, n) +=! I(b, m) * W1(n, m)\n O1(b, n) = O1(b, n) + B1(n)\n O1(b, n) = fmax(O1(b, n), 0)\n}\n\"\"\"\nfcrelu = tc.define(lang, name=\"fcrelu\")\n```\n\nThis `fcrelu` function takes PyTorch Tensors as input and returns a PyTorch Tensor. It takes input `I`, weight `W1`, bias `B1` and returns output `O1`.\n\n#### 4. Let's create some dummy input tensors", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "```python\nB, M, N = 100, 128, 100\nI, W1, B1 = torch.randn(B, M).cuda(), torch.randn(N, M).cuda(), torch.randn(N).cuda()\n```\n\n#### 5. Now autotune the function for your input sizes\n\n```python\nfcrelu.autotune(I, W1, B1, cache=\"fcrelu_100_128_100.tc\")\n```\n\nThe autotuner is your biggest friend. You generally do not want to use a `tc` function without autotuning it first.\n\nWhen the autotuning is running, the current best performance is displayed. If you are satisfied with the current result or you are out of time, stop the tuning procedure by pressing `Ctrl+C`.\n\n![tc-autotuner](https://pytorch.org/static/img/tc_autotuner.gif)\n\n`cache` saves the results of the autotuned kernel search and saves it to the file `fcrelu_100_128_100.tc`. The next time you call the same line of code, it loads the results of the autotuning without recomputing it.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "The autotuner has a few hyperparameters (just like your ConvNet has learning rate, number of layers, etc.). We pick reasonable defaults, but you can read about using advanced options [here](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/writing_layers.html#specifying-mapping-options).\n\n#### 6. Call the function with the inputs, to get your result\n\n```python\nout = fcrelu(I, W1, B1)\n```\n\nNow, let's look at how to write TC expressions.\n\n## A quick primer on the TC language\n\nThe TC notation focuses on the mathematical nature of the layer, leaving performance considerations to it's backend code that uses Halide and polyhedral compilation techniques which accumulate decades of cutting edge Loop Nest Optimization (LNO) research.\n\nTC is close to [np.einsum](https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html). We shall quickly learn TC by example", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "```python\nlang = \"\"\"\ndef matmul(float(M,N) A, float(N,K) B) -> (output) {\n output(i, j) +=! A(i, kk) * B(kk, j)\n}\n\"\"\"\n```\n\nIn this example, we define a function `matmul` which takes two input `A` and `B` of shapes `M x N` and `N x K` and returns a single `output`. The shape of `output` is automatically inferred by the TC language (discussed below).\n\nLet's look at this line:\n\n```python\noutput(i, j) +=! A(i, kk) * B(kk, j)\n```\n\nIt says:\n\n- `output(i, j)` means output is 2D.\n- for each location `output(i, j)`, we add (`+=`) `A(i, kk) * B(kk, j)`.\n- `i` is well-defined as all locations in `A` dim=0, i.e. `i in range(0, M)`\n- `j` is well-defined as all locations in `B` dim=1, i.e. `j in range(0, K)`\n- `kk` is inferred as all locations from `0` to `N`\n\nThe shape of output is inferred from the maximum values `i` and `j` can take, which is `M` and `K`, so output is of size `M x K`.\n\nThe `!` symbol initializes output with `0.0`. It is equivalent to:\n\n```python\noutput(i, j) = 0\noutput(i, j) += A(i, kk) * B(kk, j)\n```", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "**Scalar inputs and range constraints: implement AvgPool2d**\n\n```python\n\"\"\"\n\n{% raw %}def avgpool(float(B, C, H, W) input) -> (output) {{{% endraw %}\n output(b, c, h, w) += input(b, c, h * {sH} + kh, w * {sW} + kw) where kh in 0:{kH}, kw in 0:{kW}\n{% raw %}}}{% endraw %}\n\n\"\"\"\navgpool = tc.define(LANG, name=\"avgpool\", constants={\"sH\":1, \"sW\":1, \"kH\":2, \"kW\":2})\n```\n\nhere the `where` keyword can take ranges of values to operate on. `0:{kH}` is equivalent `range(kH)` in Python.\n\nNote: the syntax for passing in scalars is subject to change in the next release.\n\n## torch.nn layers\n\nWe added some sugar-coating around the basic PyTorch integration of TC to make it easy to integrate TC into larger `torch.nn` models by defining the forward and backward TC expressions and taking `Variable` inputs / outputs. Here is an [example](https://github.com/facebookresearch/TensorComprehensions/blob/master/test_python/layers/test_convolution_train.py) of defining a convolution layer with TC.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "## Some essentials that you will miss (we're working on them)\n\n### Autotuning for variable-length sequences\n\nThe TC auto-tuner requires all input sizes to be specified before-hand. For example, if you have input `I1` which is an image batch, the autotuner wants to know the exact shape of `I1` to generate an optimized kernel. You cannot specify: `image with height between 200 and 300`. This is more essential in sequence data such as NLP, where each sentence can have a different length.\n\nThe reason why the autotuner is non-parametric is because it's harder and harder to auto-tune parametric constraints, this is active research. Hence, for the first release, we made a conscious decision to give you the tool in a form where we know it works well.\n\nAs a work-around, if you know that you have a few specific shapes of interest, you can run the autotuner with these multiple shapes.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "```python\nrelu = tc.define(LANG, name=\"relu\")\nbatch, channels = 16, 3\ntc.autotune((batch, channels, 32, 32)) # image of size 32 x 32\ntc.autotune((batch, channels, 48, 48)) # image of size 48 x 48\ntc.autotune((batch, channels, 64, 64)) # image of size 64 x 64\n```\n\nNow the autotuner is tuned for these three specific image sizes `32x32`, `48x48` and `64x64`.\n\n### Lack of loops\n\nIf you want to write an RNN, it's easy to see it as a `for` loop over time. However, the TC language does not have loops yet. If you really want to write RNNs, you can write unrolled loops.\n\n### Strided-Tensors\n\nThe TC backend does not support non-contiguous Tensors yet. If the inputs you give are not contiguous, they are made contiguous before passing to the TC backend.\n\n### Reshaping Tensors within a TC expression\n\nYou cannot write this operation in TC: `torch.matmul(...).view(...).mean(...)`. Whenever there is need for a `view` to change the shape of an input, you have to get the output, `view` it at the PyTorch level.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "## Getting Started\n\n- [Walk through Tutorial](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/writing_layers.html) to quickly get started with understanding and using Tensor Comprehensions PyTorch package.\n- [Over 20 examples](https://github.com/facebookresearch/TensorComprehensions/tree/master/test_python/layers) of various ML layers with TC, including `avgpool`, `maxpool`, `matmul`, matmul - give output buffers and `batch-matmul`, `convolution`, `strided-convolution`, `batchnorm`, `copy`, `cosine similarity`, `Linear`, `Linear + ReLU`, `group-convolutions`, strided `group-convolutions`, `indexing`, `Embedding` (lookup table), small-mobilenet, `softmax`, `tensordot`, `transpose`\n- [Detailed docs](https://facebookresearch.github.io/TensorComprehensions/framework/pytorch_integration/getting_started.html) on Tensor Comprehensions and integration with PyTorch.\n\n## Communication", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "## Communication\n\n- [Slack](https://tensorcomprehensions.herokuapp.com/): For discussion around framework integration, build support, collaboration, etc. join our slack channel.\n- Email: tensorcomp@fb.com\n- [GitHub](https://github.com/facebookresearch/TensorComprehensions): bug reports, feature requests, install issues, RFCs, thoughts, etc.\n\n## Acknowledgements\n\nWe would like to thank Soumith Chintala, [Edward Yang](https://github.com/ezyang) and [Sam Gross](https://github.com/colesbury) for their immense guidance and help in making the integration API nice and smooth. We would also like to thank rest of the PyTorch team and our pre-release users for their helpful feedback that guided us in making the integration better.", "metadata": {"source": "https://pytorch.org/blog/tensor-comprehensions/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchVision\u2019s New Multi-Weight Support API\"\nauthor: Vasilis Vryniotis\nfeatured-img: \"assets/images/torchvision_featured.png\"\n---\n\nTorchVision has a new backwards compatible API for building models with multi-weight support. The new API allows loading different pre-trained weights on the same model variant, keeps track of vital meta-data such as the classification labels and includes the preprocessing transforms necessary for using the models. In this blog post, we plan to review the prototype API, show-case its features and highlight key differences with the existing one.\n\n
\n \n
\n\nWe are hoping to get your thoughts about the API prior finalizing it. To collect your feedback, we have created a [Github issue](https://github.com/pytorch/vision/issues/5088) where you can post your thoughts, questions and comments.\n\n## Limitations of the current API", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "TorchVision currently provides pre-trained models which could be a starting point for transfer learning or used as-is in Computer Vision applications. The typical way to instantiate a pre-trained model and make a prediction is:\n\n```Python\nimport torch\n\nfrom PIL import Image\nfrom torchvision import models as M\nfrom torchvision.transforms import transforms as T\n\n\nimg = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n\n# Step 1: Initialize model\nmodel = M.resnet50(pretrained=True)\nmodel.eval()\n\n# Step 2: Define and initialize the inference transforms\npreprocess = T.Compose([\n T.Resize([256, ]),\n T.CenterCrop(224),\n T.PILToTensor(),\n T.ConvertImageDtype(torch.float),\n T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\n# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\nprediction = model(batch).squeeze(0).softmax(0)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "# Step 4: Use the model and print the predicted category\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\nwith open(\"imagenet_classes.txt\", \"r\") as f:\n categories = [s.strip() for s in f.readlines()]\n category_name = categories[class_id]\nprint(f\"{category_name}: {100 * score}%\")\n\n```\n\nThere are a few limitations with the above approach:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "1. **Inability to support multiple pre-trained weights:** Since the `pretrained` variable is boolean, we can only offer one set of weights. This poses a severe limitation when we significantly [improve the accuracy of existing models](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and we want to make those improvements available to the community. It also stops us from offering pre-trained weights of the same model variant on different datasets.\n2. **Missing inference/preprocessing transforms:** The user is forced to define the necessary transforms prior using the model. The inference transforms are usually linked to the training process and dataset used to estimate the weights. Any minor discrepancies in these transforms (such as interpolation value, resize/crop sizes etc) can lead to major reductions in accuracy or unusable models.\n3. **Lack of meta-data:** Critical pieces of information in relation to the weights are unavailable to the users. For example, one needs to look into external sources and the documentation to find things like the [category labels](https://github.com/pytorch/vision/issues/1946), the training recipe, the accuracy metrics etc.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "The new API addresses the above limitations and reduces the amount of boilerplate code needed for standard tasks.\n\n## Overview of the prototype API\n\nLet\u2019s see how we can achieve exactly the same results as above using the new API:\n\n```Python\nfrom PIL import Image\nfrom torchvision.prototype import models as PM\n\n\nimg = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n\n# Step 1: Initialize model\nweights = PM.ResNet50_Weights.IMAGENET1K_V1\nmodel = PM.resnet50(weights=weights)\nmodel.eval()\n\n# Step 2: Initialize the inference transforms\npreprocess = weights.transforms()\n\n# Step 3: Apply inference preprocessing transforms\nbatch = preprocess(img).unsqueeze(0)\nprediction = model(batch).squeeze(0).softmax(0)\n\n# Step 4: Use the model and print the predicted category\nclass_id = prediction.argmax().item()\nscore = prediction[class_id].item()\ncategory_name = weights.meta[\"categories\"][class_id]\nprint(f\"{category_name}: {100 * score}*%*\")\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "As we can see the new API eliminates the aforementioned limitations. Let\u2019s explore the new features in detail.\n\n### Multi-weight support\n\nAt the heart of the new API, we have the ability to define multiple different weights for the same model variant. Each model building method (eg `resnet50`) has an associated Enum class (eg `ResNet50_Weights`) which has as many entries as the number of pre-trained weights available. Additionally, each Enum class has a `DEFAULT` alias which points to the best available weights for the specific model. This allows the users who want to always use the best available weights to do so without modifying their code.\n\nHere is an example of initializing models with different weights:\n\n```python\nfrom torchvision.prototype.models import resnet50, ResNet50_Weights\n\n# Legacy weights with accuracy 76.130%\nmodel = resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\n\n# New weights with accuracy 80.858%\nmodel = resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "# Best available weights (currently alias for IMAGENET1K_V2)\nmodel = resnet50(weights=ResNet50_Weights.DEFAULT)\n\n# No weights - random initialization\nmodel = resnet50(weights=None)\n```\n\n### Associated meta-data & preprocessing transforms\n\nThe weights of each model are associated with meta-data. The type of information we store depends on the task of the model (Classification, Detection, Segmentation etc). Typical information includes a link to the training recipe, the interpolation mode, information such as the categories and validation metrics. These values are programmatically accessible via the `meta` attribute:\n\n```Python\nfrom torchvision.prototype.models import ResNet50_Weights\n\n# Accessing a single record\nsize = ResNet50_Weights.IMAGENET1K_V2.meta[\"size\"]\n\n# Iterating the items of the meta-data dictionary\nfor k, v in ResNet50_Weights.IMAGENET1K_V2.meta.items():\n print(k, v)\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "Additionally, each weights entry is associated with the necessary preprocessing transforms. All current preprocessing transforms are JIT-scriptable and can be accessed via the `transforms` attribute. Prior using them with the data, the transforms need to be initialized/constructed. This lazy initialization scheme is done to ensure the solution is memory efficient. The input of the transforms can be either a `PIL.Image` or a `Tensor` read using `torchvision.io`.\n\n```Python\nfrom torchvision.prototype.models import ResNet50_Weights\n\n# Initializing preprocessing at standard 224x224 resolution\npreprocess = ResNet50_Weights.IMAGENET1K_V2.transforms()\n\n# Initializing preprocessing at 400x400 resolution\npreprocess = ResNet50_Weights.IMAGENET1K_V2.transforms(crop_size=400, resize_size=400)\n\n# Once initialized the callable can accept the image data:\n# img_preprocessed = preprocess(img)\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "Associating the weights with their meta-data and preprocessing will boost transparency, improve reproducibility and make it easier to document how a set of weights was produced.\n\n### Get weights by name\n\nThe ability to link directly the weights with their properties (meta data, preprocessing callables etc) is the reason why our implementation uses Enums instead of Strings. Nevertheless for cases when only the name of the weights is available, we offer a method capable of linking Weight names to their Enums:\n\n```Python\nfrom torchvision.prototype.models import get_weight\n\n# Weights can be retrieved by name:\nassert get_weight(\"ResNet50_Weights.IMAGENET1K_V1\") == ResNet50_Weights.IMAGENET1K_V1\nassert get_weight(\"ResNet50_Weights.IMAGENET1K_V2\") == ResNet50_Weights.IMAGENET1K_V2\n\n# Including using the DEFAULT alias:\nassert get_weight(\"ResNet50_Weights.DEFAULT\") == ResNet50_Weights.IMAGENET1K_V2\n```\n\n## Deprecations", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "## Deprecations\n\nIn the new API the boolean `pretrained` and `pretrained_backbone` parameters, which were previously used to load weights to the full model or to its backbone, are deprecated. The current implementation is fully backwards compatible as it seamlessly maps the old parameters to the new ones. Using the old parameters to the new builders emits the following deprecation warnings:\n\n```Python\n>>> model = torchvision.prototype.models.resnet50(pretrained=True)\n UserWarning: The parameter 'pretrained' is deprecated, please use 'weights' instead.\nUserWarning:\nArguments other than a weight enum or `None` for 'weights' are deprecated.\nThe current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`.\nYou can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.\n```\n\nAdditionally the builder methods require using keyword parameters. The use of positional parameter is deprecated and using them emits the following warning:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "```Python\n>>> model = torchvision.prototype.models.resnet50(None)\nUserWarning:\nUsing 'weights' as positional parameter(s) is deprecated.\nPlease use keyword parameter(s) instead.\n```\n\n## Testing the new API\n\nMigrating to the new API is very straightforward. The following method calls between the 2 APIs are all equivalent:\n\n```\n# Using pretrained weights:\ntorchvision.prototype.models.resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)\ntorchvision.models.resnet50(pretrained=True)\ntorchvision.models.resnet50(True)\n\n# Using no weights:\ntorchvision.prototype.models.resnet50(weights=None)\ntorchvision.models.resnet50(pretrained=False)\ntorchvision.models.resnet50(False)\n```\n\nNote that the prototype features are available only on the nightly versions of TorchVision, so to use it you need to install it as follows:\n\n```\nconda install torchvision -c pytorch-nightly\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "For alternative ways to install the nightly have a look on the PyTorch [download page](https://pytorch.org/get-started/locally/). You can also install TorchVision from source from the latest main; for more information have a look on our [repo](https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md).\n\n## Accessing state-of-the-art model weights with the new API\n\nIf you are still unconvinced about giving a try to the new API, here is one more reason to do so. We\u2019ve recently refreshed our [training recipe](https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/) and achieved SOTA accuracy from many of our models. The improved weights can easily be accessed via the new API. Here is a quick overview of the model improvements:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} +{"page_content": "| Model | Old Acc@1 | New Acc@1 |\n| -------------------------- | --------- | --------- |\n| EfficientNet B1 | 78.642 | 79.838 |\n| MobileNetV3 Large | 74.042 | 75.274 |\n| Quantized ResNet50 | 75.92 | 80.282 |\n| Quantized ResNeXt101 32x8d | 78.986 | 82.574 |\n| RegNet X 400mf | 72.834 | 74.864 |\n| RegNet X 800mf | 75.212 | 77.522 |\n| RegNet X 1 6gf | 77.04 | 79.668 |\n| RegNet X 3 2gf | 78.364 | 81.198 |\n| RegNet X 8gf | 79.344 | 81.682 |\n| RegNet X 16gf | 80.058 | 82.72 |\n| RegNet X 32gf | 80.622 | 83.018 |\n| RegNet Y 400mf | 74.046 | 75.806 |\n| RegNet Y 800mf | 76.42 | 78.838 |\n| RegNet Y 1 6gf | 77.95 | 80.882 |\n| RegNet Y 3 2gf | 78.948 | 81.984 |\n| RegNet Y 8gf | 80.032 | 82.828 |\n| RegNet Y 16gf | 80.424 | 82.89 |\n| RegNet Y 32gf | 80.878 | 83.366 |\n| ResNet50 | 76.13 | 80.858 |\n| ResNet101 | 77.374 | 81.886 |\n| ResNet152 | 78.312 | 82.284 |\n| ResNeXt50 32x4d | 77.618 | 81.198 |\n| ResNeXt101 32x8d | 79.312 | 82.834 |\n| Wide ResNet50 2 | 78.468 | 81.602 |\n| Wide ResNet101 2 | 78.848 | 82.51 |", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} {"page_content": "Please spare a few minutes to provide your feedback on the new API, as this is crucial for graduating it from prototype and including it in the next release. You can do this on the dedicated [Github Issue](https://github.com/pytorch/vision/issues/5088). We are looking forward to reading your comments!", "metadata": {"source": "https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Optimizing CUDA Recurrent Neural Networks with TorchScript\"\nauthor: \"The PyTorch Team\"\ndate: 2019-05-01 8:00:00 -0500\n---\n\nThis week, we officially released PyTorch 1.1, a large feature update to PyTorch 1.0. One of the new features we've added is better support for fast, custom Recurrent Neural Networks (fastrnns) with TorchScript (the PyTorch JIT) (https://pytorch.org/docs/stable/jit.html).", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "RNNs are popular models that have shown good performance on a variety of NLP tasks that come in different shapes and sizes. PyTorch implements a number of the most popular ones, the [Elman RNN](https://pytorch.org/docs/master/nn.html?highlight=rnn#torch.nn.RNN), [GRU](https://pytorch.org/docs/master/nn.html?highlight=gru#torch.nn.GRU), and [LSTM](https://pytorch.org/docs/master/nn.html?highlight=lstm#torch.nn.LSTM) as well as multi-layered and bidirectional variants.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "However, many users want to implement their own custom RNNs, taking ideas from recent literature. Applying [Layer Normalization](https://arxiv.org/abs/1607.06450) to LSTMs is one such use case. Because the PyTorch CUDA LSTM implementation uses a fused kernel, it is difficult to insert normalizations or even modify the base LSTM implementation. Many users have turned to writing custom implementations using standard PyTorch operators, but such code suffers from high overhead: most PyTorch operations launch at", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "least one kernel on the GPU and RNNs generally run many operations due to their recurrent nature. However, we can apply TorchScript to fuse operations and optimize our code automatically, launching fewer, more optimized kernels on the GPU.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Our goal is for users to be able to write fast, custom RNNs in TorchScript without writing specialized CUDA kernels to achieve similar performance. In this post, we'll provide a tutorial for how to write your own fast RNNs with TorchScript. To better understand the optimizations TorchScript applies, we'll examine how those work on a standard LSTM implementation but most of the optimizations can be applied to general RNNs.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Writing custom RNNs\n\nTo get started, you can use [this file](https://github.com/pytorch/pytorch/blob/master/benchmarks/fastrnns/custom_lstms.py) as a template to write your own custom RNNs.\n\nWe are constantly improving our infrastructure on trying to make the performance better. If you want to gain the speed/optimizations that TorchScript currently provides (like operator fusion, batch matrix multiplications, etc.), here are some guidelines to follow. The next section explains the optimizations in depth.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "1. If the customized operations are all element-wise, that's great because you can get the benefits of the PyTorch JIT's operator fusion automatically!\n\n2. If you have more complex operations (e.g. reduce ops mixed with element-wise ops), consider grouping the reduce operations and element-wise ops separately in order to fuse the element-wise operations into a single fusion group.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "3. If you want to know about what has been fused in your custom RNN, you can inspect the operation's optimized graph by using `graph_for` . Using `LSTMCell` as an example:\n\n ```python\n # get inputs and states for LSTMCell\n\n inputs = get_lstm_inputs()\n\n # instantiate a ScriptModule\n\n cell = LSTMCell(input_size, hidden_size)\n\n # print the optimized graph using graph_for\n\n out = cell(inputs)\n print(cell.graph_for(inputs))", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "This will generate the optimized TorchScript graph (a.k.a PyTorch JIT IR) for the specialized inputs that you provides:", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "```\n graph(%x : Float(*, *),\n %hx : Float(*, *),\n %cx : Float(*, *),\n %w_ih : Float(*, *),\n %w_hh : Float(*, *),\n %b_ih : Float(*),\n %b_hh : Float(*)):\n %hy : Float(*, *), %cy : Float(*, *) = prim::DifferentiableGraph_0(%cx, %b_hh, %b_ih, %hx, %w_hh, %x, %w_ih)\n %30 : (Float(*, *), Float(*, *)) = prim::TupleConstruct(%hy, %cy)\n return (%30)\n with prim::DifferentiableGraph_0 = graph(%13 : Float(*, *),", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "%29 : Float(*),\n %33 : Float(*),\n %40 : Float(*, *),\n %43 : Float(*, *),\n %45 : Float(*, *),\n %48 : Float(*, *)):\n %49 : Float(*, *) = aten::t(%48)\n %47 : Float(*, *) = aten::mm(%45, %49)\n %44 : Float(*, *) = aten::t(%43)\n %42 : Float(*, *) = aten::mm(%40, %44)\n ...some broadcast sizes operations...", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "%hy : Float(*, *), %287 : Float(*, *), %cy : Float(*, *), %outgate.1 : Float(*, *), %cellgate.1 : Float(*, *), %forgetgate.1 : Float(*, *), %ingate.1 : Float(*, *) = prim::FusionGroup_0(%13, %346, %345, %344, %343)\n ...some broadcast sizes operations...\n return (%hy, %cy, %49, %44, %196, %199, %340, %192, %325, %185, %ingate.1, %forgetgate.1, %cellgate.1, %outgate.1, %395, %396, %287)\n with prim::FusionGroup_0 = graph(%13 : Float(*, *),\n %71 : Tensor,", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "%76 : Tensor,\n %81 : Tensor,\n %86 : Tensor):\n ...some chunks, constants, and add operations...\n %ingate.1 : Float(*, *) = aten::sigmoid(%38)\n %forgetgate.1 : Float(*, *) = aten::sigmoid(%34)\n %cellgate.1 : Float(*, *) = aten::tanh(%30)\n %outgate.1 : Float(*, *) = aten::sigmoid(%26)\n %14 : Float(*, *) = aten::mul(%forgetgate.1, %13)\n %11 : Float(*, *) = aten::mul(%ingate.1, %cellgate.1)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "%cy : Float(*, *) = aten::add(%14, %11, %69)\n %4 : Float(*, *) = aten::tanh(%cy)\n %hy : Float(*, *) = aten::mul(%outgate.1, %4)\n return (%hy, %4, %cy, %outgate.1, %cellgate.1, %forgetgate.1, %ingate.1)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "From the above graph we can see that it has a `prim::FusionGroup_0` subgraph that is fusing all element-wise operations in LSTMCell (transpose and matrix multiplication are not element-wise ops). Some graph nodes might be hard to understand in the first place but we will explain some of them in the optimization section, we also omitted some long verbose operators in this post that is there just for correctness.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Variable-length sequences best practices\n\nTorchScript does not support PackedSequence. Generally, when one is handling variable-length sequences, it is best to pad them into a single tensor and send that tensor through a TorchScript LSTM. Here's an example:\n\n```python\nsequences = [...] # List[Tensor], each Tensor is T' x C\npadded = torch.utils.rnn.pad_sequence(sequences)\nlengths = [seq.size(0) for seq in sequences]\npadded # T x N x C, where N is batch size and T is the max of all T'", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "model = LSTM(...)\noutput, hiddens = model(padded)\noutput # T x N x C", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Of course, `output` may have some garbage data in the padded regions; use `lengths` to keep track of which part you don't need.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Optimizations\n\nWe will now explain the optimizations performed by the PyTorch JIT to speed up custom RNNs. We will use a simple custom LSTM model in TorchScript to illustrate the optimizations, but many of these are general and apply to other RNNs. \n\nTo illustrate the optimizations we did and how we get benefits from those optimizations, we will run a simple custom LSTM model written in TorchScript (you can refer the code in the custom_lstm.py or the below code snippets) and time our changes.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "We set up the environment in a machine equipped with 2 Intel Xeon chip and one Nvidia P100, with cuDNN v7.3, CUDA 9.2 installed. The basic set up for the LSTM model is as follows:\n\n```\ninput_size = 512\nhidden_size = 512\nmini_batch = 64\nnumLayers = 1\nseq_length = 100", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "The most important thing PyTorch JIT did is to compile the python program to a PyTorch JIT IR, which is an intermediate representation used to model the program's graph structure. This IR can then benefit from whole program optimization, hardware acceleration and overall has the potential to provide large computation gains. In this example, we run the initial TorchScript model with only compiler optimization passes that are provided by the JIT, including common subexpression elimination, constant pooling,", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "constant propagation, dead code elimination and some peephole optimizations. We run the model training for 100 times after warm up and average the training time. The initial results for model forward time is around 27ms and backward time is around 64ms, which is a bit far away from what PyTorch cuDNN LSTM provided. Next we will explain the major optimizations we did on how we improve the performance on training or inferencing, starting with LSTMCell and LSTMLayer, and some misc optimizations.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "LSTM Cell (forward)\n\nAlmost all the computations in an LSTM happen in the LSTMCell, so it's important for us to take a look at the computations it contains and how can we improve their speed. Below is a sample LSTMCell implementation in TorchScript:", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "```python\nclass LSTMCell(jit.ScriptModule):\n def __init__(self, input_size, hidden_size):\n super(LSTMCell, self).__init__()\n self.input_size = input_size\n self.hidden_size = hidden_size\n self.weight_ih = Parameter(torch.randn(4 * hidden_size, input_size))\n self.weight_hh = Parameter(torch.randn(4 * hidden_size, hidden_size))\n self.bias_ih = Parameter(torch.randn(4 * hidden_size))\n self.bias_hh = Parameter(torch.randn(4 * hidden_size))", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "@jit.script_method\n def forward(self, input, state):\n # type: (Tensor, Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]\n hx, cx = state\n gates = (torch.mm(input, self.weight_ih.t()) + self.bias_ih +\n torch.mm(hx, self.weight_hh.t()) + self.bias_hh)\n ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "ingate = torch.sigmoid(ingate)\n forgetgate = torch.sigmoid(forgetgate)\n cellgate = torch.tanh(cellgate)\n outgate = torch.sigmoid(outgate)\n\n cy = (forgetgate * cx) + (ingate * cellgate)\n hy = outgate * torch.tanh(cy)\n\n return hy, (hy, cy)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "This graph representation (IR) that TorchScript generated enables several optimizations and scalable computations. In addition to the typical compiler optimizations that we could do (CSE, constant propagation, etc. ) we can also run other IR transformations to make our code run faster.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Element-wise operator fusion. PyTorch JIT will automatically fuse element-wise ops, so when you have adjacent operators that are all element-wise, JIT will automatically group all those operations together into a single FusionGroup, this FusionGroup can then be launched with a single GPU/CPU kernel and performed in one pass. This avoids expensive memory reads and writes for each operation.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Reordering chunks and pointwise ops to enable more fusion. An LSTM cell adds gates together (a pointwise operation), and then chunks the gates into four pieces: the ifco gates. Then, it performs pointwise operations on the ifco gates like above. This leads to two fusion groups in practice: one fusion group for the element-wise ops pre-chunk, and one group for the element-wise ops post-chunk.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "The interesting thing to note here is that pointwise operations commute with `torch.chunk`: Instead of performing pointwise ops on some input tensors and chunking the output, we can chunk the input tensors and then perform the same pointwise ops on the output tensors. By moving the chunk to before the first fusion group, we can merge the first and second fusion groups into one big group.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Optimizing CUDA Recurrent Neural Networks with TorchScript\"\nauthor: \"The PyTorch Team\"\ndate: 2019-05-01 8:00:00 -0500\n---\n\nThis week, we officially released PyTorch 1.1, a large feature update to PyTorch 1.0. One of the new features we've added is better support for fast, custom Recurrent Neural Networks (fastrnns) with TorchScript (the PyTorch JIT) (https://pytorch.org/docs/stable/jit.html). \n\nRNNs are popular models that have shown good performance on a variety of NLP tasks that come in different shapes and sizes. PyTorch implements a number of the most popular ones, the [Elman RNN](https://pytorch.org/docs/master/nn.html?highlight=rnn#torch.nn.RNN), [GRU](https://pytorch.org/docs/master/nn.html?highlight=gru#torch.nn.GRU), and [LSTM](https://pytorch.org/docs/master/nn.html?highlight=lstm#torch.nn.LSTM) as well as multi-layered and bidirectional variants.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "However, many users want to implement their own custom RNNs, taking ideas from recent literature. Applying [Layer Normalization](https://arxiv.org/abs/1607.06450) to LSTMs is one such use case. Because the PyTorch CUDA LSTM implementation uses a fused kernel, it is difficult to insert normalizations or even modify the base LSTM implementation. Many users have turned to writing custom implementations using standard PyTorch operators, but such code suffers from high overhead: most PyTorch operations launch at least one kernel on the GPU and RNNs generally run many operations due to their recurrent nature. However, we can apply TorchScript to fuse operations and optimize our code automatically, launching fewer, more optimized kernels on the GPU.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "Our goal is for users to be able to write fast, custom RNNs in TorchScript without writing specialized CUDA kernels to achieve similar performance. In this post, we'll provide a tutorial for how to write your own fast RNNs with TorchScript. To better understand the optimizations TorchScript applies, we'll examine how those work on a standard LSTM implementation but most of the optimizations can be applied to general RNNs.\n\n## Writing custom RNNs\n\nTo get started, you can use [this file](https://github.com/pytorch/pytorch/blob/master/benchmarks/fastrnns/custom_lstms.py) as a template to write your own custom RNNs.\n\nWe are constantly improving our infrastructure on trying to make the performance better. If you want to gain the speed/optimizations that TorchScript currently provides (like operator fusion, batch matrix multiplications, etc.), here are some guidelines to follow. The next section explains the optimizations in depth.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "1. If the customized operations are all element-wise, that's great because you can get the benefits of the PyTorch JIT's operator fusion automatically!\n\n2. If you have more complex operations (e.g. reduce ops mixed with element-wise ops), consider grouping the reduce operations and element-wise ops separately in order to fuse the element-wise operations into a single fusion group. \n\n3. If you want to know about what has been fused in your custom RNN, you can inspect the operation's optimized graph by using `graph_for` . Using `LSTMCell` as an example:\n\n ```python\n # get inputs and states for LSTMCell\n\n inputs = get_lstm_inputs()\n\n # instantiate a ScriptModule\n\n cell = LSTMCell(input_size, hidden_size)\n\n # print the optimized graph using graph_for\n\n out = cell(inputs)\n print(cell.graph_for(inputs))\n\n ```\n\n This will generate the optimized TorchScript graph (a.k.a PyTorch JIT IR) for the specialized inputs that you provides:", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "```\n graph(%x : Float(*, *),\n %hx : Float(*, *),\n %cx : Float(*, *),\n %w_ih : Float(*, *),\n %w_hh : Float(*, *),\n %b_ih : Float(*),\n %b_hh : Float(*)):\n %hy : Float(*, *), %cy : Float(*, *) = prim::DifferentiableGraph_0(%cx, %b_hh, %b_ih, %hx, %w_hh, %x, %w_ih)\n %30 : (Float(*, *), Float(*, *)) = prim::TupleConstruct(%hy, %cy)\n return (%30)\n with prim::DifferentiableGraph_0 = graph(%13 : Float(*, *),\n %29 : Float(*),\n %33 : Float(*),\n %40 : Float(*, *),\n %43 : Float(*, *),\n %45 : Float(*, *),\n %48 : Float(*, *)):\n %49 : Float(*, *) = aten::t(%48)\n %47 : Float(*, *) = aten::mm(%45, %49)\n %44 : Float(*, *) = aten::t(%43)\n %42 : Float(*, *) = aten::mm(%40, %44)\n ...some broadcast sizes operations...\n %hy : Float(*, *), %287 : Float(*, *), %cy : Float(*, *), %outgate.1 : Float(*, *), %cellgate.1 : Float(*, *), %forgetgate.1 : Float(*, *), %ingate.1 : Float(*, *) = prim::FusionGroup_0(%13, %346, %345, %344, %343)\n ...some broadcast sizes operations...\n return (%hy, %cy, %49, %44, %196, %199, %340, %192, %325, %185, %ingate.1, %forgetgate.1, %cellgate.1, %outgate.1, %395, %396, %287)\n with prim::FusionGroup_0 = graph(%13 : Float(*, *),\n %71 : Tensor,\n %76 : Tensor,\n %81 : Tensor,\n %86 : Tensor):\n ...some chunks, constants, and add operations...\n %ingate.1 : Float(*, *) = aten::sigmoid(%38)\n %forgetgate.1 : Float(*, *) = aten::sigmoid(%34)\n %cellgate.1 : Float(*, *) = aten::tanh(%30)\n %outgate.1 : Float(*, *) = aten::sigmoid(%26)\n %14 : Float(*, *) = aten::mul(%forgetgate.1, %13)\n %11 : Float(*, *) = aten::mul(%ingate.1, %cellgate.1)\n %cy : Float(*, *) = aten::add(%14, %11, %69)\n %4 : Float(*, *) = aten::tanh(%cy)\n %hy : Float(*, *) = aten::mul(%outgate.1, %4)\n return (%hy, %4, %cy, %outgate.1, %cellgate.1, %forgetgate.1, %ingate.1)\n ```", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "From the above graph we can see that it has a `prim::FusionGroup_0` subgraph that is fusing all element-wise operations in LSTMCell (transpose and matrix multiplication are not element-wise ops). Some graph nodes might be hard to understand in the first place but we will explain some of them in the optimization section, we also omitted some long verbose operators in this post that is there just for correctness. \n\n## Variable-length sequences best practices\n\nTorchScript does not support PackedSequence. Generally, when one is handling variable-length sequences, it is best to pad them into a single tensor and send that tensor through a TorchScript LSTM. Here's an example:\n\n```python\nsequences = [...] # List[Tensor], each Tensor is T' x C\npadded = torch.utils.rnn.pad_sequence(sequences)\nlengths = [seq.size(0) for seq in sequences]\npadded # T x N x C, where N is batch size and T is the max of all T'\n\nmodel = LSTM(...)\noutput, hiddens = model(padded)\noutput # T x N x C\n```", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "Of course, `output` may have some garbage data in the padded regions; use `lengths` to keep track of which part you don't need.\n\n## Optimizations\n\nWe will now explain the optimizations performed by the PyTorch JIT to speed up custom RNNs. We will use a simple custom LSTM model in TorchScript to illustrate the optimizations, but many of these are general and apply to other RNNs. \n\nTo illustrate the optimizations we did and how we get benefits from those optimizations, we will run a simple custom LSTM model written in TorchScript (you can refer the code in the custom_lstm.py or the below code snippets) and time our changes.\n\nWe set up the environment in a machine equipped with 2 Intel Xeon chip and one Nvidia P100, with cuDNN v7.3, CUDA 9.2 installed. The basic set up for the LSTM model is as follows:\n\n```\ninput_size = 512\nhidden_size = 512\nmini_batch = 64\nnumLayers = 1\nseq_length = 100 \n```", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "The most important thing PyTorch JIT did is to compile the python program to a PyTorch JIT IR, which is an intermediate representation used to model the program's graph structure. This IR can then benefit from whole program optimization, hardware acceleration and overall has the potential to provide large computation gains. In this example, we run the initial TorchScript model with only compiler optimization passes that are provided by the JIT, including common subexpression elimination, constant pooling, constant propagation, dead code elimination and some peephole optimizations. We run the model training for 100 times after warm up and average the training time. The initial results for model forward time is around 27ms and backward time is around 64ms, which is a bit far away from what PyTorch cuDNN LSTM provided. Next we will explain the major optimizations we did on how we improve the performance on training or inferencing, starting with LSTMCell and LSTMLayer, and some misc optimizations.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "### LSTM Cell (forward)\n\nAlmost all the computations in an LSTM happen in the LSTMCell, so it's important for us to take a look at the computations it contains and how can we improve their speed. Below is a sample LSTMCell implementation in TorchScript:\n\n```python\nclass LSTMCell(jit.ScriptModule):\n def __init__(self, input_size, hidden_size):\n super(LSTMCell, self).__init__()\n self.input_size = input_size\n self.hidden_size = hidden_size\n self.weight_ih = Parameter(torch.randn(4 * hidden_size, input_size))\n self.weight_hh = Parameter(torch.randn(4 * hidden_size, hidden_size))\n self.bias_ih = Parameter(torch.randn(4 * hidden_size))\n self.bias_hh = Parameter(torch.randn(4 * hidden_size))", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "@jit.script_method\n def forward(self, input, state):\n # type: (Tensor, Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]\n hx, cx = state\n gates = (torch.mm(input, self.weight_ih.t()) + self.bias_ih +\n torch.mm(hx, self.weight_hh.t()) + self.bias_hh)\n ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)\n\n ingate = torch.sigmoid(ingate)\n forgetgate = torch.sigmoid(forgetgate)\n cellgate = torch.tanh(cellgate)\n outgate = torch.sigmoid(outgate)\n\n cy = (forgetgate * cx) + (ingate * cellgate)\n hy = outgate * torch.tanh(cy)\n\n return hy, (hy, cy)\n```\n\n\nThis graph representation (IR) that TorchScript generated enables several optimizations and scalable computations. In addition to the typical compiler optimizations that we could do (CSE, constant propagation, etc. ) we can also run other IR transformations to make our code run faster.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "* Element-wise operator fusion. PyTorch JIT will automatically fuse element-wise ops, so when you have adjacent operators that are all element-wise, JIT will automatically group all those operations together into a single FusionGroup, this FusionGroup can then be launched with a single GPU/CPU kernel and performed in one pass. This avoids expensive memory reads and writes for each operation.\n* Reordering chunks and pointwise ops to enable more fusion. An LSTM cell adds gates together (a pointwise operation), and then chunks the gates into four pieces: the ifco gates. Then, it performs pointwise operations on the ifco gates like above. This leads to two fusion groups in practice: one fusion group for the element-wise ops pre-chunk, and one group for the element-wise ops post-chunk.\n The interesting thing to note here is that pointwise operations commute with `torch.chunk`: Instead of performing pointwise ops on some input tensors and chunking the output, we can chunk the input tensors and then perform the same pointwise ops on the output tensors. By moving the chunk to before the first fusion group, we can merge the first and second fusion groups into one big group.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} {"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Tensor creation on the CPU is expensive, but there is ongoing work to make it faster. At this point, a LSTMCell runs three CUDA kernels: two `gemm` kernels and one for the single pointwise group. One of the things we noticed was that there was a large gap between the finish of the second `gemm` and the start of the single pointwise group. This gap was a period of time when the GPU was idling around and not doing anything. Looking into it more, we discovered that the problem was that `torch.chunk`", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "constructs new tensors and that tensor construction was not as fast as it could be. Instead of constructing new Tensor objects, we taught the fusion compiler how to manipulate a data pointer and strides to do the `torch.chunk` before sending it into the fused kernel, shrinking the amount of idle time between the second gemm and the launch of the element-wise fusion group. This give us around 1.2x increase speed up on the LSTM forward pass.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "By doing the above tricks, we are able to fuse the almost all `LSTMCell` forward graph (except the two gemm kernels) into a single fusion group, which corresponds to the `prim::FusionGroup_0` in the above IR graph. It will then be launched into a single fused kernel for execution. With these optimizations the model performance improves significantly with average forward time reduced by around 17ms (1.7x speedup) to 10ms, and average backward time reduce by 37ms to 27ms (1.37x speed up).", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "LSTM Layer (forward)\n\n```python\nclass LSTMLayer(jit.ScriptModule):\n def __init__(self, cell, *cell_args):\n super(LSTMLayer, self).__init__()\n self.cell = cell(*cell_args)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "@jit.script_method\n def forward(self, input, state):\n # type: (Tensor, Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]\n inputs = input.unbind(0)\n outputs = torch.jit.annotate(List[Tensor], [])\n for i in range(len(inputs)):\n out, state = self.cell(inputs[i], state)\n outputs += [out]\n return torch.stack(outputs), state", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "We did several tricks on the IR we generated for TorchScript LSTM to boost the performance, some example optimizations we did:", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Loop Unrolling: We automatically unroll loops in the code (for big loops, we unroll a small subset of it), which then empowers us to do further optimizations on the for loops control flow. For example, the fuser can fuse together operations across iterations of the loop body, which results in a good performance improvement for control flow intensive models like LSTMs.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Batch Matrix Multiplication: For RNNs where the input is pre-multiplied (i.e. the model has a lot of matrix multiplies with the same LHS or RHS), we can efficiently batch those operations together into a single matrix multiply while chunking the outputs to achieve equivalent semantics.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "By applying these techniques, we reduced our time in the forward pass by an additional 1.6ms to 8.4ms (1.2x speed up) and timing in backward by 7ms to around 20ms (1.35x speed up).", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "LSTM Layer (backward)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* \u201cTree\u201d Batch Matrix Muplication: It is often the case that a single weight is reused multiple times in the LSTM backward graph, forming a tree where the leaves are matrix multiplies and nodes are adds. These nodes can be combined together by concatenating the LHSs and RHSs in different dimensions, then computed as a single matrix multiplication. The formula of equivalence can be denoted as follows:\n \n $L1 * R1 + L2 * R2 = torch.cat((L1, L2), dim=1) * torch.cat((R1, R2), dim=0)$", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Autograd is a critical component of what makes PyTorch such an elegant ML framework. As such, we carried this through to PyTorch JIT, but using a new **Automatic Differentiation** (AD) mechanism that works on the IR level. JIT automatic differentiation will slice the forward graph into symbolically differentiable subgraphs, and generate backwards nodes for those subgraphs. Taking the above IR as an example, we group the graph nodes into a single `prim::DifferentiableGraph_0` for the operations that has", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "AD formulas. For operations that have not been added to AD formulas, we will fall back to Autograd during execution.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Optimizing the backwards path is hard, and the implicit broadcasting semantics make the optimization of automatic differentiation harder. PyTorch makes it convenient to write tensor operations without worrying about the shapes by broadcasting the tensors for you. For performance, the painful point in backward is that we need to have a summation for such kind of broadcastable operations. This results in the derivative of every broadcastable op being followed by a summation. Since we cannot currently fuse", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "reduce operations, this causes FusionGroups to break into multiple small groups leading to bad performance. To deal with this, refer to this great [post](http://lernapparat.de/fast-lstm-pytorch/) written by Thomas Viehmann.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "Misc Optimizations", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* In addition to the steps laid about above, we also eliminated overhead between CUDA kernel launches and unnecessary tensor allocations. One example is when you do a tensor device look up. This can provide some poor performance initially with a lot of unnecessary allocations. When we remove these this results in a reduction from milliseconds to nanoseconds between kernel launches.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "* Lastly, there might be normalization applied in the custom LSTMCell like LayerNorm. Since LayerNorm and other normalization ops contains reduce operations, it is hard to fuse it in its entirety. Instead, we automatically decompose Layernorm to a statistics computation (reduce operations) + element-wise transformations, and then fuse those element-wise parts together. As of this post, there are some limitations on our auto differentiation and graph fuser infrastructure which limits the current support to", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "inference mode only. We plan to add backward support in a future release.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "With the above optimizations on operation fusion, loop unrolling, batch matrix multiplication and some misc optimizations, we can see a clear performance increase on our custom TorchScript LSTM forward and backward from the following figure: \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "There are a number of additional optimizations that we did not cover in this post. In addition to the ones laid out in this post, we now see that our custom LSTM forward pass is on par with cuDNN. We are also working on optimizing backward more and expect to see improvements in future releases. Besides the speed that TorchScript provides, we introduced a much more flexible API that enable you to hand draft a lot more custom RNNs, which cuDNN could not provide.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch, a year in....\"\nauthor: \"The PyTorch Team\"\ndate: 2018-01-19 12:00:00 -0500\nredirect_from: /2018/01/19/a-year-in.html\n---\n\nToday marks 1 year since PyTorch was released publicly. It's been a wild ride \u2014 our quest to build a flexible deep learning research platform. Over the last year, we've seen an amazing community of people using, contributing to and evangelizing PyTorch \u2014 thank you for the love.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Looking back, we wanted to summarize PyTorch over the past year: the progress, the news and highlights from the community.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Community\n\nWe've been blessed with a strong organic community of researchers and engineers who fell in love with PyTorch. The core team has engineers and researchers from multiple countries, companies and universities, and we couldn't have made PyTorch what it is without each contribution.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Research papers, packages and Github\n\nWithin days of release, users from the community started to implement their favorite research papers in PyTorch and release the code on Github. Open-source code is a primary and essential tool for researchers today.\n\nFolks came together to create [torchtext](https://github.com/pytorch/text), [torchvision](https://github.com/pytorch/vision) and [torchaudio](https://github.com/pytorch/audio) packages to help facilitate and democratize research in different domains.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "The first community package based on PyTorch came from Brandon Amos, [titled Block](https://twitter.com/brandondamos/status/828652480573607937), and helped with easier manipulation of block matrices. The Locus Lab at **CMU** subsequently went on to [publish PyTorch packages](https://github.com/locuslab) and implementations for most of their research. The first research paper code came from Sergey Zagoruyko titled [Paying more attention to attention](https://twitter.com/PyTorch/status/822561885744726016).", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Jun-Yan Zhu, Taesung Park, Phillip Isola, Alyosha Efros and team from **U.C.Berkeley** released the hugely popular [Cycle-GAN and pix2pix](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix) which does image to image transforms.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "The researchers at **HarvardNLP** and **Systran** started developing and improving [OpenNMT in PyTorch](https://github.com/OpenNMT/OpenNMT-py), seeded by initial reimplementation of the [Lua]Torch code from Adam Lerer at Facebook.\n\nThe MagicPony team at **Twitter** contributed implementations of their [Super-resolution work early on into PyTorch's examples](https://twitter.com/Rob_Bishop/status/821793080877588480).", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "**Salesforce Research** released several packages, including their highlight release of [PyTorch-QRNN](https://twitter.com/Smerity/status/917472260851560448), a type of RNN that is 2x to 17x faster than standard LSTMs optimized by CuDNN. James Bradbury and team form one of the most active and engaging forces in the PyTorch community.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "

We're releasing @PyTorch-QRNN, 2-17x faster than NVIDIA's cuDNN LSTM.
Speed thanks to 50 lines of CUDA via CuPy.https://t.co/KaWhN4yDZd pic.twitter.com/yoLYj3pMI0

— Smerity (@Smerity) October 9,", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "2017
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Researchers from **Uber**, **Northeastern** and **Stanford** came together to form an active probabilistic programming community around their packages [Pyro](http://pyro.ai/) and [ProbTorch](https://github.com/probtorch/probtorch). They are actively developing the torch.distributions core package. This community is so active and fast-moving, we had our first pytorch-probabilistic-programming meetup at NIPS 2017 with Fritz Obermeyer, Noah Goodman, Jan-Willem van de Meent, Brooks Paige, Dustin Tran and 22", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "additional attendees discussing how to make the world bayesian.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "**NVIDIA** Researchers released three high-quality repositories that implemented [pix2pix-HD](https://github.com/NVIDIA/pix2pixHD), [Sentiment Neuron](https://github.com/NVIDIA/sentiment-discovery) and [FlowNet2](https://github.com/NVIDIA/flownet2-pytorch) papers. Their analysis of scalability of different [Data Parallel models in PyTorch](https://github.com/NVIDIA/sentiment-discovery/blob/master/analysis/scale.md) was helpful to the community.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\nThe Allen Institute for AI released [AllenNLP](http://allennlp.org/) which includes several state-of-the-art models in NLP \u2014 reference implementations and easy to use [web demos](http://demo.allennlp.org/machine-comprehension) for standard NLP tasks.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "We also had our first Kaggle winning team grt123 in July. They won the DataScience Bowl 2017 on Lung Cancer detection and [subsequently released their PyTorch implementations](https://twitter.com/PyTorch/status/881573658166267904).\n\nOn the visualization front, Tzu-Wei Huang implemented a [TensorBoard-PyTorch plugin](https://github.com/lanpa/tensorboard-pytorch) and Facebook AI Research released PyTorch compatibility for their [visdom](https://github.com/facebookresearch/visdom) visualization package.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "
\n \n \n
\n\nLastly, **Facebook AI Research** released several projects such as [ParlAI, fairseq-py, VoiceLoop and FaderNetworks](https://github.com/facebookresearch/) that implemented cutting-edge models and interfaced datasets in multiple domains.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "There are countless good projects that we haven't highlighted for the lack of space, you can find a curated list [here](https://github.com/soumith?tab=stars).", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "We would also like to give a huge shout-out to folks who actively help others out on the Forums, especially [ptrblck](https://discuss.pytorch.org/u/ptrblck/summary), [jpeg729](https://discuss.pytorch.org/u/jpeg729/summary), [QuantScientist](https://discuss.pytorch.org/u/quantscientist/summary), [albanD](https://discuss.pytorch.org/u/alband/summary), [Thomas Viehmann](https://discuss.pytorch.org/u/tom/summary) and [chenyuntc](https://discuss.pytorch.org/u/chenyuntc/summary). You are providing an invaluable", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "service, thank you so much!", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Metrics\n\nIn terms of sheer numbers,", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "* 87,769 lines of Python code on github that [import torch](https://github.com/search?l=Python&q=import+torch&type=Code)\n* [3,983 repositories on Github that mention PyTorch in their name or description](https://github.com/search?q=pytorch&type=Repositories)\n* More than half a million downloads of PyTorch binaries. 651,916 to be precise.\n* **5,400 users** wrote **21,500 posts** discussing 5,200 topics on our forums discuss.pytorch.org (http://discuss.pytorch.org/)", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "* 131 mentions of PyTorch on Reddit's /r/machinelearning since the day of release. In the same period, TensorFlow was mentioned 255 times.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Research Metrics\n\nPyTorch is a research-focused framework. So one of the metrics of interest is to see the usage of PyTorch in machine learning research papers.\n\n\n* In the recent ICLR2018 conference submissions, PyTorch was mentioned in **87 papers**, compared to TensorFlow at 228 papers, Keras at 42 papers, Theano and Matlab at 32 papers.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "* [Monthly arxiv.org mentions for frameworks](https://twitter.com/fchollet/status/951828914103402497) had PyTorch at 72 mentions, with TensorFlow at 273 mentions, Keras at 100 mentions, Caffe at 94 mentions and Theano at 53 mentions.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Courses, Tutorials and Books\n\nWhen we released PyTorch, we had good API documentation, but our tutorials were limited to a few ipython notebooks \u2014 helpful, but not good enough.\n\n[Sasank Chilamkurthy](https://github.com/chsasank) took it upon himself to revamp the tutorials into the [beautiful website](https://pytorch.org/tutorials/) that it is today.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "[Sean Robertson](https://github.com/spro/practical-pytorch) and [Justin Johnson](https://github.com/jcjohnson/pytorch-examples) wrote great new tutorials \u2014 in NLP, and to learn by example. [Yunjey Choi](https://github.com/yunjey/pytorch-tutorial) wrote a beautiful tutorial where most models were implemented in 30 lines or less.\nEach new tutorial helped users find their way faster, with different approaches to learning.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "[Goku Mohandas and Delip Rao](https://twitter.com/PyTorch/status/888500355943641088) switched the code content of their book-in-progress to use PyTorch.\n\nWe've seen quite a few university machine learning courses being taught with PyTorch as the primary tool, such as Harvard's [CS287](https://harvard-ml-courses.github.io/cs287-web/). Taking it one step further and democratizing learning, we had three online courses pop up that teach using PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "- **Fast.ai's** \u201cDeep Learning for Coders\u201d is a popular online course. In September, Jeremy and Rachel [announced that the next fast.ai courses will be nearly entirely based on PyTorch](http://www.fast.ai/2017/09/08/introducing-pytorch-for-fastai/).\n- Ritchie Ng, a researcher with ties to NUS Singapore and Tsinghua released [a Udemy course](https://www.udemy.com/practical-deep-learning-with-pytorch/) titled Practical Deep Learning with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "- Sung Kim from HKUST released an [online course on Youtube](https://www.youtube.com/playlist?list=PLlMkM4tgfjnJ3I-dbhO9JTw7gNty6o_2m) that was aimed towards a general audience, titled: \u201cPyTorch Zero to All\u201d.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Engineering\n\nOver the last year we implemented multiple features, improved performance across the board and fixed lots of bugs. A full list of the work we've done is found in our [release notes](https://github.com/pytorch/pytorch/releases).\nHere are highlights from our work over the last year:", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Higher-order gradients\n\n With the release of several papers that implement penalties of gradients and with ongoing research in 2nd order gradient methods, this was an essential and sought-after feature. In August, we implemented a generalized interface that can take n-th order derivatives and increased the coverage of functions that support higher-order gradients over time, such that at the moment of writing almost all ops support this.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Distributed PyTorch\n\nIn August, we released a small distributed package that followed the highly popular MPI-collective approach. The package has multiple backends such as TCP, MPI, Gloo and NCCL2 to support various types of CPU/GPU collective operations and use-cases, and integrates distributed technologies such as Infiniband and RoCE. Distributed is hard, and we had bugs in the initial iteration. Over subsequent releases, we made the package more stable and improved performance.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Closer to NumPy\n\nOne of the biggest demands from users were NumPy features that they were familiar with. Features such as Broadcasting and Advanced Indexing are convenient and save users a lot of verbosity. We implemented these features and started to align our API to be closer to NumPy. Over time, we expect to get closer and closer to NumPy's API where appropriate.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Sparse Tensors\n\nIn March, we released a small package supporting sparse Tensors and in May we released CUDA support for the sparse package. The package is small and limited in functionality, and is used for implementing Sparse Embeddings and commonly used sparse paradigms in deep learning. This package is still small in scope and there's demand to expand it \u2014 if you are interested in working on expanding the sparse package, reach out to us on our [Discussion Boards](https://discuss.pytorch.org/)", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Performance\n\nPerformance is always an ongoing battle, especially for PyTorch which is a dynamic framework that wants to maximize flexibility. Over the last year, we've improved performance across board, from our core Tensor library to the neural network operators, writing faster micro-optimized across board.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "* We've added specialized AVX and AVX2 intrinsics for Tensor operations\n* Wrote faster GPU kernels for frequent workloads like concatenation and Softmax (among many other things)\n* Rewrote the code for several neural network operators (too many to list), but notably nn.Embedding and group convolutions.\n\n**Reducing framework overhead by 10x across board**", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Since PyTorch is a dynamic graph framework, we create a new graph on the fly at every iteration of a training loop. Hence, the framework overhead has to be low, or the workload has to be large enough that the framework overhead is hidden. In August, the authors of DyNet (Graham Neubig and team) showcased that it's much faster than PyTorch on small NLP models. This was an interesting challenge, we didn't realize that models of those sizes were being trained. In a multi-month (and ongoing) effort, we embarked", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "upon a significant rewrite of PyTorch internals that reduced the framework overhead from more than 10 microseconds per operator execution to as little as 1 microsecond.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "**ATen**\n\nAs we embarked upon a redesign of the PyTorch internals, we built the [ATen C++11](https://github.com/pytorch/pytorch/tree/master/aten) library that now powers all of the PyTorch backend. ATen has an API that mirrors PyTorch's Python API, which makes it a convenient C++ library for Tensor computation. ATen can be built and used independently of PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Exporting models to production \u2014 ONNX Support and the JIT compiler\n\nOne of the common requests we've received was to export PyTorch models to another framework. Users engaged in a rapid research cycle in PyTorch and when they were done, they wanted to ship it to larger projects with C++ only requirements.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "With this in mind, we built a tracer for PyTorch \u2014 which can export PyTorch models into an intermediate representation.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "The subsequent trace can be either used to run the current PyTorch model more efficiently (by running optimization passes on it), or be converted to the [ONNX](http://onnx.ai/) format to be shipped to other frameworks such as Caffe2, MXNet, TensorFlow and others or directly to the hardware accelerated libraries like CoreML or TensorRT. Over the next year, you will hear more about the JIT compiler for performance improvements.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "Users being funny :)\n\nOur users express their support in funny ways, made us laugh, thanks for this :)", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "

I've been using PyTorch a few months now and I've never felt better. I have more energy. My skin is clearer. My eye sight has improved.

— Andrej Karpathy (@karpathy) May 26, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "

Talk to your doctor to find out if PyTorch is right for you.

— Sean Robertson (@sprobertson) May 26, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "

PyTorch gave me so much life that my skin got cleared, my grades are up, my bills are paid and my crops are watered.

— Adam Will \u00f0\ufe0f\u200d\u00f0 (@adam_will_do_it) May 26, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "

So have I! But my hair is also shiner and I've lost weight. @PyTorch for the win. https://t.co/qgU4oIOB4K

— Mariya (@thinkmariya) May 26, 2017
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch for AMD ROCm\u2122 Platform now available as Python package'\nauthor: Niles Burbank \u2013 Director PM at AMD, Mayank Daga \u2013 Director, Deep Learning Software at AMD\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "With the PyTorch 1.8 release, we are delighted to announce a new installation option for users of\nPyTorch on the ROCm\u2122 open software platform. An installable Python package is now hosted on\npytorch.org, along with instructions for local installation in the same simple, selectable format as\nPyTorch packages for CPU-only configurations and other GPU platforms. PyTorch on ROCm includes full\ncapability for mixed-precision and large-scale training using AMD\u2019s MIOpen & RCCL libraries. This", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "provides a new option for data scientists, researchers, students, and others in the community to get\nstarted with accelerated PyTorch using AMD GPUs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "The ROCm Ecosystem", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "ROCm is AMD\u2019s open source software platform for GPU-accelerated high performance computing and\nmachine learning. Since the original ROCm release in 2016, the ROCm platform has evolved to support\nadditional libraries and tools, a wider set of Linux\u00ae distributions, and a range of new GPUs. This includes\nthe AMD Instinct\u2122 MI100, the first GPU based on AMD CDNA\u2122 architecture. \n \nThe ROCm ecosystem has an established history of support for PyTorch, which was initially implemented", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "as a fork of the PyTorch project, and more recently through ROCm support in the upstream PyTorch\ncode. PyTorch users can install PyTorch for ROCm using AMD\u2019s public PyTorch docker image, and can of\ncourse build PyTorch for ROCm from source. With PyTorch 1.8, these existing installation options are\nnow complemented by the availability of an installable Python package.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "The primary focus of ROCm has always been high performance computing at scale. The combined\ncapabilities of ROCm and AMD\u2019s Instinct family of data center GPUs are particularly suited to the\nchallenges of HPC at data center scale. PyTorch is a natural fit for this environment, as HPC and ML\nworkflows become more intertwined.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "Getting started with PyTorch for ROCm", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "The scope for this build of PyTorch is AMD GPUs with ROCm support, running on Linux. The GPUs\nsupported by ROCm include all of AMD\u2019s Instinct family of compute-focused data center GPUs, along\nwith some other select GPUs. A current list of supported GPUs can be found in the [ROCm Github\nrepository](https://github.com/RadeonOpenCompute/ROCm#supported-gpus). After confirming that the target system includes supported GPUs and the current 4.0.1", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "release of ROCm, installation of PyTorch follows the same simple Pip-based installation as any other\nPython package. As with PyTorch builds for other platforms, the configurator at [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/) provides the specific command line to be run.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "PyTorch for ROCm is built from the upstream PyTorch repository, and is a full featured implementation.\nNotably, it includes support for distributed training across multiple GPUs and supports accelerated\nmixed precision training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "More information", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "A list of ROCm supported GPUs and operating systems can be found at\n[https://github.com/RadeonOpenCompute/ROCm](https://github.com/RadeonOpenCompute/ROCm)\nGeneral documentation on the ROCm platform is available at [https://rocmdocs.amd.com/en/latest/](https://rocmdocs.amd.com/en/latest/)", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "ROCm Learning Center at [https://developer.amd.com/resources/rocm-resources/rocm-learning-center/](https://developer.amd.com/resources/rocm-resources/rocm-learning-center/) General information on AMD\u2019s offerings for HPC and ML can be found at [https://amd.com/hpc](https://amd.com/hpc)", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "Feedback\nAn engaged user base is a tremendously important part of the PyTorch ecosystem. We would be deeply\nappreciative of feedback on the PyTorch for ROCm experience in the [PyTorch discussion forum](https://discuss.pytorch.org/) and, where appropriate, reporting any issues via [Github](https://github.com/pytorch/pytorch).", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"A Tour of PyTorch Internals (Part I)\"\nauthor: \"Trevor Killeen\"\ndate: 2017-05-11 12:00:00 -0500\nredirect_from: /2017/05/11/Internals.html\n---\n\nThe fundamental unit in PyTorch is the Tensor. This post will serve as an overview for how we implement Tensors in PyTorch, such that the user can interact with it from the Python shell. In particular, we want to answer four main questions:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "- How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?\n- How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?\n- How does PyTorch cwrap work to generate code for Tensor methods?\n- How does PyTorch's build system take all of these components to compile and generate a workable application?", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Extending the Python Interpreter\n\nPyTorch defines a new package `torch`. In this post we will consider the `._C` module. This module is known as an \"extension module\" - a Python module written in C. Such modules allow us to define new built-in object types (e.g. the `Tensor`) and to call C/C++ functions.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The `._C` module is defined in `torch/csrc/Module.cpp`. The `init_C()` / `PyInit__C()` function creates the module and adds the method definitions as appropriate. This module is passed around to a number of different `__init()` functions that add further objects to the module, register new types, etc.\n\nOne collection of these `__init()` calls is the following:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "```cpp\nASSERT_TRUE(THPDoubleTensor_init(module));\nASSERT_TRUE(THPFloatTensor_init(module));\nASSERT_TRUE(THPHalfTensor_init(module));\nASSERT_TRUE(THPLongTensor_init(module));\nASSERT_TRUE(THPIntTensor_init(module));\nASSERT_TRUE(THPShortTensor_init(module));\nASSERT_TRUE(THPCharTensor_init(module));\nASSERT_TRUE(THPByteTensor_init(module));", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "These `__init()` functions add the Tensor object for each type to the `._C` module so that they can be used in the module. Let's learn how these methods work.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The THPTensor Type\n\nMuch like the underlying `TH` and `THC` libraries, PyTorch defines a \"generic\" Tensor which is then specialized to a number of different types. Before considering how this specialization works, let's first consider how defining a new type in Python works, and how we create the generic `THPTensor` type.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The Python runtime sees all Python objects as variables of type `PyObject *`, which serves as a \"base type\" for all Python objects. Every Python type contains the refcount for the object, and a pointer to the object's *type object*. The type object determines the properties of the type. For example, it might contain a list of methods associated with the type, and which C functions get called to implement those methods. The object also contains any fields necessary to represent its state.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The formula for defining a new type is as follows:\n\n- Create a struct that defines what the new object will contain\n- Define the type object for the type", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The struct itself could be very simple. Inn Python, all floating point types are actually objects on the heap. The Python float struct is defined as:\n```cpp\ntypedef struct {\n PyObject_HEAD\n double ob_fval;\n} PyFloatObject;\n```\nThe `PyObject_HEAD` is a macro that brings in the code that implements an object's reference counting, and a pointer to the corresponding type object. So in this case, to implement a float, the only other \"state\" needed is the floating point value itself.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Now, let's see the struct for our `THPTensor` type:\n```cpp\nstruct THPTensor {\n PyObject_HEAD\n THTensor *cdata;\n};\n```\nPretty simple, right? We are just wrapping the underlying `TH` tensor by storing a pointer to it.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The key part is defining the \"type object\" for a new type. An example definition of a type object for our Python float takes the form:\n```cpp\nstatic PyTypeObject py_FloatType = {\n PyVarObject_HEAD_INIT(NULL, 0)\n \"py.FloatObject\", /* tp_name */\n sizeof(PyFloatObject), /* tp_basicsize */\n 0, /* tp_itemsize */\n 0, /* tp_dealloc */\n 0, /* tp_print */\n 0, /* tp_getattr */", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "0, /* tp_setattr */\n 0, /* tp_as_async */\n 0, /* tp_repr */\n 0, /* tp_as_number */\n 0, /* tp_as_sequence */\n 0, /* tp_as_mapping */\n 0, /* tp_hash */\n 0, /* tp_call */\n 0, /* tp_str */\n 0, /* tp_getattro */", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "0, /* tp_setattro */\n 0, /* tp_as_buffer */\n Py_TPFLAGS_DEFAULT, /* tp_flags */\n \"A floating point number\", /* tp_doc */\n};\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The easiest way to think of a *type object* is as a set of fields which define the properties of the object. For example, the `tp_basicsize` field is set to `sizeof(PyFloatObject)`. This is so that Python knows how much memory to allocate when calling `PyObject_New()` for a `PyFloatObject.` The full list of fields you can set is defined in `object.h` in the CPython backend:\nhttps://github.com/python/cpython/blob/master/Include/object.h.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The type object for our `THPTensor` is `THPTensorType`, defined in `csrc/generic/Tensor.cpp`. This object defines the name, size, mapping methods, etc. for a `THPTensor`.\n\nAs an example, let's take a look at the `tp_new` function we set in the `PyTypeObject`:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "```cpp\nPyTypeObject THPTensorType = {\n PyVarObject_HEAD_INIT(NULL, 0)\n ...\n THPTensor_(pynew), /* tp_new */\n};\n```\nThe `tp_new` function enables object creation. It is responsible for creating (as opposed to initializing) objects of that type and is equivalent to the `__new__()` method at the Python level. The C implementation is a static method that is passed the type being instantiated and any arguments, and returns a newly created object.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "```cpp\nstatic PyObject * THPTensor_(pynew)(PyTypeObject *type, PyObject *args, PyObject *kwargs)\n{\n HANDLE_TH_ERRORS\n Py_ssize_t num_args = args ? PyTuple_Size(args) : 0;", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "THPTensorPtr self = (THPTensor *)type->tp_alloc(type, 0);\n// more code below\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The first thing our new function does is allocate the `THPTensor`. It then runs through a series of initializations based off of the args passed to the function. For example, when creating a `THPTensor` *x* from another `THPTensor` *y*, we set the newly created `THPTensor`'s `cdata` field to be the result of calling `THTensor_(newWithTensor)` with the *y*'s underlying `TH` Tensor as an argument. Similar constructors exist for sizes, storages, NumPy arrays, and sequences.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "** Note that we solely use `tp_new`, and not a combination of `tp_new` and `tp_init` (which corresponds to the `__init__()` function).\n\nThe other important thing defined in Tensor.cpp is how indexing works. PyTorch Tensors support Python's **Mapping Protocol**. This allows us to do things like:\n```python\nx = torch.Tensor(10).fill_(1)\ny = x[3] // y == 1\nx[4] = 2\n// etc.\n```\n** Note that this indexing extends to Tensor with more than one dimension", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "We are able to use the `[]`-style notation by defining the three mapping methods described [here.](https://docs.python.org/3.7/c-api/typeobj.html#c.PyMappingMethods)\n\nThe most important methods are `THPTensor_(getValue)` and `THPTensor_(setValue)` which describe how to index a Tensor, for returning a new Tensor/Scalar, or updating the values of an existing Tensor in place. Read through these implementations to better understand how PyTorch supports basic tensor indexing.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Generic Builds (Part One)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "We could spend a ton of time exploring various aspects of the `THPTensor` and how it relates to defining a new Python object. But we still need to see how the `THPTensor_(init)()` function is translated to the `THPIntTensor_init()` we used in our module initialization. How do we take our `Tensor.cpp` file that defines a \"generic\" Tensor and use it to generate Python objects for all the permutations of types? To put it another way, `Tensor.cpp` is littered with lines of code like:\n```cpp", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "return THPTensor_(New)(THTensor_(new)(LIBRARY_STATE_NOARGS));\n```\nThis illustrates both cases we need to make type-specific:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "* Our output code will call `THPTensor_New(...)` in place of `THPTensor_(New)`\n* Our output code will call `THTensor_new(...)` in place of `THTensor_(new)`", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "In other words, for all supported Tensor types, we need to \"generate\" source code that has done the above substitutions. This is part of the \"build\" process for PyTorch. PyTorch relies on Setuptools (https://setuptools.readthedocs.io/en/latest/) for building the package, and we define a `setup.py` file in the top-level directory to customize the build process.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "One component building an Extension module using Setuptools is to list the source files involved in the compilation. However, our `csrc/generic/Tensor.cpp` file is not listed! So how does the code in this file end up being a part of the end product?", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Recall that we are calling the `THPTensor*` functions (such as `init`) from the directory above `generic`. If we take a look in this directory, there is another file `Tensor.cpp` defined. The last line of this file is important:\n```cpp\n//generic_include TH torch/csrc/generic/Tensor.cpp\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Note that this `Tensor.cpp` file is included in `setup.py`, but it is wrapped in a call to a Python helper function called `split_types`. This function takes as input a file, and looks for the \"//generic_include\" string in the file contents. If it is found, it generates a new output file for each Tensor type, with the following changes:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "- The output file is renamed to `Tensor.cpp`\n- The output file is slightly modified as follows:\n\n```cpp\n# Before:\n//generic_include TH torch/csrc/generic/Tensor.cpp\n\n# After:\n#define TH_GENERIC_FILE \"torch/src/generic/Tensor.cpp\"\n#include \"TH/THGenerateType.h\"\n```\nIncluding the header file on the second line has the side effect of including the source code in `Tensor.cpp` with some additional context defined. Let's take a look at one of the headers:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "```cpp\n#ifndef TH_GENERIC_FILE\n#error \"You must define TH_GENERIC_FILE before including THGenerateFloatType.h\"\n#endif", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "#define real float\n#define accreal double\n#define TH_CONVERT_REAL_TO_ACCREAL(_val) (accreal)(_val)\n#define TH_CONVERT_ACCREAL_TO_REAL(_val) (real)(_val)\n#define Real Float\n#define THInf FLT_MAX\n#define TH_REAL_IS_FLOAT\n#line 1 TH_GENERIC_FILE\n#include TH_GENERIC_FILE\n#undef accreal\n#undef real\n#undef Real\n#undef THInf\n#undef TH_REAL_IS_FLOAT\n#undef TH_CONVERT_REAL_TO_ACCREAL\n#undef TH_CONVERT_ACCREAL_TO_REAL\n\n#ifndef THGenerateManyTypes\n#undef TH_GENERIC_FILE\n#endif", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "What this is doing is bringing in the code from the generic `Tensor.cpp` file and surrounding it with the following macro definitions. For example, we define real as a float, so any code in the generic Tensor implementation that refers to something as a real will have that real replaced with a float. In the corresponding file `THGenerateIntType.h`, the same macro would replace `real` with `int`.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "These output files are returned from `split_types` and added to the list of source files, so we can see how the `.cpp` code for different types is created.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "There are a few things to note here: First, the `split_types` function is not strictly necessary. We could wrap the code in `Tensor.cpp` in a single file, repeating it for each type. The reason we split the code into separate files is to speed up compilation. Second, what we mean when we talk about the type replacement (e.g. replace real with a float) is that the C preprocessor will perform these substitutions during compilation. Merely surrounding the source code with these macros has no side effects until", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "preprocessing.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Generic Builds (Part Two)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Now that we have source files for all the Tensor types, we need to consider how the corresponding header declarations are created, and also how the conversions from `THTensor_(method)` and `THPTensor_(method)` to `THTensor_method` and `THPTensor_method` work. For example, `csrc/generic/Tensor.h` has declarations like:\n```cpp\nTHP_API PyObject * THPTensor_(New)(THTensor *ptr);\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "We use the same strategy for generating code in the source files for the headers. In `csrc/Tensor.h`, we do the following:\n```cpp\n#include \"generic/Tensor.h\"\n#include ", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "#include \"generic/Tensor.h\"\n#include \n```\nThis has the same effect, where we draw in the code from the generic header, wrapped with the same macro definitions, for each type. The only difference is that the resulting code is contained all within the same header file, as opposed to being split into multiple source files.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Lastly, we need to consider how we \"convert\" or \"substitute\" the function types. If we look in the same header file, we see a bunch of `#define` statements, including:\n```cpp\n#define THPTensor_(NAME) TH_CONCAT_4(THP,Real,Tensor_,NAME)\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "This macro says that any string in the source code matching the format `THPTensor_(NAME)` should be replaced with `THPRealTensor_NAME`, where Real is derived from whatever the symbol Real is `#define`'d to be at the time. Because our header code and source code is surrounded by macro definitions for all the types as seen above, after the preprocessor has run, the resulting code is what we would expect. The code in the `TH` library defines the same macro for `THTensor_(NAME)`, supporting the translation of", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "those functions as well. In this way, we end up with header and source files with specialized code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Module Objects and Type Methods", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Now we have seen how we have wrapped `TH`'s Tensor definition in `THP`, and generated THP methods such as `THPFloatTensor_init(...)`. Now we can explore what the above code actually does in terms of the module we are creating. The key line in `THPTensor_(init)` is:\n```cpp\n# THPTensorBaseStr, THPTensorType are also macros that are specific\n# to each type\nPyModule_AddObject(module, THPTensorBaseStr, (PyObject *)&THPTensorType);\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "This function registers our Tensor objects to the extension module, so we can use THPFloatTensor, THPIntTensor, etc. in our Python code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Just being able to create Tensors isn't very useful - we need to be able to call all the methods that `TH` defines. A simple example shows calling the in-place `zero_` method on a Tensor.\n```python\nx = torch.FloatTensor(10)\nx.zero_()\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Let's start by seeing how we add methods to newly defined types. One of the fields in the \"type object\" is `tp_methods`. This field holds an array of method definitions (`PyMethodDef`s) and is used to associate methods (and their underlying C/C++ implementations) with a type. Suppose we wanted to define a new method on our `PyFloatObject` that replaces the value. We could implement this as follows:\n```cpp\nstatic PyObject * replace(PyFloatObject *self, PyObject *args) {\n\tdouble val;", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "if (!PyArg_ParseTuple(args, \"d\", &val))\n\t\treturn NULL;\n\tself->ob_fval = val;\n\tPy_RETURN_NONE\n}\n```\nThis is equivalent to the Python method:\n```python\ndef replace(self, val):\n\tself.ob_fval = val\n```\nIt is instructive to read more about how defining methods works in CPython. In general, methods take as the first parameter the instance of the object, and optionally parameters for the positional arguments and keyword arguments. This static function is registered as a method on our float:\n```cpp", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "static PyMethodDef float_methods[] = {\n\t{\"replace\", (PyCFunction)replace, METH_VARARGS,\n\t\"replace the value in the float\"\n\t},\n\t{NULL} /* Sentinel */\n}\n```\nThis registers a method called replace, which is implemented by the C function of the same name. The `METH_VARARGS` flag indicates that the method takes a tuple of arguments representing all the arguments to the function. This array is set to the `tp_methods` field of the type object, and then we can use the `replace` method on objects of that type.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "We would like to be able to call all of the methods for `TH` tensors on our `THP` tensor equivalents. However, writing wrappers for all of the `TH` methods would be time-consuming and error prone. We need a better way to do this.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "PyTorch cwrap", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "PyTorch implements its own cwrap tool to wrap the `TH` Tensor methods for use in the Python backend. We define a `.cwrap` file containing a series of C method declarations in our custom [YAML format](http://yaml.org). The cwrap tool takes this file and outputs `.cpp` source files containing the wrapped methods in a format that is compatible with our `THPTensor` Python object and the Python C extension method calling format. This tool is used to generate code to wrap not only `TH`, but also `CuDNN`. It is", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "defined to be extensible.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "An example YAML \"declaration\" for the in-place `addmv_` function is as follows:\n```\n[[\n name: addmv_\n cname: addmv\n return: self\n arguments:\n - THTensor* self\n - arg: real beta\n default: AS_REAL(1)\n - THTensor* self\n - arg: real alpha\n default: AS_REAL(1)\n - THTensor* mat\n - THTensor* vec\n]]\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The architecture of the cwrap tool is very simple. It reads in a file, and then processes it with a series of **plugins.** See `tools/cwrap/plugins/__init__.py` for documentation on all the ways a plugin can alter the code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "The source code generation occurs in a series of passes. First, the YAML \"declaration\" is parsed and processed. Then the source code is generated piece-by-piece - adding things like argument checks and extractions, defining the method header, and the actual call to the underlying library such as `TH`. Finally, the cwrap tool allows for processing the entire file at a time. The resulting output for `addmv_` can be [explored here](https://gist.github.com/killeent/c00de46c2a896335a52552604cc4d74b).", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "In order to interface with the CPython backend, the tool generates an array of `PyMethodDef`s that can be stored or appended to the `THPTensor`'s `tp_methods` field.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "In the specific case of wrapping Tensor methods, the build process first generates the output source file from `TensorMethods.cwrap`. This source file is `#include`'d in the generic Tensor source file. This all occurs before the preprocessor does its magic. As a result, all of the method wrappers that are generated undergo the same pass as the `THPTensor` code above. Thus a single generic declaration and definition is specialized for each type as well.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Putting It All Together\n\nSo far, we have shown how we extend the Python interpreter to create a new extension module, how such a module defines our new `THPTensor` type, and how we can generate source code for Tensors of all types that interface with `TH`. Briefly, we will touch on compilation.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Setuptools allows us to define an Extension for compilation. The entire `torch._C` extension is compiled by collecting all of the source files, header files, libraries, etc. and creating a setuptools `Extension`. Then setuptools handles building the extension itself. I will explore the build process more in a subsequent post.\n\nTo summarize, let's revisit our four questions:\n\n- **How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?**", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "It uses CPython's framework for extending the Python interpreter and defining new types, while taking special care to generate code for all types.\n\n- **How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?**\n\nIt does so by defining a new type, `THPTensor`, that is backed by a `TH` Tensor. Function calls are forwarded to this tensor via the CPython backend's conventions.\n\n- **How does PyTorch cwrap work to generate code for Tensor methods?**", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "It takes our custom YAML-formatted code and generates source code for each method by processing it through a series of steps using a number of plugins.\n\n- **How does PyTorch's build system take all of these components to compile and generate a workable application?**\n\nIt takes a bunch of source/header files, libraries, and compilation directives to build an extension using Setuptools.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "This is just a snapshot of parts of the build system for PyTorch. There is more nuance, and detail, but I hope this serves as a gentle introduction to a lot of the components of our Tensor library.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "Resources:\n\n - is invaluable for understanding how to write C/C++ Extension to Python", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.12: TorchArrow, Functional API for Modules and nvFuser, are now available\"\nauthor: Team PyTorch\nfeatured-img: ''\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the release of PyTorch 1.12 ([release note](https://github.com/pytorch/pytorch/releases/tag/v1.12.0))! This release is composed of over 3124 commits, 433 contributors. Along with 1.12, we are releasing beta versions of AWS S3 Integration, PyTorch Vision Models on Channels Last on CPU, Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16 and FSDP API. We want to sincerely thank our dedicated community for your contributions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Summary:\n- Functional APIs to functionally apply module computation with a given set of parameters\n- Complex32 and Complex Convolutions in PyTorch\n- DataPipes from TorchData fully backward compatible with DataLoader \n- functorch with improved coverage for APIs\n- nvFuser a deep learning compiler for PyTorch\n- Changes to float32 matrix multiplication precision on Ampere and later CUDA hardware\n- TorchArrow, a new beta library for machine learning preprocessing over batch data", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Frontend APIs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Introducing TorchArrow\n\nWe\u2019ve got a new Beta release ready for you to try and use: TorchArrow. This is a library for machine learning preprocessing over batch data. It features a performant and Pandas-style, easy-to-use API in order to speed up your preprocessing workflows and development.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Currently, it provides a Python DataFrame interface with the following features:\n- High-performance CPU backend, vectorized and extensible User-Defined Functions (UDFs) with [Velox](https://github.com/facebookincubator/velox)\n- Seamless handoff with PyTorch or other model authoring, such as Tensor collation and easily plugging into PyTorch DataLoader and DataPipes\n- Zero copy for external readers via Arrow in-memory columnar format", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "For more details, please find our [10-min tutorial](https://github.com/pytorch/torcharrow/blob/main/tutorial/tutorial.ipynb), installation [instructions](https://github.com/pytorch/torcharrow#installation), API [documentation](https://pytorch.org/torcharrow/beta/), and a [prototype](https://github.com/pytorch/torchrec/tree/main/examples/torcharrow) for data preprocessing in TorchRec.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Functional API for Modules\n\nPyTorch 1.12 introduces a new beta feature to functionally apply Module computation with a given set of parameters. Sometimes, the traditional PyTorch Module usage pattern that maintains a static set of parameters internally is too restrictive. This is often the case when implementing algorithms for meta-learning, where multiple sets of parameters may need to be maintained across optimizer steps.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "The new ``torch.nn.utils.stateless.functional_call()`` API allows for: \n- Module computation with full flexibility over the set of parameters used\n- No need to reimplement your module in a functional way\n- Any parameter or buffer present in the module can be swapped with an externally-defined value for use in the call. Naming for referencing parameters / buffers follows the fully-qualified form in the module\u2019s ``state_dict()``", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Example:\n```python\nimport torch\nfrom torch import nn\nfrom torch.nn.utils.stateless import functional_call\n\nclass MyModule(nn.Module):\n def __init__(self):\n super().__init__()\n self.fc1 = nn.Linear(3, 3)\n self.bn = nn.BatchNorm1d(3)\n self.fc2 = nn.Linear(3, 3)\n\n def forward(self, x):\n return self.fc2(self.bn(self.fc1(x)))\n\nm = MyModule()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "# Define parameter / buffer values to use during module computation.\nmy_weight = torch.randn(3, 3, requires_grad=True)\nmy_bias = torch.tensor([1., 2., 3.], requires_grad=True)\nparams_and_buffers = {\n 'fc1.weight': my_weight,\n 'fc1.bias': my_bias,\n # Custom buffer values can be used too.\n 'bn.running_mean': torch.randn(3),\n}\n\n# Apply module computation to the input with the specified parameters / buffers.\ninp = torch.randn(5, 3)\noutput = functional_call(m, params_and_buffers, inp)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Complex32 and Complex Convolutions in PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch today natively supports complex numbers, complex autograd, complex modules, and numerous complex operations, including linear algebra and Fast Fourier Transform (FFT) operators. Many libraries, including torchaudio and ESPNet, already make use of complex numbers in PyTorch, and PyTorch 1.12 further extends complex functionality with complex convolutions and the experimental complex32 (\u201ccomplex half\u201d) data type that enables half precision FFT operations. Due to the bugs in CUDA 11.3 package, we", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "recommend using CUDA 11.6 package from wheels if you are using complex numbers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Forward-mode Automatic Differentiation\n\nForward-mode AD allows the computation of directional derivatives (or equivalently, Jacobian-vector products) eagerly in the forward pass. PyTorch 1.12 significantly improves the operator coverage for forward-mode AD. See our [tutorial](https://pytorch.org/tutorials/search.html?q=forward-mode+automatic+differentiation+%28beta%29&check_keywords=yes&area=default#) for more information.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "TorchData \n\n#### BC DataLoader + DataPipe\n\n\\`DataPipe\\` from TorchData becomes fully backward compatible with the existing \\`DataLoader\\` regarding shuffle determinism and dynamic sharding in both multiprocessing and distributed environments. For more details, please check out the [tutorial](https://pytorch.org/data/0.4.0/tutorial.html#working-with-dataloader).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) AWS S3 Integration\n\nDataPipes based on [AWSSDK](https://github.com/aws/aws-sdk-cpp) have been integrated into TorchData. It provides the following features backed by native AWSSDK:\n- Retrieve list of urls from each S3 bucket based on prefix\n\t- Support timeout to prevent hanging indefinitely\n\t- Support to specify S3 bucket region\n\t\t\n- Load data from S3 urls\n\t- Support buffered and multi-part download\n\t- Support to specify S3 bucket region", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "AWS native DataPipes are still in the beta phase. And, we will keep tuning them to improve their performance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) DataLoader2\n\nDataLoader2 became available in prototype mode. We are introducing new ways to interact between DataPipes, DataLoading API, and backends (aka ReadingServices). Feature is stable in terms of API, but functionally not complete yet. We welcome early adopters and feedback, as well as potential contributors.\n\nFor more details, please checkout the [link](https://github.com/pytorch/data/tree/main/torchdata/dataloader2).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "functorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Inspired by [Google JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples of these include:\n- [running ensembles of models on a single machine](https://pytorch.org/functorch/stable/notebooks/ensembling.html)\n- [efficiently computing Jacobians and Hessians](https://pytorch.org/functorch/stable/notebooks/jacobians_hessians.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "- [computing per-sample-gradients (or other per-sample quantities)](https://pytorch.org/functorch/stable/notebooks/per_sample_grads.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re excited to announce functorch 0.2.0 with a number of improvements and new experimental features.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Significantly improved coverage\n\nWe significantly improved coverage for ``functorch.jvp`` (our forward-mode autodiff API) and other APIs that rely on it (``functorch.{jacfwd, hessian}``).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) functorch.experimental.functionalize", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Given a function f, ``functionalize(f)`` returns a new function without mutations (with caveats). This is useful for constructing traces of PyTorch functions without in-place operations. For example, you can use ``make_fx(functionalize(f))`` to construct a mutation-free trace of a pytorch function. To learn more, please see the [documentation](https://pytorch.org/functorch/stable/generated/functorch.experimental.functionalize.html#functorch.experimental.functionalize).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "For more details, please see our [installation instructions](https://pytorch.org/functorch/stable/install.html), [documentation](https://pytorch.org/functorch/), [tutorials](https://pytorch.org/functorch), and [release notes](https://github.com/pytorch/functorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Performance Improvements", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Introducing nvFuser, a deep learning compiler for PyTorch\n\nIn PyTorch 1.12, Torchscript is updating its default fuser (for Volta and later CUDA accelerators) to nvFuser, which supports a wider range of operations and is faster than NNC, the previous fuser for CUDA devices. A soon to be published blog post will elaborate on nvFuser and show how it speeds up training on a variety of networks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "See [the nvFuser documentation](https://github.com/pytorch/pytorch/blob/release/1.12/torch/csrc/jit/codegen/cuda/README.md) for more details on usage and debugging.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Changes to float32 matrix multiplication precision on Ampere and later CUDA hardware", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch supports a variety of \u201cmixed precision\u201d techniques, like the torch.amp (Automated Mixed Precision) module and performing float32 matrix multiplications using the TensorFloat32 datatype on Ampere and later CUDA hardware for faster internal computations. In PyTorch 1.12 we\u2019re changing the default behavior of float32 matrix multiplications to always use full IEEE fp32 precision, which is more precise but slower than using the TensorFloat32 datatype for internal computation. For devices with a", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "particularly high ratio of TensorFloat32 to float32 throughput such as A100, this change in defaults can result in a large slowdown.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "If you\u2019ve been using TensorFloat32 matrix multiplications then you can continue to do so by setting ``torch.backends.cuda.matmul.allow_tf32 = True``\n\nwhich is supported since PyTorch 1.7. Starting in PyTorch 1.12 the new matmul precision API can be used, too: ``torch.set_float32_matmul_precision(\u201chighest\u201d|\u201dhigh\u201d|\u201dmedium\u201d)``", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "To reiterate, PyTorch\u2019s new default is \u201chighest\u201d precision for all device types. We think this provides better consistency across device types for matrix multiplications. Documentation for the new precision API can be found [here](https://pytorch.org/docs/master/generated/torch.set_float32_matmul_precision.html?highlight=precision#torch.set_float32_matmul_precision). Setting the \u201chigh\u201d or \u201cmedium\u201d precision types will enable TensorFloat32 on Ampere and later CUDA devices. If you\u2019re updating to PyTorch 1.12", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "then to preserve the current behavior and faster performance of matrix multiplications on Ampere devices, set precision to \u201chigh\u201d.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Using mixed precision techniques is essential for training many modern deep learning networks efficiently, and if you\u2019re already using torch.amp this change is unlikely to affect you. If you\u2019re not familiar with mixed precision training then see our soon to be published \u201cWhat Every User Should Know About Mixed Precision Training in PyTorch\u201d blogpost.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Accelerating PyTorch Vision Models with Channels Last on CPU", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Memory formats have a significant impact on performance when running vision models, generally Channels Last is more favorable from a performance perspective due to better data locality. 1.12 includes fundamental concepts of memory formats and demonstrates performance benefits using Channels Last on popular PyTorch vision models on Intel\u00ae Xeon\u00ae Scalable processors.\n- Enables Channels Last memory format support for the commonly used operators in CV domain on CPU, applicable for both inference and training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "- Provides native level optimization on Channels Last kernels from ATen, applicable for both AVX2 and AVX512\n- Delivers 1.3x to 1.8x inference performance gain over Channels First for TorchVision models on Intel\u00ae Xeon\u00ae Ice Lake (or newer) CPUs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Reduced precision numeric formats like bfloat16 improves PyTorch performance across multiple deep learning training workloads. PyTorch 1.12 includes the latest software enhancements on bfloat16 which applies to a broader scope of user scenarios and showcases even higher performance gains. The main improvements include:\n- 2x hardware compute throughput vs. float32 with the new bfloat16 native instruction VDPBF16PS, introduced on Intel\u00ae Xeon\u00ae Cooper Lake CPUs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "- 1/2 memory footprint of float32, faster speed for memory bandwidth intensive operators\n- 1.4x to 2.2x inference performance gain over float32 for TorchVision models on Intel\u00ae Xeon\u00ae Cooper Lake (or newer) CPUs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Introducing Accelerated PyTorch Training on Mac", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "With the PyTorch 1.12 release, developers and researchers can now take advantage of Apple silicon GPUs for significantly faster model training. This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac. Accelerated GPU training is enabled using Apple\u2019s Metal Performance Shaders (MPS) as a backend. The benefits include performance speedup from accelerated GPU training and the ability to train larger networks or batch sizes locally. Learn more", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "[here](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\n Accelerated GPU training and evaluation speedups over CPU-only (times faster)\n

\n\nAlongside the new MPS device support, the M1 binaries for Core and Domain libraries that have been available for the last few releases are now an official prototype feature. These binaries can be used to run PyTorch natively on Apple Silicon.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) BetterTransformer: Fastpath execution for Transformer Encoder Inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "PyTorch now supports CPU and GPU fastpath implementations (\u201cBetterTransformer\u201d) for several Transformer Encoder modules including TransformerEncoder, TransformerEncoderLayer, and MultiHeadAttention (MHA). The BetterTransformer fastpath architecture Better Transformer is consistently faster \u2013 2x for many common execution scenarios, depending on model and input characteristics. The new BetterTransformer-enabled modules are API compatible with previous releases of the PyTorch Transformer API and will", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "accelerate existing models if they meet fastpath execution requirements, as well as read models trained with previous versions of PyTorch. PyTorch 1.12 includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "- BetterTransformer integration for Torchtext\u2019s pretrained RoBERTa and XLM-R models\n- Torchtext which builds on the PyTorch Transformer API\n- Fastpath execution for improved performance by reducing execution overheads with fused kernels which combines multiple operators into a single kernel", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "- Option to achieve additional speedups by taking advantage of data sparsity during the processing of padding tokens in natural-language processing (by setting enable_nested_tensor=True when creating a TransformerEncoder)\n- Diagnostics to help users understand why fastpath execution did not occur", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Distributed", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Fully Sharded Data Parallel (FSDP) API", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "[FSDP API](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/) helps easily scale large model training by sharding a model\u2019s parameters, gradients and optimizer states across data parallel workers while maintaining the simplicity of data parallelism. The prototype version was released in PyTorch 1.11 with a minimum set of features that helped [scaling tests of models with up to 1T", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "parameters](https://medium.com/pytorch/training-a-1-trillion-parameter-model-with-pytorch-fully-sharded-data-parallel-on-aws-3ac13aa96cff).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "In this beta release, FSDP API added the following features to support various production workloads. Highlights of the the newly added features in this beta release include: \n1. Universal sharding strategy API - Users can easily change between sharding strategies with a single line change, and thus compare and use DDP (only data sharding), FSDP (full model and data sharding), or Zero2 (only sharding of optimizer and gradients) to optimize memory and performance for their specific training needs", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "2. Fine grained mixed precision policies - Users can specify a mix of half and full data types (bfloat16, fp16 or fp32) for model parameters, gradient communication, and buffers via mixed precision policies. Models are automatically saved in fp32 to allow for maximum portability\n3. Transformer auto wrapping policy - allows for optimal wrapping of Transformer based models by registering the models layer class, and thus accelerated training performance", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "4. Faster model initialization using device_id init - initialization is performed in a streaming fashion to avoid OOM issues and optimize init performance vs CPU init \n5. Rank0 streaming for full model saving of larger models - Fully sharded models can be saved by all GPU\u2019s streaming their shards to the rank 0 GPU, and the model is built in full state on the rank 0 CPU for saving", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "For more details and example code, please checkout the [documentation](https://pytorch.org/docs/1.11/fsdp.html?highlight=fsdp#module-torch.distributed.fsdp) and the [tutorial](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers! \n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.4 released, domain libraries updated'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Today, we\u2019re announcing the availability of PyTorch 1.4, along with updates to the PyTorch domain libraries. These releases build on top of the announcements from [NeurIPS 2019](https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/), where we shared the availability of PyTorch Elastic, a new classification framework for image and video, and the addition of Preferred Networks to the PyTorch community. For those that attended the workshops at NeurIPS, the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "content can be found [here](https://research.fb.com/neurips-2019-expo-workshops/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.4\n\nThe 1.4 release of PyTorch adds new capabilities, including the ability to do fine grain build level customization for PyTorch Mobile, and new experimental features including support for model parallel training and Java language bindings.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Mobile - Build level customization", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Following the open sourcing of [PyTorch Mobile in the 1.3 release](https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/), PyTorch 1.4 adds additional mobile support including the ability to customize build scripts at a fine-grain level. This allows mobile developers to optimize library size by only including the operators used by their models and, in the process, reduce their on device footprint significantly. Initial results show that, for example, a customized", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "MobileNetV2 is 40% to 50% smaller than the prebuilt PyTorch mobile library. You can learn more [here](https://pytorch.org/mobile/home/) about how to create your own custom builds and, as always, please engage with the community on the [PyTorch forums](https://discuss.pytorch.org/c/mobile) to provide any feedback you have.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Example code snippet for selectively compiling only the operators needed for MobileNetV2:\n\n```python\n# Dump list of operators used by MobileNetV2:\nimport torch, yaml\nmodel = torch.jit.load('MobileNetV2.pt')\nops = torch.jit.export_opnames(model)\nwith open('MobileNetV2.yaml', 'w') as output:\n yaml.dump(ops, output)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "```console\n# Build PyTorch Android library customized for MobileNetV2:\nSELECTED_OP_LIST=MobileNetV2.yaml scripts/build_pytorch_android.sh arm64-v8a\n\n# Build PyTorch iOS library customized for MobileNetV2:\nSELECTED_OP_LIST=MobileNetV2.yaml BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 scripts/build_ios.sh\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Distributed model parallel training (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "With the scale of models, such as RoBERTa, continuing to increase into the billions of parameters, model parallel training has become ever more important to help researchers push the limits. This release provides a distributed RPC framework to support distributed model parallel training. It allows for running functions remotely and referencing remote objects without copying the real data around, and provides autograd and optimizer APIs to transparently run backwards and update parameters across RPC", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "boundaries.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "To learn more about the APIs and the design of this feature, see the links below:\n\n* [API documentation](https://pytorch.org/docs/stable/rpc.html)\n* [Distributed Autograd design doc](https://pytorch.org/docs/stable/notes/distributed_autograd.html)\n* [Remote Reference design doc](https://pytorch.org/docs/stable/notes/rref.html)\n\nFor the full tutorials, see the links below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "* [A full RPC tutorial](https://pytorch.org/tutorials/intermediate/rpc_tutorial.html)\n* [Examples using model parallel training for reinforcement learning and with an LSTM](https://github.com/pytorch/examples/tree/master/distributed/rpc)\n\nAs always, you can connect with community members and discuss more on the [forums](https://discuss.pytorch.org/c/distributed/distributed-rpc).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Java bindings (Experimental)\n\nIn addition to supporting Python and C++, this release adds experimental support for Java bindings. Based on the interface developed for Android in PyTorch Mobile, the new bindings allow you to invoke TorchScript models from any Java program. Note that the Java bindings are only available for Linux for this release, and for inference only. We expect support to expand in subsequent releases. See the code snippet below for how to use PyTorch within Java:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "```java\nModule mod = Module.load(\"demo-model.pt1\");\nTensor data =\n Tensor.fromBlob(\n new int[] {1, 2, 3, 4, 5, 6}, // data\n new long[] {2, 3} // shape\n );\nIValue result = mod.forward(IValue.from(data), IValue.from(3.0));\nTensor output = result.toTensor();\nSystem.out.println(\"shape: \" + Arrays.toString(output.shape()));\nSystem.out.println(\"data: \" + Arrays.toString(output.getDataAsFloatArray()));", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Learn more about how to use PyTorch from Java [here](https://github.com/pytorch/java-demo), and see the full Javadocs API documentation [here](https://pytorch.org/javadoc/1.4.0/).\n\nFor the full 1.4 release notes, see [here](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Domain Libraries\n\nPyTorch domain libraries like torchvision, torchtext, and torchaudio complement PyTorch with common datasets, models, and transforms. We\u2019re excited to share new releases for all three domain libraries alongside the PyTorch 1.4 core release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "torchvision 0.5\n\nThe improvements to torchvision 0.5 mainly focus on adding support for production deployment including quantization, TorchScript, and ONNX. Some of the highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "* All models in torchvision are now torchscriptable making them easier to ship into non-Python production environments\n* ResNets, MobileNet, ShuffleNet, GoogleNet and InceptionV3 now have quantized counterparts with pre-trained models, and also include scripts for quantization-aware training.\n* In partnership with the Microsoft team, we\u2019ve added ONNX support for all models including Mask R-CNN.\n\nLearn more about torchvision 0.5 [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "torchaudio 0.4\n\nImprovements in torchaudio 0.4 focus on enhancing the currently available transformations, datasets, and backend support. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "* SoX is now optional, and a new extensible backend dispatch mechanism exposes SoundFile as an alternative to SoX.\n* The interface for datasets has been unified. This enables the addition of two large datasets: LibriSpeech and Common Voice.\n* New filters such as biquad, data augmentation such as time and frequency masking, transforms such as MFCC, gain and dither, and new feature computation such as deltas, are now available.\n* Transformations now support batches and are jitable.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "* An interactive speech recognition demo with voice activity detection is available for experimentation.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Learn more about torchaudio 0.4 [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "torchtext 0.5\n\ntorchtext 0.5 focuses mainly on improvements to the dataset loader APIs, including compatibility with core PyTorch APIs, but also adds support for unsupervised text tokenization. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "* Added bindings for SentencePiece for unsupervised text tokenization .\n* Added a new unsupervised learning dataset - enwik9.\n* Made revisions to PennTreebank, WikiText103, WikiText2, IMDb to make them compatible with torch.utils.data. Those datasets are in an experimental folder and we welcome your feedback.\n\nLearn more about torchtext 0.5 [here](https://github.com/pytorch/text/releases).\n\n*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*\n\nCheers!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Stochastic Weight Averaging in PyTorch'\nauthor: Pavel Izmailov and Andrew Gordon Wilson\nredirect_from: /2019/04/29/road-to-1.0.html\n---", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this blogpost we describe the recently proposed Stochastic Weight Averaging (SWA) technique [1, 2], and its new implementation in [`torchcontrib`](https://github.com/pytorch/contrib). SWA is a simple procedure that improves generalization in deep learning over Stochastic Gradient Descent (SGD) at no additional cost, and can be used as a drop-in replacement for any other optimizer in PyTorch. SWA has a wide range of applications and features:", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. SWA has been shown to significantly improve generalization in computer vision tasks, including VGG, ResNets, Wide ResNets and DenseNets on ImageNet and CIFAR benchmarks [1, 2].\n2. SWA provides state-of-the-art performance on key benchmarks in semi-supervised learning and domain adaptation [2].\n3. SWA is shown to improve the stability of training as well as the final average rewards of policy-gradient methods in deep reinforcement learning [3].", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "4. An extension of SWA can obtain efficient Bayesian model averaging, as well as high quality uncertainty estimates and calibration in deep learning [4].\n5. SWA for low precision training, SWALP, can match the performance of full-precision SGD even with all numbers quantized down to 8 bits, including gradient accumulators [5].", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In short, SWA performs an equal average of the weights traversed by SGD with a modified learning rate schedule (see the left panel of Figure 1.). SWA solutions end up in the center of a wide flat region of loss, while SGD tends to converge to the boundary of the low-loss region, making it susceptible to the shift between train and test error surfaces (see the middle and right panels of Figure 1).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 1.** Illustrations of SWA and SGD with a Preactivation ResNet-164 on CIFAR-100 [1]. **Left:** test error surface for three FGE samples and the corresponding SWA solution (averaging in weight space). **Middle** and **Right:** test error and train loss surfaces showing the weights proposed by SGD (at convergence) and SWA, starting from the same initialization of SGD after 125 training epochs. Please see [1] for details on how these figures were constructed.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**With our new implementation in [torchcontrib](https://github.com/pytorch/contrib) using SWA is as easy as using any other optimizer in PyTorch:**\n\n```python\nfrom torchcontrib.optim import SWA\n\n...\n...\n\n# training loop\nbase_opt = torch.optim.SGD(model.parameters(), lr=0.1)\nopt = torchcontrib.optim.SWA(base_opt, swa_start=10, swa_freq=5, swa_lr=0.05)\nfor _ in range(100):\n opt.zero_grad()\n loss_fn(model(input), target).backward()\n opt.step()\nopt.swap_swa_sgd()", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "You can wrap any optimizer from `torch.optim` using the `SWA` class, and then train your model as usual. When training is complete you simply call `swap_swa_sgd()` to set the weights of your model to their SWA averages. Below we explain the SWA procedure and the parameters of the `SWA` class in detail. We emphasize that SWA can be combined with *any* optimization procedure, such as Adam, in the same way that it can be combined with SGD.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Is this just Averaged SGD?", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "At a high level, averaging SGD iterates dates back several decades in convex optimization [6, 7], where it is sometimes referred to as Polyak-Ruppert averaging, or *averaged* SGD. **But the details matter**. *Averaged SGD* is often employed in conjunction with a decaying learning rate, and an exponentially moving average, typically for convex optimization. In convex optimization, the focus has been on improved rates of convergence. In deep learning, this form of averaged SGD smooths the trajectory of SGD", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "iterates, but does not perform very differently.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "By contrast, SWA is focused on an **equal average** of SGD iterates with a modified **cyclical or high constant learning rate**, and exploits the flatness of training objectives [8] specific to **deep learning** for **improved generalization**.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Stochastic Weight Averaging", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "There are two important ingredients that make SWA work. First, SWA uses a modified learning rate schedule so that SGD continues to explore the set of high-performing networks instead of simply converging to a single solution. For example, we can use the standard decaying learning rate strategy for the first 75% of training time, and then set the learning rate to a reasonably high constant value for the remaining 25% of the time (see the Figure 2 below). The second ingredient is to average the weights of the", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "networks traversed by SGD. For example, we can maintain a running average of the weights obtained in the end of every epoch within the last 25% of training time (see Figure 2).", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 2.** Illustration of the learning rate schedule adopted by SWA. Standard decaying schedule is used for the first 75% of the training and then a high constant value is used for the remaining 25%. The SWA averages are formed during the last 25% of training.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In our implementation the auto mode of the `SWA` optimizer allows us to run the procedure described above. To run SWA in auto mode you just need to wrap your optimizer `base_opt` of choice (can be SGD, Adam, or any other `torch.optim.Optimizer`) with `SWA(base_opt, swa_start, swa_freq, swa_lr)`. After `swa_start` optimization steps the learning rate will be switched to a constant value `swa_lr`, and in the end of every `swa_freq` optimization steps a snapshot of the weights will be added to the SWA running", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "average. Once you run `opt.swap_swa_sgd()`, the weights of your model are replaced with their SWA running averages.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Batch Normalization", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "One important detail to keep in mind is batch normalization. Batch normalization layers compute running statistics of activations during training. Note that the SWA averages of the weights are never used to make predictions during training, and so the batch normalization layers do not have the activation statistics computed after you reset the weights of your model with `opt.swap_swa_sgd()`. To compute the activation statistics you can just make a forward pass on your training data using the SWA model once", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "the training is finished. In the `SWA` class we provide a helper function `opt.bn_update(train_loader, model)`. It updates the activation statistics for every batch normalization layer in the model by making a forward pass on the `train_loader` data loader. You only need to call this function once in the end of training.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Advanced Learning-Rate Schedules\n\nSWA can be used with any learning rate schedule that encourages exploration of the flat region of solutions. For example, you can use cyclical learning rates in the last 25% of the training time instead of a constant value, and average the weights of the networks corresponding to the lowest values of the learning rate within each cycle (see Figure 3).\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 3.** Illustration of SWA with an alternative learning rate schedule. Cyclical learning rates are adopted in the last 25% of training, and models for averaging are collected in the end of each cycle.\n\nIn our implementation you can implement custom learning rate and weight averaging strategies by using `SWA` in the manual mode. The following code is equivalent to the auto mode code presented in the beginning of this blogpost.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nopt = torchcontrib.optim.SWA(base_opt)\nfor i in range(100):\n opt.zero_grad()\n loss_fn(model(input), target).backward()\n opt.step()\n if i > 10 and i % 5 == 0:\n opt.update_swa()\nopt.swap_swa_sgd()", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In manual mode you don\u2019t specify `swa_start`, `swa_lr` and `swa_freq`, and just call `opt.update_swa()` whenever you want to update the SWA running averages (for example in the end of each learning rate cycle). In manual mode `SWA` doesn\u2019t change the learning rate, so you can use any schedule you want as you would normally do with any other `torch.optim.Optimizer`.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Why does it work?\n\nSGD converges to a solution within a wide flat region of loss. The weight space is extremely high-dimensional, and most of the volume of the flat region is concentrated near the boundary, so SGD solutions will always be found near the boundary of the flat region of the loss. SWA on the other hand averages multiple SGD solutions, which allows it to move towards the center of the flat region.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We expect solutions that are centered in the flat region of the loss to generalize better than those near the boundary. Indeed, train and test error surfaces are not perfectly aligned in the weight space. Solutions that are centered in the flat region are not as susceptible to the shifts between train and test error surfaces as those near the boundary. In Figure 4 below we show the train loss and test error surfaces along the direction connecting the SWA and SGD solutions. As you can see, while SWA solution", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "has a higher train loss compared to the SGD solution, it is centered in the region of low loss, and has a substantially better test error.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n**Figure 4.** Train loss and test error along the line connecting the SWA solution (circle) and SGD solution (square). SWA solution is centered in a wide region of low train loss while the SGD solution lies near the boundary. Because of the shift between train loss and test error surfaces, SWA solution leads to much better generalization.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Examples and Results\n\nWe released a GitHub repo [here](https://github.com/izmailovpavel/contrib_swa_examples) with examples of using the `torchcontrib` implementation of SWA for training DNNs. For example, these examples can be used to achieve the following results on CIFAR-100:", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "| DNN (Budget) | SGD | SWA 1 Budget | SWA 1.25 Budgets | SWA 1.5 Budgets |\n| ------------------------- |:------------:|:------------:|:----------------:|:---------------:|\n| VGG16 (200) | 72.55 \u00b1 0.10 | 73.91 \u00b1 0.12 | 74.17 \u00b1 0.15 | 74.27 \u00b1 0.25 |\n| PreResNet110 (150) | 76.77 \u00b1 0.38 | 78.75 \u00b1 0.16 | 78.91 \u00b1 0.29 | 79.10 \u00b1 0.21 |\n| PreResNet164 (150) | 78.49 \u00b1 0.36 | 79.77 \u00b1 0.17 | 80.18 \u00b1 0.23 | 80.35 \u00b1 0.16 |", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "| WideResNet28x10 (200) | 80.82 \u00b1 0.23 | 81.46 \u00b1 0.23 | 81.91 \u00b1 0.27 | 82.15 \u00b1 0.27 |", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Semi-Supervised Learning", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In a follow-up [paper](https://arxiv.org/abs/1806.05594) SWA was applied to semi-supervised learning, where it illustrated improvements beyond the best reported results in multiple settings. For example, with SWA you can get 95% accuracy on CIFAR-10 if you only have the training labels for 4k training data points (the previous best reported result on this problem was 93.7%). This paper also explores averaging multiple times within epochs, which can accelerate convergence and find still flatter solutions in", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "a given time.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n\n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 5.** Performance of fast-SWA on semi-supervised learning with CIFAR-10. fast-SWA achieves record results in every setting considered.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Calibration and Uncertainty Estimates", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[SWA-Gaussian](https://arxiv.org/abs/1902.02476) (SWAG) is a simple, scalable and convenient approach to uncertainty estimation and calibration in Bayesian deep learning. Similarly to SWA, which maintains a running average of SGD iterates, SWAG estimates the first and second moments of the iterates to construct a Gaussian distribution over weights. SWAG distribution approximates the shape of the true posterior: Figure 6 below shows the SWAG distribution on top of the posterior log-density for PreResNet-164", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "on CIFAR-100.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n\n
\n**Figure 6.** SWAG distribution on top of posterior log-density for PreResNet-164 on CIFAR-100. The shape of SWAG distribution is aligned with the posterior.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Empirically, SWAG performs on par or better than popular alternatives including MC dropout, KFAC Laplace, and temperature scaling on uncertainty quantification, out-of-distribution detection, calibration and transfer learning in computer vision tasks. Code for SWAG is available [here](https://github.com/wjmaddox/swa_gaussian).", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Reinforcement Learning\n\nIn another follow-up [paper](http://www.gatsby.ucl.ac.uk/~balaji/udl-camera-ready/UDL-24.pdf) SWA was shown to improve the performance of policy gradient methods A2C and DDPG on several Atari games and MuJoCo environments.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "| Environment | A2C | A2C + SWA |\n|---------------|:----------------:|:----------------:|\n| Breakout | 522 \u00b1 34 | 703 \u00b1 60 |\n| Qbert | 18777 \u00b1 778 | 21272 \u00b1 655 |\n| SpaceInvaders | 7727 \u00b1 1121 | 21676 \u00b1 8897 |\n| Seaquest | 1779 \u00b1 4 | 1795 \u00b1 4 |\n| CrazyClimber | 147030 \u00b1 10239 | 139752 \u00b1 11618 |\n| BeamRider | 9999 \u00b1 402 | 11321 \u00b1 1065 |", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Low Precision Training", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We can filter through quantization noise by combining weights that have been rounded down with weights that have been rounded up. Moreover, by averaging weights to find a flat region of the loss surface, large perturbations of the weights will not affect the quality of the solution (Figures 7 and 8). Recent work shows that by adapting SWA to the low precision setting, in a method called SWALP, one can *match the performance of full-precision SGD even with all training in 8 bits* [5]. This is quite a", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "practically important result, given that (1) SGD training in 8 bits performs notably worse than full precision SGD, and (2) low precision training is significantly harder than predictions in low precision after training (the usual setting). For example, a ResNet-164 trained on CIFAR-100 with float (16-bit) SGD achieves 22.2% error, while 8-bit SGD achieves 24.0% error. By contrast, SWALP with 8 bit training achieves 21.8% error.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
\n\n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**Figure 7.** Quantizing in a flat region can still provide solutions with low loss.\n\n
\n\n
\n\n**Figure 8.** Low precision SGD training (with a modified learning rate schedule) and SWALP.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Conclusion", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "One of the greatest open questions in deep learning is why SGD manages to find good solutions, given that the training objectives are highly multimodal, and there are in principle many settings of parameters that achieve no training loss but poor generalization. By understanding geometric features such as flatness, which relate to generalization, we can begin to resolve these questions and build optimizers that provide even better generalization, and many other useful features, such as uncertainty", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "representation. We have presented SWA, a simple drop-in replacement for standard SGD, which can in principle benefit anyone training a deep neural network. SWA has been demonstrated to have strong performance in a number of areas, including computer vision, semi-supervised learning, reinforcement learning, uncertainty representation, calibration, Bayesian model averaging, and low precision training.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* Tensor creation on the CPU is expensive, but there is ongoing work to make it faster. At this point, a LSTMCell runs three CUDA kernels: two `gemm` kernels and one for the single pointwise group. One of the things we noticed was that there was a large gap between the finish of the second `gemm` and the start of the single pointwise group. This gap was a period of time when the GPU was idling around and not doing anything. Looking into it more, we discovered that the problem was that `torch.chunk` constructs new tensors and that tensor construction was not as fast as it could be. Instead of constructing new Tensor objects, we taught the fusion compiler how to manipulate a data pointer and strides to do the `torch.chunk` before sending it into the fused kernel, shrinking the amount of idle time between the second gemm and the launch of the element-wise fusion group. This give us around 1.2x increase speed up on the LSTM forward pass.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "By doing the above tricks, we are able to fuse the almost all `LSTMCell` forward graph (except the two gemm kernels) into a single fusion group, which corresponds to the `prim::FusionGroup_0` in the above IR graph. It will then be launched into a single fused kernel for execution. With these optimizations the model performance improves significantly with average forward time reduced by around 17ms (1.7x speedup) to 10ms, and average backward time reduce by 37ms to 27ms (1.37x speed up). \n\n### LSTM Layer (forward)\n\n```python\nclass LSTMLayer(jit.ScriptModule):\n def __init__(self, cell, *cell_args):\n super(LSTMLayer, self).__init__()\n self.cell = cell(*cell_args)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "@jit.script_method\n def forward(self, input, state):\n # type: (Tensor, Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]\n inputs = input.unbind(0)\n outputs = torch.jit.annotate(List[Tensor], [])\n for i in range(len(inputs)):\n out, state = self.cell(inputs[i], state)\n outputs += [out]\n return torch.stack(outputs), state\n```\n\nWe did several tricks on the IR we generated for TorchScript LSTM to boost the performance, some example optimizations we did:", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "* Loop Unrolling: We automatically unroll loops in the code (for big loops, we unroll a small subset of it), which then empowers us to do further optimizations on the for loops control flow. For example, the fuser can fuse together operations across iterations of the loop body, which results in a good performance improvement for control flow intensive models like LSTMs.\n* Batch Matrix Multiplication: For RNNs where the input is pre-multiplied (i.e. the model has a lot of matrix multiplies with the same LHS or RHS), we can efficiently batch those operations together into a single matrix multiply while chunking the outputs to achieve equivalent semantics. \n\nBy applying these techniques, we reduced our time in the forward pass by an additional 1.6ms to 8.4ms (1.2x speed up) and timing in backward by 7ms to around 20ms (1.35x speed up). \n\n### LSTM Layer (backward)", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "* \u201cTree\u201d Batch Matrix Muplication: It is often the case that a single weight is reused multiple times in the LSTM backward graph, forming a tree where the leaves are matrix multiplies and nodes are adds. These nodes can be combined together by concatenating the LHSs and RHSs in different dimensions, then computed as a single matrix multiplication. The formula of equivalence can be denoted as follows:\n \n $L1 * R1 + L2 * R2 = torch.cat((L1, L2), dim=1) * torch.cat((R1, R2), dim=0)$\n \n* Autograd is a critical component of what makes PyTorch such an elegant ML framework. As such, we carried this through to PyTorch JIT, but using a new **Automatic Differentiation** (AD) mechanism that works on the IR level. JIT automatic differentiation will slice the forward graph into symbolically differentiable subgraphs, and generate backwards nodes for those subgraphs. Taking the above IR as an example, we group the graph nodes into a single `prim::DifferentiableGraph_0` for the operations that has AD formulas. For operations that have not been added to AD formulas, we will fall back to Autograd during execution.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "* Optimizing the backwards path is hard, and the implicit broadcasting semantics make the optimization of automatic differentiation harder. PyTorch makes it convenient to write tensor operations without worrying about the shapes by broadcasting the tensors for you. For performance, the painful point in backward is that we need to have a summation for such kind of broadcastable operations. This results in the derivative of every broadcastable op being followed by a summation. Since we cannot currently fuse reduce operations, this causes FusionGroups to break into multiple small groups leading to bad performance. To deal with this, refer to this great [post](http://lernapparat.de/fast-lstm-pytorch/) written by Thomas Viehmann.\n\n### Misc Optimizations", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "* In addition to the steps laid about above, we also eliminated overhead between CUDA kernel launches and unnecessary tensor allocations. One example is when you do a tensor device look up. This can provide some poor performance initially with a lot of unnecessary allocations. When we remove these this results in a reduction from milliseconds to nanoseconds between kernel launches.\n* Lastly, there might be normalization applied in the custom LSTMCell like LayerNorm. Since LayerNorm and other normalization ops contains reduce operations, it is hard to fuse it in its entirety. Instead, we automatically decompose Layernorm to a statistics computation (reduce operations) + element-wise transformations, and then fuse those element-wise parts together. As of this post, there are some limitations on our auto differentiation and graph fuser infrastructure which limits the current support to inference mode only. We plan to add backward support in a future release.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "With the above optimizations on operation fusion, loop unrolling, batch matrix multiplication and some misc optimizations, we can see a clear performance increase on our custom TorchScript LSTM forward and backward from the following figure: \n\n
\n \n
\n\n\nThere are a number of additional optimizations that we did not cover in this post. In addition to the ones laid out in this post, we now see that our custom LSTM forward pass is on par with cuDNN. We are also working on optimizing backward more and expect to see improvements in future releases. Besides the speed that TorchScript provides, we introduced a much more flexible API that enable you to hand draft a lot more custom RNNs, which cuDNN could not provide.", "metadata": {"source": "https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch, a year in....\"\nauthor: \"The PyTorch Team\"\ndate: 2018-01-19 12:00:00 -0500\nredirect_from: /2018/01/19/a-year-in.html\n---\n\nToday marks 1 year since PyTorch was released publicly. It's been a wild ride \u2014 our quest to build a flexible deep learning research platform. Over the last year, we've seen an amazing community of people using, contributing to and evangelizing PyTorch \u2014 thank you for the love.\n\nLooking back, we wanted to summarize PyTorch over the past year: the progress, the news and highlights from the community.\n\n## Community\n\nWe've been blessed with a strong organic community of researchers and engineers who fell in love with PyTorch. The core team has engineers and researchers from multiple countries, companies and universities, and we couldn't have made PyTorch what it is without each contribution.\n\n\n### Research papers, packages and Github", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "Within days of release, users from the community started to implement their favorite research papers in PyTorch and release the code on Github. Open-source code is a primary and essential tool for researchers today.\n\nFolks came together to create [torchtext](https://github.com/pytorch/text), [torchvision](https://github.com/pytorch/vision) and [torchaudio](https://github.com/pytorch/audio) packages to help facilitate and democratize research in different domains.\n\nThe first community package based on PyTorch came from Brandon Amos, [titled Block](https://twitter.com/brandondamos/status/828652480573607937), and helped with easier manipulation of block matrices. The Locus Lab at **CMU** subsequently went on to [publish PyTorch packages](https://github.com/locuslab) and implementations for most of their research. The first research paper code came from Sergey Zagoruyko titled [Paying more attention to attention](https://twitter.com/PyTorch/status/822561885744726016).", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "Jun-Yan Zhu, Taesung Park, Phillip Isola, Alyosha Efros and team from **U.C.Berkeley** released the hugely popular [Cycle-GAN and pix2pix](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix) which does image to image transforms.\n\n
\n \n
\n\nThe researchers at **HarvardNLP** and **Systran** started developing and improving [OpenNMT in PyTorch](https://github.com/OpenNMT/OpenNMT-py), seeded by initial reimplementation of the [Lua]Torch code from Adam Lerer at Facebook.\n\nThe MagicPony team at **Twitter** contributed implementations of their [Super-resolution work early on into PyTorch's examples](https://twitter.com/Rob_Bishop/status/821793080877588480).", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "**Salesforce Research** released several packages, including their highlight release of [PyTorch-QRNN](https://twitter.com/Smerity/status/917472260851560448), a type of RNN that is 2x to 17x faster than standard LSTMs optimized by CuDNN. James Bradbury and team form one of the most active and engaging forces in the PyTorch community.\n\n

We're releasing @PyTorch-QRNN, 2-17x faster than NVIDIA's cuDNN LSTM.
Speed thanks to 50 lines of CUDA via CuPy.https://t.co/KaWhN4yDZd pic.twitter.com/yoLYj3pMI0

— Smerity (@Smerity) October 9, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "Researchers from **Uber**, **Northeastern** and **Stanford** came together to form an active probabilistic programming community around their packages [Pyro](http://pyro.ai/) and [ProbTorch](https://github.com/probtorch/probtorch). They are actively developing the torch.distributions core package. This community is so active and fast-moving, we had our first pytorch-probabilistic-programming meetup at NIPS 2017 with Fritz Obermeyer, Noah Goodman, Jan-Willem van de Meent, Brooks Paige, Dustin Tran and 22 additional attendees discussing how to make the world bayesian.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "**NVIDIA** Researchers released three high-quality repositories that implemented [pix2pix-HD](https://github.com/NVIDIA/pix2pixHD), [Sentiment Neuron](https://github.com/NVIDIA/sentiment-discovery) and [FlowNet2](https://github.com/NVIDIA/flownet2-pytorch) papers. Their analysis of scalability of different [Data Parallel models in PyTorch](https://github.com/NVIDIA/sentiment-discovery/blob/master/analysis/scale.md) was helpful to the community.\n\n
\n \n
\n\nThe Allen Institute for AI released [AllenNLP](http://allennlp.org/) which includes several state-of-the-art models in NLP \u2014 reference implementations and easy to use [web demos](http://demo.allennlp.org/machine-comprehension) for standard NLP tasks.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "We also had our first Kaggle winning team grt123 in July. They won the DataScience Bowl 2017 on Lung Cancer detection and [subsequently released their PyTorch implementations](https://twitter.com/PyTorch/status/881573658166267904).\n\nOn the visualization front, Tzu-Wei Huang implemented a [TensorBoard-PyTorch plugin](https://github.com/lanpa/tensorboard-pytorch) and Facebook AI Research released PyTorch compatibility for their [visdom](https://github.com/facebookresearch/visdom) visualization package.\n\n
\n \n \n
\n\nLastly, **Facebook AI Research** released several projects such as [ParlAI, fairseq-py, VoiceLoop and FaderNetworks](https://github.com/facebookresearch/) that implemented cutting-edge models and interfaced datasets in multiple domains.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "There are countless good projects that we haven't highlighted for the lack of space, you can find a curated list [here](https://github.com/soumith?tab=stars).\n\nWe would also like to give a huge shout-out to folks who actively help others out on the Forums, especially [ptrblck](https://discuss.pytorch.org/u/ptrblck/summary), [jpeg729](https://discuss.pytorch.org/u/jpeg729/summary), [QuantScientist](https://discuss.pytorch.org/u/quantscientist/summary), [albanD](https://discuss.pytorch.org/u/alband/summary), [Thomas Viehmann](https://discuss.pytorch.org/u/tom/summary) and [chenyuntc](https://discuss.pytorch.org/u/chenyuntc/summary). You are providing an invaluable service, thank you so much!\n\n## Metrics\n\nIn terms of sheer numbers,", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "* 87,769 lines of Python code on github that [import torch](https://github.com/search?l=Python&q=import+torch&type=Code)\n* [3,983 repositories on Github that mention PyTorch in their name or description](https://github.com/search?q=pytorch&type=Repositories)\n* More than half a million downloads of PyTorch binaries. 651,916 to be precise.\n* **5,400 users** wrote **21,500 posts** discussing 5,200 topics on our forums discuss.pytorch.org (http://discuss.pytorch.org/)\n* 131 mentions of PyTorch on Reddit's /r/machinelearning since the day of release. In the same period, TensorFlow was mentioned 255 times.\n\n\n### Research Metrics\n\nPyTorch is a research-focused framework. So one of the metrics of interest is to see the usage of PyTorch in machine learning research papers.\n\n\n* In the recent ICLR2018 conference submissions, PyTorch was mentioned in **87 papers**, compared to TensorFlow at 228 papers, Keras at 42 papers, Theano and Matlab at 32 papers.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "* [Monthly arxiv.org mentions for frameworks](https://twitter.com/fchollet/status/951828914103402497) had PyTorch at 72 mentions, with TensorFlow at 273 mentions, Keras at 100 mentions, Caffe at 94 mentions and Theano at 53 mentions.\n\n## Courses, Tutorials and Books\n\nWhen we released PyTorch, we had good API documentation, but our tutorials were limited to a few ipython notebooks \u2014 helpful, but not good enough.\n\n[Sasank Chilamkurthy](https://github.com/chsasank) took it upon himself to revamp the tutorials into the [beautiful website](https://pytorch.org/tutorials/) that it is today.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "[Sean Robertson](https://github.com/spro/practical-pytorch) and [Justin Johnson](https://github.com/jcjohnson/pytorch-examples) wrote great new tutorials \u2014 in NLP, and to learn by example. [Yunjey Choi](https://github.com/yunjey/pytorch-tutorial) wrote a beautiful tutorial where most models were implemented in 30 lines or less.\nEach new tutorial helped users find their way faster, with different approaches to learning.\n\n[Goku Mohandas and Delip Rao](https://twitter.com/PyTorch/status/888500355943641088) switched the code content of their book-in-progress to use PyTorch.\n\nWe've seen quite a few university machine learning courses being taught with PyTorch as the primary tool, such as Harvard's [CS287](https://harvard-ml-courses.github.io/cs287-web/). Taking it one step further and democratizing learning, we had three online courses pop up that teach using PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "- **Fast.ai's** \u201cDeep Learning for Coders\u201d is a popular online course. In September, Jeremy and Rachel [announced that the next fast.ai courses will be nearly entirely based on PyTorch](http://www.fast.ai/2017/09/08/introducing-pytorch-for-fastai/).\n- Ritchie Ng, a researcher with ties to NUS Singapore and Tsinghua released [a Udemy course](https://www.udemy.com/practical-deep-learning-with-pytorch/) titled Practical Deep Learning with PyTorch.\n- Sung Kim from HKUST released an [online course on Youtube](https://www.youtube.com/playlist?list=PLlMkM4tgfjnJ3I-dbhO9JTw7gNty6o_2m) that was aimed towards a general audience, titled: \u201cPyTorch Zero to All\u201d.\n\n\n## Engineering\n\nOver the last year we implemented multiple features, improved performance across the board and fixed lots of bugs. A full list of the work we've done is found in our [release notes](https://github.com/pytorch/pytorch/releases).\nHere are highlights from our work over the last year:\n\n## Higher-order gradients", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "With the release of several papers that implement penalties of gradients and with ongoing research in 2nd order gradient methods, this was an essential and sought-after feature. In August, we implemented a generalized interface that can take n-th order derivatives and increased the coverage of functions that support higher-order gradients over time, such that at the moment of writing almost all ops support this.\n\n\n## Distributed PyTorch\n\nIn August, we released a small distributed package that followed the highly popular MPI-collective approach. The package has multiple backends such as TCP, MPI, Gloo and NCCL2 to support various types of CPU/GPU collective operations and use-cases, and integrates distributed technologies such as Infiniband and RoCE. Distributed is hard, and we had bugs in the initial iteration. Over subsequent releases, we made the package more stable and improved performance.\n\n## Closer to NumPy", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "## Closer to NumPy\n\nOne of the biggest demands from users were NumPy features that they were familiar with. Features such as Broadcasting and Advanced Indexing are convenient and save users a lot of verbosity. We implemented these features and started to align our API to be closer to NumPy. Over time, we expect to get closer and closer to NumPy's API where appropriate.\n\n## Sparse Tensors\n\nIn March, we released a small package supporting sparse Tensors and in May we released CUDA support for the sparse package. The package is small and limited in functionality, and is used for implementing Sparse Embeddings and commonly used sparse paradigms in deep learning. This package is still small in scope and there's demand to expand it \u2014 if you are interested in working on expanding the sparse package, reach out to us on our [Discussion Boards](https://discuss.pytorch.org/)\n\n\n## Performance", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "## Performance\n\nPerformance is always an ongoing battle, especially for PyTorch which is a dynamic framework that wants to maximize flexibility. Over the last year, we've improved performance across board, from our core Tensor library to the neural network operators, writing faster micro-optimized across board.\n\n* We've added specialized AVX and AVX2 intrinsics for Tensor operations\n* Wrote faster GPU kernels for frequent workloads like concatenation and Softmax (among many other things)\n* Rewrote the code for several neural network operators (too many to list), but notably nn.Embedding and group convolutions.\n\n**Reducing framework overhead by 10x across board**", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "Since PyTorch is a dynamic graph framework, we create a new graph on the fly at every iteration of a training loop. Hence, the framework overhead has to be low, or the workload has to be large enough that the framework overhead is hidden. In August, the authors of DyNet (Graham Neubig and team) showcased that it's much faster than PyTorch on small NLP models. This was an interesting challenge, we didn't realize that models of those sizes were being trained. In a multi-month (and ongoing) effort, we embarked upon a significant rewrite of PyTorch internals that reduced the framework overhead from more than 10 microseconds per operator execution to as little as 1 microsecond.\n\n**ATen**", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "**ATen**\n\nAs we embarked upon a redesign of the PyTorch internals, we built the [ATen C++11](https://github.com/pytorch/pytorch/tree/master/aten) library that now powers all of the PyTorch backend. ATen has an API that mirrors PyTorch's Python API, which makes it a convenient C++ library for Tensor computation. ATen can be built and used independently of PyTorch.\n\n## Exporting models to production \u2014 ONNX Support and the JIT compiler\n\nOne of the common requests we've received was to export PyTorch models to another framework. Users engaged in a rapid research cycle in PyTorch and when they were done, they wanted to ship it to larger projects with C++ only requirements.", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "With this in mind, we built a tracer for PyTorch \u2014 which can export PyTorch models into an intermediate representation.\nThe subsequent trace can be either used to run the current PyTorch model more efficiently (by running optimization passes on it), or be converted to the [ONNX](http://onnx.ai/) format to be shipped to other frameworks such as Caffe2, MXNet, TensorFlow and others or directly to the hardware accelerated libraries like CoreML or TensorRT. Over the next year, you will hear more about the JIT compiler for performance improvements.\n\n\n## Users being funny :)\n\nOur users express their support in funny ways, made us laugh, thanks for this :)", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "

I've been using PyTorch a few months now and I've never felt better. I have more energy. My skin is clearer. My eye sight has improved.

— Andrej Karpathy (@karpathy) May 26, 2017
\n\n\n

Talk to your doctor to find out if PyTorch is right for you.

— Sean Robertson (@sprobertson) May 26, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "

PyTorch gave me so much life that my skin got cleared, my grades are up, my bills are paid and my crops are watered.

— Adam Will \u00f0\ufe0f\u200d\u00f0 (@adam_will_do_it) May 26, 2017
\n\n\n

So have I! But my hair is also shiner and I've lost weight. @PyTorch for the win. https://t.co/qgU4oIOB4K

— Mariya (@thinkmariya) May 26, 2017
\n", "metadata": {"source": "https://pytorch.org/blog/a-year-in/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch for AMD ROCm\u2122 Platform now available as Python package'\nauthor: Niles Burbank \u2013 Director PM at AMD, Mayank Daga \u2013 Director, Deep Learning Software at AMD\n---\n\nWith the PyTorch 1.8 release, we are delighted to announce a new installation option for users of\nPyTorch on the ROCm\u2122 open software platform. An installable Python package is now hosted on\npytorch.org, along with instructions for local installation in the same simple, selectable format as\nPyTorch packages for CPU-only configurations and other GPU platforms. PyTorch on ROCm includes full\ncapability for mixed-precision and large-scale training using AMD\u2019s MIOpen & RCCL libraries. This\nprovides a new option for data scientists, researchers, students, and others in the community to get\nstarted with accelerated PyTorch using AMD GPUs.\n\n
\n \n
\n\n## The ROCm Ecosystem", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} +{"page_content": "ROCm is AMD\u2019s open source software platform for GPU-accelerated high performance computing and\nmachine learning. Since the original ROCm release in 2016, the ROCm platform has evolved to support\nadditional libraries and tools, a wider set of Linux\u00ae distributions, and a range of new GPUs. This includes\nthe AMD Instinct\u2122 MI100, the first GPU based on AMD CDNA\u2122 architecture. \n \nThe ROCm ecosystem has an established history of support for PyTorch, which was initially implemented\nas a fork of the PyTorch project, and more recently through ROCm support in the upstream PyTorch\ncode. PyTorch users can install PyTorch for ROCm using AMD\u2019s public PyTorch docker image, and can of\ncourse build PyTorch for ROCm from source. With PyTorch 1.8, these existing installation options are\nnow complemented by the availability of an installable Python package.", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} +{"page_content": "The primary focus of ROCm has always been high performance computing at scale. The combined\ncapabilities of ROCm and AMD\u2019s Instinct family of data center GPUs are particularly suited to the\nchallenges of HPC at data center scale. PyTorch is a natural fit for this environment, as HPC and ML\nworkflows become more intertwined.\n\n### Getting started with PyTorch for ROCm", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} +{"page_content": "The scope for this build of PyTorch is AMD GPUs with ROCm support, running on Linux. The GPUs\nsupported by ROCm include all of AMD\u2019s Instinct family of compute-focused data center GPUs, along\nwith some other select GPUs. A current list of supported GPUs can be found in the [ROCm Github\nrepository](https://github.com/RadeonOpenCompute/ROCm#supported-gpus). After confirming that the target system includes supported GPUs and the current 4.0.1\nrelease of ROCm, installation of PyTorch follows the same simple Pip-based installation as any other\nPython package. As with PyTorch builds for other platforms, the configurator at [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/) provides the specific command line to be run.\n\nPyTorch for ROCm is built from the upstream PyTorch repository, and is a full featured implementation.\nNotably, it includes support for distributed training across multiple GPUs and supports accelerated\nmixed precision training.\n\n### More information", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} +{"page_content": "### More information\n\nA list of ROCm supported GPUs and operating systems can be found at\n[https://github.com/RadeonOpenCompute/ROCm](https://github.com/RadeonOpenCompute/ROCm)\nGeneral documentation on the ROCm platform is available at [https://rocmdocs.amd.com/en/latest/](https://rocmdocs.amd.com/en/latest/)\nROCm Learning Center at [https://developer.amd.com/resources/rocm-resources/rocm-learning-center/](https://developer.amd.com/resources/rocm-resources/rocm-learning-center/) General information on AMD\u2019s offerings for HPC and ML can be found at [https://amd.com/hpc](https://amd.com/hpc)\n\n### Feedback\nAn engaged user base is a tremendously important part of the PyTorch ecosystem. We would be deeply\nappreciative of feedback on the PyTorch for ROCm experience in the [PyTorch discussion forum](https://discuss.pytorch.org/) and, where appropriate, reporting any issues via [Github](https://github.com/pytorch/pytorch).", "metadata": {"source": "https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"A Tour of PyTorch Internals (Part I)\"\nauthor: \"Trevor Killeen\"\ndate: 2017-05-11 12:00:00 -0500\nredirect_from: /2017/05/11/Internals.html\n---\n\nThe fundamental unit in PyTorch is the Tensor. This post will serve as an overview for how we implement Tensors in PyTorch, such that the user can interact with it from the Python shell. In particular, we want to answer four main questions:\n\n- How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?\n- How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?\n- How does PyTorch cwrap work to generate code for Tensor methods?\n- How does PyTorch's build system take all of these components to compile and generate a workable application?\n\n## Extending the Python Interpreter", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "PyTorch defines a new package `torch`. In this post we will consider the `._C` module. This module is known as an \"extension module\" - a Python module written in C. Such modules allow us to define new built-in object types (e.g. the `Tensor`) and to call C/C++ functions.\n\nThe `._C` module is defined in `torch/csrc/Module.cpp`. The `init_C()` / `PyInit__C()` function creates the module and adds the method definitions as appropriate. This module is passed around to a number of different `__init()` functions that add further objects to the module, register new types, etc.\n\nOne collection of these `__init()` calls is the following:\n\n```cpp\nASSERT_TRUE(THPDoubleTensor_init(module));\nASSERT_TRUE(THPFloatTensor_init(module));\nASSERT_TRUE(THPHalfTensor_init(module));\nASSERT_TRUE(THPLongTensor_init(module));\nASSERT_TRUE(THPIntTensor_init(module));\nASSERT_TRUE(THPShortTensor_init(module));\nASSERT_TRUE(THPCharTensor_init(module));\nASSERT_TRUE(THPByteTensor_init(module));\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "These `__init()` functions add the Tensor object for each type to the `._C` module so that they can be used in the module. Let's learn how these methods work.\n\n## The THPTensor Type\n\nMuch like the underlying `TH` and `THC` libraries, PyTorch defines a \"generic\" Tensor which is then specialized to a number of different types. Before considering how this specialization works, let's first consider how defining a new type in Python works, and how we create the generic `THPTensor` type.\n\nThe Python runtime sees all Python objects as variables of type `PyObject *`, which serves as a \"base type\" for all Python objects. Every Python type contains the refcount for the object, and a pointer to the object's *type object*. The type object determines the properties of the type. For example, it might contain a list of methods associated with the type, and which C functions get called to implement those methods. The object also contains any fields necessary to represent its state.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "The formula for defining a new type is as follows:\n\n- Create a struct that defines what the new object will contain\n- Define the type object for the type\n\nThe struct itself could be very simple. Inn Python, all floating point types are actually objects on the heap. The Python float struct is defined as:\n```cpp\ntypedef struct {\n PyObject_HEAD\n double ob_fval;\n} PyFloatObject;\n```\nThe `PyObject_HEAD` is a macro that brings in the code that implements an object's reference counting, and a pointer to the corresponding type object. So in this case, to implement a float, the only other \"state\" needed is the floating point value itself.\n\nNow, let's see the struct for our `THPTensor` type:\n```cpp\nstruct THPTensor {\n PyObject_HEAD\n THTensor *cdata;\n};\n```\nPretty simple, right? We are just wrapping the underlying `TH` tensor by storing a pointer to it.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "The key part is defining the \"type object\" for a new type. An example definition of a type object for our Python float takes the form:\n```cpp\nstatic PyTypeObject py_FloatType = {\n PyVarObject_HEAD_INIT(NULL, 0)\n \"py.FloatObject\", /* tp_name */\n sizeof(PyFloatObject), /* tp_basicsize */\n 0, /* tp_itemsize */\n 0, /* tp_dealloc */\n 0, /* tp_print */\n 0, /* tp_getattr */\n 0, /* tp_setattr */\n 0, /* tp_as_async */\n 0, /* tp_repr */\n 0, /* tp_as_number */\n 0, /* tp_as_sequence */\n 0, /* tp_as_mapping */\n 0, /* tp_hash */\n 0, /* tp_call */\n 0, /* tp_str */\n 0, /* tp_getattro */\n 0, /* tp_setattro */\n 0, /* tp_as_buffer */\n Py_TPFLAGS_DEFAULT, /* tp_flags */\n \"A floating point number\", /* tp_doc */\n};\n```\nThe easiest way to think of a *type object* is as a set of fields which define the properties of the object. For example, the `tp_basicsize` field is set to `sizeof(PyFloatObject)`. This is so that Python knows how much memory to allocate when calling `PyObject_New()` for a `PyFloatObject.` The full list of fields you can set is defined in `object.h` in the CPython backend:\nhttps://github.com/python/cpython/blob/master/Include/object.h.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "The type object for our `THPTensor` is `THPTensorType`, defined in `csrc/generic/Tensor.cpp`. This object defines the name, size, mapping methods, etc. for a `THPTensor`.\n\nAs an example, let's take a look at the `tp_new` function we set in the `PyTypeObject`:\n\n```cpp\nPyTypeObject THPTensorType = {\n PyVarObject_HEAD_INIT(NULL, 0)\n ...\n THPTensor_(pynew), /* tp_new */\n};\n```\nThe `tp_new` function enables object creation. It is responsible for creating (as opposed to initializing) objects of that type and is equivalent to the `__new__()` method at the Python level. The C implementation is a static method that is passed the type being instantiated and any arguments, and returns a newly created object.\n\n```cpp\nstatic PyObject * THPTensor_(pynew)(PyTypeObject *type, PyObject *args, PyObject *kwargs)\n{\n HANDLE_TH_ERRORS\n Py_ssize_t num_args = args ? PyTuple_Size(args) : 0;", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "THPTensorPtr self = (THPTensor *)type->tp_alloc(type, 0);\n// more code below\n```\nThe first thing our new function does is allocate the `THPTensor`. It then runs through a series of initializations based off of the args passed to the function. For example, when creating a `THPTensor` *x* from another `THPTensor` *y*, we set the newly created `THPTensor`'s `cdata` field to be the result of calling `THTensor_(newWithTensor)` with the *y*'s underlying `TH` Tensor as an argument. Similar constructors exist for sizes, storages, NumPy arrays, and sequences.\n\n** Note that we solely use `tp_new`, and not a combination of `tp_new` and `tp_init` (which corresponds to the `__init__()` function).\n\nThe other important thing defined in Tensor.cpp is how indexing works. PyTorch Tensors support Python's **Mapping Protocol**. This allows us to do things like:\n```python\nx = torch.Tensor(10).fill_(1)\ny = x[3] // y == 1\nx[4] = 2\n// etc.\n```\n** Note that this indexing extends to Tensor with more than one dimension", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "We are able to use the `[]`-style notation by defining the three mapping methods described [here.](https://docs.python.org/3.7/c-api/typeobj.html#c.PyMappingMethods)\n\nThe most important methods are `THPTensor_(getValue)` and `THPTensor_(setValue)` which describe how to index a Tensor, for returning a new Tensor/Scalar, or updating the values of an existing Tensor in place. Read through these implementations to better understand how PyTorch supports basic tensor indexing.\n\n### Generic Builds (Part One)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "We could spend a ton of time exploring various aspects of the `THPTensor` and how it relates to defining a new Python object. But we still need to see how the `THPTensor_(init)()` function is translated to the `THPIntTensor_init()` we used in our module initialization. How do we take our `Tensor.cpp` file that defines a \"generic\" Tensor and use it to generate Python objects for all the permutations of types? To put it another way, `Tensor.cpp` is littered with lines of code like:\n```cpp\nreturn THPTensor_(New)(THTensor_(new)(LIBRARY_STATE_NOARGS));\n```\nThis illustrates both cases we need to make type-specific:\n\n* Our output code will call `THPTensor_New(...)` in place of `THPTensor_(New)`\n* Our output code will call `THTensor_new(...)` in place of `THTensor_(new)`", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "In other words, for all supported Tensor types, we need to \"generate\" source code that has done the above substitutions. This is part of the \"build\" process for PyTorch. PyTorch relies on Setuptools (https://setuptools.readthedocs.io/en/latest/) for building the package, and we define a `setup.py` file in the top-level directory to customize the build process.\n\nOne component building an Extension module using Setuptools is to list the source files involved in the compilation. However, our `csrc/generic/Tensor.cpp` file is not listed! So how does the code in this file end up being a part of the end product?", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Recall that we are calling the `THPTensor*` functions (such as `init`) from the directory above `generic`. If we take a look in this directory, there is another file `Tensor.cpp` defined. The last line of this file is important:\n```cpp\n//generic_include TH torch/csrc/generic/Tensor.cpp\n```\nNote that this `Tensor.cpp` file is included in `setup.py`, but it is wrapped in a call to a Python helper function called `split_types`. This function takes as input a file, and looks for the \"//generic_include\" string in the file contents. If it is found, it generates a new output file for each Tensor type, with the following changes:\n\n- The output file is renamed to `Tensor.cpp`\n- The output file is slightly modified as follows:\n\n```cpp\n# Before:\n//generic_include TH torch/csrc/generic/Tensor.cpp", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "# After:\n#define TH_GENERIC_FILE \"torch/src/generic/Tensor.cpp\"\n#include \"TH/THGenerateType.h\"\n```\nIncluding the header file on the second line has the side effect of including the source code in `Tensor.cpp` with some additional context defined. Let's take a look at one of the headers:\n\n```cpp\n#ifndef TH_GENERIC_FILE\n#error \"You must define TH_GENERIC_FILE before including THGenerateFloatType.h\"\n#endif\n\n#define real float\n#define accreal double\n#define TH_CONVERT_REAL_TO_ACCREAL(_val) (accreal)(_val)\n#define TH_CONVERT_ACCREAL_TO_REAL(_val) (real)(_val)\n#define Real Float\n#define THInf FLT_MAX\n#define TH_REAL_IS_FLOAT\n#line 1 TH_GENERIC_FILE\n#include TH_GENERIC_FILE\n#undef accreal\n#undef real\n#undef Real\n#undef THInf\n#undef TH_REAL_IS_FLOAT\n#undef TH_CONVERT_REAL_TO_ACCREAL\n#undef TH_CONVERT_ACCREAL_TO_REAL\n\n#ifndef THGenerateManyTypes\n#undef TH_GENERIC_FILE\n#endif\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "What this is doing is bringing in the code from the generic `Tensor.cpp` file and surrounding it with the following macro definitions. For example, we define real as a float, so any code in the generic Tensor implementation that refers to something as a real will have that real replaced with a float. In the corresponding file `THGenerateIntType.h`, the same macro would replace `real` with `int`.\n\nThese output files are returned from `split_types` and added to the list of source files, so we can see how the `.cpp` code for different types is created.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "There are a few things to note here: First, the `split_types` function is not strictly necessary. We could wrap the code in `Tensor.cpp` in a single file, repeating it for each type. The reason we split the code into separate files is to speed up compilation. Second, what we mean when we talk about the type replacement (e.g. replace real with a float) is that the C preprocessor will perform these substitutions during compilation. Merely surrounding the source code with these macros has no side effects until preprocessing.\n\n### Generic Builds (Part Two)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Now that we have source files for all the Tensor types, we need to consider how the corresponding header declarations are created, and also how the conversions from `THTensor_(method)` and `THPTensor_(method)` to `THTensor_method` and `THPTensor_method` work. For example, `csrc/generic/Tensor.h` has declarations like:\n```cpp\nTHP_API PyObject * THPTensor_(New)(THTensor *ptr);\n```\nWe use the same strategy for generating code in the source files for the headers. In `csrc/Tensor.h`, we do the following:\n```cpp\n#include \"generic/Tensor.h\"\n#include \n\n#include \"generic/Tensor.h\"\n#include \n```\nThis has the same effect, where we draw in the code from the generic header, wrapped with the same macro definitions, for each type. The only difference is that the resulting code is contained all within the same header file, as opposed to being split into multiple source files.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Lastly, we need to consider how we \"convert\" or \"substitute\" the function types. If we look in the same header file, we see a bunch of `#define` statements, including:\n```cpp\n#define THPTensor_(NAME) TH_CONCAT_4(THP,Real,Tensor_,NAME)\n```\nThis macro says that any string in the source code matching the format `THPTensor_(NAME)` should be replaced with `THPRealTensor_NAME`, where Real is derived from whatever the symbol Real is `#define`'d to be at the time. Because our header code and source code is surrounded by macro definitions for all the types as seen above, after the preprocessor has run, the resulting code is what we would expect. The code in the `TH` library defines the same macro for `THTensor_(NAME)`, supporting the translation of those functions as well. In this way, we end up with header and source files with specialized code.\n\n#### Module Objects and Type Methods", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Now we have seen how we have wrapped `TH`'s Tensor definition in `THP`, and generated THP methods such as `THPFloatTensor_init(...)`. Now we can explore what the above code actually does in terms of the module we are creating. The key line in `THPTensor_(init)` is:\n```cpp\n# THPTensorBaseStr, THPTensorType are also macros that are specific\n# to each type\nPyModule_AddObject(module, THPTensorBaseStr, (PyObject *)&THPTensorType);\n```\nThis function registers our Tensor objects to the extension module, so we can use THPFloatTensor, THPIntTensor, etc. in our Python code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Just being able to create Tensors isn't very useful - we need to be able to call all the methods that `TH` defines. A simple example shows calling the in-place `zero_` method on a Tensor.\n```python\nx = torch.FloatTensor(10)\nx.zero_()\n```\nLet's start by seeing how we add methods to newly defined types. One of the fields in the \"type object\" is `tp_methods`. This field holds an array of method definitions (`PyMethodDef`s) and is used to associate methods (and their underlying C/C++ implementations) with a type. Suppose we wanted to define a new method on our `PyFloatObject` that replaces the value. We could implement this as follows:\n```cpp\nstatic PyObject * replace(PyFloatObject *self, PyObject *args) {\n\tdouble val;\n\tif (!PyArg_ParseTuple(args, \"d\", &val))\n\t\treturn NULL;\n\tself->ob_fval = val;\n\tPy_RETURN_NONE\n}\n```\nThis is equivalent to the Python method:\n```python\ndef replace(self, val):\n\tself.ob_fval = val\n```\nIt is instructive to read more about how defining methods works in CPython. In general, methods take as the first parameter the instance of the object, and optionally parameters for the positional arguments and keyword arguments. This static function is registered as a method on our float:\n```cpp\nstatic PyMethodDef float_methods[] = {\n\t{\"replace\", (PyCFunction)replace, METH_VARARGS,\n\t\"replace the value in the float\"\n\t},\n\t{NULL} /* Sentinel */\n}\n```\nThis registers a method called replace, which is implemented by the C function of the same name. The `METH_VARARGS` flag indicates that the method takes a tuple of arguments representing all the arguments to the function. This array is set to the `tp_methods` field of the type object, and then we can use the `replace` method on objects of that type.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "We would like to be able to call all of the methods for `TH` tensors on our `THP` tensor equivalents. However, writing wrappers for all of the `TH` methods would be time-consuming and error prone. We need a better way to do this.\n\n### PyTorch cwrap\n\nPyTorch implements its own cwrap tool to wrap the `TH` Tensor methods for use in the Python backend. We define a `.cwrap` file containing a series of C method declarations in our custom [YAML format](http://yaml.org). The cwrap tool takes this file and outputs `.cpp` source files containing the wrapped methods in a format that is compatible with our `THPTensor` Python object and the Python C extension method calling format. This tool is used to generate code to wrap not only `TH`, but also `CuDNN`. It is defined to be extensible.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "An example YAML \"declaration\" for the in-place `addmv_` function is as follows:\n```\n[[\n name: addmv_\n cname: addmv\n return: self\n arguments:\n - THTensor* self\n - arg: real beta\n default: AS_REAL(1)\n - THTensor* self\n - arg: real alpha\n default: AS_REAL(1)\n - THTensor* mat\n - THTensor* vec\n]]\n```\nThe architecture of the cwrap tool is very simple. It reads in a file, and then processes it with a series of **plugins.** See `tools/cwrap/plugins/__init__.py` for documentation on all the ways a plugin can alter the code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "The source code generation occurs in a series of passes. First, the YAML \"declaration\" is parsed and processed. Then the source code is generated piece-by-piece - adding things like argument checks and extractions, defining the method header, and the actual call to the underlying library such as `TH`. Finally, the cwrap tool allows for processing the entire file at a time. The resulting output for `addmv_` can be [explored here](https://gist.github.com/killeent/c00de46c2a896335a52552604cc4d74b).\n\nIn order to interface with the CPython backend, the tool generates an array of `PyMethodDef`s that can be stored or appended to the `THPTensor`'s `tp_methods` field.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "In the specific case of wrapping Tensor methods, the build process first generates the output source file from `TensorMethods.cwrap`. This source file is `#include`'d in the generic Tensor source file. This all occurs before the preprocessor does its magic. As a result, all of the method wrappers that are generated undergo the same pass as the `THPTensor` code above. Thus a single generic declaration and definition is specialized for each type as well.\n\n### Putting It All Together\n\nSo far, we have shown how we extend the Python interpreter to create a new extension module, how such a module defines our new `THPTensor` type, and how we can generate source code for Tensors of all types that interface with `TH`. Briefly, we will touch on compilation.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "Setuptools allows us to define an Extension for compilation. The entire `torch._C` extension is compiled by collecting all of the source files, header files, libraries, etc. and creating a setuptools `Extension`. Then setuptools handles building the extension itself. I will explore the build process more in a subsequent post.\n\nTo summarize, let's revisit our four questions:\n\n- **How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?**\n\nIt uses CPython's framework for extending the Python interpreter and defining new types, while taking special care to generate code for all types.\n\n- **How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?**\n\nIt does so by defining a new type, `THPTensor`, that is backed by a `TH` Tensor. Function calls are forwarded to this tensor via the CPython backend's conventions.\n\n- **How does PyTorch cwrap work to generate code for Tensor methods?**", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "It takes our custom YAML-formatted code and generates source code for each method by processing it through a series of steps using a number of plugins.\n\n- **How does PyTorch's build system take all of these components to compile and generate a workable application?**\n\nIt takes a bunch of source/header files, libraries, and compilation directives to build an extension using Setuptools.\n\nThis is just a snapshot of parts of the build system for PyTorch. There is more nuance, and detail, but I hope this serves as a gentle introduction to a lot of the components of our Tensor library.\n\n### Resources:\n\n - is invaluable for understanding how to write C/C++ Extension to Python", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-1/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.12: TorchArrow, Functional API for Modules and nvFuser, are now available\"\nauthor: Team PyTorch\nfeatured-img: ''\n---\n\nWe are excited to announce the release of PyTorch 1.12 ([release note](https://github.com/pytorch/pytorch/releases/tag/v1.12.0))! This release is composed of over 3124 commits, 433 contributors. Along with 1.12, we are releasing beta versions of AWS S3 Integration, PyTorch Vision Models on Channels Last on CPU, Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16 and FSDP API. We want to sincerely thank our dedicated community for your contributions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "Summary:\n- Functional APIs to functionally apply module computation with a given set of parameters\n- Complex32 and Complex Convolutions in PyTorch\n- DataPipes from TorchData fully backward compatible with DataLoader \n- functorch with improved coverage for APIs\n- nvFuser a deep learning compiler for PyTorch\n- Changes to float32 matrix multiplication precision on Ampere and later CUDA hardware\n- TorchArrow, a new beta library for machine learning preprocessing over batch data\n\n## Frontend APIs\n\n### Introducing TorchArrow\n\nWe\u2019ve got a new Beta release ready for you to try and use: TorchArrow. This is a library for machine learning preprocessing over batch data. It features a performant and Pandas-style, easy-to-use API in order to speed up your preprocessing workflows and development.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "Currently, it provides a Python DataFrame interface with the following features:\n- High-performance CPU backend, vectorized and extensible User-Defined Functions (UDFs) with [Velox](https://github.com/facebookincubator/velox)\n- Seamless handoff with PyTorch or other model authoring, such as Tensor collation and easily plugging into PyTorch DataLoader and DataPipes\n- Zero copy for external readers via Arrow in-memory columnar format\n\nFor more details, please find our [10-min tutorial](https://github.com/pytorch/torcharrow/blob/main/tutorial/tutorial.ipynb), installation [instructions](https://github.com/pytorch/torcharrow#installation), API [documentation](https://pytorch.org/torcharrow/beta/), and a [prototype](https://github.com/pytorch/torchrec/tree/main/examples/torcharrow) for data preprocessing in TorchRec.\n\n### (Beta) Functional API for Modules", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 1.12 introduces a new beta feature to functionally apply Module computation with a given set of parameters. Sometimes, the traditional PyTorch Module usage pattern that maintains a static set of parameters internally is too restrictive. This is often the case when implementing algorithms for meta-learning, where multiple sets of parameters may need to be maintained across optimizer steps. \n\nThe new ``torch.nn.utils.stateless.functional_call()`` API allows for: \n- Module computation with full flexibility over the set of parameters used\n- No need to reimplement your module in a functional way\n- Any parameter or buffer present in the module can be swapped with an externally-defined value for use in the call. Naming for referencing parameters / buffers follows the fully-qualified form in the module\u2019s ``state_dict()``\n\nExample:\n```python\nimport torch\nfrom torch import nn\nfrom torch.nn.utils.stateless import functional_call", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "class MyModule(nn.Module):\n def __init__(self):\n super().__init__()\n self.fc1 = nn.Linear(3, 3)\n self.bn = nn.BatchNorm1d(3)\n self.fc2 = nn.Linear(3, 3)\n\n def forward(self, x):\n return self.fc2(self.bn(self.fc1(x)))\n\nm = MyModule()\n\n# Define parameter / buffer values to use during module computation.\nmy_weight = torch.randn(3, 3, requires_grad=True)\nmy_bias = torch.tensor([1., 2., 3.], requires_grad=True)\nparams_and_buffers = {\n 'fc1.weight': my_weight,\n 'fc1.bias': my_bias,\n # Custom buffer values can be used too.\n 'bn.running_mean': torch.randn(3),\n}\n\n# Apply module computation to the input with the specified parameters / buffers.\ninp = torch.randn(5, 3)\noutput = functional_call(m, params_and_buffers, inp)\n```\n\n### (Beta) Complex32 and Complex Convolutions in PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch today natively supports complex numbers, complex autograd, complex modules, and numerous complex operations, including linear algebra and Fast Fourier Transform (FFT) operators. Many libraries, including torchaudio and ESPNet, already make use of complex numbers in PyTorch, and PyTorch 1.12 further extends complex functionality with complex convolutions and the experimental complex32 (\u201ccomplex half\u201d) data type that enables half precision FFT operations. Due to the bugs in CUDA 11.3 package, we recommend using CUDA 11.6 package from wheels if you are using complex numbers.\n\n### (Beta) Forward-mode Automatic Differentiation", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "Forward-mode AD allows the computation of directional derivatives (or equivalently, Jacobian-vector products) eagerly in the forward pass. PyTorch 1.12 significantly improves the operator coverage for forward-mode AD. See our [tutorial](https://pytorch.org/tutorials/search.html?q=forward-mode+automatic+differentiation+%28beta%29&check_keywords=yes&area=default#) for more information.\n\n### TorchData \n\n#### BC DataLoader + DataPipe\n\n\\`DataPipe\\` from TorchData becomes fully backward compatible with the existing \\`DataLoader\\` regarding shuffle determinism and dynamic sharding in both multiprocessing and distributed environments. For more details, please check out the [tutorial](https://pytorch.org/data/0.4.0/tutorial.html#working-with-dataloader).\n\n#### (Beta) AWS S3 Integration", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "DataPipes based on [AWSSDK](https://github.com/aws/aws-sdk-cpp) have been integrated into TorchData. It provides the following features backed by native AWSSDK:\n- Retrieve list of urls from each S3 bucket based on prefix\n\t- Support timeout to prevent hanging indefinitely\n\t- Support to specify S3 bucket region\n\t\t\n- Load data from S3 urls\n\t- Support buffered and multi-part download\n\t- Support to specify S3 bucket region\n\nAWS native DataPipes are still in the beta phase. And, we will keep tuning them to improve their performance.\n\n#### (Prototype) DataLoader2\n\nDataLoader2 became available in prototype mode. We are introducing new ways to interact between DataPipes, DataLoading API, and backends (aka ReadingServices). Feature is stable in terms of API, but functionally not complete yet. We welcome early adopters and feedback, as well as potential contributors.\n\nFor more details, please checkout the [link](https://github.com/pytorch/data/tree/main/torchdata/dataloader2).\n\n### functorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "### functorch\n\nInspired by [Google JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples of these include:\n- [running ensembles of models on a single machine](https://pytorch.org/functorch/stable/notebooks/ensembling.html)\n- [efficiently computing Jacobians and Hessians](https://pytorch.org/functorch/stable/notebooks/jacobians_hessians.html)\n- [computing per-sample-gradients (or other per-sample quantities)](https://pytorch.org/functorch/stable/notebooks/per_sample_grads.html)\n\nWe\u2019re excited to announce functorch 0.2.0 with a number of improvements and new experimental features.\n\n#### Significantly improved coverage\n\nWe significantly improved coverage for ``functorch.jvp`` (our forward-mode autodiff API) and other APIs that rely on it (``functorch.{jacfwd, hessian}``).\n\n#### (Prototype) functorch.experimental.functionalize", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "Given a function f, ``functionalize(f)`` returns a new function without mutations (with caveats). This is useful for constructing traces of PyTorch functions without in-place operations. For example, you can use ``make_fx(functionalize(f))`` to construct a mutation-free trace of a pytorch function. To learn more, please see the [documentation](https://pytorch.org/functorch/stable/generated/functorch.experimental.functionalize.html#functorch.experimental.functionalize).\n\nFor more details, please see our [installation instructions](https://pytorch.org/functorch/stable/install.html), [documentation](https://pytorch.org/functorch/), [tutorials](https://pytorch.org/functorch), and [release notes](https://github.com/pytorch/functorch/releases).\n\n## Performance Improvements\n\n### Introducing nvFuser, a deep learning compiler for PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "In PyTorch 1.12, Torchscript is updating its default fuser (for Volta and later CUDA accelerators) to nvFuser, which supports a wider range of operations and is faster than NNC, the previous fuser for CUDA devices. A soon to be published blog post will elaborate on nvFuser and show how it speeds up training on a variety of networks. \n\nSee [the nvFuser documentation](https://github.com/pytorch/pytorch/blob/release/1.12/torch/csrc/jit/codegen/cuda/README.md) for more details on usage and debugging.\n\n### Changes to float32 matrix multiplication precision on Ampere and later CUDA hardware", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch supports a variety of \u201cmixed precision\u201d techniques, like the torch.amp (Automated Mixed Precision) module and performing float32 matrix multiplications using the TensorFloat32 datatype on Ampere and later CUDA hardware for faster internal computations. In PyTorch 1.12 we\u2019re changing the default behavior of float32 matrix multiplications to always use full IEEE fp32 precision, which is more precise but slower than using the TensorFloat32 datatype for internal computation. For devices with a particularly high ratio of TensorFloat32 to float32 throughput such as A100, this change in defaults can result in a large slowdown.\n\nIf you\u2019ve been using TensorFloat32 matrix multiplications then you can continue to do so by setting ``torch.backends.cuda.matmul.allow_tf32 = True``\n\nwhich is supported since PyTorch 1.7. Starting in PyTorch 1.12 the new matmul precision API can be used, too: ``torch.set_float32_matmul_precision(\u201chighest\u201d|\u201dhigh\u201d|\u201dmedium\u201d)``", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "To reiterate, PyTorch\u2019s new default is \u201chighest\u201d precision for all device types. We think this provides better consistency across device types for matrix multiplications. Documentation for the new precision API can be found [here](https://pytorch.org/docs/master/generated/torch.set_float32_matmul_precision.html?highlight=precision#torch.set_float32_matmul_precision). Setting the \u201chigh\u201d or \u201cmedium\u201d precision types will enable TensorFloat32 on Ampere and later CUDA devices. If you\u2019re updating to PyTorch 1.12 then to preserve the current behavior and faster performance of matrix multiplications on Ampere devices, set precision to \u201chigh\u201d.\n\nUsing mixed precision techniques is essential for training many modern deep learning networks efficiently, and if you\u2019re already using torch.amp this change is unlikely to affect you. If you\u2019re not familiar with mixed precision training then see our soon to be published \u201cWhat Every User Should Know About Mixed Precision Training in PyTorch\u201d blogpost.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Accelerating PyTorch Vision Models with Channels Last on CPU\n\nMemory formats have a significant impact on performance when running vision models, generally Channels Last is more favorable from a performance perspective due to better data locality. 1.12 includes fundamental concepts of memory formats and demonstrates performance benefits using Channels Last on popular PyTorch vision models on Intel\u00ae Xeon\u00ae Scalable processors.\n- Enables Channels Last memory format support for the commonly used operators in CV domain on CPU, applicable for both inference and training\n- Provides native level optimization on Channels Last kernels from ATen, applicable for both AVX2 and AVX512\n- Delivers 1.3x to 1.8x inference performance gain over Channels First for TorchVision models on Intel\u00ae Xeon\u00ae Ice Lake (or newer) CPUs\n\n### (Beta) Empowering PyTorch on Intel\u00ae Xeon\u00ae Scalable processors with Bfloat16", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "Reduced precision numeric formats like bfloat16 improves PyTorch performance across multiple deep learning training workloads. PyTorch 1.12 includes the latest software enhancements on bfloat16 which applies to a broader scope of user scenarios and showcases even higher performance gains. The main improvements include:\n- 2x hardware compute throughput vs. float32 with the new bfloat16 native instruction VDPBF16PS, introduced on Intel\u00ae Xeon\u00ae Cooper Lake CPUs\n- 1/2 memory footprint of float32, faster speed for memory bandwidth intensive operators\n- 1.4x to 2.2x inference performance gain over float32 for TorchVision models on Intel\u00ae Xeon\u00ae Cooper Lake (or newer) CPUs\n\n### (Prototype) Introducing Accelerated PyTorch Training on Mac", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "With the PyTorch 1.12 release, developers and researchers can now take advantage of Apple silicon GPUs for significantly faster model training. This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac. Accelerated GPU training is enabled using Apple\u2019s Metal Performance Shaders (MPS) as a backend. The benefits include performance speedup from accelerated GPU training and the ability to train larger networks or batch sizes locally. Learn more [here](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/). \n\n

\n \n

\n\n

\n Accelerated GPU training and evaluation speedups over CPU-only (times faster)\n

\n\nAlongside the new MPS device support, the M1 binaries for Core and Domain libraries that have been available for the last few releases are now an official prototype feature. These binaries can be used to run PyTorch natively on Apple Silicon.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "### (Prototype) BetterTransformer: Fastpath execution for Transformer Encoder Inference", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "PyTorch now supports CPU and GPU fastpath implementations (\u201cBetterTransformer\u201d) for several Transformer Encoder modules including TransformerEncoder, TransformerEncoderLayer, and MultiHeadAttention (MHA). The BetterTransformer fastpath architecture Better Transformer is consistently faster \u2013 2x for many common execution scenarios, depending on model and input characteristics. The new BetterTransformer-enabled modules are API compatible with previous releases of the PyTorch Transformer API and will accelerate existing models if they meet fastpath execution requirements, as well as read models trained with previous versions of PyTorch. PyTorch 1.12 includes: \n- BetterTransformer integration for Torchtext\u2019s pretrained RoBERTa and XLM-R models\n- Torchtext which builds on the PyTorch Transformer API\n- Fastpath execution for improved performance by reducing execution overheads with fused kernels which combines multiple operators into a single kernel\n- Option to achieve additional speedups by taking advantage of data sparsity during the processing of padding tokens in natural-language processing (by setting enable_nested_tensor=True when creating a TransformerEncoder)\n- Diagnostics to help users understand why fastpath execution did not occur", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\n## Distributed\n\n### (Beta) Fully Sharded Data Parallel (FSDP) API\n\n[FSDP API](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/) helps easily scale large model training by sharding a model\u2019s parameters, gradients and optimizer states across data parallel workers while maintaining the simplicity of data parallelism. The prototype version was released in PyTorch 1.11 with a minimum set of features that helped [scaling tests of models with up to 1T parameters](https://medium.com/pytorch/training-a-1-trillion-parameter-model-with-pytorch-fully-sharded-data-parallel-on-aws-3ac13aa96cff).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "In this beta release, FSDP API added the following features to support various production workloads. Highlights of the the newly added features in this beta release include: \n1. Universal sharding strategy API - Users can easily change between sharding strategies with a single line change, and thus compare and use DDP (only data sharding), FSDP (full model and data sharding), or Zero2 (only sharding of optimizer and gradients) to optimize memory and performance for their specific training needs\n2. Fine grained mixed precision policies - Users can specify a mix of half and full data types (bfloat16, fp16 or fp32) for model parameters, gradient communication, and buffers via mixed precision policies. Models are automatically saved in fp32 to allow for maximum portability\n3. Transformer auto wrapping policy - allows for optimal wrapping of Transformer based models by registering the models layer class, and thus accelerated training performance\n4. Faster model initialization using device_id init - initialization is performed in a streaming fashion to avoid OOM issues and optimize init performance vs CPU init \n5. Rank0 streaming for full model saving of larger models - Fully sharded models can be saved by all GPU\u2019s streaming their shards to the rank 0 GPU, and the model is built in full state on the rank 0 CPU for saving", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "For more details and example code, please checkout the [documentation](https://pytorch.org/docs/1.11/fsdp.html?highlight=fsdp#module-torch.distributed.fsdp) and the [tutorial](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html). \n\n\nThanks for reading, If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch), and [LinkedIn](https://www.linkedin.com/company/pytorch).\n\nCheers! \n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.12-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.4 released, domain libraries updated'\nauthor: Team PyTorch\n---\n\nToday, we\u2019re announcing the availability of PyTorch 1.4, along with updates to the PyTorch domain libraries. These releases build on top of the announcements from [NeurIPS 2019](https://pytorch.org/blog/pytorch-adds-new-tools-and-libraries-welcomes-preferred-networks-to-its-community/), where we shared the availability of PyTorch Elastic, a new classification framework for image and video, and the addition of Preferred Networks to the PyTorch community. For those that attended the workshops at NeurIPS, the content can be found [here](https://research.fb.com/neurips-2019-expo-workshops/).\n\n## PyTorch 1.4\n\nThe 1.4 release of PyTorch adds new capabilities, including the ability to do fine grain build level customization for PyTorch Mobile, and new experimental features including support for model parallel training and Java language bindings.\n\n### PyTorch Mobile - Build level customization", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "Following the open sourcing of [PyTorch Mobile in the 1.3 release](https://pytorch.org/blog/pytorch-1-dot-3-adds-mobile-privacy-quantization-and-named-tensors/), PyTorch 1.4 adds additional mobile support including the ability to customize build scripts at a fine-grain level. This allows mobile developers to optimize library size by only including the operators used by their models and, in the process, reduce their on device footprint significantly. Initial results show that, for example, a customized MobileNetV2 is 40% to 50% smaller than the prebuilt PyTorch mobile library. You can learn more [here](https://pytorch.org/mobile/home/) about how to create your own custom builds and, as always, please engage with the community on the [PyTorch forums](https://discuss.pytorch.org/c/mobile) to provide any feedback you have.\n\nExample code snippet for selectively compiling only the operators needed for MobileNetV2:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "```python\n# Dump list of operators used by MobileNetV2:\nimport torch, yaml\nmodel = torch.jit.load('MobileNetV2.pt')\nops = torch.jit.export_opnames(model)\nwith open('MobileNetV2.yaml', 'w') as output:\n yaml.dump(ops, output)\n```\n\n```console\n# Build PyTorch Android library customized for MobileNetV2:\nSELECTED_OP_LIST=MobileNetV2.yaml scripts/build_pytorch_android.sh arm64-v8a\n\n# Build PyTorch iOS library customized for MobileNetV2:\nSELECTED_OP_LIST=MobileNetV2.yaml BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 scripts/build_ios.sh\n```\n\n### Distributed model parallel training (Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "With the scale of models, such as RoBERTa, continuing to increase into the billions of parameters, model parallel training has become ever more important to help researchers push the limits. This release provides a distributed RPC framework to support distributed model parallel training. It allows for running functions remotely and referencing remote objects without copying the real data around, and provides autograd and optimizer APIs to transparently run backwards and update parameters across RPC boundaries.\n\nTo learn more about the APIs and the design of this feature, see the links below:\n\n* [API documentation](https://pytorch.org/docs/stable/rpc.html)\n* [Distributed Autograd design doc](https://pytorch.org/docs/stable/notes/distributed_autograd.html)\n* [Remote Reference design doc](https://pytorch.org/docs/stable/notes/rref.html)\n\nFor the full tutorials, see the links below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "* [A full RPC tutorial](https://pytorch.org/tutorials/intermediate/rpc_tutorial.html)\n* [Examples using model parallel training for reinforcement learning and with an LSTM](https://github.com/pytorch/examples/tree/master/distributed/rpc)\n\nAs always, you can connect with community members and discuss more on the [forums](https://discuss.pytorch.org/c/distributed/distributed-rpc).\n\n### Java bindings (Experimental)\n\nIn addition to supporting Python and C++, this release adds experimental support for Java bindings. Based on the interface developed for Android in PyTorch Mobile, the new bindings allow you to invoke TorchScript models from any Java program. Note that the Java bindings are only available for Linux for this release, and for inference only. We expect support to expand in subsequent releases. See the code snippet below for how to use PyTorch within Java:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "```java\nModule mod = Module.load(\"demo-model.pt1\");\nTensor data =\n Tensor.fromBlob(\n new int[] {1, 2, 3, 4, 5, 6}, // data\n new long[] {2, 3} // shape\n );\nIValue result = mod.forward(IValue.from(data), IValue.from(3.0));\nTensor output = result.toTensor();\nSystem.out.println(\"shape: \" + Arrays.toString(output.shape()));\nSystem.out.println(\"data: \" + Arrays.toString(output.getDataAsFloatArray()));\n```\n\nLearn more about how to use PyTorch from Java [here](https://github.com/pytorch/java-demo), and see the full Javadocs API documentation [here](https://pytorch.org/javadoc/1.4.0/).\n\nFor the full 1.4 release notes, see [here](https://github.com/pytorch/pytorch/releases).\n\n## Domain Libraries\n\nPyTorch domain libraries like torchvision, torchtext, and torchaudio complement PyTorch with common datasets, models, and transforms. We\u2019re excited to share new releases for all three domain libraries alongside the PyTorch 1.4 core release.\n\n### torchvision 0.5", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "### torchvision 0.5\n\nThe improvements to torchvision 0.5 mainly focus on adding support for production deployment including quantization, TorchScript, and ONNX. Some of the highlights include:\n\n* All models in torchvision are now torchscriptable making them easier to ship into non-Python production environments\n* ResNets, MobileNet, ShuffleNet, GoogleNet and InceptionV3 now have quantized counterparts with pre-trained models, and also include scripts for quantization-aware training.\n* In partnership with the Microsoft team, we\u2019ve added ONNX support for all models including Mask R-CNN.\n\nLearn more about torchvision 0.5 [here](https://github.com/pytorch/vision/releases).\n\n### torchaudio 0.4\n\nImprovements in torchaudio 0.4 focus on enhancing the currently available transformations, datasets, and backend support. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "* SoX is now optional, and a new extensible backend dispatch mechanism exposes SoundFile as an alternative to SoX.\n* The interface for datasets has been unified. This enables the addition of two large datasets: LibriSpeech and Common Voice.\n* New filters such as biquad, data augmentation such as time and frequency masking, transforms such as MFCC, gain and dither, and new feature computation such as deltas, are now available.\n* Transformations now support batches and are jitable.\n* An interactive speech recognition demo with voice activity detection is available for experimentation.\n\nLearn more about torchaudio 0.4 [here](https://github.com/pytorch/audio/releases).\n\n### torchtext 0.5\n\ntorchtext 0.5 focuses mainly on improvements to the dataset loader APIs, including compatibility with core PyTorch APIs, but also adds support for unsupervised text tokenization. Highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "* Added bindings for SentencePiece for unsupervised text tokenization .\n* Added a new unsupervised learning dataset - enwik9.\n* Made revisions to PennTreebank, WikiText103, WikiText2, IMDb to make them compatible with torch.utils.data. Those datasets are in an experimental folder and we welcome your feedback.\n\nLearn more about torchtext 0.5 [here](https://github.com/pytorch/text/releases).\n\n*We\u2019d like to thank the entire PyTorch team and the community for all their contributions to this work.*\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1-dot-4-released-and-domain-libraries-updated/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Stochastic Weight Averaging in PyTorch'\nauthor: Pavel Izmailov and Andrew Gordon Wilson\nredirect_from: /2019/04/29/road-to-1.0.html\n---\n\nIn this blogpost we describe the recently proposed Stochastic Weight Averaging (SWA) technique [1, 2], and its new implementation in [`torchcontrib`](https://github.com/pytorch/contrib). SWA is a simple procedure that improves generalization in deep learning over Stochastic Gradient Descent (SGD) at no additional cost, and can be used as a drop-in replacement for any other optimizer in PyTorch. SWA has a wide range of applications and features:", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "1. SWA has been shown to significantly improve generalization in computer vision tasks, including VGG, ResNets, Wide ResNets and DenseNets on ImageNet and CIFAR benchmarks [1, 2].\n2. SWA provides state-of-the-art performance on key benchmarks in semi-supervised learning and domain adaptation [2].\n3. SWA is shown to improve the stability of training as well as the final average rewards of policy-gradient methods in deep reinforcement learning [3].\n4. An extension of SWA can obtain efficient Bayesian model averaging, as well as high quality uncertainty estimates and calibration in deep learning [4].\n5. SWA for low precision training, SWALP, can match the performance of full-precision SGD even with all numbers quantized down to 8 bits, including gradient accumulators [5].", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In short, SWA performs an equal average of the weights traversed by SGD with a modified learning rate schedule (see the left panel of Figure 1.). SWA solutions end up in the center of a wide flat region of loss, while SGD tends to converge to the boundary of the low-loss region, making it susceptible to the shift between train and test error surfaces (see the middle and right panels of Figure 1).\n\n
\n \n
\n\n**Figure 1.** Illustrations of SWA and SGD with a Preactivation ResNet-164 on CIFAR-100 [1]. **Left:** test error surface for three FGE samples and the corresponding SWA solution (averaging in weight space). **Middle** and **Right:** test error and train loss surfaces showing the weights proposed by SGD (at convergence) and SWA, starting from the same initialization of SGD after 125 training epochs. Please see [1] for details on how these figures were constructed.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**With our new implementation in [torchcontrib](https://github.com/pytorch/contrib) using SWA is as easy as using any other optimizer in PyTorch:**\n\n```python\nfrom torchcontrib.optim import SWA\n\n...\n...\n\n# training loop\nbase_opt = torch.optim.SGD(model.parameters(), lr=0.1)\nopt = torchcontrib.optim.SWA(base_opt, swa_start=10, swa_freq=5, swa_lr=0.05)\nfor _ in range(100):\n opt.zero_grad()\n loss_fn(model(input), target).backward()\n opt.step()\nopt.swap_swa_sgd()\n```\n\nYou can wrap any optimizer from `torch.optim` using the `SWA` class, and then train your model as usual. When training is complete you simply call `swap_swa_sgd()` to set the weights of your model to their SWA averages. Below we explain the SWA procedure and the parameters of the `SWA` class in detail. We emphasize that SWA can be combined with *any* optimization procedure, such as Adam, in the same way that it can be combined with SGD.\n\n## Is this just Averaged SGD?", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "At a high level, averaging SGD iterates dates back several decades in convex optimization [6, 7], where it is sometimes referred to as Polyak-Ruppert averaging, or *averaged* SGD. **But the details matter**. *Averaged SGD* is often employed in conjunction with a decaying learning rate, and an exponentially moving average, typically for convex optimization. In convex optimization, the focus has been on improved rates of convergence. In deep learning, this form of averaged SGD smooths the trajectory of SGD iterates, but does not perform very differently.\n\nBy contrast, SWA is focused on an **equal average** of SGD iterates with a modified **cyclical or high constant learning rate**, and exploits the flatness of training objectives [8] specific to **deep learning** for **improved generalization**.\n\n## Stochastic Weight Averaging", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "There are two important ingredients that make SWA work. First, SWA uses a modified learning rate schedule so that SGD continues to explore the set of high-performing networks instead of simply converging to a single solution. For example, we can use the standard decaying learning rate strategy for the first 75% of training time, and then set the learning rate to a reasonably high constant value for the remaining 25% of the time (see the Figure 2 below). The second ingredient is to average the weights of the networks traversed by SGD. For example, we can maintain a running average of the weights obtained in the end of every epoch within the last 25% of training time (see Figure 2).\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Figure 2.** Illustration of the learning rate schedule adopted by SWA. Standard decaying schedule is used for the first 75% of the training and then a high constant value is used for the remaining 25%. The SWA averages are formed during the last 25% of training.\n\nIn our implementation the auto mode of the `SWA` optimizer allows us to run the procedure described above. To run SWA in auto mode you just need to wrap your optimizer `base_opt` of choice (can be SGD, Adam, or any other `torch.optim.Optimizer`) with `SWA(base_opt, swa_start, swa_freq, swa_lr)`. After `swa_start` optimization steps the learning rate will be switched to a constant value `swa_lr`, and in the end of every `swa_freq` optimization steps a snapshot of the weights will be added to the SWA running average. Once you run `opt.swap_swa_sgd()`, the weights of your model are replaced with their SWA running averages.\n\n## Batch Normalization", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "One important detail to keep in mind is batch normalization. Batch normalization layers compute running statistics of activations during training. Note that the SWA averages of the weights are never used to make predictions during training, and so the batch normalization layers do not have the activation statistics computed after you reset the weights of your model with `opt.swap_swa_sgd()`. To compute the activation statistics you can just make a forward pass on your training data using the SWA model once the training is finished. In the `SWA` class we provide a helper function `opt.bn_update(train_loader, model)`. It updates the activation statistics for every batch normalization layer in the model by making a forward pass on the `train_loader` data loader. You only need to call this function once in the end of training.\n\n## Advanced Learning-Rate Schedules", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "SWA can be used with any learning rate schedule that encourages exploration of the flat region of solutions. For example, you can use cyclical learning rates in the last 25% of the training time instead of a constant value, and average the weights of the networks corresponding to the lowest values of the learning rate within each cycle (see Figure 3).\n\n
\n \n
\n\n**Figure 3.** Illustration of SWA with an alternative learning rate schedule. Cyclical learning rates are adopted in the last 25% of training, and models for averaging are collected in the end of each cycle.\n\nIn our implementation you can implement custom learning rate and weight averaging strategies by using `SWA` in the manual mode. The following code is equivalent to the auto mode code presented in the beginning of this blogpost.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nopt = torchcontrib.optim.SWA(base_opt)\nfor i in range(100):\n opt.zero_grad()\n loss_fn(model(input), target).backward()\n opt.step()\n if i > 10 and i % 5 == 0:\n opt.update_swa()\nopt.swap_swa_sgd()\n```\n\nIn manual mode you don\u2019t specify `swa_start`, `swa_lr` and `swa_freq`, and just call `opt.update_swa()` whenever you want to update the SWA running averages (for example in the end of each learning rate cycle). In manual mode `SWA` doesn\u2019t change the learning rate, so you can use any schedule you want as you would normally do with any other `torch.optim.Optimizer`.\n\n## Why does it work?\n\nSGD converges to a solution within a wide flat region of loss. The weight space is extremely high-dimensional, and most of the volume of the flat region is concentrated near the boundary, so SGD solutions will always be found near the boundary of the flat region of the loss. SWA on the other hand averages multiple SGD solutions, which allows it to move towards the center of the flat region.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We expect solutions that are centered in the flat region of the loss to generalize better than those near the boundary. Indeed, train and test error surfaces are not perfectly aligned in the weight space. Solutions that are centered in the flat region are not as susceptible to the shifts between train and test error surfaces as those near the boundary. In Figure 4 below we show the train loss and test error surfaces along the direction connecting the SWA and SGD solutions. As you can see, while SWA solution has a higher train loss compared to the SGD solution, it is centered in the region of low loss, and has a substantially better test error.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Figure 4.** Train loss and test error along the line connecting the SWA solution (circle) and SGD solution (square). SWA solution is centered in a wide region of low train loss while the SGD solution lies near the boundary. Because of the shift between train loss and test error surfaces, SWA solution leads to much better generalization.\n\n## Examples and Results\n\nWe released a GitHub repo [here](https://github.com/izmailovpavel/contrib_swa_examples) with examples of using the `torchcontrib` implementation of SWA for training DNNs. For example, these examples can be used to achieve the following results on CIFAR-100:", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "| DNN (Budget) | SGD | SWA 1 Budget | SWA 1.25 Budgets | SWA 1.5 Budgets |\n| ------------------------- |:------------:|:------------:|:----------------:|:---------------:|\n| VGG16 (200) | 72.55 \u00b1 0.10 | 73.91 \u00b1 0.12 | 74.17 \u00b1 0.15 | 74.27 \u00b1 0.25 |\n| PreResNet110 (150) | 76.77 \u00b1 0.38 | 78.75 \u00b1 0.16 | 78.91 \u00b1 0.29 | 79.10 \u00b1 0.21 |\n| PreResNet164 (150) | 78.49 \u00b1 0.36 | 79.77 \u00b1 0.17 | 80.18 \u00b1 0.23 | 80.35 \u00b1 0.16 |\n| WideResNet28x10 (200) | 80.82 \u00b1 0.23 | 81.46 \u00b1 0.23 | 81.91 \u00b1 0.27 | 82.15 \u00b1 0.27 |\n\n## Semi-Supervised Learning", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In a follow-up [paper](https://arxiv.org/abs/1806.05594) SWA was applied to semi-supervised learning, where it illustrated improvements beyond the best reported results in multiple settings. For example, with SWA you can get 95% accuracy on CIFAR-10 if you only have the training labels for 4k training data points (the previous best reported result on this problem was 93.7%). This paper also explores averaging multiple times within epochs, which can accelerate convergence and find still flatter solutions in a given time.\n
\n\n
\n\n**Figure 5.** Performance of fast-SWA on semi-supervised learning with CIFAR-10. fast-SWA achieves record results in every setting considered.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Calibration and Uncertainty Estimates\n[SWA-Gaussian](https://arxiv.org/abs/1902.02476) (SWAG) is a simple, scalable and convenient approach to uncertainty estimation and calibration in Bayesian deep learning. Similarly to SWA, which maintains a running average of SGD iterates, SWAG estimates the first and second moments of the iterates to construct a Gaussian distribution over weights. SWAG distribution approximates the shape of the true posterior: Figure 6 below shows the SWAG distribution on top of the posterior log-density for PreResNet-164 on CIFAR-100.\n
\n\n
\n**Figure 6.** SWAG distribution on top of posterior log-density for PreResNet-164 on CIFAR-100. The shape of SWAG distribution is aligned with the posterior.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Empirically, SWAG performs on par or better than popular alternatives including MC dropout, KFAC Laplace, and temperature scaling on uncertainty quantification, out-of-distribution detection, calibration and transfer learning in computer vision tasks. Code for SWAG is available [here](https://github.com/wjmaddox/swa_gaussian).\n\n## Reinforcement Learning\n\nIn another follow-up [paper](http://www.gatsby.ucl.ac.uk/~balaji/udl-camera-ready/UDL-24.pdf) SWA was shown to improve the performance of policy gradient methods A2C and DDPG on several Atari games and MuJoCo environments.\n\n| Environment | A2C | A2C + SWA |\n|---------------|:----------------:|:----------------:|\n| Breakout | 522 \u00b1 34 | 703 \u00b1 60 |\n| Qbert | 18777 \u00b1 778 | 21272 \u00b1 655 |\n| SpaceInvaders | 7727 \u00b1 1121 | 21676 \u00b1 8897 |\n| Seaquest | 1779 \u00b1 4 | 1795 \u00b1 4 |\n| CrazyClimber | 147030 \u00b1 10239 | 139752 \u00b1 11618 |\n| BeamRider | 9999 \u00b1 402 | 11321 \u00b1 1065 |", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Low Precision Training\nWe can filter through quantization noise by combining weights that have been rounded down with weights that have been rounded up. Moreover, by averaging weights to find a flat region of the loss surface, large perturbations of the weights will not affect the quality of the solution (Figures 7 and 8). Recent work shows that by adapting SWA to the low precision setting, in a method called SWALP, one can *match the performance of full-precision SGD even with all training in 8 bits* [5]. This is quite a practically important result, given that (1) SGD training in 8 bits performs notably worse than full precision SGD, and (2) low precision training is significantly harder than predictions in low precision after training (the usual setting). For example, a ResNet-164 trained on CIFAR-100 with float (16-bit) SGD achieves 22.2% error, while 8-bit SGD achieves 24.0% error. By contrast, SWALP with 8 bit training achieves 21.8% error.\n
\n\n
", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Figure 7.** Quantizing in a flat region can still provide solutions with low loss.\n\n
\n\n
\n\n**Figure 8.** Low precision SGD training (with a modified learning rate schedule) and SWALP.\n\n## Conclusion", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Conclusion\n\nOne of the greatest open questions in deep learning is why SGD manages to find good solutions, given that the training objectives are highly multimodal, and there are in principle many settings of parameters that achieve no training loss but poor generalization. By understanding geometric features such as flatness, which relate to generalization, we can begin to resolve these questions and build optimizers that provide even better generalization, and many other useful features, such as uncertainty representation. We have presented SWA, a simple drop-in replacement for standard SGD, which can in principle benefit anyone training a deep neural network. SWA has been demonstrated to have strong performance in a number of areas, including computer vision, semi-supervised learning, reinforcement learning, uncertainty representation, calibration, Bayesian model averaging, and low precision training.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} {"page_content": "We encourage you try out SWA! Using SWA is now as easy as using any other optimizer in PyTorch. And even if you have already trained your model with SGD (or any other optimizer), it\u2019s very easy to realize the benefits of SWA by running SWA for a small number of epochs starting with a pre-trained model.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [1] Averaging Weights Leads to Wider Optima and Better Generalization; Pavel Izmailov, Dmitry Podoprikhin, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson; Uncertainty in Artificial Intelligence (UAI), 2018\n- [2] There Are Many Consistent Explanations of Unlabeled Data: Why You Should Average; Ben Athiwaratkun, Marc Finzi, Pavel Izmailov, Andrew Gordon Wilson; International Conference on Learning Representations (ICLR), 2019", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [3] Improving Stability in Deep Reinforcement Learning with Weight Averaging; Evgenii Nikishin, Pavel Izmailov, Ben Athiwaratkun, Dmitrii Podoprikhin, Timur Garipov, Pavel Shvechikov, Dmitry Vetrov, Andrew Gordon Wilson, UAI 2018 Workshop: Uncertainty in Deep Learning, 2018\n- [4] A Simple Baseline for Bayesian Uncertainty in Deep Learning, Wesley Maddox, Timur Garipov, Pavel Izmailov, Andrew Gordon Wilson, arXiv pre-print, 2019: [https://arxiv.org/abs/1902.02476](https://arxiv.org/abs/1902.02476)", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [5] SWALP : Stochastic Weight Averaging in Low Precision Training, Guandao Yang, Tianyi Zhang, Polina Kirichenko, Junwen Bai, Andrew Gordon Wilson, Christopher De Sa, To appear at the International Conference on Machine Learning (ICML), 2019.\n- [6] David Ruppert. Efficient estimations from a slowly convergent Robbins-Monro process. Technical report, Cornell University Operations Research and Industrial Engineering, 1988.", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- [7] Acceleration of stochastic approximation by averaging. Boris T Polyak and Anatoli B Juditsky. SIAM Journal on Control and Optimization, 30(4):838\u2013855, 1992.\n- [8] Loss Surfaces, Mode Connectivity, and Fast Ensembling of DNNs, Timur Garipov, Pavel Izmailov, Dmitrii Podoprikhin, Dmitry Vetrov, Andrew Gordon Wilson. Neural Information Processing Systems (NeurIPS), 2018", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing the PlayTorch app: Rapidly Create Mobile AI Experiences\"\nauthor: PlayTorch Team\nfeatured-img: \"\"\n---\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "In December, we announced PyTorch Live, a toolkit for building AI-powered mobile prototypes in minutes. The initial release included a command-line interface to set up a development environment and an SDK for building AI-powered experiences in React Native. Today, we're excited to share that PyTorch Live will now be known as PlayTorch. This new release provides an improved and simplified developer experience. PlayTorch development is independent from the PyTorch project and the PlayTorch code repository is", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "moving into the Meta Research GitHub organization.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "A New Workflow: The PlayTorch App\n\nThe PlayTorch team is excited to announce that we have partnered with [Expo](https://expo.dev) to change the way AI powered mobile experiences are built. Our new release simplifies the process of building mobile AI experiences by eliminating the need for a complicated development environment. You will now be able to build cross platform AI powered prototypes from the very browser you are using to read this blog.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "In order to make this happen, we are releasing the [PlayTorch app](https://playtorch.dev/) which is able to run AI-powered experiences built in the [Expo Snack](https://snack.expo.dev/@playtorch/playtorch-starter?supportedPlatforms=my-device) web based code editor.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "The PlayTorch app can be downloaded from the Apple App Store and Google Play Store. With the app installed, you can head over to [playtorch.dev/snack](https://playtorch.dev/snack) and write the code for your AI-powered PlayTorch Snack. When you want to try what you\u2019ve built, you can use the PlayTorch app\u2019s QR code scanner to scan the QR code on the Snack page and load the code to your device.\n\nNOTE: PlayTorch Snacks will not work in the Expo Go app.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "More to Explore in the PlayTorch App\n\n### AI Demos\n\nThe PlayTorch app comes with several examples of how you can build AI powered experiences with a variety of different machine learning models from object detection to natural language processing. See what can be built with the PlayTorch SDK and be inspired to make something of your own as you play with the examples.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "Sharing Your Creations\n\nAny PlayTorch Snack that you run in the PlayTorch app can be shared with others in an instant. When they open the link on their device, the PlayTorch app will instantly load what you\u2019ve built from the cloud so they can experience it first hand.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "When you have something you want to share, let us know on [Discord](https://discord.gg/sQkXTqEt33) or [Twitter](https://twitter.com/PlayTorch) or embed the PlayTorch Snack on your own webpage.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "SDK Overhaul\n\nWe learned a lot from the community after our initial launch in December and have been hard at work over the past several months to make the PlayTorch SDK (formerly known as PyTorch Live) simple, performant, and robust. In our initial version, the SDK relied on config files to define how a model ingested and output data.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "Today, we are happy to announce the next version of our SDK can handle data processing in JavaScript for your prototypes with the new PlayTorch API that leverages the JavaScript Interface (JSI) to directly call C++ code. Not only have we completely redone the way you can interact with models, but we have also greatly expanded the variety of supported model architectures.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "A New Data Processing API for Prototyping\n\nWith this JSI API, we now allow users direct access to tensors (data format for machine learning). Instead of only having access to predefined transformations, you can now manipulate tensors however you would like for your prototypes.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "No more switching back and forth between code and config. You will now be able to write everything in JavaScript and have access to all of the type annotations and autocomplete features available to you in those languages.\n\nCheck out our [tutorials](https://playtorch.dev/tutorials) to see the new Data Processing API in action, take a deeper dive in the [API docs](https://playtorch.dev/docs/api/core/), or inspect the code yourself on [GitHub](https://github.com/facebookresearch/playtorch).", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "Expanded Use Cases\n\nWith the new version of the SDK, we have added support for several cutting edge models.\n\n

\n \n

\n\nImage-to-image transformations are now supported thanks to our robust JSI API, so you can see what your world would look like if it were an anime.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "Translate French to English with an AI powered translator using the Seq2Seq model.\n\n

\n \n

\n\nUse DeepLab V3 to segment images!", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "Start Playing\n\nIf you want to start creating AI experiences yourself, head over to [playtorch.dev](https://playtorch.dev) and try out our [tutorials](https://playtorch.dev/tutorials/). Each tutorial will guide you through building a simple AI powered experience that you can instantly run on your phone and share with others.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "How to Get Support\n\nJoin us on [Discord](https://discord.gg/sQkXTqEt33), collaborate with us on [GitHub](https://github.com/facebookresearch/playtorch), or follow us on [Twitter](https://twitter.com/playtorch). Got questions or feedback? We\u2019d love to hear from you!", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Overview of PyTorch Autograd Engine'\nauthor: Preferred Networks, Inc.\n---\n\nThis blog post is based on PyTorch version 1.8, although it should apply for older versions too, since most of the mechanics have remained constant.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "To help understand the concepts explained here, it is recommended that you read the awesome blog post by [@ezyang](https://twitter.com/ezyang): [PyTorch internals](http://blog.ezyang.com/2019/05/pytorch-internals/) if you are not familiar with PyTorch architecture components such as ATen or c10d.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "What is autograd?\n\n**Background**", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "PyTorch computes the gradient of a function with respect to the inputs by using automatic differentiation. Automatic differentiation is a technique that, given a computational graph, calculates the gradients of the inputs. Automatic differentiation can be performed in two different ways; forward and reverse mode. Forward mode means that we calculate the gradients along with the result of the function, while reverse mode requires us to evaluate the function first, and then we calculate the gradients starting", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "from the output. While both modes have their pros and cons, the reverse mode is the de-facto choice since the number of outputs is smaller than the number of inputs, which allows a much more efficient computation. Check [3] to learn more about this.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Automatic differentiation relies on a classic calculus formula known as the chain-rule. The chain rule allows us to calculate very complex derivatives by splitting them and recombining them later.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Formally speaking, given a composite function , we can calculate its derivative as . This result is what makes automatic differentiation work.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "By combining the derivatives of the simpler functions that compose a larger one, such as a neural network, it is possible to compute the exact value of the gradient at a given point rather than relying on the numerical approximation, which would require multiple perturbations in the input to obtain a value.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "To get the intuition of how the reverse mode works, let\u2019s look at a simple function . Figure 1 shows its computational graph where the inputs x, y in the left, flow through a series of operations to generate the output z.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Figure 1: Computational graph of f(x, y) = log(x*y)

\n
\n\nThe automatic differentiation engine will normally execute this graph. It will also extend it to calculate the derivatives of w with respect to the inputs x, y, and the intermediate result v.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "The example function can be decomposed in f and g, where and . Every time the engine executes an operation in the graph, the derivative of that operation is added to the graph to be executed later in the backward pass. Note, that the engine knows the derivatives of the basic functions.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "In the example above, when multiplying x and y to obtain v, the engine will extend the graph to calculate the partial derivatives of the multiplication by using the multiplication derivative definition that it already knows. and . The resulting extended graph is shown in Figure 2, where the *MultDerivative* node also calculates the product of the resulting gradients", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "by an input gradient to apply the chain rule; this will be explicitly seen in the following operations. Note that the backward graph (green nodes) will not be executed until all the forward steps are completed.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "- [1] Averaging Weights Leads to Wider Optima and Better Generalization; Pavel Izmailov, Dmitry Podoprikhin, Timur Garipov, Dmitry Vetrov, Andrew Gordon Wilson; Uncertainty in Artificial Intelligence (UAI), 2018\n- [2] There Are Many Consistent Explanations of Unlabeled Data: Why You Should Average; Ben Athiwaratkun, Marc Finzi, Pavel Izmailov, Andrew Gordon Wilson; International Conference on Learning Representations (ICLR), 2019\n- [3] Improving Stability in Deep Reinforcement Learning with Weight Averaging; Evgenii Nikishin, Pavel Izmailov, Ben Athiwaratkun, Dmitrii Podoprikhin, Timur Garipov, Pavel Shvechikov, Dmitry Vetrov, Andrew Gordon Wilson, UAI 2018 Workshop: Uncertainty in Deep Learning, 2018\n- [4] A Simple Baseline for Bayesian Uncertainty in Deep Learning, Wesley Maddox, Timur Garipov, Pavel Izmailov, Andrew Gordon Wilson, arXiv pre-print, 2019: [https://arxiv.org/abs/1902.02476](https://arxiv.org/abs/1902.02476)\n- [5] SWALP : Stochastic Weight Averaging in Low Precision Training, Guandao Yang, Tianyi Zhang, Polina Kirichenko, Junwen Bai, Andrew Gordon Wilson, Christopher De Sa, To appear at the International Conference on Machine Learning (ICML), 2019.\n- [6] David Ruppert. Efficient estimations from a slowly convergent Robbins-Monro process. Technical report, Cornell University Operations Research and Industrial Engineering, 1988.\n- [7] Acceleration of stochastic approximation by averaging. Boris T Polyak and Anatoli B Juditsky. SIAM Journal on Control and Optimization, 30(4):838\u2013855, 1992.\n- [8] Loss Surfaces, Mode Connectivity, and Fast Ensembling of DNNs, Timur Garipov, Pavel Izmailov, Dmitrii Podoprikhin, Dmitry Vetrov, Andrew Gordon Wilson. Neural Information Processing Systems (NeurIPS), 2018", "metadata": {"source": "https://pytorch.org/blog/stochastic-weight-averaging-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing the PlayTorch app: Rapidly Create Mobile AI Experiences\"\nauthor: PlayTorch Team\nfeatured-img: \"\"\n---\n\n

\n \n

\n\nIn December, we announced PyTorch Live, a toolkit for building AI-powered mobile prototypes in minutes. The initial release included a command-line interface to set up a development environment and an SDK for building AI-powered experiences in React Native. Today, we're excited to share that PyTorch Live will now be known as PlayTorch. This new release provides an improved and simplified developer experience. PlayTorch development is independent from the PyTorch project and the PlayTorch code repository is moving into the Meta Research GitHub organization.\n\n## A New Workflow: The PlayTorch App", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "The PlayTorch team is excited to announce that we have partnered with [Expo](https://expo.dev) to change the way AI powered mobile experiences are built. Our new release simplifies the process of building mobile AI experiences by eliminating the need for a complicated development environment. You will now be able to build cross platform AI powered prototypes from the very browser you are using to read this blog.\n\nIn order to make this happen, we are releasing the [PlayTorch app](https://playtorch.dev/) which is able to run AI-powered experiences built in the [Expo Snack](https://snack.expo.dev/@playtorch/playtorch-starter?supportedPlatforms=my-device) web based code editor.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "The PlayTorch app can be downloaded from the Apple App Store and Google Play Store. With the app installed, you can head over to [playtorch.dev/snack](https://playtorch.dev/snack) and write the code for your AI-powered PlayTorch Snack. When you want to try what you\u2019ve built, you can use the PlayTorch app\u2019s QR code scanner to scan the QR code on the Snack page and load the code to your device.\n\nNOTE: PlayTorch Snacks will not work in the Expo Go app.\n\n## More to Explore in the PlayTorch App\n\n### AI Demos\n\nThe PlayTorch app comes with several examples of how you can build AI powered experiences with a variety of different machine learning models from object detection to natural language processing. See what can be built with the PlayTorch SDK and be inspired to make something of your own as you play with the examples.\n\n

\n \n

\n\n### Sharing Your Creations", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "Any PlayTorch Snack that you run in the PlayTorch app can be shared with others in an instant. When they open the link on their device, the PlayTorch app will instantly load what you\u2019ve built from the cloud so they can experience it first hand.\n\n

\n \n

\n\nWhen you have something you want to share, let us know on [Discord](https://discord.gg/sQkXTqEt33) or [Twitter](https://twitter.com/PlayTorch) or embed the PlayTorch Snack on your own webpage.\n\n## SDK Overhaul\n\nWe learned a lot from the community after our initial launch in December and have been hard at work over the past several months to make the PlayTorch SDK (formerly known as PyTorch Live) simple, performant, and robust. In our initial version, the SDK relied on config files to define how a model ingested and output data.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "Today, we are happy to announce the next version of our SDK can handle data processing in JavaScript for your prototypes with the new PlayTorch API that leverages the JavaScript Interface (JSI) to directly call C++ code. Not only have we completely redone the way you can interact with models, but we have also greatly expanded the variety of supported model architectures.\n\n## A New Data Processing API for Prototyping\n\nWith this JSI API, we now allow users direct access to tensors (data format for machine learning). Instead of only having access to predefined transformations, you can now manipulate tensors however you would like for your prototypes.\n\n

\n \n

\n\nNo more switching back and forth between code and config. You will now be able to write everything in JavaScript and have access to all of the type annotations and autocomplete features available to you in those languages.", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "Check out our [tutorials](https://playtorch.dev/tutorials) to see the new Data Processing API in action, take a deeper dive in the [API docs](https://playtorch.dev/docs/api/core/), or inspect the code yourself on [GitHub](https://github.com/facebookresearch/playtorch).\n\n### Expanded Use Cases\n\nWith the new version of the SDK, we have added support for several cutting edge models.\n\n

\n \n

\n\nImage-to-image transformations are now supported thanks to our robust JSI API, so you can see what your world would look like if it were an anime.\n\n

\n \n

\n\nTranslate French to English with an AI powered translator using the Seq2Seq model.\n\n

\n \n

\n\nUse DeepLab V3 to segment images!\n\n## Start Playing", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "## Start Playing\n\nIf you want to start creating AI experiences yourself, head over to [playtorch.dev](https://playtorch.dev) and try out our [tutorials](https://playtorch.dev/tutorials/). Each tutorial will guide you through building a simple AI powered experience that you can instantly run on your phone and share with others.\n\n## How to Get Support\n\nJoin us on [Discord](https://discord.gg/sQkXTqEt33), collaborate with us on [GitHub](https://github.com/facebookresearch/playtorch), or follow us on [Twitter](https://twitter.com/playtorch). Got questions or feedback? We\u2019d love to hear from you!", "metadata": {"source": "https://pytorch.org/blog/introducing-the-playtorch-app/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Overview of PyTorch Autograd Engine'\nauthor: Preferred Networks, Inc.\n---\n\nThis blog post is based on PyTorch version 1.8, although it should apply for older versions too, since most of the mechanics have remained constant.\n\nTo help understand the concepts explained here, it is recommended that you read the awesome blog post by [@ezyang](https://twitter.com/ezyang): [PyTorch internals](http://blog.ezyang.com/2019/05/pytorch-internals/) if you are not familiar with PyTorch architecture components such as ATen or c10d.\n\n### What is autograd?\n\n**Background**", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "**Background**\n\nPyTorch computes the gradient of a function with respect to the inputs by using automatic differentiation. Automatic differentiation is a technique that, given a computational graph, calculates the gradients of the inputs. Automatic differentiation can be performed in two different ways; forward and reverse mode. Forward mode means that we calculate the gradients along with the result of the function, while reverse mode requires us to evaluate the function first, and then we calculate the gradients starting from the output. While both modes have their pros and cons, the reverse mode is the de-facto choice since the number of outputs is smaller than the number of inputs, which allows a much more efficient computation. Check [3] to learn more about this.\n\nAutomatic differentiation relies on a classic calculus formula known as the chain-rule. The chain rule allows us to calculate very complex derivatives by splitting them and recombining them later.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Formally speaking, given a composite function , we can calculate its derivative as . This result is what makes automatic differentiation work.\nBy combining the derivatives of the simpler functions that compose a larger one, such as a neural network, it is possible to compute the exact value of the gradient at a given point rather than relying on the numerical approximation, which would require multiple perturbations in the input to obtain a value.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "To get the intuition of how the reverse mode works, let\u2019s look at a simple function . Figure 1 shows its computational graph where the inputs x, y in the left, flow through a series of operations to generate the output z.\n\n
\n \n

Figure 1: Computational graph of f(x, y) = log(x*y)

\n
\n\nThe automatic differentiation engine will normally execute this graph. It will also extend it to calculate the derivatives of w with respect to the inputs x, y, and the intermediate result v.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "The example function can be decomposed in f and g, where and . Every time the engine executes an operation in the graph, the derivative of that operation is added to the graph to be executed later in the backward pass. Note, that the engine knows the derivatives of the basic functions.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "In the example above, when multiplying x and y to obtain v, the engine will extend the graph to calculate the partial derivatives of the multiplication by using the multiplication derivative definition that it already knows. and . The resulting extended graph is shown in Figure 2, where the *MultDerivative* node also calculates the product of the resulting gradients by an input gradient to apply the chain rule; this will be explicitly seen in the following operations. Note that the backward graph (green nodes) will not be executed until all the forward steps are completed.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} {"page_content": "
\n \n

Figure 2: Computational graph extended after executing the logarithm

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Continuing, the engine now calculates the operation and extends the graph again with the log derivative that it knows to be . This is shown in figure 3. This operation generates the result that when propagated backward and multiplied by the multiplication derivative as in the chain rule, generates the derivatives , .", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Figure 3: Computational graph extended after executing the logarithm

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "The original computation graph is extended with a new dummy variable z that is the same w. The derivative of z with respect to w is 1 as they are the same variable, this trick allows us to apply the chain rule to calculate the derivatives of the inputs. After the forward pass is complete, we start the backward pass, by supplying the initial value of 1.0 for . This is shown in Figure 4.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Continuing, the engine now calculates the operation and extends the graph again with the log derivative that it knows to be . This is shown in figure 3. This operation generates the result that when propagated backward and multiplied by the multiplication derivative as in the chain rule, generates the derivatives , .", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "
\n \n

Figure 3: Computational graph extended after executing the logarithm

\n
\n\nThe original computation graph is extended with a new dummy variable z that is the same w. The derivative of z with respect to w is 1 as they are the same variable, this trick allows us to apply the chain rule to calculate the derivatives of the inputs. After the forward pass is complete, we start the backward pass, by supplying the initial value of 1.0 for . This is shown in Figure 4.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} {"page_content": "
\n \n

Figure 4: Computational graph extended for reverse auto differentiation

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Then following the green graph we execute the LogDerivative operation that the auto differentiation engine introduced, and multiply its result by to obtain the gradient as per the chain rule states. Next, the multiplication derivative is executed in the same way, and the desired", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "derivatives are finally obtained.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Formally, what we are doing here, and PyTorch autograd engine also does, is computing a Jacobian-vector product (Jvp) to calculate the gradients of the model parameters, since the model parameters and inputs are vectors.\n\n**The Jacobian-vector product**", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "When we calculate the gradient of a vector-valued function (a function whose inputs and outputs are vectors), we are essentially constructing a Jacobian matrix .", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Thanks to the chain rule, multiplying the Jacobian matrix of a function by a vector with the previously calculated gradients", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "of a scalar function results in the gradients of the scalar output with respect to the vector-valued function inputs.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "As an example, let\u2019s look at some functions in python notation to show how the chain rule applies.\n
\n ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
def f(x1, x2):\n      a = x1 * x2\n      y1 = log(a)\n      y2 = sin(x2)\n      return (y1, y2)\n  
\n\n \n\n
def g(y1, y2):\n      return y1 * y2\n  
\n\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Now, if we derive this by hand using the chain rule and the definition of the derivatives, we obtain the following set of identities that we can directly plug into the Jacobian matrix of ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Next, let\u2019s consider the gradients for the scalar function ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
\n

", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "If we now calculate the transpose-Jacobian vector product obeying the chain rule, we obtain the following expression:\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Evaluating the Jvp for yields the result:", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "\nWe can execute the same expression in PyTorch and calculate the gradient of the input:\n
\n
>>> import torch
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
>>> x = torch.tensor([0.5, 0.75], requires_grad=True)
\n
>>> y = torch.log(x[0] * x[1]) * torch.sin(x[1])
\n
>>> y.backward(1.0)
\n
>>> x.grad
\n tensor([1.3633,\n 0.1912])\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Then following the green graph we execute the LogDerivative operation that the auto differentiation engine introduced, and multiply its result by to obtain the gradient as per the chain rule states. Next, the multiplication derivative is executed in the same way, and the desired derivatives are finally obtained.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Formally, what we are doing here, and PyTorch autograd engine also does, is computing a Jacobian-vector product (Jvp) to calculate the gradients of the model parameters, since the model parameters and inputs are vectors.\n\n**The Jacobian-vector product**\n\nWhen we calculate the gradient of a vector-valued function (a function whose inputs and outputs are vectors), we are essentially constructing a Jacobian matrix .", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Thanks to the chain rule, multiplying the Jacobian matrix of a function by a vector with the previously calculated gradients of a scalar function results in the gradients of the scalar output with respect to the vector-valued function inputs.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "As an example, let\u2019s look at some functions in python notation to show how the chain rule applies.\n
\n \n\n
def f(x1, x2):\n      a = x1 * x2\n      y1 = log(a)\n      y2 = sin(x2)\n      return (y1, y2)\n  
\n\n \n\n
def g(y1, y2):\n      return y1 * y2\n  
\n\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "\n\nNow, if we derive this by hand using the chain rule and the definition of the derivatives, we obtain the following set of identities that we can directly plug into the Jacobian matrix of ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "
\n

\n

\n

\n

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Next, let\u2019s consider the gradients for the scalar function \n\n
\n

\n

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "If we now calculate the transpose-Jacobian vector product obeying the chain rule, we obtain the following expression:\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Evaluating the Jvp for yields the result:\n\nWe can execute the same expression in PyTorch and calculate the gradient of the input:\n
\n
>>> import torch
\n
>>> x = torch.tensor([0.5, 0.75], requires_grad=True)
\n
>>> y = torch.log(x[0] * x[1]) * torch.sin(x[1])
\n
>>> y.backward(1.0)
\n
>>> x.grad
\n tensor([1.3633,\n 0.1912])\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} {"page_content": "The result is the same as our hand-calculated Jacobian-vector product!\nHowever, PyTorch never constructed the matrix as it could grow prohibitively large but instead, created a graph of operations that traversed backward while applying the Jacobian-vector products defined in [tools/autograd/derivatives.yaml](https://github.com/pytorch/pytorch/blob/master/tools/autograd/derivatives.yaml).\n\n**Going through the graph**", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Every time PyTorch executes an operation, the autograd engine constructs the graph to be traversed backward.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "The reverse mode auto differentiation starts by adding a scalar variable at the end so that as we saw in the introduction. This is the initial gradient value that is supplied to the Jvp engine calculation as we saw in the section above.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch, the initial gradient is explicitly set by the user when he calls the backward method.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Then, the Jvp calculation starts but it never constructs the matrix. Instead, when PyTorch records the computational graph, the derivatives of the executed forward operations are added (Backward Nodes). Figure 5 shows a backward graph generated by the execution of the functions and seen before.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Figure 5: Computational Graph extended with the backward pass

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Once the forward pass is done, the results are used in the backward pass where the derivatives in the computational graph are executed. The basic derivatives are stored in the [tools/autograd/derivatives.yaml](https://github.com/pytorch/pytorch/blob/master/tools/autograd/derivatives.yaml) file and they are not regular derivatives but the Jvp versions of them [3]. They take their primitive function inputs and outputs as parameters along with the gradient of the function outputs with respect to the final", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "outputs. By repeatedly multiplying the resulting gradients by the next Jvp derivatives in the graph, the gradients up to the inputs will be generated following the chain rule.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
\n \n

Figure 6: How the chain rule is applied in backward differentiation

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Figure 6 represents the process by showing the chain rule. We started with a value of 1.0 as detailed before which is the already calculated gradient highlighted in green. And we move to the next node in the graph. The *backward* function registered in", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "[derivatives.yaml](https://github.com/pytorch/pytorch/blob/a0a7a2d648f05b0192e6943c9684406cdf404fbf/tools/autograd/derivatives.yaml#L635-L636) will calculate the associated", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": " value highlighted in red and multiply it by . By the chain rule this results in which will be the already calculated gradient (green) when we process the next backward node in the graph.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Every time PyTorch executes an operation, the autograd engine constructs the graph to be traversed backward.\nThe reverse mode auto differentiation starts by adding a scalar variable at the end so that as we saw in the introduction. This is the initial gradient value that is supplied to the Jvp engine calculation as we saw in the section above.\n\nIn PyTorch, the initial gradient is explicitly set by the user when he calls the backward method.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Then, the Jvp calculation starts but it never constructs the matrix. Instead, when PyTorch records the computational graph, the derivatives of the executed forward operations are added (Backward Nodes). Figure 5 shows a backward graph generated by the execution of the functions and seen before.\n\n
\n \n

Figure 5: Computational Graph extended with the backward pass

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Once the forward pass is done, the results are used in the backward pass where the derivatives in the computational graph are executed. The basic derivatives are stored in the [tools/autograd/derivatives.yaml](https://github.com/pytorch/pytorch/blob/master/tools/autograd/derivatives.yaml) file and they are not regular derivatives but the Jvp versions of them [3]. They take their primitive function inputs and outputs as parameters along with the gradient of the function outputs with respect to the final outputs. By repeatedly multiplying the resulting gradients by the next Jvp derivatives in the graph, the gradients up to the inputs will be generated following the chain rule.\n\n
\n \n

Figure 6: How the chain rule is applied in backward differentiation

\n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "Figure 6 represents the process by showing the chain rule. We started with a value of 1.0 as detailed before which is the already calculated gradient highlighted in green. And we move to the next node in the graph. The *backward* function registered in [derivatives.yaml](https://github.com/pytorch/pytorch/blob/a0a7a2d648f05b0192e6943c9684406cdf404fbf/tools/autograd/derivatives.yaml#L635-L636) will calculate the associated\n value highlighted in red and multiply it by . By the chain rule this results in which will be the already calculated gradient (green) when we process the next backward node in the graph.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} {"page_content": "You may also have noticed that in Figure 5 there is a gradient generated from two different sources. When two different functions share an input, the gradients with respect to the output are aggregated for that input, and calculations using that gradient can\u2019t proceed unless all the paths have been aggregated together.\n\nLet\u2019s see an example of how the derivatives are stored in PyTorch.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "Suppose that we are currently processing the backward propagation of the function, in the *LogBackward* node in Figure 2. The derivative of in", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "[`derivatives.yaml`](https://github.com/pytorch/pytorch/blob/a0a7a2d648f05b0192e6943c9684406cdf404fbf/tools/autograd/derivatives.yaml#L635-L636) is specified as `grad.div(self.conj())`. `grad` is the already calculated gradient and `self.conj()` is the complex", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "conjugate of the input vector. For complex numbers PyTorch calculates a special derivative called the conjugate Wirtinger derivative [6]. This derivative takes the complex number and its conjugate and by operating some magic that is described in [6], they are the direction of steepest descent when plugged into optimizers.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "This code translates to , the corresponding green, and red squares in Figure 3. Continuing, the autograd engine will execute the next operation; backward of the multiplication. As before, the inputs are the", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "original function\u2019s inputs and the gradient calculated from the backward step. This step will keep repeating until we reach the gradient with respect to the inputs and the computation will be finished. The gradient of is only completed once the multiplication and sin gradients are added together. As you can see, we computed the equivalent of the Jvp but without constructing the matrix.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "In the next post we will dive inside PyTorch code to see how this graph is constructed and where are the relevant pieces should you want to experiment with it!", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "References", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
    \n
  1. https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html
  2. \n
  3. https://web.stanford.edu/class/cs224n/readings/gradient-notes.pdf
  4. ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
  5. https://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/slides/lec10.pdf
  6. \n
  7. https://mustafaghali11.medium.com/how-pytorch-backward-function-works-55669b3b7c62
  8. ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "
  9. https://indico.cern.ch/event/708041/contributions/3308814/attachments/1813852/2963725/automatic_differentiation_and_deep_learning.pdf
  10. \n
  11. https://pytorch.org/docs/stable/notes/autograd.html#complex-autograd-doc
  12. ", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "

    Recommended: shows why the backprop is formally expressed with the Jacobian

    \n
  13. https://cs.ubc.ca/~fwood/CS340/lectures/AD1.pdf
  14. \n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Case Study: Amazon Ads Uses PyTorch and AWS Inferentia to Scale Models for Ads Processing\"\nauthor: Yashal Kanungo \u2013 Applied Scientist, Kamran Khan - Sr. Technical Product Manager, Shubha Kumbadakone \u2013 Sr. Specialist, ML Frameworks\nfeatured-img: \"\"\n---\n\nAmazon Ads uses PyTorch, TorchServe, and AWS Inferentia to reduce inference costs by 71% and drive scale out.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Amazon Ads helps companies build their brand and connect with shoppers through ads shown both within and beyond Amazon\u2019s store, including websites, apps, and streaming TV content in more than 15 countries. Businesses and brands of all sizes, including registered sellers, vendors, book vendors, Kindle Direct Publishing (KDP) authors, app developers, and agencies can upload their own ad creatives, which can include images, video, audio, and, of course, products sold on Amazon.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nTo promote an accurate, safe, and pleasant shopping experience, these ads must comply with content guidelines. For example, ads cannot flash on and off, products must be featured in an appropriate context, and images and text should be appropriate for a general audience. To help ensure that ads meet the required policies and standards, we needed to develop scalable mechanisms and tools.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "As a solution, we used machine learning (ML) models to surface ads that might need revision. As deep neural networks flourished over the past decade, our data science team began exploring more versatile deep learning (DL) methods capable of processing text, images, audio, or video with minimal human intervention. To that end, we\u2019ve used PyTorch to build computer vision (CV) and natural language processing (NLP) models that automatically flag potentially non-compliant ads. PyTorch is intuitive, flexible, and", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "user-friendly, and has made our transition to using DL models seamless. Deploying these new models on [AWS Inferentia-based Amazon EC2 Inf1 instances](https://aws.amazon.com/ec2/instance-types/inf1/), rather than on GPU-based instances, reduced our inference latency by 30 percent and our inference costs by 71 percent for the same workloads.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Transition to deep learning\n\nOur ML systems paired classical models with word embeddings to evaluate ad text. But our requirements evolved, and as the volume of submissions continued to expand, we needed a method nimble enough to scale along with our business. In addition, our models must be fast and serve ads within milliseconds to provide an optimal customer experience.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Over the last decade, DL has become very popular in numerous domains, including natural language, vision, and audio. Because deep neural networks channel data sets through many layers \u2014 extracting progressively higher-level features \u2014 they can make more nuanced inferences than classical ML models. Rather than simply detecting prohibited language, for example, a DL model can reject an ad for making false claims.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "In addition, DL techniques are transferable\u2013 a model trained for one task can be adapted to carry out a related task. For instance, a pre-trained neural network can be optimized to detect objects in images and then fine-tuned to identify specific objects that are not allowed to be displayed in an ad.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Deep neural networks can automate two of classical ML\u2019s most time-consuming steps: feature engineering and data labeling. Unlike traditional supervised learning approaches, which require exploratory data analysis and hand-engineered features, deep neural networks learn the relevant features directly from the data. DL models can also analyze unstructured data, like text and images, without the preprocessing necessary in ML. Deep neural networks scale effectively with more data and perform especially well in", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "applications involving large data sets.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We chose PyTorch to develop our models because it helped us maximize the performance of our systems. With PyTorch, we can serve our customers better while taking advantage of Python\u2019s most intuitive concepts. The programming in PyTorch is object-oriented: it groups processing functions with the data they modify. As a result, our codebase is modular, and we can reuse pieces of code in different applications. In addition, PyTorch\u2019s eager mode allows loops and control structures and, therefore, more complex", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "operations in the model. Eager mode makes it easy to prototype and iterate upon our models, and we can work with various data structures. This flexibility helps us update our models quickly to meet changing business requirements.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "\u201cBefore this, we experimented with other frameworks that were \u201cPythonic,\u201d but PyTorch was the clear winner for us here.\u201d said Yashal Kanungo, Applied Scientist. \u201cUsing PyTorch was easy because the structure felt native to Python programming, which the data scientists were very familiar with\u201d.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Training pipeline", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Today, we build our text models entirely in PyTorch. To save time and money, we often skip the early stages of training by fine-tuning a pre-trained NLP model for language analysis. If we need a new model to evaluate images or video, we start by browsing PyTorch\u2019s [torchvision](https://pytorch.org/vision/stable/index.html) library, which offers pretrained options for image and video classification, object detection, instance segmentation, and pose estimation. For specialized tasks, we build a custom model", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "from the ground up. PyTorch is perfect for this, because eager mode and the user-friendly front end make it easy to experiment with different architectures.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "Suppose that we are currently processing the backward propagation of the function, in the *LogBackward* node in Figure 2. The derivative of in [`derivatives.yaml`](https://github.com/pytorch/pytorch/blob/a0a7a2d648f05b0192e6943c9684406cdf404fbf/tools/autograd/derivatives.yaml#L635-L636) is specified as `grad.div(self.conj())`. `grad` is the already calculated gradient and `self.conj()` is the complex conjugate of the input vector. For complex numbers PyTorch calculates a special derivative called the conjugate Wirtinger derivative [6]. This derivative takes the complex number and its conjugate and by operating some magic that is described in [6], they are the direction of steepest descent when plugged into optimizers.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "This code translates to , the corresponding green, and red squares in Figure 3. Continuing, the autograd engine will execute the next operation; backward of the multiplication. As before, the inputs are the original function\u2019s inputs and the gradient calculated from the backward step. This step will keep repeating until we reach the gradient with respect to the inputs and the computation will be finished. The gradient of is only completed once the multiplication and sin gradients are added together. As you can see, we computed the equivalent of the Jvp but without constructing the matrix.", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "In the next post we will dive inside PyTorch code to see how this graph is constructed and where are the relevant pieces should you want to experiment with it!\n\n### References", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "
    \n
  1. https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html
  2. \n
  3. https://web.stanford.edu/class/cs224n/readings/gradient-notes.pdf
  4. \n
  5. https://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/slides/lec10.pdf
  6. \n
  7. https://mustafaghali11.medium.com/how-pytorch-backward-function-works-55669b3b7c62
  8. \n
  9. https://indico.cern.ch/event/708041/contributions/3308814/attachments/1813852/2963725/automatic_differentiation_and_deep_learning.pdf
  10. \n
  11. https://pytorch.org/docs/stable/notes/autograd.html#complex-autograd-doc
  12. \n

    Recommended: shows why the backprop is formally expressed with the Jacobian

    \n
  13. https://cs.ubc.ca/~fwood/CS340/lectures/AD1.pdf
  14. \n
", "metadata": {"source": "https://pytorch.org/blog/overview-of-pytorch-autograd-engine/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Case Study: Amazon Ads Uses PyTorch and AWS Inferentia to Scale Models for Ads Processing\"\nauthor: Yashal Kanungo \u2013 Applied Scientist, Kamran Khan - Sr. Technical Product Manager, Shubha Kumbadakone \u2013 Sr. Specialist, ML Frameworks\nfeatured-img: \"\"\n---\n\nAmazon Ads uses PyTorch, TorchServe, and AWS Inferentia to reduce inference costs by 71% and drive scale out.\n\nAmazon Ads helps companies build their brand and connect with shoppers through ads shown both within and beyond Amazon\u2019s store, including websites, apps, and streaming TV content in more than 15 countries. Businesses and brands of all sizes, including registered sellers, vendors, book vendors, Kindle Direct Publishing (KDP) authors, app developers, and agencies can upload their own ad creatives, which can include images, video, audio, and, of course, products sold on Amazon.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "To promote an accurate, safe, and pleasant shopping experience, these ads must comply with content guidelines. For example, ads cannot flash on and off, products must be featured in an appropriate context, and images and text should be appropriate for a general audience. To help ensure that ads meet the required policies and standards, we needed to develop scalable mechanisms and tools.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "As a solution, we used machine learning (ML) models to surface ads that might need revision. As deep neural networks flourished over the past decade, our data science team began exploring more versatile deep learning (DL) methods capable of processing text, images, audio, or video with minimal human intervention. To that end, we\u2019ve used PyTorch to build computer vision (CV) and natural language processing (NLP) models that automatically flag potentially non-compliant ads. PyTorch is intuitive, flexible, and user-friendly, and has made our transition to using DL models seamless. Deploying these new models on [AWS Inferentia-based Amazon EC2 Inf1 instances](https://aws.amazon.com/ec2/instance-types/inf1/), rather than on GPU-based instances, reduced our inference latency by 30 percent and our inference costs by 71 percent for the same workloads.\n\n## Transition to deep learning", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "Our ML systems paired classical models with word embeddings to evaluate ad text. But our requirements evolved, and as the volume of submissions continued to expand, we needed a method nimble enough to scale along with our business. In addition, our models must be fast and serve ads within milliseconds to provide an optimal customer experience.\n\nOver the last decade, DL has become very popular in numerous domains, including natural language, vision, and audio. Because deep neural networks channel data sets through many layers \u2014 extracting progressively higher-level features \u2014 they can make more nuanced inferences than classical ML models. Rather than simply detecting prohibited language, for example, a DL model can reject an ad for making false claims.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "In addition, DL techniques are transferable\u2013 a model trained for one task can be adapted to carry out a related task. For instance, a pre-trained neural network can be optimized to detect objects in images and then fine-tuned to identify specific objects that are not allowed to be displayed in an ad.\n\nDeep neural networks can automate two of classical ML\u2019s most time-consuming steps: feature engineering and data labeling. Unlike traditional supervised learning approaches, which require exploratory data analysis and hand-engineered features, deep neural networks learn the relevant features directly from the data. DL models can also analyze unstructured data, like text and images, without the preprocessing necessary in ML. Deep neural networks scale effectively with more data and perform especially well in applications involving large data sets.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "We chose PyTorch to develop our models because it helped us maximize the performance of our systems. With PyTorch, we can serve our customers better while taking advantage of Python\u2019s most intuitive concepts. The programming in PyTorch is object-oriented: it groups processing functions with the data they modify. As a result, our codebase is modular, and we can reuse pieces of code in different applications. In addition, PyTorch\u2019s eager mode allows loops and control structures and, therefore, more complex operations in the model. Eager mode makes it easy to prototype and iterate upon our models, and we can work with various data structures. This flexibility helps us update our models quickly to meet changing business requirements.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "\u201cBefore this, we experimented with other frameworks that were \u201cPythonic,\u201d but PyTorch was the clear winner for us here.\u201d said Yashal Kanungo, Applied Scientist. \u201cUsing PyTorch was easy because the structure felt native to Python programming, which the data scientists were very familiar with\u201d.\n\n### Training pipeline\n\nToday, we build our text models entirely in PyTorch. To save time and money, we often skip the early stages of training by fine-tuning a pre-trained NLP model for language analysis. If we need a new model to evaluate images or video, we start by browsing PyTorch\u2019s [torchvision](https://pytorch.org/vision/stable/index.html) library, which offers pretrained options for image and video classification, object detection, instance segmentation, and pose estimation. For specialized tasks, we build a custom model from the ground up. PyTorch is perfect for this, because eager mode and the user-friendly front end make it easy to experiment with different architectures.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} {"page_content": "_To learn how to finetune neural networks in PyTorch, head to [this tutorial](https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#finetuning-from-a-pretrained-model)._", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Before we begin training, we optimize our model\u2019s [hyperparameters](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-define-ranges.html), the variables that define the network architecture (for example, the number of hidden layers) and training mechanics (such as learning rate and batch size). Choosing appropriate hyperparameter values is essential, because they will shape the training behavior of the model. We rely on the [Bayesian search feature in", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "SageMaker](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-how-it-works.html#automatic-tuning-bayesian-search.title), AWS\u2019s ML platform, for this step. Bayesian search treats hyperparameter tuning as a regression problem: It proposes the hyperparameter combinations that are likely to produce the best results and runs training jobs to test those values. After each trial, a regression algorithm determines the next set of hyperparameter values to test, and performance improves", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "incrementally.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We prototype and iterate upon our models using SageMaker Notebooks. Eager mode lets us prototype models quickly by building a new computational graph for each training batch; the sequence of operations can change from iteration to iteration to accommodate different data structures or to jibe with intermediate results. That frees us to adjust the network during training without starting over from scratch. These dynamic graphs are particularly valuable for recursive computations based on variable sequence", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "lengths, such as the words, sentences, and paragraphs in an ad that are analyzed with NLP.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "When we\u2019ve finalized the model architecture, we deploy training jobs on [SageMaker](https://aws.amazon.com/sagemaker/). PyTorch helps us develop large models faster by running numerous training jobs at the same time. PyTorch\u2019s [Distributed Data Parallel](https://sagemaker.readthedocs.io/en/stable/api/training/sdp_versions/v1.0.0/smd_data_parallel_pytorch.html) (DDP) module replicates a single model across multiple interconnected machines within SageMaker, and all the processes run forward passes", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "simultaneously on their own unique portion of the data set. During the backward pass, the module averages the gradients of all the processes, so each local model is updated with the same parameter values.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Model deployment pipeline\n\nWhen we deploy the model in production, we want to ensure lower inference costs without impacting prediction accuracy. Several PyTorch features and AWS services have helped us address the challenge.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "The flexibility of a dynamic graph enriches training, but in deployment we want to maximize performance and portability. An advantage of developing NLP models in PyTorch is that out of the box, they can be traced into a static sequence of operations by [TorchScript](https://pytorch.org/docs/stable/jit.html), a subset of Python specialized for ML applications. Torchscript converts PyTorch models to a more efficient, production-friendly intermediate representation (IR) graph that is easily compiled. We run a", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "sample input through the model, and TorchScript records the operations executed during the forward pass. The resulting IR graph can run in high-performance environments, including C++ and other multithreaded Python-free contexts, and optimizations such as operator fusion can speed up the runtime.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Neuron SDK and AWS Inferentia powered compute", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We deploy our models on [Amazon EC2 Inf1 instances](https://aws.amazon.com/ec2/instance-types/inf1/) powered by AWS Inferentia, Amazon's first ML silicon designed to accelerate deep learning inference workloads. Inferentia has shown to reduce inference costs by up to 70% compared to Amazon EC2 GPU-based instances.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We used the [AWS Neuron](https://aws.amazon.com/machine-learning/neuron/) SDK \u2014 a set of software tools used with Inferentia \u2014 to compile and optimize our models for deployment on EC2 Inf1 instances.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "The code snippet below shows how to compile a Hugging Face BERT model with Neuron. Like torch.jit.trace(), neuron.trace() records the model\u2019s operations on an example input during the forward pass to build a static IR graph.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nfrom transformers import BertModel, BertTokenizer\nimport torch.neuron\ntokenizer = BertTokenizer.from_pretrained(\"path to saved vocab\")\nmodel = BertModel.from_pretrained(\"path to the saved model\", returned_dict=False)\ninputs = tokenizer (\"sample input\", return_tensor=\"pt\")\nneuron_model = torch.neuron.trace(model,\n example_inputs = (inputs['input_ids'], inputs['attention_mask']),\n verbose = 1)", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "output = neuron_model(*(inputs['input_ids'], inputs['attention_mask']))\n```", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Autocasting and recalibration", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "Before we begin training, we optimize our model\u2019s [hyperparameters](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-define-ranges.html), the variables that define the network architecture (for example, the number of hidden layers) and training mechanics (such as learning rate and batch size). Choosing appropriate hyperparameter values is essential, because they will shape the training behavior of the model. We rely on the [Bayesian search feature in SageMaker](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-how-it-works.html#automatic-tuning-bayesian-search.title), AWS\u2019s ML platform, for this step. Bayesian search treats hyperparameter tuning as a regression problem: It proposes the hyperparameter combinations that are likely to produce the best results and runs training jobs to test those values. After each trial, a regression algorithm determines the next set of hyperparameter values to test, and performance improves incrementally.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "We prototype and iterate upon our models using SageMaker Notebooks. Eager mode lets us prototype models quickly by building a new computational graph for each training batch; the sequence of operations can change from iteration to iteration to accommodate different data structures or to jibe with intermediate results. That frees us to adjust the network during training without starting over from scratch. These dynamic graphs are particularly valuable for recursive computations based on variable sequence lengths, such as the words, sentences, and paragraphs in an ad that are analyzed with NLP.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "When we\u2019ve finalized the model architecture, we deploy training jobs on [SageMaker](https://aws.amazon.com/sagemaker/). PyTorch helps us develop large models faster by running numerous training jobs at the same time. PyTorch\u2019s [Distributed Data Parallel](https://sagemaker.readthedocs.io/en/stable/api/training/sdp_versions/v1.0.0/smd_data_parallel_pytorch.html) (DDP) module replicates a single model across multiple interconnected machines within SageMaker, and all the processes run forward passes simultaneously on their own unique portion of the data set. During the backward pass, the module averages the gradients of all the processes, so each local model is updated with the same parameter values.\n\n### Model deployment pipeline\n\nWhen we deploy the model in production, we want to ensure lower inference costs without impacting prediction accuracy. Several PyTorch features and AWS services have helped us address the challenge.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "The flexibility of a dynamic graph enriches training, but in deployment we want to maximize performance and portability. An advantage of developing NLP models in PyTorch is that out of the box, they can be traced into a static sequence of operations by [TorchScript](https://pytorch.org/docs/stable/jit.html), a subset of Python specialized for ML applications. Torchscript converts PyTorch models to a more efficient, production-friendly intermediate representation (IR) graph that is easily compiled. We run a sample input through the model, and TorchScript records the operations executed during the forward pass. The resulting IR graph can run in high-performance environments, including C++ and other multithreaded Python-free contexts, and optimizations such as operator fusion can speed up the runtime.\n\n### Neuron SDK and AWS Inferentia powered compute", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "We deploy our models on [Amazon EC2 Inf1 instances](https://aws.amazon.com/ec2/instance-types/inf1/) powered by AWS Inferentia, Amazon's first ML silicon designed to accelerate deep learning inference workloads. Inferentia has shown to reduce inference costs by up to 70% compared to Amazon EC2 GPU-based instances.\nWe used the [AWS Neuron](https://aws.amazon.com/machine-learning/neuron/) SDK \u2014 a set of software tools used with Inferentia \u2014 to compile and optimize our models for deployment on EC2 Inf1 instances.\n\nThe code snippet below shows how to compile a Hugging Face BERT model with Neuron. Like torch.jit.trace(), neuron.trace() records the model\u2019s operations on an example input during the forward pass to build a static IR graph.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "```python\nimport torch\nfrom transformers import BertModel, BertTokenizer\nimport torch.neuron\ntokenizer = BertTokenizer.from_pretrained(\"path to saved vocab\")\nmodel = BertModel.from_pretrained(\"path to the saved model\", returned_dict=False)\ninputs = tokenizer (\"sample input\", return_tensor=\"pt\")\nneuron_model = torch.neuron.trace(model,\n example_inputs = (inputs['input_ids'], inputs['attention_mask']),\n verbose = 1)\noutput = neuron_model(*(inputs['input_ids'], inputs['attention_mask']))\n```\n\n### Autocasting and recalibration", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} {"page_content": "Under the hood, Neuron optimizes our models for performance by autocasting them to a smaller data type. As a default, most applications represent neural network values in the 32-bit single-precision floating point (FP32) number format. Autocasting the model to a 16-bit format \u2014 half-precision floating point (FP16) or Brain Floating Point (BF16) \u2014 reduces a model\u2019s memory footprint and execution time. In our case, we decided to use FP16 to optimize for performance while maintaining high accuracy.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Autocasting to a smaller data type can, in some cases, trigger slight differences in the model\u2019s predictions. To ensure that the model\u2019s accuracy is not affected, Neuron compares the performance metrics and predictions of the FP16 and FP32 models. When autocasting diminishes the model\u2019s accuracy, we can tell the Neuron compiler to convert only the weights and certain data inputs to FP16, keeping the rest of the intermediate results in FP32. In addition, we often run a few iterations with the training data", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "to recalibrate our autocasted models. This process is much less intensive than the original training.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Deployment\n\nTo analyze multimedia ads, we run an ensemble of DL models. All ads uploaded to Amazon are run through specialized models that assess every type of content they include: images, video and audio, headlines, texts, backgrounds, and even syntax, grammar, and potentially inappropriate language. The signals we receive from these models indicate whether or not an advertisement complies with our criteria.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Deploying and monitoring multiple models is significantly complex, so we depend on [TorchServe](https://github.com/pytorch/serve), SageMaker\u2019s default PyTorch model serving library. Jointly developed by Facebook\u2019s PyTorch team and AWS to streamline the transition from prototyping to production, TorchServe helps us deploy trained PyTorch models at scale without having to write custom code. It provides a secure set of REST APIs for inference, management, metrics, and explanations. With features such as", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "multi-model serving, model versioning, ensemble support, and automatic batching, TorchServe is ideal for supporting our immense workload. You can read more about deploying your Pytorch models on SageMaker with native TorchServe integration in this [blog post](https://aws.amazon.com/blogs/machine-learning/serving-pytorch-models-in-production-with-the-amazon-sagemaker-native-torchserve-integration/).", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "In some use cases, we take advantage of PyTorch\u2019s object-oriented programming paradigm to wrap multiple DL models into one parent object \u2014 a PyTorch nn.Module \u2014 and serve them as a single ensemble. In other cases, we use TorchServe to serve individual models on separate SageMaker endpoints, running on AWS Inf1 instances.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Custom handlers", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We particularly appreciate that TorchServe allows us to embed our model initialization, preprocessing, inferencing, and post processing code in a single Python script, handler.py, which lives on the server. This script \u2014 the handler \u2014preprocesses the un-labeled data from an ad, runs that data through our models, and delivers the resulting inferences to downstream systems. TorchServe provides several default handlers that load weights and architecture and prepare the model to run on a particular device. We", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "can bundle all the additional required artifacts, such as vocabulary files or label maps, with the model in a single archive file.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "When we need to deploy models that have complex initialization processes or that originated in third-party libraries, we design custom handlers in TorchServe. These let us load any model, from any library, with any required process. The following snippet shows a simple handler that can serve Hugging Face BERT models on any SageMaker hosting endpoint instance.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nimport torch.neuron\nfrom ts.torch_handler.base_handler import BaseHandler\nimport transformers\nfrom transformers import AutoModelForSequenceClassification,AutoTokenizer", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "class MyModelHandler(BaseHandler):\n def initialize(self, context):\n self.manifest = ctx.manifest\n properties = ctx.system_properties\n model_dir = properties.get(\"model_dir\")\n serialized_file = self.manifest[\"model\"][\"serializedFile\"]\n model_pt_path = os.path.join(model_dir, serialized_file)", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "self.tokenizer = AutoTokenizer.from_pretrained(\n model_dir, do_lower_case=True\n )\n self.model = AutoModelForSequenceClassification.from_pretrained(\n model_dir\n )\n\n def preprocess(self, data):", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "input_text = data.get(\"data\")\n if input_text is None:\n input_text = data.get(\"body\")\n inputs = self.tokenizer.encode_plus(input_text, max_length=int(max_length), pad_to_max_length=True, add_special_tokens=True, return_tensors='pt')\n return inputs\n\n def inference(self,inputs):\n predictions = self.model(**inputs)\n return predictions\n\n def postprocess(self, output):\n return output\n```", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Batching", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Hardware accelerators are optimized for parallelism, and batching \u2014 feeding a model multiple inputs in a single step \u2014 helps saturate all available capacity, typically resulting in higher throughputs. Excessively high batch sizes, however, can increase latency with minimal improvement in throughputs. Experimenting with different batch sizes helps us identify the sweet spot for our models and hardware accelerator. We run experiments to determine the best batch size for our model size, payload size, and", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "request traffic patterns.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "The Neuron compiler now supports variable batch sizes. Previously, tracing a model hardcoded the predefined batch size, so we had to pad our data, which can waste compute, slow throughputs, and exacerbate latency. Inferentia is optimized to maximize throughput for small batches, reducing latency by easing the load on the system.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Parallelism\n\nModel parallelism on multi-cores also improves throughput and latency, which is crucial for our heavy workloads. Each Inferentia chip contains four NeuronCores that can either run separate models simultaneously or form a pipeline to stream a single model. In our use case, the data parallel configuration offers the highest throughput at the lowest cost, because it scales out concurrent processing requests.\n\nData Parallel:", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nModel Parallel:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Monitoring\n\nIt is critical that we monitor the accuracy of our inferences in production. Models that initially make good predictions can eventually degrade in deployment as they are exposed to a wider variety of data. This phenomenon, called model drift, usually occurs when the input data distributions or the prediction targets change.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "We use [SageMaker Model Monitor](https://aws.amazon.com/sagemaker/model-monitor/) to track parity between the training and production data. Model Monitor notifies us when predictions in production begin to deviate from the training and validation results. Thanks to this early warning, we can restore accuracy \u2014 by retraining the model if necessary \u2014 before our advertisers are affected. To track performance in real time, Model Monitor also sends us metrics about the quality of predictions, such as accuracy,", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "F-scores, and the distribution of the predicted classes.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "To determine if our application needs to scale, TorchServe logs resource utilization metrics for the CPU, Memory, and Disk at regular intervals; it also records the number of requests received versus the number served. For custom metrics, TorchServe offers a [Metrics API](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md).", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "A rewarding result", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "Our DL models, developed in PyTorch and deployed on Inferentia, sped up our ads analysis while cutting costs. Starting with our first explorations in DL, programming in PyTorch felt natural. Its user-friendly features helped smooth the course from our early experiments to the deployment of our multimodal ensembles. PyTorch lets us prototype and build models quickly, which is vital as our advertising service evolves and expands. For an added benefit, PyTorch works seamlessly with Inferentia and our AWS ML", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "stack. We look forward to building more use cases with PyTorch, so we can continue to serve our clients accurate, real-time results.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch feature classification changes'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Traditionally features in PyTorch were classified as either stable or experimental with an implicit third option of testing bleeding edge features by building master or through installing nightly builds (available via prebuilt whls). This has, in a few cases, caused some confusion around the level of readiness, commitment to the feature and backward compatibility that can be expected from a user perspective. Moving forward, we\u2019d like to better classify the 3 types of features as well as define explicitly", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "here what each mean from a user perspective.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "# New Feature Designations\n\nWe will continue to have three designations for features but, as mentioned, with a few changes: Stable, Beta (previously Experimental) and Prototype (previously Nightlies). Below is a brief description of each and a comment on the backward compatibility expected:", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Stable\nNothing changes here. A stable feature means that the user value-add is or has been proven, the API isn\u2019t expected to change, the feature is performant and all documentation exists to support end user adoption.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "*Level of commitment*: We expect to maintain these features long term and generally there should be no major performance limitations, gaps in documentation and we also expect to maintain backwards compatibility (although breaking changes can happen and notice will be given one release ahead of time).", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Beta", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "We previously called these features \u2018Experimental\u2019 and we found that this created confusion amongst some of the users. In the case of a Beta level features, the value add, similar to a Stable feature, has been proven (e.g. pruning is a commonly used technique for reducing the number of parameters in NN models, independent of the implementation details of our particular choices) and the feature generally works and is documented. This feature is tagged as Beta because the API may change based on user", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "feedback, because the performance needs to improve or because coverage across operators is not yet complete.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "*Level of commitment*: We are committing to seeing the feature through to the Stable classification. We are however not committing to Backwards Compatibility. Users can depend on us providing a solution for problems in this area going forward, but the APIs and performance characteristics of this feature may change.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Prototype", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Previously these were features that were known about by developers who paid close attention to RFCs and to features that land in master. In this case the feature is not available as part of binary distributions like PyPI or Conda (except maybe behind run-time flags), but we would like to get high bandwidth partner feedback ahead of a real release in order to gauge utility and any changes we need to make to the UX. To test these kinds of features we would, depending on the feature, recommend building from", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "master or using the nightly whls that are made available on pytorch.org. For each prototype feature, a pointer to draft docs or other instructions will be provided.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "*Level of commitment*: We are committing to gathering high bandwidth feedback only. Based on this feedback and potential further engagement between community members, we as a community will decide if we want to upgrade the level of commitment or to fail fast. Additionally, while some of these features might be more speculative (e.g. new Frontend APIs), others have obvious utility (e.g. model optimization) but may be in a state where gathering feedback outside of high bandwidth channels is not practical,", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "e.g. the feature may be in an earlier state, may be moving fast (PRs are landing too quickly to catch a major release) and/or generally active development is underway.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "# What changes for current features?\n\nFirst and foremost, you can find these designations on [pytorch.org/docs](http://pytorch.org/docs). We will also be linking any early stage features here for clarity.\n\nAdditionally, the following features will be reclassified under this new rubric:", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "1. [High Level Autograd APIs](https://pytorch.org/docs/stable/autograd.html#functional-higher-level-api): Beta (was Experimental)\n2. [Eager Mode Quantization](https://pytorch.org/docs/stable/quantization.html): Beta (was Experimental)\n3. [Named Tensors](https://pytorch.org/docs/stable/named_tensor.html): Prototype (was Experimental)\n4. [TorchScript/RPC](https://pytorch.org/docs/stable/rpc.html#rpc): Prototype (was Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "5. [Channels Last Memory Layout](https://pytorch.org/docs/stable/tensor_attributes.html#torch-memory-format): Beta (was Experimental)\n6. [Custom C++ Classes](https://pytorch.org/docs/stable/jit.html?highlight=experimental): Beta (was Experimental)\n7. [PyTorch Mobile](https://pytorch.org/mobile/home/): Beta (was Experimental)\n8. [Java Bindings](https://pytorch.org/docs/stable/packages.html#): Beta (was Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "9. [Torch.Sparse](https://pytorch.org/docs/stable/sparse.html?highlight=experimental#): Beta (was Experimental)", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "Cheers,\n\nJoe, Greg, Woo & Jessica", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing TorchRec, a library for modern production recommendation systems'\nauthor: Meta AI - Donny Greenberg, Colin Taylor, Dmytro Ivchenko, Xing Liu, Anirudh Sudarshan \nfeatured-img: ''\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce [TorchRec](https://github.com/pytorch/torchrec), a PyTorch domain library for Recommendation Systems. This new library provides common sparsity and parallelism primitives, enabling researchers to build state-of-the-art personalization models and deploy them in production.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "How did we get here?", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Recommendation Systems (RecSys) comprise a large footprint of production-deployed AI today, but you might not know it from looking at Github. Unlike areas like Vision and NLP, much of the ongoing innovation and development in RecSys is behind closed company doors. For academic researchers studying these techniques or companies building personalized user experiences, the field is far from democratized. Further, RecSys as an area is largely defined by learning models over sparse and/or sequential events,", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "which has large overlaps with other areas of AI. Many of the techniques are transferable, particularly for scaling and distributed execution. A large portion of the global investment in AI is in developing these RecSys techniques, so cordoning them off blocks this investment from flowing into the broader AI field.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "By mid-2020, the PyTorch team received a lot of feedback that there hasn't been a large-scale production-quality recommender systems package in the open-source PyTorch ecosystem. While we were trying to find a good answer, a group of engineers at Meta wanted to contribute Meta\u2019s production RecSys stack as a PyTorch domain library, with a strong commitment to growing an ecosystem around it. This seemed like a good idea that benefits researchers and companies across the RecSys domain. So, starting from", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Meta\u2019s stack, we began modularizing and designing a fully-scalable codebase that is adaptable for diverse recommendation use-cases. Our goal was to extract the key building blocks from across Meta\u2019s software stack to simultaneously enable creative exploration and scale. After nearly two years, a battery of benchmarks, migrations, and testing across Meta, we\u2019re excited to finally embark on this journey together with the RecSys community. We want this package to open a dialogue and collaboration across the", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "RecSys industry, starting with Meta as the first sizable contributor.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Introducing TorchRec", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "TorchRec includes a scalable low-level modeling foundation alongside rich batteries-included modules. We initially target \u201ctwo-tower\u201d ([[1]], [[2]]) architectures that have separate submodules to learn representations of candidate items and the query or context. Input signals can be a mix of floating point \u201cdense\u201d features or high-cardinality categorical \u201csparse\u201d features that require large embedding tables to be trained. Efficient training of such architectures involves combining data parallelism that", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "replicates the \u201cdense\u201d part of computation and model parallelism that partitions large embedding tables across many nodes.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "In particular, the library includes:\n- Modeling primitives, such as embedding bags and jagged tensors, that enable easy authoring of large, performant multi-device/multi-node models using hybrid data-parallelism and model-parallelism.\n- Optimized RecSys kernels powered by [FBGEMM](https://github.com/pytorch/FBGEMM) , including support for sparse and quantized operations.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "- A sharder which can partition embedding tables with a variety of different strategies including data-parallel, table-wise, row-wise, table-wise-row-wise, and column-wise sharding.\n- A planner which can automatically generate optimized sharding plans for models.\n- Pipelining to overlap dataloading device transfer (copy to GPU), inter-device communications (input_dist), and computation (forward, backward) for increased performance.\n- GPU inference support.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "- Common modules for RecSys, such as models and public datasets (Criteo & Movielens).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "To showcase the flexibility of this tooling, let\u2019s look at the following code snippet, pulled from our DLRM Event Prediction example:\n```python\n# Specify the sparse embedding layers\neb_configs = [\n EmbeddingBagConfig(\n name=f\"t_{feature_name}\",\n embedding_dim=64,\n num_embeddings=100_000,\n feature_names=[feature_name],\n )\n for feature_idx, feature_name in enumerate(DEFAULT_CAT_NAMES)\n]", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "# Import and instantiate the model with the embedding configuration\n# The \"meta\" device indicates lazy instantiation, with no memory allocated\ntrain_model = DLRM(\n embedding_bag_collection=EmbeddingBagCollection(\n tables=eb_configs, device=torch.device(\"meta\")\n ),\n dense_in_features=len(DEFAULT_INT_NAMES),\n dense_arch_layer_sizes=[512, 256, 64],\n over_arch_layer_sizes=[512, 512, 256, 1],\n dense_device=device,\n)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "# Distribute the model over many devices, just as one would with DDP.\nmodel = DistributedModelParallel(\n module=train_model,\n device=device,\n)\n\noptimizer = torch.optim.SGD(params, lr=args.learning_rate)\n# Optimize the model in a standard loop just as you would any other model!\n# Or, you can use the pipeliner to synchronize communication and compute\nfor epoch in range(epochs):\n # Train", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Scaling Performance", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "TorchRec has state-of-the-art infrastructure for scaled Recommendations AI, powering some of the largest models at Meta. It was used to train a 1.25 trillion parameter model, pushed to production in January, and a 3 trillion parameter model which will be in production soon. This should be a good indication that PyTorch is fully capable of the largest scale RecSys problems in industry. We\u2019ve heard from many in the community that sharded embeddings are a pain point. TorchRec cleanly addresses that.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Unfortunately it is challenging to provide large-scale benchmarks with public datasets, as most open-source benchmarks are too small to show performance at scale.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Looking ahead", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "Open-source and open-technology have universal benefits. Meta is seeding the PyTorch community with a state-of-the-art RecSys package, with the hope that many join in on building it forward, enabling new research and helping many companies. The team behind TorchRec plan to continue this program indefinitely, building up TorchRec to meet the needs of the RecSys community, to welcome new contributors, and to continue to power personalization at Meta. We\u2019re excited to begin this journey and look forward to", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "contributions, ideas, and feedback!", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "References\n[[1]] Sampling-Bias-Corrected Neural Modeling for Large Corpus Item Recommendations\n\n[[2]] DLRM: An advanced, open source deep learning recommendation model\n\n\n[1]: https://research.google/pubs/pub48840/\n[2]: https://ai.facebook.com/blog/dlrm-an-advanced-open-source-deep-learning-recommendation-model/", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling PyTorch models on Cloud TPUs with FSDP\"\nauthor: Ronghang Hu, Vaibhav Singh, Jack Cao, Milad Mohammadi, Yeounoh Chung, Shauheen Zahirazami, Ross Girshick\nfeatured-img: \"/assets/images/scaling-pytorch-models-on-cloud-tpus-with-fsdp.jpg\"\n---", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\nThe research community has witnessed a lot of successes with large models across NLP, computer vision, and other domains in recent years. Many of these successes were enabled by Cloud TPUs -- which are powerful hardware for distributed training. To support TPUs in PyTorch, the PyTorch/XLA library provides a backend for XLA devices (most notably TPUs) and lays the groundwork for scaling large PyTorch models on TPUs.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "However, most existing modeling scaling tools in the PyTorch ecosystem assume GPU (or CPU) devices, often depend on specific features in CUDA, and do not work directly on TPUs. The lack of scaling tools makes it challenging to build large models that cannot fit into the memory of a single TPU chip.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "To support model scaling on TPUs, we implemented the widely-adopted [Fully Sharded Data Parallel (FSDP)](https://engineering.fb.com/2021/07/15/open-source/fsdp/) algorithm for XLA devices as part of the PyTorch/XLA 1.12 release. We provide an FSDP interface with a similar high-level design to the CUDA-based PyTorch FSDP class while also handling several restrictions in XLA (see Design Notes below for more details). This FSDP interface allowed us to easily build models with e.g. 10B+ parameters on TPUs and", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "has enabled many research explorations.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Using Fully Sharded Data Parallel (FSDP) in PyTorch/XLA\n\nWe provide a wrapper class `XlaFullyShardedDataParallel` over a given PyTorch model to shard its parameters across data-parallel workers. An example usage is as follows:\n\n```python\nimport torch\nimport torch_xla.core.xla_model as xm\nfrom torch_xla.distributed.fsdp import XlaFullyShardedDataParallel as FSDP", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "model = FSDP(my_module)\noptim = torch.optim.Adam(model.parameters(), lr=0.0001)\noutput = model(x, y)\nloss = output.sum()\nloss.backward()\noptim.step()", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Wrapping an `nn.Module` instance with `XlaFullyShardedDataParallel` enables the [ZeRO-2](https://arxiv.org/abs/1910.02054) algorithm on it, where its gradients and the optimizer states are sharded for the entire training process. During its forward and backward passes, the full parameters of the wrapped module are first reconstructed from their corresponding shards for computation.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "**Nested FSDP** wrapping can be used to further save memory. This allows the model to store only the full parameters of one individual layer at any given time. For nested FSDP, one should first wrap its individual submodules with an inner FSDP before wrapping the base model with an outer FSDP. This allows the model to store only the full parameters of one individual layer at any given time. And having an outer wrapper ensures to handle any leftover parameters, corresponding to the", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "[ZeRO-3](https://arxiv.org/abs/1910.02054) algorithm. Nested FSDP wrapping can be applied at any depth of submodules and there can be more than 2 layers of nesting.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "**Model checkpoint saving and loading** for models and optimizers can be done like before by saving and loading their `.state_dict()`. Meanwhile, each training process should save its own checkpoint file of the sharded model parameters and optimizer states, and load the checkpoint file for the corresponding rank when resuming (regardless of ZeRO-2 or ZeRO-3, i.e. nested wrapping or not). A command line tool and a Python interface are provided to consolidate the sharded model checkpoint files together into a", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "full/unshareded model checkpoint file.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "[**Gradient checkpointing**](https://spell.ml/blog/gradient-checkpointing-pytorch-YGypLBAAACEAefHs) (also referred to as \"activation checkpointing\" or \"rematerialization\") is another common technique for model scaling and can be used in conjunction with FSDP. We provide `checkpoint_module`, a wrapper function over a given `nn.Module` instance for gradient checkpointing (based on `torch_xla.utils.checkpoint.checkpoint`).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "The MNIST and ImageNet examples below provide illustrative usages of (plain or nested) FSDP, saving and consolidation of model checkpoints, as well as gradient checkpointing.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Starting examples of FSDP in PyTorch/XLA", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Training MNIST and ImageNet with FSDP\n\nMNIST and ImageNet classification can often be used as starting points to build more complicated deep learning models. We provide the following FSDP examples on these two datasets:", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "- MNIST: [test/test_train_mp_mnist_fsdp_with_ckpt.py](https://github.com/pytorch/xla/blob/master/test/test_train_mp_mnist_fsdp_with_ckpt.py) (it also illustrates checkpoint saving and consolidation)\n- ImageNet: [test/test_train_mp_imagenet_fsdp.py](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet_fsdp.py)", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "A comparison of them with the vanilla data-parallel examples of [MNIST](https://github.com/pytorch/xla/blob/master/test/test_train_mp_mnist.py) and [ImageNet](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet.py) illustrates how to adapt a training script to use FSDP. A major distinction to keep in mind is that when stepping the optimizer on an FSDP-wrapped model, one should directly call `optimizer.step()` instead of `xm.optimizer_step(optimizer)`. The latter reduces the gradients", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "across ranks, which is not what we need in FSDP, where the gradients are already reduced and sharded (from a reduce-scatter op in its backward pass).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Installation\n\nFSDP is available from the PyTorch/XLA 1.12 and newer nightly releases. Please refer to [https://github.com/pytorch/xla#-available-images-and-wheels](https://github.com/pytorch/xla#-available-images-and-wheels) for a guide on installation as well as Cloud TPU allocation. Then clone PyTorch/XLA repo on a TPU VM as follows\n\n```python\nmkdir -p ~/pytorch && cd ~/pytorch\ngit clone --recursive https://github.com/pytorch/xla.git\ncd ~/\n```", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Train MNIST on v3-8 TPU\n\nIt gets around 98.9 accuracy for 2 epochs:\n\n```python\npython3 ~/pytorch/xla/test/test_train_mp_mnist_fsdp_with_ckpt.py \\\n --batch_size 16 --drop_last --num_epochs 2 \\\n --use_nested_fsdp", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "The script above automatically tests consolidation of the sharded model checkpoints at the end. You can also manually consolidate the sharded checkpoint files via\n\n```python\npython3 -m torch_xla.distributed.fsdp.consolidate_sharded_ckpts \\\n --ckpt_prefix /tmp/mnist-fsdp/final_ckpt \\\n --ckpt_suffix \"_rank-*-of-*.pth\"\n```", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Train ImageNet with ResNet-50 on v3-8 TPU\n\nIt gets around 75.9 accuracy for 100 epochs, same as what one would get without using FSDP; download and preprocess the [ImageNet-1k](https://github.com/pytorch/examples/tree/master/imagenet#requirements) dataset to `/datasets/imagenet-1k`:", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "```python\npython3 ~/pytorch/xla/test/test_train_mp_imagenet_fsdp.py \\\n --datadir /datasets/imagenet-1k --drop_last \\\n --model resnet50 --test_set_batch_size 64 --eval_interval 10 \\\n --lr 0.4 --batch_size 128 --num_warmup_epochs 5 \\\n --lr_scheduler_divide_every_n_epochs 30 --lr_scheduler_divisor 10 \\\n --num_epochs 100 \\\n --use_nested_fsdp", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "You can also explore other options in these two examples, such as `--use_gradient_checkpointing` to apply gradient checkpointing (i.e. activation checkpointing) on the ResNet blocks, or `--compute_dtype bfloat16` to perform forward and backward passes in bfloat16 precision.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Examples on large-scale models\n\nWhen building large models on TPUs, we often need to be aware of the memory constraints (e.g. 16 GB per core in TPU v3 and 32 GB per chip in TPU v4). For large models that cannot fit into a single TPU memory or the host CPU memory, one should use nested FSDP to implement the ZeRO-3 algorithm interleave submodule construction with inner FSDP wrapping, so that the full model never needs to be stored in memory during construction.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "We illustrate these cases in [https://github.com/ronghanghu/ptxla_scaling_examples](https://github.com/ronghanghu/ptxla_scaling_examples), which provides examples of training a Vision Transformer (ViT) model with 10B+ parameters on a TPU v3 pod (with 128 cores) as well as other cases.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Design Notes", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "One might wonder why we need to develop a separate FSDP class in PyTorch/XLA instead of directly reusing [PyTorch's FSDP class](https://pytorch.org/docs/stable/fsdp.html) or extending it to the XLA backend. The main motivation behind a separate FSDP class in PyTorch/XLA is that the native PyTorch's FSDP class heavily relies on CUDA features that are not supported by XLA devices, while XLA also has several unique characteristics that need special handling. These distinctions require a different", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "implementation of FSDP that would be much easier to build in a separate class.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Changes in API calls\nOne prominent distinction is that the native PyTorch FSDP is built upon separate CUDA streams for asynchronous execution in eager mode, while PyTorch/XLA runs in lazy mode and also does not support streams. In addition, TPU requires that all devices homogeneously run the same program. As a result, in the PyTorch/XLA FSDP implementation, CUDA calls and per-process heterogeneity need to be replaced by XLA APIs and alternative homogeneous implementations.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Tensor Storage Handling", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Another prominent distinction is how to free a tensor's storage, which is much harder in XLA than in CUDA. To implement ZeRO-3, one needs to free the storage of full parameters after a module's forward pass, so that the next module can reuse this memory buffer for subsequent computation. PyTorch's FSPD accomplishes this on CUDA by freeing the actual storage of a parameter `p` via `p.data.storage().resize_(0)`. However, XLA tensors do not have this `.storage()` handle given that the XLA HLO IRs are", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "completely functional and do not provide any ops to deallocate a tensor or resize its storage. Below the PyTorch interface, only the XLA compiler can decide when to free a TPU device memory corresponding to an XLA tensor, and a prerequisite is that the memory can only be released when the tensor object gets deallocated in Python -- which cannot happen in FSDP because these parameter tensors are referenced as module attributes and also saved by PyTorch autograd for the backward pass.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Our solution to this issue is to split a tensor's value properties from its autograd Variable properties, and to free a `nn.Parameter` tensor by setting its `.data` attribute to a dummy scalar of size 1. This way the actual data tensor for the full parameter gets dereferenced in Python so that XLA can recycle its memory for other computation, while autograd can still trace the base `nn.Parameter` as a weak reference to the parameter data. To get this to work, one also needs to handle views over the", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "parameters as views in PyTorch also hold references to its actual data (this required fixing a shape-related issue with views in PyTorch/XLA).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Working with XLA compiler", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "The solution above should be enough to free full parameters if the XLA compiler faithfully preserves the operations and their execution order in our PyTorch program. But there is another problem -- XLA attempts to optimize the program to speed up its execution by applying common subexpression elimination (CSE) to the HLO IRs. In a naive implementation of FSDP, the XLA compiler typically eliminates the 2nd all-gather in the backward pass to reconstruct the full parameters when it sees that it is a repeated", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "computation from the forward pass, and directly holds and reuses the full parameters we want to free up after the forward pass. To guard against this undesired compiler behavior, we introduced the [optimization barrier op](https://www.tensorflow.org/xla/operation_semantics#optimizationbarrier) into PyTorch/XLA and used it to stop eliminating the 2nd all-gather. This optimization barrier is also applied to a similar case of gradient checkpointing to prevent CSE between forward and backward passes that could", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "eliminate the rematerialization.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "In the future, if the distinctions between CUDA and XLA become not as prominent as mentioned above, it could be worth considering a merge of the PyTorch/XLA FSDP with the native PyTorch FSDP to have a unified interface.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgments\n\nThanks to Junmin Hao from AWS for reviewing the PyTorch/XLA FSDP pull request. Thanks to Brian Hirsh from the Meta PyTorch team for support on the PyTorch core issues. Thanks to Isaack Karanja, Will Cromar, and Blake Hechtman from Google for support on GCP, XLA, and TPU issues.\n\nThanks to Piotr Dollar, Wan-Yen Lo, Alex Berg, Ryan Mark, Kaiming He, Xinlei Chen, Saining Xie, Shoubhik Debnath, Min Xu, and Vaibhav Aggarwal from Meta FAIR for various TPU-related discussions.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated Diffusers with PyTorch 2.0\"\nauthor: Pedro Cuenca, Patrick von Platen, Suraj Patil\n---", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 has just been released. Its flagship new feature is `torch.compile()`, a one-line code change that promises to automatically improve performance across codebases. We have previously [checked on that promise in Hugging Face Transformers and TIMM models](https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/), and delved deep into its [motivation, architecture and the road ahead](https://pytorch.org/get-started/pytorch-2.0/).", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "As important as `torch.compile()` is, there\u2019s much more to PyTorch 2.0. Notably, PyTorch 2.0 incorporates several strategies to accelerate transformer blocks, and these improvements are very relevant for diffusion models too. Techniques such as [FlashAttention](https://arxiv.org/abs/2205.14135), for example, have become very popular in the diffusion community thanks to their ability to significantly speed up Stable Diffusion and achieve larger batch sizes, and they are now part of PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "In this post we discuss how attention layers are optimized in PyTorch 2.0 and how these optimization are applied to the popular [\ud83e\udde8 Diffusers library](https://github.com/huggingface/diffusers). We finish with a benchmark that shows how the use of PyTorch 2.0 and Diffusers immediately translates to significant performance improvements across different hardware.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Accelerating transformer blocks", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 includes a _scaled dot-product attention_ function as part of `torch.nn.functional`. This function encompasses several implementations that can be applied depending on the inputs and the hardware in use. Before PyTorch 2.0, you had to search for third-party implementations and install separate packages in order to take advantage of memory optimized algorithms, such as FlashAttention. The available implementations are:", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "* FlashAttention, from the official [FlashAttention project](https://github.com/HazyResearch/flash-attention). \n* Memory-Efficient Attention, from the [xFormers project](https://github.com/facebookresearch/xformers).\n* A native C++ implementation suitable for non-CUDA devices or when high-precision is required.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "All these methods are available by default, and PyTorch will try to select the optimal one automatically through the use of the new scaled dot-product attention (SDPA) API. You can also individually toggle them for finer-grained control, see [the documentation](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention) for details.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Using scaled dot-product attention in diffusers", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "The incorporation of Accelerated PyTorch 2.0 Transformer attention to the Diffusers library was achieved through the use of the [`set_attn_processor` method](https://huggingface.co/docs/diffusers/v0.13.0/en/api/models#diffusers.UNet2DConditionModel.set_attn_processor), which allows for pluggable attention modules to be configured. In this case, a [new attention processor was created](https://github.com/huggingface/diffusers/blob/856dad57/src/diffusers/models/cross_attention.py#L469), which is [enabled by", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "default when PyTorch 2.0 is available](https://github.com/huggingface/diffusers/blob/856dad57bb7a9ee13af4a08492e524b0a145a2c5/src/diffusers/models/cross_attention.py#L105). For clarity, this is how you could enable it manually (but it\u2019s usually not necessary since diffusers will automatically take care of it):", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "```\nfrom diffusers import StableDiffusionPipeline\nfrom diffusers.models.cross_attention import AttnProcessor2_0\n\npipe = StableDiffusionPipeline.from_pretrained(\"runwayml/stable-diffusion-v1-5\")\npipe.to(\"cuda\")\npipe.unet.set_attn_processor(AttnProcessor2_0())\n\nprompt = \"a photo of an astronaut riding a horse on mars\"\nimage = pipe(prompt).images[0]\n```", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Stable Diffusion Benchmark\n\nWe ran a number of tests using accelerated dot-product attention from PyTorch 2.0 in Diffusers. We installed diffusers from pip and used nightly versions of PyTorch 2.0, since our tests were performed before the official release. We also used `torch.set_float32_matmul_precision('high')` to enable additional fast matrix multiplication algorithms.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "We compared results with the traditional attention implementation in `diffusers` (referred to as `vanilla` below) as well as with the best-performing solution in pre-2.0 PyTorch: PyTorch 1.13.1 with the xFormers package (v0.0.16) installed.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Results were measured without compilation (i.e., no code changes at all), and also with a single call to `torch.compile()` to wrap the UNet module. We did not compile the image decoder because most of the time is spent in the 50 denoising iterations that run UNet evaluations.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Results in float32\n\n![Diffusers Speedup vs xFormers float32](/assets/images/3-16-accelerated-d/fig1-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "The following figures explore performance improvement vs batch size for various representative GPUs belonging to different generations. We collected data for each combination until we reached maximum memory utilization. Vanilla attention runs out of memory earlier than xFormers or PyTorch 2.0, which explains the missing bars for larger batch sizes. Similarly, A100 (we used the 40 GB version) is capable of running batch sizes of 64, but the other GPUs could only reach 32 in our tests.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "![Diffusers Inference Speedup vs Vanilla and xFormers Attention (A100, float32)](/assets/images/3-16-accelerated-d/fig2-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (3090, float32)](/assets/images/3-16-accelerated-d/fig3-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (4090, float32)](/assets/images/3-16-accelerated-d/fig4-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "![Diffusers Inference Speedup vs Vanilla and xFormers Attention (V100, float32)](/assets/images/3-16-accelerated-d/fig5-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "We found very significant performance improvements over vanilla attention across the board, without even using `torch.compile()`. An out of the box installation of PyTorch 2.0 and diffusers yields about 50% speedup on A100 and between 35% and 50% on 4090 GPUs, depending on batch size. Performance improvements are more pronounced for modern CUDA architectures such as Ada (4090) or Ampere (A100), but they are still very significant for older architectures still heavily in use in cloud services.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "In addition to faster speeds, the accelerated transformers implementation in PyTorch 2.0 allows much larger batch sizes to be used. A single 40GB A100 GPU runs out of memory with a batch size of 10, and 24 GB high-end consumer cards such as 3090 and 4090 cannot generate 8 images at once. Using PyTorch 2.0 and diffusers we could achieve batch sizes of **48** for 3090 and 4090, and **64** for A100. This is of great significance for cloud services and applications, as they can efficiently process more images", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "at a time.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "When compared with PyTorch 1.13.1 + xFormers, the new accelerated transformers implementation is still faster and requires no additional packages or dependencies. In this case we found moderate speedups of up to 2% on datacenter cards such as A100 or T4, but performance was great on the two last generations of consumer cards: up to 20% speed improvement on 3090 and between 10% and 45% on 4090, depending on batch size.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "When `torch.compile()` is used, we get an additional performance boost of (typically) 2% and 3% over the previous improvements. As compilation takes some time, this is better geared towards user-facing inference services or training.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Results in float16\n\n![Diffusers Speedup vs xFormers float16](/assets/images/3-16-accelerated-d/fig6-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (A100, float16)](/assets/images/3-16-accelerated-d/fig7-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (4090, float16)](/assets/images/3-16-accelerated-d/fig8-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "![Diffusers Inference Speedup vs Vanilla and xFormers Attention (3090, float16)](/assets/images/3-16-accelerated-d/fig9-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "When we consider `float16` inference, the performance improvements of the accelerated transformers implementation in PyTorch 2.0 are between 20% and 28% over standard attention, across all the GPUs we tested, except for the 4090, which belongs to the more modern Ada architecture. This GPU benefits from a dramatic performance improvement when using PyTorch 2.0 nightlies. With respect to optimized SDPA vs xFormers, results are usually on par for most GPUs, except again for the 4090. Adding `torch.compile()`", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "to the mix boosts performance a few more percentage points across the board.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Conclusions\n\nPyTorch 2.0 comes with multiple features to optimize the crucial components of the foundational transformer block, and they can be further improved with the use of `torch.compile`. These optimizations lead to significant memory and time improvements for diffusion models, and remove the need for third-party library installations.\n\nTo take advantage of these speed and memory improvements all you have to do is upgrade to PyTorch 2.0 and use diffusers >= 0.13.0.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "For more examples and in-detail benchmark numbers, please also have a look at the [Diffusers with PyTorch 2.0](https://huggingface.co/docs/diffusers/v0.13.0/en/optimization/torch2.0) docs.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgement\n\nThe authors are grateful to the PyTorch team for creating such excellent software.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Get Started with PyTorch 2.0 Summary and Overview\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/Pytorch_2_0_Animation_AdobeExpress.gif\"\n---\n\nIntroducing PyTorch 2.0, our first steps toward the next generation 2-series release of PyTorch. Over the last few years we have innovated and iterated from PyTorch 1.0 to the most recent 1.13 and moved to the newly formed PyTorch Foundation, part of the Linux Foundation.", "metadata": {"source": "https://pytorch.org/blog/getting-started-with-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "To complement the PyTorch 2.0 announcement and conference, we have also posted a comprehensive introduction and technical overview within the Get Started menu at [https://pytorch.org/get-started/pytorch-2.0](https://pytorch.org/get-started/pytorch-2.0).\n\nWe also wanted to ensure you had all the information to quickly leverage PyTorch 2.0 in your models so we added the technical requirements, tutorial, user experience, Hugging Face benchmarks and FAQs to get you started today!", "metadata": {"source": "https://pytorch.org/blog/getting-started-with-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "Autocasting to a smaller data type can, in some cases, trigger slight differences in the model\u2019s predictions. To ensure that the model\u2019s accuracy is not affected, Neuron compares the performance metrics and predictions of the FP16 and FP32 models. When autocasting diminishes the model\u2019s accuracy, we can tell the Neuron compiler to convert only the weights and certain data inputs to FP16, keeping the rest of the intermediate results in FP32. In addition, we often run a few iterations with the training data to recalibrate our autocasted models. This process is much less intensive than the original training.\n\n### Deployment", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "### Deployment\n\nTo analyze multimedia ads, we run an ensemble of DL models. All ads uploaded to Amazon are run through specialized models that assess every type of content they include: images, video and audio, headlines, texts, backgrounds, and even syntax, grammar, and potentially inappropriate language. The signals we receive from these models indicate whether or not an advertisement complies with our criteria.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "Deploying and monitoring multiple models is significantly complex, so we depend on [TorchServe](https://github.com/pytorch/serve), SageMaker\u2019s default PyTorch model serving library. Jointly developed by Facebook\u2019s PyTorch team and AWS to streamline the transition from prototyping to production, TorchServe helps us deploy trained PyTorch models at scale without having to write custom code. It provides a secure set of REST APIs for inference, management, metrics, and explanations. With features such as multi-model serving, model versioning, ensemble support, and automatic batching, TorchServe is ideal for supporting our immense workload. You can read more about deploying your Pytorch models on SageMaker with native TorchServe integration in this [blog post](https://aws.amazon.com/blogs/machine-learning/serving-pytorch-models-in-production-with-the-amazon-sagemaker-native-torchserve-integration/).", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "In some use cases, we take advantage of PyTorch\u2019s object-oriented programming paradigm to wrap multiple DL models into one parent object \u2014 a PyTorch nn.Module \u2014 and serve them as a single ensemble. In other cases, we use TorchServe to serve individual models on separate SageMaker endpoints, running on AWS Inf1 instances.\n\n### Custom handlers\n\nWe particularly appreciate that TorchServe allows us to embed our model initialization, preprocessing, inferencing, and post processing code in a single Python script, handler.py, which lives on the server. This script \u2014 the handler \u2014preprocesses the un-labeled data from an ad, runs that data through our models, and delivers the resulting inferences to downstream systems. TorchServe provides several default handlers that load weights and architecture and prepare the model to run on a particular device. We can bundle all the additional required artifacts, such as vocabulary files or label maps, with the model in a single archive file.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "When we need to deploy models that have complex initialization processes or that originated in third-party libraries, we design custom handlers in TorchServe. These let us load any model, from any library, with any required process. The following snippet shows a simple handler that can serve Hugging Face BERT models on any SageMaker hosting endpoint instance.\n\n```python\nimport torch\nimport torch.neuron\nfrom ts.torch_handler.base_handler import BaseHandler\nimport transformers\nfrom transformers import AutoModelForSequenceClassification,AutoTokenizer\n\nclass MyModelHandler(BaseHandler):\n def initialize(self, context):\n self.manifest = ctx.manifest\n properties = ctx.system_properties\n model_dir = properties.get(\"model_dir\")\n serialized_file = self.manifest[\"model\"][\"serializedFile\"]\n model_pt_path = os.path.join(model_dir, serialized_file)", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "self.tokenizer = AutoTokenizer.from_pretrained(\n model_dir, do_lower_case=True\n )\n self.model = AutoModelForSequenceClassification.from_pretrained(\n model_dir\n )\n\n def preprocess(self, data):\n\n input_text = data.get(\"data\")\n if input_text is None:\n input_text = data.get(\"body\")\n inputs = self.tokenizer.encode_plus(input_text, max_length=int(max_length), pad_to_max_length=True, add_special_tokens=True, return_tensors='pt')\n return inputs\n\n def inference(self,inputs):\n predictions = self.model(**inputs)\n return predictions\n\n def postprocess(self, output):\n return output\n```\n\n### Batching", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "### Batching\n\nHardware accelerators are optimized for parallelism, and batching \u2014 feeding a model multiple inputs in a single step \u2014 helps saturate all available capacity, typically resulting in higher throughputs. Excessively high batch sizes, however, can increase latency with minimal improvement in throughputs. Experimenting with different batch sizes helps us identify the sweet spot for our models and hardware accelerator. We run experiments to determine the best batch size for our model size, payload size, and request traffic patterns.\n\nThe Neuron compiler now supports variable batch sizes. Previously, tracing a model hardcoded the predefined batch size, so we had to pad our data, which can waste compute, slow throughputs, and exacerbate latency. Inferentia is optimized to maximize throughput for small batches, reducing latency by easing the load on the system.\n\n### Parallelism", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "### Parallelism\n\nModel parallelism on multi-cores also improves throughput and latency, which is crucial for our heavy workloads. Each Inferentia chip contains four NeuronCores that can either run separate models simultaneously or form a pipeline to stream a single model. In our use case, the data parallel configuration offers the highest throughput at the lowest cost, because it scales out concurrent processing requests.\n\nData Parallel:\n\n

\n \n

\n\nModel Parallel:\n\n

\n \n

\n\n### Monitoring\n\nIt is critical that we monitor the accuracy of our inferences in production. Models that initially make good predictions can eventually degrade in deployment as they are exposed to a wider variety of data. This phenomenon, called model drift, usually occurs when the input data distributions or the prediction targets change.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "We use [SageMaker Model Monitor](https://aws.amazon.com/sagemaker/model-monitor/) to track parity between the training and production data. Model Monitor notifies us when predictions in production begin to deviate from the training and validation results. Thanks to this early warning, we can restore accuracy \u2014 by retraining the model if necessary \u2014 before our advertisers are affected. To track performance in real time, Model Monitor also sends us metrics about the quality of predictions, such as accuracy, F-scores, and the distribution of the predicted classes.\n\nTo determine if our application needs to scale, TorchServe logs resource utilization metrics for the CPU, Memory, and Disk at regular intervals; it also records the number of requests received versus the number served. For custom metrics, TorchServe offers a [Metrics API](https://github.com/pytorch/serve/blob/master/docs/metrics_api.md).\n\n### A rewarding result", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "Our DL models, developed in PyTorch and deployed on Inferentia, sped up our ads analysis while cutting costs. Starting with our first explorations in DL, programming in PyTorch felt natural. Its user-friendly features helped smooth the course from our early experiments to the deployment of our multimodal ensembles. PyTorch lets us prototype and build models quickly, which is vital as our advertising service evolves and expands. For an added benefit, PyTorch works seamlessly with Inferentia and our AWS ML stack. We look forward to building more use cases with PyTorch, so we can continue to serve our clients accurate, real-time results.", "metadata": {"source": "https://pytorch.org/blog/amazon-ads-case-study/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch feature classification changes'\nauthor: Team PyTorch\n---\n\nTraditionally features in PyTorch were classified as either stable or experimental with an implicit third option of testing bleeding edge features by building master or through installing nightly builds (available via prebuilt whls). This has, in a few cases, caused some confusion around the level of readiness, commitment to the feature and backward compatibility that can be expected from a user perspective. Moving forward, we\u2019d like to better classify the 3 types of features as well as define explicitly here what each mean from a user perspective.\n\n# New Feature Designations\n\nWe will continue to have three designations for features but, as mentioned, with a few changes: Stable, Beta (previously Experimental) and Prototype (previously Nightlies). Below is a brief description of each and a comment on the backward compatibility expected:", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "## Stable\nNothing changes here. A stable feature means that the user value-add is or has been proven, the API isn\u2019t expected to change, the feature is performant and all documentation exists to support end user adoption.\n\n*Level of commitment*: We expect to maintain these features long term and generally there should be no major performance limitations, gaps in documentation and we also expect to maintain backwards compatibility (although breaking changes can happen and notice will be given one release ahead of time).", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "## Beta\nWe previously called these features \u2018Experimental\u2019 and we found that this created confusion amongst some of the users. In the case of a Beta level features, the value add, similar to a Stable feature, has been proven (e.g. pruning is a commonly used technique for reducing the number of parameters in NN models, independent of the implementation details of our particular choices) and the feature generally works and is documented. This feature is tagged as Beta because the API may change based on user feedback, because the performance needs to improve or because coverage across operators is not yet complete.\n\n*Level of commitment*: We are committing to seeing the feature through to the Stable classification. We are however not committing to Backwards Compatibility. Users can depend on us providing a solution for problems in this area going forward, but the APIs and performance characteristics of this feature may change.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n## Prototype\nPreviously these were features that were known about by developers who paid close attention to RFCs and to features that land in master. In this case the feature is not available as part of binary distributions like PyPI or Conda (except maybe behind run-time flags), but we would like to get high bandwidth partner feedback ahead of a real release in order to gauge utility and any changes we need to make to the UX. To test these kinds of features we would, depending on the feature, recommend building from master or using the nightly whls that are made available on pytorch.org. For each prototype feature, a pointer to draft docs or other instructions will be provided.", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "*Level of commitment*: We are committing to gathering high bandwidth feedback only. Based on this feedback and potential further engagement between community members, we as a community will decide if we want to upgrade the level of commitment or to fail fast. Additionally, while some of these features might be more speculative (e.g. new Frontend APIs), others have obvious utility (e.g. model optimization) but may be in a state where gathering feedback outside of high bandwidth channels is not practical, e.g. the feature may be in an earlier state, may be moving fast (PRs are landing too quickly to catch a major release) and/or generally active development is underway.\n\n# What changes for current features?\n\nFirst and foremost, you can find these designations on [pytorch.org/docs](http://pytorch.org/docs). We will also be linking any early stage features here for clarity.\n\nAdditionally, the following features will be reclassified under this new rubric:", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "1. [High Level Autograd APIs](https://pytorch.org/docs/stable/autograd.html#functional-higher-level-api): Beta (was Experimental)\n2. [Eager Mode Quantization](https://pytorch.org/docs/stable/quantization.html): Beta (was Experimental)\n3. [Named Tensors](https://pytorch.org/docs/stable/named_tensor.html): Prototype (was Experimental)\n4. [TorchScript/RPC](https://pytorch.org/docs/stable/rpc.html#rpc): Prototype (was Experimental)\n5. [Channels Last Memory Layout](https://pytorch.org/docs/stable/tensor_attributes.html#torch-memory-format): Beta (was Experimental)\n6. [Custom C++ Classes](https://pytorch.org/docs/stable/jit.html?highlight=experimental): Beta (was Experimental)\n7. [PyTorch Mobile](https://pytorch.org/mobile/home/): Beta (was Experimental)\n8. [Java Bindings](https://pytorch.org/docs/stable/packages.html#): Beta (was Experimental)\n9. [Torch.Sparse](https://pytorch.org/docs/stable/sparse.html?highlight=experimental#): Beta (was Experimental)\n\n\nCheers,\n\nJoe, Greg, Woo & Jessica", "metadata": {"source": "https://pytorch.org/blog/pytorch-feature-classification-changes/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Introducing TorchRec, a library for modern production recommendation systems'\nauthor: Meta AI - Donny Greenberg, Colin Taylor, Dmytro Ivchenko, Xing Liu, Anirudh Sudarshan \nfeatured-img: ''\n---\n\nWe are excited to announce [TorchRec](https://github.com/pytorch/torchrec), a PyTorch domain library for Recommendation Systems. This new library provides common sparsity and parallelism primitives, enabling researchers to build state-of-the-art personalization models and deploy them in production.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "## How did we get here?\nRecommendation Systems (RecSys) comprise a large footprint of production-deployed AI today, but you might not know it from looking at Github. Unlike areas like Vision and NLP, much of the ongoing innovation and development in RecSys is behind closed company doors. For academic researchers studying these techniques or companies building personalized user experiences, the field is far from democratized. Further, RecSys as an area is largely defined by learning models over sparse and/or sequential events, which has large overlaps with other areas of AI. Many of the techniques are transferable, particularly for scaling and distributed execution. A large portion of the global investment in AI is in developing these RecSys techniques, so cordoning them off blocks this investment from flowing into the broader AI field.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "By mid-2020, the PyTorch team received a lot of feedback that there hasn't been a large-scale production-quality recommender systems package in the open-source PyTorch ecosystem. While we were trying to find a good answer, a group of engineers at Meta wanted to contribute Meta\u2019s production RecSys stack as a PyTorch domain library, with a strong commitment to growing an ecosystem around it. This seemed like a good idea that benefits researchers and companies across the RecSys domain. So, starting from Meta\u2019s stack, we began modularizing and designing a fully-scalable codebase that is adaptable for diverse recommendation use-cases. Our goal was to extract the key building blocks from across Meta\u2019s software stack to simultaneously enable creative exploration and scale. After nearly two years, a battery of benchmarks, migrations, and testing across Meta, we\u2019re excited to finally embark on this journey together with the RecSys community. We want this package to open a dialogue and collaboration across the RecSys industry, starting with Meta as the first sizable contributor.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "## Introducing TorchRec\nTorchRec includes a scalable low-level modeling foundation alongside rich batteries-included modules. We initially target \u201ctwo-tower\u201d ([[1]], [[2]]) architectures that have separate submodules to learn representations of candidate items and the query or context. Input signals can be a mix of floating point \u201cdense\u201d features or high-cardinality categorical \u201csparse\u201d features that require large embedding tables to be trained. Efficient training of such architectures involves combining data parallelism that replicates the \u201cdense\u201d part of computation and model parallelism that partitions large embedding tables across many nodes.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "In particular, the library includes:\n- Modeling primitives, such as embedding bags and jagged tensors, that enable easy authoring of large, performant multi-device/multi-node models using hybrid data-parallelism and model-parallelism.\n- Optimized RecSys kernels powered by [FBGEMM](https://github.com/pytorch/FBGEMM) , including support for sparse and quantized operations.\n- A sharder which can partition embedding tables with a variety of different strategies including data-parallel, table-wise, row-wise, table-wise-row-wise, and column-wise sharding.\n- A planner which can automatically generate optimized sharding plans for models.\n- Pipelining to overlap dataloading device transfer (copy to GPU), inter-device communications (input_dist), and computation (forward, backward) for increased performance.\n- GPU inference support.\n- Common modules for RecSys, such as models and public datasets (Criteo & Movielens).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "To showcase the flexibility of this tooling, let\u2019s look at the following code snippet, pulled from our DLRM Event Prediction example:\n```python\n# Specify the sparse embedding layers\neb_configs = [\n EmbeddingBagConfig(\n name=f\"t_{feature_name}\",\n embedding_dim=64,\n num_embeddings=100_000,\n feature_names=[feature_name],\n )\n for feature_idx, feature_name in enumerate(DEFAULT_CAT_NAMES)\n]\n\n# Import and instantiate the model with the embedding configuration\n# The \"meta\" device indicates lazy instantiation, with no memory allocated\ntrain_model = DLRM(\n embedding_bag_collection=EmbeddingBagCollection(\n tables=eb_configs, device=torch.device(\"meta\")\n ),\n dense_in_features=len(DEFAULT_INT_NAMES),\n dense_arch_layer_sizes=[512, 256, 64],\n over_arch_layer_sizes=[512, 512, 256, 1],\n dense_device=device,\n)\n\n# Distribute the model over many devices, just as one would with DDP.\nmodel = DistributedModelParallel(\n module=train_model,\n device=device,\n)", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "optimizer = torch.optim.SGD(params, lr=args.learning_rate)\n# Optimize the model in a standard loop just as you would any other model!\n# Or, you can use the pipeliner to synchronize communication and compute\nfor epoch in range(epochs):\n # Train\n```\n\n\n## Scaling Performance\nTorchRec has state-of-the-art infrastructure for scaled Recommendations AI, powering some of the largest models at Meta. It was used to train a 1.25 trillion parameter model, pushed to production in January, and a 3 trillion parameter model which will be in production soon. This should be a good indication that PyTorch is fully capable of the largest scale RecSys problems in industry. We\u2019ve heard from many in the community that sharded embeddings are a pain point. TorchRec cleanly addresses that. Unfortunately it is challenging to provide large-scale benchmarks with public datasets, as most open-source benchmarks are too small to show performance at scale.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "## Looking ahead\nOpen-source and open-technology have universal benefits. Meta is seeding the PyTorch community with a state-of-the-art RecSys package, with the hope that many join in on building it forward, enabling new research and helping many companies. The team behind TorchRec plan to continue this program indefinitely, building up TorchRec to meet the needs of the RecSys community, to welcome new contributors, and to continue to power personalization at Meta. We\u2019re excited to begin this journey and look forward to contributions, ideas, and feedback!\n\n\n## References\n[[1]] Sampling-Bias-Corrected Neural Modeling for Large Corpus Item Recommendations\n\n[[2]] DLRM: An advanced, open source deep learning recommendation model\n\n\n[1]: https://research.google/pubs/pub48840/\n[2]: https://ai.facebook.com/blog/dlrm-an-advanced-open-source-deep-learning-recommendation-model/", "metadata": {"source": "https://pytorch.org/blog/introducing-torchrec/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Scaling PyTorch models on Cloud TPUs with FSDP\"\nauthor: Ronghang Hu, Vaibhav Singh, Jack Cao, Milad Mohammadi, Yeounoh Chung, Shauheen Zahirazami, Ross Girshick\nfeatured-img: \"/assets/images/scaling-pytorch-models-on-cloud-tpus-with-fsdp.jpg\"\n---\n\n## Introduction\n\nThe research community has witnessed a lot of successes with large models across NLP, computer vision, and other domains in recent years. Many of these successes were enabled by Cloud TPUs -- which are powerful hardware for distributed training. To support TPUs in PyTorch, the PyTorch/XLA library provides a backend for XLA devices (most notably TPUs) and lays the groundwork for scaling large PyTorch models on TPUs.\n\nHowever, most existing modeling scaling tools in the PyTorch ecosystem assume GPU (or CPU) devices, often depend on specific features in CUDA, and do not work directly on TPUs. The lack of scaling tools makes it challenging to build large models that cannot fit into the memory of a single TPU chip.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "To support model scaling on TPUs, we implemented the widely-adopted [Fully Sharded Data Parallel (FSDP)](https://engineering.fb.com/2021/07/15/open-source/fsdp/) algorithm for XLA devices as part of the PyTorch/XLA 1.12 release. We provide an FSDP interface with a similar high-level design to the CUDA-based PyTorch FSDP class while also handling several restrictions in XLA (see Design Notes below for more details). This FSDP interface allowed us to easily build models with e.g. 10B+ parameters on TPUs and has enabled many research explorations.\n\n## Using Fully Sharded Data Parallel (FSDP) in PyTorch/XLA\n\nWe provide a wrapper class `XlaFullyShardedDataParallel` over a given PyTorch model to shard its parameters across data-parallel workers. An example usage is as follows:\n\n```python\nimport torch\nimport torch_xla.core.xla_model as xm\nfrom torch_xla.distributed.fsdp import XlaFullyShardedDataParallel as FSDP", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "model = FSDP(my_module)\noptim = torch.optim.Adam(model.parameters(), lr=0.0001)\noutput = model(x, y)\nloss = output.sum()\nloss.backward()\noptim.step()\n```\n\nWrapping an `nn.Module` instance with `XlaFullyShardedDataParallel` enables the [ZeRO-2](https://arxiv.org/abs/1910.02054) algorithm on it, where its gradients and the optimizer states are sharded for the entire training process. During its forward and backward passes, the full parameters of the wrapped module are first reconstructed from their corresponding shards for computation.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "**Nested FSDP** wrapping can be used to further save memory. This allows the model to store only the full parameters of one individual layer at any given time. For nested FSDP, one should first wrap its individual submodules with an inner FSDP before wrapping the base model with an outer FSDP. This allows the model to store only the full parameters of one individual layer at any given time. And having an outer wrapper ensures to handle any leftover parameters, corresponding to the [ZeRO-3](https://arxiv.org/abs/1910.02054) algorithm. Nested FSDP wrapping can be applied at any depth of submodules and there can be more than 2 layers of nesting.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "**Model checkpoint saving and loading** for models and optimizers can be done like before by saving and loading their `.state_dict()`. Meanwhile, each training process should save its own checkpoint file of the sharded model parameters and optimizer states, and load the checkpoint file for the corresponding rank when resuming (regardless of ZeRO-2 or ZeRO-3, i.e. nested wrapping or not). A command line tool and a Python interface are provided to consolidate the sharded model checkpoint files together into a full/unshareded model checkpoint file.\n\n[**Gradient checkpointing**](https://spell.ml/blog/gradient-checkpointing-pytorch-YGypLBAAACEAefHs) (also referred to as \"activation checkpointing\" or \"rematerialization\") is another common technique for model scaling and can be used in conjunction with FSDP. We provide `checkpoint_module`, a wrapper function over a given `nn.Module` instance for gradient checkpointing (based on `torch_xla.utils.checkpoint.checkpoint`).", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "The MNIST and ImageNet examples below provide illustrative usages of (plain or nested) FSDP, saving and consolidation of model checkpoints, as well as gradient checkpointing.\n\n## Starting examples of FSDP in PyTorch/XLA\n\n### Training MNIST and ImageNet with FSDP\n\nMNIST and ImageNet classification can often be used as starting points to build more complicated deep learning models. We provide the following FSDP examples on these two datasets:\n\n- MNIST: [test/test_train_mp_mnist_fsdp_with_ckpt.py](https://github.com/pytorch/xla/blob/master/test/test_train_mp_mnist_fsdp_with_ckpt.py) (it also illustrates checkpoint saving and consolidation)\n- ImageNet: [test/test_train_mp_imagenet_fsdp.py](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet_fsdp.py)", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "A comparison of them with the vanilla data-parallel examples of [MNIST](https://github.com/pytorch/xla/blob/master/test/test_train_mp_mnist.py) and [ImageNet](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet.py) illustrates how to adapt a training script to use FSDP. A major distinction to keep in mind is that when stepping the optimizer on an FSDP-wrapped model, one should directly call `optimizer.step()` instead of `xm.optimizer_step(optimizer)`. The latter reduces the gradients across ranks, which is not what we need in FSDP, where the gradients are already reduced and sharded (from a reduce-scatter op in its backward pass).\n\n#### Installation\n\nFSDP is available from the PyTorch/XLA 1.12 and newer nightly releases. Please refer to [https://github.com/pytorch/xla#-available-images-and-wheels](https://github.com/pytorch/xla#-available-images-and-wheels) for a guide on installation as well as Cloud TPU allocation. Then clone PyTorch/XLA repo on a TPU VM as follows", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "```python\nmkdir -p ~/pytorch && cd ~/pytorch\ngit clone --recursive https://github.com/pytorch/xla.git\ncd ~/\n```\n\n#### Train MNIST on v3-8 TPU\n\nIt gets around 98.9 accuracy for 2 epochs:\n\n```python\npython3 ~/pytorch/xla/test/test_train_mp_mnist_fsdp_with_ckpt.py \\\n --batch_size 16 --drop_last --num_epochs 2 \\\n --use_nested_fsdp\n```\n\nThe script above automatically tests consolidation of the sharded model checkpoints at the end. You can also manually consolidate the sharded checkpoint files via\n\n```python\npython3 -m torch_xla.distributed.fsdp.consolidate_sharded_ckpts \\\n --ckpt_prefix /tmp/mnist-fsdp/final_ckpt \\\n --ckpt_suffix \"_rank-*-of-*.pth\"\n```\n\n#### Train ImageNet with ResNet-50 on v3-8 TPU\n\nIt gets around 75.9 accuracy for 100 epochs, same as what one would get without using FSDP; download and preprocess the [ImageNet-1k](https://github.com/pytorch/examples/tree/master/imagenet#requirements) dataset to `/datasets/imagenet-1k`:", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "```python\npython3 ~/pytorch/xla/test/test_train_mp_imagenet_fsdp.py \\\n --datadir /datasets/imagenet-1k --drop_last \\\n --model resnet50 --test_set_batch_size 64 --eval_interval 10 \\\n --lr 0.4 --batch_size 128 --num_warmup_epochs 5 \\\n --lr_scheduler_divide_every_n_epochs 30 --lr_scheduler_divisor 10 \\\n --num_epochs 100 \\\n --use_nested_fsdp\n```\n\nYou can also explore other options in these two examples, such as `--use_gradient_checkpointing` to apply gradient checkpointing (i.e. activation checkpointing) on the ResNet blocks, or `--compute_dtype bfloat16` to perform forward and backward passes in bfloat16 precision.\n\n### Examples on large-scale models", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "When building large models on TPUs, we often need to be aware of the memory constraints (e.g. 16 GB per core in TPU v3 and 32 GB per chip in TPU v4). For large models that cannot fit into a single TPU memory or the host CPU memory, one should use nested FSDP to implement the ZeRO-3 algorithm interleave submodule construction with inner FSDP wrapping, so that the full model never needs to be stored in memory during construction.\n\nWe illustrate these cases in [https://github.com/ronghanghu/ptxla_scaling_examples](https://github.com/ronghanghu/ptxla_scaling_examples), which provides examples of training a Vision Transformer (ViT) model with 10B+ parameters on a TPU v3 pod (with 128 cores) as well as other cases.\n\n### Design Notes", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "### Design Notes\n\nOne might wonder why we need to develop a separate FSDP class in PyTorch/XLA instead of directly reusing [PyTorch's FSDP class](https://pytorch.org/docs/stable/fsdp.html) or extending it to the XLA backend. The main motivation behind a separate FSDP class in PyTorch/XLA is that the native PyTorch's FSDP class heavily relies on CUDA features that are not supported by XLA devices, while XLA also has several unique characteristics that need special handling. These distinctions require a different implementation of FSDP that would be much easier to build in a separate class.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "#### Changes in API calls\nOne prominent distinction is that the native PyTorch FSDP is built upon separate CUDA streams for asynchronous execution in eager mode, while PyTorch/XLA runs in lazy mode and also does not support streams. In addition, TPU requires that all devices homogeneously run the same program. As a result, in the PyTorch/XLA FSDP implementation, CUDA calls and per-process heterogeneity need to be replaced by XLA APIs and alternative homogeneous implementations.\n\n#### Tensor Storage Handling", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "Another prominent distinction is how to free a tensor's storage, which is much harder in XLA than in CUDA. To implement ZeRO-3, one needs to free the storage of full parameters after a module's forward pass, so that the next module can reuse this memory buffer for subsequent computation. PyTorch's FSPD accomplishes this on CUDA by freeing the actual storage of a parameter `p` via `p.data.storage().resize_(0)`. However, XLA tensors do not have this `.storage()` handle given that the XLA HLO IRs are completely functional and do not provide any ops to deallocate a tensor or resize its storage. Below the PyTorch interface, only the XLA compiler can decide when to free a TPU device memory corresponding to an XLA tensor, and a prerequisite is that the memory can only be released when the tensor object gets deallocated in Python -- which cannot happen in FSDP because these parameter tensors are referenced as module attributes and also saved by PyTorch autograd for the backward pass.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "Our solution to this issue is to split a tensor's value properties from its autograd Variable properties, and to free a `nn.Parameter` tensor by setting its `.data` attribute to a dummy scalar of size 1. This way the actual data tensor for the full parameter gets dereferenced in Python so that XLA can recycle its memory for other computation, while autograd can still trace the base `nn.Parameter` as a weak reference to the parameter data. To get this to work, one also needs to handle views over the parameters as views in PyTorch also hold references to its actual data (this required fixing a shape-related issue with views in PyTorch/XLA).\n\n#### Working with XLA compiler", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "The solution above should be enough to free full parameters if the XLA compiler faithfully preserves the operations and their execution order in our PyTorch program. But there is another problem -- XLA attempts to optimize the program to speed up its execution by applying common subexpression elimination (CSE) to the HLO IRs. In a naive implementation of FSDP, the XLA compiler typically eliminates the 2nd all-gather in the backward pass to reconstruct the full parameters when it sees that it is a repeated computation from the forward pass, and directly holds and reuses the full parameters we want to free up after the forward pass. To guard against this undesired compiler behavior, we introduced the [optimization barrier op](https://www.tensorflow.org/xla/operation_semantics#optimizationbarrier) into PyTorch/XLA and used it to stop eliminating the 2nd all-gather. This optimization barrier is also applied to a similar case of gradient checkpointing to prevent CSE between forward and backward passes that could eliminate the rematerialization.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "In the future, if the distinctions between CUDA and XLA become not as prominent as mentioned above, it could be worth considering a merge of the PyTorch/XLA FSDP with the native PyTorch FSDP to have a unified interface.\n\n## Acknowledgments\n\nThanks to Junmin Hao from AWS for reviewing the PyTorch/XLA FSDP pull request. Thanks to Brian Hirsh from the Meta PyTorch team for support on the PyTorch core issues. Thanks to Isaack Karanja, Will Cromar, and Blake Hechtman from Google for support on GCP, XLA, and TPU issues.\n\nThanks to Piotr Dollar, Wan-Yen Lo, Alex Berg, Ryan Mark, Kaiming He, Xinlei Chen, Saining Xie, Shoubhik Debnath, Min Xu, and Vaibhav Aggarwal from Meta FAIR for various TPU-related discussions.", "metadata": {"source": "https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated Diffusers with PyTorch 2.0\"\nauthor: Pedro Cuenca, Patrick von Platen, Suraj Patil\n---\n\nPyTorch 2.0 has just been released. Its flagship new feature is `torch.compile()`, a one-line code change that promises to automatically improve performance across codebases. We have previously [checked on that promise in Hugging Face Transformers and TIMM models](https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/), and delved deep into its [motivation, architecture and the road ahead](https://pytorch.org/get-started/pytorch-2.0/).", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "As important as `torch.compile()` is, there\u2019s much more to PyTorch 2.0. Notably, PyTorch 2.0 incorporates several strategies to accelerate transformer blocks, and these improvements are very relevant for diffusion models too. Techniques such as [FlashAttention](https://arxiv.org/abs/2205.14135), for example, have become very popular in the diffusion community thanks to their ability to significantly speed up Stable Diffusion and achieve larger batch sizes, and they are now part of PyTorch 2.0.\n\nIn this post we discuss how attention layers are optimized in PyTorch 2.0 and how these optimization are applied to the popular [\ud83e\udde8 Diffusers library](https://github.com/huggingface/diffusers). We finish with a benchmark that shows how the use of PyTorch 2.0 and Diffusers immediately translates to significant performance improvements across different hardware.\n\n\n## Accelerating transformer blocks", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 2.0 includes a _scaled dot-product attention_ function as part of `torch.nn.functional`. This function encompasses several implementations that can be applied depending on the inputs and the hardware in use. Before PyTorch 2.0, you had to search for third-party implementations and install separate packages in order to take advantage of memory optimized algorithms, such as FlashAttention. The available implementations are:\n* FlashAttention, from the official [FlashAttention project](https://github.com/HazyResearch/flash-attention). \n* Memory-Efficient Attention, from the [xFormers project](https://github.com/facebookresearch/xformers).\n* A native C++ implementation suitable for non-CUDA devices or when high-precision is required.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "All these methods are available by default, and PyTorch will try to select the optimal one automatically through the use of the new scaled dot-product attention (SDPA) API. You can also individually toggle them for finer-grained control, see [the documentation](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention) for details.\n\n\n## Using scaled dot-product attention in diffusers", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "The incorporation of Accelerated PyTorch 2.0 Transformer attention to the Diffusers library was achieved through the use of the [`set_attn_processor` method](https://huggingface.co/docs/diffusers/v0.13.0/en/api/models#diffusers.UNet2DConditionModel.set_attn_processor), which allows for pluggable attention modules to be configured. In this case, a [new attention processor was created](https://github.com/huggingface/diffusers/blob/856dad57/src/diffusers/models/cross_attention.py#L469), which is [enabled by default when PyTorch 2.0 is available](https://github.com/huggingface/diffusers/blob/856dad57bb7a9ee13af4a08492e524b0a145a2c5/src/diffusers/models/cross_attention.py#L105). For clarity, this is how you could enable it manually (but it\u2019s usually not necessary since diffusers will automatically take care of it):\n\n```\nfrom diffusers import StableDiffusionPipeline\nfrom diffusers.models.cross_attention import AttnProcessor2_0", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "pipe = StableDiffusionPipeline.from_pretrained(\"runwayml/stable-diffusion-v1-5\")\npipe.to(\"cuda\")\npipe.unet.set_attn_processor(AttnProcessor2_0())\n\nprompt = \"a photo of an astronaut riding a horse on mars\"\nimage = pipe(prompt).images[0]\n```\n\n## Stable Diffusion Benchmark\n\nWe ran a number of tests using accelerated dot-product attention from PyTorch 2.0 in Diffusers. We installed diffusers from pip and used nightly versions of PyTorch 2.0, since our tests were performed before the official release. We also used `torch.set_float32_matmul_precision('high')` to enable additional fast matrix multiplication algorithms.\n\nWe compared results with the traditional attention implementation in `diffusers` (referred to as `vanilla` below) as well as with the best-performing solution in pre-2.0 PyTorch: PyTorch 1.13.1 with the xFormers package (v0.0.16) installed.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "Results were measured without compilation (i.e., no code changes at all), and also with a single call to `torch.compile()` to wrap the UNet module. We did not compile the image decoder because most of the time is spent in the 50 denoising iterations that run UNet evaluations.\n\n\n### Results in float32\n\n![Diffusers Speedup vs xFormers float32](/assets/images/3-16-accelerated-d/fig1-latest.png){:width=\"100%\"}\n\nThe following figures explore performance improvement vs batch size for various representative GPUs belonging to different generations. We collected data for each combination until we reached maximum memory utilization. Vanilla attention runs out of memory earlier than xFormers or PyTorch 2.0, which explains the missing bars for larger batch sizes. Similarly, A100 (we used the 40 GB version) is capable of running batch sizes of 64, but the other GPUs could only reach 32 in our tests.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "![Diffusers Inference Speedup vs Vanilla and xFormers Attention (A100, float32)](/assets/images/3-16-accelerated-d/fig2-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (3090, float32)](/assets/images/3-16-accelerated-d/fig3-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (4090, float32)](/assets/images/3-16-accelerated-d/fig4-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (V100, float32)](/assets/images/3-16-accelerated-d/fig5-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "We found very significant performance improvements over vanilla attention across the board, without even using `torch.compile()`. An out of the box installation of PyTorch 2.0 and diffusers yields about 50% speedup on A100 and between 35% and 50% on 4090 GPUs, depending on batch size. Performance improvements are more pronounced for modern CUDA architectures such as Ada (4090) or Ampere (A100), but they are still very significant for older architectures still heavily in use in cloud services.\n\nIn addition to faster speeds, the accelerated transformers implementation in PyTorch 2.0 allows much larger batch sizes to be used. A single 40GB A100 GPU runs out of memory with a batch size of 10, and 24 GB high-end consumer cards such as 3090 and 4090 cannot generate 8 images at once. Using PyTorch 2.0 and diffusers we could achieve batch sizes of **48** for 3090 and 4090, and **64** for A100. This is of great significance for cloud services and applications, as they can efficiently process more images at a time.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "When compared with PyTorch 1.13.1 + xFormers, the new accelerated transformers implementation is still faster and requires no additional packages or dependencies. In this case we found moderate speedups of up to 2% on datacenter cards such as A100 or T4, but performance was great on the two last generations of consumer cards: up to 20% speed improvement on 3090 and between 10% and 45% on 4090, depending on batch size.\n\nWhen `torch.compile()` is used, we get an additional performance boost of (typically) 2% and 3% over the previous improvements. As compilation takes some time, this is better geared towards user-facing inference services or training.\n\n\n### Results in float16\n\n![Diffusers Speedup vs xFormers float16](/assets/images/3-16-accelerated-d/fig6-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (A100, float16)](/assets/images/3-16-accelerated-d/fig7-latest.png){:width=\"100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "![Diffusers Inference Speedup vs Vanilla and xFormers Attention (4090, float16)](/assets/images/3-16-accelerated-d/fig8-latest.png){:width=\"100%\"}\n\n![Diffusers Inference Speedup vs Vanilla and xFormers Attention (3090, float16)](/assets/images/3-16-accelerated-d/fig9-latest.png){:width=\"100%\"}\n\nWhen we consider `float16` inference, the performance improvements of the accelerated transformers implementation in PyTorch 2.0 are between 20% and 28% over standard attention, across all the GPUs we tested, except for the 4090, which belongs to the more modern Ada architecture. This GPU benefits from a dramatic performance improvement when using PyTorch 2.0 nightlies. With respect to optimized SDPA vs xFormers, results are usually on par for most GPUs, except again for the 4090. Adding `torch.compile()` to the mix boosts performance a few more percentage points across the board.\n\n\n## Conclusions", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "## Conclusions\n\nPyTorch 2.0 comes with multiple features to optimize the crucial components of the foundational transformer block, and they can be further improved with the use of `torch.compile`. These optimizations lead to significant memory and time improvements for diffusion models, and remove the need for third-party library installations.\n\nTo take advantage of these speed and memory improvements all you have to do is upgrade to PyTorch 2.0 and use diffusers >= 0.13.0.\n\nFor more examples and in-detail benchmark numbers, please also have a look at the [Diffusers with PyTorch 2.0](https://huggingface.co/docs/diffusers/v0.13.0/en/optimization/torch2.0) docs.\n\n\n## Acknowledgement\n\nThe authors are grateful to the PyTorch team for creating such excellent software.", "metadata": {"source": "https://pytorch.org/blog/accelerated-diffusers-pt-20/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Get Started with PyTorch 2.0 Summary and Overview\"\nauthor: Team PyTorch\nfeatured-img: \"assets/images/Pytorch_2_0_Animation_AdobeExpress.gif\"\n---\n\nIntroducing PyTorch 2.0, our first steps toward the next generation 2-series release of PyTorch. Over the last few years we have innovated and iterated from PyTorch 1.0 to the most recent 1.13 and moved to the newly formed PyTorch Foundation, part of the Linux Foundation.\n\nTo complement the PyTorch 2.0 announcement and conference, we have also posted a comprehensive introduction and technical overview within the Get Started menu at [https://pytorch.org/get-started/pytorch-2.0](https://pytorch.org/get-started/pytorch-2.0).\n\nWe also wanted to ensure you had all the information to quickly leverage PyTorch 2.0 in your models so we added the technical requirements, tutorial, user experience, Hugging Face benchmarks and FAQs to get you started today!", "metadata": {"source": "https://pytorch.org/blog/getting-started-with-pytorch-2.0/", "category": "pytorch blogs"}} {"page_content": "Finally we are launching a new \u201cAsk the Engineers: 2.0 Live Q&A\u201d series that allows you to go deeper on a range of topics with PyTorch subject matter experts. We hope this content is helpful for the entire community and level of users/contributors.\n\n[https://pytorch.org/get-started/pytorch-2.0](https://pytorch.org/get-started/pytorch-2.0)", "metadata": {"source": "https://pytorch.org/blog/getting-started-with-pytorch-2.0/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'New Library Releases in PyTorch 1.10, including TorchX, TorchAudio, TorchVision'\nauthor: Team PyTorch \n---\n\nToday, we are announcing a number of new features and improvements to PyTorch libraries, alongside the [PyTorch 1.10 release](https://pytorch.org/blog/pytorch-1.10-released/). Some highlights include:\n\nSome highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchX** - a new SDK for quickly building and deploying ML applications from research & development to production. \n* **TorchAudio** - Added text-to-speech pipeline, self-supervised model support, multi-channel support and MVDR beamforming module, RNN transducer (RNNT) loss function, and batch and filterbank support to `lfilter` function. See the TorchAudio release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchVision** - Added new RegNet and EfficientNet models, FX based feature extraction added to utilities, two new Automatic Augmentation techniques: Rand Augment and Trivial Augment, and updated training recipes. See the TorchVision release notes [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "# Introducing TorchX\nTorchX is a new SDK for quickly building and deploying ML applications from research & development to production. It offers various builtin components that encode MLOps best practices and make advanced features like distributed training and hyperparameter optimization accessible to all.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Users can get started with TorchX 0.1 with no added setup cost since it supports popular ML schedulers and pipeline orchestrators that are already widely adopted and deployed in production. No two production environments are the same. To comply with various use cases, TorchX\u2019s core APIs allow tons of customization at well-defined extension points so that even the most unique applications can be serviced without customizing the whole vertical stack.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Read the [documentation](https://pytorch.org/torchx) for more details and try out this feature using this quickstart [tutorial](https://pytorch.org/torchx/latest/quickstart.html). \n\n\n# TorchAudio 0.10", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Text-to-speech pipeline\nTorchAudio now adds the Tacotron2 model and pretrained weights. It is now possible to build a text-to-speech pipeline with existing vocoder implementations like WaveRNN and Griffin-Lim. Building a TTS pipeline requires matching data processing and pretrained weights, which are often non-trivial to users. So TorchAudio introduces a bundle API so that constructing pipelines for specific pretrained weights is easy. The following example illustrates this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> import torchaudio\n>>>\n>>> bundle = torchaudio.pipelines.TACOTRON2_WAVERNN_CHAR_LJSPEECH\n>>>\n>>> # Build text processor, Tacotron2 and vocoder (WaveRNN) model\n>>> processor = bundle.get_text_processor()\n>>> tacotron2 = bundle.get_tacotron2()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 107M/107M [00:01<00:00, 87.9MB/s]\n>>> vocoder = bundle.get_vocoder()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 16.7M/16.7M [00:00<00:00, 78.1MB/s]\n>>>\n>>> text = \"Hello World!\"\n>>>", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": ">>> # Encode text\n>>> input, lengths = processor(text)\n>>>\n>>> # Generate (mel-scale) spectrogram\n>>> specgram, lengths, _ = tacotron2.infer(input, lengths)\n>>>\n>>> # Convert spectrogram to waveform\n>>> waveforms, lengths = vocoder(specgram, lengths)\n>>>\n>>> # Save audio\n>>> torchaudio.save('hello-world.wav', waveforms, vocoder.sample_rate)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For the details of this API please refer to [the documentation](https://pytorch.org/audio/0.10.0/pipelines#tacotron2-text-to-speech). You can also try this from [the tutorial](https://pytorch.org/tutorials/intermediate/text_to_speech_with_torchaudio.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Self-Supervised Model Support", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio added HuBERT model architecture and pre-trained weight support for wav2vec 2.0 and HuBERT. HuBERT and wav2vec 2.0 are novel ways for audio representation learning and they yield high accuracy when fine-tuned on downstream tasks. These models can serve as baseline in future research, therefore, TorchAudio is providing a simple way to run the model. Similar to the TTS pipeline, the pretrained weights and associated information, such as expected sample rates and output class labels (for fine-tuned", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "weights) are put together as a bundle, so that they can be used to build pipelines. The following example illustrates this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> import torchaudio\n>>>\n>>> bundle = torchaudio.pipelines.HUBERT_ASR_LARGE\n>>>\n>>> # Build the model and load pretrained weight.\n>>> model = bundle.get_model()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 1.18G/1.18G [00:17<00:00, 73.8MB/s]\n>>> # Check the corresponding labels of the output.\n>>> labels = bundle.get_labels()\n>>> print(labels)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "('', '', '', '', '|', 'E', 'T', 'A', 'O', 'N', 'I', 'H', 'S', 'R', 'D', 'L', 'U', 'M', 'W', 'C', 'F', 'G', 'Y', 'P', 'B', 'V', 'K', \"'\", 'X', 'J', 'Q', 'Z')\n>>>\n>>> # Infer the label probability distribution\n>>> waveform, sample_rate = torchaudio.load(hello-world.wav')\n>>>\n>>> emissions, _ = model(waveform)\n>>>\n>>> # Pass emission to (hypothetical) decoder\n>>> transcripts = ctc_decode(emissions, labels)\n>>> print(transcripts[0])\nHELLO WORLD", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/0.10.0/pipelines#wav2vec-2-0-hubert-representation-learning) for more details and try out this feature using this [tutorial](https://pytorch.org/tutorials/intermediate/speech_command_recognition_with_torchaudio_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Multi-channel support and MVDR beamforming \nFar-field speech recognition is a more challenging task compared to near-field recognition. Multi-channel methods such as beamforming help reduce the noises and enhance the target speech.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio now adds support for differentiable Minimum Variance Distortionless Response (MVDR) beamforming on multi-channel audio using Time-Frequency masks. Researchers can easily assemble it with any multi-channel ASR pipeline. There are three solutions (ref_channel, stv_evd, stv_power) and it supports single-channel and multi-channel (perform average in the method) masks. It provides an online option that recursively updates the parameters for streaming audio. We also provide a tutorial on how to apply", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "MVDR beamforming to the multi-channel audio in the example directory.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> from torchaudio.transforms import MVDR, Spectrogram, InverseSpectrogram\n>>>\n>>> # Load the multi-channel noisy audio\n>>> waveform_mix, sr = torchaudio.load('mix.wav')\n>>> # Initialize the stft and istft modules\n>>> stft = Spectrogram(n_fft=1024, hop_length=256, return_complex=True, power=None)\n>>> istft = InverseSpectrogram(n_fft=1024, hop_length=256)\n>>> # Get the noisy spectrogram\n>>> specgram_mix = stft(waveform_mix)\n>>> # Get the Time-Frequency mask via machine learning models", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": ">>> mask = model(waveform)\n>>> # Initialize the MVDR module \n>>> mvdr = MVDR(ref_channel=0, solution=\u201dref_channel\u201d, multi_mask=False)\n>>> # Apply MVDR beamforming\n>>> specgram_enhanced = mvdr(specgram_mix, mask)\n>>> # Get the enhanced waveform via iSTFT\n>>> waveform_enhanced = istft(specgram_enhanced, length=waveform.shape[-1])\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/0.10.0/transforms.html#mvdr) for more details and try out this feature using the [MVDR tutorial](https://github.com/pytorch/audio/blob/main/examples/beamforming/MVDR_tutorial.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) RNN Transducer Loss \nThe RNN transducer (RNNT) loss is part of the RNN transducer pipeline, which is a popular architecture for speech recognition tasks. Recently it has gotten attention for being used in a streaming setting, and has also achieved state-of-the-art WER for the LibriSpeech benchmark.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio\u2019s loss function supports float16 and float32 logits, has autograd and torchscript support, and can be run on both CPU and GPU, which has a custom CUDA kernel implementation for improved performance. The implementation is consistent with the original loss function in [Sequence Transduction with Recurrent Neural Networks](https://arxiv.org/pdf/1211.3711.pdf), but relies on code from [Alignment Restricted Streaming Recurrent Neural Network Transducer](https://arxiv.org/pdf/2011.03072.pdf). Special", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "thanks to Jay Mahadeokar and Ching-Feng Yeh for their code contributions and guidance.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/0.10.0/transforms.html#rnntloss) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Batch support and filter bank support \n`torchaudio.functional.lfilter` now supports batch processing and multiple filters.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Emformer Module\nAutomatic speech recognition (ASR) research and productization have increasingly focused on on-device applications. Towards supporting such efforts, TorchAudio now includes [Emformer](https://arxiv.org/abs/2010.10759), a memory-efficient transformer architecture that has achieved state-of-the-art results on LibriSpeech in low-latency streaming scenarios, as a prototype feature.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/main/prototype.html#emformer) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "GPU Build \nGPU builds that support custom CUDA kernels in TorchAudio, like the one being used for RNN transducer loss, have been added. Following this change, TorchAudio\u2019s binary distribution now includes CPU-only versions and CUDA-enabled versions. To use CUDA-enabled binaries, PyTorch also needs to be compatible with CUDA.\n\n# TorchVision 0.11", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) New Models", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[RegNet](https://arxiv.org/abs/2003.13678) and [EfficientNet](https://arxiv.org/abs/1905.11946) are two popular architectures that can be scaled to different computational budgets. In this release we include 22 pre-trained weights for their classification variants. The models were trained on ImageNet and the accuracies of the pre-trained models obtained on ImageNet val can be found below (see [#4403](https://github.com/pytorch/vision/pull/4403#issuecomment-930381524),", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[#4530](https://github.com/pytorch/vision/pull/4530#issuecomment-933213238) and [#4293](https://github.com/pytorch/vision/pull/4293) for more details).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The models can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models\n\nx = torch.rand(1, 3, 224, 224)\n\nregnet = models.regnet_y_400mf(pretrained=True)\nregnet.eval()\npredictions = regnet(x)\n\nefficientnet = models.efficientnet_b0(pretrained=True)\nefficientnet.eval()\npredictions = efficientnet(x)\n```\nSee the full list of new models on the [torchvision.models](https://pytorch.org/vision/master/models.html) documentation page.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank Ross Wightman and Luke Melas-Kyriazi for contributing the weights of the EfficientNet variants.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) FX-based Feature Extraction \nA new Feature Extraction method has been added to our utilities. It uses [torch.fx](https://pytorch.org/docs/stable/fx.html) and enables us to retrieve the outputs of intermediate layers of a network which is useful for feature extraction and visualization. \n\nHere is an example of how to use the new utility:\n\n```python\nimport torch\nfrom torchvision.models import resnet50\nfrom torchvision.models.feature_extraction import create_feature_extractor", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "x = torch.rand(1, 3, 224, 224)\n\nmodel = resnet50()\n\nreturn_nodes = {\n\"layer4.2.relu_2\": \"layer4\"\n}\nmodel2 = create_feature_extractor(model, return_nodes=return_nodes)\nintermediate_outputs = model2(x)\n\nprint(intermediate_outputs['layer4'].shape)\n```\nWe would like to thank Alexander Soare for developing this utility.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) New Data Augmentations", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Two new Automatic Augmentation techniques were added: [RandAugment](https://arxiv.org/abs/1909.13719) and [Trivial Augment](https://arxiv.org/abs/2103.10158). They apply a series of transformations on the original data to enhance them and to boost the performance of the models. The new techniques build on top of the previously added [AutoAugment](https://github.com/pytorch/vision/pull/3123) and focus on simplifying the approach, reducing the search space for the optimal policy and improving the performance", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "gain in terms of accuracy. These techniques enable users to reproduce recipes to achieve state-of-the-art performance on the offered models. Additionally, it enables users to apply these techniques in order to do transfer learning and achieve optimal accuracy on new datasets.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Both methods can be used as drop-in replacement of the AutoAugment technique as seen below:\n\n```python\nfrom torchvision import transforms\n\nt = transforms.RandAugment()\n# t = transforms.TrivialAugmentWide()\ntransformed = t(image)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "transform = transforms.Compose([\ntransforms.Resize(256),\ntransforms.RandAugment(), # transforms.TrivialAugmentWide()\ntransforms.ToTensor()])\n```\nRead the [automatic augmentation transforms](https://pytorch.org/vision/master/transforms.html#automatic-augmentation-transforms) for more details.\n\nWe would like to thank Samuel G. M\u00fcller for contributing to Trivial Augment and for his help on refactoring the AA package.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Updated Training Recipes\nWe have updated our training reference scripts to add support for Exponential Moving Average, Label Smoothing, Learning-Rate Warmup, [Mixup](https://arxiv.org/abs/1710.09412), [Cutmix](https://arxiv.org/abs/1905.04899) and other [SOTA primitives](https://github.com/pytorch/vision/issues/3911). The above enabled us to improve the classification Acc@1 of some pre-trained models by over 4 points. A major update of the existing pre-trained weights is expected in the next release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "* **TorchX** - a new SDK for quickly building and deploying ML applications from research & development to production. \n* **TorchAudio** - Added text-to-speech pipeline, self-supervised model support, multi-channel support and MVDR beamforming module, RNN transducer (RNNT) loss function, and batch and filterbank support to `lfilter` function. See the TorchAudio release notes [here](https://github.com/pytorch/audio/releases).\n* **TorchVision** - Added new RegNet and EfficientNet models, FX based feature extraction added to utilities, two new Automatic Augmentation techniques: Rand Augment and Trivial Augment, and updated training recipes. See the TorchVision release notes [here](https://github.com/pytorch/vision/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# Introducing TorchX\nTorchX is a new SDK for quickly building and deploying ML applications from research & development to production. It offers various builtin components that encode MLOps best practices and make advanced features like distributed training and hyperparameter optimization accessible to all. \n\nUsers can get started with TorchX 0.1 with no added setup cost since it supports popular ML schedulers and pipeline orchestrators that are already widely adopted and deployed in production. No two production environments are the same. To comply with various use cases, TorchX\u2019s core APIs allow tons of customization at well-defined extension points so that even the most unique applications can be serviced without customizing the whole vertical stack.\n\nRead the [documentation](https://pytorch.org/torchx) for more details and try out this feature using this quickstart [tutorial](https://pytorch.org/torchx/latest/quickstart.html). \n\n\n# TorchAudio 0.10", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# TorchAudio 0.10\n\n### [Beta] Text-to-speech pipeline\nTorchAudio now adds the Tacotron2 model and pretrained weights. It is now possible to build a text-to-speech pipeline with existing vocoder implementations like WaveRNN and Griffin-Lim. Building a TTS pipeline requires matching data processing and pretrained weights, which are often non-trivial to users. So TorchAudio introduces a bundle API so that constructing pipelines for specific pretrained weights is easy. The following example illustrates this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> import torchaudio\n>>>\n>>> bundle = torchaudio.pipelines.TACOTRON2_WAVERNN_CHAR_LJSPEECH\n>>>\n>>> # Build text processor, Tacotron2 and vocoder (WaveRNN) model\n>>> processor = bundle.get_text_processor()\n>>> tacotron2 = bundle.get_tacotron2()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 107M/107M [00:01<00:00, 87.9MB/s]\n>>> vocoder = bundle.get_vocoder()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 16.7M/16.7M [00:00<00:00, 78.1MB/s]\n>>>\n>>> text = \"Hello World!\"\n>>>\n>>> # Encode text\n>>> input, lengths = processor(text)\n>>>\n>>> # Generate (mel-scale) spectrogram\n>>> specgram, lengths, _ = tacotron2.infer(input, lengths)\n>>>\n>>> # Convert spectrogram to waveform\n>>> waveforms, lengths = vocoder(specgram, lengths)\n>>>\n>>> # Save audio\n>>> torchaudio.save('hello-world.wav', waveforms, vocoder.sample_rate)\n\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```\n\nFor the details of this API please refer to [the documentation](https://pytorch.org/audio/0.10.0/pipelines#tacotron2-text-to-speech). You can also try this from [the tutorial](https://pytorch.org/tutorials/intermediate/text_to_speech_with_torchaudio.html).\n\n### (Beta) Self-Supervised Model Support \nTorchAudio added HuBERT model architecture and pre-trained weight support for wav2vec 2.0 and HuBERT. HuBERT and wav2vec 2.0 are novel ways for audio representation learning and they yield high accuracy when fine-tuned on downstream tasks. These models can serve as baseline in future research, therefore, TorchAudio is providing a simple way to run the model. Similar to the TTS pipeline, the pretrained weights and associated information, such as expected sample rates and output class labels (for fine-tuned weights) are put together as a bundle, so that they can be used to build pipelines. The following example illustrates this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> import torchaudio\n>>>\n>>> bundle = torchaudio.pipelines.HUBERT_ASR_LARGE\n>>>\n>>> # Build the model and load pretrained weight.\n>>> model = bundle.get_model()\nDownloading:\n100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 1.18G/1.18G [00:17<00:00, 73.8MB/s]\n>>> # Check the corresponding labels of the output.\n>>> labels = bundle.get_labels()\n>>> print(labels)\n('', '', '', '', '|', 'E', 'T', 'A', 'O', 'N', 'I', 'H', 'S', 'R', 'D', 'L', 'U', 'M', 'W', 'C', 'F', 'G', 'Y', 'P', 'B', 'V', 'K', \"'\", 'X', 'J', 'Q', 'Z')\n>>>\n>>> # Infer the label probability distribution\n>>> waveform, sample_rate = torchaudio.load(hello-world.wav')\n>>>\n>>> emissions, _ = model(waveform)\n>>>\n>>> # Pass emission to (hypothetical) decoder\n>>> transcripts = ctc_decode(emissions, labels)\n>>> print(transcripts[0])\nHELLO WORLD\n\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```\n\nPlease refer to the [documentation](https://pytorch.org/audio/0.10.0/pipelines#wav2vec-2-0-hubert-representation-learning) for more details and try out this feature using this [tutorial](https://pytorch.org/tutorials/intermediate/speech_command_recognition_with_torchaudio_tutorial.html).\n\n### (Beta) Multi-channel support and MVDR beamforming \nFar-field speech recognition is a more challenging task compared to near-field recognition. Multi-channel methods such as beamforming help reduce the noises and enhance the target speech.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "TorchAudio now adds support for differentiable Minimum Variance Distortionless Response (MVDR) beamforming on multi-channel audio using Time-Frequency masks. Researchers can easily assemble it with any multi-channel ASR pipeline. There are three solutions (ref_channel, stv_evd, stv_power) and it supports single-channel and multi-channel (perform average in the method) masks. It provides an online option that recursively updates the parameters for streaming audio. We also provide a tutorial on how to apply MVDR beamforming to the multi-channel audio in the example directory.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> from torchaudio.transforms import MVDR, Spectrogram, InverseSpectrogram\n>>>\n>>> # Load the multi-channel noisy audio\n>>> waveform_mix, sr = torchaudio.load('mix.wav')\n>>> # Initialize the stft and istft modules\n>>> stft = Spectrogram(n_fft=1024, hop_length=256, return_complex=True, power=None)\n>>> istft = InverseSpectrogram(n_fft=1024, hop_length=256)\n>>> # Get the noisy spectrogram\n>>> specgram_mix = stft(waveform_mix)\n>>> # Get the Time-Frequency mask via machine learning models\n>>> mask = model(waveform)\n>>> # Initialize the MVDR module \n>>> mvdr = MVDR(ref_channel=0, solution=\u201dref_channel\u201d, multi_mask=False)\n>>> # Apply MVDR beamforming\n>>> specgram_enhanced = mvdr(specgram_mix, mask)\n>>> # Get the enhanced waveform via iSTFT\n>>> waveform_enhanced = istft(specgram_enhanced, length=waveform.shape[-1])\n```\nPlease refer to the [documentation](https://pytorch.org/audio/0.10.0/transforms.html#mvdr) for more details and try out this feature using the [MVDR tutorial](https://github.com/pytorch/audio/blob/main/examples/beamforming/MVDR_tutorial.ipynb).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) RNN Transducer Loss \nThe RNN transducer (RNNT) loss is part of the RNN transducer pipeline, which is a popular architecture for speech recognition tasks. Recently it has gotten attention for being used in a streaming setting, and has also achieved state-of-the-art WER for the LibriSpeech benchmark.\n\nTorchAudio\u2019s loss function supports float16 and float32 logits, has autograd and torchscript support, and can be run on both CPU and GPU, which has a custom CUDA kernel implementation for improved performance. The implementation is consistent with the original loss function in [Sequence Transduction with Recurrent Neural Networks](https://arxiv.org/pdf/1211.3711.pdf), but relies on code from [Alignment Restricted Streaming Recurrent Neural Network Transducer](https://arxiv.org/pdf/2011.03072.pdf). Special thanks to Jay Mahadeokar and Ching-Feng Yeh for their code contributions and guidance.\n\nPlease refer to the [documentation](https://pytorch.org/audio/0.10.0/transforms.html#rnntloss) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Batch support and filter bank support \n`torchaudio.functional.lfilter` now supports batch processing and multiple filters.\n\n### (Prototype) Emformer Module\nAutomatic speech recognition (ASR) research and productization have increasingly focused on on-device applications. Towards supporting such efforts, TorchAudio now includes [Emformer](https://arxiv.org/abs/2010.10759), a memory-efficient transformer architecture that has achieved state-of-the-art results on LibriSpeech in low-latency streaming scenarios, as a prototype feature.\n\nPlease refer to the [documentation](https://pytorch.org/audio/main/prototype.html#emformer) for more details.\n\n### GPU Build \nGPU builds that support custom CUDA kernels in TorchAudio, like the one being used for RNN transducer loss, have been added. Following this change, TorchAudio\u2019s binary distribution now includes CPU-only versions and CUDA-enabled versions. To use CUDA-enabled binaries, PyTorch also needs to be compatible with CUDA.\n\n# TorchVision 0.11", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# TorchVision 0.11\n\n### (Stable) New Models\n[RegNet](https://arxiv.org/abs/2003.13678) and [EfficientNet](https://arxiv.org/abs/1905.11946) are two popular architectures that can be scaled to different computational budgets. In this release we include 22 pre-trained weights for their classification variants. The models were trained on ImageNet and the accuracies of the pre-trained models obtained on ImageNet val can be found below (see [#4403](https://github.com/pytorch/vision/pull/4403#issuecomment-930381524), [#4530](https://github.com/pytorch/vision/pull/4530#issuecomment-933213238) and [#4293](https://github.com/pytorch/vision/pull/4293) for more details). \n\nThe models can be used as follows:\n\n```python\nimport torch\nfrom torchvision import models\n\nx = torch.rand(1, 3, 224, 224)\n\nregnet = models.regnet_y_400mf(pretrained=True)\nregnet.eval()\npredictions = regnet(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "efficientnet = models.efficientnet_b0(pretrained=True)\nefficientnet.eval()\npredictions = efficientnet(x)\n```\nSee the full list of new models on the [torchvision.models](https://pytorch.org/vision/master/models.html) documentation page.\n\nWe would like to thank Ross Wightman and Luke Melas-Kyriazi for contributing the weights of the EfficientNet variants.\n\n### (Beta) FX-based Feature Extraction \nA new Feature Extraction method has been added to our utilities. It uses [torch.fx](https://pytorch.org/docs/stable/fx.html) and enables us to retrieve the outputs of intermediate layers of a network which is useful for feature extraction and visualization. \n\nHere is an example of how to use the new utility:\n\n```python\nimport torch\nfrom torchvision.models import resnet50\nfrom torchvision.models.feature_extraction import create_feature_extractor\n\n\nx = torch.rand(1, 3, 224, 224)\n\nmodel = resnet50()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "model = resnet50()\n\nreturn_nodes = {\n\"layer4.2.relu_2\": \"layer4\"\n}\nmodel2 = create_feature_extractor(model, return_nodes=return_nodes)\nintermediate_outputs = model2(x)\n\nprint(intermediate_outputs['layer4'].shape)\n```\nWe would like to thank Alexander Soare for developing this utility.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Stable) New Data Augmentations\nTwo new Automatic Augmentation techniques were added: [RandAugment](https://arxiv.org/abs/1909.13719) and [Trivial Augment](https://arxiv.org/abs/2103.10158). They apply a series of transformations on the original data to enhance them and to boost the performance of the models. The new techniques build on top of the previously added [AutoAugment](https://github.com/pytorch/vision/pull/3123) and focus on simplifying the approach, reducing the search space for the optimal policy and improving the performance gain in terms of accuracy. These techniques enable users to reproduce recipes to achieve state-of-the-art performance on the offered models. Additionally, it enables users to apply these techniques in order to do transfer learning and achieve optimal accuracy on new datasets.\n\nBoth methods can be used as drop-in replacement of the AutoAugment technique as seen below:\n\n```python\nfrom torchvision import transforms", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "t = transforms.RandAugment()\n# t = transforms.TrivialAugmentWide()\ntransformed = t(image)\n\ntransform = transforms.Compose([\ntransforms.Resize(256),\ntransforms.RandAugment(), # transforms.TrivialAugmentWide()\ntransforms.ToTensor()])\n```\nRead the [automatic augmentation transforms](https://pytorch.org/vision/master/transforms.html#automatic-augmentation-transforms) for more details.\n\nWe would like to thank Samuel G. M\u00fcller for contributing to Trivial Augment and for his help on refactoring the AA package.\n\n### Updated Training Recipes\nWe have updated our training reference scripts to add support for Exponential Moving Average, Label Smoothing, Learning-Rate Warmup, [Mixup](https://arxiv.org/abs/1710.09412), [Cutmix](https://arxiv.org/abs/1905.04899) and other [SOTA primitives](https://github.com/pytorch/vision/issues/3911). The above enabled us to improve the classification Acc@1 of some pre-trained models by over 4 points. A major update of the existing pre-trained weights is expected in the next release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} {"page_content": "Thanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join [the discussion](https://discuss.pytorch.org/) forums and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch) and [LinkedIn](https://www.linkedin.com/company/pytorch). \n\nCheers!\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.10-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchMultimodal - a library for accelerating exploration in Multimodal AI\"\nauthor: Kartikay Khandelwal, Ankita De\nfeatured-img: \"assets/images/torch-multimodal-feature-image.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "We are announcing TorchMultimodal Beta, a PyTorch domain library for training SoTA multi-task multimodal models at scale. The library provides composable building blocks (modules, transforms, loss functions) to accelerate model development, SoTA model architectures (FLAVA, MDETR, Omnivore) from published research, training and evaluation scripts, as well as notebooks for exploring these models. The library is under active development, and we\u2019d love to hear your feedback! You can find more details on how to", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "get started [here](https://github.com/facebookresearch/multimodal#installation).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "Why TorchMultimodal?", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "Interest is rising around AI models that understand multiple input types (text, images, videos and audio signals), and optionally use this understanding to generate different forms of outputs (sentences, pictures, videos). Recent work from FAIR such as [FLAVA](https://arxiv.org/abs/2112.04482), [Omnivore](https://arxiv.org/pdf/2201.08377.pdf) and [data2vec](https://arxiv.org/abs/2202.03555) have shown that [multimodal models for", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "understanding](https://ai.facebook.com/blog/advances-in-multimodal-understanding-research-at-meta-ai/) are competitive with unimodal counterparts, and in some cases are establishing the new state-of-the art. Generative models such as [Make-a-video](https://ai.facebook.com/blog/generative-ai-text-to-video/) and [Make-a-scene](https://ai.facebook.com/blog/greater-creative-control-for-ai-image-generation/) are redefining what modern AI systems can do.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "As interest in multimodal AI has grown, researchers are looking for tools and libraries to quickly experiment with ideas, and build on top of the latest research in the field. While the PyTorch ecosystem has a rich repository of libraries and frameworks, it\u2019s not always obvious how components from these interoperate with each other, or how they can be stitched together to build SoTA multimodal models.\n\nTorchMultimodal solves this problem by providing:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- **Composable and easy-to-use building blocks** which researchers can use to accelerate model development and experimentation in their own workflows. These are designed to be modular, and can be easily extended to handle new modalities.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- **End-to-end examples for training and evaluating the latest models from research.** These should serve as starting points for ongoing/future research, as well as examples for using advanced features such as integrating with FSDP and activation checkpointing for scaling up model and batch sizes.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "Introducing TorchMultimodal\n\nTorchMultimodal is a PyTorch domain library for training multi-task multimodal models at scale. In the repository, we provide:\n\n- **[Building Blocks](https://github.com/facebookresearch/multimodal/tree/main/torchmultimodal)**. A collection of modular and composable building blocks like models, fusion layers, loss functions, datasets and utilities. Some examples include:", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Contrastive Loss with Temperature](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/modules/losses/contrastive_loss_with_temperature.py#L145). Commonly used function for training models like CLIP and FLAVA. We also include variants such as [ImageTextContrastiveLoss](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/modules/losses/albef.py#L14) used in models like ALBEF.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Codebook layers](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/layers/codebook.py#L31) which compresses high dimensional data by nearest neighbor lookup in an embedding space and is a vital component of VQVAEs (provided as a [model](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/vqvae.py#L26) in the repository).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Shifted-window Attention](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/encoders/swin_transformer_3d_encoder.py#L76) window based multi-head self attention which is a vital component of encoders like Swin 3D Transformers.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Components for CLIP.](https://github.com/facebookresearch/multimodal/tree/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/clip) A popular model published by OpenAI which has proven to be extremely effective at learning text and image representations.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Multimodal GPT.](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/gpt.py) An abstraction that extends OpenAI\u2019s [GPT](https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf) architecture for multimodal generation when combined with the [generation utility](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/utils/generate.py#L33).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [MultiHeadAttention](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/layers/attention.py#L134). A critical component for attention-based models with support for fast auto-regressive decoding.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- **[Examples](https://github.com/facebookresearch/multimodal/tree/main/examples)**. A collection of examples that show how to combine these building blocks with components and common infrastructure (Lightning, TorchMetrics) from across the PyTorch Ecosystem to replicate state-of-the-art models published in literature. We currently provide five examples, which include.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [FLAVA](https://github.com/facebookresearch/multimodal/tree/main/examples/flava) \\[[paper](https://arxiv.org/abs/2112.04482)\\]. Official code for the paper accepted at CVPR, including a tutorial on finetuning FLAVA.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [MDETR](https://github.com/facebookresearch/multimodal/tree/main/examples/mdetr) \\[[paper](https://arxiv.org/abs/2104.12763)\\]. Collaboration with authors from NYU to provide an example which alleviates interoperability pain points in the PyTorch ecosystem, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb) on using MDETR for phrase grounding and visual question answering.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [Omnivore](https://github.com/facebookresearch/multimodal/tree/main/examples/omnivore) \\[[paper](https://arxiv.org/abs/2204.08058)\\]. First example in TorchMultimodal of a model which deals with Video and 3D data, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/omnivore_inference_demo.ipynb) for exploring the model.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [MUGEN](https://github.com/facebookresearch/multimodal/tree/main/examples/mugen) \\[[paper](https://arxiv.org/abs/2204.08058)\\]. Foundational work for auto-regressive [generation](https://colab.research.google.com/drive/1C3ZbH_l19g_KqW3CPeX2-8Q2sOUCpmZo?usp=sharing) and [retrieval](https://colab.research.google.com/drive/1gZfz1jsy79CNCK9t2_r43yt3z7v-w4HS?usp=sharing), including demos for text-video generation and retrieval with a large-scale synthetic dataset enriched from OpenAI", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "[coinrun](https://github.com/openai/coinrun).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "- [ALBEF](https://github.com/facebookresearch/multimodal/tree/main/examples/albef) \\[[paper](https://arxiv.org/abs/2107.07651)\\] Code for the model, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/albef/vqa_with_albef.ipynb) for using this model for Visual Question Answering.\n\nThe following code snippet showcases an example usage of several TorchMultimodal components related to CLIP:\n\n```python\n\n# instantiate clip transform\nclip_transform = CLIPTransform()", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "# pass the transform to your dataset. Here we use coco captions\ndataset = CocoCaptions(root= ..., annFile=..., transforms=clip_transform)\ndataloader = DataLoader(dataset, batch_size=16)\n\n# instantiate model. Here we use clip with vit-L as the image encoder\nmodel= clip_vit_l14()\n\n# define loss and other things needed for training\nclip_loss = ContrastiveLossWithTemperature()\noptim = torch.optim.AdamW(model.parameters(), lr = 1e-5)\nepochs = 1", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "# write your train loop\nfor _ in range(epochs):\n\tfor batch_idx, batch in enumerate(dataloader):\n\t\timage, text = batch\n\t\timage_embeddings, text_embeddings = model(image, text)\n\t\tloss = contrastive_loss_with_temperature(image_embeddings, text_embeddings)\n\t\tloss.backward()\n\t\toptimizer.step()", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "Apart from the code, we are also **releasing a tutorial for fine-tuning multimodal foundation models, and a blog post (with code pointers) on how to scale up such models using techniques from PyTorch Distributed (FSDP and activation checkpointing)**. We hope such examples and tutorials will serve to demystify a number of advanced features available in the PyTorch ecosystem.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "What\u2019s Next?\n\nWhile this is an exciting launch, there\u2019s a lot more to come. The library is under development and we are working on adding some of the exciting developments in the space of diffusion models, and examples to showcase common trends from research. As you explore and use the library, we\u2019d love to hear any feedback you might have! You can find more details on how to get started [here](https://github.com/facebookresearch/multimodal#installation).", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "Team\n\nThe primary contributors and developers of TorchMultimodal include Ankita De, Evan Smothers, Kartikay Khandelwal, Lan Gong, Laurence Rouesnel, Nahiyan Malik, Rafi Ayub and Yosua Michael Maranatha.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the Winners of the 2021 PyTorch Annual Hackathon'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/social_hackathon21.png'\n---\n\nMore than 1,900 people worked hard in this year\u2019s PyTorch Annual Hackathon to create unique tools and applications for PyTorch developers and researchers.\n\n*Notice: None of the projects submitted to the hackathon are associated with or offered by Meta Platforms, Inc.*", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "This year, participants could enter their projects into following three categories:\n* **PyTorch Developer Tools**: a tool or library for improving productivity and efficiency for PyTorch researchers and developers.\n* **Web and Mobile Applications Powered by PyTorch**: a web or mobile interface and/or an embedded device built using PyTorch.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "* **PyTorch Responsible AI Development Tools**: a tool, library, or web/mobile app to support researchers and developers in creating responsible AI that factors in fairness, security, privacy, and more throughout its entire development process.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "The virtual hackathon ran from September 8 through November 2, 2021, with more than 1,900 registered participants from 110 countries, submitting a total of 65 projects. Entrants were judged on their idea\u2019s quality, originality, potential impact, and how well they implemented it. All projects can be viewed [here](https://pytorch2021.devpost.com/project-gallery).\n\nMeet the winners of each category below!", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "PYTORCH DEVELOPER TOOLS\n\n#### **First Place: [RaNNC](https://devpost.com/software/rannc-rapid-neural-network-connector)**\nRaNNC is a middleware to automate hybrid model/data parallelism for training very large-scale neural networks capable of training 100 billion parameter models without any manual tuning.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Second Place: [XiTorch](https://devpost.com/software/xitorch-differentiable-scientific-computing-library)**\nXiTorch provides first and higher order gradients of functional routines, such as optimization, rootfinder, and ODE solver. It also contains operations for implicit linear operators (e.g. large matrix that is expressed only by its matrix-vector multiplication) such as symmetric eigen-decomposition, linear solve, and singular value decomposition.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Third Place: [TorchLiberator](https://devpost.com/software/torchliberator-partial-weight-loading)**\nTorchLiberator automates model surgery, finding the maximum correspondence between weights in two networks.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Honorable Mentions**\n* [PADL](https://devpost.com/software/doch) manages your entire PyTorch work flow with a single python abstraction and a beautiful functional API, so there\u2019s no more complex configuration or juggling preprocessing, postprocessing and forward passes.\n* [PyTree](https://devpost.com/software/pytree) is a PyTorch package for recursive neural networks that provides highly generic recursive neural network implementations as well as efficient batching methods.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "* [IndicLP](https://devpost.com/software/indiclp) makes it easier for developers and researchers to build applications and models in Indian Languages, thus making NLP a more diverse field.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "WEB/MOBILE APPLICATIONS POWERED BY PYTORCH\n\n#### **First Place: [PyTorch Driving Guardian](https://devpost.com/software/pytorch-driving-guardian)**\nPyTorch Driving Guardian is a tool that monitors driver alertness, emotional state, and potential blind spots on the road. \n\n#### **Second Place: [Kronia](https://devpost.com/software/kronia)**\nKronia is an Android mobile app built to maximize the harvest outputs for farmers.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Third Place: [Heyoh camera for Mac](https://devpost.com/software/heyoh-camera)**\nHeyoh is a Mac virtual camera for Zoom and Meets that augments live video by recognizing hand gestures and smiles and shows animated effects to other video participants.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Honorable Mentions**\n* [Mamma AI](https://devpost.com/software/mamma-ai) is a tool that helps doctors with the breast cancer identification process by identifying areas likely to have cancer using ultrasonic and x-ray images. \n* [AgingClock](https://devpost.com/software/agingclock) is a tool that predicts biological age first with methylation genome data, then blood test data and eventually with multimodal omics and lifestyle data.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "* [Iris](https://devpost.com/software/iris-7s3yna) is an open source photos platform which is more of an alternative of Google Photos that includes features such as Listing photos, Detecting Categories, Detecting and Classifying Faces from Photos, Detecting and Clustering by Location and Things in Photos.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "PYTORCH RESPONSIBLE AI DEVELOPMENT TOOLS\n\n#### **First Place: [FairWell](https://devpost.com/software/fairwell-a-tool-to-bid-goodbye-to-unknown-ai-biasness)**\nFairWell aims to address model bias on specific groups of people by allowing data scientists to evaluate their dataset and model predictions and take steps to make their datasets more inclusive and their models less biased.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Second Place: [promp2slip](https://devpost.com/software/promp2slip)**\nPromp2slip is a library that tests the ethics of language models by using natural adversarial texts.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Third Place: [Phorch](https://devpost.com/software/phorch)**\nPhorch adversarially attacks the data using FIGA (Feature Importance Guided Attack) and creates 3 different attack sets of data based on certain parameters. These features are utilized to implement adversarial training as a defense against FIGA using neural net architecture in PyTorch.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} -{"page_content": "**Honorable Mentions**\n* [Greenops](https://devpost.com/software/greenops) helps to measure the footprints of deep learning models at training, testing and evaluating to reduce energy consumption and carbon footprints.\n* [Xaitk-saliency](https://devpost.com/software/xaitk-saliency) is an open-source, explainable AI toolkit for visual saliency algorithm interfaces and implementations, built for analytic and autonomy applications.\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Introducing TorchMultimodal - a library for accelerating exploration in Multimodal AI\"\nauthor: Kartikay Khandelwal, Ankita De\nfeatured-img: \"assets/images/torch-multimodal-feature-image.png\"\n---\n\nWe are announcing TorchMultimodal Beta, a PyTorch domain library for training SoTA multi-task multimodal models at scale. The library provides composable building blocks (modules, transforms, loss functions) to accelerate model development, SoTA model architectures (FLAVA, MDETR, Omnivore) from published research, training and evaluation scripts, as well as notebooks for exploring these models. The library is under active development, and we\u2019d love to hear your feedback! You can find more details on how to get started [here](https://github.com/facebookresearch/multimodal#installation).\n\n## Why TorchMultimodal?", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "Interest is rising around AI models that understand multiple input types (text, images, videos and audio signals), and optionally use this understanding to generate different forms of outputs (sentences, pictures, videos). Recent work from FAIR such as [FLAVA](https://arxiv.org/abs/2112.04482), [Omnivore](https://arxiv.org/pdf/2201.08377.pdf) and [data2vec](https://arxiv.org/abs/2202.03555) have shown that [multimodal models for understanding](https://ai.facebook.com/blog/advances-in-multimodal-understanding-research-at-meta-ai/) are competitive with unimodal counterparts, and in some cases are establishing the new state-of-the art. Generative models such as [Make-a-video](https://ai.facebook.com/blog/generative-ai-text-to-video/) and [Make-a-scene](https://ai.facebook.com/blog/greater-creative-control-for-ai-image-generation/) are redefining what modern AI systems can do.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "As interest in multimodal AI has grown, researchers are looking for tools and libraries to quickly experiment with ideas, and build on top of the latest research in the field. While the PyTorch ecosystem has a rich repository of libraries and frameworks, it\u2019s not always obvious how components from these interoperate with each other, or how they can be stitched together to build SoTA multimodal models.\n\nTorchMultimodal solves this problem by providing:\n\n- **Composable and easy-to-use building blocks** which researchers can use to accelerate model development and experimentation in their own workflows. These are designed to be modular, and can be easily extended to handle new modalities.\n\n- **End-to-end examples for training and evaluating the latest models from research.** These should serve as starting points for ongoing/future research, as well as examples for using advanced features such as integrating with FSDP and activation checkpointing for scaling up model and batch sizes.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "## Introducing TorchMultimodal\n\nTorchMultimodal is a PyTorch domain library for training multi-task multimodal models at scale. In the repository, we provide:\n\n- **[Building Blocks](https://github.com/facebookresearch/multimodal/tree/main/torchmultimodal)**. A collection of modular and composable building blocks like models, fusion layers, loss functions, datasets and utilities. Some examples include:\n\n - [Contrastive Loss with Temperature](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/modules/losses/contrastive_loss_with_temperature.py#L145). Commonly used function for training models like CLIP and FLAVA. We also include variants such as [ImageTextContrastiveLoss](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/modules/losses/albef.py#L14) used in models like ALBEF.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "- [Codebook layers](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/layers/codebook.py#L31) which compresses high dimensional data by nearest neighbor lookup in an embedding space and is a vital component of VQVAEs (provided as a [model](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/vqvae.py#L26) in the repository).\n\n - [Shifted-window Attention](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/encoders/swin_transformer_3d_encoder.py#L76) window based multi-head self attention which is a vital component of encoders like Swin 3D Transformers.\n\n - [Components for CLIP.](https://github.com/facebookresearch/multimodal/tree/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/clip) A popular model published by OpenAI which has proven to be extremely effective at learning text and image representations.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "- [Multimodal GPT.](https://github.com/facebookresearch/multimodal/blob/4d2236877467ff8f56aa1935dd92d7782751b135/torchmultimodal/models/gpt.py) An abstraction that extends OpenAI\u2019s [GPT](https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf) architecture for multimodal generation when combined with the [generation utility](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/utils/generate.py#L33).\n\n - [MultiHeadAttention](https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/layers/attention.py#L134). A critical component for attention-based models with support for fast auto-regressive decoding.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "- **[Examples](https://github.com/facebookresearch/multimodal/tree/main/examples)**. A collection of examples that show how to combine these building blocks with components and common infrastructure (Lightning, TorchMetrics) from across the PyTorch Ecosystem to replicate state-of-the-art models published in literature. We currently provide five examples, which include.\n\n - [FLAVA](https://github.com/facebookresearch/multimodal/tree/main/examples/flava) \\[[paper](https://arxiv.org/abs/2112.04482)\\]. Official code for the paper accepted at CVPR, including a tutorial on finetuning FLAVA.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "- [MDETR](https://github.com/facebookresearch/multimodal/tree/main/examples/mdetr) \\[[paper](https://arxiv.org/abs/2104.12763)\\]. Collaboration with authors from NYU to provide an example which alleviates interoperability pain points in the PyTorch ecosystem, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb) on using MDETR for phrase grounding and visual question answering.\n\n - [Omnivore](https://github.com/facebookresearch/multimodal/tree/main/examples/omnivore) \\[[paper](https://arxiv.org/abs/2204.08058)\\]. First example in TorchMultimodal of a model which deals with Video and 3D data, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/omnivore_inference_demo.ipynb) for exploring the model.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "- [MUGEN](https://github.com/facebookresearch/multimodal/tree/main/examples/mugen) \\[[paper](https://arxiv.org/abs/2204.08058)\\]. Foundational work for auto-regressive [generation](https://colab.research.google.com/drive/1C3ZbH_l19g_KqW3CPeX2-8Q2sOUCpmZo?usp=sharing) and [retrieval](https://colab.research.google.com/drive/1gZfz1jsy79CNCK9t2_r43yt3z7v-w4HS?usp=sharing), including demos for text-video generation and retrieval with a large-scale synthetic dataset enriched from OpenAI [coinrun](https://github.com/openai/coinrun).\n\n - [ALBEF](https://github.com/facebookresearch/multimodal/tree/main/examples/albef) \\[[paper](https://arxiv.org/abs/2107.07651)\\] Code for the model, including a [notebook](https://github.com/facebookresearch/multimodal/blob/main/examples/albef/vqa_with_albef.ipynb) for using this model for Visual Question Answering.\n\nThe following code snippet showcases an example usage of several TorchMultimodal components related to CLIP:\n\n```python", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "```python\n\n# instantiate clip transform\nclip_transform = CLIPTransform()\n\n# pass the transform to your dataset. Here we use coco captions\ndataset = CocoCaptions(root= ..., annFile=..., transforms=clip_transform)\ndataloader = DataLoader(dataset, batch_size=16)\n\n# instantiate model. Here we use clip with vit-L as the image encoder\nmodel= clip_vit_l14()\n\n# define loss and other things needed for training\nclip_loss = ContrastiveLossWithTemperature()\noptim = torch.optim.AdamW(model.parameters(), lr = 1e-5)\nepochs = 1\n\n# write your train loop\nfor _ in range(epochs):\n\tfor batch_idx, batch in enumerate(dataloader):\n\t\timage, text = batch\n\t\timage_embeddings, text_embeddings = model(image, text)\n\t\tloss = contrastive_loss_with_temperature(image_embeddings, text_embeddings)\n\t\tloss.backward()\n\t\toptimizer.step()\n```", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "Apart from the code, we are also **releasing a tutorial for fine-tuning multimodal foundation models, and a blog post (with code pointers) on how to scale up such models using techniques from PyTorch Distributed (FSDP and activation checkpointing)**. We hope such examples and tutorials will serve to demystify a number of advanced features available in the PyTorch ecosystem.\n\n## What\u2019s Next?\n\nWhile this is an exciting launch, there\u2019s a lot more to come. The library is under development and we are working on adding some of the exciting developments in the space of diffusion models, and examples to showcase common trends from research. As you explore and use the library, we\u2019d love to hear any feedback you might have! You can find more details on how to get started [here](https://github.com/facebookresearch/multimodal#installation).\n\n## Team", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "## Team\n\nThe primary contributors and developers of TorchMultimodal include Ankita De, Evan Smothers, Kartikay Khandelwal, Lan Gong, Laurence Rouesnel, Nahiyan Malik, Rafi Ayub and Yosua Michael Maranatha.", "metadata": {"source": "https://pytorch.org/blog/introducing-torchmultimodal/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the Winners of the 2021 PyTorch Annual Hackathon'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/social_hackathon21.png'\n---\n\nMore than 1,900 people worked hard in this year\u2019s PyTorch Annual Hackathon to create unique tools and applications for PyTorch developers and researchers.\n\n*Notice: None of the projects submitted to the hackathon are associated with or offered by Meta Platforms, Inc.*\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "This year, participants could enter their projects into following three categories:\n* **PyTorch Developer Tools**: a tool or library for improving productivity and efficiency for PyTorch researchers and developers.\n* **Web and Mobile Applications Powered by PyTorch**: a web or mobile interface and/or an embedded device built using PyTorch.\n* **PyTorch Responsible AI Development Tools**: a tool, library, or web/mobile app to support researchers and developers in creating responsible AI that factors in fairness, security, privacy, and more throughout its entire development process.\n\nThe virtual hackathon ran from September 8 through November 2, 2021, with more than 1,900 registered participants from 110 countries, submitting a total of 65 projects. Entrants were judged on their idea\u2019s quality, originality, potential impact, and how well they implemented it. All projects can be viewed [here](https://pytorch2021.devpost.com/project-gallery).\n\nMeet the winners of each category below!\n\n## PYTORCH DEVELOPER TOOLS", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **First Place: [RaNNC](https://devpost.com/software/rannc-rapid-neural-network-connector)**\nRaNNC is a middleware to automate hybrid model/data parallelism for training very large-scale neural networks capable of training 100 billion parameter models without any manual tuning.\n\n#### **Second Place: [XiTorch](https://devpost.com/software/xitorch-differentiable-scientific-computing-library)**\nXiTorch provides first and higher order gradients of functional routines, such as optimization, rootfinder, and ODE solver. It also contains operations for implicit linear operators (e.g. large matrix that is expressed only by its matrix-vector multiplication) such as symmetric eigen-decomposition, linear solve, and singular value decomposition.\n\n#### **Third Place: [TorchLiberator](https://devpost.com/software/torchliberator-partial-weight-loading)**\nTorchLiberator automates model surgery, finding the maximum correspondence between weights in two networks.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **Honorable Mentions**\n* [PADL](https://devpost.com/software/doch) manages your entire PyTorch work flow with a single python abstraction and a beautiful functional API, so there\u2019s no more complex configuration or juggling preprocessing, postprocessing and forward passes.\n* [PyTree](https://devpost.com/software/pytree) is a PyTorch package for recursive neural networks that provides highly generic recursive neural network implementations as well as efficient batching methods. \n* [IndicLP](https://devpost.com/software/indiclp) makes it easier for developers and researchers to build applications and models in Indian Languages, thus making NLP a more diverse field. \n\n## WEB/MOBILE APPLICATIONS POWERED BY PYTORCH\n\n#### **First Place: [PyTorch Driving Guardian](https://devpost.com/software/pytorch-driving-guardian)**\nPyTorch Driving Guardian is a tool that monitors driver alertness, emotional state, and potential blind spots on the road.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **Second Place: [Kronia](https://devpost.com/software/kronia)**\nKronia is an Android mobile app built to maximize the harvest outputs for farmers. \n\n#### **Third Place: [Heyoh camera for Mac](https://devpost.com/software/heyoh-camera)**\nHeyoh is a Mac virtual camera for Zoom and Meets that augments live video by recognizing hand gestures and smiles and shows animated effects to other video participants.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **Honorable Mentions**\n* [Mamma AI](https://devpost.com/software/mamma-ai) is a tool that helps doctors with the breast cancer identification process by identifying areas likely to have cancer using ultrasonic and x-ray images. \n* [AgingClock](https://devpost.com/software/agingclock) is a tool that predicts biological age first with methylation genome data, then blood test data and eventually with multimodal omics and lifestyle data.\n* [Iris](https://devpost.com/software/iris-7s3yna) is an open source photos platform which is more of an alternative of Google Photos that includes features such as Listing photos, Detecting Categories, Detecting and Classifying Faces from Photos, Detecting and Clustering by Location and Things in Photos.\n\n## PYTORCH RESPONSIBLE AI DEVELOPMENT TOOLS", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **First Place: [FairWell](https://devpost.com/software/fairwell-a-tool-to-bid-goodbye-to-unknown-ai-biasness)**\nFairWell aims to address model bias on specific groups of people by allowing data scientists to evaluate their dataset and model predictions and take steps to make their datasets more inclusive and their models less biased. \n\n#### **Second Place: [promp2slip](https://devpost.com/software/promp2slip)**\nPromp2slip is a library that tests the ethics of language models by using natural adversarial texts. \n\n#### **Third Place: [Phorch](https://devpost.com/software/phorch)**\nPhorch adversarially attacks the data using FIGA (Feature Importance Guided Attack) and creates 3 different attack sets of data based on certain parameters. These features are utilized to implement adversarial training as a defense against FIGA using neural net architecture in PyTorch.", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} +{"page_content": "#### **Honorable Mentions**\n* [Greenops](https://devpost.com/software/greenops) helps to measure the footprints of deep learning models at training, testing and evaluating to reduce energy consumption and carbon footprints.\n* [Xaitk-saliency](https://devpost.com/software/xaitk-saliency) is an open-source, explainable AI toolkit for visual saliency algorithm interfaces and implementations, built for analytic and autonomy applications.\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/announcing-the-winners-of-the-2021-pytorch-annual-hackathon/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 1.8 Release, including Compiler and Distributed Training updates, and New Mobile Tutorials'\nauthor: Team PyTorch \n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the availability of PyTorch 1.8. This release is composed of more than 3,000 commits since 1.7. It includes major updates and new features for compilation, code optimization, frontend APIs for scientific computing, and AMD ROCm support through binaries that are available via pytorch.org. It also provides improved features for large-scale training for pipeline and model parallelism, and gradient compression. A few of the highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "1. Support for doing python to python functional transformations via ```torch.fx```;\n2. Added or stabilized APIs to support FFTs (```torch.fft```), Linear Algebra functions (```torch.linalg```), added support for autograd for complex tensors and updates to improve performance for calculating hessians and jacobians; and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "3. Significant updates and improvements to distributed training including: Improved NCCL reliability; Pipeline parallelism support; RPC profiling; and support for communication hooks adding gradient compression.\nSee the full release notes [here](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Along with 1.8, we are also releasing major updates to PyTorch libraries including [TorchCSPRNG](https://github.com/pytorch/csprng), [TorchVision](https://github.com/pytorch/vision), [TorchText](https://github.com/pytorch/text) and [TorchAudio](https://github.com/pytorch/audio). For more on the library releases, see the post [here](http://pytorch.org/blog/pytorch-1.8-new-library-releases). As previously noted, features in PyTorch releases are classified as Stable, Beta and Prototype. You can learn more", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "about the definitions in the post [here](https://pytorch.org/blog/pytorch-feature-classification-changes/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "# New and Updated APIs\nThe PyTorch 1.8 release brings a host of new and updated API surfaces ranging from additional APIs for NumPy compatibility, also support for ways to improve and scale your code for performance at both inference and training time. Here is a brief summary of the major features coming in this release:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Stable] ```Torch.fft``` support for high performance NumPy style FFTs\nAs part of PyTorch\u2019s goal to support scientific computing, we have invested in improving our FFT support and with PyTorch 1.8, we are releasing the ```torch.fft``` module. This module implements the same functions as NumPy\u2019s ```np.fft``` module, but with support for hardware acceleration and autograd.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* See this [blog post](https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/) for more details\n* [Documentation](https://pytorch.org/docs/1.8.0/fft.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Support for NumPy style linear algebra functions via ```torch.linalg```\nThe ```torch.linalg``` module, modeled after NumPy\u2019s [np.linalg](https://numpy.org/doc/stable/reference/routines.linalg.html?highlight=linalg#module-numpy.linalg) module, brings NumPy-style support for common linear algebra operations including Cholesky decompositions, determinants, eigenvalues and many others.\n* [Documentation](https://pytorch.org/docs/1.8.0/linalg.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Python code Transformations with FX\nFX allows you to write transformations of the form ```transform(input_module : nn.Module)``` -> ```nn.Module```, where you can feed in a ```Module``` instance and get a transformed ```Module``` instance out of it.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "This kind of functionality is applicable in many scenarios. For example, the FX-based Graph Mode Quantization product is releasing as a prototype contemporaneously with FX. Graph Mode Quantization automates the process of quantizing a neural net and does so by leveraging FX\u2019s program capture, analysis and transformation facilities. We are also developing many other transformation products with FX and we are excited to share this powerful toolkit with the community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Because FX transforms consume and produce nn.Module instances, they can be used within many existing PyTorch workflows. This includes workflows that, for example, train in Python then deploy via TorchScript.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "You can read more about FX in the official [documentation](https://pytorch.org/docs/master/fx.html). You can also find several examples of program transformations implemented using ```torch.fx``` [here](https://github.com/pytorch/examples/tree/master/fx). We are constantly improving FX and invite you to share any feedback you have about the toolkit on the [forums](https://discuss.pytorch.org/) or [issue tracker](https://github.com/pytorch/pytorch/issues).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "We\u2019d like to acknowledge [TorchScript](https://pytorch.org/docs/stable/jit.html) tracing, [Apache MXNet](https://mxnet.apache.org/versions/1.7.0/) hybridize, and more recently [JAX](https://github.com/google/jax) as influences for program acquisition via tracing. We\u2019d also like to acknowledge [Caffe2](https://caffe2.ai/), [JAX](https://github.com/google/jax), and [TensorFlow](https://www.tensorflow.org/) as inspiration for the value of simple, directed dataflow graph program representations and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "transformations over those representations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "# Distributed Training", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch 1.8 release added a number of new features as well as improvements to reliability and usability. Concretely, support for: [Stable level async error/timeout handling](https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group) was added to improve NCCL reliability; and stable support for [RPC based profiling](https://pytorch.org/docs/stable/rpc.html). Additionally, we have added support for pipeline parallelism as well as gradient", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "compression through the use of communication hooks in DDP. Details are below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Pipeline Parallelism\nAs machine learning models continue to grow in size, traditional Distributed DataParallel (DDP) training no longer scales as these models don\u2019t fit on a single GPU device. The new pipeline parallelism feature provides an easy to use PyTorch API to leverage pipeline parallelism as part of your training loop.\n* [RFC](https://github.com/pytorch/pytorch/issues/44827)\n* [Documentation](https://pytorch.org/docs/1.8.0/pipeline.html?highlight=pipeline#)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] DDP Communication Hook\nThe DDP communication hook is a generic interface to control how to communicate gradients across workers by overriding the vanilla allreduce in DistributedDataParallel. A few built-in communication hooks are provided including PowerSGD, and users can easily apply any of these hooks to optimize communication. Additionally, the communication hook interface can also support user-defined communication strategies for more advanced use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* [RFC](https://github.com/pytorch/pytorch/issues/39272)\n* [Documentation](https://pytorch.org/docs/1.8.0/ddp_comm_hooks.html?highlight=powersgd)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Additional Prototype Features for Distributed Training\nIn addition to the major stable and beta distributed training features in this release, we also have a number of prototype features available in our nightlies to try out and provide feedback. We have linked in the draft docs below for reference:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Prototype) ZeroRedundancyOptimizer** - Based on and in partnership with the Microsoft DeepSpeed team, this feature helps reduce per-process memory footprint by sharding optimizer states across all participating processes in the ```ProcessGroup``` gang. Refer to this [documentation](https://pytorch.org/docs/master/distributed.optim.html#torch.distributed.optim.ZeroRedundancyOptimizer) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Prototype) Process Group NCCL Send/Recv** - The NCCL send/recv API was introduced in v2.7 and this feature adds support for it in NCCL process groups. This feature will provide an option for users to implement collective operations at Python layer instead of C++ layer. Refer to this [documentation](https://pytorch.org/docs/master/distributed.html#distributed-communication-package-torch-distributed) and [code", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "examples](https://github.com/pytorch/pytorch/blob/master/torch/distributed/distributed_c10d.py#L899) to learn more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Prototype) CUDA-support in RPC using TensorPipe** - This feature should bring consequent speed improvements for users of PyTorch RPC with multiple-GPU machines, as TensorPipe will automatically leverage NVLink when available, and avoid costly copies to and from host memory when exchanging GPU tensors between processes. When not on the same machine, TensorPipe will fall back to copying the tensor to host memory and sending it as a regular CPU tensor. This will also improve the user experience as users", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "will be able to treat GPU tensors like regular CPU tensors in their code. Refer to this [documentation](https://pytorch.org/docs/1.8.0/rpc.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* **(Prototype) Remote Module** - This feature allows users to operate a module on a remote worker like using a local module, where the RPCs are transparent to the user. In the past, this functionality was implemented in an ad-hoc way and overall this feature will improve the usability of model parallelism on PyTorch. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "# PyTorch Mobile\nSupport for PyTorch Mobile is expanding with a new set of tutorials to help new users launch models on-device quicker and give existing users a tool to get more out of our framework. These include:\n* [Image segmentation DeepLabV3 on iOS](https://pytorch.org/tutorials/beginner/deeplabv3_on_ios.html)\n* [Image segmentation DeepLabV3 on Android](https://pytorch.org/tutorials/beginner/deeplabv3_on_android.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Our new demo apps also include examples of image segmentation, object detection, neural machine translation, question answering, and vision transformers. They are available on both iOS and Android:\n* [iOS demo app](https://github.com/pytorch/ios-demo-app)\n* [Android demo app](https://github.com/pytorch/android-demo-app)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "In addition to performance improvements on CPU for MobileNetV3 and other models, we also revamped our Android GPU backend prototype for broader models coverage and faster inferencing:\n* [Android tutorial](https://pytorch.org/tutorials/prototype/vulkan_workflow.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Lastly, we are launching the PyTorch Mobile Lite Interpreter as a prototype feature in this release. The Lite Interpreter allows users to reduce the runtime binary size. Please try these out and send us your feedback on the [PyTorch Forums](https://discuss.pytorch.org/c/mobile/). All our latest updates can be found on the [PyTorch Mobile page](https://pytorch.org/mobile/home/)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] PyTorch Mobile Lite Interpreter\nPyTorch Lite Interpreter is a streamlined version of the PyTorch runtime that can execute PyTorch programs in resource constrained devices, with reduced binary size footprint. This prototype feature reduces binary sizes by up to 70% compared to the current on-device runtime in the current release. \n* [iOS/Android Tutorial](https://pytorch.org/tutorials/prototype/lite_interpreter.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "# Performance Optimization\nIn 1.8, we are releasing the support for benchmark utils to enable users to better monitor performance. We are also opening up a new automated quantization API. See the details below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Benchmark utils\nBenchmark utils allows users to take accurate performance measurements, and provides composable tools to help with both benchmark formulation and post processing. This expected to be helpful for contributors to PyTorch to quickly understand how their contributions are impacting PyTorch performance.\n\nExample:\n```python\nfrom torch.utils.benchmark import Timer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "results = []\nfor num_threads in [1, 2, 4]:\n timer = Timer(\n stmt=\"torch.add(x, y, out=out)\",\n setup=\"\"\"\n n = 1024\n x = torch.ones((n, n))\n y = torch.ones((n, 1))\n out = torch.empty((n, n))\n \"\"\",\n num_threads=num_threads,\n )\n results.append(timer.blocked_autorange(min_run_time=5))\n print(\n f\"{num_threads} thread{'s' if num_threads > 1 else ' ':<4}\"\n f\"{results[-1].median * 1e6:>4.0f} us \" +", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "(f\"({results[0].median / results[-1].median:.1f}x)\" if num_threads > 1 else '')\n )", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "1 thread 376 us \n2 threads 189 us (2.0x)\n4 threads 99 us (3.8x)\n```\n* [Documentation](https://pytorch.org/docs/1.8.0/benchmark_utils.html?highlight=benchmark#)\n* [Tutorial](https://pytorch.org/tutorials/recipes/recipes/benchmark.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) FX Graph Mode Quantization\n FX Graph Mode Quantization is the new automated quantization API in PyTorch. It improves upon Eager Mode Quantization by adding support for functionals and automating the quantization process, although people might need to refactor the model to make the model compatible with FX Graph Mode Quantization (symbolically traceable with ```torch.fx```).\n* [Documentation](https://pytorch.org/docs/master/quantization.html#prototype-fx-graph-mode-quantization)\n* Tutorials:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "* [(Prototype) FX Graph Mode Post Training Dynamic Quantization](https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_dynamic.html)\n * [(Prototype) FX Graph Mode Post Training Static Qunatization](https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_static.html)\n * [(Prototype) FX Graph Mode Quantization User Guide](https://pytorch.org/tutorials/prototype/fx_graph_mode_quant_guide.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "# Hardware Support", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Ability to Extend the PyTorch Dispatcher for a new backend in C++\nIn PyTorch 1.8, you can now create new out-of-tree devices that live outside the ```pytorch/pytorch``` repo. The tutorial linked below shows how to register your device and keep it in sync with native PyTorch devices.\n* [Tutorial](https://pytorch.org/tutorials/advanced/extend_dispatcher.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "[Beta] AMD GPU Binaries Now Available\nStarting in PyTorch 1.8, we have added support for ROCm wheels providing an easy onboarding to using AMD GPUs. You can simply go to the standard [PyTorch installation selector](https://pytorch.org/get-started/locally/) and choose ROCm as an installation option and execute the provided command.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading, and if you are excited about these updates and want to participate in the future of PyTorch, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues).\n\nCheers!\n\n***Team PyTorch***", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Efficient PyTorch I/O library for Large Datasets, Many Files, Many GPUs'\nauthor: Alex Aizman, Gavin Maltby, Thomas Breuel\n---\n\nData sets are growing bigger every day and GPUs are getting faster. This means there are more data sets for deep learning researchers and engineers to train and validate their models.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* Many datasets for research in still image recognition are becoming available with 10 million or more images, including OpenImages and Places.\n* million YouTube videos [(YouTube 8M)](https://research.google.com/youtube8m/) consume about 300 TB in 720p, used for research in object recognition, video analytics, and action recognition.\n* The Tobacco Corpus consists of about 20 million scanned HD pages, useful for OCR and text analytics research.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "We are excited to announce the availability of PyTorch 1.8. This release is composed of more than 3,000 commits since 1.7. It includes major updates and new features for compilation, code optimization, frontend APIs for scientific computing, and AMD ROCm support through binaries that are available via pytorch.org. It also provides improved features for large-scale training for pipeline and model parallelism, and gradient compression. A few of the highlights include:\n1. Support for doing python to python functional transformations via ```torch.fx```;\n2. Added or stabilized APIs to support FFTs (```torch.fft```), Linear Algebra functions (```torch.linalg```), added support for autograd for complex tensors and updates to improve performance for calculating hessians and jacobians; and\n3. Significant updates and improvements to distributed training including: Improved NCCL reliability; Pipeline parallelism support; RPC profiling; and support for communication hooks adding gradient compression.\nSee the full release notes [here](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "Along with 1.8, we are also releasing major updates to PyTorch libraries including [TorchCSPRNG](https://github.com/pytorch/csprng), [TorchVision](https://github.com/pytorch/vision), [TorchText](https://github.com/pytorch/text) and [TorchAudio](https://github.com/pytorch/audio). For more on the library releases, see the post [here](http://pytorch.org/blog/pytorch-1.8-new-library-releases). As previously noted, features in PyTorch releases are classified as Stable, Beta and Prototype. You can learn more about the definitions in the post [here](https://pytorch.org/blog/pytorch-feature-classification-changes/). \n\n# New and Updated APIs\nThe PyTorch 1.8 release brings a host of new and updated API surfaces ranging from additional APIs for NumPy compatibility, also support for ways to improve and scale your code for performance at both inference and training time. Here is a brief summary of the major features coming in this release:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### [Stable] ```Torch.fft``` support for high performance NumPy style FFTs\nAs part of PyTorch\u2019s goal to support scientific computing, we have invested in improving our FFT support and with PyTorch 1.8, we are releasing the ```torch.fft``` module. This module implements the same functions as NumPy\u2019s ```np.fft``` module, but with support for hardware acceleration and autograd.\n* See this [blog post](https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/) for more details\n* [Documentation](https://pytorch.org/docs/1.8.0/fft.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### [Beta] Support for NumPy style linear algebra functions via ```torch.linalg```\nThe ```torch.linalg``` module, modeled after NumPy\u2019s [np.linalg](https://numpy.org/doc/stable/reference/routines.linalg.html?highlight=linalg#module-numpy.linalg) module, brings NumPy-style support for common linear algebra operations including Cholesky decompositions, determinants, eigenvalues and many others.\n* [Documentation](https://pytorch.org/docs/1.8.0/linalg.html)\n\n## [Beta] Python code Transformations with FX\nFX allows you to write transformations of the form ```transform(input_module : nn.Module)``` -> ```nn.Module```, where you can feed in a ```Module``` instance and get a transformed ```Module``` instance out of it.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "This kind of functionality is applicable in many scenarios. For example, the FX-based Graph Mode Quantization product is releasing as a prototype contemporaneously with FX. Graph Mode Quantization automates the process of quantizing a neural net and does so by leveraging FX\u2019s program capture, analysis and transformation facilities. We are also developing many other transformation products with FX and we are excited to share this powerful toolkit with the community.\n\nBecause FX transforms consume and produce nn.Module instances, they can be used within many existing PyTorch workflows. This includes workflows that, for example, train in Python then deploy via TorchScript.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "You can read more about FX in the official [documentation](https://pytorch.org/docs/master/fx.html). You can also find several examples of program transformations implemented using ```torch.fx``` [here](https://github.com/pytorch/examples/tree/master/fx). We are constantly improving FX and invite you to share any feedback you have about the toolkit on the [forums](https://discuss.pytorch.org/) or [issue tracker](https://github.com/pytorch/pytorch/issues).\n\nWe\u2019d like to acknowledge [TorchScript](https://pytorch.org/docs/stable/jit.html) tracing, [Apache MXNet](https://mxnet.apache.org/versions/1.7.0/) hybridize, and more recently [JAX](https://github.com/google/jax) as influences for program acquisition via tracing. We\u2019d also like to acknowledge [Caffe2](https://caffe2.ai/), [JAX](https://github.com/google/jax), and [TensorFlow](https://www.tensorflow.org/) as inspiration for the value of simple, directed dataflow graph program representations and transformations over those representations.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "# Distributed Training\nThe PyTorch 1.8 release added a number of new features as well as improvements to reliability and usability. Concretely, support for: [Stable level async error/timeout handling](https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group) was added to improve NCCL reliability; and stable support for [RPC based profiling](https://pytorch.org/docs/stable/rpc.html). Additionally, we have added support for pipeline parallelism as well as gradient compression through the use of communication hooks in DDP. Details are below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### [Beta] Pipeline Parallelism\nAs machine learning models continue to grow in size, traditional Distributed DataParallel (DDP) training no longer scales as these models don\u2019t fit on a single GPU device. The new pipeline parallelism feature provides an easy to use PyTorch API to leverage pipeline parallelism as part of your training loop.\n* [RFC](https://github.com/pytorch/pytorch/issues/44827)\n* [Documentation](https://pytorch.org/docs/1.8.0/pipeline.html?highlight=pipeline#)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### [Beta] DDP Communication Hook\nThe DDP communication hook is a generic interface to control how to communicate gradients across workers by overriding the vanilla allreduce in DistributedDataParallel. A few built-in communication hooks are provided including PowerSGD, and users can easily apply any of these hooks to optimize communication. Additionally, the communication hook interface can also support user-defined communication strategies for more advanced use cases.\n* [RFC](https://github.com/pytorch/pytorch/issues/39272)\n* [Documentation](https://pytorch.org/docs/1.8.0/ddp_comm_hooks.html?highlight=powersgd)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### Additional Prototype Features for Distributed Training\nIn addition to the major stable and beta distributed training features in this release, we also have a number of prototype features available in our nightlies to try out and provide feedback. We have linked in the draft docs below for reference:\n* **(Prototype) ZeroRedundancyOptimizer** - Based on and in partnership with the Microsoft DeepSpeed team, this feature helps reduce per-process memory footprint by sharding optimizer states across all participating processes in the ```ProcessGroup``` gang. Refer to this [documentation](https://pytorch.org/docs/master/distributed.optim.html#torch.distributed.optim.ZeroRedundancyOptimizer) for more details. \n* **(Prototype) Process Group NCCL Send/Recv** - The NCCL send/recv API was introduced in v2.7 and this feature adds support for it in NCCL process groups. This feature will provide an option for users to implement collective operations at Python layer instead of C++ layer. Refer to this [documentation](https://pytorch.org/docs/master/distributed.html#distributed-communication-package-torch-distributed) and [code examples](https://github.com/pytorch/pytorch/blob/master/torch/distributed/distributed_c10d.py#L899) to learn more.\n* **(Prototype) CUDA-support in RPC using TensorPipe** - This feature should bring consequent speed improvements for users of PyTorch RPC with multiple-GPU machines, as TensorPipe will automatically leverage NVLink when available, and avoid costly copies to and from host memory when exchanging GPU tensors between processes. When not on the same machine, TensorPipe will fall back to copying the tensor to host memory and sending it as a regular CPU tensor. This will also improve the user experience as users will be able to treat GPU tensors like regular CPU tensors in their code. Refer to this [documentation](https://pytorch.org/docs/1.8.0/rpc.html) for more details.\n* **(Prototype) Remote Module** - This feature allows users to operate a module on a remote worker like using a local module, where the RPCs are transparent to the user. In the past, this functionality was implemented in an ad-hoc way and overall this feature will improve the usability of model parallelism on PyTorch. Refer to this [documentation](https://pytorch.org/docs/master/rpc.html#remotemodule) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "# PyTorch Mobile\nSupport for PyTorch Mobile is expanding with a new set of tutorials to help new users launch models on-device quicker and give existing users a tool to get more out of our framework. These include:\n* [Image segmentation DeepLabV3 on iOS](https://pytorch.org/tutorials/beginner/deeplabv3_on_ios.html)\n* [Image segmentation DeepLabV3 on Android](https://pytorch.org/tutorials/beginner/deeplabv3_on_android.html)\n\nOur new demo apps also include examples of image segmentation, object detection, neural machine translation, question answering, and vision transformers. They are available on both iOS and Android:\n* [iOS demo app](https://github.com/pytorch/ios-demo-app)\n* [Android demo app](https://github.com/pytorch/android-demo-app)\n\nIn addition to performance improvements on CPU for MobileNetV3 and other models, we also revamped our Android GPU backend prototype for broader models coverage and faster inferencing:\n* [Android tutorial](https://pytorch.org/tutorials/prototype/vulkan_workflow.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "Lastly, we are launching the PyTorch Mobile Lite Interpreter as a prototype feature in this release. The Lite Interpreter allows users to reduce the runtime binary size. Please try these out and send us your feedback on the [PyTorch Forums](https://discuss.pytorch.org/c/mobile/). All our latest updates can be found on the [PyTorch Mobile page](https://pytorch.org/mobile/home/)\n\n### [Prototype] PyTorch Mobile Lite Interpreter\nPyTorch Lite Interpreter is a streamlined version of the PyTorch runtime that can execute PyTorch programs in resource constrained devices, with reduced binary size footprint. This prototype feature reduces binary sizes by up to 70% compared to the current on-device runtime in the current release. \n* [iOS/Android Tutorial](https://pytorch.org/tutorials/prototype/lite_interpreter.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "# Performance Optimization\nIn 1.8, we are releasing the support for benchmark utils to enable users to better monitor performance. We are also opening up a new automated quantization API. See the details below:\n\n### (Beta) Benchmark utils\nBenchmark utils allows users to take accurate performance measurements, and provides composable tools to help with both benchmark formulation and post processing. This expected to be helpful for contributors to PyTorch to quickly understand how their contributions are impacting PyTorch performance.\n\nExample:\n```python\nfrom torch.utils.benchmark import Timer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "results = []\nfor num_threads in [1, 2, 4]:\n timer = Timer(\n stmt=\"torch.add(x, y, out=out)\",\n setup=\"\"\"\n n = 1024\n x = torch.ones((n, n))\n y = torch.ones((n, 1))\n out = torch.empty((n, n))\n \"\"\",\n num_threads=num_threads,\n )\n results.append(timer.blocked_autorange(min_run_time=5))\n print(\n f\"{num_threads} thread{'s' if num_threads > 1 else ' ':<4}\"\n f\"{results[-1].median * 1e6:>4.0f} us \" +\n (f\"({results[0].median / results[-1].median:.1f}x)\" if num_threads > 1 else '')\n )\n\n1 thread 376 us \n2 threads 189 us (2.0x)\n4 threads 99 us (3.8x)\n```\n* [Documentation](https://pytorch.org/docs/1.8.0/benchmark_utils.html?highlight=benchmark#)\n* [Tutorial](https://pytorch.org/tutorials/recipes/recipes/benchmark.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "### (Prototype) FX Graph Mode Quantization\n FX Graph Mode Quantization is the new automated quantization API in PyTorch. It improves upon Eager Mode Quantization by adding support for functionals and automating the quantization process, although people might need to refactor the model to make the model compatible with FX Graph Mode Quantization (symbolically traceable with ```torch.fx```).\n* [Documentation](https://pytorch.org/docs/master/quantization.html#prototype-fx-graph-mode-quantization)\n* Tutorials:\n * [(Prototype) FX Graph Mode Post Training Dynamic Quantization](https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_dynamic.html)\n * [(Prototype) FX Graph Mode Post Training Static Qunatization](https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_static.html)\n * [(Prototype) FX Graph Mode Quantization User Guide](https://pytorch.org/tutorials/prototype/fx_graph_mode_quant_guide.html)\n\n# Hardware Support", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "# Hardware Support\n\n### [Beta] Ability to Extend the PyTorch Dispatcher for a new backend in C++\nIn PyTorch 1.8, you can now create new out-of-tree devices that live outside the ```pytorch/pytorch``` repo. The tutorial linked below shows how to register your device and keep it in sync with native PyTorch devices.\n* [Tutorial](https://pytorch.org/tutorials/advanced/extend_dispatcher.html)\n\n### [Beta] AMD GPU Binaries Now Available\nStarting in PyTorch 1.8, we have added support for ROCm wheels providing an easy onboarding to using AMD GPUs. You can simply go to the standard [PyTorch installation selector](https://pytorch.org/get-started/locally/) and choose ROCm as an installation option and execute the provided command.\n\nThanks for reading, and if you are excited about these updates and want to participate in the future of PyTorch, we encourage you to join the [discussion forums](https://discuss.pytorch.org/) and [open GitHub issues](https://github.com/pytorch/pytorch/issues).\n\nCheers!\n\n***Team PyTorch***", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.8-released/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Efficient PyTorch I/O library for Large Datasets, Many Files, Many GPUs'\nauthor: Alex Aizman, Gavin Maltby, Thomas Breuel\n---\n\nData sets are growing bigger every day and GPUs are getting faster. This means there are more data sets for deep learning researchers and engineers to train and validate their models.\n\n* Many datasets for research in still image recognition are becoming available with 10 million or more images, including OpenImages and Places.\n* million YouTube videos [(YouTube 8M)](https://research.google.com/youtube8m/) consume about 300 TB in 720p, used for research in object recognition, video analytics, and action recognition.\n* The Tobacco Corpus consists of about 20 million scanned HD pages, useful for OCR and text analytics research.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} {"page_content": "Although the most commonly encountered big data sets right now involve images and videos, big datasets occur in many other domains and involve many other kinds of data types: web pages, financial transactions, network traces, brain scans, etc.\n\nHowever, working with the large amount of data sets presents a number of challenges:", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* **Dataset Size:** datasets often exceed the capacity of node-local disk storage, requiring distributed storage systems and efficient network access.\n* **Number of Files:** datasets often consist of billions of files with uniformly random access patterns, something that often overwhelms both local and network file systems.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* **Data Rates:** training jobs on large datasets often use many GPUs, requiring aggregate I/O bandwidths to the dataset of many GBytes/s; these can only be satisfied by massively parallel I/O systems.\n* **Shuffling and Augmentation:** training data needs to be shuffled and augmented prior to training.\n* **Scalability:** users often want to develop and test on small datasets and then rapidly scale up to large datasets.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Traditional local and network file systems, and even object storage servers, are not designed for these kinds of applications. [The WebDataset I/O library](https://github.com/tmbdev/webdataset) for PyTorch, together with the optional [AIStore server](https://github.com/NVIDIA/aistore) and [Tensorcom](https://github.com/NVlabs/tensorcom) RDMA libraries, provide an efficient, simple, and standards-based solution to all these problems. The library is simple enough for day-to-day use, is based on mature open", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "source standards, and is easy to migrate to from existing file-based datasets.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Using WebDataset is simple and requires little effort, and it will let you scale up the same code from running local experiments to using hundreds of GPUs on clusters or in the cloud with linearly scalable performance. Even on small problems and on your desktop, it can speed up I/O tenfold and simplifies data management and processing of large datasets. The rest of this blog post tells you how to get started with WebDataset and how it works.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "The WebDataset Library\n\nThe WebDataset library provides a simple solution to the challenges listed above. Currently, it is available as a separate library [(github.com/tmbdev/webdataset)](https://github.com/tmbdev/webdataset), but it is on track for being incorporated into PyTorch (see [RFC 38419](https://github.com/pytorch/pytorch/issues/38419)). The WebDataset implementation is small (about 1500 LOC) and has no external dependencies.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Instead of inventing a new format, WebDataset represents large datasets as collections of POSIX tar archive files consisting of the original data files. The WebDataset library can use such tar archives directly for training, without the need for unpacking or local storage.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "WebDataset scales perfectly from small, local datasets to petascale datasets and training on hundreds of GPUs and allows data to be stored on local disk, on web servers, or dedicated file servers. For container-based training, WebDataset eliminates the need for volume plugins or node-local storage. As an additional benefit, datasets need not be unpacked prior to training, simplifying the distribution and use of research data.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "WebDataset implements PyTorch\u2019s [IterableDataset](https://pytorch.org/docs/stable/data.html#torch.utils.data.IterableDataset) interface and can be used like existing DataLoader-based code. Since data is stored as files inside an archive, existing loading and data augmentation code usually requires minimal modification.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "The WebDataset library is a complete solution for working with large datasets and distributed training in PyTorch (and also works with TensorFlow, Keras, and DALI via their Python APIs). Since POSIX tar archives are a standard, widely supported format, it is easy to write other tools for manipulating datasets in this format. E.g., the [tarp](https://github.com/tmbdev/tarp) command is written in Go and can shuffle and process training datasets.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Benefits\n\nThe use of sharded, sequentially readable formats is essential for very large datasets. In addition, it has benefits in many other environments. WebDataset provides a solution that scales well from small problems on a desktop machine to very large deep learning problems in clusters or in the cloud. The following table summarizes some of the benefits in different environments.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "{:.table.table-striped.table-bordered}\n | Environment | Benefits of WebDataset |\n| ------------- | ------------- |\n| Local Cluster with AIStore | AIStore can be deployed easily as K8s containers and offers linear scalability and near 100% utilization of network and I/O bandwidth. Suitable for petascale deep learning. |", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "| Cloud Computing | WebDataset deep learning jobs can be trained directly against datasets stored in cloud buckets; no volume plugins required. Local and cloud jobs work identically. Suitable for petascale learning. |\n| Local Cluster with existing distributed FS or object store | WebDataset\u2019s large sequential reads improve performance with existing distributed stores and eliminate the need for dedicated volume plugins. |", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "| Educational Environments | WebDatasets can be stored on existing web servers and web caches, and can be accessed directly by students by URL |\n| Training on Workstations from Local Drives | Jobs can start training as the data still downloads. Data doesn\u2019t need to be unpacked for training. Ten-fold improvements in I/O performance on hard drives over random access file-based datasets. |", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "| All Environments | Datasets are represented in an archival format and contain metadata such as file types. Data is compressed in native formats (JPEG, MP4, etc.). Data management, ETL-style jobs, and data transformations and I/O are simplified and easily parallelized. |", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "We will be adding more examples giving benchmarks and showing how to use WebDataset in these environments over the coming months.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "High-Performance\nFor high-performance computation on local clusters, the companion open-source [AIStore](https://github.com/NVIDIA/AIStore) server provides full disk to GPU I/O bandwidth, subject only to hardware constraints. [This Bigdata 2019 Paper](https://arxiv.org/abs/2001.01858) contains detailed benchmarks and performance measurements. In addition to benchmarks, research projects at NVIDIA and Microsoft have used WebDataset for petascale datasets and billions of training samples.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Below is a benchmark of AIStore with WebDataset clients using 12 server nodes with 10 rotational drives each.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "The left axis shows the aggregate bandwidth from the cluster, while the right scale shows the measured per drive I/O bandwidth. WebDataset and AIStore scale linearly to about 300 clients, at which point they are increasingly limited by the maximum I/O bandwidth available from the rotational drives (about 150 MBytes/s per drive). For comparison, HDFS is shown. HDFS uses a similar approach to AIStore/WebDataset and also exhibits linear scaling up to about 192 clients; at that point, it hits a performance", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "limit of about 120 MBytes/s per drive, and it failed when using more than 1024 clients. Unlike HDFS, the WebDataset-based code just uses standard URLs and HTTP to access data and works identically with local files, with files stored on web servers, and with AIStore. For comparison, NFS in similar experiments delivers about 10-20 MBytes/s per drive.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Storing Datasets in Tar Archives\n\nThe format used for WebDataset is standard POSIX tar archives, the same archives used for backup and data distribution. In order to use the format to store training samples for deep learning, we adopt some simple naming conventions:\n* datasets are POSIX tar archives\n* each training sample consists of adjacent files with the same basename\n* shards are numbered consecutively", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "For example, ImageNet is stored in 1282 separate 100 Mbyte shards with names ```pythonimagenet-train-000000.tar to imagenet-train-001281.tar,``` the contents of the first shard are:", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "```python\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n03991062_24866.cls\n-r--r--r-- bigdata/bigdata 108611 2020-05-08 21:23 n03991062_24866.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n07749582_9506.cls\n-r--r--r-- bigdata/bigdata 129044 2020-05-08 21:23 n07749582_9506.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n03425413_23604.cls\n-r--r--r-- bigdata/bigdata 106255 2020-05-08 21:23 n03425413_23604.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n02795169_27274.cls", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "WebDataset datasets can be used directly from local disk, from web servers (hence the name), from cloud storage and object stores, just by changing a URL. WebDataset datasets can be used for training without unpacking, and training can even be carried out on streaming data, with no local storage.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Shuffling during training is important for many deep learning applications, and WebDataset performs shuffling both at the shard level and at the sample level. Splitting of data across multiple workers is performed at the shard level using a user-provided ```shard_selection``` function that defaults to a function that splits based on ```get_worker_info.``` (WebDataset can be combined with the [tensorcom](https://github.com/NVLabs/tensorcom) library to offload decompression/data augmentation and provide RDMA", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "and direct-to-GPU loading; see below.)", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Code Sample\nHere are some code snippets illustrating the use of WebDataset in a typical PyTorch deep learning application (you can find a full example at [http://github.com/tmbdev/pytorch-imagenet-wds](http://github.com/tmbdev/pytorch-imagenet-wds).\n\n```python\nimport webdataset as wds\nimport ...\n\nsharedurl = \"/imagenet/imagenet-train-{000000..001281}.tar\"\n\nnormalize = transforms.Normalize(\n mean=[0.485, 0.456, 0.406],\n std=[0.229, 0.224, 0.225])", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "preproc = transforms.Compose([\n transforms.RandomResizedCrop(224),\n transforms.RandomHorizontalFlip(),\n transforms.ToTensor(),\n normalize,\n])\n\ndataset = (\n wds.Dataset(sharedurl)\n .shuffle(1000)\n .decode(\"pil\")\n .rename(image=\"jpg;png\", data=\"json\")\n .map_dict(image=preproc)\n .to_tuple(\"image\", \"data\")\n)\n\nloader = torch.utils.data.DataLoader(dataset, batch_size=64, num_workers=8)\n\nfor inputs, targets in loader:\n ...", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "This code is nearly identical to the file-based I/O pipeline found in the PyTorch Imagenet example: it creates a preprocessing/augmentation pipeline, instantiates a dataset using that pipeline and a data source location, and then constructs a DataLoader instance from the dataset.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "WebDataset uses a fluent API for a configuration that internally builds up a processing pipeline. Without any added processing stages, In this example, WebDataset is used with the PyTorch DataLoader class, which replicates DataSet instances across multiple threads and performs both parallel I/O and parallel data augmentation.\n\nWebDataset instances themselves just iterate through each training sample as a dictionary:", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "```python\n# load from a web server using a separate client process\nsharedurl = \"pipe:curl -s http://server/imagenet/imagenet-train-{000000..001281}.tar\"\n\ndataset = wds.Dataset(sharedurl)\n\nfor sample in dataset:\n # sample[\"jpg\"] contains the raw image data\n # sample[\"cls\"] contains the class\n ...", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "For a general introduction to how we handle large scale training with WebDataset, see these [YouTube videos](https://www.youtube.com/playlist?list=PL0dsKxFNMcX4XcB0w1Wm-pvSfQu-eWM26).", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "Related Software", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* [AIStore](https://github.com/NVIDIA/AIStore) is an open-source object store capable of full-bandwidth disk-to-GPU data delivery (meaning that if you have 1000 rotational drives with 200 MB/s read speed, AIStore actually delivers an aggregate bandwidth of 200 GB/s to the GPUs). AIStore is fully compatible with WebDataset as a client, and in addition understands the WebDataset format, permitting it to perform shuffling, sorting, ETL, and some map-reduce operations directly in the storage system. AIStore can", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "be thought of as a remix of a distributed object store, a network file system, a distributed database, and a GPU-accelerated map-reduce implementation.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* [tarp](https://github.com/tmbdev/tarp) is a small command-line program for splitting, merging, shuffling, and processing tar archives and WebDataset datasets.\n\n* [tensorcom](https://github.com/NVLabs/tensorcom) is a library supporting distributed data augmentation and RDMA to GPU.\n\n* [pytorch-imagenet-wds](https://github.com/tmbdev/pytorch-imagenet-wds) contains an example of how to use WebDataset with ImageNet, based on the PyTorch ImageNet example.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "* [Bigdata 2019 Paper with Benchmarks](https://arxiv.org/abs/2001.01858)\n\nCheck out [the library](https://github.com/tmbdev/webdataset) and provide your feedback for [RFC 38419](https://github.com/pytorch/pytorch/issues/38419).", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 0.4.0 Migration Guide'\nredirect_from: /2018/04/22/0_4_0-migration-guide.html\n---\n\nWelcome to the migration guide for PyTorch 0.4.0. In this release we introduced [many exciting new features and critical bug fixes](https://github.com/pytorch/pytorch/releases/tag/v0.4.0), with the goal of providing users a better and cleaner interface. In this guide, we will cover the most important changes in migrating existing code from previous versions:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "- `Tensors` and `Variables` have merged\n- Support for 0-dimensional (scalar) `Tensors`\n- Deprecation of the `volatile` flag\n- `dtypes`, `devices`, and Numpy-style `Tensor` creation functions\n- Writing device-agnostic code\n- New edge-case constraints on names of submodules, parameters, and buffers in `nn.Module`", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Merging [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) and `Variable` and classes", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) and `torch.autograd.Variable` are now the same class. More precisely, [`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) is capable of tracking history and behaves like the old `Variable`; `Variable` wrapping continues to work as before but returns an object of type [`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html). This means that you don't need the `Variable` wrapper everywhere in your code anymore.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "The `type()` of a [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) has changed\n\nNote also that the `type()` of a Tensor no longer reflects the data type. Use `isinstance()` or `x.type()`instead:\n\n```python\n>>> x = torch.DoubleTensor([1, 1, 1])\n>>> print(type(x)) # was torch.DoubleTensor\n\"\"\n>>> print(x.type()) # OK: 'torch.DoubleTensor'\n'torch.DoubleTensor'\n>>> print(isinstance(x, torch.DoubleTensor)) # OK: True\nTrue\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "When does [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html) start tracking history now?\n\n`requires_grad`, the central flag for [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html), is now an attribute on `Tensors`. The same rules previously used for `Variables` applies to `Tensors`; [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html) starts tracking history when any input `Tensor` of an operation has `requires_grad=True`. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> x = torch.ones(1) # create a tensor with requires_grad=False (default)\n>>> x.requires_grad\nFalse\n>>> y = torch.ones(1) # another tensor with requires_grad=False\n>>> z = x + y\n>>> # both inputs have requires_grad=False. so does the output\n>>> z.requires_grad\nFalse\n>>> # then autograd won't track this computation. let's verify!\n>>> z.backward()\nRuntimeError: element 0 of tensors does not require grad and does not have a grad_fn\n>>>\n>>> # now create a tensor with requires_grad=True", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": ">>> w = torch.ones(1, requires_grad=True)\n>>> w.requires_grad\nTrue\n>>> # add to the previous result that has require_grad=False\n>>> total = w + z\n>>> # the total sum now requires grad!\n>>> total.requires_grad\nTrue\n>>> # autograd can compute the gradients as well\n>>> total.backward()\n>>> w.grad\ntensor([ 1.])\n>>> # and no computation is wasted to compute gradients for x, y and z, which don't require grad\n>>> z.grad == x.grad == y.grad == None\nTrue\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Manipulating `requires_grad` flag\n\nOther than directly setting the attribute, you can change this flag `in-place` using [`my_tensor.requires_grad_()`](https://pytorch.org/docs/0.4.0/tensors.html#torch.Tensor.requires_grad_), or, as in the above example, at creation time by passing it in as an argument (default is `False`), e.g.,\n\n```python\n>>> existing_tensor.requires_grad_()\n>>> existing_tensor.requires_grad\nTrue\n>>> my_tensor = torch.zeros(3, 4, requires_grad=True)\n>>> my_tensor.requires_grad\nTrue\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "What about `.data?`\n\n`.data` was the primary way to get the underlying `Tensor` from a `Variable`. After this merge, calling `y = x.data` still has similar semantics. So `y` will be a `Tensor` that shares the same data with `x`, is unrelated with the computation history of `x`, and has `requires_grad=False`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "However, `.data` can be unsafe in some cases. Any changes on `x.data` wouldn't be tracked by `autograd`, and the computed gradients would be incorrect if `x` is needed in a backward pass. A safer alternative is to use [`x.detach()`](https://pytorch.org/docs/master/autograd.html#torch.Tensor.detach), which also returns a `Tensor` that shares data with `requires_grad=False`, but will have its in-place changes reported by `autograd` if `x` is needed in backward.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Here is an example of the difference between `.data` and `x.detach()` (and why we recommend using `detach` in general).\n\nIf you use `Tensor.detach()`, the gradient computation is guaranteed to be correct.\n\n```python\n>>> a = torch.tensor([1,2,3.], requires_grad = True)\n>>> out = a.sigmoid()\n>>> c = out.detach()\n>>> c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out # modified by c.zero_() !!\ntensor([ 0., 0., 0.])", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": ">>> out.sum().backward() # Requires the original value of out, but that was overwritten by c.zero_()\nRuntimeError: one of the variables needed for gradient computation has been modified by an", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "However, using `Tensor.data` can be unsafe and can easily result in incorrect gradients when a tensor is required for gradient computation but modified in-place.\n\n```python\n>>> a = torch.tensor([1,2,3.], requires_grad = True)\n>>> out = a.sigmoid()\n>>> c = out.data\n>>> c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out # out was modified by c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out.sum().backward()\n>>> a.grad # The result is very, very wrong because `out` changed!\ntensor([ 0., 0., 0.])\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Support for 0-dimensional (scalar) Tensors\n\nPreviously, indexing into a `Tensor` vector (1-dimensional tensor) gave a Python number but indexing into a `Variable` vector gave (inconsistently!) a vector of size `(1,)`! Similar behavior existed with reduction functions, e.g. `tensor.sum()` would return a Python number, but `variable.sum()` would return a vector of size `(1,)`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Fortunately, this release introduces proper scalar (0-dimensional tensor) support in PyTorch! Scalars can be created using the new `torch.tensor` function (which will be explained in more detail later; for now just think of it as the PyTorch equivalent of `numpy.array`). Now you can do things like:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> torch.tensor(3.1416) # create a scalar directly\ntensor(3.1416)\n>>> torch.tensor(3.1416).size() # scalar is 0-dimensional\ntorch.Size([])\n>>> torch.tensor([3]).size() # compare to a vector of size 1\ntorch.Size([1])\n>>>\n>>> vector = torch.arange(2, 6) # this is a vector\n>>> vector\ntensor([ 2., 3., 4., 5.])\n>>> vector.size()\ntorch.Size([4])\n>>> vector[3] # indexing into a vector gives a scalar\ntensor(5.)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": ">>> vector[3].item() # .item() gives the value as a Python number\n5.0\n>>> mysum = torch.tensor([2, 3]).sum()\n>>> mysum\ntensor(5)\n>>> mysum.size()\ntorch.Size([])\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Accumulating losses\n\nConsider the widely used pattern `total_loss += loss.data[0]`. Before 0.4.0. `loss` was a `Variable` wrapping a tensor of size `(1,)`, but in 0.4.0 `loss` is now a scalar and has `0` dimensions. Indexing into a scalar doesn't make sense (it gives a warning now, but will be a hard error in 0.5.0). Use `loss.item()` to get the Python number from a scalar.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Note that if you don't convert to a Python number when accumulating losses, you may find increased memory usage in your program. This is because the right-hand-side of the above expression used to be a Python float, while it is now a zero-dim Tensor. The total loss is thus accumulating Tensors and their gradient history, which may keep around large autograd graphs for much longer than necessary.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Deprecation of volatile flag\n\nThe `volatile` flag is now deprecated and has no effect. Previously, any computation that involves a `Variable` with `volatile=True` wouldn't be tracked by `autograd`. This has now been replaced by a [set of more flexible context managers](https://pytorch.org/docs/0.4.0/torch.html#locally-disabling-gradient-computation) including `torch.no_grad()`, `torch.set_grad_enabled(grad_mode)`, and others.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> x = torch.zeros(1, requires_grad=True)\n>>> with torch.no_grad():\n... y = x * 2\n>>> y.requires_grad\nFalse\n>>>\n>>> is_train = False\n>>> with torch.set_grad_enabled(is_train):\n... y = x * 2\n>>> y.requires_grad\nFalse\n>>> torch.set_grad_enabled(True) # this can also be used as a function\n>>> y = x * 2\n>>> y.requires_grad\nTrue\n>>> torch.set_grad_enabled(False)\n>>> y = x * 2\n>>> y.requires_grad\nFalse\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`dtypes`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype), [`devices`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) and NumPy-style creation functions", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "In previous versions of PyTorch, we used to specify data type (e.g. float vs double), device type (cpu vs cuda) and layout (dense vs sparse) together as a \"tensor type\". For example, `torch.cuda.sparse.DoubleTensor` was the `Tensor` type representing the `double` data type, living on CUDA devices, and with [COO sparse tensor](https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)) layout.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "In this release, we introduce [`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype), [`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) and [`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout) classes to allow better management of these properties via NumPy-style creation functions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype)\n\nBelow is a complete list of available [`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype)s (data types) and their corresponding tensor types.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| Data | `type torch.dtype` | Tensor types |\n|------|------------------|--------------|\n| 32-bit floating point | `torch.float32` or `torch.float` | `torch.*.FloatTensor`\n| 64-bit floating point | `torch.float64` or `torch.double` | `torch.*.DoubleTensor`\n| 16-bit floating point | `torch.float16` or `torch.half` | `torch.*.HalfTensor`\n| 8-bit integer (unsigned) | `torch.uint8` | `torch.*.ByteTensor`\n| 8-bit integer (signed) | `torch.int8` | `torch.*.CharTensor`", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| 16-bit integer (signed) | `torch.int16` or `torch.short` | `torch.*.ShortTensor`\n| 32-bit integer (signed) | `torch.int32` or `torch.int` | `torch.*.IntTensor`\n| 64-bit integer (signed) | `torch.int64` or `torch.long` | `torch.*.LongTensor`", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "The dtype of a tensor can be access via its `dtype` attribute.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device)\n\nA [`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) contains a device type (`'cpu'` or `'cuda'`) and optional device ordinal (id) for the device type. It can be initialized with `torch.device('{device_type}')` or `torch.device('{device_type}:{device_ordinal}')`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "If the device ordinal is not present, this represents the current device for the device type; e.g., `torch.device('cuda')` is equivalent to `torch.device('cuda:X')` where `X` is the result of `torch.cuda.current_device()`.\n\nThe device of a tensor can be accessed via its `device` attribute.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout)\n\n[`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout) represents the data layout of a [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html). Currently `torch.strided` (dense tensors, the default) and `torch.sparse_coo` (sparse tensors with COO format) are supported.\n\nThe layout of a tensor can be access via its `layout` attribute.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Creating Tensors\n\n[Methods that create a](https://pytorch.org/docs/0.4.0/torch.html#creation-ops) [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) now also take in `dtype`, `device`, `layout`, and `requires_grad` options to specify the desired attributes on the returned `Tensor`. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> device = torch.device(\"cuda:1\")\n>>> x = torch.randn(3, 3, dtype=torch.float64, device=device)\ntensor([[-0.6344, 0.8562, -1.2758],\n [ 0.8414, 1.7962, 1.0589],\n [-0.1369, -1.0462, -0.4373]], dtype=torch.float64, device='cuda:1')\n>>> x.requires_grad # default is False\nFalse\n>>> x = torch.zeros(3, requires_grad=True)\n>>> x.requires_grad\nTrue\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.tensor(data, ...)`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "[`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) is one of the newly added [tensor creation methods](https://pytorch.org/docs/0.4.0/torch.html#creation-ops). It takes in array-like data of all kinds and copies the contained values into a new `Tensor`. As mentioned earlier, [`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) is the PyTorch equivalent of NumPy's `numpy.array`constructor. Unlike the `torch.*Tensor` methods, you can also create zero-dimensional", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "`Tensor`s (aka scalars) this way (a single python number is treated as a Size in the `torch.*Tensor methods`). Moreover, if a `dtype` argument isn't given, it will infer the suitable `dtype` given the data. It is the recommended way to create a tensor from existing data like a Python list. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n>>> cuda = torch.device(\"cuda\")\n>>> torch.tensor([[1], [2], [3]], dtype=torch.half, device=cuda)\ntensor([[ 1],\n [ 2],\n [ 3]], device='cuda:0')\n>>> torch.tensor(1) # scalar\ntensor(1)\n>>> torch.tensor([1, 2.3]).dtype # type inferece\ntorch.float32\n>>> torch.tensor([1, 2]).dtype # type inferece\ntorch.int64", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "We've also added more tensor creation methods. Some of them have `torch.*_like` and/or `tensor.new_*` variants.\n\n- `torch.*_like` takes in an input `Tensor` instead of a shape. It returns a `Tensor` with same attributes as the input `Tensor` by default unless otherwise specified:\n\n ```python\n >>> x = torch.randn(3, dtype=torch.float64)\n >>> torch.zeros_like(x)\n tensor([ 0., 0., 0.], dtype=torch.float64)\n >>> torch.zeros_like(x, dtype=torch.int)\n tensor([ 0, 0, 0], dtype=torch.int32)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "- `tensor.new_*` can also create `Tensors` with same attributes as `tensor`, but it always takes in a shape argument:\n\n ```python\n >>> x = torch.randn(3, dtype=torch.float64)\n >>> x.new_ones(2)\n tensor([ 1., 1.], dtype=torch.float64)\n >>> x.new_ones(4, dtype=torch.int)\n tensor([ 1, 1, 1, 1], dtype=torch.int32)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "To specify the desired shape, you can either use a tuple (e.g., `torch.zeros((2, 3))`) or variable arguments (e.g., `torch.zeros(2, 3)`) in most cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| Name | Returned `Tensor` | `torch.*_like` variant | `tensor.new_*` variant |\n|------|-----------------|----------------------|----------------------|\n| [`torch.empty`](https://pytorch.org/docs/0.4.0/torch.html#torch.empty) | uninitialized memory | \u2714 | \u2714 |\n| [`torch.zeros`](https://pytorch.org/docs/0.4.0/torch.html#torch.zeros) | all zeros | \u2714 | \u2714 |\n| [`torch.ones`](https://pytorch.org/docs/0.4.0/torch.html#torch.ones) | all ones | \u2714 | \u2714 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| [`torch.full`](https://pytorch.org/docs/0.4.0/torch.html#torch.full) | filled with a given value | \u2714 | \u2714 |\n| [`torch.rand`](https://pytorch.org/docs/0.4.0/torch.html#torch.rand) | i.i.d. continuous Uniform[0, 1) | \u2714 |\n| [`torch.randn`](https://pytorch.org/docs/0.4.0/torch.html#torch.randn) | i.i.d. `Normal(0, 1)` | \u2714 |\n| [`torch.randint`](https://pytorch.org/docs/0.4.0/torch.html#torch.randint) | i.i.d. discrete Uniform in given range | \u2714 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| [`torch.randperm`](https://pytorch.org/docs/0.4.0/torch.html#torch.randperm) | random permutation of `{0, 1, ..., n - 1}` |\n| [`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) | copied from existing data (list, NumPy ndarray, etc.) | | \u2714 |\n| [`torch.from_numpy`*](https://pytorch.org/docs/0.4.0/torch.html#torch.from_numpy) | from NumPy `ndarray` (sharing storage without copying) |", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "| [`torch.arange`](https://pytorch.org/docs/0.4.0/torch.html#torch.arange), [`torch.range`](https://pytorch.org/docs/0.4.0/torch.html#torch.range), and [`torch.linspace`](https://pytorch.org/docs/0.4.0/torch.html#torch.linspace) | uniformly spaced values in a given range |\n| [`torch.logspace`](https://pytorch.org/docs/0.4.0/torch.html#torch.logspace) | logarithmically spaced values in a given range |\n| [`torch.eye`](https://pytorch.org/docs/0.4.0/torch.html#torch.eye) | identity matrix |", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "*: [`torch.from_numpy`](https://pytorch.org/docs/0.4.0/torch.html#torch.from_numpy) only takes in a NumPy `ndarray` as its input argument.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Writing device-agnostic code\n\nPrevious versions of PyTorch made it difficult to write code that was device agnostic (i.e. that could run on both CUDA-enabled and CPU-only machines without modification).\n\nPyTorch 0.4.0 makes this easier in two ways:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "- The `device` attribute of a Tensor gives the [torch.device](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) for all Tensors (`get_device` only works for CUDA tensors)\n- The `to` method of `Tensors` and `Modules` can be used to easily move objects to different devices (instead of having to call `cpu()` or `cuda()` based on the context)\n\nWe recommend the following pattern:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "```python\n# at beginning of the script\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n...\n\n# then whenever you get a new Tensor or Module\n# this won't copy if they are already on the desired device\ninput = data.to(device)\nmodel = MyModule(...).to(device)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "New edge-case constraints on names of submodules, parameters, and buffers in `nn.Module`\n\n`name` that is an empty string or contains `\".\"` is no longer permitted in `module.add_module(name, value)`, `module.add_parameter(name, value)` or `module.add_buffer(name, value)` because such names may cause lost data in the `state_dict`. If you are loading a checkpoint for modules containing such names, please update the module definition and patch the `state_dict` before loading it.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Code Samples (Putting it all together)\n\nTo get a flavor of the overall recommended changes in 0.4.0, let's look at a quick example for a common code pattern in both 0.3.1 and 0.4.0:\n\n- 0.3.1 (old):\n ```python\n model = MyRNN()\n if use_cuda:\n model = model.cuda()", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "# train\n total_loss = 0\n for input, target in train_loader:\n input, target = Variable(input), Variable(target)\n hidden = Variable(torch.zeros(*h_shape)) # init hidden\n if use_cuda:\n input, target, hidden = input.cuda(), target.cuda(), hidden.cuda()\n ... # get loss and optimize\n total_loss += loss.data[0]\n\n # evaluate\n for input, target in test_loader:\n input = Variable(input, volatile=True)\n if use_cuda:\n ...\n ...", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "- 0.4.0 (new):\n ```python\n # torch.device object used throughout this script\n device = torch.device(\"cuda\" if use_cuda else \"cpu\")\n\n model = MyRNN().to(device)\n\n # train\n total_loss = 0\n for input, target in train_loader:\n input, target = input.to(device), target.to(device)\n hidden = input.new_zeros(*h_shape) # has the same device & dtype as `input`\n ... # get loss and optimize\n total_loss += loss.item() # get Python number from 1-element Tensor", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "# evaluate\n with torch.no_grad(): # operations inside don't track history\n for input, target in test_loader:\n ...", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "Thank you for reading! Please refer to our [documentation](https://pytorch.org/docs/0.4.0/index.html) and [release notes](https://github.com/pytorch/pytorch/releases/tag/v0.4.0) for more details.\n\nHappy PyTorch-ing!", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Efficient Large-Scale Training with Pytorch FSDP and AWS\"\nauthor: Less Wright, Hamid Shojanazeri, Geeta Chauhan\nfeatured-img: \"assets/images/largeblog_index_1.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Cutting-edge AI models are becoming extremely large. The cost and overhead of training these models is increasing rapidly, and involves large amounts of engineering and guesswork to find the right training regime. FSDP reduces these costs significantly by enabling you to train much larger models with the same amount of resources. FSDP lowers the memory footprint on your GPUs, and is usable via a lightweight configuration that requires substantially less effort, typically with just a few lines of code.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The main performance gains in FSDP come from maximizing the overlap between network communication and model computation, and eliminating the memory redundancy inherent in traditional data parallel training (DDP). PyTorch FSDP can train models approximately 4x larger on the same server resources as DDP and 20x larger if we combine activation checkpointing and activation offloading.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Since PyTorch 1.12, FSDP is now in beta status, and has added a number of new features that can be tuned to further accelerate your model training. \n\nIn this series of blog posts, we will explain multiple performance optimizations you can run with FSDP to boost your distributed training speed and model sizes within the context of your available server resources. We use the HuggingFace T5 3B, 11B and DeepVit, in fine-tuning mode, as the running examples throughout the series.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "As a preview of some of the optimizations discussed in this series, we show the before and after performance scaled in Flops below (Note that these results can vary based on your server resources and model architecture). \n\n

\n\n

\n\n *T5 3B Performance measured on AWS A100 and A10 servers. Original with no optimizations and Tuned with the applied optimization ", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n *T5 11B Performance measured on A100 servers. Original with no optimizations and Tuned with the applied optimization ", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "* **Dataset Size:** datasets often exceed the capacity of node-local disk storage, requiring distributed storage systems and efficient network access.\n* **Number of Files:** datasets often consist of billions of files with uniformly random access patterns, something that often overwhelms both local and network file systems.\n* **Data Rates:** training jobs on large datasets often use many GPUs, requiring aggregate I/O bandwidths to the dataset of many GBytes/s; these can only be satisfied by massively parallel I/O systems.\n* **Shuffling and Augmentation:** training data needs to be shuffled and augmented prior to training.\n* **Scalability:** users often want to develop and test on small datasets and then rapidly scale up to large datasets.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "Traditional local and network file systems, and even object storage servers, are not designed for these kinds of applications. [The WebDataset I/O library](https://github.com/tmbdev/webdataset) for PyTorch, together with the optional [AIStore server](https://github.com/NVIDIA/aistore) and [Tensorcom](https://github.com/NVlabs/tensorcom) RDMA libraries, provide an efficient, simple, and standards-based solution to all these problems. The library is simple enough for day-to-day use, is based on mature open source standards, and is easy to migrate to from existing file-based datasets.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "Using WebDataset is simple and requires little effort, and it will let you scale up the same code from running local experiments to using hundreds of GPUs on clusters or in the cloud with linearly scalable performance. Even on small problems and on your desktop, it can speed up I/O tenfold and simplifies data management and processing of large datasets. The rest of this blog post tells you how to get started with WebDataset and how it works.\n\n## The WebDataset Library\n\nThe WebDataset library provides a simple solution to the challenges listed above. Currently, it is available as a separate library [(github.com/tmbdev/webdataset)](https://github.com/tmbdev/webdataset), but it is on track for being incorporated into PyTorch (see [RFC 38419](https://github.com/pytorch/pytorch/issues/38419)). The WebDataset implementation is small (about 1500 LOC) and has no external dependencies.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "Instead of inventing a new format, WebDataset represents large datasets as collections of POSIX tar archive files consisting of the original data files. The WebDataset library can use such tar archives directly for training, without the need for unpacking or local storage.\n\nWebDataset scales perfectly from small, local datasets to petascale datasets and training on hundreds of GPUs and allows data to be stored on local disk, on web servers, or dedicated file servers. For container-based training, WebDataset eliminates the need for volume plugins or node-local storage. As an additional benefit, datasets need not be unpacked prior to training, simplifying the distribution and use of research data.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "WebDataset implements PyTorch\u2019s [IterableDataset](https://pytorch.org/docs/stable/data.html#torch.utils.data.IterableDataset) interface and can be used like existing DataLoader-based code. Since data is stored as files inside an archive, existing loading and data augmentation code usually requires minimal modification.\n\nThe WebDataset library is a complete solution for working with large datasets and distributed training in PyTorch (and also works with TensorFlow, Keras, and DALI via their Python APIs). Since POSIX tar archives are a standard, widely supported format, it is easy to write other tools for manipulating datasets in this format. E.g., the [tarp](https://github.com/tmbdev/tarp) command is written in Go and can shuffle and process training datasets.\n\n## Benefits", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "## Benefits\n\nThe use of sharded, sequentially readable formats is essential for very large datasets. In addition, it has benefits in many other environments. WebDataset provides a solution that scales well from small problems on a desktop machine to very large deep learning problems in clusters or in the cloud. The following table summarizes some of the benefits in different environments.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "{:.table.table-striped.table-bordered}\n | Environment | Benefits of WebDataset |\n| ------------- | ------------- |\n| Local Cluster with AIStore | AIStore can be deployed easily as K8s containers and offers linear scalability and near 100% utilization of network and I/O bandwidth. Suitable for petascale deep learning. |\n| Cloud Computing | WebDataset deep learning jobs can be trained directly against datasets stored in cloud buckets; no volume plugins required. Local and cloud jobs work identically. Suitable for petascale learning. |\n| Local Cluster with existing distributed FS or object store | WebDataset\u2019s large sequential reads improve performance with existing distributed stores and eliminate the need for dedicated volume plugins. |\n| Educational Environments | WebDatasets can be stored on existing web servers and web caches, and can be accessed directly by students by URL |\n| Training on Workstations from Local Drives | Jobs can start training as the data still downloads. Data doesn\u2019t need to be unpacked for training. Ten-fold improvements in I/O performance on hard drives over random access file-based datasets. |\n| All Environments | Datasets are represented in an archival format and contain metadata such as file types. Data is compressed in native formats (JPEG, MP4, etc.). Data management, ETL-style jobs, and data transformations and I/O are simplified and easily parallelized. |", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "We will be adding more examples giving benchmarks and showing how to use WebDataset in these environments over the coming months.\n\n## High-Performance\nFor high-performance computation on local clusters, the companion open-source [AIStore](https://github.com/NVIDIA/AIStore) server provides full disk to GPU I/O bandwidth, subject only to hardware constraints. [This Bigdata 2019 Paper](https://arxiv.org/abs/2001.01858) contains detailed benchmarks and performance measurements. In addition to benchmarks, research projects at NVIDIA and Microsoft have used WebDataset for petascale datasets and billions of training samples.\n\nBelow is a benchmark of AIStore with WebDataset clients using 12 server nodes with 10 rotational drives each.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "The left axis shows the aggregate bandwidth from the cluster, while the right scale shows the measured per drive I/O bandwidth. WebDataset and AIStore scale linearly to about 300 clients, at which point they are increasingly limited by the maximum I/O bandwidth available from the rotational drives (about 150 MBytes/s per drive). For comparison, HDFS is shown. HDFS uses a similar approach to AIStore/WebDataset and also exhibits linear scaling up to about 192 clients; at that point, it hits a performance limit of about 120 MBytes/s per drive, and it failed when using more than 1024 clients. Unlike HDFS, the WebDataset-based code just uses standard URLs and HTTP to access data and works identically with local files, with files stored on web servers, and with AIStore. For comparison, NFS in similar experiments delivers about 10-20 MBytes/s per drive.\n\n## Storing Datasets in Tar Archives", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "The format used for WebDataset is standard POSIX tar archives, the same archives used for backup and data distribution. In order to use the format to store training samples for deep learning, we adopt some simple naming conventions:\n* datasets are POSIX tar archives\n* each training sample consists of adjacent files with the same basename\n* shards are numbered consecutively\n\nFor example, ImageNet is stored in 1282 separate 100 Mbyte shards with names ```pythonimagenet-train-000000.tar to imagenet-train-001281.tar,``` the contents of the first shard are:", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "```python\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n03991062_24866.cls\n-r--r--r-- bigdata/bigdata 108611 2020-05-08 21:23 n03991062_24866.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n07749582_9506.cls\n-r--r--r-- bigdata/bigdata 129044 2020-05-08 21:23 n07749582_9506.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n03425413_23604.cls\n-r--r--r-- bigdata/bigdata 106255 2020-05-08 21:23 n03425413_23604.jpg\n-r--r--r-- bigdata/bigdata 3 2020-05-08 21:23 n02795169_27274.cls\n```\n\nWebDataset datasets can be used directly from local disk, from web servers (hence the name), from cloud storage and object stores, just by changing a URL. WebDataset datasets can be used for training without unpacking, and training can even be carried out on streaming data, with no local storage.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "Shuffling during training is important for many deep learning applications, and WebDataset performs shuffling both at the shard level and at the sample level. Splitting of data across multiple workers is performed at the shard level using a user-provided ```shard_selection``` function that defaults to a function that splits based on ```get_worker_info.``` (WebDataset can be combined with the [tensorcom](https://github.com/NVLabs/tensorcom) library to offload decompression/data augmentation and provide RDMA and direct-to-GPU loading; see below.)\n\n## Code Sample\nHere are some code snippets illustrating the use of WebDataset in a typical PyTorch deep learning application (you can find a full example at [http://github.com/tmbdev/pytorch-imagenet-wds](http://github.com/tmbdev/pytorch-imagenet-wds).\n\n```python\nimport webdataset as wds\nimport ...\n\nsharedurl = \"/imagenet/imagenet-train-{000000..001281}.tar\"\n\nnormalize = transforms.Normalize(\n mean=[0.485, 0.456, 0.406],\n std=[0.229, 0.224, 0.225])", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "preproc = transforms.Compose([\n transforms.RandomResizedCrop(224),\n transforms.RandomHorizontalFlip(),\n transforms.ToTensor(),\n normalize,\n])\n\ndataset = (\n wds.Dataset(sharedurl)\n .shuffle(1000)\n .decode(\"pil\")\n .rename(image=\"jpg;png\", data=\"json\")\n .map_dict(image=preproc)\n .to_tuple(\"image\", \"data\")\n)\n\nloader = torch.utils.data.DataLoader(dataset, batch_size=64, num_workers=8)\n\nfor inputs, targets in loader:\n ...\n ```\n\nThis code is nearly identical to the file-based I/O pipeline found in the PyTorch Imagenet example: it creates a preprocessing/augmentation pipeline, instantiates a dataset using that pipeline and a data source location, and then constructs a DataLoader instance from the dataset.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "WebDataset uses a fluent API for a configuration that internally builds up a processing pipeline. Without any added processing stages, In this example, WebDataset is used with the PyTorch DataLoader class, which replicates DataSet instances across multiple threads and performs both parallel I/O and parallel data augmentation.\n\nWebDataset instances themselves just iterate through each training sample as a dictionary:\n\n```python\n# load from a web server using a separate client process\nsharedurl = \"pipe:curl -s http://server/imagenet/imagenet-train-{000000..001281}.tar\"\n\ndataset = wds.Dataset(sharedurl)\n\nfor sample in dataset:\n # sample[\"jpg\"] contains the raw image data\n # sample[\"cls\"] contains the class\n ...\n ```\n\nFor a general introduction to how we handle large scale training with WebDataset, see these [YouTube videos](https://www.youtube.com/playlist?list=PL0dsKxFNMcX4XcB0w1Wm-pvSfQu-eWM26).\n\n## Related Software", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "## Related Software\n\n* [AIStore](https://github.com/NVIDIA/AIStore) is an open-source object store capable of full-bandwidth disk-to-GPU data delivery (meaning that if you have 1000 rotational drives with 200 MB/s read speed, AIStore actually delivers an aggregate bandwidth of 200 GB/s to the GPUs). AIStore is fully compatible with WebDataset as a client, and in addition understands the WebDataset format, permitting it to perform shuffling, sorting, ETL, and some map-reduce operations directly in the storage system. AIStore can be thought of as a remix of a distributed object store, a network file system, a distributed database, and a GPU-accelerated map-reduce implementation.\n\n* [tarp](https://github.com/tmbdev/tarp) is a small command-line program for splitting, merging, shuffling, and processing tar archives and WebDataset datasets.\n\n* [tensorcom](https://github.com/NVLabs/tensorcom) is a library supporting distributed data augmentation and RDMA to GPU.", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "* [pytorch-imagenet-wds](https://github.com/tmbdev/pytorch-imagenet-wds) contains an example of how to use WebDataset with ImageNet, based on the PyTorch ImageNet example.\n\n* [Bigdata 2019 Paper with Benchmarks](https://arxiv.org/abs/2001.01858)\n\nCheck out [the library](https://github.com/tmbdev/webdataset) and provide your feedback for [RFC 38419](https://github.com/pytorch/pytorch/issues/38419).", "metadata": {"source": "https://pytorch.org/blog/efficient-pytorch-io-library-for-large-datasets-many-files-many-gpus/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'PyTorch 0.4.0 Migration Guide'\nredirect_from: /2018/04/22/0_4_0-migration-guide.html\n---\n\nWelcome to the migration guide for PyTorch 0.4.0. In this release we introduced [many exciting new features and critical bug fixes](https://github.com/pytorch/pytorch/releases/tag/v0.4.0), with the goal of providing users a better and cleaner interface. In this guide, we will cover the most important changes in migrating existing code from previous versions:\n\n- `Tensors` and `Variables` have merged\n- Support for 0-dimensional (scalar) `Tensors`\n- Deprecation of the `volatile` flag\n- `dtypes`, `devices`, and Numpy-style `Tensor` creation functions\n- Writing device-agnostic code\n- New edge-case constraints on names of submodules, parameters, and buffers in `nn.Module`\n\n## Merging [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) and `Variable` and classes", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "[`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) and `torch.autograd.Variable` are now the same class. More precisely, [`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) is capable of tracking history and behaves like the old `Variable`; `Variable` wrapping continues to work as before but returns an object of type [`torch.Tensor`](https://pytorch.org/docs/0.4.0/tensors.html). This means that you don't need the `Variable` wrapper everywhere in your code anymore.\n\n### The `type()` of a [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) has changed\n\nNote also that the `type()` of a Tensor no longer reflects the data type. Use `isinstance()` or `x.type()`instead:\n\n```python\n>>> x = torch.DoubleTensor([1, 1, 1])\n>>> print(type(x)) # was torch.DoubleTensor\n\"\"\n>>> print(x.type()) # OK: 'torch.DoubleTensor'\n'torch.DoubleTensor'\n>>> print(isinstance(x, torch.DoubleTensor)) # OK: True\nTrue\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "### When does [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html) start tracking history now?\n\n`requires_grad`, the central flag for [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html), is now an attribute on `Tensors`. The same rules previously used for `Variables` applies to `Tensors`; [`autograd`](https://pytorch.org/docs/0.4.0/autograd.html) starts tracking history when any input `Tensor` of an operation has `requires_grad=True`. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> x = torch.ones(1) # create a tensor with requires_grad=False (default)\n>>> x.requires_grad\nFalse\n>>> y = torch.ones(1) # another tensor with requires_grad=False\n>>> z = x + y\n>>> # both inputs have requires_grad=False. so does the output\n>>> z.requires_grad\nFalse\n>>> # then autograd won't track this computation. let's verify!\n>>> z.backward()\nRuntimeError: element 0 of tensors does not require grad and does not have a grad_fn\n>>>\n>>> # now create a tensor with requires_grad=True\n>>> w = torch.ones(1, requires_grad=True)\n>>> w.requires_grad\nTrue\n>>> # add to the previous result that has require_grad=False\n>>> total = w + z\n>>> # the total sum now requires grad!\n>>> total.requires_grad\nTrue\n>>> # autograd can compute the gradients as well\n>>> total.backward()\n>>> w.grad\ntensor([ 1.])\n>>> # and no computation is wasted to compute gradients for x, y and z, which don't require grad\n>>> z.grad == x.grad == y.grad == None\nTrue\n```\n\n#### Manipulating `requires_grad` flag", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "Other than directly setting the attribute, you can change this flag `in-place` using [`my_tensor.requires_grad_()`](https://pytorch.org/docs/0.4.0/tensors.html#torch.Tensor.requires_grad_), or, as in the above example, at creation time by passing it in as an argument (default is `False`), e.g.,\n\n```python\n>>> existing_tensor.requires_grad_()\n>>> existing_tensor.requires_grad\nTrue\n>>> my_tensor = torch.zeros(3, 4, requires_grad=True)\n>>> my_tensor.requires_grad\nTrue\n```\n\n### What about `.data?`\n\n`.data` was the primary way to get the underlying `Tensor` from a `Variable`. After this merge, calling `y = x.data` still has similar semantics. So `y` will be a `Tensor` that shares the same data with `x`, is unrelated with the computation history of `x`, and has `requires_grad=False`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "However, `.data` can be unsafe in some cases. Any changes on `x.data` wouldn't be tracked by `autograd`, and the computed gradients would be incorrect if `x` is needed in a backward pass. A safer alternative is to use [`x.detach()`](https://pytorch.org/docs/master/autograd.html#torch.Tensor.detach), which also returns a `Tensor` that shares data with `requires_grad=False`, but will have its in-place changes reported by `autograd` if `x` is needed in backward.\n\nHere is an example of the difference between `.data` and `x.detach()` (and why we recommend using `detach` in general).\n\nIf you use `Tensor.detach()`, the gradient computation is guaranteed to be correct.\n\n```python\n>>> a = torch.tensor([1,2,3.], requires_grad = True)\n>>> out = a.sigmoid()\n>>> c = out.detach()\n>>> c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out # modified by c.zero_() !!\ntensor([ 0., 0., 0.])", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": ">>> out.sum().backward() # Requires the original value of out, but that was overwritten by c.zero_()\nRuntimeError: one of the variables needed for gradient computation has been modified by an\n```\n\nHowever, using `Tensor.data` can be unsafe and can easily result in incorrect gradients when a tensor is required for gradient computation but modified in-place.\n\n```python\n>>> a = torch.tensor([1,2,3.], requires_grad = True)\n>>> out = a.sigmoid()\n>>> c = out.data\n>>> c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out # out was modified by c.zero_()\ntensor([ 0., 0., 0.])\n\n>>> out.sum().backward()\n>>> a.grad # The result is very, very wrong because `out` changed!\ntensor([ 0., 0., 0.])\n```\n\n## Support for 0-dimensional (scalar) Tensors", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "Previously, indexing into a `Tensor` vector (1-dimensional tensor) gave a Python number but indexing into a `Variable` vector gave (inconsistently!) a vector of size `(1,)`! Similar behavior existed with reduction functions, e.g. `tensor.sum()` would return a Python number, but `variable.sum()` would return a vector of size `(1,)`.\n\nFortunately, this release introduces proper scalar (0-dimensional tensor) support in PyTorch! Scalars can be created using the new `torch.tensor` function (which will be explained in more detail later; for now just think of it as the PyTorch equivalent of `numpy.array`). Now you can do things like:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> torch.tensor(3.1416) # create a scalar directly\ntensor(3.1416)\n>>> torch.tensor(3.1416).size() # scalar is 0-dimensional\ntorch.Size([])\n>>> torch.tensor([3]).size() # compare to a vector of size 1\ntorch.Size([1])\n>>>\n>>> vector = torch.arange(2, 6) # this is a vector\n>>> vector\ntensor([ 2., 3., 4., 5.])\n>>> vector.size()\ntorch.Size([4])\n>>> vector[3] # indexing into a vector gives a scalar\ntensor(5.)\n>>> vector[3].item() # .item() gives the value as a Python number\n5.0\n>>> mysum = torch.tensor([2, 3]).sum()\n>>> mysum\ntensor(5)\n>>> mysum.size()\ntorch.Size([])\n```\n\n### Accumulating losses\n\nConsider the widely used pattern `total_loss += loss.data[0]`. Before 0.4.0. `loss` was a `Variable` wrapping a tensor of size `(1,)`, but in 0.4.0 `loss` is now a scalar and has `0` dimensions. Indexing into a scalar doesn't make sense (it gives a warning now, but will be a hard error in 0.5.0). Use `loss.item()` to get the Python number from a scalar.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "Note that if you don't convert to a Python number when accumulating losses, you may find increased memory usage in your program. This is because the right-hand-side of the above expression used to be a Python float, while it is now a zero-dim Tensor. The total loss is thus accumulating Tensors and their gradient history, which may keep around large autograd graphs for much longer than necessary.\n\n## Deprecation of volatile flag\n\nThe `volatile` flag is now deprecated and has no effect. Previously, any computation that involves a `Variable` with `volatile=True` wouldn't be tracked by `autograd`. This has now been replaced by a [set of more flexible context managers](https://pytorch.org/docs/0.4.0/torch.html#locally-disabling-gradient-computation) including `torch.no_grad()`, `torch.set_grad_enabled(grad_mode)`, and others.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> x = torch.zeros(1, requires_grad=True)\n>>> with torch.no_grad():\n... y = x * 2\n>>> y.requires_grad\nFalse\n>>>\n>>> is_train = False\n>>> with torch.set_grad_enabled(is_train):\n... y = x * 2\n>>> y.requires_grad\nFalse\n>>> torch.set_grad_enabled(True) # this can also be used as a function\n>>> y = x * 2\n>>> y.requires_grad\nTrue\n>>> torch.set_grad_enabled(False)\n>>> y = x * 2\n>>> y.requires_grad\nFalse\n```\n\n## [`dtypes`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype), [`devices`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) and NumPy-style creation functions", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "In previous versions of PyTorch, we used to specify data type (e.g. float vs double), device type (cpu vs cuda) and layout (dense vs sparse) together as a \"tensor type\". For example, `torch.cuda.sparse.DoubleTensor` was the `Tensor` type representing the `double` data type, living on CUDA devices, and with [COO sparse tensor](https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)) layout.\n\nIn this release, we introduce [`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype), [`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) and [`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout) classes to allow better management of these properties via NumPy-style creation functions.\n\n### [`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "Below is a complete list of available [`torch.dtype`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.dtype)s (data types) and their corresponding tensor types.\n\n| Data | `type torch.dtype` | Tensor types |\n|------|------------------|--------------|\n| 32-bit floating point | `torch.float32` or `torch.float` | `torch.*.FloatTensor`\n| 64-bit floating point | `torch.float64` or `torch.double` | `torch.*.DoubleTensor`\n| 16-bit floating point | `torch.float16` or `torch.half` | `torch.*.HalfTensor`\n| 8-bit integer (unsigned) | `torch.uint8` | `torch.*.ByteTensor`\n| 8-bit integer (signed) | `torch.int8` | `torch.*.CharTensor`\n| 16-bit integer (signed) | `torch.int16` or `torch.short` | `torch.*.ShortTensor`\n| 32-bit integer (signed) | `torch.int32` or `torch.int` | `torch.*.IntTensor`\n| 64-bit integer (signed) | `torch.int64` or `torch.long` | `torch.*.LongTensor`\n\nThe dtype of a tensor can be access via its `dtype` attribute.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "### [`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device)\n\nA [`torch.device`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) contains a device type (`'cpu'` or `'cuda'`) and optional device ordinal (id) for the device type. It can be initialized with `torch.device('{device_type}')` or `torch.device('{device_type}:{device_ordinal}')`.\n\nIf the device ordinal is not present, this represents the current device for the device type; e.g., `torch.device('cuda')` is equivalent to `torch.device('cuda:X')` where `X` is the result of `torch.cuda.current_device()`.\n\nThe device of a tensor can be accessed via its `device` attribute.\n\n### [`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "[`torch.layout`](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.layout) represents the data layout of a [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html). Currently `torch.strided` (dense tensors, the default) and `torch.sparse_coo` (sparse tensors with COO format) are supported.\n\nThe layout of a tensor can be access via its `layout` attribute.\n\n### Creating Tensors\n\n[Methods that create a](https://pytorch.org/docs/0.4.0/torch.html#creation-ops) [`Tensor`](https://pytorch.org/docs/0.4.0/tensors.html) now also take in `dtype`, `device`, `layout`, and `requires_grad` options to specify the desired attributes on the returned `Tensor`. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> device = torch.device(\"cuda:1\")\n>>> x = torch.randn(3, 3, dtype=torch.float64, device=device)\ntensor([[-0.6344, 0.8562, -1.2758],\n [ 0.8414, 1.7962, 1.0589],\n [-0.1369, -1.0462, -0.4373]], dtype=torch.float64, device='cuda:1')\n>>> x.requires_grad # default is False\nFalse\n>>> x = torch.zeros(3, requires_grad=True)\n>>> x.requires_grad\nTrue\n```\n\n##### [`torch.tensor(data, ...)`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor)", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "[`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) is one of the newly added [tensor creation methods](https://pytorch.org/docs/0.4.0/torch.html#creation-ops). It takes in array-like data of all kinds and copies the contained values into a new `Tensor`. As mentioned earlier, [`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) is the PyTorch equivalent of NumPy's `numpy.array`constructor. Unlike the `torch.*Tensor` methods, you can also create zero-dimensional `Tensor`s (aka scalars) this way (a single python number is treated as a Size in the `torch.*Tensor methods`). Moreover, if a `dtype` argument isn't given, it will infer the suitable `dtype` given the data. It is the recommended way to create a tensor from existing data like a Python list. For example,", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> cuda = torch.device(\"cuda\")\n>>> torch.tensor([[1], [2], [3]], dtype=torch.half, device=cuda)\ntensor([[ 1],\n [ 2],\n [ 3]], device='cuda:0')\n>>> torch.tensor(1) # scalar\ntensor(1)\n>>> torch.tensor([1, 2.3]).dtype # type inferece\ntorch.float32\n>>> torch.tensor([1, 2]).dtype # type inferece\ntorch.int64\n```\n\nWe've also added more tensor creation methods. Some of them have `torch.*_like` and/or `tensor.new_*` variants.\n\n- `torch.*_like` takes in an input `Tensor` instead of a shape. It returns a `Tensor` with same attributes as the input `Tensor` by default unless otherwise specified:\n\n ```python\n >>> x = torch.randn(3, dtype=torch.float64)\n >>> torch.zeros_like(x)\n tensor([ 0., 0., 0.], dtype=torch.float64)\n >>> torch.zeros_like(x, dtype=torch.int)\n tensor([ 0, 0, 0], dtype=torch.int32)\n ```\n\n- `tensor.new_*` can also create `Tensors` with same attributes as `tensor`, but it always takes in a shape argument:", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "```python\n >>> x = torch.randn(3, dtype=torch.float64)\n >>> x.new_ones(2)\n tensor([ 1., 1.], dtype=torch.float64)\n >>> x.new_ones(4, dtype=torch.int)\n tensor([ 1, 1, 1, 1], dtype=torch.int32)\n ```\n\nTo specify the desired shape, you can either use a tuple (e.g., `torch.zeros((2, 3))`) or variable arguments (e.g., `torch.zeros(2, 3)`) in most cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "| Name | Returned `Tensor` | `torch.*_like` variant | `tensor.new_*` variant |\n|------|-----------------|----------------------|----------------------|\n| [`torch.empty`](https://pytorch.org/docs/0.4.0/torch.html#torch.empty) | uninitialized memory | \u2714 | \u2714 |\n| [`torch.zeros`](https://pytorch.org/docs/0.4.0/torch.html#torch.zeros) | all zeros | \u2714 | \u2714 |\n| [`torch.ones`](https://pytorch.org/docs/0.4.0/torch.html#torch.ones) | all ones | \u2714 | \u2714 |\n| [`torch.full`](https://pytorch.org/docs/0.4.0/torch.html#torch.full) | filled with a given value | \u2714 | \u2714 |\n| [`torch.rand`](https://pytorch.org/docs/0.4.0/torch.html#torch.rand) | i.i.d. continuous Uniform[0, 1) | \u2714 |\n| [`torch.randn`](https://pytorch.org/docs/0.4.0/torch.html#torch.randn) | i.i.d. `Normal(0, 1)` | \u2714 |\n| [`torch.randint`](https://pytorch.org/docs/0.4.0/torch.html#torch.randint) | i.i.d. discrete Uniform in given range | \u2714 |\n| [`torch.randperm`](https://pytorch.org/docs/0.4.0/torch.html#torch.randperm) | random permutation of `{0, 1, ..., n - 1}` |\n| [`torch.tensor`](https://pytorch.org/docs/0.4.0/torch.html#torch.tensor) | copied from existing data (list, NumPy ndarray, etc.) | | \u2714 |\n| [`torch.from_numpy`*](https://pytorch.org/docs/0.4.0/torch.html#torch.from_numpy) | from NumPy `ndarray` (sharing storage without copying) |\n| [`torch.arange`](https://pytorch.org/docs/0.4.0/torch.html#torch.arange), [`torch.range`](https://pytorch.org/docs/0.4.0/torch.html#torch.range), and [`torch.linspace`](https://pytorch.org/docs/0.4.0/torch.html#torch.linspace) | uniformly spaced values in a given range |\n| [`torch.logspace`](https://pytorch.org/docs/0.4.0/torch.html#torch.logspace) | logarithmically spaced values in a given range |\n| [`torch.eye`](https://pytorch.org/docs/0.4.0/torch.html#torch.eye) | identity matrix |", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "*: [`torch.from_numpy`](https://pytorch.org/docs/0.4.0/torch.html#torch.from_numpy) only takes in a NumPy `ndarray` as its input argument.\n\n\n## Writing device-agnostic code\n\nPrevious versions of PyTorch made it difficult to write code that was device agnostic (i.e. that could run on both CUDA-enabled and CPU-only machines without modification).\n\nPyTorch 0.4.0 makes this easier in two ways:\n\n- The `device` attribute of a Tensor gives the [torch.device](https://pytorch.org/docs/0.4.0/tensor_attributes.html#torch.torch.device) for all Tensors (`get_device` only works for CUDA tensors)\n- The `to` method of `Tensors` and `Modules` can be used to easily move objects to different devices (instead of having to call `cpu()` or `cuda()` based on the context)\n\nWe recommend the following pattern:\n\n```python\n# at beginning of the script\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n...", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "...\n\n# then whenever you get a new Tensor or Module\n# this won't copy if they are already on the desired device\ninput = data.to(device)\nmodel = MyModule(...).to(device)\n```\n\n## New edge-case constraints on names of submodules, parameters, and buffers in `nn.Module`\n\n`name` that is an empty string or contains `\".\"` is no longer permitted in `module.add_module(name, value)`, `module.add_parameter(name, value)` or `module.add_buffer(name, value)` because such names may cause lost data in the `state_dict`. If you are loading a checkpoint for modules containing such names, please update the module definition and patch the `state_dict` before loading it.\n\n## Code Samples (Putting it all together)\n\nTo get a flavor of the overall recommended changes in 0.4.0, let's look at a quick example for a common code pattern in both 0.3.1 and 0.4.0:\n\n- 0.3.1 (old):\n ```python\n model = MyRNN()\n if use_cuda:\n model = model.cuda()", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "# train\n total_loss = 0\n for input, target in train_loader:\n input, target = Variable(input), Variable(target)\n hidden = Variable(torch.zeros(*h_shape)) # init hidden\n if use_cuda:\n input, target, hidden = input.cuda(), target.cuda(), hidden.cuda()\n ... # get loss and optimize\n total_loss += loss.data[0]\n\n # evaluate\n for input, target in test_loader:\n input = Variable(input, volatile=True)\n if use_cuda:\n ...\n ...\n ```\n\n- 0.4.0 (new):\n ```python\n # torch.device object used throughout this script\n device = torch.device(\"cuda\" if use_cuda else \"cpu\")\n\n model = MyRNN().to(device)\n\n # train\n total_loss = 0\n for input, target in train_loader:\n input, target = input.to(device), target.to(device)\n hidden = input.new_zeros(*h_shape) # has the same device & dtype as `input`\n ... # get loss and optimize\n total_loss += loss.item() # get Python number from 1-element Tensor", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "# evaluate\n with torch.no_grad(): # operations inside don't track history\n for input, target in test_loader:\n ...\n ```\n\nThank you for reading! Please refer to our [documentation](https://pytorch.org/docs/0.4.0/index.html) and [release notes](https://github.com/pytorch/pytorch/releases/tag/v0.4.0) for more details.\n\nHappy PyTorch-ing!", "metadata": {"source": "https://pytorch.org/blog/pytorch-0_4_0-migration-guide/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Efficient Large-Scale Training with Pytorch FSDP and AWS\"\nauthor: Less Wright, Hamid Shojanazeri, Geeta Chauhan\nfeatured-img: \"assets/images/largeblog_index_1.png\"\n---\n\nCutting-edge AI models are becoming extremely large. The cost and overhead of training these models is increasing rapidly, and involves large amounts of engineering and guesswork to find the right training regime. FSDP reduces these costs significantly by enabling you to train much larger models with the same amount of resources. FSDP lowers the memory footprint on your GPUs, and is usable via a lightweight configuration that requires substantially less effort, typically with just a few lines of code.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The main performance gains in FSDP come from maximizing the overlap between network communication and model computation, and eliminating the memory redundancy inherent in traditional data parallel training (DDP). PyTorch FSDP can train models approximately 4x larger on the same server resources as DDP and 20x larger if we combine activation checkpointing and activation offloading.\n\nSince PyTorch 1.12, FSDP is now in beta status, and has added a number of new features that can be tuned to further accelerate your model training. \n\nIn this series of blog posts, we will explain multiple performance optimizations you can run with FSDP to boost your distributed training speed and model sizes within the context of your available server resources. We use the HuggingFace T5 3B, 11B and DeepVit, in fine-tuning mode, as the running examples throughout the series.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "As a preview of some of the optimizations discussed in this series, we show the before and after performance scaled in Flops below (Note that these results can vary based on your server resources and model architecture). \n\n

\n\n

\n\n *T5 3B Performance measured on AWS A100 and A10 servers. Original with no optimizations and Tuned with the applied optimization \n\n

\n\n

\n\n *T5 11B Performance measured on A100 servers. Original with no optimizations and Tuned with the applied optimization ", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} {"page_content": "In this first post, we will provide a quick overview of FSDP and how it can make training large- scale AI models more efficient. We will highlight briefly the multiple performance options available, and dive deeper into the details on these in upcoming posts. We will then conclude with an overview on how to leverage AWS parallel cluster for large- scale training with FSDP.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Optimization \n T5 Model \n Throughput Improvement \n
Mixed Precision\n 3 B\n 5x\n
11 B\n 10x\n
Activation Checkpointing (AC)\n 3 B\n 10x\n
11 B\n 100x", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "
Transformer Wrapping Policy\n 3 B\n 2x\n
11 B\n Unable to run the experiment without the Transformer wrapping policy.\n
Full Shard Strategy\n 3 B\n 1.5x\n
11 B\n Not able to run with Zero2\n
", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_Performance optimization gains on T5 models over non-optimized._", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In our experiments with the T5 3B model, using the [transformer wrapping policy](https://www.youtube.com/watch?v=HQeKwCsnH4k&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=2) resulted in >2x higher throughput measured in TFLOPS versus the default wrapping policy. [Activation checkpointing](https://www.youtube.com/watch?v=5B4d0FuxSQc&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=3) resulted in 10x improvement by reinvesting the freed memory from the checkpoints into larger batch size. [Mixed", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "precision](https://www.youtube.com/watch?v=-caN92JtKqA&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=4) with BFloat16 resulted in ~5x improvement versus FP32 and finally the [full sharding strategy](https://www.youtube.com/watch?v=a3iW6Cggccw&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=5) versus zero2 (DDP) resulted in 1.5x improvement.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We ran similar experiments for a larger model, T5 11B, but the larger model size resulted in some changes to the experiment space. Specifically, we found that two optimizations, transformer wrapping policy and activation checkpointing, were needed to enable us to run these experiments on 3 nodes (each node had 8 A100 gpus with 80 GB of memory). With these optimizations, we could fit a batch size of 50 and get higher throughput compared to removing each one of them. Thus rather than running on/off solely", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "for a single optimization test as with the 3B model, the larger model experiments were done with 1 of 3 optimizations turned on/off while always running the other two in order to allow a usable batch size for both test states for each item.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Based on TFLOP comparisons, with the 11B model, we saw even more payoff from the optimizations. Mixed precision(~10x improvement) and activation checkpointing (~100x improvement) had a much larger impact with the 11B model compared to the 3B parameter model. With mixed precision we could fit ~2x larger batch sizes and with activation checkpointing >15x batch sizes (from 3 with no activation checkpointing to 50 with activation checkpointing) which translated into large throughput improvements.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We also have observed that for these larger models > 3B, using Zero2 sharding strategy would result in minimal room left in memory for the batch data, and had to go with very small batch sizes (e.g 1-2) that essentially makes full sharding strategy a necessity to enable fitting larger batches sizes.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_Note - this tutorial assumes a basic understanding of FSDP. To learn more about basics of FSDP please refer to the [getting started](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html) and [advanced FSDP ](https://pytorch.org/tutorials/intermediate/FSDP_adavnced_tutorial.html)tutorials._\n\n**What is FSDP? How does it make Large-Scale Training More Efficient**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**FSDP** expands upon distributed data parallel, by parallelizing not just data, but the model parameters, the optimizer states and gradients associated with the model. Specifically - **each** **GPU only stores a subset of the entire model** **and the associated subset of optimizer states and gradients.**\n\n_To show the evolution of distributed training, we can start from the beginning, where AI models were simply trained on a single GPU._", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "DDP (Distributed Data Parallel) was the initial step up from training with only a single GPU, and was an effort to address the data and model size growth, where multiple GPUs each housed their own copy of the same model. The gain here is that the data for each batch could be split and processed independently on each GPU, all at the same time,thus parallelizing the processing of the data set and increasing training speed by the increasing number of GPUs. The tradeoff is the need to communicate the gradients", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "between each GPU to synchronize the models after the backward pass.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "FSDP expands on scaling models by removing the redundancy of optimizer calculations and state storage, as well as gradient and memory storage of model parameters that are present in DDP (DDP = Distributed Data Parallel). This redundancy reduction, along with increased communication overlap where model parameter communication takes place at the same time as model computation, is what allows FSDP to train much larger models with the same resources as DDP.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "A key point is that this efficiency also allows for AI models that are larger than a single GPU to be trained. The model size available for training is now increased to the aggregate memory of all GPUs, rather than the size of a single GPU. (And as a point of note, FSDP can go beyond aggregated GPU memory by leveraging CPU memory as well, though we will not directly cover this aspect here).", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "As discussed in a previous [blog post](https://medium.com/pytorch/pytorch-data-parallel-best-practices-on-google-cloud-6c8da2be180d), with DDP the largest model that we could train on 32, A100 gpus with 40 GB memory (4 nodes) was up to 3B parameters, and batch size of 128, with the help of activation checkpointing. By contrast, using FSDP we were able to train up to 81B model size, combining activation checkpointing, along with activation and parameter offloading. In another", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[experiment](https://medium.com/pytorch/training-a-1-trillion-parameter-model-with-pytorch-fully-sharded-data-parallel-on-aws-3ac13aa96cff), we benchmarked a 1T parameter model with FSDP using 512 gpus.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\nFor intuition on the parameter level workings of FSDP, below we show an animation detailing how the model parameters are sharded and communicated assuming a two GPU scenario and a simple 8 parameter model:\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_Above - the animations walk through the steps involved with the initial sharding of the model amongst ranks, and we start the `all_gathers` and forward pass_\n\n

\n\n

\n\n_We continue through the model with the forward pass. After each FSDP unit completes, non-locally owned params are dropped to free memory, and optionally activations can be checkpointed. This continues until we finish the forward pass and compute the loss._", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n_During the backward pass, another `all_gather` is used to load the parameters and the gradients are computed. These gradients are then `reduce_scattered` so that the local owners of each param can aggregate and prepare to update the weights._\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_Finally, each rank passes the summed gradients through the optimizer states and updates the weights to complete the mini-batch._\n\nWith the model now distributed across the entire set of available GPUs, the logical question is how data moves through the model given this sharding of model parameters.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This is accomplished by FSDP coordinating with all GPUs to effectively share (communicate) the respective parts of the model. The model is decomposed into FSDP units and parameters within each unit are flattened and then sharded across all GPUs. Within each FSDP unit, GPU\u2019s are assigned interleaving ownership of individual model parameters.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "By interleaving, we mean the following - assuming 2 gpus with an id of 1 and 2, the FSDP unit ownership pattern would be [12121212], rather than a contiguous chunk of [111222].\n\nDuring training, an `all_gather` is initiated and the locally owned model parameters within a FSDP unit are shared by the owner GPU with the other non-owners, when they need it, on a \u2018just in time\u2019 type basis. FSDP prefetches parameters to overlap `all_gather` communication with computation.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "When those requested parameters arrive, the GPU uses the delivered parameters, in combination with the parameters it already owns, to create a fully populated FSDP unit. Thus there is a moment where each GPU hits peak memory usage while holding a fully populated FSDP unit.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Optimization \n T5 Model \n Throughput Improvement \n
Mixed Precision\n 3 B\n 5x\n
11 B\n 10x\n
Activation Checkpointing (AC)\n 3 B\n 10x\n
11 B\n 100x\n
Transformer Wrapping Policy\n 3 B\n 2x\n
11 B\n Unable to run the experiment without the Transformer wrapping policy.\n
Full Shard Strategy\n 3 B\n 1.5x\n
11 B\n Not able to run with Zero2\n
", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "_Performance optimization gains on T5 models over non-optimized._\n\nIn our experiments with the T5 3B model, using the [transformer wrapping policy](https://www.youtube.com/watch?v=HQeKwCsnH4k&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=2) resulted in >2x higher throughput measured in TFLOPS versus the default wrapping policy. [Activation checkpointing](https://www.youtube.com/watch?v=5B4d0FuxSQc&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=3) resulted in 10x improvement by reinvesting the freed memory from the checkpoints into larger batch size. [Mixed precision](https://www.youtube.com/watch?v=-caN92JtKqA&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=4) with BFloat16 resulted in ~5x improvement versus FP32 and finally the [full sharding strategy](https://www.youtube.com/watch?v=a3iW6Cggccw&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=5) versus zero2 (DDP) resulted in 1.5x improvement.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We ran similar experiments for a larger model, T5 11B, but the larger model size resulted in some changes to the experiment space. Specifically, we found that two optimizations, transformer wrapping policy and activation checkpointing, were needed to enable us to run these experiments on 3 nodes (each node had 8 A100 gpus with 80 GB of memory). With these optimizations, we could fit a batch size of 50 and get higher throughput compared to removing each one of them. Thus rather than running on/off solely for a single optimization test as with the 3B model, the larger model experiments were done with 1 of 3 optimizations turned on/off while always running the other two in order to allow a usable batch size for both test states for each item.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Based on TFLOP comparisons, with the 11B model, we saw even more payoff from the optimizations. Mixed precision(~10x improvement) and activation checkpointing (~100x improvement) had a much larger impact with the 11B model compared to the 3B parameter model. With mixed precision we could fit ~2x larger batch sizes and with activation checkpointing >15x batch sizes (from 3 with no activation checkpointing to 50 with activation checkpointing) which translated into large throughput improvements.\n\nWe also have observed that for these larger models > 3B, using Zero2 sharding strategy would result in minimal room left in memory for the batch data, and had to go with very small batch sizes (e.g 1-2) that essentially makes full sharding strategy a necessity to enable fitting larger batches sizes.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "_Note - this tutorial assumes a basic understanding of FSDP. To learn more about basics of FSDP please refer to the [getting started](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html) and [advanced FSDP ](https://pytorch.org/tutorials/intermediate/FSDP_adavnced_tutorial.html)tutorials._\n\n**What is FSDP? How does it make Large-Scale Training More Efficient**\n\n**FSDP** expands upon distributed data parallel, by parallelizing not just data, but the model parameters, the optimizer states and gradients associated with the model. Specifically - **each** **GPU only stores a subset of the entire model** **and the associated subset of optimizer states and gradients.**\n\n_To show the evolution of distributed training, we can start from the beginning, where AI models were simply trained on a single GPU._", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "DDP (Distributed Data Parallel) was the initial step up from training with only a single GPU, and was an effort to address the data and model size growth, where multiple GPUs each housed their own copy of the same model. The gain here is that the data for each batch could be split and processed independently on each GPU, all at the same time,thus parallelizing the processing of the data set and increasing training speed by the increasing number of GPUs. The tradeoff is the need to communicate the gradients between each GPU to synchronize the models after the backward pass.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "FSDP expands on scaling models by removing the redundancy of optimizer calculations and state storage, as well as gradient and memory storage of model parameters that are present in DDP (DDP = Distributed Data Parallel). This redundancy reduction, along with increased communication overlap where model parameter communication takes place at the same time as model computation, is what allows FSDP to train much larger models with the same resources as DDP.\n\nA key point is that this efficiency also allows for AI models that are larger than a single GPU to be trained. The model size available for training is now increased to the aggregate memory of all GPUs, rather than the size of a single GPU. (And as a point of note, FSDP can go beyond aggregated GPU memory by leveraging CPU memory as well, though we will not directly cover this aspect here).", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "As discussed in a previous [blog post](https://medium.com/pytorch/pytorch-data-parallel-best-practices-on-google-cloud-6c8da2be180d), with DDP the largest model that we could train on 32, A100 gpus with 40 GB memory (4 nodes) was up to 3B parameters, and batch size of 128, with the help of activation checkpointing. By contrast, using FSDP we were able to train up to 81B model size, combining activation checkpointing, along with activation and parameter offloading. In another [experiment](https://medium.com/pytorch/training-a-1-trillion-parameter-model-with-pytorch-fully-sharded-data-parallel-on-aws-3ac13aa96cff), we benchmarked a 1T parameter model with FSDP using 512 gpus.\n\n

\n\n

\n\nFor intuition on the parameter level workings of FSDP, below we show an animation detailing how the model parameters are sharded and communicated assuming a two GPU scenario and a simple 8 parameter model:", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n\n

\n\n\n_Above - the animations walk through the steps involved with the initial sharding of the model amongst ranks, and we start the `all_gathers` and forward pass_\n\n

\n\n

\n\n_We continue through the model with the forward pass. After each FSDP unit completes, non-locally owned params are dropped to free memory, and optionally activations can be checkpointed. This continues until we finish the forward pass and compute the loss._\n\n

\n\n

\n\n_During the backward pass, another `all_gather` is used to load the parameters and the gradients are computed. These gradients are then `reduce_scattered` so that the local owners of each param can aggregate and prepare to update the weights._\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "_Finally, each rank passes the summed gradients through the optimizer states and updates the weights to complete the mini-batch._\n\nWith the model now distributed across the entire set of available GPUs, the logical question is how data moves through the model given this sharding of model parameters.\n\nThis is accomplished by FSDP coordinating with all GPUs to effectively share (communicate) the respective parts of the model. The model is decomposed into FSDP units and parameters within each unit are flattened and then sharded across all GPUs. Within each FSDP unit, GPU\u2019s are assigned interleaving ownership of individual model parameters.\n\nBy interleaving, we mean the following - assuming 2 gpus with an id of 1 and 2, the FSDP unit ownership pattern would be [12121212], rather than a contiguous chunk of [111222].", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "During training, an `all_gather` is initiated and the locally owned model parameters within a FSDP unit are shared by the owner GPU with the other non-owners, when they need it, on a \u2018just in time\u2019 type basis. FSDP prefetches parameters to overlap `all_gather` communication with computation. \n\nWhen those requested parameters arrive, the GPU uses the delivered parameters, in combination with the parameters it already owns, to create a fully populated FSDP unit. Thus there is a moment where each GPU hits peak memory usage while holding a fully populated FSDP unit.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} {"page_content": "It then processes the data through the FSDP unit, and drops the parameters it received from other GPU\u2019s to free up memory for the next unit\u2026the process continues over and over proceeding through the entire model to complete the forward pass.The process is then repeated (in general) for the backward pass.(note - this is a simplified version for understanding..there is additional complexity but this should help construct a basic mental model of the FSDP process).", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "This eliminates much of the memory redundancy present in DDP, but imposes the cost of higher amounts of network communication to shuttle these requested parameters back and forth amongst all the GPUs.**Overlapping the communication timing with the computation taking place is the basis of many of the performance improvements we\u2019ll discuss in this series.** The key gains are frequently based on the fact that communication can often take place at the same time as computation.As you can surmise, **having high", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "communication speed is vital for FSDP performance.**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "**How do I optimize my training with FSDP?**\n\nThere are four main performance improvements we will cover - the transformer wrapper, activation checkpointing, mixed precision, and selecting the proper sharding strategy. The flowchart below will help as a checklist for tuning options that we will discuss in this post.\n\n

\n\n

\n\n**Wrapping policy - _for transformers, use Transformer wrapping policy_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The first performance optimization is leveraging the FSDP transformer wrapper for transformer models.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "One of the pre-defined wrapping policy is `size_based_autowrap_policy`. With `size_based_autowrap_policy`, FSDP will traverse the module structure from bottom to top, a new FSDP unit will be created once the current unit has at least the `min_num_params` specified within the size policy (this defaults to 1e8, or 100M). If the module can not be created as an FSDP unit, FSDP will continue to check its parent module. This size based wrapping policy may not be ideal for some model structures, PyTorch", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "distributed team is actively working on a new default wrapping policy in the next release which is based on size and also module execution order, users can simply tune the size and achieve the optimized performance.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the current release, you can greatly improve your performance when running Transformer models by using the \u2018transformer wrapper\u2019. You will need to provide the appropriate layer class for your model. Here, layer class is the class that houses the Multi-Head Attention and Feed Forward Network.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "FSDP will then form the FSDP units around the layer class rather than arbitrary breaks based on parameter size. By sharding the model around layer classes that are uniformly repeated within the transformer, FSDP can create uniform FSDP units that better balance the overlap of computation and communication. By contrast, size based wrapping can produce very uneven or skewed shards for models, which then have uneven matching of compute vs communication overlap. As discussed earlier, the main driver of FSDP", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "high performance is the overlap of communication and computation, and hence why the Transformer wrapper provides improved performance. Note that the Transformer wrapper can also be used for non-transformer models if these models have a list of uniform layers.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Let\u2019s compare the performance difference on a T5, 3B parameter model when running under the default wrapper and the transformer wrapper.\n\nFor default wrapping, we don\u2019t need to take any action - we simply pass the model to FSDP as shown:\n\n```python\nmodel = FSDP(\n model,\n device_id=torch.cuda.current_device(),\n )", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this case FSDP will simply wrap the whole model in a single FSDP unit.\n\nRunning on an [NVIDIA A100-SXM4\u201340GB](https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Center/a100/pdf/nvidia-a100-datasheet-us-nvidia-1758950-r4-web.pdf) with 8 GPUs, we are able to reach 2.3 TFlops and 95% GPU memory utilization with a batch size of 14.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "However, since T5 is a transformer model, we are better served to leverage the transformer wrapper for this model. \n \nTo use that, we need to isolate the layer class for the transformer, and then pass it in to create our transformer wrapper. \n\n```python\nfrom transformers.models.t5.modeling_t5 import T5Block", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "And now we can create our Transformer wrapper: \n\n```python\ntransformer_auto_wrapper_policy = functools.partial(\n transformer_auto_wrap_policy,\n transformer_layer_cls={\n T5Block, # < ---- Your Transformer layer class\n },\n )", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "With our model aware wrapper ready, we can initialize FSDP:\n\n```python\n# invoke FSDP with your transformer wrapper policy:\nmodel = FSDP(\n model,\n auto_wrap_policy=transformer_auto_wrapper_policy,\n device_id=torch.cuda.current_device(), # streaming init\n )", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Running this wrapped model, we can see some substantial performance gains.We can fit nearly double the batch size, going to 28, and with better memory and communication efficiency, we see a TFlops increase to 5.07 from 2.3.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Thus, we\u2019ve increased our training throughput by over 200% (2.19x) due to providing greater model info to FSDP! The transformer wrapping policy results in more fine-grained and balanced FSDP units each holding a layer class, which leads to a more effective communication-computation overlap.\n\n

\n\n

\n\n_Above: Graphical comparison of TFlops based on wrapper type_", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you are training a Transformer model, it pays to configure your training with FSDP using the transformer wrapper. For more information on how to isolate your layer class, please see our in depth video on Transformer wrapping [here](https://www.youtube.com/watch?v=HQeKwCsnH4k), where we walk through a number of transformers showing where the layer class can be found.\n\n**Mixed precision - _use BF16 if you have an Ampere architecture GPU_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "FSDP supports a flexible mixed precision policy that gives you granular control over parameters, gradients and buffer data types. This lets you easily leverage BFloat16 or FP16 to increase your training speed by up to 70%. \n\n*Note that BFloat 16 is only available on Ampere type GPUs. On AWS this is available with p4dn and g5 instances.\n\nBy way of comparison, we can show a 77% speed improvement when comparing fully tuned BFloat16 vs FP32 on an 8B DeepVit model.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\nWe have obtained even greater acceleration using BFloat16 in fine-tuning a 3B HuggingFace T5 model as shown in the figures below. We observed that because of the lower precision the validation loss of BFloat16 is slightly behind in the first few epochs, but it is able to catch up and results in the same final accuracy as FP32.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "To use mixed precision, we create a policy with our desired data types, and pass it in during the FSDP initialization.\n\nTo create our policy, we need to import the MixedPrecision class, and then define our custom policy using our customized class:", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.distributed.fsdp import MixedPrecision\nbfSixteen = MixedPrecision(\n param_dtype=torch.bfloat16,\n # Gradient communication precision.\n reduce_dtype=torch.bfloat16,\n # Buffer precision.\n buffer_dtype=torch.bfloat16,\n)\nmodel = FSDP(\n model,\n auto_wrap_policy=transformer_auto_wrapper_policy,\n mixed_precision=bfloatPolicy)", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "You can mix and match the precision for parameters, gradients and buffers as you prefer:\n\n```python\ncomboPolicy = MixedPrecision(\n # Param precision\n param_dtype=torch.bfloat16,\n # Gradient communication precision.\n reduce_dtype=torch.float32,\n # Buffer precision.\n buffer_dtype=torch.float32,\n )", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For training with FP16, you will need to also use the ShardedGradScaler, which we will cover in subsequent posts. For BFloat16, it is a drop-in replacement.\n\n**AnyPrecision Optimizer - _going beyond mixed precision with full BF16 training_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "This eliminates much of the memory redundancy present in DDP, but imposes the cost of higher amounts of network communication to shuttle these requested parameters back and forth amongst all the GPUs.**Overlapping the communication timing with the computation taking place is the basis of many of the performance improvements we\u2019ll discuss in this series.** The key gains are frequently based on the fact that communication can often take place at the same time as computation.As you can surmise, **having high communication speed is vital for FSDP performance.**\n\n\n### **How do I optimize my training with FSDP?**\n\nThere are four main performance improvements we will cover - the transformer wrapper, activation checkpointing, mixed precision, and selecting the proper sharding strategy. The flowchart below will help as a checklist for tuning options that we will discuss in this post.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Wrapping policy - _for transformers, use Transformer wrapping policy_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The first performance optimization is leveraging the FSDP transformer wrapper for transformer models. \n \nOne of the pre-defined wrapping policy is `size_based_autowrap_policy`. With `size_based_autowrap_policy`, FSDP will traverse the module structure from bottom to top, a new FSDP unit will be created once the current unit has at least the `min_num_params` specified within the size policy (this defaults to 1e8, or 100M). If the module can not be created as an FSDP unit, FSDP will continue to check its parent module. This size based wrapping policy may not be ideal for some model structures, PyTorch distributed team is actively working on a new default wrapping policy in the next release which is based on size and also module execution order, users can simply tune the size and achieve the optimized performance. \n \nIn the current release, you can greatly improve your performance when running Transformer models by using the \u2018transformer wrapper\u2019. You will need to provide the appropriate layer class for your model. Here, layer class is the class that houses the Multi-Head Attention and Feed Forward Network.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "FSDP will then form the FSDP units around the layer class rather than arbitrary breaks based on parameter size. By sharding the model around layer classes that are uniformly repeated within the transformer, FSDP can create uniform FSDP units that better balance the overlap of computation and communication. By contrast, size based wrapping can produce very uneven or skewed shards for models, which then have uneven matching of compute vs communication overlap. As discussed earlier, the main driver of FSDP high performance is the overlap of communication and computation, and hence why the Transformer wrapper provides improved performance. Note that the Transformer wrapper can also be used for non-transformer models if these models have a list of uniform layers.\n\nLet\u2019s compare the performance difference on a T5, 3B parameter model when running under the default wrapper and the transformer wrapper.\n\nFor default wrapping, we don\u2019t need to take any action - we simply pass the model to FSDP as shown:", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nmodel = FSDP(\n model,\n device_id=torch.cuda.current_device(),\n )\n```\n\n\nIn this case FSDP will simply wrap the whole model in a single FSDP unit.\n\nRunning on an [NVIDIA A100-SXM4\u201340GB](https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Center/a100/pdf/nvidia-a100-datasheet-us-nvidia-1758950-r4-web.pdf) with 8 GPUs, we are able to reach 2.3 TFlops and 95% GPU memory utilization with a batch size of 14.\n\nHowever, since T5 is a transformer model, we are better served to leverage the transformer wrapper for this model. \n \nTo use that, we need to isolate the layer class for the transformer, and then pass it in to create our transformer wrapper. \n\n```python\nfrom transformers.models.t5.modeling_t5 import T5Block\n```\n\nAnd now we can create our Transformer wrapper: \n\n```python\ntransformer_auto_wrapper_policy = functools.partial(\n transformer_auto_wrap_policy,\n transformer_layer_cls={\n T5Block, # < ---- Your Transformer layer class\n },\n )\n```", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "With our model aware wrapper ready, we can initialize FSDP:\n\n```python\n# invoke FSDP with your transformer wrapper policy:\nmodel = FSDP(\n model,\n auto_wrap_policy=transformer_auto_wrapper_policy,\n device_id=torch.cuda.current_device(), # streaming init\n )\n```\n\nRunning this wrapped model, we can see some substantial performance gains.We can fit nearly double the batch size, going to 28, and with better memory and communication efficiency, we see a TFlops increase to 5.07 from 2.3.\n\nThus, we\u2019ve increased our training throughput by over 200% (2.19x) due to providing greater model info to FSDP! The transformer wrapping policy results in more fine-grained and balanced FSDP units each holding a layer class, which leads to a more effective communication-computation overlap.\n\n

\n\n

\n\n_Above: Graphical comparison of TFlops based on wrapper type_", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "If you are training a Transformer model, it pays to configure your training with FSDP using the transformer wrapper. For more information on how to isolate your layer class, please see our in depth video on Transformer wrapping [here](https://www.youtube.com/watch?v=HQeKwCsnH4k), where we walk through a number of transformers showing where the layer class can be found.\n\n**Mixed precision - _use BF16 if you have an Ampere architecture GPU_**\n\nFSDP supports a flexible mixed precision policy that gives you granular control over parameters, gradients and buffer data types. This lets you easily leverage BFloat16 or FP16 to increase your training speed by up to 70%. \n\n*Note that BFloat 16 is only available on Ampere type GPUs. On AWS this is available with p4dn and g5 instances.\n\nBy way of comparison, we can show a 77% speed improvement when comparing fully tuned BFloat16 vs FP32 on an 8B DeepVit model.\n\n

\n\n

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "We have obtained even greater acceleration using BFloat16 in fine-tuning a 3B HuggingFace T5 model as shown in the figures below. We observed that because of the lower precision the validation loss of BFloat16 is slightly behind in the first few epochs, but it is able to catch up and results in the same final accuracy as FP32.\n\n

\n\n

\n\n\nTo use mixed precision, we create a policy with our desired data types, and pass it in during the FSDP initialization.\n\nTo create our policy, we need to import the MixedPrecision class, and then define our custom policy using our customized class:", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torch.distributed.fsdp import MixedPrecision\nbfSixteen = MixedPrecision(\n param_dtype=torch.bfloat16,\n # Gradient communication precision.\n reduce_dtype=torch.bfloat16,\n # Buffer precision.\n buffer_dtype=torch.bfloat16,\n)\nmodel = FSDP(\n model,\n auto_wrap_policy=transformer_auto_wrapper_policy,\n mixed_precision=bfloatPolicy)\n```\n\nYou can mix and match the precision for parameters, gradients and buffers as you prefer:\n\n```python\ncomboPolicy = MixedPrecision(\n # Param precision\n param_dtype=torch.bfloat16,\n # Gradient communication precision.\n reduce_dtype=torch.float32,\n # Buffer precision.\n buffer_dtype=torch.float32,\n )\n```\n\nFor training with FP16, you will need to also use the ShardedGradScaler, which we will cover in subsequent posts. For BFloat16, it is a drop-in replacement.\n\n**AnyPrecision Optimizer - _going beyond mixed precision with full BF16 training_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} {"page_content": "Mixed precision training, both in FSDP and elsewhere, maintains the working weights in the reduced datatype (BF16 or FP16) while keeping the master weights in full FP32. The reason for the master weights in FP32 is that running in pure BF16 will result in \u2018weight stagnation\u2019, where very small weight updates are lost due to the lower precision, and the accuracy flatlines over time while FP32 weights can continue to improve from these small updates.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In order to resolve this dilemma, we can use the new AnyPrecision optimizer available in [TorchDistX](https://github.com/pytorch/torchdistx) (Torch Distributed Experimental) that allows you to successfully train and keep the master weights in pure BF16 instead of FP32. In addition, unlike the typical storage of optimizer states in FP32, AnyPrecision is able to maintain states in pure BF16 as well.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "AnyPrecision enables pure BF16 training by maintaining an extra buffer that tracks the precision lost during the weight updates and re-applies that during the next update\u2026effectively resolving the weight stagnation issue without requiring FP32.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "As a comparison of the throughput gains available with pure BF16 training using AnyPrecision, we ran experiments using FSDP with the T5 11B model with regular FP32 training, Mixed Precision training with BF16, and pure BF16 training using the AnyPrecision optimizer on 3 nodes with A100 gpus as mentioned previously.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\nAs shown above, training with AnyPrecision and pure BF16 resulted in 2x the throughput vs Mixed Precision, and over 20x improvement vs FP32.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The potential tradeoff is the impact on final accuracy - in the cases we tested, the accuracy was equal or better than FP32 due to a regularization effect from the slightly reduced precision, but your results may vary. \n \nAnyPrecision optimizer is available for you to test with [here](https://github.com/pytorch/torchdistx), and is a drop in replacement for AdamW optimizer. \n\n**Activation checkpointing - _increasing throughput by trading compute for memory_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n**FSDP supports activation checkpointing once the model has been sharded**, and makes it easy to implement. The graph above shows ~4x throughput improvement using activation checkpointing.\n\nActivation checkpointing is where the intermediate activations are freed during the forward pass, and a checkpoint is left as a placeholder. This generally increases available GPU memory by over 30%.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The tradeoff is that during the backward pass, these previously removed intermediate activations must be re-calculated again using information in the checkpoint (duplicate compute), but by leveraging the increased GPU memory, one can increase the batch size such that the net throughput can increase substantially.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\n# verify we have FSDP activation support ready by importing:\nfrom torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (\n checkpoint_wrapper,\n CheckpointImpl,\n apply_activation_checkpointing_wrapper,\n)", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "The steps required to implement activation checkpointing is to first import the FSDP checkpointing functions. We need declare our checkpointer wrapper type which is non-reentrant and create a check function to identify which layer to wrap as follows\n\n```python\nnon_reentrant_wrapper = partial(\n checkpoint_wrapper,\n offload_to_cpu=False,\n checkpoint_impl=CheckpointImpl.NO_REENTRANT,\n)\ncheck_fn = lambda submodule: isinstance(submodule, T5Block)", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```python\napply_activation_checkpointing_wrapper(\n model, checkpoint_wrapper_fn=non_reentrant_wrapper, check_fn=check_fn\n )", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_Important note - this must be run after the model has been initialized with FSDP._\n\nHowever, hopefully you\u2019ve seen how some initial tuning with FSDP options can have a large impact on your training performance. \n\nWith that, we turn our attention from how to scale within FSDP, to how to scale your server hardware for FSDP using AWS.\n\n**Large Scale Training with FSDP on AWS - _For multi-node prioritize high speed network_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "AWS provides several services that can be used to run distributed training with FSDP: [Amazon EC2 Accelerated Computing instances](https://aws.amazon.com/ec2/instance-types/#Accelerated_Computing), AWS [ParallelCluster](https://aws.amazon.com/hpc/parallelcluster/), and Amazon [Sagemaker](https://aws.amazon.com/sagemaker/features/?nc=sn&loc=2).", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In this series of blog posts, we used [Amazon EC2 p4d](https://aws.amazon.com/ec2/instance-types/p4/) instances in a single-instance multi-GPU configuration and in a multi-instance configuration using AWS [ParallelCluster](https://aws.amazon.com/hpc/parallelcluster/) and SageMaker in order to run our training jobs.\n\nHere, we\u2019ll focus specifically on AWS parallel cluster and provide an overview of how to utilize it for training purposes.\n\n**AWS ParallelCluster Setup**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

AWS ParallelCluster is an open source, cluster management tool that makes it easy for you to deploy and manage High Performance Computing (HPC) clusters on AWS. AWS ParallelCluster uses yaml configuration files to provision all the necessary resources. It also supports multiple instance types, job submission queues, shared file systems like Amazon EFS (NFS) or Amazon FSx for Lustre, and job schedulers like AWS Batch", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "and Slurm.

", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n\n

\n\n**Workflow on Clusters**\n\nThe high level idea is to have a cluster that has a head node which controls the compute nodes. The actual training job runs on the compute nodes. Overall steps to run a training job on a cluster are as follows:", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. Set up an AWS ParallelCuster (we discuss below)\n2. Connect to the head node, and import the training code/ setup the environment.\n3. Pull the data and place it in a shared folder that compute nodes can access (FSx Lustre drive).\n4. Run the training job using a job scheduler (in this case Slurm).\n\n**Setup AWS ParallelCuster**\n\nTo setup AWS ParallelCluster,", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "1. **Deploy a network stack.** This step is optional since you could use your account default VPC and let AWS ParallelCluster create your subnets and security groups. However, we prefer to compartmentalize our desired network infrastructure and do this deployment via a CloudFormation stack.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Since we deploy a public and a private subnet, we want to create them into an Availability Zone that contains our target instances, in this case p4d. We consult their availability in the region we use (us-east-1) through the following AWS CLI command:\n\n `aws ec2 describe-instance-type-offerings --location-type availability-zone \\ --filters Name=instance-type,Values=p4d.24xlarge --region us-east-1 --output table`", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We see three availability zones containing p4d instances, we pick one of them (`us-east-1c`, yours may be different) when deploying our network stack. This can be done with the AWS Console or the AWS CLI. In our case we use the latter as follows\n\n `aws cloudformation create-stack --stack-name VPC-Large-Scale --capabilities CAPABILITY_IAM --template-body file://VPC-Large-Scale.yaml --parameters ParameterKey=SubnetsAZ,ParameterValue=us-east-1c`", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "CloudFormation will deploy our new VPC, subnets, security groups and endpoints on our behalf. Once done, you can retrieve the IDs of the public and private subnets by querying the stack outputs and the values `PublicSubnet` and `PrivateSubnet`.\n\n For example, using the AWS CLI for the private subnet:\n\n `aws cloudformation describe-stacks --stack-name VPC-Large-Scale --query \"Stacks[0].Outputs[?OutputKey=='PrivateSubnet'].OutputValue\" --output text`", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "2. **Create ParallelCluster,** The cluster configuration file specifies the resources for our cluster. These resources include instance type for Head node, compute nodes, access to S3 buckets, shared storage where our data will be located. We will use Amazon FSx for Lustre that offers a fully managed shared storage service with [Lustre]().", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[Here](https://github.com/lessw2020/t5_11/blob/main/hpc-cluster/cluster.yaml) is an example of a cluster configuration file. We can use AWs ParallelCluster CLI to create the cluster. Please note that the private and public subnet IDs will need to be replaced by the ones you retrieved earlier. You will be able to control the cluster using the AWS ParallelCluster CLI to start, stop, pause, etc.\n\n ```\n pcluster create-cluster --cluster-name my-hpc-cluster --cluster-configuration cluster.yaml", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "3. **SSH to Head node -** once the cluster is ready, we can connect to the Head node using the SSH protocol, pull our training code with and place the data in the shared storage specified in the cluster configuration file.\n\n pcluster ssh --cluster-name cluster -i your-key_pair", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "4. **Launch the training job -** now that we have the data and training code, we can launch the slurm job for training. Here is an [example](https://github.com/lessw2020/t5_11/blob/main/hpc-cluster/modified-bert.slurm) of a slurm script to launch the job using torchrun.\n\nMore details on how to set up the cluster is out of the scope of this post, however we will have a separate post on it.\n\n**What\u2019s next?**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "With this post we provided a high level overview of FSDP and how it efficiently scales distributed AI training. The flowchart included will help provide a checklist for you to review tuning options discussed such as the transformer wrapper and activation checkpointing.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "In the next posts, we will continue with the T5 model and go deeper into each of the topics above, specifically with sharding strategy and other optimizations to provide more insight and details. For now, a good reference for the sharding strategy is in our video tutorial [here](https://www.youtube.com/watch?v=a3iW6Cggccw&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=5):", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "If you have questions or find an issue, please find the authors [Less](https://www.linkedin.com/in/less-wright-22b59017/), [Hamid](https://www.linkedin.com/in/hamid-nazeri/) and [Geeta](https://www.linkedin.com/in/geetachauhan/) or open an issue on[ PyTorch github](https://github.com/pytorch/pytorch).\n\n**Special thanks to:**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Pytorch Distributed team, Shen Li, Rohan Varma, Yanli Zhao, Andrew Gu, Anjali Sridhar, Ana Simoes, Pierre-Yves Aquilanti, Sundar Ranganathan, and the broader AWS team for supporting us with providing infrastructure and technical support for running the large scale experiments.\n\n**Resources:**\n\n_[FSDP video series](https://www.youtube.com/playlist?list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT)_\n\n_[Getting started with FSDP](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html)_", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "_[Advanced tutorial on FSDP](https://pytorch.org/tutorials/intermediate/FSDP_adavnced_tutorial.html)_\n\n_[API documentation](https://pytorch.org/docs/stable/fsdp.html?highlight=fsdp#module-torch.distributed.fsdp)_\n\n", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Performance Debugging of Production PyTorch Models at Meta\"\nauthor: CK Luk, Lei Tian\nfeatured-img: \"/assets/images/performance-debugging-of-production-pytorch-models-at-meta-1.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "1. Meta\u2019s AI Performance Profiling (MAIProf)\n\n

\n \n

\n\n

\nFigure 1: A simplified illustration of the Meta\u2019s AI performance profiling (MAIProf) infrastructure.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Figure 1 gives a simplified illustration of the AI performance profiling infrastructure at Meta. ML research and performance engineers submit through the User Portal a profiling request for a training job to the Profiling Service, which subsequently broadcasts the request to all the GPU hosts running the training job. When the Monitoring Daemon on a GPU host receives the profiling request, it will notify the Kineto GPU tracer (built on top of NVIDIA\u2019s libcupti) inside the PyTorch program corresponding to", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "the training job. As a result, Kineto traces will be collected and uploaded to the Object Store asynchronously (in more details: there is one Kineto trace collected for each individual GPU, each is treated and stored as a blob; an example will be given in Section 2). Meanwhile, MAIProf also collects a variety of aggregated performance metrics: the Monitoring Daemon on every GPU host continuously reads performance counters from NVIDIA\u2019s DCGM/NVML and logs them to a Time Series DB.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "In order to resolve this dilemma, we can use the new AnyPrecision optimizer available in [TorchDistX](https://github.com/pytorch/torchdistx) (Torch Distributed Experimental) that allows you to successfully train and keep the master weights in pure BF16 instead of FP32. In addition, unlike the typical storage of optimizer states in FP32, AnyPrecision is able to maintain states in pure BF16 as well. \n \nAnyPrecision enables pure BF16 training by maintaining an extra buffer that tracks the precision lost during the weight updates and re-applies that during the next update\u2026effectively resolving the weight stagnation issue without requiring FP32. \n \nAs a comparison of the throughput gains available with pure BF16 training using AnyPrecision, we ran experiments using FSDP with the T5 11B model with regular FP32 training, Mixed Precision training with BF16, and pure BF16 training using the AnyPrecision optimizer on 3 nodes with A100 gpus as mentioned previously.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n\n

\n\nAs shown above, training with AnyPrecision and pure BF16 resulted in 2x the throughput vs Mixed Precision, and over 20x improvement vs FP32.\n\nThe potential tradeoff is the impact on final accuracy - in the cases we tested, the accuracy was equal or better than FP32 due to a regularization effect from the slightly reduced precision, but your results may vary. \n \nAnyPrecision optimizer is available for you to test with [here](https://github.com/pytorch/torchdistx), and is a drop in replacement for AdamW optimizer. \n\n**Activation checkpointing - _increasing throughput by trading compute for memory_**\n\n

\n\n

\n\n**FSDP supports activation checkpointing once the model has been sharded**, and makes it easy to implement. The graph above shows ~4x throughput improvement using activation checkpointing.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Activation checkpointing is where the intermediate activations are freed during the forward pass, and a checkpoint is left as a placeholder. This generally increases available GPU memory by over 30%.\n\nThe tradeoff is that during the backward pass, these previously removed intermediate activations must be re-calculated again using information in the checkpoint (duplicate compute), but by leveraging the increased GPU memory, one can increase the batch size such that the net throughput can increase substantially.\n\n```python\n# verify we have FSDP activation support ready by importing:\nfrom torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (\n checkpoint_wrapper,\n CheckpointImpl,\n apply_activation_checkpointing_wrapper,\n)\n```\n\n\nThe steps required to implement activation checkpointing is to first import the FSDP checkpointing functions. We need declare our checkpointer wrapper type which is non-reentrant and create a check function to identify which layer to wrap as follows", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "```python\nnon_reentrant_wrapper = partial(\n checkpoint_wrapper,\n offload_to_cpu=False,\n checkpoint_impl=CheckpointImpl.NO_REENTRANT,\n)\ncheck_fn = lambda submodule: isinstance(submodule, T5Block)\n```\n\n```python\napply_activation_checkpointing_wrapper(\n model, checkpoint_wrapper_fn=non_reentrant_wrapper, check_fn=check_fn\n )\n```\n\n_Important note - this must be run after the model has been initialized with FSDP._\n\nHowever, hopefully you\u2019ve seen how some initial tuning with FSDP options can have a large impact on your training performance. \n\nWith that, we turn our attention from how to scale within FSDP, to how to scale your server hardware for FSDP using AWS.\n\n**Large Scale Training with FSDP on AWS - _For multi-node prioritize high speed network_**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "AWS provides several services that can be used to run distributed training with FSDP: [Amazon EC2 Accelerated Computing instances](https://aws.amazon.com/ec2/instance-types/#Accelerated_Computing), AWS [ParallelCluster](https://aws.amazon.com/hpc/parallelcluster/), and Amazon [Sagemaker](https://aws.amazon.com/sagemaker/features/?nc=sn&loc=2).\n\nIn this series of blog posts, we used [Amazon EC2 p4d](https://aws.amazon.com/ec2/instance-types/p4/) instances in a single-instance multi-GPU configuration and in a multi-instance configuration using AWS [ParallelCluster](https://aws.amazon.com/hpc/parallelcluster/) and SageMaker in order to run our training jobs.\n\nHere, we\u2019ll focus specifically on AWS parallel cluster and provide an overview of how to utilize it for training purposes.\n\n**AWS ParallelCluster Setup**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

AWS ParallelCluster is an open source, cluster management tool that makes it easy for you to deploy and manage High Performance Computing (HPC) clusters on AWS. AWS ParallelCluster uses yaml configuration files to provision all the necessary resources. It also supports multiple instance types, job submission queues, shared file systems like Amazon EFS (NFS) or Amazon FSx for Lustre, and job schedulers like AWS Batch and Slurm.

\n\n

\n\n

\n\n**Workflow on Clusters**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "The high level idea is to have a cluster that has a head node which controls the compute nodes. The actual training job runs on the compute nodes. Overall steps to run a training job on a cluster are as follows:\n\n1. Set up an AWS ParallelCuster (we discuss below)\n2. Connect to the head node, and import the training code/ setup the environment.\n3. Pull the data and place it in a shared folder that compute nodes can access (FSx Lustre drive).\n4. Run the training job using a job scheduler (in this case Slurm).\n\n**Setup AWS ParallelCuster**\n\nTo setup AWS ParallelCluster,\n\n1. **Deploy a network stack.** This step is optional since you could use your account default VPC and let AWS ParallelCluster create your subnets and security groups. However, we prefer to compartmentalize our desired network infrastructure and do this deployment via a CloudFormation stack.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Since we deploy a public and a private subnet, we want to create them into an Availability Zone that contains our target instances, in this case p4d. We consult their availability in the region we use (us-east-1) through the following AWS CLI command:\n\n `aws ec2 describe-instance-type-offerings --location-type availability-zone \\ --filters Name=instance-type,Values=p4d.24xlarge --region us-east-1 --output table`\n\n We see three availability zones containing p4d instances, we pick one of them (`us-east-1c`, yours may be different) when deploying our network stack. This can be done with the AWS Console or the AWS CLI. In our case we use the latter as follows\n\n `aws cloudformation create-stack --stack-name VPC-Large-Scale --capabilities CAPABILITY_IAM --template-body file://VPC-Large-Scale.yaml --parameters ParameterKey=SubnetsAZ,ParameterValue=us-east-1c`", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "CloudFormation will deploy our new VPC, subnets, security groups and endpoints on our behalf. Once done, you can retrieve the IDs of the public and private subnets by querying the stack outputs and the values `PublicSubnet` and `PrivateSubnet`.\n\n For example, using the AWS CLI for the private subnet:\n\n `aws cloudformation describe-stacks --stack-name VPC-Large-Scale --query \"Stacks[0].Outputs[?OutputKey=='PrivateSubnet'].OutputValue\" --output text`\n\n2. **Create ParallelCluster,** The cluster configuration file specifies the resources for our cluster. These resources include instance type for Head node, compute nodes, access to S3 buckets, shared storage where our data will be located. We will use Amazon FSx for Lustre that offers a fully managed shared storage service with [Lustre]().", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "[Here](https://github.com/lessw2020/t5_11/blob/main/hpc-cluster/cluster.yaml) is an example of a cluster configuration file. We can use AWs ParallelCluster CLI to create the cluster. Please note that the private and public subnet IDs will need to be replaced by the ones you retrieved earlier. You will be able to control the cluster using the AWS ParallelCluster CLI to start, stop, pause, etc.\n\n ```\n pcluster create-cluster --cluster-name my-hpc-cluster --cluster-configuration cluster.yaml\n ```\n\n3. **SSH to Head node -** once the cluster is ready, we can connect to the Head node using the SSH protocol, pull our training code with and place the data in the shared storage specified in the cluster configuration file.\n\n pcluster ssh --cluster-name cluster -i your-key_pair", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "4. **Launch the training job -** now that we have the data and training code, we can launch the slurm job for training. Here is an [example](https://github.com/lessw2020/t5_11/blob/main/hpc-cluster/modified-bert.slurm) of a slurm script to launch the job using torchrun.\n\nMore details on how to set up the cluster is out of the scope of this post, however we will have a separate post on it.\n\n**What\u2019s next?**\n\nWith this post we provided a high level overview of FSDP and how it efficiently scales distributed AI training. The flowchart included will help provide a checklist for you to review tuning options discussed such as the transformer wrapper and activation checkpointing.", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "In the next posts, we will continue with the T5 model and go deeper into each of the topics above, specifically with sharding strategy and other optimizations to provide more insight and details. For now, a good reference for the sharding strategy is in our video tutorial [here](https://www.youtube.com/watch?v=a3iW6Cggccw&list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT&index=5):\n\nIf you have questions or find an issue, please find the authors [Less](https://www.linkedin.com/in/less-wright-22b59017/), [Hamid](https://www.linkedin.com/in/hamid-nazeri/) and [Geeta](https://www.linkedin.com/in/geetachauhan/) or open an issue on[ PyTorch github](https://github.com/pytorch/pytorch).\n\n**Special thanks to:**\n\nPytorch Distributed team, Shen Li, Rohan Varma, Yanli Zhao, Andrew Gu, Anjali Sridhar, Ana Simoes, Pierre-Yves Aquilanti, Sundar Ranganathan, and the broader AWS team for supporting us with providing infrastructure and technical support for running the large scale experiments.\n\n**Resources:**", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "**Resources:**\n\n_[FSDP video series](https://www.youtube.com/playlist?list=PL_lsbAsL_o2BT6aerEKgIoufVD_fodnuT)_\n\n_[Getting started with FSDP](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html)_\n\n_[Advanced tutorial on FSDP](https://pytorch.org/tutorials/intermediate/FSDP_adavnced_tutorial.html)_\n\n_[API documentation](https://pytorch.org/docs/stable/fsdp.html?highlight=fsdp#module-torch.distributed.fsdp)_\n\n", "metadata": {"source": "https://pytorch.org/blog/efficient-large-scale-training-with-pytorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Performance Debugging of Production PyTorch Models at Meta\"\nauthor: CK Luk, Lei Tian\nfeatured-img: \"/assets/images/performance-debugging-of-production-pytorch-models-at-meta-1.png\"\n---\n\n## 1. Meta\u2019s AI Performance Profiling (MAIProf)\n\n

\n \n

\n\n

\nFigure 1: A simplified illustration of the Meta\u2019s AI performance profiling (MAIProf) infrastructure.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "Figure 1 gives a simplified illustration of the AI performance profiling infrastructure at Meta. ML research and performance engineers submit through the User Portal a profiling request for a training job to the Profiling Service, which subsequently broadcasts the request to all the GPU hosts running the training job. When the Monitoring Daemon on a GPU host receives the profiling request, it will notify the Kineto GPU tracer (built on top of NVIDIA\u2019s libcupti) inside the PyTorch program corresponding to the training job. As a result, Kineto traces will be collected and uploaded to the Object Store asynchronously (in more details: there is one Kineto trace collected for each individual GPU, each is treated and stored as a blob; an example will be given in Section 2). Meanwhile, MAIProf also collects a variety of aggregated performance metrics: the Monitoring Daemon on every GPU host continuously reads performance counters from NVIDIA\u2019s DCGM/NVML and logs them to a Time Series DB.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} {"page_content": "Once both trace and metrics collections are completed, the Profiling Service will automatically download traces from the Object Store for trace analysis and performance metrics from the Time Series DB for metric analysis. Finally, an overall profiling report with detailed and insightful analysis is delivered to the user.\n\nTo serve production uses, we deliberately made the following design choices for MAIProf:", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "- **No source-code change required in the PyTorch models**: profiling is triggered by sampling the execution of an unmodified model for a user-specified amount of time.\n- **Provide a holistic view of performance**: MAIProf performs system-wide analysis that cover both CPU and GPU. Under the hood, it invokes various CPU tools (e.g., Python tracer, Autograd Observer) and GPU tools (e.g., Kineto, DCGM) and correlates their results.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "- **Provide multiple tools that target a wide range of AI partitioners**: At Meta, there are engineers with different backgrounds who may need to tune their AI workload performance. Some of them are AI experts while others are general software engineers. Therefore, MAIProf provides a variety of tools for different levels of performance debugging, from high-level automatic trace comprehension to low-level trace analysis.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "- **Support distributed GPU profiling**: MAIProf can collect profiling data from multiple hosts, each with multiple GPUs. It then shows a combined view/analysis of the entire system.\n- **Highly scalable**: MAIProf is built as a service on top of existing infrastructures in Meta data centers such as a scalable storage system called Manifold. Its profiling capability can be easily scaled by adding more machines in the service pool with the increase of workloads.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "2. Case Study: Optimizing a Protection PyTorch Model\n\nTo be concrete, we use a case study on a protection PyTorch model used in production. First, we discuss our steps for identifying the performance bottlenecks in the model with MAIProf. Then we describe the corresponding optimizations applied and their impacts.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "2.1 Performance Bottlenecks", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Step 1: \n\nInspect the CPU and GPU utilization on the same timeline, as shown in Figure 2.\n\n

\n \n

\n\n

\nFigure 2: CPU usage over time (the top) vs. GPU usage over time (the bottom).\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "The first performance anomaly we noticed in Figure 2 is the pattern: *\u201cGPU-idle, GPU-active, GPU-idle, GPU-active \u2026\u201d* throughout the training. Overall, the GPU is idle for more than half of the training time (this is bad for performance because the GPU is a higher-performance device and so we want it to be utilized as much as possible).", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Step 2:\n\nCollect a Python function call trace on the CPU with MAIProf while the GPU is idle, which is shown in Figure 3.\n\n

\n \n

\n\n

\nFigure 3: A Python call trace.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "The Python trace shows that most of the CPU time is spent inside a Python function `sharded_iterrows()`. From the source code of the model, we learned that this function processes a big feature table in parallel. The number of worker threads used is controlled by a configurable parameter (`num_worker_threads`). Also, after investigating how the feature table is generated, we understood the performance anomaly: the training dataset is too large to fit in the CPU memory all at once; it needs to be broken into", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "multiple sub-datasets, each has sufficient data for running 10 epochs. Consequently, a new sub-dataset needs to be read from the disk to memory every 10 epochs, during which the GPU is totally idle.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Step 3:\n\nCollect GPU performance metrics, which is shown in Figure 4.\n\n

\n \n

\n\n

\nFigure 4: GPU performance metrics in MAIProf.\n

\n\nWe made the following observations from Figure 4:", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "- The streaming multiprocessor (SM) runs the model\u2019s CUDA kernels. Its utilization [1] is 9.1%, indicating that the parallel compute units on the GPU are not well utilized.\n- Tensor Core utilization is 0, meaning that Tensor Core (the mixed-precision compute unit on GPU) [2] is not used at all.\n- Max GPU memory utilization is 47.13%, indicating that half of the GPU memory is left unused.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Step 4:\n\nCollect a GPU trace (aka Kineto trace) of the training loop as shown in Figure 5.\n\n

\n \n

\n\n

\nFigure 5: A GPU trace (aka Kineto trace) of the training loop.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "Since commonly used PyTorch functions are already annotated, their names are automatically shown on the trace. With them, we can roughly divide the trace into the four phases in a training iteration: (1) data loading, (2) forward pass, (3) backward pass, (4) gradient optimization (note: In Figure 5, the \u201coptimizer\u201d phase is from the previous batch while the other three phases are from the current batch).", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "2.2 Optimizations\n\nWe performed four simple optimizations that target the bottlenecks identified above, each requiring only a change in a config parameter or at most a few source lines. They are listed in Figure 6.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "| Optimization | Amount of changes | Bottlenecks addressed |\n| ------------ | ----------------- | --------------------- |\n|Tune `num_worker_threads` by trying a few possible values within the number of CPU cores on each host. | 1 source line | GPU totally idle time |\n| Double the batch sizes | 2 config parameters | GPU memory under-utilization |\n| Use [automatic mixed precision](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html) in PyTorch | 13 source lines | Zero Tensor Core utilization |", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "| Use [mulitensor optimizer](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html#torch.optim.AdamW) in PyTorch | 1 source line | Many small GPU kernels in the optimizer |", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure 6: Four simple optimizations applied.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "3. Concluding Remarks\n\nPerformance tuning for PyTorch in production environments is increasingly important. A capable performance-debugging tool is a key to this process. We demonstrate with a case study on a production model that MAIProf is a powerful infrastructure for identifying optimization opportunities.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "At Meta, MAIProf has been used by 100s of engineers, from performance novices to experts, to identify many more types of bottlenecks. These include slow data loading, small and/or slow GPU kernels, distributed training issues such as load imbalance and excessive communication. MAIProf covers major classes of models, including recommendation, vision, and natural language processing. In summary, it is now an indispensable tool for tuning the performance of production PyTorch workloads.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "References\n\n[1] [https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/ cudaexperiments/kernellevel/achievedoccupancy.htm](https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/cudaexperiments/kernellevel/achievedoccupancy.htm)\n\n[2] [https://www.nvidia.com/en-us/data-center/tensor-cores/](https://www.nvidia.com/en-us/data-center/tensor-cores/)", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'The torch.fft module: Accelerated Fast Fourier Transforms with Autograd in PyTorch'\nauthor: Mike Ruberry, Peter Bell, and Joe Spisak \n---\n\nThe Fast Fourier Transform (FFT) calculates the Discrete Fourier Transform in O(n log n) time. It is foundational to a wide variety of numerical algorithms and signal processing techniques since it makes working in signals\u2019 \u201cfrequency domains\u201d as tractable as working in their spatial or temporal domains.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "As part of PyTorch\u2019s goal to support hardware-accelerated deep learning and scientific computing, we have invested in improving our FFT support, and with PyTorch 1.8, we are releasing the ``torch.fft`` module. This module implements the same functions as NumPy\u2019s ``np.fft`` module, but with support for accelerators, like GPUs, and autograd.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Getting started\n\nGetting started with the new ``torch.fft`` module is easy whether you are familiar with NumPy\u2019s ``np.fft`` module or not. While complete documentation for each function in the module can be found [here](https://pytorch.org/docs/1.8.0/fft.html), a breakdown of what it offers is:", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "* ``fft``, which computes a complex FFT over a single dimension, and ``ifft``, its inverse\n* the more general ``fftn`` and ``ifftn``, which support multiple dimensions\n* The \u201creal\u201d FFT functions, ``rfft``, ``irfft``, ``rfftn``, ``irfftn``, designed to work with signals that are real-valued in their time domains\n* The \"Hermitian\" FFT functions, ``hfft`` and ``ihfft``, designed to work with signals that are real-valued in their frequency domains", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "* Helper functions, like ``fftfreq``, ``rfftfreq``, ``fftshift``, ``ifftshift``, that make it easier to manipulate signals", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "We think these functions provide a straightforward interface for FFT functionality, as vetted by the NumPy community, although we are always interested in feedback and suggestions!\n\nTo better illustrate how easy it is to move from NumPy\u2019s ``np.fft`` module to PyTorch\u2019s ``torch.fft`` module, let\u2019s look at a NumPy implementation of a simple low-pass filter that removes high-frequency variance from a 2-dimensional image, a form of noise reduction or blurring:", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport numpy as np\nimport numpy.fft as fft\n\ndef lowpass_np(input, limit):\n pass1 = np.abs(fft.rfftfreq(input.shape[-1])) < limit\n pass2 = np.abs(fft.fftfreq(input.shape[-2])) < limit\n kernel = np.outer(pass2, pass1)\n \n fft_input = fft.rfft2(input)\n return fft.irfft2(fft_input * kernel, s=input.shape[-2:])", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Now let\u2019s see the same filter implemented in PyTorch:\n\n```python\nimport torch\nimport torch.fft as fft\n\ndef lowpass_torch(input, limit):\n pass1 = torch.abs(fft.rfftfreq(input.shape[-1])) < limit\n pass2 = torch.abs(fft.fftfreq(input.shape[-2])) < limit\n kernel = torch.outer(pass2, pass1)\n \n fft_input = fft.rfft2(input)\n return fft.irfft2(fft_input * kernel, s=input.shape[-2:])", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Not only do current uses of NumPy\u2019s ``np.fft`` module translate directly to ``torch.fft``, the ``torch.fft`` operations also support tensors on accelerators, like GPUs and autograd. This makes it possible to (among other things) develop new neural network modules using the FFT.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Performance\n\nThe ``torch.fft`` module is not only easy to use \u2014 it is also fast! PyTorch natively supports Intel\u2019s MKL-FFT library on Intel CPUs, and NVIDIA\u2019s cuFFT library on CUDA devices, and we have carefully optimized how we use those libraries to maximize performance. While your own results will depend on your CPU and CUDA hardware, computing Fast Fourier Transforms on CUDA devices can be many times faster than computing it on the CPU, especially for larger signals.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "In the future, we may add support for additional math libraries to support more hardware. See below for where you can request additional hardware support.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Updating from older PyTorch versions", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Some PyTorch users might know that older versions of PyTorch also offered FFT functionality with the ``torch.fft()`` function. Unfortunately, this function had to be removed because its name conflicted with the new module\u2019s name, and we think the new functionality is the best way to use the Fast Fourier Transform in PyTorch. In particular, ``torch.fft()`` was developed before PyTorch supported complex tensors, while the ``torch.fft`` module was designed to work with them.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "PyTorch also has a \u201cShort Time Fourier Transform\u201d, ``torch.stft``, and its inverse ``torch.istft``. These functions are being kept but updated to support complex tensors.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "Future\n\nAs mentioned, PyTorch 1.8 offers the torch.fft module, which makes it easy to use the Fast Fourier Transform (FFT) on accelerators and with support for autograd. We encourage you to try it out!", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "While this module has been modeled after NumPy\u2019s ``np.fft`` module so far, we are not stopping there. We are eager to hear from you, our community, on what FFT-related functionality you need, and we encourage you to create posts on our forums at [https://discuss.pytorch.org/](https://discuss.pytorch.org/), or [file issues on our Github](https://github.com/pytorch/pytorch/issues/new?assignees=&labels=&template=feature-request.md) with your feedback and requests. Early adopters have already started asking", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "about Discrete Cosine Transforms and support for more hardware platforms, for example, and we are investigating those features now.", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "We look forward to hearing from you and seeing what the community does with PyTorch\u2019s new FFT functionality!", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Internals Part II - The Build System\"\nauthor: \"Trevor Killeen\"\ndate: 2017-06-27 12:00:00 -0500\nredirect_from: /2017/06/27/Internals2.html\n---\n\nIn the first [post]({{ site.baseurl }}{% link _posts/2017-5-11-a-tour-of-pytorch-internals-1.md %}) I explained how we generate a `torch.Tensor` object that you can use in your Python interpreter. Next, I will explore the build system for PyTorch. The PyTorch codebase has a variety of components:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- The core Torch libraries: TH, THC, THNN, THCUNN\n - Vendor libraries: CuDNN, NCCL\n - Python Extension libraries\n - Additional third-party libraries: NumPy, MKL, LAPACK\n\nHow does a simple invocation of `python setup.py install` do the work that allows you to call `import torch` and use the PyTorch library in your code?", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The first part of this document will explain the build process from and end-user point of view. This will explain how we take the components above to build the library. The second part of the document will be important for PyTorch developers. It will document ways to improve your iteration speed by building only a subset of the code that you are working on.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Setuptools and PyTorch's setup( ) function\n\nPython uses [Setuptools](https://setuptools.readthedocs.io/en/latest/index.html) to build the library. Setuptools is an extension to the original distutils system from the core Python library. The core component of Setuptools is the `setup.py` file which contains all the information needed to build the project. The most important function is the `setup()` function which serves as the main entry point. Let's take a look at the one in PyTorch:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\nsetup(name=\"torch\", version=version,\n description=\"Tensors and Dynamic neural networks in Python with strong GPU acceleration\",\n ext_modules=extensions,\n cmdclass={\n 'build': build,\n 'build_py': build_py,\n 'build_ext': build_ext,\n 'build_deps': build_deps,\n 'build_module': build_module,\n 'develop': develop,\n 'install': install,\n 'clean': clean,\n },\n packages=packages,\n package_data={'torch': [", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "'lib/*.so*', 'lib/*.dylib*',\n 'lib/torch_shm_manager',\n 'lib/*.h',\n 'lib/include/TH/*.h', 'lib/include/TH/generic/*.h',\n 'lib/include/THC/*.h', 'lib/include/THC/generic/*.h']},\n install_requires=['pyyaml'],\n )", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The function is composed entirely of keyword arguments, which serve two purposes:\n\n- Metadata (e.g. name, description, version)\n- The contents of the package\n\nWe are concerned with #2. Let's break down the individual components:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- **ext_modules**: Python modules are either \"pure\" modules, containing only Python code, or \"extension\" modules written in the low-level language of the Python implementation. Here we are listing the extension modules in the build, including the main `torch._C` library that contains our Python Tensor", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- **cmdclass**: When using the `setup.py` script from the command line, the user must specify one or more \"commands\", code snippets that perform a specific action. For example, the \"install\" command builds and installs the package. This mapping routes specific commands to functions in `setup.py` that implement them\n - **packages**: The list of packages in the project. These are \"pure\" - i.e. they only contain Python code. These are defined elsewhere in `setup.py`", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- **package_data**: Additional files that need to be installed into a package: in this case the header files and shared libraries that the build will generate must be included in our installation\n - **install_requires**: In order to build PyTorch, we need pyyaml. Setuptools will handle making sure that pyyaml will be available, downloading and installing it if necessary", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "We will consider these components in more detail, but for now it is instructive to look at the end product of an installation -- i.e. what Setuptools does after building the code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "site_packages\n\nThird party packages are by default installed into the `lib//site_packages` directory associated with your Python binary. For example, because I am using an [Miniconda](https://conda.io/miniconda.html) environment, my Python binary is found at:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ which python\n~/local/miniconda2/envs/p3/bin/python\n```\nAnd thus packages are installed into:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages\n```\nI installed PyTorch, and let's take a look into torch folder in site-packages:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n(p3) killeent@devgpu047:site-packages$ cd torch\n(p3) killeent@devgpu047:torch$ ls\nautograd backends _C.cpython-36m-x86_64-linux-gnu.so cuda distributed _dl.cpython-36m-x86_64-linux-gnu.so functional.py __init__.py legacy lib multiprocessing nn optim __pycache__ serialization.py _six.py sparse storage.py _tensor_docs.py tensor.py _tensor_str.py _thnn _torch_docs.py utils _utils.py version.py", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Note that everything we would expect to be here is here:\n\n - All the \"pure\" packages are here [todo print packages from setup.py to explain]\n - The extension libraries are here - the ._C* and ._dl* shared libraries\n - The package_data is here: the contents of lib/ match exactly what we described in the setup function:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n(p3) killeent@devgpu047:torch$ ls lib/\ninclude libnccl.so.1 libTHC.so.1 libTHCUNN.so.1 libTHNN.so.1 libTH.so.1 THCUNN.h torch_shm_manager libnccl.so libshm.so libTHCS.so.1 libTHD.so.1 libTHPP.so.1 libTHS.so.1 THNN.h", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The Python interpreter looks into `site_packages` during an import. If we call `import torch` in our Python code it will find the module here and initialize and import it. You can read more about the import system [here](https://docs.python.org/3/tutorial/modules.html).", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Building Individual Parts\n\nNext, we will look at the various individual components of the build from start to finish. This will illustrate how we combine all the code we mentioned in the introduction.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Backend Torch and Vendor Libraries\n\nLet's take a look at the `install` cmd override in PyTorch's `setup.py`:\n\n```python\nclass install(setuptools.command.install.install):\n\n def run(self):\n if not self.skip_build:\n self.run_command('build_deps')\n setuptools.command.install.install.run(self)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "We note the first thing it does is run a command called \"build_deps\" - let's take a look at it's `run()` method:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\ndef run(self):\n from tools.nnwrap import generate_wrappers as generate_nn_wrappers\n build_all_cmd = ['bash', 'torch/lib/build_all.sh']\n if WITH_CUDA:\n build_all_cmd += ['--with-cuda']\n if WITH_NCCL and not SYSTEM_NCCL:\n build_all_cmd += ['--with-nccl']\n if WITH_DISTRIBUTED:\n build_all_cmd += ['--with-distributed']\n if subprocess.call(build_all_cmd) != 0:\n sys.exit(1)\n generate_nn_wrappers()", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Here we note that that we have a shell script `build_all.sh` in the `torch/lib/` directory. This script is configurable by whether we are on a system with CUDA enabled, the NCCL library enabled, and PyTorch's distributed library enabled.\n\nLet's take a look in `torch/lib`:\n\n```bash\n(p3) killeent@devgpu047:lib (master)$ ls\nbuild_all.sh libshm nccl README.md TH THC THCS THCUNN THD THNN THPP THS", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Here we see the directories for all the backend libraries. `TH`, `THC`, `THNN`, `THCUNN`, and `nccl` are [git subtrees](https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/) that are in sync with the libraries in e.g. [github.com/torch](https://github.com/torch/torch7/tree/master/lib/TH). `THS`, `THCS`, `THD`, `THPP` and `libshm` are libraries specific to PyTorch. All of the libraries contain `CMakeLists.txt` - indicating they are built with CMake.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The `build_all.sh` is essentially a script that runs the CMake configure step on all of these libraries, and then `make install`. Let's run `./build_all.sh` and see what we are left with:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n(p3) killeent@devgpu047:lib (master)$ ./build_all.sh --with-cuda --with-nccl --with-distributed\n[various CMake output logs]\n(p3) killeent@devgpu047:lib (master)$ ls\nbuild build_all.sh include libnccl.so libnccl.so.1 libshm libshm.so libTHC.so.1 libTHCS.so.1 libTHCUNN.so.1 libTHD.so.1 libTHNN.so.1 libTHPP.so.1 libTH.so.1 libTHS.so.1 nccl README.md TH THC THCS THCUNN THCUNN.h THD THNN THNN.h THPP THS tmp_install torch_shm_manager", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Now there are a number of extra things in the directory:\n\n - Shared library files for each library\n - Headers for `THNN` and `THCUNN`\n - `build` and `tmp_install` directories\n - The `torch_shm_manager` executable\n\nLet's explore further. In the shell script, we create the `build` directory and a subdir for each library to build:\n\n```bash\n# We create a build directory for the library, which will\n# contain the cmake output. $1 is the library to be built\n mkdir -p build/$1\n cd build/$1", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Thus e.g. `build/TH` contains the CMake configuration output including the `Makefile` for building TH, and also the result of running make install in this directory.\n\nLet's also look at `tmp_install`:\n\n```bash\n(p3) killeent@devgpu047:lib (master)$ ls tmp_install/\nbin include lib share", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "`tmp_install` looks like a standard install directory containing binaries, header files and library files. For example, `tmp_install/include/TH` contains all the `TH` headers, and `tmp_install/lib/` contains the `libTH.so.1` file.\n\nSo why have this directory? It is used to compile the libraries that depend on each other. For example, the `THC` library depends on the `TH` library and its headers. This is referenced in the build shell script as arguments to the `cmake` command:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n# install_dir is tmp_install\ncmake ...\n\t-DTH_INCLUDE_PATH=\"$INSTALL_DIR/include\" \\\n\t-DTH_LIB_PATH=\"$INSTALL_DIR/lib\" \\", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "And indeed if we look at the `THC` library we built:\n\n```bash\n(p3) killeent@devgpu047:lib (master)$ ldd libTHC.so.1\n\t...\n\tlibTH.so.1 => /home/killeent/github/pytorch/torch/lib/tmp_install/lib/./libTH.so.1 (0x00007f84478b7000)", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The way the `build_all.sh` specifies the include and library paths is a little messy but this is representative of the overall idea. Finally, at the end of the script:\n\n```bash\n# If all the builds succeed we copy the libraries, headers,\n# binaries to torch/lib\ncp $INSTALL_DIR/lib/* .\ncp THNN/generic/THNN.h .\ncp THCUNN/generic/THCUNN.h .\ncp -r $INSTALL_DIR/include .\ncp $INSTALL_DIR/bin/* .", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As we can see, at the end, we copy everything to the top-level `torch/lib` directory - explaining the contents we saw above. We'll see why we do this next:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "NN Wrappers\n\nBriefly, let's touch on the last part of the `build_deps` command: `generate_nn_wrappers()`. We bind into the backend libraries using PyTorch's custom `cwrap` tooling, which we touched upon in a previous post. For binding `TH` and `THC` we manually write the YAML declarations for each function. However, due to the relative simplicity of the `THNN` and `THCUNN` libraries, we auto-generate both the cwrap declarations and the resulting C++ code.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The reason we copy the `THNN.h` and `THCUNN.h` header files into `torch/lib` is that this is where the `generate_nn_wrappers()` code expects these files to be located. `generate_nn_wrappers()` does a few things:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "1. Parses the header files, generating cwrap YAML declarations and writing them to output `.cwrap` files\n2. Calls `cwrap` with the appropriate plugins on these `.cwrap` files to generate source code for each\n3. Parses the headers *a second time* to generate `THNN_generic.h` - a library that takes `THPP` Tensors, PyTorch's \"generic\" C++ Tensor Library, and calls into the appropriate `THNN`/`THCUNN` library function based on the dynamic type of the Tensor", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "If we take a look into `torch/csrc/nn` after running `generate_nn_wrappers()` we can see the output:\n\n```bash\n(p3) killeent@devgpu047:nn (master)$ ls\nTHCUNN.cpp THCUNN.cwrap THNN.cpp THNN.cwrap THNN_generic.cpp THNN_generic.cwrap THNN_generic.h THNN_generic.inc.h", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "For example, the code generates cwrap like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```\n[[\n name: FloatBatchNormalization_updateOutput\n return: void\n cname: THNN_FloatBatchNormalization_updateOutput\n arguments:\n - void* state\n - THFloatTensor* input\n - THFloatTensor* output\n - type: THFloatTensor*\n name: weight\n nullable: True\n - type: THFloatTensor*\n name: bias\n nullable: True\n - THFloatTensor* running_mean\n - THFloatTensor* running_var\n - THFloatTensor* save_mean\n - THFloatTensor* save_std\n - bool train\n - double momentum", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- double eps\n]]", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "with corresponding `.cpp`:\n\n```cpp\nextern \"C\" void THNN_FloatBatchNormalization_updateOutput(void*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, bool, double, double);", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "PyObject * FloatBatchNormalization_updateOutput(PyObject *_unused, PyObject *args) {\n\t// argument checking, unpacking\n\t PyThreadState *_save = NULL;\n try {\n Py_UNBLOCK_THREADS;\n THNN_FloatBatchNormalization_updateOutput(arg_state, arg_input, arg_output, arg_weight, arg_bias, arg_running_mean, arg_running_var, arg_save_mean, arg_save_std, arg_train, arg_momentum, arg_eps);\n Py_BLOCK_THREADS;\n Py_RETURN_NONE;\n } catch (...) {\n if (_save) {", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Py_BLOCK_THREADS;\n }\n throw;\n }", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "...\n}", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "In the `THPP` generated code, the function looks like this:\n\n```cpp\nvoid BatchNormalization_updateOutput(thpp::Tensor* input, thpp::Tensor* output, thpp::Tensor* weight, thpp::Tensor* bias, thpp::Tensor* running_mean, thpp::Tensor* running_var, thpp::Tensor* save_mean, thpp::Tensor* save_std, bool train, double momentum, double eps) {\n\t// Call appropriate THNN function based on tensor type, whether its on CUDA, etc.\n}\n```\n\nWe will look a little more at how these source files are used later.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "\"Building\" the Pure Python Modules\n\nNow that we have built the backend libraries (the \"dependencies\") we can move forward with building the actual PyTorch code. The next Setuptools command that runs is `build_py`, which is used to build all the \"Pure\" python modules in our library. These are the \"packages\" passed to `setup.py`.\n\nThe packages are found using the Setuptools' utility function `find_packages()`:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\npackages = find_packages(exclude=('tools.*',))", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "['torch', 'torch._thnn', 'torch.autograd', 'torch.backends', 'torch.cuda', 'torch.distributed', 'torch.legacy', 'torch.multiprocessing', 'torch.nn', 'torch.optim', 'torch.sparse', 'torch.utils', 'torch.autograd._functions', 'torch.backends.cudnn', 'torch.legacy.nn', 'torch.legacy.optim', 'torch.nn._functions', 'torch.nn.backends', 'torch.nn.modules', 'torch.nn.parallel', 'torch.nn.utils', 'torch.nn._functions.thnn', 'torch.utils.data', 'torch.utils.ffi', 'torch.utils.serialization', 'torch.utils.trainer',", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "'torch.utils.backcompat', 'torch.utils.trainer.plugins']", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As we can see, `find_package` has recursively traversed the `torch` directory, finding all the directory paths that have an `__init__.py` file.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "When building with Setuptools, the tool creates a `build` directory in the distribution root, i.e. the same location as the `setup.py` file. Because PyTorch is composed of both \"Pure\" python modules and Extension Modules, we need to preserve information about the Operating System and Python version used when performing the build. So if we look in my `build` directory, we see:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ ls build\nlib.linux-x86_64-3.6 temp.linux-x86_64-3.6", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "This indicates that I've built the project on `linux-x86-64` using Python 3.6. The lib directory contains the library files, while the temp directory contains files generated during the build that aren't needed in the final installation.\n\nBecause \"Pure\" python modules are just Python code, and don't need to be \"compiled\", the `build_py` process simply copies files from their locations as found by `find_packages` to the equivalent location in `build/`. So our build output is littered with lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\ncopying torch/autograd/_functions/blas.py -> build/lib.linux-x86_64-3.6/torch/autograd/_functions", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "We also noted earlier that we could pass files and directories to the `package_data` keyword argument to the main `setup()` function, and that Setuptools would handle copying those files to the installation location. During `build_py`, these files are copied to the `build/` directory, so we also see lines like:\n\n```bash\ncopying torch/lib/libTH.so.1 -> build/lib.linux-x86_64-3.6/torch/lib\n...\ncopying torch/lib/include/THC/generic/THCTensor.h -> build/lib.linux-x86_64-3.6/torch/lib/include/THC/generic\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Building the Extension Modules\n\nFinally, we need to build the Extension Modules, i.e. the PyTorch modules written in C++ using the CPython backend. This also constitutes the majority of the code logic in `setup.py`. Our overridden `build_ext` Command has some special logic before the extensions themselves are actually built:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom tools.cwrap import cwrap\nfrom tools.cwrap.plugins.THPPlugin import THPPlugin\nfrom tools.cwrap.plugins.ArgcountSortPlugin import ArgcountSortPlugin\nfrom tools.cwrap.plugins.AutoGPU import AutoGPU\nfrom tools.cwrap.plugins.BoolOption import BoolOption\nfrom tools.cwrap.plugins.KwargsPlugin import KwargsPlugin\nfrom tools.cwrap.plugins.NullableArguments import NullableArguments\nfrom tools.cwrap.plugins.CuDNNPlugin import CuDNNPlugin\nfrom tools.cwrap.plugins.WrapDim import WrapDim", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "from tools.cwrap.plugins.AssertNDim import AssertNDim\nfrom tools.cwrap.plugins.Broadcast import Broadcast\nfrom tools.cwrap.plugins.ProcessorSpecificPlugin import ProcessorSpecificPlugin\n thp_plugin = THPPlugin()\n cwrap('torch/csrc/generic/TensorMethods.cwrap', plugins=[\n ProcessorSpecificPlugin(), BoolOption(), thp_plugin,\n AutoGPU(condition='IS_CUDA'), ArgcountSortPlugin(), KwargsPlugin(),\n AssertNDim(), WrapDim(), Broadcast()\n ])", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "cwrap('torch/csrc/cudnn/cuDNN.cwrap', plugins=[\n CuDNNPlugin(), NullableArguments()\n ])", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Recall above that I documented that we auto-generated C++ code for calling into the `THNN` etc. libraries. Here is where we bind `TH`, `THC` and `CuDNN`. We take the YAML declarations in `TensorMethods.cwrap`, and use them to generate output C++ source files that contain implementations that work within PyTorch's C++ Ecosystem. For example, a simple declaration like zero_:\n\n```\n[[\n name: zero_\n cname: zero\n return: self\n arguments:\n - THTensor* self\n]]", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Generates code like:\n\n```cpp\n PyObject * THPTensor_(zero_)(PyObject *self, PyObject *args, PyObject *kwargs) {\n\t...\n\tTHTensor_(zero)(LIBRARY_STATE arg_self);\n\t...\n}\n```\n\nIn the previous post we documented how these functions are tied to specific Tensor types, so I won't expand on that there. For the build process its enough to know that these C++ files are generated prior to the extension being built, because these source files are used during Extension compilation.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Specifying the Extensions\n\nUnlike pure modules, it\u2019s not enough just to list modules or packages and expect the Setuptools to go out and find the right files; you have to specify the extension name, source file(s), and any compile/link requirements (include directories, libraries to link with, etc.).", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "The bulk (200~ LOC at the time of this writing) of the `setup.py` goes into specifying how to build these Extensions. Here, some of the choices we make in `build_all.sh` begin to make sense. For example, we saw that our build script specified a `tmp_install` directory where we installed our backend libraries. In our `setup.py` code, we reference this directory when adding to the list of directories containing header files to include:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\n# tmp_install_path is torch/lib/tmp_install\ninclude_dirs += [\n cwd,\n os.path.join(cwd, \"torch\", \"csrc\"),\n tmp_install_path + \"/include\",\n tmp_install_path + \"/include/TH\",\n tmp_install_path + \"/include/THPP\",\n tmp_install_path + \"/include/THNN\",", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Similarly, we copied the shared object libraries to `torch/csrc` at the end of the `build_all.sh` script. We reference these locations directly in our `setup.py` code when identifying libraries that we may link against:\n\n```python\n# lib_path is torch/lib\nTH_LIB = os.path.join(lib_path, 'libTH.so.1')\nTHS_LIB = os.path.join(lib_path, 'libTHS.so.1')\nTHC_LIB = os.path.join(lib_path, 'libTHC.so.1')\nTHCS_LIB = os.path.join(lib_path, 'libTHCS.so.1')\nTHNN_LIB = os.path.join(lib_path, 'libTHNN.so.1')\n# ...", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Let's consider how we build the main `torch._C` Extension Module:\n\n```python\nC = Extension(\"torch._C\",\n libraries=main_libraries,\n sources=main_sources,\n language='c++',\n extra_compile_args=main_compile_args + extra_compile_args,\n include_dirs=include_dirs,\n library_dirs=library_dirs,\n extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')],\n )", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- The main libraries are all the libraries we link against. This includes things like `shm`, PyTorch's shared memory management library, and also system libraries like `cudart` and `cudnn`. Note that the `TH` libraries *are not* listed here\n - The main sources are the C++ files that make up the C++ backend for PyTorch\n - The compile args are various flags that configure compilation. For example, we might want to add debug flags when compiling in debug mode", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- The include dirs are the paths to all the directories containing header files. This is also another example where the `build_all.sh` script is important - for example, we look for the `TH` header files in `torch/lib/tmp_install/include/TH` - which is the install location we specified with our CMake configuration", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- The library dirs are directories to search for shared libraries at link time. For example, we include `torch/lib` - the location we copied our `.so` files to at the end of `build_all.sh`, but also the paths to the CUDA and CuDNN directories", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "- The link arguments are used when linking object files together to create the extension. In PyTorch, this includes more *normal* options like decided to link `libstdc++` statically. However, there is one key component: **this is where we link the backend TH libraries**. Note that we have lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```python\n# The explicit paths to .so files we described above\nmain_link_args = [TH_LIB, THS_LIB, THPP_LIB, THNN_LIB]", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "You might be wondering why we do this as opposed to adding these libraries to the list we pass to the `libraries` keyword argument. After all, that is a list of libraries to link against. The issue is that Lua Torch installs often set the `LD_LIBRARY_PATH` variable, and thus we could mistakenly link against a `TH` library built for Lua Torch, instead of the library we have built locally. This would be problematic because the code could be out of date, and also there are various configuration options for Lua", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Torch's `TH` that would not play nicely with PyTorch.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As such, we manually specify the paths to the shared libraries we generated directly to the linker.\n\nThere are other extensions needed to power PyTorch and they are built in a similar way. The Setuptools library invokes the C++ compiler and linker to build all of these extensions. If the builds succeed, we have successfully *built* the PyTorch library and we can move on to installation.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Installation\n\nAfter building has finished, installation is quite simple. We simply have to copy everything from our `build/lib.linux-x86_64-3.6` directory to the appropriate installation directory. Recall that we noted above that this directory is the `site_packages` directory associated with our Python binary. As a result, we see lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\nrunning install_lib\ncreating /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncopying build/lib.linux-x86_64-3.6/torch/_C.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncopying build/lib.linux-x86_64-3.6/torch/_dl.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncreating /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "copying build/lib.linux-x86_64-3.6/torch/_thnn/_THNN.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn\ncopying build/lib.linux-x86_64-3.6/torch/_thnn/_THCUNN.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "- **No source-code change required in the PyTorch models**: profiling is triggered by sampling the execution of an unmodified model for a user-specified amount of time.\n- **Provide a holistic view of performance**: MAIProf performs system-wide analysis that cover both CPU and GPU. Under the hood, it invokes various CPU tools (e.g., Python tracer, Autograd Observer) and GPU tools (e.g., Kineto, DCGM) and correlates their results. \n- **Provide multiple tools that target a wide range of AI partitioners**: At Meta, there are engineers with different backgrounds who may need to tune their AI workload performance. Some of them are AI experts while others are general software engineers. Therefore, MAIProf provides a variety of tools for different levels of performance debugging, from high-level automatic trace comprehension to low-level trace analysis.\n- **Support distributed GPU profiling**: MAIProf can collect profiling data from multiple hosts, each with multiple GPUs. It then shows a combined view/analysis of the entire system.\n- **Highly scalable**: MAIProf is built as a service on top of existing infrastructures in Meta data centers such as a scalable storage system called Manifold. Its profiling capability can be easily scaled by adding more machines in the service pool with the increase of workloads.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "## 2. Case Study: Optimizing a Protection PyTorch Model\n\nTo be concrete, we use a case study on a protection PyTorch model used in production. First, we discuss our steps for identifying the performance bottlenecks in the model with MAIProf. Then we describe the corresponding optimizations applied and their impacts.\n\n### 2.1 Performance Bottlenecks\n\n#### Step 1: \n\nInspect the CPU and GPU utilization on the same timeline, as shown in Figure 2.\n\n

\n \n

\n\n

\nFigure 2: CPU usage over time (the top) vs. GPU usage over time (the bottom).\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "The first performance anomaly we noticed in Figure 2 is the pattern: *\u201cGPU-idle, GPU-active, GPU-idle, GPU-active \u2026\u201d* throughout the training. Overall, the GPU is idle for more than half of the training time (this is bad for performance because the GPU is a higher-performance device and so we want it to be utilized as much as possible).\n\n#### Step 2:\n\nCollect a Python function call trace on the CPU with MAIProf while the GPU is idle, which is shown in Figure 3.\n\n

\n \n

\n\n

\nFigure 3: A Python call trace.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "The Python trace shows that most of the CPU time is spent inside a Python function `sharded_iterrows()`. From the source code of the model, we learned that this function processes a big feature table in parallel. The number of worker threads used is controlled by a configurable parameter (`num_worker_threads`). Also, after investigating how the feature table is generated, we understood the performance anomaly: the training dataset is too large to fit in the CPU memory all at once; it needs to be broken into multiple sub-datasets, each has sufficient data for running 10 epochs. Consequently, a new sub-dataset needs to be read from the disk to memory every 10 epochs, during which the GPU is totally idle.\n\n#### Step 3:\n\nCollect GPU performance metrics, which is shown in Figure 4.\n\n

\n \n

\n\n

\nFigure 4: GPU performance metrics in MAIProf.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "We made the following observations from Figure 4:\n\n- The streaming multiprocessor (SM) runs the model\u2019s CUDA kernels. Its utilization [1] is 9.1%, indicating that the parallel compute units on the GPU are not well utilized.\n- Tensor Core utilization is 0, meaning that Tensor Core (the mixed-precision compute unit on GPU) [2] is not used at all.\n- Max GPU memory utilization is 47.13%, indicating that half of the GPU memory is left unused.\n\n#### Step 4:\n\nCollect a GPU trace (aka Kineto trace) of the training loop as shown in Figure 5.\n\n

\n \n

\n\n

\nFigure 5: A GPU trace (aka Kineto trace) of the training loop.\n

", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "Since commonly used PyTorch functions are already annotated, their names are automatically shown on the trace. With them, we can roughly divide the trace into the four phases in a training iteration: (1) data loading, (2) forward pass, (3) backward pass, (4) gradient optimization (note: In Figure 5, the \u201coptimizer\u201d phase is from the previous batch while the other three phases are from the current batch).\n\n### 2.2 Optimizations\n\nWe performed four simple optimizations that target the bottlenecks identified above, each requiring only a change in a config parameter or at most a few source lines. They are listed in Figure 6.", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "| Optimization | Amount of changes | Bottlenecks addressed |\n| ------------ | ----------------- | --------------------- |\n|Tune `num_worker_threads` by trying a few possible values within the number of CPU cores on each host. | 1 source line | GPU totally idle time |\n| Double the batch sizes | 2 config parameters | GPU memory under-utilization |\n| Use [automatic mixed precision](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html) in PyTorch | 13 source lines | Zero Tensor Core utilization |\n| Use [mulitensor optimizer](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html#torch.optim.AdamW) in PyTorch | 1 source line | Many small GPU kernels in the optimizer |\n\n

\nFigure 6: Four simple optimizations applied.\n

\n\n## 3. Concluding Remarks", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "Performance tuning for PyTorch in production environments is increasingly important. A capable performance-debugging tool is a key to this process. We demonstrate with a case study on a production model that MAIProf is a powerful infrastructure for identifying optimization opportunities. \n\nAt Meta, MAIProf has been used by 100s of engineers, from performance novices to experts, to identify many more types of bottlenecks. These include slow data loading, small and/or slow GPU kernels, distributed training issues such as load imbalance and excessive communication. MAIProf covers major classes of models, including recommendation, vision, and natural language processing. In summary, it is now an indispensable tool for tuning the performance of production PyTorch workloads.\n\n## References", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "## References\n\n[1] [https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/ cudaexperiments/kernellevel/achievedoccupancy.htm](https://docs.nvidia.com/gameworks/content/developertools/desktop/analysis/report/cudaexperiments/kernellevel/achievedoccupancy.htm)\n\n[2] [https://www.nvidia.com/en-us/data-center/tensor-cores/](https://www.nvidia.com/en-us/data-center/tensor-cores/)", "metadata": {"source": "https://pytorch.org/blog/performance-debugging-of-production-pytorch-models-at-meta/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'The torch.fft module: Accelerated Fast Fourier Transforms with Autograd in PyTorch'\nauthor: Mike Ruberry, Peter Bell, and Joe Spisak \n---\n\nThe Fast Fourier Transform (FFT) calculates the Discrete Fourier Transform in O(n log n) time. It is foundational to a wide variety of numerical algorithms and signal processing techniques since it makes working in signals\u2019 \u201cfrequency domains\u201d as tractable as working in their spatial or temporal domains.\n\nAs part of PyTorch\u2019s goal to support hardware-accelerated deep learning and scientific computing, we have invested in improving our FFT support, and with PyTorch 1.8, we are releasing the ``torch.fft`` module. This module implements the same functions as NumPy\u2019s ``np.fft`` module, but with support for accelerators, like GPUs, and autograd. \n\n## Getting started", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "## Getting started\n\nGetting started with the new ``torch.fft`` module is easy whether you are familiar with NumPy\u2019s ``np.fft`` module or not. While complete documentation for each function in the module can be found [here](https://pytorch.org/docs/1.8.0/fft.html), a breakdown of what it offers is:\n\n* ``fft``, which computes a complex FFT over a single dimension, and ``ifft``, its inverse\n* the more general ``fftn`` and ``ifftn``, which support multiple dimensions\n* The \u201creal\u201d FFT functions, ``rfft``, ``irfft``, ``rfftn``, ``irfftn``, designed to work with signals that are real-valued in their time domains\n* The \"Hermitian\" FFT functions, ``hfft`` and ``ihfft``, designed to work with signals that are real-valued in their frequency domains\n* Helper functions, like ``fftfreq``, ``rfftfreq``, ``fftshift``, ``ifftshift``, that make it easier to manipulate signals", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "We think these functions provide a straightforward interface for FFT functionality, as vetted by the NumPy community, although we are always interested in feedback and suggestions!\n\nTo better illustrate how easy it is to move from NumPy\u2019s ``np.fft`` module to PyTorch\u2019s ``torch.fft`` module, let\u2019s look at a NumPy implementation of a simple low-pass filter that removes high-frequency variance from a 2-dimensional image, a form of noise reduction or blurring:\n\n```python\nimport numpy as np\nimport numpy.fft as fft\n\ndef lowpass_np(input, limit):\n pass1 = np.abs(fft.rfftfreq(input.shape[-1])) < limit\n pass2 = np.abs(fft.fftfreq(input.shape[-2])) < limit\n kernel = np.outer(pass2, pass1)\n \n fft_input = fft.rfft2(input)\n return fft.irfft2(fft_input * kernel, s=input.shape[-2:])\n```\n\nNow let\u2019s see the same filter implemented in PyTorch:\n\n```python\nimport torch\nimport torch.fft as fft", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "def lowpass_torch(input, limit):\n pass1 = torch.abs(fft.rfftfreq(input.shape[-1])) < limit\n pass2 = torch.abs(fft.fftfreq(input.shape[-2])) < limit\n kernel = torch.outer(pass2, pass1)\n \n fft_input = fft.rfft2(input)\n return fft.irfft2(fft_input * kernel, s=input.shape[-2:])\n```\n\nNot only do current uses of NumPy\u2019s ``np.fft`` module translate directly to ``torch.fft``, the ``torch.fft`` operations also support tensors on accelerators, like GPUs and autograd. This makes it possible to (among other things) develop new neural network modules using the FFT.\n\n\n## Performance", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "## Performance\n\nThe ``torch.fft`` module is not only easy to use \u2014 it is also fast! PyTorch natively supports Intel\u2019s MKL-FFT library on Intel CPUs, and NVIDIA\u2019s cuFFT library on CUDA devices, and we have carefully optimized how we use those libraries to maximize performance. While your own results will depend on your CPU and CUDA hardware, computing Fast Fourier Transforms on CUDA devices can be many times faster than computing it on the CPU, especially for larger signals.\n\nIn the future, we may add support for additional math libraries to support more hardware. See below for where you can request additional hardware support.\n\n## Updating from older PyTorch versions", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "Some PyTorch users might know that older versions of PyTorch also offered FFT functionality with the ``torch.fft()`` function. Unfortunately, this function had to be removed because its name conflicted with the new module\u2019s name, and we think the new functionality is the best way to use the Fast Fourier Transform in PyTorch. In particular, ``torch.fft()`` was developed before PyTorch supported complex tensors, while the ``torch.fft`` module was designed to work with them.\n\nPyTorch also has a \u201cShort Time Fourier Transform\u201d, ``torch.stft``, and its inverse ``torch.istft``. These functions are being kept but updated to support complex tensors. \n\n## Future\n\nAs mentioned, PyTorch 1.8 offers the torch.fft module, which makes it easy to use the Fast Fourier Transform (FFT) on accelerators and with support for autograd. We encourage you to try it out!", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "While this module has been modeled after NumPy\u2019s ``np.fft`` module so far, we are not stopping there. We are eager to hear from you, our community, on what FFT-related functionality you need, and we encourage you to create posts on our forums at [https://discuss.pytorch.org/](https://discuss.pytorch.org/), or [file issues on our Github](https://github.com/pytorch/pytorch/issues/new?assignees=&labels=&template=feature-request.md) with your feedback and requests. Early adopters have already started asking about Discrete Cosine Transforms and support for more hardware platforms, for example, and we are investigating those features now.\n\nWe look forward to hearing from you and seeing what the community does with PyTorch\u2019s new FFT functionality!", "metadata": {"source": "https://pytorch.org/blog/the-torch.fft-module-accelerated-fast-fourier-transforms-with-autograd-in-pyTorch/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Internals Part II - The Build System\"\nauthor: \"Trevor Killeen\"\ndate: 2017-06-27 12:00:00 -0500\nredirect_from: /2017/06/27/Internals2.html\n---\n\nIn the first [post]({{ site.baseurl }}{% link _posts/2017-5-11-a-tour-of-pytorch-internals-1.md %}) I explained how we generate a `torch.Tensor` object that you can use in your Python interpreter. Next, I will explore the build system for PyTorch. The PyTorch codebase has a variety of components:\n\n - The core Torch libraries: TH, THC, THNN, THCUNN\n - Vendor libraries: CuDNN, NCCL\n - Python Extension libraries\n - Additional third-party libraries: NumPy, MKL, LAPACK\n\nHow does a simple invocation of `python setup.py install` do the work that allows you to call `import torch` and use the PyTorch library in your code?", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "The first part of this document will explain the build process from and end-user point of view. This will explain how we take the components above to build the library. The second part of the document will be important for PyTorch developers. It will document ways to improve your iteration speed by building only a subset of the code that you are working on.\n\n### Setuptools and PyTorch's setup( ) function\n\nPython uses [Setuptools](https://setuptools.readthedocs.io/en/latest/index.html) to build the library. Setuptools is an extension to the original distutils system from the core Python library. The core component of Setuptools is the `setup.py` file which contains all the information needed to build the project. The most important function is the `setup()` function which serves as the main entry point. Let's take a look at the one in PyTorch:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\nsetup(name=\"torch\", version=version,\n description=\"Tensors and Dynamic neural networks in Python with strong GPU acceleration\",\n ext_modules=extensions,\n cmdclass={\n 'build': build,\n 'build_py': build_py,\n 'build_ext': build_ext,\n 'build_deps': build_deps,\n 'build_module': build_module,\n 'develop': develop,\n 'install': install,\n 'clean': clean,\n },\n packages=packages,\n package_data={'torch': [\n 'lib/*.so*', 'lib/*.dylib*',\n 'lib/torch_shm_manager',\n 'lib/*.h',\n 'lib/include/TH/*.h', 'lib/include/TH/generic/*.h',\n 'lib/include/THC/*.h', 'lib/include/THC/generic/*.h']},\n install_requires=['pyyaml'],\n )\n```\n\nThe function is composed entirely of keyword arguments, which serve two purposes:\n\n- Metadata (e.g. name, description, version)\n- The contents of the package\n\nWe are concerned with #2. Let's break down the individual components:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "- **ext_modules**: Python modules are either \"pure\" modules, containing only Python code, or \"extension\" modules written in the low-level language of the Python implementation. Here we are listing the extension modules in the build, including the main `torch._C` library that contains our Python Tensor\n - **cmdclass**: When using the `setup.py` script from the command line, the user must specify one or more \"commands\", code snippets that perform a specific action. For example, the \"install\" command builds and installs the package. This mapping routes specific commands to functions in `setup.py` that implement them\n - **packages**: The list of packages in the project. These are \"pure\" - i.e. they only contain Python code. These are defined elsewhere in `setup.py`\n - **package_data**: Additional files that need to be installed into a package: in this case the header files and shared libraries that the build will generate must be included in our installation\n - **install_requires**: In order to build PyTorch, we need pyyaml. Setuptools will handle making sure that pyyaml will be available, downloading and installing it if necessary", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "We will consider these components in more detail, but for now it is instructive to look at the end product of an installation -- i.e. what Setuptools does after building the code.\n\n### site_packages\n\nThird party packages are by default installed into the `lib//site_packages` directory associated with your Python binary. For example, because I am using an [Miniconda](https://conda.io/miniconda.html) environment, my Python binary is found at:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ which python\n~/local/miniconda2/envs/p3/bin/python\n```\nAnd thus packages are installed into:\n\n```bash\n/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages\n```\nI installed PyTorch, and let's take a look into torch folder in site-packages:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n(p3) killeent@devgpu047:site-packages$ cd torch\n(p3) killeent@devgpu047:torch$ ls\nautograd backends _C.cpython-36m-x86_64-linux-gnu.so cuda distributed _dl.cpython-36m-x86_64-linux-gnu.so functional.py __init__.py legacy lib multiprocessing nn optim __pycache__ serialization.py _six.py sparse storage.py _tensor_docs.py tensor.py _tensor_str.py _thnn _torch_docs.py utils _utils.py version.py\n```\n\nNote that everything we would expect to be here is here:\n\n - All the \"pure\" packages are here [todo print packages from setup.py to explain]\n - The extension libraries are here - the ._C* and ._dl* shared libraries\n - The package_data is here: the contents of lib/ match exactly what we described in the setup function:\n\n```bash\n(p3) killeent@devgpu047:torch$ ls lib/\ninclude libnccl.so.1 libTHC.so.1 libTHCUNN.so.1 libTHNN.so.1 libTH.so.1 THCUNN.h torch_shm_manager libnccl.so libshm.so libTHCS.so.1 libTHD.so.1 libTHPP.so.1 libTHS.so.1 THNN.h\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "The Python interpreter looks into `site_packages` during an import. If we call `import torch` in our Python code it will find the module here and initialize and import it. You can read more about the import system [here](https://docs.python.org/3/tutorial/modules.html).\n\n### Building Individual Parts\n\nNext, we will look at the various individual components of the build from start to finish. This will illustrate how we combine all the code we mentioned in the introduction.\n\n### Backend Torch and Vendor Libraries\n\nLet's take a look at the `install` cmd override in PyTorch's `setup.py`:\n\n```python\nclass install(setuptools.command.install.install):\n\n def run(self):\n if not self.skip_build:\n self.run_command('build_deps')\n setuptools.command.install.install.run(self)\n```\n\nWe note the first thing it does is run a command called \"build_deps\" - let's take a look at it's `run()` method:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\ndef run(self):\n from tools.nnwrap import generate_wrappers as generate_nn_wrappers\n build_all_cmd = ['bash', 'torch/lib/build_all.sh']\n if WITH_CUDA:\n build_all_cmd += ['--with-cuda']\n if WITH_NCCL and not SYSTEM_NCCL:\n build_all_cmd += ['--with-nccl']\n if WITH_DISTRIBUTED:\n build_all_cmd += ['--with-distributed']\n if subprocess.call(build_all_cmd) != 0:\n sys.exit(1)\n generate_nn_wrappers()\n```\n\nHere we note that that we have a shell script `build_all.sh` in the `torch/lib/` directory. This script is configurable by whether we are on a system with CUDA enabled, the NCCL library enabled, and PyTorch's distributed library enabled.\n\nLet's take a look in `torch/lib`:\n\n```bash\n(p3) killeent@devgpu047:lib (master)$ ls\nbuild_all.sh libshm nccl README.md TH THC THCS THCUNN THD THNN THPP THS\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "Here we see the directories for all the backend libraries. `TH`, `THC`, `THNN`, `THCUNN`, and `nccl` are [git subtrees](https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/) that are in sync with the libraries in e.g. [github.com/torch](https://github.com/torch/torch7/tree/master/lib/TH). `THS`, `THCS`, `THD`, `THPP` and `libshm` are libraries specific to PyTorch. All of the libraries contain `CMakeLists.txt` - indicating they are built with CMake.\n\nThe `build_all.sh` is essentially a script that runs the CMake configure step on all of these libraries, and then `make install`. Let's run `./build_all.sh` and see what we are left with:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n(p3) killeent@devgpu047:lib (master)$ ./build_all.sh --with-cuda --with-nccl --with-distributed\n[various CMake output logs]\n(p3) killeent@devgpu047:lib (master)$ ls\nbuild build_all.sh include libnccl.so libnccl.so.1 libshm libshm.so libTHC.so.1 libTHCS.so.1 libTHCUNN.so.1 libTHD.so.1 libTHNN.so.1 libTHPP.so.1 libTH.so.1 libTHS.so.1 nccl README.md TH THC THCS THCUNN THCUNN.h THD THNN THNN.h THPP THS tmp_install torch_shm_manager\n```\n\nNow there are a number of extra things in the directory:\n\n - Shared library files for each library\n - Headers for `THNN` and `THCUNN`\n - `build` and `tmp_install` directories\n - The `torch_shm_manager` executable\n\nLet's explore further. In the shell script, we create the `build` directory and a subdir for each library to build:\n\n```bash\n# We create a build directory for the library, which will\n# contain the cmake output. $1 is the library to be built\n mkdir -p build/$1\n cd build/$1\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "Thus e.g. `build/TH` contains the CMake configuration output including the `Makefile` for building TH, and also the result of running make install in this directory.\n\nLet's also look at `tmp_install`:\n\n```bash\n(p3) killeent@devgpu047:lib (master)$ ls tmp_install/\nbin include lib share\n```\n\n`tmp_install` looks like a standard install directory containing binaries, header files and library files. For example, `tmp_install/include/TH` contains all the `TH` headers, and `tmp_install/lib/` contains the `libTH.so.1` file.\n\nSo why have this directory? It is used to compile the libraries that depend on each other. For example, the `THC` library depends on the `TH` library and its headers. This is referenced in the build shell script as arguments to the `cmake` command:\n\n```bash\n# install_dir is tmp_install\ncmake ...\n\t-DTH_INCLUDE_PATH=\"$INSTALL_DIR/include\" \\\n\t-DTH_LIB_PATH=\"$INSTALL_DIR/lib\" \\\n```\n\nAnd indeed if we look at the `THC` library we built:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n(p3) killeent@devgpu047:lib (master)$ ldd libTHC.so.1\n\t...\n\tlibTH.so.1 => /home/killeent/github/pytorch/torch/lib/tmp_install/lib/./libTH.so.1 (0x00007f84478b7000)\n```\n\nThe way the `build_all.sh` specifies the include and library paths is a little messy but this is representative of the overall idea. Finally, at the end of the script:\n\n```bash\n# If all the builds succeed we copy the libraries, headers,\n# binaries to torch/lib\ncp $INSTALL_DIR/lib/* .\ncp THNN/generic/THNN.h .\ncp THCUNN/generic/THCUNN.h .\ncp -r $INSTALL_DIR/include .\ncp $INSTALL_DIR/bin/* .\n```\n\nAs we can see, at the end, we copy everything to the top-level `torch/lib` directory - explaining the contents we saw above. We'll see why we do this next:\n\n### NN Wrappers", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "### NN Wrappers\n\nBriefly, let's touch on the last part of the `build_deps` command: `generate_nn_wrappers()`. We bind into the backend libraries using PyTorch's custom `cwrap` tooling, which we touched upon in a previous post. For binding `TH` and `THC` we manually write the YAML declarations for each function. However, due to the relative simplicity of the `THNN` and `THCUNN` libraries, we auto-generate both the cwrap declarations and the resulting C++ code.\n\nThe reason we copy the `THNN.h` and `THCUNN.h` header files into `torch/lib` is that this is where the `generate_nn_wrappers()` code expects these files to be located. `generate_nn_wrappers()` does a few things:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "1. Parses the header files, generating cwrap YAML declarations and writing them to output `.cwrap` files\n2. Calls `cwrap` with the appropriate plugins on these `.cwrap` files to generate source code for each\n3. Parses the headers *a second time* to generate `THNN_generic.h` - a library that takes `THPP` Tensors, PyTorch's \"generic\" C++ Tensor Library, and calls into the appropriate `THNN`/`THCUNN` library function based on the dynamic type of the Tensor\n\nIf we take a look into `torch/csrc/nn` after running `generate_nn_wrappers()` we can see the output:\n\n```bash\n(p3) killeent@devgpu047:nn (master)$ ls\nTHCUNN.cpp THCUNN.cwrap THNN.cpp THNN.cwrap THNN_generic.cpp THNN_generic.cwrap THNN_generic.h THNN_generic.inc.h\n```\n\nFor example, the code generates cwrap like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```\n[[\n name: FloatBatchNormalization_updateOutput\n return: void\n cname: THNN_FloatBatchNormalization_updateOutput\n arguments:\n - void* state\n - THFloatTensor* input\n - THFloatTensor* output\n - type: THFloatTensor*\n name: weight\n nullable: True\n - type: THFloatTensor*\n name: bias\n nullable: True\n - THFloatTensor* running_mean\n - THFloatTensor* running_var\n - THFloatTensor* save_mean\n - THFloatTensor* save_std\n - bool train\n - double momentum\n - double eps\n]]\n```\n\nwith corresponding `.cpp`:\n\n```cpp\nextern \"C\" void THNN_FloatBatchNormalization_updateOutput(void*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, THFloatTensor*, bool, double, double);", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "PyObject * FloatBatchNormalization_updateOutput(PyObject *_unused, PyObject *args) {\n\t// argument checking, unpacking\n\t PyThreadState *_save = NULL;\n try {\n Py_UNBLOCK_THREADS;\n THNN_FloatBatchNormalization_updateOutput(arg_state, arg_input, arg_output, arg_weight, arg_bias, arg_running_mean, arg_running_var, arg_save_mean, arg_save_std, arg_train, arg_momentum, arg_eps);\n Py_BLOCK_THREADS;\n Py_RETURN_NONE;\n } catch (...) {\n if (_save) {\n Py_BLOCK_THREADS;\n }\n throw;\n }\n\n ...\n}\n```\n\nIn the `THPP` generated code, the function looks like this:\n\n```cpp\nvoid BatchNormalization_updateOutput(thpp::Tensor* input, thpp::Tensor* output, thpp::Tensor* weight, thpp::Tensor* bias, thpp::Tensor* running_mean, thpp::Tensor* running_var, thpp::Tensor* save_mean, thpp::Tensor* save_std, bool train, double momentum, double eps) {\n\t// Call appropriate THNN function based on tensor type, whether its on CUDA, etc.\n}\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "We will look a little more at how these source files are used later.\n\n### \"Building\" the Pure Python Modules\n\nNow that we have built the backend libraries (the \"dependencies\") we can move forward with building the actual PyTorch code. The next Setuptools command that runs is `build_py`, which is used to build all the \"Pure\" python modules in our library. These are the \"packages\" passed to `setup.py`.\n\nThe packages are found using the Setuptools' utility function `find_packages()`:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\npackages = find_packages(exclude=('tools.*',))\n['torch', 'torch._thnn', 'torch.autograd', 'torch.backends', 'torch.cuda', 'torch.distributed', 'torch.legacy', 'torch.multiprocessing', 'torch.nn', 'torch.optim', 'torch.sparse', 'torch.utils', 'torch.autograd._functions', 'torch.backends.cudnn', 'torch.legacy.nn', 'torch.legacy.optim', 'torch.nn._functions', 'torch.nn.backends', 'torch.nn.modules', 'torch.nn.parallel', 'torch.nn.utils', 'torch.nn._functions.thnn', 'torch.utils.data', 'torch.utils.ffi', 'torch.utils.serialization', 'torch.utils.trainer', 'torch.utils.backcompat', 'torch.utils.trainer.plugins']\n```\n\nAs we can see, `find_package` has recursively traversed the `torch` directory, finding all the directory paths that have an `__init__.py` file.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "When building with Setuptools, the tool creates a `build` directory in the distribution root, i.e. the same location as the `setup.py` file. Because PyTorch is composed of both \"Pure\" python modules and Extension Modules, we need to preserve information about the Operating System and Python version used when performing the build. So if we look in my `build` directory, we see:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ ls build\nlib.linux-x86_64-3.6 temp.linux-x86_64-3.6\n```\n\nThis indicates that I've built the project on `linux-x86-64` using Python 3.6. The lib directory contains the library files, while the temp directory contains files generated during the build that aren't needed in the final installation.\n\nBecause \"Pure\" python modules are just Python code, and don't need to be \"compiled\", the `build_py` process simply copies files from their locations as found by `find_packages` to the equivalent location in `build/`. So our build output is littered with lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\ncopying torch/autograd/_functions/blas.py -> build/lib.linux-x86_64-3.6/torch/autograd/_functions\n```\n\nWe also noted earlier that we could pass files and directories to the `package_data` keyword argument to the main `setup()` function, and that Setuptools would handle copying those files to the installation location. During `build_py`, these files are copied to the `build/` directory, so we also see lines like:\n\n```bash\ncopying torch/lib/libTH.so.1 -> build/lib.linux-x86_64-3.6/torch/lib\n...\ncopying torch/lib/include/THC/generic/THCTensor.h -> build/lib.linux-x86_64-3.6/torch/lib/include/THC/generic\n```\n\n### Building the Extension Modules\n\nFinally, we need to build the Extension Modules, i.e. the PyTorch modules written in C++ using the CPython backend. This also constitutes the majority of the code logic in `setup.py`. Our overridden `build_ext` Command has some special logic before the extensions themselves are actually built:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom tools.cwrap import cwrap\nfrom tools.cwrap.plugins.THPPlugin import THPPlugin\nfrom tools.cwrap.plugins.ArgcountSortPlugin import ArgcountSortPlugin\nfrom tools.cwrap.plugins.AutoGPU import AutoGPU\nfrom tools.cwrap.plugins.BoolOption import BoolOption\nfrom tools.cwrap.plugins.KwargsPlugin import KwargsPlugin\nfrom tools.cwrap.plugins.NullableArguments import NullableArguments\nfrom tools.cwrap.plugins.CuDNNPlugin import CuDNNPlugin\nfrom tools.cwrap.plugins.WrapDim import WrapDim\nfrom tools.cwrap.plugins.AssertNDim import AssertNDim\nfrom tools.cwrap.plugins.Broadcast import Broadcast\nfrom tools.cwrap.plugins.ProcessorSpecificPlugin import ProcessorSpecificPlugin\n thp_plugin = THPPlugin()\n cwrap('torch/csrc/generic/TensorMethods.cwrap', plugins=[\n ProcessorSpecificPlugin(), BoolOption(), thp_plugin,\n AutoGPU(condition='IS_CUDA'), ArgcountSortPlugin(), KwargsPlugin(),\n AssertNDim(), WrapDim(), Broadcast()\n ])\n cwrap('torch/csrc/cudnn/cuDNN.cwrap', plugins=[\n CuDNNPlugin(), NullableArguments()\n ])\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "Recall above that I documented that we auto-generated C++ code for calling into the `THNN` etc. libraries. Here is where we bind `TH`, `THC` and `CuDNN`. We take the YAML declarations in `TensorMethods.cwrap`, and use them to generate output C++ source files that contain implementations that work within PyTorch's C++ Ecosystem. For example, a simple declaration like zero_:\n\n```\n[[\n name: zero_\n cname: zero\n return: self\n arguments:\n - THTensor* self\n]]\n```\n\nGenerates code like:\n\n```cpp\n PyObject * THPTensor_(zero_)(PyObject *self, PyObject *args, PyObject *kwargs) {\n\t...\n\tTHTensor_(zero)(LIBRARY_STATE arg_self);\n\t...\n}\n```\n\nIn the previous post we documented how these functions are tied to specific Tensor types, so I won't expand on that there. For the build process its enough to know that these C++ files are generated prior to the extension being built, because these source files are used during Extension compilation.\n\n### Specifying the Extensions", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "Unlike pure modules, it\u2019s not enough just to list modules or packages and expect the Setuptools to go out and find the right files; you have to specify the extension name, source file(s), and any compile/link requirements (include directories, libraries to link with, etc.).\n\nThe bulk (200~ LOC at the time of this writing) of the `setup.py` goes into specifying how to build these Extensions. Here, some of the choices we make in `build_all.sh` begin to make sense. For example, we saw that our build script specified a `tmp_install` directory where we installed our backend libraries. In our `setup.py` code, we reference this directory when adding to the list of directories containing header files to include:\n\n```python\n# tmp_install_path is torch/lib/tmp_install\ninclude_dirs += [\n cwd,\n os.path.join(cwd, \"torch\", \"csrc\"),\n tmp_install_path + \"/include\",\n tmp_install_path + \"/include/TH\",\n tmp_install_path + \"/include/THPP\",\n tmp_install_path + \"/include/THNN\",\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "Similarly, we copied the shared object libraries to `torch/csrc` at the end of the `build_all.sh` script. We reference these locations directly in our `setup.py` code when identifying libraries that we may link against:\n\n```python\n# lib_path is torch/lib\nTH_LIB = os.path.join(lib_path, 'libTH.so.1')\nTHS_LIB = os.path.join(lib_path, 'libTHS.so.1')\nTHC_LIB = os.path.join(lib_path, 'libTHC.so.1')\nTHCS_LIB = os.path.join(lib_path, 'libTHCS.so.1')\nTHNN_LIB = os.path.join(lib_path, 'libTHNN.so.1')\n# ...\n```\n\nLet's consider how we build the main `torch._C` Extension Module:\n\n```python\nC = Extension(\"torch._C\",\n libraries=main_libraries,\n sources=main_sources,\n language='c++',\n extra_compile_args=main_compile_args + extra_compile_args,\n include_dirs=include_dirs,\n library_dirs=library_dirs,\n extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')],\n )\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "- The main libraries are all the libraries we link against. This includes things like `shm`, PyTorch's shared memory management library, and also system libraries like `cudart` and `cudnn`. Note that the `TH` libraries *are not* listed here\n - The main sources are the C++ files that make up the C++ backend for PyTorch\n - The compile args are various flags that configure compilation. For example, we might want to add debug flags when compiling in debug mode\n - The include dirs are the paths to all the directories containing header files. This is also another example where the `build_all.sh` script is important - for example, we look for the `TH` header files in `torch/lib/tmp_install/include/TH` - which is the install location we specified with our CMake configuration\n - The library dirs are directories to search for shared libraries at link time. For example, we include `torch/lib` - the location we copied our `.so` files to at the end of `build_all.sh`, but also the paths to the CUDA and CuDNN directories\n - The link arguments are used when linking object files together to create the extension. In PyTorch, this includes more *normal* options like decided to link `libstdc++` statically. However, there is one key component: **this is where we link the backend TH libraries**. Note that we have lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\n# The explicit paths to .so files we described above\nmain_link_args = [TH_LIB, THS_LIB, THPP_LIB, THNN_LIB]\n```\n\nYou might be wondering why we do this as opposed to adding these libraries to the list we pass to the `libraries` keyword argument. After all, that is a list of libraries to link against. The issue is that Lua Torch installs often set the `LD_LIBRARY_PATH` variable, and thus we could mistakenly link against a `TH` library built for Lua Torch, instead of the library we have built locally. This would be problematic because the code could be out of date, and also there are various configuration options for Lua Torch's `TH` that would not play nicely with PyTorch.\n\nAs such, we manually specify the paths to the shared libraries we generated directly to the linker.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "There are other extensions needed to power PyTorch and they are built in a similar way. The Setuptools library invokes the C++ compiler and linker to build all of these extensions. If the builds succeed, we have successfully *built* the PyTorch library and we can move on to installation.\n\n### Installation\n\nAfter building has finished, installation is quite simple. We simply have to copy everything from our `build/lib.linux-x86_64-3.6` directory to the appropriate installation directory. Recall that we noted above that this directory is the `site_packages` directory associated with our Python binary. As a result, we see lines like:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\nrunning install_lib\ncreating /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncopying build/lib.linux-x86_64-3.6/torch/_C.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncopying build/lib.linux-x86_64-3.6/torch/_dl.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch\ncreating /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn\ncopying build/lib.linux-x86_64-3.6/torch/_thnn/_THNN.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn\ncopying build/lib.linux-x86_64-3.6/torch/_thnn/_THCUNN.cpython-36m-x86_64-linux-gnu.so -> /home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/_thnn\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} {"page_content": "Finally lets power up the Python interpreter. When the Python interpreter executes an import statement, it searches for Python code and extension modules along a search path. A default value for the path is configured into the Python binary when the interpreter is built.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n# note we are now in my home directory\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import sys\n>>> sys.path", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "['', '/home/killeent/local/miniconda2/envs/p3/lib/python36.zip', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/lib-dynload', '/home/killeent/.local/lib/python3.6/site-packages', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages', '/home/killeent/github/pytorch', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg']", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As we can see, the `site-packages` directory we copied our PyTorch installation to is part of search path. Now let's load the `torch` module and see its location:\n\n```python\n>>> import torch\n>>> import inspect\n>>> inspect.getfile(torch)\n'/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/__init__.py'", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As we can see, we have loaded the module from `site_packages` as expected - and our build and installation is successful!\n\n**Note:** Python prepends the empty string to `sys.path` to represent the current working directory - making it the first place we search for a module. So if we run Python from the pytorch directory, we would accidentally load the local version of PyTorch rather than our installed version. This is something to watch out for.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Addendum - Developer Efficiency, 3rd Party Libraries, Things I Didn't Cover\n\nThe entire installation loop for PyTorch can be quite time-consuming. On my devserver, it takes around 5 minutes for an installation from source. Often times, when developing PyTorch, we only want to work on a subset of the entire project, and re-build only that subset in order to test changes. Fortunately, our build system enables this.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Setuptools Develop Mode\n\nThe main tool that supports this is Setuptools `develop` command. The documentation states that:\n\n>This command allows you to deploy your project\u2019s source for use in one or more \u201cstaging areas\u201d where it will be available for importing. This deployment is done in such a way that changes to the project source are immediately available in the staging area(s), without needing to run a build or install step after each change.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "But how does it work? Suppose we run `python setup.py build develop` in the PyTorch directory. The `build` command is run, building our dependencies (`TH`, `THPP`, etc.) and the extension libraries. However, if we look inside `site-packages`:\n\n```bash\n(p3) killeent@devgpu047:site-packages$ ls -la torch*\n-rw-r--r--. 1 killeent users 31 Jun 27 08:02 torch.egg-link", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Looking at the contents of the `torch.egg-link` file, it simply references the PyTorch directory:\n\n```bash\n(p3) killeent@devgpu047:site-packages$ cat torch.egg-link\n/home/killeent/github/pytorch", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "If we navigate back to the PyTorch directory, we see there is a new directory `torch.egg-info`:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n(p3) killeent@devgpu047:pytorch (master)$ ls -la torch.egg-info/\ntotal 28\ndrwxr-xr-x. 2 killeent users 4096 Jun 27 08:09 .\ndrwxr-xr-x. 10 killeent users 4096 Jun 27 08:01 ..\n-rw-r--r--. 1 killeent users 1 Jun 27 08:01 dependency_links.txt\n-rw-r--r--. 1 killeent users 255 Jun 27 08:01 PKG-INFO\n-rw-r--r--. 1 killeent users 7 Jun 27 08:01 requires.txt\n-rw-r--r--. 1 killeent users 16080 Jun 27 08:01 SOURCES.txt\n-rw-r--r--. 1 killeent users 12 Jun 27 08:01 top_level.txt", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "This file contains metadata about the PyTorch project. For example, `requirements.txt` lists all of the dependencies for setting up PyTorch:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ cat torch.egg-info/requires.txt\npyyaml", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Without going into too much detail, `develop` allows us to essentially treat the PyTorch repo itself as if it were in `site-packages`, so we can import the module and it just works:\n\n```bash\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import torch\n>>> torch.__file__\n'/home/killeent/github/pytorch/torch/__init__.py'", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As a result, the following consequences hold:\n\n- If we change a Python source file, the changes are automatically picked up, and we don't have to run any commands to let the Python interpreter *see* this change\n- If we change a C++ Source File in one of the extension libraries, we can re-run the `develop` command, it will re-build the extension\n\nThus we can develop the PyTorch codebases seamlessly, and test our changes in an easy way.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Working on the Dependency Libraries", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "If we are working on the dependencies (e.g. `TH`, `THPP`, etc.) we can re-build our changes more quickly by simply running the `build_deps` command directly. This will automatically call into `build_all.sh` to re-build our libraries, and copy the generated libraries appropriately. If we are using Setuptools `develop` mode, we will be using the local extension library built in the PyTorch directory. Because we have specified the paths to the shared libraries when compiling our Extension Libraries, the", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "changes will be picked up:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "```bash\n# we are using the local extension\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import torch\n>>> torch._C.__file__\n'/home/killeent/github/pytorch/torch/_C.cpython-36m-x86_64-linux-gnu.so'", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "# it references the local shared object library we just re-built\n(p3) killeent@devgpu047:~$ ldd /home/killeent/github/pytorch/torch/_C.cpython-36m-x86_64-linux-gnu.so\n# ...\nlibTH.so.1 => /home/killeent/github/pytorch/torch/lib/libTH.so.1 (0x00007f543d0e2000)\n# ...", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "As such, we can test any changes here without having to do a full rebuild.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "3rd Party Libraries\n\nPyTorch has dependencies on some 3rd party libraries. The usual mechanism for using these libraries is to install them via Anaconda, and then link against them. For example, we can use the `mkl` library with PyTorch by doing:\n\n```bash\n# installed to miniconda2/envs/p3/lib/libmkl_intel_lp64.so\nconda install mkl", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "And then as long as we have the path to this `lib` directory on our `$CMAKE_PREFIX_PATH`, it will successfully find this library when compiling:\n\n```bash\n# in the site-packages dir\n(p3) killeent@devgpu047:torch$ ldd _C.cpython-36m-x86_64-linux-gnu.so\n# ...\nlibmkl_intel_lp64.so => /home/killeent/local/miniconda2/envs/p3/lib/libmkl_intel_lp64.so (0x00007f3450bba000)\n# ...\n```", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "Not Covered, But Also Relevant\n\n- How `ccache` is used to speed up build times\n- How PyTorch's top-level `__init__.py` file handles the initial module import and pulling together all the various modules and extension libraries\n- The CMake build system, how the backend libraries are configured and built with CMake", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'An Overview of the PyTorch Mobile Demo Apps'\nauthor: Jeff Tang and Mark Saroufim\nfeatured-img: 'assets/images/android-demo-app.png'\ndate: 2021-06-18 12:00:00 -0500\n---\n\n\nPyTorch Mobile provides a runtime environment to execute state-of-the-art machine learning models on mobile devices. Latency is reduced, privacy preserved, and models can run on mobile devices anytime, anywhere.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "In this blog post, we provide a quick overview of 10 currently available PyTorch Mobile powered demo apps running various state-of-the-art PyTorch 1.9 machine learning models spanning images, video, audio and text.\n\nIt\u2019s never been easier to deploy a state-of-the-art ML model to a phone. You don\u2019t need any domain knowledge in Machine Learning and we hope one of the below examples resonates enough with you to be the starting point for your next project.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Computer Vision", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Image Classification\nThis app demonstrates how to use PyTorch C++ libraries on iOS and Android to classify a static image with the MobileNetv2/3 model.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS #1](https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld) [iOS #2](https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruIOS) [Android #1](https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp) [Android #2](https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruAndroid)\n\n [iOS](https://youtu.be/amTepUIR93k) [Android](https://youtu.be/5Lxuu16_28o)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Live Image Classification\nThis app demonstrates how to run a quantized MobileNetV2 and Resnet18 models to classify images in real time with an iOS and Android device camera.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/PyTorchDemo) [Android](https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "
\n\n\n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Image Segmentation\nThis app demonstrates how to use the PyTorch DeepLabV3 model to segment images. The updated app for PyTorch 1.9 also demonstrates how to create the model using the Mobile Interpreter and load the model with the LiteModuleLoader API.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ImageSegmentation) [Android](https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://pytorch.org/tutorials/beginner/deeplabv3_on_ios.html) [Android](https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Vision Transformer for Handwritten Digit Recognition\nThis app demonstrates how to use Facebook's latest optimized Vision Transformer DeiT model to do image classification and handwritten digit recognition.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ViT4MNIST) [Android](https://github.com/pytorch/android-demo-app/tree/master/ViT4MNIST)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [Android](https://drive.google.com/file/d/11L5mIjrLn7B7VdwjQl5vJv3ZVK4hcYut/view?usp=sharing)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Object Detection\nThis app demonstrates how to convert the popular YOLOv5 model and use it on an iOS app that detects objects from pictures in your photos, taken with camera, or with live camera.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ObjectDetection) [Android](https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://drive.google.com/file/d/1pIDrUDnCD5uF-mIz8nbSlZcXxPlRBKhl/view) [Android](https://drive.google.com/file/d/1-5AoRONUqZPZByM-fy0m7r8Ct11OnlIT/view)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "D2Go\nThis app demonstrates how to create and use a much lighter and faster Facebook D2Go model to detect objects from pictures in your photos, taken with camera, or with live camera.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/D2Go) [Android](https://github.com/pytorch/android-demo-app/tree/master/D2Go)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://drive.google.com/file/d/1GO2Ykfv5ut2Mfoc06Y3QUTFkS7407YA4/view) [Android](https://drive.google.com/file/d/18-2hLc-7JAKtd1q00X-5pHQCAdyJg7dZ/view?usp=sharing)\n\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Video", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Video Classification\nThis app demonstrates how to use a pre-trained PyTorchVideo model to perform video classification on tested videos, videos from the Photos library, or even real-time videos.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/TorchVideo) [Android](https://github.com/pytorch/android-demo-app/tree/master/TorchVideo)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://drive.google.com/file/d/1ijb4UIuF2VQiab4xfAsBwrQXCInvb9wd/view) [Android](https://drive.google.com/file/d/193tkZgt5Rlk7u-EQPcvkoFtmOQ14-zCC/view) [Deep Dive](https://www.youtube.com/watch?v=Qb4vDm-ruwI)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Natural Language Processing", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Text Classification\nThis app demonstrates how to use a pre-trained Reddit model to perform text classification.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/PyTorchDemo) [Android](https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Machine Translation\nThis app demonstrates how to convert a sequence-to-sequence neural machine translation model trained with the code in the PyTorch NMT tutorial for french to english translation.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/Seq2SeqNMT) [Android](https://github.com/pytorch/android-demo-app/tree/master/Seq2SeqNMT)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://drive.google.com/file/d/17Edk-yAyfzijHPR_2ZDAIX7VY-TkQnLf/view) [Android](https://drive.google.com/file/d/110KN3Pa9DprkBWnzj8Ppa8KMymhmBI61/view?usp=sharing)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Question Answering\nThis app demonstrates how to use the DistilBERT Hugging Face transformer model to answer questions about Pytorch Mobile itself.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/QuestionAnswering) [Android](https://github.com/pytorch/android-demo-app/tree/master/QuestionAnswering)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": " [iOS](https://drive.google.com/file/d/1QIB3yoP4I3zUU0bLCpvUqPV5Kv8f8JvB/view) [Android](https://drive.google.com/file/d/10hwGNFo5tylalKwut_CWFPJmV7JRdDKF/view?usp=sharing)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Audio", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "Speech Recognition\nThis app demonstrates how to convert Facebook AI's torchaudio-powered wav2vec 2.0, one of the leading models in speech recognition to TorchScript before deploying it.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/SpeechRecognition) [Android](https://github.com/pytorch/android-demo-app/tree/master/SpeechRecognition)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "We really hope one of these demo apps stood out for you. For the full list, make sure to visit the [iOS](https://github.com/pytorch/ios-demo-app) and [Android](https://github.com/pytorch/android-demo-app) demo app repos. You should also definitely check out the video [An Overview of the PyTorch Mobile Demo Apps](https://www.youtube.com/watch?v=Qb4vDm-ruwI) which provides both an overview of the PyTorch mobile demo apps and a deep dive into the PyTorch Video app for iOS and Android.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerating PyTorch Vision Models with Channels Last on CPU\"\nauthor: Mingfei Ma (Intel), Vitaly Fedyunin (Meta), Wei Wei (Meta)\nfeatured-img: '/assets/images/accelerating-pytorch-vision-models-with-channels-last-on-cpu-2.png'\n---", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Overview\n\nMemory formats has significant impact on performance when running vision models, generally Channels Last is a more favorable from performance perspective due to better data locality.\n\nThis blog will introduce fundamental concepts of memory formats and demonstrate performance benefits using Channels Last on popular PyTorch vision models on Intel\u00ae Xeon\u00ae Scalable processors.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Memory Formats Introduction\n\nMemory format refers to data representation that describes how a multidimensional (nD) array is stored in linear (1D) memory address space. The concept of memory format has two aspects:", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "- **Physical Order** is the layout of data storage in physical memory. For vision models, usually we talk about NCHW, NHWC. These are the descriptions of physical memory layout, also referred as Channels First and Channels Last respectively.\n- **Logical Order** is a convention on how to describe tensor shape and stride. In PyTorch, this convention is NCHW. No matter what the physical order is, tensor shape and stride will always be depicted in the order of NCHW.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Fig-1 is the physical memory layout of a tensor with shape of [1, 3, 4, 4] on both Channels First and Channels Last memory format (channels denoted as R, G, B respectively):\n\n

\n \n

\n\n

\nFig-1 Physical memory layout of Channels First and Channels Last\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Memory Formats Propagation\n\nThe general rule for PyTorch memory format propagation is to preserve the input tensor\u2019s memory format. Which means a Channels First input will generate a Channels First output and a Channels Last input will generate a Channels Last output.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "For Convolution layers, PyTorch uses oneDNN (oneAPI Deep Neural Network Library) by default to achieve optimal performance on Intel CPUs. Since it is physically impossible to achieve highly optimized performance directly with Channels Frist memory format, input and weight are firstly converted to blocked format and then computed. oneDNN may choose different blocked formats according to input shapes, data type and hardware architecture, for vectorization and cache reuse purposes. The blocked format is opaque", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "to PyTorch, so the output needs to be converted back to Channels First. Though blocked format would bring about optimal computing performance, the format conversions may add overhead and therefore offset the performance gain.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "On the other hand, oneDNN is optimized for Channels Last memory format to use it for optimal performance directly and PyTorch will simply pass a memory view to oneDNN. Which means the conversion of input and output tensor is saved. Fig-2 indicates memory format propagation behavior of convolution on PyTorch CPU (the solid arrow indicates a memory format conversion, and the dashed arrow indicates a memory view):", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nFig-2 CPU Conv memory format propagation\n

", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "On PyTorch, the default memory format is Channels First. In case a particular operator doesn't have support on Channels Last, the NHWC input would be treated as a non-contiguous NCHW and therefore fallback to Channels First, which will consume the previous memory bandwidth on CPU and result in suboptimal performance.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Therefore, it is very important to extend the scope of Channels Last support for optimal performance. And we have implemented Channels Last kernels for the commonly use operators in CV domain, applicable for both inference and training, such as:\n\n- Activations (e.g., ReLU, PReLU, etc.)\n- Convolution (e.g., Conv2d)\n- Normalization (e.g., BatchNorm2d, GroupNorm, etc.)\n- Pooling (e.g., AdaptiveAvgPool2d, MaxPool2d, etc.)\n- Shuffle (e.g., ChannelShuffle, PixelShuffle)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Refer to [Operators-with-Channels-Last-support](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support) for details.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Native Level Optimization on Channels Last\n\nAs mentioned above, PyTorch uses oneDNN to achieve optimal performance on Intel CPUs for convolutions. The rest of memory format aware operators are optimized at PyTorch native level, which doesn\u2019t require any third-party library support.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "- **Cache friendly parallelization scheme:** keep the same parallelization scheme for all the memory format aware operators, this will help increase data locality when passing each layer\u2019s output to the next.\n- **Vectorization on multiple archs:** generally, we can vectorize on the most inner dimension on Channels Last memory format. And each of the vectorized CPU kernels will be generated for both AVX2 and AVX512.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "While contributing to Channels Last kernels, we tried our best to optimize Channels First counterparts as well. The fact is some operators are physically impossible to achieve optimal performance on Channels First, such as Convolution, Pooling, etc.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Run Vision Models on Channels Last\n\nThe Channels Last related APIs are documented at [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html). Typically, we can convert a 4D tensor from Channels First to Channels Last by:\n\n```python\n# convert x to channels last\n# suppose x\u2019s shape is (N, C, H, W)\n# then x\u2019s stride will be (HWC, 1, WC, C)\nx = x.to(memory_format=torch.channels_last)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "To run models on Channels Last memory format, simply need to convert input and model to Channels Last and then you are ready to go. The following is a minimal example showing how to run ResNet50 with TorchVision on Channels Last memory format:\n\n```python\nimport torch\nfrom torchvision.models import resnet50\n\nN, C, H, W = 1, 3, 224, 224\nx = torch.rand(N, C, H, W)\nmodel = resnet50()\nmodel.eval()", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "# convert input and model to channels last\nx = x.to(memory_format=torch.channels_last)\nmodel = model.to(memory_format=torch.channels_last)\nmodel(x)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "The Channels Last optimization is implemented at native kernel level, which means you may apply other functionalities such as torch.fx and torch script together with Channels Last as well.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Performance Gains\n\nWe benchmarked inference performance of TorchVision models on Intel\u00ae Xeon\u00ae Platinum 8380 CPU @ 2.3 GHz, single instance per socket (batch size = 2 x number of physical cores). Results show that Channels Last has 1.3x to 1.8x performance gain over Channels First.\n\n

\n \n

\n\nThe performance gain primarily comes from two aspects:", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "- For Convolution layers, Channels Last saved the memory format conversion to blocked format for activations, which improves the overall computation efficiency.\n- For Pooling and Upsampling layers, Channels Last can use vectorized logic along the most inner dimension, e.g., \u201cC\u201d, while Channels First can\u2019t.\n\nFor memory format non aware layers, Channels Last and Channels First has the same performance.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Conclusion & Future Work\n\nIn this blog we introduced fundamental concepts of Channels Last and demonstrated the performance benefits of CPU using Channels Last on vision models. The current work is limited to 2D models at the current stage, and we will extend the optimization effort to 3D models in near future!", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgement\n\nThe results presented in this blog is a joint effort of Meta and Intel PyTorch team. Special thanks to Vitaly Fedyunin and Wei Wei from Meta who spent precious time and gave substantial assistance! Together we made one more step on the path of improving the PyTorch CPU eco system.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "References\n\n- [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html)\n- [oneDNN guide on memory formats](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html)\n- [PyTorch operators with Channels Last support](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the PyTorch Enterprise Support Program'\nauthor: Team PyTorch\n---", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "Today, we are excited to announce the [PyTorch Enterprise Support Program](http://pytorch.org/enterprise-support-program), a participatory program that enables service providers to develop and offer tailored enterprise-grade support to their customers. This new offering, built in collaboration between Facebook and Microsoft, was created in direct response to feedback from PyTorch enterprise users who are developing models in production at scale for mission-critical applications.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch Enterprise Support Program is available to any service provider. It is designed to mutually benefit all program Participants by sharing and improving PyTorch long-term support (LTS), including contributions of hotfixes and other improvements found while working closely with customers and on their systems.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "To benefit the open source community, all hotfixes developed by Participants will be tested and fed back to the LTS releases of PyTorch regularly through PyTorch\u2019s standard pull request process. To participate in the program, a service provider must apply and meet a set of program terms and certification requirements. Once accepted, the service provider becomes a program Participant and can offer a packaged PyTorch Enterprise support service with LTS, prioritized troubleshooting, useful integrations, and", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "more.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "As one of the founding members and an inaugural member of the PyTorch Enterprise Support Program, Microsoft is launching [PyTorch Enterprise on Microsoft Azure](https://Aka.ms/PyTorchEnterpriseHeroBlog) to deliver a reliable production experience for PyTorch users. Microsoft will support each PyTorch release for as long as it is current. In addition, it will support selected releases for two years, enabling a stable production experience. Microsoft Premier and Unified Support customers can access", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "prioritized troubleshooting for hotfixes, bugs, and security patches at no additional cost. Microsoft will extensively test PyTorch releases for performance regression. The latest release of PyTorch will be integrated with [Azure Machine Learning](https://azure.microsoft.com/en-us/services/machine-learning/) and other PyTorch add-ons including [ONNX Runtime](https://www.onnxruntime.ai/) for faster inference.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Enterprise on Microsoft Azure not only benefits its customers, but also the PyTorch community users. All improvements will be tested and fed back to the future release for PyTorch so everyone in the community can use them.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "As an organization or PyTorch user, the standard way of researching and deploying with different release versions of PyTorch does not change. If your organization is looking for the managed long-term support, prioritized patches, bug fixes, and additional enterprise-grade support, then you should reach out to service providers participating in the program.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} -{"page_content": "To learn more and participate in the program as a service provider, visit the [PyTorch Enterprise Support Program](http://pytorch.org/enterprise-support-program). If you want to learn more about Microsoft\u2019s offering, visit [PyTorch Enterprise on Microsoft Azure](https://Aka.ms/PyTorchEnterpriseHeroBlog).\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} +{"page_content": "```bash\n# note we are now in my home directory\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import sys\n>>> sys.path\n['', '/home/killeent/local/miniconda2/envs/p3/lib/python36.zip', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/lib-dynload', '/home/killeent/.local/lib/python3.6/site-packages', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages', '/home/killeent/github/pytorch', '/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg']\n```\n\nAs we can see, the `site-packages` directory we copied our PyTorch installation to is part of search path. Now let's load the `torch` module and see its location:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```python\n>>> import torch\n>>> import inspect\n>>> inspect.getfile(torch)\n'/home/killeent/local/miniconda2/envs/p3/lib/python3.6/site-packages/torch/__init__.py'\n```\n\nAs we can see, we have loaded the module from `site_packages` as expected - and our build and installation is successful!\n\n**Note:** Python prepends the empty string to `sys.path` to represent the current working directory - making it the first place we search for a module. So if we run Python from the pytorch directory, we would accidentally load the local version of PyTorch rather than our installed version. This is something to watch out for.\n\n### Addendum - Developer Efficiency, 3rd Party Libraries, Things I Didn't Cover", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "The entire installation loop for PyTorch can be quite time-consuming. On my devserver, it takes around 5 minutes for an installation from source. Often times, when developing PyTorch, we only want to work on a subset of the entire project, and re-build only that subset in order to test changes. Fortunately, our build system enables this.\n\n### Setuptools Develop Mode\n\nThe main tool that supports this is Setuptools `develop` command. The documentation states that:\n\n>This command allows you to deploy your project\u2019s source for use in one or more \u201cstaging areas\u201d where it will be available for importing. This deployment is done in such a way that changes to the project source are immediately available in the staging area(s), without needing to run a build or install step after each change.", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "But how does it work? Suppose we run `python setup.py build develop` in the PyTorch directory. The `build` command is run, building our dependencies (`TH`, `THPP`, etc.) and the extension libraries. However, if we look inside `site-packages`:\n\n```bash\n(p3) killeent@devgpu047:site-packages$ ls -la torch*\n-rw-r--r--. 1 killeent users 31 Jun 27 08:02 torch.egg-link\n```\n\nLooking at the contents of the `torch.egg-link` file, it simply references the PyTorch directory:\n\n```bash\n(p3) killeent@devgpu047:site-packages$ cat torch.egg-link\n/home/killeent/github/pytorch\n```\n\nIf we navigate back to the PyTorch directory, we see there is a new directory `torch.egg-info`:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n(p3) killeent@devgpu047:pytorch (master)$ ls -la torch.egg-info/\ntotal 28\ndrwxr-xr-x. 2 killeent users 4096 Jun 27 08:09 .\ndrwxr-xr-x. 10 killeent users 4096 Jun 27 08:01 ..\n-rw-r--r--. 1 killeent users 1 Jun 27 08:01 dependency_links.txt\n-rw-r--r--. 1 killeent users 255 Jun 27 08:01 PKG-INFO\n-rw-r--r--. 1 killeent users 7 Jun 27 08:01 requires.txt\n-rw-r--r--. 1 killeent users 16080 Jun 27 08:01 SOURCES.txt\n-rw-r--r--. 1 killeent users 12 Jun 27 08:01 top_level.txt\n```\n\nThis file contains metadata about the PyTorch project. For example, `requirements.txt` lists all of the dependencies for setting up PyTorch:\n\n```bash\n(p3) killeent@devgpu047:pytorch (master)$ cat torch.egg-info/requires.txt\npyyaml\n```\n\nWithout going into too much detail, `develop` allows us to essentially treat the PyTorch repo itself as if it were in `site-packages`, so we can import the module and it just works:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import torch\n>>> torch.__file__\n'/home/killeent/github/pytorch/torch/__init__.py'\n```\n\nAs a result, the following consequences hold:\n\n- If we change a Python source file, the changes are automatically picked up, and we don't have to run any commands to let the Python interpreter *see* this change\n- If we change a C++ Source File in one of the extension libraries, we can re-run the `develop` command, it will re-build the extension\n\nThus we can develop the PyTorch codebases seamlessly, and test our changes in an easy way.\n\n#### Working on the Dependency Libraries", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "If we are working on the dependencies (e.g. `TH`, `THPP`, etc.) we can re-build our changes more quickly by simply running the `build_deps` command directly. This will automatically call into `build_all.sh` to re-build our libraries, and copy the generated libraries appropriately. If we are using Setuptools `develop` mode, we will be using the local extension library built in the PyTorch directory. Because we have specified the paths to the shared libraries when compiling our Extension Libraries, the changes will be picked up:\n\n```bash\n# we are using the local extension\n(p3) killeent@devgpu047:~$ python\nPython 3.6.1 |Continuum Analytics, Inc.| (default, Mar 22 2017, 19:54:23)\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import torch\n>>> torch._C.__file__\n'/home/killeent/github/pytorch/torch/_C.cpython-36m-x86_64-linux-gnu.so'", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "# it references the local shared object library we just re-built\n(p3) killeent@devgpu047:~$ ldd /home/killeent/github/pytorch/torch/_C.cpython-36m-x86_64-linux-gnu.so\n# ...\nlibTH.so.1 => /home/killeent/github/pytorch/torch/lib/libTH.so.1 (0x00007f543d0e2000)\n# ...\n```\n\nAs such, we can test any changes here without having to do a full rebuild.\n\n#### 3rd Party Libraries\n\nPyTorch has dependencies on some 3rd party libraries. The usual mechanism for using these libraries is to install them via Anaconda, and then link against them. For example, we can use the `mkl` library with PyTorch by doing:\n\n```bash\n# installed to miniconda2/envs/p3/lib/libmkl_intel_lp64.so\nconda install mkl\n```\n\nAnd then as long as we have the path to this `lib` directory on our `$CMAKE_PREFIX_PATH`, it will successfully find this library when compiling:", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "```bash\n# in the site-packages dir\n(p3) killeent@devgpu047:torch$ ldd _C.cpython-36m-x86_64-linux-gnu.so\n# ...\nlibmkl_intel_lp64.so => /home/killeent/local/miniconda2/envs/p3/lib/libmkl_intel_lp64.so (0x00007f3450bba000)\n# ...\n```\n\n### Not Covered, But Also Relevant\n\n- How `ccache` is used to speed up build times\n- How PyTorch's top-level `__init__.py` file handles the initial module import and pulling together all the various modules and extension libraries\n- The CMake build system, how the backend libraries are configured and built with CMake", "metadata": {"source": "https://pytorch.org/blog/a-tour-of-pytorch-internals-2/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'An Overview of the PyTorch Mobile Demo Apps'\nauthor: Jeff Tang and Mark Saroufim\nfeatured-img: 'assets/images/android-demo-app.png'\ndate: 2021-06-18 12:00:00 -0500\n---\n\n\nPyTorch Mobile provides a runtime environment to execute state-of-the-art machine learning models on mobile devices. Latency is reduced, privacy preserved, and models can run on mobile devices anytime, anywhere.\n\nIn this blog post, we provide a quick overview of 10 currently available PyTorch Mobile powered demo apps running various state-of-the-art PyTorch 1.9 machine learning models spanning images, video, audio and text.\n\nIt\u2019s never been easier to deploy a state-of-the-art ML model to a phone. You don\u2019t need any domain knowledge in Machine Learning and we hope one of the below examples resonates enough with you to be the starting point for your next project.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "## Computer Vision\n### Image Classification\nThis app demonstrates how to use PyTorch C++ libraries on iOS and Android to classify a static image with the MobileNetv2/3 model.\n\n [iOS #1](https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld) [iOS #2](https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruIOS) [Android #1](https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp) [Android #2](https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruAndroid)\n\n [iOS](https://youtu.be/amTepUIR93k) [Android](https://youtu.be/5Lxuu16_28o)\n\n
\n \n
\n\n\n### Live Image Classification\nThis app demonstrates how to run a quantized MobileNetV2 and Resnet18 models to classify images in real time with an iOS and Android device camera.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": " [iOS](https://github.com/pytorch/ios-demo-app/tree/master/PyTorchDemo) [Android](https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp)\n\n
\n\n\n
\n\n\n### Image Segmentation\nThis app demonstrates how to use the PyTorch DeepLabV3 model to segment images. The updated app for PyTorch 1.9 also demonstrates how to create the model using the Mobile Interpreter and load the model with the LiteModuleLoader API.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ImageSegmentation) [Android](https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": " [iOS](https://pytorch.org/tutorials/beginner/deeplabv3_on_ios.html) [Android](https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation)\n\n
\n \n
\n\n\n### Vision Transformer for Handwritten Digit Recognition\nThis app demonstrates how to use Facebook's latest optimized Vision Transformer DeiT model to do image classification and handwritten digit recognition.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ViT4MNIST) [Android](https://github.com/pytorch/android-demo-app/tree/master/ViT4MNIST)\n\n [Android](https://drive.google.com/file/d/11L5mIjrLn7B7VdwjQl5vJv3ZVK4hcYut/view?usp=sharing)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n\n### Object Detection\nThis app demonstrates how to convert the popular YOLOv5 model and use it on an iOS app that detects objects from pictures in your photos, taken with camera, or with live camera.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/ObjectDetection) [Android](https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection)\n\n [iOS](https://drive.google.com/file/d/1pIDrUDnCD5uF-mIz8nbSlZcXxPlRBKhl/view) [Android](https://drive.google.com/file/d/1-5AoRONUqZPZByM-fy0m7r8Ct11OnlIT/view)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "### D2Go\nThis app demonstrates how to create and use a much lighter and faster Facebook D2Go model to detect objects from pictures in your photos, taken with camera, or with live camera.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/D2Go) [Android](https://github.com/pytorch/android-demo-app/tree/master/D2Go)\n\n [iOS](https://drive.google.com/file/d/1GO2Ykfv5ut2Mfoc06Y3QUTFkS7407YA4/view) [Android](https://drive.google.com/file/d/18-2hLc-7JAKtd1q00X-5pHQCAdyJg7dZ/view?usp=sharing)\n\n\n
\n \n
\n\n\n## Video\n### Video Classification\nThis app demonstrates how to use a pre-trained PyTorchVideo model to perform video classification on tested videos, videos from the Photos library, or even real-time videos.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": " [iOS](https://github.com/pytorch/ios-demo-app/tree/master/TorchVideo) [Android](https://github.com/pytorch/android-demo-app/tree/master/TorchVideo)\n\n [iOS](https://drive.google.com/file/d/1ijb4UIuF2VQiab4xfAsBwrQXCInvb9wd/view) [Android](https://drive.google.com/file/d/193tkZgt5Rlk7u-EQPcvkoFtmOQ14-zCC/view) [Deep Dive](https://www.youtube.com/watch?v=Qb4vDm-ruwI)\n\n
\n \n
\n\n\n\n## Natural Language Processing\n### Text Classification\nThis app demonstrates how to use a pre-trained Reddit model to perform text classification.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/PyTorchDemo) [Android](https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp)", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n\n### Machine Translation\nThis app demonstrates how to convert a sequence-to-sequence neural machine translation model trained with the code in the PyTorch NMT tutorial for french to english translation.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/Seq2SeqNMT) [Android](https://github.com/pytorch/android-demo-app/tree/master/Seq2SeqNMT)\n\n [iOS](https://drive.google.com/file/d/17Edk-yAyfzijHPR_2ZDAIX7VY-TkQnLf/view) [Android](https://drive.google.com/file/d/110KN3Pa9DprkBWnzj8Ppa8KMymhmBI61/view?usp=sharing)\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "### Question Answering\nThis app demonstrates how to use the DistilBERT Hugging Face transformer model to answer questions about Pytorch Mobile itself.\n\n [iOS](https://github.com/pytorch/ios-demo-app/tree/master/QuestionAnswering) [Android](https://github.com/pytorch/android-demo-app/tree/master/QuestionAnswering)\n\n [iOS](https://drive.google.com/file/d/1QIB3yoP4I3zUU0bLCpvUqPV5Kv8f8JvB/view) [Android](https://drive.google.com/file/d/10hwGNFo5tylalKwut_CWFPJmV7JRdDKF/view?usp=sharing)\n\n
\n \n
\n\n\n## Audio\n### Speech Recognition\nThis app demonstrates how to convert Facebook AI's torchaudio-powered wav2vec 2.0, one of the leading models in speech recognition to TorchScript before deploying it.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": " [iOS](https://github.com/pytorch/ios-demo-app/tree/master/SpeechRecognition) [Android](https://github.com/pytorch/android-demo-app/tree/master/SpeechRecognition)\n\n
\n \n
\n\n\nWe really hope one of these demo apps stood out for you. For the full list, make sure to visit the [iOS](https://github.com/pytorch/ios-demo-app) and [Android](https://github.com/pytorch/android-demo-app) demo app repos. You should also definitely check out the video [An Overview of the PyTorch Mobile Demo Apps](https://www.youtube.com/watch?v=Qb4vDm-ruwI) which provides both an overview of the PyTorch mobile demo apps and a deep dive into the PyTorch Video app for iOS and Android.", "metadata": {"source": "https://pytorch.org/blog/mobile-demo-apps-overview/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerating PyTorch Vision Models with Channels Last on CPU\"\nauthor: Mingfei Ma (Intel), Vitaly Fedyunin (Meta), Wei Wei (Meta)\nfeatured-img: '/assets/images/accelerating-pytorch-vision-models-with-channels-last-on-cpu-2.png'\n---\n\n## Overview\n\nMemory formats has significant impact on performance when running vision models, generally Channels Last is a more favorable from performance perspective due to better data locality.\n\nThis blog will introduce fundamental concepts of memory formats and demonstrate performance benefits using Channels Last on popular PyTorch vision models on Intel\u00ae Xeon\u00ae Scalable processors.\n\n## Memory Formats Introduction\n\nMemory format refers to data representation that describes how a multidimensional (nD) array is stored in linear (1D) memory address space. The concept of memory format has two aspects:", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "- **Physical Order** is the layout of data storage in physical memory. For vision models, usually we talk about NCHW, NHWC. These are the descriptions of physical memory layout, also referred as Channels First and Channels Last respectively.\n- **Logical Order** is a convention on how to describe tensor shape and stride. In PyTorch, this convention is NCHW. No matter what the physical order is, tensor shape and stride will always be depicted in the order of NCHW.\n\nFig-1 is the physical memory layout of a tensor with shape of [1, 3, 4, 4] on both Channels First and Channels Last memory format (channels denoted as R, G, B respectively):\n\n

\n \n

\n\n

\nFig-1 Physical memory layout of Channels First and Channels Last\n

\n\n## Memory Formats Propagation", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "The general rule for PyTorch memory format propagation is to preserve the input tensor\u2019s memory format. Which means a Channels First input will generate a Channels First output and a Channels Last input will generate a Channels Last output. \n\nFor Convolution layers, PyTorch uses oneDNN (oneAPI Deep Neural Network Library) by default to achieve optimal performance on Intel CPUs. Since it is physically impossible to achieve highly optimized performance directly with Channels Frist memory format, input and weight are firstly converted to blocked format and then computed. oneDNN may choose different blocked formats according to input shapes, data type and hardware architecture, for vectorization and cache reuse purposes. The blocked format is opaque to PyTorch, so the output needs to be converted back to Channels First. Though blocked format would bring about optimal computing performance, the format conversions may add overhead and therefore offset the performance gain.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "On the other hand, oneDNN is optimized for Channels Last memory format to use it for optimal performance directly and PyTorch will simply pass a memory view to oneDNN. Which means the conversion of input and output tensor is saved. Fig-2 indicates memory format propagation behavior of convolution on PyTorch CPU (the solid arrow indicates a memory format conversion, and the dashed arrow indicates a memory view):\n\n

\n \n

\n\n

\nFig-2 CPU Conv memory format propagation\n

\n\nOn PyTorch, the default memory format is Channels First. In case a particular operator doesn't have support on Channels Last, the NHWC input would be treated as a non-contiguous NCHW and therefore fallback to Channels First, which will consume the previous memory bandwidth on CPU and result in suboptimal performance.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "Therefore, it is very important to extend the scope of Channels Last support for optimal performance. And we have implemented Channels Last kernels for the commonly use operators in CV domain, applicable for both inference and training, such as:\n\n- Activations (e.g., ReLU, PReLU, etc.)\n- Convolution (e.g., Conv2d)\n- Normalization (e.g., BatchNorm2d, GroupNorm, etc.)\n- Pooling (e.g., AdaptiveAvgPool2d, MaxPool2d, etc.)\n- Shuffle (e.g., ChannelShuffle, PixelShuffle)\n\nRefer to [Operators-with-Channels-Last-support](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support) for details.\n\n## Native Level Optimization on Channels Last\n\nAs mentioned above, PyTorch uses oneDNN to achieve optimal performance on Intel CPUs for convolutions. The rest of memory format aware operators are optimized at PyTorch native level, which doesn\u2019t require any third-party library support.", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "- **Cache friendly parallelization scheme:** keep the same parallelization scheme for all the memory format aware operators, this will help increase data locality when passing each layer\u2019s output to the next.\n- **Vectorization on multiple archs:** generally, we can vectorize on the most inner dimension on Channels Last memory format. And each of the vectorized CPU kernels will be generated for both AVX2 and AVX512.\n\nWhile contributing to Channels Last kernels, we tried our best to optimize Channels First counterparts as well. The fact is some operators are physically impossible to achieve optimal performance on Channels First, such as Convolution, Pooling, etc.\n\n## Run Vision Models on Channels Last\n\nThe Channels Last related APIs are documented at [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html). Typically, we can convert a 4D tensor from Channels First to Channels Last by:", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "```python\n# convert x to channels last\n# suppose x\u2019s shape is (N, C, H, W)\n# then x\u2019s stride will be (HWC, 1, WC, C)\nx = x.to(memory_format=torch.channels_last)\n```\n\nTo run models on Channels Last memory format, simply need to convert input and model to Channels Last and then you are ready to go. The following is a minimal example showing how to run ResNet50 with TorchVision on Channels Last memory format:\n\n```python\nimport torch\nfrom torchvision.models import resnet50\n\nN, C, H, W = 1, 3, 224, 224\nx = torch.rand(N, C, H, W)\nmodel = resnet50()\nmodel.eval()\n\n# convert input and model to channels last\nx = x.to(memory_format=torch.channels_last)\nmodel = model.to(memory_format=torch.channels_last)\nmodel(x)\n```\n\nThe Channels Last optimization is implemented at native kernel level, which means you may apply other functionalities such as torch.fx and torch script together with Channels Last as well.\n\n## Performance Gains", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "## Performance Gains\n\nWe benchmarked inference performance of TorchVision models on Intel\u00ae Xeon\u00ae Platinum 8380 CPU @ 2.3 GHz, single instance per socket (batch size = 2 x number of physical cores). Results show that Channels Last has 1.3x to 1.8x performance gain over Channels First.\n\n

\n \n

\n\nThe performance gain primarily comes from two aspects:\n\n- For Convolution layers, Channels Last saved the memory format conversion to blocked format for activations, which improves the overall computation efficiency.\n- For Pooling and Upsampling layers, Channels Last can use vectorized logic along the most inner dimension, e.g., \u201cC\u201d, while Channels First can\u2019t.\n\nFor memory format non aware layers, Channels Last and Channels First has the same performance.\n\n## Conclusion & Future Work", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "In this blog we introduced fundamental concepts of Channels Last and demonstrated the performance benefits of CPU using Channels Last on vision models. The current work is limited to 2D models at the current stage, and we will extend the optimization effort to 3D models in near future!\n\n## Acknowledgement\n\nThe results presented in this blog is a joint effort of Meta and Intel PyTorch team. Special thanks to Vitaly Fedyunin and Wei Wei from Meta who spent precious time and gave substantial assistance! Together we made one more step on the path of improving the PyTorch CPU eco system.\n\n## References\n\n- [PyTorch memory format tutorial](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html)\n- [oneDNN guide on memory formats](https://oneapi-src.github.io/oneDNN/dev_guide_understanding_memory_formats.html)\n- [PyTorch operators with Channels Last support](https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support)", "metadata": {"source": "https://pytorch.org/blog/accelerating-pytorch-vision-models-with-channels-last-on-cpu/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing the PyTorch Enterprise Support Program'\nauthor: Team PyTorch\n---\n\nToday, we are excited to announce the [PyTorch Enterprise Support Program](http://pytorch.org/enterprise-support-program), a participatory program that enables service providers to develop and offer tailored enterprise-grade support to their customers. This new offering, built in collaboration between Facebook and Microsoft, was created in direct response to feedback from PyTorch enterprise users who are developing models in production at scale for mission-critical applications.\n\nThe PyTorch Enterprise Support Program is available to any service provider. It is designed to mutually benefit all program Participants by sharing and improving PyTorch long-term support (LTS), including contributions of hotfixes and other improvements found while working closely with customers and on their systems.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} +{"page_content": "To benefit the open source community, all hotfixes developed by Participants will be tested and fed back to the LTS releases of PyTorch regularly through PyTorch\u2019s standard pull request process. To participate in the program, a service provider must apply and meet a set of program terms and certification requirements. Once accepted, the service provider becomes a program Participant and can offer a packaged PyTorch Enterprise support service with LTS, prioritized troubleshooting, useful integrations, and more.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} +{"page_content": "As one of the founding members and an inaugural member of the PyTorch Enterprise Support Program, Microsoft is launching [PyTorch Enterprise on Microsoft Azure](https://Aka.ms/PyTorchEnterpriseHeroBlog) to deliver a reliable production experience for PyTorch users. Microsoft will support each PyTorch release for as long as it is current. In addition, it will support selected releases for two years, enabling a stable production experience. Microsoft Premier and Unified Support customers can access prioritized troubleshooting for hotfixes, bugs, and security patches at no additional cost. Microsoft will extensively test PyTorch releases for performance regression. The latest release of PyTorch will be integrated with [Azure Machine Learning](https://azure.microsoft.com/en-us/services/machine-learning/) and other PyTorch add-ons including [ONNX Runtime](https://www.onnxruntime.ai/) for faster inference.", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} +{"page_content": "PyTorch Enterprise on Microsoft Azure not only benefits its customers, but also the PyTorch community users. All improvements will be tested and fed back to the future release for PyTorch so everyone in the community can use them.\n\nAs an organization or PyTorch user, the standard way of researching and deploying with different release versions of PyTorch does not change. If your organization is looking for the managed long-term support, prioritized patches, bug fixes, and additional enterprise-grade support, then you should reach out to service providers participating in the program.\n\nTo learn more and participate in the program as a service provider, visit the [PyTorch Enterprise Support Program](http://pytorch.org/enterprise-support-program). If you want to learn more about Microsoft\u2019s offering, visit [PyTorch Enterprise on Microsoft Azure](https://Aka.ms/PyTorchEnterpriseHeroBlog).\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/announcing-pytorch-enterprise/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'Everything You Need To Know About Torchvision\u2019s SSD Implementation'\nauthor: Vasilis Vryniotis\nfeatured-img: 'assets/images/prediction-examples.png'\n---\n\nIn TorchVision v0.10, we\u2019ve released two new Object Detection models based on the SSD architecture. Our plan is to cover the key implementation details of the algorithms along with information on how they were trained in a two-part article.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "In part 1 of the series, we will focus on the original implementation of the SSD algorithm as described on the [Single Shot MultiBox Detector paper](https://arxiv.org/abs/1512.02325). We will briefly give a high-level description of how the algorithm works, then go through its main components, highlight key parts of its code, and finally discuss how we trained the released model. Our goal is to cover all the necessary details to reproduce the model including those optimizations which are not covered on the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "paper but are part on the [original implementation](https://github.com/weiliu89/caffe/tree/ssd).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "# How Does SSD Work?\n\nReading the aforementioned paper is highly recommended but here is a quick oversimplified refresher. Our target is to detect the locations of objects in an image along with their categories. Here is the Figure 5 from the [SSD paper](https://arxiv.org/abs/1512.02325) with prediction examples of the model:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "The SSD algorithm uses a CNN backbone, passes the input image through it and takes the convolutional outputs from different levels of the network. The list of these outputs are called feature maps. These feature maps are then passed through the Classification and Regression heads which are responsible for predicting the class and the location of the boxes.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Since the feature maps of each image contain outputs from different levels of the network, their size varies and thus they can capture objects of different dimensions. On top of each, we tile several default boxes which can be thought as our rough prior guesses. For each default box, we predict whether there is an object (along with its class) and its offset (correction over the original location). During training time, we need to first match the ground truth to the default boxes and then we use those", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "matches to estimate our loss. During inference, similar prediction boxes are combined to estimate the final predictions.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "# The SSD Network Architecture\n\nIn this section, we will discuss the key components of SSD. Our code follows closely [the paper](https://arxiv.org/abs/1512.02325) and makes use of many of the undocumented optimizations included in [the official implementation](https://github.com/weiliu89/caffe/tree/ssd).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "DefaultBoxGenerator", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "The [DefaultBoxGenerator class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L134) is responsible for generating the default boxes of SSD and operates similarly to the [AnchorGenerator](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L9) of FasterRCNN (for more info on their differences see pages 4-6 of the paper). It produces a set of predefined boxes", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "of specific width and height which are tiled across the image and serve as the first rough prior guesses of where objects might be located. Here is Figure 1 from the SSD paper with a visualization of ground truths and default boxes:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "The class is parameterized by a set of hyperparameters that control [their shape](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L139) and [tiling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L140-L149). The implementation will provide [automatically good", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "guesses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L162-L171) with the default parameters for those who want to experiment with new backbones/datasets but one can also pass [optimized custom values](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L144-L147).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "SSDMatcher", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "The [SSDMatcher class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L348) extends the standard [Matcher](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L227) used by FasterRCNN and it is responsible for matching the default boxes to the ground truth. After estimating the [IoUs of all", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "combinations](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L349), we use the matcher to find for each default box the best [candidate](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L296) ground truth with overlap higher than the [IoU", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "threshold](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L350-L351). The SSD version of the matcher has an extra step to ensure that each ground truth is matched with the default box that has the [highest overlap](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L356-L360). The results of the matcher are used in the loss estimation during the training process of the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "model.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Classification and Regression Heads\n\nThe [SSDHead class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L38) is responsible for initializing the Classification and Regression parts of the network. Here are a few notable details about their code:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* Both the [Classification](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L90) and the [Regression](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L99) head inherit from the [same class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L51) which is responsible for making the predictions for each feature", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "map.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* Each level of the feature map uses a separate 3x3 Convolution to estimate the [class logits](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L92-L94) and [box locations](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L101-L103).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* The [number of predictions](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L79) that each head makes per level depends on the number of default boxes and the sizes of the feature maps.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Backbone Feature Extractor\n\nThe [feature extractor](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L413) reconfigures and enhances a standard VGG backbone with extra layers as depicted on the Figure 2 of the SSD paper: \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "The class supports all VGG models of TorchVision and one can create a similar extractor class for other types of CNNs (see [this example for ResNet](https://github.com/pytorch/vision/blob/644bdcdc438c1723714950d0771da76333b53954/torchvision/models/detection/ssd.py#L600)). Here are a few implementation details of the class:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* [Patching](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L419-L420) the ```ceil_mode parameter``` of the 3rd Maxpool layer is necessary to get the same feature map sizes as the paper. This is due to small differences between PyTorch and the original Caffe implementation of the model.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* It adds a series of [extra feature layers](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L430-L456)on top of VGG. If the highres parameter is ```True``` during its construction, it will append an [extra convolution](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L457-L464). This is useful for the SSD512 version of the model.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* As discussed on section 3 of the paper, the fully connected layers of the original VGG are converted to convolutions with the [first one using Atrous](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L469). Moreover maxpool5\u2019s stride and kernel size is [modified](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L468).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* As described on section 3.1, L2 normalization is used on the [output of conv4_3](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L484) and a set of [learnable weights](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L422-L423) are introduced to control its scaling.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "SSD Algorithm\n\nThe final key piece of the implementation is on the [SSD class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L108). Here are some notable details:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* The algorithm is [parameterized](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L167-L176) by a set of arguments similar to other detection models. The mandatory parameters are: the backbone which is responsible for [estimating the feature maps](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L137-L139), the ```anchor_generator``` which should be a [configured", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "instance](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L140-L141) of the ```DefaultBoxGenerator``` class, the size to which the [input images](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L142-L143) will be resized and the ```num_classes``` for classification", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "[excluding](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L144) the background.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* If a [head](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L150-L151) is not provided, the constructor will [initialize](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L194) the default ```SSDHead```. To do so, we need to know the number of output channels for each feature map produced by the backbone. Initially we try to [retrieve this", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "information](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L186) from the backbone but if not available we will [dynamically estimate it](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L189).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* The algorithm [reuses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L183) the standard [BoxCoder class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L129) used by other Detection models. The class is responsible for [encoding and decoding](https://leimao.github.io/blog/Bounding-Box-Encoding-Decoding/) the bounding boxes and is configured to use the same prior", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "variances as the [original implementation](https://github.com/weiliu89/caffe/blob/2c4e4c2899ad7c3a997afef2c1fbac76adca1bad/examples/ssd/ssd_coco.py#L326).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* Though we reuse the standard [GeneralizedRCNNTransform class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/transform.py#L64) to resize and normalize the input images, the SSD algorithm [configures](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L203-L204) it to ensure that the image size will remain fixed.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "In part 1 of the series, we will focus on the original implementation of the SSD algorithm as described on the [Single Shot MultiBox Detector paper](https://arxiv.org/abs/1512.02325). We will briefly give a high-level description of how the algorithm works, then go through its main components, highlight key parts of its code, and finally discuss how we trained the released model. Our goal is to cover all the necessary details to reproduce the model including those optimizations which are not covered on the paper but are part on the [original implementation](https://github.com/weiliu89/caffe/tree/ssd).\n\n# How Does SSD Work?\n\nReading the aforementioned paper is highly recommended but here is a quick oversimplified refresher. Our target is to detect the locations of objects in an image along with their categories. Here is the Figure 5 from the [SSD paper](https://arxiv.org/abs/1512.02325) with prediction examples of the model:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\nThe SSD algorithm uses a CNN backbone, passes the input image through it and takes the convolutional outputs from different levels of the network. The list of these outputs are called feature maps. These feature maps are then passed through the Classification and Regression heads which are responsible for predicting the class and the location of the boxes.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "Since the feature maps of each image contain outputs from different levels of the network, their size varies and thus they can capture objects of different dimensions. On top of each, we tile several default boxes which can be thought as our rough prior guesses. For each default box, we predict whether there is an object (along with its class) and its offset (correction over the original location). During training time, we need to first match the ground truth to the default boxes and then we use those matches to estimate our loss. During inference, similar prediction boxes are combined to estimate the final predictions. \n\n# The SSD Network Architecture\n\nIn this section, we will discuss the key components of SSD. Our code follows closely [the paper](https://arxiv.org/abs/1512.02325) and makes use of many of the undocumented optimizations included in [the official implementation](https://github.com/weiliu89/caffe/tree/ssd).\n\n### DefaultBoxGenerator", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "The [DefaultBoxGenerator class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L134) is responsible for generating the default boxes of SSD and operates similarly to the [AnchorGenerator](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L9) of FasterRCNN (for more info on their differences see pages 4-6 of the paper). It produces a set of predefined boxes of specific width and height which are tiled across the image and serve as the first rough prior guesses of where objects might be located. Here is Figure 1 from the SSD paper with a visualization of ground truths and default boxes:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "The class is parameterized by a set of hyperparameters that control [their shape](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L139) and [tiling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L140-L149). The implementation will provide [automatically good guesses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L162-L171) with the default parameters for those who want to experiment with new backbones/datasets but one can also pass [optimized custom values](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/anchor_utils.py#L144-L147).\n\n### SSDMatcher", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "The [SSDMatcher class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L348) extends the standard [Matcher](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L227) used by FasterRCNN and it is responsible for matching the default boxes to the ground truth. After estimating the [IoUs of all combinations](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L349), we use the matcher to find for each default box the best [candidate](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L296) ground truth with overlap higher than the [IoU threshold](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L350-L351). The SSD version of the matcher has an extra step to ensure that each ground truth is matched with the default box that has the [highest overlap](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L356-L360). The results of the matcher are used in the loss estimation during the training process of the model.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "### Classification and Regression Heads\n\nThe [SSDHead class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L38) is responsible for initializing the Classification and Regression parts of the network. Here are a few notable details about their code:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "* Both the [Classification](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L90) and the [Regression](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L99) head inherit from the [same class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L51) which is responsible for making the predictions for each feature map. \n* Each level of the feature map uses a separate 3x3 Convolution to estimate the [class logits](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L92-L94) and [box locations](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L101-L103). \n* The [number of predictions](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L79) that each head makes per level depends on the number of default boxes and the sizes of the feature maps.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "### Backbone Feature Extractor\n\nThe [feature extractor](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L413) reconfigures and enhances a standard VGG backbone with extra layers as depicted on the Figure 2 of the SSD paper: \n\n
\n \n
\n\nThe class supports all VGG models of TorchVision and one can create a similar extractor class for other types of CNNs (see [this example for ResNet](https://github.com/pytorch/vision/blob/644bdcdc438c1723714950d0771da76333b53954/torchvision/models/detection/ssd.py#L600)). Here are a few implementation details of the class:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "* [Patching](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L419-L420) the ```ceil_mode parameter``` of the 3rd Maxpool layer is necessary to get the same feature map sizes as the paper. This is due to small differences between PyTorch and the original Caffe implementation of the model. \n* It adds a series of [extra feature layers](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L430-L456)on top of VGG. If the highres parameter is ```True``` during its construction, it will append an [extra convolution](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L457-L464). This is useful for the SSD512 version of the model.\n* As discussed on section 3 of the paper, the fully connected layers of the original VGG are converted to convolutions with the [first one using Atrous](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L469). Moreover maxpool5\u2019s stride and kernel size is [modified](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L468).\n* As described on section 3.1, L2 normalization is used on the [output of conv4_3](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L484) and a set of [learnable weights](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L422-L423) are introduced to control its scaling.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "### SSD Algorithm\n\nThe final key piece of the implementation is on the [SSD class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L108). Here are some notable details:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "* The algorithm is [parameterized](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L167-L176) by a set of arguments similar to other detection models. The mandatory parameters are: the backbone which is responsible for [estimating the feature maps](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L137-L139), the ```anchor_generator``` which should be a [configured instance](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L140-L141) of the ```DefaultBoxGenerator``` class, the size to which the [input images](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L142-L143) will be resized and the ```num_classes``` for classification [excluding](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L144) the background.\n* If a [head](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L150-L151) is not provided, the constructor will [initialize](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L194) the default ```SSDHead```. To do so, we need to know the number of output channels for each feature map produced by the backbone. Initially we try to [retrieve this information](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L186) from the backbone but if not available we will [dynamically estimate it](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L189).\n* The algorithm [reuses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L183) the standard [BoxCoder class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/_utils.py#L129) used by other Detection models. The class is responsible for [encoding and decoding](https://leimao.github.io/blog/Bounding-Box-Encoding-Decoding/) the bounding boxes and is configured to use the same prior variances as the [original implementation](https://github.com/weiliu89/caffe/blob/2c4e4c2899ad7c3a997afef2c1fbac76adca1bad/examples/ssd/ssd_coco.py#L326).\n* Though we reuse the standard [GeneralizedRCNNTransform class](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/transform.py#L64) to resize and normalize the input images, the SSD algorithm [configures](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L203-L204) it to ensure that the image size will remain fixed.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} {"page_content": "Here are the two core methods of the implementation:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* The ```compute_loss``` method estimates the standard Multi-box loss as described on page 5 of the SSD paper. It uses the [smooth L1 loss](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L244) for regression and the standard [cross-entropy loss](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L262-L266) with [hard-negative", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "sampling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L268-L276) for classification.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* As in all detection models, the forward method currently has different behaviour depending on whether the model is on training or eval mode. It starts by [resizing & normalizing the input images](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L309-L310) and then [passes them through the backbone](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L324-L325) to get the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "feature maps. The feature maps are then [passed through the head](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L331-L332) to get the predictions and then the method [generates the default boxes](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L334-L335).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* If the model is on [training mode](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L339-L352), the forward will estimate the [IoUs of the default boxes with the ground truth](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L349), use the ```SSDmatcher``` to [produce", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "matches](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L350) and finally [estimate the losses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L352) by calling the ```compute_loss method```.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "* If the model is on [eval mode](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L353-L355), we first select the best detections by keeping only the ones that [pass the score threshold](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L384), select the [most promising", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "boxes](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L388-L391) and run NMS to [clean up and select](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L401-L403) the best predictions. Finally we [postprocess the predictions](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L355) to resize them to the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "original image size.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "# The SSD300 VGG16 Model\n\nThe SSD is a family of models because it can be configured with different backbones and different Head configurations. In this section, we will focus on the provided [SSD pre-trained model](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L522-L523). We will discuss the details of its configuration and the training process used to reproduce the reported results.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Training process\n\nThe model was trained using the COCO dataset and all of its hyper-parameters and scripts can be found in our [references](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssd300-vgg16) folder. Below we provide details on the most notable aspects of the training process.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Paper Hyperparameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "In order to achieve the best possible results on COCO, we adopted the hyperparameters described on the section 3 of the paper concerning the optimizer configuration, the weight regularization etc. Moreover we found it useful to adopt the optimizations that appear in the [official implementation](https://github.com/weiliu89/caffe/blob/ssd/examples/ssd/ssd_coco.py#L310-L321) concerning the [tiling", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "configuration](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L579-L581) of the DefaultBox generator. This optimization was not described in the paper but it was crucial for improving the detection precision of smaller objects.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Data Augmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Implementing the [SSD Data Augmentation strategy](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/references/detection/transforms.py#L20-L239) as described on page 6 and page 12 of the paper was critical to reproducing the results. More specifically the use of random \u201cZoom In\u201d and \u201cZoom Out\u201d transformations make the model robust to various input sizes and improve its precision on the small and medium objects. Finally since the VGG16 has quite a few parameters, the photometric", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "distortions [included in the augmentations](https://github.com/pytorch/vision/blob/43d772067fe77965ec8fc49c799de5cea44b8aa2/references/detection/presets.py#L11-L18) have a regularization effect and help avoid the overfitting.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Weight Initialization & Input Scaling", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Another aspect that we found beneficial was to follow the [weight initialization scheme](https://github.com/intel/caffe/blob/master/models/intel_optimized_models/ssd/VGGNet/coco/SSD_300x300/train.prototxt) proposed by the paper. To do that, we had to adapt our input scaling method by [undoing the 0-1 scaling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L583-L587) performed by ```ToTensor()``` and use [pre-trained ImageNet", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "weights](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L24-L26) fitted with this scaling (shoutout to [Max deGroot](https://github.com/amdegroot) for providing them in his repo). All the weights of new convolutions were [initialized using Xavier](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L30-L35) and their biases were set to zero. After initialization, the network", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "was [trained end-to-end](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L571-L572).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "LR Scheme", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "As reported on the paper, after applying aggressive data augmentations it\u2019s necessary to train the models for longer. Our experiments confirm this and we had to tweak the Learning rate, batch sizes and overall steps to achieve the best results. Our [proposed learning scheme](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssd300-vgg16) is configured to be rather on the safe side, showed signs of plateauing between the steps and thus one is", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "likely to be able to train a similar model by doing only 66% of our epochs.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "# Breakdown of Key Accuracy Improvements", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "It is important to note that implementing a model directly from a paper is an iterative process that circles between coding, training, bug fixing and adapting the configuration until we match the accuracies reported on the paper. Quite often it also involves simplifying the training recipe or enhancing it with more recent methodologies. It is definitely not a linear process where incremental accuracy improvements are achieved by improving a single direction at a time but instead involves exploring different", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "hypothesis, making incremental improvements in different aspects and doing a lot of backtracking.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "With that in mind, below we try to summarize the optimizations that affected our accuracy the most. We did this by grouping together the various experiments in 4 main groups and attributing the experiment improvements to the closest match. Note that the Y-axis of the graph starts from 18 instead from 0 to make the difference between optimizations more visible:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Model Configuration | mAP delta | mAP | \n| ------------- | ------------- | ------------- |\n| Baseline with \"FasterRCNN-style\" Hyperparams | - | 19.5 | \n| + Paper Hyperparams | 1.6 | 21.1 | \n| + Data Augmentation | 1.8 | 22.9 | \n| + Weight Initialization & Input Scaling | 1 | 23.9 | \n| + LR scheme | 1.2 | 25.1 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "Our final model achieves an mAP of 25.1 and reproduces exactly the COCO results reported on the paper. Here is a [detailed breakdown](https://github.com/pytorch/vision/pull/3403) of the accuracy metrics.\n\n\nWe hope you found the part 1 of the series interesting. On the part 2, we will focus on the implementation of SSDlite and discuss its differences from SSD. Until then, we are looking forward to your feedback.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.13 release, including beta versions of functorch and improved support for Apple\u2019s new M1 chips.\"\nauthor: Team PyTorch\nfeatured-img: \"/assets/images/blog-2022-10-25-Pytorch-1.13-Release.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the release of PyTorch\u00ae 1.13 ([release note](https://github.com/pytorch/pytorch/releases/tag/v1.13.0))! This includes Stable versions of BetterTransformer. We deprecated CUDA 10.2 and 11.3 and completed migration of CUDA 11.6 and 11.7. Beta includes improved support for Apple M1 chips and functorch, a library that offers composable vmap (vectorization) and autodiff transforms, being included in-tree with the PyTorch release. This release is composed of over 3,749", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "commits and 467 contributors since 1.12.1. We want to sincerely thank our dedicated community for your contributions.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Summary:\n\n- The [BetterTransformer](#stable-features) feature set supports fastpath execution for common Transformer models during Inference out-of-the-box, without the need to modify the model. Additional improvements include accelerated add+matmul linear algebra kernels for sizes commonly used in Transformer models and Nested Tensors is now enabled by default.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "- Timely [deprecating older CUDA versions](#introduction-of-cuda-116-and-117-and-deprecation-of-cuda-102-and-113) allows us to proceed with introducing the latest CUDA version as they are introduced by Nvidia\u00ae, and hence allows support for C++17 in PyTorch and new NVIDIA Open GPU Kernel Modules.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "- Previously, [functorch](#beta-features) was released out-of-tree in a separate package. After installing PyTorch, a user will be able to `import functorch` and use functorch without needing to install another package.\n\n- PyTorch is offering native builds for Apple\u00ae silicon machines that use Apple's new [M1 chip](#beta-support-for-m1-devices) as a beta feature, providing improved support across PyTorch's APIs.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "
\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "\n \n
StableBetaPrototype
Better Transformer Enable Intel\u00ae VTune\u2122 Profiler\u2019s Instrumentation and Tracing Technology APIs Arm\u00ae Compute Library backend support for AWS Graviton
CUDA 10.2 and 11.3 CI/CD Deprecation Extend NNC to support channels last and bf16 CUDA Sanitizer
 Functorch now in PyTorch Core Library 
  Beta Support for M1 devices 
\n
", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Along with 1.13, we are also releasing major updates to the PyTorch libraries, more details can be found in this [blog](https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Stable Features", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Stable) BetterTransformer API", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "The [BetterTransformer](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/) feature set, first released in PyTorch 1.12, is stable. PyTorch BetterTransformer supports fastpath execution for common Transformer models during Inference out-of-the-box, without the need to modify the model. To complement the improvements in Better Transformer, we have also accelerated add+matmul linear algebra kernels for sizes commonly used in Transformer models.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Reflecting the performance benefits for many NLP users, Nested Tensors use for Better Transformer is now enabled by default. To ensure compatibility, a mask check is performed to ensure a contiguous mask is supplied. In Transformer Encoder, the mask check for src_key_padding_mask may be suppressed by setting mask_check=False. This accelerates processing for users than can guarantee that only aligned masks are provided. Finally, better error messages are provided to diagnose incorrect inputs, together with", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "improved diagnostics why fastpath execution cannot be used.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Better Transformer is directly integrated into the PyTorch TorchText library, enabling TorchText users to transparently and automatically take advantage of BetterTransformer speed and efficiency performance. ([Tutorial](https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html))\n\n

\n\n

\n\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure: BetterTransformer fastpath execution is now stable and enables sparsity optimization using Nested Tensor representation as default\n

", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Introduction of CUDA 11.6 and 11.7 and deprecation of CUDA 10.2 and 11.3\n\nTimely deprecating older CUDA versions allows us to proceed with introducing the latest CUDA version as they are introduced by Nvidia\u00ae, and hence allows developers to use the latest features of CUDA and benefit from correctness fixes provided by the latest version.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Decommissioning of CUDA 10.2. CUDA 11 is the first CUDA version to support C++17. Hence decommissioning legacy CUDA 10.2 was a major step in adding support for C++17 in PyTorch. It also helps to improve PyTorch code by eliminating legacy CUDA 10.2 specific instructions.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Decommissioning of CUDA 11.3 and introduction of CUDA 11.7 brings compatibility support for the new NVIDIA Open GPU Kernel Modules and another significant highlight is the lazy loading support. CUDA 11.7 is shipped with cuDNN 8.5.0 which contains a number of optimizations accelerating transformer-based models, 30% reduction in library size , and various improvements in the runtime fusion engine. Learn more on CUDA 11.7 with our [release", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "notes](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Beta Features", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Beta) functorch\n\nInspired by [Google\u00ae JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples include:", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re excited to announce that, as a first step towards closer integration with PyTorch, functorch has moved to inside the PyTorch library and no longer requires the installation of a separate functorch package. After installing PyTorch via conda or pip, you\u2019ll be able to `import functorch\u2019 in your program. Learn more with our [detailed instructions](https://pytorch.org/functorch/1.13/install.html), [nightly](https://pytorch.org/functorch/nightly/) and [release", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "notes](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Intel\u00ae VTune\u2122 Profiler's Instrumentation and Tracing Technology APIs (ITT) integration\n\nPyTorch users are able to visualize op-level timeline of PyTorch scripts execution in Intel\u00ae VTune\u2122 Profiler when they need to analyze per-op performance with low-level performance metrics on Intel platforms.\n\n```python\nwith torch.autograd.profiler.emit_itt():\n for i in range(10):\n torch.itt.range_push('step_{}'.format(i))\n model(input)\n torch.itt.range_pop()", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": " \nLearn more with our [tutorial](https://pytorch.org/tutorials/recipes/profile_with_itt.html).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Beta) NNC: Add BF16 and Channels last support\n\nTorchScript graph-mode inference performance on x86 CPU is boosted by adding channels last and BF16 support to NNC. PyTorch users may benefit from channels last optimization on most popular x86 CPUs and benefit from BF16 optimization on Intel Cooper Lake Processor and Sapphire Rapids Processor. >2X geomean performance boost is observed on broad vision models with these two optimizations on Intel Cooper Lake Processor.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "The performance benefit can be obtained with existing TorchScript, channels last and BF16 Autocast APIs. See code snippet below. We will migrate the optimizations in NNC to the new PyTorch DL Compiler TorchInductor.\n\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nimport torchvision.models as models\nmodel = models.resnet50(pretrained=True)\n# Convert the model to channels-last\nmodel = model.to(memory_format=torch.channels_last)\nmodel.eval()\ndata = torch.rand(1, 3, 224, 224)\n# Convert the data to channels-lastdata = data.to(memory_format=torch.channels_last)\n# Enable autocast to run with BF16\nwith torch.cpu.amp.autocast(), torch.no_grad():\n# Trace the model\nmodel = torch.jit.trace(model, torch.rand(1, 3, 224, 224))", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "model = torch.jit.freeze(model)\n\t# Run the traced model\n\tmodel(data)\n```", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Support for M1 Devices\n\nSince v1.12, PyTorch has been offering native builds for Apple\u00ae silicon machines that use Apple's new M1 chip as a prototype feature. In this release, we bring this feature to beta, providing improved support across PyTorch's APIs.\n\nWe now run tests for all submodules except `torch.distributed` on M1 macOS 12.6 instances. With this improved testing, we were able to fix features such as cpp extension and convolution correctness for certain inputs.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "To get started, just install PyTorch v1.13 on your Apple silicon Mac running macOS 12 or later with a native version (arm64) of Python. Learn more with our [release notes](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "Prototype Features\n\n", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Arm\u00ae Compute Library (ACL) backend support for AWS Graviton\n\nWe achieved substantial improvements for CV and NLP inference on aarch64 cpu with Arm Compute Library (acl) to enable acl backend for pytorch and torch-xla modules. Highlights include:\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "- Enabled mkldnn + acl as the default backend for aarch64 torch wheel.\n- Enabled mkldnn matmul operator for aarch64 bf16 device.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "- Brought TensorFlow xla+acl feature into torch-xla. We enhanced the TensorFlow xla with Arm Compute Library runtime for aarch64 cpu. These changes are included in TensorFlow master and then the upcoming TF 2.10. Once the torch-xla repo is updated for the tensorflow commit, it will have compiling support for torch-xla. We observed ~2.5-3x improvement for MLPerf Bert inference compared to the torch 1.12 wheel on Graviton3.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) CUDA Sanitizer", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "When enabled, the sanitizer begins to analyze low-level CUDA operations invoked as a result of the user\u2019s PyTorch code to detect data race errors caused by unsynchronized data access from different CUDA streams. The errors found are then printed along with stack traces of faulty accesses, much like [Thread Sanitizer](https://clang.llvm.org/docs/ThreadSanitizer.html) does. An example of a simple error and the output produced by the sanitizer can be viewed", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "[here](https://gist.github.com/sypneiwski/5989d634f7090913b80012be835e811d). It will be especially useful for machine learning applications, where corrupted data can be easy to miss for a human and the errors may not always manifest themselves; the sanitizer will always be able to detect them.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) Limited Python 3.11 support\n\nBinaries for Linux with Python 3.11 support are available to download via pip. Please follow the instructions on the [get started page](https://pytorch.org/get-started/locally/). Please note that Python 3.11 support is only a preview. In particular, features including Distributed, Profiler, FX and JIT might not be fully functional yet.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Celebrate PyTorch 2.0 with New Performance Features for AI Developers\"\nauthor: Intel\n---\n\nCongratulations to the PyTorch Foundation for its release of **PyTorch 2.0**! In this blog, I discuss the four features for which Intel made significant contributions to PyTorch 2.0:\n\n1. TorchInductor\n2. GNN\n3. INT8 Inference Optimization\n4. oneDNN Graph API", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "We at Intel are delighted to be part of the PyTorch community and appreciate the collaboration with and feedback from our colleagues at [Meta](http://www.meta.com/) as we co-developed these features.\n\n\nLet\u2019s get started.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "1. TorchInductor CPU FP32 Inference Optimized\n\n\nAs part of the PyTorch 2.0 compilation stack, TorchInductor CPU backend optimization brings notable performance improvements via graph compilation over the PyTorch eager mode.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The TorchInductor CPU backend is sped up by leveraging the technologies from the [Intel\u00ae Extension for PyTorch](http://github.com/intel/intel-extension-for-pytorch) for Conv/GEMM ops with post-op fusion and weight prepacking, and PyTorch ATen CPU kernels for memory-bound ops with explicit vectorization on top of OpenMP*-based thread parallelization.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "With these optimizations on top of the powerful loop fusions in TorchInductor codegen, we achieved up to a **1.7x** FP32 inference performance boost over three representative deep learning benchmarks: TorchBench, HuggingFace, and timm1. Training and low-precision support are under development.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "See the Improvements\n\n\nThe performance improvements on various backends are tracked on this [TouchInductor CPU Performance Dashboard](http://github.com/pytorch/pytorch/issues/93531#issuecomment-1457373890).", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Improve Graph Neural Network (GNN) in PyG for Inference and Training Performance on CPU\n\n\nGNN is a powerful tool to analyze graph structure data. This feature is designed to improve GNN inference and training performance on Intel\u00ae CPUs, including the new 4th Gen Intel\u00ae Xeon\u00ae Scalable processors.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Geometric (PyG) is a very popular library built upon PyTorch to perform GNN workflows. Currently on CPU, GNN models of PyG run slowly due to the lack of GNN-related sparse matrix multiplication operations (i.e., SpMM_reduce) and the lack of several critical kernel-level optimizations (scatter/gather, etc.) tuned for GNN compute.\n\n\nTo address this, optimizations are provided for message passing between adjacent neural network nodes:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "* **scatter_reduce:** performance hotspot in message-passing when the edge index is stored in coordinate format (COO).\n* **gather:** backward computation of scatter_reduce, specially tuned for the GNN compute when the index is an expanded tensor.\n* **torch.sparse.mm with reduce flag:** performance hotspot in message-passing when the edge index is stored in compressed sparse row (CSR). Supported reduce flag for: sum, mean, amax, amin.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "End-to-end performance benchmark results for both inference and training on 3rd Gen Intel\u00ae Xeon\u00ae Scalable processors 8380 platform and on 4th Gen 8480+ platform are discussed in [Accelerating PyG on Intel CPUs](http://www.pyg.org/ns-newsarticle-accelerating-pyg-on-intel-cpus).", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Optimize int8 Inference with Unified Quantization Backend for x86 CPU Platforms \n\n\nThe new X86 quantization backend is a combination of [FBGEMM](http://github.com/pytorch/FBGEMM) (Facebook General Matrix-Matrix Multiplication) and [oneAPI Deep Neural Network Library (oneDNN](http://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html)) backends and replaces FBGEMM as the default quantization backend for x86 platforms. The result: better end-to-end int8 inference performance than FBGEMM.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Users access the x86 quantization backend by default for x86 platforms, and the selection between different kernels is automatically done behind the scenes. The rules of selection are based on prior performance testing data done by Intel during feature development. Thus, the x86 backend replaces FBGEMM and may offer better performance, depending on the use case.\n\n\nThe selection rules are:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "* On platforms without VNNI (e.g., Intel\u00ae Core\u2122 i7 processors), FBGEMM is always used.\n* On platforms with VNNI (e.g., 2nd-4th Gen Intel\u00ae Xeon\u00ae Scalable processors and future platforms):\n * For linear, FBGEMM is always used.\n * For convolution layers, FBGEMM is used for depth-wise convolution whose layers > 100; otherwise, oneDNN is used.\n\nNote that as the kernels continue to evolve.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The selection rules above are subject to change to achieve better performance. Performance metrics for through-put speed-up ratios of unified x86 backend vs. pure FBGEMM are discussed in [[RFC] Unified quantization backend for x86 CPU platforms #83888](http://github.com/pytorch/pytorch/issues/83888).", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Leverage oneDNN Graph API to Accelerate Inference on CPU", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[oneDNN Graph API](http://spec.oneapi.io/onednn-graph/latest/introduction.html) extends [oneDNN](http://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html) with a flexible graph API to maximize the optimization opportunity for generating efficient code on Intel\u00ae AI hardware. It automatically identifies the graph partitions to be accelerated via fusion. The [fusion patterns](http://github.com/oneapi-src/oneDNN/blob/dev-graph/doc/programming_model/ops_and_patterns.md#fusion-patterns) focus on", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "fusing compute-intensive operations such as convolution, matmul, and their neighbor operations for both inference and training use cases.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Currently, BFloat16 and Float32 datatypes are supported and only inference workloads can be optimized. BF16 is only optimized on machines with Intel\u00ae Advanced Vector Extensions 512 (Intel\u00ae AVX-512) BF16 support.\n\n\nFew or no modifications are needed in PyTorch to support newer oneDNN Graph fusions/optimized kernels. To use oneDNN Graph, users can:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "* Either use the API _torch.jit.enable_onednn_fusion(True)_ before JIT tracing a model, OR \u2026\n* Use its context manager, viz. _with torch.jit.fuser(\u201cfuser3\u201d)._\n* For accelerating [BFloat16 inference](http://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16), we rely on eager-mode AMP (Automatic Mixed Precision) support in PyTorch and disable JIT mode\u2019s AMP.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "See the [PyTorch performance tuning guide](http://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#use-onednn-graph-with-torchscript-for-inference).", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Next Steps", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Get the Software\n\n\n[Try out PyTorch 2.0](http://pytorch.org/get-started/locally/) and realize the performance benefits for yourself from these Intel-contributed features.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "We encourage you to check out Intel\u2019s other [AI Tools](https://www.intel.com/content/www/us/en/developer/topic-technology/artificial-intelligence/tools.html) and [Framework](https://www.intel.com/content/www/us/en/developer/tools/frameworks/overview.html) optimizations and learn about the open, standards-based [oneAPI](https://www.intel.com/content/www/us/en/developer/tools/oneapi/overview.html) multiarchitecture, multivendor programming model that forms the foundation of Intel\u2019s AI software portfolio.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "For more details about 4th Gen Intel Xeon Scalable processor, visit [AI Platform](https://www.intel.com/content/www/us/en/developer/topic-technology/artificial-intelligence/platform.html) where you can learn about how Intel is empowering developers to run high-performance, efficient end-to-end AI pipelines.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Resources\n\n* [PyTorch Get Started](http://pytorch.org/get-started/pytorch-2.0/)\n* [Dev Discussions](http://dev-discuss.pytorch.org/t/pytorch-release-2-0-execution-update/1077)\n* [Documentation](http://pytorch.org/docs/2.0/)", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "* The ```compute_loss``` method estimates the standard Multi-box loss as described on page 5 of the SSD paper. It uses the [smooth L1 loss](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L244) for regression and the standard [cross-entropy loss](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L262-L266) with [hard-negative sampling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L268-L276) for classification. \n* As in all detection models, the forward method currently has different behaviour depending on whether the model is on training or eval mode. It starts by [resizing & normalizing the input images](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L309-L310) and then [passes them through the backbone](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L324-L325) to get the feature maps. The feature maps are then [passed through the head](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L331-L332) to get the predictions and then the method [generates the default boxes](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L334-L335). \n * If the model is on [training mode](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L339-L352), the forward will estimate the [IoUs of the default boxes with the ground truth](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L349), use the ```SSDmatcher``` to [produce matches](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L350) and finally [estimate the losses](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L352) by calling the ```compute_loss method```.\n * If the model is on [eval mode](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L353-L355), we first select the best detections by keeping only the ones that [pass the score threshold](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L384), select the [most promising boxes](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L388-L391) and run NMS to [clean up and select](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L401-L403) the best predictions. Finally we [postprocess the predictions](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L355) to resize them to the original image size.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "# The SSD300 VGG16 Model\n\nThe SSD is a family of models because it can be configured with different backbones and different Head configurations. In this section, we will focus on the provided [SSD pre-trained model](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L522-L523). We will discuss the details of its configuration and the training process used to reproduce the reported results.\n\n### Training process\n\nThe model was trained using the COCO dataset and all of its hyper-parameters and scripts can be found in our [references](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssd300-vgg16) folder. Below we provide details on the most notable aspects of the training process.\n\n### Paper Hyperparameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "In order to achieve the best possible results on COCO, we adopted the hyperparameters described on the section 3 of the paper concerning the optimizer configuration, the weight regularization etc. Moreover we found it useful to adopt the optimizations that appear in the [official implementation](https://github.com/weiliu89/caffe/blob/ssd/examples/ssd/ssd_coco.py#L310-L321) concerning the [tiling configuration](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L579-L581) of the DefaultBox generator. This optimization was not described in the paper but it was crucial for improving the detection precision of smaller objects. \n\n### Data Augmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "Implementing the [SSD Data Augmentation strategy](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/references/detection/transforms.py#L20-L239) as described on page 6 and page 12 of the paper was critical to reproducing the results. More specifically the use of random \u201cZoom In\u201d and \u201cZoom Out\u201d transformations make the model robust to various input sizes and improve its precision on the small and medium objects. Finally since the VGG16 has quite a few parameters, the photometric distortions [included in the augmentations](https://github.com/pytorch/vision/blob/43d772067fe77965ec8fc49c799de5cea44b8aa2/references/detection/presets.py#L11-L18) have a regularization effect and help avoid the overfitting. \n\n### Weight Initialization & Input Scaling", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "Another aspect that we found beneficial was to follow the [weight initialization scheme](https://github.com/intel/caffe/blob/master/models/intel_optimized_models/ssd/VGGNet/coco/SSD_300x300/train.prototxt) proposed by the paper. To do that, we had to adapt our input scaling method by [undoing the 0-1 scaling](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L583-L587) performed by ```ToTensor()``` and use [pre-trained ImageNet weights](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L24-L26) fitted with this scaling (shoutout to [Max deGroot](https://github.com/amdegroot) for providing them in his repo). All the weights of new convolutions were [initialized using Xavier](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L30-L35) and their biases were set to zero. After initialization, the network was [trained end-to-end](https://github.com/pytorch/vision/blob/33db2b3ebfdd2f73a9228f430fa7dd91c3b18078/torchvision/models/detection/ssd.py#L571-L572).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "### LR Scheme\n\nAs reported on the paper, after applying aggressive data augmentations it\u2019s necessary to train the models for longer. Our experiments confirm this and we had to tweak the Learning rate, batch sizes and overall steps to achieve the best results. Our [proposed learning scheme](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssd300-vgg16) is configured to be rather on the safe side, showed signs of plateauing between the steps and thus one is likely to be able to train a similar model by doing only 66% of our epochs.\n\n# Breakdown of Key Accuracy Improvements", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "It is important to note that implementing a model directly from a paper is an iterative process that circles between coding, training, bug fixing and adapting the configuration until we match the accuracies reported on the paper. Quite often it also involves simplifying the training recipe or enhancing it with more recent methodologies. It is definitely not a linear process where incremental accuracy improvements are achieved by improving a single direction at a time but instead involves exploring different hypothesis, making incremental improvements in different aspects and doing a lot of backtracking. \n\nWith that in mind, below we try to summarize the optimizations that affected our accuracy the most. We did this by grouping together the various experiments in 4 main groups and attributing the experiment improvements to the closest match. Note that the Y-axis of the graph starts from 18 instead from 0 to make the difference between optimizations more visible:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\n| Model Configuration | mAP delta | mAP | \n| ------------- | ------------- | ------------- |\n| Baseline with \"FasterRCNN-style\" Hyperparams | - | 19.5 | \n| + Paper Hyperparams | 1.6 | 21.1 | \n| + Data Augmentation | 1.8 | 22.9 | \n| + Weight Initialization & Input Scaling | 1 | 23.9 | \n| + LR scheme | 1.2 | 25.1 | \n\nOur final model achieves an mAP of 25.1 and reproduces exactly the COCO results reported on the paper. Here is a [detailed breakdown](https://github.com/pytorch/vision/pull/3403) of the accuracy metrics.\n\n\nWe hope you found the part 1 of the series interesting. On the part 2, we will focus on the implementation of SSDlite and discuss its differences from SSD. Until then, we are looking forward to your feedback.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssd-implementation/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 1.13 release, including beta versions of functorch and improved support for Apple\u2019s new M1 chips.\"\nauthor: Team PyTorch\nfeatured-img: \"/assets/images/blog-2022-10-25-Pytorch-1.13-Release.png\"\n---\n\nWe are excited to announce the release of PyTorch\u00ae 1.13 ([release note](https://github.com/pytorch/pytorch/releases/tag/v1.13.0))! This includes Stable versions of BetterTransformer. We deprecated CUDA 10.2 and 11.3 and completed migration of CUDA 11.6 and 11.7. Beta includes improved support for Apple M1 chips and functorch, a library that offers composable vmap (vectorization) and autodiff transforms, being included in-tree with the PyTorch release. This release is composed of over 3,749 commits and 467 contributors since 1.12.1. We want to sincerely thank our dedicated community for your contributions.\n\nSummary:", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "Summary:\n\n- The [BetterTransformer](#stable-features) feature set supports fastpath execution for common Transformer models during Inference out-of-the-box, without the need to modify the model. Additional improvements include accelerated add+matmul linear algebra kernels for sizes commonly used in Transformer models and Nested Tensors is now enabled by default.\n\n- Timely [deprecating older CUDA versions](#introduction-of-cuda-116-and-117-and-deprecation-of-cuda-102-and-113) allows us to proceed with introducing the latest CUDA version as they are introduced by Nvidia\u00ae, and hence allows support for C++17 in PyTorch and new NVIDIA Open GPU Kernel Modules.\n\n- Previously, [functorch](#beta-features) was released out-of-tree in a separate package. After installing PyTorch, a user will be able to `import functorch` and use functorch without needing to install another package.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "- PyTorch is offering native builds for Apple\u00ae silicon machines that use Apple's new [M1 chip](#beta-support-for-m1-devices) as a beta feature, providing improved support across PyTorch's APIs.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "Along with 1.13, we are also releasing major updates to the PyTorch libraries, more details can be found in this [blog](https://pytorch.org/blog/new-library-updates-in-pytorch-1.13/).\n\n## Stable Features\n\n### (Stable) BetterTransformer API\n\nThe [BetterTransformer](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/) feature set, first released in PyTorch 1.12, is stable. PyTorch BetterTransformer supports fastpath execution for common Transformer models during Inference out-of-the-box, without the need to modify the model. To complement the improvements in Better Transformer, we have also accelerated add+matmul linear algebra kernels for sizes commonly used in Transformer models.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "Reflecting the performance benefits for many NLP users, Nested Tensors use for Better Transformer is now enabled by default. To ensure compatibility, a mask check is performed to ensure a contiguous mask is supplied. In Transformer Encoder, the mask check for src_key_padding_mask may be suppressed by setting mask_check=False. This accelerates processing for users than can guarantee that only aligned masks are provided. Finally, better error messages are provided to diagnose incorrect inputs, together with improved diagnostics why fastpath execution cannot be used.\n\nBetter Transformer is directly integrated into the PyTorch TorchText library, enabling TorchText users to transparently and automatically take advantage of BetterTransformer speed and efficiency performance. ([Tutorial](https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html))\n\n

\n\n

\n\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure: BetterTransformer fastpath execution is now stable and enables sparsity optimization using Nested Tensor representation as default\n

\n\n### Introduction of CUDA 11.6 and 11.7 and deprecation of CUDA 10.2 and 11.3\n\nTimely deprecating older CUDA versions allows us to proceed with introducing the latest CUDA version as they are introduced by Nvidia\u00ae, and hence allows developers to use the latest features of CUDA and benefit from correctness fixes provided by the latest version.\n\nDecommissioning of CUDA 10.2. CUDA 11 is the first CUDA version to support C++17. Hence decommissioning legacy CUDA 10.2 was a major step in adding support for C++17 in PyTorch. It also helps to improve PyTorch code by eliminating legacy CUDA 10.2 specific instructions.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "Decommissioning of CUDA 11.3 and introduction of CUDA 11.7 brings compatibility support for the new NVIDIA Open GPU Kernel Modules and another significant highlight is the lazy loading support. CUDA 11.7 is shipped with cuDNN 8.5.0 which contains a number of optimizations accelerating transformer-based models, 30% reduction in library size , and various improvements in the runtime fusion engine. Learn more on CUDA 11.7 with our [release notes](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html).\n\n## Beta Features\n\n### (Beta) functorch\n\nInspired by [Google\u00ae JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples include:", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "\n\n\nWe\u2019re excited to announce that, as a first step towards closer integration with PyTorch, functorch has moved to inside the PyTorch library and no longer requires the installation of a separate functorch package. After installing PyTorch via conda or pip, you\u2019ll be able to `import functorch\u2019 in your program. Learn more with our [detailed instructions](https://pytorch.org/functorch/1.13/install.html), [nightly](https://pytorch.org/functorch/nightly/) and [release notes](https://github.com/pytorch/pytorch/releases).", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Intel\u00ae VTune\u2122 Profiler's Instrumentation and Tracing Technology APIs (ITT) integration\n\nPyTorch users are able to visualize op-level timeline of PyTorch scripts execution in Intel\u00ae VTune\u2122 Profiler when they need to analyze per-op performance with low-level performance metrics on Intel platforms.\n\n```python\nwith torch.autograd.profiler.emit_itt():\n for i in range(10):\n torch.itt.range_push('step_{}'.format(i))\n model(input)\n torch.itt.range_pop()\n```\n\n \nLearn more with our [tutorial](https://pytorch.org/tutorials/recipes/profile_with_itt.html).\n\n### (Beta) NNC: Add BF16 and Channels last support", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "TorchScript graph-mode inference performance on x86 CPU is boosted by adding channels last and BF16 support to NNC. PyTorch users may benefit from channels last optimization on most popular x86 CPUs and benefit from BF16 optimization on Intel Cooper Lake Processor and Sapphire Rapids Processor. >2X geomean performance boost is observed on broad vision models with these two optimizations on Intel Cooper Lake Processor.\n\nThe performance benefit can be obtained with existing TorchScript, channels last and BF16 Autocast APIs. See code snippet below. We will migrate the optimizations in NNC to the new PyTorch DL Compiler TorchInductor.\n\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "```python\nimport torch\nimport torchvision.models as models\nmodel = models.resnet50(pretrained=True)\n# Convert the model to channels-last\nmodel = model.to(memory_format=torch.channels_last)\nmodel.eval()\ndata = torch.rand(1, 3, 224, 224)\n# Convert the data to channels-lastdata = data.to(memory_format=torch.channels_last)\n# Enable autocast to run with BF16\nwith torch.cpu.amp.autocast(), torch.no_grad():\n# Trace the model\nmodel = torch.jit.trace(model, torch.rand(1, 3, 224, 224))\n\tmodel = torch.jit.freeze(model)\n\t# Run the traced model\n\tmodel(data)\n```\n\n### (Beta) Support for M1 Devices\n\nSince v1.12, PyTorch has been offering native builds for Apple\u00ae silicon machines that use Apple's new M1 chip as a prototype feature. In this release, we bring this feature to beta, providing improved support across PyTorch's APIs.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "We now run tests for all submodules except `torch.distributed` on M1 macOS 12.6 instances. With this improved testing, we were able to fix features such as cpp extension and convolution correctness for certain inputs.\n\nTo get started, just install PyTorch v1.13 on your Apple silicon Mac running macOS 12 or later with a native version (arm64) of Python. Learn more with our [release notes](https://github.com/pytorch/pytorch/releases).\n\n## Prototype Features\n\n\n\n### (Prototype) Arm\u00ae Compute Library (ACL) backend support for AWS Graviton\n\nWe achieved substantial improvements for CV and NLP inference on aarch64 cpu with Arm Compute Library (acl) to enable acl backend for pytorch and torch-xla modules. Highlights include:\n ", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "- Enabled mkldnn + acl as the default backend for aarch64 torch wheel.\n- Enabled mkldnn matmul operator for aarch64 bf16 device.\n- Brought TensorFlow xla+acl feature into torch-xla. We enhanced the TensorFlow xla with Arm Compute Library runtime for aarch64 cpu. These changes are included in TensorFlow master and then the upcoming TF 2.10. Once the torch-xla repo is updated for the tensorflow commit, it will have compiling support for torch-xla. We observed ~2.5-3x improvement for MLPerf Bert inference compared to the torch 1.12 wheel on Graviton3.\n\n### (Prototype) CUDA Sanitizer", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "When enabled, the sanitizer begins to analyze low-level CUDA operations invoked as a result of the user\u2019s PyTorch code to detect data race errors caused by unsynchronized data access from different CUDA streams. The errors found are then printed along with stack traces of faulty accesses, much like [Thread Sanitizer](https://clang.llvm.org/docs/ThreadSanitizer.html) does. An example of a simple error and the output produced by the sanitizer can be viewed [here](https://gist.github.com/sypneiwski/5989d634f7090913b80012be835e811d). It will be especially useful for machine learning applications, where corrupted data can be easy to miss for a human and the errors may not always manifest themselves; the sanitizer will always be able to detect them.\n\n### (Prototype) Limited Python 3.11 support", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "Binaries for Linux with Python 3.11 support are available to download via pip. Please follow the instructions on the [get started page](https://pytorch.org/get-started/locally/). Please note that Python 3.11 support is only a preview. In particular, features including Distributed, Profiler, FX and JIT might not be fully functional yet.", "metadata": {"source": "https://pytorch.org/blog/PyTorch-1.13-release/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Celebrate PyTorch 2.0 with New Performance Features for AI Developers\"\nauthor: Intel\n---\n\nCongratulations to the PyTorch Foundation for its release of **PyTorch 2.0**! In this blog, I discuss the four features for which Intel made significant contributions to PyTorch 2.0:\n\n1. TorchInductor\n2. GNN\n3. INT8 Inference Optimization\n4. oneDNN Graph API\n\nWe at Intel are delighted to be part of the PyTorch community and appreciate the collaboration with and feedback from our colleagues at [Meta](http://www.meta.com/) as we co-developed these features.\n\n\nLet\u2019s get started.\n\n\n## 1. TorchInductor CPU FP32 Inference Optimized\n\n\nAs part of the PyTorch 2.0 compilation stack, TorchInductor CPU backend optimization brings notable performance improvements via graph compilation over the PyTorch eager mode.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "The TorchInductor CPU backend is sped up by leveraging the technologies from the [Intel\u00ae Extension for PyTorch](http://github.com/intel/intel-extension-for-pytorch) for Conv/GEMM ops with post-op fusion and weight prepacking, and PyTorch ATen CPU kernels for memory-bound ops with explicit vectorization on top of OpenMP*-based thread parallelization.\n\n\nWith these optimizations on top of the powerful loop fusions in TorchInductor codegen, we achieved up to a **1.7x** FP32 inference performance boost over three representative deep learning benchmarks: TorchBench, HuggingFace, and timm1. Training and low-precision support are under development.\n\n\n### See the Improvements\n\n\nThe performance improvements on various backends are tracked on this [TouchInductor CPU Performance Dashboard](http://github.com/pytorch/pytorch/issues/93531#issuecomment-1457373890).\n\n\n## Improve Graph Neural Network (GNN) in PyG for Inference and Training Performance on CPU", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "GNN is a powerful tool to analyze graph structure data. This feature is designed to improve GNN inference and training performance on Intel\u00ae CPUs, including the new 4th Gen Intel\u00ae Xeon\u00ae Scalable processors.\n\n\nPyTorch Geometric (PyG) is a very popular library built upon PyTorch to perform GNN workflows. Currently on CPU, GNN models of PyG run slowly due to the lack of GNN-related sparse matrix multiplication operations (i.e., SpMM_reduce) and the lack of several critical kernel-level optimizations (scatter/gather, etc.) tuned for GNN compute.\n\n\nTo address this, optimizations are provided for message passing between adjacent neural network nodes:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "* **scatter_reduce:** performance hotspot in message-passing when the edge index is stored in coordinate format (COO).\n* **gather:** backward computation of scatter_reduce, specially tuned for the GNN compute when the index is an expanded tensor.\n* **torch.sparse.mm with reduce flag:** performance hotspot in message-passing when the edge index is stored in compressed sparse row (CSR). Supported reduce flag for: sum, mean, amax, amin.\n\nEnd-to-end performance benchmark results for both inference and training on 3rd Gen Intel\u00ae Xeon\u00ae Scalable processors 8380 platform and on 4th Gen 8480+ platform are discussed in [Accelerating PyG on Intel CPUs](http://www.pyg.org/ns-newsarticle-accelerating-pyg-on-intel-cpus).\n\n\n## Optimize int8 Inference with Unified Quantization Backend for x86 CPU Platforms", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "The new X86 quantization backend is a combination of [FBGEMM](http://github.com/pytorch/FBGEMM) (Facebook General Matrix-Matrix Multiplication) and [oneAPI Deep Neural Network Library (oneDNN](http://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html)) backends and replaces FBGEMM as the default quantization backend for x86 platforms. The result: better end-to-end int8 inference performance than FBGEMM.\n\n\nUsers access the x86 quantization backend by default for x86 platforms, and the selection between different kernels is automatically done behind the scenes. The rules of selection are based on prior performance testing data done by Intel during feature development. Thus, the x86 backend replaces FBGEMM and may offer better performance, depending on the use case.\n\n\nThe selection rules are:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "* On platforms without VNNI (e.g., Intel\u00ae Core\u2122 i7 processors), FBGEMM is always used.\n* On platforms with VNNI (e.g., 2nd-4th Gen Intel\u00ae Xeon\u00ae Scalable processors and future platforms):\n * For linear, FBGEMM is always used.\n * For convolution layers, FBGEMM is used for depth-wise convolution whose layers > 100; otherwise, oneDNN is used.\n\nNote that as the kernels continue to evolve.\n\n\nThe selection rules above are subject to change to achieve better performance. Performance metrics for through-put speed-up ratios of unified x86 backend vs. pure FBGEMM are discussed in [[RFC] Unified quantization backend for x86 CPU platforms #83888](http://github.com/pytorch/pytorch/issues/83888).\n\n\n## Leverage oneDNN Graph API to Accelerate Inference on CPU", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "[oneDNN Graph API](http://spec.oneapi.io/onednn-graph/latest/introduction.html) extends [oneDNN](http://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html) with a flexible graph API to maximize the optimization opportunity for generating efficient code on Intel\u00ae AI hardware. It automatically identifies the graph partitions to be accelerated via fusion. The [fusion patterns](http://github.com/oneapi-src/oneDNN/blob/dev-graph/doc/programming_model/ops_and_patterns.md#fusion-patterns) focus on fusing compute-intensive operations such as convolution, matmul, and their neighbor operations for both inference and training use cases.\n\n\nCurrently, BFloat16 and Float32 datatypes are supported and only inference workloads can be optimized. BF16 is only optimized on machines with Intel\u00ae Advanced Vector Extensions 512 (Intel\u00ae AVX-512) BF16 support.\n\n\nFew or no modifications are needed in PyTorch to support newer oneDNN Graph fusions/optimized kernels. To use oneDNN Graph, users can:", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "* Either use the API _torch.jit.enable_onednn_fusion(True)_ before JIT tracing a model, OR \u2026\n* Use its context manager, viz. _with torch.jit.fuser(\u201cfuser3\u201d)._\n* For accelerating [BFloat16 inference](http://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16), we rely on eager-mode AMP (Automatic Mixed Precision) support in PyTorch and disable JIT mode\u2019s AMP.\n\nSee the [PyTorch performance tuning guide](http://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#use-onednn-graph-with-torchscript-for-inference).\n\n\n## Next Steps\n\n\n### Get the Software\n\n\n[Try out PyTorch 2.0](http://pytorch.org/get-started/locally/) and realize the performance benefits for yourself from these Intel-contributed features.", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "We encourage you to check out Intel\u2019s other [AI Tools](https://www.intel.com/content/www/us/en/developer/topic-technology/artificial-intelligence/tools.html) and [Framework](https://www.intel.com/content/www/us/en/developer/tools/frameworks/overview.html) optimizations and learn about the open, standards-based [oneAPI](https://www.intel.com/content/www/us/en/developer/tools/oneapi/overview.html) multiarchitecture, multivendor programming model that forms the foundation of Intel\u2019s AI software portfolio.\n\n\nFor more details about 4th Gen Intel Xeon Scalable processor, visit [AI Platform](https://www.intel.com/content/www/us/en/developer/topic-technology/artificial-intelligence/platform.html) where you can learn about how Intel is empowering developers to run high-performance, efficient end-to-end AI pipelines.\n\n\n### PyTorch Resources", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "* [PyTorch Get Started](http://pytorch.org/get-started/pytorch-2.0/)\n* [Dev Discussions](http://dev-discuss.pytorch.org/t/pytorch-release-2-0-execution-update/1077)\n* [Documentation](http://pytorch.org/docs/2.0/)", "metadata": {"source": "https://pytorch.org/blog/celebrate-pytorch-2.0/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"What Every User Should Know About Mixed Precision Training in PyTorch\"\nauthor: Syed Ahmed, Christian Sarofeen, Mike Ruberry, Eddie Yan, Natalia Gimelshein, Michael Carilli, Szymon Migacz, Piotr Bialecki, Paulius Micikevicius, Dusan Stosic, Dong Yang, and Naoya Maruyama\nfeatured-img: ''\n---", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Efficient training of modern neural networks often relies on using lower precision data types. Peak float16 matrix multiplication and convolution performance is 16x faster than peak float32 performance on A100 GPUs. And since the float16 and bfloat16 data types are only half the size of float32 they can double the performance of bandwidth-bound kernels and reduce the memory required to train a network, allowing for larger models, larger batches, or larger inputs. Using a module like", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "[torch.amp](https://pytorch.org/docs/master/amp.html) (short for \u201cAutomated Mixed Precision\u201d) makes it easy to get the speed and memory usage benefits of lower precision data types while preserving convergence behavior.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Going faster and using less memory is always advantageous \u2013 deep learning practitioners can test more model architectures and hyperparameters, and larger, more powerful models can be trained. Training very large models like those described in [Narayanan et al.](https://arxiv.org/pdf/2104.04473.pdf) and [Brown et al.](https://arxiv.org/pdf/2005.14165.pdf) (which take thousands of GPUs months to train even with expert handwritten optimizations) is infeasible without using mixed precision.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "We\u2019ve talked about mixed precision techniques before ([here](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/), [here](https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html), and [here](https://developer.nvidia.com/automatic-mixed-precision)), and this blog post is a summary of those techniques and an introduction if you\u2019re new to mixed precision.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Mixed Precision Training in Practice\n\nMixed precision training techniques \u2013 the use of the lower precision float16 or bfloat16 data types alongside the float32 data type \u2013 are broadly applicable and effective. See Figure 1 for a sampling of models successfully trained with mixed precision, and Figures 2 and 3 for example speedups using torch.amp.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n Figure 1: Sampling of DL Workloads Successfully Trained with float16 (Source).\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n Figure 2: Performance of mixed precision training using torch.amp on NVIDIA 8xV100 vs. float32 training on 8xV100 GPU. Bars represent the speedup factor of torch.amp over float32. \n(Higher is better.) (Source).\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n Figure 3. Performance of mixed precision training using torch.amp on NVIDIA 8xA100 vs. 8xV100 GPU. Bars represent the speedup factor of A100 over V100.\n(Higher is Better.) (Source).\n

\n\nSee the [NVIDIA Deep Learning Examples repository](https://github.com/NVIDIA/DeepLearningExamples) for more sample mixed precision workloads.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Similar performance charts can be seen in [3D medical image analysis](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/dong_yang-mixed-precision-training-for-3d-medical-image-analysis.pdf), [gaze estimation](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/shalini_de_mello-mixed-precision-training-for-faze.pdf), [video synthesis](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/tingchun_wang-mixed-precision-vid2vid.pdf), [conditional", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "GANs](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/mingyu_liu-amp-imaginaire.pdf), and [convolutional LSTMs](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/wonmin_byeon-mixed-precision-training-for-convolutional-tensor-train-lstm.pdf). [Huang et al](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/). showed that mixed precision training is 1.5x to 5.5x faster over float32 on V100 GPUs, and an additional 1.3x to 2.5x", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "faster on A100 GPUs on a variety of networks. On very large networks the need for mixed precision is even more evident. [Narayanan et al](https://arxiv.org/pdf/2104.04473.pdf). reports that it would take 34 days to train GPT-3 175B on 1024 A100 GPUs (with a batch size of 1536), but it\u2019s estimated it would take over a year using float32!", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Getting Started With Mixed Precision Using torch.amp", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "torch.amp, introduced in PyTorch 1.6, makes it easy to leverage mixed precision training using the float16 or bfloat16 dtypes. See this [blog post](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/), [tutorial](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html), and [documentation](https://pytorch.org/docs/master/amp.html) for more details. Figure 4 shows an example of applying AMP with grad scaling to a network.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "```console\nimport torch\n# Creates once at the beginning of training\nscaler = torch.cuda.amp.GradScaler()\n\nfor data, label in data_iter:\n optimizer.zero_grad()\n # Casts operations to mixed precision\n with torch.amp.autocast(device_type=\u201ccuda\u201d, dtype=torch.float16):\n loss = model(data)\n\n # Scales the loss, and calls backward()\n # to create scaled gradients\n scaler.scale(loss).backward()\n\n # Unscales gradients and calls\n # or skips optimizer.step()\n scaler.step(optimizer)", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "# Updates the scale for next iteration\n scaler.update()", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\n Figure 4: AMP recipe\n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Picking The Right Approach\n\nOut-of-the-box mixed precision training with either float16 or bfloat16 is effective at speeding up the convergence of many deep learning models, but some models may require more careful numerical accuracy management. Here are some options:", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Full float32 precision. Floating point tensors and modules are created in float32 precision by default in PyTorch, but this is a historic artifact not representative of training most modern deep learning networks. It\u2019s rare that networks need this much numerical accuracy.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Enabling TensorFloat32 (TF32) mode. On Ampere and later CUDA devices matrix multiplications and convolutions can use the TensorFloat32 (TF32) mode for faster but slightly less accurate computations. See the [Accelerating AI Training with NVIDIA TF32 Tensor Cores](https://developer.nvidia.com/blog/accelerating-ai-training-with-tf32-tensor-cores/) blog post for more details. By default PyTorch enables TF32 mode for convolutions but not matrix multiplications, and unless a network requires full float32", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "precision we recommend enabling this setting for matrix multiplications, too (see the documentation [here](https://pytorch.org/docs/master/generated/torch.set_float32_matmul_precision.html?highlight=precision#torch.set_float32_matmul_precision) for how to do so). It can significantly speed up computations with typically negligible loss of numerical accuracy.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Using torch.amp with bfloat16 or float16. Both these low precision floating point data types are usually comparably fast, but some networks may only converge with one vs the other. If a network requires more precision it may need to use float16, and if a network requires more dynamic range it may need to use bfloat16, whose dynamic range is equal to that of float32. If overflows are observed, for example, then we suggest trying bfloat16.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "There are even more advanced options than those presented here, like using torch.amp\u2019s autocasting for only parts of a model, or managing mixed precision directly. These topics are largely beyond the scope of this blog post, but see the \u201cBest Practices\u201d section below.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Best Practices\n\nWe strongly recommend using mixed precision with torch.amp or the TF32 mode (on Ampere and later CUDA devices) whenever possible when training a network. If one of those approaches doesn\u2019t work, however, we recommend the following:", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- High Performance Computing (HPC) applications, regression tasks, and generative networks may simply require full float32 IEEE precision to converge as expected.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Try selectively applying torch.amp. In particular we recommend first disabling it on regions performing operations from the torch.linalg module or when doing pre- or post-processing. These operations are often especially sensitive. Note that TF32 mode is a global switch and can\u2019t be used selectively on regions of a network. Enable TF32 first to check if a network\u2019s operators are sensitive to the mode, otherwise disable it.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- If you encounter type mismatches while using torch.amp we don\u2019t suggest inserting manual casts to start. This error is indicative of something being off with the network, and it\u2019s usually worth investigating first.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- Figure out by experimentation if your network is sensitive to range and/or precision of a format. For example [fine-tuning bfloat16-pretrained models in float16](https://github.com/huggingface/transformers/pull/10956) can easily run into range issues in float16 because of the potentially large range from training in bfloat16, so users should stick with bfloat16 fine-tuning if the model was trained in bfloat16.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "- The performance gain of mixed precision training can depend on multiple factors (e.g. compute-bound vs memory-bound problems) and users should use the [tuning guide](https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html) to remove other bottlenecks in their training scripts. Although having similar theoretical performance benefits, BF16 and FP16 can have different speeds in practice. It\u2019s recommended to try the mentioned formats and use the one with best speed while maintaining the desired", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "numeric behavior.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "For more details, refer to the [AMP Tutorial](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html), [Training Neural Networks with Tensor Cores](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/.), and see the post \u201c[More In-Depth Details of Floating Point Precision](https://dev-discuss.pytorch.org/t/more-in-depth-details-of-floating-point-precision/654)\" on PyTorch Dev Discussion.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "Conclusion\n\nMixed precision training is an essential tool for training deep learning models on modern hardware, and it will become even more important in the future as the performance gap between lower precision operations and float32 continues to grow on newer hardware, as reflected in Figure 5.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "

\nFigure 5: Relative peak throughput of float16 (FP16) vs float32 matrix multiplications on Volta and Ampere GPUs. On Ampere relative peak throughput for the TensorFloat32 (TF32) mode and bfloat16 matrix multiplications are shown, too. The relative peak throughput of low precision data types like float16 and bfloat16 vs. float32 matrix multiplications is expected to grow as new hardware is released.\n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "PyTorch\u2019s torch.amp module makes it easy to get started with mixed precision, and we highly recommend using it to train faster and reduce memory usage. torch.amp supports both float16 and bfloat16 mixed precision.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "There are still some networks that are tricky to train with mixed precision, and for these networks we recommend trying TF32 accelerated matrix multiplications on Ampere and later CUDA hardware. Networks are rarely so precision sensitive that they require full float32 precision for every operation.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Efficient training of modern neural networks often relies on using lower precision data types. Peak float16 matrix multiplication and convolution performance is 16x faster than peak float32 performance on A100 GPUs. And since the float16 and bfloat16 data types are only half the size of float32 they can double the performance of bandwidth-bound kernels and reduce the memory required to train a network, allowing for larger models, larger batches, or larger inputs. Using a module like [torch.amp](https://pytorch.org/docs/master/amp.html) (short for \u201cAutomated Mixed Precision\u201d) makes it easy to get the speed and memory usage benefits of lower precision data types while preserving convergence behavior.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Going faster and using less memory is always advantageous \u2013 deep learning practitioners can test more model architectures and hyperparameters, and larger, more powerful models can be trained. Training very large models like those described in [Narayanan et al.](https://arxiv.org/pdf/2104.04473.pdf) and [Brown et al.](https://arxiv.org/pdf/2005.14165.pdf) (which take thousands of GPUs months to train even with expert handwritten optimizations) is infeasible without using mixed precision.\n\nWe\u2019ve talked about mixed precision techniques before ([here](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/), [here](https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html), and [here](https://developer.nvidia.com/automatic-mixed-precision)), and this blog post is a summary of those techniques and an introduction if you\u2019re new to mixed precision.\n\n## Mixed Precision Training in Practice", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Mixed precision training techniques \u2013 the use of the lower precision float16 or bfloat16 data types alongside the float32 data type \u2013 are broadly applicable and effective. See Figure 1 for a sampling of models successfully trained with mixed precision, and Figures 2 and 3 for example speedups using torch.amp.\n\n

\n \n

\n\n

\n Figure 1: Sampling of DL Workloads Successfully Trained with float16 (Source).\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\n Figure 2: Performance of mixed precision training using torch.amp on NVIDIA 8xV100 vs. float32 training on 8xV100 GPU. Bars represent the speedup factor of torch.amp over float32. \n(Higher is better.) (Source).\n

\n\n

\n \n

\n\n

\n Figure 3. Performance of mixed precision training using torch.amp on NVIDIA 8xA100 vs. 8xV100 GPU. Bars represent the speedup factor of A100 over V100.\n(Higher is Better.) (Source).\n

\n\nSee the [NVIDIA Deep Learning Examples repository](https://github.com/NVIDIA/DeepLearningExamples) for more sample mixed precision workloads.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "Similar performance charts can be seen in [3D medical image analysis](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/dong_yang-mixed-precision-training-for-3d-medical-image-analysis.pdf), [gaze estimation](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/shalini_de_mello-mixed-precision-training-for-faze.pdf), [video synthesis](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/tingchun_wang-mixed-precision-vid2vid.pdf), [conditional GANs](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/mingyu_liu-amp-imaginaire.pdf), and [convolutional LSTMs](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/files/wonmin_byeon-mixed-precision-training-for-convolutional-tensor-train-lstm.pdf). [Huang et al](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/). showed that mixed precision training is 1.5x to 5.5x faster over float32 on V100 GPUs, and an additional 1.3x to 2.5x faster on A100 GPUs on a variety of networks. On very large networks the need for mixed precision is even more evident. [Narayanan et al](https://arxiv.org/pdf/2104.04473.pdf). reports that it would take 34 days to train GPT-3 175B on 1024 A100 GPUs (with a batch size of 1536), but it\u2019s estimated it would take over a year using float32!", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "## Getting Started With Mixed Precision Using torch.amp\n\ntorch.amp, introduced in PyTorch 1.6, makes it easy to leverage mixed precision training using the float16 or bfloat16 dtypes. See this [blog post](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/), [tutorial](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html), and [documentation](https://pytorch.org/docs/master/amp.html) for more details. Figure 4 shows an example of applying AMP with grad scaling to a network.\n\n```console\nimport torch\n# Creates once at the beginning of training\nscaler = torch.cuda.amp.GradScaler()\n\nfor data, label in data_iter:\n optimizer.zero_grad()\n # Casts operations to mixed precision\n with torch.amp.autocast(device_type=\u201ccuda\u201d, dtype=torch.float16):\n loss = model(data)\n\n # Scales the loss, and calls backward()\n # to create scaled gradients\n scaler.scale(loss).backward()", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "# Unscales gradients and calls\n # or skips optimizer.step()\n scaler.step(optimizer)\n\n # Updates the scale for next iteration\n scaler.update()\n```\n\n

\n Figure 4: AMP recipe\n

\n\n### Picking The Right Approach\n\nOut-of-the-box mixed precision training with either float16 or bfloat16 is effective at speeding up the convergence of many deep learning models, but some models may require more careful numerical accuracy management. Here are some options:", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "- Full float32 precision. Floating point tensors and modules are created in float32 precision by default in PyTorch, but this is a historic artifact not representative of training most modern deep learning networks. It\u2019s rare that networks need this much numerical accuracy.\n- Enabling TensorFloat32 (TF32) mode. On Ampere and later CUDA devices matrix multiplications and convolutions can use the TensorFloat32 (TF32) mode for faster but slightly less accurate computations. See the [Accelerating AI Training with NVIDIA TF32 Tensor Cores](https://developer.nvidia.com/blog/accelerating-ai-training-with-tf32-tensor-cores/) blog post for more details. By default PyTorch enables TF32 mode for convolutions but not matrix multiplications, and unless a network requires full float32 precision we recommend enabling this setting for matrix multiplications, too (see the documentation [here](https://pytorch.org/docs/master/generated/torch.set_float32_matmul_precision.html?highlight=precision#torch.set_float32_matmul_precision) for how to do so). It can significantly speed up computations with typically negligible loss of numerical accuracy.\n- Using torch.amp with bfloat16 or float16. Both these low precision floating point data types are usually comparably fast, but some networks may only converge with one vs the other. If a network requires more precision it may need to use float16, and if a network requires more dynamic range it may need to use bfloat16, whose dynamic range is equal to that of float32. If overflows are observed, for example, then we suggest trying bfloat16.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "There are even more advanced options than those presented here, like using torch.amp\u2019s autocasting for only parts of a model, or managing mixed precision directly. These topics are largely beyond the scope of this blog post, but see the \u201cBest Practices\u201d section below.\n\n### Best Practices\n\nWe strongly recommend using mixed precision with torch.amp or the TF32 mode (on Ampere and later CUDA devices) whenever possible when training a network. If one of those approaches doesn\u2019t work, however, we recommend the following:", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "- High Performance Computing (HPC) applications, regression tasks, and generative networks may simply require full float32 IEEE precision to converge as expected.\n- Try selectively applying torch.amp. In particular we recommend first disabling it on regions performing operations from the torch.linalg module or when doing pre- or post-processing. These operations are often especially sensitive. Note that TF32 mode is a global switch and can\u2019t be used selectively on regions of a network. Enable TF32 first to check if a network\u2019s operators are sensitive to the mode, otherwise disable it.\n- If you encounter type mismatches while using torch.amp we don\u2019t suggest inserting manual casts to start. This error is indicative of something being off with the network, and it\u2019s usually worth investigating first.\n- Figure out by experimentation if your network is sensitive to range and/or precision of a format. For example [fine-tuning bfloat16-pretrained models in float16](https://github.com/huggingface/transformers/pull/10956) can easily run into range issues in float16 because of the potentially large range from training in bfloat16, so users should stick with bfloat16 fine-tuning if the model was trained in bfloat16.\n- The performance gain of mixed precision training can depend on multiple factors (e.g. compute-bound vs memory-bound problems) and users should use the [tuning guide](https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html) to remove other bottlenecks in their training scripts. Although having similar theoretical performance benefits, BF16 and FP16 can have different speeds in practice. It\u2019s recommended to try the mentioned formats and use the one with best speed while maintaining the desired numeric behavior.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "For more details, refer to the [AMP Tutorial](https://pytorch.org/tutorials/recipes/recipes/amp_recipe.html), [Training Neural Networks with Tensor Cores](https://nvlabs.github.io/eccv2020-mixed-precision-tutorial/.), and see the post \u201c[More In-Depth Details of Floating Point Precision](https://dev-discuss.pytorch.org/t/more-in-depth-details-of-floating-point-precision/654)\" on PyTorch Dev Discussion.\n\n## Conclusion\n\nMixed precision training is an essential tool for training deep learning models on modern hardware, and it will become even more important in the future as the performance gap between lower precision operations and float32 continues to grow on newer hardware, as reflected in Figure 5.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} +{"page_content": "

\nFigure 5: Relative peak throughput of float16 (FP16) vs float32 matrix multiplications on Volta and Ampere GPUs. On Ampere relative peak throughput for the TensorFloat32 (TF32) mode and bfloat16 matrix multiplications are shown, too. The relative peak throughput of low precision data types like float16 and bfloat16 vs. float32 matrix multiplications is expected to grow as new hardware is released.\n

\n\nPyTorch\u2019s torch.amp module makes it easy to get started with mixed precision, and we highly recommend using it to train faster and reduce memory usage. torch.amp supports both float16 and bfloat16 mixed precision.\n\nThere are still some networks that are tricky to train with mixed precision, and for these networks we recommend trying TF32 accelerated matrix multiplications on Ampere and later CUDA hardware. Networks are rarely so precision sensitive that they require full float32 precision for every operation.", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} {"page_content": "If you have questions or suggestions for torch.amp or mixed precision support in PyTorch then let us know by posting to the [mixed precision category on the PyTorch Forums](https://discuss.pytorch.org/c/mixed-precision/27) or [filing an issue on the PyTorch GitHub page](https://github.com/pytorch/pytorch/issues/new/choose).", "metadata": {"source": "https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Everything you need to know about TorchVision\u2019s MobileNetV3 implementation'\nauthor: Vasilis Vryniotis and Francisco Massa\n---", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "In TorchVision v0.9, we released a series of [new mobile-friendly models](https://pytorch.org/blog/ml-models-torchvision-v0.9/) that can be used for Classification, Object Detection and Semantic Segmentation. In this article, we will dig deep into the code of the models, share notable implementation details, explain how we configured and trained them, and highlight important tradeoffs we made during their tuning. Our goal is to disclose technical details that typically remain undocumented in the original", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "papers and repos of the models.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Network Architecture", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The implementation of the [MobileNetV3 architecture](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py) follows closely the [original paper](https://arxiv.org/abs/1905.02244). It is customizable and offers different configurations for building Classification, Object Detection and Semantic Segmentation backbones. It was designed to follow a similar structure to MobileNetV2 and the two share [common building", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "blocks](https://github.com/pytorch/vision/blob/cac8a97b0bd14eddeff56f87a890d5cc85776e18/torchvision/models/mobilenetv2.py#L32).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Off-the-shelf, we offer the two variants described on the paper: the [Large](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L196-L214) and the [Small](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L215-L229). Both are constructed using the same code with the only difference being their configuration which describes the number of blocks, their sizes, their activation", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "functions etc.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Configuration parameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Even though one can write a [custom InvertedResidual setting](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L105) and pass it to the MobileNetV3 class directly, for the majority of applications we can adapt the existing configs by passing parameters to the [model building methods](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L253). Some of the key configuration parameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "are the following:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- The `width_mult` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) is a multiplier that affects the number of channels of the model. The default value is 1 and by increasing or decreasing it one can change the number of filters of all convolutions, including the ones of the first and last layers. The implementation ensures that the number of filters is always a [multiple of", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "8](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L56-L57). This is a hardware optimization trick which allows for faster vectorization of operations.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- The `reduced_tail` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) halves the number of channels on the [last blocks](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L210-L214) of the network. This version is used by some Object Detection and Semantic Segmentation models. It\u2019s a speed optimization which is described on the [MobileNetV3", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "paper](https://arxiv.org/abs/1905.02244) and reportedly leads to a 15% latency reduction without a significant negative effect on accuracy.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- The `dilated` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) affects the [last 3](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L210-L212) InvertedResidual blocks of the model and turns their normal depthwise Convolutions to Atrous Convolutions. This is used to control the output stride of these blocks and has a [significant positive", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "effect](https://arxiv.org/abs/1706.05587) on the accuracy of Semantic Segmentation models.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Implementation details\n\nBelow we provide additional information on some notable implementation details of the architecture.\nThe [MobileNetV3 class](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L101) is responsible for building a network out of the provided configuration. Here are some implementation details of the class:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- The last convolution block expands the output of the last InvertedResidual block by a [factor of 6](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L149). The implementation is aligned with the Large and Small configurations described on the paper and can adapt to different values of the multiplier parameter.\n\n- Similarly to other models such as MobileNetV2, a dropout layer is placed just before the final Linear layer of the classifier.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The [InvertedResidual class](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L60) is the main building block of the network. Here are some notable implementation details of the block along with its visualization which comes from Figure 4 of the paper:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- There is no [expansion step](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L73-L76) if the input channels and the expanded channels are the same. This happens on the first convolution block of the network.\n\n- There is always a [projection step](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L86-L88) even when the expanded channels are the same as the output channels.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "- The activation method of the depthwise block is placed [before](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L82-L84) the Squeeze-and-Excite layer as this improves marginally the accuracy.\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Classification\n\nIn this section we provide benchmarks of the pre-trained models and details on how they were configured, trained and quantized.\n\n**Benchmarks**\n\nHere is how to initialize the pre-trained models:\n```\nlarge = torchvision.models.mobilenet_v3_large(pretrained=True, width_mult=1.0, reduced_tail=False, dilated=False)\nsmall = torchvision.models.mobilenet_v3_small(pretrained=True)\nquantized = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Below we have the detailed benchmarks between new and selected previous models. As we can see MobileNetV3-Large is a viable replacement of ResNet50 for users who are willing to sacrifice a bit of accuracy for a roughly 6x speed-up:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Model | Acc@1 | Acc@5 | Inference on CPU (sec) | # Params (M) |\n|-----------------------------|--------:|--------:|------------------------:|--------------:|\n| MobileNetV3-Large | 74.042 | 91.340 | 0.0411 | 5.48 |\n| MobileNetV3-Small | 67.668 | 87.402 | 0.0165 | 2.54 |\n| Quantized MobileNetV3-Large | 73.004 | 90.858 | 0.0162 | 2.96 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| MobileNetV2 | 71.880 | 90.290 | 0.0608 | 3.50 |\n| ResNet50 | 76.150 | 92.870 | 0.2545 | 25.56 |\n| ResNet18 | 69.760 | 89.080 | 0.1032 | 11.69 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Note that the inference times are measured on CPU. They are not absolute benchmarks, but they allow for relative comparisons between models.\n\n**Training process**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "All pre-trained models are configured with a width multiplier of 1, have full tails, are non-dilated, and were fitted on ImageNet. Both the Large and Small variants were trained using the same hyper-parameters and scripts which can be found in our [references](https://github.com/pytorch/vision/tree/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification#mobilenetv3-large--small) folder. Below we provide details on the most notable aspects of the training process.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Achieving fast and stable training**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "[Configuring RMSProp](https://github.com/pytorch/vision/blob/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification/train.py#L172-L173) correctly was crucial to achieve fast training with numerical stability. The authors of the paper used TensorFlow in their experiments and in their runs they reported using [quite high](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet#v3) `rmsprop_epsilon` comparing to the default. Typically this hyper-parameter takes small values as", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "it\u2019s used to avoid zero denominators, but in this specific model choosing the right value seems important to avoid numerical instabilities in the loss.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Another important detail is that though PyTorch\u2019s and TensorFlow\u2019s RMSProp implementations typically behave similarly, there are [a few differences](https://github.com/pytorch/pytorch/issues/32545) with the most notable in our setup being how the epsilon hyperparameter is handled. More specifically, PyTorch adds the epsilon [outside of the square root calculation](https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/python/training/rmsprop.py#L25) while TensorFlow [adds it", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "inside](https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/python/training/rmsprop.py#L25). The result of this implementation detail is that one needs to adjust the epsilon value while porting the hyper parameter of the paper. A reasonable approximation can be taken with the formula `PyTorch_eps = sqrt(TF_eps)`.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Increasing our accuracy by tuning hyperparameters & improving our training recipe**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "After configuring the optimizer to achieve fast and stable training, we turned into optimizing the accuracy of the model. There are a few techniques that helped us achieve this. First of all, to avoid overfitting we augmented out data using the AutoAugment algorithm, followed by RandomErasing. Additionally we tuned parameters such as the weight decay using cross validation. We also found beneficial to perform [weight", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "averaging](https://github.com/pytorch/vision/blob/674e8140042c2a3cbb1eb9ebad1fa49501599130/references/classification/utils.py#L259) across different epoch checkpoints after the end of the training. Finally, though not used in our published training recipe, we found that using Label Smoothing, Stochastic Depth and LR noise injection improve the overall accuracy by over [1.5 points](https://rwightman.github.io/pytorch-image-models/training_hparam_examples/#mobilenetv3-large-100-75766-top-1-92542-top-5).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The graph and table depict a simplified summary of the most important iterations for improving the accuracy of the MobileNetV3 Large variant. Note that the actual number of iterations done while training the model was significantly larger and that the progress in accuracy was not always monotonically increasing. Also note that the Y-axis of the graph starts from 70% instead from 0% to make the difference between iterations more visible:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Iteration | Acc@1 | Acc@5 |\n|-------------------------------------------------|--------:|--------:|\n| Baseline with \"MobileNetV2-style\" Hyperparams | 71.542 | 90.068 |\n| + RMSProp with default eps | 70.684 | 89.38 |\n| + RMSProp with adjusted eps & LR scheme | 71.764 | 90.178 |\n| + Data Augmentation & Tuned Hyperparams | 73.86 | 91.292 |\n| + Checkpoint Averaging | 74.028 | 91.382 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| + Label Smoothing & Stochastic Depth & LR noise | 75.536 | 92.368 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Note that once we\u2019ve achieved an acceptable accuracy, we verified the model performance on the hold-out test dataset which hasn't been used before for training or hyper-parameter tuning. This process helps us detect overfitting and is always performed for all pre-trained models prior their release.\n\n**Quantization**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "We currently offer quantized weights for the QNNPACK backend of the [MobileNetV3-Large variant](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/quantization/mobilenetv3.py#L115) which provides a speed-up of 2.5x. To quantize the model, Quantized Aware Training (QAT) was used. The hyper parameters and the scripts used to train the model can be found in our", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "[references](https://github.com/pytorch/vision/tree/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification#quantized) folder.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Note that QAT allows us to model the effects of quantization and adjust the weights so that we can improve the model accuracy. This translates to an accuracy increase of 1.8 points comparing to simple post-training quantization:\n\n| Quantization Status | Acc@1 | Acc@5 |\n|----------------------------|--------:|--------:|\n| Non-quantized | 74.042 | 91.340 |\n| Quantized Aware Training | 73.004 | 90.858 |\n| Post-training Quantization | 71.160 | 89.834 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Object Detection", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "In this section, we will first provide benchmarks of the released models, and then discuss how the MobileNetV3-Large backbone was used in a Feature Pyramid Network along with the FasterRCNN detector to perform Object Detection. We will also explain how the network was trained and tuned alongside with any tradeoffs we had to make. We will not cover details about how it was used with", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "[SSDlite](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/detection/ssdlite.py) as this will be discussed on a future article.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Benchmarks**\n\nHere is how the models are initialized:\n```\nhigh_res = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True) \nlow_res = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True)", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Below are some benchmarks between new and selected previous models. As we can see the high resolution Faster R-CNN with MobileNetV3-Large FPN backbone seems a viable replacement of the equivalent ResNet50 model for those users who are willing to sacrifice few accuracy points for a 5x speed-up:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Model | mAP | Inference on CPU (sec) | # Params (M) |\n|--------------------------------------------------|------:|------------------------:|--------------:|\n| Faster R-CNN MobileNetV3-Large FPN (High-Res) | 32.8 | 0.8409 | 19.39 |\n| Faster R-CNN MobileNetV3-Large 320 FPN (Low-Res) | 22.8 | 0.1679 | 19.39 |\n| Faster R-CNN ResNet-50 FPN | 37.0 | 4.1514 | 41.76 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| RetinaNet ResNet-50 FPN | 36.4 | 4.8825 | 34.01 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Implementation details**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The Detector uses a FPN-style backbone which extracts features from different convolutions of the MobileNetV3 model. [By default](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L165-L166) the pre-trained model uses the output of the 13th InvertedResidual block and the output of the Convolution prior to the pooling layer but the implementation supports using the outputs of [more", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "stages](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L147-L150).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "All feature maps extracted from the network have their output projected down to [256 channels](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L160) by the FPN block as this greatly improves the speed of the network. These feature maps provided by the FPN backbone are used by the FasterRCNN detector to provide box and class predictions at [different", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "scales](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L382-L389).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Training & Tuning process**\n\nWe currently offer two pre-trained models capable of doing object detection at different resolutions. Both models were trained on the COCO dataset using the same hyper-parameters and scripts which can be found in our [references](https://github.com/pytorch/vision/tree/e35793a1a4000db1f9f99673437c514e24e65451/references/detection#faster-r-cnn-mobilenetv3-large-fpn) folder.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The [High Resolution detector](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L398-L399) was trained with images of 800-1333px, while the mobile-friendly [Low Resolution detector](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L398-L399) was trained with images of 320-640px. The reason why we provide two separate sets of pre-trained weights is because", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "training a detector directly on the smaller images leads to a 5 mAP increase in precision comparing to passing small images to the pre-trained high-res model. Both backbones were initialized with weights fitted on ImageNet and the [3 last stages](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L377-L378) of their weights where fined-tuned during the training process.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "An additional speed optimization can be applied on the mobile-friendly model by [tuning the RPN NMS thresholds](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L423-L424). By sacrificing only 0.2 mAP of precision we were able to improve the CPU speed of the model by roughly 45%. The details of the optimization can be seen below:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Tuning Status | mAP | Inference on CPU (sec) |\n|---------------|------:|------------------------:|\n| Before | 23.0 | 0.2904 |\n| After | 22.8 | 0.1679 |\n\nBelow we provide some examples of visualizing the predictions of the Faster R-CNN MobileNetV3-Large FPN model:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Semantic Segmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "In this section we will start by providing some benchmarks of the released pre-trained models. Then we will discuss how a MobileNetV3-Large backbone was combined with segmentation heads such as [LR-ASPP](https://arxiv.org/abs/1905.02244), [DeepLabV3](https://arxiv.org/abs/1706.05587) and the [FCN](https://arxiv.org/abs/1411.4038) to conduct Semantic Segmentation. We will also explain how the network was trained and propose a few optional optimization techniques for speed critical applications.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**Benchmarks**\n\nThis is how to initialize the pre-trained models:\n\n```\nlraspp = torchvision.models.segmentation.lraspp_mobilenet_v3_large(pretrained=True) \ndeeplabv3 = torchvision.models.segmentation.deeplabv3_mobilenet_v3_large(pretrained=True)", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Everything you need to know about TorchVision\u2019s MobileNetV3 implementation'\nauthor: Vasilis Vryniotis and Francisco Massa\n---\n\nIn TorchVision v0.9, we released a series of [new mobile-friendly models](https://pytorch.org/blog/ml-models-torchvision-v0.9/) that can be used for Classification, Object Detection and Semantic Segmentation. In this article, we will dig deep into the code of the models, share notable implementation details, explain how we configured and trained them, and highlight important tradeoffs we made during their tuning. Our goal is to disclose technical details that typically remain undocumented in the original papers and repos of the models.\n\n### Network Architecture", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "The implementation of the [MobileNetV3 architecture](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py) follows closely the [original paper](https://arxiv.org/abs/1905.02244). It is customizable and offers different configurations for building Classification, Object Detection and Semantic Segmentation backbones. It was designed to follow a similar structure to MobileNetV2 and the two share [common building blocks](https://github.com/pytorch/vision/blob/cac8a97b0bd14eddeff56f87a890d5cc85776e18/torchvision/models/mobilenetv2.py#L32).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "Off-the-shelf, we offer the two variants described on the paper: the [Large](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L196-L214) and the [Small](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L215-L229). Both are constructed using the same code with the only difference being their configuration which describes the number of blocks, their sizes, their activation functions etc.\n\n### Configuration parameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "Even though one can write a [custom InvertedResidual setting](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L105) and pass it to the MobileNetV3 class directly, for the majority of applications we can adapt the existing configs by passing parameters to the [model building methods](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L253). Some of the key configuration parameters are the following:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "- The `width_mult` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) is a multiplier that affects the number of channels of the model. The default value is 1 and by increasing or decreasing it one can change the number of filters of all convolutions, including the ones of the first and last layers. The implementation ensures that the number of filters is always a [multiple of 8](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L56-L57). This is a hardware optimization trick which allows for faster vectorization of operations.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "- The `reduced_tail` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) halves the number of channels on the [last blocks](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L210-L214) of the network. This version is used by some Object Detection and Semantic Segmentation models. It\u2019s a speed optimization which is described on the [MobileNetV3 paper](https://arxiv.org/abs/1905.02244) and reportedly leads to a 15% latency reduction without a significant negative effect on accuracy.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "- The `dilated` [parameter](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L188) affects the [last 3](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L210-L212) InvertedResidual blocks of the model and turns their normal depthwise Convolutions to Atrous Convolutions. This is used to control the output stride of these blocks and has a [significant positive effect](https://arxiv.org/abs/1706.05587) on the accuracy of Semantic Segmentation models.\n\n### Implementation details\n\nBelow we provide additional information on some notable implementation details of the architecture.\nThe [MobileNetV3 class](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L101) is responsible for building a network out of the provided configuration. Here are some implementation details of the class:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "- The last convolution block expands the output of the last InvertedResidual block by a [factor of 6](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L149). The implementation is aligned with the Large and Small configurations described on the paper and can adapt to different values of the multiplier parameter.\n\n- Similarly to other models such as MobileNetV2, a dropout layer is placed just before the final Linear layer of the classifier.\n\nThe [InvertedResidual class](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L60) is the main building block of the network. Here are some notable implementation details of the block along with its visualization which comes from Figure 4 of the paper:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "- There is no [expansion step](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L73-L76) if the input channels and the expanded channels are the same. This happens on the first convolution block of the network.\n\n- There is always a [projection step](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L86-L88) even when the expanded channels are the same as the output channels.\n\n- The activation method of the depthwise block is placed [before](https://github.com/pytorch/vision/blob/11bf27e37190b320216c349e39b085fb33aefed1/torchvision/models/mobilenetv3.py#L82-L84) the Squeeze-and-Excite layer as this improves marginally the accuracy.\n\n
\n \n
\n\n### Classification", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "### Classification\n\nIn this section we provide benchmarks of the pre-trained models and details on how they were configured, trained and quantized.\n\n**Benchmarks**\n\nHere is how to initialize the pre-trained models:\n```\nlarge = torchvision.models.mobilenet_v3_large(pretrained=True, width_mult=1.0, reduced_tail=False, dilated=False)\nsmall = torchvision.models.mobilenet_v3_small(pretrained=True)\nquantized = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\n```\n\nBelow we have the detailed benchmarks between new and selected previous models. As we can see MobileNetV3-Large is a viable replacement of ResNet50 for users who are willing to sacrifice a bit of accuracy for a roughly 6x speed-up:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Model | Acc@1 | Acc@5 | Inference on CPU (sec) | # Params (M) |\n|-----------------------------|--------:|--------:|------------------------:|--------------:|\n| MobileNetV3-Large | 74.042 | 91.340 | 0.0411 | 5.48 |\n| MobileNetV3-Small | 67.668 | 87.402 | 0.0165 | 2.54 |\n| Quantized MobileNetV3-Large | 73.004 | 90.858 | 0.0162 | 2.96 |\n| MobileNetV2 | 71.880 | 90.290 | 0.0608 | 3.50 |\n| ResNet50 | 76.150 | 92.870 | 0.2545 | 25.56 |\n| ResNet18 | 69.760 | 89.080 | 0.1032 | 11.69 |\n\nNote that the inference times are measured on CPU. They are not absolute benchmarks, but they allow for relative comparisons between models.\n\n**Training process**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "**Training process**\n\nAll pre-trained models are configured with a width multiplier of 1, have full tails, are non-dilated, and were fitted on ImageNet. Both the Large and Small variants were trained using the same hyper-parameters and scripts which can be found in our [references](https://github.com/pytorch/vision/tree/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification#mobilenetv3-large--small) folder. Below we provide details on the most notable aspects of the training process.\n\n **Achieving fast and stable training**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "[Configuring RMSProp](https://github.com/pytorch/vision/blob/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification/train.py#L172-L173) correctly was crucial to achieve fast training with numerical stability. The authors of the paper used TensorFlow in their experiments and in their runs they reported using [quite high](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet#v3) `rmsprop_epsilon` comparing to the default. Typically this hyper-parameter takes small values as it\u2019s used to avoid zero denominators, but in this specific model choosing the right value seems important to avoid numerical instabilities in the loss.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "Another important detail is that though PyTorch\u2019s and TensorFlow\u2019s RMSProp implementations typically behave similarly, there are [a few differences](https://github.com/pytorch/pytorch/issues/32545) with the most notable in our setup being how the epsilon hyperparameter is handled. More specifically, PyTorch adds the epsilon [outside of the square root calculation](https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/python/training/rmsprop.py#L25) while TensorFlow [adds it inside](https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/python/training/rmsprop.py#L25). The result of this implementation detail is that one needs to adjust the epsilon value while porting the hyper parameter of the paper. A reasonable approximation can be taken with the formula `PyTorch_eps = sqrt(TF_eps)`.\n\n**Increasing our accuracy by tuning hyperparameters & improving our training recipe**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "After configuring the optimizer to achieve fast and stable training, we turned into optimizing the accuracy of the model. There are a few techniques that helped us achieve this. First of all, to avoid overfitting we augmented out data using the AutoAugment algorithm, followed by RandomErasing. Additionally we tuned parameters such as the weight decay using cross validation. We also found beneficial to perform [weight averaging](https://github.com/pytorch/vision/blob/674e8140042c2a3cbb1eb9ebad1fa49501599130/references/classification/utils.py#L259) across different epoch checkpoints after the end of the training. Finally, though not used in our published training recipe, we found that using Label Smoothing, Stochastic Depth and LR noise injection improve the overall accuracy by over [1.5 points](https://rwightman.github.io/pytorch-image-models/training_hparam_examples/#mobilenetv3-large-100-75766-top-1-92542-top-5).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "The graph and table depict a simplified summary of the most important iterations for improving the accuracy of the MobileNetV3 Large variant. Note that the actual number of iterations done while training the model was significantly larger and that the progress in accuracy was not always monotonically increasing. Also note that the Y-axis of the graph starts from 70% instead from 0% to make the difference between iterations more visible:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Iteration | Acc@1 | Acc@5 |\n|-------------------------------------------------|--------:|--------:|\n| Baseline with \"MobileNetV2-style\" Hyperparams | 71.542 | 90.068 |\n| + RMSProp with default eps | 70.684 | 89.38 |\n| + RMSProp with adjusted eps & LR scheme | 71.764 | 90.178 |\n| + Data Augmentation & Tuned Hyperparams | 73.86 | 91.292 |\n| + Checkpoint Averaging | 74.028 | 91.382 |\n| + Label Smoothing & Stochastic Depth & LR noise | 75.536 | 92.368 |\n\nNote that once we\u2019ve achieved an acceptable accuracy, we verified the model performance on the hold-out test dataset which hasn't been used before for training or hyper-parameter tuning. This process helps us detect overfitting and is always performed for all pre-trained models prior their release.\n\n**Quantization**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "**Quantization**\n\nWe currently offer quantized weights for the QNNPACK backend of the [MobileNetV3-Large variant](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/quantization/mobilenetv3.py#L115) which provides a speed-up of 2.5x. To quantize the model, Quantized Aware Training (QAT) was used. The hyper parameters and the scripts used to train the model can be found in our [references](https://github.com/pytorch/vision/tree/c2ab0c59f42babf9ad01aa616cd8a901daac86dd/references/classification#quantized) folder.\n\nNote that QAT allows us to model the effects of quantization and adjust the weights so that we can improve the model accuracy. This translates to an accuracy increase of 1.8 points comparing to simple post-training quantization:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Quantization Status | Acc@1 | Acc@5 |\n|----------------------------|--------:|--------:|\n| Non-quantized | 74.042 | 91.340 |\n| Quantized Aware Training | 73.004 | 90.858 |\n| Post-training Quantization | 71.160 | 89.834 |\n\n### Object Detection\n\nIn this section, we will first provide benchmarks of the released models, and then discuss how the MobileNetV3-Large backbone was used in a Feature Pyramid Network along with the FasterRCNN detector to perform Object Detection. We will also explain how the network was trained and tuned alongside with any tradeoffs we had to make. We will not cover details about how it was used with [SSDlite](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/detection/ssdlite.py) as this will be discussed on a future article.\n\n**Benchmarks**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "**Benchmarks**\n\nHere is how the models are initialized:\n```\nhigh_res = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True) \nlow_res = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True)\n```\n\nBelow are some benchmarks between new and selected previous models. As we can see the high resolution Faster R-CNN with MobileNetV3-Large FPN backbone seems a viable replacement of the equivalent ResNet50 model for those users who are willing to sacrifice few accuracy points for a 5x speed-up:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Model | mAP | Inference on CPU (sec) | # Params (M) |\n|--------------------------------------------------|------:|------------------------:|--------------:|\n| Faster R-CNN MobileNetV3-Large FPN (High-Res) | 32.8 | 0.8409 | 19.39 |\n| Faster R-CNN MobileNetV3-Large 320 FPN (Low-Res) | 22.8 | 0.1679 | 19.39 |\n| Faster R-CNN ResNet-50 FPN | 37.0 | 4.1514 | 41.76 |\n| RetinaNet ResNet-50 FPN | 36.4 | 4.8825 | 34.01 |\n\n**Implementation details**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "The Detector uses a FPN-style backbone which extracts features from different convolutions of the MobileNetV3 model. [By default](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L165-L166) the pre-trained model uses the output of the 13th InvertedResidual block and the output of the Convolution prior to the pooling layer but the implementation supports using the outputs of [more stages](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L147-L150).", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "All feature maps extracted from the network have their output projected down to [256 channels](https://github.com/pytorch/vision/blob/eca37cf735064702189ff5d5b1428cbe25ab2bcf/torchvision/models/detection/backbone_utils.py#L160) by the FPN block as this greatly improves the speed of the network. These feature maps provided by the FPN backbone are used by the FasterRCNN detector to provide box and class predictions at [different scales](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L382-L389).\n\n**Training & Tuning process**\n\nWe currently offer two pre-trained models capable of doing object detection at different resolutions. Both models were trained on the COCO dataset using the same hyper-parameters and scripts which can be found in our [references](https://github.com/pytorch/vision/tree/e35793a1a4000db1f9f99673437c514e24e65451/references/detection#faster-r-cnn-mobilenetv3-large-fpn) folder.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "The [High Resolution detector](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L398-L399) was trained with images of 800-1333px, while the mobile-friendly [Low Resolution detector](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L398-L399) was trained with images of 320-640px. The reason why we provide two separate sets of pre-trained weights is because training a detector directly on the smaller images leads to a 5 mAP increase in precision comparing to passing small images to the pre-trained high-res model. Both backbones were initialized with weights fitted on ImageNet and the [3 last stages](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L377-L378) of their weights where fined-tuned during the training process.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "An additional speed optimization can be applied on the mobile-friendly model by [tuning the RPN NMS thresholds](https://github.com/pytorch/vision/blob/7af30ee9ab64039d04150d118e8b72473184fd6e/torchvision/models/detection/faster_rcnn.py#L423-L424). By sacrificing only 0.2 mAP of precision we were able to improve the CPU speed of the model by roughly 45%. The details of the optimization can be seen below:\n\n| Tuning Status | mAP | Inference on CPU (sec) |\n|---------------|------:|------------------------:|\n| Before | 23.0 | 0.2904 |\n| After | 22.8 | 0.1679 |\n\nBelow we provide some examples of visualizing the predictions of the Faster R-CNN MobileNetV3-Large FPN model:\n\n
\n \n
\n\n### Semantic Segmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "In this section we will start by providing some benchmarks of the released pre-trained models. Then we will discuss how a MobileNetV3-Large backbone was combined with segmentation heads such as [LR-ASPP](https://arxiv.org/abs/1905.02244), [DeepLabV3](https://arxiv.org/abs/1706.05587) and the [FCN](https://arxiv.org/abs/1411.4038) to conduct Semantic Segmentation. We will also explain how the network was trained and propose a few optional optimization techniques for speed critical applications.\n\n**Benchmarks**\n\nThis is how to initialize the pre-trained models:\n\n```\nlraspp = torchvision.models.segmentation.lraspp_mobilenet_v3_large(pretrained=True) \ndeeplabv3 = torchvision.models.segmentation.deeplabv3_mobilenet_v3_large(pretrained=True)\n```", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} {"page_content": "Below are the detailed benchmarks between new and selected existing models. As we can see, the DeepLabV3 with a MobileNetV3-Large backbone is a viable replacement of FCN with ResNet50 for the majority of applications as it achieves similar accuracy with a 8.5x speed-up. We also observe that the LR-ASPP network supersedes the equivalent FCN in all metrics:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Model | mIoU | Global Pixel Acc | Inference on CPU (sec) | # Params (M) |\n|--------------------------------------|------:|------------------:|------------------------:|--------------:|\n| LR-ASPP MobileNetV3-Large | 57.9 | 91.2 | 0.3278 | 3.22 |\n| DeepLabV3 MobileNetV3-Large | 60.3 | 91.2 | 0.5869 | 11.03 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| FCN MobileNetV3-Large (not released) | 57.8 | 90.9 | 0.3702 | 5.05 |\n| DeepLabV3 ResNet50 | 66.4 | 92.4 | 6.3531 | 39.64 |\n| FCN ResNet50 | 60.5 | 91.4 | 5.0146 | 32.96 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Implementation details\n\nIn this section we will discuss important implementation details of tested segmentation heads. Note that all models described in this section use a dilated MobileNetV3-Large backbone.\n\n**LR-ASPP**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The LR-ASPP is the Lite variant of the Reduced Atrous Spatial Pyramid Pooling model proposed by the authors of the MobileNetV3 paper. Unlike the other segmentation models in TorchVision, it does not make use of an [auxiliary loss](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L185-L186). Instead it uses [low and high-level", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "features](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L92-L100) with output strides of 8 and 16 respectively.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Unlike the paper where a 49x49 AveragePooling layer with variable strides is used, [our implementation](https://github.com/pytorch/vision/blob/e2db2eddbb1699a59fbb5ccbec912979048ef3bf/torchvision/models/segmentation/lraspp.py#L53) uses an `AdaptiveAvgPool2d` layer to process the global features. This is because the authors of the paper tailored the head to the Cityscapes dataset while our focus is to provide a general purpose implementation that can work on multiple datasets. Finally our implementation", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "always has a bilinear interpolation [before returning the output](https://github.com/pytorch/vision/blob/e2db2eddbb1699a59fbb5ccbec912979048ef3bf/torchvision/models/segmentation/lraspp.py#L35) to ensure that the sizes of the input and output images match exactly.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "**DeepLabV3 & FCN**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "The combination of MobileNetV3 with DeepLabV3 and FCN follows closely the ones of other models and the stage estimation for these methods is identical to LR-ASPP. The only notable difference is that instead of using high and low level features, [we attach](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L37-L45) the normal loss to the feature map with output stride 16 and an auxiliary loss on the feature map with output stride", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "8.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Finally we should note that the FCN version of the model was not released because it was completely superseded by the LR-ASPP both in terms of speed and accuracy. The [pre-trained weights](https://github.com/pytorch/vision/pull/3276/commits/1641d5f4c7d41f534444fab340c598d61a91bd12#diff-ccff7af514d99eeb40416c8b9ec30f032d1a3f450aaa4057958ca39ab174452eL17) are still available and can be used with minimal changes to the code.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Training & Tuning process", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "We currently offer two MobileNetV3 pre-trained models capable of doing semantic segmentation: the LR-ASPP and the DeepLabV3. The backbones of the models were [initialized with ImageNet weights](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L89-L90) and trained end-to-end. Both architectures were trained on the COCO dataset using the same scripts with similar hyper-parameters. Their details can be found in our", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "[references](https://github.com/pytorch/vision/tree/a78d0d83d0a499fe8480d7a9f493676e746c4699/references/segmentation#deeplabv3_mobilenet_v3_large) folder.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Normally, during inference the images are [resized to 520 pixels](https://github.com/pytorch/vision/blob/a78d0d83d0a499fe8480d7a9f493676e746c4699/references/segmentation/train.py#L30-L33). An optional speed optimization is to construct a Low Res configuration of the model by using the High-Res pre-trained weights and reducing the inference resizing to 320 pixels. This will improve the CPU execution times by roughly 60% while sacrificing a couple of mIoU points. The detailed numbers of this optimization can", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "be found on the table below:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Low-Res Configuration | mIoU Difference | Speed Improvement | mIoU | Global Pixel Acc | Inference on CPU (sec) |\n|--------------------------------------|-----------------:|-------------------:|------:|------------------:|------------------------:|\n| LR-ASPP MobileNetV3-Large| -2.1 | 65.26% | 55.8 | 90.3 | 0.1139 |\n| DeepLabV3 MobileNetV3-Large | -3.8 | 63.86% | 56.5 | 90.3 | 0.2121 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "| FCN MobileNetV3-Large (not released) | -3.0 | 57.57% | 54.8 | 90.1 | 0.1571 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} -{"page_content": "Here are some examples of visualizing the predictions of the LR-ASPP MobileNetV3-Large model:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Model | mIoU | Global Pixel Acc | Inference on CPU (sec) | # Params (M) |\n|--------------------------------------|------:|------------------:|------------------------:|--------------:|\n| LR-ASPP MobileNetV3-Large | 57.9 | 91.2 | 0.3278 | 3.22 |\n| DeepLabV3 MobileNetV3-Large | 60.3 | 91.2 | 0.5869 | 11.03 |\n| FCN MobileNetV3-Large (not released) | 57.8 | 90.9 | 0.3702 | 5.05 |\n| DeepLabV3 ResNet50 | 66.4 | 92.4 | 6.3531 | 39.64 |\n| FCN ResNet50 | 60.5 | 91.4 | 5.0146 | 32.96 |\n\n### Implementation details\n\nIn this section we will discuss important implementation details of tested segmentation heads. Note that all models described in this section use a dilated MobileNetV3-Large backbone.\n\n**LR-ASPP**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "**LR-ASPP**\n\nThe LR-ASPP is the Lite variant of the Reduced Atrous Spatial Pyramid Pooling model proposed by the authors of the MobileNetV3 paper. Unlike the other segmentation models in TorchVision, it does not make use of an [auxiliary loss](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L185-L186). Instead it uses [low and high-level features](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L92-L100) with output strides of 8 and 16 respectively.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "Unlike the paper where a 49x49 AveragePooling layer with variable strides is used, [our implementation](https://github.com/pytorch/vision/blob/e2db2eddbb1699a59fbb5ccbec912979048ef3bf/torchvision/models/segmentation/lraspp.py#L53) uses an `AdaptiveAvgPool2d` layer to process the global features. This is because the authors of the paper tailored the head to the Cityscapes dataset while our focus is to provide a general purpose implementation that can work on multiple datasets. Finally our implementation always has a bilinear interpolation [before returning the output](https://github.com/pytorch/vision/blob/e2db2eddbb1699a59fbb5ccbec912979048ef3bf/torchvision/models/segmentation/lraspp.py#L35) to ensure that the sizes of the input and output images match exactly.\n\n**DeepLabV3 & FCN**", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "**DeepLabV3 & FCN**\n\nThe combination of MobileNetV3 with DeepLabV3 and FCN follows closely the ones of other models and the stage estimation for these methods is identical to LR-ASPP. The only notable difference is that instead of using high and low level features, [we attach](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L37-L45) the normal loss to the feature map with output stride 16 and an auxiliary loss on the feature map with output stride 8.\n\nFinally we should note that the FCN version of the model was not released because it was completely superseded by the LR-ASPP both in terms of speed and accuracy. The [pre-trained weights](https://github.com/pytorch/vision/pull/3276/commits/1641d5f4c7d41f534444fab340c598d61a91bd12#diff-ccff7af514d99eeb40416c8b9ec30f032d1a3f450aaa4057958ca39ab174452eL17) are still available and can be used with minimal changes to the code.\n\n### Training & Tuning process", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "We currently offer two MobileNetV3 pre-trained models capable of doing semantic segmentation: the LR-ASPP and the DeepLabV3. The backbones of the models were [initialized with ImageNet weights](https://github.com/pytorch/vision/blob/b94a4014a68d08f37697f4672729571a46f0042d/torchvision/models/segmentation/segmentation.py#L89-L90) and trained end-to-end. Both architectures were trained on the COCO dataset using the same scripts with similar hyper-parameters. Their details can be found in our [references](https://github.com/pytorch/vision/tree/a78d0d83d0a499fe8480d7a9f493676e746c4699/references/segmentation#deeplabv3_mobilenet_v3_large) folder.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "Normally, during inference the images are [resized to 520 pixels](https://github.com/pytorch/vision/blob/a78d0d83d0a499fe8480d7a9f493676e746c4699/references/segmentation/train.py#L30-L33). An optional speed optimization is to construct a Low Res configuration of the model by using the High-Res pre-trained weights and reducing the inference resizing to 320 pixels. This will improve the CPU execution times by roughly 60% while sacrificing a couple of mIoU points. The detailed numbers of this optimization can be found on the table below:", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} +{"page_content": "| Low-Res Configuration | mIoU Difference | Speed Improvement | mIoU | Global Pixel Acc | Inference on CPU (sec) |\n|--------------------------------------|-----------------:|-------------------:|------:|------------------:|------------------------:|\n| LR-ASPP MobileNetV3-Large| -2.1 | 65.26% | 55.8 | 90.3 | 0.1139 |\n| DeepLabV3 MobileNetV3-Large | -3.8 | 63.86% | 56.5 | 90.3 | 0.2121 |\n| FCN MobileNetV3-Large (not released) | -3.0 | 57.57% | 54.8 | 90.1 | 0.1571 |\n\nHere are some examples of visualizing the predictions of the LR-ASPP MobileNetV3-Large model:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} {"page_content": "We hope that you found this article interesting. We are looking forward to your feedback to see if this is the type of content you would like us to publish more often. If the community finds that such posts are useful, we will be happy to publish more articles that cover the implementation details of newly introduced Machine Learning models.", "metadata": {"source": "https://pytorch.org/blog/torchvision-mobilenet-v3-implementation/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch strengthens its governance by joining the Linux Foundation\"\nauthor: Soumith Chintala\nfeatured-img: \"/assets/images/pytorch-foundation-blog-image.jpg\"\n---", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "Today, I am proud to announce that PyTorch is moving to the [Linux Foundation (LF)](https://www.linuxfoundation.org/) as a top-level project under the name PyTorch Foundation. The [core mission](https://www.linuxfoundation.org/about/) of the Linux Foundation is the collaborative development of open source software. With a governing board of leaders from AMD, Amazon Web Services (AWS), Google Cloud, Meta, Microsoft Azure and NVIDIA, this model aligns with where PyTorch stands today and what it needs to", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "travel forward. The creation of the PyTorch Foundation will ensure business decisions are being made in a transparent and open manner by a diverse group of members for years to come. The technical decisions remain in control of individual maintainers. I\u2019m excited that the Linux Foundation will be our new home as they have notable experience supporting large open-source projects like ours such as Kubernetes and NodeJS. At this pivotal moment, I want to take a look back at how we started, share why we are", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "moving, and what\u2019s ahead.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "This January, PyTorch celebrated its 5 year anniversary! I reflected on what it meant to me in this [tweet thread](https://soumith.ch/posts/2022/01/pytorch-retro/), and [this](https://www.youtube.com/watch?v=r7qB7mKJOFk) conversation with my colleagues Mike Schroepfer, Lin Qiao, and Yann LeCun. When we started PyTorch development in 2016, it was a collective effort by a band of people from the [Lua]Torch community with a big chunk of people and funding from Meta and individuals contributing from NVIDIA,", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "Twitter and other entities.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "Since 2017, PyTorch has grown far beyond our initial vision. With over [2,400 contributors](https://github.com/pytorch/pytorch/graphs/contributors) who have built nearly 154,000 projects using PyTorch as a foundation, PyTorch has become one of the primary platforms for AI research, as well as commercial production use. We\u2019ve seen its impact across industry and academia, from large companies to numerous university courses at Stanford, NYU, EPFL, Oxford, and other academic institutions. As a maintainer of", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "PyTorch, the journey has been extremely fulfilling, with the impact of the project seen in various fields from self-driving cars to healthcare to aerospace.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "As PyTorch grew, many companies have made foundational investments around it. While Meta remains the largest contributor to PyTorch, companies such as AMD, Amazon Web Services (AWS), Google Cloud, HuggingFace, Lightning AI, Microsoft Azure, Nvidia, and many others have made significant investments, including both technical contributions and community building efforts. They\u2019ve established teams around PyTorch or filled significant voids within the PyTorch community and sent countless contributions to the", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "PyTorch core and to the ecosystem around it \u2014 PyTorch is an important part of their future. With PyTorch continuing to grow as a multi-stakeholder project, it\u2019s time to move to a broader open-source foundation.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "The business governance of PyTorch was fairly unstructured for quite some time since launch \u2013 we operated like a scrappy startup. Team members at Meta spent the time and energy to structure this properly and organize PyTorch into an organizationally more healthy entity. Meta helped PyTorch with introducing many structures, such as [Contributor License Agreements](https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/), [Branding", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "Guidelines](https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf), and Trademark registration. Keeping PyTorch\u2019s organizational health up to check is essential and beneficial for the community. The next stage of our organizational progress is to support the interests of multiple stakeholders, hence moving to a foundation is good. We chose the Linux Foundation as it has vast organization experience hosting large multi-stakeholder open-source projects with the right balance of", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "organizational structure and finding specific solutions for these projects.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "Simultaneously, the technical governance of PyTorch has been a loosely structured community model of open-source development \u2014 A set of people maintaining PyTorch by area with their responsibility often tied to their individual identity rather than their employment. While we kept a codified list at the [PyTorch - Maintainers](https://pytorch.org/docs/stable/community/persons_of_interest.html) page, the technical governance was not formalized nor codified. As PyTorch scales as a community, the next step is", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "to structure and codify. The [PyTorch Technical Governance](https://pytorch.org/docs/master/community/governance.html) now supports a hierarchical maintainer structure and clear outlining of processes around day to day work and escalations. This doesn\u2019t change how we run things, but it does add discipline and openness that at our scale feels essential and timely.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "It\u2019s been an exciting journey since 2016. I am grateful for the experiences and people I\u2019ve met along the way. PyTorch started with a small group of contributors which have grown and diversified over the years, all bringing in new ideas and innovations that would not have been possible without our community. We want to continue the open-source spirit \u2013 for the community and by the community. Thank you to our contributors, maintainers, users, supporters and new foundation members. We look forward to the next", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "chapter of PyTorch with the PyTorch Foundation.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Fast Beam Search Decoding in PyTorch with TorchAudio and Flashlight Text\"\nauthor: Caroline Chen, Jacob Kahn (@jacob_d_kahn)\nfeatured-img: \"/assets/images/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text-6.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Beam search decoding with industry-leading speed from [Flashlight Text](https://github.com/flashlight/text) (part of the [Flashlight](https://arxiv.org/abs/2201.12465) ML framework) is now available with official support in [TorchAudio](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder), bringing high-performance beam search and text utilities for speech and text applications built on top of PyTorch. The current integration supports CTC-style decoding, but it can be used for *any modeling", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "setting that outputs token-level probability distributions over time steps*.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "A brief beam search refresher\n\nIn speech and language settings, *beam search* is an efficient, greedy algorithm that can convert sequences of *continuous values* (i.e. probabilities or scores) into *graphs* or *sequences* (i.e. tokens, word-pieces, words) using *optional constraints* on valid sequences (i.e. a lexicon), *optional external scoring* (i.e. an LM which scores valid sequences), and other *score adjustments* for particular sequences.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "In the example that follows, we'll consider \u2014 a token set of {\u03f5, a, b}, where \u03f5 is a special token that we can imagine denotes a space between words or a pause in speech. Graphics here and below are taken from Awni Hannun's excellent [distill.pub writeup](https://distill.pub/2017/ctc/) on CTC and beam search.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "Today, I am proud to announce that PyTorch is moving to the [Linux Foundation (LF)](https://www.linuxfoundation.org/) as a top-level project under the name PyTorch Foundation. The [core mission](https://www.linuxfoundation.org/about/) of the Linux Foundation is the collaborative development of open source software. With a governing board of leaders from AMD, Amazon Web Services (AWS), Google Cloud, Meta, Microsoft Azure and NVIDIA, this model aligns with where PyTorch stands today and what it needs to travel forward. The creation of the PyTorch Foundation will ensure business decisions are being made in a transparent and open manner by a diverse group of members for years to come. The technical decisions remain in control of individual maintainers. I\u2019m excited that the Linux Foundation will be our new home as they have notable experience supporting large open-source projects like ours such as Kubernetes and NodeJS. At this pivotal moment, I want to take a look back at how we started, share why we are moving, and what\u2019s ahead.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "This January, PyTorch celebrated its 5 year anniversary! I reflected on what it meant to me in this [tweet thread](https://soumith.ch/posts/2022/01/pytorch-retro/), and [this](https://www.youtube.com/watch?v=r7qB7mKJOFk) conversation with my colleagues Mike Schroepfer, Lin Qiao, and Yann LeCun. When we started PyTorch development in 2016, it was a collective effort by a band of people from the [Lua]Torch community with a big chunk of people and funding from Meta and individuals contributing from NVIDIA, Twitter and other entities.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "Since 2017, PyTorch has grown far beyond our initial vision. With over [2,400 contributors](https://github.com/pytorch/pytorch/graphs/contributors) who have built nearly 154,000 projects using PyTorch as a foundation, PyTorch has become one of the primary platforms for AI research, as well as commercial production use. We\u2019ve seen its impact across industry and academia, from large companies to numerous university courses at Stanford, NYU, EPFL, Oxford, and other academic institutions. As a maintainer of PyTorch, the journey has been extremely fulfilling, with the impact of the project seen in various fields from self-driving cars to healthcare to aerospace.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "As PyTorch grew, many companies have made foundational investments around it. While Meta remains the largest contributor to PyTorch, companies such as AMD, Amazon Web Services (AWS), Google Cloud, HuggingFace, Lightning AI, Microsoft Azure, Nvidia, and many others have made significant investments, including both technical contributions and community building efforts. They\u2019ve established teams around PyTorch or filled significant voids within the PyTorch community and sent countless contributions to the PyTorch core and to the ecosystem around it \u2014 PyTorch is an important part of their future. With PyTorch continuing to grow as a multi-stakeholder project, it\u2019s time to move to a broader open-source foundation.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "The business governance of PyTorch was fairly unstructured for quite some time since launch \u2013 we operated like a scrappy startup. Team members at Meta spent the time and energy to structure this properly and organize PyTorch into an organizationally more healthy entity. Meta helped PyTorch with introducing many structures, such as [Contributor License Agreements](https://pytorch.org/blog/a-contributor-license-agreement-for-pytorch/), [Branding Guidelines](https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf), and Trademark registration. Keeping PyTorch\u2019s organizational health up to check is essential and beneficial for the community. The next stage of our organizational progress is to support the interests of multiple stakeholders, hence moving to a foundation is good. We chose the Linux Foundation as it has vast organization experience hosting large multi-stakeholder open-source projects with the right balance of organizational structure and finding specific solutions for these projects.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "Simultaneously, the technical governance of PyTorch has been a loosely structured community model of open-source development \u2014 A set of people maintaining PyTorch by area with their responsibility often tied to their individual identity rather than their employment. While we kept a codified list at the [PyTorch - Maintainers](https://pytorch.org/docs/stable/community/persons_of_interest.html) page, the technical governance was not formalized nor codified. As PyTorch scales as a community, the next step is to structure and codify. The [PyTorch Technical Governance](https://pytorch.org/docs/master/community/governance.html) now supports a hierarchical maintainer structure and clear outlining of processes around day to day work and escalations. This doesn\u2019t change how we run things, but it does add discipline and openness that at our scale feels essential and timely.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "It\u2019s been an exciting journey since 2016. I am grateful for the experiences and people I\u2019ve met along the way. PyTorch started with a small group of contributors which have grown and diversified over the years, all bringing in new ideas and innovations that would not have been possible without our community. We want to continue the open-source spirit \u2013 for the community and by the community. Thank you to our contributors, maintainers, users, supporters and new foundation members. We look forward to the next chapter of PyTorch with the PyTorch Foundation.", "metadata": {"source": "https://pytorch.org/blog/PyTorchfoundation/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Fast Beam Search Decoding in PyTorch with TorchAudio and Flashlight Text\"\nauthor: Caroline Chen, Jacob Kahn (@jacob_d_kahn)\nfeatured-img: \"/assets/images/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text-6.png\"\n---\n\nBeam search decoding with industry-leading speed from [Flashlight Text](https://github.com/flashlight/text) (part of the [Flashlight](https://arxiv.org/abs/2201.12465) ML framework) is now available with official support in [TorchAudio](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder), bringing high-performance beam search and text utilities for speech and text applications built on top of PyTorch. The current integration supports CTC-style decoding, but it can be used for *any modeling setting that outputs token-level probability distributions over time steps*.\n\n## A brief beam search refresher", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "In speech and language settings, *beam search* is an efficient, greedy algorithm that can convert sequences of *continuous values* (i.e. probabilities or scores) into *graphs* or *sequences* (i.e. tokens, word-pieces, words) using *optional constraints* on valid sequences (i.e. a lexicon), *optional external scoring* (i.e. an LM which scores valid sequences), and other *score adjustments* for particular sequences.\n\nIn the example that follows, we'll consider \u2014 a token set of {\u03f5, a, b}, where \u03f5 is a special token that we can imagine denotes a space between words or a pause in speech. Graphics here and below are taken from Awni Hannun's excellent [distill.pub writeup](https://distill.pub/2017/ctc/) on CTC and beam search.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} {"page_content": "With a greedy-like approach, beam search considers the next viable token given an existing sequence of tokens \u2014 in the example above, a, b, b is a valid sequence, but a, b, a is not. We *rank* each possible next token at each step of the beam search according to a scoring function. Scoring functions (s) typically looks something like:\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Where **\u0177** is a potential path/sequence of tokens, **x** is the input *(P(\u0177|x)* represents the model's predictions over time), and \ud835\udefc is a weight on the language model probability *(P(y)* the probability of the sequence under the language model). Some scoring functions add *\ud835\udf37* which adjusts a score based on the length of the predicted sequence **|\u0177|**. This particular scoring function is used in [FAIR's prior", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "work](https://arxiv.org/pdf/1911.08460.pdf) on end-to-end ASR, and there are many variations on scoring functions which can vary across application areas.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Given a particular sequence, to assess the next viable token in that sequence (perhaps constrained by a set of allowed words or sequences, such as a lexicon of words), the beam search algorithm scores the sequence with each candidate token added, and sorts token candidates based on those scores. For efficiency and since the number of paths is exponential in the token set size, the *top-k* highest-scoring candidates are kept \u2014 *k* represents the *beam size*.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

There are many other nuances with how beam search can progress: similar hypothesis sequences can be \u201cmerged\u201d, for instance.\n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "The scoring function can be further augmented to up/down-weight token insertion or long or short words. Scoring with *stronger external language* models, while incurring computational cost, can also significantly improve performance; this is frequently referred to as *LM fusion*. There are many other knobs to tune for decoding \u2014 these are documented in [TorchAudio\u2019s documentation](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder) and explored further in [TorchAudio\u2019s ASR Inference", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "tutorial](https://pytorch.org/audio/0.12.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html#beam-search-decoder-parameters). Since decoding is quite efficient, parameters can be easily swept and tuned.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Beam search has been used in ASR extensively over the years in far too many works to cite, and in strong, recent results and systems including [wav2vec 2.0](https://proceedings.neurips.cc/paper/2020/file/92d1e1eb1cd6f9fba3227870bb6d7f07-Paper.pdf) and [NVIDIA's NeMo](https://developer.nvidia.com/nvidia-nemo).", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Why beam search?\n\nBeam search remains a fast competitor to heavier-weight decoding approaches such as [RNN-Transducer](https://arxiv.org/pdf/1211.3711.pdf) that Google has invested in putting [on-device](https://ai.googleblog.com/2019/03/an-all-neural-on-device-speech.html) and has shown strong results with on [common benchmarks](https://arxiv.org/pdf/2010.10504.pdf). Autoregressive text models at scale can benefit from beam search as well. Among other things, beam search gives:", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "- A flexible performance/latency tradeoff \u2014 by adjusting beam size and the external LM, users can sacrifice latency for accuracy or pay for more accurate results with a small latency cost. Decoding with no external LM can improve results at very little performance cost.\n- Portability without retraining \u2014 existing neural models can benefit from multiple decoding setups and plug-and-play with external LMs without training or fine-tuning.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "- A compelling complexity/accuracy tradeoff \u2014 adding beam search to an existing modeling pipeline incurs little additional complexity and can improve performance.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Performance Benchmarks", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Today's most commonly-used beam search decoding libraries today that support external language model integration include Kensho's [pyctcdecode](https://github.com/kensho-technologies/pyctcdecode), NVIDIA's [NeMo toolkit](https://github.com/NVIDIA/NeMo/tree/stable/scripts/asr_language_modeling). We benchmark the TorchAudio + Flashlight decoder against them with a *wav2vec 2.0* base model trained on 100 hours of audio evaluated on [LibriSpeech](https://www.openslr.org/12) dev-other with the official", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "[KenLM](https://github.com/kpu/kenlm/) 3-gram LM. Benchmarks were run on Intel E5-2698 CPUs on a single thread. All computation was in-memory \u2014 KenLM memory mapping was disabled as it wasn't widely supported.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "When benchmarking, we measure the *time-to-WER (word error rate)* \u2014 because of subtle differences in the implementation of decoding algorithms and the complex relationships between parameters and decoding speed, some hyperparameters differed across runs. To fairly assess performance, we first sweep for parameters that achieve a baseline WER, minimizing beam size if possible.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\n

\nDecoding performance on Librispeech dev-other of a pretrained wav2vec 2.0 model. TorchAudio + Flashlight decoding outperforms by an order of magnitude at low WERs.\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "

\nTime-to-WER results, deferring to smaller beam size, across decoders. The TorchAudio + Flashlight decoder scales far better with larger beam sizes and at lower WERs.\n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio API and Usage\n\nTorchAudio provides a Python API for CTC beam search decoding, with support for the following:\n\n- lexicon and lexicon-free decoding\n- KenLM n-gram language model integration\n- character and word-piece decoding\n- sample pretrained LibriSpeech KenLM models and corresponding lexicon and token files\n- various customizable beam search parameters (beam size, pruning threshold, LM weight...)\n\nTo set up the decoder, use the factory function torchaudio.models.decoder.ctc_decoder", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchaudio.models.decoder import ctc_decoder, download_pretrained_files\nfiles = download_pretrained_files(\"librispeech-4-gram\")\ndecoder = ctc_decoder(\n lexicon=files.lexicon,\n tokens=files.tokens,\n lm=files.lm,\n nbest=1,\n ... additional optional customizable args ...\n)", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Given emissions of shape *(batch, time, num_tokens)*, the decoder will compute and return a List of batch Lists, each consisting of the nbest hypotheses corresponding to the emissions. Each hypothesis can be further broken down into tokens, words (if a lexicon is provided), score, and timesteps components.\n\n```python\nemissions = acoustic_model(waveforms) # (B, T, N)\nbatch_hypotheses = decoder(emissions) # List[List[CTCHypothesis]]", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "# transcript for a lexicon decoder\ntranscripts = [\" \".join(hypo[0].words) for hypo in batch_hypotheses]\n\n# transcript for a lexicon free decoder, splitting by sil token\nbatch_tokens = [decoder.idxs_to_tokens(hypo[0].tokens) for hypo in batch_hypotheses]\ntranscripts = [\"\".join(tokens) for tokens in batch_tokens]", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/stable/models.decoder.html#ctcdecoder) for more API details, and the tutorial ([ASR Inference Decoding](https://pytorch.org/audio/main/tutorials/asr_inference_with_ctc_decoder_tutorial.html)) or sample [inference script](https://github.com/pytorch/audio/tree/main/examples/asr/librispeech_ctc_decoder) for more usage examples.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Upcoming Improvements\n\n**Full NNLM support** \u2014 decoding with large neural language models (e.g. transformers) remains somewhat unexplored at scale. Already supported in Flashlight, we plan to add support in TorchAudio, allowing users to use custom decoder-compatible LMs. Custom word level language models are already available in the nightly TorchAudio build, and is slated to be released in TorchAudio 0.13.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "**Autoregressive/seq2seq decoding** \u2014 Flashlight Text also supports [sequence-to-sequence (seq2seq) decoding](https://github.com/flashlight/text/blob/main/flashlight/lib/text/decoder/LexiconSeq2SeqDecoder.h) for autoregressive models, which we hope to add bindings for and add to TorchAudio and TorchText with efficient GPU implementations as well.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "**Better build support** \u2014 to benefit from improvements in Flashlight Text, TorchAudio will directly submodule Flashlight Text to make upstreaming modifications and improvements easier. This is already in effect in the nightly TorchAudio build, and is slated to be released in TorchAudio 0.13.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "Citation\n\nTo cite the decoder, please use the following:", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "```python\n@inproceedings{kahn2022flashlight,\n title={Flashlight: Enabling innovation in tools for machine learning},\n author={Kahn, Jacob D and Pratap, Vineel and Likhomanenko, Tatiana and Xu, Qiantong and Hannun, Awni and Cai, Jeff and Tomasello, Paden and Lee, Ann and Grave, Edouard and Avidov, Gilad and others},\n booktitle={International Conference on Machine Learning},\n pages={10557--10574},\n year={2022},\n organization={PMLR}\n}\n```\n```python\n@inproceedings{yang2022torchaudio,", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "title={Torchaudio: Building blocks for audio and speech processing},\n author={Yang, Yao-Yuan and Hira, Moto and Ni, Zhaoheng and Astafurov, Artyom and Chen, Caroline and Puhrsch, Christian and Pollack, David and Genzel, Dmitriy and Greenberg, Donny and Yang, Edward Z and others},\n booktitle={ICASSP 2022-2022 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},\n pages={6982--6986},\n year={2022},\n organization={IEEE}\n}\n```", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'New PyTorch Library Releases in PyTorch 1.9, including TorchVision, TorchAudio, and more'\nauthor: Team PyTorch \n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Today, we are announcing updates to a number of PyTorch libraries, alongside the [PyTorch 1.9 release](https://pytorch.org/blog/pytorch-1.9-released/). The updates include new releases for the domain libraries including TorchVision, TorchText and TorchAudio. These releases, along with the PyTorch 1.9 release, include a number of new features and improvements that will provide a broad set of updates for the PyTorch community.\n\nSome highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchVision** - Added new SSD and SSDLite models, quantized kernels for object detection, GPU Jpeg decoding, and iOS support. See [release notes](https://github.com/pytorch/vision/releases) here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchAudio** - Added wav2vec 2.0 model deployable in non-Python environments (including C++, Android, and iOS). Many performance improvements in lfilter, spectral operations, resampling. Added options for quality control in sampling (i.e. Kaiser window support). Initiated the migration of complex tensors operations. Improved autograd support. See [release notes](https://github.com/pytorch/audio/releases) here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "* **TorchText** - Added a new high-performance Vocab module that provides common functional APIs for NLP workflows. See [release notes](https://github.com/pytorch/text/releases) here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We\u2019d like to thank the community for their support and work on this latest release.\n\nFeatures in PyTorch releases are classified as Stable, Beta, and Prototype. You can learn more about the definitions in [this blog post](https://pytorch.org/blog/pytorch-feature-classification-changes/). \n\n# TorchVision 0.10", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Quantized kernels for object detection \nThe forward pass of the nms and roi_align operators now support tensors with a quantized dtype, which can help lower the memory footprint of object detection models, particularly on mobile environments. For more details, refer to [the documentation](https://pytorch.org/vision/stable/ops.html#torchvision.ops.roi_align).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Speed optimizations for Tensor transforms \nThe resize and flip transforms have been optimized and its runtime improved by up to 5x on the CPU.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Documentation improvements \nSignificant improvements were made to the documentation. In particular, a new gallery of examples is available. These examples visually illustrate how each transform acts on an image, and also properly documents and illustrates the output of the segmentation models.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The example gallery will be extended in the future to provide more comprehensive examples and serve as a reference for common torchvision tasks. For more details, refer to [the documentation](https://pytorch.org/vision/stable/auto_examples/index.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) New models for detection \n[SSD](https://arxiv.org/abs/1512.02325) and [SSDlite](https://arxiv.org/abs/1801.04381) are two popular object detection architectures that are efficient in terms of speed and provide good results for low resolution pictures. In this release, we provide implementations for the original SSD model with VGG16 backbone and for its mobile-friendly variant SSDlite with MobileNetV3-Large backbone.\n\nThe models were pre-trained on COCO train2017 and can be used as follows:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\nimport torch\nimport torchvision\n\n# Original SSD variant\nx = [torch.rand(3, 300, 300), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.ssd300_vgg16(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)\n\n# Mobile-friendly SSDlite variant\nx = [torch.rand(3, 320, 320), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.ssdlite320_mobilenet_v3_large(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The following accuracies can be obtained on COCO val2017 (full results available in [#3403](https://github.com/pytorch/vision/pull/3403) and [#3757](https://github.com/pytorch/vision/pull/3757)):\n\n\n {:.table.table-striped.table-bordered}\n| Model | mAP | mAP@50 | mAP@75 |\n| ------------- | ------------- | ------------- | ------------- |\n| SSD300 VGG16 | 25.1 | 41.5 | 26.2 | \n| SSDlite320 MobileNetV3-Large | 21.3 | 34.3 | 22.1 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For more details, refer to [the documentation](https://pytorch.org/vision/stable/models.html#id37).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) JPEG decoding on the GPU \nDecoding jpegs is now possible on GPUs with the use of [nvjpeg](https://developer.nvidia.com/nvjpeg), which should be readily available in your CUDA setup. The decoding time of a single image should be about 2 to 3 times faster than with libjpeg on CPU. While the resulting tensor will be stored on the GPU device, the input raw tensor still needs to reside on the host (CPU), because the first stages of the decoding process take place on the host:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "from torchvision.io.image import read_file, decode_jpeg", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\ndata = read_file('path_to_image.jpg') # raw data is on CPU\nimg = decode_jpeg(data, device='cuda') # decoded image in on GPU\n```\nFor more details, see [the documentation](https://pytorch.org/vision/stable/io.html#torchvision.io.decode_jpeg).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) iOS support \nTorchVision 0.10 now provides pre-compiled iOS binaries for its C++ operators, which means you can run Faster R-CNN and Mask R-CNN on iOS. An example app on how to build a program leveraging those ops can be found [here](https://github.com/pytorch/vision/tree/master/ios/VisionTestApp). \n\n# TorchAudio 0.9.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Complex Tensor Migration \nTorchAudio has functions that handle complex-valued tensors. These functions follow a convention to use an extra dimension to represent real and imaginary parts. In PyTorch 1.6, the native complex type was introduced. As its API is getting stable, torchaudio has started to migrate to the native complex type.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Where **\u0177** is a potential path/sequence of tokens, **x** is the input *(P(\u0177|x)* represents the model's predictions over time), and \ud835\udefc is a weight on the language model probability *(P(y)* the probability of the sequence under the language model). Some scoring functions add *\ud835\udf37* which adjusts a score based on the length of the predicted sequence **|\u0177|**. This particular scoring function is used in [FAIR's prior work](https://arxiv.org/pdf/1911.08460.pdf) on end-to-end ASR, and there are many variations on scoring functions which can vary across application areas.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "Given a particular sequence, to assess the next viable token in that sequence (perhaps constrained by a set of allowed words or sequences, such as a lexicon of words), the beam search algorithm scores the sequence with each candidate token added, and sorts token candidates based on those scores. For efficiency and since the number of paths is exponential in the token set size, the *top-k* highest-scoring candidates are kept \u2014 *k* represents the *beam size*.\n\n

\n \n

\n\n

There are many other nuances with how beam search can progress: similar hypothesis sequences can be \u201cmerged\u201d, for instance.\n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "The scoring function can be further augmented to up/down-weight token insertion or long or short words. Scoring with *stronger external language* models, while incurring computational cost, can also significantly improve performance; this is frequently referred to as *LM fusion*. There are many other knobs to tune for decoding \u2014 these are documented in [TorchAudio\u2019s documentation](https://pytorch.org/audio/0.12.0/models.decoder.html#ctcdecoder) and explored further in [TorchAudio\u2019s ASR Inference tutorial](https://pytorch.org/audio/0.12.0/tutorials/asr_inference_with_ctc_decoder_tutorial.html#beam-search-decoder-parameters). Since decoding is quite efficient, parameters can be easily swept and tuned.\n\nBeam search has been used in ASR extensively over the years in far too many works to cite, and in strong, recent results and systems including [wav2vec 2.0](https://proceedings.neurips.cc/paper/2020/file/92d1e1eb1cd6f9fba3227870bb6d7f07-Paper.pdf) and [NVIDIA's NeMo](https://developer.nvidia.com/nvidia-nemo).", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "## Why beam search?\n\nBeam search remains a fast competitor to heavier-weight decoding approaches such as [RNN-Transducer](https://arxiv.org/pdf/1211.3711.pdf) that Google has invested in putting [on-device](https://ai.googleblog.com/2019/03/an-all-neural-on-device-speech.html) and has shown strong results with on [common benchmarks](https://arxiv.org/pdf/2010.10504.pdf). Autoregressive text models at scale can benefit from beam search as well. Among other things, beam search gives:", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "- A flexible performance/latency tradeoff \u2014 by adjusting beam size and the external LM, users can sacrifice latency for accuracy or pay for more accurate results with a small latency cost. Decoding with no external LM can improve results at very little performance cost.\n- Portability without retraining \u2014 existing neural models can benefit from multiple decoding setups and plug-and-play with external LMs without training or fine-tuning.\n- A compelling complexity/accuracy tradeoff \u2014 adding beam search to an existing modeling pipeline incurs little additional complexity and can improve performance.\n\n## Performance Benchmarks", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "Today's most commonly-used beam search decoding libraries today that support external language model integration include Kensho's [pyctcdecode](https://github.com/kensho-technologies/pyctcdecode), NVIDIA's [NeMo toolkit](https://github.com/NVIDIA/NeMo/tree/stable/scripts/asr_language_modeling). We benchmark the TorchAudio + Flashlight decoder against them with a *wav2vec 2.0* base model trained on 100 hours of audio evaluated on [LibriSpeech](https://www.openslr.org/12) dev-other with the official [KenLM](https://github.com/kpu/kenlm/) 3-gram LM. Benchmarks were run on Intel E5-2698 CPUs on a single thread. All computation was in-memory \u2014 KenLM memory mapping was disabled as it wasn't widely supported.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "When benchmarking, we measure the *time-to-WER (word error rate)* \u2014 because of subtle differences in the implementation of decoding algorithms and the complex relationships between parameters and decoding speed, some hyperparameters differed across runs. To fairly assess performance, we first sweep for parameters that achieve a baseline WER, minimizing beam size if possible.\n\n

\n \n

\n\n

\nDecoding performance on Librispeech dev-other of a pretrained wav2vec 2.0 model. TorchAudio + Flashlight decoding outperforms by an order of magnitude at low WERs.\n

\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "

\nTime-to-WER results, deferring to smaller beam size, across decoders. The TorchAudio + Flashlight decoder scales far better with larger beam sizes and at lower WERs.\n

\n\n## TorchAudio API and Usage\n\nTorchAudio provides a Python API for CTC beam search decoding, with support for the following:\n\n- lexicon and lexicon-free decoding\n- KenLM n-gram language model integration\n- character and word-piece decoding\n- sample pretrained LibriSpeech KenLM models and corresponding lexicon and token files\n- various customizable beam search parameters (beam size, pruning threshold, LM weight...)\n\nTo set up the decoder, use the factory function torchaudio.models.decoder.ctc_decoder\n\n```python\nfrom torchaudio.models.decoder import ctc_decoder, download_pretrained_files\nfiles = download_pretrained_files(\"librispeech-4-gram\")\ndecoder = ctc_decoder(\n lexicon=files.lexicon,\n tokens=files.tokens,\n lm=files.lm,\n nbest=1,\n ... additional optional customizable args ...\n)\n```", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "Given emissions of shape *(batch, time, num_tokens)*, the decoder will compute and return a List of batch Lists, each consisting of the nbest hypotheses corresponding to the emissions. Each hypothesis can be further broken down into tokens, words (if a lexicon is provided), score, and timesteps components.\n\n```python\nemissions = acoustic_model(waveforms) # (B, T, N)\nbatch_hypotheses = decoder(emissions) # List[List[CTCHypothesis]]\n\n# transcript for a lexicon decoder\ntranscripts = [\" \".join(hypo[0].words) for hypo in batch_hypotheses]\n\n# transcript for a lexicon free decoder, splitting by sil token\nbatch_tokens = [decoder.idxs_to_tokens(hypo[0].tokens) for hypo in batch_hypotheses]\ntranscripts = [\"\".join(tokens) for tokens in batch_tokens]\n```", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "Please refer to the [documentation](https://pytorch.org/audio/stable/models.decoder.html#ctcdecoder) for more API details, and the tutorial ([ASR Inference Decoding](https://pytorch.org/audio/main/tutorials/asr_inference_with_ctc_decoder_tutorial.html)) or sample [inference script](https://github.com/pytorch/audio/tree/main/examples/asr/librispeech_ctc_decoder) for more usage examples.\n\n## Upcoming Improvements\n\n**Full NNLM support** \u2014 decoding with large neural language models (e.g. transformers) remains somewhat unexplored at scale. Already supported in Flashlight, we plan to add support in TorchAudio, allowing users to use custom decoder-compatible LMs. Custom word level language models are already available in the nightly TorchAudio build, and is slated to be released in TorchAudio 0.13.", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "**Autoregressive/seq2seq decoding** \u2014 Flashlight Text also supports [sequence-to-sequence (seq2seq) decoding](https://github.com/flashlight/text/blob/main/flashlight/lib/text/decoder/LexiconSeq2SeqDecoder.h) for autoregressive models, which we hope to add bindings for and add to TorchAudio and TorchText with efficient GPU implementations as well.\n\n**Better build support** \u2014 to benefit from improvements in Flashlight Text, TorchAudio will directly submodule Flashlight Text to make upstreaming modifications and improvements easier. This is already in effect in the nightly TorchAudio build, and is slated to be released in TorchAudio 0.13.\n\n## Citation\n\nTo cite the decoder, please use the following:", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "```python\n@inproceedings{kahn2022flashlight,\n title={Flashlight: Enabling innovation in tools for machine learning},\n author={Kahn, Jacob D and Pratap, Vineel and Likhomanenko, Tatiana and Xu, Qiantong and Hannun, Awni and Cai, Jeff and Tomasello, Paden and Lee, Ann and Grave, Edouard and Avidov, Gilad and others},\n booktitle={International Conference on Machine Learning},\n pages={10557--10574},\n year={2022},\n organization={PMLR}\n}\n```\n```python\n@inproceedings{yang2022torchaudio,\n title={Torchaudio: Building blocks for audio and speech processing},\n author={Yang, Yao-Yuan and Hira, Moto and Ni, Zhaoheng and Astafurov, Artyom and Chen, Caroline and Puhrsch, Christian and Pollack, David and Genzel, Dmitriy and Greenberg, Donny and Yang, Edward Z and others},\n booktitle={ICASSP 2022-2022 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},\n pages={6982--6986},\n year={2022},\n organization={IEEE}\n}\n```", "metadata": {"source": "https://pytorch.org/blog/fast-beam-search-decoding-in-pytorch-with-torchaudio-and-flashlight-text/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'New PyTorch Library Releases in PyTorch 1.9, including TorchVision, TorchAudio, and more'\nauthor: Team PyTorch \n---\n\nToday, we are announcing updates to a number of PyTorch libraries, alongside the [PyTorch 1.9 release](https://pytorch.org/blog/pytorch-1.9-released/). The updates include new releases for the domain libraries including TorchVision, TorchText and TorchAudio. These releases, along with the PyTorch 1.9 release, include a number of new features and improvements that will provide a broad set of updates for the PyTorch community.\n\nSome highlights include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "* **TorchVision** - Added new SSD and SSDLite models, quantized kernels for object detection, GPU Jpeg decoding, and iOS support. See [release notes](https://github.com/pytorch/vision/releases) here.\n* **TorchAudio** - Added wav2vec 2.0 model deployable in non-Python environments (including C++, Android, and iOS). Many performance improvements in lfilter, spectral operations, resampling. Added options for quality control in sampling (i.e. Kaiser window support). Initiated the migration of complex tensors operations. Improved autograd support. See [release notes](https://github.com/pytorch/audio/releases) here.\n* **TorchText** - Added a new high-performance Vocab module that provides common functional APIs for NLP workflows. See [release notes](https://github.com/pytorch/text/releases) here.\n\nWe\u2019d like to thank the community for their support and work on this latest release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "Features in PyTorch releases are classified as Stable, Beta, and Prototype. You can learn more about the definitions in [this blog post](https://pytorch.org/blog/pytorch-feature-classification-changes/). \n\n# TorchVision 0.10\n\n### (Stable) Quantized kernels for object detection \nThe forward pass of the nms and roi_align operators now support tensors with a quantized dtype, which can help lower the memory footprint of object detection models, particularly on mobile environments. For more details, refer to [the documentation](https://pytorch.org/vision/stable/ops.html#torchvision.ops.roi_align). \n\n### (Stable) Speed optimizations for Tensor transforms \nThe resize and flip transforms have been optimized and its runtime improved by up to 5x on the CPU.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Stable) Documentation improvements \nSignificant improvements were made to the documentation. In particular, a new gallery of examples is available. These examples visually illustrate how each transform acts on an image, and also properly documents and illustrates the output of the segmentation models.\n\nThe example gallery will be extended in the future to provide more comprehensive examples and serve as a reference for common torchvision tasks. For more details, refer to [the documentation](https://pytorch.org/vision/stable/auto_examples/index.html).\n\n### (Beta) New models for detection \n[SSD](https://arxiv.org/abs/1512.02325) and [SSDlite](https://arxiv.org/abs/1801.04381) are two popular object detection architectures that are efficient in terms of speed and provide good results for low resolution pictures. In this release, we provide implementations for the original SSD model with VGG16 backbone and for its mobile-friendly variant SSDlite with MobileNetV3-Large backbone.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "The models were pre-trained on COCO train2017 and can be used as follows:\n\n```python\nimport torch\nimport torchvision\n\n# Original SSD variant\nx = [torch.rand(3, 300, 300), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.ssd300_vgg16(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)\n\n# Mobile-friendly SSDlite variant\nx = [torch.rand(3, 320, 320), torch.rand(3, 500, 400)]\nm_detector = torchvision.models.detection.ssdlite320_mobilenet_v3_large(pretrained=True)\nm_detector.eval()\npredictions = m_detector(x)\n```\n\nThe following accuracies can be obtained on COCO val2017 (full results available in [#3403](https://github.com/pytorch/vision/pull/3403) and [#3757](https://github.com/pytorch/vision/pull/3757)):\n\n\n {:.table.table-striped.table-bordered}\n| Model | mAP | mAP@50 | mAP@75 |\n| ------------- | ------------- | ------------- | ------------- |\n| SSD300 VGG16 | 25.1 | 41.5 | 26.2 | \n| SSDlite320 MobileNetV3-Large | 21.3 | 34.3 | 22.1 |", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "For more details, refer to [the documentation](https://pytorch.org/vision/stable/models.html#id37).\n\n### (Beta) JPEG decoding on the GPU \nDecoding jpegs is now possible on GPUs with the use of [nvjpeg](https://developer.nvidia.com/nvjpeg), which should be readily available in your CUDA setup. The decoding time of a single image should be about 2 to 3 times faster than with libjpeg on CPU. While the resulting tensor will be stored on the GPU device, the input raw tensor still needs to reside on the host (CPU), because the first stages of the decoding process take place on the host:\nfrom torchvision.io.image import read_file, decode_jpeg\n\n```python\ndata = read_file('path_to_image.jpg') # raw data is on CPU\nimg = decode_jpeg(data, device='cuda') # decoded image in on GPU\n```\nFor more details, see [the documentation](https://pytorch.org/vision/stable/io.html#torchvision.io.decode_jpeg).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) iOS support \nTorchVision 0.10 now provides pre-compiled iOS binaries for its C++ operators, which means you can run Faster R-CNN and Mask R-CNN on iOS. An example app on how to build a program leveraging those ops can be found [here](https://github.com/pytorch/vision/tree/master/ios/VisionTestApp). \n\n# TorchAudio 0.9.0\n\n### (Stable) Complex Tensor Migration \nTorchAudio has functions that handle complex-valued tensors. These functions follow a convention to use an extra dimension to represent real and imaginary parts. In PyTorch 1.6, the native complex type was introduced. As its API is getting stable, torchaudio has started to migrate to the native complex type.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} {"page_content": "In this release, we added support for native complex tensors, and you can opt-in to use them. Using the native complex types, we have verified that affected functions continue to support autograd and TorchScript, moreover, switching to native complex types improves their performance. For more details, refer to [pytorch/audio#1337](https://github.com/pytorch/audio/issues/1337).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Filtering Improvement", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "In release 0.8, we added the C++ implementation of the core part of ```lfilter``` for CPU, which improved the performance. In this release, we optimized some internal operations of the CPU implementation for further performance improvement. We also added autograd support to both CPU and GPU. Now ```lfilter``` and all the ```biquad``` filters (```biquad```, ```band_biquad```, ```bass_biquad```, ```treble_biquad```, ```allpass_biquad```, ```lowpass_biquad```, ```highpass_biquad```, ```bandpass_biquad```,", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```equalizer_biquad``` and ```bandrefect_biquad```) benefit from the performance improvement and support autograd. We also moved the implementation of overdrive to C++ for performance improvement. For more details, refer to [the documentation](https://pytorch.org/audio/0.9.0/functional.html#lfilter).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Improved Autograd Support \nAlong with the work of Complex Tensor Migration and Filtering Improvement, we also added autograd tests to transforms. `lfilter`, `biquad` and its variants, and most transforms are now guaranteed to support autograd. For more details, refer to [the release note](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) Improved Windows Support \nTorchaudio implements some operations in C++ for reasons such as performance and integration with third-party libraries. These C++ components were only available on Linux and macOS. In this release, we have added support to Windows. With this, the efficient filtering implementation mentioned above is also available on Windows.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "However, please note that not all the C++ components are available for Windows. \u201csox_io\u201d backend and ```torchaudio.functional.compute_kaldi_pitch``` are not supported.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Stable) I/O Functions Migration \nSince the 0.6 release, we have continuously improved I/O functionality. Specifically, in 0.8 we changed the default backend from \u201csox\u201d to \u201csox_io\u201d and applied the same switch to API of the \u201csoundfile\u201d backend. The 0.9 release concludes this migration by removing the deprecated backends. For more details, please refer to [#903](https://github.com/pytorch/audio/issues/903).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Wav2Vec2.0 Model", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We have added the model architectures from [Wav2Vec2.0](https://arxiv.org/abs/2006.11477). You can import fine-tuned models parameters published on [fairseq](https://github.com/pytorch/fairseq/tree/master/examples/wav2vec) and [Hugging Face Hub](https://huggingface.co/models?filter=wav2vec2). Our model definition supports TorchScript, and it is possible to deploy the model to non-Python environments, such as C++, [Android](https://github.com/pytorch/android-demo-app/tree/master/SpeechRecognition) and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[iOS](https://github.com/pytorch/ios-demo-app/tree/master/SpeechRecognition).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "The following code snippet illustrates such a use case. Please check out our [c++ example directory](https://github.com/pytorch/audio/tree/master/examples/libtorchaudio) for the complete example. Currently, it is designed for running inference. If you would like more support for training, please file a feature request.\n\n```python\n# Import fine-tuned model from Hugging Face Hub\nimport transformers\nfrom torchaudio.models.wav2vec2.utils import import_huggingface_model", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "original = Wav2Vec2ForCTC.from_pretrained(\"facebook/wav2vec2-base-960h\")\nimported = import_huggingface_model(original)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Import fine-tuned model from fairseq\nimport fairseq\nfrom torchaudio.models.wav2vec2.utils import import_fairseq_model\n\noriginal, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task(\n [\"wav2vec_small_960h.pt\"], arg_overrides={'data': \"\"})\nimported = import_fairseq_model(original[0].w2v_encoder)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n# Build uninitialized model and load state dict\nfrom torchaudio.models import wav2vec2_base\n\nmodel = wav2vec2_base(num_out=32)\nmodel.load_state_dict(imported.state_dict())\n\n# Quantize / script / optimize for mobile\nquantized_model = torch.quantization.quantize_dynamic(\n model, qconfig_spec={torch.nn.Linear}, dtype=torch.qint8)\nscripted_model = torch.jit.script(quantized_model)\noptimized_model = optimize_for_mobile(scripted_model)\noptimized_model.save(\"model_for_deployment.pt\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For more details, see [the documentation](https://pytorch.org/audio/0.9.0/models.html#wav2vec2-0).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) Resampling Improvement \nIn release 0.8, we vectorized the operation in ```torchaudio.compliance.kaldi.resample_waveform```, which improved the performance of ```resample_waveform``` and ```torchaudio.transforms.Resample```. In this release, we have further revised the way the resampling algorithm is implemented.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "We have:\n* Added Kaiser Window support for a wider range of resampling quality.\n* Added ```rolloff``` parameter for anti-aliasing control.\n* Added the mechanism to precompute the kernel and cache it in ```torchaudio.transforms.Resample``` for even faster operation.\n* Moved the implementation from ```torchaudio.compliance.kaldi.resample_waveform``` to ```torchaudio.functional.resample``` and deprecated ```torchaudio.compliance.kaldi.resample_waveform```.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For more details, see [the documentation](https://pytorch.org/audio/0.9.0/transforms.html#resample).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Prototype) RNN Transducer Loss \nThe RNN transducer loss is used in training RNN transducer models, which is a popular architecture for speech recognition tasks. The prototype loss in torchaudio currently supports autograd, torchscript, float16 and float32, and can also be run on both CPU and CUDA. For more details, please refer to [the documentation](https://pytorch.org/audio/master/rnnt_loss.html).\n\n# TorchText 0.10.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "(Beta) New Vocab Module", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "In this release, we introduce a new Vocab module that replaces the current Vocab class. The new Vocab provides common functional APIs for NLP workflows. This module is backed by an efficient C++ implementation that reduces batch look-up time by up-to ~85% (refer to summary of [#1248](https://github.com/pytorch/text/pull/1248) and [#1290](https://github.com/pytorch/text/pull/1290) for further information on benchmarks), and provides support for TorchScript. We provide accompanying factory functions that can", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "be used to build the Vocab object either through a python ordered dictionary or an Iterator that yields lists of tokens.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "```python\n#creating Vocab from text file\nimport io\nfrom torchtext.vocab import build_vocab_from_iterator\n#generator that yield list of tokens\ndef yield_tokens(file_path):\n with io.open(file_path, encoding = 'utf-8') as f:\n for line in f:\n yield line.strip().split()\n#get Vocab object\nvocab_obj = build_vocab_from_iterator(yield_tokens(file_path), specials=[\"\"])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#creating Vocab through ordered dict\nfrom torchtext.vocab import vocab\nfrom collections import Counter, OrderedDict\ncounter = Counter([\"a\", \"a\", \"b\", \"b\", \"b\"])\nsorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[1], reverse=True)\nordered_dict = OrderedDict(sorted_by_freq_tuples)\nvocab_obj = vocab(ordered_dict)\n\n#common API usage\n\n#look-up index\nvocab_obj[\"a\"]\n\n#batch look-up indices\nvocab_obj.looup_indices([\"a\",\"b\"])\n#support forward API of PyTorch nn Modules\nvocab_obj([\"a\",\"b\"])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "#batch look-up tokens\nvocab_obj.lookup_tokens([0,1])\n\n#set default index to return when token not found\nvocab_obj.set_default_index(0)\nvocab_obj[\"out_of_vocabulary\"] #prints 0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "For more details, refer to [the documentation](https://pytorch.org/text/stable/vocab.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Thanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join [the discussion](https://discuss.pytorch.org/) forums and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Facebook](https://www.facebook.com/pytorch/), [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch) or", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "[LinkedIn](https://www.linkedin.com/company/pytorch).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "Cheers!\n\n-Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Everything You Need To Know About Torchvision\u2019s SSDlite Implementation'\nauthor: Vasilis Vryniotis\nfeatured-img: 'assets/images/mAP-of-SSD320-MobileNetV3-Large.png'\n---\n\nIn the [previous article](https://pytorch.org/blog/torchvision-ssd-implementation/), we\u2019ve discussed how the SSD algorithm works, covered its implementation details and presented its training process. If you have not read the previous blog post, I encourage you to check it out before continuing.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "In this part 2 of the series, we will focus on the mobile-friendly variant of SSD called SSDlite. Our plan is to first go through the main components of the algorithm highlighting the parts that differ from the original SSD, then discuss how the released model was trained and finally provide detailed benchmarks for all the new Object Detection models that we explored.\n\n# The SSDlite Network Architecture", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "The SSDlite is an adaptation of SSD which was first briefly introduced on the [MobileNetV2 paper](https://arxiv.org/abs/1801.04381) and later reused on the [MobileNetV3 paper](https://arxiv.org/abs/1905.02244). Because the main focus of the two papers was to introduce novel CNN architectures, most of the implementation details of SSDlite were not clarified. Our code follows all the details presented on the two papers and where necessary fills the gaps from the [official", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "implementation](https://github.com/tensorflow/models/tree/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection).", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "As noted before, the SSD is a family of models because one can configure it with different backbones (such as VGG, MobileNetV3 etc) and different Heads (such as using regular convolutions, separable convolutions etc). Thus many of the SSD components remain the same in SSDlite. Below we discuss only those that are different", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Classification and Regression Heads", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Following the Section 6.2 of the MobileNetV2 paper, SSDlite replaces the regular convolutions used on the original Heads with separable convolutions. Consequently, our implementation introduces [new heads](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L65-L95) that use [3x3 Depthwise convolutions and 1x1", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "projections](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L26-L36). Since all other components of the SSD method remain the same, to create an SSDlite model our implementation [initializes the SSDlite head](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L222-L223) and passes it directly to the SSD constructor.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Backbone Feature Extractor", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Our implementation introduces a new class for building MobileNet [feature extractors](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L98). Following the Section 6.3 of the MobileNetV3 paper, the backbone returns the [output of the expansion layer](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L106) of the Inverted Bottleneck block which has an output stride of 16", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "and the [output of the layer just before the pooling](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L107) which has an output stride of 32. Moreover, all [extra blocks](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L111-L116) of the backbone are replaced with [lightweight", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "equivalents](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L39-L54) which use a 1x1 compression, a separable 3x3 convolution with stride 2 and a 1x1 expansion. Finally to ensure that the heads have enough prediction power even when small [width multipliers](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L99) are used, the [minimum", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "depth](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L110) size of all convolutions is controlled by the ```min_depth``` hyperparameter.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "# The SSDlite320 MobileNetV3-Large model\n\n
\n \n
\n\nThis section discusses the configuration of the provided [SSDlite pre-trained](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L159-L162) model along with the training processes followed to replicate the paper results as closely as possible.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Training process\n\nAll of the hyperparameters and scripts used to train the model on the COCO dataset can be found in our [references](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssdlite320-mobilenetv3-large) folder. Here we discuss the most notable details of the training process.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Tuned Hyperparameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Though the papers don\u2019t provide any information on the hyperparameters used for training the models (such as regularization, learning rate and the batch size), the parameters listed in the [configuration files](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config) on the official repo were good starting points and using cross validation we adjusted them to their optimal values. All the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "above gave us a significant boost over the baseline SSD configuration.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Data Augmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Key important difference of SSDlite comparing to SSD is that the backbone of the first has only a fraction of the weights of the latter. This is why in SSDlite, the Data Augmentation focuses more on making the model robust to objects of variable sizes than trying to avoid overfitting. Consequently, SSDlite [uses only a subset](https://github.com/pytorch/vision/blob/43d772067fe77965ec8fc49c799de5cea44b8aa2/references/detection/presets.py#L19-L24) of the SSD transformations and this way it avoids the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "over-regularization of the model.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "LR Scheme", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Due to the reliance on Data Augmentation to make the model robust to small and medium sized objects, we found that it is particularly beneficial for the training recipe to use large number of epochs. More specifically by using roughly 3x more epochs than SSD we are able to increase our precision by 4.2mAP points and by using a 6x multiplier we improve by 4.9mAP. Increasing further the epochs seems to yield diminishing returns and makes the training too slow and impractical, nevertheless based on the [model", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "configuration](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L154) it seems that the authors of the paper used an equivalent *16x multiplier*.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Weight Initialization & Input Scaling & ReLU6", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "A set of final optimizations that brought our implementation very close to the official one and helped us bridge the accuracy gap was training the backbone [from scratch](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L139-L141) instead of initializing from ImageNet, adapting our [weight initialization", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "scheme](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L57-L62), changing our [Input Scaling](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L216-L219) and replacing all standard ReLUs added on the SSDlite heads with ReLU6. Note that since we trained the model from random weights, we additionally applied the speed optimization described on the paper of using a", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "[reduced tail](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L196-L197) on the backbone.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Implementation Differences", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Comparing the above implementation with the one on the official repo, we\u2019ve identified a few differences. Most of them are minor and they are related to how we initialize the weights (for example [Normal initialization](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L57-L62) vs [Truncated", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Normal](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L104-L107)), how we parameterize the LR Scheduling (for example [smaller](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/references/detection/engine.py#L21-L22) vs", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "[larger](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L169-L170) warmup rate, [shorter](https://github.com/pytorch/vision/tree/master/references/detection#ssdlite320-mobilenetv3-large) vs [longer](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L154) training)", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "etc. The biggest known difference lies in the way we compute the Classification loss. More specifically the implementation of SSDlite with MobileNetV3 backbone on the official repo [doesn\u2019t use the SSD\u2019s Multibox loss](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L121-L124) but instead uses RetinaNet\u2019s [focal loss](https://arxiv.org/abs/1708.02002). This is a rather significant", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "deviation from the paper and since TorchVision already offers a full implementation of RetinaNet, we decided to implement SSDlite using the normal Multi-box SSD loss.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Break down of key accuracy improvements", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "As discussed in previous articles, reproducing research papers and porting them to code is not a journey of monotonically increasing accuracies, especially in cases where the full training and implementation details are not known. Typically the process involves lots of backtracking as one needs to identify those implementation details and parameters that have significant impact on the accuracy from those that don\u2019t. Below we try to visualize the most important iterations that improved our accuracy from the", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "baseline:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
\n\n\n {:.table.table-striped.table-bordered}\n| **Iteration** | **mAP** | \n| ------------- | ------------- |\n| Baseline with \"SSD-style\" Hyperparams | 10.6 | \n| + Tuned Hyperparams | 14.2 | \n| + SSDlite Data Augmentation | 15.2 |\n| + 3x LR Scheme | 19.4 |\n| + 6x LR Scheme | 20.1 | \n| + Weight Initialization & Input Scaling & ReLU6 | 21.3 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "The order of optimizations presented above is accurate, though a bit idealized in some cases. For example, though different schedulers were tested during the Hyperparameter tuning phase, none of them provided significant improvements and thus we maintained the MultiStepLR which was used in the baseline. Nevertheless while later experimenting with different LR Schemes, we found it beneficial to switch to CosineAnnealingLR, as it required less configuration. Consequently, we believe that the main takeaway", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "from the above summary should be that even by starting with a correct implementation and a set of optimal hyperparams from a model of the same family, there is always accuracy points to be found by optimizing the training recipe and tuning the implementation. Admittedly the above is a rather extreme case where the accuracy doubled, but still in many cases there is a large number of optimizations that can help us push the accuracy significantly.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "# Benchmarks\n\nHere is how to initialize the two pre-trained models:\n\n```python\nssdlite = torchvision.models.detection.ssdlite320_mobilenet_v3_large(pretrained=True)\nssd = torchvision.models.detection.ssd300_vgg16(pretrained=True)", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "Below are the benchmarks between the new and selected previous detection models:", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "{:.table.table-striped.table-bordered}\n| **Model** | **mAP** | **Inference on CPU (sec)** | **# Params (M)** |\n| ------------- | ------------- | ------------- | ------------- |\n| SSDlite320 MobileNetV3-Large | 21.3 | 0.0911 | 3.44 |\n| SSD300 VGG16 | 25.1 | 0.8303 | 35.64 |\n| SSD512 VGG16 (not released) | 28.8| 2.2494 | 37.08 |\n| SSD512 ResNet50 (not released) | 30.2 | 1.1137 | 42.70 |\n| Faster R-CNN MobileNetV3-Large 320 FPN (Low-Res) | 22.8 | 0.1679 | 19.39|", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "| Faster R-CNN MobileNetV3-Large FPN (High-Res) | 32.8 | 0.8409 | 19.39 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "As we can see, the SSDlite320 MobileNetV3-Large model is by far the fastest and smallest model and thus it\u2019s an excellent candidate for real-world mobile applications. Though its accuracy is lower than the pre-trained low-resolution Faster R-CNN equivalent, the SSDlite framework is adaptable and one can boost its accuracy by introducing heavier heads with more convolutions.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "On the other hand, the SSD300 VGG16 model is rather slow and less accurate. This is mainly because of its VGG16 backbone. Though extremely important and influential, the VGG architecture is nowadays quite outdated. Thus though the specific model has historical and research value and hence it\u2019s included in TorchVision, we recommend to users who want high-resolution detectors for real world applications to either combine SSD with alternative backbones (see this", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "[example](https://github.com/pytorch/vision/pull/3760) on how to create one) or use one of the Faster R-CNN pre-trained models.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "### (Stable) Filtering Improvement \nIn release 0.8, we added the C++ implementation of the core part of ```lfilter``` for CPU, which improved the performance. In this release, we optimized some internal operations of the CPU implementation for further performance improvement. We also added autograd support to both CPU and GPU. Now ```lfilter``` and all the ```biquad``` filters (```biquad```, ```band_biquad```, ```bass_biquad```, ```treble_biquad```, ```allpass_biquad```, ```lowpass_biquad```, ```highpass_biquad```, ```bandpass_biquad```, ```equalizer_biquad``` and ```bandrefect_biquad```) benefit from the performance improvement and support autograd. We also moved the implementation of overdrive to C++ for performance improvement. For more details, refer to [the documentation](https://pytorch.org/audio/0.9.0/functional.html#lfilter).\n \n### (Stable) Improved Autograd Support \nAlong with the work of Complex Tensor Migration and Filtering Improvement, we also added autograd tests to transforms. `lfilter`, `biquad` and its variants, and most transforms are now guaranteed to support autograd. For more details, refer to [the release note](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Stable) Improved Windows Support \nTorchaudio implements some operations in C++ for reasons such as performance and integration with third-party libraries. These C++ components were only available on Linux and macOS. In this release, we have added support to Windows. With this, the efficient filtering implementation mentioned above is also available on Windows.\n\nHowever, please note that not all the C++ components are available for Windows. \u201csox_io\u201d backend and ```torchaudio.functional.compute_kaldi_pitch``` are not supported. \n\n### (Stable) I/O Functions Migration \nSince the 0.6 release, we have continuously improved I/O functionality. Specifically, in 0.8 we changed the default backend from \u201csox\u201d to \u201csox_io\u201d and applied the same switch to API of the \u201csoundfile\u201d backend. The 0.9 release concludes this migration by removing the deprecated backends. For more details, please refer to [#903](https://github.com/pytorch/audio/issues/903).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "### (Beta) Wav2Vec2.0 Model\nWe have added the model architectures from [Wav2Vec2.0](https://arxiv.org/abs/2006.11477). You can import fine-tuned models parameters published on [fairseq](https://github.com/pytorch/fairseq/tree/master/examples/wav2vec) and [Hugging Face Hub](https://huggingface.co/models?filter=wav2vec2). Our model definition supports TorchScript, and it is possible to deploy the model to non-Python environments, such as C++, [Android](https://github.com/pytorch/android-demo-app/tree/master/SpeechRecognition) and [iOS](https://github.com/pytorch/ios-demo-app/tree/master/SpeechRecognition). \n\nThe following code snippet illustrates such a use case. Please check out our [c++ example directory](https://github.com/pytorch/audio/tree/master/examples/libtorchaudio) for the complete example. Currently, it is designed for running inference. If you would like more support for training, please file a feature request.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n# Import fine-tuned model from Hugging Face Hub\nimport transformers\nfrom torchaudio.models.wav2vec2.utils import import_huggingface_model\n\noriginal = Wav2Vec2ForCTC.from_pretrained(\"facebook/wav2vec2-base-960h\")\nimported = import_huggingface_model(original)\n```\n\n```python\n# Import fine-tuned model from fairseq\nimport fairseq\nfrom torchaudio.models.wav2vec2.utils import import_fairseq_model\n\noriginal, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task(\n [\"wav2vec_small_960h.pt\"], arg_overrides={'data': \"\"})\nimported = import_fairseq_model(original[0].w2v_encoder)\n```\n\n```python\n# Build uninitialized model and load state dict\nfrom torchaudio.models import wav2vec2_base\n\nmodel = wav2vec2_base(num_out=32)\nmodel.load_state_dict(imported.state_dict())", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# Quantize / script / optimize for mobile\nquantized_model = torch.quantization.quantize_dynamic(\n model, qconfig_spec={torch.nn.Linear}, dtype=torch.qint8)\nscripted_model = torch.jit.script(quantized_model)\noptimized_model = optimize_for_mobile(scripted_model)\noptimized_model.save(\"model_for_deployment.pt\")\n```\n\nFor more details, see [the documentation](https://pytorch.org/audio/0.9.0/models.html#wav2vec2-0). \n\n### (Beta) Resampling Improvement \nIn release 0.8, we vectorized the operation in ```torchaudio.compliance.kaldi.resample_waveform```, which improved the performance of ```resample_waveform``` and ```torchaudio.transforms.Resample```. In this release, we have further revised the way the resampling algorithm is implemented.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "We have:\n* Added Kaiser Window support for a wider range of resampling quality.\n* Added ```rolloff``` parameter for anti-aliasing control.\n* Added the mechanism to precompute the kernel and cache it in ```torchaudio.transforms.Resample``` for even faster operation.\n* Moved the implementation from ```torchaudio.compliance.kaldi.resample_waveform``` to ```torchaudio.functional.resample``` and deprecated ```torchaudio.compliance.kaldi.resample_waveform```.\n\nFor more details, see [the documentation](https://pytorch.org/audio/0.9.0/transforms.html#resample). \n\n### (Prototype) RNN Transducer Loss \nThe RNN transducer loss is used in training RNN transducer models, which is a popular architecture for speech recognition tasks. The prototype loss in torchaudio currently supports autograd, torchscript, float16 and float32, and can also be run on both CPU and CUDA. For more details, please refer to [the documentation](https://pytorch.org/audio/master/rnnt_loss.html).\n\n# TorchText 0.10.0", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "# TorchText 0.10.0\n\n### (Beta) New Vocab Module \nIn this release, we introduce a new Vocab module that replaces the current Vocab class. The new Vocab provides common functional APIs for NLP workflows. This module is backed by an efficient C++ implementation that reduces batch look-up time by up-to ~85% (refer to summary of [#1248](https://github.com/pytorch/text/pull/1248) and [#1290](https://github.com/pytorch/text/pull/1290) for further information on benchmarks), and provides support for TorchScript. We provide accompanying factory functions that can be used to build the Vocab object either through a python ordered dictionary or an Iterator that yields lists of tokens.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "```python\n#creating Vocab from text file\nimport io\nfrom torchtext.vocab import build_vocab_from_iterator\n#generator that yield list of tokens\ndef yield_tokens(file_path):\n with io.open(file_path, encoding = 'utf-8') as f:\n for line in f:\n yield line.strip().split()\n#get Vocab object\nvocab_obj = build_vocab_from_iterator(yield_tokens(file_path), specials=[\"\"])\n\n#creating Vocab through ordered dict\nfrom torchtext.vocab import vocab\nfrom collections import Counter, OrderedDict\ncounter = Counter([\"a\", \"a\", \"b\", \"b\", \"b\"])\nsorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[1], reverse=True)\nordered_dict = OrderedDict(sorted_by_freq_tuples)\nvocab_obj = vocab(ordered_dict)\n\n#common API usage\n\n#look-up index\nvocab_obj[\"a\"]\n\n#batch look-up indices\nvocab_obj.looup_indices([\"a\",\"b\"])\n#support forward API of PyTorch nn Modules\nvocab_obj([\"a\",\"b\"])\n\n#batch look-up tokens\nvocab_obj.lookup_tokens([0,1])", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "#set default index to return when token not found\nvocab_obj.set_default_index(0)\nvocab_obj[\"out_of_vocabulary\"] #prints 0\n```\n\nFor more details, refer to [the documentation](https://pytorch.org/text/stable/vocab.html). \n\nThanks for reading. If you\u2019re interested in these updates and want to join the PyTorch community, we encourage you to join [the discussion](https://discuss.pytorch.org/) forums and [open GitHub issues](https://github.com/pytorch/pytorch/issues). To get the latest news from PyTorch, follow us on [Facebook](https://www.facebook.com/pytorch/), [Twitter](https://twitter.com/PyTorch), [Medium](https://medium.com/pytorch), [YouTube](https://www.youtube.com/pytorch) or [LinkedIn](https://www.linkedin.com/company/pytorch). \n\nCheers!\n\n-Team PyTorch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.9-new-library-releases/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Everything You Need To Know About Torchvision\u2019s SSDlite Implementation'\nauthor: Vasilis Vryniotis\nfeatured-img: 'assets/images/mAP-of-SSD320-MobileNetV3-Large.png'\n---\n\nIn the [previous article](https://pytorch.org/blog/torchvision-ssd-implementation/), we\u2019ve discussed how the SSD algorithm works, covered its implementation details and presented its training process. If you have not read the previous blog post, I encourage you to check it out before continuing.\n\nIn this part 2 of the series, we will focus on the mobile-friendly variant of SSD called SSDlite. Our plan is to first go through the main components of the algorithm highlighting the parts that differ from the original SSD, then discuss how the released model was trained and finally provide detailed benchmarks for all the new Object Detection models that we explored.\n\n# The SSDlite Network Architecture", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "The SSDlite is an adaptation of SSD which was first briefly introduced on the [MobileNetV2 paper](https://arxiv.org/abs/1801.04381) and later reused on the [MobileNetV3 paper](https://arxiv.org/abs/1905.02244). Because the main focus of the two papers was to introduce novel CNN architectures, most of the implementation details of SSDlite were not clarified. Our code follows all the details presented on the two papers and where necessary fills the gaps from the [official implementation](https://github.com/tensorflow/models/tree/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection). \n\nAs noted before, the SSD is a family of models because one can configure it with different backbones (such as VGG, MobileNetV3 etc) and different Heads (such as using regular convolutions, separable convolutions etc). Thus many of the SSD components remain the same in SSDlite. Below we discuss only those that are different\n\n## Classification and Regression Heads", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "Following the Section 6.2 of the MobileNetV2 paper, SSDlite replaces the regular convolutions used on the original Heads with separable convolutions. Consequently, our implementation introduces [new heads](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L65-L95) that use [3x3 Depthwise convolutions and 1x1 projections](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L26-L36). Since all other components of the SSD method remain the same, to create an SSDlite model our implementation [initializes the SSDlite head](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L222-L223) and passes it directly to the SSD constructor.\n\n## Backbone Feature Extractor", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "Our implementation introduces a new class for building MobileNet [feature extractors](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L98). Following the Section 6.3 of the MobileNetV3 paper, the backbone returns the [output of the expansion layer](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L106) of the Inverted Bottleneck block which has an output stride of 16 and the [output of the layer just before the pooling](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L107) which has an output stride of 32. Moreover, all [extra blocks](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L111-L116) of the backbone are replaced with [lightweight equivalents](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L39-L54) which use a 1x1 compression, a separable 3x3 convolution with stride 2 and a 1x1 expansion. Finally to ensure that the heads have enough prediction power even when small [width multipliers](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L99) are used, the [minimum depth](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L110) size of all convolutions is controlled by the ```min_depth``` hyperparameter.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "# The SSDlite320 MobileNetV3-Large model\n\n
\n \n
\n\nThis section discusses the configuration of the provided [SSDlite pre-trained](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L159-L162) model along with the training processes followed to replicate the paper results as closely as possible. \n\n## Training process\n\nAll of the hyperparameters and scripts used to train the model on the COCO dataset can be found in our [references](https://github.com/pytorch/vision/blob/e35793a1a4000db1f9f99673437c514e24e65451/references/detection/README.md#ssdlite320-mobilenetv3-large) folder. Here we discuss the most notable details of the training process.\n\n### Tuned Hyperparameters", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "Though the papers don\u2019t provide any information on the hyperparameters used for training the models (such as regularization, learning rate and the batch size), the parameters listed in the [configuration files](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config) on the official repo were good starting points and using cross validation we adjusted them to their optimal values. All the above gave us a significant boost over the baseline SSD configuration.\n\n### Data Augmentation", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "Key important difference of SSDlite comparing to SSD is that the backbone of the first has only a fraction of the weights of the latter. This is why in SSDlite, the Data Augmentation focuses more on making the model robust to objects of variable sizes than trying to avoid overfitting. Consequently, SSDlite [uses only a subset](https://github.com/pytorch/vision/blob/43d772067fe77965ec8fc49c799de5cea44b8aa2/references/detection/presets.py#L19-L24) of the SSD transformations and this way it avoids the over-regularization of the model.\n\n### LR Scheme", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "### LR Scheme\n\nDue to the reliance on Data Augmentation to make the model robust to small and medium sized objects, we found that it is particularly beneficial for the training recipe to use large number of epochs. More specifically by using roughly 3x more epochs than SSD we are able to increase our precision by 4.2mAP points and by using a 6x multiplier we improve by 4.9mAP. Increasing further the epochs seems to yield diminishing returns and makes the training too slow and impractical, nevertheless based on the [model configuration](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L154) it seems that the authors of the paper used an equivalent *16x multiplier*. \n\n### Weight Initialization & Input Scaling & ReLU6", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "A set of final optimizations that brought our implementation very close to the official one and helped us bridge the accuracy gap was training the backbone [from scratch](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L139-L141) instead of initializing from ImageNet, adapting our [weight initialization scheme](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L57-L62), changing our [Input Scaling](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L216-L219) and replacing all standard ReLUs added on the SSDlite heads with ReLU6. Note that since we trained the model from random weights, we additionally applied the speed optimization described on the paper of using a [reduced tail](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L196-L197) on the backbone.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "### Implementation Differences", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "Comparing the above implementation with the one on the official repo, we\u2019ve identified a few differences. Most of them are minor and they are related to how we initialize the weights (for example [Normal initialization](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/torchvision/models/detection/ssdlite.py#L57-L62) vs [Truncated Normal](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L104-L107)), how we parameterize the LR Scheduling (for example [smaller](https://github.com/pytorch/vision/blob/b6f733046c9259f354d060cd808241a558d7d596/references/detection/engine.py#L21-L22) vs [larger](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L169-L170) warmup rate, [shorter](https://github.com/pytorch/vision/tree/master/references/detection#ssdlite320-mobilenetv3-large) vs [longer](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L154) training) etc. The biggest known difference lies in the way we compute the Classification loss. More specifically the implementation of SSDlite with MobileNetV3 backbone on the official repo [doesn\u2019t use the SSD\u2019s Multibox loss](https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/object_detection/samples/configs/ssdlite_mobilenet_v3_large_320x320_coco.config#L121-L124) but instead uses RetinaNet\u2019s [focal loss](https://arxiv.org/abs/1708.02002). This is a rather significant deviation from the paper and since TorchVision already offers a full implementation of RetinaNet, we decided to implement SSDlite using the normal Multi-box SSD loss.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "## Break down of key accuracy improvements\n\nAs discussed in previous articles, reproducing research papers and porting them to code is not a journey of monotonically increasing accuracies, especially in cases where the full training and implementation details are not known. Typically the process involves lots of backtracking as one needs to identify those implementation details and parameters that have significant impact on the accuracy from those that don\u2019t. Below we try to visualize the most important iterations that improved our accuracy from the baseline:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "{:.table.table-striped.table-bordered}\n| **Iteration** | **mAP** | \n| ------------- | ------------- |\n| Baseline with \"SSD-style\" Hyperparams | 10.6 | \n| + Tuned Hyperparams | 14.2 | \n| + SSDlite Data Augmentation | 15.2 |\n| + 3x LR Scheme | 19.4 |\n| + 6x LR Scheme | 20.1 | \n| + Weight Initialization & Input Scaling & ReLU6 | 21.3 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "The order of optimizations presented above is accurate, though a bit idealized in some cases. For example, though different schedulers were tested during the Hyperparameter tuning phase, none of them provided significant improvements and thus we maintained the MultiStepLR which was used in the baseline. Nevertheless while later experimenting with different LR Schemes, we found it beneficial to switch to CosineAnnealingLR, as it required less configuration. Consequently, we believe that the main takeaway from the above summary should be that even by starting with a correct implementation and a set of optimal hyperparams from a model of the same family, there is always accuracy points to be found by optimizing the training recipe and tuning the implementation. Admittedly the above is a rather extreme case where the accuracy doubled, but still in many cases there is a large number of optimizations that can help us push the accuracy significantly. \n\n# Benchmarks", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "# Benchmarks\n\nHere is how to initialize the two pre-trained models:\n\n```python\nssdlite = torchvision.models.detection.ssdlite320_mobilenet_v3_large(pretrained=True)\nssd = torchvision.models.detection.ssd300_vgg16(pretrained=True)\n```\n\nBelow are the benchmarks between the new and selected previous detection models:\n\n {:.table.table-striped.table-bordered}\n| **Model** | **mAP** | **Inference on CPU (sec)** | **# Params (M)** |\n| ------------- | ------------- | ------------- | ------------- |\n| SSDlite320 MobileNetV3-Large | 21.3 | 0.0911 | 3.44 |\n| SSD300 VGG16 | 25.1 | 0.8303 | 35.64 |\n| SSD512 VGG16 (not released) | 28.8| 2.2494 | 37.08 |\n| SSD512 ResNet50 (not released) | 30.2 | 1.1137 | 42.70 |\n| Faster R-CNN MobileNetV3-Large 320 FPN (Low-Res) | 22.8 | 0.1679 | 19.39|\n| Faster R-CNN MobileNetV3-Large FPN (High-Res) | 32.8 | 0.8409 | 19.39 |", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} +{"page_content": "As we can see, the SSDlite320 MobileNetV3-Large model is by far the fastest and smallest model and thus it\u2019s an excellent candidate for real-world mobile applications. Though its accuracy is lower than the pre-trained low-resolution Faster R-CNN equivalent, the SSDlite framework is adaptable and one can boost its accuracy by introducing heavier heads with more convolutions. \n\nOn the other hand, the SSD300 VGG16 model is rather slow and less accurate. This is mainly because of its VGG16 backbone. Though extremely important and influential, the VGG architecture is nowadays quite outdated. Thus though the specific model has historical and research value and hence it\u2019s included in TorchVision, we recommend to users who want high-resolution detectors for real world applications to either combine SSD with alternative backbones (see this [example](https://github.com/pytorch/vision/pull/3760) on how to create one) or use one of the Faster R-CNN pre-trained models.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} {"page_content": "We hope you enjoyed the 2nd and final part of the SSD series. We are looking forward to your feedback.", "metadata": {"source": "https://pytorch.org/blog/torchvision-ssdlite-implementation/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Annual Hackathon 2021'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/social_hackathon21.png'\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re excited to announce the PyTorch Annual Hackathon 2021! This year, we\u2019re looking to support the community in creating innovative PyTorch tools, libraries, and applications. 2021 is the third year we\u2019re hosting this Hackathon, and we welcome you to join the PyTorch community and put your machine learning skills into action. Submissions start on September 8 and end on November 3. Good luck to everyone!", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "Submission Categories\nYou can enter your PyTorch projects into three categories:\n\n* **PyTorch Responsible AI Development Tools & Libraries** - Build an AI development tool or library that helps develop AI models and applications responsibly. These tools, libraries, and apps need to support a researcher or developer to factor in fairness, security, and privacy throughout the entire machine learning development process of data gathering, model training, model validation, inferences, monitoring, and more.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "* **Web and Mobile Applications Powered by PyTorch** - Build an application with the web, mobile interface, and/or embedded device powered by PyTorch so the end users can interact with it. The submission must be built on PyTorch or use PyTorch-based libraries such as torchvision, torchtext, and fast.ai.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "* **PyTorch Developer Tools & Libraries** - Build a creative, useful, and well-implemented tool or library for improving the productivity and efficiency of PyTorch researchers and developers. The submission must be a machine learning algorithm, model, or application built using PyTorch or PyTorch-based libraries.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "Prizes\nSubmissions will be judged on the idea\u2019s quality, originality, implementation, and potential impact.\n\n* **First-Place Winners** in each category of the Hackathon will receive $5,000 in cash, along with a 30-minute call with the PyTorch development team. \n\n* **Second-Place Winners** will receive $3,000.\n\n\n* **Third-Place Winners** will receive $2,000.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "All winners will also receive the opportunity to create blog posts that will be featured throughout PyTorch channels as well as an exclusive Github badge. Honorable Mentions will also be awarded to the following three highest-scoring entries in each category and will receive $1,000 each.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "Cloud Computing Credits\nRequest $100 in credits from Amazon Web Services or Google Cloud for your computing costs. Please allow 3 business days for your request to be reviewed. Credits will be provided to verified registrants until the supplies run out. For more information, see https://pytorch2021.devpost.com/details/sponsors.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "2020 Winning Projects\n\n[DeMask](https://devpost.com/software/asteroid-the-pytorch-based-source-separation-toolkit) won first place in the PyTorch Developer Tools category. Built using Asteroid, a PyTorch-based audio source separation toolkit, DeMask is an end-to-end model for enhancing speech while wearing face masks.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "[Q&Aid](https://devpost.com/software/pytorchxai) won first place in the Web/Mobile Applications Powered by PyTorch category. Backed by PyTorch core algorithms and models, Q&Aid is a conceptual health care chatbot aimed at making health care diagnoses and facilitating communication between patients and doctors.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "[FairTorch](https://devpost.com/software/a-qeysp1) won first place in the PyTorch Responsible AI Development Tools category. FairTorch is a PyTorch fairness library that lets developers add constraints to their models to equalize metrics across subgroups by simply adding a few lines of code.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "How to Join\nIf you\u2019re interested in joining this year\u2019s PyTorch Hackathon, register at [http://pytorch2021.devpost.com](http://pytorch2021.devpost.com).", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated Generative Diffusion Models with PyTorch 2\"\nauthor: Grigory Sizov, Michael Gschwind, Hamid Shojanazeri, Driss Guessous, Daniel Haziza, Christian Puhrsch\n---\n\n**TL;DR**: PyTorch 2.0 nightly offers out-of-the-box performance improvement for Generative Diffusion models by using the new `torch.compile()` compiler and optimized implementations of Multihead Attention integrated with PyTorch 2.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Introduction\n\nA large part of the recent progress in Generative AI came from denoising diffusion models, which allow producing high quality images and videos from text prompts. This family includes Imagen, DALLE, Latent Diffusion, and others. However, all models in this family share a common drawback: generation is rather slow, due to the iterative nature of the sampling process by which the images are produced. This makes it important to optimize the code running inside the sampling loop.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "We took an open source implementation of a popular text-to-image diffusion model as a starting point and accelerated its generation using two optimizations available in PyTorch 2: compilation and fast attention implementation. Together with a few minor memory processing improvements in the code these optimizations give up to 49% inference speedup relative to the original implementation without [xFormers](https://github.com/facebookresearch/xformers), and 39% inference speedup relative to using the original", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "code with xFormers (excluding the compilation time), depending on the GPU architecture and batch size. Importantly, the speedup comes without a need to install xFormers or any other extra dependencies.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Announcing PyTorch Annual Hackathon 2021'\nauthor: Team PyTorch\nfeatured-img: 'assets/images/social_hackathon21.png'\n---\n\nWe\u2019re excited to announce the PyTorch Annual Hackathon 2021! This year, we\u2019re looking to support the community in creating innovative PyTorch tools, libraries, and applications. 2021 is the third year we\u2019re hosting this Hackathon, and we welcome you to join the PyTorch community and put your machine learning skills into action. Submissions start on September 8 and end on November 3. Good luck to everyone!\n\n
\n \n
\n\n## Submission Categories\nYou can enter your PyTorch projects into three categories:", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} +{"page_content": "* **PyTorch Responsible AI Development Tools & Libraries** - Build an AI development tool or library that helps develop AI models and applications responsibly. These tools, libraries, and apps need to support a researcher or developer to factor in fairness, security, and privacy throughout the entire machine learning development process of data gathering, model training, model validation, inferences, monitoring, and more. \n\n* **Web and Mobile Applications Powered by PyTorch** - Build an application with the web, mobile interface, and/or embedded device powered by PyTorch so the end users can interact with it. The submission must be built on PyTorch or use PyTorch-based libraries such as torchvision, torchtext, and fast.ai.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} +{"page_content": "* **PyTorch Developer Tools & Libraries** - Build a creative, useful, and well-implemented tool or library for improving the productivity and efficiency of PyTorch researchers and developers. The submission must be a machine learning algorithm, model, or application built using PyTorch or PyTorch-based libraries.\n\n## Prizes\nSubmissions will be judged on the idea\u2019s quality, originality, implementation, and potential impact.\n\n* **First-Place Winners** in each category of the Hackathon will receive $5,000 in cash, along with a 30-minute call with the PyTorch development team. \n\n* **Second-Place Winners** will receive $3,000.\n\n\n* **Third-Place Winners** will receive $2,000.\n\nAll winners will also receive the opportunity to create blog posts that will be featured throughout PyTorch channels as well as an exclusive Github badge. Honorable Mentions will also be awarded to the following three highest-scoring entries in each category and will receive $1,000 each.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} +{"page_content": "## Cloud Computing Credits\nRequest $100 in credits from Amazon Web Services or Google Cloud for your computing costs. Please allow 3 business days for your request to be reviewed. Credits will be provided to verified registrants until the supplies run out. For more information, see https://pytorch2021.devpost.com/details/sponsors. \n\n## 2020 Winning Projects\n\n[DeMask](https://devpost.com/software/asteroid-the-pytorch-based-source-separation-toolkit) won first place in the PyTorch Developer Tools category. Built using Asteroid, a PyTorch-based audio source separation toolkit, DeMask is an end-to-end model for enhancing speech while wearing face masks.\n\n[Q&Aid](https://devpost.com/software/pytorchxai) won first place in the Web/Mobile Applications Powered by PyTorch category. Backed by PyTorch core algorithms and models, Q&Aid is a conceptual health care chatbot aimed at making health care diagnoses and facilitating communication between patients and doctors.", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} +{"page_content": "[FairTorch](https://devpost.com/software/a-qeysp1) won first place in the PyTorch Responsible AI Development Tools category. FairTorch is a PyTorch fairness library that lets developers add constraints to their models to equalize metrics across subgroups by simply adding a few lines of code.\n\n## How to Join\nIf you\u2019re interested in joining this year\u2019s PyTorch Hackathon, register at [http://pytorch2021.devpost.com](http://pytorch2021.devpost.com).", "metadata": {"source": "https://pytorch.org/blog/pytorch-hackathon-2021/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Accelerated Generative Diffusion Models with PyTorch 2\"\nauthor: Grigory Sizov, Michael Gschwind, Hamid Shojanazeri, Driss Guessous, Daniel Haziza, Christian Puhrsch\n---\n\n**TL;DR**: PyTorch 2.0 nightly offers out-of-the-box performance improvement for Generative Diffusion models by using the new `torch.compile()` compiler and optimized implementations of Multihead Attention integrated with PyTorch 2.\n\n## Introduction\n\nA large part of the recent progress in Generative AI came from denoising diffusion models, which allow producing high quality images and videos from text prompts. This family includes Imagen, DALLE, Latent Diffusion, and others. However, all models in this family share a common drawback: generation is rather slow, due to the iterative nature of the sampling process by which the images are produced. This makes it important to optimize the code running inside the sampling loop.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "We took an open source implementation of a popular text-to-image diffusion model as a starting point and accelerated its generation using two optimizations available in PyTorch 2: compilation and fast attention implementation. Together with a few minor memory processing improvements in the code these optimizations give up to 49% inference speedup relative to the original implementation without [xFormers](https://github.com/facebookresearch/xformers), and 39% inference speedup relative to using the original code with xFormers (excluding the compilation time), depending on the GPU architecture and batch size. Importantly, the speedup comes without a need to install xFormers or any other extra dependencies.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} {"page_content": "The table below shows the improvement in runtime between the original implementation with xFormers installed and our optimized version with PyTorch-integrated memory efficient attention (originally developed for and released in the [xFormers](https://github.com/facebookresearch/xformers) library) and PyTorch compilation. The compilation time is excluded.\n\n**Runtime improvement in % compared to original+xFormers**\n\nSee the absolute runtime numbers in section \u201cBenchmarking setup and results summary\u201d", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
GPU\n Batch size 1\n Batch size 2\n Batch size 4\n
P100 (no compilation)\n -3.8\n 0.44\n 5.47\n
T4\n 2.12\n 10.51\n 14.2\n
A10\n -2.34\n 8.99\n 10.57\n
V100\n 18.63\n 6.39\n 10.43\n
A100\n 38.5\n 20.33\n 12.17\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "One can notice the following:\n\n\n\n* The improvements are significant for powerful GPUs like A100 and V100. For those GPUs the improvement is most pronounced for batch size 1\n* For less powerful GPUs we observe smaller speedups (or in two cases slight regressions). The batch size trend is reversed here: improvement is larger for larger batches", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "In the following sections we describe the applied optimizations and provide detailed benchmarking data, comparing the generation time with various optimization features on/off.\n\nSpecifically, we benchmark 5 configurations and the plots below compare their absolute performance for different GPUs and batch sizes. For definitions of these configurations see section \u201cBenchmarking setup and results\u201d.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 1](/assets/images/2023-04-11-accelerated-generative-diffusion-models1.png){:style=\"max-height:800px; width:100%\"} \n\n![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 2](/assets/images/2023-04-11-accelerated-generative-diffusion-models2.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 1](/assets/images/2023-04-11-accelerated-generative-diffusion-models3.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Optimizations \n\nHere we\u2019ll go into more detail about the optimizations introduced into the model code. These optimizations rely on features of PyTorch 2.0 which has been released recently.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Optimized Attention", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "One part of the code which we optimized is the scaled dot-product attention. Attention is known to be a heavy operation: naive implementation materializes the attention matrix, leading to time and memory complexity quadratic in sequence length. It is common for diffusion models to use attention (`CrossAttention`) as part of Transformer blocks in multiple parts of the U-Net. Since the U-Net runs at every sampling step, this becomes a critical point to optimize. Instead of custom attention implementation one", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "can use `torch.nn.MultiheadAttention,` which in PyTorch 2 has optimized attention implementation is integrated into it. This optimization schematically boils down to the following pseudocode:", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "```\nclass CrossAttention(nn.Module):\n def __init__(self, ...):\n # Create matrices: Q, K, V, out_proj\n ...\n def forward(self, x, context=None, mask=None):\n # Compute out = SoftMax(Q*K/sqrt(d))V\n # Return out_proj(out)\n \u2026", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "gets replaced with\n\n```\nclass CrossAttention(nn.Module):\n def __init__(self, ...):\n self.mha = nn.MultiheadAttention(...)\n def forward(self, x, context):\n\treturn self.mha(x, context, context)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "The optimized implementation of attention was available already in PyTorch 1.13 (see [here](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/)) and widely adopted (see e.g. [HuggingFace transformers library example](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)). In particular, it integrates memory-efficient attention from the [xFormers](https://github.com/facebookresearch/xformers) library and flash", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "attention from [https://arxiv.org/abs/2205.14135](https://arxiv.org/abs/2205.14135). PyTorch 2.0 expands this to additional attention functions such as cross attention and custom kernels for further acceleration, making it applicable to diffusion models.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Flash attention is available on GPUs with compute capability SM 7.5 or SM 8.x - for example, on T4, A10, and A100, which are included in our benchmark (you can check compute capability of each NVIDIA GPU [here](https://developer.nvidia.com/cuda-gpus#compute)). However, in our tests on A100 the memory efficient attention performed better than flash attention for the particular case of diffusion models, due to the small number of attention heads and small batch size. PyTorch understands this and in this case", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "chooses memory efficient attention over flash attention when both are available (see the logic [here](https://github.com/pytorch/pytorch/blob/d8e795ecd53670682bd3b2e5ff1f378402b147d5/aten/src/ATen/native/transformers/cuda/sdp_utils.h#L33-L71)). For full control over the attention backends (memory-efficient attention, flash attention, \u201cvanilla math\u201d, or any future ones), power users can enable and disable them manually with the help of the context manager", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "[torch.backends.cuda.sdp_kernel](https://pytorch.org/docs/master/backends.html#torch.backends.cuda.sdp_kernel).", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Compilation\n\nCompilation is a [new feature of PyTorch 2.0](https://pytorch.org/get-started/pytorch-2.0/#user-experience), enabling significant speedups with a very simple user experience. To invoke the default behavior, simply wrap a PyTorch module or a function into `torch.compile`:\n\n\n```\nmodel = torch.compile(model)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "PyTorch compiler then turns Python code into a set of instructions which can be executed efficiently without Python overhead. The compilation happens dynamically the first time the code is executed. With the default behavior, under the hood PyTorch utilized [TorchDynamo](https://pytorch.org/docs/master/dynamo/index.html) to compile the code and [TorchInductor](https://dev-discuss.pytorch.org/t/torchinductor-a-pytorch-native-compiler-with-define-by-run-ir-and-symbolic-shapes/747) to further optimize it. See", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "[this tutorial](https://pytorch.org/tutorials/intermediate/dynamo_tutorial.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Although the one-liner above is enough for compilation, certain modifications in the code can squeeze a larger speedup. In particular, one should avoid so-called graph breaks - places in the code which PyTorch can\u2019t compile. As opposed to previous PyTorch compilation approaches (like TorchScript), PyTorch 2 compiler doesn\u2019t break in this case. Instead it falls back on eager execution - so the code runs, but with reduced performance. We introduced a few minor changes to the model code to get rid of graph", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "breaks. This included eliminating functions from libraries not supported by the compiler, such as `inspect.isfunction` and `einops.rearrange`. See this [doc](https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break) to learn more about graph breaks and how to eliminate them.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Theoretically, one can apply `torch.compile `on the whole diffusion sampling loop. However, in practice it is enough to just compile the U-Net. The reason is that `torch.compile` doesn\u2019t yet have a loop analyzer and would recompile the code for each iteration of the sampling loop. Moreover, compiled sampler code is likely to generate graph breaks - so one would need to adjust it if one wants to get a good performance from the compiled version.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Note that compilation [requires GPU compute capability >= SM 7.0](https://github.com/openai/triton/blob/b5d32896b1f89fc44a82f8df3bb010934c53f4f5/README.md?plain=1#L66-L68) to run in non-eager mode. This covers all GPUs in our benchmarks - T4, V100, A10, A100 - except for P100 (see the [full list](https://developer.nvidia.com/cuda-gpus#compute)).", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Other optimizations\n\nIn addition, we have improved efficiency of GPU memory operations by eliminating some common pitfalls, e.g. creating a tensor on GPU directly rather than creating it on CPU and later moving to GPU. The places where such optimizations were necessary were determined by line-profiling and looking at CPU/GPU traces and [Flame Graphs](https://github.com/brendangregg/FlameGraph).", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Benchmarking setup and results summary\n\nWe have two versions of code to compare: _original_ and _optimized_. On top of this, several optimization features (xFormers, PyTorch memory efficient attention, compilation) can be turned on/off. Overall, as mentioned in the introduction, we will be benchmarking 5 configurations:", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* _Original code without xFormers_\n* _Original code with xFormers_\n* _Optimized code with vanilla math attention backend and no compilation_\n* _Optimized code with memory-efficient attention backend and no compilation_\n* _Optimized code with memory-efficient attention backend and compilation_", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "As the _original version_ we took the version of the code which uses PyTorch 1.12 and a custom implementation of attention. The _optimized version_ uses `nn.MultiheadAttention` in `CrossAttention` and PyTorch 2.0.0.dev20230111+cu117. It also has a few other minor optimizations in PyTorch-related code. \n\nThe table below shows runtime of each version of the code in seconds, and the percentage improvement compared to the _original with xFormers. _The compilation time is excluded.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "**Runtimes for batch size 1. In parenthesis - relative improvement with respect to the \u201cOriginal with xFormers\u201d row**", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 30.4s (-19.3%)\n 29.8s (-77.3%)\n 13.0s (-83.9%)\n 10.9s (-33.1%)\n 8.0s (-19.3%)\n
Original with xFormers\n 25.5s (0.0%)\n 16.8s (0.0%)\n 7.1s (0.0%)\n 8.2s (0.0%)\n 6.7s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 27.3s (-7.0%)\n 19.9s (-18.7%)\n 13.2s (-87.2%)\n 7.5s (8.7%)\n 5.7s (15.1%)\n
Optimized with mem. efficient attention, no compilation\n 26.5s (-3.8%)\n 16.8s (0.2%)\n 7.1s (-0.8%)\n 6.9s (16.0%)\n 5.3s (20.6%)\n
Optimized with mem. efficient attention and compilation\n -\n 16.4s (2.1%)\n 7.2s (-2.3%)\n 6.6s (18.6%)\n 4.1s (38.5%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
GPU\n Batch size 1\n Batch size 2\n Batch size 4\n
P100 (no compilation)\n -3.8\n 0.44\n 5.47\n
T4\n 2.12\n 10.51\n 14.2\n
A10\n -2.34\n 8.99\n 10.57\n
V100\n 18.63\n 6.39\n 10.43\n
A100\n 38.5\n 20.33\n 12.17\n
\n\n\nOne can notice the following:", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "* The improvements are significant for powerful GPUs like A100 and V100. For those GPUs the improvement is most pronounced for batch size 1\n* For less powerful GPUs we observe smaller speedups (or in two cases slight regressions). The batch size trend is reversed here: improvement is larger for larger batches\n\nIn the following sections we describe the applied optimizations and provide detailed benchmarking data, comparing the generation time with various optimization features on/off.\n\nSpecifically, we benchmark 5 configurations and the plots below compare their absolute performance for different GPUs and batch sizes. For definitions of these configurations see section \u201cBenchmarking setup and results\u201d.\n\n\n\n![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 1](/assets/images/2023-04-11-accelerated-generative-diffusion-models1.png){:style=\"max-height:800px; width:100%\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 2](/assets/images/2023-04-11-accelerated-generative-diffusion-models2.png){:style=\"max-height:800px; width:100%\"} \n\n![Benchmark of denoising diffusion text-to-image generation across GPU architectures, batch size 1](/assets/images/2023-04-11-accelerated-generative-diffusion-models3.png){:style=\"max-height:800px; width:100%\"} \n\n\n\t\t\t\n\n\n## Optimizations \n\nHere we\u2019ll go into more detail about the optimizations introduced into the model code. These optimizations rely on features of PyTorch 2.0 which has been released recently. \n\n\n### Optimized Attention", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "One part of the code which we optimized is the scaled dot-product attention. Attention is known to be a heavy operation: naive implementation materializes the attention matrix, leading to time and memory complexity quadratic in sequence length. It is common for diffusion models to use attention (`CrossAttention`) as part of Transformer blocks in multiple parts of the U-Net. Since the U-Net runs at every sampling step, this becomes a critical point to optimize. Instead of custom attention implementation one can use `torch.nn.MultiheadAttention,` which in PyTorch 2 has optimized attention implementation is integrated into it. This optimization schematically boils down to the following pseudocode:\n\n\n\n```\nclass CrossAttention(nn.Module):\n def __init__(self, ...):\n # Create matrices: Q, K, V, out_proj\n ...\n def forward(self, x, context=None, mask=None):\n # Compute out = SoftMax(Q*K/sqrt(d))V\n # Return out_proj(out)\n \u2026\n```\n\ngets replaced with", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "gets replaced with\n\n```\nclass CrossAttention(nn.Module):\n def __init__(self, ...):\n self.mha = nn.MultiheadAttention(...)\n def forward(self, x, context):\n\treturn self.mha(x, context, context)\n```\n\n\nThe optimized implementation of attention was available already in PyTorch 1.13 (see [here](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/)) and widely adopted (see e.g. [HuggingFace transformers library example](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)). In particular, it integrates memory-efficient attention from the [xFormers](https://github.com/facebookresearch/xformers) library and flash attention from [https://arxiv.org/abs/2205.14135](https://arxiv.org/abs/2205.14135). PyTorch 2.0 expands this to additional attention functions such as cross attention and custom kernels for further acceleration, making it applicable to diffusion models.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "Flash attention is available on GPUs with compute capability SM 7.5 or SM 8.x - for example, on T4, A10, and A100, which are included in our benchmark (you can check compute capability of each NVIDIA GPU [here](https://developer.nvidia.com/cuda-gpus#compute)). However, in our tests on A100 the memory efficient attention performed better than flash attention for the particular case of diffusion models, due to the small number of attention heads and small batch size. PyTorch understands this and in this case chooses memory efficient attention over flash attention when both are available (see the logic [here](https://github.com/pytorch/pytorch/blob/d8e795ecd53670682bd3b2e5ff1f378402b147d5/aten/src/ATen/native/transformers/cuda/sdp_utils.h#L33-L71)). For full control over the attention backends (memory-efficient attention, flash attention, \u201cvanilla math\u201d, or any future ones), power users can enable and disable them manually with the help of the context manager [torch.backends.cuda.sdp_kernel](https://pytorch.org/docs/master/backends.html#torch.backends.cuda.sdp_kernel).", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "### Compilation\n\nCompilation is a [new feature of PyTorch 2.0](https://pytorch.org/get-started/pytorch-2.0/#user-experience), enabling significant speedups with a very simple user experience. To invoke the default behavior, simply wrap a PyTorch module or a function into `torch.compile`:\n\n\n```\nmodel = torch.compile(model)\n```\n\n\nPyTorch compiler then turns Python code into a set of instructions which can be executed efficiently without Python overhead. The compilation happens dynamically the first time the code is executed. With the default behavior, under the hood PyTorch utilized [TorchDynamo](https://pytorch.org/docs/master/dynamo/index.html) to compile the code and [TorchInductor](https://dev-discuss.pytorch.org/t/torchinductor-a-pytorch-native-compiler-with-define-by-run-ir-and-symbolic-shapes/747) to further optimize it. See [this tutorial](https://pytorch.org/tutorials/intermediate/dynamo_tutorial.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "Although the one-liner above is enough for compilation, certain modifications in the code can squeeze a larger speedup. In particular, one should avoid so-called graph breaks - places in the code which PyTorch can\u2019t compile. As opposed to previous PyTorch compilation approaches (like TorchScript), PyTorch 2 compiler doesn\u2019t break in this case. Instead it falls back on eager execution - so the code runs, but with reduced performance. We introduced a few minor changes to the model code to get rid of graph breaks. This included eliminating functions from libraries not supported by the compiler, such as `inspect.isfunction` and `einops.rearrange`. See this [doc](https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break) to learn more about graph breaks and how to eliminate them.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "Theoretically, one can apply `torch.compile `on the whole diffusion sampling loop. However, in practice it is enough to just compile the U-Net. The reason is that `torch.compile` doesn\u2019t yet have a loop analyzer and would recompile the code for each iteration of the sampling loop. Moreover, compiled sampler code is likely to generate graph breaks - so one would need to adjust it if one wants to get a good performance from the compiled version.\n\nNote that compilation [requires GPU compute capability >= SM 7.0](https://github.com/openai/triton/blob/b5d32896b1f89fc44a82f8df3bb010934c53f4f5/README.md?plain=1#L66-L68) to run in non-eager mode. This covers all GPUs in our benchmarks - T4, V100, A10, A100 - except for P100 (see the [full list](https://developer.nvidia.com/cuda-gpus#compute)). \n\n\n### Other optimizations", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "In addition, we have improved efficiency of GPU memory operations by eliminating some common pitfalls, e.g. creating a tensor on GPU directly rather than creating it on CPU and later moving to GPU. The places where such optimizations were necessary were determined by line-profiling and looking at CPU/GPU traces and [Flame Graphs](https://github.com/brendangregg/FlameGraph).\n\n\n## Benchmarking setup and results summary\n\nWe have two versions of code to compare: _original_ and _optimized_. On top of this, several optimization features (xFormers, PyTorch memory efficient attention, compilation) can be turned on/off. Overall, as mentioned in the introduction, we will be benchmarking 5 configurations:\n\n\n\n* _Original code without xFormers_\n* _Original code with xFormers_\n* _Optimized code with vanilla math attention backend and no compilation_\n* _Optimized code with memory-efficient attention backend and no compilation_\n* _Optimized code with memory-efficient attention backend and compilation_", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "As the _original version_ we took the version of the code which uses PyTorch 1.12 and a custom implementation of attention. The _optimized version_ uses `nn.MultiheadAttention` in `CrossAttention` and PyTorch 2.0.0.dev20230111+cu117. It also has a few other minor optimizations in PyTorch-related code. \n\nThe table below shows runtime of each version of the code in seconds, and the percentage improvement compared to the _original with xFormers. _The compilation time is excluded.\n\n**Runtimes for batch size 1. In parenthesis - relative improvement with respect to the \u201cOriginal with xFormers\u201d row**", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 30.4s (-19.3%)\n 29.8s (-77.3%)\n 13.0s (-83.9%)\n 10.9s (-33.1%)\n 8.0s (-19.3%)\n
Original with xFormers\n 25.5s (0.0%)\n 16.8s (0.0%)\n 7.1s (0.0%)\n 8.2s (0.0%)\n 6.7s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 27.3s (-7.0%)\n 19.9s (-18.7%)\n 13.2s (-87.2%)\n 7.5s (8.7%)\n 5.7s (15.1%)\n
Optimized with mem. efficient attention, no compilation\n 26.5s (-3.8%)\n 16.8s (0.2%)\n 7.1s (-0.8%)\n 6.9s (16.0%)\n 5.3s (20.6%)\n
Optimized with mem. efficient attention and compilation\n -\n 16.4s (2.1%)\n 7.2s (-2.3%)\n 6.6s (18.6%)\n 4.1s (38.5%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} {"page_content": "**Runtimes for batch size 2**", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 58.0s (-21.6%)\n 57.6s (-84.0%)\n 24.4s (-95.2%)\n 18.6s (-63.0%)\n 12.0s (-50.6%)\n
Original with xFormers\n 47.7s (0.0%)\n 31.3s (0.0%)\n 12.5s (0.0%)\n 11.4s (0.0%)\n 8.0s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 49.3s (-3.5%)\n 37.9s (-21.0%)\n 17.8s (-42.2%)\n 12.7s (-10.7%)\n 7.8s (1.8%)\n
Optimized with mem. efficient attention, no compilation\n 47.5s (0.4%)\n 31.2s (0.5%)\n 12.2s (2.6%)\n 11.5s (-0.7%)\n 7.0s (12.6%)\n
Optimized with mem. efficient attention and compilation\n -\n 28.0s (10.5%)\n 11.4s (9.0%)\n 10.7s (6.4%)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "6.4s (20.3%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 58.0s (-21.6%)\n 57.6s (-84.0%)\n 24.4s (-95.2%)\n 18.6s (-63.0%)\n 12.0s (-50.6%)\n
Original with xFormers\n 47.7s (0.0%)\n 31.3s (0.0%)\n 12.5s (0.0%)\n 11.4s (0.0%)\n 8.0s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 49.3s (-3.5%)\n 37.9s (-21.0%)\n 17.8s (-42.2%)\n 12.7s (-10.7%)\n 7.8s (1.8%)\n
Optimized with mem. efficient attention, no compilation\n 47.5s (0.4%)\n 31.2s (0.5%)\n 12.2s (2.6%)\n 11.5s (-0.7%)\n 7.0s (12.6%)\n
Optimized with mem. efficient attention and compilation\n -\n 28.0s (10.5%)\n 11.4s (9.0%)\n 10.7s (6.4%)\n 6.4s (20.3%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} {"page_content": "**Runtimes for batch size 4**", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 117.9s (-20.0%)\n 112.4s (-81.8%)\n 47.2s (-101.7%)\n 35.8s (-71.9%)\n 22.8s (-78.9%)\n
Original with xFormers\n 98.3s (0.0%)\n 61.8s (0.0%)\n 23.4s (0.0%)\n 20.8s (0.0%)\n 12.7s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 101.1s (-2.9%)\n 73.0s (-18.0%)\n 28.3s (-21.0%)\n 23.3s (-11.9%)\n 14.5s (-13.9%)\n
Optimized with mem. efficient attention, no compilation\n 92.9s (5.5%)\n 61.1s (1.2%)\n 23.9s (-1.9%)\n 20.8s (-0.1%)\n 12.8s (-0.9%)\n
Optimized with mem. efficient attention and compilation\n -\n 53.1s (14.2%)\n 20.9s (10.6%)\n 18.6s (10.4%)\n 11.2s (12.2%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "To minimize fluctuations and external influence on the performance of the benchmarked code, we ran each version of the code one after another, and then repeated this sequence 10 times: A, B, C, D, E, A, B, \u2026 So the results of a typical run would look like the one in the picture below.. Note that one shouldn\u2019t rely on comparison of absolute run times between different graphs, but comparison of run times_ inside_ one graph is pretty reliable, thanks to our benchmarking setup.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "![Denoising diffusion model generation benchmarks](/assets/images/2023-04-11-accelerated-generative-diffusion-models4.png){:style=\"max-height:700px\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Each run of text-to-image generation script produces several batches, the number of which is regulated by the CLI parameter `--n_iter`. In the benchmarks we used `n_iter = 2`, but introduced an additional \u201cwarm-up\u201d iteration, which doesn\u2019t contribute to the run time. This was necessary for the runs with compilation, because compilation happens the first time the code runs, and so the first iteration is much longer than all subsequent. To make comparison fair, we also introduced this additional \u201cwarm-up\u201d", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "iteration to all other runs.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "The numbers in the table above are for number of iterations 2 (plus a \u201cwarm-up one\u201d), prompt \u201dA photo\u201d, seed 1, PLMS sampler, and autocast turned on.\n\nBenchmarks were done using P100, V100, A100, A10 and T4 GPUs. The T4 benchmarks were done in Google Colab Pro. The A10 benchmarks were done on g5.4xlarge AWS instances with 1 GPU.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Conclusions and next steps", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "We have shown that new features of PyTorch 2 - compiler and optimized attention implementation - give performance improvements exceeding or comparable with what previously required installation of an external dependency (xFormers). PyTorch achieved this, in particular, by integrating memory efficient attention from xFormers into its codebase. This is a significant improvement for user experience, given that xFormers, being a state-of-the-art library, in many scenarios requires custom installation process", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "and long builds.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "There are a few natural directions in which this work can be continued:", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* The optimizations we implemented and described here are only benchmarked for text-to-image inference so far. It would be interesting to see how they affect training performance. PyTorch compilation can be directly applied to training; enabling training with PyTorch optimized attention is on the roadmap\n* We intentionally minimized changes to the original model code. Further profiling and optimization can probably bring more improvements", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* At the moment compilation is applied only to the U-Net model inside the sampler. Since there is a lot happening outside of U-Net (e.g. operations directly in the sampling loop), it would be beneficial to compile the whole sampler. However, this would require analysis of the compilation process to avoid recompilation at every sampling step\n* Current code only applies compilation within the PLMS sampler, but it should be trivial to extend it to other samplers", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* Besides text-to-image generation, diffusion models are also applied to other tasks - image-to-image and inpainting. It would be interesting to measure how their performance improves from PyTorch 2 optimizations", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "See if you can increase performance of open source diffusion models using the methods we described, and share the results!", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Resources", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* PyTorch 2.0 overview, which has a lot of information on `torch.compile:` [https://pytorch.org/get-started/pytorch-2.0/](https://pytorch.org/get-started/pytorch-2.0/) \n* Tutorial on `torch.compile`: [https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html)\n* General compilation troubleshooting: [https://pytorch.org/docs/master/dynamo/troubleshooting.html](https://pytorch.org/docs/master/dynamo/troubleshooting.html)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* Details on graph breaks: [https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break](https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break)\n* Details on guards: [https://pytorch.org/docs/master/dynamo/guards-overview.html](https://pytorch.org/docs/master/dynamo/guards-overview.html)\n* Video deep dive on TorchDynamo [https://www.youtube.com/watch?v=egZB5Uxki0I](https://www.youtube.com/watch?v=egZB5Uxki0I)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "* Tutorial on optimized attention in PyTorch 1.12: [https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html](https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements\n\nWe would like to thank Geeta Chauhan, Natalia Gimelshein, Patrick Labatut, Bert Maher, Mark Saroufim, Michael Voznesensky and Francisco Massa for their valuable advice and early feedback on the text.\n\nSpecial thanks to Yudong Tao initiating the work on using PyTorch native attention in diffusion models.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"New Library Updates in PyTorch 2.0\"\n---", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Summary\n\nWe are bringing a number of improvements to the current PyTorch libraries, alongside the [PyTorch 2.0 release](/blog/pytorch-2.0-release/). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Along with 2.0, we are also releasing a series of beta updates to the PyTorch domain libraries, including those that are in-tree, and separate libraries including TorchAudio, TorchVision, and TorchText. An update for TorchX is also being released as it moves to community supported mode. Please find the list of the latest stable versions and updates below.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "**Latest Stable Library Versions (Full List)**\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
TorchArrow 0.1.0\n TorchRec 0.4.0\n TorchVision 0.15\n
TorchAudio 2.0\n TorchServe 0.7.1\n TorchX 0.4.0\n
TorchData 0.6.0\n TorchText 0.15.0\n PyTorch on XLA Devices 1.14\n
", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "*To see [prior versions](https://pytorch.org/docs/stable/index.html) or (unstable) nightlies, click on versions in the top left menu above \u2018Search Docs\u2019.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchAudio", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Data augmentation operators", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The release adds several data augmentation operators under torchaudio.functional and torchaudio.transforms:\n* torchaudio.functional.add_noise\n* torchaudio.functional.convolve\n* torchaudio.functional.deemphasis\n* torchaudio.functional.fftconvolve\n* torchaudio.functional.preemphasis\n* torchaudio.functional.speed\n* torchaudio.transforms.AddNoise\n* torchaudio.transforms.Convolve\n* torchaudio.transforms.Deemphasis\n* torchaudio.transforms.FFTConvolve\n* torchaudio.transforms.Preemphasis", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "* torchaudio.transforms.Speed\n* torchaudio.transforms.SpeedPerturbation", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "The operators can be used to synthetically diversify training data to improve the generalizability of downstream models.\n\nFor usage details, please refer to the [functional](https://pytorch.org/audio/2.0.0/functional.html) and [transform](https://pytorch.org/audio/2.0.0/transforms.html) documentation and [Audio Data Augmentation](https://pytorch.org/audio/2.0.0/tutorials/audio_data_augmentation_tutorial.html) tutorial.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] WavLM and XLS-R models\n\nThe release adds two self-supervised learning models for speech and audio.\n\n* [WavLM](https://ieeexplore.ieee.org/document/9814838) that is robust to noise and reverberation.\n* [XLS-R](https://arxiv.org/abs/2111.09296) that is trained on cross-lingual datasets.\n\nBesides the model architectures, torchaudio also supports corresponding pre-trained pipelines:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "* torchaudio.pipelines.WAVLM_BASE\n* torchaudio.pipelines.WAVLM_BASE_PLUS\n* torchaudio.pipelines.WAVLM_LARGE\n* torchaudio.pipelines.WAV2VEC_XLSR_300M\n* torchaudio.pipelines.WAV2VEC_XLSR_1B\n* torchaudio.pipelines.WAV2VEC_XLSR_2B\n\nFor usage details, please refer to the [factory function](https://pytorch.org/audio/2.0.0/generated/torchaudio.models.Wav2Vec2Model.html#factory-functions) and [pre-trained pipelines](https://pytorch.org/audio/2.0.0/pipelines.html#id3) documentation.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchRL \n\nThe initial release of torchrl includes several features that span across the entire RL domain. TorchRL can already be used in online, offline, multi-agent, multi-task and distributed RL settings, among others. See below:", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Environment wrappers and transforms\n\ntorchrl.envs includes several wrappers around common environment libraries. This allows users to swap one library with another without effort. These wrappers build an interface between these simulators and torchrl:\n\n* dm_control: \n* Gym\n* Brax\n* EnvPool\n* Jumanji\n* Habitat", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "It also comes with many commonly used transforms and vectorized environment utilities that allow for a fast execution across simulation libraries. Please refer to the [documentation](https://pytorch.org/rl/reference/envs.html) for more detail.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Datacollectors\n\nData collection in RL is made easy via the usage of single process or multiprocessed/distributed data collectors that execute the policy in the environment over a desired duration and deliver samples according to the user\u2019s needs. These can be found in torchrl.collectors and are documented [here](https://pytorch.org/rl/reference/collectors.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Objective modules\n\nSeveral objective functions are included in torchrl.objectives, among which: \n\n* A generic PPOLoss class and derived ClipPPOLoss and KLPPOLoss\n* SACLoss and DiscreteSACLoss\n* DDPGLoss\n* DQNLoss\n* REDQLoss\n* A2CLoss\n* TD3Loss\n* ReinforceLoss\n* Dreamer\n\nVectorized value function operators also appear in the library. Check the documentation [here](https://pytorch.org/rl/reference/objectives.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Models and exploration strategies\n\nWe provide multiple models, modules and exploration strategies. Get a detailed description in [the doc](https://pytorch.org/rl/reference/modules.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Composable replay buffer\n\nA composable replay buffer class is provided that can be used to store data in multiple contexts including single and multi-agent, on and off-policy and many more.. Components include:\n\n* Storages (list, physical or memory-based contiguous storages)\n* Samplers (Prioritized, sampler without repetition)\n* Writers\n* Possibility to add transforms\n\nReplay buffers and other data utilities are documented [here](https://pytorch.org/rl/reference/data.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Logging tools and trainer\n\nWe support multiple logging tools including tensorboard, wandb and mlflow.\n\nWe provide a generic Trainer class that allows for easy code recycling and checkpointing.\n\nThese features are documented [here](https://pytorch.org/rl/reference/trainers.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TensorDict\n\nTensorDict is a new data carrier for PyTorch.\n\n\n### [Beta] TensorDict: specialized dictionary for PyTorch\n\nTensorDict allows you to execute many common operations across batches of tensors carried by a single container. TensorDict supports many shape and device or storage operations, and can readily be used in distributed settings. Check the [documentation](https://pytorch-labs.github.io/tensordict/) to know more.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] @tensorclass: a dataclass for PyTorch\n\nLike TensorDict, [tensorclass](https://pytorch-labs.github.io/tensordict/reference/prototype.html) provides the opportunity to write dataclasses with built-in torch features such as shape or device operations.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] tensordict.nn: specialized modules for TensorDict\n\nThe [tensordict.nn module](https://pytorch-labs.github.io/tensordict/reference/nn.html) provides specialized nn.Module subclasses that make it easy to build arbitrarily complex graphs that can be executed with TensorDict inputs. It is compatible with the latest PyTorch features such as functorch, torch.fx and torch.compile.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchRec", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] KeyedJaggedTensor All-to-All Redesign and Input Dist Fusion\n\nWe observed performance regression due to a bottleneck in sparse data distribution for models that have multiple, large KJTs to redistribute.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "To combat this we altered the comms pattern to transport the minimum data required in the initial collective to support the collective calls for the actual KJT tensor data. This data sent in the initial collective, \u2018splits\u2019 means more data is transmitted over the comms stream overall, but the CPU is blocked for significantly shorter amounts of time leading to better overall QPS.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Furthermore, we altered the TorchRec train pipeline to group the initial collective calls for the splits together before launching the more expensive KJT tensor collective calls. This fusion minimizes the CPU blocked time as launching each subsequent input distribution is no longer dependent on the previous input distribution.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "With this feature, variable batch sizes are now natively supported across ranks. These features are documented [here](https://github.com/pytorch/torchrec/commit/d0d23bef8aef5a79a1061fbc842c97bb68b91463).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchVision", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Extending TorchVision\u2019s Transforms to Object Detection, Segmentation & Video tasks \n\nTorchVision is extending its Transforms API! Here is what\u2019s new:\n\n* You can use them not only for Image Classification but also for Object Detection, Instance & Semantic Segmentation and Video Classification.\n* You can use new functional transforms for transforming Videos, Bounding Boxes and Segmentation Masks.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "Learn more about these new transforms [from our docs](https://pytorch.org/vision/stable/auto_examples/), and submit any feedback in our [dedicated issue](https://github.com/pytorch/vision/issues/6753).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchText", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Adding scriptable T5 and Flan-T5 to the TorchText library with incremental decoding support!", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchText has added the T5 model architecture with pre-trained weights for both the [original T5 paper](https://arxiv.org/abs/1910.10683) and [Flan-T5](https://arxiv.org/abs/2210.11416). The model is fully torchscriptable and features an optimized [multiheaded attention implementation](https://pytorch.org/docs/master/generated/torch.ao.nn.quantizable.MultiheadAttention.html?highlight=multihead#torch.ao.nn.quantizable.MultiheadAttention). We include several examples of how to utilize the model including", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "summarization, classification, and translation.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "For more details, please refer to [our docs](https://pytorch.org/text/stable/models.html).", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "TorchX\n\nTorchX is moving to community supported mode. More details will be coming in at a later time.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Deprecation of CUDA 11.6 and Python 3.7 Support\"\n---\n\nFor the upcoming PyTorch 2.0 feature release (target March 2023), we will target CUDA 11.7 as the stable version and CUDA 11.8 as the experimental version of CUDA and Python >=3.8, <=3.11. \n\nIf you are still using or depending on CUDA 11.6 or Python 3.7 builds, we strongly recommend moving to at least CUDA 11.7 and Python 3.8, as it would be the minimum versions required for PyTorch 2.0.", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} -{"page_content": "**Please note that as of Feb 1, CUDA 11.6 and Python 3.7 are no longer included in the nightlies**\n\nPlease refer to the Release Compatibility Matrix for PyTorch releases:", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n
PyTorch Version\n Python\n Stable CUDA\n Experimental CUDA\n
2.0\n >=3.8, <=3.11\n CUDA 11.7, CUDNN 8.5.0.96\n CUDA 11.8, CUDNN 8.7.0.84\n
1.13\n >=3.7, <=3.10\n CUDA 11.6, CUDNN 8.3.2.44\n CUDA 11.7, CUDNN 8.5.0.96\n
1.12\n >=3.7, <=3.10\n CUDA 11.3, CUDNN 8.3.2.44\n CUDA 11.6, CUDNN 8.3.2.44\n
", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} -{"page_content": "As of 2/1/2023\n\nFor more information on PyTorch releases, updated compatibility matrix and release policies, please see (and bookmark) [Readme](https://github.com/pytorch/pytorch/blob/master/RELEASE.md#release-compatibility-matrix).", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'torchvision 0.3: segmentation, detection models, new datasets and more..'\nauthor: Francisco Massa\nredirect_from: /2019/05/23/torchvision03.html\n---", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "PyTorch domain libraries like torchvision provide convenient access to common datasets and models that can be used to quickly create a state-of-the-art baseline. Moreover, they also provide common abstractions to reduce boilerplate code that users might have to otherwise repeatedly write. The torchvision 0.3 release brings several new features including models for semantic segmentation, object detection, instance segmentation, and person keypoint detection, as well as custom C++ / CUDA ops specific to", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "computer vision.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "
\n \n
", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "New features include:\n\n**Reference training / evaluation scripts:** torchvision now provides, under the references/ folder, scripts for training and evaluation of the following tasks: classification, semantic segmentation, object detection, instance segmentation and person keypoint detection. These serve as a log of how to train a specific model and provide baseline training and evaluation scripts to quickly bootstrap research.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "**torchvision ops:** torchvision now contains custom C++ / CUDA operators. Those operators are specific to computer vision, and make it easier to build object detection models. These operators currently do not support PyTorch script mode, but support for it is planned for in the next release. Some of the ops supported include:", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "* roi_pool (and the module version RoIPool)\n* roi_align (and the module version RoIAlign)\n* nms, for non-maximum suppression of bounding boxes\n* box_iou, for computing the intersection over union metric between two sets of bounding boxes\n* box_area, for computing the area of a set of bounding boxes\n\nHere are a few examples on using torchvision ops:\n\n```python\nimport torch\nimport torchvision", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "# create 10 random boxes\nboxes = torch.rand(10, 4) * 100\n# they need to be in [x0, y0, x1, y1] format\nboxes[:, 2:] += boxes[:, :2]\n# create a random image\nimage = torch.rand(1, 3, 200, 200)\n# extract regions in `image` defined in `boxes`, rescaling\n# them to have a size of 3x3\npooled_regions = torchvision.ops.roi_align(image, [boxes], output_size=(3, 3))\n# check the size\nprint(pooled_regions.shape)\n# torch.Size([10, 3, 3, 3])", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "# or compute the intersection over union between\n# all pairs of boxes\nprint(torchvision.ops.box_iou(boxes, boxes).shape)\n# torch.Size([10, 10])", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "**New models and datasets:** torchvision now adds support for object detection, instance segmentation and person keypoint detection models. In addition, several popular datasets have been added. Note: The API is currently experimental and might change in future versions of torchvision. New models include:", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "Segmentation Models\n\nThe 0.3 release also contains models for dense pixelwise prediction on images.\nIt adds FCN and DeepLabV3 segmentation models, using a ResNet50 and ResNet101 backbones.\nPre-trained weights for ResNet101 backbone are available, and have been trained on a subset of COCO train2017, which contains the same 20 categories as those from Pascal VOC.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "The pre-trained models give the following results on the subset of COCO val2017 which contain the same 20 categories as those present in Pascal VOC:\n\nNetwork | mean IoU | global pixelwise acc\n-- | -- | --\nFCN ResNet101 | 63.7 | 91.9\nDeepLabV3 ResNet101 | 67.4 | 92.4", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "Detection Models\n\nNetwork | box AP | mask AP | keypoint AP\n-- | -- | -- | --\nFaster R-CNN ResNet-50 FPN trained on COCO | 37.0 | \u00a0 | \u00a0\nMask R-CNN ResNet-50 FPN trained on COCO | 37.9 | 34.6 | \u00a0\nKeypoint R-CNN ResNet-50 FPN trained on COCO | 54.6 | \u00a0 | 65.0\n\nThe implementations of the models for object detection, instance segmentation and keypoint detection are fast, specially during training.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "In the following table, we use 8 V100 GPUs, with CUDA 10.0 and CUDNN 7.4 to report the results. During training, we use a batch size of 2 per GPU, and during testing a batch size of 1 is used.\n\nFor test time, we report the time for the model evaluation and post-processing (including mask pasting in image), but not the time for computing the precision-recall.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "Network | train time (s / it) | test time (s / it) | memory (GB)\n-- | -- | -- | --\nFaster R-CNN ResNet-50 FPN | 0.2288 | 0.0590 | 5.2\nMask R-CNN ResNet-50 FPN | 0.2728 | 0.0903 | 5.4\nKeypoint R-CNN ResNet-50 FPN | 0.3789 | 0.1242 | 6.8\n\n\nYou can load and use pre-trained detection and segmentation models with a few lines of code\n\n```python\nimport torchvision", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)\n# set it to evaluation mode, as the model behaves differently\n# during training and during evaluation\nmodel.eval()\n\nimage = PIL.Image.open('/path/to/an/image.jpg')\nimage_tensor = torchvision.transforms.functional.to_tensor(image)", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "# pass a list of (potentially different sized) tensors\n# to the model, in 0-1 range. The model will take care of\n# batching them together and normalizing\noutput = model([image_tensor])\n# output is a list of dict, containing the postprocessed predictions\n```", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "Classification Models\n\nThe following classification models were added:\n\n* GoogLeNet (Inception v1)\n* MobileNet V2\n* ShuffleNet v2\n* ResNeXt-50 32x4d and ResNeXt-101 32x8d", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "Datasets\n\nThe following datasets were added:\n\n* Caltech101, Caltech256, and CelebA\n* ImageNet dataset (improving on ImageFolder, provides class-strings)\n* Semantic Boundaries Dataset\n* VisionDataset as a base class for all datasets\n\n\nIn addition, we've added more image transforms, general improvements and bug fixes, as well as improved documentation.", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "**See the full release notes [here](https://github.com/pytorch/vision/releases) as well as this getting started tutorial [on Google Colab here](https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb), which describes how to fine tune your own instance segmentation model on a custom dataset.**\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Prototype Features Now Available - APIs for Hardware Accelerated Mobile and ARM64 Builds'\nauthor: Team PyTorch\n---\n\nToday, we are announcing four PyTorch prototype features. The first three of these will enable Mobile machine-learning developers to execute models on the full set of hardware (HW) engines making up a system-on-chip (SOC). This gives developers options to optimize their model execution for unique performance, power, and system-level concurrency.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "These features include enabling execution on the following on-device HW engines:\n* DSP and NPUs using the Android Neural Networks API (NNAPI), developed in collaboration with Google\n* GPU execution on Android via Vulkan\n* GPU execution on iOS via Metal\n\nThis release also includes developer efficiency benefits with newly introduced support for ARM64 builds for Linux.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "Below, you\u2019ll find brief descriptions of each feature with the links to get you started. These features are available through our [nightly builds](https://pytorch.org/). Reach out to us on the [PyTorch Forums](https://discuss.pytorch.org/) for any comment or feedback. We would love to get your feedback on those and hear how you are using them!", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "NNAPI Support with Google Android", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "The Google Android and PyTorch teams collaborated to enable support for Android\u2019s Neural Networks API (NNAPI) via PyTorch Mobile. Developers can now unlock high-performance execution on Android phones as their machine-learning models will be able to access additional hardware blocks on the phone\u2019s system-on-chip. NNAPI allows Android apps to run computationally intensive neural networks on the most powerful and efficient parts of the chips that power mobile phones, including DSPs (Digital Signal Processors)", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "and NPUs (specialized Neural Processing Units). The API was introduced in Android 8 (Oreo) and significantly expanded in Android 10 and 11 to support a richer set of AI models. With this integration, developers can now seamlessly access NNAPI directly from PyTorch Mobile. This initial release includes fully-functional support for a core set of features and operators, and Google and Facebook will be working to expand capabilities in the coming months.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "**Links**\n* [Android Blog: Android Neural Networks API 1.3 and PyTorch Mobile support](https://android-developers.googleblog.com/2020/11/android-neural-networks-api-13.html)\n* [PyTorch Medium Blog: Support for Android NNAPI with PyTorch Mobile](http://bit.ly/android-nnapi-pytorch-mobile-announcement)", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "PyTorch Mobile GPU support", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "Inferencing on GPU can provide great performance on many models types, especially those utilizing high-precision floating-point math. Leveraging the GPU for ML model execution as those found in SOCs from Qualcomm, Mediatek, and Apple allows for CPU-offload, freeing up the Mobile CPU for non-ML use cases. This initial prototype level support provided for on device GPUs is via the Metal API specification for iOS, and the Vulkan API specification for Android. As this feature is in an early stage: performance", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "is not optimized and model coverage is limited. We expect this to improve significantly over the course of 2021 and would like to hear from you which models and devices you would like to see performance improvements on.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "**Links**\n* [Prototype source workflows](https://github.com/pytorch/tutorials/tree/master/prototype_source)", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "ARM64 Builds for Linux\n\nWe will now provide prototype level PyTorch builds for ARM64 devices on Linux. As we see more ARM usage in our community with platforms such as Raspberry Pis and Graviton(2) instances spanning both at the edge and on servers respectively. This feature is available through our [nightly builds](https://pytorch.org/).\n\nWe value your feedback on these features and look forward to collaborating with you to continuously improve them further!\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Enterprise Support Program Update\"\nauthor: Team PyTorch\nfeatured-img: \"\"\n---\n\nOn May 25, 2021, we announced the [PyTorch Enterprise Support Program](https://pytorch.org/blog/announcing-pytorch-enterprise/) (ESP) that enabled providers to develop and offer tailored enterprise-grade support to their customers.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} -{"page_content": "The program enabled Program certified service providers to develop and offer tailored enterprise-grade support to their customers through contribution of hotfixes and other improvements requested by PyTorch enterprise users who were developing models in production at scale for mission-critical applications. However, as we evaluate community feedback, we found ongoing ESP support was not necessary at this time and will immediately divert these resources to other areas to improve the user experience for the", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} -{"page_content": "entire community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} -{"page_content": "Today, we are removing the PyTorch long-term support (LTS 1.8.2) download link from the \u201cGet Started\u201d page from the \u201c[Start Locally](https://pytorch.org/get-started/locally/)\u201d download option in order to simplify the user experience. One can download PyTorch v1.8.2 in [previous versions](/get-started/previous-versions/#v182-with-lts-support). Please note that it is only supported for Python while it is being deprecated. If there are any updates to ESP/LTS, we will update future blogs.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nPlease reach out to [marketing@pytorch.org](mailto:marketing@pytorch.org) with any questions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'New Releases: PyTorch 1.2, torchtext 0.4, torchaudio 0.3, and torchvision 0.4'\nauthor: Team PyTorch\nredirect_from: /2019/08/06/pytorch_aug2019_releases.html\n---\n\nSince the release of PyTorch 1.0, we\u2019ve seen the community expand to add new tools, contribute to a growing set of models available in the PyTorch Hub, and continually increase usage in both research and production.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "From a core perspective, PyTorch has continued to add features to support both research and production usage, including the ability to bridge these two worlds via [TorchScript](https://pytorch.org/docs/stable/jit.html). Today, we are excited to announce that we have four new releases including PyTorch 1.2, torchvision 0.4, torchaudio 0.3, and torchtext 0.4. You can get started now with any of these releases at [pytorch.org](https://pytorch.org/get-started/locally/).\n\n# PyTorch 1.2", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "With PyTorch 1.2, the open source ML framework takes a major step forward for production usage with the addition of an improved and more polished TorchScript environment. These improvements make it even easier to ship production models, expand support for exporting ONNX formatted models, and enhance module level support for Transformers. In addition to these new features, [TensorBoard](https://pytorch.org/docs/stable/tensorboard.html) is now no longer experimental - you can simply type `from", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "torch.utils.tensorboard import SummaryWriter` to get started.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "TorchScript Improvements\n\nSince its release in PyTorch 1.0, TorchScript has provided a path to production for eager PyTorch models. The TorchScript compiler converts PyTorch models to a statically typed graph representation, opening up opportunities for\noptimization and execution in constrained environments where Python is not available. You can incrementally convert your model to TorchScript, mixing compiled code seamlessly with Python.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 1.2 significantly expands TorchScript's support for the subset of Python used in PyTorch models and delivers a new, easier-to-use API for compiling your models to TorchScript. See the [migration guide](https://pytorch.org/docs/master/jit.html#migrating-to-pytorch-1-2-recursive-scripting-api) for details. Below is an example usage of the new API:\n\n```python\nimport torch", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "class MyModule(torch.nn.Module):\n def __init__(self, N, M):\n super(MyModule, self).__init__()\n self.weight = torch.nn.Parameter(torch.rand(N, M))\n\n def forward(self, input):\n if input.sum() > 0:\n output = self.weight.mv(input)\n else:\n output = self.weight + input\n return output\n\n# Compile the model code to a static representation\nmy_script_module = torch.jit.script(MyModule(3, 4))", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "# Save the compiled code and model data so it can be loaded elsewhere\nmy_script_module.save(\"my_script_module.pt\")", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "To learn more, see our [Introduction to TorchScript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript.html) and [Loading a\nPyTorch Model in C++](https://pytorch.org/tutorials/advanced/cpp_export.html) tutorials.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Expanded ONNX Export", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "The [ONNX](http://onnx.ai/) community continues to grow with an open [governance structure](https://github.com/onnx/onnx/wiki/Expanded-ONNX-Steering-Committee-Announced!) and additional steering committee members, special interest groups (SIGs), and working groups (WGs). In collaboration with Microsoft, we\u2019ve added full support to export ONNX Opset versions 7(v1.2), 8(v1.3), 9(v1.4) and 10 (v1.5). We\u2019ve have also enhanced the constant folding pass to support Opset 10, the latest available version of ONNX.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "ScriptModule has also been improved including support for multiple outputs, tensor factories, and tuples as inputs and outputs. Additionally, users are now able to register their own symbolic to export custom ops, and specify the dynamic dimensions of inputs during export. Here is a summary of the all of the major improvements:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "* Support for multiple Opsets including the ability to export dropout, slice, flip, and interpolate in Opset 10.\n* Improvements to ScriptModule including support for multiple outputs, tensor factories, and tuples as inputs and outputs.\n* More than a dozen additional PyTorch operators supported including the ability to export a custom operator.\n* Many big fixes and test infra improvements.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "You can try out the latest tutorial [here](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html), contributed by @lara-hdr at Microsoft. A big thank you to the entire Microsoft team for all of their hard work to make this release happen!", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "nn.Transformer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "In PyTorch 1.2, we now include a standard [nn.Transformer](https://pytorch.org/docs/stable/nn.html?highlight=transformer#torch.nn.Transformer) module, based on the paper \u201c[Attention is All You Need](https://arxiv.org/abs/1706.03762)\u201d. The `nn.Transformer` module relies entirely on an [attention mechanism](https://pytorch.org/docs/stable/nn.html?highlight=nn%20multiheadattention#torch.nn.MultiheadAttention) to draw global dependencies between input and output. The individual components of the", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "`nn.Transformer` module are designed so they can be adopted independently. For example, the [nn.TransformerEncoder](https://pytorch.org/docs/stable/nn.html?highlight=nn%20transformerencoder#torch.nn.TransformerEncoder) can be used by itself, without the larger `nn.Transformer`. The new APIs include:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "* `nn.Transformer`\n* `nn.TransformerEncoder` and `nn.TransformerEncoderLayer`\n* `nn.TransformerDecoder` and `nn.TransformerDecoderLayer`\n\n
\n \n
\n\nSee the [Transformer Layers](https://pytorch.org/docs/stable/nn.html#transformer-layers) documentation for more information. See [here](https://github.com/pytorch/pytorch/releases) for the full PyTorch 1.2 release notes.\n\n# Domain API Library Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "PyTorch domain libraries like torchvision, torchtext, and torchaudio provide convenient access to common datasets, models, and transforms that can be used to quickly create a state-of-the-art baseline. Moreover, they also provide common abstractions to reduce boilerplate code that users might have to otherwise repeatedly write. Since research domains have distinct requirements, an ecosystem of specialized libraries called domain APIs (DAPI) has emerged around PyTorch to simplify the development of new and", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "existing algorithms in a number of fields. We\u2019re excited to release three updated DAPI libraries for text, audio, and vision that compliment the PyTorch 1.2 core release.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Torchaudio 0.3 with Kaldi Compatibility, New Transforms\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Torchaudio specializes in machine understanding of audio waveforms. It is an ML library that provides relevant signal processing functionality (but is not a general signal processing library). It leverages PyTorch\u2019s GPU support to provide many tools and transformations for waveforms to make data loading and standardization easier and more readable. For example, it offers data loaders for waveforms using sox, and transformations such as spectrograms, resampling, and mu-law encoding and decoding.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "We are happy to announce the availability of torchaudio 0.3.0, with a focus on standardization and complex numbers, a transformation (resample) and two new functionals (phase_vocoder, ISTFT), Kaldi compatibility, and a new tutorial. Torchaudio was redesigned to be an extension of PyTorch and a part of the domain APIs (DAPI) ecosystem.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Standardization\n\nSignificant effort in solving machine learning problems goes into data preparation. In this new release, we've updated torchaudio's interfaces for its transformations to standardize around the following vocabulary and conventions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Tensors are assumed to have channel as the first dimension and time as the last dimension (when applicable). This makes it consistent with PyTorch's dimensions. For size names, the prefix `n_` is used (e.g. \"a tensor of size (`n_freq`, `n_mel`)\") whereas dimension names do not have this prefix (e.g. \"a tensor of dimension (channel, time)\"). The input of all transforms and functions now assumes channel first. This is done to be consistent with PyTorch, which has channel followed by the number of samples. The", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "channel parameter of all transforms and functions is now deprecated.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "The output of `STFT` is (channel, frequency, time, 2), meaning for each channel, the columns are the Fourier transform of a certain window, so as we travel horizontally we can see each column (the Fourier transformed waveform) change over time. This matches the output of librosa so we no longer need to transpose in our test comparisons with `Spectrogram`, `MelScale`, `MelSpectrogram`, and `MFCC`. Moreover, because of these new conventions, we deprecated `LC2CL` and `BLC2CBL` which were used to transfer from", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "one shape of signal to another.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "As part of this release, we're also introducing support for complex numbers via tensors of dimension (..., 2), and providing `magphase` to convert such a tensor into its magnitude and phase, and similarly `complex_norm` and `angle`.\n\nThe details of the standardization are provided in the [README](https://github.com/pytorch/audio/blob/v0.3.0/README.md#Conventions).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Functionals, Transformations, and Kaldi Compatibility\n\nPrior to the standardization, we separated state and computation into `torchaudio.transforms` and `torchaudio.functional`.\n\nAs part of the transforms, we're adding a new transformation in 0.3.0: `Resample`. `Resample` can upsample or downsample a waveform to a different frequency.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "As part of the functionals, we're introducing: `phase_vocoder`, a phase vocoder to change the speed of a waveform without changing its pitch, and `ISTFT`, the inverse `STFT` implemented to be compatible with STFT provided by PyTorch. This separation allows us to make functionals weak scriptable and to utilize JIT in 0.3.0. We thus have JIT and CUDA support for the following transformations: `Spectrogram`, `AmplitudeToDB` (previously named `SpectrogramToDB`), `MelScale`,", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "`MelSpectrogram`, `MFCC`, `MuLawEncoding`, `MuLawDecoding` (previously named `MuLawExpanding`).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "We now also provide a compatibility interface with Kaldi to ease onboarding and reduce a user's code dependency on Kaldi. We now have an interface for `spectrogram`, `fbank`, and `resample_waveform`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "New Tutorial\n\nTo showcase the new conventions and transformations, we have a [new tutorial](https://pytorch.org/tutorials/beginner/audio_preprocessing_tutorial.html) demonstrating how to preprocess waveforms using torchaudio. This tutorial walks through an example of loading a waveform and applying some of the available transformations to it.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "We are excited to see an active community around torchaudio and eager to further grow and support it. We encourage you to go ahead and experiment for yourself with this tutorial and the two datasets that are available: VCTK and YESNO! They have an interface to download the datasets and preprocess them in a convenient format. You can find the details in the release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Torchtext 0.4 with supervised learning datasets", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "A key focus area of torchtext is to provide the fundamental elements to help accelerate NLP research. This includes easy access to commonly used datasets and basic preprocessing pipelines for working on raw text based data. The torchtext 0.4.0 release includes several popular supervised learning baselines with \"one-command\" data loading. A [tutorial](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) is included to show how to use the new datasets for text classification analysis.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "We also added and improved on a few functions such as [get_tokenizer](https://pytorch.org/text/data.html?highlight=get_tokenizer#torchtext.data.get_tokenizer) and [build_vocab_from_iterator](https://pytorch.org/text/vocab.html#build-vocab-from-iterator) to make it easier to implement future datasets. Additional examples can be found [here](https://github.com/pytorch/text/tree/master/examples/text_classification).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Text classification is an important task in Natural Language Processing with many applications, such as sentiment analysis. The new release includes several popular [text classification datasets](https://pytorch.org/text/datasets.html?highlight=textclassification#torchtext.datasets.TextClassificationDataset) for supervised learning including:\n\n* AG_NEWS\n* SogouNews\n* DBpedia\n* YelpReviewPolarity\n* YelpReviewFull\n* YahooAnswers\n* AmazonReviewPolarity\n* AmazonReviewFull", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Each dataset comes with two parts (train vs. test), and can be easily loaded with a single command. The datasets also support an ngrams feature to capture the partial information about the local word order. Take a look at the tutorial [here](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) to learn more about how to use the new datasets for supervised problems such as text classification analysis.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torchtext.datasets.text_classification import DATASETS\ntrain_dataset, test_dataset = DATASETS['AG_NEWS'](ngrams=2)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "In addition to the domain library, PyTorch provides many tools to make data loading easy. Users now can load and preprocess the text classification datasets with some well supported tools, like [torch.utils.data.DataLoader](https://pytorch.org/docs/stable/_modules/torch/utils/data/dataloader.html) and [torch.utils.data.IterableDataset](https://pytorch.org/docs/master/data.html#torch.utils.data.IterableDataset). Here are a few lines to wrap the data with DataLoader. More examples can be found", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "[here](https://github.com/pytorch/text/tree/master/examples/text_classification).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "```python\nfrom torch.utils.data import DataLoader\ndata = DataLoader(train_dataset, collate_fn=generate_batch)", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Check out the release notes [here](https://github.com/pytorch/text/releases) to learn more and try out the [tutorial here](http://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Torchvision 0.4 with Support for Video\n\nVideo is now a first-class citizen in torchvision, with support for data loading, datasets, pre-trained models, and transforms. The 0.4 release of torchvision includes:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "* Efficient IO primitives for reading/writing video files (including audio), with support for arbitrary encodings and formats.\n* Standard video datasets, compatible with `torch.utils.data.Dataset` and `torch.utils.data.DataLoader`.\n* Pre-trained models built on the Kinetics-400 dataset for action classification on videos (including the training scripts).\n* Reference training scripts for training your own video models.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "We wanted working with video data in PyTorch to be as straightforward as possible, without compromising too much on performance.\nAs such, we avoid the steps that would require re-encoding the videos beforehand, as it would involve:\n\n* A preprocessing step which duplicates the dataset in order to re-encode it.\n* An overhead in time and space because this re-encoding is time-consuming.\n* Generally, an external script should be used to perform the re-encoding.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Additionally, we provide APIs such as the utility class, `VideoClips`, that simplifies the task of enumerating all possible clips of fixed size in a list of video files by creating an index of all clips in a set of videos. It also allows you to specify a fixed frame-rate for the videos. An example of the API is provided below:\n\n```python\nfrom torchvision.datasets.video_utils import VideoClips", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "class MyVideoDataset(object):\n def __init__(self, video_paths):\n self.video_clips = VideoClips(video_paths,\n clip_length_in_frames=16,\n frames_between_clips=1,\n frame_rate=15)\n\n def __getitem__(self, idx):\n video, audio, info, video_idx = self.video_clips.get_clip(idx)\n return video, audio\n\n def __len__(self):\n return self.video_clips.num_clips()", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} -{"page_content": "Most of the user-facing API is in Python, similar to PyTorch, which makes it easily extensible. Plus, the underlying implementation is fast \u2014 torchvision decodes as little as possible from the video on-the-fly in order to return a clip from the video.\n\nCheck out the torchvision 0.4 [release notes here](https://github.com/pytorch/vision/releases) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Configuration\n P100\n T4\n A10\n V100\n A100\n
Original without xFormers\n 117.9s (-20.0%)\n 112.4s (-81.8%)\n 47.2s (-101.7%)\n 35.8s (-71.9%)\n 22.8s (-78.9%)\n
Original with xFormers\n 98.3s (0.0%)\n 61.8s (0.0%)\n 23.4s (0.0%)\n 20.8s (0.0%)\n 12.7s (0.0%)\n
Optimized with vanilla math attention, no compilation\n 101.1s (-2.9%)\n 73.0s (-18.0%)\n 28.3s (-21.0%)\n 23.3s (-11.9%)\n 14.5s (-13.9%)\n
Optimized with mem. efficient attention, no compilation\n 92.9s (5.5%)\n 61.1s (1.2%)\n 23.9s (-1.9%)\n 20.8s (-0.1%)\n 12.8s (-0.9%)\n
Optimized with mem. efficient attention and compilation\n -\n 53.1s (14.2%)\n 20.9s (10.6%)\n 18.6s (10.4%)\n 11.2s (12.2%)\n
", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "To minimize fluctuations and external influence on the performance of the benchmarked code, we ran each version of the code one after another, and then repeated this sequence 10 times: A, B, C, D, E, A, B, \u2026 So the results of a typical run would look like the one in the picture below.. Note that one shouldn\u2019t rely on comparison of absolute run times between different graphs, but comparison of run times_ inside_ one graph is pretty reliable, thanks to our benchmarking setup.\n\n\n\n\n![Denoising diffusion model generation benchmarks](/assets/images/2023-04-11-accelerated-generative-diffusion-models4.png){:style=\"max-height:700px\"}", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "Each run of text-to-image generation script produces several batches, the number of which is regulated by the CLI parameter `--n_iter`. In the benchmarks we used `n_iter = 2`, but introduced an additional \u201cwarm-up\u201d iteration, which doesn\u2019t contribute to the run time. This was necessary for the runs with compilation, because compilation happens the first time the code runs, and so the first iteration is much longer than all subsequent. To make comparison fair, we also introduced this additional \u201cwarm-up\u201d iteration to all other runs. \n\nThe numbers in the table above are for number of iterations 2 (plus a \u201cwarm-up one\u201d), prompt \u201dA photo\u201d, seed 1, PLMS sampler, and autocast turned on.\n\nBenchmarks were done using P100, V100, A100, A10 and T4 GPUs. The T4 benchmarks were done in Google Colab Pro. The A10 benchmarks were done on g5.4xlarge AWS instances with 1 GPU.\n\n\n## Conclusions and next steps", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "We have shown that new features of PyTorch 2 - compiler and optimized attention implementation - give performance improvements exceeding or comparable with what previously required installation of an external dependency (xFormers). PyTorch achieved this, in particular, by integrating memory efficient attention from xFormers into its codebase. This is a significant improvement for user experience, given that xFormers, being a state-of-the-art library, in many scenarios requires custom installation process and long builds.\n\nThere are a few natural directions in which this work can be continued:", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "* The optimizations we implemented and described here are only benchmarked for text-to-image inference so far. It would be interesting to see how they affect training performance. PyTorch compilation can be directly applied to training; enabling training with PyTorch optimized attention is on the roadmap\n* We intentionally minimized changes to the original model code. Further profiling and optimization can probably bring more improvements\n* At the moment compilation is applied only to the U-Net model inside the sampler. Since there is a lot happening outside of U-Net (e.g. operations directly in the sampling loop), it would be beneficial to compile the whole sampler. However, this would require analysis of the compilation process to avoid recompilation at every sampling step\n* Current code only applies compilation within the PLMS sampler, but it should be trivial to extend it to other samplers\n* Besides text-to-image generation, diffusion models are also applied to other tasks - image-to-image and inpainting. It would be interesting to measure how their performance improves from PyTorch 2 optimizations", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "See if you can increase performance of open source diffusion models using the methods we described, and share the results! \n\n\n## Resources", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "* PyTorch 2.0 overview, which has a lot of information on `torch.compile:` [https://pytorch.org/get-started/pytorch-2.0/](https://pytorch.org/get-started/pytorch-2.0/) \n* Tutorial on `torch.compile`: [https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html)\n* General compilation troubleshooting: [https://pytorch.org/docs/master/dynamo/troubleshooting.html](https://pytorch.org/docs/master/dynamo/troubleshooting.html)\n* Details on graph breaks: [https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break](https://pytorch.org/docs/master/dynamo/faq.html#identifying-the-cause-of-a-graph-break)\n* Details on guards: [https://pytorch.org/docs/master/dynamo/guards-overview.html](https://pytorch.org/docs/master/dynamo/guards-overview.html)\n* Video deep dive on TorchDynamo [https://www.youtube.com/watch?v=egZB5Uxki0I](https://www.youtube.com/watch?v=egZB5Uxki0I) \n* Tutorial on optimized attention in PyTorch 1.12: [https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html](https://pytorch.org/tutorials/beginner/bettertransformer_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "## Acknowledgements\n\nWe would like to thank Geeta Chauhan, Natalia Gimelshein, Patrick Labatut, Bert Maher, Mark Saroufim, Michael Voznesensky and Francisco Massa for their valuable advice and early feedback on the text.\n\nSpecial thanks to Yudong Tao initiating the work on using PyTorch native attention in diffusion models.", "metadata": {"source": "https://pytorch.org/blog/accelerated-generative-diffusion-models/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"New Library Updates in PyTorch 2.0\"\n---\n\n## Summary\n\nWe are bringing a number of improvements to the current PyTorch libraries, alongside the [PyTorch 2.0 release](/blog/pytorch-2.0-release/). These updates demonstrate our focus on developing common and extensible APIs across all domains to make it easier for our community to build ecosystem projects on PyTorch. \n\nAlong with 2.0, we are also releasing a series of beta updates to the PyTorch domain libraries, including those that are in-tree, and separate libraries including TorchAudio, TorchVision, and TorchText. An update for TorchX is also being released as it moves to community supported mode. Please find the list of the latest stable versions and updates below.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "**Latest Stable Library Versions (Full List)**\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
TorchArrow 0.1.0\n TorchRec 0.4.0\n TorchVision 0.15\n
TorchAudio 2.0\n TorchServe 0.7.1\n TorchX 0.4.0\n
TorchData 0.6.0\n TorchText 0.15.0\n PyTorch on XLA Devices 1.14\n
\n\n\n*To see [prior versions](https://pytorch.org/docs/stable/index.html) or (unstable) nightlies, click on versions in the top left menu above \u2018Search Docs\u2019.\n\n\n## TorchAudio \n\n### [Beta] Data augmentation operators", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "The release adds several data augmentation operators under torchaudio.functional and torchaudio.transforms:\n* torchaudio.functional.add_noise\n* torchaudio.functional.convolve\n* torchaudio.functional.deemphasis\n* torchaudio.functional.fftconvolve\n* torchaudio.functional.preemphasis\n* torchaudio.functional.speed\n* torchaudio.transforms.AddNoise\n* torchaudio.transforms.Convolve\n* torchaudio.transforms.Deemphasis\n* torchaudio.transforms.FFTConvolve\n* torchaudio.transforms.Preemphasis\n* torchaudio.transforms.Speed\n* torchaudio.transforms.SpeedPerturbation\n\nThe operators can be used to synthetically diversify training data to improve the generalizability of downstream models.\n\nFor usage details, please refer to the [functional](https://pytorch.org/audio/2.0.0/functional.html) and [transform](https://pytorch.org/audio/2.0.0/transforms.html) documentation and [Audio Data Augmentation](https://pytorch.org/audio/2.0.0/tutorials/audio_data_augmentation_tutorial.html) tutorial.\n\n\n### [Beta] WavLM and XLS-R models", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "The release adds two self-supervised learning models for speech and audio.\n\n* [WavLM](https://ieeexplore.ieee.org/document/9814838) that is robust to noise and reverberation.\n* [XLS-R](https://arxiv.org/abs/2111.09296) that is trained on cross-lingual datasets.\n\nBesides the model architectures, torchaudio also supports corresponding pre-trained pipelines:\n\n* torchaudio.pipelines.WAVLM_BASE\n* torchaudio.pipelines.WAVLM_BASE_PLUS\n* torchaudio.pipelines.WAVLM_LARGE\n* torchaudio.pipelines.WAV2VEC_XLSR_300M\n* torchaudio.pipelines.WAV2VEC_XLSR_1B\n* torchaudio.pipelines.WAV2VEC_XLSR_2B\n\nFor usage details, please refer to the [factory function](https://pytorch.org/audio/2.0.0/generated/torchaudio.models.Wav2Vec2Model.html#factory-functions) and [pre-trained pipelines](https://pytorch.org/audio/2.0.0/pipelines.html#id3) documentation.\n\n\n## TorchRL", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "## TorchRL \n\nThe initial release of torchrl includes several features that span across the entire RL domain. TorchRL can already be used in online, offline, multi-agent, multi-task and distributed RL settings, among others. See below:\n\n\n### [Beta] Environment wrappers and transforms\n\ntorchrl.envs includes several wrappers around common environment libraries. This allows users to swap one library with another without effort. These wrappers build an interface between these simulators and torchrl:\n\n* dm_control: \n* Gym\n* Brax\n* EnvPool\n* Jumanji\n* Habitat\n\nIt also comes with many commonly used transforms and vectorized environment utilities that allow for a fast execution across simulation libraries. Please refer to the [documentation](https://pytorch.org/rl/reference/envs.html) for more detail.\n\n\n### [Beta] Datacollectors", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "Data collection in RL is made easy via the usage of single process or multiprocessed/distributed data collectors that execute the policy in the environment over a desired duration and deliver samples according to the user\u2019s needs. These can be found in torchrl.collectors and are documented [here](https://pytorch.org/rl/reference/collectors.html).\n\n\n### [Beta] Objective modules\n\nSeveral objective functions are included in torchrl.objectives, among which: \n\n* A generic PPOLoss class and derived ClipPPOLoss and KLPPOLoss\n* SACLoss and DiscreteSACLoss\n* DDPGLoss\n* DQNLoss\n* REDQLoss\n* A2CLoss\n* TD3Loss\n* ReinforceLoss\n* Dreamer\n\nVectorized value function operators also appear in the library. Check the documentation [here](https://pytorch.org/rl/reference/objectives.html).\n\n\n### [Beta] Models and exploration strategies\n\nWe provide multiple models, modules and exploration strategies. Get a detailed description in [the doc](https://pytorch.org/rl/reference/modules.html).\n\n\n### [Beta] Composable replay buffer", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "A composable replay buffer class is provided that can be used to store data in multiple contexts including single and multi-agent, on and off-policy and many more.. Components include:\n\n* Storages (list, physical or memory-based contiguous storages)\n* Samplers (Prioritized, sampler without repetition)\n* Writers\n* Possibility to add transforms\n\nReplay buffers and other data utilities are documented [here](https://pytorch.org/rl/reference/data.html).\n\n\n### [Beta] Logging tools and trainer\n\nWe support multiple logging tools including tensorboard, wandb and mlflow.\n\nWe provide a generic Trainer class that allows for easy code recycling and checkpointing.\n\nThese features are documented [here](https://pytorch.org/rl/reference/trainers.html).\n\n\n## TensorDict\n\nTensorDict is a new data carrier for PyTorch.\n\n\n### [Beta] TensorDict: specialized dictionary for PyTorch", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "TensorDict allows you to execute many common operations across batches of tensors carried by a single container. TensorDict supports many shape and device or storage operations, and can readily be used in distributed settings. Check the [documentation](https://pytorch-labs.github.io/tensordict/) to know more.\n\n\n### [Beta] @tensorclass: a dataclass for PyTorch\n\nLike TensorDict, [tensorclass](https://pytorch-labs.github.io/tensordict/reference/prototype.html) provides the opportunity to write dataclasses with built-in torch features such as shape or device operations. \n\n\n### [Beta] tensordict.nn: specialized modules for TensorDict\n\nThe [tensordict.nn module](https://pytorch-labs.github.io/tensordict/reference/nn.html) provides specialized nn.Module subclasses that make it easy to build arbitrarily complex graphs that can be executed with TensorDict inputs. It is compatible with the latest PyTorch features such as functorch, torch.fx and torch.compile.\n\n\n## TorchRec", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "## TorchRec\n\n\n### [Beta] KeyedJaggedTensor All-to-All Redesign and Input Dist Fusion\n\nWe observed performance regression due to a bottleneck in sparse data distribution for models that have multiple, large KJTs to redistribute. \n\nTo combat this we altered the comms pattern to transport the minimum data required in the initial collective to support the collective calls for the actual KJT tensor data. This data sent in the initial collective, \u2018splits\u2019 means more data is transmitted over the comms stream overall, but the CPU is blocked for significantly shorter amounts of time leading to better overall QPS.\n\nFurthermore, we altered the TorchRec train pipeline to group the initial collective calls for the splits together before launching the more expensive KJT tensor collective calls. This fusion minimizes the CPU blocked time as launching each subsequent input distribution is no longer dependent on the previous input distribution.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "With this feature, variable batch sizes are now natively supported across ranks. These features are documented [here](https://github.com/pytorch/torchrec/commit/d0d23bef8aef5a79a1061fbc842c97bb68b91463).\n\n\n## TorchVision \n\n\n### [Beta] Extending TorchVision\u2019s Transforms to Object Detection, Segmentation & Video tasks \n\nTorchVision is extending its Transforms API! Here is what\u2019s new:\n\n* You can use them not only for Image Classification but also for Object Detection, Instance & Semantic Segmentation and Video Classification.\n* You can use new functional transforms for transforming Videos, Bounding Boxes and Segmentation Masks.\n\nLearn more about these new transforms [from our docs](https://pytorch.org/vision/stable/auto_examples/), and submit any feedback in our [dedicated issue](https://github.com/pytorch/vision/issues/6753).\n\n\n## TorchText \n\n### [Beta] Adding scriptable T5 and Flan-T5 to the TorchText library with incremental decoding support!", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "TorchText has added the T5 model architecture with pre-trained weights for both the [original T5 paper](https://arxiv.org/abs/1910.10683) and [Flan-T5](https://arxiv.org/abs/2210.11416). The model is fully torchscriptable and features an optimized [multiheaded attention implementation](https://pytorch.org/docs/master/generated/torch.ao.nn.quantizable.MultiheadAttention.html?highlight=multihead#torch.ao.nn.quantizable.MultiheadAttention). We include several examples of how to utilize the model including summarization, classification, and translation.\n\nFor more details, please refer to [our docs](https://pytorch.org/text/stable/models.html).\n\n\n## TorchX\n\nTorchX is moving to community supported mode. More details will be coming in at a later time.", "metadata": {"source": "https://pytorch.org/blog/new-library-updates-in-pytorch-2.0/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Deprecation of CUDA 11.6 and Python 3.7 Support\"\n---\n\nFor the upcoming PyTorch 2.0 feature release (target March 2023), we will target CUDA 11.7 as the stable version and CUDA 11.8 as the experimental version of CUDA and Python >=3.8, <=3.11. \n\nIf you are still using or depending on CUDA 11.6 or Python 3.7 builds, we strongly recommend moving to at least CUDA 11.7 and Python 3.8, as it would be the minimum versions required for PyTorch 2.0.\n\n**Please note that as of Feb 1, CUDA 11.6 and Python 3.7 are no longer included in the nightlies**\n\nPlease refer to the Release Compatibility Matrix for PyTorch releases:", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
PyTorch Version\n Python\n Stable CUDA\n Experimental CUDA\n
2.0\n >=3.8, <=3.11\n CUDA 11.7, CUDNN 8.5.0.96\n CUDA 11.8, CUDNN 8.7.0.84\n
1.13\n >=3.7, <=3.10\n CUDA 11.6, CUDNN 8.3.2.44\n CUDA 11.7, CUDNN 8.5.0.96\n
1.12\n >=3.7, <=3.10\n CUDA 11.3, CUDNN 8.3.2.44\n CUDA 11.6, CUDNN 8.3.2.44\n
\n\n\nAs of 2/1/2023\n\nFor more information on PyTorch releases, updated compatibility matrix and release policies, please see (and bookmark) [Readme](https://github.com/pytorch/pytorch/blob/master/RELEASE.md#release-compatibility-matrix).", "metadata": {"source": "https://pytorch.org/blog/deprecation-cuda-python-support/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'torchvision 0.3: segmentation, detection models, new datasets and more..'\nauthor: Francisco Massa\nredirect_from: /2019/05/23/torchvision03.html\n---\n\nPyTorch domain libraries like torchvision provide convenient access to common datasets and models that can be used to quickly create a state-of-the-art baseline. Moreover, they also provide common abstractions to reduce boilerplate code that users might have to otherwise repeatedly write. The torchvision 0.3 release brings several new features including models for semantic segmentation, object detection, instance segmentation, and person keypoint detection, as well as custom C++ / CUDA ops specific to computer vision.\n\n
\n \n
\n\n\n### New features include:", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "**Reference training / evaluation scripts:** torchvision now provides, under the references/ folder, scripts for training and evaluation of the following tasks: classification, semantic segmentation, object detection, instance segmentation and person keypoint detection. These serve as a log of how to train a specific model and provide baseline training and evaluation scripts to quickly bootstrap research.\n\n**torchvision ops:** torchvision now contains custom C++ / CUDA operators. Those operators are specific to computer vision, and make it easier to build object detection models. These operators currently do not support PyTorch script mode, but support for it is planned for in the next release. Some of the ops supported include:", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "* roi_pool (and the module version RoIPool)\n* roi_align (and the module version RoIAlign)\n* nms, for non-maximum suppression of bounding boxes\n* box_iou, for computing the intersection over union metric between two sets of bounding boxes\n* box_area, for computing the area of a set of bounding boxes\n\nHere are a few examples on using torchvision ops:\n\n```python\nimport torch\nimport torchvision\n\n# create 10 random boxes\nboxes = torch.rand(10, 4) * 100\n# they need to be in [x0, y0, x1, y1] format\nboxes[:, 2:] += boxes[:, :2]\n# create a random image\nimage = torch.rand(1, 3, 200, 200)\n# extract regions in `image` defined in `boxes`, rescaling\n# them to have a size of 3x3\npooled_regions = torchvision.ops.roi_align(image, [boxes], output_size=(3, 3))\n# check the size\nprint(pooled_regions.shape)\n# torch.Size([10, 3, 3, 3])\n\n# or compute the intersection over union between\n# all pairs of boxes\nprint(torchvision.ops.box_iou(boxes, boxes).shape)\n# torch.Size([10, 10])\n```", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "**New models and datasets:** torchvision now adds support for object detection, instance segmentation and person keypoint detection models. In addition, several popular datasets have been added. Note: The API is currently experimental and might change in future versions of torchvision. New models include:\n\n### Segmentation Models\n\nThe 0.3 release also contains models for dense pixelwise prediction on images.\nIt adds FCN and DeepLabV3 segmentation models, using a ResNet50 and ResNet101 backbones.\nPre-trained weights for ResNet101 backbone are available, and have been trained on a subset of COCO train2017, which contains the same 20 categories as those from Pascal VOC.\n\nThe pre-trained models give the following results on the subset of COCO val2017 which contain the same 20 categories as those present in Pascal VOC:\n\nNetwork | mean IoU | global pixelwise acc\n-- | -- | --\nFCN ResNet101 | 63.7 | 91.9\nDeepLabV3 ResNet101 | 67.4 | 92.4\n\n### Detection Models", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "### Detection Models\n\nNetwork | box AP | mask AP | keypoint AP\n-- | -- | -- | --\nFaster R-CNN ResNet-50 FPN trained on COCO | 37.0 | \u00a0 | \u00a0\nMask R-CNN ResNet-50 FPN trained on COCO | 37.9 | 34.6 | \u00a0\nKeypoint R-CNN ResNet-50 FPN trained on COCO | 54.6 | \u00a0 | 65.0\n\nThe implementations of the models for object detection, instance segmentation and keypoint detection are fast, specially during training.\n\nIn the following table, we use 8 V100 GPUs, with CUDA 10.0 and CUDNN 7.4 to report the results. During training, we use a batch size of 2 per GPU, and during testing a batch size of 1 is used.\n\nFor test time, we report the time for the model evaluation and post-processing (including mask pasting in image), but not the time for computing the precision-recall.\n\nNetwork | train time (s / it) | test time (s / it) | memory (GB)\n-- | -- | -- | --\nFaster R-CNN ResNet-50 FPN | 0.2288 | 0.0590 | 5.2\nMask R-CNN ResNet-50 FPN | 0.2728 | 0.0903 | 5.4\nKeypoint R-CNN ResNet-50 FPN | 0.3789 | 0.1242 | 6.8", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "You can load and use pre-trained detection and segmentation models with a few lines of code\n\n```python\nimport torchvision\n\nmodel = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)\n# set it to evaluation mode, as the model behaves differently\n# during training and during evaluation\nmodel.eval()\n\nimage = PIL.Image.open('/path/to/an/image.jpg')\nimage_tensor = torchvision.transforms.functional.to_tensor(image)\n\n# pass a list of (potentially different sized) tensors\n# to the model, in 0-1 range. The model will take care of\n# batching them together and normalizing\noutput = model([image_tensor])\n# output is a list of dict, containing the postprocessed predictions\n```\n\n### Classification Models\n\nThe following classification models were added:\n\n* GoogLeNet (Inception v1)\n* MobileNet V2\n* ShuffleNet v2\n* ResNeXt-50 32x4d and ResNeXt-101 32x8d\n\n### Datasets\n\nThe following datasets were added:", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "* Caltech101, Caltech256, and CelebA\n* ImageNet dataset (improving on ImageFolder, provides class-strings)\n* Semantic Boundaries Dataset\n* VisionDataset as a base class for all datasets\n\n\nIn addition, we've added more image transforms, general improvements and bug fixes, as well as improved documentation.\n\n**See the full release notes [here](https://github.com/pytorch/vision/releases) as well as this getting started tutorial [on Google Colab here](https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb), which describes how to fine tune your own instance segmentation model on a custom dataset.**\n\nCheers!\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/torchvision03/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Prototype Features Now Available - APIs for Hardware Accelerated Mobile and ARM64 Builds'\nauthor: Team PyTorch\n---\n\nToday, we are announcing four PyTorch prototype features. The first three of these will enable Mobile machine-learning developers to execute models on the full set of hardware (HW) engines making up a system-on-chip (SOC). This gives developers options to optimize their model execution for unique performance, power, and system-level concurrency.\n\nThese features include enabling execution on the following on-device HW engines:\n* DSP and NPUs using the Android Neural Networks API (NNAPI), developed in collaboration with Google\n* GPU execution on Android via Vulkan\n* GPU execution on iOS via Metal\n\nThis release also includes developer efficiency benefits with newly introduced support for ARM64 builds for Linux.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "Below, you\u2019ll find brief descriptions of each feature with the links to get you started. These features are available through our [nightly builds](https://pytorch.org/). Reach out to us on the [PyTorch Forums](https://discuss.pytorch.org/) for any comment or feedback. We would love to get your feedback on those and hear how you are using them!\n\n## NNAPI Support with Google Android", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "The Google Android and PyTorch teams collaborated to enable support for Android\u2019s Neural Networks API (NNAPI) via PyTorch Mobile. Developers can now unlock high-performance execution on Android phones as their machine-learning models will be able to access additional hardware blocks on the phone\u2019s system-on-chip. NNAPI allows Android apps to run computationally intensive neural networks on the most powerful and efficient parts of the chips that power mobile phones, including DSPs (Digital Signal Processors) and NPUs (specialized Neural Processing Units). The API was introduced in Android 8 (Oreo) and significantly expanded in Android 10 and 11 to support a richer set of AI models. With this integration, developers can now seamlessly access NNAPI directly from PyTorch Mobile. This initial release includes fully-functional support for a core set of features and operators, and Google and Facebook will be working to expand capabilities in the coming months.", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "**Links**\n* [Android Blog: Android Neural Networks API 1.3 and PyTorch Mobile support](https://android-developers.googleblog.com/2020/11/android-neural-networks-api-13.html)\n* [PyTorch Medium Blog: Support for Android NNAPI with PyTorch Mobile](http://bit.ly/android-nnapi-pytorch-mobile-announcement)\n\n## PyTorch Mobile GPU support", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "Inferencing on GPU can provide great performance on many models types, especially those utilizing high-precision floating-point math. Leveraging the GPU for ML model execution as those found in SOCs from Qualcomm, Mediatek, and Apple allows for CPU-offload, freeing up the Mobile CPU for non-ML use cases. This initial prototype level support provided for on device GPUs is via the Metal API specification for iOS, and the Vulkan API specification for Android. As this feature is in an early stage: performance is not optimized and model coverage is limited. We expect this to improve significantly over the course of 2021 and would like to hear from you which models and devices you would like to see performance improvements on.\n\n**Links**\n* [Prototype source workflows](https://github.com/pytorch/tutorials/tree/master/prototype_source)\n\n## ARM64 Builds for Linux", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "We will now provide prototype level PyTorch builds for ARM64 devices on Linux. As we see more ARM usage in our community with platforms such as Raspberry Pis and Graviton(2) instances spanning both at the edge and on servers respectively. This feature is available through our [nightly builds](https://pytorch.org/).\n\nWe value your feedback on these features and look forward to collaborating with you to continuously improve them further!\n\nThank you,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch Enterprise Support Program Update\"\nauthor: Team PyTorch\nfeatured-img: \"\"\n---\n\nOn May 25, 2021, we announced the [PyTorch Enterprise Support Program](https://pytorch.org/blog/announcing-pytorch-enterprise/) (ESP) that enabled providers to develop and offer tailored enterprise-grade support to their customers.\n\nThe program enabled Program certified service providers to develop and offer tailored enterprise-grade support to their customers through contribution of hotfixes and other improvements requested by PyTorch enterprise users who were developing models in production at scale for mission-critical applications. However, as we evaluate community feedback, we found ongoing ESP support was not necessary at this time and will immediately divert these resources to other areas to improve the user experience for the entire community.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} +{"page_content": "Today, we are removing the PyTorch long-term support (LTS 1.8.2) download link from the \u201cGet Started\u201d page from the \u201c[Start Locally](https://pytorch.org/get-started/locally/)\u201d download option in order to simplify the user experience. One can download PyTorch v1.8.2 in [previous versions](/get-started/previous-versions/#v182-with-lts-support). Please note that it is only supported for Python while it is being deprecated. If there are any updates to ESP/LTS, we will update future blogs.\n\n

\n \n

\n\nPlease reach out to [marketing@pytorch.org](mailto:marketing@pytorch.org) with any questions.", "metadata": {"source": "https://pytorch.org/blog/pytorch-enterprise-support-update/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'New Releases: PyTorch 1.2, torchtext 0.4, torchaudio 0.3, and torchvision 0.4'\nauthor: Team PyTorch\nredirect_from: /2019/08/06/pytorch_aug2019_releases.html\n---\n\nSince the release of PyTorch 1.0, we\u2019ve seen the community expand to add new tools, contribute to a growing set of models available in the PyTorch Hub, and continually increase usage in both research and production.\n\nFrom a core perspective, PyTorch has continued to add features to support both research and production usage, including the ability to bridge these two worlds via [TorchScript](https://pytorch.org/docs/stable/jit.html). Today, we are excited to announce that we have four new releases including PyTorch 1.2, torchvision 0.4, torchaudio 0.3, and torchtext 0.4. You can get started now with any of these releases at [pytorch.org](https://pytorch.org/get-started/locally/).\n\n# PyTorch 1.2", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "# PyTorch 1.2\n\nWith PyTorch 1.2, the open source ML framework takes a major step forward for production usage with the addition of an improved and more polished TorchScript environment. These improvements make it even easier to ship production models, expand support for exporting ONNX formatted models, and enhance module level support for Transformers. In addition to these new features, [TensorBoard](https://pytorch.org/docs/stable/tensorboard.html) is now no longer experimental - you can simply type `from torch.utils.tensorboard import SummaryWriter` to get started.\n\n## TorchScript Improvements\n\nSince its release in PyTorch 1.0, TorchScript has provided a path to production for eager PyTorch models. The TorchScript compiler converts PyTorch models to a statically typed graph representation, opening up opportunities for\noptimization and execution in constrained environments where Python is not available. You can incrementally convert your model to TorchScript, mixing compiled code seamlessly with Python.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 1.2 significantly expands TorchScript's support for the subset of Python used in PyTorch models and delivers a new, easier-to-use API for compiling your models to TorchScript. See the [migration guide](https://pytorch.org/docs/master/jit.html#migrating-to-pytorch-1-2-recursive-scripting-api) for details. Below is an example usage of the new API:\n\n```python\nimport torch\n\nclass MyModule(torch.nn.Module):\n def __init__(self, N, M):\n super(MyModule, self).__init__()\n self.weight = torch.nn.Parameter(torch.rand(N, M))\n\n def forward(self, input):\n if input.sum() > 0:\n output = self.weight.mv(input)\n else:\n output = self.weight + input\n return output\n\n# Compile the model code to a static representation\nmy_script_module = torch.jit.script(MyModule(3, 4))\n\n# Save the compiled code and model data so it can be loaded elsewhere\nmy_script_module.save(\"my_script_module.pt\")\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "To learn more, see our [Introduction to TorchScript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript.html) and [Loading a\nPyTorch Model in C++](https://pytorch.org/tutorials/advanced/cpp_export.html) tutorials.\n\n## Expanded ONNX Export", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "The [ONNX](http://onnx.ai/) community continues to grow with an open [governance structure](https://github.com/onnx/onnx/wiki/Expanded-ONNX-Steering-Committee-Announced!) and additional steering committee members, special interest groups (SIGs), and working groups (WGs). In collaboration with Microsoft, we\u2019ve added full support to export ONNX Opset versions 7(v1.2), 8(v1.3), 9(v1.4) and 10 (v1.5). We\u2019ve have also enhanced the constant folding pass to support Opset 10, the latest available version of ONNX. ScriptModule has also been improved including support for multiple outputs, tensor factories, and tuples as inputs and outputs. Additionally, users are now able to register their own symbolic to export custom ops, and specify the dynamic dimensions of inputs during export. Here is a summary of the all of the major improvements:", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "* Support for multiple Opsets including the ability to export dropout, slice, flip, and interpolate in Opset 10.\n* Improvements to ScriptModule including support for multiple outputs, tensor factories, and tuples as inputs and outputs.\n* More than a dozen additional PyTorch operators supported including the ability to export a custom operator.\n* Many big fixes and test infra improvements.\n\nYou can try out the latest tutorial [here](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html), contributed by @lara-hdr at Microsoft. A big thank you to the entire Microsoft team for all of their hard work to make this release happen!\n\n## nn.Transformer", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "## nn.Transformer\n\nIn PyTorch 1.2, we now include a standard [nn.Transformer](https://pytorch.org/docs/stable/nn.html?highlight=transformer#torch.nn.Transformer) module, based on the paper \u201c[Attention is All You Need](https://arxiv.org/abs/1706.03762)\u201d. The `nn.Transformer` module relies entirely on an [attention mechanism](https://pytorch.org/docs/stable/nn.html?highlight=nn%20multiheadattention#torch.nn.MultiheadAttention) to draw global dependencies between input and output. The individual components of the `nn.Transformer` module are designed so they can be adopted independently. For example, the [nn.TransformerEncoder](https://pytorch.org/docs/stable/nn.html?highlight=nn%20transformerencoder#torch.nn.TransformerEncoder) can be used by itself, without the larger `nn.Transformer`. The new APIs include:\n\n* `nn.Transformer`\n* `nn.TransformerEncoder` and `nn.TransformerEncoderLayer`\n* `nn.TransformerDecoder` and `nn.TransformerDecoderLayer`", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "
\n \n
\n\nSee the [Transformer Layers](https://pytorch.org/docs/stable/nn.html#transformer-layers) documentation for more information. See [here](https://github.com/pytorch/pytorch/releases) for the full PyTorch 1.2 release notes.\n\n# Domain API Library Updates", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "PyTorch domain libraries like torchvision, torchtext, and torchaudio provide convenient access to common datasets, models, and transforms that can be used to quickly create a state-of-the-art baseline. Moreover, they also provide common abstractions to reduce boilerplate code that users might have to otherwise repeatedly write. Since research domains have distinct requirements, an ecosystem of specialized libraries called domain APIs (DAPI) has emerged around PyTorch to simplify the development of new and existing algorithms in a number of fields. We\u2019re excited to release three updated DAPI libraries for text, audio, and vision that compliment the PyTorch 1.2 core release.\n\n## Torchaudio 0.3 with Kaldi Compatibility, New Transforms\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "Torchaudio specializes in machine understanding of audio waveforms. It is an ML library that provides relevant signal processing functionality (but is not a general signal processing library). It leverages PyTorch\u2019s GPU support to provide many tools and transformations for waveforms to make data loading and standardization easier and more readable. For example, it offers data loaders for waveforms using sox, and transformations such as spectrograms, resampling, and mu-law encoding and decoding.\n\nWe are happy to announce the availability of torchaudio 0.3.0, with a focus on standardization and complex numbers, a transformation (resample) and two new functionals (phase_vocoder, ISTFT), Kaldi compatibility, and a new tutorial. Torchaudio was redesigned to be an extension of PyTorch and a part of the domain APIs (DAPI) ecosystem.\n\n### Standardization", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "### Standardization\n\nSignificant effort in solving machine learning problems goes into data preparation. In this new release, we've updated torchaudio's interfaces for its transformations to standardize around the following vocabulary and conventions.\n\nTensors are assumed to have channel as the first dimension and time as the last dimension (when applicable). This makes it consistent with PyTorch's dimensions. For size names, the prefix `n_` is used (e.g. \"a tensor of size (`n_freq`, `n_mel`)\") whereas dimension names do not have this prefix (e.g. \"a tensor of dimension (channel, time)\"). The input of all transforms and functions now assumes channel first. This is done to be consistent with PyTorch, which has channel followed by the number of samples. The channel parameter of all transforms and functions is now deprecated.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "The output of `STFT` is (channel, frequency, time, 2), meaning for each channel, the columns are the Fourier transform of a certain window, so as we travel horizontally we can see each column (the Fourier transformed waveform) change over time. This matches the output of librosa so we no longer need to transpose in our test comparisons with `Spectrogram`, `MelScale`, `MelSpectrogram`, and `MFCC`. Moreover, because of these new conventions, we deprecated `LC2CL` and `BLC2CBL` which were used to transfer from one shape of signal to another.\n\nAs part of this release, we're also introducing support for complex numbers via tensors of dimension (..., 2), and providing `magphase` to convert such a tensor into its magnitude and phase, and similarly `complex_norm` and `angle`.\n\nThe details of the standardization are provided in the [README](https://github.com/pytorch/audio/blob/v0.3.0/README.md#Conventions).\n\n### Functionals, Transformations, and Kaldi Compatibility", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "Prior to the standardization, we separated state and computation into `torchaudio.transforms` and `torchaudio.functional`.\n\nAs part of the transforms, we're adding a new transformation in 0.3.0: `Resample`. `Resample` can upsample or downsample a waveform to a different frequency.\n\nAs part of the functionals, we're introducing: `phase_vocoder`, a phase vocoder to change the speed of a waveform without changing its pitch, and `ISTFT`, the inverse `STFT` implemented to be compatible with STFT provided by PyTorch. This separation allows us to make functionals weak scriptable and to utilize JIT in 0.3.0. We thus have JIT and CUDA support for the following transformations: `Spectrogram`, `AmplitudeToDB` (previously named `SpectrogramToDB`), `MelScale`,\n`MelSpectrogram`, `MFCC`, `MuLawEncoding`, `MuLawDecoding` (previously named `MuLawExpanding`).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "We now also provide a compatibility interface with Kaldi to ease onboarding and reduce a user's code dependency on Kaldi. We now have an interface for `spectrogram`, `fbank`, and `resample_waveform`.\n\n### New Tutorial\n\nTo showcase the new conventions and transformations, we have a [new tutorial](https://pytorch.org/tutorials/beginner/audio_preprocessing_tutorial.html) demonstrating how to preprocess waveforms using torchaudio. This tutorial walks through an example of loading a waveform and applying some of the available transformations to it.\n\nWe are excited to see an active community around torchaudio and eager to further grow and support it. We encourage you to go ahead and experiment for yourself with this tutorial and the two datasets that are available: VCTK and YESNO! They have an interface to download the datasets and preprocess them in a convenient format. You can find the details in the release notes [here](https://github.com/pytorch/audio/releases).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "## Torchtext 0.4 with supervised learning datasets\n\nA key focus area of torchtext is to provide the fundamental elements to help accelerate NLP research. This includes easy access to commonly used datasets and basic preprocessing pipelines for working on raw text based data. The torchtext 0.4.0 release includes several popular supervised learning baselines with \"one-command\" data loading. A [tutorial](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) is included to show how to use the new datasets for text classification analysis. We also added and improved on a few functions such as [get_tokenizer](https://pytorch.org/text/data.html?highlight=get_tokenizer#torchtext.data.get_tokenizer) and [build_vocab_from_iterator](https://pytorch.org/text/vocab.html#build-vocab-from-iterator) to make it easier to implement future datasets. Additional examples can be found [here](https://github.com/pytorch/text/tree/master/examples/text_classification).", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "Text classification is an important task in Natural Language Processing with many applications, such as sentiment analysis. The new release includes several popular [text classification datasets](https://pytorch.org/text/datasets.html?highlight=textclassification#torchtext.datasets.TextClassificationDataset) for supervised learning including:\n\n* AG_NEWS\n* SogouNews\n* DBpedia\n* YelpReviewPolarity\n* YelpReviewFull\n* YahooAnswers\n* AmazonReviewPolarity\n* AmazonReviewFull\n\nEach dataset comes with two parts (train vs. test), and can be easily loaded with a single command. The datasets also support an ngrams feature to capture the partial information about the local word order. Take a look at the tutorial [here](https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html) to learn more about how to use the new datasets for supervised problems such as text classification analysis.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "```python\nfrom torchtext.datasets.text_classification import DATASETS\ntrain_dataset, test_dataset = DATASETS['AG_NEWS'](ngrams=2)\n```\n\nIn addition to the domain library, PyTorch provides many tools to make data loading easy. Users now can load and preprocess the text classification datasets with some well supported tools, like [torch.utils.data.DataLoader](https://pytorch.org/docs/stable/_modules/torch/utils/data/dataloader.html) and [torch.utils.data.IterableDataset](https://pytorch.org/docs/master/data.html#torch.utils.data.IterableDataset). Here are a few lines to wrap the data with DataLoader. More examples can be found [here](https://github.com/pytorch/text/tree/master/examples/text_classification).\n\n```python\nfrom torch.utils.data import DataLoader\ndata = DataLoader(train_dataset, collate_fn=generate_batch)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "Check out the release notes [here](https://github.com/pytorch/text/releases) to learn more and try out the [tutorial here](http://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html).\n\n## Torchvision 0.4 with Support for Video\n\nVideo is now a first-class citizen in torchvision, with support for data loading, datasets, pre-trained models, and transforms. The 0.4 release of torchvision includes:\n\n* Efficient IO primitives for reading/writing video files (including audio), with support for arbitrary encodings and formats.\n* Standard video datasets, compatible with `torch.utils.data.Dataset` and `torch.utils.data.DataLoader`.\n* Pre-trained models built on the Kinetics-400 dataset for action classification on videos (including the training scripts).\n* Reference training scripts for training your own video models.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "We wanted working with video data in PyTorch to be as straightforward as possible, without compromising too much on performance.\nAs such, we avoid the steps that would require re-encoding the videos beforehand, as it would involve:\n\n* A preprocessing step which duplicates the dataset in order to re-encode it.\n* An overhead in time and space because this re-encoding is time-consuming.\n* Generally, an external script should be used to perform the re-encoding.\n\nAdditionally, we provide APIs such as the utility class, `VideoClips`, that simplifies the task of enumerating all possible clips of fixed size in a list of video files by creating an index of all clips in a set of videos. It also allows you to specify a fixed frame-rate for the videos. An example of the API is provided below:\n\n```python\nfrom torchvision.datasets.video_utils import VideoClips", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} +{"page_content": "class MyVideoDataset(object):\n def __init__(self, video_paths):\n self.video_clips = VideoClips(video_paths,\n clip_length_in_frames=16,\n frames_between_clips=1,\n frame_rate=15)\n\n def __getitem__(self, idx):\n video, audio, info, video_idx = self.video_clips.get_clip(idx)\n return video, audio\n\n def __len__(self):\n return self.video_clips.num_clips()\n```\n\nMost of the user-facing API is in Python, similar to PyTorch, which makes it easily extensible. Plus, the underlying implementation is fast \u2014 torchvision decodes as little as possible from the video on-the-fly in order to return a clip from the video.\n\nCheck out the torchvision 0.4 [release notes here](https://github.com/pytorch/vision/releases) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} {"page_content": "We look forward to continuing our collaboration with the community and hearing your feedback as we further improve and expand the PyTorch deep learning platform.\n\n*We\u2019d like to thank the entire PyTorch team and the community for all of the contributions to this work!*", "metadata": {"source": "https://pytorch.org/blog/pytorch-1.2-and-domain-api-release/", "category": "pytorch blogs"}} {"page_content": "---\nlayout: blog_detail\ntitle: 'How to Train State-Of-The-Art Models Using TorchVision\u2019s Latest Primitives'\nauthor: Vasilis Vryniotis\nfeatured-img: 'assets/images/fx-image2.png'\n---\n\n", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "A few weeks ago, TorchVision v0.11 was released packed with numerous new primitives, models and training recipe improvements which allowed achieving state-of-the-art (SOTA) results. The project was dubbed \u201c[TorchVision with Batteries Included](https://github.com/pytorch/vision/issues/3911)\u201d and aimed to modernize our library. We wanted to enable researchers to reproduce papers and conduct research more easily by using common building blocks. Moreover, we aspired to provide the necessary tools to Applied ML", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "practitioners to train their models on their own data using the same SOTA techniques as in research. Finally, we wanted to refresh our pre-trained weights and offer\u00a0better off-the-shelf models to our users, hoping that they would build better applications.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Though there is still much work to be done, we wanted to share with you some exciting results from the above work. We will showcase how one can use the new tools included in TorchVision to achieve state-of-the-art results on a highly competitive and well-studied architecture such as ResNet50 [[1]](https://arxiv.org/abs/1512.03385). We will share the exact recipe used to improve our baseline by over 4.7 accuracy points to reach a final top-1 accuracy of 80.9% and share the journey for deriving the new", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "training process. Moreover, we will show that this recipe generalizes well to other model variants and families. We hope that the above will influence future research for developing stronger generalizable training methodologies and will inspire the community to adopt and contribute to our efforts.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The Results\n\nUsing our new training recipe found on ResNet50, we\u2019ve refreshed the pre-trained weights of the following models:\n\n\n| Model | Accuracy@1 | Accuracy@5| \n|----------|:--------:|:----------:|\n| ResNet50 | 80.858 | 95.434| \n|----------|:--------:|:----------:|\n| ResNet101 | 81.886 | 95.780| \n|----------|:--------:|:----------:|\n| ResNet152 | 82.284 | 96.002| \n|----------|:--------:|:----------:|\n| ResNeXt50-32x4d | 81.198 | 95.340|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Note that the accuracy of all models except RetNet50 can be further improved by adjusting their training parameters slightly, but our focus was to have a single robust recipe which performs well for all. \n\n**UPDATE:** We have refreshed the majority of popular classification models of TorchVision, you can find the details on this [blog post](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/).\n\nThere are currently two ways to use the latest weights of the model.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Using the Multi-pretrained weight API", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "We are currently working on a new prototype mechanism which will extend the model builder methods of TorchVision to [support multiple weights](https://github.com/pytorch/vision/issues/4611). Along with the weights, we store useful [meta-data](https://github.com/pytorch/vision/blob/c5fb79f8fad60511c89957c4970cc2a5cfc8432e/torchvision/prototype/models/resnet.py#L94-L103) (such as the labels, the accuracy, links to recipe etc) and the preprocessing transforms necessary for using the models. Example:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "```python\n from PIL import Image\n from torchvision import prototype as P\n img = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n \u00a0\n # Initialize model\n weights = P.models.ResNet50_Weights.IMAGENET1K_V2\n model = P.models.resnet50(weights=weights)\n model.eval()", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "# Initialize inference transforms\n preprocess = weights.transforms()\n \u00a0\n # Apply inference preprocessing transforms\n batch = preprocess(img).unsqueeze(0)\n prediction = model(batch).squeeze(0).softmax(0)\n \u00a0\n # Make predictions\n label = prediction.argmax().item()\n score = prediction[label].item()\n \u00a0\n # Use meta to get the labels\n category_name = weights.meta['categories'][label]\n print(f\"{category_name}: {100 * score}%\")\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Using the legacy API\n\nThose who don\u2019t want to use a prototype API have the option of accessing the new weights via the legacy API using the following approach:\n\n```python\n from torchvision.models import resnet\n \u00a0\n # Overwrite the URL of the previous weights\n resnet.model_urls[\"resnet50\"] = \"https://download.pytorch.org/models/resnet50-11ad3fa6.pth\"\n \u00a0\n # Initialize the model using the legacy API\n model = resnet.resnet50(pretrained=True)\n \u00a0\n # TODO: Apply preprocessing + call the model\n # ...\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The Training Recipe", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Our goal was to use the newly introduced primitives of TorchVision to derive a new strong training recipe which achieves state-of-the-art results for the vanilla ResNet50 architecture when trained from scratch on ImageNet with no additional external data. Though by using architecture specific tricks\u00a0[[2]](https://arxiv.org/abs/1812.01187) one could further improve the accuracy, we\u2019ve decided not to include them so that the recipe can be used in other architectures. Our recipe\u00a0heavily focuses on simplicity", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "and builds upon work by FAIR [[3]](https://arxiv.org/abs/2103.06877), [[4]](https://arxiv.org/abs/2106.14881), [[5]](https://arxiv.org/abs/1906.06423), [[6]](https://arxiv.org/abs/2012.12877), [[7]](https://arxiv.org/abs/2110.00476).\u00a0Our findings align with the\u00a0parallel study of Wightman et al. [[7]](https://arxiv.org/abs/2110.00476), who also report major accuracy improvements by focusing on the training recipes.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Without further ado, here are the main parameters of our recipe:\n\n```python\n # Optimizer & LR scheme\n ngpus=8,\n batch_size=128,\u00a0 # per GPU\n\n epochs=600, \n opt='sgd', \u00a0\n momentum=0.9,\n\n lr=0.5, \n lr_scheduler='cosineannealinglr', \n lr_warmup_epochs=5, \n lr_warmup_method='linear', \n lr_warmup_decay=0.01, \n\n\n # Regularization and Augmentation\n weight_decay=2e-05, \n norm_weight_decay=0.0,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "label_smoothing=0.1, \n mixup_alpha=0.2, \n cutmix_alpha=1.0, \n auto_augment='ta_wide', \n random_erase=0.1, \n \n ra_sampler=True,\n ra_reps=4,\n\n\n # EMA configuration\n model_ema=True, \n model_ema_steps=32, \n model_ema_decay=0.99998, \n\n\n # Resizing\n interpolation='bilinear', \n val_resize_size=232, \n val_crop_size=224, \n train_crop_size=176,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Using our standard [training reference script](https://github.com/pytorch/vision/tree/main/references/classification), we can train a ResNet50 using the following command:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "```\ntorchrun --nproc_per_node=8 train.py --model resnet50 --batch-size 128 --lr 0.5 \\\n--lr-scheduler cosineannealinglr --lr-warmup-epochs 5 --lr-warmup-method linear \\\n--auto-augment ta_wide --epochs 600 --random-erase 0.1 --weight-decay 0.00002\u00a0\\\n--norm-weight-decay 0.0 --label-smoothing 0.1 --mixup-alpha 0.2 --cutmix-alpha 1.0\u00a0\\\n--train-crop-size 176 --model-ema --val-resize-size 232 --ra-sampler --ra-reps 4\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Methodology\n\nThere are a few principles we kept in mind during our explorations:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "1. Training is a stochastic process and the validation metric we try to optimize is a random variable. This is due to the random weight initialization scheme employed and the existence of random effects during the training process. This means that we can\u2019t do a single run to assess the effect of a recipe change. The standard practice is doing multiple runs (usually 3 to 5) and studying the summarization stats (such as mean, std, median, max, etc).", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "2. There is usually a significant interaction between different parameters, especially for techniques that focus on Regularization and reducing overfitting. Thus changing the value of one can have effects on the optimal configurations of others. To account for that one can either adopt a greedy search approach (which often leads to suboptimal results but tractable experiments) or apply grid search (which leads to better results but is computationally expensive). In this work, we used a mixture of both.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "3. Techniques that are non-deterministic or introduce noise usually require longer training cycles to improve model performance. To keep things tractable, we initially used short training cycles (small number of epochs) to decide which paths can be eliminated early and which should be explored using longer training.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "4. There is a risk of overfitting the validation dataset [[8]](https://arxiv.org/abs/1902.10811) because of the repeated experiments. To mitigate some of the risk, we apply only training optimizations that provide a significant accuracy improvements and use K-fold cross validation to verify optimizations done on the validation set. Moreover we confirm that our recipe ingredients generalize well on other models for which we didn\u2019t optimize the hyper-parameters.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Break down of key accuracy improvements", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "As discussed in\u00a0[earlier blogposts](https://pytorch.org/blog/torchvision-ssdlite-implementation/#break-down-of-key-accuracy-improvements), training models is not a journey of monotonically increasing accuracies and the process involves a lot of backtracking. To quantify the effect of each optimization, below we attempt to show-case an idealized linear journey of deriving the final recipe starting from the original recipe of TorchVision. We would like to clarify that this is an oversimplification of the", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "actual path we followed and thus it should be taken with a grain of salt.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "

\n\"Cumulative\n

\n\nIn the table below, we provide a summary of the performance of stacked incremental improvements on top of Baseline. Unless denoted otherwise, we report the model with best Acc@1 out of 3 runs:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "| | Accuracy@1 | Accuracy@5| Incremental Diff|Absolute Diff|\n|----------|:--------:|:----------:|:---------|:--------:|\n| ResNet50 Baseline |76.130 | 92.862| 0.000|0.000|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + LR optimizations | 76.494 |93.198| 0.364|0.364\n|----------|:--------:|:----------:|:---------|:--------:|\n| + TrivialAugment | 76.806| 93.272|0.312| 0.676|\n|----------|:--------:|:----------:|:---------|:--------:|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "| + Long Training | 78.606| 94.052| 1.800|2.476|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Random Erasing | 78.796 | 94.094|0.190|2.666\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Label Smoothing |79.114| 94.374| 0.318|2.984|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Mixup | 79.232| 94.536| 0.118|3.102|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Cutmix |79.510| 94.642| 0.278|3.380|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "|----------|:--------:|:----------:|:---------|:--------:|\n| + Weight Decay tuning |80.036|94.746| 0.526|3.906|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + FixRes mitigations |80.196|94.672| 0.160|4.066|\n|----------|:--------:|:----------:|:---------|:--------:|\n|+ EMA |80.450|94.908| 0.254|4.320|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Inference Resize tuning * |80.674|95.166| 0.224|4.544|\n|----------|:--------:|:----------:|:---------|:--------:|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "| + Repeated Augmentation ** |80.858|95.434| 0.184|4.728|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "*The tuning of the inference size was done on top of the last model. See below for details.\n\n** Community contribution done after the release of the article. See below for details.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Baseline\n\nOur baseline is the previously released ResNet50 model of TorchVision. It was trained with the following recipe:\n\n```python \n # Optimizer & LR scheme\n ngpus=8,\n batch_size=32,\u00a0 # per GPU\n\n epochs=90, \n opt='sgd', \u00a0\n momentum=0.9,\n\n lr=0.1, \n lr_scheduler='steplr', \n lr_step_size=30, \n lr_gamma=0.1, \n\n\n # Regularization\n weight_decay=1e-4,\n\n\n # Resizing\n interpolation='bilinear', \n val_resize_size=256, \n val_crop_size=224, \n train_crop_size=224,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Most of the above parameters are the defaults on our [training scripts](https://github.com/pytorch/vision/tree/main/references/classification). We will start building on top of this baseline by introducing optimizations until we gradually arrive at the final recipe.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "LR optimizations", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "There are a few parameter updates we can apply to improve both the accuracy and the speed of our training. This can be achieved by increasing the batch size and tuning the LR. Another common method is to apply warmup and gradually increase our learning rate. This is beneficial especially when we use very high learning rates and helps with the stability of the training in the early epochs. Finally, another optimization is to apply Cosine Schedule to adjust our LR during the epochs. A big advantage of cosine", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "is that there are no hyper-parameters to optimize, which cuts down our search space.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Here are the additional optimizations applied on top of the baseline recipe. Note that we\u2019ve run multiple experiments to determine the optimal configuration of the parameters:\n\n```python\n batch_size=128,\u00a0 # per GPU\n\n lr=0.5, \n lr_scheduler='cosineannealinglr', \n lr_warmup_epochs=5, \n lr_warmup_method='linear', \n lr_warmup_decay=0.01,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The above optimizations increase our top-1 Accuracy by 0.364 points comparing to the baseline. Note that in order to combine the different LR strategies we use the newly introduced [SequentialLR](https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.SequentialLR.html#torch.optim.lr_scheduler.SequentialLR) scheduler.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "TrivialAugment\n\nThe original model was trained using basic augmentation transforms such as Random resized crops and horizontal flips. An easy way to improve our accuracy is to apply more complex \u201cAutomatic-Augmentation\u201d techniques. The one that performed best for us is TrivialAugment\u00a0[[9]](https://arxiv.org/abs/2103.10158), which\u00a0is extremely simple and can be considered \u201cparameter free\u201d, which means it can help us cut down our search space further.\n\nHere is the update applied on top of the previous step:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "```\nauto_augment='ta_wide',", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The use of TrivialAugment increased our top-1 Accuracy by\u00a00.312 points\u00a0compared to the previous step.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Long Training\n\nLonger training cycles are beneficial when our recipe contains ingredients that behave randomly. More specifically as we start adding more and more techniques that introduce noise, increasing the number of epochs becomes crucial. Note that at early stages of our exploration, we used relatively short cycles of roughly 200 epochs which was later increased to 400 as we started narrowing down most of the parameters and finally increased to 600 epochs at the final versions of the recipe.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below we see the update applied on top of the earlier steps:\n\n```\nepochs=600,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "This further increases our top-1 Accuracy by 1.8 points on top of the previous step. This is the biggest increase we will observe in this iterative process. It\u2019s worth noting that the effect of this single optimization is overstated and somehow misleading. Just increasing the number of epochs on top of the old baseline won\u2019t yield such significant improvements. Nevertheless the combination of the LR optimizations with strong Augmentation strategies helps the model benefit from longer cycles. It\u2019s also worth", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "mentioning that the reason we introduce the lengthy training cycles so early in the process is because in the next steps we will introduce techniques that\u00a0require significantly more epochs to provide good results.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Random Erasing", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Another data augmentation technique known to help the classification accuracy is Random Erasing [[10]](https://arxiv.org/abs/1708.04896), [[11]](https://arxiv.org/abs/1708.04552). Often paired with Automatic Augmentation methods, it usually yields additional improvements in accuracy due to its regularization effect. In our experiments we tuned only the probability of applying the method via a grid search and found that it\u2019s beneficial to keep its probability at low levels, typically around 10%.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Here is the extra parameter introduced on top of the previous:\n\n```\nrandom_erase=0.1,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Applying Random Erasing increases our Acc@1 by further\u00a00.190 points.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Label Smoothing", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "A good technique to reduce overfitting is to stop the model from becoming overconfident. This can be achieved by softening the ground truth using Label Smoothing\u00a0[[12]](https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Szegedy_Rethinking_the_Inception_CVPR_2016_paper.pdf). There is a single parameter which controls the degree of smoothing (the higher the stronger) that we need to specify. Though optimizing it via grid search is possible, we found that values around 0.05-0.15 yield similar", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "results, so to avoid overfitting it we used the same value as on the\u00a0paper that introduced it.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below we can find the extra config added on this step:\n\n```\nlabel_smoothing=0.1,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "We use PyTorch\u2019s newly introduced\u00a0[CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=label_smoothing) label_smoothing parameter and that increases our accuracy by an additional\u00a00.318 points.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Mixup and Cutmix", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Two data augmentation techniques often used to produce SOTA results are Mixup and Cutmix\u00a0[[13]](https://arxiv.org/abs/1710.09412), [[14]](https://arxiv.org/abs/1905.04899). They both provide strong regularization effects by softening not only the labels but also the images. In our setup we found it beneficial to apply one of them randomly with equal probability. Each is parameterized with a\u00a0hyperparameter alpha, which controls the shape of the Beta distribution from which the smoothing probability is", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "sampled. We did a very limited grid search, focusing primarily on common values proposed on the papers.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below you will find the optimal values for the alpha parameters of the two techniques:\n\n```\nmixup_alpha=0.2, \ncutmix_alpha=1.0,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Applying mixup increases our accuracy by\u00a00.118 points and combining it with cutmix improves it by additional 0.278 points.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Weight Decay tuning\n\nOur standard recipe uses L2 regularization to reduce overfitting. The Weight Decay parameter controls the degree of the regularization (the larger the stronger) and is applied universally to all learned parameters of the model by default. In this recipe, we apply two optimizations to the standard approach. First we perform grid search to tune the parameter of weight decay and second we disable weight decay for the parameters of the normalization layers.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below you can find the optimal configuration of weight decay for our recipe:\n\n```\nweight_decay=2e-05, \nnorm_weight_decay=0.0,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The above update improves our accuracy by a further\u00a00.526 points, providing additional experimental evidence for a known fact that tuning weight decay has significant effects on the performance of the model. Our approach for separating the Normalization parameters from the rest was inspired by\u00a0[ClassyVision\u2019s](https://github.com/facebookresearch/ClassyVision) approach.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "FixRes mitigations", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "An important property identified early in our experiments is the fact that the models performed significantly better if the resolution used during validation was increased from the 224x224 of training. This effect is studied in detail on the FixRes paper [[5]](https://arxiv.org/abs/1906.06423)\u00a0and two mitigations are proposed: a) one could try to reduce the training resolution so that the accuracy on the validation resolution is maximized or b) one could fine-tune the model on a two-phase training so that", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "it adjusts on the target resolution. Since we didn\u2019t want to introduce a 2-phase training, we went for option a). This means that we reduced the train crop size from 224 and used grid search to find the one that maximizes the validation on resolution of 224x224.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below you can see the optimal value used on our recipe:\n\n```\nval_crop_size=224, \ntrain_crop_size=176,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The above optimization improved our accuracy by an additional 0.160 points and sped up our training by 10%.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "It\u2019s worth noting that the FixRes effect still persists, meaning that the model continues to perform better on validation when we increase the resolution. Moreover, further reducing the training crop-size actually hurts the accuracy. This intuitively makes sense because one can only reduce the resolution so much before critical details start disappearing from the picture. Finally, we should note that the above FixRes mitigation seems to benefit models with similar depth to ResNet50. Deeper variants with", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "larger receptive fields seem to be slightly negatively affected (typically by 0.1-0.2 points). Hence we consider this part of the recipe optional. Below we visualize the performance of the best available checkpoints (with the full recipe) for models trained with 176 and 224 resolution:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "
\n\"Best\n\"Best\n
", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Exponential Moving Average (EMA)\n\nEMA is a technique that allows one to push the accuracy of a model without increasing its complexity or inference time. It performs an exponential moving average on the model weights and this leads to increased accuracy and more stable models. The averaging happens every few iterations and its decay parameter was tuned via grid search.\u00a0\n\nBelow you can see the optimal values for our recipe:\n\n```\nmodel_ema=True, \nmodel_ema_steps=32, \nmodel_ema_decay=0.99998,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The use of EMA increases our accuracy by\u00a00.254 points comparing to the previous step. Note that TorchVision\u2019s\u00a0[EMA implementation](https://github.com/pytorch/vision/pull/4406) is build on top of PyTorch\u2019s [AveragedModel](https://pytorch.org/docs/stable/optim.html#stochastic-weight-averaging) class with the key difference being that it averages not only the model parameters but also its buffers. Moreover, we have adopted tricks from\u00a0[Pycls](https://github.com/facebookresearch/pycls/tree/main/pycls)\u00a0which", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "allow us to parameterize the decay in a way that doesn\u2019t depend on the number of epochs.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Inference Resize tuning", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Unlike all other steps of the process which involved training models with different parameters, this optimization was done on top of the final model. During inference, the image is resized to a specific resolution and then a central 224x224 crop is taken from it. The original recipe used a resize size of 256, which caused a similar discrepancy as the one described on the FixRes paper [[5]](https://arxiv.org/abs/1906.06423). By bringing this resize value closer to the target inference resolution, one can", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "improve the accuracy. To select the value we run a short grid search between interval [224, 256] with step of 8. To avoid overfitting, the value was selected using half of the validation set and confirmed using the other half.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below you can see the optimal value used on our recipe:\n\n```\nval_resize_size=232,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The above is an optimization which improved our accuracy by\u00a00.224 points.\u00a0It\u2019s worth noting that the optimal value for ResNet50 works also best for ResNet101, ResNet152 and ResNeXt50, which hints that it generalizes across models:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "
\n\"ResNet50\n\"ResNet101\n\"Best\n
", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "[UPDATE] Repeated Augmentation", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Repeated Augmentation [[15]](https://arxiv.org/abs/1901.09335), [[16]](https://arxiv.org/abs/1902.05509) is another technique which can improve the overall accuracy and has been used by other strong recipes such as those at [[6]](https://arxiv.org/abs/2012.12877), [[7]](https://arxiv.org/abs/2110.00476). Tal Ben-Nun, a community contributor, has [further improved](https://github.com/pytorch/vision/pull/5201) upon our original recipe by proposing training the model with 4 repetitions. His contribution came", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "after the release of this article.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Below you can see the optimal value used on our recipe:\n\n```\nra_sampler=True,\nra_reps=4,", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "The above is the final optimization which improved our accuracy by\u00a00.184 points.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Optimizations that were tested but not adopted\n\nDuring the early stages of our research, we experimented with additional techniques, configurations and optimizations. Since our target was to keep our recipe as simple as possible, we decided not to include anything that didn\u2019t provide a significant improvement. Here are a few approaches that we took but didn\u2019t make it to our final recipe:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "- **Optimizers:** Using more complex optimizers such as Adam, RMSProp or SGD with Nesterov momentum didn\u2019t\u00a0provide significantly better results than vanilla SGD with momentum.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "- **LR Schedulers:**\u00a0We tried different LR Scheduler schemes such as StepLR and Exponential. Though the latter tends to work better with EMA, it often requires additional hyper-parameters such as defining the minimum LR to work well. Instead, we just use cosine annealing decaying the LR up to zero and choose the checkpoint with the highest accuracy.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "- **Automatic Augmentations:**\u00a0We\u2019ve tried different augmentation strategies such as AutoAugment and RandAugment. None of these outperformed the simpler parameter-free TrivialAugment.\n- **Interpolation:** Using bicubic or nearest interpolation didn\u2019t\u00a0provide significantly better results than bilinear.\n- **Normalization layers:** Using Sync Batch Norm didn\u2019t yield\u00a0significantly better results than using the regular Batch Norm.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Acknowledgements", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "We would like to thank\u00a0Piotr Dollar, Mannat Singh and Hugo Touvron for providing their insights and feedback during the development of the recipe and for their previous research work on which our recipe is based on. Their support was invaluable for achieving the above result. Moreover, we would like to thank\u00a0Prabhat Roy, Kai Zhang, Yiwen Song, Joel Schlosser, Ilqar Ramazanli, Francisco Massa, Mannat Singh, Xiaoliang Dai, Samuel Gabriel, Allen Goodman and Tal Ben-Nun for their contributions to the Batteries", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "Included project.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "References", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "1. Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. \u201cDeep Residual Learning for Image Recognition\u201d.\n2. Tong He, Zhi Zhang, Hang Zhang, Zhongyue Zhang, Junyuan Xie, Mu Li. \u201cBag of Tricks for Image Classification with Convolutional Neural Networks\u201d\n3. Piotr Doll\u00e1r, Mannat Singh, Ross Girshick. \u201cFast and Accurate Model Scaling\u201d\n4. Tete Xiao, Mannat Singh, Eric Mintun, Trevor Darrell, Piotr Doll\u00e1r, Ross Girshick. \u201cEarly Convolutions Help Transformers See Better\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "5. Hugo Touvron, Andrea Vedaldi, Matthijs Douze, Herv\u00e9 J\u00e9gou. \u201cFixing the train-test resolution discrepancy\n6. Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Herv\u00e9 J\u00e9gou. \u201cTraining data-efficient image transformers & distillation through attention\u201d\n7. Ross Wightman, Hugo Touvron, Herv\u00e9 J\u00e9gou. \u201cResNet strikes back: An improved training procedure in timm\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "8. Benjamin Recht, Rebecca Roelofs, Ludwig Schmidt, Vaishaal Shankar. \u201cDo ImageNet Classifiers Generalize to ImageNet?\u201d\n9. Samuel G. M\u00fcller, Frank Hutter. \u201cTrivialAugment: Tuning-free Yet State-of-the-Art Data Augmentation\u201d\n10. Zhun Zhong, Liang Zheng, Guoliang Kang, Shaozi Li, Yi Yang. \u201cRandom Erasing Data Augmentation\u201d\n11. Terrance DeVries, Graham W. Taylor. \u201cImproved Regularization of Convolutional Neural Networks with Cutout\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "12. Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jon Shlens, Zbigniew Wojna. \u201cRethinking the Inception Architecture for Computer Vision\u201d\n13. Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz. \u201cmixup: Beyond Empirical Risk Minimization\u201d\n14. Sangdoo Yun, Dongyoon Han, Seong Joon Oh, Sanghyuk Chun, Junsuk Choe, Youngjoon Yoo. \u201cCutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "15. Elad Hoffer, Tal Ben-Nun, Itay Hubara, Niv Giladi, Torsten Hoefler, Daniel Soudry. \u201cAugment your batch: better training with larger batches\u201d\n16. Maxim Berman, Herv\u00e9 J\u00e9gou, Andrea Vedaldi, Iasonas Kokkinos, Matthijs Douze. \u201cMultigrain: a unified image embedding for classes and instances\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: 'Updates & Improvements to PyTorch Tutorials'\nauthor: Team PyTorch\n---\n\nPyTorch.org provides researchers and developers with documentation, installation instructions, latest news, community projects, tutorials, and more. Today, we are introducing usability and content improvements including tutorials in additional categories, a new recipe format for quickly referencing common topics, sorting using tags, and an updated homepage. \n\nLet\u2019s take a look at them in detail.", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "TUTORIALS HOME PAGE UPDATE\nThe tutorials home page now provides clear actions that developers can take. For new PyTorch users, there is an easy-to-discover button to take them directly to \u201cA 60 Minute Blitz\u201d. Right next to it, there is a button to view all recipes which are designed to teach specific features quickly with examples. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "In addition to the existing left navigation bar, tutorials can now be quickly filtered by multi-select tags. Let\u2019s say you want to view all tutorials related to \u201cProduction\u201d and \u201cQuantization\u201d. You can select the \u201cProduction\u201d and \u201cQuantization\u201d filters as shown in the image shown below:\n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "The following additional resources can also be found at the bottom of the Tutorials homepage:\n* [PyTorch Cheat Sheet](https://pytorch.org/tutorials/beginner/ptcheat.html)\n* [PyTorch Examples](https://github.com/pytorch/examples)\n* [Tutorial on GitHub](https://github.com/pytorch/tutorials)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "PYTORCH RECIPES \nRecipes are new bite-sized, actionable examples designed to teach researchers and developers how to use specific PyTorch features. Some notable new recipes include:\n* [Loading Data in PyTorch](https://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html)\n* [Model Interpretability Using Captum](https://pytorch.org/tutorials/recipes/recipes/Captum_Recipe.html)\n* [How to Use TensorBoard](https://pytorch.org/tutorials/recipes/recipes/tensorboard_with_pytorch.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "View the full recipes [here](http://pytorch.org/tutorials/recipes/recipes_index.html).", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "LEARNING PYTORCH", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "This section includes tutorials designed for users new to PyTorch. Based on community feedback, we have made updates to the current [Deep Learning with PyTorch: A 60 Minute Blitz](https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html) tutorial, one of our most popular tutorials for beginners. Upon completion, one can understand what PyTorch and neural networks are, and be able to build and train a simple image classification network. Updates include adding explanations to clarify output", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "meanings and linking back to where users can read more in the docs, cleaning up confusing syntax errors, and reconstructing and explaining new concepts for easier readability.", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "DEPLOYING MODELS IN PRODUCTION\nThis section includes tutorials for developers looking to take their PyTorch models to production. The tutorials include:\n* [Deploying PyTorch in Python via a REST API with Flask](https://pytorch.org/tutorials/intermediate/flask_rest_api_tutorial.html)\n* [Introduction to TorchScript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html)\n* [Loading a TorchScript Model in C++](https://pytorch.org/tutorials/advanced/cpp_export.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "* [Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "FRONTEND APIS\nPyTorch provides a number of frontend API features that can help developers to code, debug, and validate their models more efficiently. This section includes tutorials that teach what these features are and how to use them. Some tutorials to highlight: \n* [Introduction to Named Tensors in PyTorch](https://pytorch.org/tutorials/intermediate/named_tensor_tutorial.html)\n* [Using the PyTorch C++ Frontend](https://pytorch.org/tutorials/advanced/cpp_frontend.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "* [Extending TorchScript with Custom C++ Operators](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html)\n* [Extending TorchScript with Custom C++ Classes](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html)\n* [Autograd in C++ Frontend](https://pytorch.org/tutorials/advanced/cpp_autograd.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "MODEL OPTIMIZATION\nDeep learning models often consume large amounts of memory, power, and compute due to their complexity. This section provides tutorials for model optimization:\n* [Pruning](https://pytorch.org/tutorials/intermediate/pruning_tutorial.html)\n* [Dynamic Quantization on BERT](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html)\n* [Static Quantization with Eager Mode in PyTorch](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "PARALLEL AND DISTRIBUTED TRAINING\nPyTorch provides features that can accelerate performance in research and production such as native support for asynchronous execution of collective operations and peer-to-peer communication that is accessible from Python and C++. This section includes tutorials on parallel and distributed training: \n* [Single-Machine Model Parallel Best Practices](https://pytorch.org/tutorials/intermediate/model_parallel_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "* [Getting started with Distributed Data Parallel](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)\n* [Getting started with Distributed RPC Framework](https://pytorch.org/tutorials/intermediate/rpc_tutorial.html)\n* [Implementing a Parameter Server Using Distributed RPC Framework](https://pytorch.org/tutorials/intermediate/rpc_param_server_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "Making these improvements are just the first step of improving PyTorch.org for the community. Please submit your suggestions [here](https://github.com/pytorch/tutorials/pulls).\n\nCheers,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 2.0: Our next generation release that is faster, more Pythonic and Dynamic as ever\"\n---", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "We are excited to announce the release of [PyTorch\u00ae 2.0](https://github.com/pytorch/pytorch/releases/tag/v2.0.0) which we highlighted during the [PyTorch Conference](https://www.youtube.com/@PyTorch/playlists?view=50&sort=dd&shelf_id=2) on 12/2/22! PyTorch 2.0 offers the same eager-mode development and user experience, while fundamentally changing and supercharging how PyTorch operates at compiler level under the hood with faster performance and support for Dynamic Shapes and Distributed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "This next-generation release includes a Stable version of Accelerated Transformers (formerly called Better Transformers); Beta includes torch.compile as the main API for PyTorch 2.0, the scaled_dot_product_attention function as part of torch.nn.functional, the MPS backend, functorch APIs in the torch.func module; and other Beta/Prototype improvements across various inferences, performance and training optimization features on GPUs and CPUs. For a comprehensive introduction and technical overview of", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "torch.compile, please visit the 2.0 [Get Started page](/get-started/pytorch-2.0).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Along with 2.0, we are also releasing a series of beta updates to the PyTorch domain libraries, including those that are in-tree, and separate libraries including TorchAudio, TorchVision, and TorchText. An update for TorchX is also being released as it moves to community supported mode. More details can be found in this [library blog](/blog/new-library-updates-in-pytorch-2.0/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "A few weeks ago, TorchVision v0.11 was released packed with numerous new primitives, models and training recipe improvements which allowed achieving state-of-the-art (SOTA) results. The project was dubbed \u201c[TorchVision with Batteries Included](https://github.com/pytorch/vision/issues/3911)\u201d and aimed to modernize our library. We wanted to enable researchers to reproduce papers and conduct research more easily by using common building blocks. Moreover, we aspired to provide the necessary tools to Applied ML practitioners to train their models on their own data using the same SOTA techniques as in research. Finally, we wanted to refresh our pre-trained weights and offer\u00a0better off-the-shelf models to our users, hoping that they would build better applications.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "Though there is still much work to be done, we wanted to share with you some exciting results from the above work. We will showcase how one can use the new tools included in TorchVision to achieve state-of-the-art results on a highly competitive and well-studied architecture such as ResNet50 [[1]](https://arxiv.org/abs/1512.03385). We will share the exact recipe used to improve our baseline by over 4.7 accuracy points to reach a final top-1 accuracy of 80.9% and share the journey for deriving the new training process. Moreover, we will show that this recipe generalizes well to other model variants and families. We hope that the above will influence future research for developing stronger generalizable training methodologies and will inspire the community to adopt and contribute to our efforts.\n\n## The Results\n\nUsing our new training recipe found on ResNet50, we\u2019ve refreshed the pre-trained weights of the following models:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "| Model | Accuracy@1 | Accuracy@5| \n|----------|:--------:|:----------:|\n| ResNet50 | 80.858 | 95.434| \n|----------|:--------:|:----------:|\n| ResNet101 | 81.886 | 95.780| \n|----------|:--------:|:----------:|\n| ResNet152 | 82.284 | 96.002| \n|----------|:--------:|:----------:|\n| ResNeXt50-32x4d | 81.198 | 95.340| \n\nNote that the accuracy of all models except RetNet50 can be further improved by adjusting their training parameters slightly, but our focus was to have a single robust recipe which performs well for all. \n\n**UPDATE:** We have refreshed the majority of popular classification models of TorchVision, you can find the details on this [blog post](https://pytorch.org/blog/introducing-torchvision-new-multi-weight-support-api/).\n\nThere are currently two ways to use the latest weights of the model.\n\n## Using the Multi-pretrained weight API", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "We are currently working on a new prototype mechanism which will extend the model builder methods of TorchVision to [support multiple weights](https://github.com/pytorch/vision/issues/4611). Along with the weights, we store useful [meta-data](https://github.com/pytorch/vision/blob/c5fb79f8fad60511c89957c4970cc2a5cfc8432e/torchvision/prototype/models/resnet.py#L94-L103) (such as the labels, the accuracy, links to recipe etc) and the preprocessing transforms necessary for using the models. Example:\n\n```python\n from PIL import Image\n from torchvision import prototype as P\n img = Image.open(\"test/assets/encode_jpeg/grace_hopper_517x606.jpg\")\n \u00a0\n # Initialize model\n weights = P.models.ResNet50_Weights.IMAGENET1K_V2\n model = P.models.resnet50(weights=weights)\n model.eval()", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "# Initialize inference transforms\n preprocess = weights.transforms()\n \u00a0\n # Apply inference preprocessing transforms\n batch = preprocess(img).unsqueeze(0)\n prediction = model(batch).squeeze(0).softmax(0)\n \u00a0\n # Make predictions\n label = prediction.argmax().item()\n score = prediction[label].item()\n \u00a0\n # Use meta to get the labels\n category_name = weights.meta['categories'][label]\n print(f\"{category_name}: {100 * score}%\")\n```\n\n## Using the legacy API\n\nThose who don\u2019t want to use a prototype API have the option of accessing the new weights via the legacy API using the following approach:\n\n```python\n from torchvision.models import resnet\n \u00a0\n # Overwrite the URL of the previous weights\n resnet.model_urls[\"resnet50\"] = \"https://download.pytorch.org/models/resnet50-11ad3fa6.pth\"\n \u00a0\n # Initialize the model using the legacy API\n model = resnet.resnet50(pretrained=True)\n \u00a0\n # TODO: Apply preprocessing + call the model\n # ...\n```\n\n## The Training Recipe", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "Our goal was to use the newly introduced primitives of TorchVision to derive a new strong training recipe which achieves state-of-the-art results for the vanilla ResNet50 architecture when trained from scratch on ImageNet with no additional external data. Though by using architecture specific tricks\u00a0[[2]](https://arxiv.org/abs/1812.01187) one could further improve the accuracy, we\u2019ve decided not to include them so that the recipe can be used in other architectures. Our recipe\u00a0heavily focuses on simplicity and builds upon work by FAIR [[3]](https://arxiv.org/abs/2103.06877), [[4]](https://arxiv.org/abs/2106.14881), [[5]](https://arxiv.org/abs/1906.06423), [[6]](https://arxiv.org/abs/2012.12877), [[7]](https://arxiv.org/abs/2110.00476).\u00a0Our findings align with the\u00a0parallel study of Wightman et al. [[7]](https://arxiv.org/abs/2110.00476), who also report major accuracy improvements by focusing on the training recipes.\n\nWithout further ado, here are the main parameters of our recipe:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "```python\n # Optimizer & LR scheme\n ngpus=8,\n batch_size=128,\u00a0 # per GPU\n\n epochs=600, \n opt='sgd', \u00a0\n momentum=0.9,\n\n lr=0.5, \n lr_scheduler='cosineannealinglr', \n lr_warmup_epochs=5, \n lr_warmup_method='linear', \n lr_warmup_decay=0.01, \n\n\n # Regularization and Augmentation\n weight_decay=2e-05, \n norm_weight_decay=0.0,\n\n label_smoothing=0.1, \n mixup_alpha=0.2, \n cutmix_alpha=1.0, \n auto_augment='ta_wide', \n random_erase=0.1, \n \n ra_sampler=True,\n ra_reps=4,\n\n\n # EMA configuration\n model_ema=True, \n model_ema_steps=32, \n model_ema_decay=0.99998, \n\n\n # Resizing\n interpolation='bilinear', \n val_resize_size=232, \n val_crop_size=224, \n train_crop_size=176,\n```\n\nUsing our standard [training reference script](https://github.com/pytorch/vision/tree/main/references/classification), we can train a ResNet50 using the following command:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "```\ntorchrun --nproc_per_node=8 train.py --model resnet50 --batch-size 128 --lr 0.5 \\\n--lr-scheduler cosineannealinglr --lr-warmup-epochs 5 --lr-warmup-method linear \\\n--auto-augment ta_wide --epochs 600 --random-erase 0.1 --weight-decay 0.00002\u00a0\\\n--norm-weight-decay 0.0 --label-smoothing 0.1 --mixup-alpha 0.2 --cutmix-alpha 1.0\u00a0\\\n--train-crop-size 176 --model-ema --val-resize-size 232 --ra-sampler --ra-reps 4\n```\n\n## Methodology\n\nThere are a few principles we kept in mind during our explorations:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "1. Training is a stochastic process and the validation metric we try to optimize is a random variable. This is due to the random weight initialization scheme employed and the existence of random effects during the training process. This means that we can\u2019t do a single run to assess the effect of a recipe change. The standard practice is doing multiple runs (usually 3 to 5) and studying the summarization stats (such as mean, std, median, max, etc).\n2. There is usually a significant interaction between different parameters, especially for techniques that focus on Regularization and reducing overfitting. Thus changing the value of one can have effects on the optimal configurations of others. To account for that one can either adopt a greedy search approach (which often leads to suboptimal results but tractable experiments) or apply grid search (which leads to better results but is computationally expensive). In this work, we used a mixture of both.\n3. Techniques that are non-deterministic or introduce noise usually require longer training cycles to improve model performance. To keep things tractable, we initially used short training cycles (small number of epochs) to decide which paths can be eliminated early and which should be explored using longer training.\n4. There is a risk of overfitting the validation dataset [[8]](https://arxiv.org/abs/1902.10811) because of the repeated experiments. To mitigate some of the risk, we apply only training optimizations that provide a significant accuracy improvements and use K-fold cross validation to verify optimizations done on the validation set. Moreover we confirm that our recipe ingredients generalize well on other models for which we didn\u2019t optimize the hyper-parameters.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Break down of key accuracy improvements\n\nAs discussed in\u00a0[earlier blogposts](https://pytorch.org/blog/torchvision-ssdlite-implementation/#break-down-of-key-accuracy-improvements), training models is not a journey of monotonically increasing accuracies and the process involves a lot of backtracking. To quantify the effect of each optimization, below we attempt to show-case an idealized linear journey of deriving the final recipe starting from the original recipe of TorchVision. We would like to clarify that this is an oversimplification of the actual path we followed and thus it should be taken with a grain of salt.\u00a0\n\n

\n\"Cumulative\n

\n\nIn the table below, we provide a summary of the performance of stacked incremental improvements on top of Baseline. Unless denoted otherwise, we report the model with best Acc@1 out of 3 runs:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "| | Accuracy@1 | Accuracy@5| Incremental Diff|Absolute Diff|\n|----------|:--------:|:----------:|:---------|:--------:|\n| ResNet50 Baseline |76.130 | 92.862| 0.000|0.000|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + LR optimizations | 76.494 |93.198| 0.364|0.364\n|----------|:--------:|:----------:|:---------|:--------:|\n| + TrivialAugment | 76.806| 93.272|0.312| 0.676|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Long Training | 78.606| 94.052| 1.800|2.476|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Random Erasing | 78.796 | 94.094|0.190|2.666\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Label Smoothing |79.114| 94.374| 0.318|2.984|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Mixup | 79.232| 94.536| 0.118|3.102|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Cutmix |79.510| 94.642| 0.278|3.380|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Weight Decay tuning |80.036|94.746| 0.526|3.906|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + FixRes mitigations |80.196|94.672| 0.160|4.066|\n|----------|:--------:|:----------:|:---------|:--------:|\n|+ EMA |80.450|94.908| 0.254|4.320|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Inference Resize tuning * |80.674|95.166| 0.224|4.544|\n|----------|:--------:|:----------:|:---------|:--------:|\n| + Repeated Augmentation ** |80.858|95.434| 0.184|4.728|", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "*The tuning of the inference size was done on top of the last model. See below for details.\n\n** Community contribution done after the release of the article. See below for details.\n\n## Baseline\n\nOur baseline is the previously released ResNet50 model of TorchVision. It was trained with the following recipe:\n\n```python \n # Optimizer & LR scheme\n ngpus=8,\n batch_size=32,\u00a0 # per GPU\n\n epochs=90, \n opt='sgd', \u00a0\n momentum=0.9,\n\n lr=0.1, \n lr_scheduler='steplr', \n lr_step_size=30, \n lr_gamma=0.1, \n\n\n # Regularization\n weight_decay=1e-4,\n\n\n # Resizing\n interpolation='bilinear', \n val_resize_size=256, \n val_crop_size=224, \n train_crop_size=224,\n```\n\nMost of the above parameters are the defaults on our [training scripts](https://github.com/pytorch/vision/tree/main/references/classification). We will start building on top of this baseline by introducing optimizations until we gradually arrive at the final recipe.\n\n## LR optimizations", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## LR optimizations\n\nThere are a few parameter updates we can apply to improve both the accuracy and the speed of our training. This can be achieved by increasing the batch size and tuning the LR. Another common method is to apply warmup and gradually increase our learning rate. This is beneficial especially when we use very high learning rates and helps with the stability of the training in the early epochs. Finally, another optimization is to apply Cosine Schedule to adjust our LR during the epochs. A big advantage of cosine is that there are no hyper-parameters to optimize, which cuts down our search space.\n\nHere are the additional optimizations applied on top of the baseline recipe. Note that we\u2019ve run multiple experiments to determine the optimal configuration of the parameters:\n\n```python\n batch_size=128,\u00a0 # per GPU\n\n lr=0.5, \n lr_scheduler='cosineannealinglr', \n lr_warmup_epochs=5, \n lr_warmup_method='linear', \n lr_warmup_decay=0.01,\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "The above optimizations increase our top-1 Accuracy by 0.364 points comparing to the baseline. Note that in order to combine the different LR strategies we use the newly introduced [SequentialLR](https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.SequentialLR.html#torch.optim.lr_scheduler.SequentialLR) scheduler.\n\n## TrivialAugment\n\nThe original model was trained using basic augmentation transforms such as Random resized crops and horizontal flips. An easy way to improve our accuracy is to apply more complex \u201cAutomatic-Augmentation\u201d techniques. The one that performed best for us is TrivialAugment\u00a0[[9]](https://arxiv.org/abs/2103.10158), which\u00a0is extremely simple and can be considered \u201cparameter free\u201d, which means it can help us cut down our search space further.\n\nHere is the update applied on top of the previous step:\n\n```\nauto_augment='ta_wide',\n```\n\nThe use of TrivialAugment increased our top-1 Accuracy by\u00a00.312 points\u00a0compared to the previous step.\n\n## Long Training", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Long Training\n\nLonger training cycles are beneficial when our recipe contains ingredients that behave randomly. More specifically as we start adding more and more techniques that introduce noise, increasing the number of epochs becomes crucial. Note that at early stages of our exploration, we used relatively short cycles of roughly 200 epochs which was later increased to 400 as we started narrowing down most of the parameters and finally increased to 600 epochs at the final versions of the recipe.\n\nBelow we see the update applied on top of the earlier steps:\n\n```\nepochs=600,\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "```\nepochs=600,\n```\n\nThis further increases our top-1 Accuracy by 1.8 points on top of the previous step. This is the biggest increase we will observe in this iterative process. It\u2019s worth noting that the effect of this single optimization is overstated and somehow misleading. Just increasing the number of epochs on top of the old baseline won\u2019t yield such significant improvements. Nevertheless the combination of the LR optimizations with strong Augmentation strategies helps the model benefit from longer cycles. It\u2019s also worth mentioning that the reason we introduce the lengthy training cycles so early in the process is because in the next steps we will introduce techniques that\u00a0require significantly more epochs to provide good results.\n\n## Random Erasing", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Random Erasing\n\nAnother data augmentation technique known to help the classification accuracy is Random Erasing [[10]](https://arxiv.org/abs/1708.04896), [[11]](https://arxiv.org/abs/1708.04552). Often paired with Automatic Augmentation methods, it usually yields additional improvements in accuracy due to its regularization effect. In our experiments we tuned only the probability of applying the method via a grid search and found that it\u2019s beneficial to keep its probability at low levels, typically around 10%.\u00a0\n\nHere is the extra parameter introduced on top of the previous:\n\n```\nrandom_erase=0.1,\n```\n\nApplying Random Erasing increases our Acc@1 by further\u00a00.190 points.\n\n## Label Smoothing", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Label Smoothing\n\nA good technique to reduce overfitting is to stop the model from becoming overconfident. This can be achieved by softening the ground truth using Label Smoothing\u00a0[[12]](https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Szegedy_Rethinking_the_Inception_CVPR_2016_paper.pdf). There is a single parameter which controls the degree of smoothing (the higher the stronger) that we need to specify. Though optimizing it via grid search is possible, we found that values around 0.05-0.15 yield similar results, so to avoid overfitting it we used the same value as on the\u00a0paper that introduced it.\n\nBelow we can find the extra config added on this step:\n\n```\nlabel_smoothing=0.1,\n```\n\nWe use PyTorch\u2019s newly introduced\u00a0[CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=label_smoothing) label_smoothing parameter and that increases our accuracy by an additional\u00a00.318 points.\n\n## Mixup and Cutmix", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Mixup and Cutmix\n\nTwo data augmentation techniques often used to produce SOTA results are Mixup and Cutmix\u00a0[[13]](https://arxiv.org/abs/1710.09412), [[14]](https://arxiv.org/abs/1905.04899). They both provide strong regularization effects by softening not only the labels but also the images. In our setup we found it beneficial to apply one of them randomly with equal probability. Each is parameterized with a\u00a0hyperparameter alpha, which controls the shape of the Beta distribution from which the smoothing probability is sampled. We did a very limited grid search, focusing primarily on common values proposed on the papers.\u00a0\n\nBelow you will find the optimal values for the alpha parameters of the two techniques:\n\n```\nmixup_alpha=0.2, \ncutmix_alpha=1.0,\n```\n\nApplying mixup increases our accuracy by\u00a00.118 points and combining it with cutmix improves it by additional 0.278 points.\n\n## Weight Decay tuning", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "Our standard recipe uses L2 regularization to reduce overfitting. The Weight Decay parameter controls the degree of the regularization (the larger the stronger) and is applied universally to all learned parameters of the model by default. In this recipe, we apply two optimizations to the standard approach. First we perform grid search to tune the parameter of weight decay and second we disable weight decay for the parameters of the normalization layers.\u00a0\n\nBelow you can find the optimal configuration of weight decay for our recipe:\n\n```\nweight_decay=2e-05, \nnorm_weight_decay=0.0,\n```\n\nThe above update improves our accuracy by a further\u00a00.526 points, providing additional experimental evidence for a known fact that tuning weight decay has significant effects on the performance of the model. Our approach for separating the Normalization parameters from the rest was inspired by\u00a0[ClassyVision\u2019s](https://github.com/facebookresearch/ClassyVision) approach.\n\n## FixRes mitigations", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "An important property identified early in our experiments is the fact that the models performed significantly better if the resolution used during validation was increased from the 224x224 of training. This effect is studied in detail on the FixRes paper [[5]](https://arxiv.org/abs/1906.06423)\u00a0and two mitigations are proposed: a) one could try to reduce the training resolution so that the accuracy on the validation resolution is maximized or b) one could fine-tune the model on a two-phase training so that it adjusts on the target resolution. Since we didn\u2019t want to introduce a 2-phase training, we went for option a). This means that we reduced the train crop size from 224 and used grid search to find the one that maximizes the validation on resolution of 224x224.\n\nBelow you can see the optimal value used on our recipe:\n\n```\nval_crop_size=224, \ntrain_crop_size=176,\n```\n\nThe above optimization improved our accuracy by an additional 0.160 points and sped up our training by 10%.", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "It\u2019s worth noting that the FixRes effect still persists, meaning that the model continues to perform better on validation when we increase the resolution. Moreover, further reducing the training crop-size actually hurts the accuracy. This intuitively makes sense because one can only reduce the resolution so much before critical details start disappearing from the picture. Finally, we should note that the above FixRes mitigation seems to benefit models with similar depth to ResNet50. Deeper variants with larger receptive fields seem to be slightly negatively affected (typically by 0.1-0.2 points). Hence we consider this part of the recipe optional. Below we visualize the performance of the best available checkpoints (with the full recipe) for models trained with 176 and 224 resolution:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "
\n\"Best\n\"Best\n
\n\n## Exponential Moving Average (EMA)\n\nEMA is a technique that allows one to push the accuracy of a model without increasing its complexity or inference time. It performs an exponential moving average on the model weights and this leads to increased accuracy and more stable models. The averaging happens every few iterations and its decay parameter was tuned via grid search.\u00a0\n\nBelow you can see the optimal values for our recipe:\n\n```\nmodel_ema=True, \nmodel_ema_steps=32, \nmodel_ema_decay=0.99998,\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "The use of EMA increases our accuracy by\u00a00.254 points comparing to the previous step. Note that TorchVision\u2019s\u00a0[EMA implementation](https://github.com/pytorch/vision/pull/4406) is build on top of PyTorch\u2019s [AveragedModel](https://pytorch.org/docs/stable/optim.html#stochastic-weight-averaging) class with the key difference being that it averages not only the model parameters but also its buffers. Moreover, we have adopted tricks from\u00a0[Pycls](https://github.com/facebookresearch/pycls/tree/main/pycls)\u00a0which allow us to parameterize the decay in a way that doesn\u2019t depend on the number of epochs.\n\n## Inference Resize tuning", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "Unlike all other steps of the process which involved training models with different parameters, this optimization was done on top of the final model. During inference, the image is resized to a specific resolution and then a central 224x224 crop is taken from it. The original recipe used a resize size of 256, which caused a similar discrepancy as the one described on the FixRes paper [[5]](https://arxiv.org/abs/1906.06423). By bringing this resize value closer to the target inference resolution, one can improve the accuracy. To select the value we run a short grid search between interval [224, 256] with step of 8. To avoid overfitting, the value was selected using half of the validation set and confirmed using the other half.\n\nBelow you can see the optimal value used on our recipe:\n\n```\nval_resize_size=232,\n```", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "The above is an optimization which improved our accuracy by\u00a00.224 points.\u00a0It\u2019s worth noting that the optimal value for ResNet50 works also best for ResNet101, ResNet152 and ResNeXt50, which hints that it generalizes across models:\n\n\n
\n\"ResNet50\n\"ResNet101\n\"Best\n
\n\n## [UPDATE] Repeated Augmentation", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "Repeated Augmentation [[15]](https://arxiv.org/abs/1901.09335), [[16]](https://arxiv.org/abs/1902.05509) is another technique which can improve the overall accuracy and has been used by other strong recipes such as those at [[6]](https://arxiv.org/abs/2012.12877), [[7]](https://arxiv.org/abs/2110.00476). Tal Ben-Nun, a community contributor, has [further improved](https://github.com/pytorch/vision/pull/5201) upon our original recipe by proposing training the model with 4 repetitions. His contribution came after the release of this article.\n\nBelow you can see the optimal value used on our recipe:\n\n```\nra_sampler=True,\nra_reps=4,\n```\n\nThe above is the final optimization which improved our accuracy by\u00a00.184 points.\u00a0\n\n## Optimizations that were tested but not adopted", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "During the early stages of our research, we experimented with additional techniques, configurations and optimizations. Since our target was to keep our recipe as simple as possible, we decided not to include anything that didn\u2019t provide a significant improvement. Here are a few approaches that we took but didn\u2019t make it to our final recipe:", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "- **Optimizers:** Using more complex optimizers such as Adam, RMSProp or SGD with Nesterov momentum didn\u2019t\u00a0provide significantly better results than vanilla SGD with momentum.\n- **LR Schedulers:**\u00a0We tried different LR Scheduler schemes such as StepLR and Exponential. Though the latter tends to work better with EMA, it often requires additional hyper-parameters such as defining the minimum LR to work well. Instead, we just use cosine annealing decaying the LR up to zero and choose the checkpoint with the highest accuracy.\n- **Automatic Augmentations:**\u00a0We\u2019ve tried different augmentation strategies such as AutoAugment and RandAugment. None of these outperformed the simpler parameter-free TrivialAugment.\n- **Interpolation:** Using bicubic or nearest interpolation didn\u2019t\u00a0provide significantly better results than bilinear.\n- **Normalization layers:** Using Sync Batch Norm didn\u2019t yield\u00a0significantly better results than using the regular Batch Norm.\n\n## Acknowledgements", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "## Acknowledgements\n\nWe would like to thank\u00a0Piotr Dollar, Mannat Singh and Hugo Touvron for providing their insights and feedback during the development of the recipe and for their previous research work on which our recipe is based on. Their support was invaluable for achieving the above result. Moreover, we would like to thank\u00a0Prabhat Roy, Kai Zhang, Yiwen Song, Joel Schlosser, Ilqar Ramazanli, Francisco Massa, Mannat Singh, Xiaoliang Dai, Samuel Gabriel, Allen Goodman and Tal Ben-Nun for their contributions to the Batteries Included project.\n\n## References", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "1. Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. \u201cDeep Residual Learning for Image Recognition\u201d.\n2. Tong He, Zhi Zhang, Hang Zhang, Zhongyue Zhang, Junyuan Xie, Mu Li. \u201cBag of Tricks for Image Classification with Convolutional Neural Networks\u201d\n3. Piotr Doll\u00e1r, Mannat Singh, Ross Girshick. \u201cFast and Accurate Model Scaling\u201d\n4. Tete Xiao, Mannat Singh, Eric Mintun, Trevor Darrell, Piotr Doll\u00e1r, Ross Girshick. \u201cEarly Convolutions Help Transformers See Better\u201d\n5. Hugo Touvron, Andrea Vedaldi, Matthijs Douze, Herv\u00e9 J\u00e9gou. \u201cFixing the train-test resolution discrepancy\n6. Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Herv\u00e9 J\u00e9gou. \u201cTraining data-efficient image transformers & distillation through attention\u201d\n7. Ross Wightman, Hugo Touvron, Herv\u00e9 J\u00e9gou. \u201cResNet strikes back: An improved training procedure in timm\u201d\n8. Benjamin Recht, Rebecca Roelofs, Ludwig Schmidt, Vaishaal Shankar. \u201cDo ImageNet Classifiers Generalize to ImageNet?\u201d\n9. Samuel G. M\u00fcller, Frank Hutter. \u201cTrivialAugment: Tuning-free Yet State-of-the-Art Data Augmentation\u201d\n10. Zhun Zhong, Liang Zheng, Guoliang Kang, Shaozi Li, Yi Yang. \u201cRandom Erasing Data Augmentation\u201d\n11. Terrance DeVries, Graham W. Taylor. \u201cImproved Regularization of Convolutional Neural Networks with Cutout\u201d\n12. Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jon Shlens, Zbigniew Wojna. \u201cRethinking the Inception Architecture for Computer Vision\u201d\n13. Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz. \u201cmixup: Beyond Empirical Risk Minimization\u201d\n14. Sangdoo Yun, Dongyoon Han, Seong Joon Oh, Sanghyuk Chun, Junsuk Choe, Youngjoon Yoo. \u201cCutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features\u201d\n15. Elad Hoffer, Tal Ben-Nun, Itay Hubara, Niv Giladi, Torsten Hoefler, Daniel Soudry. \u201cAugment your batch: better training with larger batches\u201d\n16. Maxim Berman, Herv\u00e9 J\u00e9gou, Andrea Vedaldi, Iasonas Kokkinos, Matthijs Douze. \u201cMultigrain: a unified image embedding for classes and instances\u201d", "metadata": {"source": "https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: 'Updates & Improvements to PyTorch Tutorials'\nauthor: Team PyTorch\n---\n\nPyTorch.org provides researchers and developers with documentation, installation instructions, latest news, community projects, tutorials, and more. Today, we are introducing usability and content improvements including tutorials in additional categories, a new recipe format for quickly referencing common topics, sorting using tags, and an updated homepage. \n\nLet\u2019s take a look at them in detail. \n\n## TUTORIALS HOME PAGE UPDATE\nThe tutorials home page now provides clear actions that developers can take. For new PyTorch users, there is an easy-to-discover button to take them directly to \u201cA 60 Minute Blitz\u201d. Right next to it, there is a button to view all recipes which are designed to teach specific features quickly with examples. \n\n
\n \n
", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "In addition to the existing left navigation bar, tutorials can now be quickly filtered by multi-select tags. Let\u2019s say you want to view all tutorials related to \u201cProduction\u201d and \u201cQuantization\u201d. You can select the \u201cProduction\u201d and \u201cQuantization\u201d filters as shown in the image shown below:\n\n
\n \n
\n\nThe following additional resources can also be found at the bottom of the Tutorials homepage:\n* [PyTorch Cheat Sheet](https://pytorch.org/tutorials/beginner/ptcheat.html)\n* [PyTorch Examples](https://github.com/pytorch/examples)\n* [Tutorial on GitHub](https://github.com/pytorch/tutorials)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## PYTORCH RECIPES \nRecipes are new bite-sized, actionable examples designed to teach researchers and developers how to use specific PyTorch features. Some notable new recipes include:\n* [Loading Data in PyTorch](https://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html)\n* [Model Interpretability Using Captum](https://pytorch.org/tutorials/recipes/recipes/Captum_Recipe.html)\n* [How to Use TensorBoard](https://pytorch.org/tutorials/recipes/recipes/tensorboard_with_pytorch.html)\n\nView the full recipes [here](http://pytorch.org/tutorials/recipes/recipes_index.html).", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## LEARNING PYTORCH\nThis section includes tutorials designed for users new to PyTorch. Based on community feedback, we have made updates to the current [Deep Learning with PyTorch: A 60 Minute Blitz](https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html) tutorial, one of our most popular tutorials for beginners. Upon completion, one can understand what PyTorch and neural networks are, and be able to build and train a simple image classification network. Updates include adding explanations to clarify output meanings and linking back to where users can read more in the docs, cleaning up confusing syntax errors, and reconstructing and explaining new concepts for easier readability.", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## DEPLOYING MODELS IN PRODUCTION\nThis section includes tutorials for developers looking to take their PyTorch models to production. The tutorials include:\n* [Deploying PyTorch in Python via a REST API with Flask](https://pytorch.org/tutorials/intermediate/flask_rest_api_tutorial.html)\n* [Introduction to TorchScript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html)\n* [Loading a TorchScript Model in C++](https://pytorch.org/tutorials/advanced/cpp_export.html)\n* [Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## FRONTEND APIS\nPyTorch provides a number of frontend API features that can help developers to code, debug, and validate their models more efficiently. This section includes tutorials that teach what these features are and how to use them. Some tutorials to highlight: \n* [Introduction to Named Tensors in PyTorch](https://pytorch.org/tutorials/intermediate/named_tensor_tutorial.html)\n* [Using the PyTorch C++ Frontend](https://pytorch.org/tutorials/advanced/cpp_frontend.html)\n* [Extending TorchScript with Custom C++ Operators](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html)\n* [Extending TorchScript with Custom C++ Classes](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html)\n* [Autograd in C++ Frontend](https://pytorch.org/tutorials/advanced/cpp_autograd.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## MODEL OPTIMIZATION\nDeep learning models often consume large amounts of memory, power, and compute due to their complexity. This section provides tutorials for model optimization:\n* [Pruning](https://pytorch.org/tutorials/intermediate/pruning_tutorial.html)\n* [Dynamic Quantization on BERT](https://pytorch.org/tutorials/intermediate/dynamic_quantization_bert_tutorial.html)\n* [Static Quantization with Eager Mode in PyTorch](https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html)", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "## PARALLEL AND DISTRIBUTED TRAINING\nPyTorch provides features that can accelerate performance in research and production such as native support for asynchronous execution of collective operations and peer-to-peer communication that is accessible from Python and C++. This section includes tutorials on parallel and distributed training: \n* [Single-Machine Model Parallel Best Practices](https://pytorch.org/tutorials/intermediate/model_parallel_tutorial.html)\n* [Getting started with Distributed Data Parallel](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)\n* [Getting started with Distributed RPC Framework](https://pytorch.org/tutorials/intermediate/rpc_tutorial.html)\n* [Implementing a Parameter Server Using Distributed RPC Framework](https://pytorch.org/tutorials/intermediate/rpc_param_server_tutorial.html)\n\nMaking these improvements are just the first step of improving PyTorch.org for the community. Please submit your suggestions [here](https://github.com/pytorch/tutorials/pulls).\n\nCheers,", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "Cheers,\n\nTeam PyTorch", "metadata": {"source": "https://pytorch.org/blog/updates-improvements-to-pytorch-tutorials/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"PyTorch 2.0: Our next generation release that is faster, more Pythonic and Dynamic as ever\"\n---\n\nWe are excited to announce the release of [PyTorch\u00ae 2.0](https://github.com/pytorch/pytorch/releases/tag/v2.0.0) which we highlighted during the [PyTorch Conference](https://www.youtube.com/@PyTorch/playlists?view=50&sort=dd&shelf_id=2) on 12/2/22! PyTorch 2.0 offers the same eager-mode development and user experience, while fundamentally changing and supercharging how PyTorch operates at compiler level under the hood with faster performance and support for Dynamic Shapes and Distributed.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "This next-generation release includes a Stable version of Accelerated Transformers (formerly called Better Transformers); Beta includes torch.compile as the main API for PyTorch 2.0, the scaled_dot_product_attention function as part of torch.nn.functional, the MPS backend, functorch APIs in the torch.func module; and other Beta/Prototype improvements across various inferences, performance and training optimization features on GPUs and CPUs. For a comprehensive introduction and technical overview of torch.compile, please visit the 2.0 [Get Started page](/get-started/pytorch-2.0).\n\nAlong with 2.0, we are also releasing a series of beta updates to the PyTorch domain libraries, including those that are in-tree, and separate libraries including TorchAudio, TorchVision, and TorchText. An update for TorchX is also being released as it moves to community supported mode. More details can be found in this [library blog](/blog/new-library-updates-in-pytorch-2.0/).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} {"page_content": "This release is composed of over 4,541 commits and 428 contributors since 1.13.1. We want to sincerely thank our dedicated community for your contributions. As always, we encourage you to try these out and report any issues as we improve 2.0 and the overall 2-series this year.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Summary: \n* torch.compile is the main API for PyTorch 2.0, which wraps your model and returns a compiled model. It is a fully additive (and optional) feature and hence 2.0 is 100% backward compatible by definition.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* As an underpinning technology of torch.compile, TorchInductor with Nvidia and AMD GPUs will rely on OpenAI Triton deep learning compiler to generate performant code and hide low level hardware details. OpenAI Triton-generated kernels achieve performance that's on par with hand-written kernels and specialized cuda libraries such as cublas.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Accelerated Transformers introduce high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA). The API is integrated with torch.compile() and model developers may also use the [scaled dot product attention](#beta-scaled-dot-product-attention-20) kernels directly by calling the new scaled_dot_product_attention() operator.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Metal Performance Shaders (MPS) backend provides GPU accelerated PyTorch training on Mac platforms with added support for Top 60 most used ops, bringing coverage to over 300 operators.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Amazon AWS optimizes the PyTorch CPU inference on AWS Graviton3 based [C7g instances](https://aws.amazon.com/blogs/aws/new-amazon-ec2-c7g-instances-powered-by-aws-graviton3-processors/). PyTorch 2.0 improves inference performance on Graviton compared to the previous releases, including improvements for Resnet50 and Bert. \n* New prototype features and technologies across TensorParallel, DTensor, 2D parallel, TorchDynamo, AOTAutograd, PrimTorch and TorchInductor.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\nStable\n Beta\n Prototype\n Performance Improvements\n
\n\nAccelerated PT 2 Transformers\n \n\ntorch.compile\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "DTensor\n \n\nCUDA support for 11.7 & 11.8 (deprecating CUDA 11.6) \n
\n \n\nPyTorch MPS Backend\n \n\nTensorParallel\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Python 3.8 (deprecating Python 3.7)\n
\n \n\nScaled dot product attention\n \n\n2D Parallel\n \n\nAWS Graviton3\n
\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "functorch\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Torch.compile (dynamic=True)\n \n
\n Dispatchable Collectives\n \n
\n Torch.set_default & torch.device\n \n \n
\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "X86 quantization backend\n \n \n
\n \n\nGNN inference and training performance\n \n \n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "*To see a full list of public 2.0, 1.13 and 1.12 feature submissions click [here](https://docs.google.com/spreadsheets/d/1H3jazwO8BBCwK8JwLNYspLiHfUrzshEtyqjL-X93I9g/edit#gid=790902532).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Stable Features", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Stable] Accelerated PyTorch 2 Transformers", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "The PyTorch 2.0 release includes a new high-performance implementation of the PyTorch Transformer API. In releasing Accelerated PT2 Transformers, our goal is to make training and deployment of state-of-the-art Transformer models affordable across the industry. This release introduces high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA), extending the inference \u201cfastpath\u201d architecture, previously known as \"Better Transformer.\"", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Similar to the \u201cfastpath\u201d architecture, custom kernels are fully integrated into the PyTorch Transformer API \u2013 thus, using the native Transformer and MultiHeadAttention API will enable users to:\n\n* transparently see significant speed improvements; \n* support many more use cases including models using Cross-Attention, Transformer Decoders, and for training models; and\n* continue to use fastpath inference for fixed and variable sequence length Transformer Encoder and Self Attention use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "To take full advantage of different hardware models and Transformer use cases, multiple SDPA custom kernels are supported (see below), with custom kernel selection logic that will pick the highest-performance kernel for a given model and hardware type. In addition to the existing Transformer API, model developers may also use the [scaled dot product attention](#beta-scaled-dot-product-attention-20) kernels directly by calling the new scaled_dot_product_attention() operator. Accelerated PyTorch 2", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Transformers are integrated with torch.compile() . To use your model while benefiting from the additional acceleration of PT2-compilation (for inference or training), pre-process the model with `model = torch.compile(model)`.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "We have achieved major speedups for training transformer models and in particular large language models with Accelerated PyTorch 2 Transformers using a combination of custom kernels and torch.compile().", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "![alt_text](/assets/images/pytorch20post.png \"Accelerated PyTorch 2 speed\"){:width=\"100%\"}\nFigure: Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Beta Features", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] torch.compile\n\ntorch.compile is the main API for PyTorch 2.0, which wraps your model and returns a compiled model. It is a fully additive (and optional) feature and hence 2.0 is 100% backward compatible by definition.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Underpinning torch.compile are new technologies \u2013 TorchDynamo, AOTAutograd, PrimTorch and TorchInductor:\n* TorchDynamo captures PyTorch programs safely using Python Frame Evaluation Hooks and is a significant innovation that was a result of 5 years of our R&D into safe graph capture. \n* AOTAutograd overloads PyTorch\u2019s autograd engine as a tracing autodiff for generating ahead-of-time backward traces.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* PrimTorch canonicalizes ~2000+ PyTorch operators down to a closed set of ~250 primitive operators that developers can target to build a complete PyTorch backend. This substantially lowers the barrier of writing a PyTorch feature or backend.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* TorchInductor is a deep learning compiler that generates fast code for multiple accelerators and backends. For NVIDIA and AMD GPUs, it uses OpenAI Triton as a key building block. For intel CPUs, we generate C++ code using multithreading, vectorized instructions and offloading appropriate operations to mkldnn when possible.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "With all the new technologies, torch.compile is able to work 93% of time across 165 open-source models and runs 20% faster on average at float32 precision and 36% faster on average at AMP precision.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "For more information, please refer to [https://pytorch.org/get-started/pytorch-2.0/](https://pytorch.org/get-started/pytorch-2.0/) and for TorchInductor CPU with Intel [here](https://dev-discuss.pytorch.org/t/torchinductor-update-5-cpu-backend-backend-performance-update-and-deep-dive-on-key-optimizations/1117).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] PyTorch MPS Backend\n\nMPS backend provides GPU-accelerated PyTorch training on Mac platforms. This release brings improved correctness, stability, and operator coverage.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "MPS backend now includes support for the Top 60 most used ops, along with the most frequently requested operations by the community, bringing coverage to over 300 operators. The major focus of the release was to enable full OpInfo-based forward and gradient mode testing to address silent correctness issues. These changes have resulted in wider adoption of MPS backend by 3rd party networks such as Stable Diffusion, YoloV5, WhisperAI, along with increased coverage for Torchbench networks and Basic tutorials.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "We encourage developers to update to the latest macOS release to see the best performance and stability on the MPS backend.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Links", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "1. [MPS Backend](https://pytorch.org/docs/stable/notes/mps.html)\n2. [Developer information](https://github.com/pytorch/pytorch/wiki/MPS-Backend)\n3. [Accelerated PyTorch training on Mac](https://developer.apple.com/metal/pytorch/)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "4. [Metal](https://developer.apple.com/documentation/metal?language=objc), [Metal Performance Shaders](https://developer.apple.com/documentation/metalperformanceshaders?language=objc) & [Metal Performance Shaders Graph](https://developer.apple.com/documentation/metalperformanceshadersgraph?language=objc)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Scaled dot product attention 2.0 \n\nWe are thrilled to announce the release of PyTorch 2.0, which introduces a powerful scaled dot product attention function as part of torch.nn.functional. This function includes multiple implementations that can be seamlessly applied depending on the input and hardware in use.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "In previous versions of PyTorch, you had to rely on third-party implementations and install separate packages to take advantage of memory-optimized algorithms like [FlashAttention](https://github.com/HazyResearch/flash-attention). With PyTorch 2.0, all these implementations are readily available by default.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "These implementations include [FlashAttention](https://arxiv.org/abs/2205.14135) from HazyResearch, Memory-Efficient Attention from the [xFormers](https://github.com/facebookresearch/xformers) project, and a native C++ implementation that is ideal for non-CUDA devices or when high-precision is required.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 will automatically select the optimal implementation for your use case, but you can also toggle them individually for finer-grained control. Additionally, the scaled dot product attention function can be used to build common transformer architecture components.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Learn more with the [documentation](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html?highlight=scaled_dot_product#torch.nn.functional.scaled_dot_product_attention) and this [tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] functorch -> torch.func", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Inspired by [Google JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples include:\n* [model ensembling](https://pytorch.org/tutorials/intermediate/ensembling.html)\n* [efficiently computing jacobians and hessians](https://pytorch.org/tutorials/intermediate/jacobians_hessians.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* [computing per-sample-gradients (or other per-sample quantities)](https://pytorch.org/tutorials/intermediate/per_sample_grads.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "We\u2019re excited to announce that, as the final step of upstreaming and integrating functorch into PyTorch, the functorch APIs are now available in the torch.func module. Our function transform APIs are identical to before, but we have changed how the interaction with NN modules work. Please see the [docs](https://pytorch.org/docs/master/func.html) and the [migration guide](https://pytorch.org/docs/master/func.migrating.html) for more details.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Furthermore, we have [added support for torch.autograd.Function](https://pytorch.org/docs/master/notes/extending.func.html): one is now able to apply function transformations (e.g. vmap, grad, jvp) over torch.autograd.Function.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Dispatchable Collectives", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Dispatchable collectives is an improvement to the existing init_process_group() API which changes backend to an optional argument. For users, the main advantage of this feature is that it will allow them to write code that can run on both GPU and CPU machines without having to change the backend specification. The dispatchability feature will also make it easier for users to support both GPU and CPU collectives, as they will no longer need to specify the backend manually (e.g. \u201cNCCL\u201d or \u201cGLOO\u201d). Existing", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "backend specifications by users will be honored and will not require change.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Usage example:\n```\nimport torch.distributed.dist\n\u2026\n# old\ndist.init_process_group(backend=\u201dnccl\u201d, ...)\ndist.all_reduce(...) # with CUDA tensors works\ndist.all_reduce(...) # with CPU tensors does not work\n\n# new\ndist.init_process_group(...) # backend is optional\ndist.all_reduce(...) # with CUDA tensors works\ndist.all_reduce(...) # with CPU tensors works", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Learn more [here](https://pytorch.org/docs/master/distributed.html#torch.distributed.init_process_group).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] torch.set_default_device and torch.device as context manager\n\ntorch.set_default_device allows users to change the default device that factory functions in PyTorch allocate on. For example, if you torch.set_default_device(\u2018cuda\u2019), a call to torch.empty(2) will allocate on CUDA (rather than on CPU). You can also use torch.device as a context manager to change the default device on a local basis. This resolves a long standing feature request from PyTorch\u2019s initial release for a way to do this.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Learn more [here](https://pytorch.org/tutorials/recipes/recipes/changing_default_device.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] \"X86\" as the new default quantization backend for x86 CPU", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "The new X86 quantization backend, which utilizes FBGEMM and oneDNN kernel libraries, replaces FBGEMM as the default quantization backend for x86 CPU platforms and offers improved int8 inference performance compared to the original FBGEMM backend, leveraging the strengths of both libraries, with 1.3X \u2013 2X inference performance speedup measured on 40+ deep learning models. The new backend is functionally compatible with the original FBGEMM backend.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "**Table: Geomean Speedup of X86 Quantization Backend vs. FBGEMM Backend**\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n 1 core/instance\n 2 cores/instance\n 4 cores/instance\n 1 socket (32 cores)/instance\n
Intel(R) Xeon(R) Platinum 8358 CPU @ 2.60GHz\n 1.76X\n 1.80X\n 2.04X\n 1.34X\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "By default, users on x86 platforms will utilize the x86 quantization backend and their PyTorch programs will remain unchanged when using the default backend. Alternatively, users have the option to specify \"X86\" as the quantization backend explicitly. Example code is shown below:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "```\nimport torch\nfrom torch.ao.quantization import get_default_qconfig_mappingfrom torch.quantization.quantize_fx\nimport prepare_fx, convert_fx\n \n# get default configuration\nqconfig_mapping = get_default_qconfig_mapping()\n \n# or explicitly specify the backend\n# qengine = 'x86'\n# torch.backends.quantized.engine = qengine\n# qconfig_mapping = get_default_qconfig_mapping(qengine)\n \n# construct fp32 model\nmodel_fp32 = ...\n \n# prepare\nprepared_model = prepare_fx(model_fp32, qconfig_mapping, example_inputs=x)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "# calibrate\n...\n \n# convert\nquantized_model = convert_fx(prepared_model)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Find more information: [https://github.com/pytorch/pytorch/issues/83888](https://github.com/pytorch/pytorch/issues/83888) and [https://www.intel.com/content/www/us/en/developer/articles/technical/accelerate-pytorch-int8-inf-with-new-x86-backend.html](https://www.intel.com/content/www/us/en/developer/articles/technical/accelerate-pytorch-int8-inf-with-new-x86-backend.html).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] GNN inference and training optimization on CPU", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "PyTorch 2.0 includes several critical optimizations to improve GNN inference and training performance on CPU. Before 2.0, GNN models of PyG suffers from low efficiency on CPU due to lack of performance tuning for several critical kernels (scatter/gather, etc) and the lack of GNN-related sparse matrix multiplication ops. To be specific, optimizations include:\n* scatter_reduce: performance hotspot in Message Passing when the edge index is stored in Coordinate format (COO).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* gather: backward of scatter_reduce, specially tuned for the GNN compute when the index is an expanded tensor.\n* torch.sparse.mm with reduce flag: performance hotspot in Message Passing when the edge index is stored in Compressed Sparse Row (CSR). Supported reduce flag of: sum, mean, amax, amin.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "On PyG benchmarks/examples, OGB benchmarks, a 1.12x - 4.07x performance speedup is measured (1.13.1 compared with 2.0) for single node inference and training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Model-Dataset\n Option\n Speedup Ratio\n
GCN-Reddit (inference)\n 512-2-64-dense\n 1.22x\n
1024-3-128-dense\n 1.25x\n
512-2-64-sparse\n 1.31x\n
1024-3-128-sparse\n 1.68x\n
512-2-64-dense\n 1.22x\n
\nGraphSage-ogbn-products (inference)\n 1024-3-128-dense\n 1.15x\n
512-2-64-sparse\n 1.20x\n
1024-3-128-sparse\n 1.33x\n
full-batch-sparse\n 4.07x\n
GCN-PROTEINS (training)\n 3-32\n 1.67x\n
GCN-REDDIT-BINARY (training)\n 3-32\n 1.67x\n
GCN-Reddit (training)\n 512-2-64-dense\n 1.20x\n
1024-3-128-dense\n 1.12x\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Learn more: [PyG CPU Performance Optimization](https://www.pyg.org/ns-newsarticle-accelerating-pyg-on-intel-cpus).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Beta] Accelerating inference on CPU with PyTorch by leveraging oneDNN Graph", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[oneDNN Graph API](https://spec.oneapi.io/onednn-graph/latest/introduction.html) extends [oneDNN](https://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html) with a flexible graph API to maximize the optimization opportunity for generating efficient code on AI hardware. \n* It automatically identifies the graph partitions to be accelerated via fusion.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* The [fusion patterns](https://github.com/oneapi-src/oneDNN/blob/dev-graph/doc/programming_model/ops_and_patterns.md#fusion-patterns) focus on fusing compute-intensive operations such as convolution, matmul and their neighbor operations for both inference and training use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Although work is ongoing to integrate oneDNN Graph with TorchDynamo as well, its integration with the PyTorch JIT Fuser attained beta status in PyTorch 2.0 for [Float32](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-float) & [BFloat16](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16) inference (on machines that support AVX512_BF16 ISA).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "From a developer\u2019s/researcher\u2019s perspective, the usage is quite simple & intuitive, with the only change in code being an API invocation:\n* Leverage oneDNN Graph, with [JIT-tracing](https://pytorch.org/docs/stable/generated/torch.jit.trace.html), a model is profiled with an example input. \n* The context manager _with torch.jit.fuser(\u201cfuser3\u201d):_ can also be used instead of invoking _torch.jit.enable_onednn_fusion(True)_.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* For accelerating [BFloat16 inference](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16), we rely on eager-mode AMP (Automatic Mixed Precision) support in PyTorch & disable JIT mode\u2019s AMP, as both of them are currently divergent:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "```\n# Assuming we have a model of the name 'model'\n \nexample_input = torch.rand(1, 3, 224, 224)\n \n# enable oneDNN Graph\ntorch.jit.enable_onednn_fusion(True)\n# Disable AMP for JIT\ntorch._C._jit_set_autocast_mode(False)\nwith torch.no_grad(), torch.cpu.amp.autocast():\n\tmodel = torch.jit.trace(model, (example_input))\n\tmodel = torch.jit.freeze(model)\n \t# 2 warm-ups (2 for tracing/scripting with an example, 3 without an example)\n\tmodel(example_input)\n\tmodel(example_input)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "# speedup would be observed in subsequent runs.\n\tmodel(example_input)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Learn more [here](https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#use-onednn-graph-with-torchscript-for-inference).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Prototype Features", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Distributed API", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] DTensor", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "PyTorch [DistributedTensor](https://github.com/pytorch/pytorch/blob/master/torch/distributed/_tensor/README.md) (DTensor) is a prototyping effort with distributed tensor primitives to allow easier distributed computation authoring in the SPMD (Single Program Multiple Devices) paradigm. The primitives are simple but powerful when used to express tensor distributions with both sharded and replicated parallelism strategies. PyTorch DTensor empowered PyTorch [Tensor", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Parallelism](https://pytorch.org/docs/master/distributed.tensor.parallel.html) along with other advanced parallelism explorations. In addition, it also offers a uniform way to save/load state_dict for distributed checkpointing purposes, even when there\u2019re complex tensor distribution strategies such as combining tensor parallelism with parameter sharding in FSDP. More details can be found in this [RFC](https://github.com/pytorch/pytorch/issues/88838) and the [DTensor examples", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "notebook](https://colab.research.google.com/drive/12Pl5fvh0eLPUrcVO7s6yY4n2_RZo8pLR#scrollTo=stYPKb9Beq4e).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] TensorParallel\n\nWe now support DTensor based Tensor Parallel which users can distribute their model parameters across different GPU devices. We also support Pairwise Parallel which shards two concatenated linear layers in a col-wise and row-wise style separately so that only one collective(all-reduce/reduce-scatter) is needed in the end. More details can be found in this [example](https://github.com/pytorch/examples/blob/main/distributed/tensor_parallelism/example.py).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] 2D Parallel\n\nWe implemented the integration of the aforementioned TP with FullyShardedDataParallel(FSDP) as 2D parallel to further scale large model training. More details can be found in this [slide](https://docs.google.com/presentation/d/17g6WqrO00rP3MsxbRENsPpjrlSkwiA_QB4r93_eB5is/edit?usp=sharing) and [code example](https://github.com/pytorch/pytorch/blob/master/test/distributed/tensor/parallel/test_2d_parallel.py).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Prototype] torch.compile(dynamic=True)\n\nExperimental support for PT2 compilation with dynamic shapes is available in this release. Inference compilation with inductor for simple models is supported, but there are a lot of limitations:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Training available in a future release (This is partially fixed in nightlies!)\n* Minifier available in a future release.\n* It is easy to end up in a situation where the dimension you wanted to be dynamic gets specialized anyway. Some of these issues are fixed in nightlies, others are not.\n* We do not appropriately propagate Inductor guards to the top-level, this is tracked at [#96296](https://github.com/pytorch/pytorch/issues/96296).\n* Data-dependent operations like nonzero still require a graph break.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "* Dynamic does not work with non-standard modes like reduce-overhead or max-autotune.\n* There are many bugs in Inductor compilation. To track known bugs, check the [dynamic shapes](https://github.com/pytorch/pytorch/issues?q=is%3Aopen+is%3Aissue+label%3A%22module%3A+dynamic+shapes%22) label on the PyTorch issue tracker.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "For the latest and greatest news about dynamic shapes support on master, check out [our status reports](https://dev-discuss.pytorch.org/t/state-of-symbolic-shapes-branch/777/43).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Highlights/Performance Improvements", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "[Deprecation of Cuda 11.6 and Python 3.7 support](https://pytorch.org/blog/deprecation-cuda-python-support/) for PyTorch 2.0\n\nIf you are still using or depending on CUDA 11.6 or Python 3.7 builds, we strongly recommend moving to at least CUDA 11.7 and Python 3.8, as it would be the minimum versions required for PyTorch 2.0. For more detail, please refer to the [Release Compatibility Matrix for PyTorch](https://github.com/pytorch/pytorch/blob/master/RELEASE.md#release-compatibility-matrix) releases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Python 3.11 support on Anaconda Platform", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Due to lack of Python 3.11 support for packages that PyTorch depends on, including NumPy, SciPy, SymPy, Pillow and others on the Anaconda platform. We will not be releasing Conda binaries compiled with Python 3.11 for PyTorch Release 2.0. The Pip packages with Python 3.11 support will be released, hence if you intend to use PyTorch 2.0 with Python 3.11 please use our Pip packages. Please note: Conda packages with Python 3.11 support will be made available on our nightly channel. Also we are planning on", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "releasing Conda Python 3.11 binaries as part of future release once Anaconda provides these key dependencies. More information and instructions on how to download the Pip packages can be found [here](https://dev-discuss.pytorch.org/t/pytorch-2-0-message-concerning-python-3-11-support-on-anaconda-platform/1087).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "Optimized PyTorch Inference with AWS Graviton processors", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "The optimizations focused on three key areas: GEMM kernels, bfloat16 support, primitive caching and the memory allocator. For aarch64 platforms, PyTorch supports Arm Compute Library (ACL) GEMM kernels via Mkldnn(OneDNN) backend. The ACL library provides Neon/SVE GEMM kernels for fp32 and bfloat16 formats. The bfloat16 support on c7g allows efficient deployment of bfloat16 trained, AMP (Automatic Mixed Precision) trained, or even the standard fp32 trained models. The standard fp32 models leverage bfloat16", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "kernels via OneDNN fast math mode, without any model quantization. Next we implemented primitive caching for conv, matmul and inner product operators. More information on the updated PyTorch user guide with the upcoming 2.0 release improvements and TorchBench benchmark details can be found [here](https://github.com/aws/aws-graviton-getting-started).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} -{"page_content": "---\nlayout: blog_detail\ntitle: \"Case Study: PathAI Uses PyTorch to Improve Patient Outcomes with AI-powered Pathology\"\nauthor: Logan Kilpatrick - Sr. Technology Advocate, Harshith Padigela - ML Engineer, Syed Ashar Javed - ML Technical Lead, Robert Egger - Biomedical Data Scientist\nfeatured-img: \"/assets/images/2022-7-15-PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology-1.png\"\n---", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "\u200b[\u200bPathAI](https://pathai.com) is the leading provider of AI-powered technology tools and services for pathology (the study of disease). Our platform was built to enable substantial improvements to the accuracy of diagnosis and the measurement of therapeutic efficacy for complex diseases, leveraging modern approaches in machine learning like image segmentation, graph neural networks, and multiple instance learning.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Traditional manual pathology is prone to [subjectivity and observer variability](https://www.journal-of-hepatology.eu/article/S0168-8278(20)30399-8/fulltext) that can negatively affect diagnoses and drug development trials. Before we dive into how we use PyTorch to improve our diagnosis workflow, let us first lay out the traditional analog Pathology workflow without machine learning.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "How Traditional Biopharma Works", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "There are many avenues that biopharma companies take to discover novel therapeutics or diagnostics. One of those avenues relies heavily on the analysis of pathology slides to answer a variety of questions: how does a particular cellular communication pathway work? Can a specific disease state be linked to the presence or lack of a particular protein? Why did a particular drug in a clinical trial work for some patients but not others? Might there be an association between patient outcomes and a novel", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "biomarker?", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "To help answer these questions, biopharma companies rely on expert pathologists to analyze slides and help evaluate the questions they might have.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "As you might imagine, it takes an expert board certified pathologist to make accurate interpretations and diagnosis. In [one study](https://www.bmj.com/content/357/bmj.j2813.full), a single biopsy result was given to 36 different pathologists and the outcome was 18 different diagnoses varying in severity from no treatment to aggressive treatment necessary. Pathologists also often solicit feedback from colleagues in difficult edge cases. Given the complexity of the problem, even with expert training and", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "collaboration, pathologists can still have a hard time making a correct diagnosis. This potential variance can be the difference between a drug being approved and it failing the clinical trial.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "How PathAI utilizes machine learning to power drug development", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "PathAI develops machine learning models which provide insights for drug development R&D, for powering clinical trials, and for making diagnoses. To this end, PathAI leverages PyTorch for slide level inference using a variety of methods including graph neural networks (GNN) as well as multiple instance learning. In this context, \u201cslides\u201d refers to full size scanned images of glass slides, which are pieces of glass with a thin slice of tissue between them, stained to show various cell formations. PyTorch", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "enables our teams using these different methodologies to share a common framework which is robust enough to work in all the conditions we need. PyTorch\u2019s high level, imperative, and pythonic syntax allows us to prototype models quickly and then take those models to scale once we have the results we want.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Multi-instance learning on gigabyte images", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "One of the uniquely challenging aspects of applying ML to pathology is the immense size of the images. These digital slides can often be 100,000 x 100,000 pixels or more in resolution and gigabytes in size. Loading the full image in GPU memory and applying traditional computer vision algorithms on them is an almost impossible task. It also takes both a considerable amount of time and resources to have a full slide image (100k x 100k) annotated, especially when annotators need to be domain experts", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "(board-certified pathologists). We often build models to predict image-level labels, like the presence of cancer, on a patient slide which covers a few thousand pixels in the whole image. The cancerous area is sometimes a tiny fraction of the entire slide, which makes the ML problem similar to finding a needle in a haystack. On the other hand, some problems like the prediction of certain histological biomarkers require an aggregation of information from the whole slide which is again hard due to the size of", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "the images. All these factors add significant algorithmic, computational, and logistical complexity when applying ML techniques to pathology problems.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Breaking down the image into smaller patches, learning patch representations, and then pooling those representations to predict an image-level label is one way to solve this problem as is depicted in the image below. One popular method for doing this is called [Multiple Instance Learning (MIL)](https://paperswithcode.com/task/multiple-instance-learning). Each patch is considered an \u2018instance\u2019 and a set of patches forms a \u2018bag\u2019. The individual patch representations are pooled together to predict a final", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "bag-level label. Algorithmically, the individual patch instances in the bag do not require labels and hence allow us to learn bag-level labels in a weakly-supervised way. They also use permutation invariant pooling functions which make the prediction independent of the order of patches and allows for an efficient aggregation of information. Typically, attention based pooling functions are used which not only allow for efficient aggregation but also provide attention values for each patch in the bag. These", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "values indicate the importance of the corresponding patch in the prediction and can be visualized to better understand the model predictions. This element of interpretability can be very important to drive adoption of these models in the real world and we use variations like [Additive MIL models](https://arxiv.org/pdf/2206.01794.pdf) to enable such spatial explainability. Computationally, MIL models circumvent the problem of applying neural networks to large image sizes since patch representations are", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "obtained independently of the size of the image.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "

\n \n

\n\nAt PathAI, we use custom MIL models based on deep nets to predict image-level labels. The overview of this process is as follows:", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "1. Select patches from a slide using different sampling approaches.\n2. Construct a bag of patches based on random sampling or heuristic rules.\n3. Generate patch representations for each instance based on pre-trained models or large-scale representation learning models.\n4. Apply permutation invariant pooling functions to get the final slide-level score.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Now that we have walked through some of the high-level details around MIL in PyTorch, let\u2019s look at some code to see how simple it is to go from ideation to code in production with PyTorch. We begin by defining a sampler, transformations, and our MIL dataset:\n\n```python\n# Create a bag sampler which randomly samples patches from a slide\nbag_sampler = RandomBagSampler(bag_size=12)\n\n# Setup the transformations\ncrop_transform = FlipRotateCenterCrop(use_flips=True)", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "# Create the dataset which loads patches for each bag\ntrain_dataset = MILDataset(\n bag_sampler=bag_sampler,\n samples_loader=sample_loader,\n transform=crop_transform,\n)", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "After we have defined our sampler and dataset, we need to define the model we will actually train with said dataset. PyTorch\u2019s familiar model definition syntax makes this easy to do while also allowing us to create bespoke models at the same time.\n\n```python\nclassifier = DefaultPooledClassifier(hidden_dims=[256, 256], input_dims=1024, output_dims=1)\n\npooling = DefaultAttentionModule(\n input_dims=1024,\n hidden_dims=[256, 256],\n output_activation=StableSoftmax()\n)", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "# Define the model which is a composition of the featurizer, pooling module and a classifier\nmodel = DefaultMILGraph(featurizer=ShuffleNetV2(), classifier=classifier, pooling = pooling)", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Since these models are trained end-to-end, they offer a powerful way to go directly from a gigapixel whole slide image to a single label. Due to their wide applicability to different biological problems, two aspects of their implementation and deployment are important:", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "1. Configurable control over each part of the pipeline including the data loaders, the modular parts of the model, and their interaction with each other.\n2. Ability to rapidly iterate through the ideate-implement-experiment-productionize loop.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "PyTorch has various advantages when it comes to MIL modeling. It offers an intuitive way to create dynamic computational graphs with flexible control flow which is great for rapid research experimentation. The map-style datasets, configurable sampler and batch-samplers allow us to customize how we construct bags of patches, enabling faster experimentation. Since MIL models are IO heavy, data parallelism and pythonic data loaders make the task very efficient and user friendly. Lastly, the object-oriented", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "nature of PyTorch enables building of reusable modules which aid in the rapid experimentation, maintainable implementation and ease of building compositional components of the pipeline.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Exploring spatial tissue organization with GNNs in PyTorch\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "In both healthy and diseased tissue, the spatial arrangement and structure of cells can oftentimes be as important as the cells themselves. For example, when assessing lung cancers, pathologists try to look at the overall grouping and structure of tumor cells (do they form solid sheets? Or do they occur in smaller, localized clusters?) to determine if the cancer belongs to specific subtypes which can have vastly [different prognosis](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3369269/). Such spatial", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "relationships between cells and other tissue structures can be modeled using graphs to capture tissue topology and cellular composition at the same time. [Graph Neural Networks](https://openaccess.thecvf.com/content_CVPRW_2020/papers/w16/Lu_Capturing_Cellular_Topology_in_Multi-Gigapixel_Pathology_Images_CVPRW_2020_paper.pdf) (GNNs) allow learning spatial patterns within these graphs that relate to other clinical variables, for example overexpression of genes in certain cancers.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "Summary: \n* torch.compile is the main API for PyTorch 2.0, which wraps your model and returns a compiled model. It is a fully additive (and optional) feature and hence 2.0 is 100% backward compatible by definition.\n* As an underpinning technology of torch.compile, TorchInductor with Nvidia and AMD GPUs will rely on OpenAI Triton deep learning compiler to generate performant code and hide low level hardware details. OpenAI Triton-generated kernels achieve performance that's on par with hand-written kernels and specialized cuda libraries such as cublas.\n* Accelerated Transformers introduce high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA). The API is integrated with torch.compile() and model developers may also use the [scaled dot product attention](#beta-scaled-dot-product-attention-20) kernels directly by calling the new scaled_dot_product_attention() operator. \n* Metal Performance Shaders (MPS) backend provides GPU accelerated PyTorch training on Mac platforms with added support for Top 60 most used ops, bringing coverage to over 300 operators. \n* Amazon AWS optimizes the PyTorch CPU inference on AWS Graviton3 based [C7g instances](https://aws.amazon.com/blogs/aws/new-amazon-ec2-c7g-instances-powered-by-aws-graviton3-processors/). PyTorch 2.0 improves inference performance on Graviton compared to the previous releases, including improvements for Resnet50 and Bert. \n* New prototype features and technologies across TensorParallel, DTensor, 2D parallel, TorchDynamo, AOTAutograd, PrimTorch and TorchInductor.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\nStable\n Beta\n Prototype\n Performance Improvements\n
\n\nAccelerated PT 2 Transformers\n \n\ntorch.compile\n \n\nDTensor\n \n\nCUDA support for 11.7 & 11.8 (deprecating CUDA 11.6) \n
\n \n\nPyTorch MPS Backend\n \n\nTensorParallel\n \n\nPython 3.8 (deprecating Python 3.7)\n
\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Scaled dot product attention\n \n\n2D Parallel\n \n\nAWS Graviton3\n
\n \n\nfunctorch\n \n\nTorch.compile (dynamic=True)\n \n
\n Dispatchable Collectives\n \n
\n Torch.set_default & torch.device\n \n \n
\n ", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "X86 quantization backend\n \n \n
\n \n\nGNN inference and training performance\n \n \n
\n\n\n*To see a full list of public 2.0, 1.13 and 1.12 feature submissions click [here](https://docs.google.com/spreadsheets/d/1H3jazwO8BBCwK8JwLNYspLiHfUrzshEtyqjL-X93I9g/edit#gid=790902532).\n\n\n## Stable Features\n\n\n### [Stable] Accelerated PyTorch 2 Transformers", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "The PyTorch 2.0 release includes a new high-performance implementation of the PyTorch Transformer API. In releasing Accelerated PT2 Transformers, our goal is to make training and deployment of state-of-the-art Transformer models affordable across the industry. This release introduces high-performance support for training and inference using a custom kernel architecture for scaled dot product attention (SPDA), extending the inference \u201cfastpath\u201d architecture, previously known as \"Better Transformer.\"\n\nSimilar to the \u201cfastpath\u201d architecture, custom kernels are fully integrated into the PyTorch Transformer API \u2013 thus, using the native Transformer and MultiHeadAttention API will enable users to:\n\n* transparently see significant speed improvements; \n* support many more use cases including models using Cross-Attention, Transformer Decoders, and for training models; and\n* continue to use fastpath inference for fixed and variable sequence length Transformer Encoder and Self Attention use cases.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "To take full advantage of different hardware models and Transformer use cases, multiple SDPA custom kernels are supported (see below), with custom kernel selection logic that will pick the highest-performance kernel for a given model and hardware type. In addition to the existing Transformer API, model developers may also use the [scaled dot product attention](#beta-scaled-dot-product-attention-20) kernels directly by calling the new scaled_dot_product_attention() operator. Accelerated PyTorch 2 Transformers are integrated with torch.compile() . To use your model while benefiting from the additional acceleration of PT2-compilation (for inference or training), pre-process the model with `model = torch.compile(model)`.\n\nWe have achieved major speedups for training transformer models and in particular large language models with Accelerated PyTorch 2 Transformers using a combination of custom kernels and torch.compile().", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "![alt_text](/assets/images/pytorch20post.png \"Accelerated PyTorch 2 speed\"){:width=\"100%\"}\nFigure: Using scaled dot product attention with custom kernels and torch.compile delivers significant speedups for training large language models, such as for [nanoGPT](https://github.com/karpathy/nanoGPT) shown here.\n\n\n\n## Beta Features\n\n\n### [Beta] torch.compile\n\ntorch.compile is the main API for PyTorch 2.0, which wraps your model and returns a compiled model. It is a fully additive (and optional) feature and hence 2.0 is 100% backward compatible by definition.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Underpinning torch.compile are new technologies \u2013 TorchDynamo, AOTAutograd, PrimTorch and TorchInductor:\n* TorchDynamo captures PyTorch programs safely using Python Frame Evaluation Hooks and is a significant innovation that was a result of 5 years of our R&D into safe graph capture. \n* AOTAutograd overloads PyTorch\u2019s autograd engine as a tracing autodiff for generating ahead-of-time backward traces. \n* PrimTorch canonicalizes ~2000+ PyTorch operators down to a closed set of ~250 primitive operators that developers can target to build a complete PyTorch backend. This substantially lowers the barrier of writing a PyTorch feature or backend. \n* TorchInductor is a deep learning compiler that generates fast code for multiple accelerators and backends. For NVIDIA and AMD GPUs, it uses OpenAI Triton as a key building block. For intel CPUs, we generate C++ code using multithreading, vectorized instructions and offloading appropriate operations to mkldnn when possible.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "With all the new technologies, torch.compile is able to work 93% of time across 165 open-source models and runs 20% faster on average at float32 precision and 36% faster on average at AMP precision. \n\nFor more information, please refer to [https://pytorch.org/get-started/pytorch-2.0/](https://pytorch.org/get-started/pytorch-2.0/) and for TorchInductor CPU with Intel [here](https://dev-discuss.pytorch.org/t/torchinductor-update-5-cpu-backend-backend-performance-update-and-deep-dive-on-key-optimizations/1117).\n\n\n### [Beta] PyTorch MPS Backend\n\nMPS backend provides GPU-accelerated PyTorch training on Mac platforms. This release brings improved correctness, stability, and operator coverage.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "MPS backend now includes support for the Top 60 most used ops, along with the most frequently requested operations by the community, bringing coverage to over 300 operators. The major focus of the release was to enable full OpInfo-based forward and gradient mode testing to address silent correctness issues. These changes have resulted in wider adoption of MPS backend by 3rd party networks such as Stable Diffusion, YoloV5, WhisperAI, along with increased coverage for Torchbench networks and Basic tutorials. We encourage developers to update to the latest macOS release to see the best performance and stability on the MPS backend. \n\n \n\nLinks", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Links\n\n\n\n1. [MPS Backend](https://pytorch.org/docs/stable/notes/mps.html)\n2. [Developer information](https://github.com/pytorch/pytorch/wiki/MPS-Backend)\n3. [Accelerated PyTorch training on Mac](https://developer.apple.com/metal/pytorch/) \n4. [Metal](https://developer.apple.com/documentation/metal?language=objc), [Metal Performance Shaders](https://developer.apple.com/documentation/metalperformanceshaders?language=objc) & [Metal Performance Shaders Graph](https://developer.apple.com/documentation/metalperformanceshadersgraph?language=objc)\n\n\n### [Beta] Scaled dot product attention 2.0 \n\nWe are thrilled to announce the release of PyTorch 2.0, which introduces a powerful scaled dot product attention function as part of torch.nn.functional. This function includes multiple implementations that can be seamlessly applied depending on the input and hardware in use.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "In previous versions of PyTorch, you had to rely on third-party implementations and install separate packages to take advantage of memory-optimized algorithms like [FlashAttention](https://github.com/HazyResearch/flash-attention). With PyTorch 2.0, all these implementations are readily available by default.\n\nThese implementations include [FlashAttention](https://arxiv.org/abs/2205.14135) from HazyResearch, Memory-Efficient Attention from the [xFormers](https://github.com/facebookresearch/xformers) project, and a native C++ implementation that is ideal for non-CUDA devices or when high-precision is required.\n\nPyTorch 2.0 will automatically select the optimal implementation for your use case, but you can also toggle them individually for finer-grained control. Additionally, the scaled dot product attention function can be used to build common transformer architecture components.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Learn more with the [documentation](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html?highlight=scaled_dot_product#torch.nn.functional.scaled_dot_product_attention) and this [tutorial](https://pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html).\n\n\n### [Beta] functorch -> torch.func \n\nInspired by [Google JAX](https://github.com/google/jax), functorch is a library that offers composable vmap (vectorization) and autodiff transforms. It enables advanced autodiff use cases that would otherwise be tricky to express in PyTorch. Examples include:\n* [model ensembling](https://pytorch.org/tutorials/intermediate/ensembling.html)\n* [efficiently computing jacobians and hessians](https://pytorch.org/tutorials/intermediate/jacobians_hessians.html)\n* [computing per-sample-gradients (or other per-sample quantities)](https://pytorch.org/tutorials/intermediate/per_sample_grads.html)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "We\u2019re excited to announce that, as the final step of upstreaming and integrating functorch into PyTorch, the functorch APIs are now available in the torch.func module. Our function transform APIs are identical to before, but we have changed how the interaction with NN modules work. Please see the [docs](https://pytorch.org/docs/master/func.html) and the [migration guide](https://pytorch.org/docs/master/func.migrating.html) for more details.\n\nFurthermore, we have [added support for torch.autograd.Function](https://pytorch.org/docs/master/notes/extending.func.html): one is now able to apply function transformations (e.g. vmap, grad, jvp) over torch.autograd.Function.\n\n\n### [Beta] Dispatchable Collectives", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Dispatchable collectives is an improvement to the existing init_process_group() API which changes backend to an optional argument. For users, the main advantage of this feature is that it will allow them to write code that can run on both GPU and CPU machines without having to change the backend specification. The dispatchability feature will also make it easier for users to support both GPU and CPU collectives, as they will no longer need to specify the backend manually (e.g. \u201cNCCL\u201d or \u201cGLOO\u201d). Existing backend specifications by users will be honored and will not require change.\n\nUsage example:\n```\nimport torch.distributed.dist\n\u2026\n# old\ndist.init_process_group(backend=\u201dnccl\u201d, ...)\ndist.all_reduce(...) # with CUDA tensors works\ndist.all_reduce(...) # with CPU tensors does not work\n\n# new\ndist.init_process_group(...) # backend is optional\ndist.all_reduce(...) # with CUDA tensors works\ndist.all_reduce(...) # with CPU tensors works\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Learn more [here](https://pytorch.org/docs/master/distributed.html#torch.distributed.init_process_group).\n\n\n### [Beta] torch.set_default_device and torch.device as context manager\n\ntorch.set_default_device allows users to change the default device that factory functions in PyTorch allocate on. For example, if you torch.set_default_device(\u2018cuda\u2019), a call to torch.empty(2) will allocate on CUDA (rather than on CPU). You can also use torch.device as a context manager to change the default device on a local basis. This resolves a long standing feature request from PyTorch\u2019s initial release for a way to do this.\n\nLearn more [here](https://pytorch.org/tutorials/recipes/recipes/changing_default_device.html). \n\n\n### [Beta] \"X86\" as the new default quantization backend for x86 CPU", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "The new X86 quantization backend, which utilizes FBGEMM and oneDNN kernel libraries, replaces FBGEMM as the default quantization backend for x86 CPU platforms and offers improved int8 inference performance compared to the original FBGEMM backend, leveraging the strengths of both libraries, with 1.3X \u2013 2X inference performance speedup measured on 40+ deep learning models. The new backend is functionally compatible with the original FBGEMM backend.\n\n\n**Table: Geomean Speedup of X86 Quantization Backend vs. FBGEMM Backend**\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n 1 core/instance\n 2 cores/instance\n 4 cores/instance\n 1 socket (32 cores)/instance\n
Intel(R) Xeon(R) Platinum 8358 CPU @ 2.60GHz\n 1.76X\n 1.80X\n 2.04X\n 1.34X\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "By default, users on x86 platforms will utilize the x86 quantization backend and their PyTorch programs will remain unchanged when using the default backend. Alternatively, users have the option to specify \"X86\" as the quantization backend explicitly. Example code is shown below:\n\n```\nimport torch\nfrom torch.ao.quantization import get_default_qconfig_mappingfrom torch.quantization.quantize_fx\nimport prepare_fx, convert_fx\n \n# get default configuration\nqconfig_mapping = get_default_qconfig_mapping()\n \n# or explicitly specify the backend\n# qengine = 'x86'\n# torch.backends.quantized.engine = qengine\n# qconfig_mapping = get_default_qconfig_mapping(qengine)\n \n# construct fp32 model\nmodel_fp32 = ...\n \n# prepare\nprepared_model = prepare_fx(model_fp32, qconfig_mapping, example_inputs=x)\n \n# calibrate\n...\n \n# convert\nquantized_model = convert_fx(prepared_model)\n```", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Find more information: [https://github.com/pytorch/pytorch/issues/83888](https://github.com/pytorch/pytorch/issues/83888) and [https://www.intel.com/content/www/us/en/developer/articles/technical/accelerate-pytorch-int8-inf-with-new-x86-backend.html](https://www.intel.com/content/www/us/en/developer/articles/technical/accelerate-pytorch-int8-inf-with-new-x86-backend.html).\n\n\n### [Beta] GNN inference and training optimization on CPU", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "PyTorch 2.0 includes several critical optimizations to improve GNN inference and training performance on CPU. Before 2.0, GNN models of PyG suffers from low efficiency on CPU due to lack of performance tuning for several critical kernels (scatter/gather, etc) and the lack of GNN-related sparse matrix multiplication ops. To be specific, optimizations include:\n* scatter_reduce: performance hotspot in Message Passing when the edge index is stored in Coordinate format (COO).\n* gather: backward of scatter_reduce, specially tuned for the GNN compute when the index is an expanded tensor.\n* torch.sparse.mm with reduce flag: performance hotspot in Message Passing when the edge index is stored in Compressed Sparse Row (CSR). Supported reduce flag of: sum, mean, amax, amin.\n\nOn PyG benchmarks/examples, OGB benchmarks, a 1.12x - 4.07x performance speedup is measured (1.13.1 compared with 2.0) for single node inference and training.", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Model-Dataset\n Option\n Speedup Ratio\n
GCN-Reddit (inference)\n 512-2-64-dense\n 1.22x\n
1024-3-128-dense\n 1.25x\n
512-2-64-sparse\n 1.31x\n
1024-3-128-sparse\n 1.68x\n
512-2-64-dense\n 1.22x\n
\nGraphSage-ogbn-products (inference)\n 1024-3-128-dense\n 1.15x\n
512-2-64-sparse\n 1.20x\n
1024-3-128-sparse\n 1.33x\n
full-batch-sparse\n 4.07x\n
GCN-PROTEINS (training)\n 3-32\n 1.67x\n
GCN-REDDIT-BINARY (training)\n 3-32\n 1.67x\n
GCN-Reddit (training)\n 512-2-64-dense\n 1.20x\n
1024-3-128-dense\n 1.12x\n
", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Learn more: [PyG CPU Performance Optimization](https://www.pyg.org/ns-newsarticle-accelerating-pyg-on-intel-cpus).\n\n\n### [Beta] Accelerating inference on CPU with PyTorch by leveraging oneDNN Graph", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "[oneDNN Graph API](https://spec.oneapi.io/onednn-graph/latest/introduction.html) extends [oneDNN](https://spec.oneapi.io/versions/latest/elements/oneDNN/source/index.html) with a flexible graph API to maximize the optimization opportunity for generating efficient code on AI hardware. \n* It automatically identifies the graph partitions to be accelerated via fusion. \n* The [fusion patterns](https://github.com/oneapi-src/oneDNN/blob/dev-graph/doc/programming_model/ops_and_patterns.md#fusion-patterns) focus on fusing compute-intensive operations such as convolution, matmul and their neighbor operations for both inference and training use cases. \n* Although work is ongoing to integrate oneDNN Graph with TorchDynamo as well, its integration with the PyTorch JIT Fuser attained beta status in PyTorch 2.0 for [Float32](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-float) & [BFloat16](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16) inference (on machines that support AVX512_BF16 ISA).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "From a developer\u2019s/researcher\u2019s perspective, the usage is quite simple & intuitive, with the only change in code being an API invocation:\n* Leverage oneDNN Graph, with [JIT-tracing](https://pytorch.org/docs/stable/generated/torch.jit.trace.html), a model is profiled with an example input. \n* The context manager _with torch.jit.fuser(\u201cfuser3\u201d):_ can also be used instead of invoking _torch.jit.enable_onednn_fusion(True)_.\n* For accelerating [BFloat16 inference](https://github.com/pytorch/pytorch/tree/master/torch/csrc/jit/codegen/onednn#example-with-bfloat16), we rely on eager-mode AMP (Automatic Mixed Precision) support in PyTorch & disable JIT mode\u2019s AMP, as both of them are currently divergent:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "```\n# Assuming we have a model of the name 'model'\n \nexample_input = torch.rand(1, 3, 224, 224)\n \n# enable oneDNN Graph\ntorch.jit.enable_onednn_fusion(True)\n# Disable AMP for JIT\ntorch._C._jit_set_autocast_mode(False)\nwith torch.no_grad(), torch.cpu.amp.autocast():\n\tmodel = torch.jit.trace(model, (example_input))\n\tmodel = torch.jit.freeze(model)\n \t# 2 warm-ups (2 for tracing/scripting with an example, 3 without an example)\n\tmodel(example_input)\n\tmodel(example_input)\n \n\t# speedup would be observed in subsequent runs.\n\tmodel(example_input)\n```\n\n\nLearn more [here](https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#use-onednn-graph-with-torchscript-for-inference).\n\n\n## Prototype Features\n\n### Distributed API\n\n#### [Prototype] DTensor", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "PyTorch [DistributedTensor](https://github.com/pytorch/pytorch/blob/master/torch/distributed/_tensor/README.md) (DTensor) is a prototyping effort with distributed tensor primitives to allow easier distributed computation authoring in the SPMD (Single Program Multiple Devices) paradigm. The primitives are simple but powerful when used to express tensor distributions with both sharded and replicated parallelism strategies. PyTorch DTensor empowered PyTorch [Tensor Parallelism](https://pytorch.org/docs/master/distributed.tensor.parallel.html) along with other advanced parallelism explorations. In addition, it also offers a uniform way to save/load state_dict for distributed checkpointing purposes, even when there\u2019re complex tensor distribution strategies such as combining tensor parallelism with parameter sharding in FSDP. More details can be found in this [RFC](https://github.com/pytorch/pytorch/issues/88838) and the [DTensor examples notebook](https://colab.research.google.com/drive/12Pl5fvh0eLPUrcVO7s6yY4n2_RZo8pLR#scrollTo=stYPKb9Beq4e).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "#### [Prototype] TensorParallel\n\nWe now support DTensor based Tensor Parallel which users can distribute their model parameters across different GPU devices. We also support Pairwise Parallel which shards two concatenated linear layers in a col-wise and row-wise style separately so that only one collective(all-reduce/reduce-scatter) is needed in the end. More details can be found in this [example](https://github.com/pytorch/examples/blob/main/distributed/tensor_parallelism/example.py).\n\n\n#### [Prototype] 2D Parallel\n\nWe implemented the integration of the aforementioned TP with FullyShardedDataParallel(FSDP) as 2D parallel to further scale large model training. More details can be found in this [slide](https://docs.google.com/presentation/d/17g6WqrO00rP3MsxbRENsPpjrlSkwiA_QB4r93_eB5is/edit?usp=sharing) and [code example](https://github.com/pytorch/pytorch/blob/master/test/distributed/tensor/parallel/test_2d_parallel.py).\n\n\n#### [Prototype] torch.compile(dynamic=True)", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Experimental support for PT2 compilation with dynamic shapes is available in this release. Inference compilation with inductor for simple models is supported, but there are a lot of limitations:", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "* Training available in a future release (This is partially fixed in nightlies!)\n* Minifier available in a future release.\n* It is easy to end up in a situation where the dimension you wanted to be dynamic gets specialized anyway. Some of these issues are fixed in nightlies, others are not.\n* We do not appropriately propagate Inductor guards to the top-level, this is tracked at [#96296](https://github.com/pytorch/pytorch/issues/96296).\n* Data-dependent operations like nonzero still require a graph break.\n* Dynamic does not work with non-standard modes like reduce-overhead or max-autotune.\n* There are many bugs in Inductor compilation. To track known bugs, check the [dynamic shapes](https://github.com/pytorch/pytorch/issues?q=is%3Aopen+is%3Aissue+label%3A%22module%3A+dynamic+shapes%22) label on the PyTorch issue tracker.\n\nFor the latest and greatest news about dynamic shapes support on master, check out [our status reports](https://dev-discuss.pytorch.org/t/state-of-symbolic-shapes-branch/777/43).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "## Highlights/Performance Improvements\n\n\n### [Deprecation of Cuda 11.6 and Python 3.7 support](https://pytorch.org/blog/deprecation-cuda-python-support/) for PyTorch 2.0\n\nIf you are still using or depending on CUDA 11.6 or Python 3.7 builds, we strongly recommend moving to at least CUDA 11.7 and Python 3.8, as it would be the minimum versions required for PyTorch 2.0. For more detail, please refer to the [Release Compatibility Matrix for PyTorch](https://github.com/pytorch/pytorch/blob/master/RELEASE.md#release-compatibility-matrix) releases.\n\n\n### Python 3.11 support on Anaconda Platform", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "Due to lack of Python 3.11 support for packages that PyTorch depends on, including NumPy, SciPy, SymPy, Pillow and others on the Anaconda platform. We will not be releasing Conda binaries compiled with Python 3.11 for PyTorch Release 2.0. The Pip packages with Python 3.11 support will be released, hence if you intend to use PyTorch 2.0 with Python 3.11 please use our Pip packages. Please note: Conda packages with Python 3.11 support will be made available on our nightly channel. Also we are planning on releasing Conda Python 3.11 binaries as part of future release once Anaconda provides these key dependencies. More information and instructions on how to download the Pip packages can be found [here](https://dev-discuss.pytorch.org/t/pytorch-2-0-message-concerning-python-3-11-support-on-anaconda-platform/1087).\n\n\n### Optimized PyTorch Inference with AWS Graviton processors", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "The optimizations focused on three key areas: GEMM kernels, bfloat16 support, primitive caching and the memory allocator. For aarch64 platforms, PyTorch supports Arm Compute Library (ACL) GEMM kernels via Mkldnn(OneDNN) backend. The ACL library provides Neon/SVE GEMM kernels for fp32 and bfloat16 formats. The bfloat16 support on c7g allows efficient deployment of bfloat16 trained, AMP (Automatic Mixed Precision) trained, or even the standard fp32 trained models. The standard fp32 models leverage bfloat16 kernels via OneDNN fast math mode, without any model quantization. Next we implemented primitive caching for conv, matmul and inner product operators. More information on the updated PyTorch user guide with the upcoming 2.0 release improvements and TorchBench benchmark details can be found [here](https://github.com/aws/aws-graviton-getting-started).", "metadata": {"source": "https://pytorch.org/blog/pytorch-2.0-release/", "category": "pytorch blogs"}} +{"page_content": "---\nlayout: blog_detail\ntitle: \"Case Study: PathAI Uses PyTorch to Improve Patient Outcomes with AI-powered Pathology\"\nauthor: Logan Kilpatrick - Sr. Technology Advocate, Harshith Padigela - ML Engineer, Syed Ashar Javed - ML Technical Lead, Robert Egger - Biomedical Data Scientist\nfeatured-img: \"/assets/images/2022-7-15-PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology-1.png\"\n---\n\n\u200b[\u200bPathAI](https://pathai.com) is the leading provider of AI-powered technology tools and services for pathology (the study of disease). Our platform was built to enable substantial improvements to the accuracy of diagnosis and the measurement of therapeutic efficacy for complex diseases, leveraging modern approaches in machine learning like image segmentation, graph neural networks, and multiple instance learning.\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "Traditional manual pathology is prone to [subjectivity and observer variability](https://www.journal-of-hepatology.eu/article/S0168-8278(20)30399-8/fulltext) that can negatively affect diagnoses and drug development trials. Before we dive into how we use PyTorch to improve our diagnosis workflow, let us first lay out the traditional analog Pathology workflow without machine learning.\n\n## How Traditional Biopharma Works\n\nThere are many avenues that biopharma companies take to discover novel therapeutics or diagnostics. One of those avenues relies heavily on the analysis of pathology slides to answer a variety of questions: how does a particular cellular communication pathway work? Can a specific disease state be linked to the presence or lack of a particular protein? Why did a particular drug in a clinical trial work for some patients but not others? Might there be an association between patient outcomes and a novel biomarker?", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "To help answer these questions, biopharma companies rely on expert pathologists to analyze slides and help evaluate the questions they might have.\u00a0\n\nAs you might imagine, it takes an expert board certified pathologist to make accurate interpretations and diagnosis. In [one study](https://www.bmj.com/content/357/bmj.j2813.full), a single biopsy result was given to 36 different pathologists and the outcome was 18 different diagnoses varying in severity from no treatment to aggressive treatment necessary. Pathologists also often solicit feedback from colleagues in difficult edge cases. Given the complexity of the problem, even with expert training and collaboration, pathologists can still have a hard time making a correct diagnosis. This potential variance can be the difference between a drug being approved and it failing the clinical trial.\n\n## How PathAI utilizes machine learning to power drug development", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "PathAI develops machine learning models which provide insights for drug development R&D, for powering clinical trials, and for making diagnoses. To this end, PathAI leverages PyTorch for slide level inference using a variety of methods including graph neural networks (GNN) as well as multiple instance learning. In this context, \u201cslides\u201d refers to full size scanned images of glass slides, which are pieces of glass with a thin slice of tissue between them, stained to show various cell formations. PyTorch enables our teams using these different methodologies to share a common framework which is robust enough to work in all the conditions we need. PyTorch\u2019s high level, imperative, and pythonic syntax allows us to prototype models quickly and then take those models to scale once we have the results we want.\u00a0\n\n## Multi-instance learning on gigabyte images", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "One of the uniquely challenging aspects of applying ML to pathology is the immense size of the images. These digital slides can often be 100,000 x 100,000 pixels or more in resolution and gigabytes in size. Loading the full image in GPU memory and applying traditional computer vision algorithms on them is an almost impossible task. It also takes both a considerable amount of time and resources to have a full slide image (100k x 100k) annotated, especially when annotators need to be domain experts (board-certified pathologists). We often build models to predict image-level labels, like the presence of cancer, on a patient slide which covers a few thousand pixels in the whole image. The cancerous area is sometimes a tiny fraction of the entire slide, which makes the ML problem similar to finding a needle in a haystack. On the other hand, some problems like the prediction of certain histological biomarkers require an aggregation of information from the whole slide which is again hard due to the size of the images. All these factors add significant algorithmic, computational, and logistical complexity when applying ML techniques to pathology problems.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "Breaking down the image into smaller patches, learning patch representations, and then pooling those representations to predict an image-level label is one way to solve this problem as is depicted in the image below. One popular method for doing this is called [Multiple Instance Learning (MIL)](https://paperswithcode.com/task/multiple-instance-learning). Each patch is considered an \u2018instance\u2019 and a set of patches forms a \u2018bag\u2019. The individual patch representations are pooled together to predict a final bag-level label. Algorithmically, the individual patch instances in the bag do not require labels and hence allow us to learn bag-level labels in a weakly-supervised way. They also use permutation invariant pooling functions which make the prediction independent of the order of patches and allows for an efficient aggregation of information. Typically, attention based pooling functions are used which not only allow for efficient aggregation but also provide attention values for each patch in the bag. These values indicate the importance of the corresponding patch in the prediction and can be visualized to better understand the model predictions. This element of interpretability can be very important to drive adoption of these models in the real world and we use variations like [Additive MIL models](https://arxiv.org/pdf/2206.01794.pdf) to enable such spatial explainability. Computationally, MIL models circumvent the problem of applying neural networks to large image sizes since patch representations are obtained independently of the size of the image.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "

\n \n

\n\nAt PathAI, we use custom MIL models based on deep nets to predict image-level labels. The overview of this process is as follows:\n\n1. Select patches from a slide using different sampling approaches.\n2. Construct a bag of patches based on random sampling or heuristic rules.\n3. Generate patch representations for each instance based on pre-trained models or large-scale representation learning models.\n4. Apply permutation invariant pooling functions to get the final slide-level score.\n\nNow that we have walked through some of the high-level details around MIL in PyTorch, let\u2019s look at some code to see how simple it is to go from ideation to code in production with PyTorch. We begin by defining a sampler, transformations, and our MIL dataset:", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "```python\n# Create a bag sampler which randomly samples patches from a slide\nbag_sampler = RandomBagSampler(bag_size=12)\n\n# Setup the transformations\ncrop_transform = FlipRotateCenterCrop(use_flips=True)\n\n# Create the dataset which loads patches for each bag\ntrain_dataset = MILDataset(\n bag_sampler=bag_sampler,\n samples_loader=sample_loader,\n transform=crop_transform,\n)\n```\n\nAfter we have defined our sampler and dataset, we need to define the model we will actually train with said dataset. PyTorch\u2019s familiar model definition syntax makes this easy to do while also allowing us to create bespoke models at the same time.\n\n```python\nclassifier = DefaultPooledClassifier(hidden_dims=[256, 256], input_dims=1024, output_dims=1)\n\npooling = DefaultAttentionModule(\n input_dims=1024,\n hidden_dims=[256, 256],\n output_activation=StableSoftmax()\n)", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "# Define the model which is a composition of the featurizer, pooling module and a classifier\nmodel = DefaultMILGraph(featurizer=ShuffleNetV2(), classifier=classifier, pooling = pooling)\n```\n\nSince these models are trained end-to-end, they offer a powerful way to go directly from a gigapixel whole slide image to a single label. Due to their wide applicability to different biological problems, two aspects of their implementation and deployment are important:\n\n1. Configurable control over each part of the pipeline including the data loaders, the modular parts of the model, and their interaction with each other.\n2. Ability to rapidly iterate through the ideate-implement-experiment-productionize loop.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "PyTorch has various advantages when it comes to MIL modeling. It offers an intuitive way to create dynamic computational graphs with flexible control flow which is great for rapid research experimentation. The map-style datasets, configurable sampler and batch-samplers allow us to customize how we construct bags of patches, enabling faster experimentation. Since MIL models are IO heavy, data parallelism and pythonic data loaders make the task very efficient and user friendly. Lastly, the object-oriented nature of PyTorch enables building of reusable modules which aid in the rapid experimentation, maintainable implementation and ease of building compositional components of the pipeline.\n\n## Exploring spatial tissue organization with GNNs in PyTorch\n\n

\n \n

", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "In both healthy and diseased tissue, the spatial arrangement and structure of cells can oftentimes be as important as the cells themselves. For example, when assessing lung cancers, pathologists try to look at the overall grouping and structure of tumor cells (do they form solid sheets? Or do they occur in smaller, localized clusters?) to determine if the cancer belongs to specific subtypes which can have vastly [different prognosis](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3369269/). Such spatial relationships between cells and other tissue structures can be modeled using graphs to capture tissue topology and cellular composition at the same time. [Graph Neural Networks](https://openaccess.thecvf.com/content_CVPRW_2020/papers/w16/Lu_Capturing_Cellular_Topology_in_Multi-Gigapixel_Pathology_Images_CVPRW_2020_paper.pdf) (GNNs) allow learning spatial patterns within these graphs that relate to other clinical variables, for example overexpression of genes in certain cancers.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} {"page_content": "In late 2020, when PathAI started using GNNs on tissue samples, PyTorch had the best and most mature support for GNN functionality via the [PyG package](https://pytorch-geometric.readthedocs.io/en/latest/). This made PyTorch the natural choice for our team given that GNN models were something that we knew would be an important ML concept we wanted to explore.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "One of the main value-adds of GNN\u2019s in the context of tissue samples is that the graph itself can uncover spatial relationships that would otherwise be very difficult to find by visual inspection alone. In our recent [AACR publication](https://aacrjournals.org/cancerres/article/82/12_Supplement/1922/701539), we showed that by using GNNs, we can better understand the way the presence of immune cell aggregates (specifically tertiary lymphoid structures, or TLS) in the tumor microenvironment can influence", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "patient prognosis. In this case, the GNN approach was used to predict expression of genes associated with the presence of TLS, and identify histological features beyond the TLS region itself that are relevant to TLS. Such insights into gene expression are difficult to identify from tissue sample images when unassisted by ML models.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "One of the most promising GNN variations we have had success with is [self attention graph pooling](https://arxiv.org/pdf/1904.08082.pdf). Let\u2019s take a look at how we define our Self Attention Graph Pooling (SAGPool) model using PyTorch and PyG:", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "```python\nclass SAGPool(torch.nn.Module):\n def __init__(self, ...):\n super().__init__()\n self.conv1 = GraphConv(in_features, hidden_features, aggr='mean')\n self.convs = torch.nn.ModuleList()\n self.pools = torch.nn.ModuleList()\n self.convs.extend([GraphConv(hidden_features, hidden_features, aggr='mean') for i in range(num_layers - 1)])\n self.pools.extend([SAGPooling(hidden_features, ratio, GNN=GraphConv, min_score=min_score) for i in range((num_layers) // 2)])", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "self.jump = JumpingKnowledge(mode='cat')\n self.lin1 = Linear(num_layers * hidden_features, hidden_features)\n self.lin2 = Linear(hidden_features, out_features)\n self.out_activation = out_activation\n self.dropout = dropout", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "In the above code, we begin by defining a single convolutional graph layer and then add two [module list layers](https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html) which allow us to pass in a variable number of layers. We then take our [empty module list and append](https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html?highlight=extend#torch.nn.ModuleList.extend) a variable number of `GraphConv` layers followed by a variable number of `SAGPooling` layers. We finish up our", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "`SAGPool` definition by adding a [JumpingKnowledge Layer](https://pytorch-geometric.readthedocs.io/en/latest/modules/nn.html#torch_geometric.nn.models.JumpingKnowledge), two linear layers, our activation function, and our dropout value. PyTorch\u2019s intuitive syntax allows us to abstract away the complexity of working with state of the art methods like SAG Poolings while also maintaining the common approach to model development we are familiar with.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Models like our SAG Pool one described above are just one example of how GNNs with PyTorch are allowing us to explore new and novel ideas. We also recently explored [multimodal CNN - GNN hybrid models](https://openaccess.thecvf.com/content/CVPR2022W/CVMI/papers/Dwivedi_Multi_Stain_Graph_Fusion_for_Multimodal_Integration_in_Pathology_CVPRW_2022_paper.pdf) which ended up being 20% more accurate than traditional Pathologist consensus scores. These innovations and interplay between traditional CNNs and GNNs are", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "again enabled by the short research to production model development loop.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "Improving Patient Outcomes", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "In order to achieve our mission of improving patient outcomes with AI-powered pathology, PathAI needs to rely on an ML development framework that (1) facilitates quick iteration and easy extension (i.e. Model configuration as code) during initial phases of development and exploration (2) scales model training and inference to massive images (3) easily and robustly serves models for production uses of our products (in clinical trials and beyond). As we\u2019ve demonstrated, PyTorch offers us all of these", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} -{"page_content": "capabilities and more. We are incredibly excited about the future of PyTorch and cannot wait to see what other impactful challenges we can solve using the framework.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "One of the main value-adds of GNN\u2019s in the context of tissue samples is that the graph itself can uncover spatial relationships that would otherwise be very difficult to find by visual inspection alone. In our recent [AACR publication](https://aacrjournals.org/cancerres/article/82/12_Supplement/1922/701539), we showed that by using GNNs, we can better understand the way the presence of immune cell aggregates (specifically tertiary lymphoid structures, or TLS) in the tumor microenvironment can influence patient prognosis. In this case, the GNN approach was used to predict expression of genes associated with the presence of TLS, and identify histological features beyond the TLS region itself that are relevant to TLS. Such insights into gene expression are difficult to identify from tissue sample images when unassisted by ML models.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "One of the most promising GNN variations we have had success with is [self attention graph pooling](https://arxiv.org/pdf/1904.08082.pdf). Let\u2019s take a look at how we define our Self Attention Graph Pooling (SAGPool) model using PyTorch and PyG:\n\n```python\nclass SAGPool(torch.nn.Module):\n def __init__(self, ...):\n super().__init__()\n self.conv1 = GraphConv(in_features, hidden_features, aggr='mean')\n self.convs = torch.nn.ModuleList()\n self.pools = torch.nn.ModuleList()\n self.convs.extend([GraphConv(hidden_features, hidden_features, aggr='mean') for i in range(num_layers - 1)])\n self.pools.extend([SAGPooling(hidden_features, ratio, GNN=GraphConv, min_score=min_score) for i in range((num_layers) // 2)])\n self.jump = JumpingKnowledge(mode='cat')\n self.lin1 = Linear(num_layers * hidden_features, hidden_features)\n self.lin2 = Linear(hidden_features, out_features)\n self.out_activation = out_activation\n self.dropout = dropout\n```", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "In the above code, we begin by defining a single convolutional graph layer and then add two [module list layers](https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html) which allow us to pass in a variable number of layers. We then take our [empty module list and append](https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html?highlight=extend#torch.nn.ModuleList.extend) a variable number of `GraphConv` layers followed by a variable number of `SAGPooling` layers. We finish up our `SAGPool` definition by adding a [JumpingKnowledge Layer](https://pytorch-geometric.readthedocs.io/en/latest/modules/nn.html#torch_geometric.nn.models.JumpingKnowledge), two linear layers, our activation function, and our dropout value. PyTorch\u2019s intuitive syntax allows us to abstract away the complexity of working with state of the art methods like SAG Poolings while also maintaining the common approach to model development we are familiar with.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "Models like our SAG Pool one described above are just one example of how GNNs with PyTorch are allowing us to explore new and novel ideas. We also recently explored [multimodal CNN - GNN hybrid models](https://openaccess.thecvf.com/content/CVPR2022W/CVMI/papers/Dwivedi_Multi_Stain_Graph_Fusion_for_Multimodal_Integration_in_Pathology_CVPRW_2022_paper.pdf) which ended up being 20% more accurate than traditional Pathologist consensus scores. These innovations and interplay between traditional CNNs and GNNs are again enabled by the short research to production model development loop.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}} +{"page_content": "## Improving Patient Outcomes\nIn order to achieve our mission of improving patient outcomes with AI-powered pathology, PathAI needs to rely on an ML development framework that (1) facilitates quick iteration and easy extension (i.e. Model configuration as code) during initial phases of development and exploration (2) scales model training and inference to massive images (3) easily and robustly serves models for production uses of our products (in clinical trials and beyond). As we\u2019ve demonstrated, PyTorch offers us all of these capabilities and more. We are incredibly excited about the future of PyTorch and cannot wait to see what other impactful challenges we can solve using the framework.", "metadata": {"source": "https://pytorch.org/blog/PathAI-Uses-PyTorch-to-Improve-Patient-Outcomes-with-AI-powered-Pathology/", "category": "pytorch blogs"}}