Repository navigation
Conversation
[Medium impact] The changes since the previous review appear safe to merge; no new blocking issue was found. SummaryThe PR compares backend choices with native and padded head widths, then pads Q/K/V only when padding improves the choice. It trims attention output back to the original value width.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Native Q/K/V widths] --> B[Compare native and padded backend choices]
B --> C{Does padding improve the choice?}
C -->|Yes| D[Pad Q/K/V]
C -->|No| E[Keep native widths]
D --> F[Run selected attention backend]
E --> F
F --> G[Trim output if padded]
Reviews (12) · Last reviewed commit: "Improve cache clear usability" · Reviewed by Greptile |
8d821bf to
bdc487e
Compare
|
PTAL. |
|
Hey, friendly ping. |
| return None | ||
|
|
||
| if qkv_head_pad: | ||
| attn_out = _trim_output(attn_out, num_attention_heads, head_dim_qk, orig_head_dim_v) |
There was a problem hiding this comment.
Would _trim_output(attn_out, num_attention_heads, head_dim_v, orig_head_dim_v) read a bit better, even though head_dim_qk = head_dim_v at the point?
There was a problem hiding this comment.
You might also need to be careful when attn_out is a Tuple for example with return_max_logit=True. There might be some logic needed to hitting that path. Thanks.
There was a problem hiding this comment.
For the former, I actually deliberately used qk to highlight that fact, but I'm fine with changing it to head_dim_v.
For the latter; thanks a lot! I didn't catch that because I was only looking at the type hints, which appear to be outdated. I'll update it as well if you don't mind. :)
There was a problem hiding this comment.
Would you like tests to be added for the return_max_logit=True case as well?
There was a problem hiding this comment.
I think we can leave the type hints alone in backends.py. Currently TE doesn't have any mypy, pyright, or pytype checks anywhere - maybe something we should do in the future, and there's a lot more places to change when we do. :)
I think it's ok without the specific tests for return_max_logit=True. That's just "one of" the peripheral features that could or could not be affected when combined with the padding/backend reselection. If you like, you can add this flag to one of the mla tests you added (just by adding a kwarg to one of the configs in model_configs_mla).
While support for native QKV shapes has been added, some MLA kernels still benefit from having QKV padded to the same head dimension. Since it's not easy to automate this decision, we probe `get_attention_backend` twice with and without padding to figure out which version selects the better kernel. If padding results in a better kernel than not padding, we pad; otherwise, Q, K, and V stay native. Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
It could be that all `native_use_*_attention` values are False, so this needs to be handled. Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Improve variable names along the way. Signed-off-by: janEbert <janpabloe@nvidia.com>
ebcc35d to
4dcde58
Compare
|
I'm sorry for the late reply; doesn't make it easier to keep this in your head, for sure. |
Signed-off-by: janEbert <janpabloe@nvidia.com>
| quantizers=None, | ||
| fp8_output: bool = False, | ||
| ) -> torch.Tensor: | ||
| ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: |
There was a problem hiding this comment.
How about this to prevent it from going stale - we may have more output tensors as more features are getting added.
Union[torch.Tensor, Tuple[torch.Tensor, ...]]
There was a problem hiding this comment.
I would argue it's good if it can go stale – in theory, a type checker should complain for all locations where a new n-tuple isn't correctly handled.
WDYT? Still change?
There was a problem hiding this comment.
See comment above. I think we can remove the changes you made, for now.
There was a problem hiding this comment.
I reverted the varlength tuple change, if that is what you meant. If it's not, then I'd still argue to keep the type hint updates in some form (even if in a new PR). Even if it's not used by any static analysis tooling, it's extremely helpful just for human code reading.
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
|
I've also been fighting with the caching. It's extremely difficult due to the various interactions. Since you seemed to have started working on it, would you like to take it over? The problem I've found is that, on the one hand, the |
Also add batch size config. Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
This reverts commit 4029149. Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
|
@janEbert can you take a look at my recent changes and see if they follow your original logic? Thanks. |
|
/te-ci pytorch L0 L1 L3 |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
|
I believe there is one case in which padding is activated even though it shouldn't be, but it may never be possible in the current state: With native FlashAttention available and selected, padded FlashAttention not selected, but padded FusedAttention selected. In that case, we do pad, even though native FlashAttention would already be the best choice. |
|
Too bad, doesn't work. Guess I'd have to be a member of a team for this repo. :) |
| if _attention_backends["backend_selection_requires_update"]: | ||
| dpa_utils.clear_should_pad_qkv_head_dim_cache() |
There was a problem hiding this comment.
I added another commit to allow passing an invalidate_cache boolean as an optional argument to should_pad_qkv_head_dim (in this case invalidate_cache=_attention_backends["backend_selection_requires_update"]). Then we can call the cache clear inside the function, which may be better in terms of user-facing visibility and API. Adjusted the test accordingly.
If you don't like the change, feel free to revert it.
Signed-off-by: janEbert <janpabloe@nvidia.com>
Description
While support for native QKV shapes has been added, some MLA kernels
still benefit from having QKV padded to the same head dimension. Since
it's not easy to automate this decision, we probe
get_attention_backendtwice with and without padding to figure outwhich version selects the better kernel.
If padding results in a better kernel than not padding, we pad;
otherwise, Q, K, and V stay native.
Ref NVIDIA/Megatron-LM#6240, ref NVIDIA/Megatron-LM#6241.
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: