|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""Qwen3-ASR configuration (MLX-free). |
| 3 | +
|
| 4 | +Keep this module free of MLX imports so vLLM compat code can import config and |
| 5 | +shape helpers during planning/registration without pulling in the model stack. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from dataclasses import dataclass, field |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class Qwen3ASRAudioConfig: |
| 15 | + """Audio encoder configuration.""" |
| 16 | + |
| 17 | + num_mel_bins: int = 128 |
| 18 | + d_model: int = 896 |
| 19 | + encoder_layers: int = 18 |
| 20 | + encoder_attention_heads: int = 14 |
| 21 | + encoder_ffn_dim: int = 3584 |
| 22 | + downsample_hidden_size: int = 480 |
| 23 | + output_dim: int = 1024 |
| 24 | + max_source_positions: int = 1500 |
| 25 | + n_window: int = 50 |
| 26 | + n_window_infer: int = 800 |
| 27 | + activation_function: str = "gelu" |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def cnn_output_length(num_frames: int) -> int: |
| 31 | + """Return time length after 3x Conv2d(stride=2) downsampling.""" |
| 32 | + length = num_frames |
| 33 | + for _ in range(3): |
| 34 | + length = (length - 1) // 2 + 1 |
| 35 | + return int(length) |
| 36 | + |
| 37 | + def feat_extract_output_length(self, num_mel_frames: int) -> int: |
| 38 | + """Return number of audio tokens produced from a mel with N time frames.""" |
| 39 | + chunk_size = self.n_window * 2 |
| 40 | + frames_per_full_chunk = self.cnn_output_length(chunk_size) |
| 41 | + full_chunks, remainder = divmod(num_mel_frames, chunk_size) |
| 42 | + if remainder == 0: |
| 43 | + return int(full_chunks * frames_per_full_chunk) |
| 44 | + return int( |
| 45 | + full_chunks * frames_per_full_chunk + self.cnn_output_length(remainder) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class Qwen3ASRTextConfig: |
| 51 | + """Text decoder (Qwen3 LM) configuration.""" |
| 52 | + |
| 53 | + hidden_size: int = 1024 |
| 54 | + num_hidden_layers: int = 28 |
| 55 | + num_attention_heads: int = 16 |
| 56 | + num_key_value_heads: int = 8 |
| 57 | + head_dim: int = 128 |
| 58 | + intermediate_size: int = 3072 |
| 59 | + vocab_size: int = 151936 |
| 60 | + rms_norm_eps: float = 1e-6 |
| 61 | + rope_theta: float = 1000000.0 |
| 62 | + tie_word_embeddings: bool = True |
| 63 | + |
| 64 | + |
| 65 | +@dataclass |
| 66 | +class Qwen3ASRConfig: |
| 67 | + """Top-level Qwen3-ASR model configuration.""" |
| 68 | + |
| 69 | + audio_config: Qwen3ASRAudioConfig = field(default_factory=Qwen3ASRAudioConfig) |
| 70 | + text_config: Qwen3ASRTextConfig = field(default_factory=Qwen3ASRTextConfig) |
| 71 | + audio_token_id: int = 151676 |
| 72 | + audio_start_token_id: int = 151669 |
| 73 | + audio_end_token_id: int = 151670 |
| 74 | + eos_token_id: int = 151643 |
| 75 | + # Compatibility with Whisper interface for load_model dispatching |
| 76 | + n_mels: int = 128 |
| 77 | + n_audio_ctx: int = 1500 |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def from_dict(cls, d: dict) -> Qwen3ASRConfig: |
| 81 | + """Create config from config.json dictionary.""" |
| 82 | + thinker = d.get("thinker_config", d) |
| 83 | + |
| 84 | + audio_dict = thinker.get("audio_config", {}) |
| 85 | + audio_cfg = Qwen3ASRAudioConfig( |
| 86 | + num_mel_bins=audio_dict.get("num_mel_bins", 128), |
| 87 | + d_model=audio_dict.get("d_model", 896), |
| 88 | + encoder_layers=audio_dict.get("encoder_layers", 18), |
| 89 | + encoder_attention_heads=audio_dict.get("encoder_attention_heads", 14), |
| 90 | + encoder_ffn_dim=audio_dict.get("encoder_ffn_dim", 3584), |
| 91 | + downsample_hidden_size=audio_dict.get("downsample_hidden_size", 480), |
| 92 | + output_dim=audio_dict.get("output_dim", 1024), |
| 93 | + max_source_positions=audio_dict.get("max_source_positions", 1500), |
| 94 | + n_window=audio_dict.get("n_window", 50), |
| 95 | + n_window_infer=audio_dict.get("n_window_infer", 800), |
| 96 | + activation_function=audio_dict.get("activation_function", "gelu"), |
| 97 | + ) |
| 98 | + |
| 99 | + text_dict = thinker.get("text_config", {}) |
| 100 | + text_cfg = Qwen3ASRTextConfig( |
| 101 | + hidden_size=text_dict.get("hidden_size", 1024), |
| 102 | + num_hidden_layers=text_dict.get("num_hidden_layers", 28), |
| 103 | + num_attention_heads=text_dict.get("num_attention_heads", 16), |
| 104 | + num_key_value_heads=text_dict.get("num_key_value_heads", 8), |
| 105 | + head_dim=text_dict.get("head_dim", 128), |
| 106 | + intermediate_size=text_dict.get("intermediate_size", 3072), |
| 107 | + vocab_size=text_dict.get("vocab_size", 151936), |
| 108 | + rms_norm_eps=text_dict.get("rms_norm_eps", 1e-6), |
| 109 | + rope_theta=text_dict.get("rope_theta", 1000000.0), |
| 110 | + tie_word_embeddings=text_dict.get("tie_word_embeddings", True), |
| 111 | + ) |
| 112 | + |
| 113 | + return cls( |
| 114 | + audio_config=audio_cfg, |
| 115 | + text_config=text_cfg, |
| 116 | + audio_token_id=thinker.get("audio_token_id", 151676), |
| 117 | + audio_start_token_id=thinker.get("audio_start_token_id", 151669), |
| 118 | + audio_end_token_id=thinker.get("audio_end_token_id", 151670), |
| 119 | + n_mels=audio_cfg.num_mel_bins, |
| 120 | + n_audio_ctx=audio_cfg.max_source_positions, |
| 121 | + ) |
0 commit comments