o
    پij5                     @   s6  d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ z
ddlmZ d	ZW n eyS   d
ZY nw ddlmZ ddlmZ eeZe Zd6ddZ e   G dd deZ!d7de"e	B de"dB fddZ#de$e" de"de"de$e" fddZ%de$e" de$e" fddZ&dZ'de"de(fdd Z)d	efde$e" d!e(d"e(dee*e"ej+f ddf fd#d$Z,d%e"d&e"de-fd'd(Z.		d8de$e" d!e(dee*e"ej+f ddf fd)d*Z/d+ej+d,ej+ddfd-d.Z0d/e"d0e-de"dB fd1d2Z1d3ee*e"ej+f  de"fd4d5Z2dS )9zMUtilities for downloading, loading, initializing and verifying model weights.    N)	GeneratorIterable)Path)	safe_open)DTensor)tqdm)SafetensorsStreamerTF)get_local_torch_device)init_loggerreturnc                  C   s:   dt jvrzddl} dtj_W dS  ty   Y dS w dS )z#automatically activates hf_transferHF_HUB_ENABLE_HF_TRANSFERr   NT)osenvironhf_transferhuggingface_hub	constantsr   ImportError)r    r   e/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/loader/weight_utils.pyenable_hf_transfer(   s   
r   c                       s   e Zd Z fddZ  ZS )DisabledTqdmc                    s   d|d< t  j|i | d S )NTdisable)super__init__)selfargskwargs	__class__r   r   r   9   s   zDisabledTqdm.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r   7   s    r   model_name_or_path	cache_dirc                 C   sl   |pt }t| } tjtj|dd | dd}t|	 
 }|| d }tjtj||dd}|S )NT)exist_ok/-z.locki  )mode)temp_dirstrr   makedirspathdirnamereplacehashlibsha256encode	hexdigestfilelockFileLockjoin)r#   r$   lock_dir
model_name	hash_namelock_file_namelockr   r   r   get_lock>   s   r;   hf_weights_files	hf_folder
index_filec                    s   t j||}t j|s| S t|}t|d }W d    n1 s%w   Y  t  |D ]} t j|||  q/ fdd| D } | S )N
weight_mapc                    s   g | ]}| v r|qS r   r   .0fweight_files_in_indexr   r   
<listcomp>a       z6filter_duplicate_safetensors_files.<locals>.<listcomp>)	r   r,   r5   isfileopenjsonloadsetadd)r<   r=   r>   index_file_namerB   r?   weight_namer   rC   r   "filter_duplicate_safetensors_filesP   s   
rO   c                    s   g d  fdd| D } | S )z
    Exclude files that are not needed for inference.

    See https://github.com/huggingface/transformers/blob/v4.34.0/src/transformers/trainer.py#L227-L233
    )ztraining_args.binzoptimizer.binzoptimizer.ptzscheduler.ptz	scaler.ptc                    s&   g | ] t  fd dD s qS )c                 3   s    | ]}  |V  qd S N)endswith)rA   xrB   r   r   	<genexpr>s   s    zCfilter_files_not_needed_for_inference.<locals>.<listcomp>.<genexpr>)any)rA   	blacklistrS   r   rE   r   s
    z9filter_files_not_needed_for_inference.<locals>.<listcomp>r   )r<   r   rV   r   %filter_files_not_needed_for_inferencee   s
   
rX   z_{desc}: {percentage:3.0f}% Completed | {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]
	file_pathc              
   C   s   z"t | ddd}t| }W d   W dS 1 sw   Y  W dS  tyB } ztd| t|jt| W Y d}~dS d}~ww )z
    Validate that a safetensors file is readable and not corrupted.

    Args:
        file_path: Path to the safetensors file

    Returns:
        True if file is valid, False if corrupted
    ptcpu	frameworkdeviceNTz0Corrupted safetensors file detected: %s - %s: %sF)	r   listkeys	Exceptionloggererrortyper   r*   )rY   rB   _er   r   r   _validate_safetensors_file   s"   

