Skip to content

sam3 : SAM 3.1 (detector-only, transformers weights) support - #38

Open
ivanovalex32 wants to merge 1 commit into
PABannier:mainfrom
ivanovalex32:pr-sam31
Open

ivanovalex32 wants to merge 1 commit into
PABannier:mainfrom
ivanovalex32:pr-sam31

Conversation

@ivanovalex32

Copy link
Copy Markdown

Adds support for SAM 3.1 (detector-only checkpoint converted from transformers weights, e.g. unsloth/sam3.1).

What's included

  • convert_sam31_to_ggml.py — safetensors -> ggml converter with strict key validation and on-the-fly RoPE freqs_cis synthesis (window + global blocks).
  • Runtime support for detector-only files: sam3_load_model scans tensor names and disables tracker registration when the file has no tracker weights; PVS/tracking APIs fail with a clear error.
  • Text encoder padding mask fix: sam3_fill_causal_mask now also masks padded token ids (aligns text features with the transformers reference).
  • ViT debug tensor names (dbg_block_%d_out, prefix dumps) for the e2e test.
  • --detect / --text-prompt modes in sam3_benchmark for benchmarking detector-only models.
  • tests/dump_sam31_pcs_reference.py — transformers-based Python reference for the e2e comparison.

Test harness fixes (Release builds)

The phase5/phase6 dump helpers previously failed in Release builds (#ifndef NDEBUG-guarded debug tensors missing from the graph). Missing dump tensors are now skipped with a warning instead of failing the run, and the FPN/neck inputs of the phase5 graph are materialized as graph inputs to avoid allocator reuse of encode-stage buffers.

GPU / HIP testing

test_e2e_pcs now accepts --gpu (and --n-threads N) and prints a performance summary (encode, phase5, end-to-end PCS). On a build with the HIP backend it validates the GPU path against the same Python reference.

HIP numbers (gfx1151, ROCm), measured with sam3_benchmark --detect (10 frames):

model load (ms) detect/frame (ms)
sam3.1-f16 399.7 1697.6
sam3.1-f32 596.2 2775.1

Validation

  • e2e PCS vs transformers reference (f32 model): 28/28 PASS (score diff 0.037, box L1 1.65 px, mask agreement 0.996).
  • Server validation (validate_cases.sh): 6/6 IoU/pixel-match PASS on CPU and HIP GPU.

- convert_sam31_to_ggml.py: safetensors->ggml converter with strict key
  validation and freqs_cis synthesis; --ftype 0/1
- runtime: tracker_present detection (file scan for neck.trk.) with PVS-API
  guards for detector-only models; padding mask in text encoder
  (sam3_fill_causal_mask now masks pad tokens as keys)
- tests: e2e reference generator (transformers, ViT eps=1e-5, 0/1 masks),
  img_feats/img_pe as owned input tensors in phase5 dump, dbg_block_%d_out
  vit dumps
- benchmark: --detect mode (text-prompt PCS per frame) for detector-only
  models
- docs: README model zoo + feature matrix entries for SAM 3.1
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