Skip to content

Fix Illegal Instruction/IMA errors when using DP attention -- num_tokens_for_logprob calculation#12115

Merged
ch-wan merged 2 commits intosgl-project:mainfrom
YAMY1234:gpqa_fix_root
Oct 25, 2025
Merged

Fix Illegal Instruction/IMA errors when using DP attention -- num_tokens_for_logprob calculation#12115
ch-wan merged 2 commits intosgl-project:mainfrom
YAMY1234:gpqa_fix_root

Conversation

@YAMY1234
Copy link
Copy Markdown
Contributor

@YAMY1234 YAMY1234 commented Oct 25, 2025

Motivation

Finding the root fix for #12052

Issue: When running with --enable-dp-attention, the DP gather operation in logits stage uses incorrect metadata, causing Triton kernel to access out-of-bounds memory.

AssertionError: src OOB: sz=661, chunk_size=7168, required=4738048, src.numel=21504

Root cause: The scheduler calculates num_tokens_for_logprob incorrectly by always assuming all tokens need logits computation, but when return_logprob=False, LogitsProcessor only constructs pruned_states with the last token per request (batch_size tokens). This mismatch causes DP gather to expect 661 tokens but only receive 3 tokens, leading to out-of-bounds memory access.

Modifications

Key debug log before crash:


================================================================================
ERROR at DP1 TP1
Exception: src OOB: sz=661, chunk_size=7168, required=4738048, src.numel=21504
================================================================================
hidden_states.shape: (1111, 7168)
local_hidden_states.shape: (3, 7168)
hidden_states.dtype: torch.bfloat16
local_hidden_states.dtype: torch.bfloat16
hidden_states.device: cuda:1
local_hidden_states.device: cuda:1

--- LogitsMetadata ---
global_dp_buffer_len: 1111
dp_local_start_pos: 67
dp_local_num_tokens: 661
global_num_tokens_gpu: [67, 899, 54, 63, 67, 80, 61, 58]
global_num_tokens_for_logprob_gpu: [67, 661, 54, 63, 67, 80, 61, 58]
global_num_tokens_for_logprob_cpu: [67, 661, 54, 63, 67, 80, 61, 58]

--- Batch Info ---
extend_seq_lens_cpu: [253, 291, 355]
extend_logprob_start_lens_cpu: [62, 58, 118]
extend_logprob_pruned_lens_cpu: False
extend_return_logprob: False

--- Calculated Values ---
local_hidden_states.shape[0]: 3
max(extend_len - start_len, 1) per req: [191, 233, 237]
sum of above: 661
================================================================================

File: sglang/python/sglang/srt/managers/scheduler.py

In prepare_mlp_sync_batch_raw(), distinguish two cases when calculating num_tokens_for_logprob for extend mode:
When return_logprob=True: Keep original logic - sum of max(extend_len - logprob_start_len, 1) across all requests (needs logits for input logprob computation)

When return_logprob=False: Use batch_size() - only need last token per request for sampling
This ensures the token count synchronized across DP ranks matches the actual pruned_states size in LogitsProcessor, preventing the memory access violation.

Accuracy Tests

Repeat: 8, mean: 0.795
Scores: ['0.803', '0.808', '0.768', '0.828', '0.823', '0.773', '0.813', '0.742']

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @YAMY1234, 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 resolves a critical out-of-bounds memory access error that manifested as an AssertionError: src OOB when utilizing distributed attention (--enable-dp-attention). The root cause was an inconsistency in how the scheduler calculated the number of tokens requiring logit computation versus the actual tokens processed by LogitsProcessor, particularly when return_logprob was set to False. The implemented fix ensures that the token count synchronized across distributed ranks accurately reflects the actual data size, thereby preventing memory access violations and enhancing the stability and correctness of the system.

Highlights

  • DP Attention Error Fix: Addresses an "AssertionError: src OOB" occurring when using --enable-dp-attention due to an incorrect num_tokens_for_logprob calculation in the scheduler, which led to Triton kernel accessing out-of-bounds memory.
  • Conditional Logit Token Calculation: Modifies the prepare_mlp_sync_batch_raw function to conditionally calculate num_tokens_for_logprob. If return_logprob is False, it now correctly uses local_batch.batch_size() to account for only the last token per request, aligning with LogitsProcessor behavior and preventing memory access violations.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 effectively addresses a critical out-of-bounds memory access error that occurs when using DP attention without returning logprobs. The root cause—an incorrect calculation of num_tokens_for_logprob—is well-understood, and the proposed fix correctly adjusts the logic based on the return_logprob flag. The change is clear, targeted, and should resolve the reported issue. I've included one minor suggestion to improve code style and memory efficiency.

@ch-wan ch-wan added the run-ci label Oct 25, 2025
Copy link
Copy Markdown
Collaborator

@Fridge003 Fridge003 left a comment

Choose a reason for hiding this comment

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

wonderful job!

@ch-wan ch-wan merged commit c849297 into sgl-project:main Oct 25, 2025
102 of 107 checks passed
@whybeyoung
Copy link
Copy Markdown
Collaborator

wonderful job!

Sign up for free to 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