PyTorch: Speed up PAF cost computation#3117
Merged
AlexEMG merged 6 commits intoDeepLabCut:mainfrom Oct 24, 2025
Merged
Conversation
Improve PAF performance by performing affinity computation on the GPU with advanced indexing. - Affinities are now calculated using torch operations. - The cost per batch dictionary is created more efficiently.
maximpavliv
approved these changes
Oct 14, 2025
Contributor
maximpavliv
left a comment
There was a problem hiding this comment.
Good job! ✅
- Code is clearer, docstrings are much more detailed, and variable naming is improved (
batch_size,paf_limb_inds). - GPU usage is now more efficient, avoiding unnecessary early CPU transfers → speed is improved.
- The inference results slighly differ from expected results.
AlexEMG
approved these changes
Oct 24, 2025
deruyter92
added a commit
to deruyter92/DeepLabCut-live
that referenced
this pull request
Jan 21, 2026
This commit updates the PAF predictor to follow the DeepLabCut implementation in version 3.0.0.rc13. See DeepLabCut/DeepLabCut#3117
MMathisLab
pushed a commit
to DeepLabCut/DeepLabCut-live
that referenced
this pull request
Jan 22, 2026
* DEKRPredictor: add non-maximum suppression (NMS) This commit Updates the DEKR predictor to follow the DeepLabCut implementation in version 3.0.0rc7, see DeepLabCut/DeepLabCut#2907 * DEKRPredictor: speed up with vectorized operations This commit updates the DEKRPredictor to follow the DeepLabCut implementation in version 3.0.0rc13. see DeepLabCut/DeepLabCut#3121 * PartAffinityFieldPredictor (PAF): Speed up cost computation This commit updates the PAF predictor to follow the DeepLabCut implementation in version 3.0.0.rc13. See DeepLabCut/DeepLabCut#3117 * HeatmapPredictor (single animal): speed up with vecorized operations This commit updates the `HeatmapPredictor` in single_predictor.py to follow the implementation in DeepLabCut 3.0.0rc13. See DeepLabCut/DeepLabCut#3110
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improve PAF performance by performing affinity computation on the GPU with advanced indexing.
Details
This implementation tries to delegate the parts that can be parallelized to the GPU by using torch operations instead of numpy ones. (thanks to @maximpavliv for running the benchmark)
The figure below shows the parts of the execution that can be optimized.
The part outlined by the red rectangle concerning
compute_peaks_and_costsis now as optimized as I could make it.The blue rectangle is concerned with the assembly procedure which did not get into in this PR. There is a lot of room for optimization in there as well, which may require a lot of refactoring/changes.