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

Input batch sharding strategy BATCH #884

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

apoorvtintin
Copy link

@apoorvtintin apoorvtintin commented Dec 11, 2024

Axlearn currently supports DataPartitionType strategies FULL and REPLICATED for input batches. This PR adds support for a third sharding strategy DataPartitionType.BATCH.

This sharding strategy shards the input batch on sharding axis inferred as "batch_axis". This ensures batches are replicated on the "model" dimension and thus avoids unnecessary collectives to reshard input batches when Tensor parallelism is used.

@apoorvtintin apoorvtintin force-pushed the mainline_upstream_input_sharding branch 2 times, most recently from 79a8c21 to 03052e4 Compare December 11, 2024 21:10
@@ -607,6 +610,7 @@ def host_to_global_device_array(
host_arrays: Nested[Union[np.ndarray, Tensor]],
*,
partition: DataPartitionType = DataPartitionType.FULL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @markblee plans to remove the DataPartitionType enum and rely on https://jax.readthedocs.io/en/latest/_autosummary/jax.make_array_from_process_local_data.html to support flexible partition specs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that sounds promising.

Hello @markblee, let me know if this PR is needed till you make your changes, or if you have your design in mind I can reshape the PR to make it compatible with your design.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think make_array_from_process_local_data is already merged: #781

@hanzhi713
Copy link
Member

@apoorvtintin With this new sharding strategy, how do you plan to support model parallelism across hosts (i.e. per host bs < 1)? If this is not a supported use case, then this change LGTM. I could imagine the support for model parallelism across hosts is not needed in the short term for neuron since the number of chips per host is large.

@kelvin-zou
Copy link
Contributor

kelvin-zou commented Jan 9, 2025

Sorry for the late review, I should've checked this earlier.
@apoorvtintin can you actually look into logical batch and use that instead (

- Convert a global physical batch to a global logical batch (physical_to_logical_batch).
)? From what I see here, this feature is a bit redundant (and less flexible as well) than the logical batch size, and with that, probably you don't need this PR if you just want to run something like batch_size=0.5 or 0.25 per chip with model parallelism.

@@ -423,6 +423,7 @@ def get_trainer_kwargs(
raise NotImplementedError(f"Unknown model size {model_size}.")
model_kwargs = trainer_kwargs.pop("model_kwargs")
model_kwargs.setdefault("vocab_size", vocab_size)
trainer_kwargs["input_partition_type"] = None if backend != "neuron" else DataPartitionType.BATCH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go with a configmodifier instead of hardcode in the code, otherwise it becomes hard for people to debug

),
out_shardings=dict(
replicated=None,
per_example=utils.input_partition_spec(),
per_example=utils.data_partition_type_to_spec( partition=self.config.input_partition_type, batch_axis_names=self.config.batch_axis_names),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run through pylint? this line seems quite long.

"""Returns a PartitionSpec for the given partition type."""
if partition == DataPartitionType.FULL:
return input_partition_spec()
elif partition == DataPartitionType.REPLICATED:
return None
elif partition == DataPartitionType.BATCH:
return PartitionSpec(batch_axis_names)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of directly assigning PartitionSpec, maybe extend input_partition_spec with an argument batch_axis_names, with default None?

@@ -37,16 +36,15 @@ def is_supported(
)
)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using a different pylint style? Those lines should not be removed.

@hanzhi713
Copy link
Member

Sorry for the late review, I should've checked this earlier. @apoorvtintin can you actually look into logical batch and use that instead (

- Convert a global physical batch to a global logical batch (physical_to_logical_batch).

)? From what I see here, this feature is a bit redundant (and less flexible as well) than the logical batch size, and with that, probably you don't need this PR if you just want to run something like batch_size=0.5 or 0.25 per chip with model parallelism.

I think the primary purpose of this PR (as stated in the description) is to avoid the resharding cost from physical bs to logical bs when there's no need to shard data along model axis. This could remove the collective for inputs re-sharding and thus reduce step time.

@apoorvtintin May I know how much time do we save by removing this collective? If the benefit is not significant, we can stick to the old code.

@kelvin-zou
Copy link
Contributor

I think the primary purpose of this PR (as stated in the description) is to avoid the resharding cost from physical bs to logical bs when there's no need to shard data along model axis. This could remove the collective for inputs re-sharding and thus reduce step time.

But input collective is only triggered within host, which should be insignificant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants