Repository navigation
Studio: Mask from text runs SAM 3.1 in bf16 unless the card is short of memory - #14736
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[skip ci]
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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]
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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:Re-measured through
MaskSegmenter.segmenton one shared B200 (same image, 5 concepts, rounds interleaved as bf16 / INT8-ConvRot / bf16, each round the median of 5 calls, 3 rounds):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
autoruns bf16 while the card has room for it. It picks a gated pre-quantized file (INT8-ConvRot, or plain INT8 withUNSLOTH_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).model.safetensors.UNSLOTH_SAM3_PRECISION=int8/fp8load those files whatever the memory, andbf16stays bf16.Tests
studio/backend/tests/test_diffusion_segment_quant.py: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).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.test_a_loaded_segmenter_keeps_its_memory_reading: the reading is reused while loaded and cleared on unload.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_gateand 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.pyandtest_diffusion_segment.pypass 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.segmenton 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.UNSLOTH_SAM3_INT8_CONVROT=0So 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_infodrops 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_callread 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_findfails at 2048 and passes at 2560.Not covered