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,326 @@
|
|
|
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
|
+
import logging
|
|
10
|
+
from collections import defaultdict, OrderedDict
|
|
11
|
+
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
|
12
|
+
|
|
13
|
+
import torch
|
|
14
|
+
import torch.distributed as dist
|
|
15
|
+
from torch import nn
|
|
16
|
+
from torchrec.distributed.embedding_types import (
|
|
17
|
+
EmbeddingComputeKernel,
|
|
18
|
+
GroupedEmbeddingConfig,
|
|
19
|
+
ShardedEmbeddingTable,
|
|
20
|
+
)
|
|
21
|
+
from torchrec.distributed.types import Shard, ShardedTensor, ShardedTensorMetadata
|
|
22
|
+
from torchrec.distributed.utils import append_prefix
|
|
23
|
+
from torchrec.modules.embedding_configs import pooling_type_to_str
|
|
24
|
+
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
|
|
25
|
+
|
|
26
|
+
logger: logging.Logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class BaseEmbedding(abc.ABC, nn.Module):
|
|
30
|
+
"""
|
|
31
|
+
Abstract base class for grouped `nn.Embedding` and `nn.EmbeddingBag`
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
@abc.abstractmethod
|
|
35
|
+
def forward(
|
|
36
|
+
self,
|
|
37
|
+
features: KeyedJaggedTensor,
|
|
38
|
+
) -> torch.Tensor:
|
|
39
|
+
"""
|
|
40
|
+
Args:
|
|
41
|
+
features (KeyedJaggedTensor):
|
|
42
|
+
Returns:
|
|
43
|
+
torch.Tensor: sparse gradient parameter names.
|
|
44
|
+
"""
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
def sparse_grad_parameter_names(
|
|
48
|
+
self, destination: Optional[List[str]] = None, prefix: str = ""
|
|
49
|
+
) -> List[str]:
|
|
50
|
+
destination = [] if destination is None else destination
|
|
51
|
+
return destination
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
@abc.abstractmethod
|
|
55
|
+
def config(self) -> GroupedEmbeddingConfig:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_state_dict(
|
|
60
|
+
embedding_tables: List[ShardedEmbeddingTable],
|
|
61
|
+
params: Union[
|
|
62
|
+
nn.ModuleList,
|
|
63
|
+
List[Union[nn.Module, torch.Tensor]],
|
|
64
|
+
List[torch.Tensor],
|
|
65
|
+
],
|
|
66
|
+
pg: Optional[dist.ProcessGroup] = None,
|
|
67
|
+
destination: Optional[Dict[str, Any]] = None,
|
|
68
|
+
prefix: str = "",
|
|
69
|
+
) -> Dict[str, Any]:
|
|
70
|
+
if destination is None:
|
|
71
|
+
destination = OrderedDict()
|
|
72
|
+
# pyre-ignore [16]
|
|
73
|
+
destination._metadata = OrderedDict()
|
|
74
|
+
"""
|
|
75
|
+
It is possible for there to be multiple shards from a table on a single rank.
|
|
76
|
+
We accumulate them in key_to_local_shards. Repeat shards should have identical
|
|
77
|
+
global ShardedTensorMetadata.
|
|
78
|
+
"""
|
|
79
|
+
key_to_local_shards: Dict[str, List[Shard]] = defaultdict(list)
|
|
80
|
+
key_to_global_metadata: Dict[str, ShardedTensorMetadata] = {}
|
|
81
|
+
|
|
82
|
+
def get_key_from_embedding_table(embedding_table: ShardedEmbeddingTable) -> str:
|
|
83
|
+
return prefix + f"{embedding_table.name}.weight"
|
|
84
|
+
|
|
85
|
+
for embedding_table, param in zip(embedding_tables, params):
|
|
86
|
+
key = get_key_from_embedding_table(embedding_table)
|
|
87
|
+
assert embedding_table.local_rows == param.size(0)
|
|
88
|
+
if embedding_table.compute_kernel not in [
|
|
89
|
+
EmbeddingComputeKernel.QUANT,
|
|
90
|
+
EmbeddingComputeKernel.QUANT_UVM,
|
|
91
|
+
EmbeddingComputeKernel.QUANT_UVM_CACHING,
|
|
92
|
+
]:
|
|
93
|
+
assert embedding_table.local_cols == param.size(1)
|
|
94
|
+
# for inference there is no pg, all tensors are local
|
|
95
|
+
if embedding_table.global_metadata is not None and pg is not None:
|
|
96
|
+
# set additional field of sharded tensor based on local tensor properties
|
|
97
|
+
embedding_table.global_metadata.tensor_properties.dtype = param.dtype
|
|
98
|
+
embedding_table.global_metadata.tensor_properties.requires_grad = (
|
|
99
|
+
param.requires_grad
|
|
100
|
+
)
|
|
101
|
+
key_to_global_metadata[key] = embedding_table.global_metadata
|
|
102
|
+
|
|
103
|
+
key_to_local_shards[key].append(
|
|
104
|
+
Shard(param, embedding_table.local_metadata)
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
destination[key] = param
|
|
108
|
+
|
|
109
|
+
if pg is not None:
|
|
110
|
+
# Populate the remaining destinations that have a global metadata
|
|
111
|
+
for key in key_to_local_shards:
|
|
112
|
+
global_metadata = key_to_global_metadata[key]
|
|
113
|
+
destination[
|
|
114
|
+
key
|
|
115
|
+
] = ShardedTensor._init_from_local_shards_and_global_metadata(
|
|
116
|
+
local_shards=key_to_local_shards[key],
|
|
117
|
+
sharded_tensor_metadata=global_metadata,
|
|
118
|
+
process_group=pg,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
return destination
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class GroupedEmbedding(BaseEmbedding):
|
|
125
|
+
def __init__(
|
|
126
|
+
self,
|
|
127
|
+
config: GroupedEmbeddingConfig,
|
|
128
|
+
sparse: bool,
|
|
129
|
+
pg: Optional[dist.ProcessGroup] = None,
|
|
130
|
+
device: Optional[torch.device] = None,
|
|
131
|
+
) -> None:
|
|
132
|
+
super().__init__()
|
|
133
|
+
torch._C._log_api_usage_once(f"torchrec.distributed.{self.__class__.__name__}")
|
|
134
|
+
self._config = config
|
|
135
|
+
self._pg = pg
|
|
136
|
+
self._emb_modules: nn.ModuleList = nn.ModuleList()
|
|
137
|
+
self._sparse = sparse
|
|
138
|
+
for embedding_config in self._config.embedding_tables:
|
|
139
|
+
self._emb_modules.append(
|
|
140
|
+
nn.Embedding(
|
|
141
|
+
num_embeddings=embedding_config.local_rows,
|
|
142
|
+
embedding_dim=embedding_config.local_cols,
|
|
143
|
+
device=device,
|
|
144
|
+
sparse=self._sparse,
|
|
145
|
+
_weight=torch.empty(
|
|
146
|
+
embedding_config.local_rows,
|
|
147
|
+
embedding_config.local_cols,
|
|
148
|
+
device=device,
|
|
149
|
+
).uniform_(
|
|
150
|
+
embedding_config.get_weight_init_min(),
|
|
151
|
+
embedding_config.get_weight_init_max(),
|
|
152
|
+
),
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
def forward(self, features: KeyedJaggedTensor) -> torch.Tensor:
|
|
157
|
+
indices_dict: Dict[str, torch.Tensor] = {}
|
|
158
|
+
indices_list = torch.split(features.values(), features.length_per_key())
|
|
159
|
+
for key, indices in zip(features.keys(), indices_list):
|
|
160
|
+
indices_dict[key] = indices
|
|
161
|
+
unpooled_embeddings: List[torch.Tensor] = []
|
|
162
|
+
for embedding_config, emb_module in zip(
|
|
163
|
+
self._config.embedding_tables, self._emb_modules
|
|
164
|
+
):
|
|
165
|
+
for feature_name in embedding_config.feature_names:
|
|
166
|
+
unpooled_embeddings.append(emb_module(input=indices_dict[feature_name]))
|
|
167
|
+
return torch.cat(unpooled_embeddings, dim=0)
|
|
168
|
+
|
|
169
|
+
# pyre-fixme[14]: `state_dict` overrides method defined in `Module` inconsistently.
|
|
170
|
+
def state_dict(
|
|
171
|
+
self,
|
|
172
|
+
destination: Optional[Dict[str, Any]] = None,
|
|
173
|
+
prefix: str = "",
|
|
174
|
+
keep_vars: bool = False,
|
|
175
|
+
) -> Dict[str, Any]:
|
|
176
|
+
params = [
|
|
177
|
+
emb_module.weight if keep_vars else emb_module.weight.data
|
|
178
|
+
for emb_module in self._emb_modules
|
|
179
|
+
]
|
|
180
|
+
return get_state_dict(
|
|
181
|
+
self._config.embedding_tables, params, self._pg, destination, prefix
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def named_parameters(
|
|
185
|
+
self, prefix: str = "", recurse: bool = True
|
|
186
|
+
) -> Iterator[Tuple[str, nn.Parameter]]:
|
|
187
|
+
for config, emb_module in zip(
|
|
188
|
+
self._config.embedding_tables,
|
|
189
|
+
self._emb_modules,
|
|
190
|
+
):
|
|
191
|
+
param = emb_module.weight
|
|
192
|
+
assert config.local_rows == param.size(0)
|
|
193
|
+
assert config.local_cols == param.size(1)
|
|
194
|
+
yield append_prefix(prefix, f"{config.name}.weight"), param
|
|
195
|
+
|
|
196
|
+
def sparse_grad_parameter_names(
|
|
197
|
+
self, destination: Optional[List[str]] = None, prefix: str = ""
|
|
198
|
+
) -> List[str]:
|
|
199
|
+
destination = [] if destination is None else destination
|
|
200
|
+
if self._sparse:
|
|
201
|
+
for config in self._config.embedding_tables:
|
|
202
|
+
destination.append(append_prefix(prefix, f"{config.name}.weight"))
|
|
203
|
+
return destination
|
|
204
|
+
|
|
205
|
+
@property
|
|
206
|
+
def config(self) -> GroupedEmbeddingConfig:
|
|
207
|
+
return self._config
|
|
208
|
+
|
|
209
|
+
def named_buffers(
|
|
210
|
+
self, prefix: str = "", recurse: bool = True
|
|
211
|
+
) -> Iterator[Tuple[str, torch.Tensor]]:
|
|
212
|
+
for config, emb_module in zip(
|
|
213
|
+
self._config.embedding_tables,
|
|
214
|
+
self._emb_modules,
|
|
215
|
+
):
|
|
216
|
+
yield append_prefix(prefix, f"{config.name}.weight"), emb_module.weight
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class GroupedEmbeddingBag(BaseEmbedding):
|
|
220
|
+
def __init__(
|
|
221
|
+
self,
|
|
222
|
+
config: GroupedEmbeddingConfig,
|
|
223
|
+
sparse: bool,
|
|
224
|
+
pg: Optional[dist.ProcessGroup] = None,
|
|
225
|
+
device: Optional[torch.device] = None,
|
|
226
|
+
) -> None:
|
|
227
|
+
super().__init__()
|
|
228
|
+
torch._C._log_api_usage_once(f"torchrec.distributed.{self.__class__.__name__}")
|
|
229
|
+
self._config = config
|
|
230
|
+
self._pg = pg
|
|
231
|
+
self._emb_modules: nn.ModuleList = nn.ModuleList()
|
|
232
|
+
self._sparse = sparse
|
|
233
|
+
self._emb_names: List[str] = []
|
|
234
|
+
self._lengths_per_emb: List[int] = []
|
|
235
|
+
|
|
236
|
+
for embedding_config in self._config.embedding_tables:
|
|
237
|
+
self._emb_modules.append(
|
|
238
|
+
nn.EmbeddingBag(
|
|
239
|
+
num_embeddings=embedding_config.local_rows,
|
|
240
|
+
embedding_dim=embedding_config.local_cols,
|
|
241
|
+
mode=pooling_type_to_str(embedding_config.pooling),
|
|
242
|
+
device=device,
|
|
243
|
+
include_last_offset=True,
|
|
244
|
+
sparse=self._sparse,
|
|
245
|
+
_weight=torch.empty(
|
|
246
|
+
embedding_config.local_rows,
|
|
247
|
+
embedding_config.local_cols,
|
|
248
|
+
device=device,
|
|
249
|
+
).uniform_(
|
|
250
|
+
embedding_config.get_weight_init_min(),
|
|
251
|
+
embedding_config.get_weight_init_max(),
|
|
252
|
+
),
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def forward(self, features: KeyedJaggedTensor) -> torch.Tensor:
|
|
257
|
+
pooled_embeddings: List[torch.Tensor] = []
|
|
258
|
+
for embedding_config, emb_module in zip(
|
|
259
|
+
self._config.embedding_tables, self._emb_modules
|
|
260
|
+
):
|
|
261
|
+
for feature_name in embedding_config.feature_names:
|
|
262
|
+
values = features[feature_name].values()
|
|
263
|
+
offsets = features[feature_name].offsets()
|
|
264
|
+
weights = features[feature_name].weights_or_none()
|
|
265
|
+
if weights is not None and not torch.is_floating_point(weights):
|
|
266
|
+
weights = None
|
|
267
|
+
pooled_embeddings.append(
|
|
268
|
+
emb_module(
|
|
269
|
+
input=values,
|
|
270
|
+
offsets=offsets,
|
|
271
|
+
per_sample_weights=weights,
|
|
272
|
+
)
|
|
273
|
+
)
|
|
274
|
+
return torch.cat(pooled_embeddings, dim=1)
|
|
275
|
+
|
|
276
|
+
# pyre-fixme[14]: `state_dict` overrides method defined in `Module` inconsistently.
|
|
277
|
+
def state_dict(
|
|
278
|
+
self,
|
|
279
|
+
destination: Optional[Dict[str, Any]] = None,
|
|
280
|
+
prefix: str = "",
|
|
281
|
+
keep_vars: bool = False,
|
|
282
|
+
) -> Dict[str, Any]:
|
|
283
|
+
params = [
|
|
284
|
+
emb_module.weight if keep_vars else emb_module.weight.data
|
|
285
|
+
for emb_module in self._emb_modules
|
|
286
|
+
]
|
|
287
|
+
return get_state_dict(
|
|
288
|
+
self._config.embedding_tables, params, self._pg, destination, prefix
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
def named_parameters(
|
|
292
|
+
self, prefix: str = "", recurse: bool = True
|
|
293
|
+
) -> Iterator[Tuple[str, nn.Parameter]]:
|
|
294
|
+
for config, emb_module in zip(
|
|
295
|
+
self._config.embedding_tables,
|
|
296
|
+
self._emb_modules,
|
|
297
|
+
):
|
|
298
|
+
param = emb_module.weight
|
|
299
|
+
assert config.local_rows == param.size(0)
|
|
300
|
+
assert config.local_cols == param.size(1)
|
|
301
|
+
yield append_prefix(prefix, f"{config.name}.weight"), param
|
|
302
|
+
|
|
303
|
+
def named_buffers(
|
|
304
|
+
self, prefix: str = "", recurse: bool = True
|
|
305
|
+
) -> Iterator[Tuple[str, torch.Tensor]]:
|
|
306
|
+
for config, emb_module in zip(
|
|
307
|
+
self._config.embedding_tables,
|
|
308
|
+
self._emb_modules,
|
|
309
|
+
):
|
|
310
|
+
param = emb_module.weight
|
|
311
|
+
assert config.local_rows == param.size(0)
|
|
312
|
+
assert config.local_cols == param.size(1)
|
|
313
|
+
yield append_prefix(prefix, f"{config.name}.weight"), param
|
|
314
|
+
|
|
315
|
+
def sparse_grad_parameter_names(
|
|
316
|
+
self, destination: Optional[List[str]] = None, prefix: str = ""
|
|
317
|
+
) -> List[str]:
|
|
318
|
+
destination = [] if destination is None else destination
|
|
319
|
+
if self._sparse:
|
|
320
|
+
for config in self._config.embedding_tables:
|
|
321
|
+
destination.append(append_prefix(prefix, f"{config.name}.weight"))
|
|
322
|
+
return destination
|
|
323
|
+
|
|
324
|
+
@property
|
|
325
|
+
def config(self) -> GroupedEmbeddingConfig:
|
|
326
|
+
return self._config
|