Repository navigation
Add optional CUDA Graph fast path for the image encoder - #775
Open
trueoneplusone wants to merge 1 commit into
Open
trueoneplusone wants to merge 1 commit into
trueoneplusone wants to merge 1 commit into
Conversation
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
This PR adds an opt-in
cuda_graph_image_encoder=Truefast path tobuild_sam2_video_predictorfor fixed-signature CUDA video inference.It lazily captures
forward_imageinto a native PyTorchCUDAGraphand replays it on subsequent frames to reduce CPU kernel-launch overhead. The existing eager path remains the default.Refs #768.
Why this scope
The image encoder is a good CUDA Graph boundary because its tensor signature is fixed across video frames and it does not depend on SAM2's temporal memory bookkeeping.
I intentionally do not capture
track_stepor alterframe_idx/memory selection. Those paths use real temporal distances for memory and object-pointer positional encoding, so forcing a sentinel frame index or static memory structure could silently change tracking semantics.Correctness details
CUDA Graph outputs live at fixed addresses and are overwritten on the next replay. SAM2 caches image features between interactions/frames, so exposing those buffers directly would create cross-frame aliasing.
This implementation therefore clones the captured output tree before returning it, preserving frame-owned feature tensors.
The graph is automatically re-captured when any relevant input signature component changes:
Training, gradient-enabled, and non-CUDA execution fall back to eager mode.
cuda_graph_image_encoder=Trueis currently mutually exclusive withvos_optimized=True/compile_image_encoder=Trueto avoid stacking two independent graph/compiler fast paths without validation.Local validation
Environment:
notebooks/videos/bedroomEnd-to-end runs through the public builder API:
For both resolutions, eager vs. CUDA Graph outputs had:
I also checked multiple different input frames at the encoder boundary and observed exact feature parity.
Tests
Added coverage for:
Local checks:
pytest -q tests/test_cuda_graph_image_encoder.py→ 3 passedufmt check ...→ passedgit diff --check→ passedThe new option is documented in the README.