Skip to content

[BugFix] Sample the inclusive high bound in integer Bounded.rand - #4497

Merged
theap06 merged 1 commit into
pytorch:mainfrom
jayzuccarelli:fix-bounded-int-rand
Sep 29, 2026
Merged

theap06 merged 1 commit into
pytorch:mainfrom
jayzuccarelli:fix-bounded-int-rand

Conversation

@jayzuccarelli

@jayzuccarelli jayzuccarelli commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Bounded treats both bounds as inclusive (is_in and project accept high), but the integer branch of rand scaled a [0, 1) draw by high - low and cast it to the target dtype. The cast truncates toward zero, so high was never drawn, Bounded(0, 1, dtype=torch.int64).rand() always returned 0, and ranges that straddle zero never produced low and drew 0 twice as often as other values.

Draw floor(u * (high - low + 1)) + low instead, clamped to high.

Description

Describe your changes in detail.

Motivation and Context

Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax close #15213 if this solves the issue #15213

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

AI assistance (Claude) was used to find and fix this; I reviewed the change and ran the tests.

Bounded treats both bounds as inclusive (is_in and project accept high), but
the integer branch of rand scaled a [0, 1) draw by high - low and cast it to
the target dtype. The cast truncates toward zero, so high was never drawn,
Bounded(0, 1, dtype=torch.int64).rand() always returned 0, and ranges that
straddle zero never produced low and drew 0 twice as often as other values.

Draw floor(u * (high - low + 1)) + low instead, clamped to high.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pytorch-bot

pytorch-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4497

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Cancelled Job, 2 Unrelated Failures

As of commit dfbeae8 with merge base 71a4143 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Sep 29, 2026

Copy link
Copy Markdown

Hi @jayzuccarelli!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 29, 2026
@meta-cla

meta-cla Bot commented Sep 29, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@theap06 theap06 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for contributing and catching this! LGTM.

@theap06
theap06 merged commit bf2ba80 into pytorch:main Sep 29, 2026
150 of 155 checks passed
bsprenger pushed a commit that referenced this pull request Oct 9, 2026
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants