Add option to use CUDA graphs with TRT for RF-DETR Object Detection in inference_models#1938
Open
Add option to use CUDA graphs with TRT for RF-DETR Object Detection in inference_models#1938
inference_models#1938Conversation
…seem to be TRT packages for them yet.
…ts to True and reference in RFDETR TRT classes
Contributor
Author
|
I have added LRU caching with (shape, dtype, device) keys for CUDA graph execution, and added a test for this caching in I have run GPU |
…ow/inference into feature/rfdetr-trt-use-cudagraphs
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.
What does this PR do?
Adds a
use_cuda_graphflag toRFDetrForObjectDetectionTRT.forwardwhich enables capturing the CUDA graph and replaying it, unlocking a nice ~10% FPS speedup (observed range was anything from 7% to 12% depending on how saturated the GPU is) onrfdetr-nano.After the CUDA graph is captured in
execute_trt_engine, it and other state related to it is packaged into aTRTCudaGraphStatedataclass which is returned back up to theRFDetrForObjectDetectionTRTinstance, where it is cached. This lets subsequent forward passes simply replay the graph instead of recapturing it.Because this PR changes the return signature of
infer_with_trt_engine, there are a lot of files with 1-or-2 lines of changes where I'm just trying to prevent unpacking errors.Type of Change
Testing
Test details:
I added an integration test at
inference_models/tests/integration_tests/models/test_rfdetr_predictions_trt.pyI also added a profiling script at
inference_models/development/profiling/profile_rfdetr_trt_cudagraphs.pybenchmarks the speed ofRFDetrForObjectDetectionTRTboth with and without CUDA graph. Here are some example results (some variance was observed here which depended on GPU cooldown):I intended to also write a similar script for the
rfdetr-seg-*models, but as far as I can tell, the seg models don't haveinference_modelspackages yet, other thanrfdetr-seg-preview, and that only hastorchandonnxpackages. I did have Claude make me a script that downloads the onnx package, uses one of the development compilation convenience functions to compile it to TRT, and then runs my benchmark... but that script is an utter mess (to the point that it's not even included in the PR), and I'm getting the impression that support for TRT as a backend for RF-DETR-Seg might be a work in progress ininference_models? The results from the script nonetheless seem to confirm a speedup for seg models too:EDIT 2026-02-11: Added a VRAM-profiling script
inference_models/development/profiling/profile_cudagraph_vram.pyto test what CPU and GPU memory usage look like when caching multiple shapes. Below are three plots of VRAM and system RAM usage in different scenarios.I also added a test to check that cache eviction works the way I would expect it to, in
test_yolov8_object_detection_predictions_trt.py.Checklist
Additional Context
N/A