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,930 @@
|
|
|
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 dataclasses import dataclass, field
|
|
9
|
+
from typing import Any, List, Optional, Tuple, TypeVar
|
|
10
|
+
|
|
11
|
+
import torch
|
|
12
|
+
import torch.distributed as dist
|
|
13
|
+
from torch import Tensor
|
|
14
|
+
from torch.autograd import Function
|
|
15
|
+
from torch.autograd.profiler import record_function
|
|
16
|
+
from torchrec.distributed.types import Awaitable, NoWait
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
torch.ops.load_library("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops")
|
|
20
|
+
torch.ops.load_library("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpu")
|
|
21
|
+
except OSError:
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# OSS
|
|
26
|
+
try:
|
|
27
|
+
import fbgemm_gpu # @manual # noqa
|
|
28
|
+
except ImportError:
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
W = TypeVar("W")
|
|
33
|
+
|
|
34
|
+
# TODO: T96382816, NE Parity Backward compatibility
|
|
35
|
+
GRADIENT_DIVISION: bool = True
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def set_gradient_division(val: bool) -> None:
|
|
39
|
+
global GRADIENT_DIVISION
|
|
40
|
+
GRADIENT_DIVISION = val
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
Some commonly used notations for comm ops:
|
|
45
|
+
B - batch size
|
|
46
|
+
T - number of embedding tables
|
|
47
|
+
D - embedding dimension
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Request(Awaitable[W]):
|
|
52
|
+
"""
|
|
53
|
+
Defines a collective operation request for a process group on a tensor.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
pg (dist.ProcessGroup): The process group the request is for.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
def __init__(self, pg: dist.ProcessGroup) -> None:
|
|
60
|
+
super().__init__()
|
|
61
|
+
self.pg: dist.ProcessGroup = pg
|
|
62
|
+
# pyre-fixme[11]: Annotation dist.Work is not defined as a type.
|
|
63
|
+
self.req: Optional[dist.Work] = None
|
|
64
|
+
self.tensor: Optional[W] = None
|
|
65
|
+
self.a2ai = None # type: ignore
|
|
66
|
+
self.rsi = None # type: ignore
|
|
67
|
+
self.wait_function = None # type: ignore
|
|
68
|
+
|
|
69
|
+
def _wait_impl(self) -> W:
|
|
70
|
+
"""
|
|
71
|
+
Calls the wait function for this request.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
ret = self.wait_function.apply(self.pg, self, self.tensor)
|
|
75
|
+
self.req = None
|
|
76
|
+
self.tensor = None
|
|
77
|
+
return ret
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass
|
|
81
|
+
class All2AllPooledInfo(object):
|
|
82
|
+
"""
|
|
83
|
+
The data class that collects the attributes when calling the `alltoall_pooled`
|
|
84
|
+
operation.
|
|
85
|
+
|
|
86
|
+
Attributes:
|
|
87
|
+
batch_size_per_rank (List[int]): batch size in each rank
|
|
88
|
+
dim_sum_per_rank (List[int]): number of features (sum of dimensions) of the
|
|
89
|
+
embedding in each rank.
|
|
90
|
+
dim_sum_per_rank_tensor (Optional[Tensor]): the tensor version of
|
|
91
|
+
`dim_sum_per_rank`, this is only used by the fast kernel of
|
|
92
|
+
`_recat_pooled_embedding_grad_out`.
|
|
93
|
+
cumsum_dim_sum_per_rank_tensor (Optional[Tensor]): cumulative sum of
|
|
94
|
+
`dim_sum_per_rank`, this is only used by the fast kernel of
|
|
95
|
+
`_recat_pooled_embedding_grad_out`.
|
|
96
|
+
B_local (int): local batch size before scattering.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
batch_size_per_rank: List[int]
|
|
100
|
+
dim_sum_per_rank: List[int]
|
|
101
|
+
dim_sum_per_rank_tensor: Optional[Tensor]
|
|
102
|
+
cumsum_dim_sum_per_rank_tensor: Optional[Tensor]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class All2AllSequenceInfo(object):
|
|
107
|
+
"""
|
|
108
|
+
The data class that collects the attributes when calling the `alltoall_sequence`
|
|
109
|
+
operation.
|
|
110
|
+
|
|
111
|
+
Attributes:
|
|
112
|
+
embedding_dim (int): embedding dimension.
|
|
113
|
+
lengths_after_sparse_data_all2all (Tensor): lengths of sparse features after
|
|
114
|
+
AlltoAll.
|
|
115
|
+
forward_recat_tensor (Tensor): recat tensor for forward.
|
|
116
|
+
backward_recat_tensor (Tensor): recat tensor for backward.
|
|
117
|
+
input_splits (List[int]): input splits.
|
|
118
|
+
output_splits (List[int]): output splits.
|
|
119
|
+
lengths_sparse_before_features_all2all (Optional[Tensor]): lengths of sparse
|
|
120
|
+
features before AlltoAll.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
embedding_dim: int
|
|
124
|
+
lengths_after_sparse_data_all2all: Tensor
|
|
125
|
+
forward_recat_tensor: Tensor
|
|
126
|
+
backward_recat_tensor: Tensor
|
|
127
|
+
input_splits: List[int]
|
|
128
|
+
output_splits: List[int]
|
|
129
|
+
permuted_lengths_after_sparse_data_all2all: Optional[Tensor] = None
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@dataclass
|
|
133
|
+
class All2AllVInfo(object):
|
|
134
|
+
"""
|
|
135
|
+
The data class that collects the attributes when calling the `alltoallv` operation.
|
|
136
|
+
|
|
137
|
+
Attributes:
|
|
138
|
+
dim_sum_per_rank (List[int]): number of features (sum of dimensions) of the
|
|
139
|
+
embedding in each rank.
|
|
140
|
+
B_global (int): global batch size for each rank.
|
|
141
|
+
B_local (int): local batch size before scattering.
|
|
142
|
+
B_local_list: (List[int]): local batch sizes for each embedding table locally
|
|
143
|
+
(in my current rank).
|
|
144
|
+
D_local_list (List[int]): embedding dimension of each embedding table locally
|
|
145
|
+
(in my current rank).
|
|
146
|
+
input_split_sizes (List[int]): The input split sizes for each rank, this
|
|
147
|
+
remembers how to split the input when doing the `all_to_all_single` operation.
|
|
148
|
+
output_split_sizes (List[int]): The output split sizes for each rank, this
|
|
149
|
+
remembers how to fill the output when doing the `all_to_all_single` operation.
|
|
150
|
+
"""
|
|
151
|
+
|
|
152
|
+
dims_sum_per_rank: List[int]
|
|
153
|
+
B_global: int
|
|
154
|
+
B_local: int
|
|
155
|
+
B_local_list: List[int]
|
|
156
|
+
D_local_list: List[int]
|
|
157
|
+
input_split_sizes: List[int] = field(default_factory=list)
|
|
158
|
+
output_split_sizes: List[int] = field(default_factory=list)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@dataclass
|
|
162
|
+
class ReduceScatterInfo(object):
|
|
163
|
+
"""
|
|
164
|
+
The data class that collects the attributes when calling the `reduce_scatter_pooled`
|
|
165
|
+
operation.
|
|
166
|
+
|
|
167
|
+
Attributes:
|
|
168
|
+
input_sizes (List[int]): the sizes of the input tensors. This remembers the
|
|
169
|
+
sizes of the input tensors when running the backward pass and producing the
|
|
170
|
+
gradient.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
input_sizes: List[int]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@dataclass
|
|
177
|
+
class All2AllDenseInfo(object):
|
|
178
|
+
"""
|
|
179
|
+
The data class that collects the attributes when calling the alltoall_dense
|
|
180
|
+
operation.
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
output_splits: List[int]
|
|
184
|
+
batch_size: int
|
|
185
|
+
input_shape: List[int]
|
|
186
|
+
input_splits: List[int]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _get_split_lengths_by_len(
|
|
190
|
+
world_size: int, my_rank: int, n: int
|
|
191
|
+
) -> Tuple[int, List[int]]:
|
|
192
|
+
k, m = divmod(n, world_size)
|
|
193
|
+
if m == 0:
|
|
194
|
+
splits = [k] * world_size
|
|
195
|
+
my_len = k
|
|
196
|
+
else:
|
|
197
|
+
splits = [(k + 1) if i < m else k for i in range(world_size)]
|
|
198
|
+
my_len = splits[my_rank]
|
|
199
|
+
return (my_len, splits)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def alltoall_pooled(
|
|
203
|
+
a2a_pooled_embs_tensor: Tensor,
|
|
204
|
+
batch_size_per_rank: List[int],
|
|
205
|
+
dim_sum_per_rank: List[int],
|
|
206
|
+
dim_sum_per_rank_tensor: Optional[Tensor] = None,
|
|
207
|
+
cumsum_dim_sum_per_rank_tensor: Optional[Tensor] = None,
|
|
208
|
+
group: Optional[dist.ProcessGroup] = None,
|
|
209
|
+
) -> Awaitable[Tensor]:
|
|
210
|
+
"""
|
|
211
|
+
Performs AlltoAll operation for a single pooled embedding tensor. Each process
|
|
212
|
+
splits the input pooled embeddings tensor based on the world size, and then scatters
|
|
213
|
+
the split list to all processes in the group. Then concatenates the received tensors
|
|
214
|
+
from all processes in the group and returns a single output tensor.
|
|
215
|
+
|
|
216
|
+
Args:
|
|
217
|
+
a2a_pooled_embs_tensor (Tensor): input pooled embeddings. Must be pooled
|
|
218
|
+
together before passing into this function. Its shape is B x D_local_sum,
|
|
219
|
+
where D_local_sum is the dimension sum of all the local
|
|
220
|
+
embedding tables.
|
|
221
|
+
batch_size_per_rank (List[int]): batch size in each rank.
|
|
222
|
+
dim_sum_per_rank (List[int]): number of features (sum of dimensions) of the
|
|
223
|
+
embedding in each rank.
|
|
224
|
+
dim_sum_per_rank_tensor (Optional[Tensor]): the tensor version of
|
|
225
|
+
`dim_sum_per_rank`, this is only used by the fast kernel of
|
|
226
|
+
`_recat_pooled_embedding_grad_out`.
|
|
227
|
+
cumsum_dim_sum_per_rank_tensor (Optional[Tensor]): cumulative sum of
|
|
228
|
+
`dim_sum_per_rank`, this is only used by the fast kernel of
|
|
229
|
+
`_recat_pooled_embedding_grad_out`.
|
|
230
|
+
group (Optional[dist.ProcessGroup]): The process group to work on. If None, the
|
|
231
|
+
default process group will be used.
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
Awaitable[List[Tensor]]: async work handle (`Awaitable`), which can be `wait()` later to get the resulting tensor.
|
|
235
|
+
|
|
236
|
+
.. warning::
|
|
237
|
+
`alltoall_pooled` is experimental and subject to change.
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
if group is None:
|
|
241
|
+
group = dist.distributed_c10d._get_default_group()
|
|
242
|
+
|
|
243
|
+
if dist.get_world_size(group) <= 1:
|
|
244
|
+
return NoWait(a2a_pooled_embs_tensor)
|
|
245
|
+
|
|
246
|
+
myreq = Request(group)
|
|
247
|
+
a2ai = All2AllPooledInfo(
|
|
248
|
+
batch_size_per_rank=batch_size_per_rank,
|
|
249
|
+
dim_sum_per_rank=dim_sum_per_rank,
|
|
250
|
+
dim_sum_per_rank_tensor=dim_sum_per_rank_tensor,
|
|
251
|
+
cumsum_dim_sum_per_rank_tensor=cumsum_dim_sum_per_rank_tensor,
|
|
252
|
+
)
|
|
253
|
+
# pyre-fixme[16]: `All2All_Pooled_Req` has no attribute `apply`.
|
|
254
|
+
All2All_Pooled_Req.apply(group, myreq, a2ai, a2a_pooled_embs_tensor)
|
|
255
|
+
return myreq
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def alltoall_sequence(
|
|
259
|
+
# (T, B, L_i * D) flattened
|
|
260
|
+
a2a_sequence_embs_tensor: Tensor,
|
|
261
|
+
forward_recat_tensor: Tensor,
|
|
262
|
+
backward_recat_tensor: Tensor,
|
|
263
|
+
lengths_after_sparse_data_all2all: Tensor,
|
|
264
|
+
input_splits: List[int],
|
|
265
|
+
output_splits: List[int],
|
|
266
|
+
group: Optional[dist.ProcessGroup] = None,
|
|
267
|
+
) -> Awaitable[Tensor]:
|
|
268
|
+
"""
|
|
269
|
+
Performs AlltoAll operation for sequence embeddings. Each process splits the input
|
|
270
|
+
tensor based on the world size, and then scatters the split list to all processes in
|
|
271
|
+
the group. Then concatenates the received tensors from all processes in the group
|
|
272
|
+
and returns a single output tensor.
|
|
273
|
+
|
|
274
|
+
NOTE:
|
|
275
|
+
AlltoAll operator for (T * B * L_i, D) tensors.
|
|
276
|
+
Does not support mixed dimensions.
|
|
277
|
+
|
|
278
|
+
Args:
|
|
279
|
+
a2a_sequence_embs_tensor (Tensor): input embeddings. Usually with the shape of
|
|
280
|
+
(T * B * L_i, D), where B - batch size, T - number of embedding tables,
|
|
281
|
+
D - embedding dimension.
|
|
282
|
+
forward_recat_tensor (Tensor): recat tensor for forward.
|
|
283
|
+
backward_recat_tensor (Tensor): recat tensor for backward.
|
|
284
|
+
lengths_after_sparse_data_all2all (Tensor): lengths of sparse features after
|
|
285
|
+
AlltoAll.
|
|
286
|
+
input_splits (Tensor): input splits.
|
|
287
|
+
output_splits (Tensor): output splits.
|
|
288
|
+
group (Optional[dist.ProcessGroup]): The process group to work on. If None, the
|
|
289
|
+
default process group will be used.
|
|
290
|
+
|
|
291
|
+
Returns:
|
|
292
|
+
Awaitable[List[Tensor]]: async work handle (`Awaitable`), which can be `wait()` later to get the resulting tensor.
|
|
293
|
+
|
|
294
|
+
.. warning::
|
|
295
|
+
`alltoall_sequence` is experimental and subject to change.
|
|
296
|
+
"""
|
|
297
|
+
|
|
298
|
+
if group is None:
|
|
299
|
+
group = dist.distributed_c10d._get_default_group()
|
|
300
|
+
|
|
301
|
+
if dist.get_world_size(group) <= 1:
|
|
302
|
+
return NoWait(a2a_sequence_embs_tensor)
|
|
303
|
+
|
|
304
|
+
myreq = Request(group)
|
|
305
|
+
a2ai = All2AllSequenceInfo(
|
|
306
|
+
embedding_dim=a2a_sequence_embs_tensor.shape[1],
|
|
307
|
+
lengths_after_sparse_data_all2all=lengths_after_sparse_data_all2all,
|
|
308
|
+
forward_recat_tensor=forward_recat_tensor,
|
|
309
|
+
backward_recat_tensor=backward_recat_tensor,
|
|
310
|
+
input_splits=input_splits,
|
|
311
|
+
output_splits=output_splits,
|
|
312
|
+
)
|
|
313
|
+
# sequence of embeddings, bags are definitely non-uniform
|
|
314
|
+
|
|
315
|
+
# pyre-fixme[16]: `All2All_Seq_Req` has no attribute `apply`.
|
|
316
|
+
All2All_Seq_Req.apply(group, myreq, a2ai, a2a_sequence_embs_tensor)
|
|
317
|
+
return myreq
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def alltoallv(
|
|
321
|
+
inputs: List[Tensor],
|
|
322
|
+
out_split: Optional[List[int]] = None,
|
|
323
|
+
per_rank_split_lengths: Optional[List[int]] = None,
|
|
324
|
+
group: Optional[dist.ProcessGroup] = None,
|
|
325
|
+
) -> Awaitable[List[Tensor]]:
|
|
326
|
+
"""
|
|
327
|
+
Performs `alltoallv` operation for a list of input embeddings. Each process scatters
|
|
328
|
+
the list to all processes in the group.
|
|
329
|
+
|
|
330
|
+
Args:
|
|
331
|
+
input (List[Tensor]): list of tensors to scatter, one per rank. The tensors in
|
|
332
|
+
the list usually have different lengths.
|
|
333
|
+
out_split (Optional[List[int]]): output split sizes (or dim_sum_per_rank), if
|
|
334
|
+
not specified, we will use `per_rank_split_lengths` to construct a output
|
|
335
|
+
split with the assumption that all the embs have the same dimension.
|
|
336
|
+
per_rank_split_lengths (Optional[List[int]]): split lengths per rank. If not
|
|
337
|
+
specified, the `out_split` must be specified.
|
|
338
|
+
group (Optional[dist.ProcessGroup]): The process group to work on. If None, the
|
|
339
|
+
default process group will be used.
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
Awaitable[List[Tensor]]: async work handle (`Awaitable`), which can be `wait()` later to get the resulting list of tensors.
|
|
343
|
+
|
|
344
|
+
.. warning::
|
|
345
|
+
`alltoallv` is experimental and subject to change.
|
|
346
|
+
"""
|
|
347
|
+
|
|
348
|
+
if group is None:
|
|
349
|
+
group = dist.distributed_c10d._get_default_group()
|
|
350
|
+
|
|
351
|
+
world_size = dist.get_world_size(group)
|
|
352
|
+
my_rank = dist.get_rank(group)
|
|
353
|
+
|
|
354
|
+
myreq = Request(group)
|
|
355
|
+
B_global, _ = inputs[0].size()
|
|
356
|
+
D_local_list = [e.size()[1] for e in inputs]
|
|
357
|
+
B_local, B_local_list = _get_split_lengths_by_len(world_size, my_rank, B_global)
|
|
358
|
+
|
|
359
|
+
if out_split is not None:
|
|
360
|
+
dims_sum_per_rank = out_split
|
|
361
|
+
elif per_rank_split_lengths is not None:
|
|
362
|
+
# all the embs have the same dimension
|
|
363
|
+
dims_sum_per_rank = [s * D_local_list[0] for s in per_rank_split_lengths]
|
|
364
|
+
else:
|
|
365
|
+
raise RuntimeError("Need to specify either out_split or per_rank_split_lengths")
|
|
366
|
+
|
|
367
|
+
a2ai = All2AllVInfo(
|
|
368
|
+
dims_sum_per_rank=dims_sum_per_rank,
|
|
369
|
+
B_local=B_local,
|
|
370
|
+
B_local_list=B_local_list,
|
|
371
|
+
D_local_list=D_local_list,
|
|
372
|
+
B_global=B_global,
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
# pyre-fixme[16]: `All2Allv_Req` has no attribute `apply`.
|
|
376
|
+
All2Allv_Req.apply(group, myreq, a2ai, inputs)
|
|
377
|
+
|
|
378
|
+
return myreq
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def reduce_scatter_pooled(
|
|
382
|
+
inputs: List[Tensor],
|
|
383
|
+
group: Optional[dist.ProcessGroup] = None,
|
|
384
|
+
) -> Awaitable[Tensor]:
|
|
385
|
+
"""
|
|
386
|
+
Performs reduce-scatter operation for a pooled embeddings tensor split into world
|
|
387
|
+
size number of chunks. The result of the reduce operation gets scattered to all
|
|
388
|
+
processes in the group. Then concatenates the received tensors from all processes in
|
|
389
|
+
the group and returns a single output tensor.
|
|
390
|
+
|
|
391
|
+
Args:
|
|
392
|
+
inputs (List[Tensor]): list of tensors to scatter, one per rank.
|
|
393
|
+
group (Optional[dist.ProcessGroup]): The process group to work on. If None, the
|
|
394
|
+
default process group will be used.
|
|
395
|
+
|
|
396
|
+
Returns:
|
|
397
|
+
Awaitable[List[Tensor]]: async work handle (Awaitable), which can be `wait()` later to get the resulting tensor.
|
|
398
|
+
|
|
399
|
+
.. warning::
|
|
400
|
+
`reduce_scatter_pooled` is experimental and subject to change.
|
|
401
|
+
"""
|
|
402
|
+
|
|
403
|
+
if group is None:
|
|
404
|
+
group = dist.distributed_c10d._get_default_group()
|
|
405
|
+
|
|
406
|
+
if dist.get_world_size(group) <= 1:
|
|
407
|
+
return NoWait(inputs[dist.get_rank(group)])
|
|
408
|
+
|
|
409
|
+
myreq = Request(group)
|
|
410
|
+
# pyre-fixme[6]: For 1st param expected `List[int]` but got `List[Size]`.
|
|
411
|
+
rsi = ReduceScatterInfo(input_sizes=[tensor.size() for tensor in inputs])
|
|
412
|
+
# pyre-fixme[16]: `ReduceScatter_Req` has no attribute `apply`.
|
|
413
|
+
ReduceScatter_Req.apply(group, myreq, rsi, *inputs)
|
|
414
|
+
return myreq
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
# TODO: improve performance of _recat_pooled_embedding_grad_out, see T87591139
|
|
418
|
+
def _recat_pooled_embedding_grad_out(
|
|
419
|
+
grad_output: Tensor, num_features_per_rank: List[int]
|
|
420
|
+
) -> Tensor:
|
|
421
|
+
grad_outputs_by_rank = grad_output.split(num_features_per_rank, dim=1)
|
|
422
|
+
return torch.cat(
|
|
423
|
+
[
|
|
424
|
+
grad_output_by_rank.contiguous().view(-1)
|
|
425
|
+
for grad_output_by_rank in grad_outputs_by_rank
|
|
426
|
+
],
|
|
427
|
+
dim=0,
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def _recat_seq_embedding(
|
|
432
|
+
input_embeddings: Tensor,
|
|
433
|
+
split_sizes: List[int],
|
|
434
|
+
T_local: int,
|
|
435
|
+
my_size: int,
|
|
436
|
+
forward: bool,
|
|
437
|
+
) -> Tensor:
|
|
438
|
+
seq_embeddings_by_rank = input_embeddings.split(split_sizes)
|
|
439
|
+
if forward:
|
|
440
|
+
return torch.cat(
|
|
441
|
+
[
|
|
442
|
+
seq_embeddings_by_rank[t * my_size + i]
|
|
443
|
+
# .contiguous().view(-1)
|
|
444
|
+
for i in range(my_size)
|
|
445
|
+
for t in range(T_local)
|
|
446
|
+
],
|
|
447
|
+
dim=0,
|
|
448
|
+
)
|
|
449
|
+
else:
|
|
450
|
+
return torch.cat(
|
|
451
|
+
[
|
|
452
|
+
seq_embeddings_by_rank[i * T_local + t]
|
|
453
|
+
# .contiguous()
|
|
454
|
+
# .view(-1)
|
|
455
|
+
for t in range(T_local)
|
|
456
|
+
for i in range(my_size)
|
|
457
|
+
],
|
|
458
|
+
dim=0,
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
class All2All_Pooled_Req(Function):
|
|
463
|
+
@staticmethod
|
|
464
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
465
|
+
def forward(
|
|
466
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
467
|
+
ctx,
|
|
468
|
+
pg: dist.ProcessGroup,
|
|
469
|
+
myreq: Request[Tensor],
|
|
470
|
+
a2ai: All2AllPooledInfo,
|
|
471
|
+
input_embeddings: Tensor,
|
|
472
|
+
) -> Tensor:
|
|
473
|
+
my_rank = dist.get_rank(pg)
|
|
474
|
+
(B_global, D_local_sum) = input_embeddings.shape
|
|
475
|
+
|
|
476
|
+
dim_sum_per_rank = a2ai.dim_sum_per_rank
|
|
477
|
+
batch_size_per_rank = a2ai.batch_size_per_rank
|
|
478
|
+
B_local = batch_size_per_rank[my_rank]
|
|
479
|
+
assert B_global == sum(batch_size_per_rank)
|
|
480
|
+
|
|
481
|
+
sharded_input_embeddings = input_embeddings.view(-1)
|
|
482
|
+
D_global_sum = sum(dim_sum_per_rank)
|
|
483
|
+
sharded_output_embeddings = torch.empty(
|
|
484
|
+
B_local * D_global_sum,
|
|
485
|
+
dtype=input_embeddings.dtype,
|
|
486
|
+
device=input_embeddings.device,
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
with record_function("## alltoall_fwd_single ##"):
|
|
490
|
+
req = dist.all_to_all_single(
|
|
491
|
+
output=sharded_output_embeddings,
|
|
492
|
+
input=sharded_input_embeddings,
|
|
493
|
+
output_split_sizes=[
|
|
494
|
+
B_local * D_rank_sum for D_rank_sum in dim_sum_per_rank
|
|
495
|
+
],
|
|
496
|
+
input_split_sizes=[
|
|
497
|
+
D_local_sum * B_rank for B_rank in batch_size_per_rank
|
|
498
|
+
],
|
|
499
|
+
group=pg,
|
|
500
|
+
async_op=True,
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
myreq.req = req
|
|
504
|
+
myreq.tensor = sharded_output_embeddings
|
|
505
|
+
myreq.a2ai = a2ai
|
|
506
|
+
myreq.wait_function = All2All_Pooled_Wait
|
|
507
|
+
ctx.myreq = myreq
|
|
508
|
+
ctx.pg = pg
|
|
509
|
+
return sharded_output_embeddings
|
|
510
|
+
|
|
511
|
+
@staticmethod
|
|
512
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
513
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
514
|
+
def backward(ctx, *unused) -> Tuple[None, None, None, Tensor]:
|
|
515
|
+
pg = ctx.pg
|
|
516
|
+
my_rank = dist.get_rank(pg)
|
|
517
|
+
myreq = ctx.myreq
|
|
518
|
+
a2ai = myreq.a2ai
|
|
519
|
+
myreq.req.wait()
|
|
520
|
+
myreq.req = None
|
|
521
|
+
grad_output = myreq.tensor
|
|
522
|
+
dim_sum_per_rank = a2ai.dim_sum_per_rank
|
|
523
|
+
batch_size_per_rank = a2ai.batch_size_per_rank
|
|
524
|
+
D_local_sum = dim_sum_per_rank[my_rank]
|
|
525
|
+
B_global = sum(batch_size_per_rank)
|
|
526
|
+
grad_input = grad_output.view(B_global, D_local_sum)
|
|
527
|
+
if GRADIENT_DIVISION:
|
|
528
|
+
grad_input.div_(dist.get_world_size(ctx.pg))
|
|
529
|
+
myreq.tensor = None
|
|
530
|
+
return (None, None, None, grad_input)
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
class All2All_Pooled_Wait(Function):
|
|
534
|
+
@staticmethod
|
|
535
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
536
|
+
def forward(
|
|
537
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
538
|
+
ctx,
|
|
539
|
+
pg: dist.ProcessGroup,
|
|
540
|
+
myreq: Request[Tensor],
|
|
541
|
+
sharded_output_embeddings: Tensor,
|
|
542
|
+
) -> Tensor:
|
|
543
|
+
my_rank = dist.get_rank(pg)
|
|
544
|
+
a2ai = myreq.a2ai
|
|
545
|
+
ctx.a2ai = a2ai
|
|
546
|
+
myreq.req.wait()
|
|
547
|
+
myreq.req = None
|
|
548
|
+
myreq.tensor = None
|
|
549
|
+
ctx.pg = pg
|
|
550
|
+
ctx.myreq = myreq
|
|
551
|
+
dim_sum_per_rank = a2ai.dim_sum_per_rank
|
|
552
|
+
batch_size_per_rank = a2ai.batch_size_per_rank
|
|
553
|
+
B_local = batch_size_per_rank[my_rank]
|
|
554
|
+
|
|
555
|
+
outputs_by_rank = sharded_output_embeddings.split(
|
|
556
|
+
[B_local * D_rank_sum for D_rank_sum in dim_sum_per_rank]
|
|
557
|
+
)
|
|
558
|
+
result = torch.cat(
|
|
559
|
+
[output.view(B_local, -1) for output in outputs_by_rank], dim=1
|
|
560
|
+
)
|
|
561
|
+
return result
|
|
562
|
+
|
|
563
|
+
@staticmethod
|
|
564
|
+
# pyre-fixme[14]: `backward` overrides method defined in `Function` inconsistently.
|
|
565
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
566
|
+
def backward(ctx, grad_output: Tensor) -> Tuple[None, None, Tensor]:
|
|
567
|
+
myreq = ctx.myreq
|
|
568
|
+
a2ai = ctx.a2ai
|
|
569
|
+
pg = ctx.pg
|
|
570
|
+
my_rank = dist.get_rank(pg)
|
|
571
|
+
dim_sum_per_rank = a2ai.dim_sum_per_rank
|
|
572
|
+
batch_size_per_rank = a2ai.batch_size_per_rank
|
|
573
|
+
|
|
574
|
+
D_local_sum = dim_sum_per_rank[my_rank]
|
|
575
|
+
(B_local, D_global_sum) = grad_output.shape
|
|
576
|
+
B_global = sum(batch_size_per_rank)
|
|
577
|
+
sharded_grad_input_sizes = B_global * D_local_sum
|
|
578
|
+
assert sum(dim_sum_per_rank) == D_global_sum
|
|
579
|
+
|
|
580
|
+
sharded_grad_output = _recat_pooled_embedding_grad_out(
|
|
581
|
+
grad_output.contiguous(),
|
|
582
|
+
dim_sum_per_rank,
|
|
583
|
+
)
|
|
584
|
+
|
|
585
|
+
sharded_grad_input = torch.empty(
|
|
586
|
+
sharded_grad_input_sizes, device=grad_output.device, dtype=grad_output.dtype
|
|
587
|
+
)
|
|
588
|
+
output_split_sizes = [
|
|
589
|
+
D_local_sum * B_rank for B_rank in a2ai.batch_size_per_rank
|
|
590
|
+
]
|
|
591
|
+
with record_function("## alltoall_bwd_single ##"):
|
|
592
|
+
req = dist.all_to_all_single(
|
|
593
|
+
output=sharded_grad_input,
|
|
594
|
+
input=sharded_grad_output,
|
|
595
|
+
output_split_sizes=output_split_sizes,
|
|
596
|
+
input_split_sizes=[
|
|
597
|
+
B_local * D_rank_sum for D_rank_sum in dim_sum_per_rank
|
|
598
|
+
],
|
|
599
|
+
group=pg,
|
|
600
|
+
async_op=True,
|
|
601
|
+
)
|
|
602
|
+
myreq.req = req
|
|
603
|
+
myreq.tensor = sharded_grad_input
|
|
604
|
+
# Note - this mismatch is by design! We return sharded_grad_output to allow PyTorch shape matching to proceed correctly.
|
|
605
|
+
return (None, None, sharded_grad_output)
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
class All2All_Seq_Req(Function):
|
|
609
|
+
@staticmethod
|
|
610
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
611
|
+
def forward(
|
|
612
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
613
|
+
ctx,
|
|
614
|
+
pg: dist.ProcessGroup,
|
|
615
|
+
myreq: Request[Tensor],
|
|
616
|
+
a2ai: All2AllSequenceInfo,
|
|
617
|
+
sharded_input_embeddings: Tensor,
|
|
618
|
+
) -> Tensor:
|
|
619
|
+
world_size = dist.get_world_size(pg)
|
|
620
|
+
my_rank = dist.get_rank(pg)
|
|
621
|
+
D = a2ai.embedding_dim
|
|
622
|
+
forward_recat_tensor = a2ai.forward_recat_tensor
|
|
623
|
+
lengths_after_sparse_data_all2all = a2ai.lengths_after_sparse_data_all2all * D
|
|
624
|
+
input_splits = [i * D for i in a2ai.output_splits]
|
|
625
|
+
output_splits = [i * D for i in a2ai.input_splits]
|
|
626
|
+
local_T = lengths_after_sparse_data_all2all.shape[0]
|
|
627
|
+
if local_T > 0:
|
|
628
|
+
with record_function("## alltoall_seq_embedding_fwd_permute ##"):
|
|
629
|
+
(
|
|
630
|
+
permuted_lengths_after_sparse_data_all2all,
|
|
631
|
+
sharded_input_embeddings,
|
|
632
|
+
_,
|
|
633
|
+
) = torch.ops.fbgemm.permute_2D_sparse_data(
|
|
634
|
+
forward_recat_tensor,
|
|
635
|
+
lengths_after_sparse_data_all2all.view(local_T * world_size, -1),
|
|
636
|
+
sharded_input_embeddings.view(-1),
|
|
637
|
+
None,
|
|
638
|
+
sharded_input_embeddings.numel(),
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
else:
|
|
642
|
+
permuted_lengths_after_sparse_data_all2all = None
|
|
643
|
+
sharded_output_embeddings = torch.empty(
|
|
644
|
+
sum(output_splits),
|
|
645
|
+
dtype=sharded_input_embeddings.dtype,
|
|
646
|
+
device=sharded_input_embeddings.device,
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
with record_function("## alltoall_seq_embedding_fwd_single ##"):
|
|
650
|
+
req = dist.all_to_all_single(
|
|
651
|
+
output=sharded_output_embeddings,
|
|
652
|
+
input=sharded_input_embeddings,
|
|
653
|
+
output_split_sizes=output_splits,
|
|
654
|
+
input_split_sizes=input_splits,
|
|
655
|
+
group=pg,
|
|
656
|
+
async_op=True,
|
|
657
|
+
)
|
|
658
|
+
a2ai.permuted_lengths_after_sparse_data_all2all = (
|
|
659
|
+
permuted_lengths_after_sparse_data_all2all
|
|
660
|
+
)
|
|
661
|
+
a2ai.input_splits = input_splits
|
|
662
|
+
a2ai.output_splits = output_splits
|
|
663
|
+
myreq.req = req
|
|
664
|
+
myreq.tensor = sharded_output_embeddings
|
|
665
|
+
myreq.a2ai = a2ai
|
|
666
|
+
myreq.wait_function = All2All_Seq_Req_Wait
|
|
667
|
+
ctx.myreq = myreq
|
|
668
|
+
ctx.pg = pg
|
|
669
|
+
ctx.my_rank = my_rank
|
|
670
|
+
ctx.world_size = world_size
|
|
671
|
+
return sharded_output_embeddings
|
|
672
|
+
|
|
673
|
+
@staticmethod
|
|
674
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
675
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
676
|
+
def backward(ctx, *unused) -> Tuple[None, None, None, Tensor]:
|
|
677
|
+
myreq = ctx.myreq
|
|
678
|
+
a2ai = myreq.a2ai
|
|
679
|
+
D = a2ai.embedding_dim
|
|
680
|
+
backward_recat_tensor = a2ai.backward_recat_tensor
|
|
681
|
+
permuted_lengths_after_sparse_data_all2all = (
|
|
682
|
+
a2ai.permuted_lengths_after_sparse_data_all2all
|
|
683
|
+
)
|
|
684
|
+
myreq.req.wait()
|
|
685
|
+
sharded_grad_input = myreq.tensor
|
|
686
|
+
myreq.req = None
|
|
687
|
+
myreq.tensor = None
|
|
688
|
+
|
|
689
|
+
if permuted_lengths_after_sparse_data_all2all is not None:
|
|
690
|
+
with record_function("## alltoall_seq_embedding_bwd_permute ##"):
|
|
691
|
+
_, sharded_grad_input, _ = torch.ops.fbgemm.permute_2D_sparse_data(
|
|
692
|
+
backward_recat_tensor,
|
|
693
|
+
permuted_lengths_after_sparse_data_all2all,
|
|
694
|
+
sharded_grad_input,
|
|
695
|
+
None,
|
|
696
|
+
sharded_grad_input.numel(),
|
|
697
|
+
)
|
|
698
|
+
return (None, None, None, sharded_grad_input.view(-1, D))
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
class All2All_Seq_Req_Wait(Function):
|
|
702
|
+
@staticmethod
|
|
703
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
704
|
+
def forward(
|
|
705
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
706
|
+
ctx,
|
|
707
|
+
pg: dist.ProcessGroup,
|
|
708
|
+
myreq: Request[Tensor],
|
|
709
|
+
sharded_output_embeddings: Tensor,
|
|
710
|
+
) -> Tensor:
|
|
711
|
+
a2ai = myreq.a2ai
|
|
712
|
+
D = a2ai.embedding_dim
|
|
713
|
+
ctx.a2ai = a2ai
|
|
714
|
+
myreq.req.wait()
|
|
715
|
+
myreq.req = None
|
|
716
|
+
myreq.tensor = None
|
|
717
|
+
ctx.pg = pg
|
|
718
|
+
ctx.myreq = myreq
|
|
719
|
+
return sharded_output_embeddings.view(-1, D)
|
|
720
|
+
|
|
721
|
+
@staticmethod
|
|
722
|
+
# pyre-fixme[14]: `backward` overrides method defined in `Function` inconsistently.
|
|
723
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
724
|
+
def backward(ctx, sharded_grad_output: Tensor) -> Tuple[None, None, Tensor]:
|
|
725
|
+
myreq = ctx.myreq
|
|
726
|
+
a2ai = ctx.a2ai
|
|
727
|
+
pg = ctx.pg
|
|
728
|
+
input_splits = a2ai.output_splits
|
|
729
|
+
output_splits = a2ai.input_splits
|
|
730
|
+
sharded_grad_input = torch.empty(
|
|
731
|
+
sum(output_splits),
|
|
732
|
+
device=sharded_grad_output.device,
|
|
733
|
+
dtype=sharded_grad_output.dtype,
|
|
734
|
+
)
|
|
735
|
+
with record_function("## alltoall_seq_embedding_bwd_single ##"):
|
|
736
|
+
req = dist.all_to_all_single(
|
|
737
|
+
output=sharded_grad_input,
|
|
738
|
+
input=sharded_grad_output.view(-1),
|
|
739
|
+
output_split_sizes=output_splits,
|
|
740
|
+
input_split_sizes=input_splits,
|
|
741
|
+
group=pg,
|
|
742
|
+
async_op=True,
|
|
743
|
+
)
|
|
744
|
+
myreq.req = req
|
|
745
|
+
myreq.tensor = sharded_grad_input
|
|
746
|
+
|
|
747
|
+
# Note - this mismatch is by design! We return sharded_grad_output
|
|
748
|
+
# to allow PyTorch shape matching to proceed correctly.
|
|
749
|
+
return (None, None, sharded_grad_output.view(-1))
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
class All2Allv_Req(Function):
|
|
753
|
+
@staticmethod
|
|
754
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
755
|
+
def forward(
|
|
756
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
757
|
+
ctx,
|
|
758
|
+
pg: dist.ProcessGroup,
|
|
759
|
+
myreq: Request[Tensor],
|
|
760
|
+
a2ai: All2AllVInfo,
|
|
761
|
+
inputs: List[Tensor],
|
|
762
|
+
) -> Tensor:
|
|
763
|
+
input_split_sizes = [m * sum(a2ai.D_local_list) for m in a2ai.B_local_list]
|
|
764
|
+
output_split_sizes = [a2ai.B_local * e for e in a2ai.dims_sum_per_rank]
|
|
765
|
+
input = torch.cat(inputs, dim=1).view([-1])
|
|
766
|
+
output = input.new_empty(sum(output_split_sizes))
|
|
767
|
+
with record_function("## alltoallv_bwd_single ##"):
|
|
768
|
+
req = dist.all_to_all_single(
|
|
769
|
+
output,
|
|
770
|
+
input,
|
|
771
|
+
output_split_sizes,
|
|
772
|
+
input_split_sizes,
|
|
773
|
+
group=pg,
|
|
774
|
+
async_op=True,
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
myreq.req = req
|
|
778
|
+
myreq.tensor = output
|
|
779
|
+
myreq.wait_function = All2Allv_Wait
|
|
780
|
+
a2ai.input_split_sizes = input_split_sizes
|
|
781
|
+
a2ai.output_split_sizes = output_split_sizes
|
|
782
|
+
myreq.a2ai = a2ai
|
|
783
|
+
ctx.a2ai = a2ai
|
|
784
|
+
ctx.myreq = myreq
|
|
785
|
+
return output
|
|
786
|
+
|
|
787
|
+
@staticmethod
|
|
788
|
+
# pyre-fixme[3]: Return type must be annotated.
|
|
789
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
790
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
791
|
+
def backward(ctx, *grad_output):
|
|
792
|
+
a2ai = ctx.a2ai
|
|
793
|
+
myreq = ctx.myreq
|
|
794
|
+
myreq.req.wait()
|
|
795
|
+
myreq.req = None
|
|
796
|
+
grad_input = myreq.tensor
|
|
797
|
+
grad_inputs = grad_input.view([a2ai.B_global, -1]).split(
|
|
798
|
+
a2ai.D_local_list, dim=1
|
|
799
|
+
)
|
|
800
|
+
grad_inputs = [gin.contiguous() for gin in grad_inputs]
|
|
801
|
+
myreq.tensor = None
|
|
802
|
+
return (None, None, None, *grad_inputs)
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
class All2Allv_Wait(Function):
|
|
806
|
+
@staticmethod
|
|
807
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
808
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
809
|
+
def forward(ctx, pg: dist.ProcessGroup, myreq, output) -> Tuple[Tensor]:
|
|
810
|
+
a2ai = myreq.a2ai
|
|
811
|
+
ctx.a2ai = a2ai
|
|
812
|
+
myreq.req.wait()
|
|
813
|
+
myreq.req = None
|
|
814
|
+
myreq.tensor = None
|
|
815
|
+
ctx.pg = pg
|
|
816
|
+
ctx.myreq = myreq
|
|
817
|
+
outputs = tuple(
|
|
818
|
+
[
|
|
819
|
+
out.view([a2ai.B_local, -1])
|
|
820
|
+
for out in output.split(a2ai.output_split_sizes)
|
|
821
|
+
]
|
|
822
|
+
)
|
|
823
|
+
return outputs
|
|
824
|
+
|
|
825
|
+
@staticmethod
|
|
826
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
827
|
+
def backward(ctx, *grad_outputs) -> Tuple[None, None, Tensor]:
|
|
828
|
+
pg = ctx.pg
|
|
829
|
+
myreq = ctx.myreq
|
|
830
|
+
a2ai = ctx.a2ai
|
|
831
|
+
grad_outputs = [gout.contiguous().view([-1]) for gout in grad_outputs]
|
|
832
|
+
grad_output = torch.cat(grad_outputs)
|
|
833
|
+
grad_input = grad_output.new_empty([a2ai.B_global * sum(a2ai.D_local_list)])
|
|
834
|
+
with record_function("## alltoall_bwd_single ##"):
|
|
835
|
+
req = dist.all_to_all_single(
|
|
836
|
+
grad_input,
|
|
837
|
+
grad_output,
|
|
838
|
+
a2ai.input_split_sizes,
|
|
839
|
+
a2ai.output_split_sizes,
|
|
840
|
+
group=pg,
|
|
841
|
+
async_op=True,
|
|
842
|
+
)
|
|
843
|
+
myreq.req = req
|
|
844
|
+
myreq.tensor = grad_input
|
|
845
|
+
return (None, None, grad_output)
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
class ReduceScatter_Req(Function):
|
|
849
|
+
@staticmethod
|
|
850
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
851
|
+
def forward(
|
|
852
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
853
|
+
ctx,
|
|
854
|
+
pg: dist.ProcessGroup,
|
|
855
|
+
myreq: Request[Tensor],
|
|
856
|
+
rsi: ReduceScatterInfo,
|
|
857
|
+
*inputs: Any,
|
|
858
|
+
) -> Tensor:
|
|
859
|
+
my_rank = dist.get_rank(pg)
|
|
860
|
+
output = inputs[my_rank].new_empty(
|
|
861
|
+
inputs[my_rank].size(),
|
|
862
|
+
dtype=inputs[my_rank].dtype,
|
|
863
|
+
device=inputs[my_rank].device,
|
|
864
|
+
)
|
|
865
|
+
with record_function("## reduce_scatter ##"):
|
|
866
|
+
req = dist.reduce_scatter(output, list(inputs), group=pg, async_op=True)
|
|
867
|
+
myreq.req = req
|
|
868
|
+
myreq.tensor = output
|
|
869
|
+
myreq.wait_function = ReduceScatter_Wait
|
|
870
|
+
myreq.rsi = rsi
|
|
871
|
+
ctx.myreq = myreq
|
|
872
|
+
ctx.pg = pg
|
|
873
|
+
return output
|
|
874
|
+
|
|
875
|
+
@staticmethod
|
|
876
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
877
|
+
def backward(ctx, *unused: Tensor) -> Tuple[Optional[Tensor], ...]:
|
|
878
|
+
myreq = ctx.myreq
|
|
879
|
+
myreq.req.wait()
|
|
880
|
+
myreq.req = None
|
|
881
|
+
grad_inputs = list(myreq.tensor)
|
|
882
|
+
# Make it equivalent to running on a single rank.
|
|
883
|
+
if GRADIENT_DIVISION:
|
|
884
|
+
for grad_input in grad_inputs:
|
|
885
|
+
grad_input.div_(dist.get_world_size(ctx.pg))
|
|
886
|
+
myreq.tensor = None
|
|
887
|
+
return (None, None, None, *grad_inputs)
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
class ReduceScatter_Wait(Function):
|
|
891
|
+
@staticmethod
|
|
892
|
+
# pyre-fixme[14]: `forward` overrides method defined in `Function` inconsistently.
|
|
893
|
+
def forward(
|
|
894
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
895
|
+
ctx,
|
|
896
|
+
pg: dist.ProcessGroup,
|
|
897
|
+
myreq: Request[Tensor],
|
|
898
|
+
output: Tensor,
|
|
899
|
+
) -> Tensor:
|
|
900
|
+
myreq.req.wait()
|
|
901
|
+
myreq.req = None
|
|
902
|
+
myreq.tensor = None
|
|
903
|
+
ctx.myreq = myreq
|
|
904
|
+
ctx.pg = pg
|
|
905
|
+
return output
|
|
906
|
+
|
|
907
|
+
@staticmethod
|
|
908
|
+
# pyre-fixme[14]: `backward` overrides method defined in `Function` inconsistently.
|
|
909
|
+
# pyre-fixme[2]: Parameter must be annotated.
|
|
910
|
+
def backward(ctx, grad_output: Tensor) -> Tuple[None, None, Tensor]:
|
|
911
|
+
myreq = ctx.myreq
|
|
912
|
+
rsi = myreq.rsi
|
|
913
|
+
grad_inputs = [
|
|
914
|
+
grad_output.new_empty(
|
|
915
|
+
in_size,
|
|
916
|
+
dtype=grad_output.dtype,
|
|
917
|
+
device=grad_output.device,
|
|
918
|
+
)
|
|
919
|
+
for in_size in rsi.input_sizes
|
|
920
|
+
]
|
|
921
|
+
with record_function("## reduce_scatter_bw (all_gather) ##"):
|
|
922
|
+
req = dist.all_gather(
|
|
923
|
+
grad_inputs,
|
|
924
|
+
grad_output.contiguous(),
|
|
925
|
+
group=ctx.pg,
|
|
926
|
+
async_op=True,
|
|
927
|
+
)
|
|
928
|
+
myreq.req = req
|
|
929
|
+
myreq.tensor = grad_inputs
|
|
930
|
+
return (None, None, grad_output)
|