Skip to content

Add support for multi-query and grouped-query attention - #338

Merged
ksivaman merged 12 commits into
NVIDIA:mainfrom
cyanguwa:dpa/mqa_gqa
Jul 29, 2023
Merged

Add support for multi-query and grouped-query attention#338
ksivaman merged 12 commits into
NVIDIA:mainfrom
cyanguwa:dpa/mqa_gqa

Conversation

@cyanguwa

Copy link
Copy Markdown
Collaborator

This PR adds support for multi-query attention (MQA) and grouped-query attention (GQA). Specifically, it enables the use of MQA and GQA in FlashAttention() and UnfusedDotProductAttention. MQA and MHA are special cases of GQA, i.e. when num_gqa_groups=1 and num_heads. GQA only applies to keys and values, not queries.

Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci

Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci

Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Comment threadtests/pytorch/test_fused_attn.py Outdated
Comment threadtransformer_engine/pytorch/attention.py Outdated
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
@ksivaman

Copy link
Copy Markdown
Member

/te-ci

Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci

@blahBlahhhJ

Copy link
Copy Markdown
Contributor

Hi, overall LGTM with only one small thing. I see @ksivaman mentioned that this PR uses a PR in MegatronLM as a reference. The main difference I see is that in TE you use a Q projection and a KV projection while in megatron it's packed to be a single QKV projection matrix. Is there any particular reasons why you separate them? I think it would be better if the implementations are consistent so that people don't need to convert checkpoints back and forth. especially because I see megatron core uses some TE layers internally.

@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

Hi @blahBlahhhJ ,

With MQA and GQA, the kv projection size might be different from the query projection size. That itself isn't a problem for the projection gemm, but when we do ColumnParallel and the split of parameters (into q, k, v, in TE), the q_projection_size + 2 * kv_projection_size might not divide well by 3. To be safe and to make the TP balancing easier (I'm sure we can parallelize it too with a non-balanced TP situation, if we really want to do it), I coded the projection as Q and KV rather than QKV.

@ksivamanksivaman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@ksivaman
ksivaman merged commit 1cb4b25 into NVIDIA:mainJul 29, 2023
ksivaman added a commit that referenced this pull request Jul 31, 2023
* add support for multi-query/grouped-query attention
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix lint
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* revert to flash-attn 1.0.6 and build 2.0.0.post1 manually in CI
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* add keyword name for DPA input
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix fused attn tests
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix skipif for pytest
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* Update transformer_engine/pytorch/attention.py
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
* Update tests/pytorch/test_fused_attn.py
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
* Fix TP and SP case
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
* add skipifs for pytest
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* remove higher limit for flash-attn version
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
---------
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
Co-authored-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
@cyanguwa
cyanguwa deleted the dpa/mqa_gqa branch February 21, 2024 23:59
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@cyanguwa@ksivaman@blahBlahhhJ@ptrendx