test_voxtral_realtime.py 5.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from dataclasses import asdict

import pytest
from mistral_common.audio import Audio
from mistral_common.protocol.instruct.chunk import RawAudio
from mistral_common.protocol.transcription.request import (
    StreamingMode,
    TranscriptionRequest,
)
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
13
from mistral_common.tokens.tokenizers.tekken import SpecialTokenPolicy
14
15
16

from vllm import LLM, EngineArgs, SamplingParams
from vllm.assets.audio import AudioAsset
17
from vllm.engine.arg_utils import AsyncEngineArgs
18
from vllm.v1.engine.async_llm import AsyncLLM
19

20
MODEL_NAME = "mistralai/Voxtral-Mini-4B-Realtime-2602"
21
22
23
24
25
26
27
28
29
ENGINE_CONFIG = dict(
    model=MODEL_NAME,
    max_model_len=8192,
    max_num_seqs=4,
    limit_mm_per_prompt={"audio": 1},
    config_format="mistral",
    load_format="mistral",
    tokenizer_mode="mistral",
    enforce_eager=True,
30
    gpu_memory_utilization=0.9,
31
)
32

33
34
35
36
37

EXPECTED_TEXT = [
    (
        " First words I spoke in the original phonograph. "
        "A little piece of practical poetry. Mary had a little lamb,"
38
        " its fleece was quite a slow, and everywhere that Mary went, "
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        "the lamb was sure to go."
    ),
    (
        " And the 0-1 pitch on the way to Edgar Martinez. Swung on"
        " the line. Down the left field line for OBS. Here comes Joy. "
        "Here is Junior to third base. They're going to wave him in. "
        "The throw to the plate will be late. The Mariners are going"
        " to play. For the American League Championship, "
        "I don't believe it. It just continues. My, oh, my."
    ),
]


@pytest.fixture
def audio_assets() -> list[AudioAsset]:
    return [AudioAsset("mary_had_lamb"), AudioAsset("winning_call")]


@pytest.fixture
def tokenizer() -> MistralTokenizer:
    return MistralTokenizer.from_hf_hub(MODEL_NAME)


@pytest.fixture
def engine() -> LLM:
    engine_args = EngineArgs(**ENGINE_CONFIG)
65
66
67
    return LLM(**asdict(engine_args))


68
69
70
71
@pytest.fixture
def async_engine() -> AsyncLLM:
    engine_args = AsyncEngineArgs(**ENGINE_CONFIG)
    return AsyncLLM.from_engine_args(engine_args)
72

73

74
def test_voxtral_realtime_forward(audio_assets, tokenizer, engine):
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    audio_config = tokenizer.instruct_tokenizer.tokenizer.audio

    def from_file(file_path: str):
        audio = Audio.from_file(file_path, strict=False)
        req = TranscriptionRequest(
            audio=RawAudio.from_audio(audio),
            streaming=StreamingMode.OFFLINE,
            language=None,
        )
        tokenized = tokenizer.instruct_tokenizer.encode_transcription(req)

        return (tokenized.tokens, tokenized.audios[0].audio_array)

    tokenized_list = [
        from_file(audio_asset.get_local_path()) for audio_asset in audio_assets
    ]

    inputs = []
    sampling_params = []

    for tokens, audio_array in tokenized_list:
        num_samples = audio_array.shape[0]
97
        max_tokens = audio_config.num_audio_tokens(num_samples) - len(tokens) - 1
98
99
100
101
102
103
104
105
        sampling_params.append(SamplingParams(temperature=0.0, max_tokens=max_tokens))

        input_dict = {
            "multi_modal_data": {"audio": [(audio_array, None)]},
            "prompt_token_ids": tokens,
        }
        inputs.append(input_dict)

106
    outputs = engine.generate(
107
108
109
110
111
        inputs,
        sampling_params=sampling_params,
    )

    texts = [out.outputs[0].text for out in outputs]
112
113
114
115
    assert texts == EXPECTED_TEXT


@pytest.mark.asyncio
116
async def test_voxtral_realtime_generator(audio_assets, tokenizer, async_engine):
117
118
119
    # Lazy import to avoid CUDA-reinitialization error
    from vllm.model_executor.models.voxtral_realtime import VoxtralRealtimeBuffer

120
    sampling_params = SamplingParams(temperature=0.0, max_tokens=1)
121
    audio_config = tokenizer.instruct_tokenizer.audio_encoder.audio_config
122
123
124
125
126

    output_tokens_list = []
    for i, audio_asset in enumerate(audio_assets):
        output_tokens = []
        audio = Audio.from_file(audio_asset.get_local_path(), strict=False)
127
128
129
130
131

        req = TranscriptionRequest(
            streaming=StreamingMode.OFFLINE,
            audio=RawAudio.from_audio(audio),
            language=None,
132
        )
133
134
135
136
137
        audio_enc = tokenizer.encode_transcription(req)

        buffer = VoxtralRealtimeBuffer(audio_config, audio_enc.tokens)
        await buffer.append_audio(audio_enc.audios[0].audio_array)
        await buffer.append_audio(None)
138
139
140
141

        request_id = f"session-{i}"

        async for resp in async_engine.generate(
142
            prompt=buffer.get_input_stream(),
143
144
145
146
147
            sampling_params=sampling_params,
            request_id=request_id,
        ):
            tokens = resp.outputs[0].token_ids[-1:]
            output_tokens.extend(tokens)
148
            await buffer.append_tokens(tokens)
149
150
151

        output_tokens_list.append(output_tokens)

152
153
154
155
    texts = [
        tokenizer.decode(output_tokens, special_token_policy=SpecialTokenPolicy.IGNORE)
        for output_tokens in output_tokens_list
    ]
156
157
    texts[1] = texts[1].replace("a base hit", "OBS").replace("oh my", "oh, my")
    assert texts == EXPECTED_TEXT