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,423 @@
1
+ #!/usr/bin/env python3
2
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ # All rights reserved.
4
+ #
5
+ # This source code is licensed under the BSD-style license found in the
6
+ # LICENSE file in the root directory of this source tree.
7
+
8
+ import abc
9
+ from dataclasses import dataclass, field
10
+ from enum import Enum
11
+ from typing import cast, Dict, List, Optional, Tuple, Union
12
+
13
+ import torch
14
+ from torch import nn
15
+ from torchrec.distributed.planner.constants import (
16
+ BATCH_SIZE,
17
+ CROSS_NODE_BANDWIDTH,
18
+ DDR_CAP,
19
+ HBM_CAP,
20
+ INTRA_NODE_BANDWIDTH,
21
+ POOLING_FACTOR,
22
+ )
23
+ from torchrec.distributed.types import ModuleSharder, ShardingPlan
24
+ from torchrec.modules.embedding_modules import EmbeddingCollectionInterface
25
+
26
+ # ---- TOPOLOGY ---- #
27
+
28
+
29
+ @dataclass(repr=True, order=True, eq=True)
30
+ class Storage:
31
+ """
32
+ Representation of the storage capacities of a hardware used in training.
33
+ """
34
+
35
+ hbm: int
36
+ ddr: int
37
+
38
+ def __add__(self, other: "Storage") -> "Storage":
39
+ return Storage(
40
+ hbm=self.hbm + other.hbm,
41
+ ddr=self.ddr + other.ddr,
42
+ )
43
+
44
+ def __sub__(self, other: "Storage") -> "Storage":
45
+ return Storage(
46
+ hbm=self.hbm - other.hbm,
47
+ ddr=self.ddr - other.ddr,
48
+ )
49
+
50
+ def __hash__(self) -> int:
51
+ return hash((self.hbm, self.ddr))
52
+
53
+ def fits_in(self, other: "Storage") -> bool:
54
+ return self.hbm <= other.hbm and self.ddr <= other.ddr
55
+
56
+
57
+ @dataclass
58
+ class DeviceHardware:
59
+ """
60
+ Representation of a device in a process group. 'perf' is an estimation of network,
61
+ CPU, and storage usages.
62
+ """
63
+
64
+ rank: int
65
+ storage: Storage
66
+ perf: float = 0
67
+
68
+
69
+ class Topology:
70
+ def __init__(
71
+ self,
72
+ world_size: int,
73
+ compute_device: str,
74
+ hbm_cap: Optional[int] = None,
75
+ ddr_cap: int = DDR_CAP,
76
+ local_world_size: Optional[int] = None,
77
+ intra_host_bw: float = INTRA_NODE_BANDWIDTH,
78
+ inter_host_bw: float = CROSS_NODE_BANDWIDTH,
79
+ batch_size: int = BATCH_SIZE,
80
+ ) -> None:
81
+ """
82
+ Representation of a network of devices in a cluster.
83
+ """
84
+ # validate input
85
+ assert compute_device in [
86
+ "cpu",
87
+ "cuda",
88
+ ], f"unsupported compute device {compute_device}"
89
+
90
+ self._compute_device = compute_device
91
+ self._world_size = world_size
92
+
93
+ hbm_per_device = 0
94
+ if self._compute_device == "cuda":
95
+ hbm_per_device = hbm_cap if hbm_cap else HBM_CAP
96
+
97
+ self._devices: List[DeviceHardware] = []
98
+ for rank in range(world_size):
99
+ self._devices.append(
100
+ DeviceHardware(
101
+ rank=rank,
102
+ storage=Storage(hbm=hbm_per_device, ddr=ddr_cap),
103
+ )
104
+ )
105
+
106
+ self._local_world_size: int = (
107
+ local_world_size if local_world_size else world_size
108
+ )
109
+ self._intra_host_bw = intra_host_bw
110
+ self._inter_host_bw = inter_host_bw
111
+ self._batch_size = batch_size
112
+
113
+ @property
114
+ def batch_size(self) -> int:
115
+ return self._batch_size
116
+
117
+ @property
118
+ def compute_device(self) -> str:
119
+ return self._compute_device
120
+
121
+ @property
122
+ def devices(self) -> List[DeviceHardware]:
123
+ return self._devices
124
+
125
+ @property
126
+ def world_size(self) -> int:
127
+ return self._world_size
128
+
129
+ @property
130
+ def local_world_size(self) -> int:
131
+ return self._local_world_size
132
+
133
+ @property
134
+ def intra_host_bw(self) -> float:
135
+ return self._intra_host_bw
136
+
137
+ @property
138
+ def inter_host_bw(self) -> float:
139
+ return self._inter_host_bw
140
+
141
+ def __repr__(self) -> str:
142
+ topology_repr: str = f"world_size={self._world_size} \n"
143
+ topology_repr += f"compute_device={self._compute_device}\n"
144
+ topology_repr += "devices=\n"
145
+ for idx, device in enumerate(self._devices):
146
+ topology_repr += f"\tdevice {idx} {device}\n"
147
+ topology_repr += f"local_world_size={self._local_world_size} \n"
148
+ topology_repr += f"intra_host_bw={self._intra_host_bw} \n"
149
+ topology_repr += f"inter_host_bw={self._inter_host_bw} \n"
150
+ return topology_repr
151
+
152
+
153
+ # ---- INPUT / OUTPUT ----- #
154
+
155
+
156
+ @dataclass
157
+ class Shard:
158
+ """
159
+ Representation of a subset of an embedding table. 'size' and 'offset' fully
160
+ determine the tensors in the shard. 'storage' is an estimation of how much it takes
161
+ to store the shard with an estimation 'perf'.
162
+ """
163
+
164
+ size: List[int]
165
+ offset: List[int]
166
+ storage: Optional[Storage] = None
167
+ perf: Optional[float] = None
168
+ rank: Optional[int] = None
169
+
170
+ def __hash__(self) -> int:
171
+ return hash(
172
+ (
173
+ tuple(self.size),
174
+ tuple(self.offset),
175
+ self.storage,
176
+ self.perf,
177
+ self.rank,
178
+ )
179
+ )
180
+
181
+
182
+ @dataclass
183
+ class ShardingOption:
184
+ """
185
+ One way of sharding an embedding table.
186
+ """
187
+
188
+ name: str
189
+ tensor: torch.Tensor
190
+ module: Tuple[str, nn.Module]
191
+ upstream_modules: List[Tuple[str, nn.Module]]
192
+ downstream_modules: List[Tuple[str, nn.Module]]
193
+ input_lengths: List[float]
194
+ batch_size: int # per single device
195
+ sharding_type: str
196
+ partition_by: str # {DEVICE, HOST, UNIFORM}
197
+ compute_kernel: str
198
+ # relevant to planner output, must be populated if sharding option
199
+ # part of final solution
200
+ shards: List[Shard] = field(default_factory=list)
201
+ dependency: Optional[str] = None
202
+
203
+ @property
204
+ def fqn(self) -> str:
205
+ return self.module[0] + "." + self.name
206
+
207
+ @property
208
+ def path(self) -> str:
209
+ return self.module[0]
210
+
211
+ @property
212
+ def num_shards(self) -> int:
213
+ return len(self.shards)
214
+
215
+ @property
216
+ def num_inputs(self) -> int:
217
+ return len(self.input_lengths)
218
+
219
+ @property
220
+ def total_storage(self) -> Storage:
221
+ storage: Storage = Storage(hbm=0, ddr=0)
222
+ for shard in self.shards:
223
+ storage += cast(Storage, shard.storage)
224
+ return storage
225
+
226
+ @property
227
+ def is_pooled(self) -> bool:
228
+ if isinstance(self.module[1], EmbeddingCollectionInterface):
229
+ return False
230
+ for name, module in self.module[1].named_modules():
231
+ if self.name in name:
232
+ if isinstance(module, EmbeddingCollectionInterface):
233
+ return False
234
+ return True
235
+
236
+ def __hash__(self) -> int:
237
+ return hash(
238
+ (
239
+ self.fqn,
240
+ self.sharding_type,
241
+ self.compute_kernel,
242
+ tuple(self.shards),
243
+ )
244
+ )
245
+
246
+
247
+ class PartitionByType(Enum):
248
+ """
249
+ Well-known partition types.
250
+ """
251
+
252
+ # Partitioning based on device
253
+ DEVICE = "device"
254
+ # Partitioning based on host
255
+ HOST = "host"
256
+ # Uniform, (ie. fixed layout)
257
+ UNIFORM = "uniform"
258
+
259
+
260
+ @dataclass
261
+ class ParameterConstraints:
262
+ """
263
+ Stores user provided constraints around the sharding plan.
264
+
265
+ If provided, `pooling_factors`, `num_poolings`, and `batch_sizes` must match in
266
+ length, as per sample.
267
+ """
268
+
269
+ sharding_types: Optional[List[str]] = None
270
+ compute_kernels: Optional[List[str]] = None
271
+ min_partition: Optional[int] = None # CW sharding
272
+ caching_ratio: Optional[float] = None # UVM caching
273
+ pooling_factors: List[float] = field(
274
+ default_factory=lambda: [POOLING_FACTOR]
275
+ ) # average number of embedding lookups required per sample
276
+ num_poolings: Optional[List[float]] = None # number of poolings per sample in batch
277
+ batch_sizes: Optional[List[int]] = None # batch size per input feature
278
+
279
+
280
+ class PlannerError(Exception):
281
+ ...
282
+
283
+
284
+ # ---- PLANNER COMPONENTS ---- #
285
+
286
+
287
+ class StorageReservation(abc.ABC):
288
+ """
289
+ Reserves storage space for non-sharded parts of the model.
290
+ """
291
+
292
+ @abc.abstractmethod
293
+ def reserve(
294
+ self,
295
+ topology: Topology,
296
+ module: nn.Module,
297
+ sharders: List[ModuleSharder[nn.Module]],
298
+ constraints: Optional[Dict[str, ParameterConstraints]] = None,
299
+ ) -> Topology:
300
+ ...
301
+
302
+
303
+ class PerfModel(abc.ABC):
304
+ @abc.abstractmethod
305
+ def rate(self, plan: List[ShardingOption]) -> float:
306
+ ...
307
+
308
+
309
+ class ShardEstimator(abc.ABC):
310
+ """
311
+ Estimates shard perf or storage, requires fully specified sharding options.
312
+ """
313
+
314
+ @abc.abstractmethod
315
+ def __init__(
316
+ self,
317
+ topology: Topology,
318
+ constraints: Optional[Dict[str, ParameterConstraints]] = None,
319
+ ) -> None:
320
+ ...
321
+
322
+ @abc.abstractmethod
323
+ def estimate(
324
+ self,
325
+ sharding_options: List[ShardingOption],
326
+ sharder_map: Optional[Dict[str, ModuleSharder[nn.Module]]] = None,
327
+ ) -> None:
328
+ # update sharding_options with per shard estimate in-place
329
+ ...
330
+
331
+
332
+ class Enumerator(abc.ABC):
333
+ """
334
+ Generates all relevant sharding options for given topology, constraints, nn.Module,
335
+ and sharders.
336
+ """
337
+
338
+ @abc.abstractmethod
339
+ def __init__(
340
+ self,
341
+ topology: Topology,
342
+ constraints: Optional[Dict[str, ParameterConstraints]] = None,
343
+ estimator: Optional[Union[ShardEstimator, List[ShardEstimator]]] = None,
344
+ ) -> None:
345
+ ...
346
+
347
+ @abc.abstractmethod
348
+ def enumerate(
349
+ self,
350
+ module: nn.Module,
351
+ sharders: List[ModuleSharder[nn.Module]],
352
+ ) -> List[ShardingOption]:
353
+ """
354
+ See class description.
355
+ """
356
+ ...
357
+
358
+
359
+ class Proposer(abc.ABC):
360
+ """
361
+ Prosposes complete lists of sharding options which can be parititioned to generate a
362
+ plan.
363
+ """
364
+
365
+ @abc.abstractmethod
366
+ def load(
367
+ self,
368
+ search_space: List[ShardingOption],
369
+ ) -> None:
370
+ ...
371
+
372
+ @abc.abstractmethod
373
+ def feedback(
374
+ self,
375
+ partitionable: bool,
376
+ plan: Optional[List[ShardingOption]] = None,
377
+ perf_rating: Optional[float] = None,
378
+ ) -> None:
379
+ ...
380
+
381
+ @abc.abstractmethod
382
+ def propose(self) -> Optional[List[ShardingOption]]:
383
+ ...
384
+
385
+
386
+ class Partitioner(abc.ABC):
387
+ """
388
+ Partitions shards.
389
+
390
+ Today we have multiple strategies ie. (Greedy, BLDM, Linear).
391
+ """
392
+
393
+ @abc.abstractmethod
394
+ def partition(
395
+ self,
396
+ proposal: List[ShardingOption],
397
+ storage_constraint: Topology,
398
+ ) -> List[ShardingOption]:
399
+ # modifies sharding_options and topology in-place
400
+ ...
401
+
402
+
403
+ class Stats(abc.ABC):
404
+ """
405
+ Logs statistics related to the sharding plan.
406
+ """
407
+
408
+ @abc.abstractmethod
409
+ def log(
410
+ self,
411
+ sharding_plan: ShardingPlan,
412
+ topology: Topology,
413
+ storage_reservation: StorageReservation,
414
+ num_proposals: int,
415
+ num_plans: int,
416
+ run_time: float,
417
+ best_plan: List[ShardingOption],
418
+ debug: bool = False,
419
+ ) -> None:
420
+ """
421
+ See class description
422
+ """
423
+ ...
@@ -0,0 +1,30 @@
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 operator
9
+ from functools import reduce
10
+ from typing import Any, Iterable, Type, Union
11
+
12
+ # pyre-ignore[2]
13
+ def sharder_name(t: Type[Any]) -> str:
14
+ return t.__module__ + "." + t.__name__
15
+
16
+
17
+ def bytes_to_gb(num_bytes: int) -> float:
18
+ return float(num_bytes / (1024 * 1024 * 1024))
19
+
20
+
21
+ def bytes_to_mb(num_bytes: Union[float, int]) -> float:
22
+ return float(num_bytes / (1024 * 1024))
23
+
24
+
25
+ def gb_to_bytes(gb: float) -> int:
26
+ return int(gb * 1024 * 1024 * 1024)
27
+
28
+
29
+ def prod(iterable: Iterable[int]) -> int:
30
+ return reduce(operator.mul, iterable, 1)