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.
Files changed (167) hide show
  1. examples/__init__.py +0 -0
  2. examples/datasets/__init__.py +8 -0
  3. examples/datasets/criteo_dataframes.py +101 -0
  4. examples/ray/__init__.py +0 -0
  5. examples/ray/compute_world_size.py +51 -0
  6. examples/ray/train_torchrec.py +133 -0
  7. examples/retrieval/__init__.py +6 -0
  8. examples/retrieval/data/__init__.py +6 -0
  9. examples/retrieval/data/dataloader.py +44 -0
  10. examples/retrieval/knn_index.py +67 -0
  11. examples/retrieval/modules/__init__.py +6 -0
  12. examples/retrieval/modules/two_tower.py +229 -0
  13. examples/retrieval/two_tower_retrieval.py +192 -0
  14. examples/retrieval/two_tower_train.py +253 -0
  15. examples/torcharrow/__init__.py +8 -0
  16. examples/torcharrow/dataloader.py +116 -0
  17. examples/torcharrow/run.py +126 -0
  18. fbgemm_gpu/__init__.py +16 -0
  19. fbgemm_gpu/batched_unary_embeddings_ops.py +82 -0
  20. fbgemm_gpu/enums.py +21 -0
  21. fbgemm_gpu/fbgemm_gpu_py.so +0 -0
  22. fbgemm_gpu/permute_pooled_embedding_modules.py +73 -0
  23. fbgemm_gpu/permute_pooled_embedding_modules_split.py +76 -0
  24. fbgemm_gpu/split_embedding_codegen_lookup_invokers/__init__.py +19 -0
  25. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_adagrad.py +120 -0
  26. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_adam.py +149 -0
  27. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad.py +128 -0
  28. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad_with_counter.py +134 -0
  29. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_rowwise_adagrad_with_weight_decay.py +128 -0
  30. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_approx_sgd.py +105 -0
  31. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_args.py +54 -0
  32. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_lamb.py +149 -0
  33. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_lars_sgd.py +128 -0
  34. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_partial_rowwise_adam.py +149 -0
  35. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_partial_rowwise_lamb.py +149 -0
  36. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad.py +128 -0
  37. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad_with_counter.py +134 -0
  38. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_adagrad_with_weight_decay.py +128 -0
  39. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_rowwise_weighted_adagrad.py +130 -0
  40. fbgemm_gpu/split_embedding_codegen_lookup_invokers/lookup_sgd.py +105 -0
  41. fbgemm_gpu/split_embedding_configs.py +182 -0
  42. fbgemm_gpu/split_embedding_inference_converter.py +208 -0
  43. fbgemm_gpu/split_table_batched_embeddings_ops.py +2503 -0
  44. fbgemm_gpu/uvm.py +35 -0
  45. torchrec/__init__.py +27 -0
  46. torchrec/datasets/__init__.py +31 -0
  47. torchrec/datasets/criteo.py +831 -0
  48. torchrec/datasets/movielens.py +140 -0
  49. torchrec/datasets/random.py +188 -0
  50. torchrec/datasets/scripts/__init__.py +0 -0
  51. torchrec/datasets/scripts/contiguous_preproc_criteo.py +75 -0
  52. torchrec/datasets/scripts/npy_preproc_criteo.py +73 -0
  53. torchrec/datasets/test_utils/__init__.py +0 -0
  54. torchrec/datasets/test_utils/criteo_test_utils.py +139 -0
  55. torchrec/datasets/utils.py +341 -0
  56. torchrec/distributed/__init__.py +55 -0
  57. torchrec/distributed/batched_embedding_kernel.py +697 -0
  58. torchrec/distributed/collective_utils.py +71 -0
  59. torchrec/distributed/comm.py +149 -0
  60. torchrec/distributed/comm_ops.py +930 -0
  61. torchrec/distributed/dist_data.py +949 -0
  62. torchrec/distributed/embedding.py +638 -0
  63. torchrec/distributed/embedding_kernel.py +326 -0
  64. torchrec/distributed/embedding_lookup.py +760 -0
  65. torchrec/distributed/embedding_sharding.py +660 -0
  66. torchrec/distributed/embedding_tower_sharding.py +992 -0
  67. torchrec/distributed/embedding_types.py +369 -0
  68. torchrec/distributed/embeddingbag.py +731 -0
  69. torchrec/distributed/fused_embeddingbag.py +148 -0
  70. torchrec/distributed/grouped_position_weighted.py +103 -0
  71. torchrec/distributed/model_parallel.py +555 -0
  72. torchrec/distributed/planner/__init__.py +24 -0
  73. torchrec/distributed/planner/constants.py +77 -0
  74. torchrec/distributed/planner/enumerators.py +353 -0
  75. torchrec/distributed/planner/parallelized_planners.py +307 -0
  76. torchrec/distributed/planner/partitioners.py +280 -0
  77. torchrec/distributed/planner/perf_models.py +24 -0
  78. torchrec/distributed/planner/planners.py +290 -0
  79. torchrec/distributed/planner/proposers.py +270 -0
  80. torchrec/distributed/planner/shard_estimators.py +971 -0
  81. torchrec/distributed/planner/stats.py +477 -0
  82. torchrec/distributed/planner/storage_reservations.py +168 -0
  83. torchrec/distributed/planner/types.py +423 -0
  84. torchrec/distributed/planner/utils.py +30 -0
  85. torchrec/distributed/quant_embedding.py +307 -0
  86. torchrec/distributed/quant_embedding_kernel.py +295 -0
  87. torchrec/distributed/quant_embeddingbag.py +288 -0
  88. torchrec/distributed/sharding/__init__.py +0 -0
  89. torchrec/distributed/sharding/cw_sequence_sharding.py +71 -0
  90. torchrec/distributed/sharding/cw_sharding.py +242 -0
  91. torchrec/distributed/sharding/dp_sequence_sharding.py +87 -0
  92. torchrec/distributed/sharding/dp_sharding.py +217 -0
  93. torchrec/distributed/sharding/rw_sequence_sharding.py +130 -0
  94. torchrec/distributed/sharding/rw_sharding.py +393 -0
  95. torchrec/distributed/sharding/sequence_sharding.py +69 -0
  96. torchrec/distributed/sharding/tw_sequence_sharding.py +211 -0
  97. torchrec/distributed/sharding/tw_sharding.py +469 -0
  98. torchrec/distributed/sharding/twcw_sharding.py +32 -0
  99. torchrec/distributed/sharding/twrw_sharding.py +543 -0
  100. torchrec/distributed/sharding/vb_cw_sharding.py +91 -0
  101. torchrec/distributed/sharding/vb_rw_sharding.py +250 -0
  102. torchrec/distributed/sharding/vb_sharding.py +54 -0
  103. torchrec/distributed/sharding/vb_tw_sharding.py +148 -0
  104. torchrec/distributed/sharding/vb_twrw_sharding.py +388 -0
  105. torchrec/distributed/test_utils/__init__.py +0 -0
  106. torchrec/distributed/test_utils/multi_process.py +132 -0
  107. torchrec/distributed/test_utils/test_model.py +818 -0
  108. torchrec/distributed/test_utils/test_model_parallel.py +74 -0
  109. torchrec/distributed/test_utils/test_model_parallel_base.py +117 -0
  110. torchrec/distributed/test_utils/test_sharding.py +280 -0
  111. torchrec/distributed/train_pipeline.py +531 -0
  112. torchrec/distributed/types.py +594 -0
  113. torchrec/distributed/utils.py +186 -0
  114. torchrec/fused_planner.py +57 -0
  115. torchrec/fx/__init__.py +15 -0
  116. torchrec/fx/tracer.py +78 -0
  117. torchrec/inference/__init__.py +25 -0
  118. torchrec/inference/client.py +127 -0
  119. torchrec/inference/model_packager.py +102 -0
  120. torchrec/inference/modules.py +155 -0
  121. torchrec/inference/state_dict_transform.py +103 -0
  122. torchrec/metrics/__init__.py +0 -0
  123. torchrec/metrics/auc.py +159 -0
  124. torchrec/metrics/calibration.py +108 -0
  125. torchrec/metrics/ctr.py +104 -0
  126. torchrec/metrics/metric_module.py +358 -0
  127. torchrec/metrics/metrics_config.py +152 -0
  128. torchrec/metrics/metrics_namespace.py +114 -0
  129. torchrec/metrics/model_utils.py +85 -0
  130. torchrec/metrics/mse.py +134 -0
  131. torchrec/metrics/ne.py +164 -0
  132. torchrec/metrics/rec_metric.py +686 -0
  133. torchrec/metrics/throughput.py +174 -0
  134. torchrec/models/__init__.py +27 -0
  135. torchrec/models/deepfm.py +346 -0
  136. torchrec/models/dlrm.py +708 -0
  137. torchrec/modules/__init__.py +34 -0
  138. torchrec/modules/activation.py +55 -0
  139. torchrec/modules/crossnet.py +446 -0
  140. torchrec/modules/deepfm.py +213 -0
  141. torchrec/modules/embedding_configs.py +148 -0
  142. torchrec/modules/embedding_modules.py +381 -0
  143. torchrec/modules/embedding_tower.py +150 -0
  144. torchrec/modules/feature_processor.py +74 -0
  145. torchrec/modules/fused_embedding_modules.py +625 -0
  146. torchrec/modules/lazy_extension.py +258 -0
  147. torchrec/modules/mlp.py +172 -0
  148. torchrec/modules/utils.py +117 -0
  149. torchrec/optim/__init__.py +35 -0
  150. torchrec/optim/clipping.py +53 -0
  151. torchrec/optim/fused.py +42 -0
  152. torchrec/optim/keyed.py +399 -0
  153. torchrec/optim/rowwise_adagrad.py +222 -0
  154. torchrec/optim/test_utils/__init__.py +19 -0
  155. torchrec/optim/warmup.py +147 -0
  156. torchrec/quant/__init__.py +34 -0
  157. torchrec/quant/embedding_modules.py +545 -0
  158. torchrec/sparse/__init__.py +33 -0
  159. torchrec/sparse/jagged_tensor.py +1281 -0
  160. torchrec/sparse/test_utils/__init__.py +45 -0
  161. torchrec/streamable.py +46 -0
  162. torchrec/test_utils/__init__.py +111 -0
  163. torchrec-0.2.dev0.dist-info/LICENSE +29 -0
  164. torchrec-0.2.dev0.dist-info/METADATA +175 -0
  165. torchrec-0.2.dev0.dist-info/RECORD +167 -0
  166. torchrec-0.2.dev0.dist-info/WHEEL +5 -0
  167. torchrec-0.2.dev0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,270 @@
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
+ import itertools
10
+ import logging
11
+ from typing import cast, Dict, List, Optional, Set, Tuple
12
+
13
+ from torchrec.distributed.planner.types import Proposer, ShardingOption
14
+ from torchrec.distributed.planner.utils import prod
15
+
16
+ logger: logging.Logger = logging.getLogger(__name__)
17
+
18
+ MAX_PROPOSALS: int = int(1e4)
19
+
20
+
21
+ class GreedyProposer(Proposer):
22
+ """
23
+ Proposes sharding plans in greedy fashion.
24
+
25
+ Sorts sharding options for each shardable parameter by perf.
26
+ On each iteration, finds parameter with largest current storage usage and tries its
27
+ next sharding option.
28
+ """
29
+
30
+ def __init__(self, use_depth: bool = True) -> None:
31
+ self._use_depth: bool = use_depth
32
+ self._sharding_options_by_fqn: Dict[str, List[ShardingOption]] = {}
33
+ self._current_proposal: Dict[str, int] = {}
34
+
35
+ def load(self, search_space: List[ShardingOption]) -> None:
36
+ self._reset()
37
+ for sharding_option in search_space:
38
+ fqn = sharding_option.fqn
39
+ if fqn not in self._sharding_options_by_fqn:
40
+ self._sharding_options_by_fqn[fqn] = []
41
+ self._sharding_options_by_fqn[fqn].append(sharding_option)
42
+
43
+ for sharding_options in self._sharding_options_by_fqn.values():
44
+ sharding_options.sort(
45
+ key=lambda x: _sharding_option_score(x, self._use_depth)
46
+ )
47
+
48
+ self._current_proposal = {
49
+ fqn: 0 for fqn in self._sharding_options_by_fqn.keys()
50
+ }
51
+
52
+ def _reset(self) -> None:
53
+ self._sharding_options_by_fqn = {}
54
+ self._current_proposal = {}
55
+
56
+ def propose(self) -> Optional[List[ShardingOption]]:
57
+ if self._current_proposal:
58
+ return copy.deepcopy(
59
+ [
60
+ self._sharding_options_by_fqn[fqn][index]
61
+ for fqn, index in self._current_proposal.items()
62
+ ]
63
+ )
64
+ else:
65
+ return None
66
+
67
+ def feedback(
68
+ self,
69
+ partitionable: bool,
70
+ plan: Optional[List[ShardingOption]] = None,
71
+ perf_rating: Optional[float] = None,
72
+ ) -> None:
73
+ # static strategy, ignore feedback and just provide next proposal
74
+ largest_fqn: Optional[str] = None
75
+ largest_storage: Tuple[float, float, float, float] = (0, 0, 0, 0)
76
+ for fqn, sharding_options in self._sharding_options_by_fqn.items():
77
+ index = self._current_proposal[fqn]
78
+ if index + 1 < len(sharding_options):
79
+ sharding_option = sharding_options[index]
80
+ current_storage = (
81
+ # pyre-fixme [16]: `Optional` has no attribute `hbm`
82
+ max([shard.storage.hbm for shard in sharding_option.shards]),
83
+ sum([shard.storage.hbm for shard in sharding_option.shards]),
84
+ # pyre-fixme [16]: `Optional` has no attribute `ddr`
85
+ max([shard.storage.ddr for shard in sharding_option.shards]),
86
+ sum([shard.storage.ddr for shard in sharding_option.shards]),
87
+ )
88
+ if current_storage > largest_storage:
89
+ largest_fqn = fqn
90
+ largest_storage = current_storage
91
+
92
+ if largest_fqn is not None:
93
+ self._current_proposal[largest_fqn] += 1
94
+ else:
95
+ self._current_proposal = {}
96
+
97
+
98
+ class UniformProposer(Proposer):
99
+ """
100
+ Proposes uniform sharding plans, plans that have the same sharding type for all
101
+ sharding options.
102
+ """
103
+
104
+ def __init__(self, use_depth: bool = True) -> None:
105
+ self._use_depth: bool = use_depth
106
+ self._grouped_sharding_options: List[List[ShardingOption]] = []
107
+ self._proposal_index: int = 0
108
+
109
+ def load(self, search_space: List[ShardingOption]) -> None:
110
+ self._reset()
111
+ all_fqns = set()
112
+ sharding_options_by_type_and_fqn: Dict[
113
+ str, Dict[str, List[ShardingOption]]
114
+ ] = {}
115
+
116
+ for sharding_option in search_space:
117
+ sharding_type = sharding_option.sharding_type
118
+ fqn = sharding_option.fqn
119
+ all_fqns.add(fqn)
120
+
121
+ if sharding_type not in sharding_options_by_type_and_fqn:
122
+ sharding_options_by_type_and_fqn[sharding_type] = {}
123
+ if fqn not in sharding_options_by_type_and_fqn[sharding_type]:
124
+ sharding_options_by_type_and_fqn[sharding_type][fqn] = []
125
+
126
+ sharding_options_by_type_and_fqn[sharding_type][fqn].append(sharding_option)
127
+
128
+ for sharding_options_by_fqn in sharding_options_by_type_and_fqn.values():
129
+ for sharding_options in sharding_options_by_fqn.values():
130
+ sharding_options.sort(
131
+ key=lambda x: _sharding_option_score(x, self._use_depth)
132
+ )
133
+
134
+ for sharding_options_by_fqn in sharding_options_by_type_and_fqn.values():
135
+ if sharding_options_by_fqn.keys() == all_fqns:
136
+ self._grouped_sharding_options.append(
137
+ [
138
+ sorted_sharding_options[0]
139
+ for sorted_sharding_options in sharding_options_by_fqn.values()
140
+ ]
141
+ )
142
+
143
+ def _reset(self) -> None:
144
+ self._grouped_sharding_options = []
145
+ self._proposal_index = 0
146
+
147
+ def propose(self) -> Optional[List[ShardingOption]]:
148
+ if self._proposal_index < len(self._grouped_sharding_options):
149
+ return copy.deepcopy(self._grouped_sharding_options[self._proposal_index])
150
+ else:
151
+ return None
152
+
153
+ def feedback(
154
+ self,
155
+ partitionable: bool,
156
+ plan: Optional[List[ShardingOption]] = None,
157
+ perf_rating: Optional[float] = None,
158
+ ) -> None:
159
+ # static strategy, ignore feedback and just provide next proposal
160
+ self._proposal_index += 1
161
+
162
+
163
+ class GridSearchProposer(Proposer):
164
+ def __init__(self, max_proposals: int = MAX_PROPOSALS) -> None:
165
+ self._max_proposals: int = max_proposals
166
+ self._sharding_options_by_fqn: Dict[str, List[ShardingOption]] = {}
167
+ self._proposal_index: int = 0
168
+ self._proposals: List[List[int]] = []
169
+
170
+ def load(self, search_space: List[ShardingOption]) -> None:
171
+ self._reset()
172
+ for sharding_option in search_space:
173
+ fqn = sharding_option.fqn
174
+ if fqn not in self._sharding_options_by_fqn:
175
+ self._sharding_options_by_fqn[fqn] = []
176
+ self._sharding_options_by_fqn[fqn].append(sharding_option)
177
+
178
+ for sharding_options in self._sharding_options_by_fqn.values():
179
+ sharding_options.sort(key=lambda x: _sharding_option_score(x))
180
+
181
+ total_proposals = prod(
182
+ [
183
+ len(sharding_options)
184
+ for sharding_options in self._sharding_options_by_fqn.values()
185
+ ]
186
+ )
187
+ if total_proposals > self._max_proposals:
188
+ total_proposals = (
189
+ "{:.2e}".format(total_proposals)
190
+ if total_proposals > 1e6
191
+ else total_proposals
192
+ )
193
+ logger.info(
194
+ "Skipping grid search proposer as there are too many proposals.\n"
195
+ f"Total proposals to search: {total_proposals}\n"
196
+ f"Max proposals allowed: {self._max_proposals}\n"
197
+ )
198
+ return
199
+ sharding_options_by_fqn_indices = [
200
+ range(len(sharding_options))
201
+ for sharding_options in self._sharding_options_by_fqn.values()
202
+ ]
203
+ self._proposals = list(itertools.product(*sharding_options_by_fqn_indices))
204
+
205
+ def _reset(self) -> None:
206
+ self._sharding_options_by_fqn = {}
207
+ self._proposal_index = 0
208
+ self._proposals = []
209
+
210
+ def propose(self) -> Optional[List[ShardingOption]]:
211
+ if self._proposals and self._proposal_index < len(self._proposals):
212
+ proposal_indices = self._proposals[self._proposal_index]
213
+ return [
214
+ sharding_options[index]
215
+ for index, sharding_options in zip(
216
+ proposal_indices, self._sharding_options_by_fqn.values()
217
+ )
218
+ ]
219
+ else:
220
+ return None
221
+
222
+ def feedback(
223
+ self,
224
+ partitionable: bool,
225
+ plan: Optional[List[ShardingOption]] = None,
226
+ perf_rating: Optional[float] = None,
227
+ ) -> None:
228
+ # static strategy, ignore feedback and just provide next proposal
229
+ self._proposal_index += 1
230
+
231
+
232
+ def _sharding_option_score(
233
+ sharding_option: ShardingOption, use_depth: bool = True
234
+ ) -> float:
235
+ return (
236
+ max([cast(float, shard.perf) for shard in sharding_option.shards])
237
+ if use_depth
238
+ else sum([cast(float, shard.perf) for shard in sharding_option.shards])
239
+ )
240
+
241
+
242
+ def proposers_to_proposals_list(
243
+ proposers_list: List[Proposer], search_space: List[ShardingOption]
244
+ ) -> List[List[ShardingOption]]:
245
+ """
246
+ only works for static_feedback proposers (the path of proposals to check is independent of the performance of the proposals)
247
+ """
248
+
249
+ proposals_list = []
250
+
251
+ proposal_cache: Set[Tuple[int, ...]] = set()
252
+
253
+ for proposer in proposers_list:
254
+ proposer.load(search_space=search_space)
255
+
256
+ for proposer in proposers_list:
257
+ proposal = proposer.propose()
258
+
259
+ while proposal:
260
+ proposal_key = tuple(sorted(map(hash, proposal)))
261
+ proposer.feedback(partitionable=True)
262
+ if proposal_key in proposal_cache:
263
+ proposal = proposer.propose()
264
+ continue
265
+
266
+ proposals_list.append(proposal)
267
+ proposal_cache.add(proposal_key)
268
+ proposal = proposer.propose()
269
+
270
+ return proposals_list