diff --git a/.ci/docker/requirements.txt b/.ci/docker/requirements.txt index 58e73327ca..a25c4494b6 100644 --- a/.ci/docker/requirements.txt +++ b/.ci/docker/requirements.txt @@ -69,5 +69,5 @@ pycocotools semilearn==0.3.2 torchao==0.10.0 segment_anything==1.0 -torchrec==1.1.0; platform_system == "Linux" +torchrec==1.2.0; platform_system == "Linux" fbgemm-gpu==1.2.0; platform_system == "Linux" diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index e02ea6028f..9d2ec2d815 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -51,7 +51,6 @@ "intermediate_source/text_to_speech_with_torchaudio", "intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release. "advanced_source/semi_structured_sparse", # reenable after 3303 is fixed. - "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixe ] def tutorial_source_dirs() -> List[Path]: diff --git a/intermediate_source/torchrec_intro_tutorial.py b/intermediate_source/torchrec_intro_tutorial.py index 5f9464decd..81b7663c11 100644 --- a/intermediate_source/torchrec_intro_tutorial.py +++ b/intermediate_source/torchrec_intro_tutorial.py @@ -2,7 +2,7 @@ Introduction to TorchRec ================================== -**TorchRec** is a PyTorch library tailored for building scalable and efficient recommendation systems using embeddings. +**TorchRec** is a PyTorch library tailored for building scalable and efficient recommendation systems using embeddings. This tutorial guides you through the installation process, introduces the concept of embeddings, and highlights their importance in recommendation systems. It offers practical demonstrations on implementing embeddings with PyTorch and TorchRec, focusing on handling large embedding tables through distributed training and advanced optimizations. @@ -11,7 +11,7 @@ .. grid-item-card:: :octicon:`mortar-board;1em;` What you will learn :class-card: card-prerequisites - + * Fundamentals of embeddings and their role in recommendation systems * How to set up TorchRec to manage and implement embeddings in PyTorch environments * Explore advanced techniques for distributing large embedding tables across multiple GPUs @@ -30,7 +30,7 @@ # Install Dependencies # ^^^^^^^^^^^^^^^^^^^^ # -# Before running this tutorial in Google Colab or other environment, install the +# Before running this tutorial in Google Colab, make sure to install the # following dependencies: # # .. code-block:: sh @@ -40,54 +40,53 @@ # !pip3 install torchmetrics==1.0.3 # !pip3 install torchrec --index-url https://download.pytorch.org/whl/cu121 # -# .. note:: +# .. note:: # If you are running this in Google Colab, make sure to switch to a GPU runtime type. # For more information, # see `Enabling CUDA `__ # - ###################################################################### # Embeddings # ~~~~~~~~~~ -# +# # When building recommendation systems, categorical features typically # have massive cardinality, posts, users, ads, and so on. -# +# # In order to represent these entities and model these relationships, # **embeddings** are used. In machine learning, **embeddings are a vectors # of real numbers in a high-dimensional space used to represent meaning in # complex data like words, images, or users**. -# +# # Embeddings in RecSys # ~~~~~~~~~~~~~~~~~~~~ -# +# # Now you might wonder, how are these embeddings generated in the first # place? Well, embeddings are represented as individual rows in an # **Embedding Table**, also referred to as embedding weights. The reason # for this is that embeddings or embedding table weights are trained just # like all of the other weights of the model via gradient descent! -# +# # Embedding tables are simply a large matrix for storing embeddings, with # two dimensions (B, N), where: -# +# # * B is the number of embeddings stored by the table # * N is the number of dimensions per embedding (N-dimensional embedding). -# +# # The inputs to embedding tables represent embedding lookups to retrieve # the embedding for a specific index or row. In recommendation systems, such # as those used in many large systems, unique IDs are not only used for # specific users, but also across entities like posts and ads to serve as # lookup indices to respective embedding tables! -# +# # Embeddings are trained in RecSys through the following process: # # * **Input/lookup indices are fed into the model, as unique IDs**. IDs are # hashed to the total size of the embedding table to prevent issues when # the ID > number of rows -# -# * Embeddings are then retrieved and **pooled, such as taking the sum or +# +# * Embeddings are then retrieved and **pooled, such as taking the sum or # mean of the embeddings**. This is required as there can be a variable number of # embeddings per example while the model expects consistent shapes. # @@ -100,17 +99,17 @@ # for an example, and **all weights of the model are updated through # gradient descent and backpropagation, including the embedding weights** # that were associated with the example. -# +# # These embeddings are crucial for representing categorical features, such # as users, posts, and ads, in order to capture relationships and make # good recommendations. The `Deep learning recommendation # model `__ (DLRM) paper talks more # about the technical details of using embedding tables in RecSys. -# +# # This tutorial introduces the concept of embeddings, showcase # TorchRec specific modules and data types, and depict how distributed training # works with TorchRec. -# +# import torch @@ -118,19 +117,19 @@ ###################################################################### # Embeddings in PyTorch # --------------------- -# -# In PyTorch, we have the following types of embeddings: +# +# In PyTorch, we have the following types of embeddings: # # * :class:`torch.nn.Embedding`: An embedding table where forward pass returns the # embeddings themselves as is. -# +# # * :class:`torch.nn.EmbeddingBag`: Embedding table where forward pass returns # embeddings that are then pooled, for example, sum or mean, otherwise known # as **Pooled Embeddings**. -# +# # In this section, we will go over a very brief introduction to performing -# embedding lookups by passing in indices into the table. -# +# embedding lookups by passing in indices into the table. +# num_embeddings, embedding_dim = 10, 4 @@ -181,13 +180,13 @@ # systems! These tables represent entities and their relationships. For # example, the relationship between a given user and the pages and posts # they have liked. -# +# ###################################################################### # TorchRec Features Overview # ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -# +# # In the section above we've learned how to use embedding tables, one of the foundations of # modern recommendation systems! These tables represent entities and # relationships, such as users, pages, posts, etc. Given that these @@ -197,26 +196,25 @@ # collisions, these tables can become quite massive (think about the number of ads # for example). In fact, these tables can become so massive that they # won't be able to fit on 1 GPU, even with 80G of memory. -# +# # In order to train models with massive embedding tables, sharding these # tables across GPUs is required, which then introduces a whole new set of # problems and opportunities in parallelism and optimization. Luckily, we have -# the TorchRec library that has encountered, consolidated, and addressed +# the TorchRec library `__ that has encountered, consolidated, and addressed # many of these concerns. TorchRec serves as a **library that provides # primitives for large scale distributed embeddings**. -# +# # Next, we will explore the major features of the TorchRec # library. We will start with ``torch.nn.Embedding`` and will extend that to # custom TorchRec modules, explore distributed training environment with # generating a sharding plan for embeddings, look at inherent TorchRec # optimizations, and extend the model to be ready for inference in C++. # Below is a quick outline of what this section consists of: -# +# # * TorchRec Modules and Data Types # * Distributed Training, Sharding, and Optimizations -# * Inference -# -# Let's begin with importing TorchRec: +# +# Let's begin with importing TorchRec: import torchrec @@ -224,26 +222,26 @@ ###################################################################### # TorchRec Modules and Data Types # ---------------------------------- -# +# # This section goes over TorchRec Modules and data types including such # entities as ``EmbeddingCollection`` and ``EmbeddingBagCollection``, # ``JaggedTensor``, ``KeyedJaggedTensor``, ``KeyedTensor`` and more. # # From ``EmbeddingBag`` to ``EmbeddingBagCollection`` # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # We have already explored :class:`torch.nn.Embedding` and :class:`torch.nn.EmbeddingBag`. # TorchRec extends these modules by creating collections of embeddings, in # other words modules that can have multiple embedding tables, with # ``EmbeddingCollection`` and ``EmbeddingBagCollection`` # We will use ``EmbeddingBagCollection`` to represent a group of # embedding bags. -# +# # In the example code below, we create an ``EmbeddingBagCollection`` (EBC) -# with two embedding bags, 1 representing **products** and 1 representing **users**. +# with two embedding bags, 1 representing **products** and 1 representing **users**. # Each table, ``product_table`` and ``user_table``, is represented by a 64 dimension # embedding of size 4096. -# +# ebc = torchrec.EmbeddingBagCollection( device="cpu", @@ -261,8 +259,8 @@ num_embeddings=4096, feature_names=["user"], pooling=torchrec.PoolingType.SUM, - ) - ] + ), + ], ) print(ebc.embedding_bags) @@ -270,7 +268,7 @@ ###################################################################### # Let’s inspect the forward method for ``EmbeddingBagCollection`` and the # module’s inputs and outputs: -# +# import inspect @@ -282,13 +280,13 @@ ###################################################################### # TorchRec Input/Output Data Types # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # TorchRec has distinct data types for input and output of its modules: # ``JaggedTensor``, ``KeyedJaggedTensor``, and ``KeyedTensor``. Now you # might ask, why create new data types to represent sparse features? To # answer that question, we must understand how sparse features are # represented in code. -# +# # Sparse features are otherwise known as ``id_list_feature`` and # ``id_score_list_feature``, and are the **IDs** that will be used as # indices to an embedding table to retrieve the embedding for that ID. To @@ -299,11 +297,11 @@ # these features in code is that in each input example, **the number of # IDs is variable**. One day a user might have interacted with only one ad # while the next day they interact with three. -# +# # A simple representation is shown below, where we have a ``lengths`` # tensor denoting how many indices are in an example for a batch and a # ``values`` tensor containing the indices themselves. -# +# # Batch Size 2 # 1 ID in example 1, 2 IDs in example 2 @@ -315,7 +313,7 @@ ###################################################################### # Next, let's look at the offsets as well as what is contained in each batch -# +# # Lengths can be converted to offsets for easy indexing of values id_list_feature_offsets = torch.cumsum(id_list_feature_lengths, dim=0) @@ -394,16 +392,16 @@ # Congrats! You now understand TorchRec modules and data types. # Give yourself a pat on the back for making it this far. Next, we will # learn about distributed training and sharding. -# +# ###################################################################### # Distributed Training and Sharding # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # Now that we have a grasp on TorchRec modules and data types, it's time # to take it to the next level. -# +# # Remember, the main purpose of TorchRec is to provide primitives for # distributed embeddings. So far, we've only worked with embedding tables # on a single device. This has been possible given how small the embedding tables @@ -411,18 +409,18 @@ # Embedding tables often get massive, where one table can't fit on a single # GPU, creating the requirement for multiple devices and a distributed # environment. -# +# # In this section, we will explore setting up a distributed environment, # exactly how actual production training is done, and explore sharding # embedding tables, all with TorchRec. -# +# # **This section will also only use 1 GPU, though it will be treated in a # distributed fashion. This is only a limitation for training, as training # has a process per GPU. Inference does not run into this requirement** -# +# # In the example code below, we set up our PyTorch distributed environment. # -# .. warning:: +# .. warning:: # If you are running this in Google Colab, you can only call this cell once, # calling it again will cause an error as you can only initialize the process # group once. @@ -450,46 +448,46 @@ ###################################################################### # Distributed Embeddings # ~~~~~~~~~~~~~~~~~~~~~~ -# +# # We have already worked with the main TorchRec module: # ``EmbeddingBagCollection``. We have examined how it works along with how # data is represented in TorchRec. However, we have not yet explored one # of the main parts of TorchRec, which is **distributed embeddings**. -# +# # GPUs are the most popular choice for ML workloads by far today, as they # are able to do magnitudes more floating point operations/s # (`FLOPs `__) than CPU. However, # GPUs come with the limitation of scarce fast memory (HBM which is # analogous to RAM for CPU), typically, ~10s of GBs. -# +# # A RecSys model can contain embedding tables that far exceed the memory # limit for 1 GPU, hence the need for distribution of the embedding tables # across multiple GPUs, otherwise known as **model parallel**. On the # other hand, **data parallel** is where the entire model is replicated on # each GPU, which each GPU taking in a distinct batch of data for # training, syncing gradients on the backwards pass. -# +# # Parts of the model that **require less compute but more memory # (embeddings) are distributed with model parallel** while parts that # **require more compute and less memory (dense layers, MLP, etc.) are # distributed with data parallel**. -# +# # Sharding # ~~~~~~~~ -# +# # In order to distribute an embedding table, we split up the embedding # table into parts and place those parts onto different devices, also # known as “sharding”. -# +# # There are many ways to shard embedding tables. The most common ways are: # # * Table-Wise: the table is placed entirely onto one device # * Column-Wise: columns of embedding tables are sharded # * Row-Wise: rows of embedding tables are sharded -# +# # Sharded Modules # ~~~~~~~~~~~~~~~ -# +# # While all of this seems like a lot to deal with and implement, you're in # luck. **TorchRec provides all the primitives for easy distributed # training and inference**! In fact, TorchRec modules have two corresponding @@ -498,23 +496,23 @@ # # * **The module sharder**: This class exposes a ``shard`` API # that handles sharding a TorchRec Module, producing a sharded module. -# * For ``EmbeddingBagCollection``, the sharder is `EmbeddingBagCollectionSharder `__ +# * For ``EmbeddingBagCollection``, the sharder is `EmbeddingBagCollectionSharder ` # * **Sharded module**: This class is a sharded variant of a TorchRec module. # It has the same input/output as a the regular TorchRec module, but much # more optimized and works in a distributed environment. -# * For ``EmbeddingBagCollection``, the sharded variant is `ShardedEmbeddingBagCollection `__ -# +# * For ``EmbeddingBagCollection``, the sharded variant is `ShardedEmbeddingBagCollection` +# # Every TorchRec module has an unsharded and sharded variant. -# +# # * The unsharded version is meant to be prototyped and experimented with. # * The sharded version is meant to be used in a distributed environment for # distributed training and inference. -# +# # The sharded versions of TorchRec modules, for example # ``EmbeddingBagCollection``, will handle everything that is needed for Model # Parallelism, such as communication between GPUs for distributing # embeddings to the correct GPUs. -# +# # Refresher of our ``EmbeddingBagCollection`` module ebc @@ -535,10 +533,10 @@ ###################################################################### # Planner # ~~~~~~~ -# +# # Before we can show how sharding works, we must know about the # **planner**, which helps us determine the best sharding configuration. -# +# # Given a number of embedding tables and a number of ranks, there are many # different sharding configurations that are possible. For example, given # 2 embedding tables and 2 GPUs, you can: @@ -546,34 +544,34 @@ # * Place 1 table on each GPU # * Place both tables on a single GPU and no tables on the other # * Place certain rows and columns on each GPU -# +# # Given all of these possibilities, we typically want a sharding # configuration that is optimal for performance. -# +# # That is where the planner comes in. The planner is able to determine # given the number of embedding tables and the number of GPUs, what is the optimal # configuration. Turns out, this is incredibly difficult to do manually, # with tons of factors that engineers have to consider to ensure an # optimal sharding plan. Luckily, TorchRec provides an auto planner when -# the planner is used. -# +# the planner is used. +# # The TorchRec planner: -# +# # * Assesses memory constraints of hardware # * Estimates compute based on memory fetches as embedding lookups # * Addresses data specific factors # * Considers other hardware specifics like bandwidth to generate an optimal sharding plan -# +# # In order to take into consideration all these variables, The TorchRec # planner can take in `various amounts of data for embedding tables, # constraints, hardware information, and # topology `__ # to aid in generating the optimal sharding plan for a model, which is # routinely provided across stacks. -# +# # To learn more about sharding, see our `sharding # tutorial `__. -# +# # In our case, 1 GPU and compute on CUDA device planner = EmbeddingShardingPlanner( @@ -592,16 +590,16 @@ ###################################################################### # Planner Result # ~~~~~~~~~~~~~~ -# +# # As you can see above, when running the planner there is quite a bit of output. # We can see a lot of stats being calculated along with where our # tables end up being placed. -# +# # The result of running the planner is a static plan, which can be reused # for sharding! This allows sharding to be static for production models # instead of determining a new sharding plan everytime. Below, we use the # sharding plan to finally generate our ``ShardedEmbeddingBagCollection``. -# +# # The static plan that was generated plan @@ -617,15 +615,15 @@ ###################################################################### # GPU Training with ``LazyAwaitable`` # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -# +# # Remember that TorchRec is a highly optimized library for distributed # embeddings. A concept that TorchRec introduces to enable higher # performance for training on GPU is a -# `LazyAwaitable `__. +# `LazyAwaitable `. # You will see ``LazyAwaitable`` types as outputs of various sharded # TorchRec modules. All a ``LazyAwaitable`` type does is delay calculating some # result as long as possible, and it does it by acting like an async type. -# +# from typing import List @@ -669,19 +667,19 @@ def _wait_impl(self) -> torch.Tensor: ###################################################################### # Anatomy of Sharded TorchRec modules # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # We have now successfully sharded an ``EmbeddingBagCollection`` given a # sharding plan that we generated! The sharded module has common APIs from # TorchRec which abstract away distributed communication/compute amongst # multiple GPUs. In fact, these APIs are highly optimized for performance # in training and inference. **Below are the three common APIs for # distributed training/inference** that are provided by TorchRec: -# +# # * ``input_dist``: Handles distributing inputs from GPU to GPU. # * ``lookups``: Does the actual embedding lookup in an optimized, # batched manner using FBGEMM TBE (more on this later). # * ``output_dist``: Handles distributing outputs from GPU to GPU. -# +# # The distribution of inputs and outputs is done through `NCCL # Collectives `__, # namely @@ -690,13 +688,13 @@ def _wait_impl(self) -> torch.Tensor: # TorchRec interfaces with PyTorch distributed for collectives and # provides clean abstractions to the end users, removing the concern for # the lower level details. -# +# # The backwards pass does all of these collectives but in the reverse # order for distribution of gradients. ``input_dist``, ``lookup``, and # ``output_dist`` all depend on the sharding scheme. Since we sharded in a # table-wise fashion, these APIs are modules that are constructed by -# `TwPooledEmbeddingSharding `__. -# +# `TwPooledEmbeddingSharding`. +# sharded_ebc @@ -710,27 +708,27 @@ def _wait_impl(self) -> torch.Tensor: ###################################################################### # Optimizing Embedding Lookups # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # In performing lookups for a collection of embedding tables, a trivial # solution would be to iterate through all the ``nn.EmbeddingBags`` and do # a lookup per table. This is exactly what the standard, unsharded # ``EmbeddingBagCollection`` does. However, while this solution # is simple, it is extremely slow. -# +# # `FBGEMM `__ is a # library that provides GPU operators (otherwise known as kernels) that # are very optimized. One of these operators is known as **Table Batched # Embedding** (TBE), provides two major optimizations: -# +# # - Table batching, which allows you to look up multiple embeddings with # one kernel call. # - Optimizer Fusion, which allows the module to update itself given the # canonical pytorch optimizers and arguments. -# +# # The ``ShardedEmbeddingBagCollection`` uses the FBGEMM TBE as the lookup # instead of traditional ``nn.EmbeddingBags`` for optimized embedding # lookups. -# +# sharded_ebc._lookups @@ -738,28 +736,28 @@ def _wait_impl(self) -> torch.Tensor: ###################################################################### # ``DistributedModelParallel`` # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # We have now explored sharding a single ``EmbeddingBagCollection``! We were # able to take the ``EmbeddingBagCollectionSharder`` and use the unsharded # ``EmbeddingBagCollection`` to generate a # ``ShardedEmbeddingBagCollection`` module. This workflow is fine, but # typically when implementing model parallel, -# `DistributedModelParallel `__ +# `DistributedModelParallel` # (DMP) is used as the standard interface. When wrapping your model (in # our case ``ebc``), with DMP, the following will occur: -# +# # 1. Decide how to shard the model. DMP will collect the available # sharders and come up with a plan of the optimal way to shard the # embedding table(s) (for example, ``EmbeddingBagCollection``) # 2. Actually shard the model. This includes allocating memory for each # embedding table on the appropriate device(s). -# +# # DMP takes in everything that we've just experimented with, like a static # sharding plan, a list of sharders, etc. However, it also has some nice # defaults to seamlessly shard a TorchRec model. In this toy example, # since we have two embedding tables and one GPU, TorchRec will place both # on the single GPU. -# +# ebc @@ -771,10 +769,12 @@ def _wait_impl(self) -> torch.Tensor: model +from fbgemm_gpu.split_embedding_configs import EmbOptimType + ###################################################################### # Sharding Best Practices # ~~~~~~~~~~~~~~~~~~~~~~~ -# +# # Currently, our configuration is only sharding on 1 GPU (or rank), which # is trivial: just place all the tables on 1 GPUs memory. However, in real # production use cases, embedding tables are **typically sharded on @@ -783,26 +783,26 @@ def _wait_impl(self) -> torch.Tensor: # proper sharding configuration (to prevent out of memory issues) while # keeping it balanced not only in terms of memory but also compute for # optimal performance. -# +# ###################################################################### # Adding in the Optimizer # ~~~~~~~~~~~~~~~~~~~~~~~ -# +# # Remember that TorchRec modules are hyperoptimized for large scale # distributed training. An important optimization is in regards to the -# optimizer. +# optimizer. # # TorchRec modules provide a seamless API to fuse the # backwards pass and optimize step in training, providing a significant # optimization in performance and decreasing the memory used, alongside # granularity in assigning distinct optimizers to distinct model # parameters. -# +# # Optimizer Classes # ^^^^^^^^^^^^^^^^^ -# +# # TorchRec uses ``CombinedOptimizer``, which contains a collection of # ``KeyedOptimizers``. A ``CombinedOptimizer`` effectively makes it easy # to handle multiple optimizers for various sub groups in the model. A @@ -810,47 +810,46 @@ def _wait_impl(self) -> torch.Tensor: # initialized through a dictionary of parameters exposes the parameters. # Each ``TBE`` module in a ``EmbeddingBagCollection`` will have it's own # ``KeyedOptimizer`` which combines into one ``CombinedOptimizer``. -# +# # Fused optimizer in TorchRec # ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -# +# # Using ``DistributedModelParallel``, the **optimizer is fused, which # means that the optimizer update is done in the backward**. This is an # optimization in TorchRec and FBGEMM, where the optimizer embedding # gradients are not materialized and applied directly to the parameters. # This brings significant memory savings as embedding gradients are # typically size of the parameters themselves. -# +# # You can, however, choose to make the optimizer ``dense`` which does not # apply this optimization and let's you inspect the embedding gradients or # apply computations to it as you wish. A dense optimizer in this case # would be your `canonical PyTorch model training loop with # optimizer. `__ -# +# # Once the optimizer is created through ``DistributedModelParallel``, you # still need to manage an optimizer for the other parameters not # associated with TorchRec embedding modules. To find the other # parameters, -# use ``in_backward_optimizer_filter(model.named_parameters())``. +# use ``in_backward_optimizer_filter(model.named_parameters())``. # Apply an optimizer to those parameters as you would a normal Torch # optimizer and combine this and the ``model.fused_optimizer`` into one # ``CombinedOptimizer`` that you can use in your training loop to # ``zero_grad`` and ``step`` through. -# +# # Adding an Optimizer to ``EmbeddingBagCollection`` # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -# +# # We will do this in two ways, which are equivalent, but give you options # depending on your preferences: # # 1. Passing optimizer kwargs through ``fused_params`` in sharder. # 2. Through ``apply_optimizer_in_backward``, which converts the optimizer # parameters to ``fused_params`` to pass to the ``TBE`` in the ``EmbeddingBagCollection`` or ``EmbeddingCollection``. -# +# # Option 1: Passing optimizer kwargs through fused parameters from torchrec.optim.optimizers import in_backward_optimizer_filter -from fbgemm_gpu.split_embedding_configs import EmbOptimType # We initialize the sharder with @@ -864,17 +863,25 @@ def _wait_impl(self) -> torch.Tensor: sharder_with_fused_params = EmbeddingBagCollectionSharder(fused_params=fused_params) # We'll use same plan and unsharded EBC as before but this time with our new sharder -sharded_ebc_fused_params = sharder_with_fused_params.shard(ebc, plan.plan[""], env, torch.device("cuda")) +sharded_ebc_fused_params = sharder_with_fused_params.shard( + ebc, plan.plan[""], env, torch.device("cuda") +) # Looking at the optimizer of each, we can see that the learning rate changed, which indicates our optimizer has been applied correctly. # If seen, we can also look at the TBE logs of the cell to see that our new optimizer is indeed being applied print(f"Original Sharded EBC fused optimizer: {sharded_ebc.fused_optimizer}") -print(f"Sharded EBC with fused parameters fused optimizer: {sharded_ebc_fused_params.fused_optimizer}") +print( + f"Sharded EBC with fused parameters fused optimizer: {sharded_ebc_fused_params.fused_optimizer}" +) print(f"Type of optimizer: {type(sharded_ebc_fused_params.fused_optimizer)}") -from torch.distributed.optim import _apply_optimizer_in_backward as apply_optimizer_in_backward import copy + +from torch.distributed.optim import ( + _apply_optimizer_in_backward as apply_optimizer_in_backward, +) + # Option 2: Applying optimizer through apply_optimizer_in_backward # Note: we need to call apply_optimizer_in_backward on unsharded model first and then shard it @@ -886,7 +893,9 @@ def _wait_impl(self) -> torch.Tensor: print(f"{name=}") apply_optimizer_in_backward(torch.optim.SGD, [param], optimizer_kwargs) -sharded_ebc_apply_opt = sharder.shard(ebc_apply_opt, plan.plan[""], env, torch.device("cuda")) +sharded_ebc_apply_opt = sharder.shard( + ebc_apply_opt, plan.plan[""], env, torch.device("cuda") +) # Now when we print the optimizer, we will see our new learning rate, you can verify momentum through the TBE logs as well if outputted print(sharded_ebc_apply_opt.fused_optimizer) @@ -896,7 +905,11 @@ def _wait_impl(self) -> torch.Tensor: # Practically, just non TorchRec module parameters. Since our module is just a TorchRec EBC # there are no other parameters that aren't associated with TorchRec print("Non Fused Model Parameters:") -print(dict(in_backward_optimizer_filter(sharded_ebc_fused_params.named_parameters())).keys()) +print( + dict( + in_backward_optimizer_filter(sharded_ebc_fused_params.named_parameters()) + ).keys() +) # Here we do a dummy backwards call and see that parameter updates for fused # optimizers happen as a result of the backward pass @@ -915,202 +928,23 @@ def _wait_impl(self) -> torch.Tensor: print(f"Second Iteration Loss: {loss}") -###################################################################### -# Inference -# ~~~~~~~~~ -# -# Now that we are able to train distributed embeddings, how can we take -# the trained model and optimize it for inference? Inference is typically -# very sensitive to **performance and size of the model**. Running just -# the trained model in a Python environment is incredibly inefficient. -# There are two key differences between inference and training -# environments: -# -# * **Quantization**: Inference models are typically -# quantized, where model parameters lose precision for lower latency in -# predictions and reduced model size. For example FP32 (4 bytes) in -# trained model to INT8 (1 byte) for each embedding weight. This is also -# necessary given the vast scale of embedding tables, as we want to use as -# few devices as possible for inference to minimize latency. -# -# * **C++ environment**: Inference latency is very important, so in order to ensure -# ample performance, the model is typically ran in a C++ environment, -# along with the situations where we don't have a Python runtime, like on -# device. -# -# TorchRec provides primitives for converting a TorchRec model into being -# inference ready with: -# -# * APIs for quantizing the model, introducing -# optimizations automatically with FBGEMM TBE -# * Sharding embeddings for distributed inference -# * Compiling the model to `TorchScript `__ -# (compatible in C++) -# -# In this section, we will go over this entire workflow of: -# -# * Quantizing the model -# * Sharding the quantized model -# * Compiling the sharded quantized model into TorchScript -# - -ebc - -class InferenceModule(torch.nn.Module): - def __init__(self, ebc: torchrec.EmbeddingBagCollection): - super().__init__() - self.ebc_ = ebc - - def forward(self, kjt: KeyedJaggedTensor): - return self.ebc_(kjt) - -module = InferenceModule(ebc) -for name, param in module.named_parameters(): - # Here, the parameters should still be FP32, as we are using a standard EBC - # FP32 is default, regularly used for training - print(name, param.shape, param.dtype) - - -###################################################################### -# Quantization -# ~~~~~~~~~~~~ -# -# As you can see above, the normal EBC contains embedding table weights as -# FP32 precision (32 bits for each weight). Here, we will use the TorchRec -# inference library to quantize the embedding weights of the model to INT8 -# - -from torch import quantization as quant -from torchrec.modules.embedding_configs import QuantConfig -from torchrec.quant.embedding_modules import ( - EmbeddingBagCollection as QuantEmbeddingBagCollection, -) - - -quant_dtype = torch.int8 - - -qconfig = QuantConfig( - # dtype of the result of the embedding lookup, post activation - # torch.float generally for compatibility with rest of the model - # as rest of the model here usually isn't quantized - activation=quant.PlaceholderObserver.with_args(dtype=torch.float), - # quantized type for embedding weights, aka parameters to actually quantize - weight=quant.PlaceholderObserver.with_args(dtype=quant_dtype), -) -qconfig_spec = { - # Map of module type to qconfig - torchrec.EmbeddingBagCollection: qconfig, -} -mapping = { - # Map of module type to quantized module type - torchrec.EmbeddingBagCollection: QuantEmbeddingBagCollection, -} - - -module = InferenceModule(ebc) - -# Quantize the module -qebc = quant.quantize_dynamic( - module, - qconfig_spec=qconfig_spec, - mapping=mapping, - inplace=False, -) - - -print(f"Quantized EBC: {qebc}") - -kjt = kjt.to("cpu") - -qebc(kjt) - -# Once quantized, goes from parameters -> buffers, as no longer trainable -for name, buffer in qebc.named_buffers(): - # The shapes of the tables should be the same but the dtype should be int8 now - # post quantization - print(name, buffer.shape, buffer.dtype) - - -###################################################################### -# Shard -# ~~~~~ -# -# Here we perform sharding of the TorchRec quantized model. This is to -# ensure we are using the performant module through FBGEMM TBE. Here we -# are using one device to be consistent with training (1 TBE). -# - -from torchrec import distributed as trec_dist -from torchrec.distributed.shard import _shard_modules - - -sharded_qebc = _shard_modules( - module=qebc, - device=torch.device("cpu"), - env=trec_dist.ShardingEnv.from_local( - 1, - 0, - ), -) - - -print(f"Sharded Quantized EBC: {sharded_qebc}") - -sharded_qebc(kjt) - - -###################################################################### -# Compilation -# ~~~~~~~~~~~ -# -# Now we have the optimized eager TorchRec inference model. The next step -# is to ensure that this model is loadable in C++, as currently it is only -# runnable in a Python runtime. -# -# The recommended method of compilation at Meta is two fold: `torch.fx -# tracing `__ (generate -# intermediate representation of model) and converting the result to -# TorchScript, where TorchScript is C++ compatible. -# - -from torchrec.fx import Tracer - - -tracer = Tracer(leaf_modules=["IntNBitTableBatchedEmbeddingBagsCodegen"]) - -graph = tracer.trace(sharded_qebc) -gm = torch.fx.GraphModule(sharded_qebc, graph) - -print("Graph Module Created!") - -print(gm.code) - -scripted_gm = torch.jit.script(gm) -print("Scripted Graph Module Created!") - -print(scripted_gm.code) - - ###################################################################### # Conclusion # ^^^^^^^^^^ -# -# In this tutorial, you have gone from training a distributed RecSys model all the way -# to making it inference ready. The `TorchRec repo +# In this tutorial, you have done training a distributed RecSys model +# If you are interested in the inference the `TorchRec repo # `__ has a -# full example of how to load a TorchRec TorchScript model into C++ for -# inference. -# +# full example of how to run the TorchRec in Inference mode. +# ###################################################################### # See Also # -------------- -# +# # For more information, please see our # `dlrm `__ # example, which includes multinode training on the Criteo 1TB # dataset using the methods described in `Deep Learning Recommendation Model # for Personalization and Recommendation Systems `__. -# +#