o
    ٷi/                  	   @   s  d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZ d dlmZ eeZi ddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+Zed,d- e D Zh d.Zd'hZd/e	d0ejfd1d2Zd/e	d0dfd3d4Zi dd5d	d6dd7dd8dd9dd:dd;dd<dd=dd>dd?dd>d!d@d%dAd'dBdCdDdEdFZdGdHdIdJdKdLdMdNdOZd/e	dPefdQdRZd/e	fdSdTZd/e	fdUdVZdS )W    N)init_logger)_LazyRegisteredModel_ModelRegistry)OmniDiffusionConfig)SequenceParallelConfigget_sp_plan_from_model)apply_sequence_parallelQwenImagePipeline)
qwen_imagepipeline_qwen_imager	   QwenImageEditPipeline)r
   pipeline_qwen_image_editr   QwenImageEditPlusPipeline)r
   pipeline_qwen_image_edit_plusr   QwenImageLayeredPipeline)r
   pipeline_qwen_image_layeredr   GlmImagePipeline)	glm_imagepipeline_glm_imager   ZImagePipeline)z_imagepipeline_z_imager   OvisImagePipeline)
ovis_imagepipeline_ovis_imager   WanPipeline)wan2_2pipeline_wan2_2Wan22PipelineStableAudioPipeline)stable_audiopipeline_stable_audior   WanImageToVideoPipeline)r   pipeline_wan2_2_i2vWan22I2VPipelineLongCatImagePipeline)longcat_imagepipeline_longcat_imager%   BagelPipeline)bagelpipeline_bagelr(   LongCatImageEditPipeline)r&   pipeline_longcat_image_editr+   StableDiffusion3Pipeline)sd3pipeline_sd3r-   HunyuanImage3ForCausalMM)hunyuan_image_3pipeline_hunyuan_image_3HunyuanImage3PipelineFlux2KleinPipeline)flux2_kleinpipeline_flux2_kleinr4   NextStep11Pipeline)nextstep_1_1pipeline_nextstep_1_1r7   )fluxpipeline_fluxFluxPipeline)omnigen2pipeline_omnigen2OmniGen2Pipeline)r<   r?   c                 C   s0   i | ]\}\}}}|t d | d| |dqS )vllm_omni.diffusion.models..)module_name
class_name)r   ).0
model_arch
mod_foldermod_relnamecls_name rI   P/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/registry.py
<dictcomp>u   s    rK   >   r   r7   r-   	od_configreturnc                 C   s  t | j}|dur{|| d}| jj}|dkr%| jtvr%td|tt |dkr:| jtv r:| j	s:t
d| d| _	t|jdrE| j|j_t|jdrP| j	|j_|dkrtt|d	rt| jtv rt| j	rtd
dlm} d
dlm} ||||d t||  |S td| j d)a  Initialize a diffusion model from the registry.

    This function:
    1. Loads the model class from the registry
    2. Instantiates the model with the config
    3. Configures VAE optimization settings
    4. Applies sequence parallelism if enabled (similar to diffusers' enable_parallelism)

    Args:
        od_config: The OmniDiffusion configuration.

    Returns:
        The initialized pipeline model.

    Raises:
        ValueError: If the model class is not found in the registry.
    N)rL      z]vae_patch_parallel_size=%d is set but VAE patch parallelism is only enabled for %s; ignoring.zNvae_patch_parallel_size=%d requires vae_use_tiling; automatically enabling it.Tuse_slicing
