Skip to content

Make interpolate_bilinear deterministic using decomposition - #101115

Closed
ezyang wants to merge 8 commits into
gh/ezyang/2092/basefrom
gh/ezyang/2092/head
Closed

ezyang wants to merge 8 commits into
gh/ezyang/2092/basefrom
gh/ezyang/2092/head

Conversation

@ezyang

@ezyang ezyang commented May 10, 2023 •

Copy link
Copy Markdown
Contributor

Signed-off-by: Edward Z. Yang <[email protected]>

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented May 10, 2023 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit bf24c72:
💚 Looks good so far! There are no failures yet. 💚

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

Signed-off-by: Edward Z. Yang <ezyangmeta.com>

[ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]

@ngimel ngimel 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.

Awesome!

ezyang added 4 commits May 10, 2023 19:26
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]
@ezyang ezyang mentioned this pull request May 11, 2023
Signed-off-by: Edward Z. Yang <ezyangmeta.com>

cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire

[ghstack-poisoned]
Comment thread test/test_torch.py
'upsample_bilinear2d_backward_out_cuda',
torch.device(device).type == 'cuda')

@skipIfTorchInductor("aot-autograd issue")

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.

🤔 what broke?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All of these tests are skipped this way. What broke was requires_grad was not set correctly on some of the tensors. TBH, it is probably worth figuring out what went wrong; maybe it's "requires_grad tensors was created inside graph"

Comment thread torch/nn/functional.py
# importlib is required because the import cannot be top level
# (cycle) and cannot be nested (TS doesn't support)
return importlib.import_module('torch._decomp.decompositions').upsample_bilinear2d_vec(
input, output_size, align_corners, scale_factors)

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.

is import_lib still necessary, if you're running it from inside of a region of if not torch.jit.is_scripting?

I would have thought that torchscript would know to not try to scriptify the region in this block, but maybe that's wrong

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes:

  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 141, in <listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 320, in __call__
    return method(ctx, node)
  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 644, in build_If
    build_stmts(ctx, stmt.body),
  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 141, in build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 141, in <listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/data/users/ezyang/d/pytorch/torch/jit/frontend.py", line 319, in __call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: import statements aren't supported:
  File "/data/users/ezyang/d/pytorch/torch/nn/functional.py", line 4002
                # importlib is required because the import cannot be top level
                # (cycle) and cannot be nested (TS doesn't support)
                import torch._decomp.decompositions as decomp
                ~~~~~~ <--- HERE
                return decomp.upsample_bilinear2d_vec(
                    input, output_size, align_corners, scale_factors)


----------------------------------------------------------------------

I think what is going on is that TorchScript is parsing the entire function body before eliminating is_scripting, so you can't use language features that the parser doesn't support.

@ezyang

ezyang commented May 11, 2023

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 11, 2023
@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 pushed a commit that referenced this pull request Aug 16, 2026
TorchTitan's Kimi K2.7 vision encoder resizes its learnable positional table here:

https://github.com/pytorch/torchtitan/blob/85c549b9332067f7615241e73e0c4a28d44b065f/torchtitan/models/kimi_k2_7/vision_encoder.py#L90-L106

```python
grid_table = pos_embed.permute(2, 0, 1).unsqueeze(0).float()
...
F.interpolate(grid_table, size=(h, w), mode=bicubic)
```

this PR make sure `--debug.deterministic` / `torch.use_deterministic_algorithms(True)` work. Fix is similar to  to #101115
* direct ATen calls, TorchScript, and `antialias=True` are unchanged
* deterministic decomposition is slower and can have small forward rounding differences
Pull Request resolved: #193707
Approved by: https://github.com/Skylion007
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…93707)

TorchTitan's Kimi K2.7 vision encoder resizes its learnable positional table here:

https://github.com/pytorch/torchtitan/blob/85c549b9332067f7615241e73e0c4a28d44b065f/torchtitan/models/kimi_k2_7/vision_encoder.py#L90-L106

```python
grid_table = pos_embed.permute(2, 0, 1).unsqueeze(0).float()
...
F.interpolate(grid_table, size=(h, w), mode=bicubic)
```

this PR make sure `--debug.deterministic` / `torch.use_deterministic_algorithms(True)` work. Fix is similar to  to pytorch#101115
* direct ATen calls, TorchScript, and `antialias=True` are unchanged
* deterministic decomposition is slower and can have small forward rounding differences
Pull Request resolved: pytorch#193707
Approved by: https://github.com/Skylion007
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 Merged module: dynamo release notes: nn release notes category topic: new features topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants