Skip to content

mtmd: reserve embedding storage before populating batches - #33171

Closed
Smartens-gaowz wants to merge 1 commit into
ggml-org:masterfrom
Smartens-gaowz:mtmd-embedding-reserve-v2
Closed

Smartens-gaowz wants to merge 1 commit into
ggml-org:masterfrom
Smartens-gaowz:mtmd-embedding-reserve-v2

Conversation

@Smartens-gaowz

Copy link
Copy Markdown

Overview

decod_embedd_batch:: render() append embeddings line by line. When the capacity of the std:: vector on the CPU side is insufficient,
It reallocates and moves the already written data. Before adding embeddings line by line, reserve capacity based on the number of rows and the width of each row,
Then use the original loop to fill in the embedding, positioning, and output marker fields.

Additional information

On the same NVIDIA GeForce RTX 4090 device, benchmark the baseline commit 07ba74b (A) and PR build (B) using the same input for 11 Qwen3-VL-8B workloads.
The test execution sequence is A->B->B->A. Each step starts with a warm-up request, followed by three formal requests. The result is calculated by taking the average of the three formal request times.
The average time to first token issuance (TTFT) decreased by 1.78% to 10.84%.

Workload Mean TTFT A -> B (ms) TTFT change B -> A (ms) Mean E2E A -> B (ms)
512 * 512 dashboard 91.34 -> 87.74 -3.93% 399.45 -> 396.21
1024 * 1024 dashboard 327.29 -> 315.74 -3.53% 640.35 -> 628.38
1536 * 1536 dashboard 800.08 -> 769.43 -3.83% 1,118.77 -> 1,088.39
1024 * 1024 service table 338.41 -> 326.99 -3.37% 651.43 -> 639.32
768 * 1536 vehicle inspection report 415.80 -> 370.73 -10.84% 728.48 -> 683.32
1536 * 640 warehouse process diagram 305.48 -> 285.89 -6.42% 619.35 -> 599.05
512 * 600 portrait photograph 121.12 -> 112.82 -6.85% 429.13 -> 421.18
640 * 488 newspaper scan 126.86 -> 113.40 -10.61% 435.25 -> 421.31
2875 * 1500 matrix multiplication diagram 1,600.81 -> 1,572.31 -1.78% 1,929.06 -> 1,899.89
1024 * 1024+1024 * 1024 Dashboard and service table (two images) 638.13 -> 616.84 -3.34% 955.96 -> 934.69
1536 * 640+768 * 1536 Warehouse diagram and inspection report (two images) 683.36 -> 639.39 -6.43% 1,001.30 -> 957.30

Output consistency

Model Cases A/B output comparison
Qwen3-ASR-0.6B 4 The Chinese and English results match perfectly.
Qwen3-TTS-1.7B 2 Short voice and long voice have the same byte output.
SmolVLM2-500M 3 Test the same image on both CPU and CUDA, and test a video on CUDA. In each use case, the 32 Token IDs generated before and after optimization are consistent one by one.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES
    Codex helped implement capacity reservation for MTMD embedding batches and revised the patch based on my feedback. I have reviewed the patch.
    Codex checked and analyzed the test results,I personally verified the test results.

@Smartens-gaowz
Smartens-gaowz requested review from a team as code owners November 27, 2026 05:32
@github-actions github-actions Bot added the mtmd Related to multimodal functionality (video/image/audio) label Nov 27, 2026
@Smartens-gaowz

Copy link
Copy Markdown
Author

Hello @ngxson , this PR reserves storage capacity in advance for the embedding batch of mtmd, reducing the need for expansion and data migration during the padding process. I have added performance comparisons, as well as regression verification results for Qwen3-ASR, Qwen3-TTS, and SmolVLM2. Currently, the CPU CI shows it is awaiting approval from the maintainer. Could you please approve it at your convenience and review this modification? If additional testing or code adjustments are needed, I will continue to assist. Thank you!

@ngxson

ngxson commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

I'd suggest closing this for now, it's better to fix the root cause: do not pass the embd via host any more, so no need to alloc the batch to start with.

There will be multiple PRs to make it work, but we're getting there, see: #33377

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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants