Skip to content

serve, vision, gpu-gates, tools: EmbeddingGemma 2 sampled video frames through the embedding endpoints, checked against llama-server - #397

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-video-serve
Oct 17, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-video-serve

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Part of #59 (plan and PR order in #190), item V3: bloomery-serve embeds a video clip that the client has already sampled at one frame per second, and a serve gate checks the served vectors against llama.cpp. It builds on V1 (#374) and V2 (#383) and has one commit.

What and why

Release notes: Added: EmbeddingGemma 2 embeds sampled video. Send a Video: text part followed by 1 to 32 one-FPS image_url frames to /v1/embeddings.

V1 ran ordered frames through the Gemma4 vision tower, and V2 picked those frames from a decoded clip. Neither was reachable over HTTP. This PR connects them to the embedding endpoints without a new request format.

The input is the request that V2's reference collector already sends to llama-server: a content array whose first part is a text part with the exact text Video:, followed by the sampled frames as image_url parts holding base64 PNG or JPEG data URLs (tools/ref/emb2/server_vectors.py:286-295). Mainline llama-server takes the same request through tokenize_oai_content_array (server-context.cpp:6059-6065). One request therefore works on both servers; the reference collector and serve gate use that request.

  • Video preparation (crates/vision/src/{media.rs,arch/gemma4v/media.rs}). MediaModel gains embedding_video, which names the text prefix and the frame limit, and prepare_video. The trait's default refuses by name. Gemma4's implementation returns Video: and its 32-frame limit, and prepares the already sampled frames through V2's prepare_video. Every supplied frame is kept in order and gets the video token budget instead of the image budget. If its prepared canvas exceeds the loaded projector's --embd-image-max-tokens capacity, preparation refuses it before GPU dispatch. The video rule does not enlarge the tower or its named reserve.
  • Serving (crates/serve/src/{media.rs,embeddings.rs}). When the first part is the model's prefix, embeddings::input sends the remaining parts to the crate-private media::prepare_video. That function checks the frame count before it decodes anything. It loads each file through the existing data URL loader and image keys, then decodes, prepares and drops its full-resolution RGB frame before loading the next file. Image and video inputs share this lifetime through the private prepare_with helper. Only prepared feeds accumulate between frames. The existing assemble still places the delimiters, the spans and one BOS/EOS pair, and checks the context bound. Image requests without the prefix keep the image budget. Text after the prefix, an empty frame list, a 33rd frame and a projector without a video rule fail by name.
  • Encoded video is refused. An input_video or video_url part now returns a named 400 error that says to send decoded one-FPS image_url frames after a Video: text part. The section "Out of scope" below explains why.
  • The gate (crates/gpu-gates/src/bin/gate_gemma_embedding2_serve.rs). A new --video-clips <clips.json> mode on the existing serve gate reads the manifest and reference pairs that V2's video_oracle.py writes. Before starting the server, it checks the loaded projector's SHA256 against the reference set's pinned value. It checks each frame's path and SHA256 against both reference sets before sending the content array through the real HTTP server, and compares prompt positions and the raw and normalized vectors with the unchanged bands derived from the CPU twin. It launches the server with an image minimum of 1 token while video preparation keeps its fixed minimum of 70, so image fallback cannot preserve the oracle prompt counts. It then checks that both encoded part types get the 400 refusal and the diagnostic that asks for decoded one-FPS frames. The mode reuses the gate's server launch and stop, HTTP client, reference reader and band rule. The justfile recipe now forwards extra arguments to the gate. The existing --v1 and default text modes remain in the same gate, sharing the launch and comparison helpers. Both text modes check fixture lengths and digests through the existing tokenizer-set reader before starting a plan or server. Combining --v1 with --video-clips is refused because v1 is text-only.
  • Documentation (tools/ref/emb2/README.md). A new section gives the request shape, the source citations, the gate command and the out-of-scope note.

Code shape

  • No second copy. URL parsing, data URL loading, image keys, frame sampling, assembly, spans, context checks, reference reading, band rules and the server lifetime all use their existing owners. prepare_video is the one place that turns sampled frames into feeds.
  • Model code holds only architecture facts. The Gemma4 files add the Video: prefix, the frame limit, the call to the existing prepare_video and a check against the loaded projector's token capacity. The common MediaModel trait gained a capability with a refusing default; no common owner gained a per-model branch.
  • Dead code is gone. Each new function and mode has a production caller.
  • No silent failure. Malformed content, a wrong frame count, a decode error, an encoded video part, a frame larger than the loaded tower, a context overflow and a reference mismatch fail by name. The frame count is checked before decoding.
  • Comments state what is true now.
  • No relaxed gate. Bands, tolerances and pins are unchanged, and no unsafe block was added.
  • cargo clippy and the security scan were not run. The full contrib-check --tier t0 run includes formatting, pure-crate tests and comment checks.

Duplication found or removed: image and video decoding share crates/serve/src/media.rs:464, the private prepare_with helper. Their model-specific preparation rules remain separate.

What I ran

The full CPU suites below use 4ecd4197, based on ce96affc. File-identity and HTTP-routing regression checks use d6ce8fb5 on that base. After rebasing onto 416e4f3f, the rebuilt serve gate runs all three modes at 681d034b on the DGX Spark and d80e036d on the RTX 5090. Those two heads differ only in README wording. The oracle comparison tables retain the measured results at 4ecd4197: frame preparation and inference math are unchanged.

Check Result
FAIL-first: focused serve test on test-only commit d35786a0a rc 101 at the assertion requiring the first frame's capacity error before decoding a later truncated JPEG
cargo build -p bloomery-serve --bin bloomery-serve, then cargo test -p bloomery-serve --lib (Linux x86_64) 234 passed; 0 failed
cargo test -p bloomery-vision --lib (Linux x86_64) 152 passed; 0 failed
Focused serve integration test at d80e036d 1 passed; 0 failed
cargo test -p bloomery-vision --lib video at d80e036d 1 passed; 0 failed
python3 tools/recipes.py --self-test 183 gate recipes; 0 failures
bash tools/check-recipes.sh (Linux x86_64) Recipe derivation and architecture ownership pass. Two unrelated signal self-tests failed with inherited SIGHUP ignored; both pass after restoring the default signal disposition.
just contrib-check --tier t0 Not run on this head yet.

The integration test checks both sides of the loaded-capacity boundary: a 2x1 frame takes 72 rows and fails at capacities 4, 70 and 71, then passes at 72, 73 and 140. A 576x576 frame takes 121 rows and fails at 120, then passes at 121 and 122. It also covers two distinct frames' pixels, spans and corresponding image keys, BOS/EOS, the exact context limit, 1, 32 and 33 frames, an empty or malformed video, a missing projector and both named encoded-part diagnostics.

The regression input puts a valid PNG before a truncated JPEG. With a projector too small for the first frame, preparation must return its capacity error before decoding the JPEG. The all-frames-first implementation returns the later decode error and fails the assertion. The fixed implementation returns the first frame's error.

The file-identity regression uses a copy of the pinned projector with one byte appended after its tensors. At d6ce8fb5, removing the new digest guard lets all video comparisons pass with that copy. Restoring the guard rejects it with video projector sha256 differs from the oracle before the server starts. Changing one byte in a text fixture, without changing its length, also fails before a plan or server starts in both text modes. Appending a byte to a copied PNG also fails its frame identity check; restoring the bytes makes the video gate pass.

Disabling embeddings::input's video selection makes the HTTP gate fail: its first clip reports frames 1 positions 22 instead of the oracle's 87 positions. The gate keeps --embd-image-min-tokens 1 for this check. Restoring video selection makes all six vector comparisons and both named refusals pass.

Not run here

The other pure-crate suites, contrib-check --tier t0 and the separate HTTP capacity and large-frame sweep have not been rerun on this head.

Serve gate on a DGX Spark and an RTX 5090

python3 tools/ref/emb2/video_oracle.py target/video-clips --oracle "$LCPP" --port 18097
cargo oxide build --arch "$ARCH" -- --release -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,gemma-embedding2 --bin bloomery-serve --bin gate_gemma_embedding2_serve
BLOOMERY_MODEL=qwen3moe BLOOMERY_BOX_LOCAL=1 bash tools/box.sh 'bash tools/gpu-gate.sh gate_gemma_embedding2_serve --dir target/video-serve --video-clips target/video-clips/clips.json'

