torchrec 0.2.0.dev0__37-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- examples/__init__.py +0 -0
- examples/datasets/__init__.py +8 -0
- examples/datasets/criteo_dataframes.py +101 -0
- examples/ray/__init__.py +0 -0
- examples/ray/compute_world_size.py +51 -0
- examples/ray/train_torchrec.py +133 -0
- examples/retrieval/__init__.py +6 -0
- examples/retrieval/data/__init__.py +6 -0
- examples/retrieval/data/dataloader.py +44 -0
- examples/retrieval/knn_index.py +67 -0
- examples/retrieval/modules/__init__.py +6 -0
- examples/retrieval/modules/two_tower.py +229 -0
- examples/retrieval/two_tower_retrieval.py +192 -0
- examples/retrieval/two_tower_train.py +253 -0
- examples/torcharrow/__init__.py +8 -0
- examples/torcharrow/dataloader.py +116 -0
- examples/torcharrow/run.py +126 -0
- fbgemm_gpu/__init__.py +16 -0
- fbgemm_gpu/batched_unary_embeddings_ops.py +82 -0
- fbgemm_gpu/enums.py +21 -0
- fbgemm_gpu/fbgemm_gpu_py.so +0 -0
- fbgemm_gpu/permute_pooled_embedding_modules.py +73 -0
- fbgemm_gpu/permute_pooled_embedding_modules_split.py +76 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/__init__.py +19 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_adagrad.py +120 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_adam.py +149 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad.py +128 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad_with_counter.py +134 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad_with_weight_decay.py +128 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_sgd.py +105 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_args.py +54 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_lamb.py +149 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_lars_sgd.py +128 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_partial_rowwise_adam.py +149 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_partial_rowwise_lamb.py +149 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad.py +128 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad_with_counter.py +134 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad_with_weight_decay.py +128 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_weighted_adagrad.py +130 -0
- fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_sgd.py +105 -0
- fbgemm_gpu/split_embedding_configs.py +182 -0
- fbgemm_gpu/split_embedding_inference_converter.py +208 -0
- fbgemm_gpu/split_table_batched_embeddings_ops.py +2503 -0
- fbgemm_gpu/uvm.py +35 -0
- torchrec/__init__.py +27 -0
- torchrec/datasets/__init__.py +31 -0
- torchrec/datasets/criteo.py +831 -0
- torchrec/datasets/movielens.py +140 -0
- torchrec/datasets/random.py +188 -0
- torchrec/datasets/scripts/__init__.py +0 -0
- torchrec/datasets/scripts/contiguous_preproc_criteo.py +75 -0
- torchrec/datasets/scripts/npy_preproc_criteo.py +73 -0
- torchrec/datasets/test_utils/__init__.py +0 -0
- torchrec/datasets/test_utils/criteo_test_utils.py +139 -0
- torchrec/datasets/utils.py +341 -0
- torchrec/distributed/__init__.py +55 -0
- torchrec/distributed/batched_embedding_kernel.py +697 -0
- torchrec/distributed/collective_utils.py +71 -0
- torchrec/distributed/comm.py +149 -0
- torchrec/distributed/comm_ops.py +930 -0
- torchrec/distributed/dist_data.py +949 -0
- torchrec/distributed/embedding.py +638 -0
- torchrec/distributed/embedding_kernel.py +326 -0
- torchrec/distributed/embedding_lookup.py +760 -0
- torchrec/distributed/embedding_sharding.py +660 -0
- torchrec/distributed/embedding_tower_sharding.py +992 -0
- torchrec/distributed/embedding_types.py +369 -0
- torchrec/distributed/embeddingbag.py +731 -0
- torchrec/distributed/fused_embeddingbag.py +148 -0
- torchrec/distributed/grouped_position_weighted.py +103 -0
- torchrec/distributed/model_parallel.py +555 -0
- torchrec/distributed/planner/__init__.py +24 -0
- torchrec/distributed/planner/constants.py +77 -0
- torchrec/distributed/planner/enumerators.py +353 -0
- torchrec/distributed/planner/parallelized_planners.py +307 -0
- torchrec/distributed/planner/partitioners.py +280 -0
- torchrec/distributed/planner/perf_models.py +24 -0
- torchrec/distributed/planner/planners.py +290 -0
- torchrec/distributed/planner/proposers.py +270 -0
- torchrec/distributed/planner/shard_estimators.py +971 -0
- torchrec/distributed/planner/stats.py +477 -0
- torchrec/distributed/planner/storage_reservations.py +168 -0
- torchrec/distributed/planner/types.py +423 -0
- torchrec/distributed/planner/utils.py +30 -0
- torchrec/distributed/quant_embedding.py +307 -0
- torchrec/distributed/quant_embedding_kernel.py +295 -0
- torchrec/distributed/quant_embeddingbag.py +288 -0
- torchrec/distributed/sharding/__init__.py +0 -0
- torchrec/distributed/sharding/cw_sequence_sharding.py +71 -0
- torchrec/distributed/sharding/cw_sharding.py +242 -0
- torchrec/distributed/sharding/dp_sequence_sharding.py +87 -0
- torchrec/distributed/sharding/dp_sharding.py +217 -0
- torchrec/distributed/sharding/rw_sequence_sharding.py +130 -0
- torchrec/distributed/sharding/rw_sharding.py +393 -0
- torchrec/distributed/sharding/sequence_sharding.py +69 -0
- torchrec/distributed/sharding/tw_sequence_sharding.py +211 -0
- torchrec/distributed/sharding/tw_sharding.py +469 -0
- torchrec/distributed/sharding/twcw_sharding.py +32 -0
- torchrec/distributed/sharding/twrw_sharding.py +543 -0
- torchrec/distributed/sharding/vb_cw_sharding.py +91 -0
- torchrec/distributed/sharding/vb_rw_sharding.py +250 -0
- torchrec/distributed/sharding/vb_sharding.py +54 -0
- torchrec/distributed/sharding/vb_tw_sharding.py +148 -0
- torchrec/distributed/sharding/vb_twrw_sharding.py +388 -0
- torchrec/distributed/test_utils/__init__.py +0 -0
- torchrec/distributed/test_utils/multi_process.py +132 -0
- torchrec/distributed/test_utils/test_model.py +818 -0
- torchrec/distributed/test_utils/test_model_parallel.py +74 -0
- torchrec/distributed/test_utils/test_model_parallel_base.py +117 -0
- torchrec/distributed/test_utils/test_sharding.py +280 -0
- torchrec/distributed/train_pipeline.py +531 -0
- torchrec/distributed/types.py +594 -0
- torchrec/distributed/utils.py +186 -0
- torchrec/fused_planner.py +57 -0
- torchrec/fx/__init__.py +15 -0
- torchrec/fx/tracer.py +78 -0
- torchrec/inference/__init__.py +25 -0
- torchrec/inference/client.py +127 -0
- torchrec/inference/model_packager.py +102 -0
- torchrec/inference/modules.py +155 -0
- torchrec/inference/state_dict_transform.py +103 -0
- torchrec/metrics/__init__.py +0 -0
- torchrec/metrics/auc.py +159 -0
- torchrec/metrics/calibration.py +108 -0
- torchrec/metrics/ctr.py +104 -0
- torchrec/metrics/metric_module.py +358 -0
- torchrec/metrics/metrics_config.py +152 -0
- torchrec/metrics/metrics_namespace.py +114 -0
- torchrec/metrics/model_utils.py +85 -0
- torchrec/metrics/mse.py +134 -0
- torchrec/metrics/ne.py +164 -0
- torchrec/metrics/rec_metric.py +686 -0
- torchrec/metrics/throughput.py +174 -0
- torchrec/models/__init__.py +27 -0
- torchrec/models/deepfm.py +346 -0
- torchrec/models/dlrm.py +708 -0
- torchrec/modules/__init__.py +34 -0
- torchrec/modules/activation.py +55 -0
- torchrec/modules/crossnet.py +446 -0
- torchrec/modules/deepfm.py +213 -0
- torchrec/modules/embedding_configs.py +148 -0
- torchrec/modules/embedding_modules.py +381 -0
- torchrec/modules/embedding_tower.py +150 -0
- torchrec/modules/feature_processor.py +74 -0
- torchrec/modules/fused_embedding_modules.py +625 -0
- torchrec/modules/lazy_extension.py +258 -0
- torchrec/modules/mlp.py +172 -0
- torchrec/modules/utils.py +117 -0
- torchrec/optim/__init__.py +35 -0
- torchrec/optim/clipping.py +53 -0
- torchrec/optim/fused.py +42 -0
- torchrec/optim/keyed.py +399 -0
- torchrec/optim/rowwise_adagrad.py +222 -0
- torchrec/optim/test_utils/__init__.py +19 -0
- torchrec/optim/warmup.py +147 -0
- torchrec/quant/__init__.py +34 -0
- torchrec/quant/embedding_modules.py +545 -0
- torchrec/sparse/__init__.py +33 -0
- torchrec/sparse/jagged_tensor.py +1281 -0
- torchrec/sparse/test_utils/__init__.py +45 -0
- torchrec/streamable.py +46 -0
- torchrec/test_utils/__init__.py +111 -0
- torchrec-0.2.dev0.dist-info/LICENSE +29 -0
- torchrec-0.2.dev0.dist-info/METADATA +175 -0
- torchrec-0.2.dev0.dist-info/RECORD +167 -0
- torchrec-0.2.dev0.dist-info/WHEEL +5 -0
- torchrec-0.2.dev0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
from typing import Any, cast, Dict, List, Optional, TypeVar
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
from torchrec.distributed.embedding_lookup import GroupedPooledEmbeddingsLookup
|
|
12
|
+
from torchrec.distributed.embedding_sharding import (
|
|
13
|
+
BaseEmbeddingDist,
|
|
14
|
+
BaseEmbeddingLookup,
|
|
15
|
+
BaseSparseFeaturesDist,
|
|
16
|
+
EmbeddingSharding,
|
|
17
|
+
EmbeddingShardingInfo,
|
|
18
|
+
group_tables,
|
|
19
|
+
)
|
|
20
|
+
from torchrec.distributed.embedding_types import (
|
|
21
|
+
BaseGroupedFeatureProcessor,
|
|
22
|
+
EmbeddingComputeKernel,
|
|
23
|
+
GroupedEmbeddingConfig,
|
|
24
|
+
ShardedEmbeddingTable,
|
|
25
|
+
SparseFeatures,
|
|
26
|
+
)
|
|
27
|
+
from torchrec.distributed.types import Awaitable, NoWait, ShardingEnv, ShardMetadata
|
|
28
|
+
from torchrec.streamable import Multistreamable
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
F = TypeVar("F", bound=Multistreamable)
|
|
32
|
+
T = TypeVar("T")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class BaseDpEmbeddingSharding(EmbeddingSharding[F, T]):
|
|
36
|
+
"""
|
|
37
|
+
Base class for data-parallel sharding.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
43
|
+
env: ShardingEnv,
|
|
44
|
+
device: Optional[torch.device] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
super().__init__()
|
|
47
|
+
self._env = env
|
|
48
|
+
self._device = device
|
|
49
|
+
self._rank: int = self._env.rank
|
|
50
|
+
self._world_size: int = self._env.world_size
|
|
51
|
+
sharded_tables_per_rank = self._shard(sharding_infos)
|
|
52
|
+
self._grouped_embedding_configs_per_rank: List[
|
|
53
|
+
List[GroupedEmbeddingConfig]
|
|
54
|
+
] = []
|
|
55
|
+
self._score_grouped_embedding_configs_per_rank: List[
|
|
56
|
+
List[GroupedEmbeddingConfig]
|
|
57
|
+
] = []
|
|
58
|
+
(
|
|
59
|
+
self._grouped_embedding_configs_per_rank,
|
|
60
|
+
self._score_grouped_embedding_configs_per_rank,
|
|
61
|
+
) = group_tables(sharded_tables_per_rank)
|
|
62
|
+
self._grouped_embedding_configs: List[
|
|
63
|
+
GroupedEmbeddingConfig
|
|
64
|
+
] = self._grouped_embedding_configs_per_rank[env.rank]
|
|
65
|
+
self._score_grouped_embedding_configs: List[
|
|
66
|
+
GroupedEmbeddingConfig
|
|
67
|
+
] = self._score_grouped_embedding_configs_per_rank[env.rank]
|
|
68
|
+
|
|
69
|
+
def _shard(
|
|
70
|
+
self,
|
|
71
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
72
|
+
) -> List[List[ShardedEmbeddingTable]]:
|
|
73
|
+
world_size = self._world_size
|
|
74
|
+
tables_per_rank: List[List[ShardedEmbeddingTable]] = [
|
|
75
|
+
[] for i in range(world_size)
|
|
76
|
+
]
|
|
77
|
+
for info in sharding_infos:
|
|
78
|
+
for rank in range(world_size):
|
|
79
|
+
tables_per_rank[rank].append(
|
|
80
|
+
ShardedEmbeddingTable(
|
|
81
|
+
num_embeddings=info.embedding_config.num_embeddings,
|
|
82
|
+
embedding_dim=info.embedding_config.embedding_dim,
|
|
83
|
+
name=info.embedding_config.name,
|
|
84
|
+
embedding_names=info.embedding_config.embedding_names,
|
|
85
|
+
data_type=info.embedding_config.data_type,
|
|
86
|
+
feature_names=info.embedding_config.feature_names,
|
|
87
|
+
pooling=info.embedding_config.pooling,
|
|
88
|
+
is_weighted=info.embedding_config.is_weighted,
|
|
89
|
+
has_feature_processor=info.embedding_config.has_feature_processor,
|
|
90
|
+
local_rows=info.param.size(0),
|
|
91
|
+
local_cols=info.param.size(1),
|
|
92
|
+
compute_kernel=EmbeddingComputeKernel(
|
|
93
|
+
info.param_sharding.compute_kernel
|
|
94
|
+
),
|
|
95
|
+
local_metadata=None,
|
|
96
|
+
global_metadata=None,
|
|
97
|
+
weight_init_max=info.embedding_config.weight_init_max,
|
|
98
|
+
weight_init_min=info.embedding_config.weight_init_min,
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
return tables_per_rank
|
|
102
|
+
|
|
103
|
+
def embedding_dims(self) -> List[int]:
|
|
104
|
+
embedding_dims = []
|
|
105
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
106
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
107
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
108
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
109
|
+
return embedding_dims
|
|
110
|
+
|
|
111
|
+
def embedding_names(self) -> List[str]:
|
|
112
|
+
embedding_names = []
|
|
113
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
114
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
115
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
116
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
117
|
+
return embedding_names
|
|
118
|
+
|
|
119
|
+
def embedding_shard_metadata(self) -> List[Optional[ShardMetadata]]:
|
|
120
|
+
embedding_shard_metadata = []
|
|
121
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
122
|
+
embedding_shard_metadata.extend(grouped_config.embedding_shard_metadata())
|
|
123
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
124
|
+
embedding_shard_metadata.extend(grouped_config.embedding_shard_metadata())
|
|
125
|
+
return embedding_shard_metadata
|
|
126
|
+
|
|
127
|
+
def id_list_feature_names(self) -> List[str]:
|
|
128
|
+
id_list_feature_names = []
|
|
129
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
130
|
+
id_list_feature_names.extend(grouped_config.feature_names())
|
|
131
|
+
return id_list_feature_names
|
|
132
|
+
|
|
133
|
+
def id_score_list_feature_names(self) -> List[str]:
|
|
134
|
+
id_score_list_feature_names = []
|
|
135
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
136
|
+
id_score_list_feature_names.extend(grouped_config.feature_names())
|
|
137
|
+
return id_score_list_feature_names
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class DpSparseFeaturesDist(BaseSparseFeaturesDist[SparseFeatures]):
|
|
141
|
+
"""
|
|
142
|
+
Distributes sparse features (input) to be data-parallel.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
def __init__(self) -> None:
|
|
146
|
+
super().__init__()
|
|
147
|
+
|
|
148
|
+
def forward(
|
|
149
|
+
self,
|
|
150
|
+
sparse_features: SparseFeatures,
|
|
151
|
+
) -> Awaitable[Awaitable[SparseFeatures]]:
|
|
152
|
+
"""
|
|
153
|
+
No-op as sparse features are already distributed in data-parallel fashion.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
sparse_features (SparseFeatures): input sparse features.
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
Awaitable[Awaitable[SparseFeatures]]: awaitable of awaitable of SparseFeatures.
|
|
160
|
+
"""
|
|
161
|
+
|
|
162
|
+
return NoWait(cast(Awaitable[SparseFeatures], NoWait(sparse_features)))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class DpPooledEmbeddingDist(BaseEmbeddingDist[torch.Tensor]):
|
|
166
|
+
"""
|
|
167
|
+
Distributes pooled embeddings to be data-parallel.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
def __init__(self) -> None:
|
|
171
|
+
super().__init__()
|
|
172
|
+
|
|
173
|
+
def forward(self, local_embs: torch.Tensor) -> Awaitable[torch.Tensor]:
|
|
174
|
+
"""
|
|
175
|
+
No-op as pooled embeddings are already distributed in data-parallel fashion.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
local_embs (torch.Tensor): output sequence embeddings.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
Awaitable[torch.Tensor]: awaitable of pooled embeddings tensor.
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
return NoWait(local_embs)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class DpPooledEmbeddingSharding(BaseDpEmbeddingSharding[SparseFeatures, torch.Tensor]):
|
|
188
|
+
"""
|
|
189
|
+
Shards embedding bags data-parallel, with no table sharding i.e.. a given embedding
|
|
190
|
+
table is replicated across all ranks.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def create_input_dist(
|
|
194
|
+
self, device: Optional[torch.device] = None
|
|
195
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
196
|
+
return DpSparseFeaturesDist()
|
|
197
|
+
|
|
198
|
+
def create_lookup(
|
|
199
|
+
self,
|
|
200
|
+
device: Optional[torch.device] = None,
|
|
201
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
202
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
203
|
+
) -> BaseEmbeddingLookup:
|
|
204
|
+
return GroupedPooledEmbeddingsLookup(
|
|
205
|
+
grouped_configs=self._grouped_embedding_configs,
|
|
206
|
+
grouped_score_configs=self._score_grouped_embedding_configs,
|
|
207
|
+
fused_params=fused_params,
|
|
208
|
+
pg=self._env.process_group,
|
|
209
|
+
device=device if device is not None else self._device,
|
|
210
|
+
feature_processor=feature_processor,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
def create_output_dist(
|
|
214
|
+
self,
|
|
215
|
+
device: Optional[torch.device] = None,
|
|
216
|
+
) -> BaseEmbeddingDist[torch.Tensor]:
|
|
217
|
+
return DpPooledEmbeddingDist()
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
from typing import Any, Dict, Optional
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
import torch.distributed as dist
|
|
12
|
+
from torchrec.distributed.dist_data import SequenceEmbeddingsAllToAll
|
|
13
|
+
from torchrec.distributed.embedding_lookup import GroupedEmbeddingsLookup
|
|
14
|
+
from torchrec.distributed.embedding_sharding import (
|
|
15
|
+
BaseEmbeddingLookup,
|
|
16
|
+
BaseSparseFeaturesDist,
|
|
17
|
+
)
|
|
18
|
+
from torchrec.distributed.embedding_types import (
|
|
19
|
+
BaseGroupedFeatureProcessor,
|
|
20
|
+
SparseFeatures,
|
|
21
|
+
)
|
|
22
|
+
from torchrec.distributed.sharding.rw_sharding import (
|
|
23
|
+
BaseRwEmbeddingSharding,
|
|
24
|
+
RwSparseFeaturesDist,
|
|
25
|
+
)
|
|
26
|
+
from torchrec.distributed.sharding.sequence_sharding import (
|
|
27
|
+
BaseSequenceEmbeddingDist,
|
|
28
|
+
SequenceShardingContext,
|
|
29
|
+
)
|
|
30
|
+
from torchrec.distributed.types import Awaitable
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class RwSequenceEmbeddingDist(BaseSequenceEmbeddingDist[torch.Tensor]):
|
|
34
|
+
"""
|
|
35
|
+
Redistributes sequence embedding tensor in RW fashion with an AlltoAll operation.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
pg (dist.ProcessGroup): ProcessGroup for AlltoAll communication.
|
|
39
|
+
num_features (int): total number of features.
|
|
40
|
+
device (Optional[torch.device]): device on which buffers will be allocated.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
pg: dist.ProcessGroup,
|
|
46
|
+
num_features: int,
|
|
47
|
+
device: Optional[torch.device] = None,
|
|
48
|
+
) -> None:
|
|
49
|
+
super().__init__()
|
|
50
|
+
self._dist = SequenceEmbeddingsAllToAll(pg, [num_features] * pg.size(), device)
|
|
51
|
+
|
|
52
|
+
def forward(
|
|
53
|
+
self,
|
|
54
|
+
local_embs: torch.Tensor,
|
|
55
|
+
sharding_ctx: SequenceShardingContext,
|
|
56
|
+
) -> Awaitable[torch.Tensor]:
|
|
57
|
+
"""
|
|
58
|
+
Performs AlltoAll operation on sequence embeddings tensor.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
local_embs (torch.Tensor): tensor of values to distribute.
|
|
62
|
+
sharding_ctx (SequenceShardingContext): shared context from KJTAllToAll
|
|
63
|
+
operation.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Awaitable[torch.Tensor]: awaitable of sequence embeddings.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
return self._dist(
|
|
70
|
+
local_embs,
|
|
71
|
+
lengths=sharding_ctx.lengths_after_input_dist,
|
|
72
|
+
input_splits=sharding_ctx.input_splits,
|
|
73
|
+
output_splits=sharding_ctx.output_splits,
|
|
74
|
+
unbucketize_permute_tensor=sharding_ctx.unbucketize_permute_tensor,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class RwSequenceEmbeddingSharding(
|
|
79
|
+
BaseRwEmbeddingSharding[SparseFeatures, torch.Tensor]
|
|
80
|
+
):
|
|
81
|
+
"""
|
|
82
|
+
Shards sequence (unpooled) row-wise, i.e.. a given embedding table is evenly
|
|
83
|
+
distributed by rows and table slices are placed on all ranks.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
def create_input_dist(
|
|
87
|
+
self,
|
|
88
|
+
device: Optional[torch.device] = None,
|
|
89
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
90
|
+
num_id_list_features = self._get_id_list_features_num()
|
|
91
|
+
num_id_score_list_features = self._get_id_score_list_features_num()
|
|
92
|
+
id_list_feature_hash_sizes = self._get_id_list_features_hash_sizes()
|
|
93
|
+
id_score_list_feature_hash_sizes = self._get_id_score_list_features_hash_sizes()
|
|
94
|
+
return RwSparseFeaturesDist(
|
|
95
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
96
|
+
# `Optional[ProcessGroup]`.
|
|
97
|
+
pg=self._pg,
|
|
98
|
+
num_id_list_features=num_id_list_features,
|
|
99
|
+
num_id_score_list_features=num_id_score_list_features,
|
|
100
|
+
id_list_feature_hash_sizes=id_list_feature_hash_sizes,
|
|
101
|
+
id_score_list_feature_hash_sizes=id_score_list_feature_hash_sizes,
|
|
102
|
+
device=device if device is not None else self._device,
|
|
103
|
+
is_sequence=True,
|
|
104
|
+
has_feature_processor=self._has_feature_processor,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def create_lookup(
|
|
108
|
+
self,
|
|
109
|
+
device: Optional[torch.device] = None,
|
|
110
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
111
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
112
|
+
) -> BaseEmbeddingLookup:
|
|
113
|
+
return GroupedEmbeddingsLookup(
|
|
114
|
+
grouped_configs=self._grouped_embedding_configs,
|
|
115
|
+
fused_params=fused_params,
|
|
116
|
+
pg=self._pg,
|
|
117
|
+
device=device if device is not None else self._device,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def create_output_dist(
|
|
121
|
+
self,
|
|
122
|
+
device: Optional[torch.device] = None,
|
|
123
|
+
) -> BaseSequenceEmbeddingDist[torch.Tensor]:
|
|
124
|
+
return RwSequenceEmbeddingDist(
|
|
125
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
126
|
+
# `Optional[ProcessGroup]`.
|
|
127
|
+
self._pg,
|
|
128
|
+
self._get_id_list_features_num(),
|
|
129
|
+
device if device is not None else self._device,
|
|
130
|
+
)
|