-
Notifications
You must be signed in to change notification settings - Fork 466
/
Copy pathbatched_embedding_kernel.py
1399 lines (1234 loc) · 51.5 KB
/
batched_embedding_kernel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import abc
import copy
import inspect
import itertools
import logging
import tempfile
from collections import OrderedDict
from dataclasses import dataclass
from typing import (
Any,
cast,
Dict,
Generic,
Iterator,
List,
Optional,
Tuple,
TypeVar,
Union,
)
import torch
import torch.distributed as dist
from fbgemm_gpu.split_table_batched_embeddings_ops_inference import (
IntNBitTableBatchedEmbeddingBagsCodegen,
)
from fbgemm_gpu.split_table_batched_embeddings_ops_training import (
ComputeDevice,
DenseTableBatchedEmbeddingBagsCodegen,
EmbeddingLocation,
PoolingMode,
SparseType,
SplitTableBatchedEmbeddingBagsCodegen,
)
from fbgemm_gpu.ssd_split_table_batched_embeddings_ops import (
ASSOC,
SSDTableBatchedEmbeddingBags,
)
from torch import nn
from torchrec.distributed.composable.table_batched_embedding_slice import (
TableBatchedEmbeddingSlice,
)
from torchrec.distributed.embedding_kernel import BaseEmbedding, get_state_dict
from torchrec.distributed.embedding_types import (
compute_kernel_to_embedding_location,
GroupedEmbeddingConfig,
)
from torchrec.distributed.types import (
Shard,
ShardedTensor,
ShardedTensorMetadata,
ShardingType,
ShardMetadata,
TensorProperties,
)
from torchrec.distributed.utils import append_prefix
from torchrec.modules.embedding_configs import (
data_type_to_sparse_type,
pooling_type_to_pooling_mode,
)
from torchrec.optim.fused import (
EmptyFusedOptimizer,
FusedOptimizer,
FusedOptimizerModule,
)
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
logger: logging.Logger = logging.getLogger(__name__)
def _populate_ssd_tbe_params(config: GroupedEmbeddingConfig) -> Dict[str, Any]:
"""
Construct SSD TBE params dict from config and fused params dict.
"""
fused_params = config.fused_params or {}
ssd_tbe_params: Dict[str, Any] = {}
# drop the non-ssd tbe fused params
ssd_tbe_signature = inspect.signature(
SSDTableBatchedEmbeddingBags.__init__
).parameters.keys()
invalid_keys: List[str] = []
for key, value in fused_params.items():
if key not in ssd_tbe_signature:
invalid_keys.append(key)
else:
ssd_tbe_params[key] = value
if len(invalid_keys) > 0:
logger.warning(
f"Dropping {invalid_keys} since they are not valid SSD TBE params."
)
# populate number cache sets, aka number of rows of the cache space
if "cache_sets" not in ssd_tbe_params:
cache_load_factor = fused_params.get("cache_load_factor")
if cache_load_factor:
cache_load_factor = fused_params.get("cache_load_factor")
logger.info(
f"Using cache load factor from fused params dict: {cache_load_factor}"
)
else:
cache_load_factor = 0.2
local_rows_sum: int = sum(table.local_rows for table in config.embedding_tables)
ssd_tbe_params["cache_sets"] = int(cache_load_factor * local_rows_sum / ASSOC)
# populate init min and max
if (
"ssd_uniform_init_lower" not in ssd_tbe_params
or "ssd_uniform_init_upper" not in ssd_tbe_params
):
# Right now we do not support a per table init max and min. To use
# per table init max and min, either we allow it in SSD TBE, or we
# create one SSD TBE per table.
# TODO: Solve the init problem
mins = [table.get_weight_init_min() for table in config.embedding_tables]
maxs = [table.get_weight_init_max() for table in config.embedding_tables]
ssd_tbe_params["ssd_uniform_init_lower"] = sum(mins) / len(
config.embedding_tables
)
ssd_tbe_params["ssd_uniform_init_upper"] = sum(maxs) / len(
config.embedding_tables
)
if "ssd_storage_directory" not in ssd_tbe_params:
ssd_tbe_params["ssd_storage_directory"] = tempfile.mkdtemp()
if "weights_precision" not in ssd_tbe_params:
weights_precision = data_type_to_sparse_type(config.data_type)
ssd_tbe_params["weights_precision"] = weights_precision
return ssd_tbe_params
class KeyValueEmbeddingFusedOptimizer(FusedOptimizer):
def __init__(
self,
config: GroupedEmbeddingConfig,
emb_module: SSDTableBatchedEmbeddingBags,
pg: Optional[dist.ProcessGroup] = None,
) -> None:
"""
Fused optimizer for SSD TBE. Right now it only supports tuning learning
rate.
"""
self._emb_module: SSDTableBatchedEmbeddingBags = emb_module
self._pg = pg
# TODO: support optimizer states checkpointing once FBGEMM support
# split_optimizer_states API
# pyre-ignore [33]
state: Dict[Any, Any] = {}
param_group: Dict[str, Any] = {
"params": [],
"lr": emb_module.optimizer_args.learning_rate,
}
params: Dict[str, Union[torch.Tensor, ShardedTensor]] = {}
super().__init__(params, state, [param_group])
def zero_grad(self, set_to_none: bool = False) -> None:
# pyre-ignore [16]
self._emb_module.set_learning_rate(self.param_groups[0]["lr"])
# pyre-ignore [2]
def step(self, closure: Any = None) -> None:
# pyre-ignore [16]
self._emb_module.set_learning_rate(self.param_groups[0]["lr"])
class EmbeddingFusedOptimizer(FusedOptimizer):
def __init__( # noqa C901
self,
config: GroupedEmbeddingConfig,
emb_module: SplitTableBatchedEmbeddingBagsCodegen,
pg: Optional[dist.ProcessGroup] = None,
create_for_table: Optional[str] = None,
param_weight_for_table: Optional[nn.Parameter] = None,
) -> None:
"""
Implementation of a FusedOptimizer. Designed as a base class Embedding kernels
create_for_table is an optional flag, which if passed in only creates the optimizer for a single table.
This optimizer shares data with the broader optimizer (one per embedding kernel)
and is used to share step and LR changes
"""
self._emb_module: SplitTableBatchedEmbeddingBagsCodegen = emb_module
self._pg = pg
@dataclass
class ShardParams:
optimizer_states: List[Optional[Tuple[torch.Tensor]]]
local_metadata: List[ShardMetadata]
embedding_weights: List[torch.Tensor]
def get_optimizer_rowwise_shard_metadata_and_global_metadata(
table_global_metadata: ShardedTensorMetadata,
optimizer_state: torch.Tensor,
sharding_dim: int,
) -> Tuple[Dict[ShardMetadata, ShardMetadata], ShardedTensorMetadata]:
table_global_shards_metadata: List[ShardMetadata] = (
table_global_metadata.shards_metadata
)
# column-wise sharding
# sort the metadata based on column offset and
# we construct the momentum tensor in row-wise sharded way
if sharding_dim == 1:
table_global_shards_metadata = sorted(
table_global_shards_metadata,
key=lambda shard: shard.shard_offsets[1],
)
table_shard_metadata_to_optimizer_shard_metadata = {}
for idx, table_shard_metadata in enumerate(table_global_shards_metadata):
offset = table_shard_metadata.shard_offsets[0]
# for column-wise sharding, we still create row-wise sharded metadata for optimizer
# manually create a row-wise offset
if sharding_dim == 1:
offset = idx * table_shard_metadata.shard_sizes[0]
table_shard_metadata_to_optimizer_shard_metadata[
table_shard_metadata
] = ShardMetadata(
shard_sizes=[table_shard_metadata.shard_sizes[0]],
shard_offsets=[offset],
placement=table_shard_metadata.placement,
)
tensor_properties = TensorProperties(
dtype=optimizer_state.dtype,
layout=optimizer_state.layout,
requires_grad=False,
)
len_rw_shards = (
len(table_shard_metadata_to_optimizer_shard_metadata)
if sharding_dim == 1
else 1
)
rowwise_optimizer_st_metadata = ShardedTensorMetadata(
shards_metadata=list(
table_shard_metadata_to_optimizer_shard_metadata.values()
),
size=torch.Size([table_global_metadata.size[0] * len_rw_shards]),
tensor_properties=tensor_properties,
)
return (
table_shard_metadata_to_optimizer_shard_metadata,
rowwise_optimizer_st_metadata,
)
def get_optimizer_pointwise_shard_metadata_and_global_metadata(
table_global_metadata: ShardedTensorMetadata,
optimizer_state: torch.Tensor,
) -> Tuple[Dict[ShardMetadata, ShardMetadata], ShardedTensorMetadata]:
table_global_shards_metadata: List[ShardMetadata] = (
table_global_metadata.shards_metadata
)
table_shard_metadata_to_optimizer_shard_metadata = {}
for table_shard_metadata in table_global_shards_metadata:
table_shard_metadata_to_optimizer_shard_metadata[
table_shard_metadata
] = ShardMetadata(
shard_sizes=table_shard_metadata.shard_sizes,
shard_offsets=table_shard_metadata.shard_offsets,
placement=table_shard_metadata.placement,
)
tensor_properties = TensorProperties(
dtype=optimizer_state.dtype,
layout=optimizer_state.layout,
requires_grad=False,
)
pointwise_optimizer_st_metadata = ShardedTensorMetadata(
shards_metadata=list(
table_shard_metadata_to_optimizer_shard_metadata.values()
),
size=table_global_metadata.size,
tensor_properties=tensor_properties,
)
return (
table_shard_metadata_to_optimizer_shard_metadata,
pointwise_optimizer_st_metadata,
)
# pyre-ignore [33]
state: Dict[Any, Any] = {}
param_group: Dict[str, Any] = {
"params": [],
"lr": emb_module.optimizer_args.learning_rate,
}
params: Dict[str, Union[torch.Tensor, ShardedTensor]] = {}
# Fused optimizers use buffers (they don't use autograd) and we want to make sure
# that state_dict look identical to no-fused version.
table_to_shard_params: Dict[str, ShardParams] = {}
embedding_weights_by_table = emb_module.split_embedding_weights()
all_optimizer_states = emb_module.get_optimizer_state()
optimizer_states_keys_by_table: Dict[str, List[torch.Tensor]] = {}
for (
table_config,
optimizer_states,
weight,
) in itertools.zip_longest(
config.embedding_tables,
all_optimizer_states,
embedding_weights_by_table,
):
# When EmbeddingFusedOptimizer is created for composability, only create state
if create_for_table is not None and create_for_table != table_config.name:
continue
if table_config.name not in table_to_shard_params:
table_to_shard_params[table_config.name] = ShardParams(
optimizer_states=[], local_metadata=[], embedding_weights=[]
)
optimizer_state_values = None
if optimizer_states:
optimizer_state_values = tuple(optimizer_states.values())
for optimizer_state_value in optimizer_state_values:
assert table_config.local_rows == optimizer_state_value.size(0)
optimizer_states_keys_by_table[table_config.name] = list(
optimizer_states.keys()
)
local_metadata = table_config.local_metadata
table_to_shard_params[table_config.name].optimizer_states.append(
optimizer_state_values
)
table_to_shard_params[table_config.name].local_metadata.append(
local_metadata
)
table_to_shard_params[table_config.name].embedding_weights.append(weight)
seen_tables = set()
for table_config in config.embedding_tables:
if create_for_table is not None and create_for_table != table_config.name:
continue
if table_config.name in seen_tables:
continue
seen_tables.add(table_config.name)
table_config_global_metadata: Optional[ShardedTensorMetadata] = (
copy.deepcopy(table_config.global_metadata)
)
shard_params: ShardParams = table_to_shard_params[table_config.name]
assert table_config_global_metadata is not None
if create_for_table is None:
local_weight_shards = []
for local_weight, local_metadata in zip(
shard_params.embedding_weights, shard_params.local_metadata
):
local_weight_shards.append(Shard(local_weight, local_metadata))
table_config_global_metadata.tensor_properties.dtype = (
local_weight.dtype
)
table_config_global_metadata.tensor_properties.requires_grad = (
local_weight.requires_grad
)
# TODO share this logic to create the same TableBatchedEmbeddingSlice in FusedModules below
weight = ShardedTensor._init_from_local_shards_and_global_metadata(
local_shards=local_weight_shards,
sharded_tensor_metadata=table_config_global_metadata,
process_group=self._pg,
)
param_key = table_config.name + ".weight"
else:
assert (
param_weight_for_table is not None
), "param_weight_for_table cannot be None when using create_for_table"
weight = param_weight_for_table
param_key = ""
state[weight] = {}
param_group["params"].append(weight)
params[param_key] = weight
# Setting optimizer states
sharding_dim: int = (
1 if table_config.local_cols != table_config.embedding_dim else 0
)
if all(
opt_state is not None for opt_state in shard_params.optimizer_states
):
# pyre-ignore
def get_sharded_optim_state(momentum_idx: int) -> ShardedTensor:
assert momentum_idx > 0
momentum_local_shards: List[Shard] = []
optimizer_sharded_tensor_metadata: ShardedTensorMetadata
is_rowwise_optimizer_state: bool = (
# pyre-ignore
shard_params.optimizer_states[0][momentum_idx - 1].dim()
== 1
)
if is_rowwise_optimizer_state:
(
table_shard_metadata_to_optimizer_shard_metadata,
optimizer_sharded_tensor_metadata,
) = get_optimizer_rowwise_shard_metadata_and_global_metadata(
table_config.global_metadata,
shard_params.optimizer_states[0][momentum_idx - 1],
sharding_dim,
)
else:
(
table_shard_metadata_to_optimizer_shard_metadata,
optimizer_sharded_tensor_metadata,
) = get_optimizer_pointwise_shard_metadata_and_global_metadata(
table_config.global_metadata,
shard_params.optimizer_states[0][momentum_idx - 1],
)
for optimizer_state, table_shard_local_metadata in zip(
shard_params.optimizer_states, shard_params.local_metadata
):
local_optimizer_shard_metadata = (
table_shard_metadata_to_optimizer_shard_metadata[
table_shard_local_metadata
]
)
momentum_local_shards.append(
Shard(
optimizer_state[momentum_idx - 1],
local_optimizer_shard_metadata,
)
)
# TODO we should be creating this in SPMD fashion (e.g. init_from_local_shards), and let it derive global metadata.
return ShardedTensor._init_from_local_shards_and_global_metadata(
local_shards=momentum_local_shards,
sharded_tensor_metadata=optimizer_sharded_tensor_metadata,
process_group=self._pg,
)
num_states: int = min(
# pyre-ignore
[len(opt_state) for opt_state in shard_params.optimizer_states]
)
optimizer_state_keys = []
if num_states > 0:
optimizer_state_keys = optimizer_states_keys_by_table[
table_config.name
]
for cur_state_idx in range(0, num_states):
if cur_state_idx == 0:
# for backward compatibility
cur_state_key = "momentum1"
else:
cur_state_key = optimizer_state_keys[cur_state_idx]
state[weight][f"{table_config.name}.{cur_state_key}"] = (
get_sharded_optim_state(cur_state_idx + 1)
)
super().__init__(params, state, [param_group])
def zero_grad(self, set_to_none: bool = False) -> None:
# pyre-ignore [16]
self._emb_module.set_learning_rate(self.param_groups[0]["lr"])
# pyre-ignore [2]
def step(self, closure: Any = None) -> None:
# pyre-ignore [16]
self._emb_module.set_learning_rate(self.param_groups[0]["lr"])
def _gen_named_parameters_by_table_ssd(
emb_module: SSDTableBatchedEmbeddingBags,
table_name_to_count: Dict[str, int],
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
) -> Iterator[Tuple[str, nn.Parameter]]:
"""
Return an empty tensor to indicate that the table is on remote device.
"""
for table in config.embedding_tables:
table_name = table.name
# placeholder
weight: nn.Parameter = nn.Parameter(torch.empty(0))
# pyre-ignore
weight._in_backward_optimizers = [EmptyFusedOptimizer()]
yield (table_name, weight)
def _gen_named_parameters_by_table_fused(
emb_module: SplitTableBatchedEmbeddingBagsCodegen,
table_name_to_count: Dict[str, int],
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
) -> Iterator[Tuple[str, TableBatchedEmbeddingSlice]]:
# TODO: move logic to FBGEMM to avoid accessing fbgemm internals
for t_idx, (rows, dim, location, _) in enumerate(emb_module.embedding_specs):
table_name = config.embedding_tables[t_idx].name
if table_name not in table_name_to_count:
continue
table_count = table_name_to_count.pop(table_name)
if emb_module.weights_precision == SparseType.INT8:
dim += emb_module.int8_emb_row_dim_offset
offset = emb_module.weights_physical_offsets[t_idx]
weights: torch.Tensor
if location == EmbeddingLocation.DEVICE.value:
weights = emb_module.weights_dev
elif location == EmbeddingLocation.HOST.value:
weights = emb_module.weights_host
else:
weights = emb_module.weights_uvm
weight = TableBatchedEmbeddingSlice(
data=weights,
start_offset=offset,
end_offset=offset + table_count * rows * dim,
num_embeddings=-1,
embedding_dim=dim,
)
# this reuses logic in EmbeddingFusedOptimizer but is per table
# pyre-ignore
weight._in_backward_optimizers = [
EmbeddingFusedOptimizer(
config=config,
emb_module=emb_module,
pg=pg,
create_for_table=table_name,
param_weight_for_table=weight,
)
]
yield (table_name, weight)
def _gen_named_parameters_by_table_dense(
emb_module: DenseTableBatchedEmbeddingBagsCodegen,
table_name_to_count: Dict[str, int],
config: GroupedEmbeddingConfig,
) -> Iterator[Tuple[str, TableBatchedEmbeddingSlice]]:
# TODO: move logic to FBGEMM to avoid accessing fbgemm internals
for t_idx, (rows, dim) in enumerate(emb_module.embedding_specs):
table_name = config.embedding_tables[t_idx].name
if table_name not in table_name_to_count:
continue
table_count = table_name_to_count.pop(table_name)
offset = emb_module.weights_physical_offsets[t_idx]
weight = TableBatchedEmbeddingSlice(
data=emb_module.weights,
start_offset=offset,
end_offset=offset + table_count * rows * dim,
num_embeddings=-1,
embedding_dim=dim,
)
yield (table_name, weight)
SplitWeightType = TypeVar("SplitWeightType")
class BaseBatchedEmbedding(BaseEmbedding, Generic[SplitWeightType]):
def __init__(
self,
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
device: Optional[torch.device] = None,
) -> None:
super().__init__()
torch._C._log_api_usage_once(f"torchrec.distributed.{self.__class__.__name__}")
self._config = config
self._pg = pg
self._local_rows: List[int] = []
self._weight_init_mins: List[float] = []
self._weight_init_maxs: List[float] = []
self._num_embeddings: List[int] = []
self._local_cols: List[int] = []
self._feature_table_map: List[int] = []
self.table_name_to_count: Dict[str, int] = {}
self._param_per_table: Dict[str, TableBatchedEmbeddingSlice] = {}
for idx, config in enumerate(self._config.embedding_tables):
self._local_rows.append(config.local_rows)
self._weight_init_mins.append(config.get_weight_init_min())
self._weight_init_maxs.append(config.get_weight_init_max())
self._num_embeddings.append(config.num_embeddings)
self._local_cols.append(config.local_cols)
self._feature_table_map.extend([idx] * config.num_features())
if config.name not in self.table_name_to_count:
self.table_name_to_count[config.name] = 0
self.table_name_to_count[config.name] += 1
def init_parameters(self) -> None:
# initialize embedding weights
assert len(self._num_embeddings) == len(self.split_embedding_weights())
for rows, emb_dim, weight_init_min, weight_init_max, param in zip(
self._local_rows,
self._local_cols,
self._weight_init_mins,
self._weight_init_maxs,
self.split_embedding_weights(),
):
assert param.shape == (rows, emb_dim) # pyre-ignore[16]
param.data.uniform_( # pyre-ignore[16]
weight_init_min,
weight_init_max,
)
def forward(self, features: KeyedJaggedTensor) -> torch.Tensor:
return self.emb_module(
indices=features.values().long(),
offsets=features.offsets().long(),
)
# pyre-fixme[14]: `state_dict` overrides method defined in `Module` inconsistently.
def state_dict(
self,
destination: Optional[Dict[str, Any]] = None,
prefix: str = "",
keep_vars: bool = False,
) -> Dict[str, Any]:
self.flush()
return get_state_dict(
self._config.embedding_tables,
# pyre-ignore
self.split_embedding_weights(),
self._pg,
destination,
prefix,
)
def split_embedding_weights(self) -> List[SplitWeightType]:
return self.emb_module.split_embedding_weights()
@property
@abc.abstractmethod
def emb_module(
self,
) -> Union[
DenseTableBatchedEmbeddingBagsCodegen,
SplitTableBatchedEmbeddingBagsCodegen,
IntNBitTableBatchedEmbeddingBagsCodegen,
]: ...
@property
def config(self) -> GroupedEmbeddingConfig:
return self._config
def flush(self) -> None:
pass
def purge(self) -> None:
pass
def named_split_embedding_weights(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, torch.Tensor]]:
assert (
remove_duplicate
), "remove_duplicate=False not supported in BaseBatchedEmbedding.named_split_embedding_weights"
for config, param in zip(
self._config.embedding_tables,
self.emb_module.split_embedding_weights(),
):
key = append_prefix(prefix, f"{config.name}.weight")
yield key, param
def named_parameters_by_table(
self,
) -> Iterator[Tuple[str, TableBatchedEmbeddingSlice]]:
"""
Like named_parameters(), but yields table_name and embedding_weights which are wrapped in TableBatchedEmbeddingSlice.
For a single table with multiple shards (i.e CW) these are combined into one table/weight.
Used in composability.
"""
for name, param in self._param_per_table.items():
yield name, param
class KeyValueEmbedding(BaseBatchedEmbedding[torch.Tensor], FusedOptimizerModule):
def __init__(
self,
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
device: Optional[torch.device] = None,
) -> None:
super().__init__(config, pg, device)
assert (
len(config.embedding_tables) > 0
), "Expected to see at least one table in SSD TBE, but found 0."
assert (
len({table.embedding_dim for table in config.embedding_tables}) == 1
), "Currently we expect all tables in SSD TBE to have the same embedding dimension."
ssd_tbe_params = _populate_ssd_tbe_params(config)
compute_kernel = config.embedding_tables[0].compute_kernel
embedding_location = compute_kernel_to_embedding_location(compute_kernel)
self._emb_module: SSDTableBatchedEmbeddingBags = SSDTableBatchedEmbeddingBags(
embedding_specs=list(zip(self._local_rows, self._local_cols)),
feature_table_map=self._feature_table_map,
ssd_cache_location=embedding_location,
pooling_mode=PoolingMode.NONE,
**ssd_tbe_params,
).to(device)
self._optim: KeyValueEmbeddingFusedOptimizer = KeyValueEmbeddingFusedOptimizer(
config,
self._emb_module,
pg,
)
self._param_per_table: Dict[str, nn.Parameter] = dict(
_gen_named_parameters_by_table_ssd(
emb_module=self._emb_module,
table_name_to_count=self.table_name_to_count.copy(),
config=self._config,
pg=pg,
)
)
self.init_parameters()
def init_parameters(self) -> None:
"""
An advantage of SSD TBE is that we don't need to init weights. Hence skipping.
"""
pass
@property
def emb_module(
self,
) -> SSDTableBatchedEmbeddingBags:
return self._emb_module
@property
def fused_optimizer(self) -> FusedOptimizer:
"""
SSD Embedding fuses backward with backward.
"""
return self._optim
def state_dict(
self,
destination: Optional[Dict[str, Any]] = None,
prefix: str = "",
keep_vars: bool = False,
) -> Dict[str, Any]:
if destination is None:
destination = OrderedDict()
return destination
def named_parameters(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, nn.Parameter]]:
"""
Only allowed ways to get state_dict.
"""
for name, tensor in self.named_split_embedding_weights(
prefix, recurse, remove_duplicate
):
# hack before we support optimizer on sharded parameter level
# can delete after PEA deprecation
param = nn.Parameter(tensor)
# pyre-ignore
param._in_backward_optimizers = [EmptyFusedOptimizer()]
yield name, param
def named_split_embedding_weights(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, torch.Tensor]]:
assert (
remove_duplicate
), "remove_duplicate=False not supported in BaseBatchedEmbedding.named_split_embedding_weights"
for config, tensor in zip(
self._config.embedding_tables,
self.split_embedding_weights(),
):
key = append_prefix(prefix, f"{config.name}.weight")
yield key, tensor
def flush(self) -> None:
"""
Flush the embeddings in cache back to SSD. Should be pretty expensive.
"""
self.emb_module.flush()
def purge(self) -> None:
"""
Reset the cache space. This is needed when we load state dict.
"""
# TODO: move the following to SSD TBE.
self.emb_module.lxu_cache_weights.zero_()
self.emb_module.lxu_cache_state.fill_(-1)
def split_embedding_weights(self) -> List[torch.Tensor]:
"""
Return fake tensors.
"""
return [param.data for param in self._param_per_table.values()]
class BatchedFusedEmbedding(BaseBatchedEmbedding[torch.Tensor], FusedOptimizerModule):
def __init__(
self,
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
device: Optional[torch.device] = None,
) -> None:
super().__init__(config, pg, device)
managed: List[EmbeddingLocation] = []
compute_devices: List[ComputeDevice] = []
for table in config.embedding_tables:
if device is not None and device.type == "cuda":
compute_devices.append(ComputeDevice.CUDA)
managed.append(
compute_kernel_to_embedding_location(table.compute_kernel)
)
elif device is not None and device.type == "mtia":
compute_devices.append(ComputeDevice.MTIA)
# Set EmbeddingLocation.HOST to make embedding op in FBGEMM choose CPU path.
# But the tensor will still be created on MTIA with device type "mtia".
managed.append(EmbeddingLocation.HOST)
else:
compute_devices.append(ComputeDevice.CPU)
managed.append(EmbeddingLocation.HOST)
weights_precision = data_type_to_sparse_type(config.data_type)
fused_params = config.fused_params or {}
if "cache_precision" not in fused_params:
fused_params["cache_precision"] = weights_precision
self._emb_module: SplitTableBatchedEmbeddingBagsCodegen = (
SplitTableBatchedEmbeddingBagsCodegen(
embedding_specs=list(
zip(self._local_rows, self._local_cols, managed, compute_devices)
),
feature_table_map=self._feature_table_map,
pooling_mode=PoolingMode.NONE,
weights_precision=weights_precision,
device=device,
table_names=[t.name for t in config.embedding_tables],
**fused_params,
)
)
self._optim: EmbeddingFusedOptimizer = EmbeddingFusedOptimizer(
config,
self._emb_module,
pg,
)
self._param_per_table: Dict[str, TableBatchedEmbeddingSlice] = dict(
_gen_named_parameters_by_table_fused(
emb_module=self._emb_module,
table_name_to_count=self.table_name_to_count.copy(),
config=self._config,
pg=pg,
)
)
self.init_parameters()
@property
def emb_module(
self,
) -> SplitTableBatchedEmbeddingBagsCodegen:
return self._emb_module
@property
def fused_optimizer(self) -> FusedOptimizer:
return self._optim
def named_buffers(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, torch.Tensor]]:
"""
By convention, fused parameters are designated as buffers because they no longer
have gradients available to external optimizers.
"""
# TODO can delete this override once SEA is removed
yield from ()
def named_parameters(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, nn.Parameter]]:
for name, tensor in self.named_split_embedding_weights(
prefix, recurse, remove_duplicate
):
# hack before we support optimizer on sharded parameter level
# can delete after SEA deprecation
param = nn.Parameter(tensor)
# pyre-ignore
param._in_backward_optimizers = [EmptyFusedOptimizer()]
yield name, param
def flush(self) -> None:
self._emb_module.flush()
def purge(self) -> None:
self._emb_module.reset_cache_states()
class BatchedDenseEmbedding(BaseBatchedEmbedding[torch.Tensor]):
def __init__(
self,
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
device: Optional[torch.device] = None,
) -> None:
super().__init__(config, pg, device)
weights_precision = data_type_to_sparse_type(config.data_type)
fused_params = config.fused_params or {}
output_dtype = fused_params.get("output_dtype", SparseType.FP32)
self._emb_module: DenseTableBatchedEmbeddingBagsCodegen = (
DenseTableBatchedEmbeddingBagsCodegen(
list(zip(self._local_rows, self._local_cols)),
feature_table_map=self._feature_table_map,
pooling_mode=PoolingMode.NONE,
use_cpu=device is None
or device.type == "cpu"
or not torch.cuda.is_available(),
weights_precision=weights_precision,
output_dtype=output_dtype,
)
)
self._param_per_table: Dict[str, TableBatchedEmbeddingSlice] = dict(
_gen_named_parameters_by_table_dense(
self._emb_module, self.table_name_to_count.copy(), self._config
)
)
self.init_parameters()
@property
def emb_module(
self,
) -> DenseTableBatchedEmbeddingBagsCodegen:
return self._emb_module
def named_buffers(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, torch.Tensor]]:
yield from ()
def named_parameters(
self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
) -> Iterator[Tuple[str, nn.Parameter]]:
combined_key = "/".join(
[config.name for config in self._config.embedding_tables]
)
yield append_prefix(prefix, f"{combined_key}.weight"), cast(
nn.Parameter, self._emb_module.weights
)
class BaseBatchedEmbeddingBag(BaseEmbedding, Generic[SplitWeightType]):
def __init__(
self,
config: GroupedEmbeddingConfig,
pg: Optional[dist.ProcessGroup] = None,
device: Optional[torch.device] = None,
sharding_type: Optional[ShardingType] = None,
) -> None:
super().__init__()
torch._C._log_api_usage_once(f"torchrec.distributed.{self.__class__.__name__}")
self._config = config
self._pg = pg
self._pooling: PoolingMode = pooling_type_to_pooling_mode(
config.pooling, sharding_type # pyre-ignore[6]
)
self._local_rows: List[int] = []
self._weight_init_mins: List[float] = []
self._weight_init_maxs: List[float] = []
self._num_embeddings: List[int] = []
self._local_cols: List[int] = []
self._feature_table_map: List[int] = []
self._emb_names: List[str] = []
self._lengths_per_emb: List[int] = []
self.table_name_to_count: Dict[str, int] = {}
self._param_per_table: Dict[str, TableBatchedEmbeddingSlice] = {}