|
| 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 | +# pyre-strict |
| 9 | + |
| 10 | +#!/usr/bin/env python3 |
| 11 | +from dataclasses import dataclass |
| 12 | +from typing import cast, Dict, Iterable, List, Optional, Union |
| 13 | + |
| 14 | +import torch |
| 15 | + |
| 16 | +from torch import nn |
| 17 | +from torchrec.distributed.embedding_types import EmbeddingComputeKernel |
| 18 | +from torchrec.distributed.planner import ParameterConstraints |
| 19 | +from torchrec.distributed.types import ShardingType |
| 20 | +from torchrec.modules.embedding_configs import EmbeddingBagConfig, EmbeddingConfig |
| 21 | +from torchrec.modules.embedding_modules import ( |
| 22 | + EmbeddingBagCollection, |
| 23 | + EmbeddingCollection, |
| 24 | +) |
| 25 | +from torchrec.sparse.jagged_tensor import KeyedJaggedTensor |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class EmbeddingTableProps: |
| 30 | + """ |
| 31 | + Properties of an embedding table. |
| 32 | +
|
| 33 | + Args: |
| 34 | + embedding_table_config: Config of the embedding table of Union(EmbeddingConfig or EmbeddingBagConfig) |
| 35 | + sharding (ShardingType): sharding type of the table |
| 36 | + weight_type (WeightedType): weight |
| 37 | + """ |
| 38 | + |
| 39 | + embedding_table_config: Union[EmbeddingConfig, EmbeddingBagConfig] |
| 40 | + sharding: ShardingType |
| 41 | + is_weighted: bool = False |
| 42 | + |
| 43 | + |
| 44 | +class TestECModel(nn.Module): |
| 45 | + """ |
| 46 | + Test model with EmbeddingCollection and Linear layers. |
| 47 | +
|
| 48 | + Args: |
| 49 | + tables (List[EmbeddingConfig]): list of embedding tables |
| 50 | + device (Optional[torch.device]): device on which buffers will be initialized |
| 51 | +
|
| 52 | + Example: |
| 53 | + TestECModel(tables=[EmbeddingConfig(...)]) |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, tables: List[EmbeddingConfig], device: Optional[torch.device] = None |
| 58 | + ) -> None: |
| 59 | + super().__init__() |
| 60 | + self.ec: EmbeddingCollection = EmbeddingCollection( |
| 61 | + tables=tables, |
| 62 | + device=device if device else torch.device("meta"), |
| 63 | + ) |
| 64 | + |
| 65 | + embedding_dim = tables[0].embedding_dim |
| 66 | + |
| 67 | + self.seq: nn.Sequential = nn.Sequential( |
| 68 | + *[nn.Linear(embedding_dim, embedding_dim) for _ in range(3)] |
| 69 | + ) |
| 70 | + |
| 71 | + def forward(self, features: KeyedJaggedTensor) -> torch.Tensor: |
| 72 | + """ |
| 73 | + Forward pass of the TestECModel. |
| 74 | +
|
| 75 | + Args: |
| 76 | + features (KeyedJaggedTensor): Input features for the model. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + torch.Tensor: Output tensor after processing through the model. |
| 80 | + """ |
| 81 | + |
| 82 | + lookup_result = self.ec(features) |
| 83 | + return self.seq(torch.cat([jt.values() for _, jt in lookup_result.items()])) |
| 84 | + |
| 85 | + |
| 86 | +class TestEBCModel(nn.Module): |
| 87 | + """ |
| 88 | + Test model with EmbeddingBagCollection and Linear layers. |
| 89 | +
|
| 90 | + Args: |
| 91 | + tables (List[EmbeddingBagConfig]): list of embedding tables |
| 92 | + device (Optional[torch.device]): device on which buffers will be initialized |
| 93 | +
|
| 94 | + Example: |
| 95 | + TestEBCModel(tables=[EmbeddingBagConfig(...)]) |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__( |
| 99 | + self, tables: List[EmbeddingBagConfig], device: Optional[torch.device] = None |
| 100 | + ) -> None: |
| 101 | + super().__init__() |
| 102 | + self.ebc: EmbeddingBagCollection |
| 103 | + self.ebc = EmbeddingBagCollection( |
| 104 | + tables=tables, |
| 105 | + device=device if device else torch.device("meta"), |
| 106 | + ) |
| 107 | + |
| 108 | + embedding_dim = tables[0].embedding_dim |
| 109 | + self.seq: nn.Sequential = nn.Sequential( |
| 110 | + *[nn.Linear(embedding_dim, embedding_dim) for _ in range(3)] |
| 111 | + ) |
| 112 | + |
| 113 | + def forward(self, features: KeyedJaggedTensor) -> torch.Tensor: |
| 114 | + """ |
| 115 | + Forward pass of the TestEBCModel. |
| 116 | +
|
| 117 | + Args: |
| 118 | + features (KeyedJaggedTensor): Input features for the model. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + torch.Tensor: Output tensor after processing through the model. |
| 122 | + """ |
| 123 | + |
| 124 | + lookup_result = self.ebc(features).to_dict() |
| 125 | + return self.seq(torch.cat(tuple(lookup_result.values()))) |
| 126 | + |
| 127 | + |
| 128 | +def create_ec_model( |
| 129 | + tables: Iterable[EmbeddingTableProps], |
| 130 | + device: Optional[torch.device] = None, |
| 131 | +) -> nn.Module: |
| 132 | + """ |
| 133 | + Create an EmbeddingCollection model with the given tables. |
| 134 | +
|
| 135 | + Args: |
| 136 | + tables (List[EmbeddingTableProps]): list of embedding tables |
| 137 | + device (Optional[torch.device]): device on which buffers will be initialized |
| 138 | +
|
| 139 | + Returns: |
| 140 | + nn.Module: EmbeddingCollection model |
| 141 | + """ |
| 142 | + return TestECModel( |
| 143 | + tables=[ |
| 144 | + cast(EmbeddingConfig, table.embedding_table_config) for table in tables |
| 145 | + ], |
| 146 | + device=device, |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +def create_ebc_model( |
| 151 | + tables: Iterable[EmbeddingTableProps], |
| 152 | + device: Optional[torch.device] = None, |
| 153 | +) -> nn.Module: |
| 154 | + """ |
| 155 | + Create an EmbeddinBagCollection model with the given tables. |
| 156 | +
|
| 157 | + Args: |
| 158 | + tables (List[EmbeddingTableProps]): list of embedding tables |
| 159 | + device (Optional[torch.device]): device on which buffers will be initialized |
| 160 | +
|
| 161 | + Returns: |
| 162 | + nn.Module: EmbeddingCollection model |
| 163 | + """ |
| 164 | + return TestEBCModel( |
| 165 | + tables=[ |
| 166 | + cast(EmbeddingBagConfig, table.embedding_table_config) for table in tables |
| 167 | + ], |
| 168 | + device=device, |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +def generate_planner_constraints( |
| 173 | + tables: Iterable[EmbeddingTableProps], |
| 174 | +) -> dict[str, ParameterConstraints]: |
| 175 | + """ |
| 176 | + Generate planner constraints for the given tables. |
| 177 | +
|
| 178 | + Args: |
| 179 | + tables (List[EmbeddingTableProps]): list of embedding tables |
| 180 | +
|
| 181 | + Returns: |
| 182 | + Dict[str, ParameterConstraints]: planner constraints |
| 183 | + """ |
| 184 | + constraints: Dict[str, ParameterConstraints] = {} |
| 185 | + for table in tables: |
| 186 | + sharding_types = [table.sharding.value] |
| 187 | + constraints[table.embedding_table_config.name] = ParameterConstraints( |
| 188 | + sharding_types=sharding_types, |
| 189 | + compute_kernels=[EmbeddingComputeKernel.FUSED.value], |
| 190 | + feature_names=table.embedding_table_config.feature_names, |
| 191 | + pooling_factors=[1.0], |
| 192 | + ) |
| 193 | + return constraints |
0 commit comments