o
    پi+                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 de
eef dedee
ef fd	d
Zde
de
fddZde
de
fddZde
de
fddZde
de
fddZde
de
fddZde
de
fddZG dd dZdS )    N)Path)Any)PretrainedConfigWhisperConfig)loggerconfig_dictmodelreturnc           
         s    | t  t drt  t d}|o' d dp%ddk}d| v }|ro|rit  d d< |rBdg d	< nd
g d	< d v sOJ dddg}t fdd|D shJ dd| ndg d	< ndg d	< t drt	  t drddg}t fdd|D sJ dd| t dpi dp d}t dpi dpi d}|r|rJ d|rt
  |rt  t }	td|	  |	fS )Nquantizationmoenum_shared_expertsr   eagledeepseek_v3
model_typeMistralLarge3ForCausalLMEaglearchitecturesMistralLarge3ForCausalLMllama_4_scalingz+MistralLarge3 expect llama4 scaling config. original_max_position_embeddingsbetac                       g | ]}| d  v qS r    .0keyr   r   R/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/utils/mistral_utils.py
<listcomp>(       
z%adapt_config_dict.<locals>.<listcomp>z/llama_4_scaling config should define the keys: ,MixtralForCausalLMMistralForCausalLMyarnc                    r   r   r   r   r   r   r   r   ;   r   
multimodalvision_encoder_argsvision_encoderwhisper_model_argsencoder_argsz'Vision and audio are mutually exclusivezInitialized config %s)update_remap_general_mistral_argsboolget _remap_mistral_quantization_argslower_remap_moe_argsalljoin_remap_mistral_yarn_args_remap_mistral_vision_args_remap_mistral_audio_argsr   	from_dictr   debug)
r   r   kwargsis_moeis_mistral_large_3is_eaglellama_4_scaling_config_keys	is_visionis_audioconfigr   r   r   adapt_config_dict   sx   








r?   r>   c                 C   sR   |  dr| d}n| d}|  d}ddg| ddi|d} |r'|| d< | S )Nr$   r&   quantization_configpixtralPixtralForConditionalGenerationr   )r   r   text_configvision_config)r,   pop)r>   rD   quant_configr   r   r   r3   \   s   



r3   c                 C   s   ddddd d}|  dpi }ddd| d	< | D ]\}}||v r1||}|d ur1|| d	 |< qt|d
ks?J d| | S )Nfactorr   	beta_fast	beta_slow)rG   r   r   alphaapply_scaler#      )	rope_typemscale_all_dimrope_scalingr   zUnparsed yarn config: )r,   itemsrE   len)r>   yarn_config_mapyarn_configold_namenew_namevaluer   r   r   r2   o   s$   

r2   c                 C   st   ddddddd}dd	d
ddd}|  D ]\}}|| v r$| || |< q|  D ]\}\}}| ||| |< q)| S )Nhidden_sizerms_norm_epsnum_key_value_headsnum_hidden_layersnum_attention_headsintermediate_size)dimnorm_eps
n_kv_headsn_layersn_heads
hidden_dim)r   transformer)
activationsilu)tied_embeddingsF)max_seq_len  )max_position_embeddingsrh   )r   
hidden_acttie_word_embeddingsrg   ri   )rP   rE   )r>   config_mappingtop_level_mapping_with_defaultr   new_keydefault_valuer   r   r   r*      s(   
r*   c                 C   st   |  dr8| di }| ddkr0| d}|dv sJ d|dk}d|r(d	nd
d| d< | S td| d| S )Nr
   qformat_weightfp8_e4m3qscheme_act)	NO_SCALESTENSORNzAOnly NO_SCALES and TENSOR (default) are supported for qscheme_actrs   fp8dynamicstatic)quant_methodactivation_schemer@   zFound unknown quantization='z' in config)r,   rE   
ValueError)r>   r
   rr   
is_dynamicr   r   r   r-      s   




r-   c                 C   s   | d  d}|d }|d }| d}ddgt| t|d d	 |d d
 |d d |d d |d |d |d |d |d |d |d ddd} |rQ|| d< | S )Nr$   r'   r(   downsample_argsr@   whixtralVoxtralForConditionalGenerationaudio_encoding_argsnum_mel_binswindow_sizesampling_rate
hop_lengthdownsample_factorr]   r`   rb   ra   
vocab_sizemax_source_positionsF)r   r   r   r   r   d_modelencoder_layersencoder_ffn_dimencoder_attention_headsr   r   is_encoder_decoder)r   r   rC   audio_config)rE   r,   r   r5   r   )r>   whisper_argsr(   r|   rF   r   r   r   r4      s2   




r4   c              
   C   sl   ddddddddd	d
	}|  di }| D ]\}}||v r'||}|| |< qd | d< d| d< d| d< | S )Nmoe_layer_freqfirst_k_dense_replacenum_experts_per_tokn_routed_expertsmoe_intermediate_sizerouted_scaling_factorn_shared_expertsn_group
topk_group)	route_every_nr   r   num_expertsexpert_hidden_dimrouted_scaler   num_expert_groupsnum_expert_groups_per_tokr   topk_methodsoftmaxscoring_funcrL   routing_method_type)r,   rP   rE   )r>   moe_config_map
moe_configrT   rU   rV   r   r   r   r/      s(   
r/   c                	   @   sf   e Zd Z	ddedeeB dedB fddZdefd	d
Z	ddeeB dedB deee	f fddZ
dS )MistralConfigParsermain	file_namer   revisionNc                 C   sn   t || }| std| d| |d ur5| r5t|}t|W  d    S 1 s0w   Y  d S )NzFile not found z, )r   is_fileFileNotFoundErroropenjsonload)selfr   r   r   	file_pathfiler   r   r   get_hf_file_to_dict   s   
 z'MistralConfigParser.get_hf_file_to_dictr	   c                 C   sB   d}|  |||}|d u rtd| d| dt|tsJ |S )Nzparams.jsonzFailed to load mistral 'z' config for model zT. Please check if the model is a mistral-format model and if the config file exists.)r   rz   
isinstancedict)r   r   r   config_file_namer   r   r   r   _download_mistral_config_file   s   z1MistralConfigParser._download_mistral_config_filec           	      K   s   |  ||}|dd u rtd d|d< t||\}}t|dd  }rFt|trF|jt	| }|| }dd |D |_
ttd |d |_||fS )Nri   z|The params.json file is missing 'max_position_embeddings' and could not get a value from the HF config. Defaulting to 128000rh   sliding_windowc                 S   s   g | ]
}|d u r
dndqS )Nfull_attentionsliding_attentionr   )r   
layer_typer   r   r   r   %  s    z-MistralConfigParser.parse.<locals>.<listcomp>)r   r,   r   warningr?   getattrr   listrZ   rQ   layer_typesnextfilterr   )	r   r   r   r7   r   r>   r   pattern_repeatsr   r   r   r   parse  s"   zMistralConfigParser.parse)r   )N)__name__
__module____qualname__strr   r   r   r   tupler   r   r   r   r   r   r      s$    

r   )r   pathlibr   typingr   transformersr   r   sglang.srt.utilsr   r   r   r   r?   r3   r2   r*   r-   r4   r/   r   r   r   r   r   <module>   s&   


P