Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split FPEBC #2535

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion torchrec/modules/fp_embedding_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# pyre-strict

from typing import Dict, List, Set, Union
from typing import Dict, List, Set, Tuple, Union

import torch
import torch.nn as nn
Expand Down Expand Up @@ -55,6 +55,15 @@ def apply_feature_processors_to_kjt(
)


class FeatureProcessorDictWrapper(FeatureProcessorsCollection):
def __init__(self, feature_processors: nn.ModuleDict) -> None:
super().__init__()
self._feature_processors = feature_processors

def forward(self, features: KeyedJaggedTensor) -> KeyedJaggedTensor:
return apply_feature_processors_to_kjt(features, self._feature_processors)


class FeatureProcessedEmbeddingBagCollection(nn.Module):
"""
FeatureProcessedEmbeddingBagCollection represents a EmbeddingBagCollection module and a set of feature processor modules.
Expand Down Expand Up @@ -125,6 +134,18 @@ def __init__(
feature_names_set.update(table_config.feature_names)
self._feature_names: List[str] = list(feature_names_set)

def split(
self,
) -> Tuple[FeatureProcessorsCollection, EmbeddingBagCollection]:
if isinstance(self._feature_processors, nn.ModuleDict):
return (
FeatureProcessorDictWrapper(self._feature_processors),
self._embedding_bag_collection,
)
else:
assert isinstance(self._feature_processors, FeatureProcessorsCollection)
return self._feature_processors, self._embedding_bag_collection

def forward(
self,
features: KeyedJaggedTensor,
Expand Down
18 changes: 18 additions & 0 deletions torchrec/modules/tests/test_fp_embedding_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def test_position_weighted_module_ebc_with_excessive_features(self) -> None:
self.assertEqual(pooled_embeddings.values().size(), (3, 16))
self.assertEqual(pooled_embeddings.offset_per_key(), [0, 8, 16])

# Test split method, FP then EBC
fp, ebc = fp_ebc.split()
fp_kjt = fp(features)
pooled_embeddings_split = ebc(fp_kjt)

self.assertEqual(pooled_embeddings_split.keys(), ["f1", "f2"])
self.assertEqual(pooled_embeddings_split.values().size(), (3, 16))
self.assertEqual(pooled_embeddings_split.offset_per_key(), [0, 8, 16])


class PositionWeightedModuleCollectionEmbeddingBagCollectionTest(unittest.TestCase):
def generate_fp_ebc(self) -> FeatureProcessedEmbeddingBagCollection:
Expand Down Expand Up @@ -144,3 +153,12 @@ def test_position_weighted_collection_module_ebc(self) -> None:
pooled_embeddings_gm_script.offset_per_key(),
pooled_embeddings.offset_per_key(),
)

# Test split method, FP then EBC
fp, ebc = fp_ebc.split()
fp_kjt = fp(features)
pooled_embeddings_split = ebc(fp_kjt)

self.assertEqual(pooled_embeddings_split.keys(), ["f1", "f2"])
self.assertEqual(pooled_embeddings_split.values().size(), (3, 16))
self.assertEqual(pooled_embeddings_split.offset_per_key(), [0, 8, 16])
Loading