Skip to content

[DeepSeek] Integrate M*G Group Gemm - #1046

Merged
kwen2501 merged 4 commits into
mainfrom
integrate_gemm
Apr 3, 2025
Merged

kwen2501 merged 4 commits into
mainfrom
integrate_gemm

Conversation

@kwen2501

@kwen2501 kwen2501 commented Apr 3, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Integrating the M*G Group GEMM kernel by @lessw2020 in #967. This kernel requires tokens for the same expert to reside together. So we need to permute the received tokens before the GEMM.

We also need to make sure each token group's length (i.e. m_sizes) meet the alignment requirement of the Group GEMM. This requires padding some unused index (like -1) into the permutation indices.

With this integration, we remove a few CPU sync points in the original program, for example, getting the received length, and slicing the chunk of tokens for each expert.

And most obviously, we removed the for loop needed to perform matmul for all local experts.

TODO

The generation of permutation indices are in CPU right now. We should write a kernel for it.

Test

$ torchrun --standalone --nproc-per-node 4 generate.py
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
......

Output: You are a helpful assistant.
User: What is 2+2?

Assistant: 2 + 2 equals 4.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 3, 2025
@kwen2501
kwen2501 requested review from lessw2020 and wconstab April 3, 2025 02:22

@lessw2020 lessw2020 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good!

@kwen2501
kwen2501 changed the base branch from kernel_fix to main April 3, 2025 17:09
@kwen2501
kwen2501 merged commit 2157f53 into main Apr 3, 2025
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
### Summary
Integrating the M*G Group GEMM kernel by @lessw2020 in pytorch#967. This kernel
requires tokens for the same expert to reside together. So we need to
permute the received tokens before the GEMM.

We also need to make sure each token group's length (i.e. `m_sizes`)
meet the alignment requirement of the Group GEMM. This requires padding
some unused index (like `-1`) into the permutation indices.

With this integration, we remove a few CPU sync points in the original
program, for example, getting the `received` length, and slicing the
chunk of tokens for each expert.

And most obviously, we removed the for loop needed to perform matmul for
all local experts.

### TODO
The generation of permutation indices are in CPU right now. We should
write a kernel for it.

### Test
```
$ torchrun --standalone --nproc-per-node 4 generate.py
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
......

Output: You are a helpful assistant.
User: What is 2+2?

Assistant: 2 + 2 equals 4.
```
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
### Summary
Integrating the M*G Group GEMM kernel by @lessw2020 in pytorch#967. This kernel
requires tokens for the same expert to reside together. So we need to
permute the received tokens before the GEMM.

We also need to make sure each token group's length (i.e. `m_sizes`)
meet the alignment requirement of the Group GEMM. This requires padding
some unused index (like `-1`) into the permutation indices.

With this integration, we remove a few CPU sync points in the original
program, for example, getting the `received` length, and slicing the
chunk of tokens for each expert.

And most obviously, we removed the for loop needed to perform matmul for
all local experts.

### TODO
The generation of permutation indices are in CPU right now. We should
write a kernel for it.

### Test
```
$ torchrun --standalone --nproc-per-node 4 generate.py
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
Combining expert weights for Group GEMM
......

Output: You are a helpful assistant.
User: What is 2+2?

Assistant: 2 + 2 equals 4.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants