Skip to content

vision: gemma4a prepares decoded PCM as chunked mel and counts its tower tokens - #331

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-audio
Oct 16, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-emb2-audio

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Part of #59 (plan and PR order in #190), E9: the host side of audio input for EmbeddingGemma 2. It turns decoded mono PCM into the log-mel input of the Gemma4 audio tower and counts the tower's output tokens. E10 (the audio tower) and E11 (serving audio) follow. This PR loads no weights, decodes no audio file and serves nothing.

What and why

Release notes: internal: first step of EmbeddingGemma 2 audio input, host-side mel preprocessing that the audio tower will read.

EmbeddingGemma 2 embeds audio through the Gemma4 audio tower, and mainline llama.cpp prepares that tower's input in mtmd-audio.cpp: 16 kHz mono PCM becomes a log-mel spectrogram in chunks of at most 30 seconds, and each chunk's frame count fixes how many tokens the tower returns. The tower cannot start before this input matches mainline's bit for bit, so this PR does that part first and checks it alone, on the host, with no GPU and no model file.

What changes

  • Preprocessing (crates/vision/src/arch/gemma4a.rs). Mainline's periodic Hann window, recursive FFT, HTK mel filters, magnitude (not power) spectrum and logarithmic floor, with the filter sums grouped in fours of f32 products and accumulated in f64 as the reference does. Each 30 second chunk gets its own left padding, so a long input is not cut at 30 seconds. The chunk's token count comes from its frame count.
  • Refusals. Empty PCM, a last chunk of one sample, a NaN or an infinity, and an arithmetic overflow in the spectrum are named errors (crates/vision/src/lib.rs). A chunk needs at least two samples, which follows the reference's signed division and FFT padding rule.
  • Reference and gate. tools/ref/emb2/dump_mel.cpp links the pinned, unmodified mtmd-audio.cpp (llama.cpp 6868a52cb337450658006ad2f9281c2eb63f1666) and supplies only the Gemma4 audio parameters. It writes a synthetic PCM set that refset reads through its existing manifest. just dump-ref-emb2-mel builds the set and just gate-emb2-mel compares every mel bit and every token count. A set that is already there, or a dangling symlink at its path, is refused instead of overwritten.
  • Registration. tools/gate-paths.tsv names the new gate beside the other EmbeddingGemma 2 rows and gives the gemma4a files a row of their own.

What this PR does not do

  • No audio tower, no GPU kernel and no change to the side seat's reserve count (E10).
  • No input_audio in /v1/embeddings, no audio decoder and no resampler (E11).
  • No other model's preprocessing changes. The synthetic PCM set is not described as coming from a checkpoint.

Code shape

  • No second copy. The mel, FFT and filter code has no earlier owner in crates/ or tools/. The reference harness links mainline's source and does not copy it. The set reader, the manifest check and the C++ set writer are the existing ones.
  • Model code holds only architecture facts. The sample rate, window, hop, chunk length and mel count live in gemma4a.rs. No common owner gained a branch for this model.
  • Dead code is gone. Nothing I made unused is left behind.
  • No silent failure. Bad PCM and spectral overflow are named errors, raised before any allocation grows with the input.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved. The new gate requires zero mel bit mismatches.
  • cargo fmt is clean on what I touched, and the lint count equals the base's (under "What I ran").

Duplication found or removed: none.

What I ran

Development runs used a DGX Spark in the project's dev container (bloomery-dev:cuda13.3.1), Linux aarch64, with no GPU work in these checks. The last commit changed only two iterator calls in crates/vision/src/arch/gemma4a.rs (a lint fix), so the rows from the earlier head c0505ea36623aabda22d0b0f24557f0ea2e10c38 name that head. Everything else in the commit is the same on both.

Check Result
lint step of ./tools/mac-static.sh (Mac), head 86bcae5b 59 warnings, above the ratchet's 41, from the same files as the base's 59
cargo fmt --all -- --check and cargo test --release -p bloomery-vision -p bloomery-refset --lib, head c0505ea3 rc 0, vision 148 passed
the same unit tests on 86bcae5b rc 0: vision 148 passed, refset 221 passed
just gate-emb2-mel, head c0505ea3 sources rc 0, vision 148 passed and the reference gate 1 passed
reference set from the pinned source 13 inputs, 14 chunks, 41 rows
cargo fmt --all -- --check, pre-commit sources rc 0
python3 tools/recipes.py check rc 0, 133 gate rows

