Skip to content

[dynamic shapes] Don't truncate float expressions in guarding_hint_or_throw - #202324

Closed
laithsakka wants to merge 5 commits into
gh/laithsakka/508/basefrom
gh/laithsakka/508/head
Closed

laithsakka wants to merge 5 commits into
gh/laithsakka/508/basefrom
gh/laithsakka/508/head

Conversation

@laithsakka

@laithsakka laithsakka commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Fixes #201817

ShapeEnv.evaluate_expr on a float-valued expression without an explicit
hint returned the value truncated to int and installed an unsound guard,
e.g. for FloatTrueDiv(1.0, sqrt(ToFloat(s0))) with s0=64 it returned 0 and
guarded Eq(..., 0), which fails on the very inputs it was created from.

Since #177570, compute_concrete_val uses guarding_hint_or_throw, which goes
through _maybe_realize_expr and does int(expr), truncating sympy.Float.

_guarding_hint_or_throw_base now returns a Python float for sympy.Float.
_maybe_realize_expr (shared with the int-only optimization_hint paths) is
unchanged.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: inductor release notes: fx release notes category labels Oct 28, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

The PR makes `_guarding_hint_or_throw_base` return a Python float for
`sympy.Float` results, so `ShapeEnv.evaluate_expr` no longer truncates float
expressions to int and installs a wrong guard. It also adds `SymFloat`/`float`
support to `guarding_hint_or_throw`. Recommendation: Approve. Both earlier
blocking findings are resolved and only non-blocking points remain.

Findings (4):

[minor] torch/fx/experimental/_size_hinting.py:118
Non-blocking: infinite results are still truncated, even though the new
docstring says "Float expressions return a Python float (never truncated to
int)". The checks at :157 and :181 only catch `sympy.Float`. A float expression
that evaluates to infinity becomes `sympy.oo`, which is not a `Float`. This
happens with `ToFloat(int_oo)` or when `FloatTrueDiv.eval` overflows.
`_maybe_realize_expr` then returns the int `sys.maxsize`, so
`compute_concrete_val` can still install `Eq(expr, 9223372036854775807)`, the
same kind of unsound guard this PR fixes. NaN returns None and raises
`RuntimeError("unexpected None!")`. This predates the PR and is rare. Either map
`oo`/`-oo`/`nan` to `float('inf')`/`float('-inf')`/`float('nan')` next to the
`Float` check, or narrow the docstring. (The reviewer couldn't run sympy to
confirm the `Float(inf)  oo` step.)