rg   to_cpuuse_runai_model_streamerc                 c   s   t j  pt j dk}|rdntt }dd | D }|r|D ]c}zHtj|rVtj	|}t
| tdtj| tj|rUt
| tdtj| ntj|rkt
| tdtj| W q" ty } ztd|| W Y d	}~q"d	}~ww td
t| ddd |D  d|rt +}	|	|  |	 D ]\}
}|r|
|  fV  q|
||fV  qW d	   d	S 1 sw   Y  d	S t| d| tdD ]+}t|d|d}| D ]}
||
}|
|fV  qW d	   n	1 sw   Y  qd	S )z7Iterate over the weights in the model safetensor files.r   r[   c                 S   s   g | ]}t |s|qS r   )rg   )rA   st_filer   r   r   rE      s    z0safetensors_weights_iterator.<locals>.<listcomp>zRemoved corrupted symlink: %szRemoved corrupted blob: %szRemoved corrupted file: %sz&Failed to remove corrupted file %s: %sNFound z9 corrupted safetensors file(s). Files have been removed: c                 S   s   g | ]}t j|qS r   )r   r,   basenamer@   r   r   r   rE      rF   z?. Please retry - the files will be re-downloaded automatically.z%Loading safetensors checkpoint shardsdescr   
bar_formatrZ   r\   )torchdistributedis_initializedget_rankr*   r	   r   r,   islinkrealpathremoverb   inforl   existsrG   ra   warningRuntimeErrorlenr   stream_filesget_tensorsclonedetachtor   _BAR_FORMATr   r`   
get_tensor)r<   rh   ri   enable_tqdmr^   corrupted_filesrY   	blob_pathrf   streamernametensorrj   rB   paramr   r   r   safetensors_weights_iterator   sx   



"

r   bin_filer^   c              
   C   sj   z	t j| |ddW S  ty4 } zdt|v r/tdtj|  t j| |ddW  Y d}~S  d}~ww )a`  Load a PyTorch checkpoint file, handling legacy tar format.

    PyTorch 2.6 changed the default of weights_only from False to True.
    Legacy tar format files cannot be loaded with weights_only=True.
    This function tries weights_only=True first, then falls back to False
    for legacy tar format files from trusted sources (HuggingFace Hub).
    T)map_locationweights_onlyzlegacy .tar formatz6Loading %s with weights_only=False (legacy tar format)FN)	rp   rJ   rz   r*   rb   ry   r   r,   rl   )r   r^   rf   r   r   r   _load_pt_file   s   
r   c                 c   sd    |rdnt t }tj  ptj dk}t| d| tdD ]}t||}|	 E dH  ~q dS )z3Iterate over the weights in the model bin/pt files.r[   r   zLoading pt checkpoint shardsrm   N)
r*   r	   rp   rq   rr   rs   r   r   r   items)r<   rh   r^   r   r   stater   r   r   pt_weights_iterator   s   

r   r   loaded_weightc                 C   s   z7|   dkr|  dkr| j|  W dS |  | ks/J d|  d|   d| j| W dS  ty?    w )zDefault weight loader.   zAttempted to load weight (z) into parameter ()N)numeldatafill_itemsizecopy_ra   )r   r   r   r   r   default_weight_loader  s   r   r   params_dictc                    s     dr$td  dd}||vr"td  d| d dS |S dd	g}d
dg}|D ]J}  |rxt fdd|D rR d|d  d| d| }n	 |d| }||vrttd| d  d| d| d	  dS |  S q. S )a  Remap the name of FP8 k/v_scale parameters.

    This function handles the remapping of FP8 k/v_scale parameter names.
    It detects if the given name ends with a suffix and attempts to remap
    it to the expected name format in the model. If the remapped name is not
    found in the params_dict, a warning is printed and None is returned.

    Args:
        name (str): The original loaded checkpoint parameter name.
        params_dict (dict): Dictionary containing the model's named parameters.

    Returns:
        str: The remapped parameter name if successful, or the original name
             if no remapping is needed.
        None: If the remapped name is not found in params_dict.
    z	.kv_scalezDEPRECATED. Found kv_scale in the checkpoint. This format is deprecated in favor of separate k_scale and v_scale tensors and will be removed in a future release. Functionally, we will remap kv_scale to k_scale and duplicate k_scale to v_scalez.attn.k_scalez'Found kv_scale in the checkpoint (e.g. z6), but not found the expected name in the model (e.g. z). kv_scale is not loaded.Nz.k_scalez.v_scalez.self_attn.k_proj.k_scalez.self_attn.v_proj.v_scalec                 3   s    | ]}| v V  qd S rP   r   )rA   mo_scale_namer   r   r   rT   A  s    z,maybe_remap_kv_scale_name.<locals>.<genexpr>z.self_attn.r   _projz.self_attn.attnz.attnrk   z in the checkpoint (e.g. z). z is not loaded.)rQ   rb   warning_oncer.   rU   )r   r   remapped_namepossible_scale_namesmodelopt_scale_names
scale_namer   r   r   maybe_remap_kv_scale_name  sF   

r   named_paramsc                 C   sv   t  }t| dd dD ]*\}}||  | }t|tr#|j}||	 
 dtj j q| S )a  Compute a SHA-256 checksum for a set of (name, tensor) pairs.

    Used to verify the correctness of weight refitting. After a refit,
    compare the checksum of the in-GPU model weights against the checksum
    of the on-disk tensors or the tensors in the training engine.
    c                 S   s   | d S )Nr   r   )rR   r   r   r   <lambda>`  s    z*compute_weights_checksum.<locals>.<lambda>)key)r/   r0   sortedupdater1   r   
isinstancer   _local_tensorr[   
contiguousreshapeviewrp   uint8numpyr   r2   )r   hasherr   r   tr   r   r   compute_weights_checksumV  s   	
(r   )r   NrP   )T)3__doc__r/   rI   r   tempfilecollections.abcr   r   pathlibr   r3   huggingface_hub.constantsr   rp   safetensors.torchr   torch.distributed.tensorr   	tqdm.autor   runai_model_streamerr   HAS_RUNAI_MODEL_STREAMERr   )sglang.multimodal_gen.runtime.distributedr	   1sglang.multimodal_gen.runtime.utils.logging_utilsr
   r   rb   
gettempdirr)   r   r   r*   r;   r_   rO   rX   r   boolrg   tupleTensorr   dictr   r   r   r   r   r   r   r   r   <module>   s   


D
>