@@ -2743,7 +2743,19 @@ def from_transformers(cls, config, parent_config=None) -> JetMoeConfig:
27432743
27442744
27452745@dataclasses .dataclass
2746- class WhisperConfig (BaseModelConfig ):
2746+ class SpeechToTextConfig (ArchitectureConfig ):
2747+ """Shared configuration contract for encoder-decoder speech models."""
2748+
2749+ encoder_input_name : str = "input_features"
2750+ encoder_input_channels : int | None = None
2751+ encoder_uses_attention_mask : bool = False
2752+ decoder_uses_encoder_attention_mask : bool = False
2753+ decoder_start_token_id : int | None = None
2754+ layer_norm_eps : float = 1e-5
2755+
2756+
2757+ @dataclasses .dataclass
2758+ class WhisperConfig (SpeechToTextConfig ):
27472759 """Configuration for Whisper encoder-decoder models."""
27482760
27492761 encoder_layers : int = DEFAULT_INT
@@ -2753,11 +2765,19 @@ class WhisperConfig(BaseModelConfig):
27532765 max_source_positions : int = 1500
27542766 max_target_positions : int = 448
27552767 scale_embedding : bool = False
2756- decoder_start_token_id : int | None = None
2757- layer_norm_eps : float = 1e-5
2768+
2769+ def __post_init__ (self ):
2770+ if self .encoder_input_channels is None :
2771+ self .encoder_input_channels = self .num_mel_bins
2772+ elif self .encoder_input_channels != self .num_mel_bins :
2773+ raise ValueError (
2774+ "WhisperConfig: encoder_input_channels "
2775+ f"({ self .encoder_input_channels } ) must equal num_mel_bins "
2776+ f"({ self .num_mel_bins } )."
2777+ )
27582778
27592779 @classmethod
2760- def from_transformers (cls , config ) -> WhisperConfig :
2780+ def from_transformers (cls , config , parent_config = None ) -> WhisperConfig :
27612781 if config .model_type != "whisper" :
27622782 raise ValueError (
27632783 f"WhisperConfig expects model_type='whisper', got '{ config .model_type } '"
@@ -2797,6 +2817,81 @@ def from_transformers(cls, config) -> WhisperConfig:
27972817 return cls (** options )
27982818
27992819
2820+ @dataclasses .dataclass
2821+ class MoonshineConfig (SpeechToTextConfig ):
2822+ """Configuration for Moonshine raw-waveform encoder-decoder ASR models."""
2823+
2824+ encoder_input_name : str = "input_values"
2825+ encoder_input_channels : int | None = None
2826+ encoder_uses_attention_mask : bool = True
2827+ decoder_uses_encoder_attention_mask : bool = True
2828+ encoder_num_hidden_layers : int = DEFAULT_INT
2829+ encoder_num_attention_heads : int = DEFAULT_INT
2830+ encoder_num_key_value_heads : int = DEFAULT_INT
2831+ encoder_hidden_act : str = "gelu"
2832+ decoder_hidden_act : str = "silu"
2833+
2834+ @classmethod
2835+ def from_transformers (cls , config , parent_config = None ) -> MoonshineConfig :
2836+ if config .model_type != "moonshine" :
2837+ raise ValueError (
2838+ f"MoonshineConfig expects model_type='moonshine', got '{ config .model_type } '"
2839+ )
2840+
2841+ hidden_size = config .hidden_size
2842+ decoder_heads = config .decoder_num_attention_heads
2843+ encoder_heads = config .encoder_num_attention_heads
2844+ rope_parameters = getattr (config , "rope_parameters" , None ) or getattr (
2845+ config , "rope_scaling" , None
2846+ )
2847+ rope_type = (rope_parameters or {}).get ("rope_type" , "default" )
2848+ options = dict (
2849+ vocab_size = config .vocab_size ,
2850+ hidden_size = hidden_size ,
2851+ intermediate_size = config .intermediate_size ,
2852+ num_hidden_layers = config .decoder_num_hidden_layers ,
2853+ num_attention_heads = decoder_heads ,
2854+ num_key_value_heads = getattr (config , "decoder_num_key_value_heads" , decoder_heads ),
2855+ head_dim = hidden_size // decoder_heads ,
2856+ hidden_act = getattr (config , "decoder_hidden_act" , "silu" ),
2857+ pad_token_id = getattr (config , "pad_token_id" , 2 ),
2858+ tie_word_embeddings = getattr (config , "tie_word_embeddings" , True ),
2859+ attn_qkv_bias = getattr (config , "attention_bias" , False ),
2860+ attn_o_bias = getattr (config , "attention_bias" , False ),
2861+ max_position_embeddings = getattr (config , "max_position_embeddings" , 194 ),
2862+ rope_type = rope_type ,
2863+ rope_theta = getattr (
2864+ config ,
2865+ "rope_theta" ,
2866+ (rope_parameters or {}).get ("rope_theta" , 10_000.0 ),
2867+ ),
2868+ rope_scaling = rope_parameters ,
2869+ partial_rotary_factor = getattr (
2870+ config ,
2871+ "partial_rotary_factor" ,
2872+ (rope_parameters or {}).get ("partial_rotary_factor" , 0.9 ),
2873+ ),
2874+ rope_interleave = True ,
2875+ mlp_bias = True ,
2876+ encoder_num_hidden_layers = config .encoder_num_hidden_layers ,
2877+ encoder_num_attention_heads = encoder_heads ,
2878+ encoder_num_key_value_heads = getattr (
2879+ config , "encoder_num_key_value_heads" , encoder_heads
2880+ ),
2881+ encoder_hidden_act = getattr (config , "encoder_hidden_act" , "gelu" ),
2882+ decoder_hidden_act = getattr (config , "decoder_hidden_act" , "silu" ),
2883+ decoder_start_token_id = getattr (config , "decoder_start_token_id" , 1 ),
2884+ layer_norm_eps = getattr (config , "layer_norm_eps" , 1e-5 ),
2885+ model_type = "moonshine" ,
2886+ bos_token_id = getattr (config , "bos_token_id" , 1 ),
2887+ eos_token_id = getattr (config , "eos_token_id" , 2 ),
2888+ )
2889+ resolved = _resolve_dtype (config )
2890+ if resolved is not None :
2891+ options ["dtype" ] = resolved
2892+ return cls (** options )
2893+
2894+
28002895@dataclasses .dataclass
28012896class MMSConfig (ArchitectureConfig ):
28022897 """Configuration for MMS (Massively Multilingual Speech) CTC models.
0 commit comments