Skip to content

Studio: Mask from text runs SAM 3.1 in bf16 unless the card is short of memory - #14736

Merged
danielhanchen merged 5 commits into
mainfrom
fix/sam3-auto-bf16-when-roomy
Oct 25, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix/sam3-auto-bf16-when-roomy

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Problem

Since #14310, UNSLOTH_SAM3_PRECISION=auto (the default) picks the INT8-ConvRot detector on every sm_80+ NVIDIA card, and the download fetches only that file. The PR's own B200 numbers (median of 5 calls, 5 concepts) show that this trades speed for memory:

Peak Per Find
BF16 1.82 GB 0.35 s
INT8-ConvRot (auto) 1.29 GB 0.77 s

Re-measured through MaskSegmenter.segment on one shared B200 (same image, 5 concepts, rounds interleaved as bf16 / INT8-ConvRot / bf16, each round the median of 5 calls, 3 rounds):

Weights Peak Per Find, round medians
BF16 (A) 1666 MiB 1964 MiB 0.32 / 0.25 / 0.41 s
INT8-ConvRot 999 MiB 1297 MiB 0.92 / 1.64 / 1.38 s
BF16 (A again) 0.41 / 0.67 / 0.46 s

The card was shared, so the absolute numbers are noisy (bf16 A/A spread up to 0.25 s), but INT8-ConvRot was 2.2x to 3.0x slower than the slower bf16 arm of the same round in every round.

So the default became more than 2x slower per Find to save about 0.65 GB, even on cards with plenty of free memory. The device gate needs 4 GiB free for either precision, so INT8 does not let the segmenter run on the GPU in any case where bf16 could not.

Change

  • auto runs bf16 while the card has room for it. It picks a gated pre-quantized file (INT8-ConvRot, or plain INT8 with UNSLOTH_SAM3_INT8_CONVROT=0) only when the card's free memory, minus what a resident bf16 segmenter takes off free memory (2.5 GiB), would fall under the existing tight-VRAM floor (6 GiB or 20% of the card).
  • The memory reading is only taken once this process has initialised CUDA, so the status and download routes never create a CUDA context just to plan. Off CUDA, or when free memory cannot be read, auto stays bf16.
  • The download follows the same rule: a fresh install on a card with room fetches the bf16 model.safetensors.
  • A loaded segmenter reuses the reading taken when it was placed, so the free memory read with the model already on the card does not flip the choice and reload it on the next call.
  • If the only file on disk is an INT8 one (downloaded under the old default) and the card has room, it is dequantized to bf16 on load, which is the existing CPU / MPS path.
  • Explicit picks are unchanged: UNSLOTH_SAM3_PRECISION=int8 / fp8 load those files whatever the memory, and bf16 stays bf16.

Tests

studio/backend/tests/test_diffusion_segment_quant.py:

  • New test_auto_runs_bf16_while_the_card_has_room: auto on a CUDA card with room resolves to bf16, explicit int8 / fp8 are honoured with or without memory pressure. Fails on main (auto returns int8).
  • New test_auto_memory_tight_reads_the_card_only_once_cuda_is_up: the threshold on an 80 GB and a 12 GB card, no memory read before CUDA is initialised, unknown memory reads as roomy.
  • New test_a_loaded_segmenter_keeps_its_memory_reading: the reading is reused while loaded and cleared on unload.
  • Updated test_download_plan_fetches_one_weights_file, test_start_download_uses_the_plan, test_plan_weights, test_only_a_quantized_file_on_a_cpu_is_dequantized, test_status_reports_the_precision_and_its_size, test_auto_skips_a_file_that_failed_the_gate and the gated-order test for the new default (bf16 with room, the gated file when tight). These fail on main on their first bf16 assertion.
  • test_diffusion_segment_quant.py and test_diffusion_segment.py pass with CUDA hidden.

L4 and A100 re-measurement

Colab L4 (sm_89) and A100-SXM4-40GB (sm_80), torch 2.11, transformers 5.5.0, torchao 0.17.0, unsloth/sam3.1 at the pinned revision, MaskSegmenter.segment on one COCO image with 5 concepts. Each arm runs in a fresh process; 3 rounds, order reversed every round; each value is the median of 5 calls after 2 warmup calls.

Arm Precision L4 per Find A100 per Find Peak (allocated) Free drop after a Find (L4 / A100) Mask IoU vs bf16
main, auto INT8-ConvRot 1.42 / 1.44 / 1.45 s 1.13 / 1.13 / 1.14 s 1383 MiB 1672 / 1718 MiB 0.9994 / 0.9993
this PR, auto (room) bf16 0.46 / 0.46 / 0.46 s 0.33 / 0.32 / 0.33 s 1982 MiB 2188 / 2234 MiB 1.0
this PR, int8 INT8-ConvRot 1.43 / 1.43 / 1.45 s 1.15 / 1.14 / 1.11 s 1383 MiB 0.9994 / 0.9993
this PR, int8 + UNSLOTH_SAM3_INT8_CONVROT=0 INT8 1.41 / 1.42 / 1.42 s 1.10 / 1.11 / 1.10 s 1329 MiB 0.9991 / 0.9992
this PR, auto, only the INT8 file on disk bf16 (dequantized) 0.51 / 0.51 / 0.52 s 0.38 / 0.37 / 0.37 s 2006 MiB 2236 / 2282 MiB 0.9996
main and this PR, auto, tight card INT8-ConvRot same on both same on both 1396 MiB 0.9994 / 0.9993

So with room, auto is 3.1x (L4) to 3.5x (A100) faster per Find for about 0.6 GB more peak memory, and a tight card behaves exactly as on main.

The first version held back 2048 MiB for bf16, but mem_get_info drops by 2188 to 2282 MiB once bf16 has run a Find (allocator reserve and cuBLAS workspace on top of the 1982 MiB allocated peak). On a card with free memory just above the floor plus 2048 MiB, auto loaded bf16, _after_call read the card as tight and unloaded it, and the next call reloaded the INT8 file (reproduced on both cards: one wasted bf16 load). The allowance is now 2560 MiB; with it the same card picks INT8 directly, and a card with room still picks bf16 (both measured). test_a_bf16_pick_is_not_unloaded_as_tight_after_its_first_find fails at 2048 and passes at 2560.

Not covered

  • Not measured on sm_86 / sm_90 / sm_120 cards, and the timings above are on single Colab cards (not interleaved on one host).

UNSLOTH_SAM3_PRECISION=auto picked the INT8-ConvRot detector on every
sm_80+ card. It is more than 2x slower per Find than bf16 (0.77 s vs
0.35 s on the model card's B200 run) for about 0.65 GB less peak memory,
and the GPU device gate needs 4 GiB free either way.

auto now runs bf16 while the card has room and only picks the gated
8-bit file when free memory minus the bf16 segmenter would read as
tight. The download follows the same rule, the memory is read only once
CUDA is initialised, and a loaded segmenter keeps the reading taken at
load. Explicit int8 / fp8 / bf16 picks are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T06:16:11.049220Z e8fd82d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 956580e165

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

pre-commit-ci Bot and others added 2 commits October 25, 2026 05:38
auto read a card as roomy with 2048 MiB to spare, but a resident bf16
segmenter takes 2188-2282 MiB off mem_get_info after a Find (L4, A100),
so a card just inside the band loaded bf16, read tight after the call,
unloaded it and reloaded the INT8 file on the next call.

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: e8fd82dd74

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 9cd247d into main Oct 25, 2026
25 of 42 checks passed
@danielhanchen
danielhanchen deleted the fix/sam3-auto-bf16-when-roomy branch October 25, 2026 08:45
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