Skip to content

Fix LLONG_MIN errors in torch.jit.script #153793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

xuantengh
Copy link
Contributor

Convert ast.UnaryOp to a ast.Constant node if the operand is a numeric ast.Constant.

Fixes #153722

Copy link

pytorch-bot bot commented May 18, 2025

🔗 Helpful Links

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

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

❗ 1 Merge Blocking SEVs

There is 1 active merge blocking SEVs. Please view them below:

If you must merge, use @pytorchbot merge -f.

❌ 1 New Failure, 5 Cancelled Jobs, 15 Unrelated Failures

As of commit 1bf8c10 with merge base 1075bb3 (image):

NEW FAILURE - The following job has failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@xuantengh
Copy link
Contributor Author

@pytorchbot label "topic: not user facing"

@HDCharles HDCharles requested a review from huydhn May 20, 2025 17:50
@HDCharles HDCharles added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 20, 2025
@BartlomiejStemborowski
Copy link
Contributor

@ezyang @malfet
Could you help get this PR reviewed?

@ezyang ezyang self-requested a review June 6, 2025 01:47
@ezyang
Copy link
Contributor

ezyang commented Jun 6, 2025

I am a little reluctant to merge this because TS is in maintenance mode, and I cannot tell that this is obviously safe. Here is what O3 thinks, and I think the added suggested tests are enough due diligence:


Below is a “safety audit” style review of the UnaryOpCombiner pass and the new test. I’ve focused on semantic-level corner cases rather than style/nit comments.


1. What the patch actually changes

  • Before: -9223372036854775808 is parsed as

    UnaryOp(op=USub, operand=Constant(value=9223372036854775808)).

    When TorchScript later tried to down-cast that positive literal to int64, it overflowed.

  • After: The new UnaryOpCombiner folds the unary operator into the literal early, producing a single Constant(value=-9223372036854775808).

    That value now fits in signed 64 bits, so the cast succeeds and the new test passes.


2. Edge-case behaviours worth checking

Family of cases Why it matters Expected outcome after patch
Nested unary ops (--1, ~~~0) Visit order & idempotency Should fully fold, e.g. --1 → 1
Positive no-op (+1, +True) UAdd path must exist; idempotent Stays same value
not on constants (not 0, not True) Changes type (int → bool) Produces literal True/False
bool vs int64 (-True, ~True) bool is a subclass of int; JIT tensor type inference may differ Should still infer int64, but verify
Float −0.0 (-0.0) Sign-bit preservation Constant(value=-0.0) (print may show -0.0)
Complex literals (-(1+2j)) Operator in UNARY_OPS supports complex Should fold to (-1-2j)
Values outside 64-bit (-(2**100)) We now create negative huge int; casting still fails → consistent error? Should raise same overflow error as positive huge int
Future Python AST nodes New unary op class ⇒ KeyError Guard with if type(node.op) not in UNARY_OPS


5. Overall risk assessment

  • 📈 Value: Fixes a real int64 down-cast overflow with minimal code.

  • ⚠️ Main risk: Silent type changes from not or ~ on constants.  These are legal Python semantics, but TorchScript users sometimes rely on bool→int autoconversion.

  • 🛡 Mitigation:

    • Add the corner-case test matrix above.

    • Guard the UNARY_OPS lookup.

    • Document in UnaryOpCombiner’s docstring that it pre-evaluates unary ops on literals and may change the AST shape accordingly.


With those guards in place, the pass looks safe to merge.

Copy link
Contributor

github-actions bot commented Aug 5, 2025

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open source Stale topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JIT Script doesn't handle LLONG_MIN correctly
5 participants