Llama.cpp에서 Gemma 4 이미지 설정에 대한 간단한 메모
Source: Dev.to
Issue
--image-min-tokens 로 Gemma 4 를 로드할 때 다음과 같은 충돌이 발생했습니다:
[58175] srv process_chun: processing image...
[58175] encoding image slice...
[58175] image slice encoded in 7490 ms
[58175] decoding image batch 1/2, n_tokens_batch = 2048
[58175] /Users/socg/llama.cpp-b8639/src/llama-context.cpp:1597: GGML_ASSERT((cparams.causal_attn || cparams.n_ubatch >= n_tokens_all) && "non-causal attention requires n_ubatch >= n_tokens") failed
[58175] WARNING: Using native backtrace. Set GGML_BACKTRACE_LLDB for more info.
[58175] WARNING: GGML_BACKTRACE_LLDB may cause native MacOS Terminal.app to crash.
[58175] See: https://github.com/ggml-org/llama.cpp/pull/17869
[58175] 0 libggml-base.0.9.11.dylib 0x0000000103a6136c ggml_print_backtrace + 276
[58175] 1 libggml-base.0.9.11.dylib 0x0000000103a61558 ggml_abort + 156
[58175] 2 libllama.0.0.0.dylib 0x0000000103eacd70 _ZN13llama_context6decodeERK11llama_batch + 5484
[58175] 3 libllama.0.0.0.dylib 0x0000000103eb098c llama_decode + 20
[58175] 4 libmtmd.0.0.0.dylib 0x0000000103b8f7e8 mtmd_helper_decode_image_chunk + 948
[58175] 5 libmtmd.0.0.0.dylib 0x0000000103b8fea4 mtmd_helper_eval_chunk_single + 536
[58175] 6 llama-server 0x0000000102fb4d94 _ZNK13server_tokens13process_chunkEP13llama_contextP12mtmd_contextmiiRm + 256
[58175] 7 llama-server 0x0000000102fe3318 _ZN19server_context_impl12update_slotsEv + 8396
[58175] 8 llama-server 0x0000000102faaca0 _ZN12server_queue10start_loopEx + 504
[58175] 9 llama-server 0x0000000102f3a610 main + 14376
[58175] 10 dyld 0x00000001968edd54 start + 7184
srv operator(): http client error: Failed to read connection
srv log_server_r: done request: POST /v1/chat/completions 127.0.0.1 500
srv operator(): instance name=gemma-4-31B-it-UD-Q8_K_XL exited with status 1
이 어설션 실패는 ubatch 가 충분히 크게 설정되지 않았음을 나타냅니다.
Cause
Gemma 4의 비전 인코더는 이미지 토큰에 대해 비인과적(attention) 을 사용합니다.
비인과적 attention 은 모든 이미지 토큰이 하나의 ubatch 안에 들어가야 (n_ubatch >= n_tokens_all) 합니다.
기본 ubatch 크기는 512인데, 나는 2048 이미지 토큰을 요청했기 때문에 조건이 충족되지 않았습니다.
Gemma 4 visual token budgets
Gemma 4는 여러 사전 정의된 시각 토큰 예산을 지원합니다:
| Token budget | Typical use cases |
|---|---|
| 70 / 140 | 분류, 캡션 작성, 빠른 비디오 이해 |
| 280 / 560 | 일반 멀티모달 채팅, 차트, 화면, UI 추론 |
| 1120 | OCR, 문서 파싱, 손글씨, 작은 텍스트 |
최대 예산은 1120 토큰입니다.
Fix
--image-min-tokens와--image-max-tokens를 최대 예산(1120)으로 설정합니다.--ubatch-size와--batch-size를 토큰 예산보다 최소한 크게(예: 2048) 설정하여 서버에 충분한 여유를 줍니다.
./llama-server \
-ngl 200 \
--ctx-size 65535 \
--models-dir /Users/socg/models \
--models-max 1 \
--port 5001 \
--host 0.0.0.0 \
--jinja \
--image-min-tokens 1120 \
--image-max-tokens 1120 \
--ubatch-size 2048 \
--batch-size 2048