|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +import argparse |
| 9 | +import sys |
| 10 | +from typing import List, Tuple |
| 11 | + |
| 12 | +import torch |
| 13 | +from fbgemm_gpu.split_table_batched_embeddings_ops import EmbeddingLocation |
| 14 | +from torchrec.github.benchmarks import ebc_benchmarks_utils |
| 15 | +from torchrec.modules.embedding_configs import EmbeddingBagConfig |
| 16 | +from torchrec.modules.embedding_modules import EmbeddingBagCollection |
| 17 | +from torchrec.modules.fused_embedding_modules import FusedEmbeddingBagCollection |
| 18 | + |
| 19 | +# Reference: https://github.com/facebookresearch/dlrm/blob/main/torchrec_dlrm/README.MD |
| 20 | +DLRM_NUM_EMBEDDINGS_PER_FEATURE = [ |
| 21 | + 45833188, |
| 22 | + 36746, |
| 23 | + 17245, |
| 24 | + 7413, |
| 25 | + 20243, |
| 26 | + 3, |
| 27 | + 7114, |
| 28 | + 1441, |
| 29 | + 62, |
| 30 | + 29275261, |
| 31 | + 1572176, |
| 32 | + 345138, |
| 33 | + 10, |
| 34 | + 2209, |
| 35 | + 11267, |
| 36 | + 128, |
| 37 | + 4, |
| 38 | + 974, |
| 39 | + 14, |
| 40 | + 48937457, |
| 41 | + 11316796, |
| 42 | + 40094537, |
| 43 | + 452104, |
| 44 | + 12606, |
| 45 | + 104, |
| 46 | + 35, |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +def get_shrunk_dlrm_num_embeddings(reduction_degree: int) -> List[int]: |
| 51 | + return [ |
| 52 | + num_emb if num_emb < 10000000 else int(num_emb / reduction_degree) |
| 53 | + for num_emb in DLRM_NUM_EMBEDDINGS_PER_FEATURE |
| 54 | + ] |
| 55 | + |
| 56 | + |
| 57 | +def main(argv: List[str]) -> None: |
| 58 | + args = parse_args(argv) |
| 59 | + |
| 60 | + if not args.cpu_only and torch.cuda.is_available(): |
| 61 | + device = torch.device("cuda") |
| 62 | + else: |
| 63 | + device = torch.device("cpu") |
| 64 | + |
| 65 | + if args.mode == "ebc_comparison_dlrm": |
| 66 | + print("Running EBC vs. FusedEBC on DLRM EMB") |
| 67 | + |
| 68 | + for reduction_degree in [128, 64, 32]: |
| 69 | + embedding_bag_configs: List[EmbeddingBagConfig] = [ |
| 70 | + EmbeddingBagConfig( |
| 71 | + name=f"ebc_{idx}", |
| 72 | + embedding_dim=128, |
| 73 | + num_embeddings=num_embeddings, |
| 74 | + feature_names=[f"ebc_{idx}_feat_1"], |
| 75 | + ) |
| 76 | + for idx, num_embeddings in enumerate( |
| 77 | + get_shrunk_dlrm_num_embeddings(reduction_degree) |
| 78 | + ) |
| 79 | + ] |
| 80 | + ( |
| 81 | + ebc_time_avg, |
| 82 | + ebc_time_std, |
| 83 | + fused_ebc_time_avg, |
| 84 | + fused_ebc_time_std, |
| 85 | + speedup, |
| 86 | + ) = get_ebc_comparison(embedding_bag_configs, device) |
| 87 | + |
| 88 | + print(f"when DLRM EMB is reduced by {reduction_degree} times:") |
| 89 | + print(f"ebc_time = {ebc_time_avg} +/- {ebc_time_std} sec") |
| 90 | + print(f"fused_ebc_time = {fused_ebc_time_avg} +/- {fused_ebc_time_std} sec") |
| 91 | + print(f"speedup = {speedup}") |
| 92 | + |
| 93 | + elif args.mode == "fused_ebc_uvm": |
| 94 | + print("Running DLRM EMB on FusedEBC with UVM/UVM-caching") |
| 95 | + embedding_bag_configs: List[EmbeddingBagConfig] = [ |
| 96 | + EmbeddingBagConfig( |
| 97 | + name=f"ebc_{idx}", |
| 98 | + embedding_dim=128, |
| 99 | + num_embeddings=num_embeddings, |
| 100 | + feature_names=[f"ebc_{idx}_feat_1"], |
| 101 | + ) |
| 102 | + for idx, num_embeddings in enumerate(get_shrunk_dlrm_num_embeddings(2)) |
| 103 | + ] |
| 104 | + fused_ebc_time_avg, fused_ebc_time_std = get_fused_ebc_uvm_time( |
| 105 | + embedding_bag_configs, device, EmbeddingLocation.MANAGED_CACHING |
| 106 | + ) |
| 107 | + print( |
| 108 | + f"FusedEBC with UVM caching on DLRM: {fused_ebc_time_avg} +/- {fused_ebc_time_std} sec" |
| 109 | + ) |
| 110 | + |
| 111 | + embedding_bag_configs: List[EmbeddingBagConfig] = [ |
| 112 | + EmbeddingBagConfig( |
| 113 | + name=f"ebc_{idx}", |
| 114 | + embedding_dim=128, |
| 115 | + num_embeddings=num_embeddings, |
| 116 | + feature_names=[f"ebc_{idx}_feat_1"], |
| 117 | + ) |
| 118 | + for idx, num_embeddings in enumerate(DLRM_NUM_EMBEDDINGS_PER_FEATURE) |
| 119 | + ] |
| 120 | + fused_ebc_time_avg, fused_ebc_time_std = get_fused_ebc_uvm_time( |
| 121 | + embedding_bag_configs, device, EmbeddingLocation.MANAGED |
| 122 | + ) |
| 123 | + print( |
| 124 | + f"FusedEBC with UVM management on DLRM: {fused_ebc_time_avg} plus/minus {fused_ebc_time_std} sec" |
| 125 | + ) |
| 126 | + |
| 127 | + elif args.mode == "ebc_comparison_scaling": |
| 128 | + print("Running EBC vs. FusedEBC scaling experiment") |
| 129 | + |
| 130 | + num_tables_list = [10, 100, 1000] |
| 131 | + embedding_dim_list = [4, 8, 16, 32, 64, 128] |
| 132 | + num_embeddings_list = [4, 8, 16, 32, 64, 128, 256, 1024, 2048, 4096, 8192] |
| 133 | + |
| 134 | + for num_tables in num_tables_list: |
| 135 | + for num_embeddings in num_embeddings_list: |
| 136 | + for embedding_dim in embedding_dim_list: |
| 137 | + embedding_bag_configs: List[EmbeddingBagConfig] = [ |
| 138 | + EmbeddingBagConfig( |
| 139 | + name=f"ebc_{idx}", |
| 140 | + embedding_dim=embedding_dim, |
| 141 | + num_embeddings=num_embeddings, |
| 142 | + feature_names=[f"ebc_{idx}_feat_1"], |
| 143 | + ) |
| 144 | + for idx in range(num_tables) |
| 145 | + ] |
| 146 | + ebc_time, _, fused_ebc_time, _, speedup = get_ebc_comparison( |
| 147 | + embedding_bag_configs, device, epochs=3 |
| 148 | + ) |
| 149 | + print( |
| 150 | + f"EBC num_tables = {num_tables}, num_embeddings = {num_embeddings}, embedding_dim = {embedding_dim}:" |
| 151 | + ) |
| 152 | + print( |
| 153 | + f"ebc_time = {ebc_time} sec, fused_ebc_time = {fused_ebc_time} sec, speedup = {speedup}" |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def get_fused_ebc_uvm_time( |
| 158 | + embedding_bag_configs: List[EmbeddingBagConfig], |
| 159 | + device: torch.device, |
| 160 | + location: EmbeddingLocation, |
| 161 | + epochs: int = 100, |
| 162 | +) -> Tuple[float, float]: |
| 163 | + |
| 164 | + fused_ebc = FusedEmbeddingBagCollection( |
| 165 | + tables=embedding_bag_configs, |
| 166 | + optimizer_type=torch.optim.SGD, |
| 167 | + optimizer_kwargs={"lr": 0.02}, |
| 168 | + device=device, |
| 169 | + location=location, |
| 170 | + ) |
| 171 | + |
| 172 | + dataset = ebc_benchmarks_utils.get_random_dataset( |
| 173 | + batch_size=64, |
| 174 | + num_batches=10, |
| 175 | + num_dense_features=1024, |
| 176 | + embedding_bag_configs=embedding_bag_configs, |
| 177 | + ) |
| 178 | + |
| 179 | + fused_ebc_time_avg, fused_ebc_time_std = ebc_benchmarks_utils.train( |
| 180 | + model=fused_ebc, |
| 181 | + optimizer=None, |
| 182 | + dataset=dataset, |
| 183 | + device=device, |
| 184 | + epochs=epochs, |
| 185 | + ) |
| 186 | + |
| 187 | + return fused_ebc_time_avg, fused_ebc_time_std |
| 188 | + |
| 189 | + |
| 190 | +def get_ebc_comparison( |
| 191 | + embedding_bag_configs: List[EmbeddingBagConfig], |
| 192 | + device: torch.device, |
| 193 | + epochs: int = 100, |
| 194 | +) -> Tuple[float, float, float, float, float]: |
| 195 | + |
| 196 | + # Simple EBC module wrapping a list of nn.EmbeddingBag |
| 197 | + ebc = EmbeddingBagCollection( |
| 198 | + tables=embedding_bag_configs, |
| 199 | + device=device, |
| 200 | + ) |
| 201 | + optimizer = torch.optim.SGD(ebc.parameters(), lr=0.02) |
| 202 | + |
| 203 | + # EBC with fused optimizer backed by fbgemm SplitTableBatchedEmbeddingBagsCodegen |
| 204 | + fused_ebc = FusedEmbeddingBagCollection( |
| 205 | + tables=embedding_bag_configs, |
| 206 | + optimizer_type=torch.optim.SGD, |
| 207 | + optimizer_kwargs={"lr": 0.02}, |
| 208 | + device=device, |
| 209 | + ) |
| 210 | + |
| 211 | + dataset = ebc_benchmarks_utils.get_random_dataset( |
| 212 | + batch_size=64, |
| 213 | + num_batches=10, |
| 214 | + num_dense_features=1024, |
| 215 | + embedding_bag_configs=embedding_bag_configs, |
| 216 | + ) |
| 217 | + |
| 218 | + ebc_time_avg, ebc_time_std = ebc_benchmarks_utils.train( |
| 219 | + model=ebc, |
| 220 | + optimizer=optimizer, |
| 221 | + dataset=dataset, |
| 222 | + device=device, |
| 223 | + epochs=epochs, |
| 224 | + ) |
| 225 | + fused_ebc_time_avg, fused_ebc_time_std = ebc_benchmarks_utils.train( |
| 226 | + model=fused_ebc, |
| 227 | + optimizer=None, |
| 228 | + dataset=dataset, |
| 229 | + device=device, |
| 230 | + epochs=epochs, |
| 231 | + ) |
| 232 | + speedup = ebc_time_avg / fused_ebc_time_avg |
| 233 | + |
| 234 | + return ebc_time_avg, ebc_time_std, fused_ebc_time_avg, fused_ebc_time_std, speedup |
| 235 | + |
| 236 | + |
| 237 | +def parse_args(argv: List[str]) -> argparse.Namespace: |
| 238 | + parser = argparse.ArgumentParser(description="TorchRec ebc benchmarks") |
| 239 | + parser.add_argument( |
| 240 | + "--cpu_only", |
| 241 | + action="store_true", |
| 242 | + default=False, |
| 243 | + help="specify whether to use cpu", |
| 244 | + ) |
| 245 | + parser.add_argument( |
| 246 | + "--mode", |
| 247 | + type=str, |
| 248 | + default="ebc_comparison_dlrm", |
| 249 | + help="specify 'ebc_comparison_dlrm', 'ebc_comparison_scaling' or 'fused_ebc_uvm'", |
| 250 | + ) |
| 251 | + return parser.parse_args(argv) |
| 252 | + |
| 253 | + |
| 254 | +if __name__ == "__main__": |
| 255 | + main(sys.argv[1:]) |
0 commit comments