Skip to content

simplify max(1,x) to x when x known >=1#157189

Closed
laithsakka wants to merge 4 commits intogh/laithsakka/233/basefrom
gh/laithsakka/233/head
Closed

simplify max(1,x) to x when x known >=1#157189
laithsakka wants to merge 4 commits intogh/laithsakka/233/basefrom
gh/laithsakka/233/head

Conversation

@laithsakka
Copy link
Contributor

@laithsakka laithsakka commented Jun 28, 2025

Stack from ghstack (oldest at bottom):

Creating contiguous strides creates an expression max(1, x). Often we know that x >= 1, in
which case we should simplify max(1, x) to x.

This appeared in two situations:

  1. An internal user complained about statically_known_true(x == max(1, x)) failing (internal link: https://fb.workplace.com/groups/1028545332188949/permalink/1232958568414290).
    This Fix stride comparison max(512 - s, 1) vs. (512 - s) #155938 won't be needed with this.

  2. Not simplifying the above could result in wrong ConstraintViolationErrors.
    Because we assume non-trival single arg guards shall evaporate see the logic in the function
    issue_guard in symbolic_shapes.py

with this change we longer throw ConstraintViolationErrors with the program bellow
this is blocking landing this PR from landing
internally. Due to internal export tests throwing ConstraintViolationErrors.
like

Constraints violated (width)!
  - Not all values of width = L['x'].size()[3] in the specified range 224 <= width <= 455 satisfy the generated guard max(1, 1 + (((-1) + L['x'].size()[3]) // 2)) == (1 + (((-1) + L['x'].size()[3]) // 2)).
x = torch.rand(10)
torch._dynamo.mark_dynamic(x, 0, max=20, min=5)

@torch.compile(fullgraph=True, dynamic=True)
def func(x):
    if max(1, (-1 + x.size()[0]//2)) == (-1+x.size()[0]//2):
        return x*400
    else:
        return (x*10)*100

func(x)

cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv

@pytorch-bot
Copy link

pytorch-bot bot commented Jun 28, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 8b30dd2 with merge base feea575 (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

Creating contiguous strides creates an expression max(1, x). Often we know that x >= 1, in
 which case we should simplify max(1, x) to x.

This appeared in two situations:
1) An internal user complained about statically_known_true(x == max(1, x)) failing (internal link: https://fb.workplace.com/groups/1028545332188949/permalink/1232958568414290). 
This #155938 won't be needed with this.

3) Not simplifying the above could result in wrong ConstraintViolationErrors.
Because we assume non-trival single arg guards shall evaporate see the logic at this line
with this change we longer throw ConstraintViolationErrors with the program bellow 
this is blocking landing this [PR](#155590) from landing
internally. Due to internal export tests throwing ConstraintViolationErrors.


```
x = torch.rand(10)
torch._dynamo.mark_dynamic(x, 0, max=20, min=5)

torch.compile(fullgraph=True, dynamic=True)
def func(x):
    if max(1, (-1 + x.size()[0]//2)) == (-1+x.size()[0]//2):
        return x*400
    else:
        return (x*10)*100

func(x)

```



[ghstack-poisoned]
Copy link
Contributor

@pianpwk pianpwk left a comment

Choose a reason for hiding this comment

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

change makes sense, approving to avoid revert. let's generalize this better later?

[ghstack-poisoned]
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Jun 28, 2025
ghstack-source-id: 18f47cf
Pull Request resolved: #157189
@laithsakka laithsakka added the topic: not user facing topic category label Jun 28, 2025
@laithsakka
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 28, 2025
@pytorchmergebot
Copy link
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

@github-actions github-actions bot deleted the gh/laithsakka/233/head branch July 29, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request fx Merged release notes: fx release notes category topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants