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,280 @@
|
|
|
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 copy
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import cast, List
|
|
11
|
+
|
|
12
|
+
from torchrec.distributed.planner.types import (
|
|
13
|
+
DeviceHardware,
|
|
14
|
+
PartitionByType,
|
|
15
|
+
Partitioner,
|
|
16
|
+
PlannerError,
|
|
17
|
+
ShardingOption,
|
|
18
|
+
Storage,
|
|
19
|
+
Topology,
|
|
20
|
+
)
|
|
21
|
+
from torchrec.distributed.types import ShardingType
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _sort_devices_by_perf(
|
|
25
|
+
devices: List[List[DeviceHardware]],
|
|
26
|
+
) -> List[List[DeviceHardware]]:
|
|
27
|
+
def _get_perf_sum(device_list: List[DeviceHardware]) -> float:
|
|
28
|
+
perf = 0
|
|
29
|
+
for device in device_list:
|
|
30
|
+
perf += device.perf
|
|
31
|
+
return perf
|
|
32
|
+
|
|
33
|
+
return sorted(devices, key=_get_perf_sum)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _get_uniform_sharding_options(
|
|
37
|
+
sharding_options: List[ShardingOption],
|
|
38
|
+
) -> List[ShardingOption]:
|
|
39
|
+
uniform_sharding_options: List[ShardingOption] = []
|
|
40
|
+
for sharding_option in sharding_options:
|
|
41
|
+
if sharding_option.partition_by == PartitionByType.UNIFORM.value:
|
|
42
|
+
uniform_sharding_options.append(sharding_option)
|
|
43
|
+
return uniform_sharding_options
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class ShardingOptionGroup:
|
|
48
|
+
sharding_options: List[ShardingOption]
|
|
49
|
+
storage_sum: Storage
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _group_and_sort_non_uniform_sharding_options(
|
|
53
|
+
sharding_options: List[ShardingOption],
|
|
54
|
+
) -> List[ShardingOptionGroup]:
|
|
55
|
+
sharding_option_groups_by_dependency = {}
|
|
56
|
+
for sharding_option in sharding_options:
|
|
57
|
+
if sharding_option.partition_by == PartitionByType.UNIFORM.value:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
group_key = sharding_option.dependency or sharding_option.fqn
|
|
61
|
+
if group_key not in sharding_option_groups_by_dependency:
|
|
62
|
+
sharding_option_groups_by_dependency[group_key] = ShardingOptionGroup(
|
|
63
|
+
[sharding_option], sharding_option.total_storage
|
|
64
|
+
)
|
|
65
|
+
else:
|
|
66
|
+
sharding_option_groups_by_dependency[group_key].sharding_options.append(
|
|
67
|
+
sharding_option
|
|
68
|
+
)
|
|
69
|
+
sharding_option_groups_by_dependency[
|
|
70
|
+
group_key
|
|
71
|
+
].storage_sum += sharding_option.total_storage
|
|
72
|
+
sharding_option_groups = list(sharding_option_groups_by_dependency.values())
|
|
73
|
+
|
|
74
|
+
sharding_option_groups.sort(key=lambda group: group.storage_sum, reverse=True)
|
|
75
|
+
return sharding_option_groups
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class GreedyPerfPartitioner(Partitioner):
|
|
79
|
+
"""
|
|
80
|
+
Greedy Partitioner
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
def partition(
|
|
84
|
+
self,
|
|
85
|
+
proposal: List[ShardingOption],
|
|
86
|
+
storage_constraint: Topology,
|
|
87
|
+
) -> List[ShardingOption]:
|
|
88
|
+
"""
|
|
89
|
+
Places sharding options on topology based on each sharding option's
|
|
90
|
+
`partition_by` attribute.
|
|
91
|
+
The topology, storage, and perfs are updated at the end of the placement.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
proposal (List[ShardingOption]): list of populated sharding options.
|
|
95
|
+
storage_constraint (Topology): device topology.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
List[ShardingOption]: list of sharding options for selected plan.
|
|
99
|
+
|
|
100
|
+
Example::
|
|
101
|
+
|
|
102
|
+
sharding_options = [
|
|
103
|
+
ShardingOption(partition_by="uniform",
|
|
104
|
+
shards=[
|
|
105
|
+
Shards(storage=1, perf=1),
|
|
106
|
+
Shards(storage=1, perf=1),
|
|
107
|
+
]),
|
|
108
|
+
ShardingOption(partition_by="uniform",
|
|
109
|
+
shards=[
|
|
110
|
+
Shards(storage=2, perf=2),
|
|
111
|
+
Shards(storage=2, perf=2),
|
|
112
|
+
]),
|
|
113
|
+
ShardingOption(partition_by="device",
|
|
114
|
+
shards=[
|
|
115
|
+
Shards(storage=3, perf=3),
|
|
116
|
+
Shards(storage=3, perf=3),
|
|
117
|
+
])
|
|
118
|
+
ShardingOption(partition_by="device",
|
|
119
|
+
shards=[
|
|
120
|
+
Shards(storage=4, perf=4),
|
|
121
|
+
Shards(storage=4, perf=4),
|
|
122
|
+
]),
|
|
123
|
+
]
|
|
124
|
+
topology = Topology(world_size=2)
|
|
125
|
+
|
|
126
|
+
# First [sharding_options[0] and sharding_options[1]] will be placed on the
|
|
127
|
+
# topology with the uniform strategy, resulting in
|
|
128
|
+
|
|
129
|
+
topology.devices[0].perf = (1,2)
|
|
130
|
+
topology.devices[1].perf = (1,2)
|
|
131
|
+
|
|
132
|
+
# Finally sharding_options[2] and sharding_options[3]] will be placed on the
|
|
133
|
+
# topology with the device strategy (see docstring of `partition_by_device` for
|
|
134
|
+
# more details).
|
|
135
|
+
|
|
136
|
+
topology.devices[0].perf = (1,2) + (3,4)
|
|
137
|
+
topology.devices[1].perf = (1,2) + (3,4)
|
|
138
|
+
|
|
139
|
+
# The topology updates are done after the end of all the placements (the other
|
|
140
|
+
# in the example is just for clarity).
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
_topology: Topology = copy.deepcopy(storage_constraint)
|
|
144
|
+
plan = copy.deepcopy(proposal)
|
|
145
|
+
_host_level_devices = GreedyPerfPartitioner._get_host_level_devices(_topology)
|
|
146
|
+
|
|
147
|
+
# first partition the uniform sharding options (RW & DP)
|
|
148
|
+
uniform_sharding_options = _get_uniform_sharding_options(plan)
|
|
149
|
+
GreedyPerfPartitioner._uniform_partition(
|
|
150
|
+
uniform_sharding_options, _topology.devices
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
# group the rest sharding options by colocation type (co-host, co-device, none)
|
|
154
|
+
# and sort the groups by storage in reverse order
|
|
155
|
+
sharding_option_groups = _group_and_sort_non_uniform_sharding_options(plan)
|
|
156
|
+
|
|
157
|
+
for sharding_option_group in sharding_option_groups:
|
|
158
|
+
if (
|
|
159
|
+
sharding_option_group.sharding_options[0].partition_by
|
|
160
|
+
== PartitionByType.HOST.value
|
|
161
|
+
):
|
|
162
|
+
GreedyPerfPartitioner._cohost_partition(
|
|
163
|
+
sharding_option_group, _host_level_devices
|
|
164
|
+
)
|
|
165
|
+
elif (
|
|
166
|
+
sharding_option_group.sharding_options[0].partition_by
|
|
167
|
+
== PartitionByType.DEVICE.value
|
|
168
|
+
):
|
|
169
|
+
assert (
|
|
170
|
+
len(sharding_option_group.sharding_options) == 1
|
|
171
|
+
), f"Unexpected length for sharding options: {len(sharding_option_group.sharding_options)}"
|
|
172
|
+
GreedyPerfPartitioner._device_partition(
|
|
173
|
+
sharding_option_group.sharding_options[0], _topology.devices
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
raise RuntimeError(
|
|
177
|
+
f"Unexpected sharding option group {sharding_option_group}"
|
|
178
|
+
)
|
|
179
|
+
# pyre-ignore [16]: `GreedyPerfPartitioner` has no attribute `_topology`.
|
|
180
|
+
self._topology: Topology = _topology
|
|
181
|
+
return plan
|
|
182
|
+
|
|
183
|
+
@staticmethod
|
|
184
|
+
def _device_partition(
|
|
185
|
+
sharding_option: ShardingOption, devices: List[DeviceHardware]
|
|
186
|
+
) -> None:
|
|
187
|
+
for shard in sharding_option.shards:
|
|
188
|
+
sorted_devices = sorted(devices, key=lambda device: device.perf)
|
|
189
|
+
success = False
|
|
190
|
+
for device in sorted_devices:
|
|
191
|
+
if cast(Storage, shard.storage).fits_in(device.storage):
|
|
192
|
+
shard.rank = device.rank
|
|
193
|
+
device.storage -= cast(Storage, shard.storage)
|
|
194
|
+
device.perf += cast(float, shard.perf)
|
|
195
|
+
success = True
|
|
196
|
+
break
|
|
197
|
+
if not success:
|
|
198
|
+
raise PlannerError(
|
|
199
|
+
f"Device partition failed. Couldn't find a rank for shard {shard}, devices: {devices}"
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
@staticmethod
|
|
203
|
+
def _cohost_partition(
|
|
204
|
+
sharding_option_group: ShardingOptionGroup,
|
|
205
|
+
_host_level_devices: List[List[DeviceHardware]],
|
|
206
|
+
) -> None:
|
|
207
|
+
sorted_host_level_devices = _sort_devices_by_perf(_host_level_devices)
|
|
208
|
+
for devices in sorted_host_level_devices:
|
|
209
|
+
host_devices = copy.deepcopy(devices)
|
|
210
|
+
host_storage = Storage(hbm=0, ddr=0)
|
|
211
|
+
for device in host_devices:
|
|
212
|
+
host_storage += device.storage
|
|
213
|
+
if not sharding_option_group.storage_sum.fits_in(host_storage):
|
|
214
|
+
continue
|
|
215
|
+
|
|
216
|
+
success = True
|
|
217
|
+
for sharding_option in sharding_option_group.sharding_options:
|
|
218
|
+
try:
|
|
219
|
+
if (
|
|
220
|
+
sharding_option.sharding_type
|
|
221
|
+
== ShardingType.TABLE_ROW_WISE.value
|
|
222
|
+
):
|
|
223
|
+
GreedyPerfPartitioner._uniform_partition(
|
|
224
|
+
[sharding_option], host_devices
|
|
225
|
+
)
|
|
226
|
+
elif (
|
|
227
|
+
sharding_option.sharding_type
|
|
228
|
+
== ShardingType.TABLE_COLUMN_WISE.value
|
|
229
|
+
):
|
|
230
|
+
GreedyPerfPartitioner._device_partition(
|
|
231
|
+
sharding_option, host_devices
|
|
232
|
+
)
|
|
233
|
+
else:
|
|
234
|
+
raise RuntimeError(
|
|
235
|
+
f"unexpected cohost sharding type: {sharding_option.sharding_type}"
|
|
236
|
+
)
|
|
237
|
+
except PlannerError:
|
|
238
|
+
success = False
|
|
239
|
+
break
|
|
240
|
+
if success:
|
|
241
|
+
# successfully found a host and partitioned on that host
|
|
242
|
+
# need to update the devices
|
|
243
|
+
for device, device_copy in zip(devices, host_devices):
|
|
244
|
+
device.storage = device_copy.storage
|
|
245
|
+
device.perf = device_copy.perf
|
|
246
|
+
return
|
|
247
|
+
raise PlannerError(
|
|
248
|
+
f"can't find a host for sharding option group {sharding_option_group}"
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
@staticmethod
|
|
252
|
+
def _get_host_level_devices(_topology: Topology) -> List[List[DeviceHardware]]:
|
|
253
|
+
num_hosts: int = _topology.world_size // _topology.local_world_size
|
|
254
|
+
host_level_devices: List[List[DeviceHardware]] = []
|
|
255
|
+
for i in range(num_hosts):
|
|
256
|
+
devices_in_host = _topology.devices[
|
|
257
|
+
i * _topology.local_world_size : (i + 1) * _topology.local_world_size
|
|
258
|
+
]
|
|
259
|
+
host_level_devices.append(devices_in_host)
|
|
260
|
+
return host_level_devices
|
|
261
|
+
|
|
262
|
+
@staticmethod
|
|
263
|
+
def _uniform_partition(
|
|
264
|
+
sharding_options: List[ShardingOption], devices: List[DeviceHardware]
|
|
265
|
+
) -> None:
|
|
266
|
+
for sharding_option in sharding_options:
|
|
267
|
+
if sharding_option.num_shards != len(devices):
|
|
268
|
+
raise RuntimeError(
|
|
269
|
+
f"For a uniform partition, the number of shards ({sharding_option.num_shards}) must equal the number of devices ({len(devices)})"
|
|
270
|
+
)
|
|
271
|
+
for i in range(len(devices)):
|
|
272
|
+
storage_needed = cast(Storage, sharding_option.shards[i].storage)
|
|
273
|
+
if not storage_needed.fits_in(devices[i].storage):
|
|
274
|
+
raise PlannerError(
|
|
275
|
+
f"Shard of size {storage_needed} bytes does not fit on any rank. Device memory cap: {devices[i].storage}."
|
|
276
|
+
)
|
|
277
|
+
else:
|
|
278
|
+
sharding_option.shards[i].rank = devices[i].rank
|
|
279
|
+
devices[i].storage -= storage_needed
|
|
280
|
+
devices[i].perf += cast(float, sharding_option.shards[i].perf)
|
|
@@ -0,0 +1,24 @@
|
|
|
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 cast, List
|
|
9
|
+
|
|
10
|
+
from torchrec.distributed.planner.types import PerfModel, ShardingOption, Topology
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class NoopPerfModel(PerfModel):
|
|
14
|
+
def __init__(self, topology: Topology) -> None:
|
|
15
|
+
self._topology = topology
|
|
16
|
+
|
|
17
|
+
def rate(self, plan: List[ShardingOption]) -> float:
|
|
18
|
+
perfs = [0] * self._topology.world_size
|
|
19
|
+
for sharding_option in plan:
|
|
20
|
+
for shard in sharding_option.shards:
|
|
21
|
+
# pyre-ignore [6]: Expected `typing_extensions.SupportsIndex`
|
|
22
|
+
perfs[shard.rank] += cast(float, shard.perf)
|
|
23
|
+
|
|
24
|
+
return max(perfs)
|
|
@@ -0,0 +1,290 @@
|
|
|
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 functools import reduce
|
|
9
|
+
from time import perf_counter
|
|
10
|
+
from typing import cast, Dict, List, Optional, Tuple, Union
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
import torch.distributed as dist
|
|
14
|
+
from torch import nn
|
|
15
|
+
from torchrec.distributed.collective_utils import invoke_on_rank_and_broadcast_result
|
|
16
|
+
from torchrec.distributed.planner.constants import MAX_SIZE
|
|
17
|
+
from torchrec.distributed.planner.enumerators import EmbeddingEnumerator
|
|
18
|
+
from torchrec.distributed.planner.partitioners import GreedyPerfPartitioner
|
|
19
|
+
from torchrec.distributed.planner.perf_models import NoopPerfModel
|
|
20
|
+
from torchrec.distributed.planner.proposers import (
|
|
21
|
+
GreedyProposer,
|
|
22
|
+
GridSearchProposer,
|
|
23
|
+
UniformProposer,
|
|
24
|
+
)
|
|
25
|
+
from torchrec.distributed.planner.stats import EmbeddingStats
|
|
26
|
+
from torchrec.distributed.planner.storage_reservations import (
|
|
27
|
+
HeuristicalStorageReservation,
|
|
28
|
+
)
|
|
29
|
+
from torchrec.distributed.planner.types import (
|
|
30
|
+
Enumerator,
|
|
31
|
+
ParameterConstraints,
|
|
32
|
+
Partitioner,
|
|
33
|
+
PerfModel,
|
|
34
|
+
PlannerError,
|
|
35
|
+
Proposer,
|
|
36
|
+
ShardingOption,
|
|
37
|
+
Stats,
|
|
38
|
+
Storage,
|
|
39
|
+
StorageReservation,
|
|
40
|
+
Topology,
|
|
41
|
+
)
|
|
42
|
+
from torchrec.distributed.types import (
|
|
43
|
+
EnumerableShardingSpec,
|
|
44
|
+
ModuleSharder,
|
|
45
|
+
ParameterSharding,
|
|
46
|
+
ShardingPlan,
|
|
47
|
+
ShardingPlanner,
|
|
48
|
+
ShardingType,
|
|
49
|
+
ShardMetadata,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _to_sharding_plan(
|
|
54
|
+
sharding_options: List[ShardingOption],
|
|
55
|
+
topology: Topology,
|
|
56
|
+
) -> ShardingPlan:
|
|
57
|
+
def _placement(
|
|
58
|
+
compute_device: str,
|
|
59
|
+
rank: int,
|
|
60
|
+
local_size: int,
|
|
61
|
+
) -> str:
|
|
62
|
+
param_device = compute_device
|
|
63
|
+
if compute_device == "cuda":
|
|
64
|
+
param_device = torch.device("cuda", rank % local_size)
|
|
65
|
+
return f"rank:{rank}/{param_device}"
|
|
66
|
+
|
|
67
|
+
compute_device = topology.compute_device
|
|
68
|
+
local_size = topology.local_world_size
|
|
69
|
+
|
|
70
|
+
plan = {}
|
|
71
|
+
for sharding_option in sharding_options:
|
|
72
|
+
shards = sharding_option.shards
|
|
73
|
+
sharding_type = sharding_option.sharding_type
|
|
74
|
+
|
|
75
|
+
module_plan = plan.get(sharding_option.path, {})
|
|
76
|
+
module_plan[sharding_option.name] = ParameterSharding(
|
|
77
|
+
sharding_spec=None
|
|
78
|
+
if sharding_type == ShardingType.DATA_PARALLEL.value
|
|
79
|
+
else EnumerableShardingSpec(
|
|
80
|
+
[
|
|
81
|
+
ShardMetadata(
|
|
82
|
+
shard_sizes=shard.size,
|
|
83
|
+
shard_offsets=shard.offset,
|
|
84
|
+
placement=_placement(
|
|
85
|
+
compute_device, cast(int, shard.rank), local_size
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
for shard in shards
|
|
89
|
+
]
|
|
90
|
+
),
|
|
91
|
+
sharding_type=sharding_type,
|
|
92
|
+
compute_kernel=sharding_option.compute_kernel,
|
|
93
|
+
ranks=[cast(int, shard.rank) for shard in shards],
|
|
94
|
+
)
|
|
95
|
+
plan[sharding_option.path] = module_plan
|
|
96
|
+
return ShardingPlan(plan)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class EmbeddingShardingPlanner(ShardingPlanner):
|
|
100
|
+
"""
|
|
101
|
+
Provides an optimized sharding plan for a given module with shardable parameters
|
|
102
|
+
according to the provided sharders, topology, and constraints.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def __init__(
|
|
106
|
+
self,
|
|
107
|
+
topology: Topology,
|
|
108
|
+
enumerator: Optional[Enumerator] = None,
|
|
109
|
+
storage_reservation: Optional[StorageReservation] = None,
|
|
110
|
+
proposer: Optional[Union[Proposer, List[Proposer]]] = None,
|
|
111
|
+
partitioner: Optional[Partitioner] = None,
|
|
112
|
+
performance_model: Optional[PerfModel] = None,
|
|
113
|
+
stats: Optional[Stats] = None,
|
|
114
|
+
constraints: Optional[Dict[str, ParameterConstraints]] = None,
|
|
115
|
+
debug: bool = True,
|
|
116
|
+
) -> None:
|
|
117
|
+
self._topology = topology
|
|
118
|
+
self._constraints = constraints
|
|
119
|
+
self._enumerator: Enumerator = (
|
|
120
|
+
enumerator
|
|
121
|
+
if enumerator
|
|
122
|
+
else EmbeddingEnumerator(
|
|
123
|
+
topology=topology,
|
|
124
|
+
constraints=constraints,
|
|
125
|
+
)
|
|
126
|
+
)
|
|
127
|
+
self._storage_reservation: StorageReservation = (
|
|
128
|
+
storage_reservation
|
|
129
|
+
if storage_reservation
|
|
130
|
+
else HeuristicalStorageReservation(percentage=0.15)
|
|
131
|
+
)
|
|
132
|
+
self._partitioner: Partitioner = (
|
|
133
|
+
partitioner if partitioner else GreedyPerfPartitioner()
|
|
134
|
+
)
|
|
135
|
+
if proposer:
|
|
136
|
+
self._proposers: List[Proposer] = (
|
|
137
|
+
[proposer] if not isinstance(proposer, list) else proposer
|
|
138
|
+
)
|
|
139
|
+
else:
|
|
140
|
+
self._proposers = [
|
|
141
|
+
GridSearchProposer(),
|
|
142
|
+
GreedyProposer(),
|
|
143
|
+
GreedyProposer(use_depth=False),
|
|
144
|
+
UniformProposer(),
|
|
145
|
+
]
|
|
146
|
+
self._perf_model: PerfModel = (
|
|
147
|
+
performance_model if performance_model else NoopPerfModel(topology=topology)
|
|
148
|
+
)
|
|
149
|
+
self._stats: Stats = stats if stats else EmbeddingStats()
|
|
150
|
+
self._debug = debug
|
|
151
|
+
self._num_proposals: int = 0
|
|
152
|
+
self._num_plans: int = 0
|
|
153
|
+
|
|
154
|
+
def collective_plan(
|
|
155
|
+
self,
|
|
156
|
+
module: nn.Module,
|
|
157
|
+
sharders: List[ModuleSharder[nn.Module]],
|
|
158
|
+
pg: dist.ProcessGroup,
|
|
159
|
+
) -> ShardingPlan:
|
|
160
|
+
"""
|
|
161
|
+
Call self.plan(...) on rank 0 and broadcast
|
|
162
|
+
"""
|
|
163
|
+
return invoke_on_rank_and_broadcast_result(
|
|
164
|
+
pg,
|
|
165
|
+
0,
|
|
166
|
+
self.plan,
|
|
167
|
+
module,
|
|
168
|
+
sharders,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
def plan(
|
|
172
|
+
self,
|
|
173
|
+
module: nn.Module,
|
|
174
|
+
sharders: List[ModuleSharder[nn.Module]],
|
|
175
|
+
) -> ShardingPlan:
|
|
176
|
+
|
|
177
|
+
self._num_proposals = 0
|
|
178
|
+
self._num_plans = 0
|
|
179
|
+
start_time = perf_counter()
|
|
180
|
+
best_plan = None
|
|
181
|
+
lowest_storage = Storage(MAX_SIZE, MAX_SIZE)
|
|
182
|
+
best_perf_rating = MAX_SIZE
|
|
183
|
+
|
|
184
|
+
storage_constraint: Topology = self._storage_reservation.reserve(
|
|
185
|
+
topology=self._topology,
|
|
186
|
+
module=module,
|
|
187
|
+
sharders=sharders,
|
|
188
|
+
constraints=self._constraints,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
search_space = self._enumerator.enumerate(
|
|
192
|
+
module=module,
|
|
193
|
+
sharders=sharders,
|
|
194
|
+
)
|
|
195
|
+
if not search_space:
|
|
196
|
+
# No shardable parameters
|
|
197
|
+
return ShardingPlan({})
|
|
198
|
+
|
|
199
|
+
proposal_cache: Dict[
|
|
200
|
+
Tuple[int, ...],
|
|
201
|
+
Tuple[bool, Optional[List[ShardingOption]], Optional[float]],
|
|
202
|
+
] = {}
|
|
203
|
+
|
|
204
|
+
for proposer in self._proposers:
|
|
205
|
+
proposer.load(search_space=search_space)
|
|
206
|
+
|
|
207
|
+
for proposer in self._proposers:
|
|
208
|
+
proposal = proposer.propose()
|
|
209
|
+
|
|
210
|
+
while proposal:
|
|
211
|
+
proposal_key = tuple(sorted(map(hash, proposal)))
|
|
212
|
+
if proposal_key in proposal_cache:
|
|
213
|
+
partitionable, plan, perf_rating = proposal_cache[proposal_key]
|
|
214
|
+
proposer.feedback(
|
|
215
|
+
partitionable=partitionable,
|
|
216
|
+
plan=plan,
|
|
217
|
+
perf_rating=perf_rating,
|
|
218
|
+
)
|
|
219
|
+
proposal = proposer.propose()
|
|
220
|
+
continue
|
|
221
|
+
|
|
222
|
+
self._num_proposals += 1
|
|
223
|
+
try:
|
|
224
|
+
plan = self._partitioner.partition(
|
|
225
|
+
proposal=proposal,
|
|
226
|
+
storage_constraint=storage_constraint,
|
|
227
|
+
)
|
|
228
|
+
self._num_plans += 1
|
|
229
|
+
perf_rating = self._perf_model.rate(plan=plan)
|
|
230
|
+
if perf_rating < best_perf_rating:
|
|
231
|
+
best_perf_rating = perf_rating
|
|
232
|
+
best_plan = plan
|
|
233
|
+
proposal_cache[proposal_key] = (True, plan, perf_rating)
|
|
234
|
+
proposer.feedback(
|
|
235
|
+
partitionable=True, plan=plan, perf_rating=perf_rating
|
|
236
|
+
)
|
|
237
|
+
except PlannerError:
|
|
238
|
+
current_storage = cast(
|
|
239
|
+
Storage,
|
|
240
|
+
reduce(
|
|
241
|
+
lambda x, y: x + y,
|
|
242
|
+
[
|
|
243
|
+
shard.storage
|
|
244
|
+
for option in proposal
|
|
245
|
+
for shard in option.shards
|
|
246
|
+
],
|
|
247
|
+
),
|
|
248
|
+
)
|
|
249
|
+
if current_storage < lowest_storage:
|
|
250
|
+
lowest_storage = current_storage
|
|
251
|
+
proposal_cache[proposal_key] = (False, None, None)
|
|
252
|
+
proposer.feedback(partitionable=False)
|
|
253
|
+
|
|
254
|
+
proposal = proposer.propose()
|
|
255
|
+
|
|
256
|
+
if best_plan:
|
|
257
|
+
sharding_plan = _to_sharding_plan(best_plan, self._topology)
|
|
258
|
+
|
|
259
|
+
end_time = perf_counter()
|
|
260
|
+
self._stats.log(
|
|
261
|
+
sharding_plan=sharding_plan,
|
|
262
|
+
topology=self._topology,
|
|
263
|
+
storage_reservation=self._storage_reservation,
|
|
264
|
+
num_proposals=self._num_proposals,
|
|
265
|
+
num_plans=self._num_plans,
|
|
266
|
+
run_time=end_time - start_time,
|
|
267
|
+
best_plan=best_plan,
|
|
268
|
+
debug=self._debug,
|
|
269
|
+
)
|
|
270
|
+
return sharding_plan
|
|
271
|
+
else:
|
|
272
|
+
global_storage_capacity = reduce(
|
|
273
|
+
lambda x, y: x + y,
|
|
274
|
+
[device.storage for device in self._topology.devices],
|
|
275
|
+
)
|
|
276
|
+
global_storage_constraints = reduce(
|
|
277
|
+
lambda x, y: x + y,
|
|
278
|
+
[device.storage for device in storage_constraint.devices],
|
|
279
|
+
)
|
|
280
|
+
raise PlannerError(
|
|
281
|
+
f"Unable to find a plan for this model that evaluates {self._num_proposals} proposals."
|
|
282
|
+
"\nPossible solutions:"
|
|
283
|
+
f"\n 1) Increase the number of devices ({self._topology.world_size})"
|
|
284
|
+
f"\n 2) Reduce the model size ("
|
|
285
|
+
f"\n\t Global storage: {global_storage_capacity.hbm}, "
|
|
286
|
+
f"\n\t Available for model parallel: {global_storage_constraints},"
|
|
287
|
+
f"\n\t Requirement for model parallel: {lowest_storage})"
|
|
288
|
+
f"\n 3) Reduce local batch size ({self._topology.batch_size})"
|
|
289
|
+
"\n 4) Remove planner constraints that might be reducing search space or available storage\n"
|
|
290
|
+
)
|