Set ARCH=sm_121 on the DGX Spark and ARCH=sm_86 on the RTX 5090. The comparison tables retain the three CUDA and CPU-twin reference pairs collected on each card at 56c32865 and compared at 4ecd4197. Their inputs, preparation rules and inference math are unchanged. The rebased runs rebuild the gate and run its existing --v1, default text and video modes on both cards. On the DGX Spark used for these runs, the pinned oracle pairs were prepared locally before the gate because the earlier assets were not present. The RTX 5090 reuses its retained pairs.

DGX Spark:

Check Result
Release build of bloomery-serve and gate_gemma_embedding2_serve PASS (cargo oxide build --arch sm_121)
input_video part check input_video_refused: PASS
video_url part check video_url_refused: PASS
Existing --v1 mode 122 clauses PASS (exit 0)
Default text mode All clauses PASS (exit 0)
--video-clips mode 8 clauses PASS (exit 0)

RTX 5090:

Check Result
Release build of bloomery-serve and gate_gemma_embedding2_serve PASS (cargo oxide build --arch sm_86)
input_video part check input_video_refused: PASS
video_url part check video_url_refused: PASS
Existing --v1 mode 122 clauses PASS (exit 0)
Default text mode All clauses PASS (exit 0)
--video-clips mode 8 clauses PASS (exit 0)

The gate kills its own server when it finishes, so the log line video server stopped signal: 9 (SIGKILL) is expected.

Comparison with the reference

The reference is mainline llama.cpp 6868a52cb337450658006ad2f9281c2eb63f1666, llama-server /v1/embeddings, built with CUDA on each card. Each comparison uses the references collected on that same card. The model is embeddinggemma-2-Q8_0.gguf (SHA256 2188ac1deca4b77dffefd603c2776a9d76d9d74ec01841392982ebb840b09135) with the BF16 projector mmproj-embeddinggemma-2-BF16.gguf (SHA256 d2033b3cd0223cfb2e7a2b50dc80a664c78c816d375f5b017ec3ef80ee0bc766). The flags are --embeddings --pooling mean -c 8192 -b 8192 -ub 8192 -np 1 -t 8 --image-max-tokens 140 --video-fps 1 --video-timestamp-interval 0, with -ngl 99 for CUDA and -ngl 0 for the CPU twin. Both servers and Bloomery got the same content array for each clip.

"raw" is the pooled vector and "embd" the L2-normalized one. Each band comes from the distance between the CUDA and CPU-twin references, by the gate's existing rule.

DGX Spark

Clip Vector Frames Positions Cosine / band Max absolute / band Result
clip_1s24 raw 1 87 1.074645e-4 / 2.162896e-3 9.657869e-1 / 1.636657e1 PASS
clip_1s24 embd 1 87 1.074645e-4 / 2.162896e-3 1.889179e-3 / 6.577075e-2 PASS
clip_7s30 raw 7 557 7.575400e-5 / 1.606068e-3 6.780109e-1 / 1.164258e1 PASS
clip_7s30 embd 7 557 7.575399e-5 / 1.606068e-3 1.643596e-3 / 5.667571e-2 PASS
clip_16s24 raw 16 1268 3.320901e-5 / 8.349632e-4 3.735242e-1 / 8.123197e0 PASS
clip_16s24 embd 16 1268 3.320902e-5 / 8.349632e-4 8.998923e-4 / 4.086473e-2 PASS

[derived] DGX Spark minimum cosine-band margin across six comparisons: 8.01754180e-04. No band changed.

RTX 5090

Clip Vector Frames Positions Cosine / band Max absolute / band Result
clip_1s24 raw 1 87 1.195576e-4 / 3.057292e-3 1.070839e0 / 1.941612e1 PASS
clip_1s24 embd 1 87 1.195577e-4 / 3.057292e-3 1.830898e-3 / 7.819580e-2 PASS
clip_7s30 raw 7 557 7.190838e-5 / 1.531162e-3 6.854248e-1 / 1.138409e1 PASS
clip_7s30 embd 7 557 7.190836e-5 / 1.531162e-3 1.575245e-3 / 5.533826e-2 PASS
clip_16s24 raw 16 1268 3.045011e-5 / 8.241305e-4 4.471054e-1 / 8.092833e0 PASS
clip_16s24 embd 16 1268 3.045012e-5 / 8.241305e-4 8.628108e-4 / 4.059878e-2 PASS

