o
    ٷi|                     @   s   d dl mZmZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZ e
eZG dd	 d	eZeG d
d dZG dd deZdS )    )ABCabstractmethod)	dataclass)EnumN)nn)init_logger)OmniDiffusionConfigc                   @   s   e Zd ZdZdZdZdS )OffloadStrategynonemodel_level
layer_wiseN)__name__
__module____qualname__NONEMODEL_LEVEL
LAYER_WISE r   r   V/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/offloader/base.pyr	      s    r	   c                   @   s8   e Zd ZU eed< dZeed< ededd fddZ	dS )	OffloadConfigstrategyTpin_cpu_memory	od_configreturnc                 C   s\   t |dd}t |dd}t |dd}|rtj}|rtd n	|r%tj}ntj}| ||dS )an  Extract and validate offload settings from OmniDiffusionConfig.

        For now, enforces mutual exclusion between model-level and layer-wise offloading.
        Layer-wise takes priority if both are enabled.

        Args:
            od_config: OmniDiffusionConfig with offload settings

        Returns:
            OffloadConfig with validated settings
        enable_cpu_offloadFenable_layerwise_offloadr   TzpBoth model-level and layer-wise offloading enabled. Layer-wise takes priority, disabling model-level offloading.)r   r   )getattrr	   r   loggerinfor   r   )clsr   r   r   r   r   r   r   r   from_od_config   s"   zOffloadConfig.from_od_configN)
r   r   r   r	   __annotations__r   boolclassmethodr   r    r   r   r   r   r      s
   
 r   c                   @   sX   e Zd ZdZdedejfddZede	j
ddfd	d
ZedddZdefddZdS )OffloadBackendz#Base class for CPU offload backendsconfigdevicec                 C   s   || _ || _d| _d S )NF)r%   r&   enabled)selfr%   r&   r   r   r   __init__C   s   
zOffloadBackend.__init__pipeliner   Nc                 C      t )zEnable offloading on the pipeline.

        Discovers modules, moves them to appropriate devices, and
        registers forward hooks for swapping/prefetching.

        Args:
            pipeline: Diffusion pipeline model (e.g., Wan22Pipeline)
        NotImplementedError)r(   r*   r   r   r   enableH   s   
zOffloadBackend.enablec                 C   r+   )zDisable offloading and cleanup resources.

        Removes all registered hooks. Does NOT move modules back to
        original devices (caller responsible for that).
        r,   r(   r   r   r   disableT   s   zOffloadBackend.disablec                 C   s   | j S )N)r'   r/   r   r   r   
is_enabled]   s   zOffloadBackend.is_enabled)r   N)r   r   r   __doc__r   torchr&   r)   r   r   Moduler.   r0   r"   r1   r   r   r   r   r$   @   s    r$   )abcr   r   dataclassesr   enumr   r3   r   vllm.loggerr   vllm_omni.diffusion.datar   r   r   r	   r   r$   r   r   r   r   <module>   s   (