Skip to content

Conversation

@botbigeyes
Copy link
Contributor

PR Category
Operator

Type of Change
Bug Fix

Description
The overflow check logic previously incorrectly considered special floating-point values (inf, -inf, nan), causing inconsistencies with PyTorch behavior. The logic has been fixed to first exclude special values before checking for overflow.

Issue

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @botbigeyes, 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 addresses a bug in the overflow checking mechanism, specifically for floating-point values. The previous implementation incorrectly flagged special floating-point values (infinity, negative infinity, and NaN) as overflows. The updated logic now correctly identifies and bypasses these special values before performing overflow checks, ensuring consistency with PyTorch's behavior. Additionally, a minor whitespace adjustment was made in an error message within the stack function.

Highlights

  • Improved Floating-Point Overflow Checks: The overflow detection logic for floating-point types now correctly handles special values such as "inf", "-inf", and "nan" by explicitly excluding them from the overflow comparison, aligning behavior with PyTorch.
  • Minor Formatting Adjustment: A small formatting change was applied to an f-string in the stack function's error message for improved readability and consistency.
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 aims to fix an incorrect overflow check for floating-point values by ignoring special values like inf and nan. While the intent is correct, the implementation introduces a critical regression where overflow checks are skipped for integer values being converted to float types. I've provided a comment with a suggested fix for this issue. The other changes in the PR are minor stylistic improvements.

Comment on lines 28 to 39
is_float = isinstance(fill_value, float)

if is_float and not (math.isinf(fill_value) or math.isnan(fill_value)):
if (
fill_value < -torch.finfo(dtype).max
or fill_value > torch.finfo(dtype).max
):
raise RuntimeError(
f"value cannot be converted to type {dtype} without overflow"
)

fill_value = torch.tensor(fill_value, dtype=dtype, device=device)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The current logic for checking float overflow has a regression. By checking isinstance(fill_value, float), you are skipping the overflow check when fill_value is an integer. An integer can be large enough to overflow a floating-point type (e.g., fill_value=10**40 for dtype=torch.float32), which this new logic fails to catch. The previous implementation handled this correctly.

The check should only be skipped for special floating-point values like inf and nan. For all other numeric types, including integers, the overflow check should be performed.

Here is a suggested fix that correctly handles special float values while still checking for overflow from other numeric types. It also uses torch.finfo(dtype).min which is slightly more direct than -torch.finfo(dtype).max.

Suggested change
is_float = isinstance(fill_value, float)
if is_float and not (math.isinf(fill_value) or math.isnan(fill_value)):
if (
fill_value < -torch.finfo(dtype).max
or fill_value > torch.finfo(dtype).max
):
raise RuntimeError(
f"value cannot be converted to type {dtype} without overflow"
)
fill_value = torch.tensor(fill_value, dtype=dtype, device=device)
is_special_float = isinstance(fill_value, float) and (math.isinf(fill_value) or math.isnan(fill_value))
if not is_special_float:
finfo = torch.finfo(dtype)
if fill_value > finfo.max or fill_value < finfo.min:
raise RuntimeError(
f"value cannot be converted to type {dtype} without overflow"
)
fill_value = torch.tensor(fill_value, dtype=dtype, device=device)

Comment on lines 30 to 34
if is_float and not (math.isinf(fill_value) or math.isnan(fill_value)):
if (
fill_value < -torch.finfo(dtype).max
or fill_value > torch.finfo(dtype).max
):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we merge these two if statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the structure can be merged and simplified.

@0x45f
Copy link
Collaborator

0x45f commented Nov 17, 2025

plz merge master

@botbigeyes
Copy link
Contributor Author

plz merge master

Ok, I have already merged the changes from the master branch into the full_like branch

raise RuntimeError(
f"value cannot be converted to type {dtype} without overflow"
)
if dtype == torch.float64:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why del this if?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The del this if can unify the float conversion. As long as it's a float type, fill_value will be converted to the corresponding torch.tensor after passing the overflow check,I don't understand why it's necessary to handle float64 conversion separately.

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.

3 participants