[derived] RTX 5090 minimum cosine-band margin across six comparisons: 7.93680380e-04. No band changed.

The frame and position counts in the table are also the ones both reference sets record for llama-server; the gate requires that match. The PR makes no speed claim.

Out of scope

Encoded video parts (input_video and video_url holding an MP4 or other container) get the named 400 error. Mainline llama-server accepts them with a data or url field (server-common.cpp:1228-1242) and decodes them by starting an FFmpeg subprocess (mtmd_helper_video::start_ffmpeg in tools/mtmd/mtmd-helper.cpp). Bloomery does not install or start a decoder. Does the maintainer want an FFmpeg subprocess on the server path for these parts? Until that is decided, this PR accepts sampled frames only and refuses encoded parts.

Hardware (if the change is for a machine)

DGX Spark: NVIDIA GB10, 595.84, CUDA 13.3.1 in the build container. The gate was built for sm_121.

RTX 5090: NVIDIA GeForce RTX 5090, 595.99.02, 32607 MiB, 400 W cap. The gate was built for sm_86.

What I ran into

None of this blocks the PR.

  • The server does not resample. Each supplied frame stands for one second, and the README says to prepare the frames with gemma4v::prepare_clip.
  • The served path was compared on three synthetic constant-rate clips. As in V2, variable-rate input and decoding are not covered.
  • The gate covers both refusals over HTTP. It does not send a real encoded video, because the refusal does not depend on the payload.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added documentation Improvements or additions to documentation gpu The card engine: crates/gpu*, crates/app host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset needs-gpu Changes code a card must check, and carries no gate run on a card serve The HTTP server: crates/serve tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 17, 2026

@github-actions github-actions 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.

Verdict: CLEAN: No must-fix defect was found in video routing, frame preparation, capacity checks, or gate comparisons.

Checked

Duplicates: 7 new functions/methods checked with Grep; no duplicate requiring extraction. Dryer reported 0 pairs. Existing preparation, assembly, HTTP, reference-reader, and server-lifetime owners are reused.

Complexity: videos (new, 37) has manifest validation, vector requests/comparisons, and refusal stages. run (75 → 87) adds mode branches; input (12 → 17) adds video selection; the input-contract test (3 → 12) adds boundary cases. No correctness defect found in these branches.

Silent failure: 1 new routing fallback followed. A failed prefix parse is parsed again by the ordered input iterator and reaches the client as a named 400 error.

Tests, static mutation check: changing > to >= at crates/vision/src/arch/gemma4v/media.rs:145 fails the exact-capacity assertions. Decoding all frames before preparation at crates/serve/src/media.rs:497 fails the first-frame-error assertion.

Tests, static mutation check: removing the encoded-video arm at crates/serve/src/media.rs:372 fails both changed tests' named-refusal assertions. Disabling video selection at crates/serve/src/embeddings.rs:291 fails prepared-frame assertions and the new HTTP gate's prompt-count check; the PR reports this FAIL-first run.

Gates: no bands or pins moved. Empty/33-frame inputs, exact context limits, capacity boundaries, distinct frame order, and encoded-part refusals are covered.

Adversarial: malformed later frames preserve the first preparation error; concurrent inference uses the existing model mutex; gate errors retain server cleanup through Served.

Escape catalog: no C1–C4 pattern matched.

Notes

Dryer did not search crates/gpu-gates; its additions were checked manually. Validation was static; no builds or runtime tests were run. The PR body reports GPU results.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemma_embedding2_serve.rs and carries a gate run on the author's card, which counts as verified.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@github-actions github-actions Bot added gpu-evidence Verified on the author's card: the pull request carries the gate run and its output and removed needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 17, 2026
@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit fa450a7 into midagedev:main Oct 17, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation gpu The card engine: crates/gpu*, crates/app gpu-evidence Verified on the author's card: the pull request carries the gate run and its output host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset serve The HTTP server: crates/serve tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant