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
torchrec/optim/warmup.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
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 logging
|
|
9
|
+
import math
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import Enum, unique
|
|
12
|
+
from typing import Any, List, Tuple
|
|
13
|
+
|
|
14
|
+
import torch
|
|
15
|
+
from torchrec.optim.keyed import KeyedOptimizer, OptimizerWrapper
|
|
16
|
+
|
|
17
|
+
logger: logging.Logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@unique
|
|
21
|
+
class WarmupPolicy(Enum):
|
|
22
|
+
NONE = "none"
|
|
23
|
+
LINEAR = "linear"
|
|
24
|
+
CONSTANT = "constant"
|
|
25
|
+
POLY = "poly"
|
|
26
|
+
STEP = "step"
|
|
27
|
+
INVSQRT = "inv_sqrt" # inverse square root
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class WarmupStage:
|
|
32
|
+
policy: WarmupPolicy = WarmupPolicy.LINEAR
|
|
33
|
+
max_iters: int = 1
|
|
34
|
+
value: float = 1.0
|
|
35
|
+
lr_scale: float = 1.0
|
|
36
|
+
# used as number denominator for iters in poly decay
|
|
37
|
+
# default to max_iters if not set to value > 0
|
|
38
|
+
# also used as stepsize in step decay
|
|
39
|
+
# default to 1 if not set to value > 0
|
|
40
|
+
decay_iters: int = -1
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _lr_stages(stages: List[WarmupStage]) -> List[WarmupStage]:
|
|
44
|
+
last_stage = WarmupStage(policy=WarmupPolicy.NONE, max_iters=1 << 63, value=1.0)
|
|
45
|
+
if len(stages) == 0:
|
|
46
|
+
return [last_stage]
|
|
47
|
+
|
|
48
|
+
start_iter = 0
|
|
49
|
+
for stage in stages:
|
|
50
|
+
assert stage.max_iters > start_iter, (
|
|
51
|
+
f"Max iter of the stage {stage} must be greater than the previous "
|
|
52
|
+
f"max iter {start_iter}"
|
|
53
|
+
)
|
|
54
|
+
start_iter = stage.max_iters
|
|
55
|
+
if stage.decay_iters <= 0:
|
|
56
|
+
if stage.policy == WarmupPolicy.STEP:
|
|
57
|
+
stage.decay_iters = 1
|
|
58
|
+
else:
|
|
59
|
+
stage.decay_iters = stage.max_iters
|
|
60
|
+
return stages + [last_stage]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _get_multiplier(stage: WarmupStage, iter: int) -> float:
|
|
64
|
+
multiplier = 1.0
|
|
65
|
+
if stage.policy == WarmupPolicy.LINEAR:
|
|
66
|
+
multiplier = stage.value + (1.0 - stage.value) * iter / stage.max_iters
|
|
67
|
+
elif stage.policy == WarmupPolicy.CONSTANT:
|
|
68
|
+
multiplier = stage.value
|
|
69
|
+
elif stage.policy == WarmupPolicy.POLY:
|
|
70
|
+
multiplier = math.pow(1 - iter / stage.decay_iters, stage.value)
|
|
71
|
+
elif stage.policy == WarmupPolicy.STEP:
|
|
72
|
+
multiplier = math.pow(stage.value, iter // stage.decay_iters)
|
|
73
|
+
elif stage.policy == WarmupPolicy.INVSQRT:
|
|
74
|
+
multiplier = 1.0 / math.sqrt(iter)
|
|
75
|
+
return multiplier * stage.lr_scale
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class WarmupOptimizer(OptimizerWrapper):
|
|
79
|
+
"""
|
|
80
|
+
Adjusts learning rate according to the schedule.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
optimizer (KeyedOptimizer): optimizer to wrap
|
|
84
|
+
stages (List[WarmupStage]): stages to go through
|
|
85
|
+
lr (float): initial learning rate
|
|
86
|
+
lr_param (str): learning rate parameter in parameter group.
|
|
87
|
+
param_name: Name of fake parameter to hold warmup state.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
optimizer: KeyedOptimizer,
|
|
93
|
+
stages: List[WarmupStage],
|
|
94
|
+
lr: float = 0.1,
|
|
95
|
+
lr_param: str = "lr",
|
|
96
|
+
param_name: str = "__warmup",
|
|
97
|
+
) -> None:
|
|
98
|
+
super().__init__(optimizer)
|
|
99
|
+
self._stages: List[WarmupStage] = _lr_stages(stages)
|
|
100
|
+
self._lr_param: str = lr_param
|
|
101
|
+
self._lr: float = lr
|
|
102
|
+
self._warmup_param: torch.nn.Parameter = torch.nn.Parameter()
|
|
103
|
+
# pyre-ignore [16]
|
|
104
|
+
self.params[param_name] = self._warmup_param
|
|
105
|
+
# for fused optimizer we will do first backward() pass before calling step()
|
|
106
|
+
self._set_lr(0, 0)
|
|
107
|
+
|
|
108
|
+
def _set_lr(self, iter_: int, stage_id: int) -> None:
|
|
109
|
+
lr = self._lr * _get_multiplier(self._stages[stage_id], iter_)
|
|
110
|
+
for param_group in self.param_groups:
|
|
111
|
+
# pyre-ignore [16]
|
|
112
|
+
param_group[self._lr_param] = lr
|
|
113
|
+
|
|
114
|
+
def _get_warmup_state(self) -> Tuple[int, int]:
|
|
115
|
+
if self._warmup_param in self.state:
|
|
116
|
+
iter_, stage_id = self.state[self._warmup_param]["warmup"].tolist()
|
|
117
|
+
else:
|
|
118
|
+
iter_ = 0
|
|
119
|
+
stage_id = 0
|
|
120
|
+
return iter_, stage_id
|
|
121
|
+
|
|
122
|
+
def post_load_state_dict(self) -> None:
|
|
123
|
+
iter_, stage_id = self._get_warmup_state()
|
|
124
|
+
self._set_lr(iter_, stage_id)
|
|
125
|
+
|
|
126
|
+
# pyre-ignore [2]
|
|
127
|
+
def step(self, closure: Any = None) -> None:
|
|
128
|
+
super().step(closure)
|
|
129
|
+
iter_, stage_id = self._get_warmup_state()
|
|
130
|
+
|
|
131
|
+
iter_ += 1
|
|
132
|
+
if iter_ > self._stages[stage_id].max_iters and stage_id + 1 < len(
|
|
133
|
+
self._stages
|
|
134
|
+
):
|
|
135
|
+
stage_id += 1
|
|
136
|
+
logger.info(
|
|
137
|
+
"Optimizer finishing "
|
|
138
|
+
f"{self._stages[stage_id - 1]} "
|
|
139
|
+
"switching to "
|
|
140
|
+
f"{self._stages[stage_id]}"
|
|
141
|
+
)
|
|
142
|
+
self._set_lr(iter_, stage_id)
|
|
143
|
+
|
|
144
|
+
# pyre-ignore [16]
|
|
145
|
+
self.state[self._warmup_param] = {
|
|
146
|
+
"warmup": torch.tensor([iter_, stage_id], dtype=torch.long)
|
|
147
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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 Quantization
|
|
9
|
+
|
|
10
|
+
Torchrec provides a quantized version of EmbeddingBagCollection for inference.
|
|
11
|
+
It relies on fbgemm quantized ops.
|
|
12
|
+
This reduces the size of the model weights and speeds up model execution.
|
|
13
|
+
|
|
14
|
+
Example:
|
|
15
|
+
>>> import torch.quantization as quant
|
|
16
|
+
>>> import torchrec.quant as trec_quant
|
|
17
|
+
>>> import torchrec as trec
|
|
18
|
+
>>> qconfig = quant.QConfig(
|
|
19
|
+
>>> activation=quant.PlaceholderObserver,
|
|
20
|
+
>>> weight=quant.PlaceholderObserver.with_args(dtype=torch.qint8),
|
|
21
|
+
>>> )
|
|
22
|
+
>>> quantized = quant.quantize_dynamic(
|
|
23
|
+
>>> module,
|
|
24
|
+
>>> qconfig_spec={
|
|
25
|
+
>>> trec.EmbeddingBagCollection: qconfig,
|
|
26
|
+
>>> },
|
|
27
|
+
>>> mapping={
|
|
28
|
+
>>> trec.EmbeddingBagCollection: trec_quant.EmbeddingBagCollection,
|
|
29
|
+
>>> },
|
|
30
|
+
>>> inplace=inplace,
|
|
31
|
+
>>> )
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from torchrec.quant.embedding_modules import EmbeddingBagCollection # noqa
|