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,213 @@
|
|
|
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
|
+
"""
|
|
9
|
+
Deep Factorization-Machine Modules
|
|
10
|
+
|
|
11
|
+
The following modules are based off the `Deep Factorization-Machine (DeepFM) paper
|
|
12
|
+
<https://arxiv.org/pdf/1703.04247.pdf>`_
|
|
13
|
+
|
|
14
|
+
* Class DeepFM implents the DeepFM Framework
|
|
15
|
+
* Class FactorizationMachine implements FM as noted in the above paper.
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import List
|
|
20
|
+
|
|
21
|
+
import torch
|
|
22
|
+
from torch import nn
|
|
23
|
+
from torch.fx import wrap
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@wrap
|
|
27
|
+
def _get_flatten_input(inputs: List[torch.Tensor]) -> torch.Tensor:
|
|
28
|
+
return torch.cat(
|
|
29
|
+
[input.flatten(1) for input in inputs],
|
|
30
|
+
dim=1,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class DeepFM(nn.Module):
|
|
35
|
+
r"""
|
|
36
|
+
This is the `DeepFM module <https://arxiv.org/pdf/1703.04247.pdf>`_
|
|
37
|
+
|
|
38
|
+
This module does not cover the end-end functionality of the published paper.
|
|
39
|
+
Instead, it covers only the deep component of the publication. It is used to learn
|
|
40
|
+
high-order feature interactions. If low-order feature interactions should
|
|
41
|
+
be learnt, please use `FactorizationMachine` module instead, which will share
|
|
42
|
+
the same embedding input of this module.
|
|
43
|
+
|
|
44
|
+
To support modeling flexibility, we customize the key components as:
|
|
45
|
+
|
|
46
|
+
* Different from the public paper, we change the input from raw sparse features to
|
|
47
|
+
embeddings of the features. It allows flexibility in embedding dimensions and the
|
|
48
|
+
number of embeddings, as long as all embedding tensors have the same batch size.
|
|
49
|
+
|
|
50
|
+
* On top of the public paper, we allow users to customize the hidden layer to be any
|
|
51
|
+
module, not limited to just MLP.
|
|
52
|
+
|
|
53
|
+
The general architecture of the module is like::
|
|
54
|
+
|
|
55
|
+
1 x 10 output
|
|
56
|
+
/|\
|
|
57
|
+
| pass into `dense_module`
|
|
58
|
+
|
|
|
59
|
+
1 x 90
|
|
60
|
+
/|\
|
|
61
|
+
| concat
|
|
62
|
+
|
|
|
63
|
+
1 x 20, 1 x 30, 1 x 40 list of embeddings
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
dense_module (nn.Module):
|
|
67
|
+
any customized module that can be used (such as MLP) in DeepFM. The
|
|
68
|
+
`in_features` of this module must be equal to the element counts. For
|
|
69
|
+
example, if the input embedding is `[randn(3, 2, 3), randn(3, 4, 5)]`, the
|
|
70
|
+
`in_features` should be: 2*3+4*5.
|
|
71
|
+
|
|
72
|
+
Example::
|
|
73
|
+
|
|
74
|
+
import torch
|
|
75
|
+
from torchrec.fb.modules.deepfm import DeepFM
|
|
76
|
+
from torchrec.fb.modules.mlp import LazyMLP
|
|
77
|
+
batch_size = 3
|
|
78
|
+
output_dim = 30
|
|
79
|
+
# the input embedding are a torch.Tensor of [batch_size, num_embeddings, embedding_dim]
|
|
80
|
+
input_embeddings = [
|
|
81
|
+
torch.randn(batch_size, 2, 64),
|
|
82
|
+
torch.randn(batch_size, 2, 32),
|
|
83
|
+
]
|
|
84
|
+
dense_module = nn.Linear(192, output_dim)
|
|
85
|
+
deepfm = DeepFM(dense_module=dense_module)
|
|
86
|
+
deep_fm_output = deepfm(embeddings=input_embeddings)
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(
|
|
90
|
+
self,
|
|
91
|
+
dense_module: nn.Module,
|
|
92
|
+
) -> None:
|
|
93
|
+
super().__init__()
|
|
94
|
+
self.dense_module = dense_module
|
|
95
|
+
|
|
96
|
+
def forward(
|
|
97
|
+
self,
|
|
98
|
+
embeddings: List[torch.Tensor],
|
|
99
|
+
) -> torch.Tensor:
|
|
100
|
+
"""
|
|
101
|
+
Args:
|
|
102
|
+
embeddings (List[torch.Tensor]):
|
|
103
|
+
The list of all embeddings (e.g. dense, common_sparse,
|
|
104
|
+
specialized_sparse,
|
|
105
|
+
embedding_features, raw_embedding_features) in the shape of::
|
|
106
|
+
|
|
107
|
+
(batch_size, num_embeddings, embedding_dim)
|
|
108
|
+
|
|
109
|
+
For the ease of operation, embeddings that have the same embedding
|
|
110
|
+
dimension have the option to be stacked into a single tensor. For
|
|
111
|
+
example, when we have 1 trained embedding with dimension=32, 5 native
|
|
112
|
+
embeddings with dimension=64, and 3 dense features with dimension=16, we
|
|
113
|
+
can prepare the embeddings list to be the list of::
|
|
114
|
+
|
|
115
|
+
tensor(B, 1, 32) (trained_embedding with num_embeddings=1, embedding_dim=32)
|
|
116
|
+
tensor(B, 5, 64) (native_embedding with num_embeddings=5, embedding_dim=64)
|
|
117
|
+
tensor(B, 3, 16) (dense_features with num_embeddings=3, embedding_dim=32)
|
|
118
|
+
|
|
119
|
+
.. note::
|
|
120
|
+
`batch_size` of all input tensors need to be identical.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
torch.Tensor: output of `dense_module` with flattened and concatenated `embeddings` as input.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
# flatten each embedding to be [B, N, D] -> [B, N*D], then cat them all on dim=1
|
|
127
|
+
deepfm_input = _get_flatten_input(embeddings)
|
|
128
|
+
deepfm_output = self.dense_module(deepfm_input)
|
|
129
|
+
return deepfm_output
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class FactorizationMachine(nn.Module):
|
|
133
|
+
r"""
|
|
134
|
+
This is the Factorization Machine module, mentioned in the `DeepFM paper
|
|
135
|
+
<https://arxiv.org/pdf/1703.04247.pdf>`_:
|
|
136
|
+
|
|
137
|
+
This module does not cover the end-end functionality of the published paper.
|
|
138
|
+
Instead, it covers only the FM part of the publication, and is used to learn
|
|
139
|
+
2nd-order feature interactions.
|
|
140
|
+
|
|
141
|
+
To support modeling flexibility, we customize the key components as different from
|
|
142
|
+
the public paper:
|
|
143
|
+
We change the input from raw sparse features to embeddings of the features.
|
|
144
|
+
This allows flexibility in embedding dimensions and the number of embeddings,
|
|
145
|
+
as long as all embedding tensors have the same batch size.
|
|
146
|
+
|
|
147
|
+
The general architecture of the module is like::
|
|
148
|
+
|
|
149
|
+
1 x 10 output
|
|
150
|
+
/|\
|
|
151
|
+
| pass into `dense_module`
|
|
152
|
+
|
|
|
153
|
+
1 x 90
|
|
154
|
+
/|\
|
|
155
|
+
| concat
|
|
156
|
+
|
|
|
157
|
+
1 x 20, 1 x 30, 1 x 40 list of embeddings
|
|
158
|
+
|
|
159
|
+
Example::
|
|
160
|
+
|
|
161
|
+
batch_size = 3
|
|
162
|
+
# the input embedding are in torch.Tensor of [batch_size, num_embeddings, embedding_dim]
|
|
163
|
+
input_embeddings = [
|
|
164
|
+
torch.randn(batch_size, 2, 64),
|
|
165
|
+
torch.randn(batch_size, 2, 32),
|
|
166
|
+
]
|
|
167
|
+
fm = FactorizationMachine()
|
|
168
|
+
output = fm(embeddings=input_embeddings)
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
def __init__(
|
|
172
|
+
self,
|
|
173
|
+
) -> None:
|
|
174
|
+
super().__init__()
|
|
175
|
+
|
|
176
|
+
def forward(
|
|
177
|
+
self,
|
|
178
|
+
embeddings: List[torch.Tensor],
|
|
179
|
+
) -> torch.Tensor:
|
|
180
|
+
"""
|
|
181
|
+
Args:
|
|
182
|
+
embeddings (List[torch.Tensor]):
|
|
183
|
+
The list of all embeddings (e.g. dense, common_sparse,
|
|
184
|
+
specialized_sparse, embedding_features, raw_embedding_features) in the
|
|
185
|
+
shape of::
|
|
186
|
+
|
|
187
|
+
(batch_size, num_embeddings, embedding_dim)
|
|
188
|
+
|
|
189
|
+
For the ease of operation, embeddings that have the same embedding
|
|
190
|
+
dimension have the option to be stacked into a single tensor. For
|
|
191
|
+
example, when we have 1 trained embedding with dimension=32, 5 native
|
|
192
|
+
embeddings with dimension=64, and 3 dense features with dimension=16, we
|
|
193
|
+
can prepare the embeddings list to be the list of::
|
|
194
|
+
|
|
195
|
+
tensor(B, 1, 32) (trained_embedding with num_embeddings=1, embedding_dim=32)
|
|
196
|
+
tensor(B, 5, 64) (native_embedding with num_embeddings=5, embedding_dim=64)
|
|
197
|
+
tensor(B, 3, 16) (dense_features with num_embeddings=3, embedding_dim=32)
|
|
198
|
+
|
|
199
|
+
NOTE:
|
|
200
|
+
`batch_size` of all input tensors need to be identical.
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
torch.Tensor: output of fm with flattened and concatenated `embeddings` as input. Expected to be [B, 1].
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
# flatten each embedding to be [B, N, D] -> [B, N*D], then cat them all on dim=1
|
|
207
|
+
fm_input = _get_flatten_input(embeddings)
|
|
208
|
+
sum_of_input = torch.sum(fm_input, dim=1, keepdim=True)
|
|
209
|
+
sum_of_square = torch.sum(fm_input * fm_input, dim=1, keepdim=True)
|
|
210
|
+
square_of_sum = sum_of_input * sum_of_input
|
|
211
|
+
cross_term = square_of_sum - sum_of_square
|
|
212
|
+
cross_term = torch.sum(cross_term, dim=1, keepdim=True) * 0.5 # [B, 1]
|
|
213
|
+
return cross_term
|
|
@@ -0,0 +1,148 @@
|
|
|
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 dataclasses import dataclass, field
|
|
9
|
+
from enum import Enum, unique
|
|
10
|
+
from math import sqrt
|
|
11
|
+
from typing import Dict, List, Optional
|
|
12
|
+
|
|
13
|
+
import torch
|
|
14
|
+
from fbgemm_gpu.split_embedding_configs import SparseType
|
|
15
|
+
from fbgemm_gpu.split_table_batched_embeddings_ops import PoolingMode
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@unique
|
|
19
|
+
class PoolingType(Enum):
|
|
20
|
+
SUM = "SUM"
|
|
21
|
+
MEAN = "MEAN"
|
|
22
|
+
NONE = "NONE"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@unique
|
|
26
|
+
class DataType(Enum):
|
|
27
|
+
"""
|
|
28
|
+
Our fusion implementation supports only certain types of data
|
|
29
|
+
so it makes sense to retrict in a non-fused version as well.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
FP32 = "FP32"
|
|
33
|
+
FP16 = "FP16"
|
|
34
|
+
INT8 = "INT8"
|
|
35
|
+
INT4 = "INT4"
|
|
36
|
+
INT2 = "INT2"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
DATA_TYPE_NUM_BITS: Dict[DataType, int] = {
|
|
40
|
+
DataType.FP32: 32,
|
|
41
|
+
DataType.FP16: 16,
|
|
42
|
+
DataType.INT8: 8,
|
|
43
|
+
DataType.INT4: 4,
|
|
44
|
+
DataType.INT2: 2,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def dtype_to_data_type(dtype: torch.dtype) -> DataType:
|
|
49
|
+
if dtype == torch.float:
|
|
50
|
+
return DataType.FP32
|
|
51
|
+
elif dtype == torch.float16 or dtype == torch.half:
|
|
52
|
+
return DataType.FP16
|
|
53
|
+
elif dtype == torch.quint8 or dtype == torch.qint8 or dtype == torch.int8:
|
|
54
|
+
return DataType.INT8
|
|
55
|
+
elif dtype == torch.quint4x2:
|
|
56
|
+
return DataType.INT4
|
|
57
|
+
elif dtype == torch.quint2x4:
|
|
58
|
+
return DataType.INT2
|
|
59
|
+
else:
|
|
60
|
+
raise Exception(f"Invalid data type {dtype}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def pooling_type_to_pooling_mode(pooling_type: PoolingType) -> PoolingMode:
|
|
64
|
+
if pooling_type == PoolingType.SUM:
|
|
65
|
+
return PoolingMode.SUM
|
|
66
|
+
elif pooling_type == PoolingType.MEAN:
|
|
67
|
+
return PoolingMode.MEAN
|
|
68
|
+
else:
|
|
69
|
+
raise Exception(f"Invalid pooling type {pooling_type}")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def pooling_type_to_str(pooling_type: PoolingType) -> str:
|
|
73
|
+
if pooling_type == PoolingType.SUM:
|
|
74
|
+
return "sum"
|
|
75
|
+
elif pooling_type == PoolingType.MEAN:
|
|
76
|
+
return "mean"
|
|
77
|
+
else:
|
|
78
|
+
raise ValueError(f"Unsupported pooling type {pooling_type}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def data_type_to_sparse_type(data_type: DataType) -> SparseType:
|
|
82
|
+
if data_type == DataType.FP32:
|
|
83
|
+
return SparseType.FP32
|
|
84
|
+
elif data_type == DataType.FP16:
|
|
85
|
+
return SparseType.FP16
|
|
86
|
+
elif data_type == DataType.INT8:
|
|
87
|
+
return SparseType.INT8
|
|
88
|
+
elif data_type == DataType.INT4:
|
|
89
|
+
return SparseType.INT4
|
|
90
|
+
elif data_type == DataType.INT2:
|
|
91
|
+
return SparseType.INT2
|
|
92
|
+
else:
|
|
93
|
+
raise ValueError(f"Invalid DataType {data_type}")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def data_type_to_dtype(data_type: DataType) -> torch.dtype:
|
|
97
|
+
if data_type == DataType.FP32:
|
|
98
|
+
return torch.float32
|
|
99
|
+
elif data_type == DataType.FP16:
|
|
100
|
+
return torch.float16
|
|
101
|
+
elif data_type == DataType.INT8:
|
|
102
|
+
return torch.int8
|
|
103
|
+
else:
|
|
104
|
+
raise ValueError(f"DataType {data_type} cannot be converted to dtype")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@dataclass
|
|
108
|
+
class BaseEmbeddingConfig:
|
|
109
|
+
num_embeddings: int
|
|
110
|
+
embedding_dim: int
|
|
111
|
+
name: str = ""
|
|
112
|
+
data_type: DataType = DataType.FP32
|
|
113
|
+
feature_names: List[str] = field(default_factory=list)
|
|
114
|
+
weight_init_max: Optional[float] = None
|
|
115
|
+
weight_init_min: Optional[float] = None
|
|
116
|
+
|
|
117
|
+
def get_weight_init_max(self) -> float:
|
|
118
|
+
if self.weight_init_max is None:
|
|
119
|
+
return sqrt(1 / self.num_embeddings)
|
|
120
|
+
else:
|
|
121
|
+
return self.weight_init_max
|
|
122
|
+
|
|
123
|
+
def get_weight_init_min(self) -> float:
|
|
124
|
+
if self.weight_init_min is None:
|
|
125
|
+
return -sqrt(1 / self.num_embeddings)
|
|
126
|
+
else:
|
|
127
|
+
return self.weight_init_min
|
|
128
|
+
|
|
129
|
+
def num_features(self) -> int:
|
|
130
|
+
return len(self.feature_names)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@dataclass
|
|
134
|
+
class EmbeddingTableConfig(BaseEmbeddingConfig):
|
|
135
|
+
pooling: PoolingType = PoolingType.SUM
|
|
136
|
+
is_weighted: bool = False
|
|
137
|
+
has_feature_processor: bool = False
|
|
138
|
+
embedding_names: List[str] = field(default_factory=list)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@dataclass
|
|
142
|
+
class EmbeddingBagConfig(BaseEmbeddingConfig):
|
|
143
|
+
pooling: PoolingType = PoolingType.SUM
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass
|
|
147
|
+
class EmbeddingConfig(BaseEmbeddingConfig):
|
|
148
|
+
pass
|