[Pallas] Integrate FlashAttention with SPMD#6935
Merged
alanwaketan merged 9 commits intomasterfrom Apr 18, 2024
Merged
Conversation
JackCaoG
approved these changes
Apr 17, 2024
Comment on lines
+14
to
+19
| if xr.device_type() == 'TPU': | ||
| from torch_xla.experimental.custom_kernel import jax_import_guard | ||
| jax_import_guard() | ||
| import jax | ||
| import jax.numpy as jnp | ||
| from jax.experimental import pallas as pl |
Collaborator
There was a problem hiding this comment.
nit, you can put this part in the setup class similar to https://github.com/pytorch/xla/blob/master/test/spmd/test_xla_sharding_base.py#L31-L35
Collaborator
Author
There was a problem hiding this comment.
Is python import global?
| jax.config.update('jax_default_matmul_precision', jax.lax.Precision.HIGHEST) | ||
| from torch_xla.experimental.custom_kernel import flash_attention | ||
|
|
||
| xr.use_spmd() |
Collaborator
There was a problem hiding this comment.
nit, this should be called in the setup class since it is a one time global config.
Collaborator
Author
There was a problem hiding this comment.
I probably can call this in main as well. The setup class seems overkilled for this.
jonb377
approved these changes
Apr 17, 2024
| @unittest.skipIf(xr.device_type() != 'TPU' or tpu.version() < 3, | ||
| "This test only works on TPUv3+.") | ||
| def test_flash_attention_spmd_data_parallel(self): | ||
| jax.config.update('jax_default_matmul_precision', jax.lax.Precision.HIGHEST) |
Collaborator
There was a problem hiding this comment.
Does this impact the resulting kernel?
|
|
||
| @staticmethod | ||
| def forward(ctx, q, k, v, causal=False): | ||
| def forward(ctx, q, k, v, causal=False, sharding_spec=None, mesh=None): |
Collaborator
There was a problem hiding this comment.
nit: sharding_spec -> partition_spec?
Collaborator
Author
|
Thanks Jon and Jack for the reviews. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
This pull request integrating FlashAttention with SPMD. The way it works is to create a manual sharding region for the kernel which means we wraps all the inputs with enable_manual_sharding and all the outputs with disable_manual_sharding.
Added a new test file because the original test file is not SPMD aware.
Test Plan:
PJRT_DEVICE=TPU python test/test_pallas_spmd.py