Skip to content

[pytorch][PR] [Flop Counter] Support Flop counter for a decomposed custom triton kernel in torch op - #169876

Closed
Lucaskabela wants to merge 1 commit into
pytorch:mainfrom
Lucaskabela:export-D81943083
Closed

Lucaskabela wants to merge 1 commit into
pytorch:mainfrom
Lucaskabela:export-D81943083

Conversation

@Lucaskabela

Copy link
Copy Markdown
Contributor

Summary:
Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom torch.library.op, the operator decomposes into one or more custom kernels. To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, register_flop_formula on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

Unit Test

python test/test_flop_counter.py TestFlopCounter.test_flop_counter_custom_triton
.
----------------------------------------------------------------------
Ran 1 test in 0.005s

Differential Revision: D81943083

Pulled By: Lucaskabela

@Lucaskabela
Lucaskabela requested a review from zou3519 as a code owner December 8, 2025 21:02
@pytorch-bot

pytorch-bot Bot commented Dec 8, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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:

❌ 3 New Failures, 6 Unrelated Failures

As of commit be47d44 with merge base e33fa0e (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@meta-codesync

meta-codesync Bot commented Dec 8, 2025

Copy link
Copy Markdown

@Lucaskabela has exported this pull request. If you are a Meta employee, you can view the originating Diff in D81943083.

@Lucaskabela

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Dec 8, 2025
Lucaskabela added a commit to Lucaskabela/pytorch that referenced this pull request Dec 8, 2025
…ernel in torch op (pytorch#169876)

Summary:

Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom `torch.library.op`, the operator decomposes into one or more custom kernels.  To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, `register_flop_formula` on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

### Unit Test
```
python test/test_flop_counter.py TestFlopCounter.test_flop_counter_custom_triton
```

```
.
----------------------------------------------------------------------
Ran 1 test in 0.005s
```

Differential Revision: D81943083

Pulled By: Lucaskabela
Comment thread torch/utils/flop_counter.py Resolved
Comment thread test/test_flop_counter.py Resolved
Comment thread torch/utils/flop_counter.py Resolved
Lucaskabela added a commit to Lucaskabela/pytorch that referenced this pull request Dec 10, 2025
…ernel in torch op (pytorch#169876)

Summary:

Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom `torch.library.op`, the operator decomposes into one or more custom kernels.  To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, `register_flop_formula` on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

### Unit Test
```
python test/test_flop_counter.py TestFlopCounter.test_flop_counter_custom_triton
```

```
.
----------------------------------------------------------------------
Ran 1 test in 0.005s
```

Differential Revision: D81943083

Pulled By: Lucaskabela
…ernel in torch op (pytorch#169876)

Summary:

Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom `torch.library.op`, the operator decomposes into one or more custom kernels.  To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, `register_flop_formula` on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

### Unit Test
```
python test/test_flop_counter.py 
```

```
..
----------------------------------------------------------------------
Ran 25 tests in 1.021s

OK (skipped=3)
```

Differential Revision: D81943083

Pulled By: Lucaskabela
Comment thread test/test_flop_counter.py
@torch._functorch.config.patch("activation_memory_budget", 0.1)
@torch._functorch.config.patch("activation_memory_budget_solver", "dp")
@torch._functorch.config.patch("is_non_builtin_to_include", True)
def test_flop_counter_custom_triton_op_two_kernels_auto_ac(self):

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.

New test covering op with multiple kernels @bdhirsh

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

thanks!

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 11, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@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

@Lucaskabela

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Dec 17, 2025
…stom triton kernel in torch op (pytorch#169876)

Summary:
Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom `torch.library.op`, the operator decomposes into one or more custom kernels.  To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, `register_flop_formula` on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

### Unit Test
```
python test/test_flop_counter.py TestFlopCounter.test_flop_counter_custom_triton
```

```
.
----------------------------------------------------------------------
Ran 1 test in 0.005s
```

Differential Revision: D81943083

Pulled By: Lucaskabela

Pull Request resolved: pytorch#169876
Approved by: https://github.com/bdhirsh
krastogi-in pushed a commit to krastogi-in/pytorch that referenced this pull request Jan 9, 2026
…stom triton kernel in torch op (pytorch#169876)

Summary:
Modify our flop formula API to registering triton kernels, and provide an example of how to escape hatch triton op to leverage this API

Context: For some custom `torch.library.op`, the operator decomposes into one or more custom kernels.  To date, there has not been a good system for having these counted by FlopCounterMode, as the function will decompose to the wrapped kernels, and then check the registry.

To support this use case, we modify the API, `register_flop_formula` on GPU kernels, and demonstrate how it is used to unwrap kernels in custom ops.

### Unit Test
```
python test/test_flop_counter.py TestFlopCounter.test_flop_counter_custom_triton
```

```
.
----------------------------------------------------------------------
Ran 1 test in 0.005s
```

Differential Revision: D81943083

Pulled By: Lucaskabela

Pull Request resolved: pytorch#169876
Approved by: https://github.com/bdhirsh
malfet added a commit that referenced this pull request Jan 16, 2026
If one attempts to import `torch.utils.flop_count` on MacOS (or any
other CPU system), they'll be greated with the following repeated
warning
```
% python -c "import torch.utils.flop_counter"
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
```
which was introduced by #169876

Move import to top of flop_counter utils and only warn if system is
compiled with GPU support that requires triton (i.e. CUDA/ROCM or XPU)


ghstack-source-id: d2f3c83
Pull-Request: #172614
pytorchmergebot pushed a commit that referenced this pull request Jan 16, 2026
If one attempts to import `torch.utils.flop_count` on MacOS (or any other CPU system), they'll be greeted with the following repeated warning
```
% python -c "import torch.utils.flop_counter"
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.246000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
W0115 18:52:55.247000 34027 torch/utils/flop_counter.py:45] triton not found; flop counting will not work for triton kernels
```
which was introduced by #169876

Move import to top of flop_counter utils and only warn if system is compiled with GPU support that requires triton (i.e. CUDA/ROCM or XPU)

Pull Request resolved: #172614
Approved by: https://github.com/Lucaskabela
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 fb-exported Merged meta-exported topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants