
    3j%                        S SK Jr  S SKJr  S SKJrJrJrJr  S SK	r	SSK
Jr  SSKJrJrJrJr   " S S	\5      r " S
 S\5      r\" SS9 " S S5      5       r " S S5      rg)    )annotations)	dataclass)CallableLiteralMappingSequenceN   )canonicalize_state_dict)ArchIdArchitectureModelDescriptor	StateDictc                      \ rS rSrSrSrg)UnsupportedModelError   zl
An error that will be thrown by `ArchRegistry` and `ModelLoader` if a model architecture is not supported.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r       U/home/wildlama/miniconda3/lib/python3.13/site-packages/spandrel/__helpers/registry.pyr   r          r   r   c                      \ rS rSrSrSrg)DuplicateArchitectureError   zY
An error that will be thrown by `ArchRegistry` if the same architecture is added twice.
r   Nr   r   r   r   r   r      r   r   r   T)frozenc                  f    \ rS rSr% SrS\S'    S\S'    SrS\S	'    \ S     SS
 jj5       rSr	g)ArchSupport   z[
An entry in an `ArchRegistry` that describes how to detect and load a model architecture.
Architecture[torch.nn.Module]architecturezCallable[[StateDict], bool]detectr   tuple[ArchId, ...]beforec                .    [        X R                  U5      $ )z`
Creates an `ArchSupport` from an `Architecture` by using the architecture's ``detect`` method.
)r"   r&   )archr(   s     r   from_architectureArchSupport.from_architecture/   s     4f55r   N)r   )r*   r$   r(   r'   returnr"   )
r   r   r   r   r   __annotations__r(   staticmethodr+   r   r   r   r   r"   r"      sb     0/ ('
 "$F# JL6+65G6	6 6r   r"   c                      \ rS rSrSrS rSS jrSS jrSS jrS r	SS jr
SS	 jr S   SS
 jjrSS.     SS jjr\SS j5       rSS jrSrg)ArchRegistry9   zr
A registry of architectures.

Architectures are detected/loaded in insertion order unless `before` is specified.
c                .    / U l         / U l        0 U l        g N)_architectures_ordered_by_idselfs    r   __init__ArchRegistry.__init__@   s    57/146r   c                    [        5       nU R                  Ul        U R                  Ul        U R                  Ul        U$ )z!
Returns a copy of the registry.
)r1   r5   r6   r7   )r9   news     r   copyArchRegistry.copyF   s4     n!00}}[[

r   c                    XR                   ;   $ r4   )r7   r9   ids     r   __contains__ArchRegistry.__contains__P   s    [[  r   c                2    U R                   [        U5         $ r4   )r7   r   rA   s     r   __getitem__ArchRegistry.__getitem__S   s    {{6":&&r   c                6    [        U R                  S5      5      $ )z@
Returns an iterator over all architectures in insertion order.
	insertion)iterarchitecturesr8   s    r   __iter__ArchRegistry.__iter__V   s     D&&{344r   c                ,    [        U R                  5      $ r4   )lenr5   r8   s    r   __len__ArchRegistry.__len__\   s    4&&''r   c                L    U R                   R                  [        U5      S 5      $ r4   )r7   getr   rA   s     r   rS   ArchRegistry.get_   s    {{vbz400r   c                    US:X  a  [        U R                  5      $ US:X  a  [        U R                  5      $ [        SU 35      e)z
Returns a new list with all architectures in the registry.

The order of architectures in the list is either insertion order or the order in which architectures are detected.
rI   	detectionzInvalid order: )listr5   r6   
ValueError)r9   orders     r   rK   ArchRegistry.architecturesb   sF     K++,,k!&&ug677r   F)ignore_duplicatesc                  [        U R                  5      n[        U R                  5      n/ nU H  nUR                  R
                  U;   a+  U(       a  M&  [        SUR                  R
                   35      eUR                  U5        XdUR                  R
                  '   UR                  U5        M     [        R                  U5      nX0l        Xpl
        X@l        U$ )a  
Adds the given architectures to the registry.

Throws an error if an architecture with the same ID already exists,
unless `ignore_duplicates` is True, in which case the old architecture is retained.

Throws an error if a circular dependency of `before` references is detected.

If an error is thrown, the registry is left unchanged.

Returns a list of architectures that were added.
zDuplicate architecture: )rW   r5   dictr7   r%   rB   r   appendr1   _get_orderedr6   )r9   r[   rK   new_architectures	new_by_idaddedr*   new_ordereds           r   addArchRegistry.addr   s    $ !!4!45%	!D  ##y0$0.t/@/@/C/C.DE  $$T*.2d''**+LL " #//0AB/#r   c                B  ^^^^^^ 0 m0 mU  Hb  nUTUR                   R                  '   UR                   H6  nUT;  a  / TU'   TU   R                  UR                   R                  5        M8     Md     / m[	        5       m/ mSUUUUUU4S jjmU  H  nT" U5        M     T$ )Nc                  > U R                   R                  T;   a5  [        SSR                  / TQU R                   R                  P5       35      eU T;   a  g TR	                  U 5        TR                  U R                   R                  5        TR                  U R                   R                  / 5       H  nT" TU   5        M     TR                  U 5        TR                  5         g )Nz/Circular dependency in architecture detection: z -> )r%   rB   rX   joinrd   r^   rS   pop)r*   r(   by_id
inv_beforeorderedseenstackvisits     r   ro   (ArchRegistry._get_ordered.<locals>.visit   s      ##u, EfkkRpTYRp[_[l[l[o[oRpFqErs  t|HHTNLL**--.$..):):)=)=rBeFm$ C NN4 IIKr   )r*   r"   )r%   rB   r(   r^   set)	rK   r*   r(   rj   rk   rl   rm   rn   ro   s	      @@@@@@r   r_   ArchRegistry._get_ordered   s    13
+-!D*.E$##&&'+++)+Jv&6"))$*;*;*>*>? & " &(!$ 	 	  "D$K " r   c                    [        U5      nU R                   H6  nUR                  U5      (       d  M  UR                  R	                  U5      s  $    [
        e)z
Detects the architecture of the given state dict and loads it.

This will canonicalize the state dict if it isn't already.

Throws an `UnsupportedModelError` if the model architecture is not supported.
)r
   r6   r&   r%   loadr   )r9   
state_dictr*   s      r   rt   ArchRegistry.load   sJ     -Z8
MMD{{:&&((--j99 " $#r   )r5   r7   r6   N)r-   r1   )rB   zArchId | strr-   bool)rB   str | ArchIdr-   r"   )r-   int)rB   rx   r-   zArchSupport | None)rI   )rY   z!Literal['insertion', 'detection']r-   list[ArchSupport])rK   r"   r[   rw   r-   rz   )rK   rz   r-   rz   )ru   r   r-   r   )r   r   r   r   r   r:   r>   rC   rF   rL   rP   rS   rK   rd   r/   r_   rt   r   r   r   r   r1   r1   9   s    7!'5(1
 4?808 
8& #(&#&  & 
	&P ! !F$r   r1   )
__future__r   dataclassesr   typingr   r   r   r   torchcanonicalizer
   model_descriptorr   r   r   r   	Exceptionr   rX   r   r"   r1   r   r   r   <module>r      s`    " ! 7 7  1 N NI   $6 6 6@T$ T$r   