Skip to content

fix(torch-stubs): return torch.return_types named tuples - #4916

Open
yeetypete wants to merge 2 commits into
facebook:mainfrom
yeetypete:torch-stubs-return-types
Open

yeetypete wants to merge 2 commits into
facebook:mainfrom
yeetypete:torch-stubs-return-types

Conversation

@yeetypete

@yeetypete yeetypete commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a stub for torch.return_types and use it for the operations that return named tuples. Currently sort, max(dim=...), topk, aminmax, slogdet, etc. return types are annotated as plain tuple[Tensor, Tensor] and .values, .indices, .sign, and the other documented fields are reported as missing-attribute errors.

PyTorch adds these annotations by generating its own return_types.pyi from the output names in native_functions.yaml:

https://github.com/pytorch/pytorch/blob/d6c03540dc0f040e1d8a920d19d9333de6513545/torchgen/api/python.py#L1012-L1033

For simplicity we just write the stubs manually here instead of generating them.

Note: linalg_slogdet is included as well although its missing in the PyTorch generated stubs.

Test Plan

Run test.py.

@stroxler

Copy link
Copy Markdown
Contributor

Thanks @yeetypete!

Something seems stuck here - auto-import actually failed, and I can't seem to import manually either. Maybe a rebase would help, but another option might be to just close this and open a fresh PR. What ought to happen is that a diff number should usually appear a few minutes after a PR is submitted

@github-actions github-actions Bot added size/l and removed size/l labels Sep 15, 2026
@stroxler stroxler assigned stroxler and unassigned NathanTempest Sep 15, 2026
@stroxler
stroxler requested review from stroxler and removed request for NathanTempest September 15, 2026 13:45
@yeetypete
yeetypete force-pushed the torch-stubs-return-types branch from f17cc9b to 229fca0 Compare September 18, 2026 17:28
@github-actions github-actions Bot added size/l and removed size/l labels Sep 18, 2026
@meta-codesync

meta-codesync Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D119814478. (Because this pull request was imported automatically, there will not be any future comments.)

@yeetypete

Copy link
Copy Markdown
Contributor Author

Apologies for the delay. Rebased and looks like the import worked now :)

@github-actions github-actions Bot added size/l and removed size/l labels Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants