Skip to content

Studio: account for mmproj VRAM in GGUF fit budget (#5825) - #5849

Merged
Imagineer99 merged 11 commits into
unslothai:mainfrom
hoobnn:fix/5825-mmproj-vram-accounting
Jun 12, 2026
Merged

Imagineer99 merged 11 commits into
unslothai:mainfrom
hoobnn:fix/5825-mmproj-vram-accounting

Conversation

@hoobnn

@hoobnn hoobnn commented May 29, 2026

Copy link
Copy Markdown
Contributor

Problem

Vision GGUFs load the mmproj (vision projector) onto the GPU via --mmproj alongside the weights, but Studio's context auto-sizing / GPU-selection budget in LlamaCppBackend.load_model sizes off _get_gguf_size_bytes(model_path), which counts only the weight file(s). The projector is never added, so the budget is too optimistic: context gets mis-estimated and tight vision loads spill to system RAM / OOM. Reported in #5825 (e.g. a 20.3 GB model + BF16 mmproj on 12 GB VRAM, context silently dropped to 4096).

Change

  • Add LlamaCppBackend._mmproj_vram_bytes(launch_mmproj_path) returning the projector's on-disk size, or 0 when none resolves / the file is unreadable.
  • Resolve the launch projector once before GPU selection and fold its size into the fit budget (model_size = gguf_size + mmproj_size). The same resolved launch_mmproj_path / effective_is_vision now feed both the fit budget and the --mmproj launch flag, so they can't disagree (previously _resolve_launch_mmproj_path ran twice per load).
  • The summary log reports the projector size separately so the GGUF size: figure stays accurate.

Tests

  • New studio/backend/tests/test_mmproj_vram_accounting.py — no GPU / network / subprocess, cross-platform.
  • Verified locally on macOS (Python 3.12): the inference fit/log/mmproj suites pass (test_llama_cpp_context_fit, test_log_filter_no_truncation, test_detect_mmproj_file, test_kv_cache_estimation, …).

Scope / non-goals

This fixes the VRAM accounting so the budget is honest. It does not change whether a vision model auto-loads its mmproj (also raised in #5825) — that's a separate product decision, so this links rather than closes the issue.

Vision GGUFs load the mmproj projector onto the GPU via --mmproj
alongside the weights, but the context auto-sizing / GPU-selection
budget sized off _get_gguf_size_bytes(model_path), which counts only
the weight file(s). The projector was never added, so the budget was
too optimistic: context got mis-estimated and tight vision loads
spilled to system RAM / OOM'd.

Resolve the launch projector once before GPU selection and fold its
size into the fit budget. The same resolved path feeds both the budget
and the --mmproj launch flag, so the two cannot disagree. The summary
log now reports the projector size separately, keeping "GGUF size"
accurate.

Adds _mmproj_vram_bytes() + unit tests (no GPU / network / subprocess).
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves VRAM accounting for vision-capable GGUF models by factoring the vision projector (mmproj) size into the GPU fit budget, preventing context mis-estimation and potential OOM issues. It resolves the projector path once, adds its size to the total model size, updates logging to report the projector size, and includes unit tests for this new logic. A review comment suggests improving the readability of the updated logger statement by using a nested f-string instead of mixing explicit and implicit string concatenation.

Address review: the summary log mixed explicit `+` with implicit
f-string concatenation. Extract the optional projector fragment into
`mmproj_note` so the logger.info uses uniform implicit concatenation.
No behavioral change.

@Datta0 Datta0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does look good. A few examples of before and after measures for a couple of models as predicted by the functions and then sample runs would help speed upthe review. Can you please provide those?

@hoobnn

hoobnn commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

@Datta0 Hi

I tested this locally with a small Qwen3.5 vision GGUF.

Model: unsloth/Qwen3.5-0.8B-GGUF:UD-Q4_K_XL
Runtime: llama-server 9430 (d48a56eff) on Apple M4 Pro Metal

Measured local file sizes:

File Size
Qwen3.5-0.8B-UD-Q4_K_XL.gguf 558,772,480 bytes
mmproj-BF16.gguf 207,346,528 bytes

Using the parsed GGUF metadata, the KV estimate at ctx=131072 is 1,610,612,736 bytes.

For a constrained 2 GiB device budget, the estimator result changes as follows:

Case Requested ctx Fitted ctx
before: model weights only 131,072 111,616
after: model weights + mmproj 131,072 94,720

So in this configuration, accounting for the mmproj lowers the fitted context by 16,896 tokens.

I also ran the model through llama-server both ways.

Reproduction script
#!/usr/bin/env bash
set -euo pipefail

MODEL="unsloth/Qwen3.5-0.8B-GGUF:UD-Q4_K_XL"
OUT="${TMPDIR:-/tmp}/qwen35-mmproj-run"
mkdir -p "$OUT"

echo "llama-server version:"
llama-server --version

wait_for_server() {
  local port="$1"
  for _ in $(seq 1 120); do
    if curl -fsS "http://127.0.0.1:${port}/health" >/dev/null 2>&1; then
      return 0
    fi
    sleep 1
  done
  echo "server on port ${port} did not become healthy" >&2
  return 1
}

run_text_request() {
  local port="$1"
  curl -fsS "http://127.0.0.1:${port}/v1/chat/completions" \
    -H "Content-Type: application/json" \
    --data '{
      "messages": [{"role": "user", "content": "Reply with exactly: ok"}],
      "max_tokens": 8,
      "temperature": 0
    }'
}

run_image_request() {
  local port="$1"
  curl -fsS "http://127.0.0.1:${port}/v1/chat/completions" \
    -H "Content-Type: application/json" \
    --data '{
      "messages": [{
        "role": "user",
        "content": [
          {"type": "text", "text": "What is the dominant color of this image? Answer with one word."},
          {"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"}}
        ]
      }],
      "max_tokens": 16,
      "temperature": 0
    }'
}

