{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# For tips on running notebooks in Google Colab, see\n", "# https://pytorch.org/tutorials/beginner/colab\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Introduction to TorchRec\n", "========================\n", "\n", "**TorchRec** is a PyTorch library tailored for building scalable and\n", "efficient recommendation systems using embeddings. This tutorial guides\n", "you through the installation process, introduces the concept of\n", "embeddings, and highlights their importance in recommendation systems.\n", "It offers practical demonstrations on implementing embeddings with\n", "PyTorch and TorchRec, focusing on handling large embedding tables\n", "through distributed training and advanced optimizations.\n", "\n", "

What you will learn

Prerequisites

\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Install Dependencies\n", "====================\n", "\n", "Before running this tutorial in Google Colab or other environment,\n", "install the following dependencies:\n", "\n", "``` {.sourceCode .sh}\n", "!pip3 install --pre torch --index-url https://download.pytorch.org/whl/cu121 -U\n", "!pip3 install fbgemm_gpu --index-url https://download.pytorch.org/whl/cu121\n", "!pip3 install torchmetrics==1.0.3\n", "!pip3 install torchrec --index-url https://download.pytorch.org/whl/cu121\n", "```\n", "\n", "
NOTE:
\n", "
\n", "

If you are running this in Google Colab, make sure to switch to a GPU runtime type.For more information,see Enabling CUDA

\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Embeddings\n", "==========\n", "\n", "When building recommendation systems, categorical features typically\n", "have massive cardinality, posts, users, ads, and so on.\n", "\n", "In order to represent these entities and model these relationships,\n", "**embeddings** are used. In machine learning, **embeddings are a vectors\n", "of real numbers in a high-dimensional space used to represent meaning in\n", "complex data like words, images, or users**.\n", "\n", "Embeddings in RecSys\n", "====================\n", "\n", "Now you might wonder, how are these embeddings generated in the first\n", "place? Well, embeddings are represented as individual rows in an\n", "**Embedding Table**, also referred to as embedding weights. The reason\n", "for this is that embeddings or embedding table weights are trained just\n", "like all of the other weights of the model via gradient descent!\n", "\n", "Embedding tables are simply a large matrix for storing embeddings, with\n", "two dimensions (B, N), where:\n", "\n", "- B is the number of embeddings stored by the table\n", "- N is the number of dimensions per embedding (N-dimensional\n", " embedding).\n", "\n", "The inputs to embedding tables represent embedding lookups to retrieve\n", "the embedding for a specific index or row. In recommendation systems,\n", "such as those used in many large systems, unique IDs are not only used\n", "for specific users, but also across entities like posts and ads to serve\n", "as lookup indices to respective embedding tables!\n", "\n", "Embeddings are trained in RecSys through the following process:\n", "\n", "- **Input/lookup indices are fed into the model, as unique IDs**. IDs\n", " are hashed to the total size of the embedding table to prevent\n", " issues when the ID \\> number of rows\n", "- Embeddings are then retrieved and **pooled, such as taking the sum\n", " or mean of the embeddings**. This is required as there can be a\n", " variable number of embeddings per example while the model expects\n", " consistent shapes.\n", "- The **embeddings are used in conjunction with the rest of the model\n", " to produce a prediction**, such as [Click-Through Rate\n", " (CTR)](https://support.google.com/google-ads/answer/2615875?hl=en)\n", " for an ad.\n", "- The loss is calculated with the prediction and the label for an\n", " example, and **all weights of the model are updated through gradient\n", " descent and backpropagation, including the embedding weights** that\n", " were associated with the example.\n", "\n", "These embeddings are crucial for representing categorical features, such\n", "as users, posts, and ads, in order to capture relationships and make\n", "good recommendations. The [Deep learning recommendation\n", "model](https://arxiv.org/abs/1906.00091) (DLRM) paper talks more about\n", "the technical details of using embedding tables in RecSys.\n", "\n", "This tutorial introduces the concept of embeddings, showcase TorchRec\n", "specific modules and data types, and depict how distributed training\n", "works with TorchRec.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Embeddings in PyTorch\n", "=====================\n", "\n", "In PyTorch, we have the following types of embeddings:\n", "\n", "- `torch.nn.Embedding`{.interpreted-text role=\"class\"}: An embedding\n", " table where forward pass returns the embeddings themselves as is.\n", "- `torch.nn.EmbeddingBag`{.interpreted-text role=\"class\"}: Embedding\n", " table where forward pass returns embeddings that are then pooled,\n", " for example, sum or mean, otherwise known as **Pooled Embeddings**.\n", "\n", "In this section, we will go over a very brief introduction to performing\n", "embedding lookups by passing in indices into the table.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "num_embeddings, embedding_dim = 10, 4\n", "\n", "# Initialize our embedding table\n", "weights = torch.rand(num_embeddings, embedding_dim)\n", "print(\"Weights:\", weights)\n", "\n", "# Pass in pre-generated weights just for example, typically weights are randomly initialized\n", "embedding_collection = torch.nn.Embedding(\n", " num_embeddings, embedding_dim, _weight=weights\n", ")\n", "embedding_bag_collection = torch.nn.EmbeddingBag(\n", " num_embeddings, embedding_dim, _weight=weights\n", ")\n", "\n", "# Print out the tables, we should see the same weights as above\n", "print(\"Embedding Collection Table: \", embedding_collection.weight)\n", "print(\"Embedding Bag Collection Table: \", embedding_bag_collection.weight)\n", "\n", "# Lookup rows (ids for embedding ids) from the embedding tables\n", "# 2D tensor with shape (batch_size, ids for each batch)\n", "ids = torch.tensor([[1, 3]])\n", "print(\"Input row IDS: \", ids)\n", "\n", "embeddings = embedding_collection(ids)\n", "\n", "# Print out the embedding lookups\n", "# You should see the specific embeddings be the same as the rows (ids) of the embedding tables above\n", "print(\"Embedding Collection Results: \")\n", "print(embeddings)\n", "print(\"Shape: \", embeddings.shape)\n", "\n", "# ``nn.EmbeddingBag`` default pooling is mean, so should be mean of batch dimension of values above\n", "pooled_embeddings = embedding_bag_collection(ids)\n", "\n", "print(\"Embedding Bag Collection Results: \")\n", "print(pooled_embeddings)\n", "print(\"Shape: \", pooled_embeddings.shape)\n", "\n", "# ``nn.EmbeddingBag`` is the same as ``nn.Embedding`` but just with pooling (mean, sum, and so on)\n", "# We can see that the mean of the embeddings of embedding_collection is the same as the output of the embedding_bag_collection\n", "print(\"Mean: \", torch.mean(embedding_collection(ids), dim=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Congratulations! Now you have a basic understanding of how to use\n", "embedding tables \\-\\-- one of the foundations of modern recommendation\n", "systems! These tables represent entities and their relationships. For\n", "example, the relationship between a given user and the pages and posts\n", "they have liked.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TorchRec Features Overview\n", "==========================\n", "\n", "In the section above we\\'ve learned how to use embedding tables, one of\n", "the foundations of modern recommendation systems! These tables represent\n", "entities and relationships, such as users, pages, posts, etc. Given that\n", "these entities are always increasing, a **hash** function is typically\n", "applied to make sure the IDs are within the bounds of a certain\n", "embedding table. However, in order to represent a vast amount of\n", "entities and reduce hash collisions, these tables can become quite\n", "massive (think about the number of ads for example). In fact, these\n", "tables can become so massive that they won\\'t be able to fit on 1 GPU,\n", "even with 80G of memory.\n", "\n", "In order to train models with massive embedding tables, sharding these\n", "tables across GPUs is required, which then introduces a whole new set of\n", "problems and opportunities in parallelism and optimization. Luckily, we\n", "have the TorchRec library that has encountered, consolidated, and\n", "addressed many of these concerns. TorchRec serves as a **library that\n", "provides primitives for large scale distributed embeddings**.\n", "\n", "Next, we will explore the major features of the TorchRec library. We\n", "will start with `torch.nn.Embedding` and will extend that to custom\n", "TorchRec modules, explore distributed training environment with\n", "generating a sharding plan for embeddings, look at inherent TorchRec\n", "optimizations, and extend the model to be ready for inference in C++.\n", "Below is a quick outline of what this section consists of:\n", "\n", "- TorchRec Modules and Data Types\n", "- Distributed Training, Sharding, and Optimizations\n", "- Inference\n", "\n", "Let\\'s begin with importing TorchRec:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import torchrec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TorchRec Modules and Data Types\n", "===============================\n", "\n", "This section goes over TorchRec Modules and data types including such\n", "entities as `EmbeddingCollection` and `EmbeddingBagCollection`,\n", "`JaggedTensor`, `KeyedJaggedTensor`, `KeyedTensor` and more.\n", "\n", "From `EmbeddingBag` to `EmbeddingBagCollection`\n", "-----------------------------------------------\n", "\n", "We have already explored `torch.nn.Embedding`{.interpreted-text\n", "role=\"class\"} and `torch.nn.EmbeddingBag`{.interpreted-text\n", "role=\"class\"}. TorchRec extends these modules by creating collections of\n", "embeddings, in other words modules that can have multiple embedding\n", "tables, with `EmbeddingCollection` and `EmbeddingBagCollection` We will\n", "use `EmbeddingBagCollection` to represent a group of embedding bags.\n", "\n", "In the example code below, we create an `EmbeddingBagCollection` (EBC)\n", "with two embedding bags, 1 representing **products** and 1 representing\n", "**users**. Each table, `product_table` and `user_table`, is represented\n", "by a 64 dimension embedding of size 4096.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ebc = torchrec.EmbeddingBagCollection(\n", " device=\"cpu\",\n", " tables=[\n", " torchrec.EmbeddingBagConfig(\n", " name=\"product_table\",\n", " embedding_dim=64,\n", " num_embeddings=4096,\n", " feature_names=[\"product\"],\n", " pooling=torchrec.PoolingType.SUM,\n", " ),\n", " torchrec.EmbeddingBagConfig(\n", " name=\"user_table\",\n", " embedding_dim=64,\n", " num_embeddings=4096,\n", " feature_names=[\"user\"],\n", " pooling=torchrec.PoolingType.SUM,\n", " )\n", " ]\n", ")\n", "print(ebc.embedding_bags)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's inspect the forward method for `EmbeddingBagCollection` and the\n", "module's inputs and outputs:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import inspect\n", "\n", "# Let's look at the ``EmbeddingBagCollection`` forward method\n", "# What is a ``KeyedJaggedTensor`` and ``KeyedTensor``?\n", "print(inspect.getsource(ebc.forward))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TorchRec Input/Output Data Types\n", "================================\n", "\n", "TorchRec has distinct data types for input and output of its modules:\n", "`JaggedTensor`, `KeyedJaggedTensor`, and `KeyedTensor`. Now you might\n", "ask, why create new data types to represent sparse features? To answer\n", "that question, we must understand how sparse features are represented in\n", "code.\n", "\n", "Sparse features are otherwise known as `id_list_feature` and\n", "`id_score_list_feature`, and are the **IDs** that will be used as\n", "indices to an embedding table to retrieve the embedding for that ID. To\n", "give a very simple example, imagine a single sparse feature being Ads\n", "that a user interacted with. The input itself would be a set of Ad IDs\n", "that a user interacted with, and the embeddings retrieved would be a\n", "semantic representation of those Ads. The tricky part of representing\n", "these features in code is that in each input example, **the number of\n", "IDs is variable**. One day a user might have interacted with only one ad\n", "while the next day they interact with three.\n", "\n", "A simple representation is shown below, where we have a `lengths` tensor\n", "denoting how many indices are in an example for a batch and a `values`\n", "tensor containing the indices themselves.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Batch Size 2\n", "# 1 ID in example 1, 2 IDs in example 2\n", "id_list_feature_lengths = torch.tensor([1, 2])\n", "\n", "# Values (IDs) tensor: ID 5 is in example 1, ID 7, 1 is in example 2\n", "id_list_feature_values = torch.tensor([5, 7, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, let\\'s look at the offsets as well as what is contained in each\n", "batch\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Lengths can be converted to offsets for easy indexing of values\n", "id_list_feature_offsets = torch.cumsum(id_list_feature_lengths, dim=0)\n", "\n", "print(\"Offsets: \", id_list_feature_offsets)\n", "print(\"First Batch: \", id_list_feature_values[: id_list_feature_offsets[0]])\n", "print(\n", " \"Second Batch: \",\n", " id_list_feature_values[id_list_feature_offsets[0] : id_list_feature_offsets[1]],\n", ")\n", "\n", "from torchrec import JaggedTensor\n", "\n", "# ``JaggedTensor`` is just a wrapper around lengths/offsets and values tensors!\n", "jt = JaggedTensor(values=id_list_feature_values, lengths=id_list_feature_lengths)\n", "\n", "# Automatically compute offsets from lengths\n", "print(\"Offsets: \", jt.offsets())\n", "\n", "# Convert to list of values\n", "print(\"List of Values: \", jt.to_dense())\n", "\n", "# ``__str__`` representation\n", "print(jt)\n", "\n", "from torchrec import KeyedJaggedTensor\n", "\n", "# ``JaggedTensor`` represents IDs for 1 feature, but we have multiple features in an ``EmbeddingBagCollection``\n", "# That's where ``KeyedJaggedTensor`` comes in! ``KeyedJaggedTensor`` is just multiple ``JaggedTensors`` for multiple id_list_feature_offsets\n", "# From before, we have our two features \"product\" and \"user\". Let's create ``JaggedTensors`` for both!\n", "\n", "product_jt = JaggedTensor(\n", " values=torch.tensor([1, 2, 1, 5]), lengths=torch.tensor([3, 1])\n", ")\n", "user_jt = JaggedTensor(values=torch.tensor([2, 3, 4, 1]), lengths=torch.tensor([2, 2]))\n", "\n", "# Q1: How many batches are there, and which values are in the first batch for ``product_jt`` and ``user_jt``?\n", "kjt = KeyedJaggedTensor.from_jt_dict({\"product\": product_jt, \"user\": user_jt})\n", "\n", "# Look at our feature keys for the ``KeyedJaggedTensor``\n", "print(\"Keys: \", kjt.keys())\n", "\n", "# Look at the overall lengths for the ``KeyedJaggedTensor``\n", "print(\"Lengths: \", kjt.lengths())\n", "\n", "# Look at all values for ``KeyedJaggedTensor``\n", "print(\"Values: \", kjt.values())\n", "\n", "# Can convert ``KeyedJaggedTensor`` to dictionary representation\n", "print(\"to_dict: \", kjt.to_dict())\n", "\n", "# ``KeyedJaggedTensor`` string representation\n", "print(kjt)\n", "\n", "# Q2: What are the offsets for the ``KeyedJaggedTensor``?\n", "\n", "# Now we can run a forward pass on our ``EmbeddingBagCollection`` from before\n", "result = ebc(kjt)\n", "result\n", "\n", "# Result is a ``KeyedTensor``, which contains a list of the feature names and the embedding results\n", "print(result.keys())\n", "\n", "# The results shape is [2, 128], as batch size of 2. Reread previous section if you need a refresher on how the batch size is determined\n", "# 128 for dimension of embedding. If you look at where we initialized the ``EmbeddingBagCollection``, we have two tables \"product\" and \"user\" of dimension 64 each\n", "# meaning embeddings for both features are of size 64. 64 + 64 = 128\n", "print(result.values().shape)\n", "\n", "# Nice to_dict method to determine the embeddings that belong to each feature\n", "result_dict = result.to_dict()\n", "for key, embedding in result_dict.items():\n", " print(key, embedding.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Congrats! You now understand TorchRec modules and data types. Give\n", "yourself a pat on the back for making it this far. Next, we will learn\n", "about distributed training and sharding.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Distributed Training and Sharding\n", "=================================\n", "\n", "Now that we have a grasp on TorchRec modules and data types, it\\'s time\n", "to take it to the next level.\n", "\n", "Remember, the main purpose of TorchRec is to provide primitives for\n", "distributed embeddings. So far, we\\'ve only worked with embedding tables\n", "on a single device. This has been possible given how small the embedding\n", "tables have been, but in a production setting this isn\\'t generally the\n", "case. Embedding tables often get massive, where one table can\\'t fit on\n", "a single GPU, creating the requirement for multiple devices and a\n", "distributed environment.\n", "\n", "In this section, we will explore setting up a distributed environment,\n", "exactly how actual production training is done, and explore sharding\n", "embedding tables, all with TorchRec.\n", "\n", "**This section will also only use 1 GPU, though it will be treated in a\n", "distributed fashion. This is only a limitation for training, as training\n", "has a process per GPU. Inference does not run into this requirement**\n", "\n", "In the example code below, we set up our PyTorch distributed\n", "environment.\n", "\n", "
WARNING:
\n", "
\n", "

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 processgroup once.

\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "\n", "import torch.distributed as dist\n", "\n", "# Set up environment variables for distributed training\n", "# RANK is which GPU we are on, default 0\n", "os.environ[\"RANK\"] = \"0\"\n", "# How many devices in our \"world\", colab notebook can only handle 1 process\n", "os.environ[\"WORLD_SIZE\"] = \"1\"\n", "# Localhost as we are training locally\n", "os.environ[\"MASTER_ADDR\"] = \"localhost\"\n", "# Port for distributed training\n", "os.environ[\"MASTER_PORT\"] = \"29500\"\n", "\n", "# nccl backend is for GPUs, gloo is for CPUs\n", "dist.init_process_group(backend=\"gloo\")\n", "\n", "print(f\"Distributed environment initialized: {dist}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Distributed Embeddings\n", "======================\n", "\n", "We have already worked with the main TorchRec module:\n", "`EmbeddingBagCollection`. We have examined how it works along with how\n", "data is represented in TorchRec. However, we have not yet explored one\n", "of the main parts of TorchRec, which is **distributed embeddings**.\n", "\n", "GPUs are the most popular choice for ML workloads by far today, as they\n", "are able to do magnitudes more floating point operations/s\n", "([FLOPs](https://en.wikipedia.org/wiki/FLOPS)) than CPU. However, GPUs\n", "come with the limitation of scarce fast memory (HBM which is analogous\n", "to RAM for CPU), typically, \\~10s of GBs.\n", "\n", "A RecSys model can contain embedding tables that far exceed the memory\n", "limit for 1 GPU, hence the need for distribution of the embedding tables\n", "across multiple GPUs, otherwise known as **model parallel**. On the\n", "other hand, **data parallel** is where the entire model is replicated on\n", "each GPU, which each GPU taking in a distinct batch of data for\n", "training, syncing gradients on the backwards pass.\n", "\n", "Parts of the model that **require less compute but more memory\n", "(embeddings) are distributed with model parallel** while parts that\n", "**require more compute and less memory (dense layers, MLP, etc.) are\n", "distributed with data parallel**.\n", "\n", "Sharding\n", "========\n", "\n", "In order to distribute an embedding table, we split up the embedding\n", "table into parts and place those parts onto different devices, also\n", "known as \"sharding\".\n", "\n", "There are many ways to shard embedding tables. The most common ways are:\n", "\n", "- Table-Wise: the table is placed entirely onto one device\n", "- Column-Wise: columns of embedding tables are sharded\n", "- Row-Wise: rows of embedding tables are sharded\n", "\n", "Sharded Modules\n", "===============\n", "\n", "While all of this seems like a lot to deal with and implement, you\\'re\n", "in luck. **TorchRec provides all the primitives for easy distributed\n", "training and inference**! In fact, TorchRec modules have two\n", "corresponding classes for working with any TorchRec module in a\n", "distributed environment:\n", "\n", "- **The module sharder**: This class exposes a `shard` API that\n", " handles sharding a TorchRec Module, producing a sharded module.\n", " - For `EmbeddingBagCollection`, the sharder is\n", " [EmbeddingBagCollectionSharder](https://pytorch.org/torchrec/torchrec.distributed.html#torchrec.distributed.embeddingbag.EmbeddingBagCollectionSharder)\n", "- **Sharded module**: This class is a sharded variant of a TorchRec\n", " module. It has the same input/output as a the regular TorchRec\n", " module, but much more optimized and works in a distributed\n", " environment.\n", " - For `EmbeddingBagCollection`, the sharded variant is\n", " [ShardedEmbeddingBagCollection](https://pytorch.org/torchrec/torchrec.distributed.html#torchrec.distributed.embeddingbag.ShardedEmbeddingBagCollection)\n", "\n", "Every TorchRec module has an unsharded and sharded variant.\n", "\n", "- The unsharded version is meant to be prototyped and experimented\n", " with.\n", "- The sharded version is meant to be used in a distributed environment\n", " for distributed training and inference.\n", "\n", "The sharded versions of TorchRec modules, for example\n", "`EmbeddingBagCollection`, will handle everything that is needed for\n", "Model Parallelism, such as communication between GPUs for distributing\n", "embeddings to the correct GPUs.\n", "\n", "Refresher of our `EmbeddingBagCollection` module\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ebc\n", "\n", "from torchrec.distributed.embeddingbag import EmbeddingBagCollectionSharder\n", "from torchrec.distributed.planner import EmbeddingShardingPlanner, Topology\n", "from torchrec.distributed.types import ShardingEnv\n", "\n", "# Corresponding sharder for ``EmbeddingBagCollection`` module\n", "sharder = EmbeddingBagCollectionSharder()\n", "\n", "# ``ProcessGroup`` from torch.distributed initialized 2 cells above\n", "pg = dist.GroupMember.WORLD\n", "assert pg is not None, \"Process group is not initialized\"\n", "\n", "print(f\"Process Group: {pg}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Planner\n", "=======\n", "\n", "Before we can show how sharding works, we must know about the\n", "**planner**, which helps us determine the best sharding configuration.\n", "\n", "Given a number of embedding tables and a number of ranks, there are many\n", "different sharding configurations that are possible. For example, given\n", "2 embedding tables and 2 GPUs, you can:\n", "\n", "- Place 1 table on each GPU\n", "- Place both tables on a single GPU and no tables on the other\n", "- Place certain rows and columns on each GPU\n", "\n", "Given all of these possibilities, we typically want a sharding\n", "configuration that is optimal for performance.\n", "\n", "That is where the planner comes in. The planner is able to determine\n", "given the number of embedding tables and the number of GPUs, what is the\n", "optimal configuration. Turns out, this is incredibly difficult to do\n", "manually, with tons of factors that engineers have to consider to ensure\n", "an optimal sharding plan. Luckily, TorchRec provides an auto planner\n", "when the planner is used.\n", "\n", "The TorchRec planner:\n", "\n", "- Assesses memory constraints of hardware\n", "- Estimates compute based on memory fetches as embedding lookups\n", "- Addresses data specific factors\n", "- Considers other hardware specifics like bandwidth to generate an\n", " optimal sharding plan\n", "\n", "In order to take into consideration all these variables, The TorchRec\n", "planner can take in [various amounts of data for embedding tables,\n", "constraints, hardware information, and\n", "topology](https://github.com/pytorch/torchrec/blob/main/torchrec/distributed/planner/planners.py#L147-L155)\n", "to aid in generating the optimal sharding plan for a model, which is\n", "routinely provided across stacks.\n", "\n", "To learn more about sharding, see our [sharding\n", "tutorial](https://pytorch.org/tutorials/advanced/sharding.html).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# In our case, 1 GPU and compute on CUDA device\n", "planner = EmbeddingShardingPlanner(\n", " topology=Topology(\n", " world_size=1,\n", " compute_device=\"cuda\",\n", " )\n", ")\n", "\n", "# Run planner to get plan for sharding\n", "plan = planner.collective_plan(ebc, [sharder], pg)\n", "\n", "print(f\"Sharding Plan generated: {plan}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Planner Result\n", "==============\n", "\n", "As you can see above, when running the planner there is quite a bit of\n", "output. We can see a lot of stats being calculated along with where our\n", "tables end up being placed.\n", "\n", "The result of running the planner is a static plan, which can be reused\n", "for sharding! This allows sharding to be static for production models\n", "instead of determining a new sharding plan everytime. Below, we use the\n", "sharding plan to finally generate our `ShardedEmbeddingBagCollection`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# The static plan that was generated\n", "plan\n", "\n", "env = ShardingEnv.from_process_group(pg)\n", "\n", "# Shard the ``EmbeddingBagCollection`` module using the ``EmbeddingBagCollectionSharder``\n", "sharded_ebc = sharder.shard(ebc, plan.plan[\"\"], env, torch.device(\"cuda\"))\n", "\n", "print(f\"Sharded EBC Module: {sharded_ebc}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GPU Training with `LazyAwaitable`\n", "=================================\n", "\n", "Remember that TorchRec is a highly optimized library for distributed\n", "embeddings. A concept that TorchRec introduces to enable higher\n", "performance for training on GPU is a\n", "[LazyAwaitable](https://pytorch.org/torchrec/torchrec.distributed.html#torchrec.distributed.types.LazyAwaitable).\n", "You will see `LazyAwaitable` types as outputs of various sharded\n", "TorchRec modules. All a `LazyAwaitable` type does is delay calculating\n", "some result as long as possible, and it does it by acting like an async\n", "type.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from typing import List\n", "\n", "from torchrec.distributed.types import LazyAwaitable\n", "\n", "\n", "# Demonstrate a ``LazyAwaitable`` type:\n", "class ExampleAwaitable(LazyAwaitable[torch.Tensor]):\n", " def __init__(self, size: List[int]) -> None:\n", " super().__init__()\n", " self._size = size\n", "\n", " def _wait_impl(self) -> torch.Tensor:\n", " return torch.ones(self._size)\n", "\n", "\n", "awaitable = ExampleAwaitable([3, 2])\n", "awaitable.wait()\n", "\n", "kjt = kjt.to(\"cuda\")\n", "output = sharded_ebc(kjt)\n", "# The output of our sharded ``EmbeddingBagCollection`` module is an `Awaitable`?\n", "print(output)\n", "\n", "kt = output.wait()\n", "# Now we have our ``KeyedTensor`` after calling ``.wait()``\n", "# If you are confused as to why we have a ``KeyedTensor ``output,\n", "# give yourself a refresher on the unsharded ``EmbeddingBagCollection`` module\n", "print(type(kt))\n", "\n", "print(kt.keys())\n", "\n", "print(kt.values().shape)\n", "\n", "# Same output format as unsharded ``EmbeddingBagCollection``\n", "result_dict = kt.to_dict()\n", "for key, embedding in result_dict.items():\n", " print(key, embedding.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Anatomy of Sharded TorchRec modules\n", "===================================\n", "\n", "We have now successfully sharded an `EmbeddingBagCollection` given a\n", "sharding plan that we generated! The sharded module has common APIs from\n", "TorchRec which abstract away distributed communication/compute amongst\n", "multiple GPUs. In fact, these APIs are highly optimized for performance\n", "in training and inference. **Below are the three common APIs for\n", "distributed training/inference** that are provided by TorchRec:\n", "\n", "- `input_dist`: Handles distributing inputs from GPU to GPU.\n", "- `lookups`: Does the actual embedding lookup in an optimized, batched\n", " manner using FBGEMM TBE (more on this later).\n", "- `output_dist`: Handles distributing outputs from GPU to GPU.\n", "\n", "The distribution of inputs and outputs is done through [NCCL\n", "Collectives](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/overview.html),\n", "namely\n", "[All-to-Alls](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/usage/p2p.html#all-to-all),\n", "which is where all GPUs send and receive data to and from one another.\n", "TorchRec interfaces with PyTorch distributed for collectives and\n", "provides clean abstractions to the end users, removing the concern for\n", "the lower level details.\n", "\n", "The backwards pass does all of these collectives but in the reverse\n", "order for distribution of gradients. `input_dist`, `lookup`, and\n", "`output_dist` all depend on the sharding scheme. Since we sharded in a\n", "table-wise fashion, these APIs are modules that are constructed by\n", "[TwPooledEmbeddingSharding](https://pytorch.org/torchrec/torchrec.distributed.sharding.html#torchrec.distributed.sharding.tw_sharding.TwPooledEmbeddingSharding).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sharded_ebc\n", "\n", "# Distribute input KJTs to all other GPUs and receive KJTs\n", "sharded_ebc._input_dists\n", "\n", "# Distribute output embeddings to all other GPUs and receive embeddings\n", "sharded_ebc._output_dists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optimizing Embedding Lookups\n", "============================\n", "\n", "In performing lookups for a collection of embedding tables, a trivial\n", "solution would be to iterate through all the `nn.EmbeddingBags` and do a\n", "lookup per table. This is exactly what the standard, unsharded\n", "`EmbeddingBagCollection` does. However, while this solution is simple,\n", "it is extremely slow.\n", "\n", "[FBGEMM](https://github.com/pytorch/FBGEMM/tree/main/fbgemm_gpu) is a\n", "library that provides GPU operators (otherwise known as kernels) that\n", "are very optimized. One of these operators is known as **Table Batched\n", "Embedding** (TBE), provides two major optimizations:\n", "\n", "- Table batching, which allows you to look up multiple embeddings with\n", " one kernel call.\n", "- Optimizer Fusion, which allows the module to update itself given the\n", " canonical pytorch optimizers and arguments.\n", "\n", "The `ShardedEmbeddingBagCollection` uses the FBGEMM TBE as the lookup\n", "instead of traditional `nn.EmbeddingBags` for optimized embedding\n", "lookups.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sharded_ebc._lookups" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`DistributedModelParallel`\n", "==========================\n", "\n", "We have now explored sharding a single `EmbeddingBagCollection`! We were\n", "able to take the `EmbeddingBagCollectionSharder` and use the unsharded\n", "`EmbeddingBagCollection` to generate a `ShardedEmbeddingBagCollection`\n", "module. This workflow is fine, but typically when implementing model\n", "parallel,\n", "[DistributedModelParallel](https://pytorch.org/torchrec/torchrec.distributed.html#torchrec.distributed.model_parallel.DistributedModelParallel)\n", "(DMP) is used as the standard interface. When wrapping your model (in\n", "our case `ebc`), with DMP, the following will occur:\n", "\n", "1. Decide how to shard the model. DMP will collect the available\n", " sharders and come up with a plan of the optimal way to shard the\n", " embedding table(s) (for example, `EmbeddingBagCollection`)\n", "2. Actually shard the model. This includes allocating memory for each\n", " embedding table on the appropriate device(s).\n", "\n", "DMP takes in everything that we\\'ve just experimented with, like a\n", "static sharding plan, a list of sharders, etc. However, it also has some\n", "nice defaults to seamlessly shard a TorchRec model. In this toy example,\n", "since we have two embedding tables and one GPU, TorchRec will place both\n", "on the single GPU.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ebc\n", "\n", "model = torchrec.distributed.DistributedModelParallel(ebc, device=torch.device(\"cuda\"))\n", "\n", "out = model(kjt)\n", "out.wait()\n", "\n", "model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sharding Best Practices\n", "=======================\n", "\n", "Currently, our configuration is only sharding on 1 GPU (or rank), which\n", "is trivial: just place all the tables on 1 GPUs memory. However, in real\n", "production use cases, embedding tables are **typically sharded on\n", "hundreds of GPUs**, with different sharding methods such as table-wise,\n", "row-wise, and column-wise. It is incredibly important to determine a\n", "proper sharding configuration (to prevent out of memory issues) while\n", "keeping it balanced not only in terms of memory but also compute for\n", "optimal performance.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding in the Optimizer\n", "=======================\n", "\n", "Remember that TorchRec modules are hyperoptimized for large scale\n", "distributed training. An important optimization is in regards to the\n", "optimizer.\n", "\n", "TorchRec modules provide a seamless API to fuse the backwards pass and\n", "optimize step in training, providing a significant optimization in\n", "performance and decreasing the memory used, alongside granularity in\n", "assigning distinct optimizers to distinct model parameters.\n", "\n", "Optimizer Classes\n", "-----------------\n", "\n", "TorchRec uses `CombinedOptimizer`, which contains a collection of\n", "`KeyedOptimizers`. A `CombinedOptimizer` effectively makes it easy to\n", "handle multiple optimizers for various sub groups in the model. A\n", "`KeyedOptimizer` extends the `torch.optim.Optimizer` and is initialized\n", "through a dictionary of parameters exposes the parameters. Each `TBE`\n", "module in a `EmbeddingBagCollection` will have it\\'s own\n", "`KeyedOptimizer` which combines into one `CombinedOptimizer`.\n", "\n", "Fused optimizer in TorchRec\n", "---------------------------\n", "\n", "Using `DistributedModelParallel`, the **optimizer is fused, which means\n", "that the optimizer update is done in the backward**. This is an\n", "optimization in TorchRec and FBGEMM, where the optimizer embedding\n", "gradients are not materialized and applied directly to the parameters.\n", "This brings significant memory savings as embedding gradients are\n", "typically size of the parameters themselves.\n", "\n", "You can, however, choose to make the optimizer `dense` which does not\n", "apply this optimization and let\\'s you inspect the embedding gradients\n", "or apply computations to it as you wish. A dense optimizer in this case\n", "would be your [canonical PyTorch model training loop with\n", "optimizer.](https://pytorch.org/tutorials/beginner/basics/optimization_tutorial.html)\n", "\n", "Once the optimizer is created through `DistributedModelParallel`, you\n", "still need to manage an optimizer for the other parameters not\n", "associated with TorchRec embedding modules. To find the other\n", "parameters, use\n", "`in_backward_optimizer_filter(model.named_parameters())`. Apply an\n", "optimizer to those parameters as you would a normal Torch optimizer and\n", "combine this and the `model.fused_optimizer` into one\n", "`CombinedOptimizer` that you can use in your training loop to\n", "`zero_grad` and `step` through.\n", "\n", "Adding an Optimizer to `EmbeddingBagCollection`\n", "-----------------------------------------------\n", "\n", "We will do this in two ways, which are equivalent, but give you options\n", "depending on your preferences:\n", "\n", "1. Passing optimizer kwargs through `fused_params` in sharder.\n", "2. Through `apply_optimizer_in_backward`, which converts the optimizer\n", " parameters to `fused_params` to pass to the `TBE` in the\n", " `EmbeddingBagCollection` or `EmbeddingCollection`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Option 1: Passing optimizer kwargs through fused parameters\n", "from torchrec.optim.optimizers import in_backward_optimizer_filter\n", "from fbgemm_gpu.split_embedding_configs import EmbOptimType\n", "\n", "\n", "# We initialize the sharder with\n", "fused_params = {\n", " \"optimizer\": EmbOptimType.EXACT_ROWWISE_ADAGRAD,\n", " \"learning_rate\": 0.02,\n", " \"eps\": 0.002,\n", "}\n", "\n", "# Initialize sharder with ``fused_params``\n", "sharder_with_fused_params = EmbeddingBagCollectionSharder(fused_params=fused_params)\n", "\n", "# We'll use same plan and unsharded EBC as before but this time with our new sharder\n", "sharded_ebc_fused_params = sharder_with_fused_params.shard(ebc, plan.plan[\"\"], env, torch.device(\"cuda\"))\n", "\n", "# Looking at the optimizer of each, we can see that the learning rate changed, which indicates our optimizer has been applied correctly.\n", "# If seen, we can also look at the TBE logs of the cell to see that our new optimizer is indeed being applied\n", "print(f\"Original Sharded EBC fused optimizer: {sharded_ebc.fused_optimizer}\")\n", "print(f\"Sharded EBC with fused parameters fused optimizer: {sharded_ebc_fused_params.fused_optimizer}\")\n", "\n", "print(f\"Type of optimizer: {type(sharded_ebc_fused_params.fused_optimizer)}\")\n", "\n", "from torch.distributed.optim import _apply_optimizer_in_backward as apply_optimizer_in_backward\n", "import copy\n", "# Option 2: Applying optimizer through apply_optimizer_in_backward\n", "# Note: we need to call apply_optimizer_in_backward on unsharded model first and then shard it\n", "\n", "# We can achieve the same result as we did in the previous\n", "ebc_apply_opt = copy.deepcopy(ebc)\n", "optimizer_kwargs = {\"lr\": 0.5}\n", "\n", "for name, param in ebc_apply_opt.named_parameters():\n", " print(f\"{name=}\")\n", " apply_optimizer_in_backward(torch.optim.SGD, [param], optimizer_kwargs)\n", "\n", "sharded_ebc_apply_opt = sharder.shard(ebc_apply_opt, plan.plan[\"\"], env, torch.device(\"cuda\"))\n", "\n", "# 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\n", "print(sharded_ebc_apply_opt.fused_optimizer)\n", "print(type(sharded_ebc_apply_opt.fused_optimizer))\n", "\n", "# We can also check through the filter other parameters that aren't associated with the \"fused\" optimizer(s)\n", "# Practically, just non TorchRec module parameters. Since our module is just a TorchRec EBC\n", "# there are no other parameters that aren't associated with TorchRec\n", "print(\"Non Fused Model Parameters:\")\n", "print(dict(in_backward_optimizer_filter(sharded_ebc_fused_params.named_parameters())).keys())\n", "\n", "# Here we do a dummy backwards call and see that parameter updates for fused\n", "# optimizers happen as a result of the backward pass\n", "\n", "ebc_output = sharded_ebc_fused_params(kjt).wait().values()\n", "loss = torch.sum(torch.ones_like(ebc_output) - ebc_output)\n", "print(f\"First Iteration Loss: {loss}\")\n", "\n", "loss.backward()\n", "\n", "ebc_output = sharded_ebc_fused_params(kjt).wait().values()\n", "loss = torch.sum(torch.ones_like(ebc_output) - ebc_output)\n", "# We don't call an optimizer.step(), so for the loss to have changed here,\n", "# that means that the gradients were somehow updated, which is what the\n", "# fused optimizer automatically handles for us\n", "print(f\"Second Iteration Loss: {loss}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inference\n", "=========\n", "\n", "Now that we are able to train distributed embeddings, how can we take\n", "the trained model and optimize it for inference? Inference is typically\n", "very sensitive to **performance and size of the model**. Running just\n", "the trained model in a Python environment is incredibly inefficient.\n", "There are two key differences between inference and training\n", "environments:\n", "\n", "- **Quantization**: Inference models are typically quantized, where\n", " model parameters lose precision for lower latency in predictions and\n", " reduced model size. For example FP32 (4 bytes) in trained model to\n", " INT8 (1 byte) for each embedding weight. This is also necessary\n", " given the vast scale of embedding tables, as we want to use as few\n", " devices as possible for inference to minimize latency.\n", "- **C++ environment**: Inference latency is very important, so in\n", " order to ensure ample performance, the model is typically ran in a\n", " C++ environment, along with the situations where we don\\'t have a\n", " Python runtime, like on device.\n", "\n", "TorchRec provides primitives for converting a TorchRec model into being\n", "inference ready with:\n", "\n", "- APIs for quantizing the model, introducing optimizations\n", " automatically with FBGEMM TBE\n", "- Sharding embeddings for distributed inference\n", "- Compiling the model to\n", " [TorchScript](https://pytorch.org/docs/stable/jit.html) (compatible\n", " in C++)\n", "\n", "In this section, we will go over this entire workflow of:\n", "\n", "- Quantizing the model\n", "- Sharding the quantized model\n", "- Compiling the sharded quantized model into TorchScript\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ebc\n", "\n", "class InferenceModule(torch.nn.Module):\n", " def __init__(self, ebc: torchrec.EmbeddingBagCollection):\n", " super().__init__()\n", " self.ebc_ = ebc\n", "\n", " def forward(self, kjt: KeyedJaggedTensor):\n", " return self.ebc_(kjt)\n", "\n", "module = InferenceModule(ebc)\n", "for name, param in module.named_parameters():\n", " # Here, the parameters should still be FP32, as we are using a standard EBC\n", " # FP32 is default, regularly used for training\n", " print(name, param.shape, param.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quantization\n", "============\n", "\n", "As you can see above, the normal EBC contains embedding table weights as\n", "FP32 precision (32 bits for each weight). Here, we will use the TorchRec\n", "inference library to quantize the embedding weights of the model to INT8\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from torch import quantization as quant\n", "from torchrec.modules.embedding_configs import QuantConfig\n", "from torchrec.quant.embedding_modules import (\n", " EmbeddingBagCollection as QuantEmbeddingBagCollection,\n", ")\n", "\n", "\n", "quant_dtype = torch.int8\n", "\n", "\n", "qconfig = QuantConfig(\n", " # dtype of the result of the embedding lookup, post activation\n", " # torch.float generally for compatibility with rest of the model\n", " # as rest of the model here usually isn't quantized\n", " activation=quant.PlaceholderObserver.with_args(dtype=torch.float),\n", " # quantized type for embedding weights, aka parameters to actually quantize\n", " weight=quant.PlaceholderObserver.with_args(dtype=quant_dtype),\n", ")\n", "qconfig_spec = {\n", " # Map of module type to qconfig\n", " torchrec.EmbeddingBagCollection: qconfig,\n", "}\n", "mapping = {\n", " # Map of module type to quantized module type\n", " torchrec.EmbeddingBagCollection: QuantEmbeddingBagCollection,\n", "}\n", "\n", "\n", "module = InferenceModule(ebc)\n", "\n", "# Quantize the module\n", "qebc = quant.quantize_dynamic(\n", " module,\n", " qconfig_spec=qconfig_spec,\n", " mapping=mapping,\n", " inplace=False,\n", ")\n", "\n", "\n", "print(f\"Quantized EBC: {qebc}\")\n", "\n", "kjt = kjt.to(\"cpu\")\n", "\n", "qebc(kjt)\n", "\n", "# Once quantized, goes from parameters -> buffers, as no longer trainable\n", "for name, buffer in qebc.named_buffers():\n", " # The shapes of the tables should be the same but the dtype should be int8 now\n", " # post quantization\n", " print(name, buffer.shape, buffer.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shard\n", "=====\n", "\n", "Here we perform sharding of the TorchRec quantized model. This is to\n", "ensure we are using the performant module through FBGEMM TBE. Here we\n", "are using one device to be consistent with training (1 TBE).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from torchrec import distributed as trec_dist\n", "from torchrec.distributed.shard import _shard_modules\n", "\n", "\n", "sharded_qebc = _shard_modules(\n", " module=qebc,\n", " device=torch.device(\"cpu\"),\n", " env=trec_dist.ShardingEnv.from_local(\n", " 1,\n", " 0,\n", " ),\n", ")\n", "\n", "\n", "print(f\"Sharded Quantized EBC: {sharded_qebc}\")\n", "\n", "sharded_qebc(kjt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compilation\n", "===========\n", "\n", "Now we have the optimized eager TorchRec inference model. The next step\n", "is to ensure that this model is loadable in C++, as currently it is only\n", "runnable in a Python runtime.\n", "\n", "The recommended method of compilation at Meta is two fold: [torch.fx\n", "tracing](https://pytorch.org/docs/stable/fx.html) (generate intermediate\n", "representation of model) and converting the result to TorchScript, where\n", "TorchScript is C++ compatible.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from torchrec.fx import Tracer\n", "\n", "\n", "tracer = Tracer(leaf_modules=[\"IntNBitTableBatchedEmbeddingBagsCodegen\"])\n", "\n", "graph = tracer.trace(sharded_qebc)\n", "gm = torch.fx.GraphModule(sharded_qebc, graph)\n", "\n", "print(\"Graph Module Created!\")\n", "\n", "print(gm.code)\n", "\n", "scripted_gm = torch.jit.script(gm)\n", "print(\"Scripted Graph Module Created!\")\n", "\n", "print(scripted_gm.code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conclusion\n", "==========\n", "\n", "In this tutorial, you have gone from training a distributed RecSys model\n", "all the way to making it inference ready. The [TorchRec\n", "repo](https://github.com/pytorch/torchrec/tree/main/torchrec/inference)\n", "has a full example of how to load a TorchRec TorchScript model into C++\n", "for inference.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See Also\n", "========\n", "\n", "For more information, please see our\n", "[dlrm](https://github.com/facebookresearch/dlrm/tree/main/torchrec_dlrm/)\n", "example, which includes multinode training on the Criteo 1TB dataset\n", "using the methods described in [Deep Learning Recommendation Model for\n", "Personalization and Recommendation\n", "Systems](https://arxiv.org/abs/1906.00091).\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 0 }