o
    i                  
   @   s   d Z ddlmZ dejdefddZdejdefdd	Zd
ee defddZ			ddejdededee fddZ
dejdefddZdS )z$Model inspection utilities for vLLM.    Nmodulereturnc                 C   s   t | j}g }t| dd}|dur1t |j}t| dd}|dur%t |j}d|vr1|d|  t| drA||  dd |rN| d	d
| dS t| S )zGet info string for a module.quant_methodNschemeUnquantizedzquant=
extra_repr
 (, ))	type__name__getattrappendhasattrr   replacejoinstr)r   
class_namepartsr   
quant_namer    r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/model_inspection.py_get_module_info   s   



r   childc                 C   s8   g }|   D ]\}}|| dt|  qd|S )z8Get a signature for a child module to detect duplicates.:r   )named_modulesr   r   r   )r   linesname	submoduler   r   r   _get_child_signature$   s   
r!   indicesc                 C   s   t | } g }| d  }}| dd D ]!}||d kr|}q|||kr't|n| d|  | }}q|||kr>t|n| d|  d|S )zGFormat indices into range notation (e.g., [0,1,2,4,5,6] -> '0-2, 4-6').r      N-r   )sortedr   r   r   )r"   rangesstartendidxr   r   r   _format_index_ranges,   s   $
$
r*   r	   r   indentc              	   C   s   g }d| }t |  }|s,t| }||r#| d| d|  |S | |  |S t| }||r?| d| d| dn| | d g }g }|D ]\}	}
z|t|	|
f W qM tyl   ||	|
f Y qMw |ri }|D ]\}}
t|
}||g ||
f qst|	 dd dD ]R}dd |D }|d	 d
 }t
|d|d
 }|d	  }d|d
  }t|d
krt|}| d| dt| d| |d	< n| d|d	  d| |d	< || q|D ]\}	}
|t
|
|	|d
  q|| d |S )a!  Format a module tree with indentation, grouping identical layers.

    Produces output like:
        (layers): ModuleList(
          (0-27, 29-47): 47 x LlamaDecoderLayer(
            ...
          )
          (28, 48): 2 x DifferentDecoderLayer(
            ...
          )
        )
    z  r
   z): c                 S   s   | d d S )Nr   r   )gr   r   r   <lambda>n   s    z%_format_module_tree.<locals>.<lambda>)keyc                 S   s   g | ]\}}|qS r   r   ).0r)   _r   r   r   
<listcomp>o   s    z'_format_module_tree.<locals>.<listcomp>r   r#   r	   z x r   )listnamed_childrenr   r   int
ValueErrorr!   
setdefaultr%   values_format_module_treelstriplenr*   extend)r   r   r+   r   prefixchildreninfonumberednon_numbered
child_namechild_modulesig_to_groupr)   siggroupr"   representativechild_lines
first_linechild_prefix	range_strr   r   r   r8   =   sP   .r8   modelc                 C   s   d t| S )z=Format a model into a transformers-style hierarchical string.r   )r   r8   )rK   r   r   r   format_model_inspection   s   rL   )r	   r   )__doc__torch.nnnnModuler   r   r!   r2   r4   r*   r8   rL   r   r   r   r   <module>   s"   
I