Skip to content

mtmd : support CPU_REPACK extra buffer types for encoder weights - #28563

Open
Alcpz wants to merge 19 commits into
ggml-org:masterfrom
Alcpz:Alcpz/mtmd-cpu-extra-bufts
Open

Alcpz wants to merge 19 commits into
ggml-org:masterfrom
Alcpz:Alcpz/mtmd-cpu-extra-bufts

Conversation

@Alcpz

@Alcpz Alcpz commented Sep 7, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Profiling of mtmd-cli showed that the CPU path used the non-repack codepath. This is caused by the CLIP context using default buffers (aka, not considering extra buffers), so this faster available codepath is never used.

This PR introduces a mechanism to create the encoder graph using extra buffers, which improves the performance of the encoders when they are run on CPU.

Caveats:

  • DeepSeek-OCR/OCR2 are excluded from repack selection since they merge image tiles into the batch dimension at encode time. CPU_REPACK kernels don't support 4D activations at this moment. Not sure if the proper fix is the backend or with a similar solution to model : allow reshape of tensors during load #26531.
  • I am limiting the implementation to the extra buffer type I could validate (CPU_REPACK). KleidiAI shows a numerical drift, so I am not confident for adding general support.

Additional information

More details into what was modified:

  • A dummy graph is created to distinguish "Repackable" tensors. I've seen that CLIP uses external llama APIs to avoid the llama internal loader dependency.
  • Weights are now allocated in one buffer per buffer type. Tensors placed in an extra buffer type are repacked by its tensor set API.
  • If extra buffer types fail, it falls back to the default buffer type (i.e. the current behavior).
  • Reuses the existing --no-repack flag for MTMD tools. use_extra_bufts added for this purpose. Defaults to ON (Repacks by default). Changes the default behavior for CPU users if they update.
  • Audio generation is not scoped in this PR.

Performance comparison:

Model (mmproj) Current CPU_REPACK
LFM2.5-Audio-1.5B Q8_0 160 ms 110 ms
SmolVLM-500M-Instruct Q8_0 355 ms 172 ms

SmolVLM encodes each image as two batches, showing per-batch average.

# Apple M4 Max CPU backend (ARM i8mm codepath)
# Audio
./build/bin/llama-mtmd-cli -m <model> --mmproj <mmproj> --mmproj-device none --audio tools/mtmd/test-2.mp3 -p "a" -n 1 -t 8 -v [--no-repack]
# Vision
./build/bin/llama-mtmd-cli -m <model> --mmproj <mmproj> --mmproj-device none --image tools/mtmd/test-1.jpeg  -p "a" -n 1 -t 8 -v [--no-repack]

Output Parity:

Embeddings dumped with

MTMD_DEBUG_EMBEDDINGS=/tmp/[no-]repack.bin ./build/bin/llama-mtmd-debug -p encode \
    -m SmolVLM-500M-Instruct-Q8_0.gguf --mmproj mmproj-SmolVLM-500M-Instruct-Q8_0.gguf \
    --mmproj-device none --image cb -n 512 [--no-repack]
 
md5sum /tmp/repack.bin /tmp/no-repack.bin
a06dbead1bb8c087558c796ec5cc36f8  /tmp/repack.bin
a06dbead1bb8c087558c796ec5cc36f8  /tmp/no-repack.bin

Requirements

@Alcpz
Alcpz requested review from a team and ggerganov as code owners September 7, 2026 15:48
@Alcpz Alcpz changed the title mtmd: support CPU_REPACK extra buffer types for encoder weights mtmd : support CPU_REPACK extra buffer types for encoder weights Sep 7, 2026
@github-actions github-actions Bot added server mtmd Related to multimodal functionality (video/image/audio) labels Sep 7, 2026
@Alcpz
Alcpz force-pushed the Alcpz/mtmd-cpu-extra-bufts branch from f787533 to 5d75370 Compare September 8, 2026 08:39
@Alcpz

Alcpz commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

@tdakhran

@Alcpz
Alcpz force-pushed the Alcpz/mtmd-cpu-extra-bufts branch from 5d75370 to 0427d4d Compare September 10, 2026 16:03
@Alcpz
Alcpz force-pushed the Alcpz/mtmd-cpu-extra-bufts branch from 0427d4d to ea68987 Compare September 11, 2026 09:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mtmd Related to multimodal functionality (video/image/audio) server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant