Skip to content

[library] Keep the user's module on a triton_op's function - #202256

Closed
bobrenjc93 wants to merge 17 commits into
gh/bobrenjc93/1641/basefrom
gh/bobrenjc93/1641/head
Closed

bobrenjc93 wants to merge 17 commits into
gh/bobrenjc93/1641/basefrom
gh/bobrenjc93/1641/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

torch.library.triton_op now wraps the user's function with functools.wraps, so the op's _init_fn.__module__ is the module that defined it, not torch._library.triton.

Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through torch._library.custom_ops.OPDEFS[name]._init_fn.__module__. For a custom_op that is already the user's module. For a triton_op it was torch._library.triton, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

What changes

  • triton_op's inner backend_fn is decorated with functools.wraps(fn, updated=()). This copies __module__, __name__, __qualname__, __doc__ and __wrapped__, but not __dict__.
  • Calling the op is unchanged.

Test plan

New test test_triton_op_function_is_the_users in test/test_custom_ops.py.

python test/test_custom_ops.py -k triton_op_function_is_the_users

Written with an AI assistant (Claude Code) and reviewed by me.

Co-Authored-By: Claude Opus 5.5 [email protected]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

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

✅ AUTOMATED REVIEW - Ready for human review:

The PR wraps triton_op's internal backend_fn with functools.wraps(fn,
updated=()), so CustomOpDef._init_fn's __module__, __name__, __qualname__ and
__wrapped__ come from the user's function instead of torch._library.triton. It
also adds a regression test, and the review found no issues. Recommendation:
Approve.

Reviewed commit: 95de7f4

Review run

🔗 Helpful Links

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

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

✅ No Failures