[minor] torch/fx/experimental/_size_hinting.py:117
Non-blocking: stale docstrings and comments still say "integer". The `Returns:`
sections at `_size_hinting.py:131-132` and
`torch/_inductor/sizevars.py:1133-1134` still say "The concrete integer value of
the expression", which contradicts the summary lines this PR updated just above
them. The comment at `symbolic_shapes.py:8833-8836` in `compute_concrete_val`
says `guarding_hint_or_throw` returns bool/int, but this PR exists so that call
site gets floats. The module docstring at `_size_hinting.py:4-5` ("concrete
integer hints") is also stale.

[minor] torch/_inductor/lowering.py:1518
Non-blocking, carried over: the `int()` casts on
`SizeVarAllocator.guarding_hint_or_throw` results are applied unevenly. The PR
adds them in `expand` (`lowering.py:1501-1503`, `:1518`) and at
`cutlass/gemm_template.py:1663`. They are missing from the identical
`mark_reuse(guarding_hint_or_throw(...) // old_size_product)` pattern in
`repeat` (`lowering.py:1584-1593`) and from `unfold` (`:2818-2820`). There is no
runtime effect, since Inductor sizes are integers. Either apply the casts
consistently or narrow once inside `SizeVarAllocator`.

[minor] test/test_dynamic_shapes.py:655
Non-blocking, carried over: in `test_guarding_hint_or_throw_float`, the
`hint=None` case tests the same thing as `hint=0.125`. With `hint=None`,
`SymNode.__init__` computes the hint eagerly (`compute_hint`), so `_hint` is
already 0.125. Both cases return early at `symbolic_shapes.py:159-160`, and
neither reaches the `a.shape_env.guarding_hint_or_throw(a.expr)` / `a._hint =
hint` branch at `:163-165`. Building the node with `hint=_NO_HINT` would cover
that branch. Also, `assertEqual` goes through `RelaxedNumberPair`, which ignores
int/float type differences. Adding `assertIsInstance(..., float)` would lock in
the documented "returns a Python float" behaviour for whole-number values like
8.0.

Reviewed commit: 0b558b8 (NOT the current head 71c947a)

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 71c947a with merge base 0a133b7 (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR was reverted - re-landing requires human review:

Green Light will not review this PR again, on this or any later commit; re-landing it needs a human approval.

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

Comment thread torch/fx/experimental/_size_hinting.py Resolved
@laithsakka
laithsakka requested a review from drisspg October 28, 2026 01:06
@laithsakka
laithsakka marked this pull request as draft October 28, 2026 01:06
@laithsakka

Copy link
Copy Markdown
Contributor Author

waiting for CI

@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Oct 28, 2026
@laithsakka
laithsakka marked this pull request as ready for review October 28, 2026 02:14
@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 28, 2026
github-actions[bot]

This comment was marked as outdated.

@laithsakka
laithsakka dismissed github-actions[bot]’s stale review October 28, 2026 07:25

left comment its wrong issue

Comment thread torch/_inductor/lowering.py Resolved

@aorenste aorenste left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(Reviewed by me, assisted by AI)


Merging is self-service: comment @pytorchbot merge and the bot lands it once required checks pass. Merging your Change | Bot commands

Comment thread torch/fx/experimental/_size_hinting.py Resolved
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 9, 2026
…_throw

Fixes #201817

ShapeEnv.evaluate_expr on a float-valued expression without an explicit
hint returned the value truncated to int and installed an unsound guard,
e.g. for FloatTrueDiv(1.0, sqrt(ToFloat(s0))) with s0=64 it returned 0 and
guarded Eq(..., 0), which fails on the very inputs it was created from.

Since #177570, compute_concrete_val uses guarding_hint_or_throw, which goes
through _maybe_realize_expr and does int(expr), truncating sympy.Float.

_guarding_hint_or_throw_base now returns a Python float for sympy.Float.
_maybe_realize_expr (shared with the int-only optimization_hint paths) is
unchanged.

Typing:
- Add overloads to the module-level guarding_hint_or_throw so SymBool/bool
  -> bool, SymInt/int -> int, SymNode -> int | bool | float.
- ShapeEnv and Inductor SizeVarAllocator guarding_hint_or_throw /
  guarding_hints_or_throw now return int | float (| bool for ShapeEnv).
- Int-only Inductor call sites (expand's mark_reuse size products and the
  CUTLASS EVT size_hint_fn callback) convert explicitly with int().

Test: python test/test_dynamic_shapes.py -k test_evaluate_expr_float_without_hint
ghstack-source-id: b6bcdc8
Pull-Request: #202324
@pytorchgreenlight pytorchgreenlight Bot added GL/superseded GreenLight review is superseded by changes and removed GL/human GreenLight marked as needing human review labels Nov 9, 2026
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 9, 2026
…_throw

Fixes #201817

ShapeEnv.evaluate_expr on a float-valued expression without an explicit
hint returned the value truncated to int and installed an unsound guard,
e.g. for FloatTrueDiv(1.0, sqrt(ToFloat(s0))) with s0=64 it returned 0 and
guarded Eq(..., 0), which fails on the very inputs it was created from.

Since #177570, compute_concrete_val uses guarding_hint_or_throw, which goes
through _maybe_realize_expr and does int(expr), truncating sympy.Float.

_guarding_hint_or_throw_base now returns a Python float for sympy.Float.
_maybe_realize_expr (shared with the int-only optimization_hint paths) is
unchanged.

Typing:
- Add overloads to the module-level guarding_hint_or_throw so SymBool/bool
  -> bool, SymInt/int -> int, SymNode -> int | bool | float.
- ShapeEnv and Inductor SizeVarAllocator guarding_hint_or_throw /
  guarding_hints_or_throw now return int | float (| bool for ShapeEnv).
- Int-only Inductor call sites (expand's mark_reuse size products and the
  CUTLASS EVT size_hint_fn callback) convert explicitly with int().

Test: python test/test_dynamic_shapes.py -k test_evaluate_expr_float_without_hint
ghstack-source-id: f5cf443
Pull-Request: #202324

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated review of 0b558b8: nothing blocking; ready for human review.

The PR makes _guarding_hint_or_throw_base return a Python float for sympy.Float results, so ShapeEnv.evaluate_expr no longer truncates float expressions to int and installs a wrong guard. It also adds SymFloat/float support to guarding_hint_or_throw. Recommendation: Approve. Both earlier blocking findings are resolved and only non-blocking points remain.

⚪ 4 non-blocking findings

Float expressions return a Python float (never truncated to int).

Non-blocking: infinite results are still truncated, even though the new docstring says "Float expressions return a Python float (never truncated to int)". The checks at :157 and :181 only catch sympy.Float. A float expression that evaluates to infinity becomes sympy.oo, which is not a Float. This happens with ToFloat(int_oo) or when FloatTrueDiv.eval overflows. _maybe_realize_expr then returns the int sys.maxsize, so compute_concrete_val can still install Eq(expr, 9223372036854775807), the same kind of unsound guard this PR fixes. NaN returns None and raises RuntimeError("unexpected None!"). This predates the PR and is rare. Either map oo/-oo/nan to float('inf')/float('-inf')/float('nan') next to the Float check, or narrow the docstring. (The reviewer couldn't run sympy to confirm the Float(inf) oo step.)


Return a concrete hint for an expression that is safe to use for guarding.

Non-blocking: stale docstrings and comments still say "integer". The Returns: sections at _size_hinting.py:131-132 and torch/_inductor/sizevars.py:1133-1134 still say "The concrete integer value of the expression", which contradicts the summary lines this PR updated just above them. The comment at symbolic_shapes.py:8833-8836 in compute_concrete_val says guarding_hint_or_throw returns bool/int, but this PR exists so that call site gets floats. The module docstring at _size_hinting.py:4-5 ("concrete integer hints") is also stale.


int(V.graph.sizevars.guarding_hint_or_throw(sympy_product(sizes)))

Non-blocking, carried over: the int() casts on SizeVarAllocator.guarding_hint_or_throw results are applied unevenly. The PR adds them in expand (lowering.py:1501-1503, :1518) and at cutlass/gemm_template.py:1663. They are missing from the identical mark_reuse(guarding_hint_or_throw(...) // old_size_product) pattern in repeat (lowering.py:1584-1593) and from unfold (:2818-2820). There is no runtime effect, since Inductor sizes are integers. Either apply the casts consistently or narrow once inside SizeVarAllocator.


@parametrize("hint", (None, 0.125))

Non-blocking, carried over: in test_guarding_hint_or_throw_float, the hint=None case tests the same thing as hint=0.125. With hint=None, SymNode.__init__ computes the hint eagerly (compute_hint), so _hint is already 0.125. Both cases return early at symbolic_shapes.py:159-160, and neither reaches the a.shape_env.guarding_hint_or_throw(a.expr) / a._hint = hint branch at :163-165. Building the node with hint=_NO_HINT would cover that branch. Also, assertEqual goes through RelaxedNumberPair, which ignores int/float type differences. Adding assertIsInstance(..., float) would lock in the documented "returns a Python float" behaviour for whole-number values like 8.0.

An earlier comment near this line has replies: #202324 (comment)

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@github-actions
github-actions Bot dismissed their stale review November 9, 2026 20:27

Withdrawn: the automated review of 0b558b8 found nothing blocking.

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 12, 2026
…_throw

Fixes #201817

ShapeEnv.evaluate_expr on a float-valued expression without an explicit
hint returned the value truncated to int and installed an unsound guard,
e.g. for FloatTrueDiv(1.0, sqrt(ToFloat(s0))) with s0=64 it returned 0 and
guarded Eq(..., 0), which fails on the very inputs it was created from.

Since #177570, compute_concrete_val uses guarding_hint_or_throw, which goes
through _maybe_realize_expr and does int(expr), truncating sympy.Float.

_guarding_hint_or_throw_base now returns a Python float for sympy.Float.
_maybe_realize_expr (shared with the int-only optimization_hint paths) is
unchanged.

Typing:
- Add overloads to the module-level guarding_hint_or_throw so SymBool/bool
  -> bool, SymInt/int -> int, SymNode -> int | bool | float.
- ShapeEnv and Inductor SizeVarAllocator guarding_hint_or_throw /
  guarding_hints_or_throw now return int | float (| bool for ShapeEnv).
- Int-only Inductor call sites (expand's mark_reuse size products and the
  CUTLASS EVT size_hint_fn callback) convert explicitly with int().

Test: python test/test_dynamic_shapes.py -k test_evaluate_expr_float_without_hint
ghstack-source-id: f1f992a
Pull-Request: #202324
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@laithsakka

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 7ecc36a02da201737863e25aeb30c85587723cd8 returned non-zero exit code 1

Auto-merging test/test_dynamic_shapes.py
CONFLICT (content): Merge conflict in test/test_dynamic_shapes.py
Auto-merging test/test_fake_tensor.py
Auto-merging torch/_inductor/lowering.py
Auto-merging torch/_subclasses/fake_tensor.py
Auto-merging torch/fx/experimental/symbolic_shapes.py
error: could not apply 7ecc36a02da... [dynamic shapes] Don't truncate float expressions in guarding_hint_or_throw
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Details for Dev Infra team

Raised by workflow job

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 13, 2026
…_throw

Fixes #201817

ShapeEnv.evaluate_expr on a float-valued expression without an explicit
hint returned the value truncated to int and installed an unsound guard,
e.g. for FloatTrueDiv(1.0, sqrt(ToFloat(s0))) with s0=64 it returned 0 and
guarded Eq(..., 0), which fails on the very inputs it was created from.

Since #177570, compute_concrete_val uses guarding_hint_or_throw, which goes
through _maybe_realize_expr and does int(expr), truncating sympy.Float.

_guarding_hint_or_throw_base now returns a Python float for sympy.Float.
_maybe_realize_expr (shared with the int-only optimization_hint paths) is
unchanged.

Typing:
- Add overloads to the module-level guarding_hint_or_throw so SymBool/bool
  -> bool, SymInt/int -> int, SymNode -> int | bool | float.
- ShapeEnv and Inductor SizeVarAllocator guarding_hint_or_throw /
  guarding_hints_or_throw now return int | float (| bool for ShapeEnv).
- Int-only Inductor call sites (expand's mark_reuse size products and the
  CUTLASS EVT size_hint_fn callback) convert explicitly with int().

Test: python test/test_dynamic_shapes.py -k test_evaluate_expr_float_without_hint
ghstack-source-id: 16a2d62
Pull-Request: #202324
@laithsakka

Copy link
Copy Markdown
Contributor Author

rebased

@laithsakka

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@drisspg

drisspg commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

@claude anyreason this hasnt landed yet?

@claude

claude Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Claude encountered an error after 13s —— View job


I'll analyze this and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request GL/superseded GreenLight review is superseded by changes Merged module: inductor ready for review Automated review passed, ready for a final review by a maintainer. release notes: fx release notes category Reverted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants