o
    -i                  	   @   s   d Z ddlZddlmZ ddlZddlmZ dejdee	ef dB fddZ
d	ejjd
e	dejfddZdejjdee	ee	 f fddZdejjdeee	e	ee	f  fddZde	dee	ef fddZdS )zUtils for model executor.    N)Anyis_torch_equal_or_newerweightweight_attrsc                 C   sl   |du rdS |  D ])\}}t| |rJ d| ddlm} | r-|dkr-||}t| || q
dS )a  Set attributes on a weight tensor.

    This method is used to set attributes on a weight tensor. This method
    will not overwrite existing attributes.

    Args:
        weight: The weight tensor.
        weight_attrs: A dictionary of attributes to set on the weight tensor.
    Nz'Overwriting existing tensor attribute: r   )current_platformweight_loader)itemshasattrvllm.platformsr   use_sync_weight_loadermake_synced_weight_loadersetattr)r   r   keyvaluer    r   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/model_executor/utils.pyset_weight_attrs   s   
r   layer
param_namenew_datac                 C   sf   t |tjjr
|j}tjj|dd}t| |d}|dur+t|dr+|j}t|d|i t	| || dS )a  
    Replace a parameter of a layer while maintaining the ability to reload the weight.
    Called within implementations of the `process_weights_after_loading` method.

    This function should not be called on weights which are tied/shared

    Args:
        layer: Layer containing parameter to replace
        param_name: Name of parameter to replace
        new_data: New data of the new parameter
    F)requires_gradNr   )

isinstancetorchnn	Parameterdatagetattrr
   r   r   r   )r   r   r   	new_param	old_paramr   r   r   r   replace_parameter/   s   r    modelreturnc                    s   t | dd   d urt ni   r S |  D ]6}t |dd }|d ur*t|ni }t fdd| D rJtdt| j dt|j d 	| q S )Npacked_modules_mappingc                 3   s(    | ]\}}| v o | |kV  qd S )Nr   ).0kv
parent_mapr   r   	<genexpr>U   s   & z-get_packed_modules_mapping.<locals>.<genexpr>zCan't update z;'s packed_modules_mapping safely because of conflicts from .)
r   copydeepcopychildrenanyr	   
ValueErrortype__name__update)r!   child	child_mapr   r'   r   get_packed_modules_mappingH   s   r5   c                 C   sF   t | dd  }r| S |  D ]}t |dd }|d ur |   S qg S )Nget_expert_mapping)r   r-   )r!   r(   r3   r4   r   r   r   get_moe_expert_mapping_   s   
r7   current_backendc                 C   s   | dkrt drddiS i S )Ninductorz	2.9.0.devgraph_partitionFr   )r8   r   r   r   maybe_disable_graph_partitionm   s   r;   )__doc__r+   typingr   r   vllm.utils.torch_utilsr   Tensordictstrr   r   Moduler    listr5   tupleintr7   boolr;   r   r   r   r   <module>   s$   
""
