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,543 @@
|
|
|
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 itertools
|
|
9
|
+
import math
|
|
10
|
+
from typing import Any, cast, Dict, List, Optional, TypeVar
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
import torch.distributed as dist
|
|
14
|
+
from torchrec.distributed.comm import get_local_size, intra_and_cross_node_pg
|
|
15
|
+
from torchrec.distributed.dist_data import (
|
|
16
|
+
PooledEmbeddingsAllToAll,
|
|
17
|
+
PooledEmbeddingsReduceScatter,
|
|
18
|
+
)
|
|
19
|
+
from torchrec.distributed.embedding_lookup import GroupedPooledEmbeddingsLookup
|
|
20
|
+
from torchrec.distributed.embedding_sharding import (
|
|
21
|
+
BaseEmbeddingDist,
|
|
22
|
+
BaseEmbeddingLookup,
|
|
23
|
+
BaseSparseFeaturesDist,
|
|
24
|
+
bucketize_kjt_before_all2all,
|
|
25
|
+
EmbeddingSharding,
|
|
26
|
+
EmbeddingShardingInfo,
|
|
27
|
+
group_tables,
|
|
28
|
+
SparseFeaturesAllToAll,
|
|
29
|
+
)
|
|
30
|
+
from torchrec.distributed.embedding_types import (
|
|
31
|
+
BaseGroupedFeatureProcessor,
|
|
32
|
+
EmbeddingComputeKernel,
|
|
33
|
+
GroupedEmbeddingConfig,
|
|
34
|
+
ShardedEmbeddingTable,
|
|
35
|
+
SparseFeatures,
|
|
36
|
+
)
|
|
37
|
+
from torchrec.distributed.types import (
|
|
38
|
+
Awaitable,
|
|
39
|
+
ShardedTensorMetadata,
|
|
40
|
+
ShardingEnv,
|
|
41
|
+
ShardMetadata,
|
|
42
|
+
)
|
|
43
|
+
from torchrec.streamable import Multistreamable
|
|
44
|
+
|
|
45
|
+
F = TypeVar("F", bound=Multistreamable)
|
|
46
|
+
T = TypeVar("T")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class BaseTwRwEmbeddingSharding(EmbeddingSharding[F, T]):
|
|
50
|
+
"""
|
|
51
|
+
Base class for table wise row wise sharding.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
57
|
+
env: ShardingEnv,
|
|
58
|
+
device: Optional[torch.device] = None,
|
|
59
|
+
) -> None:
|
|
60
|
+
super().__init__()
|
|
61
|
+
self._env = env
|
|
62
|
+
self._pg: Optional[dist.ProcessGroup] = self._env.process_group
|
|
63
|
+
self._world_size: int = self._env.world_size
|
|
64
|
+
self._rank: int = self._env.rank
|
|
65
|
+
self._device = device
|
|
66
|
+
intra_pg, cross_pg = intra_and_cross_node_pg(device)
|
|
67
|
+
self._intra_pg: Optional[dist.ProcessGroup] = intra_pg
|
|
68
|
+
self._cross_pg: Optional[dist.ProcessGroup] = cross_pg
|
|
69
|
+
self._local_size: int = (
|
|
70
|
+
intra_pg.size() if intra_pg else get_local_size(self._world_size)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
sharded_tables_per_rank = self._shard(sharding_infos)
|
|
74
|
+
self._grouped_embedding_configs_per_rank: List[
|
|
75
|
+
List[GroupedEmbeddingConfig]
|
|
76
|
+
] = []
|
|
77
|
+
self._score_grouped_embedding_configs_per_rank: List[
|
|
78
|
+
List[GroupedEmbeddingConfig]
|
|
79
|
+
] = []
|
|
80
|
+
self._grouped_embedding_configs_per_node: List[
|
|
81
|
+
List[GroupedEmbeddingConfig]
|
|
82
|
+
] = []
|
|
83
|
+
self._score_grouped_embedding_configs_per_node: List[
|
|
84
|
+
List[GroupedEmbeddingConfig]
|
|
85
|
+
] = []
|
|
86
|
+
(
|
|
87
|
+
self._grouped_embedding_configs_per_rank,
|
|
88
|
+
self._score_grouped_embedding_configs_per_rank,
|
|
89
|
+
) = group_tables(sharded_tables_per_rank)
|
|
90
|
+
self._grouped_embedding_configs_per_node = [
|
|
91
|
+
self._grouped_embedding_configs_per_rank[rank]
|
|
92
|
+
for rank in range(self._world_size)
|
|
93
|
+
if rank % self._local_size == 0
|
|
94
|
+
]
|
|
95
|
+
self._score_grouped_embedding_configs_per_node = [
|
|
96
|
+
self._score_grouped_embedding_configs_per_rank[rank]
|
|
97
|
+
for rank in range(self._world_size)
|
|
98
|
+
if rank % self._local_size == 0
|
|
99
|
+
]
|
|
100
|
+
self._has_feature_processor: bool = False
|
|
101
|
+
for group_config in self._score_grouped_embedding_configs_per_node[
|
|
102
|
+
self._rank // self._local_size
|
|
103
|
+
]:
|
|
104
|
+
if group_config.has_feature_processor:
|
|
105
|
+
self._has_feature_processor = True
|
|
106
|
+
|
|
107
|
+
def _shard(
|
|
108
|
+
self,
|
|
109
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
110
|
+
) -> List[List[ShardedEmbeddingTable]]:
|
|
111
|
+
world_size = self._world_size
|
|
112
|
+
local_size = self._local_size
|
|
113
|
+
tables_per_rank: List[List[ShardedEmbeddingTable]] = [
|
|
114
|
+
[] for i in range(world_size)
|
|
115
|
+
]
|
|
116
|
+
for info in sharding_infos:
|
|
117
|
+
# pyre-ignore [16]
|
|
118
|
+
table_node = info.param_sharding.ranks[0] // local_size
|
|
119
|
+
# pyre-fixme [16]
|
|
120
|
+
shards = info.param_sharding.sharding_spec.shards
|
|
121
|
+
|
|
122
|
+
# construct the global sharded_tensor_metadata
|
|
123
|
+
global_metadata = ShardedTensorMetadata(
|
|
124
|
+
shards_metadata=shards,
|
|
125
|
+
size=torch.Size(
|
|
126
|
+
[
|
|
127
|
+
info.embedding_config.num_embeddings,
|
|
128
|
+
info.embedding_config.embedding_dim,
|
|
129
|
+
]
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
for rank in range(
|
|
134
|
+
table_node * local_size,
|
|
135
|
+
(table_node + 1) * local_size,
|
|
136
|
+
):
|
|
137
|
+
rank_idx = rank - (table_node * local_size)
|
|
138
|
+
tables_per_rank[rank].append(
|
|
139
|
+
ShardedEmbeddingTable(
|
|
140
|
+
num_embeddings=info.embedding_config.num_embeddings,
|
|
141
|
+
embedding_dim=info.embedding_config.embedding_dim,
|
|
142
|
+
name=info.embedding_config.name,
|
|
143
|
+
embedding_names=info.embedding_config.embedding_names,
|
|
144
|
+
data_type=info.embedding_config.data_type,
|
|
145
|
+
feature_names=info.embedding_config.feature_names,
|
|
146
|
+
pooling=info.embedding_config.pooling,
|
|
147
|
+
is_weighted=info.embedding_config.is_weighted,
|
|
148
|
+
has_feature_processor=info.embedding_config.has_feature_processor,
|
|
149
|
+
local_rows=shards[rank_idx].shard_sizes[0],
|
|
150
|
+
local_cols=info.embedding_config.embedding_dim,
|
|
151
|
+
compute_kernel=EmbeddingComputeKernel(
|
|
152
|
+
info.param_sharding.compute_kernel
|
|
153
|
+
),
|
|
154
|
+
local_metadata=shards[rank_idx],
|
|
155
|
+
global_metadata=global_metadata,
|
|
156
|
+
weight_init_max=info.embedding_config.weight_init_max,
|
|
157
|
+
weight_init_min=info.embedding_config.weight_init_min,
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
return tables_per_rank
|
|
162
|
+
|
|
163
|
+
def embedding_dims(self) -> List[int]:
|
|
164
|
+
embedding_dims = []
|
|
165
|
+
for grouped_embedding_configs, score_grouped_embedding_configs in zip(
|
|
166
|
+
self._grouped_embedding_configs_per_node,
|
|
167
|
+
self._score_grouped_embedding_configs_per_node,
|
|
168
|
+
):
|
|
169
|
+
for grouped_config in grouped_embedding_configs:
|
|
170
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
171
|
+
for grouped_config in score_grouped_embedding_configs:
|
|
172
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
173
|
+
return embedding_dims
|
|
174
|
+
|
|
175
|
+
def embedding_names(self) -> List[str]:
|
|
176
|
+
embedding_names = []
|
|
177
|
+
for grouped_embedding_configs, score_grouped_embedding_configs in zip(
|
|
178
|
+
self._grouped_embedding_configs_per_node,
|
|
179
|
+
self._score_grouped_embedding_configs_per_node,
|
|
180
|
+
):
|
|
181
|
+
for grouped_config in grouped_embedding_configs:
|
|
182
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
183
|
+
for grouped_config in score_grouped_embedding_configs:
|
|
184
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
185
|
+
return embedding_names
|
|
186
|
+
|
|
187
|
+
def embedding_shard_metadata(self) -> List[Optional[ShardMetadata]]:
|
|
188
|
+
embedding_shard_metadata = []
|
|
189
|
+
for grouped_config in self._grouped_embedding_configs_per_node:
|
|
190
|
+
for config in grouped_config:
|
|
191
|
+
embedding_shard_metadata.extend(config.embedding_shard_metadata())
|
|
192
|
+
for grouped_config in self._score_grouped_embedding_configs_per_node:
|
|
193
|
+
for config in grouped_config:
|
|
194
|
+
embedding_shard_metadata.extend(config.embedding_shard_metadata())
|
|
195
|
+
return embedding_shard_metadata
|
|
196
|
+
|
|
197
|
+
def id_list_feature_names(self) -> List[str]:
|
|
198
|
+
id_list_feature_names = []
|
|
199
|
+
for grouped_config in self._grouped_embedding_configs_per_node:
|
|
200
|
+
for config in grouped_config:
|
|
201
|
+
id_list_feature_names.extend(config.feature_names())
|
|
202
|
+
return id_list_feature_names
|
|
203
|
+
|
|
204
|
+
def id_score_list_feature_names(self) -> List[str]:
|
|
205
|
+
id_score_list_feature_names = []
|
|
206
|
+
for grouped_config in self._score_grouped_embedding_configs_per_node:
|
|
207
|
+
for config in grouped_config:
|
|
208
|
+
id_score_list_feature_names.extend(config.feature_names())
|
|
209
|
+
return id_score_list_feature_names
|
|
210
|
+
|
|
211
|
+
def _get_id_list_features_hash_sizes(self) -> List[int]:
|
|
212
|
+
id_list_feature_hash_sizes: List[int] = []
|
|
213
|
+
for grouped_config in self._grouped_embedding_configs_per_node:
|
|
214
|
+
for config in grouped_config:
|
|
215
|
+
id_list_feature_hash_sizes.extend(config.feature_hash_sizes())
|
|
216
|
+
return id_list_feature_hash_sizes
|
|
217
|
+
|
|
218
|
+
def _get_id_score_list_features_hash_sizes(self) -> List[int]:
|
|
219
|
+
id_score_list_feature_hash_sizes: List[int] = []
|
|
220
|
+
for grouped_config in self._score_grouped_embedding_configs_per_node:
|
|
221
|
+
for config in grouped_config:
|
|
222
|
+
id_score_list_feature_hash_sizes.extend(config.feature_hash_sizes())
|
|
223
|
+
return id_score_list_feature_hash_sizes
|
|
224
|
+
|
|
225
|
+
def _dim_sum_per_node(self) -> List[int]:
|
|
226
|
+
dim_sum_per_rank = []
|
|
227
|
+
for grouped_embedding_configs, score_grouped_embedding_configs in zip(
|
|
228
|
+
self._grouped_embedding_configs_per_node,
|
|
229
|
+
self._score_grouped_embedding_configs_per_node,
|
|
230
|
+
):
|
|
231
|
+
dim_sum = 0
|
|
232
|
+
for grouped_config in grouped_embedding_configs:
|
|
233
|
+
dim_sum += grouped_config.dim_sum()
|
|
234
|
+
for grouped_config in score_grouped_embedding_configs:
|
|
235
|
+
dim_sum += grouped_config.dim_sum()
|
|
236
|
+
dim_sum_per_rank.append(dim_sum)
|
|
237
|
+
return dim_sum_per_rank
|
|
238
|
+
|
|
239
|
+
def _features_per_rank(
|
|
240
|
+
self, group: List[List[GroupedEmbeddingConfig]]
|
|
241
|
+
) -> List[int]:
|
|
242
|
+
features_per_rank = []
|
|
243
|
+
for grouped_embedding_configs in group:
|
|
244
|
+
num_features = 0
|
|
245
|
+
for grouped_config in grouped_embedding_configs:
|
|
246
|
+
num_features += grouped_config.num_features()
|
|
247
|
+
features_per_rank.append(num_features)
|
|
248
|
+
return features_per_rank
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class TwRwSparseFeaturesDist(BaseSparseFeaturesDist[SparseFeatures]):
|
|
252
|
+
"""
|
|
253
|
+
Bucketizes sparse features in TWRW fashion and then redistributes with an AlltoAll
|
|
254
|
+
collective operation.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
pg (dist.ProcessGroup): ProcessGroup for AlltoAll communication.
|
|
258
|
+
intra_pg (dist.ProcessGroup): ProcessGroup within single host group for AlltoAll
|
|
259
|
+
communication.
|
|
260
|
+
id_list_features_per_rank (List[int]): number of id list features to send to
|
|
261
|
+
each rank.
|
|
262
|
+
id_score_list_features_per_rank (List[int]): number of id score list features to
|
|
263
|
+
send to each rank.
|
|
264
|
+
id_list_feature_hash_sizes (List[int]): hash sizes of id list features.
|
|
265
|
+
id_score_list_feature_hash_sizes (List[int]): hash sizes of id score list
|
|
266
|
+
features.
|
|
267
|
+
device (Optional[torch.device]): device on which buffers will be allocated.
|
|
268
|
+
has_feature_processor (bool): existence of a feature processor (ie. position
|
|
269
|
+
weighted features).
|
|
270
|
+
|
|
271
|
+
Example::
|
|
272
|
+
|
|
273
|
+
3 features
|
|
274
|
+
2 hosts with 2 devices each
|
|
275
|
+
|
|
276
|
+
Bucketize each feature into 2 buckets
|
|
277
|
+
Staggered shuffle with feature splits [2, 1]
|
|
278
|
+
AlltoAll operation
|
|
279
|
+
|
|
280
|
+
NOTE: result of staggered shuffle and AlltoAll operation look the same after
|
|
281
|
+
reordering in AlltoAll
|
|
282
|
+
|
|
283
|
+
Result:
|
|
284
|
+
host 0 device 0:
|
|
285
|
+
feature 0 bucket 0
|
|
286
|
+
feature 1 bucket 0
|
|
287
|
+
|
|
288
|
+
host 0 device 1:
|
|
289
|
+
feature 0 bucket 1
|
|
290
|
+
feature 1 bucket 1
|
|
291
|
+
|
|
292
|
+
host 1 device 0:
|
|
293
|
+
feature 2 bucket 0
|
|
294
|
+
|
|
295
|
+
host 1 device 1:
|
|
296
|
+
feature 2 bucket 1
|
|
297
|
+
"""
|
|
298
|
+
|
|
299
|
+
def __init__(
|
|
300
|
+
self,
|
|
301
|
+
pg: dist.ProcessGroup,
|
|
302
|
+
intra_pg: dist.ProcessGroup,
|
|
303
|
+
id_list_features_per_rank: List[int],
|
|
304
|
+
id_score_list_features_per_rank: List[int],
|
|
305
|
+
id_list_feature_hash_sizes: List[int],
|
|
306
|
+
id_score_list_feature_hash_sizes: List[int],
|
|
307
|
+
device: Optional[torch.device] = None,
|
|
308
|
+
has_feature_processor: bool = False,
|
|
309
|
+
) -> None:
|
|
310
|
+
super().__init__()
|
|
311
|
+
assert (
|
|
312
|
+
pg.size() % intra_pg.size() == 0
|
|
313
|
+
), "currently group granularity must be node"
|
|
314
|
+
|
|
315
|
+
self._world_size: int = pg.size()
|
|
316
|
+
self._local_size: int = intra_pg.size()
|
|
317
|
+
self._num_cross_nodes: int = self._world_size // self._local_size
|
|
318
|
+
id_list_feature_block_sizes = [
|
|
319
|
+
math.ceil(hash_size / self._local_size)
|
|
320
|
+
for hash_size in id_list_feature_hash_sizes
|
|
321
|
+
]
|
|
322
|
+
id_score_list_feature_block_sizes = [
|
|
323
|
+
math.ceil(hash_size / self._local_size)
|
|
324
|
+
for hash_size in id_score_list_feature_hash_sizes
|
|
325
|
+
]
|
|
326
|
+
|
|
327
|
+
self._id_list_sf_staggered_shuffle: List[int] = self._staggered_shuffle(
|
|
328
|
+
id_list_features_per_rank
|
|
329
|
+
)
|
|
330
|
+
self._id_score_list_sf_staggered_shuffle: List[int] = self._staggered_shuffle(
|
|
331
|
+
id_score_list_features_per_rank
|
|
332
|
+
)
|
|
333
|
+
self.register_buffer(
|
|
334
|
+
"_id_list_feature_block_sizes_tensor",
|
|
335
|
+
torch.tensor(
|
|
336
|
+
id_list_feature_block_sizes,
|
|
337
|
+
device=device,
|
|
338
|
+
dtype=torch.int32,
|
|
339
|
+
),
|
|
340
|
+
)
|
|
341
|
+
self.register_buffer(
|
|
342
|
+
"_id_score_list_feature_block_sizes_tensor",
|
|
343
|
+
torch.tensor(
|
|
344
|
+
id_score_list_feature_block_sizes,
|
|
345
|
+
device=device,
|
|
346
|
+
dtype=torch.int32,
|
|
347
|
+
),
|
|
348
|
+
)
|
|
349
|
+
self.register_buffer(
|
|
350
|
+
"_id_list_sf_staggered_shuffle_tensor",
|
|
351
|
+
torch.tensor(
|
|
352
|
+
self._id_list_sf_staggered_shuffle,
|
|
353
|
+
device=device,
|
|
354
|
+
dtype=torch.int32,
|
|
355
|
+
),
|
|
356
|
+
)
|
|
357
|
+
self.register_buffer(
|
|
358
|
+
"_id_score_list_sf_staggered_shuffle_tensor",
|
|
359
|
+
torch.tensor(
|
|
360
|
+
self._id_score_list_sf_staggered_shuffle,
|
|
361
|
+
device=device,
|
|
362
|
+
dtype=torch.int32,
|
|
363
|
+
),
|
|
364
|
+
)
|
|
365
|
+
self._dist = SparseFeaturesAllToAll(
|
|
366
|
+
pg=pg,
|
|
367
|
+
id_list_features_per_rank=id_list_features_per_rank,
|
|
368
|
+
id_score_list_features_per_rank=id_score_list_features_per_rank,
|
|
369
|
+
device=device,
|
|
370
|
+
stagger=self._num_cross_nodes,
|
|
371
|
+
)
|
|
372
|
+
self._has_feature_processor = has_feature_processor
|
|
373
|
+
|
|
374
|
+
def forward(
|
|
375
|
+
self,
|
|
376
|
+
sparse_features: SparseFeatures,
|
|
377
|
+
) -> Awaitable[Awaitable[SparseFeatures]]:
|
|
378
|
+
"""
|
|
379
|
+
Bucketizes sparse feature values into local world size number of buckets,
|
|
380
|
+
performs staggered shuffle on the sparse features, and then performs AlltoAll
|
|
381
|
+
operation.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
sparse_features (SparseFeatures): sparse features to bucketize and
|
|
385
|
+
redistribute.
|
|
386
|
+
|
|
387
|
+
Returns:
|
|
388
|
+
Awaitable[SparseFeatures]: awaitable of SparseFeatures.
|
|
389
|
+
"""
|
|
390
|
+
|
|
391
|
+
bucketized_sparse_features = SparseFeatures(
|
|
392
|
+
id_list_features=bucketize_kjt_before_all2all(
|
|
393
|
+
sparse_features.id_list_features,
|
|
394
|
+
num_buckets=self._local_size,
|
|
395
|
+
block_sizes=self._id_list_feature_block_sizes_tensor,
|
|
396
|
+
output_permute=False,
|
|
397
|
+
bucketize_pos=self._has_feature_processor,
|
|
398
|
+
)[0].permute(
|
|
399
|
+
self._id_list_sf_staggered_shuffle,
|
|
400
|
+
self._id_list_sf_staggered_shuffle_tensor,
|
|
401
|
+
)
|
|
402
|
+
if sparse_features.id_list_features is not None
|
|
403
|
+
else None,
|
|
404
|
+
id_score_list_features=bucketize_kjt_before_all2all(
|
|
405
|
+
sparse_features.id_score_list_features,
|
|
406
|
+
num_buckets=self._local_size,
|
|
407
|
+
block_sizes=self._id_score_list_feature_block_sizes_tensor,
|
|
408
|
+
output_permute=False,
|
|
409
|
+
bucketize_pos=False,
|
|
410
|
+
)[0].permute(
|
|
411
|
+
self._id_score_list_sf_staggered_shuffle,
|
|
412
|
+
self._id_score_list_sf_staggered_shuffle_tensor,
|
|
413
|
+
)
|
|
414
|
+
if sparse_features.id_score_list_features is not None
|
|
415
|
+
else None,
|
|
416
|
+
)
|
|
417
|
+
return self._dist(bucketized_sparse_features)
|
|
418
|
+
|
|
419
|
+
def _staggered_shuffle(self, features_per_rank: List[int]) -> List[int]:
|
|
420
|
+
"""
|
|
421
|
+
Reorders sparse data such that data is in contiguous blocks and correctly
|
|
422
|
+
ordered for global TWRW layout.
|
|
423
|
+
"""
|
|
424
|
+
|
|
425
|
+
nodes = self._world_size // self._local_size
|
|
426
|
+
features_per_node = [
|
|
427
|
+
features_per_rank[node * self._local_size] for node in range(nodes)
|
|
428
|
+
]
|
|
429
|
+
node_offsets = [0] + list(itertools.accumulate(features_per_node))
|
|
430
|
+
num_features = node_offsets[-1]
|
|
431
|
+
|
|
432
|
+
return [
|
|
433
|
+
bucket * num_features + feature
|
|
434
|
+
for node in range(nodes)
|
|
435
|
+
for bucket in range(self._local_size)
|
|
436
|
+
for feature in range(node_offsets[node], node_offsets[node + 1])
|
|
437
|
+
]
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
class TwRwPooledEmbeddingDist(BaseEmbeddingDist[torch.Tensor]):
|
|
441
|
+
"""
|
|
442
|
+
Redistributes pooled embedding tensor in TWRW fashion by performing a reduce-scatter
|
|
443
|
+
operation row wise on the host level and then an AlltoAll operation table wise on
|
|
444
|
+
the global level.
|
|
445
|
+
|
|
446
|
+
Args:
|
|
447
|
+
cross_pg (dist.ProcessGroup): global level ProcessGroup for AlltoAll
|
|
448
|
+
communication.
|
|
449
|
+
intra_pg (dist.ProcessGroup): host level ProcessGroup for reduce-scatter
|
|
450
|
+
communication.
|
|
451
|
+
dim_sum_per_node (List[int]): number of features (sum of dimensions) of the
|
|
452
|
+
embedding for each host.
|
|
453
|
+
device (Optional[torch.device]): device on which buffers will be allocated.
|
|
454
|
+
"""
|
|
455
|
+
|
|
456
|
+
def __init__(
|
|
457
|
+
self,
|
|
458
|
+
cross_pg: dist.ProcessGroup,
|
|
459
|
+
intra_pg: dist.ProcessGroup,
|
|
460
|
+
dim_sum_per_node: List[int],
|
|
461
|
+
device: Optional[torch.device] = None,
|
|
462
|
+
) -> None:
|
|
463
|
+
super().__init__()
|
|
464
|
+
self._intra_dist = PooledEmbeddingsReduceScatter(intra_pg)
|
|
465
|
+
self._cross_dist = PooledEmbeddingsAllToAll(
|
|
466
|
+
cross_pg,
|
|
467
|
+
dim_sum_per_node,
|
|
468
|
+
device,
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
def forward(self, local_embs: torch.Tensor) -> Awaitable[torch.Tensor]:
|
|
472
|
+
"""
|
|
473
|
+
Performs reduce-scatter pooled operation on pooled embeddings tensor followed by
|
|
474
|
+
AlltoAll pooled operation.
|
|
475
|
+
|
|
476
|
+
Args:
|
|
477
|
+
local_embs (torch.Tensor): pooled embeddings tensor to distribute.
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
Awaitable[torch.Tensor]: awaitable of pooled embeddings tensor.
|
|
481
|
+
"""
|
|
482
|
+
|
|
483
|
+
return self._cross_dist(self._intra_dist(local_embs).wait())
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
class TwRwPooledEmbeddingSharding(
|
|
487
|
+
BaseTwRwEmbeddingSharding[SparseFeatures, torch.Tensor]
|
|
488
|
+
):
|
|
489
|
+
"""
|
|
490
|
+
Shards embedding bags table-wise then row-wise.
|
|
491
|
+
"""
|
|
492
|
+
|
|
493
|
+
def create_input_dist(
|
|
494
|
+
self, device: Optional[torch.device] = None
|
|
495
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
496
|
+
id_list_features_per_rank = self._features_per_rank(
|
|
497
|
+
self._grouped_embedding_configs_per_rank
|
|
498
|
+
)
|
|
499
|
+
id_score_list_features_per_rank = self._features_per_rank(
|
|
500
|
+
self._score_grouped_embedding_configs_per_rank
|
|
501
|
+
)
|
|
502
|
+
id_list_feature_hash_sizes = self._get_id_list_features_hash_sizes()
|
|
503
|
+
id_score_list_feature_hash_sizes = self._get_id_score_list_features_hash_sizes()
|
|
504
|
+
return TwRwSparseFeaturesDist(
|
|
505
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
506
|
+
# `Optional[ProcessGroup]`.
|
|
507
|
+
pg=self._pg,
|
|
508
|
+
intra_pg=cast(dist.ProcessGroup, self._intra_pg),
|
|
509
|
+
id_list_features_per_rank=id_list_features_per_rank,
|
|
510
|
+
id_score_list_features_per_rank=id_score_list_features_per_rank,
|
|
511
|
+
id_list_feature_hash_sizes=id_list_feature_hash_sizes,
|
|
512
|
+
id_score_list_feature_hash_sizes=id_score_list_feature_hash_sizes,
|
|
513
|
+
device=device if device is not None else self._device,
|
|
514
|
+
has_feature_processor=self._has_feature_processor,
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
def create_lookup(
|
|
518
|
+
self,
|
|
519
|
+
device: Optional[torch.device] = None,
|
|
520
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
521
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
522
|
+
) -> BaseEmbeddingLookup:
|
|
523
|
+
return GroupedPooledEmbeddingsLookup(
|
|
524
|
+
grouped_configs=self._grouped_embedding_configs_per_rank[self._rank],
|
|
525
|
+
grouped_score_configs=self._score_grouped_embedding_configs_per_rank[
|
|
526
|
+
self._rank
|
|
527
|
+
],
|
|
528
|
+
fused_params=fused_params,
|
|
529
|
+
pg=self._pg,
|
|
530
|
+
device=device if device is not None else self._device,
|
|
531
|
+
feature_processor=feature_processor,
|
|
532
|
+
)
|
|
533
|
+
|
|
534
|
+
def create_output_dist(
|
|
535
|
+
self,
|
|
536
|
+
device: Optional[torch.device] = None,
|
|
537
|
+
) -> BaseEmbeddingDist[torch.Tensor]:
|
|
538
|
+
return TwRwPooledEmbeddingDist(
|
|
539
|
+
cross_pg=cast(dist.ProcessGroup, self._cross_pg),
|
|
540
|
+
intra_pg=cast(dist.ProcessGroup, self._intra_pg),
|
|
541
|
+
dim_sum_per_node=self._dim_sum_per_node(),
|
|
542
|
+
device=device if device is not None else self._device,
|
|
543
|
+
)
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
from fbgemm_gpu.permute_pooled_embedding_modules_split import (
|
|
12
|
+
PermutePooledEmbeddingsSplit,
|
|
13
|
+
)
|
|
14
|
+
from torchrec.distributed.embedding_lookup import GroupedPooledEmbeddingsLookup
|
|
15
|
+
from torchrec.distributed.embedding_sharding import (
|
|
16
|
+
BaseEmbeddingLookup,
|
|
17
|
+
BaseSparseFeaturesDist,
|
|
18
|
+
)
|
|
19
|
+
from torchrec.distributed.embedding_types import (
|
|
20
|
+
BaseGroupedFeatureProcessor,
|
|
21
|
+
SparseFeatures,
|
|
22
|
+
)
|
|
23
|
+
from torchrec.distributed.sharding.cw_sharding import BaseCwEmbeddingSharding
|
|
24
|
+
from torchrec.distributed.sharding.vb_sharding import BaseVariableBatchEmbeddingDist
|
|
25
|
+
from torchrec.distributed.sharding.vb_tw_sharding import (
|
|
26
|
+
VariableBatchTwPooledEmbeddingDist,
|
|
27
|
+
VariableBatchTwSparseFeaturesDist,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class VariableBatchCwPooledEmbeddingSharding(
|
|
32
|
+
BaseCwEmbeddingSharding[SparseFeatures, torch.Tensor]
|
|
33
|
+
):
|
|
34
|
+
"""
|
|
35
|
+
Shards embedding bags column-wise, i.e.. a given embedding table is partitioned
|
|
36
|
+
along its columns and placed on specified ranks.
|
|
37
|
+
|
|
38
|
+
Supports variable batch size.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def create_input_dist(
|
|
42
|
+
self,
|
|
43
|
+
device: Optional[torch.device] = None,
|
|
44
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
45
|
+
return VariableBatchTwSparseFeaturesDist(
|
|
46
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
47
|
+
# `Optional[ProcessGroup]`.
|
|
48
|
+
self._pg,
|
|
49
|
+
self._id_list_features_per_rank(),
|
|
50
|
+
self._id_score_list_features_per_rank(),
|
|
51
|
+
device if device is not None else self._device,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def create_lookup(
|
|
55
|
+
self,
|
|
56
|
+
device: Optional[torch.device] = None,
|
|
57
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
58
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
59
|
+
) -> BaseEmbeddingLookup:
|
|
60
|
+
return GroupedPooledEmbeddingsLookup(
|
|
61
|
+
grouped_configs=self._grouped_embedding_configs,
|
|
62
|
+
grouped_score_configs=self._score_grouped_embedding_configs,
|
|
63
|
+
fused_params=fused_params,
|
|
64
|
+
pg=self._pg,
|
|
65
|
+
device=device if device is not None else self._device,
|
|
66
|
+
feature_processor=feature_processor,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def create_output_dist(
|
|
70
|
+
self,
|
|
71
|
+
device: Optional[torch.device] = None,
|
|
72
|
+
) -> BaseVariableBatchEmbeddingDist[torch.Tensor]:
|
|
73
|
+
callbacks = None
|
|
74
|
+
if self._permute_embeddings and self._embedding_order != list(
|
|
75
|
+
range(len(self._embedding_order))
|
|
76
|
+
):
|
|
77
|
+
assert len(self._embedding_order) == len(self._embedding_dims)
|
|
78
|
+
embedding_permute_op = PermutePooledEmbeddingsSplit(
|
|
79
|
+
self._embedding_dims,
|
|
80
|
+
self._embedding_order,
|
|
81
|
+
).to(device=device)
|
|
82
|
+
callbacks = [embedding_permute_op]
|
|
83
|
+
return VariableBatchTwPooledEmbeddingDist(
|
|
84
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
85
|
+
# `Optional[ProcessGroup]`.
|
|
86
|
+
self._pg,
|
|
87
|
+
self._dim_sum_per_rank(),
|
|
88
|
+
device if device is not None else self._device,
|
|
89
|
+
# pyre-ignore [6]
|
|
90
|
+
callbacks,
|
|
91
|
+
)
|