|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from typing import List, Iterator |
| 6 | + |
| 7 | +import torch |
| 8 | +import torch.distributed as dist |
| 9 | +from torchrec import EmbeddingBagCollection |
| 10 | +from torchrec import KeyedJaggedTensor |
| 11 | +from torchrec.distributed.model_parallel import DistributedModelParallel |
| 12 | +from torchrec.models.dlrm import DLRM |
| 13 | +from torchrec.modules.embedding_configs import EmbeddingBagConfig |
| 14 | +from torchrec.optim.keyed import KeyedOptimizerWrapper |
| 15 | + |
| 16 | + |
| 17 | +class RandomIterator(Iterator): |
| 18 | + def __init__( |
| 19 | + self, batch_size: int, num_dense: int, num_sparse: int, num_embeddings: int |
| 20 | + ) -> None: |
| 21 | + self.batch_size = batch_size |
| 22 | + self.num_dense = num_dense |
| 23 | + self.num_sparse = num_sparse |
| 24 | + self.sparse_keys = [f"feature{id}" for id in range(self.num_sparse)] |
| 25 | + self.num_embeddings = num_embeddings |
| 26 | + self.num_ids_per_feature = 3 |
| 27 | + self.num_ids_to_generate = ( |
| 28 | + self.num_sparse * self.num_ids_per_feature * self.batch_size |
| 29 | + ) |
| 30 | + |
| 31 | + def __next__(self) -> (torch.Tensor, KeyedJaggedTensor, torch.Tensor): |
| 32 | + float_features = torch.randn( |
| 33 | + self.batch_size, |
| 34 | + self.num_dense, |
| 35 | + ) |
| 36 | + labels = torch.randint( |
| 37 | + low=0, |
| 38 | + high=2, |
| 39 | + size=(self.batch_size,), |
| 40 | + ) |
| 41 | + sparse_ids = torch.randint( |
| 42 | + high=self.num_sparse, |
| 43 | + size=(self.num_ids_to_generate,), |
| 44 | + ) |
| 45 | + sparse_features = KeyedJaggedTensor.from_offsets_sync( |
| 46 | + keys=self.sparse_keys, |
| 47 | + values=sparse_ids, |
| 48 | + offsets=torch.tensor( |
| 49 | + list(range(0, self.num_ids_to_generate + 1, self.num_ids_per_feature)), |
| 50 | + dtype=torch.int32, |
| 51 | + ), |
| 52 | + ) |
| 53 | + return (float_features, sparse_features, labels) |
| 54 | + |
| 55 | + |
| 56 | +def main(argv: List[str]) -> None: |
| 57 | + batch_size = 1024 |
| 58 | + num_dense = 1000 |
| 59 | + num_sparse = 20 |
| 60 | + num_embeddings = 1000000 |
| 61 | + |
| 62 | + configs = [ |
| 63 | + EmbeddingBagConfig( |
| 64 | + name=f"table{id}", |
| 65 | + embedding_dim=64, |
| 66 | + num_embeddings=num_embeddings, |
| 67 | + feature_names=[f"feature{id}"], |
| 68 | + ) |
| 69 | + for id in range(num_sparse) |
| 70 | + ] |
| 71 | + |
| 72 | + rank = int(os.environ["LOCAL_RANK"]) |
| 73 | + if torch.cuda.is_available(): |
| 74 | + device = torch.device(f"cuda:{rank}") |
| 75 | + backend = "nccl" |
| 76 | + torch.cuda.set_device(device) |
| 77 | + else: |
| 78 | + raise Exception("Cuda not available") |
| 79 | + |
| 80 | + if not torch.distributed.is_initialized(): |
| 81 | + dist.init_process_group(backend=backend) |
| 82 | + |
| 83 | + model = DLRM( |
| 84 | + embedding_bag_collection=EmbeddingBagCollection( |
| 85 | + tables=configs, device=torch.device("meta") |
| 86 | + ), |
| 87 | + dense_in_features=num_dense, |
| 88 | + dense_arch_layer_sizes=[500, 64], |
| 89 | + over_arch_layer_sizes=[32, 16, 1], |
| 90 | + dense_device=device, |
| 91 | + ) |
| 92 | + model = DistributedModelParallel( |
| 93 | + module=model, |
| 94 | + device=device, |
| 95 | + ) |
| 96 | + optimizer = KeyedOptimizerWrapper( |
| 97 | + dict(model.named_parameters()), |
| 98 | + lambda params: torch.optim.SGD(params, lr=0.01), |
| 99 | + ) |
| 100 | + |
| 101 | + random_iterator = RandomIterator(batch_size, num_dense, num_sparse, num_embeddings) |
| 102 | + loss_fn = torch.nn.BCEWithLogitsLoss() |
| 103 | + for _ in range(10): |
| 104 | + (dense_features, sparse_features, labels) = next(random_iterator) |
| 105 | + dense_features = dense_features.to(device) |
| 106 | + sparse_features = sparse_features.to(device) |
| 107 | + output = model(dense_features, sparse_features) |
| 108 | + labels = labels.to(device) |
| 109 | + loss = loss_fn(output.squeeze(), labels.float()) |
| 110 | + torch.sum(loss, dim=0).backward() |
| 111 | + optimizer.zero_grad() |
| 112 | + optimizer.step() |
| 113 | + |
| 114 | + print( |
| 115 | + "\033[92m" + "Successfully ran a few epochs for DLRM. Installation looks good!" |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main(sys.argv[1:]) |
0 commit comments