As of commit 95de7f4 with merge base 25298e6 (image):
💚 Looks good so far! There are no failures yet. 💚

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Single-cause bugfix: triton_op's inner backend_fn now gets functools.wraps(fn, updated=()), so _init_fn keeps the user's __module__ and name
    • New test_triton_op_function_is_the_users asserts __module__, __wrapped__ is f and __qualname__, all of which fail on the old local backend_fn
  • Callers
    • _init_fn is only called at custom_ops.py:501 and named in the :793 message, which now says the user's function instead of backend_fn
    • The schema is passed explicitly from infer_schema(fn) and get_inner_triton_kernels(fn) takes fn, so the new __wrapped__ changes neither
  • Socialization
    • No signature, default or dispatch change; the stack above (#​202257, #​202258) is what reads _init_fn.__module__
    • updated=() skips copying __dict__, so no attributes from the user's function leak onto the wrapper

reason: clean
Reviewed commit: 95de7f4

Inference job

🐛 Report a wrong verdict

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 56s —— View job


The change is correct and safe to approve; the only problem is a test assertion that is always true.

PR Review: #202256

Summary

triton_op now wraps its inner backend_fn with functools.wraps(fn, updated=()). As a result, CustomOpDef._init_fn.__module__, __name__ and __qualname__ now report the user's function instead of torch._library.triton.backend_fn. No REVIEW.md files apply.

I checked these call sites and none of them change behaviour:

  • The schema is passed explicitly from infer_schema(fn), so inspect.signature following the new __wrapped__ has no effect on it.
  • In custom_ops.py, _init_fn is only called, at :501, and used for its name in an error message, at :793. That message now says f.register_fake instead of backend_fn.register_fake, which is an improvement.
  • torch/_vendor/quack/gemm_interface.py:179 reads _init_fn, but only on its own op class, not on triton ops.

Testing

  • test/test_custom_ops.py:5596: the assertion f._init_fn.__qualname__ == f._init_fn.__wrapped__.__qualname__ is always true once functools.wraps is applied, because wraps copies __qualname__ from the same object it stores in __wrapped__. It would not catch __wrapped__ pointing at the wrong function. Define the function first, wrap it with torch.library.triton_op(...)(fn), then assert op._init_fn.__wrapped__ is fn and op._init_fn.__qualname__ == fn.__qualname__. The __module__ == __name__ check is the one that guards the regression, and it is fine.

Recommendation

Approve. Fix the test assertion above before landing. One limitation for the follow-up PRs in this stack: __module__ points to the defining module even when the op is registered inside a function or in __main__. In those cases, importing that module will not register the op in a fresh process, so the precompile import step should handle or report that case.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Review replies:

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 35s —— View job


I found no new issues, and the earlier review's test-assertion finding is fixed at HEAD, so this looks ready to land.

PR Review: #202256 (re-review)

Summary

triton_op now wraps its inner backend_fn with functools.wraps(fn, updated=()). This makes CustomOpDef._init_fn report the user's __module__, __name__ and __qualname__. No REVIEW.md files apply.

The earlier finding about the always-true __qualname__ check is fixed. The test now wraps f explicitly and asserts op._init_fn.__wrapped__ is f (test/test_custom_ops.py:5598). The earlier note about ops defined in __main__ or inside a function is handled later in the stack (#202257, #202259), so it doesn't block this PR.

Apart from custom_ops.py, the only other code that reads _init_fn is torch/_vendor/quack/gemm_interface.py:179, and that code doesn't touch triton ops. In custom_ops.py, _init_fn is called at :501 and its __name__ goes into an error message at :793. The schema is passed in explicitly, so inspect.signature following the new __wrapped__ doesn't change it.

Recommendation

Approve. Nothing blocks landing.

[ghstack-poisoned]
@bobrenjc93 bobrenjc93 closed this Oct 28, 2026
@bobrenjc93 bobrenjc93 reopened this Oct 28, 2026
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Oct 28, 2026
`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 9a1509a
Pull-Request: #202256
bobrenjc93 added a commit that referenced this pull request Oct 28, 2026
`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 9a1509a
Pull-Request: #202256
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Oct 28, 2026
`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: e1fc4de
Pull-Request: #202256
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Oct 28, 2026
`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 65521ff
Pull-Request: #202256
@pytorchgreenlight pytorchgreenlight Bot added the GL/approved Approved by GreenLight label Oct 28, 2026
@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.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
[ghstack-poisoned]

@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 95de7f4: nothing blocking; ready for human review.

The PR wraps triton_op's internal backend_fn with functools.wraps(fn, updated=()), so CustomOpDef._init_fn's module, name, qualname and wrapped come from the user's function instead of torch._library.triton. It also adds a regression test, and the review found no issues. Recommendation: Approve.

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

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 29, 2026
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 0 checks:

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

dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…02256)

`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pull Request resolved: pytorch#202256
Approved by: https://github.com/pytorchgreenlight
ghstack dependencies: pytorch#202253
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…02256)

`torch.library.triton_op` now wraps the user's function with `functools.wraps`, so the op's `_init_fn.__module__` is the module that defined it, not `torch._library.triton`.

## Why precompile needs this

A precompile artifact runs on every rank in a fresh process, where nothing has imported the modules that register the custom ops its graphs call. The next commits have the artifact import those modules, found through `torch._library.custom_ops.OPDEFS[name]._init_fn.__module__`. For a `custom_op` that is already the user's module. For a `triton_op` it was `torch._library.triton`, which is importable but registers nothing, so the artifact would import the wrong module and still fail.

## What changes

- `triton_op`'s inner `backend_fn` is decorated with `functools.wraps(fn, updated=())`. This copies `__module__`, `__name__`, `__qualname__`, `__doc__` and `__wrapped__`, but not `__dict__`.
- Calling the op is unchanged.

## Test plan

New test `test_triton_op_function_is_the_users` in `test/test_custom_ops.py`.

```
python test/test_custom_ops.py -k triton_op_function_is_the_users
```

*Written with an AI assistant (Claude Code) and reviewed by me.*

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pull Request resolved: pytorch#202256
Approved by: https://github.com/pytorchgreenlight
ghstack dependencies: pytorch#202253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request GL/approved Approved by GreenLight Merged ready for review Automated review passed, ready for a final review by a maintainer. topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants