o
    پi/                     @   s  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZm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Zd dlmZ d dlmZ eeZejeZ d	d
 e!e D Z"ddiZ#e$e%e&f e'd< eddde$e%e&e%e%e%f f fddZ(e( Z)ej*ddgZ+edZ,eddG dd dZ-G dd deZ.eddG dd de.Z/deg e,f de,fddZ0eddG d d! d!e.Z1ed"dd#e%d$e.de2ej3 dB fd%d&Z4ed"dd#e%d$e.de-dB fd'd(Z5eG d)d* d*Z6e6d+d, e)7 D Z8dS )-    N)ABCabstractmethod)CallableSet)	dataclassfield)	lru_cache)NoReturnTypeVarcast)nn)init_loggerc                 C   s:   g | ]}t jt jt|r|d s|ds|qS )__.)ospathisdirjoinMODELS_PATH
startswith).0d r   a/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/models/registry.py
<listcomp>   s    r   CLIPVisionModelWithProjection)encodersclipCLIPVisionModel_IMAGE_ENCODER_MODELS)maxsizereturnc                  C   s  t } tD ]}tjt|}t|D ]}|dsq|d d }tj||}zt|ddd}|	 }W d    n1 s>w   Y  t
j||d}d }	d }
t
|D ])}t|t
jrp|jD ]}t|t
jro|jdkro|}	 nq^|
d u r|t|t
jr||}
qS|	r|
rg }|	j}t|t
jr||j n(t|t
jt
jfr|jD ]}t|t
jr||j qt|t
jr||j q|r|D ]}|| v rtd| d	 |}|||f| |< qW q ty } ztd
| d|  W Y d }~qd }~ww q| S )Nz.pyrzutf-8)encoding)filename
EntryClasszDuplicate architecture found: z. It will be overwritten.zCould not parse z to find models: )r   COMPONENT_DIRSr   r   r   r   listdirendswithopenreadastparsewalk
isinstanceAssigntargetsNameidClassDefvalueappendListTupleeltsConstantloggerwarning	Exception)discovered_models	componentcomponent_pathr%   mod_relnamefilepathfsourcetreeentry_class_nodefirst_class_defnodetargetmodel_cls_name_list
value_nodeeltmodel_cls_str
model_archer   r   r   _discover_and_register_models)   sr   







