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,393 @@
|
|
|
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, List, Optional, TypeVar
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
import torch.distributed as dist
|
|
12
|
+
from torchrec.distributed.dist_data import PooledEmbeddingsReduceScatter
|
|
13
|
+
from torchrec.distributed.embedding_lookup import GroupedPooledEmbeddingsLookup
|
|
14
|
+
from torchrec.distributed.embedding_sharding import (
|
|
15
|
+
BaseEmbeddingDist,
|
|
16
|
+
BaseEmbeddingLookup,
|
|
17
|
+
BaseSparseFeaturesDist,
|
|
18
|
+
bucketize_kjt_before_all2all,
|
|
19
|
+
EmbeddingSharding,
|
|
20
|
+
EmbeddingShardingInfo,
|
|
21
|
+
group_tables,
|
|
22
|
+
SparseFeaturesAllToAll,
|
|
23
|
+
)
|
|
24
|
+
from torchrec.distributed.embedding_types import (
|
|
25
|
+
BaseGroupedFeatureProcessor,
|
|
26
|
+
EmbeddingComputeKernel,
|
|
27
|
+
GroupedEmbeddingConfig,
|
|
28
|
+
ShardedEmbeddingTable,
|
|
29
|
+
SparseFeatures,
|
|
30
|
+
)
|
|
31
|
+
from torchrec.distributed.types import (
|
|
32
|
+
Awaitable,
|
|
33
|
+
ShardedTensorMetadata,
|
|
34
|
+
ShardingEnv,
|
|
35
|
+
ShardMetadata,
|
|
36
|
+
)
|
|
37
|
+
from torchrec.streamable import Multistreamable
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
F = TypeVar("F", bound=Multistreamable)
|
|
41
|
+
T = TypeVar("T")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class BaseRwEmbeddingSharding(EmbeddingSharding[F, T]):
|
|
45
|
+
"""
|
|
46
|
+
Base class for row-wise sharding.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
52
|
+
env: ShardingEnv,
|
|
53
|
+
device: Optional[torch.device] = None,
|
|
54
|
+
) -> None:
|
|
55
|
+
super().__init__()
|
|
56
|
+
self._env = env
|
|
57
|
+
self._pg: Optional[dist.ProcessGroup] = self._env.process_group
|
|
58
|
+
self._world_size: int = self._env.world_size
|
|
59
|
+
self._rank: int = self._env.rank
|
|
60
|
+
if device is None:
|
|
61
|
+
device = torch.device("cpu")
|
|
62
|
+
self._device = device
|
|
63
|
+
sharded_tables_per_rank = self._shard(sharding_infos)
|
|
64
|
+
self._grouped_embedding_configs_per_rank: List[
|
|
65
|
+
List[GroupedEmbeddingConfig]
|
|
66
|
+
] = []
|
|
67
|
+
self._score_grouped_embedding_configs_per_rank: List[
|
|
68
|
+
List[GroupedEmbeddingConfig]
|
|
69
|
+
] = []
|
|
70
|
+
(
|
|
71
|
+
self._grouped_embedding_configs_per_rank,
|
|
72
|
+
self._score_grouped_embedding_configs_per_rank,
|
|
73
|
+
) = group_tables(sharded_tables_per_rank)
|
|
74
|
+
self._grouped_embedding_configs: List[
|
|
75
|
+
GroupedEmbeddingConfig
|
|
76
|
+
] = self._grouped_embedding_configs_per_rank[self._rank]
|
|
77
|
+
self._score_grouped_embedding_configs: List[
|
|
78
|
+
GroupedEmbeddingConfig
|
|
79
|
+
] = self._score_grouped_embedding_configs_per_rank[self._rank]
|
|
80
|
+
|
|
81
|
+
self._has_feature_processor: bool = False
|
|
82
|
+
for group_config in self._grouped_embedding_configs:
|
|
83
|
+
if group_config.has_feature_processor:
|
|
84
|
+
self._has_feature_processor = True
|
|
85
|
+
|
|
86
|
+
def _shard(
|
|
87
|
+
self,
|
|
88
|
+
sharding_infos: List[EmbeddingShardingInfo],
|
|
89
|
+
) -> List[List[ShardedEmbeddingTable]]:
|
|
90
|
+
tables_per_rank: List[List[ShardedEmbeddingTable]] = [
|
|
91
|
+
[] for i in range(self._world_size)
|
|
92
|
+
]
|
|
93
|
+
for info in sharding_infos:
|
|
94
|
+
# pyre-fixme [16]
|
|
95
|
+
shards = info.param_sharding.sharding_spec.shards
|
|
96
|
+
|
|
97
|
+
# construct the global sharded_tensor_metadata
|
|
98
|
+
global_metadata = ShardedTensorMetadata(
|
|
99
|
+
shards_metadata=shards,
|
|
100
|
+
size=torch.Size(
|
|
101
|
+
[
|
|
102
|
+
info.embedding_config.num_embeddings,
|
|
103
|
+
info.embedding_config.embedding_dim,
|
|
104
|
+
]
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
for rank in range(self._world_size):
|
|
109
|
+
tables_per_rank[rank].append(
|
|
110
|
+
ShardedEmbeddingTable(
|
|
111
|
+
num_embeddings=info.embedding_config.num_embeddings,
|
|
112
|
+
embedding_dim=info.embedding_config.embedding_dim,
|
|
113
|
+
name=info.embedding_config.name,
|
|
114
|
+
embedding_names=info.embedding_config.embedding_names,
|
|
115
|
+
data_type=info.embedding_config.data_type,
|
|
116
|
+
feature_names=info.embedding_config.feature_names,
|
|
117
|
+
pooling=info.embedding_config.pooling,
|
|
118
|
+
is_weighted=info.embedding_config.is_weighted,
|
|
119
|
+
has_feature_processor=info.embedding_config.has_feature_processor,
|
|
120
|
+
local_rows=shards[rank].shard_sizes[0],
|
|
121
|
+
local_cols=info.embedding_config.embedding_dim,
|
|
122
|
+
compute_kernel=EmbeddingComputeKernel(
|
|
123
|
+
info.param_sharding.compute_kernel
|
|
124
|
+
),
|
|
125
|
+
local_metadata=shards[rank],
|
|
126
|
+
global_metadata=global_metadata,
|
|
127
|
+
weight_init_max=info.embedding_config.weight_init_max,
|
|
128
|
+
weight_init_min=info.embedding_config.weight_init_min,
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
return tables_per_rank
|
|
132
|
+
|
|
133
|
+
def embedding_dims(self) -> List[int]:
|
|
134
|
+
embedding_dims = []
|
|
135
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
136
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
137
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
138
|
+
embedding_dims.extend(grouped_config.embedding_dims())
|
|
139
|
+
return embedding_dims
|
|
140
|
+
|
|
141
|
+
def embedding_names(self) -> List[str]:
|
|
142
|
+
embedding_names = []
|
|
143
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
144
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
145
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
146
|
+
embedding_names.extend(grouped_config.embedding_names())
|
|
147
|
+
return embedding_names
|
|
148
|
+
|
|
149
|
+
def embedding_shard_metadata(self) -> List[Optional[ShardMetadata]]:
|
|
150
|
+
embedding_shard_metadata = []
|
|
151
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
152
|
+
embedding_shard_metadata.extend(grouped_config.embedding_shard_metadata())
|
|
153
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
154
|
+
embedding_shard_metadata.extend(grouped_config.embedding_shard_metadata())
|
|
155
|
+
return embedding_shard_metadata
|
|
156
|
+
|
|
157
|
+
def id_list_feature_names(self) -> List[str]:
|
|
158
|
+
id_list_feature_names = []
|
|
159
|
+
for grouped_config in self._grouped_embedding_configs:
|
|
160
|
+
id_list_feature_names.extend(grouped_config.feature_names())
|
|
161
|
+
return id_list_feature_names
|
|
162
|
+
|
|
163
|
+
def id_score_list_feature_names(self) -> List[str]:
|
|
164
|
+
id_score_list_feature_names = []
|
|
165
|
+
for grouped_config in self._score_grouped_embedding_configs:
|
|
166
|
+
id_score_list_feature_names.extend(grouped_config.feature_names())
|
|
167
|
+
return id_score_list_feature_names
|
|
168
|
+
|
|
169
|
+
def _get_id_list_features_num(self) -> int:
|
|
170
|
+
return sum(
|
|
171
|
+
group_config.num_features()
|
|
172
|
+
for group_config in self._grouped_embedding_configs
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def _get_id_score_list_features_num(self) -> int:
|
|
176
|
+
return sum(
|
|
177
|
+
group_config.num_features()
|
|
178
|
+
for group_config in self._score_grouped_embedding_configs
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
def _get_id_list_features_hash_sizes(self) -> List[int]:
|
|
182
|
+
id_list_feature_hash_sizes: List[int] = []
|
|
183
|
+
for group_config in self._grouped_embedding_configs:
|
|
184
|
+
id_list_feature_hash_sizes.extend(group_config.feature_hash_sizes())
|
|
185
|
+
return id_list_feature_hash_sizes
|
|
186
|
+
|
|
187
|
+
def _get_id_score_list_features_hash_sizes(self) -> List[int]:
|
|
188
|
+
id_score_list_feature_hash_sizes: List[int] = []
|
|
189
|
+
for group_config in self._score_grouped_embedding_configs:
|
|
190
|
+
id_score_list_feature_hash_sizes.extend(group_config.feature_hash_sizes())
|
|
191
|
+
return id_score_list_feature_hash_sizes
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class RwSparseFeaturesDist(BaseSparseFeaturesDist[SparseFeatures]):
|
|
195
|
+
"""
|
|
196
|
+
Bucketizes sparse features in RW fashion and then redistributes with an AlltoAll
|
|
197
|
+
collective operation.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
pg (dist.ProcessGroup): ProcessGroup for AlltoAll communication.
|
|
201
|
+
intra_pg (dist.ProcessGroup): ProcessGroup within single host group for AlltoAll
|
|
202
|
+
communication.
|
|
203
|
+
num_id_list_features (int): total number of id list features.
|
|
204
|
+
num_id_score_list_features (int): total number of id score list features
|
|
205
|
+
id_list_feature_hash_sizes (List[int]): hash sizes of id list features.
|
|
206
|
+
id_score_list_feature_hash_sizes (List[int]): hash sizes of id score list
|
|
207
|
+
features.
|
|
208
|
+
device (Optional[torch.device]): device on which buffers will be allocated.
|
|
209
|
+
is_sequence (bool): if this is for a sequence embedding.
|
|
210
|
+
has_feature_processor (bool): existence of feature processor (ie. position
|
|
211
|
+
weighted features).
|
|
212
|
+
|
|
213
|
+
"""
|
|
214
|
+
|
|
215
|
+
def __init__(
|
|
216
|
+
self,
|
|
217
|
+
pg: dist.ProcessGroup,
|
|
218
|
+
num_id_list_features: int,
|
|
219
|
+
num_id_score_list_features: int,
|
|
220
|
+
id_list_feature_hash_sizes: List[int],
|
|
221
|
+
id_score_list_feature_hash_sizes: List[int],
|
|
222
|
+
device: Optional[torch.device] = None,
|
|
223
|
+
is_sequence: bool = False,
|
|
224
|
+
has_feature_processor: bool = False,
|
|
225
|
+
) -> None:
|
|
226
|
+
super().__init__()
|
|
227
|
+
self._world_size: int = pg.size()
|
|
228
|
+
self._num_id_list_features = num_id_list_features
|
|
229
|
+
self._num_id_score_list_features = num_id_score_list_features
|
|
230
|
+
id_list_feature_block_sizes = [
|
|
231
|
+
(hash_size + self._world_size - 1) // self._world_size
|
|
232
|
+
for hash_size in id_list_feature_hash_sizes
|
|
233
|
+
]
|
|
234
|
+
id_score_list_feature_block_sizes = [
|
|
235
|
+
(hash_size + self._world_size - 1) // self._world_size
|
|
236
|
+
for hash_size in id_score_list_feature_hash_sizes
|
|
237
|
+
]
|
|
238
|
+
self.register_buffer(
|
|
239
|
+
"_id_list_feature_block_sizes_tensor",
|
|
240
|
+
torch.tensor(
|
|
241
|
+
id_list_feature_block_sizes,
|
|
242
|
+
device=device,
|
|
243
|
+
dtype=torch.int32,
|
|
244
|
+
),
|
|
245
|
+
)
|
|
246
|
+
self.register_buffer(
|
|
247
|
+
"_id_score_list_feature_block_sizes_tensor",
|
|
248
|
+
torch.tensor(
|
|
249
|
+
id_score_list_feature_block_sizes,
|
|
250
|
+
device=device,
|
|
251
|
+
dtype=torch.int32,
|
|
252
|
+
),
|
|
253
|
+
)
|
|
254
|
+
self._dist = SparseFeaturesAllToAll(
|
|
255
|
+
pg=pg,
|
|
256
|
+
id_list_features_per_rank=self._world_size * [self._num_id_list_features],
|
|
257
|
+
id_score_list_features_per_rank=self._world_size
|
|
258
|
+
* [self._num_id_score_list_features],
|
|
259
|
+
device=device,
|
|
260
|
+
)
|
|
261
|
+
self._is_sequence = is_sequence
|
|
262
|
+
self._has_feature_processor = has_feature_processor
|
|
263
|
+
self.unbucketize_permute_tensor: Optional[torch.Tensor] = None
|
|
264
|
+
|
|
265
|
+
def forward(
|
|
266
|
+
self,
|
|
267
|
+
sparse_features: SparseFeatures,
|
|
268
|
+
) -> Awaitable[Awaitable[SparseFeatures]]:
|
|
269
|
+
"""
|
|
270
|
+
Bucketizes sparse feature values into world size number of buckets and then
|
|
271
|
+
performs AlltoAll operation.
|
|
272
|
+
|
|
273
|
+
Args:
|
|
274
|
+
sparse_features (SparseFeatures): sparse features to bucketize and
|
|
275
|
+
redistribute.
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
Awaitable[SparseFeatures]: awaitable of SparseFeatures.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
if self._num_id_list_features > 0:
|
|
282
|
+
assert sparse_features.id_list_features is not None
|
|
283
|
+
(
|
|
284
|
+
id_list_features,
|
|
285
|
+
self.unbucketize_permute_tensor,
|
|
286
|
+
) = bucketize_kjt_before_all2all(
|
|
287
|
+
sparse_features.id_list_features,
|
|
288
|
+
num_buckets=self._world_size,
|
|
289
|
+
block_sizes=self._id_list_feature_block_sizes_tensor,
|
|
290
|
+
output_permute=self._is_sequence,
|
|
291
|
+
bucketize_pos=self._has_feature_processor,
|
|
292
|
+
)
|
|
293
|
+
else:
|
|
294
|
+
id_list_features = None
|
|
295
|
+
|
|
296
|
+
if self._num_id_score_list_features > 0:
|
|
297
|
+
assert sparse_features.id_score_list_features is not None
|
|
298
|
+
id_score_list_features, _ = bucketize_kjt_before_all2all(
|
|
299
|
+
sparse_features.id_score_list_features,
|
|
300
|
+
num_buckets=self._world_size,
|
|
301
|
+
block_sizes=self._id_score_list_feature_block_sizes_tensor,
|
|
302
|
+
output_permute=False,
|
|
303
|
+
bucketize_pos=False,
|
|
304
|
+
)
|
|
305
|
+
else:
|
|
306
|
+
id_score_list_features = None
|
|
307
|
+
|
|
308
|
+
bucketized_sparse_features = SparseFeatures(
|
|
309
|
+
id_list_features=id_list_features,
|
|
310
|
+
id_score_list_features=id_score_list_features,
|
|
311
|
+
)
|
|
312
|
+
return self._dist(bucketized_sparse_features)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
class RwPooledEmbeddingDist(BaseEmbeddingDist[torch.Tensor]):
|
|
316
|
+
"""
|
|
317
|
+
Redistributes pooled embedding tensor in RW fashion by performing a reduce-scatter
|
|
318
|
+
operation.
|
|
319
|
+
|
|
320
|
+
Args:
|
|
321
|
+
pg (dist.ProcessGroup): ProcessGroup for reduce-scatter communication.
|
|
322
|
+
"""
|
|
323
|
+
|
|
324
|
+
def __init__(
|
|
325
|
+
self,
|
|
326
|
+
pg: dist.ProcessGroup,
|
|
327
|
+
) -> None:
|
|
328
|
+
super().__init__()
|
|
329
|
+
self._dist = PooledEmbeddingsReduceScatter(pg)
|
|
330
|
+
|
|
331
|
+
def forward(self, local_embs: torch.Tensor) -> Awaitable[torch.Tensor]:
|
|
332
|
+
"""
|
|
333
|
+
Performs reduce-scatter pooled operation on pooled embeddings tensor.
|
|
334
|
+
|
|
335
|
+
Args:
|
|
336
|
+
local_embs (torch.Tensor): pooled embeddings tensor to distribute.
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
Awaitable[torch.Tensor]: awaitable of pooled embeddings tensor.
|
|
340
|
+
"""
|
|
341
|
+
|
|
342
|
+
return self._dist(local_embs)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class RwPooledEmbeddingSharding(BaseRwEmbeddingSharding[SparseFeatures, torch.Tensor]):
|
|
346
|
+
"""
|
|
347
|
+
Shards embedding bags row-wise, i.e.. a given embedding table is evenly distributed
|
|
348
|
+
by rows and table slices are placed on all ranks.
|
|
349
|
+
"""
|
|
350
|
+
|
|
351
|
+
def create_input_dist(
|
|
352
|
+
self,
|
|
353
|
+
device: Optional[torch.device] = None,
|
|
354
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
355
|
+
num_id_list_features = self._get_id_list_features_num()
|
|
356
|
+
num_id_score_list_features = self._get_id_score_list_features_num()
|
|
357
|
+
id_list_feature_hash_sizes = self._get_id_list_features_hash_sizes()
|
|
358
|
+
id_score_list_feature_hash_sizes = self._get_id_score_list_features_hash_sizes()
|
|
359
|
+
return RwSparseFeaturesDist(
|
|
360
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
361
|
+
# `Optional[ProcessGroup]`.
|
|
362
|
+
pg=self._pg,
|
|
363
|
+
num_id_list_features=num_id_list_features,
|
|
364
|
+
num_id_score_list_features=num_id_score_list_features,
|
|
365
|
+
id_list_feature_hash_sizes=id_list_feature_hash_sizes,
|
|
366
|
+
id_score_list_feature_hash_sizes=id_score_list_feature_hash_sizes,
|
|
367
|
+
device=device if device is not None else self._device,
|
|
368
|
+
is_sequence=False,
|
|
369
|
+
has_feature_processor=self._has_feature_processor,
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
def create_lookup(
|
|
373
|
+
self,
|
|
374
|
+
device: Optional[torch.device] = None,
|
|
375
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
376
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
377
|
+
) -> BaseEmbeddingLookup:
|
|
378
|
+
return GroupedPooledEmbeddingsLookup(
|
|
379
|
+
grouped_configs=self._grouped_embedding_configs,
|
|
380
|
+
grouped_score_configs=self._score_grouped_embedding_configs,
|
|
381
|
+
fused_params=fused_params,
|
|
382
|
+
pg=self._pg,
|
|
383
|
+
device=device if device is not None else self._device,
|
|
384
|
+
feature_processor=feature_processor,
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
def create_output_dist(
|
|
388
|
+
self,
|
|
389
|
+
device: Optional[torch.device] = None,
|
|
390
|
+
) -> BaseEmbeddingDist[torch.Tensor]:
|
|
391
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
392
|
+
# `Optional[ProcessGroup]`.
|
|
393
|
+
return RwPooledEmbeddingDist(self._pg)
|
|
@@ -0,0 +1,69 @@
|
|
|
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 abc
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import List, Optional, TypeVar
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
import torch.distributed as dist # noqa
|
|
14
|
+
from torchrec.distributed.embedding_sharding import BaseEmbeddingDist
|
|
15
|
+
from torchrec.distributed.types import Awaitable
|
|
16
|
+
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
|
|
17
|
+
from torchrec.streamable import Multistreamable
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class SequenceShardingContext(Multistreamable):
|
|
22
|
+
"""
|
|
23
|
+
Stores KJTAllToAll context and reuses it in SequenceEmbeddingsAllToAll.
|
|
24
|
+
SequenceEmbeddingsAllToAll has the same comm pattern as KJTAllToAll.
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
features_before_input_dist (Optional[KeyedJaggedTensor]): stores the original
|
|
28
|
+
KJT before input dist.
|
|
29
|
+
input_splits(List[int]): stores the input splits of KJT AlltoAll.
|
|
30
|
+
output_splits (List[int]): stores the output splits of KJT AlltoAll.
|
|
31
|
+
unbucketize_permute_tensor (Optional[torch.Tensor]): stores the permute order of
|
|
32
|
+
KJT bucketize (for row-wise sharding only).
|
|
33
|
+
lengths_after_input_dist (Optional[torch.Tensor]): stores the KJT length after
|
|
34
|
+
input dist.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
features_before_input_dist: Optional[KeyedJaggedTensor] = None
|
|
38
|
+
input_splits: List[int] = field(default_factory=list)
|
|
39
|
+
output_splits: List[int] = field(default_factory=list)
|
|
40
|
+
unbucketize_permute_tensor: Optional[torch.Tensor] = None
|
|
41
|
+
lengths_after_input_dist: Optional[torch.Tensor] = None
|
|
42
|
+
|
|
43
|
+
def record_stream(self, stream: torch.cuda.streams.Stream) -> None:
|
|
44
|
+
if self.features_before_input_dist is not None:
|
|
45
|
+
self.features_before_input_dist.record_stream(stream)
|
|
46
|
+
if self.unbucketize_permute_tensor is not None:
|
|
47
|
+
# pyre-fixme[6]: For 1st param expected `Stream` but got `Stream`.
|
|
48
|
+
self.unbucketize_permute_tensor.record_stream(stream)
|
|
49
|
+
if self.lengths_after_input_dist is not None:
|
|
50
|
+
# pyre-fixme[6]: For 1st param expected `Stream` but got `Stream`.
|
|
51
|
+
self.lengths_after_input_dist.record_stream(stream)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
T = TypeVar("T")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class BaseSequenceEmbeddingDist(BaseEmbeddingDist[T]):
|
|
58
|
+
"""
|
|
59
|
+
Base class for converting output of the sequence embedding lookup from
|
|
60
|
+
model-parallel to data-parallel.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
@abc.abstractmethod
|
|
64
|
+
def forward(
|
|
65
|
+
self,
|
|
66
|
+
local_embs: T,
|
|
67
|
+
sharding_ctx: SequenceShardingContext,
|
|
68
|
+
) -> Awaitable[torch.Tensor]:
|
|
69
|
+
pass
|
|
@@ -0,0 +1,211 @@
|
|
|
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, List, Optional
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
import torch.distributed as dist
|
|
12
|
+
from torchrec.distributed.dist_data import (
|
|
13
|
+
EmbeddingsAllToOne,
|
|
14
|
+
SequenceEmbeddingsAllToAll,
|
|
15
|
+
)
|
|
16
|
+
from torchrec.distributed.embedding_lookup import (
|
|
17
|
+
GroupedEmbeddingsLookup,
|
|
18
|
+
InferGroupedEmbeddingsLookup,
|
|
19
|
+
)
|
|
20
|
+
from torchrec.distributed.embedding_sharding import (
|
|
21
|
+
BaseEmbeddingDist,
|
|
22
|
+
BaseEmbeddingLookup,
|
|
23
|
+
BaseSparseFeaturesDist,
|
|
24
|
+
)
|
|
25
|
+
from torchrec.distributed.embedding_types import (
|
|
26
|
+
BaseGroupedFeatureProcessor,
|
|
27
|
+
SparseFeatures,
|
|
28
|
+
SparseFeaturesList,
|
|
29
|
+
)
|
|
30
|
+
from torchrec.distributed.sharding.sequence_sharding import (
|
|
31
|
+
BaseSequenceEmbeddingDist,
|
|
32
|
+
SequenceShardingContext,
|
|
33
|
+
)
|
|
34
|
+
from torchrec.distributed.sharding.tw_sharding import (
|
|
35
|
+
BaseTwEmbeddingSharding,
|
|
36
|
+
InferTwSparseFeaturesDist,
|
|
37
|
+
TwSparseFeaturesDist,
|
|
38
|
+
)
|
|
39
|
+
from torchrec.distributed.types import Awaitable
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class InferTwSequenceEmbeddingDist(BaseSequenceEmbeddingDist[List[torch.Tensor]]):
|
|
43
|
+
"""
|
|
44
|
+
Redistributes sequence embedding tensor in hierarchical fashion with an AlltoOne
|
|
45
|
+
operation.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
device (torch.device): device on which the tensors will be communicated to.
|
|
49
|
+
world_size (int): number of devices in the topology.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
device: torch.device,
|
|
55
|
+
world_size: int,
|
|
56
|
+
) -> None:
|
|
57
|
+
super().__init__()
|
|
58
|
+
self._dist: EmbeddingsAllToOne = EmbeddingsAllToOne(device, world_size, 0)
|
|
59
|
+
|
|
60
|
+
def forward(
|
|
61
|
+
self,
|
|
62
|
+
local_embs: List[torch.Tensor],
|
|
63
|
+
sharding_ctx: SequenceShardingContext,
|
|
64
|
+
) -> Awaitable[torch.Tensor]:
|
|
65
|
+
"""
|
|
66
|
+
Performs AlltoOne operation on sequence embeddings tensor.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
sharding_ctx (SequenceShardingContext): shared context from KJTAllToOne
|
|
70
|
+
operation.
|
|
71
|
+
local_embs (torch.Tensor): tensor of values to distribute.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Awaitable[torch.Tensor]: awaitable of sequence embeddings.
|
|
75
|
+
"""
|
|
76
|
+
return self._dist.forward(local_embs)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class TwSequenceEmbeddingDist(BaseSequenceEmbeddingDist[torch.Tensor]):
|
|
80
|
+
"""
|
|
81
|
+
Redistributes sequence embedding tensor in TW fashion with an AlltoAll operation.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
pg (dist.ProcessGroup): ProcessGroup for AlltoAll communication.
|
|
85
|
+
features_per_rank (List[int]): number of features (sum of dimensions) of the
|
|
86
|
+
embedding for each rank.
|
|
87
|
+
device (Optional[torch.device]): device on which buffers will be allocated.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
pg: dist.ProcessGroup,
|
|
93
|
+
features_per_rank: List[int],
|
|
94
|
+
device: Optional[torch.device] = None,
|
|
95
|
+
) -> None:
|
|
96
|
+
super().__init__()
|
|
97
|
+
self._dist = SequenceEmbeddingsAllToAll(pg, features_per_rank, device)
|
|
98
|
+
|
|
99
|
+
def forward(
|
|
100
|
+
self,
|
|
101
|
+
local_embs: torch.Tensor,
|
|
102
|
+
sharding_ctx: SequenceShardingContext,
|
|
103
|
+
) -> Awaitable[torch.Tensor]:
|
|
104
|
+
"""
|
|
105
|
+
Performs AlltoAll operation on sequence embeddings tensor.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
local_embs (torch.Tensor): tensor of values to distribute.
|
|
109
|
+
sharding_ctx (SequenceShardingContext): shared context from KJTAllToAll
|
|
110
|
+
operation.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
Awaitable[torch.Tensor]: awaitable of sequence embeddings.
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
return self._dist(
|
|
117
|
+
local_embs,
|
|
118
|
+
lengths=sharding_ctx.lengths_after_input_dist,
|
|
119
|
+
input_splits=sharding_ctx.input_splits,
|
|
120
|
+
output_splits=sharding_ctx.output_splits,
|
|
121
|
+
unbucketize_permute_tensor=None,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class TwSequenceEmbeddingSharding(
|
|
126
|
+
BaseTwEmbeddingSharding[SparseFeatures, torch.Tensor]
|
|
127
|
+
):
|
|
128
|
+
"""
|
|
129
|
+
Shards sequence (unpooled) embedding table-wise, i.e.. a given embedding table is
|
|
130
|
+
placed entirely on a selected rank.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
def create_input_dist(
|
|
134
|
+
self,
|
|
135
|
+
device: Optional[torch.device] = None,
|
|
136
|
+
) -> BaseSparseFeaturesDist[SparseFeatures]:
|
|
137
|
+
return TwSparseFeaturesDist(
|
|
138
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
139
|
+
# `Optional[ProcessGroup]`.
|
|
140
|
+
self._pg,
|
|
141
|
+
self._id_list_features_per_rank(),
|
|
142
|
+
self._id_score_list_features_per_rank(),
|
|
143
|
+
device if device is not None else self._device,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def create_lookup(
|
|
147
|
+
self,
|
|
148
|
+
device: Optional[torch.device] = None,
|
|
149
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
150
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
151
|
+
) -> BaseEmbeddingLookup:
|
|
152
|
+
assert feature_processor is None
|
|
153
|
+
return GroupedEmbeddingsLookup(
|
|
154
|
+
grouped_configs=self._grouped_embedding_configs,
|
|
155
|
+
fused_params=fused_params,
|
|
156
|
+
pg=self._pg,
|
|
157
|
+
device=device if device is not None else self._device,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def create_output_dist(
|
|
161
|
+
self,
|
|
162
|
+
device: Optional[torch.device] = None,
|
|
163
|
+
) -> BaseSequenceEmbeddingDist[torch.Tensor]:
|
|
164
|
+
return TwSequenceEmbeddingDist(
|
|
165
|
+
# pyre-fixme[6]: For 1st param expected `ProcessGroup` but got
|
|
166
|
+
# `Optional[ProcessGroup]`.
|
|
167
|
+
self._pg,
|
|
168
|
+
self._id_list_features_per_rank(),
|
|
169
|
+
device if device is not None else self._device,
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class InferTwSequenceEmbeddingSharding(
|
|
174
|
+
BaseTwEmbeddingSharding[SparseFeaturesList, List[torch.Tensor]]
|
|
175
|
+
):
|
|
176
|
+
"""
|
|
177
|
+
Shards sequence (unpooled) embedding table-wise, i.e.. a given embedding table is
|
|
178
|
+
placed entirely on a selected rank, for inference.
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
def create_input_dist(
|
|
182
|
+
self, device: Optional[torch.device] = None
|
|
183
|
+
) -> BaseSparseFeaturesDist[SparseFeaturesList]:
|
|
184
|
+
return InferTwSparseFeaturesDist(
|
|
185
|
+
self._id_list_features_per_rank(),
|
|
186
|
+
self._id_score_list_features_per_rank(),
|
|
187
|
+
self._world_size,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def create_lookup(
|
|
191
|
+
self,
|
|
192
|
+
device: Optional[torch.device] = None,
|
|
193
|
+
fused_params: Optional[Dict[str, Any]] = None,
|
|
194
|
+
feature_processor: Optional[BaseGroupedFeatureProcessor] = None,
|
|
195
|
+
) -> BaseEmbeddingLookup[SparseFeaturesList, List[torch.Tensor]]:
|
|
196
|
+
return InferGroupedEmbeddingsLookup(
|
|
197
|
+
grouped_configs_per_rank=self._grouped_embedding_configs_per_rank,
|
|
198
|
+
world_size=self._world_size,
|
|
199
|
+
fused_params=fused_params,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
def create_output_dist(
|
|
203
|
+
self,
|
|
204
|
+
device: Optional[torch.device] = None,
|
|
205
|
+
) -> BaseEmbeddingDist[List[torch.Tensor]]:
|
|
206
|
+
device = device if device is not None else self._device
|
|
207
|
+
return InferTwSequenceEmbeddingDist(
|
|
208
|
+
# pyre-fixme [6]
|
|
209
|
+
device,
|
|
210
|
+
self._world_size,
|
|
211
|
+
)
|