Skip to content

[AMD] Fix ZeroDivisionError in compute_normalized_prompt_logprobs#14721

Closed
sunxxuns wants to merge 1 commit intosgl-project:mainfrom
sunxxuns:fix-amd-select-logprobs-clean
Closed

[AMD] Fix ZeroDivisionError in compute_normalized_prompt_logprobs#14721
sunxxuns wants to merge 1 commit intosgl-project:mainfrom
sunxxuns:fix-amd-select-logprobs-clean

Conversation

@sunxxuns
Copy link
Collaborator

@sunxxuns sunxxuns commented Dec 9, 2025

No description provided.

On AMD/ROCm, input_token_logprobs can be empty in some cases,
causing ZeroDivisionError when computing normalized prompt logprobs
for the select operation.

Changes:
1. Return -inf when no valid logprobs (choice should not be selected)
2. Skip token healing adjustment if no logprobs available
3. Guard against division by zero when num_tokens <= 1

This fixes test_hellaswag_select and test_select failures on AMD.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @sunxxuns, 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 ZeroDivisionError that could arise in the sglang backend during logprobability calculations and token healing. The changes enhance the stability and reliability of the system by introducing explicit checks for missing logprobabilities and insufficient token counts, thereby preventing runtime crashes and ensuring more robust prompt processing.

Highlights

  • ZeroDivisionError Prevention: Implemented robust checks to prevent ZeroDivisionError in the compute_normalized_prompt_logprobs function and within the token healing logic.
  • Logprob Handling Enhancement: The system now returns float("-inf") when no valid logprobabilities are available for normalization, ensuring graceful handling of edge cases.
  • Token Healing Logic Refinement: Improved token healing to skip processing if logprobabilities are missing and to safeguard against division by zero when only a single token is present.
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
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 successfully addresses a ZeroDivisionError in compute_normalized_prompt_logprobs and select. The changes are generally good, but there's a logic issue in the select method's fix. When only one token is present for token healing, the code avoids the crash but leaves normalized_prompt_logprobs in an inconsistent state with a stale value. I've provided a suggestion to set the logprob to negative infinity in this case, which aligns with how empty logprobs are handled elsewhere in the code.

Comment on lines +291 to +294
if num_tokens > 1:
normalized_prompt_logprobs[i] = (
normalized_prompt_logprobs[i] * num_tokens - healed_token_logprob
) / (num_tokens - 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While this change correctly prevents a ZeroDivisionError when num_tokens is 1, it leaves normalized_prompt_logprobs[i] with a stale value. After the healed token is removed on line 295, input_token_logprobs[i] becomes empty, but the corresponding normalized_prompt_logprobs[i] is not updated, leading to an inconsistent state. To align with the logic in compute_normalized_prompt_logprobs which returns -inf for empty logprobs, it would be more correct to set normalized_prompt_logprobs[i] to -inf when num_tokens is 1.

Suggested change
if num_tokens > 1:
normalized_prompt_logprobs[i] = (
normalized_prompt_logprobs[i] * num_tokens - healed_token_logprob
) / (num_tokens - 1)
if num_tokens > 1:
normalized_prompt_logprobs[i] = (
normalized_prompt_logprobs[i] * num_tokens - healed_token_logprob
) / (num_tokens - 1)
else:
normalized_prompt_logprobs[i] = float("-inf")

@sunxxuns sunxxuns closed this Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant