Repository navigation
refactor: RAII ownership for per-stage ggml contexts and allocators - #27
Merged
Merged
Conversation
Function-local graph contexts and allocators in the image encoders, EdgeTAM perceiver, segment_pcs sub-graphs, segment_pvs, propagate_single and encode_memory are now held by ggml_context_ptr / ggml_gallocr_ptr (ggml-cpp.h). Early-return paths no longer repeat ggml_gallocr_free/ggml_free. Free points are unchanged: each owner dies at the same scope exit where the manual frees were, and sam3_encode_image hands ownership to sam3_state via release(). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq
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
Function-local graph contexts and allocators now use ggml's own smart-pointer aliases from
ggml-cpp.h(ggml_context_ptr,ggml_gallocr_ptr; they compile fine under C++14). Early-return paths no longer repeatggml_gallocr_free(galloc); ggml_free(ctx0);.Functions covered:
edgetam_encode_image,edgetam_perceiver_forward(both sub-graphs),sam2_encode_image_hiera,sam3_encode_image, the 5sam3_segment_pcssub-graphs,sam3_segment_pvs,sam3_propagate_single,sam3_encode_memory.Why
The manual cleanup was copy-pasted onto every failure branch (about 50 frees). That's easy to get wrong when adding a new early return, and it hides the actual logic.
Behaviour
.reset()calls were needed.sam3_encode_imagehands ownership tosam3_statewithrelease().sam3_state,sam3_trackerandsam3_modelmembers keep their current ownership.Verification
sam3_benchmark --filter tiny --n-frames 3: every CPU row is OK. On Metal, the q8_0 and q4_0 rows are OK at about 790 ms/frame, the same as origin/main. The q4_1 Metal rows crash with SIGSEGV on origin/main too, so that bug already existed.segment_pcs,track_frame). There's no full SAM3 model locally, so it was only compiled and its diff reviewed by hand.Merge order
This PR touches many lines, some of them near the regions changed by the sibling cleanup PRs. It should merge last and be rebased on main at that point.
🤖 Generated with Claude Code
https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq