-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support custom mask for flash attention #20
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
base: feat/eagle-support
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Iamleos, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant feature to the Flash Attention mechanism: the ability to apply custom attention masks. This enhancement provides greater flexibility for advanced attention patterns, such as those required for speculative decoding verification. The changes involve modifying the underlying Pallas kernel, updating the reference implementation, integrating the new parameters into the attention backend, and adding comprehensive test coverage to ensure correctness. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for custom attention masks in the flash attention kernel, which is a valuable feature for non-causal attention patterns like those in speculative decoding. The changes are comprehensive, affecting the reference implementation, the optimized Pallas kernel, and the testing suite. While the core logic for the custom mask in the kernel seems sound, I've identified several issues, including critical bugs in the reference implementation and test setup that will prevent the code from running as intended. There are also some inconsistencies in mask handling between the reference and kernel implementations that should be reconciled. My review provides specific suggestions to address these points and improve the overall quality and correctness of the implementation.
| mask_start = cu_kv_lens[i] | ||
| mask = custom_mask[mask_start : mask_start + kv_len] | ||
| if sliding_window is not None: | ||
| mask = jnp.logical_or(mask, q_span - sliding_window >= kv_span) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will cause a NameError when causal=False and sliding_window is not None, because q_span and kv_span are only defined within the if causal: block.
Since sliding window attention is a form of causal masking, it probably doesn't make sense to use it with a custom non-causal mask. You should consider raising a ValueError if causal=False and sliding_window is provided, or define q_span and kv_span for the non-causal case if this combination is intended to be supported.
| if custom_mask == None or custom_mask.size() < jnp.cumsum(kv_lens)[-1]: | ||
| raise ValueError( | ||
| f"use custom_mask, custom_mask length must larger than total kv length" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bug here. .size is an attribute of a JAX array, not a method. Calling custom_mask.size() will raise a TypeError. It should be custom_mask.size.
Additionally, for style and correctness, it's better to use is None and is not None for checking against None instead of == None and != None.
| if custom_mask == None or custom_mask.size() < jnp.cumsum(kv_lens)[-1]: | |
| raise ValueError( | |
| f"use custom_mask, custom_mask length must larger than total kv length" | |
| ) | |
| if custom_mask is None or custom_mask.size < jnp.cumsum(kv_lens)[-1]: | |
| raise ValueError( | |
| f"use custom_mask, custom_mask length must larger than total kv length" | |
| ) |
| spec_info = EagleVerifyInput( | ||
| custom_mask=custom_mask, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| cu_q_lens: jax.Array, # i32[padded_batch_size + 1] | ||
| cu_kv_lens: jax.Array, # i32[padded_batch_size + 1] | ||
| distribution: jax.Array, # i32[3] | ||
| custom_mask: jax.Array, # if causal is True, custom_mask shape is [patten_total_kv_len], else [0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment for custom_mask is confusing and seems to have the logic reversed. The custom mask is used when causal is False (i.e., causal=0), not when it's True. Please correct the comment to avoid confusion.
| custom_mask: jax.Array, # if causal is True, custom_mask shape is [patten_total_kv_len], else [0] | |
| custom_mask: jax.Array, # if causal is False, custom_mask shape is [patten_total_kv_len], else [0] |
| print( | ||
| f"{cu_q_lens.shape=} {cu_kv_lens.shape=} {bq_sz=} {bkv_p=} {distribution=} {chunk_prefill_size=}" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if len(mode_args) == 5: | ||
| num_heads, head_dim, num_kv_heads, page_size, dtype = mode_args | ||
| causal = True | ||
| else: | ||
| num_heads, head_dim, num_kv_heads, page_size, dtype, causal = mode_args |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def test_mha_decode_with_custom_mask(self): | ||
| pass | ||
|
|
||
| def test_gqa_prefill_with_custom_mask(self): | ||
| pass | ||
|
|
||
| def test_gqa_decode_with_custom_mask(self): | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6054f6b to
f5b4347
Compare
3bd665d to
4d18b12
Compare
4d18b12 to
335b350
Compare
43d8b3c to
708d66c
Compare
115b2da to
f79dd69
Compare
f79dd69 to
a463733
Compare
No description provided.