Skip to content

Asr - #111

Draft
aviv1ron1 wants to merge 19 commits into
mainfrom
asr
Draft

Asr#111
aviv1ron1 wants to merge 19 commits into
mainfrom
asr

Conversation

@aviv1ron1

Copy link
Copy Markdown
Collaborator

No description provided.

aviv1ron1 and others added 18 commits June 14, 2026 14:14
Signed-off-by: aviv ron <rona@il.ibm.com>
Register an ASR multimodal processor on GraniteSwitchForCausalLM: audio is
transcribed and a <|audio|> marker is replaced with the transcript tokens
(PromptReplacement), so the scheduler sizes KV for the real length. The model's
embed_multimodal supplies those positions' embeddings via the token table (the
seam a future trained audio encoder will reuse), and embed_input_ids scatters
them. Audio capability is gated per-checkpoint by config.asr_enabled.

Alpha scope: audio runs on the base model (no adapter control-token handling).

Signed-off-by: aviv ron <rona@il.ibm.com>
Signed-off-by: aviv ron <rona@il.ibm.com>
New --enable-audio / --asr-model / --asr-device flags. When audio is enabled the
composer adds the <|audio|> marker token (before the embedding resize) and writes
asr_enabled / asr_model_id / asr_device into the checkpoint config, so the vLLM
backend's gated audio processor activates for that checkpoint.

Signed-off-by: aviv ron <rona@il.ibm.com>
Signed-off-by: aviv ron <rona@il.ibm.com>
tests/unit/test_asr.py: CPU-tier unit coverage for the ASR backend (audio
coercion, mono/resample, lazy-load, transcribe with mocked pipeline); loads the
leaf module by path so it runs without the vLLM extra. docs/AUDIO.md: how to
compose --enable-audio, call it (Python + OpenAI server), the cascade design and
the future-encoder seam, and alpha limitations.

Signed-off-by: aviv ron <rona@il.ibm.com>
…ate work)

Signed-off-by: aviv ron <rona@il.ibm.com>
The Granite content-part loop only handled type=='text' and dropped audio parts,
so chat/OpenAI-server requests rendered without the <|audio|> marker and prompt
replacement failed. Add configure_audio_chat_template(): injects an elif that
appends the marker for any part whose type contains 'audio' (covers audio /
input_audio / audio_url). Called during compose when audio is enabled.

Signed-off-by: aviv ron <rona@il.ibm.com>
Set requires_raw_input_tokens=True so vLLM passes raw input_ids to forward on the
multimodal path, letting the switch detect adapter control tokens for audio
requests. embed_input_ids now applies the switch's token-exchange rewrite
(control -> substitute id) via a new SingleSwitch.apply_token_exchange helper, so
control tokens get in-distribution embeddings exactly as on the text path.

Signed-off-by: aviv ron <rona@il.ibm.com>
Signed-off-by: aviv ron <rona@il.ibm.com>
…errides in config

Signed-off-by: aviv ron <rona@il.ibm.com>
Remove the two hard caps in the audio cascade and add an encoder-agnostic
long-audio chunker:

1. Multiple clips per request. get_supported_mm_limits is now a configurable
   ceiling (asr_max_audio_clips, default 32) instead of a hardcoded 1; each
   clip's transcript is spliced at its own <|audio|> marker (the splice loop
   already supported N). For the cascade the clips cost no extra KV (transcripts
   are ordinary text tokens bounded by the context); the ceiling exists to bound
   the per-request synchronous-ASR work and the startup profiling pass.

2. Context-derived transcript budget. The fixed 2048-token cap is replaced by
   audio_token_budget() = (context - generation_reserve - prompt) / clips,
   floored at 1. get_mm_max_tokens_per_item derives from the seq_len vLLM passes;
   _call_hf_processor reads the served max_model_len and subtracts the actual
   prompt. New asr_generation_reserve_tokens (default 8192).

3. Encoder-agnostic chunker (chunking.py): split_waveform + merge_transcripts
   (word-level overlap de-dup). ASRTranscriber.transcribe gains a self_chunks
   flag: Whisper (self_chunks=True) keeps its internal timestamp stitching;
   backends with a fixed input window route through our split/transcribe/merge.
   New config: asr_self_chunks, asr_chunk_length_s, asr_chunk_overlap_s.

Compose CLI flags and docs/AUDIO.md updated. Tests: new test_chunking.py; budget
and chunked-path tests in test_asr.py; config round-trip/validation in
test_config.py; multi-clip, budget, and flag-forwarding in test_audio_processor.py.

Already-composed checkpoints inherit the new behavior via getattr defaults; no
re-compose required (only self_chunks=False needs a config change).

Signed-off-by: aviv ron <rona@il.ibm.com>
Add support for adapter switching and configurable encoders

Signed-off-by: Aviv Ron <rona@il.ibm.com>
# Conflicts:
#	src/granite_switch/config.py
#	src/granite_switch/vllm/granite_switch_model.py
#	src/granite_switch/vllm/switch/single.py
#	tutorials/notebooks/granite_speech_demo.ipynb

Signed-off-by: aviv ron <rona@il.ibm.com>
GraniteSwitchForCausalLM.embed_input_ids annotated is_multimodal as
Optional[torch.Tensor], but the module never imports Optional. The
NameError fires at class-definition time, so `import granite_switch.vllm`
raises and vLLM's plugin loader silently skips the entry point. The
architecture is then never registered with transformers, and any attempt
to serve a granite_switch checkpoint fails with "Transformers does not
recognize this architecture".

