Skip to content

Better error message when the sharding fails with Zero Division calculation #3263

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

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
36 changes: 25 additions & 11 deletions torchrec/distributed/planner/enumerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,31 @@ def enumerate(
sharder.compute_kernels(sharding_type, self._compute_device),
sharding_type,
):
(
shard_sizes,
shard_offsets,
) = calculate_shard_sizes_and_offsets(
tensor=param,
world_size=self._world_size,
local_world_size=self._local_world_size,
sharding_type=sharding_type,
col_wise_shard_dim=col_wise_shard_dim,
device_memory_sizes=self._device_memory_sizes,
)
try:
(
shard_sizes,
shard_offsets,
) = calculate_shard_sizes_and_offsets(
tensor=param,
world_size=self._world_size,
local_world_size=self._local_world_size,
sharding_type=sharding_type,
col_wise_shard_dim=col_wise_shard_dim,
device_memory_sizes=self._device_memory_sizes,
)
except ZeroDivisionError as e:
# Re-raise with additional context about the table and module
raise ValueError(
f"Failed to calculate sharding plan for table '{name}': {str(e)} "
f"Context: table_name='{name}', module_path='{child_path}', "
f"module_type='{type(child_module).__name__}', "
f"sharder='{sharder.__class__.__name__}', "
f"tensor.shape={param.shape}, sharding_type='{sharding_type}', "
f"compute_kernel='{compute_kernel}', world_size={self._world_size}, "
f"local_world_size={self._local_world_size}, "
f"col_wise_shard_dim={col_wise_shard_dim}, "
f"compute_device='{self._compute_device}'"
) from e
dependency = None
if isinstance(child_module, EmbeddingTower):
dependency = child_path
Expand Down
18 changes: 1 addition & 17 deletions torchrec/distributed/sharding_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,7 @@ def _calculate_cw_shard_sizes_and_offsets(
rows: int,
col_wise_shard_dim: Optional[int] = None,
) -> Tuple[List[List[int]], List[List[int]]]:
block_size: int = min(
(
_find_base_dim(col_wise_shard_dim, columns)
if col_wise_shard_dim
else _find_base_dim(MIN_CW_DIM, columns)
),
columns,
)

if columns % block_size != 0:
warnings.warn(
f"Dim of {columns} cannot be evenly divided with column wise shard"
"dim {col_wise_shard_dim}, overriding block_size to embedding_dim={columns}",
UserWarning,
stacklevel=2,
)
block_size = columns
block_size = _get_block_size_for_cw_shard(columns, col_wise_shard_dim)

num_col_wise_shards, _residual = divmod(columns, block_size)

Expand Down