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,134 @@
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 Any, cast, Dict, List, Optional, Type
9
+
10
+ import torch
11
+ from torchrec.metrics.metrics_namespace import MetricName, MetricNamespace, MetricPrefix
12
+ from torchrec.metrics.rec_metric import (
13
+ MetricComputationReport,
14
+ RecMetric,
15
+ RecMetricComputation,
16
+ RecMetricException,
17
+ )
18
+
19
+
20
+ ERROR_SUM = "error_sum"
21
+ WEIGHTED_NUM_SAMPES = "weighted_num_samples"
22
+
23
+
24
+ def compute_mse(
25
+ error_sum: torch.Tensor, weighted_num_samples: torch.Tensor
26
+ ) -> torch.Tensor:
27
+ return error_sum / weighted_num_samples
28
+
29
+
30
+ def compute_rmse(
31
+ error_sum: torch.Tensor, weighted_num_samples: torch.Tensor
32
+ ) -> torch.Tensor:
33
+ return torch.sqrt(error_sum / weighted_num_samples)
34
+
35
+
36
+ def compute_error_sum(
37
+ labels: torch.Tensor, predictions: torch.Tensor, weights: torch.Tensor
38
+ ) -> torch.Tensor:
39
+ predictions = predictions.double()
40
+ return torch.sum(weights * torch.square(labels - predictions), dim=-1)
41
+
42
+
43
+ def get_mse_states(
44
+ labels: torch.Tensor, predictions: torch.Tensor, weights: torch.Tensor
45
+ ) -> Dict[str, torch.Tensor]:
46
+ return {
47
+ "error_sum": compute_error_sum(labels, predictions, weights),
48
+ "weighted_num_samples": torch.sum(weights, dim=-1),
49
+ }
50
+
51
+
52
+ class MSEMetricComputation(RecMetricComputation):
53
+ r"""
54
+ This class implementation the RecMetricComputation for MSE, i.e. Mean Square Error.
55
+
56
+ The constructer arguments are defined in RecMetricComputation.
57
+ See the docstring of RecMetricComputation for more detail.
58
+ """
59
+
60
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
61
+ super().__init__(*args, **kwargs)
62
+ self._add_state(
63
+ "error_sum",
64
+ torch.zeros(self._n_tasks, dtype=torch.double),
65
+ add_window_state=True,
66
+ dist_reduce_fx="sum",
67
+ persistent=True,
68
+ )
69
+ self._add_state(
70
+ "weighted_num_samples",
71
+ torch.zeros(self._n_tasks, dtype=torch.double),
72
+ add_window_state=True,
73
+ dist_reduce_fx="sum",
74
+ persistent=True,
75
+ )
76
+
77
+ def update(
78
+ self,
79
+ *,
80
+ predictions: Optional[torch.Tensor],
81
+ labels: torch.Tensor,
82
+ weights: Optional[torch.Tensor],
83
+ ) -> None:
84
+ if predictions is None or weights is None:
85
+ raise RecMetricException(
86
+ "Inputs 'predictions' and 'weights' should not be None for MSEMetricComputation update"
87
+ )
88
+ states = get_mse_states(labels, predictions, weights)
89
+ num_samples = predictions.shape[-1]
90
+ for state_name, state_value in states.items():
91
+ state = getattr(self, state_name)
92
+ state += state_value
93
+ self._aggregate_window_state(state_name, state_value, num_samples)
94
+
95
+ def _compute(self) -> List[MetricComputationReport]:
96
+ return [
97
+ MetricComputationReport(
98
+ name=MetricName.MSE,
99
+ metric_prefix=MetricPrefix.LIFETIME,
100
+ value=compute_mse(
101
+ cast(torch.Tensor, self.error_sum),
102
+ cast(torch.Tensor, self.weighted_num_samples),
103
+ ),
104
+ ),
105
+ MetricComputationReport(
106
+ name=MetricName.RMSE,
107
+ metric_prefix=MetricPrefix.LIFETIME,
108
+ value=compute_rmse(
109
+ cast(torch.Tensor, self.error_sum),
110
+ cast(torch.Tensor, self.weighted_num_samples),
111
+ ),
112
+ ),
113
+ MetricComputationReport(
114
+ name=MetricName.MSE,
115
+ metric_prefix=MetricPrefix.WINDOW,
116
+ value=compute_mse(
117
+ self.get_window_state(ERROR_SUM),
118
+ self.get_window_state(WEIGHTED_NUM_SAMPES),
119
+ ),
120
+ ),
121
+ MetricComputationReport(
122
+ name=MetricName.RMSE,
123
+ metric_prefix=MetricPrefix.WINDOW,
124
+ value=compute_rmse(
125
+ self.get_window_state(ERROR_SUM),
126
+ self.get_window_state(WEIGHTED_NUM_SAMPES),
127
+ ),
128
+ ),
129
+ ]
130
+
131
+
132
+ class MSEMetric(RecMetric):
133
+ _namespace: MetricNamespace = MetricNamespace.MSE
134
+ _computation_class: Type[RecMetricComputation] = MSEMetricComputation
torchrec/metrics/ne.py ADDED
@@ -0,0 +1,164 @@
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 Any, cast, Dict, List, Optional, Type
9
+
10
+ import torch
11
+ from torchrec.metrics.metrics_namespace import MetricName, MetricNamespace, MetricPrefix
12
+ from torchrec.metrics.rec_metric import (
13
+ MetricComputationReport,
14
+ RecMetric,
15
+ RecMetricComputation,
16
+ RecMetricException,
17
+ )
18
+
19
+
20
+ def compute_cross_entropy(
21
+ labels: torch.Tensor,
22
+ predictions: torch.Tensor,
23
+ weights: torch.Tensor,
24
+ eta: float,
25
+ ) -> torch.Tensor:
26
+ predictions = predictions.double()
27
+ predictions.clamp_(min=eta, max=1 - eta)
28
+ cross_entropy = -weights * labels * torch.log2(predictions) - weights * (
29
+ 1.0 - labels
30
+ ) * torch.log2(1.0 - predictions)
31
+ return cross_entropy
32
+
33
+
34
+ def _compute_cross_entropy_norm(
35
+ mean_label: torch.Tensor,
36
+ pos_labels: torch.Tensor,
37
+ neg_labels: torch.Tensor,
38
+ eta: float,
39
+ ) -> torch.Tensor:
40
+ mean_label = mean_label.double()
41
+ mean_label.clamp_(min=eta, max=1 - eta)
42
+ return -pos_labels * torch.log2(mean_label) - neg_labels * torch.log2(
43
+ 1.0 - mean_label
44
+ )
45
+
46
+
47
+ def compute_ne(
48
+ ce_sum: torch.Tensor,
49
+ weighted_num_samples: torch.Tensor,
50
+ pos_labels: torch.Tensor,
51
+ neg_labels: torch.Tensor,
52
+ eta: float,
53
+ ) -> torch.Tensor:
54
+ mean_label = pos_labels / weighted_num_samples
55
+ ce_norm = _compute_cross_entropy_norm(mean_label, pos_labels, neg_labels, eta)
56
+ return ce_sum / ce_norm
57
+
58
+
59
+ def get_ne_states(
60
+ labels: torch.Tensor, predictions: torch.Tensor, weights: torch.Tensor, eta: float
61
+ ) -> Dict[str, torch.Tensor]:
62
+ cross_entropy = compute_cross_entropy(
63
+ labels,
64
+ predictions,
65
+ weights,
66
+ eta,
67
+ )
68
+ return {
69
+ "cross_entropy_sum": torch.sum(cross_entropy, dim=-1),
70
+ "weighted_num_samples": torch.sum(weights, dim=-1),
71
+ "pos_labels": torch.sum(weights * labels, dim=-1),
72
+ "neg_labels": torch.sum(weights * (1.0 - labels), dim=-1),
73
+ }
74
+
75
+
76
+ class NEMetricComputation(RecMetricComputation):
77
+ r"""
78
+ This class implementation the RecMetricComputation for NE, i.e. Normalized Entropy.
79
+
80
+ The constructer arguments are defined in RecMetricComputation.
81
+ See the docstring of RecMetricComputation for more detail.
82
+ """
83
+
84
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
85
+ super().__init__(*args, **kwargs)
86
+ self._add_state(
87
+ "cross_entropy_sum",
88
+ torch.zeros(self._n_tasks, dtype=torch.double),
89
+ add_window_state=True,
90
+ dist_reduce_fx="sum",
91
+ persistent=True,
92
+ )
93
+ self._add_state(
94
+ "weighted_num_samples",
95
+ torch.zeros(self._n_tasks, dtype=torch.double),
96
+ add_window_state=True,
97
+ dist_reduce_fx="sum",
98
+ persistent=True,
99
+ )
100
+ self._add_state(
101
+ "pos_labels",
102
+ torch.zeros(self._n_tasks, dtype=torch.double),
103
+ add_window_state=True,
104
+ dist_reduce_fx="sum",
105
+ persistent=True,
106
+ )
107
+ self._add_state(
108
+ "neg_labels",
109
+ torch.zeros(self._n_tasks, dtype=torch.double),
110
+ add_window_state=True,
111
+ dist_reduce_fx="sum",
112
+ persistent=True,
113
+ )
114
+ self.eta = 1e-12
115
+
116
+ def update(
117
+ self,
118
+ *,
119
+ predictions: Optional[torch.Tensor],
120
+ labels: torch.Tensor,
121
+ weights: Optional[torch.Tensor],
122
+ ) -> None:
123
+ if predictions is None or weights is None:
124
+ raise RecMetricException(
125
+ "Inputs 'predictions' and 'weights' should not be None for NEMetricComputation update"
126
+ )
127
+ states = get_ne_states(labels, predictions, weights, self.eta)
128
+ num_samples = predictions.shape[-1]
129
+
130
+ for state_name, state_value in states.items():
131
+ state = getattr(self, state_name)
132
+ state += state_value
133
+ self._aggregate_window_state(state_name, state_value, num_samples)
134
+
135
+ def _compute(self) -> List[MetricComputationReport]:
136
+ return [
137
+ MetricComputationReport(
138
+ name=MetricName.NE,
139
+ metric_prefix=MetricPrefix.LIFETIME,
140
+ value=compute_ne(
141
+ cast(torch.Tensor, self.cross_entropy_sum),
142
+ cast(torch.Tensor, self.weighted_num_samples),
143
+ cast(torch.Tensor, self.pos_labels),
144
+ cast(torch.Tensor, self.neg_labels),
145
+ self.eta,
146
+ ),
147
+ ),
148
+ MetricComputationReport(
149
+ name=MetricName.NE,
150
+ metric_prefix=MetricPrefix.WINDOW,
151
+ value=compute_ne(
152
+ self.get_window_state("cross_entropy_sum"),
153
+ self.get_window_state("weighted_num_samples"),
154
+ self.get_window_state("pos_labels"),
155
+ self.get_window_state("neg_labels"),
156
+ self.eta,
157
+ ),
158
+ ),
159
+ ]
160
+
161
+
162
+ class NEMetric(RecMetric):
163
+ _namespace: MetricNamespace = MetricNamespace.NE
164
+ _computation_class: Type[RecMetricComputation] = NEMetricComputation