o
    پi[                     @   s  U d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZ er7ddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZm Z  dd	l!m"Z" dd
l#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZR ddlSmTZTmUZU ddlVmWZW ddlXmYZYmZZZ ddl[m\Z\ e\e]Z^i Z_ee`eeW f ead< i Zbee`eee ee	 f f ead< dd Zcde`deee ee	 f dB fdd ZdejeG d!d" d"Zfi Zgee`eff ead#< i Zhee`e`f ead$< g Zieee`e
e`gejf f  ead%< 		d>d&e	d'ee d(eee`  d)eee
e`gejf   fd*d+Zkd,e`de`fd-d.Zld/e`deef fd0d1ZmejeG d2d3 d3Znd/e`denfd4d5Zoed6d7	d?d/e`d8eee`d9f  deen fd:d;Zpd<d= Zqeq  dS )@a  
Central registry for multimodal models.

This module provides a centralized registry for multimodal models, including pipelines
and sampling parameters. It allows for easy registration and retrieval of model
information based on model paths or other identifiers.
    N)	lru_cache)	TYPE_CHECKINGAnyCallableDictListOptionalTupleTypeUnionBackend)FastHunyuanConfigFluxPipelineConfigHunyuanConfigWanI2V480PConfigWanI2V720PConfigWanT2V480PConfigWanT2V720PConfigZImagePipelineConfig)PipelineConfig)Flux2KleinPipelineConfigFlux2PipelineConfig)GlmImagePipelineConfig)LTX2PipelineConfig)MOVA360PConfigMOVA720PConfig)QwenImageEditPipelineConfig%QwenImageEditPlus_2511_PipelineConfigQwenImageEditPlusPipelineConfigQwenImageLayeredPipelineConfigQwenImagePipelineConfig)FastWan2_1_T2V_480P_ConfigFastWan2_2_TI2V_5B_ConfigTurboWanI2V720ConfigTurboWanT2V480PConfigWan2_2_I2V_A14B_ConfigWan2_2_T2V_A14B_ConfigWan2_2_TI2V_5B_Config)Flux2KleinSamplingParamsFluxSamplingParams)GlmImageSamplingParams)FastHunyuanSamplingParamHunyuanSamplingParams)LTX2SamplingParams)MOVA_360P_SamplingParamsMOVA_720P_SamplingParams)QwenImage2512SamplingParamsQwenImageEditPlusSamplingParamsQwenImageLayeredSamplingParamsQwenImageSamplingParams)
FastWanT2V480PConfig#Turbo_Wan2_2_I2V_A14B_SamplingParam"Wan2_1_Fun_1_3B_InP_SamplingParamsWan2_2_I2V_A14B_SamplingParamWan2_2_T2V_A14B_SamplingParamWan2_2_TI2V_5B_SamplingParamWanI2V_14B_480P_SamplingParamWanI2V_14B_720P_SamplingParamWanT2V_1_3B_SamplingParamsWanT2V_14B_SamplingParams)ZImageSamplingParamsZImageTurboSamplingParams)ComposedPipelineBase)maybe_download_model_index!verify_model_config_and_directory)init_logger_PIPELINE_REGISTRY_PIPELINE_CONFIG_REGISTRYc            	   
   C   s  t rdS d} t| }t|j|jd D ]c\}}}|sxt|}t|drx|j}t	|t
s1|gn|}|D ]B}t|ts=q5|jt v rLtd|j d |t |j< t|drwt|drw|j|jft|j< td	|j d
|jj d|jj  q5qtdtt  d dS )a&  
    Automatically discover and register all ComposedPipelineBase subclasses.
    This function scans the 'sglang.multimodal_gen.runtime.pipelines' package,
    finds modules with an 'EntryClass' attribute, and maps the class's 'pipeline_name'
    to the class itself in a global registry.
    Nz'sglang.multimodal_gen.runtime.pipelines.
EntryClasszDuplicate pipeline name 'z' found. Overwriting.pipeline_config_clssampling_params_clsz-Auto-registered config classes for pipeline 'z': PipelineConfig=z, SamplingParams=z Registering pipelines complete, z pipelines registered)rE   	importlibimport_modulepkgutilwalk_packages__path____name__hasattrrH   
isinstancelist
issubclassrA   pipeline_nameloggerwarningrI   rJ   rF   debuglen)	package_namepackage_module_nameispkgpipeline_module	entry_clsentry_cls_listcls rc   R/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/registry.py _discover_and_register_pipelines}   sL   







re   pipeline_class_namereturnc                 C   s   t   t| S )z7
    Get the configuration classes for a pipeline.
    )re   rF   get)rf   rc   rc   rd   get_pipeline_config_classes   s   
ri   c                   @   s&   e Zd ZU dZeed< ee ed< dS )
ConfigInfozlEncapsulates all configuration information required to register a
    diffusers model within this framework.sampling_param_clsrI   N)rP   
__module____qualname____doc__r   __annotations__r
   r   rc   rc   rc   rd   rj      s   
 rj   _CONFIG_REGISTRY_MODEL_HF_PATH_TO_NAME_MODEL_NAME_DETECTORSrk   rI   hf_model_pathsmodel_detectorsc              
   C   s   t tt}t| |dt|< |r.|D ]}|tv r)td| dt|  d| d |t|< q|r<|D ]}t||f q2dS dS )zA
    Registers configuration classes for a new model family.
    )rk   rI   zModel path 'z' is already mapped to 'z' and will be overwritten by 'z'.N)	strrY   rp   rj   rq   rV   rW   rr   append)rk   rI   rs   rt   model_idpathdetectorrc   rc   rd   register_configs   s"   	

rz   rw   c                 C   s    d| v r|  ddd S | S )N/)rstripsplit)rw   rc   rc   rd   get_model_short_name   s   r   
model_pathc           
      C   sB  | t v rt |  }td|  d t|S t|  }tt  t	dd}|D ] }t| }||krGtd| d t | }t|  S q't
j| rSt| }nt| }|dd }g }tD ]\}}	|	|  sq|	|rtd	| d
 ||g7 }qct	|dkrt	|dkrtd |d }t|S td|  )zR
    Gets the ConfigInfo for a given model path using mappings and detectors.
    zResolved model path 'z' from exact path match.T)keyreversezResolved model name 'z' from partial path match._class_name zMatched model name 'z' using a registered detector.   z<More than one model name is matched, using the first matchedr   z$No model info found for model path: )rq   rV   rX   rp   rh   r   lowersortedkeysrY   osrx   existsrC   rB   rr   rW   RuntimeError)
r   rw   
model_nameall_model_hf_pathsregistered_model_hf_idregistered_model_nameconfigrU   matched_model_namesry   rc   rc   rd   _get_config_info   sF   





r   c                   @   s2   e Zd ZU dZee ed< eed< ee ed< dS )	ModelInfozv
    Encapsulates all configuration information required to register a
    diffusers model within this framework.
    pipeline_clsrk   rI   N)	rP   rl   rm   rn   r
   rA   ro   r   r   rc   rc   rc   rd   r   )  s
   
 r   c                 C   s2   ddl m} ddlm} ddlm} t|||dS )zt
    Get model info for diffusers backend.

    Returns a ModelInfo with DiffusersPipeline and generic configs.
    r   )DiffusersGenericPipelineConfig)DiffusersGenericSamplingParams)DiffusersPipeliner   rk   rI   )@sglang.multimodal_gen.configs.pipeline_configs.diffusers_genericr   6sglang.multimodal_gen.configs.sample.diffusers_genericr   :sglang.multimodal_gen.runtime.pipelines.diffusers_pipeliner   r   )r   r   r   r   rc   rc   rd   _get_diffusers_model_info5  s   r   r   )maxsizebackendr   c           	   
   C   s  ddl m} |du r|j}n
t|tr||}||jkr'td|  t	| S t
  ztj| r6t| }nt| }W n2 tym } z&td|  d|  ||jkrbtd t	| W  Y d}~S W Y d}~dS d}~ww |d}|std	|  d
 ||jkrtd t	| S dS t|}|s||jkrtd| d|  d t	| S td| d|  dtt  d dS t| }|s||jkrtd|  d t	| S td|  dtt  d dS td|  t||j|jd}td|  |S )ai  
    Resolves all necessary classes (pipeline, sampling, config) for a given model path.

    This function serves as the main entry point for model resolution. It performs two main tasks:
    1. Dynamically resolves the pipeline class by reading 'model_index.json' and matching
       '_class_name' against an auto-discovered registry of pipeline implementations.
    2. Resolves the associated configuration classes (for sampling and pipeline) using a
       manually registered mapping based on the model path.

    Args:
        backend: Backend to use ('auto', 'sglang', 'diffusers'). If None, uses 'auto'.

    r   r   Nz=Using diffusers backend for model '%s' (explicitly requested)z!Could not read model config for 'z': z!Falling back to diffusers backendr   z1'_class_name' not found in model_index.json for ''zPipeline class 'z' specified in 'zB' has no native sglang support. Falling back to diffusers backend.zH' is not a registered EntryClass in the framework. Available pipelines: zG. Consider using --backend diffusers to use vanilla diffusers pipeline.z2Could not resolve native configuration for model 'z%'. Falling back to diffusers backend.z+Could not resolve configuration for model 'zn'. It is not a registered model path or detected by any registered model family detectors. Known model paths: z*Using native sglang backend for model '%s'r   zFound model info: ))sglang.multimodal_gen.runtime.server_argsr   AUTOrR   ru   from_string	DIFFUSERSrV   infor   re   r   rx   r   rC   rB   	Exceptionerrorrh   rE   rW   rS   r   r   rq   r   rk   rI   )	r   r   r   r   erf   r   config_info
model_inforc   rc   rd   get_model_infoL  s   














r   c                   C   s@  t ttdd dd gd t ttdgdd gd t ttdgd	 t ttd
gdd gd t tt	dgd	 t t
tdgd	 t t
t	ddgd	 t ttdgdd gd t ttdgd	 t ttdgd	 t ttdgd	 t ttdgd	 t ttddgd	 t ttdgd	 t ttdgd	 t ttdgd	 t ttdd gd t ttdd gd t t t!dgdd gd t t"t#dd gd!d gd t t t$d"gd#d gd t t%t&d$gd%d gd t t't&d&gd'd gd t t(t)d(gd	 t t*t)d)gd	 t t(t+d*gd	 t t,t-d+gd	 t t,t.d,gd	 t t/t0d-gd	 t t1t2d.d gd d S )/Nc                 S      d|   v od|   v S )Nltxvideor   rx   rc   rc   rd   <lambda>      z#_register_configs.<locals>.<lambda>c                 S      d|   v S )Nzltx-2r   r   rc   rc   rd   r         )rk   rI   rt   z#hunyuanvideo-community/HunyuanVideoc                 S   r   )Nhunyuanr   hf_idrc   rc   rd   r     r   )rk   rI   rs   rt   zFastVideo/FastHunyuan-diffusers)rk   rI   rs   z Wan-AI/Wan2.1-T2V-1.3B-Diffusersc                 S   r   )Nwanpipeliner   r   rc   rc   rd   r     r   z*IPostYellow/TurboWan2.1-T2V-1.3B-DiffuserszWan-AI/Wan2.1-T2V-14B-Diffusersz)IPostYellow/TurboWan2.1-T2V-14B-Diffusersz.IPostYellow/TurboWan2.1-T2V-14B-720P-Diffusersz$Wan-AI/Wan2.1-I2V-14B-480P-Diffusersc                 S   r   )Nwanimagetovideor   r   rc   rc   rd   r     r   z$Wan-AI/Wan2.1-I2V-14B-720P-Diffusersz*IPostYellow/TurboWan2.2-I2V-A14B-Diffusersz'weizhou03/Wan2.1-Fun-1.3B-InP-DiffuserszWan-AI/Wan2.2-TI2V-5B-Diffusersz/FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusersz&FastVideo/FastWan2.2-TI2V-5B-Diffusersz Wan-AI/Wan2.2-T2V-A14B-Diffusersz Wan-AI/Wan2.2-I2V-A14B-Diffusersz'FastVideo/FastWan2.1-T2V-1.3B-Diffusersc                 S   r   )Nmova360pr   r   rc   rc   rd   r   2  r   c                 S   r   )Nr   720pr   r   rc   rc   rd   r   9  r   zblack-forest-labs/FLUX.1-devc                 S   r   )Nzflux.1r   r   rc   rc   rd   r   C  r   z!black-forest-labs/FLUX.2-klein-4Bz!black-forest-labs/FLUX.2-klein-9Bc                 S   s   d|   v pd|   v S )Nzflux.2-kleinzflux2-kleinr   r   rc   rc   rd   r   M  s    zblack-forest-labs/FLUX.2-devc                 S      d|   v od|   vS )Nzflux.2kleinr   r   rc   rc   rd   r   X  r   zTongyi-MAI/Z-Image-Turboc                 S   r   )Nzz-image-turbor   r   rc   rc   rd   r   a  r   zTongyi-MAI/Z-Imagec                 S   r   )Nzz-imageturbor   r   rc   rc   rd   r   j  r   zQwen/Qwen-ImagezQwen/Qwen-Image-2512zQwen/Qwen-Image-EditzQwen/Qwen-Image-Edit-2509zQwen/Qwen-Image-Edit-2511zQwen/Qwen-Image-Layeredc                 S   r   )Nz	glm-imager   r   rc   rc   rd   r     r   )3rz   r.   r   r-   r   r,   r   r=   r   r%   r>   r   r;   r   r<   r   r6   r$   r7   r:   r(   r#   r9   r'   r8   r&   r5   r"   r/   r   r0   r   r*   r   r)   r   r   r@   r   r?   r4   r!   r1   r   r2   r   r   r3   r    r+   r   rc   rc   rc   rd   _register_configs  st  


r   )NN)N)rrn   dataclassesrK   r   rM   	functoolsr   typingr   r   r   r   r   r   r	   r
   r   r   r   .sglang.multimodal_gen.configs.pipeline_configsr   r   r   r   r   r   r   r   3sglang.multimodal_gen.configs.pipeline_configs.baser   3sglang.multimodal_gen.configs.pipeline_configs.fluxr   r   8sglang.multimodal_gen.configs.pipeline_configs.glm_imager   4sglang.multimodal_gen.configs.pipeline_configs.ltx_2r   3sglang.multimodal_gen.configs.pipeline_configs.movar   r   9sglang.multimodal_gen.configs.pipeline_configs.qwen_imager   r   r   r    r!   2sglang.multimodal_gen.configs.pipeline_configs.wanr"   r#   r$   r%   r&   r'   r(   )sglang.multimodal_gen.configs.sample.fluxr)   r*   -sglang.multimodal_gen.configs.sample.glmimager+   ,sglang.multimodal_gen.configs.sample.hunyuanr,   r-   *sglang.multimodal_gen.configs.sample.ltx_2r.   )sglang.multimodal_gen.configs.sample.movar/   r0   .sglang.multimodal_gen.configs.sample.qwenimager1   r2   r3   r4   (sglang.multimodal_gen.configs.sample.wanr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   +sglang.multimodal_gen.configs.sample.zimager?   r@   Csglang.multimodal_gen.runtime.pipelines_core.composed_pipeline_baserA   6sglang.multimodal_gen.runtime.utils.hf_diffusers_utilsrB   rC   1sglang.multimodal_gen.runtime.utils.logging_utilsrD   rP   rV   rE   ru   ro   rF   re   ri   	dataclassrj   rp   rq   rr   boolrz   r   r   r   r   r   r   rc   rc   rc   rd   <module>   s   ,(
$	0$5
	"

5i 
b