Use the module's prevailing PEP 604 style (torch.Tensor | None) instead of
adding a typing import.

Signed-off-by: aviv ron <rona@il.ibm.com>
…check (#45) (#109)

The audio cascade truncated the transcript to a context-derived per-clip
budget (asr_generation_reserve_tokens held back for the answer). When that
reserve met or exceeded the served max_model_len, the budget floored to 1
token: the model saw no question and refused, with no error (#45).

Rather than patch the truncation math, remove it. The transcript is now
spliced into the prompt in full, as ordinary text tokens. A request whose
prompt + transcript(s) can't leave room for the answer within max_model_len
is rejected by vLLM's standard prompt-length check (HTTP 400) — the same
loud failure text-only requests already get, instead of a silent 1-token
transcript. This also makes audio behave identically to long text.

Details:
- Remove asr_generation_reserve_tokens entirely (config field + validation,
  compose CLI flag, processor accessor). It only existed to size the old
  truncation budget.
- Drop the budget param / ids[:budget] truncation in the processor; splice
  the full transcript.
- get_mm_max_tokens_per_item now reports max(1, seq_len // count) — the
  honest worst-case transcript positions one clip can occupy. This still
  sizes vLLM's encoder cache correctly (embed_multimodal emits one row per
  transcript token); it is a profiling hint, not a request bound.
- Remove the now-dead audio_token_budget helper and its tests.

Tests updated; docs/AUDIO.md rewritten to describe reject-not-truncate.

Co-authored-by: aviv ron <rona@il.ibm.com>

Signed-off-by: Bar Haim <barha@il.ibm.com>
Signed-off-by: aviv ron <rona@il.ibm.com>
* tests: cover audio/adapter coexistence, chat-template marker, empty clips, clip ceiling

Signed-off-by: BAR HAIM <barha@il.ibm.com>

* tests: add e2e vLLM audio serving smoke (text + adapter + audio x1/x2/x3)

CI-runnable serving smoke closing the #47 e2e box. Boots a real composed,
audio-enabled GraniteSwitch checkpoint under vLLM and drives text-only,
adapter-control-token, and audio x1/x2/x3 requests through one live engine.
Markers slow+requires_model+gpu (opt-in via -m); model built through the
compose CLI with --enable-audio; synthetic audio keeps it asset-free.

Verified green on an A100 with ibm-granite/granite-4.1-3b +
granitelib-core-r1.0: 5 passed.

Signed-off-by: BAR HAIM <barha@il.ibm.com>

* tests: e2e answerability adapter over an audio-delivered document (#47)

Adds the first correctness test for the "adapters + audio together" #47
box: the RAG answerability adapter judging a query against a document
supplied as speech rather than text.

The document is delivered as audio by placing a single <|audio|> marker
as the document text (documents=[{"text": "<|audio|>"}]); the Granite
template renders each document via `doc | tojson`, so the marker lands in
the <documents> block exactly where vLLM's ASR processor splices the
transcript — the document the adapter reasons over is the transcribed
speech. The <|answerability|> control token is inserted by the same
template (aLoRA fallback path), so the switch fires the adapter at the
generation prompt.

Ground truth comes from one real clip (hf-internal-testing/
librispeech_asr_dummy row 0), driving both classes without shipping a
fixture: an answerable question whose answer is spoken, and an
unanswerable one about content the clip never mentions. Both are
distinctive enough to survive ASR word-errors, so the assertion tests
the answerability decision, not transcription accuracy (WER is a
separate box). FLAC is decoded with soundfile from the raw bytes to
avoid requiring torchcodec (datasets>=4).

Where test_audio_serving_smoke.py only proves an adapter control token
doesn't crash the audio path, this proves the switch produces the
correct verdict end-to-end through a live vLLM engine. Verified on an
A100 with granite-4.1-3b + granitelib-rag-r1.0: 2 passed.

Markers slow+requires_model+gpu; opt-in via
  pytest -m "slow and requires_model and gpu"

Signed-off-by: BAR HAIM <barha@il.ibm.com>

* tests: use committed local speech clip for answerability-over-audio

Replace the runtime hf-internal-testing/librispeech_asr_dummy download with a
committed 3s 16 kHz WAV (SpeechT5, MIT-licensed) so the test runs offline and
carries clean provenance for merge-to-main. The spoken sentence states a
location that distil-whisper transcribes cleanly, so the questions key on the
location rather than the proper noun. Verdicts verified: 2 passed on A100
(granite-4.1-3b + granitelib-rag-r1.0).

Signed-off-by: BAR HAIM <barha@il.ibm.com>

* tests: sweep all default adapters route correctly with audio enabled

Adds the 'all default adapters route correctly with audio enabled' coverage for
issue #47. Composes the full default set (RAG + Core + Guardian) with
--enable-audio and asserts the switch maps each adapter_token_ids[i] to index
i+1 (pre-control positions stay on base 0), observed via the HF backend's
_last_adapter_indices. For granite-4.1-3b, 12 adapters resolve (context_relevance
has no 4.1-3b flavor); verified 12/12 route correctly on A100.

Loads with ignore_mismatched_sizes=True: the <|audio|> token bumps vocab_size
one past the switch's control_to_substitute_lut buffer, which SingleSwitch
rebuilds from config, so the freshly-built buffer is kept.

Signed-off-by: BAR HAIM <barha@il.ibm.com>

---------

Signed-off-by: BAR HAIM <barha@il.ibm.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants