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 LogicalRules type annotation. (Tuple[str] is a tuple with single element string, #3877

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
8 changes: 4 additions & 4 deletions flax/linen/spmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ def _mesh_assignment_free(new_assignment, existing_assignments):


def _logical_to_mesh_axes(
array_dim_names: Optional[Sequence[Optional[str]]],
rules: Optional[LogicalRules] = None,
) -> Optional[List[Union[_UnassignedAxis, None, str, Tuple[str]]]]:
array_dim_names: Optional[Sequence[Optional[str]]],
rules: Optional[LogicalRules] = None,
) -> Optional[List[Union[_UnassignedAxis, None, str, Tuple[str, ...]]]]:
"""Same as logical_to_mesh_axes, but doesn't fill in _unassigned_axis."""
if array_dim_names is None:
return None
Expand All @@ -126,7 +126,7 @@ def _logical_to_mesh_axes(
if not isinstance(rules, (tuple, list)):
raise ValueError('Unknown axis rule specification type.')
# We assign mesh axes using a priority based ruleset over logical axis names.
result: List[Union[_UnassignedAxis, None, str, Tuple[str]]]
result: List[Union[_UnassignedAxis, None, str, Tuple[str, ...]]]
result = [_unassigned_axis] * len(array_dim_names)
for rule_model_name, rule_mesh_names in rules:
if rule_model_name in array_dim_names:
Expand Down
6 changes: 4 additions & 2 deletions flax/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ class Out(Generic[T]):

LogicalNames = Tuple[Union[str, None], ...]

LogicalRules = Sequence[Tuple[str, Union[str, Tuple[str], None]]]
# Maps each logical axis to physical mesh, can be either None (replicated),
# one physical axis or a tuple of physical axes.
LogicalRules = Sequence[Tuple[str, Union[str, Tuple[str, ...], None]]]
ArrayPytree = Any # pylint: disable=invalid-name
LogicalPartitionSpec = Any # pylint: disable=invalid-name
LogicalPartitionSpecPytree = Any # pylint: disable=invalid-name
PartitionSpecPytree = Any # pylint: disable=invalid-name

Sharding = Tuple[Optional[str], ...]
Sharding = Tuple[Optional[str], ...]
Loading