# Baseline: no mmproj.
llama-server \
  -hf "$MODEL" \
  --no-mmproj \
  -c 131072 \
  -ngl all \
  --port 18080 \
  --host 127.0.0.1 \
  --no-ui \
  --log-file "$OUT/no-mmproj.log" \
  --reasoning off &
NO_MMPROJ_PID=$!

wait_for_server 18080
run_text_request 18080 > "$OUT/no-mmproj-response.json"
kill "$NO_MMPROJ_PID"
wait "$NO_MMPROJ_PID" || true

# With mmproj auto-enabled.
llama-server \
  -hf "$MODEL" \
  -c 131072 \
  -ngl all \
  --port 18081 \
  --host 127.0.0.1 \
  --no-ui \
  --log-file "$OUT/with-mmproj.log" \
  --reasoning off &
WITH_MMPROJ_PID=$!

wait_for_server 18081
run_text_request 18081 > "$OUT/with-mmproj-text-response.json"
run_image_request 18081 > "$OUT/with-mmproj-image-response.json"
kill "$WITH_MMPROJ_PID"
wait "$WITH_MMPROJ_PID" || true

echo
echo "Relevant log lines:"
grep -E "MTL0|estimated worst-case|loaded multimodal|new slot|prompt eval|image processed|model loaded|n_ctx_seq" \
  "$OUT/no-mmproj.log" "$OUT/with-mmproj.log"

Observed without mmproj:

  • model loaded successfully
  • 4 slots initialized with n_ctx = 131072
  • text request returned ok
  • prompt eval: 17 tokens in 334.87 ms, 50.77 tok/s
  • generation: 2 tokens in 46.03 ms, 43.45 tok/s

Observed with mmproj:

  • mmproj-BF16.gguf was loaded successfully
  • llama.cpp reported estimated worst-case mmproj memory usage: 383.97 MiB
  • 4 slots initialized with n_ctx = 131072
  • text request returned ok
  • a 1x1 PNG image request was processed successfully
  • image processing time: 179 ms
  • image prompt eval: 37 tokens in 368.82 ms, 100.32 tok/s
  • generation: 2 tokens in 8.69 ms, 230.20 tok/s

Note: on this machine the available Metal memory is ~38 GiB, so both real launches fit the requested 131072 context. The constrained 2 GiB calculation above is included to show the estimator behavior when the budget is tight enough for the mmproj footprint to affect the fitted context.

@hoobnn
hoobnn requested a review from Datta0 June 3, 2026 13:23
@Imagineer99 Imagineer99 self-assigned this Jun 12, 2026
@Imagineer99

Copy link
Copy Markdown
Member
image

CI passes where relevant, flaky tests fail not relevant to PR.

Tested and mmproj calculator works as expected, nice work.

@Imagineer99
Imagineer99 merged commit f033213 into unslothai:main Jun 12, 2026
1 check passed
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.

3 participants