From 3b45d053b4bbc61ce437f00891b52ce5ddde7c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hung=20Hoang=20=28=E9=BB=84=E5=85=B4=EF=BC=89?= Date: Wed, 2 Sep 2026 22:55:16 +0700 Subject: [PATCH] [Bugfix][Model] Fix CohereASR streaming audio-token estimate (unit + subsampling) (#53829) Signed-off-by: hungh Co-authored-by: Cyrus Leung --- tests/models/multimodal/test_cohere_asr.py | 34 ++++++++++++++++++++++ vllm/model_executor/models/cohere_asr.py | 12 ++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/models/multimodal/test_cohere_asr.py diff --git a/tests/models/multimodal/test_cohere_asr.py b/tests/models/multimodal/test_cohere_asr.py new file mode 100644 index 0000000000..85a9b59dd5 --- /dev/null +++ b/tests/models/multimodal/test_cohere_asr.py @@ -0,0 +1,34 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project + +from types import SimpleNamespace + +import pytest + +from vllm.config import SpeechToTextConfig +from vllm.model_executor.models.cohere_asr import CohereAsrForConditionalGeneration + + +@pytest.mark.parametrize( + ("audio_duration_s", "expected_tokens"), + # window_stride=0.01s @ 16kHz -> 160-sample hop; subsampling_factor=8. + # frames = floor(duration * 16000 / 160); tokens = ceil(frames / 8). + [(1.0, 13), (10.0, 125), (30.0, 375)], +) +def test_get_num_audio_tokens_streaming_estimate(audio_duration_s, expected_tokens): + """The duration-based estimate must convert ``window_stride`` (seconds) to a + sample hop and divide by the encoder subsampling factor. Values are pinned to + concrete numbers so a rounding regression is actually caught.""" + model_config = SimpleNamespace( + hf_config=SimpleNamespace( + preprocessor={"window_stride": 0.01, "sample_rate": 16000}, + encoder={"subsampling_factor": 8}, + ) + ) + stt_config = SpeechToTextConfig(sample_rate=16000) + + got = CohereAsrForConditionalGeneration.get_num_audio_tokens( + audio_duration_s, stt_config, model_config + ) + + assert got == expected_tokens diff --git a/vllm/model_executor/models/cohere_asr.py b/vllm/model_executor/models/cohere_asr.py index b4e72d5b3f..d7f8f87fc6 100644 --- a/vllm/model_executor/models/cohere_asr.py +++ b/vllm/model_executor/models/cohere_asr.py @@ -2177,9 +2177,15 @@ class CohereAsrForConditionalGeneration( stt_config: SpeechToTextConfig, model_config: ModelConfig, ) -> int | None: - hop_length = model_config.hf_config.preprocessor.get("window_stride") - assert hop_length is not None - return math.ceil(audio_duration_s * stt_config.sample_rate / hop_length) + hf_config = model_config.hf_config + preprocessor = hf_config.preprocessor + sample_rate = preprocessor["sample_rate"] + window_stride = preprocessor["window_stride"] + hop_length = int(window_stride * sample_rate) + # Floor-divide to match get_seq_len. + num_frames = int(audio_duration_s * sample_rate) // hop_length + subsampling_factor = hf_config.encoder["subsampling_factor"] + return math.ceil(num_frames / subsampling_factor) def get_num_encoder_cross_attn_tokens(self, num_encoder_input_tokens: int) -> int: return self.model.encoder.get_num_encoder_cross_attn_tokens(