o
    پiD                     @   s  U d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlZddlmZ ddlmZ eeZejdejfd	d
Zdeeeeeeef B f deegeeeef f fddZdeeejf e	eeejf  B deegeeeef f deeeejf eeeeeef f f fddZG dd dZdedefddZdeddfddZ dede!e fddZ"dZ#i Z$eeee f e%d< dS ) z+Utilities for selecting and loading models.    N)defaultdict)CallableIterator)AnyDictType)nn)init_loggerdtypec              	   c   s8    t  }t |  zdV  W t | dS t | w )z0Sets the default torch dtype to the given dtype.N)torchget_default_dtypeset_default_dtype)r
   	old_dtype r   ^/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/loader/utils.pyset_default_torch_dtype   s   
r   mapping_dictreturnc                    s$   dt dtt ttf f fdd}|S )a4  
    Creates a mapping function that transforms parameter names using regex patterns.

    Args:
        mapping_dict (Dict[str, str]): Dictionary mapping regex patterns to replacement patterns

    Returns:
        Callable[[str], str]: A function that maps parameter names from source to target format
    namer   c                    s   d }d }t dt d }t }| h}t|D ]_}d}  D ]R\}}	||v r(qt|| d u r1qd }
d }t|	trF|	d }
|	d }|	d }	t	||	| }|| krq|
d urY|
}|}|} |
| | |v rhd} n
|
|  d} nq|sv nq| ||fS )N      F   r   T)maxlensetrangeitemsrematch
isinstancetuplesubadd)r   merge_indextotal_split_params	max_stepsapplied_patternsvisited_names_transformedpatternreplacementcurr_merge_indexcurr_total_split_paramsnew_namer   r   r   
mapping_fn.   sH   



z+get_param_names_mapping.<locals>.mapping_fn)strr    r   )r   r0   r   r/   r   get_param_names_mapping!   s    .r2   hf_param_sdparam_names_mappingc           	         s   i }t ti }t| tr|  } | D ]I\}}||\ }} dks& du r'q|||f| < |durX|  |< t  |krW fddt|D }tj|dd} = nq|| < q||fS )a  
    Converts a Hugging Face parameter state dictionary to a custom parameter state dictionary.

    Args:
        hf_param_sd (Dict[str, torch.Tensor]): The Hugging Face parameter state dictionary
        param_names_mapping (Callable[[str], tuple[str, Any, Any]]): A function that maps parameter names from source to target format

    Returns:
        custom_param_sd (Dict[str, torch.Tensor]): The custom formatted parameter state dict
        reverse_param_names_mapping (Dict[str, Tuple[str, Any, Any]]): Maps back from custom to hf
     Nc                    s   g | ]}  | qS r   r   ).0itarget_param_nameto_merge_paramsr   r   
<listcomp>   s    
z+hf_to_custom_state_dict.<locals>.<listcomp>r   )dim)r   dictr   r   r   r   r   cat)	r3   r4   custom_param_sdreverse_param_names_mappingsource_param_namefull_tensorr#   num_params_to_mergesorted_tensorsr   r8   r   hf_to_custom_state_dict_   s4   


rE   c                   @   s   e Zd Zdd Zdd ZdS )skip_init_modulesc                 C   s:   i | _ tjtjtjtjfD ]}|j| j |< dd |_qd S )Nc                 S   s   d S Nr   )selfr   r   r   <lambda>   s    z-skip_init_modules.__enter__.<locals>.<lambda>)_orig_resetr   LinearConv1dConv2dConv3dreset_parameters)rH   clsr   r   r   	__enter__   s
   zskip_init_modules.__enter__c                 C   s   | j  D ]\}}||_qd S rG   )rJ   r   rO   )rH   exc_type	exc_value	tracebackrP   origr   r   r   __exit__   s   zskip_init_modules.__exit__N)__name__
__module____qualname__rQ   rV   r   r   r   r   rF      s    rF   module_typec                 C   s   |  dr| dd S | S )z?Normalize module types like 'text_encoder_2' -> 'text_encoder'._2N)endswith)rZ   r   r   r   _normalize_component_type   s   
r^   model_configc                 C   s   dD ]}|  |d qdS )z.Remove common extraneous HF fields if present.)_name_or_pathtransformers_version
model_typetokenizer_classtorch_dtypeN)pop)r_   keyr   r   r   _clean_hf_config_inplace   s   rg   
model_pathc                 C   s   t ttjt| dS )z.List all .safetensors files under a directory.z*.safetensors)sortedglobospathjoinr1   )rh   r   r   r   _list_safetensors_files   s   rn   i   @component_name_to_loader_cls)&__doc__
contextlibrj   rk   r   collectionsr   collections.abcr   r   typingr   r   r   r   r   1sglang.multimodal_gen.runtime.utils.logging_utilsr	   rW   loggercontextmanagerr
   r   r=   r1   r    intr2   TensorrE   rF   r^   rg   listrn   BYTES_PER_GBro   __annotations__r   r   r   r   <module>   s@   

>&
/