
    .`i              	       8   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z  fdZ
dej        j        d	e	d
ej        fdZdej        j        dee	ee	         f         fdZdej        j        deee	e	ee	f                  fdZde	dee	ef         fdZdS )zUtils for model executor.    N)Anyis_torch_equal_or_newerweightweight_attrsc                    |dS |                                 D ]h\  }}t          | |          rJ d|             ddlm} |                                r|dk    r|                    |          }t          | ||           id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	   s        m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/model_executor/utils.pyset_weight_attrsr      s     "((** $ $
U63''XX)XSV)X)XXX' 	4333332244 	F9O9O$>>uEEEU####!$ $    layer
param_namenew_datac                 .   t          |t          j        j                  r|j        }t          j                            |d          }t          | |d          }|)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
   s         r   replace_parameterr#   /   s     (EH.// !=""85"AAI+25*d+K+KIO!D!D!/_m$DEEEE:y)))))r   modelreturnc           	         t          | dd           t          j                  ni rS |                                 D ]}t          |dd           }|t          j        |          ni }t	          fd|                                D                       r:t          dt          |           j         dt          |          j         d          	                    |           S )Npacked_modules_mappingc              3   >   K   | ]\  }}|v o|         |k    V  d S )N ).0kv
parent_maps      r   	<genexpr>z-get_packed_modules_mapping.<locals>.<genexpr>U   s9      VVDAqZ6JqMQ$6VVVVVVr   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-   s      @r   get_packed_modules_mappingr:   H   s    8$??J.8.Dz***"J   !! 
) 
)E#;TBB	090EDM),,,2	VVVVIOODUDUVVVVV 	)LU 4 L L48KK4HL L L  
 i((((r   c                     t          | dd           x}r
 |            S |                                 D ]!}t          |dd           }| |            c S "g S )Nget_expert_mapping)r    r2   )r$   r-   r8   r9   s       r   get_moe_expert_mappingr=   _   sy     U$8$???z z|| ^^%% 	# 	#E';TBBI$ y{{""" %	r   current_backendc                 8    | dk    rt          d          rddiS i S )Ninductorz	2.9.0.devgraph_partitionFr   )r>   s    r   maybe_disable_graph_partitionrB   m   s+    *$$)@)M)M$!5))	r   )__doc__r0   typingr   r   vllm.utils.torch_utilsr   Tensordictstrr   r   Moduler#   listr:   tupleintr=   boolrB   r)   r   r   <module>rN      sH              : : : : : :$L$sCx.4'$ $ $ $D*UX_ *# * * * * *2eho $sDI~:N    .8?	%S#s"
#$   3 4T	?      r   