o
    
۾i                     @   s   d Z ddlmZmZ ddlmZ ddlmZ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d	d
dZedddZeG dd
 d
eZeG dd deZeG dd dZeG dd dZG dd deeeef ZdS )z'Base class for weight transfer engines.    )ABCabstractmethod)Callable)KW_ONLY	dataclassfield)AnyGenericTypeVarN)ParallelConfig)WeightTransferConfig	TInitInfoWeightTransferInitInfo)boundTUpdateInfoWeightTransferUpdateInfoc                   @   s   e Zd ZdZdS )r   z4Base class for backend-specific initialization info.N)__name__
__module____qualname____doc__ r   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/vllm/distributed/weight_transfer/base.pyr      s    c                   @   s&   e Zd ZU dZeed< dZeed< dS )r   z3Base class for backend-specific weight update info._Tis_checkpoint_formatN)r   r   r   r   r   __annotations__r   boolr   r   r   r   r      s
   
 c                   @   ,   e Zd ZU dZeedZeeef e	d< dS )WeightTransferInitRequestz1API-level weight transfer initialization request.default_factory	init_infoN)
r   r   r   r   r   dictr    strr   r   r   r   r   r   r   '      
 r   c                   @   r   )WeightTransferUpdateRequestz API-level weight update request.r   update_infoN)
r   r   r   r   r   r!   r%   r"   r   r   r   r   r   r   r$   .   r#   r$   c                	   @   s   e Zd ZU dZee ed< ee ed< dede	ddfdd	Z
d
eeef defddZdeeef defddZededdfddZededeeeeejf  gdf ddfddZedddZdS )WeightTransferEnginea  
    Base class for weight transfer engines that handle transport of model weights
    from a trainer to inference workers.

    This abstraction separates weight transfer transport logic from the worker
    implementation, allowing different backends (NCCL, CUDA IPC[TODO], RDMA[TODO]) to be
    plugged in.

    Subclasses should define:
        init_info_cls: Type of backend-specific initialization info
        update_info_cls: Type of backend-specific update info
    init_info_clsupdate_info_clsconfigparallel_configreturnNc                 C   s   || _ || _dS )z
        Initialize the weight transfer engine.

        Args:
            config: The configuration for the weight transfer engine
            parallel_config: The configuration for the parallel setup
        N)r)   r*   )selfr)   r*   r   r   r   __init__G   s   

zWeightTransferEngine.__init__	init_dictc              
   C   F   z	| j di |W S  ty" } ztd| jj d| |d}~ww )aH  
        Construct typed init info from dict with validation.

        Args:
            init_dict: Dictionary containing backend-specific initialization parameters

        Returns:
            Typed backend-specific init info dataclass

        Raises:
            ValueError: If init_dict is invalid for this backend
        zInvalid init_info for : Nr   )r'   	TypeError
ValueError	__class__r   )r,   r.   er   r   r   parse_init_infoT      z$WeightTransferEngine.parse_init_infoupdate_dictc              
   C   r/   )aH  
        Construct typed update info from dict with validation.

        Args:
            update_dict: Dictionary containing backend-specific update parameters

        Returns:
            Typed backend-specific update info dataclass

        Raises:
            ValueError: If update_dict is invalid for this backend
        zInvalid update_info for r0   Nr   )r(   r1   r2   r3   r   )r,   r7   r4   r   r   r   parse_update_infoh   r6   z&WeightTransferEngine.parse_update_infor    c                 C      t )z
        Initialize the weight transfer mechanism.
        This is called once at the beginning of training.

        Args:
            init_info: Backend-specific initialization info
        NotImplementedError)r,   r    r   r   r   init_transfer_engine|   s   	z)WeightTransferEngine.init_transfer_enginer%   load_weightsc                 C   r9   )az  
        Receive weights from the trainer and load them incrementally.

        Args:
            update_info: Backend-specific update info containing parameter metadata
                        and any backend-specific data
            load_weights: Callable that loads weights into the model. Called
                         incrementally for each weight to avoid OOM.
        r:   )r,   r%   r=   r   r   r   receive_weights   s   z$WeightTransferEngine.receive_weightsc                 C   r9   )zv
        Shutdown the weight transfer engine.
        This should be called when the worker is shutting down.
        r:   )r,   r   r   r   shutdown   s   zWeightTransferEngine.shutdown)r+   N)r   r   r   r   typer   r   r   r   r   r-   r!   r"   r   r5   r8   r   r<   r   listtupletorchTensorr>   r?   r   r   r   r   r&   5   s2   
 

r&   )r   abcr   r   collections.abcr   dataclassesr   r   r   typingr   r	   r
   rC   vllm.config.parallelr   vllm.config.weight_transferr   r   r   r   r   r   r$   r&   r   r   r   r   <module>   s&   