Skip to content

refactor: share propagation between sam3_track_frame and sam3_propagate_frame - #25

Merged
PABannier merged 1 commit into
mainfrom
cleanup/dedupe-track-propagate
Sep 16, 2026
Merged

PABannier merged 1 commit into
mainfrom
cleanup/dedupe-track-propagate

Conversation

@PABannier

Copy link
Copy Markdown
Owner

What / why

sam3_track_frame and sam3_propagate_frame repeated the same code almost line for line: propagating active masklets, propagating pending masklets (including memory encoding and storing the object pointer), memory encoding for active masklets, sam3_update_tracker, the add_mask_to_result lambda, and the resolve_overlaps / fill_holes / remove_sprinkles post-processing. This PR moves that shared code into static helpers that both public functions call. sam3_propagate_frame is now 9 statements.

Helper split

  • sam3_propagate_masklets(tracker, state, model, masklets, fi, pm, po): propagates each masklet that has memory, updates score / last_seen / MDS, and writes the binarized mask into pm. With po set (active masklets), the raw outputs are kept. With nullptr (pending masklets), memory is encoded right away, one masklet at a time as before.
  • sam3_store_propagated_memory: sam3_encode_memory + sam3_extract_obj_ptr_cpu + sam3_store_obj_ptr.
  • sam3_commit_active_masklets: memory-encodes the active masklets in po, then calls sam3_update_tracker.
  • sam3_add_mask_detection (was the lambda) and sam3_add_masklet_detections (loops over a masklet list and emits masks found in pm).
  • sam3_finish_tracked_frame: resolve_overlaps, fill_holes, remove_sprinkles, then frame_index++.

Call order is unchanged in both functions. In sam3_track_frame, pending masklets are still memory-encoded before PCS detection, and new pending masklets are still memory-encoded before the active masklets. The no_obj_ptr F16/F32 read block has not changed at all. The fprintf and SAM3_LOG calls stay in the public functions, so __func__ in the logs is unchanged.

Verification

Parity tool (byte-compare against deterministic origin/main baselines; covers encode_image, segment_pvs, visual tracker add_instance + propagate_frame over 3 frames):

PARITY OK   edgetam_f16 gpu0
PARITY OK   edgetam_f16 gpu1
PARITY OK   sam2.1_hiera_tiny_q8_0 gpu0
PARITY OK   sam2.1_hiera_tiny_q8_0 gpu1

Full Release build: no errors, no new warnings. The only warnings come from ggml/stb and were already there.

sam3_track_frame is only compile-tested. There is no full SAM3 model locally. I checked its diff line by line against the original instead.

Existing discrepancy (kept as is, likely a bug)

In sam3_track_frame, the pending-masklet loop never wrote the propagated mask into pm. sam3_propagate_frame does. So in sam3_track_frame, pending masklets from earlier frames are never emitted in the result, and their all_tracked_masks entries passed to sam3_match_detections are empty. This also applies to masklets promoted to active on this frame. This PR keeps that behavior on purpose: track_frame passes a scratch pending_pm map, with a comment at the call site. Fixing it (passing pm) should be a separate PR.

Diff

 1 file changed, 118 insertions(+), 204 deletions(-)

🤖 Generated with Claude Code

https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq

…te_frame

Move the duplicated code into static helpers: active/pending masklet
propagation, memory encode + object pointer store, active commit +
tracker update, mask-to-detection, and the post-processing. Call order
and behavior are unchanged in both functions. track_frame keeps its
pending masks in a scratch map, as before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq
@PABannier
PABannier merged commit 39bb5bc into main Sep 16, 2026
3 checks passed
@PABannier
PABannier deleted the cleanup/dedupe-track-propagate branch September 16, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant