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

fix divide NoneType error when use mc-ebc and mean pooling #2829

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 52 additions & 1 deletion torchrec/distributed/mc_embedding_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ShardedEmbeddingModule,
)
from torchrec.distributed.embeddingbag import (
_create_mean_pooling_divisor,
EmbeddingBagCollectionSharder,
ShardedEmbeddingBagCollection,
)
Expand Down Expand Up @@ -108,7 +109,7 @@ def __init__(
)
# TODO: This is a hack since _embedding_module doesn't need input
# dist, so eliminating it so all fused a2a will ignore it.
self._embedding_module._has_uninitialized_input_dist = False
# self._embedding_module._has_uninitialized_input_dist = False
embedding_shardings = (
self._embedding_module._embedding_shardings
if isinstance(self._embedding_module, ShardedEmbeddingBagCollection)
Expand Down Expand Up @@ -152,6 +153,56 @@ def input_dist(
features: KeyedJaggedTensor,
) -> Awaitable[Awaitable[KJTList]]:
# TODO: resolve incompatiblity with different contexts
if self._embedding_module._has_uninitialized_input_dist:
if isinstance(self._embedding_module, ShardedEmbeddingBagCollection):
self._features_order = []
# disable feature permutation in mc, because we should
# permute features in mc-ebc before mean pooling callback.
if self._managed_collision_collection._has_uninitialized_input_dist:
self._managed_collision_collection._create_input_dists(
input_feature_names=features.keys()
)
self._managed_collision_collection._has_uninitialized_input_dist = (
False
)
if self._managed_collision_collection._features_order:
self._features_order = (
self._managed_collision_collection._features_order
)
self._managed_collision_collection._features_order = []
if self._embedding_module._has_mean_pooling_callback:
self._embedding_module._init_mean_pooling_callback(
features.keys(),
# pyre-ignore [16]
ctx.inverse_indices,
)
self._embedding_module._has_uninitialized_input_dist = False
if isinstance(self._embedding_module, ShardedEmbeddingBagCollection):
with torch.no_grad():
if self._features_order:
features = features.permute(
self._features_order,
self._managed_collision_collection._features_order_tensor,
)
if self._embedding_module._has_mean_pooling_callback:
ctx.divisor = _create_mean_pooling_divisor(
lengths=features.lengths(),
stride=features.stride(),
keys=features.keys(),
offsets=features.offsets(),
pooling_type_to_rs_features=self._embedding_module._pooling_type_to_rs_features,
stride_per_key=features.stride_per_key(),
dim_per_key=self._embedding_module._dim_per_key,
embedding_names=self._embedding_module._embedding_names,
embedding_dims=self._embedding_module._embedding_dims,
# pyre-ignore [16]
variable_batch_per_feature=ctx.variable_batch_per_feature,
kjt_inverse_order=self._embedding_module._kjt_inverse_order,
kjt_key_indices=self._embedding_module._kjt_key_indices,
kt_key_ordering=self._embedding_module._kt_key_ordering,
inverse_indices=ctx.inverse_indices,
weights=features.weights_or_none(),
)
return self._managed_collision_collection.input_dist(
# pyre-fixme [6]
ctx,
Expand Down
12 changes: 6 additions & 6 deletions torchrec/distributed/mc_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __init__(

self._feature_to_table: Dict[str, str] = module._feature_to_table
self._table_to_features: Dict[str, List[str]] = module._table_to_features
self._has_uninitialized_input_dists: bool = True
self._has_uninitialized_input_dist: bool = True
self._input_dists: List[nn.Module] = []
self._managed_collision_modules = nn.ModuleDict()
self._create_managed_collision_modules(module)
Expand Down Expand Up @@ -587,9 +587,9 @@ def input_dist(
ctx: ManagedCollisionCollectionContext,
features: KeyedJaggedTensor,
) -> Awaitable[Awaitable[KJTList]]:
if self._has_uninitialized_input_dists:
if self._has_uninitialized_input_dist:
self._create_input_dists(input_feature_names=features.keys())
self._has_uninitialized_input_dists = False
self._has_uninitialized_input_dist = False

with torch.no_grad():
if self._features_order:
Expand Down Expand Up @@ -991,7 +991,7 @@ def __init__(

self._feature_to_table: Dict[str, str] = module._feature_to_table
self._table_to_features: Dict[str, List[str]] = module._table_to_features
self._has_uninitialized_input_dists: bool = True
self._has_uninitialized_input_dist: bool = True
self._input_dists: torch.nn.ModuleList = torch.nn.ModuleList([])
self._managed_collision_modules: nn.ModuleDict = nn.ModuleDict()
self._create_managed_collision_modules(module)
Expand Down Expand Up @@ -1199,11 +1199,11 @@ def input_dist(
ctx: ManagedCollisionCollectionContext,
features: KeyedJaggedTensor,
) -> ListOfKJTList:
if self._has_uninitialized_input_dists:
if self._has_uninitialized_input_dist:
self._create_input_dists(
input_feature_names=features.keys(), feature_device=features.device()
)
self._has_uninitialized_input_dists = False
self._has_uninitialized_input_dist = False

with torch.no_grad():
if self._features_order:
Expand Down