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,341 @@
|
|
|
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 csv
|
|
9
|
+
import math
|
|
10
|
+
import random
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from functools import partial
|
|
13
|
+
from io import IOBase
|
|
14
|
+
from typing import Any, Callable, Iterable, Iterator, List, Sequence, Tuple, TypeVar
|
|
15
|
+
|
|
16
|
+
import torch
|
|
17
|
+
from iopath.common.file_io import PathManager, PathManagerFactory
|
|
18
|
+
from torch.utils.data import functional_datapipe, get_worker_info, IterDataPipe
|
|
19
|
+
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
|
|
20
|
+
from torchrec.streamable import Pipelineable
|
|
21
|
+
|
|
22
|
+
PATH_MANAGER_KEY = "torchrec"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class Batch(Pipelineable):
|
|
27
|
+
dense_features: torch.Tensor
|
|
28
|
+
sparse_features: KeyedJaggedTensor
|
|
29
|
+
labels: torch.Tensor
|
|
30
|
+
|
|
31
|
+
def to(self, device: torch.device, non_blocking: bool = False) -> "Batch":
|
|
32
|
+
return Batch(
|
|
33
|
+
dense_features=self.dense_features.to(
|
|
34
|
+
device=device, non_blocking=non_blocking
|
|
35
|
+
),
|
|
36
|
+
sparse_features=self.sparse_features.to(
|
|
37
|
+
device=device, non_blocking=non_blocking
|
|
38
|
+
),
|
|
39
|
+
labels=self.labels.to(device=device, non_blocking=non_blocking),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def record_stream(self, stream: torch.cuda.streams.Stream) -> None:
|
|
43
|
+
# pyre-fixme[6]: For 1st param expected `Stream` but got `Stream`.
|
|
44
|
+
self.dense_features.record_stream(stream)
|
|
45
|
+
self.sparse_features.record_stream(stream)
|
|
46
|
+
# pyre-fixme[6]: For 1st param expected `Stream` but got `Stream`.
|
|
47
|
+
self.labels.record_stream(stream)
|
|
48
|
+
|
|
49
|
+
def pin_memory(self) -> "Batch":
|
|
50
|
+
return Batch(
|
|
51
|
+
dense_features=self.dense_features.pin_memory(),
|
|
52
|
+
sparse_features=self.sparse_features.pin_memory(),
|
|
53
|
+
labels=self.labels.pin_memory(),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class _IdxFilter(IterDataPipe):
|
|
58
|
+
def __init__(
|
|
59
|
+
self, datapipe: IterDataPipe, filter_fn: Callable[[int], bool]
|
|
60
|
+
) -> None:
|
|
61
|
+
super().__init__()
|
|
62
|
+
self.datapipe = datapipe
|
|
63
|
+
self.filter_fn = filter_fn
|
|
64
|
+
|
|
65
|
+
# pyre-ignore[3]
|
|
66
|
+
def __iter__(self) -> Iterator[Any]:
|
|
67
|
+
for idx, data in enumerate(self.datapipe):
|
|
68
|
+
if self.filter_fn(idx):
|
|
69
|
+
yield data
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _default_key_fn(idx: int) -> int:
|
|
73
|
+
return idx
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def train_filter(
|
|
77
|
+
key_fn: Callable[[int], int],
|
|
78
|
+
train_perc: float,
|
|
79
|
+
decimal_places: int,
|
|
80
|
+
idx: int,
|
|
81
|
+
) -> bool:
|
|
82
|
+
return (key_fn(idx) % 10**decimal_places) < round(
|
|
83
|
+
train_perc * 10**decimal_places
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def val_filter(
|
|
88
|
+
key_fn: Callable[[int], int],
|
|
89
|
+
train_perc: float,
|
|
90
|
+
decimal_places: int,
|
|
91
|
+
idx: int,
|
|
92
|
+
) -> bool:
|
|
93
|
+
return not train_filter(key_fn, train_perc, decimal_places, idx)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def idx_split_train_val(
|
|
97
|
+
datapipe: IterDataPipe,
|
|
98
|
+
train_perc: float,
|
|
99
|
+
decimal_places: int = 2,
|
|
100
|
+
key_fn: Callable[[int], int] = _default_key_fn,
|
|
101
|
+
) -> Tuple[IterDataPipe, IterDataPipe]:
|
|
102
|
+
if not 0.0 < train_perc < 1.0:
|
|
103
|
+
raise ValueError("train_perc must be in range (0.0, 1.0)")
|
|
104
|
+
return (
|
|
105
|
+
_IdxFilter(datapipe, partial(train_filter, key_fn, train_perc, decimal_places)),
|
|
106
|
+
_IdxFilter(datapipe, partial(val_filter, key_fn, train_perc, decimal_places)),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class _RandFilter(IterDataPipe):
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
datapipe: IterDataPipe,
|
|
114
|
+
filter_fn: Callable[[random.Random], bool],
|
|
115
|
+
rand_gen: random.Random,
|
|
116
|
+
) -> None:
|
|
117
|
+
super().__init__()
|
|
118
|
+
self.datapipe = datapipe
|
|
119
|
+
self.filter_fn = filter_fn
|
|
120
|
+
self.rand_gen = rand_gen
|
|
121
|
+
# pyre-ignore[4]
|
|
122
|
+
self.rand_gen_init_state: Tuple[Any, ...] = rand_gen.getstate()
|
|
123
|
+
|
|
124
|
+
# pyre-ignore[3]
|
|
125
|
+
def __iter__(self) -> Iterator[Any]:
|
|
126
|
+
self.rand_gen.setstate(self.rand_gen_init_state)
|
|
127
|
+
for data in self.datapipe:
|
|
128
|
+
if self.filter_fn(self.rand_gen):
|
|
129
|
+
yield data
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _rand_train_filter_fn(
|
|
133
|
+
train_perc: float,
|
|
134
|
+
rand_gen: random.Random,
|
|
135
|
+
) -> bool:
|
|
136
|
+
return rand_gen.random() < train_perc
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _rand_val_filter_fn(train_perc: float, rand_gen: random.Random) -> bool:
|
|
140
|
+
return not _rand_train_filter_fn(train_perc, rand_gen)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def rand_split_train_val(
|
|
144
|
+
datapipe: IterDataPipe,
|
|
145
|
+
train_perc: float,
|
|
146
|
+
random_seed: int = 0,
|
|
147
|
+
) -> Tuple[IterDataPipe, IterDataPipe]:
|
|
148
|
+
"""Via uniform random sampling, generates two IterDataPipe instances representing
|
|
149
|
+
disjoint train and val splits of the given IterDataPipe.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
datapipe (IterDataPipe): datapipe to split.
|
|
153
|
+
train_perc (float): value in range (0.0, 1.0) specifying target proportion of
|
|
154
|
+
datapipe samples to include in train split. Note that the actual proportion
|
|
155
|
+
is not guaranteed to match train_perc exactly.
|
|
156
|
+
random_seed (int): determines split membership for a given sample
|
|
157
|
+
and train_perc. Use the same value across calls to generate consistent splits.
|
|
158
|
+
Example::
|
|
159
|
+
|
|
160
|
+
datapipe = criteo_terabyte(
|
|
161
|
+
("/home/datasets/criteo/day_0.tsv", "/home/datasets/criteo/day_1.tsv")
|
|
162
|
+
)
|
|
163
|
+
train_datapipe, val_datapipe = rand_split_train_val(datapipe, 0.75)
|
|
164
|
+
train_batch = next(iter(train_datapipe))
|
|
165
|
+
val_batch = next(iter(val_datapipe))
|
|
166
|
+
"""
|
|
167
|
+
if not 0.0 < train_perc < 1.0:
|
|
168
|
+
raise ValueError("train_perc must be in range (0.0, 1.0)")
|
|
169
|
+
|
|
170
|
+
return _RandFilter(
|
|
171
|
+
datapipe, partial(_rand_train_filter_fn, train_perc), random.Random(random_seed)
|
|
172
|
+
), _RandFilter(
|
|
173
|
+
datapipe, partial(_rand_val_filter_fn, train_perc), random.Random(random_seed)
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
T = TypeVar("T")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def safe_cast(val: T, dest_type: Callable[[T], T], default: T) -> T:
|
|
181
|
+
try:
|
|
182
|
+
return dest_type(val)
|
|
183
|
+
except ValueError:
|
|
184
|
+
return default
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@functional_datapipe("limit")
|
|
188
|
+
class Limit(IterDataPipe):
|
|
189
|
+
def __init__(self, datapipe: IterDataPipe, limit: int) -> None:
|
|
190
|
+
super().__init__()
|
|
191
|
+
self.datapipe = datapipe
|
|
192
|
+
self.limit = limit
|
|
193
|
+
|
|
194
|
+
# pyre-ignore[3]
|
|
195
|
+
def __iter__(self) -> Iterator[Any]:
|
|
196
|
+
for idx, data in enumerate(self.datapipe):
|
|
197
|
+
if idx >= self.limit:
|
|
198
|
+
break
|
|
199
|
+
yield data
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
class ReadLinesFromCSV(IterDataPipe):
|
|
203
|
+
def __init__(
|
|
204
|
+
self,
|
|
205
|
+
datapipe: IterDataPipe[Tuple[str, "IOBase"]],
|
|
206
|
+
skip_first_line: bool = False,
|
|
207
|
+
# pyre-ignore[2]
|
|
208
|
+
**kw,
|
|
209
|
+
) -> None:
|
|
210
|
+
super().__init__()
|
|
211
|
+
self.datapipe = datapipe
|
|
212
|
+
self.skip_first_line = skip_first_line
|
|
213
|
+
# pyre-ignore[4]
|
|
214
|
+
self.kw = kw
|
|
215
|
+
|
|
216
|
+
def __iter__(self) -> Iterator[List[str]]:
|
|
217
|
+
for _, data in self.datapipe:
|
|
218
|
+
reader = csv.reader(data, **self.kw)
|
|
219
|
+
if self.skip_first_line:
|
|
220
|
+
next(reader, None)
|
|
221
|
+
for line in reader:
|
|
222
|
+
yield line
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
class LoadFiles(IterDataPipe[Tuple[str, "IOBase"]]):
|
|
226
|
+
"""
|
|
227
|
+
Taken and adapted from torch.utils.data.datapipes.iter.LoadFilesFromDisk
|
|
228
|
+
|
|
229
|
+
TODO:
|
|
230
|
+
Merge this back or replace this with something in core Datapipes lib
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
def __init__(
|
|
234
|
+
self,
|
|
235
|
+
datapipe: Iterable[str],
|
|
236
|
+
mode: str = "b",
|
|
237
|
+
length: int = -1,
|
|
238
|
+
path_manager_key: str = PATH_MANAGER_KEY,
|
|
239
|
+
# pyre-ignore[2]
|
|
240
|
+
**open_kw,
|
|
241
|
+
) -> None:
|
|
242
|
+
super().__init__()
|
|
243
|
+
self.datapipe: Iterable[str] = datapipe
|
|
244
|
+
self.mode: str = mode
|
|
245
|
+
if self.mode not in ("b", "t", "rb", "rt", "r"):
|
|
246
|
+
raise ValueError("Invalid mode {}".format(mode))
|
|
247
|
+
# TODO: enforce typing for each instance based on mode, otherwise
|
|
248
|
+
# `argument_validation` with this DataPipe may be potentially broken
|
|
249
|
+
self.length: int = length
|
|
250
|
+
# pyre-ignore[4]
|
|
251
|
+
self.open_kw = open_kw
|
|
252
|
+
self.path_manager: PathManager = PathManagerFactory().get(path_manager_key)
|
|
253
|
+
self.path_manager.set_strict_kwargs_checking(False)
|
|
254
|
+
|
|
255
|
+
# Remove annotation due to 'IOBase' is a general type and true type
|
|
256
|
+
# is determined at runtime based on mode. Some `DataPipe` requiring
|
|
257
|
+
# a subtype would cause mypy error.
|
|
258
|
+
# pyre-ignore[3]
|
|
259
|
+
def __iter__(self):
|
|
260
|
+
if self.mode in ("b", "t"):
|
|
261
|
+
self.mode = "r" + self.mode
|
|
262
|
+
for pathname in self.datapipe:
|
|
263
|
+
if not isinstance(pathname, str):
|
|
264
|
+
raise TypeError(
|
|
265
|
+
"Expected string type for pathname, but got {}".format(
|
|
266
|
+
type(pathname)
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
yield (
|
|
270
|
+
pathname,
|
|
271
|
+
self.path_manager.open(pathname, self.mode, **self.open_kw),
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
def __len__(self) -> int:
|
|
275
|
+
if self.length == -1:
|
|
276
|
+
raise NotImplementedError
|
|
277
|
+
return self.length
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _default_dp_selector(
|
|
281
|
+
datapipes: Sequence[IterDataPipe],
|
|
282
|
+
) -> Sequence[IterDataPipe]:
|
|
283
|
+
worker_info = get_worker_info()
|
|
284
|
+
if worker_info is None:
|
|
285
|
+
return datapipes
|
|
286
|
+
else:
|
|
287
|
+
if worker_info.num_workers > len(datapipes):
|
|
288
|
+
raise ValueError(
|
|
289
|
+
f"Number of workers {worker_info.num_workers} exceeds"
|
|
290
|
+
f"number of datapipes ({len(datapipes)})!"
|
|
291
|
+
)
|
|
292
|
+
offsets = [0]
|
|
293
|
+
for num_workers in reversed(range(1, worker_info.num_workers + 1)):
|
|
294
|
+
remaining_dps = len(datapipes) - offsets[-1]
|
|
295
|
+
dps_to_assign = math.ceil(remaining_dps / num_workers)
|
|
296
|
+
offsets.append(offsets[-1] + dps_to_assign)
|
|
297
|
+
return datapipes[offsets[worker_info.id] : offsets[worker_info.id + 1]]
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class ParallelReadConcat(IterDataPipe):
|
|
301
|
+
r""":class:`ParallelReadConcat`.
|
|
302
|
+
|
|
303
|
+
Iterable DataPipe that concatenates multiple Iterable DataPipes.
|
|
304
|
+
When used with a DataLoader, assigns a subset of datapipes to each DataLoader worker
|
|
305
|
+
to allow for parallel reading.
|
|
306
|
+
Args:
|
|
307
|
+
datapipes: IterDataPipe instances to read from.
|
|
308
|
+
dp_selector: function that each DataLoader worker would use to determine the subset of datapipes
|
|
309
|
+
to read from.
|
|
310
|
+
Example::
|
|
311
|
+
|
|
312
|
+
datapipes = [
|
|
313
|
+
criteo_terabyte(
|
|
314
|
+
(f"/home/local/datasets/criteo/shard_{idx}.tsv",),
|
|
315
|
+
)
|
|
316
|
+
.batch(100)
|
|
317
|
+
.collate()
|
|
318
|
+
for idx in range(4)
|
|
319
|
+
]
|
|
320
|
+
dataloader = DataLoader(
|
|
321
|
+
ParallelReadConcat(*datapipes), num_workers=4, batch_size=None
|
|
322
|
+
)
|
|
323
|
+
"""
|
|
324
|
+
|
|
325
|
+
def __init__(
|
|
326
|
+
self,
|
|
327
|
+
*datapipes: IterDataPipe,
|
|
328
|
+
dp_selector: Callable[
|
|
329
|
+
[Sequence[IterDataPipe]], Sequence[IterDataPipe]
|
|
330
|
+
] = _default_dp_selector,
|
|
331
|
+
) -> None:
|
|
332
|
+
super().__init__()
|
|
333
|
+
self.datapipes: Tuple[IterDataPipe, ...] = datapipes
|
|
334
|
+
self.dp_selector = dp_selector
|
|
335
|
+
|
|
336
|
+
# pyre-ignore[3]
|
|
337
|
+
def __iter__(self) -> Iterator[Any]:
|
|
338
|
+
selected_dps = self.dp_selector(self.datapipes)
|
|
339
|
+
for dp in selected_dps:
|
|
340
|
+
for data in dp:
|
|
341
|
+
yield data
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
"""Torchrec Distributed
|
|
9
|
+
|
|
10
|
+
Torchrec distributed provides the necessary modules and operations to enable model parallelism.
|
|
11
|
+
|
|
12
|
+
These include:
|
|
13
|
+
|
|
14
|
+
* model parallelism through `DistributedModelParallel`.
|
|
15
|
+
* collective operations for comms, including All-to-All and Reduce-Scatter.
|
|
16
|
+
|
|
17
|
+
* collective operations wrappers for sparse features, KJT, and various embedding
|
|
18
|
+
types.
|
|
19
|
+
|
|
20
|
+
* sharded implementations of various modules including `ShardedEmbeddingBag` for
|
|
21
|
+
`nn.EmbeddingBag`, `ShardedEmbeddingBagCollection` for `EmbeddingBagCollection`
|
|
22
|
+
|
|
23
|
+
* embedding sharders that define sharding for any sharded module implementation.
|
|
24
|
+
* support for various compute kernels, which are optimized for compute device
|
|
25
|
+
(CPU/GPU) and may include batching together embedding tables and/or optimizer
|
|
26
|
+
fusion.
|
|
27
|
+
|
|
28
|
+
* pipelined training through `TrainPipelineSparseDist` that overlaps dataloading
|
|
29
|
+
device transfer (copy to GPU), inter*device communications (input_dist), and
|
|
30
|
+
computation (forward, backward) for increased performance.
|
|
31
|
+
* quantization support for reduced precision training and inference.
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from torchrec.distributed.comm import get_local_rank, get_local_size # noqa
|
|
36
|
+
from torchrec.distributed.model_parallel import DistributedModelParallel # noqa
|
|
37
|
+
from torchrec.distributed.train_pipeline import ( # noqa
|
|
38
|
+
TrainPipeline,
|
|
39
|
+
TrainPipelineBase,
|
|
40
|
+
TrainPipelineSparseDist,
|
|
41
|
+
)
|
|
42
|
+
from torchrec.distributed.types import ( # noqa
|
|
43
|
+
Awaitable,
|
|
44
|
+
ModuleSharder,
|
|
45
|
+
NoWait,
|
|
46
|
+
ParameterSharding,
|
|
47
|
+
ShardedModule,
|
|
48
|
+
ShardedTensor,
|
|
49
|
+
ShardingEnv,
|
|
50
|
+
ShardingPlanner,
|
|
51
|
+
)
|
|
52
|
+
from torchrec.distributed.utils import ( # noqa
|
|
53
|
+
get_unsharded_module_names,
|
|
54
|
+
sharded_model_copy,
|
|
55
|
+
)
|