
    9i              	           S SK r S SKrS SKJrJrJr  S SKJr  SrSr	\" 5       r
 " S S\5      r\	S4S\S	\S
\S\4S jjrg)    N)ListTupleUnion)
get_loggertypedefaultc                       \ rS rSrSrS\4S jrS r\S 5       r	\S 5       r
S r\4S	 jr\S
S
S4S jr\S
S
S4S\S\S\4S jjrSrg
)Registry   zRegistry which support registering modules and group them by a keyname

If group name is not provided, modules will be registered to default group.
namec                 *    Xl         [        0 0U l        g N)_namedefault_group_modules)selfr   s     Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/utils/registry.py__init__Registry.__init__   s    
&+    c           	          U R                   R                  SU R                   S3-   nU R                  R	                  5        H(  u  p#USU S[        UR                  5       5       S3-  nM*     U$ )Nz (z)
group_name=z
, modules=
)	__class____name__r   r   itemslistkeys)r   
format_str
group_namegroups       r   __repr__Registry.__repr__   sr    ^^,,DJJ<s/CC
!%!4!4!6JK
| 4

-.b2 2J "7 r   c                     U R                   $ r   )r   r   s    r   r   Registry.name    s    zzr   c                     U R                   $ r   )r   r%   s    r   modulesRegistry.modules$   s    }}r   c                    U R                   R                  5        Ha  u  p[        R                  SU 35        UR	                  5        H  n[        R                  SU 35        M     [        R                  S5        Mc     g)z7logging the list of module in current registry
        r   	 N)r   r   loggerinfor   )r   r    r!   ms       r   r   Registry.list(   s^     "&!4!4!6JKK+j\23ZZ\bH% "KKO	 "7r   c                 `    X R                   ;  a  g U R                   U   R                  US 5      $ r   )r   get)r   
module_key	group_keys      r   r2   Registry.get1   s+    MM)==+//
DAAr   NFc                 B   [        U[        5      (       d   S5       eXR                  ;  a  [        5       U R                  U'   Uc  UR                  nX R                  U   ;   a%  U(       d  [        U SU R                   SU S35      eX0R                  U   U'   Xl        g )Nz%group_key is required and must be strz is already registered in [])
isinstancestrr   dictr   KeyErrorr   r4   )r   r4   module_name
module_clsforces        r   _register_moduleRegistry._register_module7   s    
 )  	H G	H  MM)'+vDMM)$ $--K--	225k]*D"jj\9+Q8 9 90:i -(r   r4   r=   r>   c                    ^ ^^^ Tb,  [        T[        5      (       d  [        S[        T5       35      eUb  T R	                  TTUTS9  U$ UUUU 4S jnU$ )a  Register module

Example:
    >>> models = Registry('models')
    >>> @models.register_module('image-classification', 'SwinT')
    >>> class SwinTransformer:
    >>>     pass

    >>> @models.register_module('SwinDefault')
    >>> class SwinTransformerDefaultGroup:
    >>>     pass

    >>> class SwinTransformer2:
    >>>     pass
    >>> MODELS.register_module('image-classification',
                                module_name='SwinT2',
                                module_cls=SwinTransformer2)

Args:
    group_key: Group name of which module will be registered,
        default group name is 'default'
    module_name: Module name
    module_cls: Module class object
    force (bool, optional): Whether to override an existing class with
        the same name. Default: False.

z,module_name must be either of None, str,got r4   r=   r>   r?   c                 ,   > TR                  TTU TS9  U $ )NrC   )r@   )r>   r?   r4   r=   r   s    r   	_register+Registry.register_module.<locals>._register{   s*    !!#'%	 " 
 r   )r9   r:   	TypeErrorr   r@   )r   r4   r=   r>   r?   rE   s   ``` ` r   register_moduleRegistry.register_moduleO   sy    @ #z+s'C'C ##'#4"57 8 8!!!#'%	 " 
 	 	 r   )r   r   )r   
__module____qualname____firstlineno____doc__r:   r   r"   propertyr   r(   r   r   r2   r@   r   rH   __static_attributes__ r   r   r
   r
      s    
,S ,     )6 B $1%)$($	)2 *7+/+/#	4#&4%(4 %)4 4r   r
   registryr4   default_argsreturnc                    [        U [        5      (       d  [        S[        U 5       35      e[        U ;  a%  Ub
  [        U;  a  [        S[         SU  SU 35      e[        U[        5      (       d  [        S[        U5       35      e[        U[        5      (       d  Ub  [        S[        U5       35      eSS	KJn  UR                  R                  5       X S
   4nUR                  U5        U R                  5       nUb*  UR                  5        H  u  pxUR                  Xx5        M     Uc  [        nUR!                  [        5      n	[        U	["        5      (       a7  UR%                  XS9n
U
c  [        U	 SUR                   SU S35      eX*l        OP[(        R*                  " U	5      (       d  [(        R,                  " U	5      (       a  U	n
O[        S[        U	5       35      e [/        U
S5      (       a  U
R0                  " S0 UD6$ U
" S0 UD6$ ! [2         a&  n[        U5      " U
R4                   SU 35      UeSnAff = f)a7  Build a module from config dict when it is a class configuration, or
call a function from config dict when it is a function configuration.

Example:
    >>> models = Registry('models')
    >>> @models.register_module('image-classification', 'SwinT')
    >>> class SwinTransformer:
    >>>     pass
    >>> swint = build_from_cfg(dict(type='SwinT'), MODELS,
    >>>     'image-classification')
    >>> # Returns an instantiated object
    >>>
    >>> @MODELS.register_module()
    >>> def swin_transformer():
    >>>     pass
    >>>       = build_from_cfg(dict(type='swin_transformer'), MODELS)
    >>> # Return a result of the calling function

Args:
    cfg (dict): Config dict. It should at least contain the key "type".
    registry (:obj:`Registry`): The registry to search the type from.
    group_key (str, optional): The name of registry group from which
        module should be searched.
    default_args (dict, optional): Default initialization arguments.
    type_name (str, optional): The name of the type in the config.
Returns:
    object: The constructed object.
zcfg must be a dict, but got Nz.`cfg` or `default_args` must contain the key "z", but got r   z8registry must be an modelscope.Registry object, but got z-default_args must be a dict or None, but got r   )LazyImportModuler   )r4   z is not in the z registry group zE. Please make sure the correct version of ModelScope library is used.z*type must be a str or valid type, but got _instantiatez: rP   )r9   r;   rG   r   	TYPE_NAMEr<   r
   modelscope.utils.import_utilsrU   r   upperimport_modulecopyr   
setdefaultr   popr:   r2   r4   inspectisclass
isfunctionhasattrrV   	Exceptionr   )cfgrQ   r4   rR   rU   sigargsr   valueobj_typeobj_clses               r   build_from_cfgrj      sI   @ c4  6tCykBCC9L#@@ L%r,12 2 h)) ##'>"24 5 	5|T**l.B ##'#5"68 9 	9 ?== )[
9C""3'88:D'--/KDOOD( 0 !	xx	"H(C  ,,x,=?*OHMM?"9+ .KLM M &		"	"g&8&8&B&B8h8HIK 	K97N++''/$//?T?" 91g))*"QC01q89s   %"H H 
I !H;;I )	importlibr^   typingr   r   r   modelscope.utils.loggerr   rW   r   r-   objectr
   r:   r;   rj   rP   r   r   <module>ro      sl      % % .		uv ut %2(,P9%P9!P9 "&P9 28P9r   