The head 4edded70 differs from 86bcae5b in two places. tools/ref/emb2/mel.sh now publishes with mv -Tn and stops when the destination appeared during the dump. The unit test keeps only the PCM rejections (2026-10-16: removed duplicate successful-input assertions; hw_mel_matches_mainline retains their coverage, bit for bit against mainline on all 13 cases). At 4edded70 the vision and refset unit tests ran again on a DGX Spark: vision 148 passed, refset 221 passed.

The lint warnings are the base's: the same 59 appear on main without this PR, and the comparison of the warning lists found them identical. I did not look for their cause.

The reference gate compared all 13 inputs and the second chunk of the longest one, and every mel value matched. A 128 value, 1 token minimum came out bit identical, and so did a 30 second input of 383872 mel values and 750 tokens. The second chunk of the longest input, 256 values and 1 token, also matched. Edge cases covered: two samples (the minimum), 159 to 161 samples around the window, 320 and 321 samples around the frame count, 800 and 801 samples around a token boundary, and 479999, 480000 and 480321 samples around 30 seconds.

FAIL-first: I planted nine edits in the development sources, one at a time, each in the code a test guards: the log floor, the Hann window, the filter scale, magnitude versus power, the FFT twiddle sign, the chunk length, the token count, the overflow guard and the reference build identity. Each failed its test with rc 101 (the log floor edit fails with 127 mismatched bits at minimum/0), and the restored file passed again.

Existing sets and a dangling symlink at the reference path are refused with rc 2, as intended.

Not run: just affected, just contrib-check, t1, and any model or GPU check, because this PR has no model or kernel code to reach them. The bit match is shown on Linux aarch64 only; another libc, CPU or float contraction setting could differ from the reference, so I do not claim more.

What I ran into

None of this blocks the PR.

  • just gate-emb2-mel needs its reference set in place. The gate reads ref_gemma_embedding2_mel from the standard refset folder. On a node that does not have it, the gate stops at a missing MANIFEST.tsv. Run just dump-ref-emb2-mel first. The development result above used a set built in the work folder.
  • The container needed three environment settings. Git did not trust the repository's owner, a reference library linked against CUDA needed the driver library, and the dumper needed the build's library path. I set a per-path safe.directory, gave only the reference build the GPU runtime and set LD_LIBRARY_PATH for the dumper. No comparison was loosened.
  • Lint count is the base's. See "What I ran".

Hardware (if the change is for a machine)

DGX Spark: NVIDIA GB10, driver 595.84, Linux aarch64. The checks above ran on the CPU of that machine, and no GPU kernel is involved.

AI help

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

…wer tokens

Match mainline's periodic Hann, recursive FFT, HTK magnitude filters and logarithmic floor. Keep chunk padding independent and refuse malformed PCM and spectral overflow by name.

The host oracle links the pinned unmodified mtmd audio source without loading a model. Its synthetic PCM family uses the existing manifest reader and set writer. The host gate compares mel bits and tower token counts at frame and chunk boundaries.
@github-actions github-actions Bot added host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 16, 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 reachable failure or proven quality defect was found in the changed code.

Checked

Duplicates: no existing mel or FFT owner found in crates/ or tools/. Dryer reported no pairs.
Complexity: crapper flagged no functions.
Silent failure: five clamp/default sites followed. Filter denominators are fixed and positive; frame saturation matches signed reference division; padding and the log floor are defined. Invalid PCM and spectral overflow return named errors.
Tests (static mutation audit): removing the length or overflow rejection at crates/vision/src/arch/gemma4a.rs:83 and line 113 makes rejects_invalid_pcm fail.
Tests (static mutation audit): changing the log floor at crates/vision/src/arch/gemma4a.rs:128 makes hw_mel_matches_mainline fail its bit comparison.
Tests (static mutation audit): dropping the architecture or build pin at crates/refset/src/arch/gemma_embedding2/mod.rs:179 makes audio_mel_requires_its_pcm_identity_architecture_build_and_trailer fail.
Gates: no existing pin, threshold or coverage removed. The new bit comparison has FAIL-first evidence in the PR body.
Escape catalog: no C1–C4 pattern matched. Shell syntax validation passed.
Adversarial checks: all 479,999 allowed chunk lengths match signed frame arithmetic and token rounding. Recursive FFT indices stay within scratch bounds. Scratch is local to each call, preventing sharing between concurrent calls.

Notes

Cargo tests and the reference gate were not rerun in this review. The PR reports CPU gate results on Linux aarch64.

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

@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 98a513f into midagedev:main Oct 16, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset 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