[PR #226] [MERGED] fix: prevent int32 overflow in k-grouped GEMM size calculations #298

Closed
opened 2026-10-26 21:15:58 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/deepseek-ai/DeepGEMM/pull/226
Author: @SolenoidWGT
Created: 11/4/2025
Status: ✅ Merged
Merged: 11/19/2025
Merged by: @LyricZhao

Base: main ← Head: fix/fix_sum_k_overflow


📝 Commits (1)

  • 62a3122 fix: prevent int32 overflow in k-grouped GEMM size calculations

📊 Changes

1 file changed (+6 additions, -6 deletions)

View changed files

📝 csrc/apis/gemm.hpp (+6 -6)

📄 Description

Fix Int32 Overflow: Prevent overflow in k-grouped GEMM size validation

Description

Fix integer overflow in k-grouped GEMM when validating tensor sizes with large dimensions.

While training the Deepseek-v3 model using DeepGemm on an H100 machine, we encountered an error in the group GEMM kernel when using long sequences (>256\text{k}). Upon investigation, we found the cause was an overflow during the calculation of the product of sum_k and hidden_size.

Root cause: When m or n are large and multiplied with sum_k, the result exceeds int32 max value (2,147,483,647), causing incorrect validation or undefined behavior.

Solution: Cast m and n to uint64_t before multiplication to safely handle large matrix dimensions.

Changes

csrc/apis/gemm.hpp:289-290: Cast to uint64_t in size assertions

// Before
DG_HOST_ASSERT(sum_mk == m * sum_k);
DG_HOST_ASSERT(sum_nk == n * sum_k);

// After
DG_HOST_ASSERT(sum_mk == static_cast<uint64_t>(m) * sum_k);
DG_HOST_ASSERT(sum_nk == static_cast<uint64_t>(n) * sum_k);

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/deepseek-ai/DeepGEMM/pull/226 **Author:** [@SolenoidWGT](https://github.com/SolenoidWGT) **Created:** 11/4/2025 **Status:** ✅ Merged **Merged:** 11/19/2025 **Merged by:** [@LyricZhao](https://github.com/LyricZhao) **Base:** `main` ← **Head:** `fix/fix_sum_k_overflow` --- ### 📝 Commits (1) - [`62a3122`](https://github.com/deepseek-ai/DeepGEMM/commit/62a312233b28e7cce4ddc762d7bdd9f8475d3b46) fix: prevent int32 overflow in k-grouped GEMM size calculations ### 📊 Changes **1 file changed** (+6 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `csrc/apis/gemm.hpp` (+6 -6) </details> ### 📄 Description ## Fix Int32 Overflow: Prevent overflow in k-grouped GEMM size validation ### Description Fix integer overflow in k-grouped GEMM when validating tensor sizes with large dimensions. While training the Deepseek-v3 model using DeepGemm on an H100 machine, we encountered an error in the group GEMM kernel when using long sequences ($>256\text{k}$). Upon investigation, we found the cause was an overflow during the calculation of the product of sum_k and hidden_size. **Root cause**: When `m` or `n` are large and multiplied with `sum_k`, the result exceeds int32 max value (2,147,483,647), causing incorrect validation or undefined behavior. **Solution**: Cast `m` and `n` to `uint64_t` before multiplication to safely handle large matrix dimensions. ### Changes `csrc/apis/gemm.hpp:289-290`: Cast to `uint64_t` in size assertions ```cpp // Before DG_HOST_ASSERT(sum_mk == m * sum_k); DG_HOST_ASSERT(sum_nk == n * sum_k); // After DG_HOST_ASSERT(sum_mk == static_cast<uint64_t>(m) * sum_k); DG_HOST_ASSERT(sum_nk == static_cast<uint64_t>(n) * sum_k); ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 21:15:58 +00:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepGEMM#298
No description provided.