"8rP   z-mz2sglang.multimodal_gen.runtime.models.dits.registry_TT)frozenc                   @   s2   e Zd ZU eed< edeej dd fddZ	dS )
_ModelInfoarchitecturemodelr!   c                 C   s   t | jdS )N)rT   )rS   __name__)rU   r   r   r   from_model_clsx   s   z_ModelInfo.from_model_clsN)
rV   
__module____qualname__str__annotations__staticmethodtyper   ModulerW   r   r   r   r   rS   t   s   
 rS   c                   @   s6   e Zd ZedefddZedeej fddZ	dS )_BaseRegisteredModelr!   c                 C      t NNotImplementedErrorselfr   r   r   inspect_model_cls      z&_BaseRegisteredModel.inspect_model_clsc                 C   r`   ra   rb   rd   r   r   r   load_model_cls   rg   z#_BaseRegisteredModel.load_model_clsN)
rV   rX   rY   r   rS   rf   r]   r   r^   rh   r   r   r   r   r_      s
    r_   c                   @   sb   e Zd ZU dZeed< eej ed< e	deej fddZ
defddZdeej fd	d
ZdS )_RegisteredModelzP
    Represents a model that has already been imported in the main process.
    
interfaces	model_clsc                 C   s   t t| | dS )N)rj   rk   )ri   rS   rW   rk   r   r   r   rW      s   z_RegisteredModel.from_model_clsr!   c                 C      | j S ra   )rj   rd   r   r   r   rf         z"_RegisteredModel.inspect_model_clsc                 C   rm   ra   rl   rd   r   r   r   rh      rn   z_RegisteredModel.load_model_clsN)rV   rX   rY   __doc__rS   r[   r]   r   r^   r\   rW   rf   rh   r   r   r   r   ri      s   
 ri   fnc                 C   s   t  a}tj|d}t| |f}tjt	|dd}z|
  W n ty8 } ztd|j  |d }~ww t|d}ttt|W  d    W  d    S 1 sXw   Y  W d    d S 1 shw   Y  d S )Nzregistry_output.tmpT)inputcapture_outputzError raised in subprocess:
rb)tempfileTemporaryDirectoryr   r   r   cloudpickledumps
subprocessrun_SUBPROCESS_COMMANDcheck_returncoder=   RuntimeErrorstderrdecoder*   r   rQ   pickleload)rp   tempdiroutput_filepathinput_bytesreturnedrO   rC   r   r   r   _run_in_subprocess   s,   
"r   c                   @   sL   e Zd ZU dZeed< eed< eed< defddZdee	j
 fdd	Zd
S )_LazyRegisteredModelzL
    Represents a model that has not been imported in the main process.
    module_namecomponent_name
class_namer!   c                    s   t  fddS )Nc                      s   t   S ra   )rS   rW   rh   r   rd   r   r   <lambda>   s    z8_LazyRegisteredModel.inspect_model_cls.<locals>.<lambda>)r   rd   r   rd   r   rf      s   
z&_LazyRegisteredModel.inspect_model_clsc                 C   s$   t | j}tttj t|| jS ra   )		importlibimport_moduler   r   r]   r   r^   getattrr   )re   modr   r   r   rh      s   z#_LazyRegisteredModel.load_model_clsN)rV   rX   rY   ro   rZ   r[   rS   rf   r]   r   r^   rh   r   r   r   r   r      s   
 r      rN   rU   c                 C   sB   ddl m} ||  z| W S  ty    td|  Y d S w )Nr   )current_platformz%Ignore import error when loading '%s')'sglang.multimodal_gen.runtime.platformsr   verify_model_archrh   r=   r;   	exception)rN   rU   r   r   r   r   _try_load_model_cls   s   

r   c                 C   s,   z|  W S  ty   td|  Y d S w )Nz+Error in inspecting model architecture '%s')rf   r=   r;   r   )rN   rU   r   r   r   _try_inspect_model_cls   s   
r   c                   @   s
  e Zd ZU eedZeeef ed< de	e fddZ
dedeej eB ddfd	d
Zdee defddZdedeej dB fddZdededB fddZdeee B dee fddZdeee B deeef fddZdeee B deeej ef fddZdS )_ModelRegistry)default_factoryregistered_modelsr!   c                 C   s
   | j  S ra   )r   keysrd   r   r   r   get_supported_archs   s   
z"_ModelRegistry.get_supported_archsrN   rk   Nc                 C   sf   || j v rtd|| t|tr'|d}t|dkr"d}t|t| }nt	
|}|| j |< dS )a  
        Register an external model to be used in vLLM.

        :code:`model_cls` can be either:

        - A :class:`torch.nn.Module` class directly referencing the model.
        - A string in the format :code:`<module>:<class>` which can be used to
          lazily import the model. This is useful to avoid initializing CUDA
          when importing the model and thus the related error
          :code:`RuntimeError: Cannot re-initialize CUDA in forked subprocess`.
        z_Model architecture %s is already registered, and will be overwritten by the new model class %s.:   z2Expected a string in the format `<module>:<class>`N)r   r;   r<   r/   rZ   splitlen
ValueErrorr   ri   rW   )re   rN   rk   	split_strmsgrU   r   r   r   register_model   s   




z_ModelRegistry.register_modelarchitecturesc                    sB   |    t fdd|D rtd| dtd| d  )Nc                 3   s    | ]}| v V  qd S ra   r   )r   archall_supported_archsr   r   	<genexpr>  s    z8_ModelRegistry._raise_for_unsupported.<locals>.<genexpr>zModel architectures z@ failed to be inspected. Please check the logs for more details.z5 are not supported for now. Supported architectures: )r   anyr   )re   r   r   r   r   _raise_for_unsupported  s   
z%_ModelRegistry._raise_for_unsupportedc                 C      || j vrd S t|| j | S ra   )r   r   re   rN   r   r   r   r   '     
z"_ModelRegistry._try_load_model_clsc                 C   r   ra   )r   r   r   r   r   r   r   -  r   z%_ModelRegistry._try_inspect_model_clsc                 C   sf   t |tr|g}|std g }|D ]}|| jvr+t| j }td| d| || q|S )Nz$No model architectures are specifiedz Unsupported model architecture: z. Registered architectures: )	r/   rZ   r;   r<   r   listr   r=   r6   )re   r   normalized_archr   r   r   r   r   _normalize_archs3  s   


z_ModelRegistry._normalize_archsc                 C   <   |  |}|D ]}| |}|d ur||f  S q| |S ra   )r   r   r   )re   r   r   
model_infor   r   r   rf   F     


z _ModelRegistry.inspect_model_clsc                 C   r   ra   )r   r   r   )re   r   r   rk   r   r   r   resolve_model_clsS  r   z _ModelRegistry.resolve_model_cls)rV   rX   rY   r   dictr   rZ   r_   r[   r   r   r]   r   r^   r   r   r	   r   r   rS   r   r   tuplerf   r   r   r   r   r   r      s8   
 
$





r   c                 C   s2   i | ]\}\}}}|t d | d| ||dqS )z%sglang.multimodal_gen.runtime.models.r   )r   r   r   )r   )r   rN   r   rA   cls_namer   r   r   
<dictcomp>b  s    r   )9r,   r   r   r   rx   sysrt   abcr   r   collections.abcr   r   dataclassesr   r   	functoolsr   typingr	   r
   r   rv   torchr   1sglang.multimodal_gen.runtime.utils.logging_utilsr   rV   r;   r   dirname__file__r   r(   r'   r   r   rZ   r   r[   rP   _SGLANG_DIFFUSION_MODELS
executablerz   rQ   rS   r_   ri   r   r   r]   r^   r   r   r   itemsModelRegistryr   r   r   r   <module>   sz   

"?
s