use_tilingvaer   )get_dit_group),maybe_wrap_vae_decode_with_patch_parallelism)vae_patch_parallel_sizegroup_getterzModel class z' not found in diffusion model registry.)DiffusionModelRegistry_try_load_model_clsmodel_class_nameparallel_configrT   _VAE_PATCH_PARALLEL_ALLOWLISTloggerwarningsortedvae_use_tilinginfohasattrrQ   vae_use_slicingrO   rP   .vllm_omni.diffusion.distributed.parallel_staterR   2vllm_omni.diffusion.distributed.vae_patch_parallelrS   #_apply_sequence_parallel_if_enabled
ValueError)rL   model_classmodelvae_pp_sizerR   rS   rI   rI   rJ   initialize_model   sN   





	ri   c                 C   sH  z|j j}|dkrW dS g d}d}|D ]`}t| |sqt| |}|du r&qt|}|du r/qt|j j|j jd}|jdkrE|jdkrEdn|jdkrLdnd}	t	d	|j
j d
| d| d|	 d|j d|j d t||| |d7 }q|dkrtd| d W dS W dS  ty }
 ztd|
 d W Y d}
~
dS d}
~
ww )a  Apply sequence parallelism hooks if SP is enabled.

    This is the centralized location for enabling SP, similar to diffusers'
    ModelMixin.enable_parallelism() method. It applies _sp_plan hooks to
    transformer models that define them.

    Note: Our "Sequence Parallelism" (SP) corresponds to "Context Parallelism" (CP) in diffusers.
    We use _sp_plan instead of diffusers' _cp_plan.

    Args:
        model: The pipeline model (e.g., ZImagePipeline).
        od_config: The OmniDiffusion configuration.
    rN   N)transformertransformer_2ditunetr   )ulysses_degreering_degreehybridulyssesringz!Applying sequence parallelism to z (z) (sp_size=z, mode=z
, ulysses=z, ring=)z)Sequence parallelism is enabled (sp_size=zs) but no transformer with _sp_plan found. SP hooks not applied. Consider adding _sp_plan to your transformer model.z&Failed to apply sequence parallelism: z. Continuing without SP hooks.)rY   sequence_parallel_sizer`   getattrr   r   rn   ro   r[   r_   	__class____name__r   r\   	Exception)rg   rL   sp_sizetransformer_attrsapplied_countattrrj   plan	sp_configmodeerI   rI   rJ   rd      sZ   




 rd    get_qwen_image_post_process_func%get_qwen_image_edit_post_process_func*get_qwen_image_edit_plus_post_process_funcget_glm_image_post_process_funcget_post_process_func get_ovis_image_post_process_funcget_wan22_post_process_func"get_stable_audio_post_process_funcget_wan22_i2v_post_process_func#get_longcat_image_post_process_funcget_bagel_post_process_funcget_sd3_image_post_process_func!get_flux2_klein_post_process_func get_nextstep11_post_process_funcr<   get_flux_post_process_funcr?   get_omnigen2_post_process_funcget_glm_image_pre_process_func$get_qwen_image_edit_pre_process_func)get_qwen_image_edit_plus_pre_process_func'get_longcat_image_edit_pre_process_func'get_qwen_image_layered_pre_process_funcget_wan22_pre_process_funcget_wan22_i2v_pre_process_funcget_omnigen2_pre_process_func)r   r   r   r+   r   r   r"   r?   	func_namec                 C   s<   t | j \}}}d| d| }t|}t||}|| S )z?Load and return a process function from the appropriate module.r@   rA   )_DIFFUSION_MODELSrX   	importlibimport_moduleru   )rL   r   rF   rG   _rB   modulefuncrI   rI   rJ   _load_process_func<  s
   

r   c                 C   "   | j tvrd S t| j  }t| |S N)rX   _DIFFUSION_POST_PROCESS_FUNCSr   rL   r   rI   rI   rJ   get_diffusion_post_process_funcE     


r   c                 C   r   r   )rX   _DIFFUSION_PRE_PROCESS_FUNCSr   r   rI   rI   rJ   get_diffusion_pre_process_funcL  r   r   )r   torch.nnnnvllm.loggerr   #vllm.model_executor.models.registryr   r   vllm_omni.diffusion.datar   'vllm_omni.diffusion.distributed.sp_planr   r   +vllm_omni.diffusion.hooks.sequence_parallelr   rw   r[   r   itemsrV   rZ   _NO_CACHE_ACCELERATIONModuleri   rd   r   r   strr   r   r   rI   rI   rI   rJ   <module>   s    %*/49>CHMRWd
	
IB	
	