-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add column wise sharding support for EmbeddingCollection (sequence em…
…beddings) (#432) Summary: Pull Request resolved: #432 Support for CW and TWCW sharding in EmbeddingCollection. Added logic to stitch feature outputs with local embedding dim after output dist to match original dim. Outputs are stored in order of rank, however, in column-wise sharding, there can be multiple shards of a table on the same rank and thereby multiple outputs on the same rank. i.e. rank 0: [f_0, f_0, f_1] rank 1: [f_0, f_1] output: [f_0, f_0, f_1, f_0, f_1] f_0 shard ranks = [0, 1, 0] Since outputs are stored by rank, the inter-shard order is lost and the shards on rank 0 would be combined first, making an incorrect combination of f_0's output with the shard ranks = [0, 0, 1]. To keep the correct shard rank of [0, 1, 0] when combining outputs, we generate permute indices for each feature to match the shard ranks. Differential Revision: D36944684 fbshipit-source-id: 67b5a4c3825583dd675926a37db79b79b7653a97
- Loading branch information
1 parent
50c1b05
commit 99251a8
Showing
8 changed files
with
254 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/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. | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
import torch | ||
from torchrec.distributed.embedding_lookup import GroupedEmbeddingsLookup | ||
from torchrec.distributed.embedding_sharding import ( | ||
BaseEmbeddingLookup, | ||
BaseSparseFeaturesDist, | ||
) | ||
from torchrec.distributed.embedding_types import ( | ||
BaseGroupedFeatureProcessor, | ||
SparseFeatures, | ||
) | ||
from torchrec.distributed.sharding.cw_sharding import BaseCwEmbeddingSharding | ||
from torchrec.distributed.sharding.sequence_sharding import BaseSequenceEmbeddingDist | ||
from torchrec.distributed.sharding.tw_sequence_sharding import TwSequenceEmbeddingDist | ||
from torchrec.distributed.sharding.tw_sharding import TwSparseFeaturesDist | ||
|
||
|
||
class CwSequenceEmbeddingSharding( | ||
BaseCwEmbeddingSharding[SparseFeatures, torch.Tensor] | ||
): | ||
""" | ||
Shards sequence (unpooled) embeddings column-wise, i.e.. a given embedding is | ||
partitioned along its columns and placed on specified ranks. | ||
""" | ||
|
||
def create_input_dist( | ||
self, | ||
device: Optional[torch.device] = None, | ||
) -> BaseSparseFeaturesDist[SparseFeatures]: | ||
return TwSparseFeaturesDist( | ||
self._pg, | ||
self._id_list_features_per_rank(), | ||
self._id_score_list_features_per_rank(), | ||
device if device is not None else self._device, | ||
) | ||
|
||
def create_lookup( | ||
self, | ||
device: Optional[torch.device] = None, | ||
fused_params: Optional[Dict[str, Any]] = None, | ||
feature_processor: Optional[BaseGroupedFeatureProcessor] = None, | ||
) -> BaseEmbeddingLookup: | ||
assert feature_processor is None | ||
return GroupedEmbeddingsLookup( | ||
grouped_configs=self._grouped_embedding_configs, | ||
fused_params=fused_params, | ||
pg=self._pg, | ||
device=device if device is not None else self._device, | ||
) | ||
|
||
def create_output_dist( | ||
self, | ||
device: Optional[torch.device] = None, | ||
) -> BaseSequenceEmbeddingDist[torch.Tensor]: | ||
return TwSequenceEmbeddingDist( | ||
self._pg, | ||
self._id_list_features_per_rank(), | ||
device if device is not None else self._device, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters