
    RЦi0                     :   S SK rS SKrS SKrS SKJr  S SKJr  S SKJrJ	r	J
r
JrJrJrJrJrJr  S SKrS SKrS SKJr  S SKJr  S SKJr  / SQrS.S	\R0                  S
\4S jjr   S/S\	S\R0                  S\S\S\R0                  4
S jjr   S/S\R0                  S\S\S\4S jjr   S/S\R0                  S\S\S\4S jjrSr  S0S\\\\4      S\\
\	4   S\S\4S jjr   S0S\R0                  S\S\4S jjr!  S0S\R0                  S\S\4S jjr"   S1S\\\\R0                  4      S\#S\\\\S 4   4   S!\\\\\R0                        4   4S" jjr$SS#.S$\\   4S% jjr%    S2S&\#S'\S(\S$\\   4S) jjr&S*\#S+\S\4S, jr'S	\R0                  S\\   4S- jr(g)3    N)defaultdict)chain)	AnyCallableDictIteratorListOptionalTupleTypeUnion)nn)Tensor)use_reentrant_ckpt)model_parametersnamed_applynamed_modulesnamed_modules_with_paramsadapt_input_convgroup_with_matchergroup_modulesgroup_parametersflatten_modulescheckpoint_seq
checkpointreinit_non_persistent_buffersmodelexclude_headc                     U(       a#  U R                  5        Vs/ s H  o"PM     snS S $ U R                  5       $ s  snf )N)
parameters)r   r   ps      V/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/models/_manipulate.pyr   r      s@     ++-.-a-.s33!! /s   ;fnmoduledepth_firstinclude_rootreturnc           	          U(       d  U(       a  U " XS9  UR                  5        H+  u  pVU(       a  SR                  X%45      OUn[        XXSSS9  M-     U(       a  U(       a  U " XS9  U$ )N)r%   name.T)r$   r%   r*   r&   r'   )named_childrenjoinr   )r$   r%   r*   r&   r'   
child_namechild_modules          r#   r   r      s]     <
&$$*$9$9$; 
59SXXt01z
rZgkl %< |
&$M    r*   c              #      #    U(       d  U(       a  X4v   U R                  5        H3  u  pEU(       a  SR                  X45      OUn[        XTUSS9 S h  vN   M5     U(       a  U(       a  X4v   g g g  N7fNr+   T)r%   r*   r&   r'   )r,   r-   r   r%   r*   r&   r'   r.   r/   s         r#   r   r   -   sx      <l$*$9$9$; 
59SXXt01z
 kX\^ 	^ 	^ %< |l ${	^s   AA5A3A5c              #   4  #    U R                   (       a  U(       d  U(       a  X4v   U R                  5        H3  u  pEU(       a  SR                  X45      OUn[        XTUSS9 S h  vN   M5     U R                   (       a  U(       a  U(       a  X4v   g g g g  N07fr2   )_parametersr,   r-   r   r3   s         r#   r   r   =   s      +,l$*$9$9$; 
59SXXt01z
,kX\^ 	^ 	^ %< kll /;k	^s   A#B%B&1B)i named_objectsgroup_matcherreturn_valuesreversec                 :  ^ [        T[        5      (       a  / n[        TR                  5       5       Hu  u  nu  pgUc  M  [        U[        [
        45      (       a/  U H'  nU[        R                  " US   5      U4US   4/-  nM)     MW  U[        R                  " U5      U4S 4/-  nMw     UmU4S jn	[        [
        5      n
U  H'  u  pX" U5         R                  U(       a  UOU5        M)     [        [
        5      nSn[        [        S U
R                  5       5      5       H3  nUS:  d  US   [        S   :w  a  US-  nX   R                  X   5        M5     U(       a9  U(       a   S5       e0 nUR                  5        H  u  nnU H  nXU'   M	     M     U$ U$ )Nr      c                   > [        T[        [        45      (       a{  T Hi  u  pnUR                  U 5      nU(       d  M   X$R	                  5       U4n[        [        [        [        R                  " [        S U5      5      5      5      s  $    [        S5      4$ T" U 5      n[        U[        R                  R                  5      (       d  U4$ [        U5      $ )Ninf)
isinstancelisttuplematchgroupsmapfloatr   from_iterablefiltercollectionsabcIterable)r*   match_fnprefixsuffixrpartsordr7   s          r#   _get_grouping)group_with_matcher.<locals>._get_groupinge   s    mdE]33,9(&NN4(1#XXZ8E UE,?,?tU@S,T!UVV -: <= %Cc;??#;#;<<t:r0   c                 
    U S L$ )N )xs    r#   <lambda>$group_with_matcher.<locals>.<lambda>|   s    Qd]r0   z-reverse mapping only sensible for name output)r>   dict	enumerateitemsr@   r?   recompiler   appendsortedrF   keysMATCH_PREV_GROUPextend)r6   r7   r8   r9   compiledgroup_ordinal
group_namemspecsspecrP   groupingkvlayer_id_to_paramlidparam_to_layer_idlmns    `                r#   r   r   P   s    -&&2;M<O<O<Q2R.M.J}%%//"E"**U1X"68H%PQ(!S TTH # bjj/-1A4HII 3S !  4 Hq!"))}!!D  $D)
CF2HMMODE7ae/221HC%%hk2 F
  Q"QQ (..0GC'*!$  1 ! r0   c                 4    [        U R                  5       XUS9$ N)r8   r9   )r   named_parametersr%   r7   r8   r9   s       r#   r   r      s$     !=W^` `r0   c                 *    [        [        U 5      XUS9$ rp   )r   r   rr   s       r#   r   r      s      !&)=_fh hr0   r   depthrK   .module_typesc              #     #    [        U[        5      n[        U[        5      (       aG  US:X  a0  [        R                  [        R
                  [        R                  4nO[        R                  4nU  H~  u  pVU(       aA  [        Xc5      (       a1  [        UR                  5       US-
  U(       a  U4OUUS9 S h  vN   MM  U(       a  X%4-   nXV4v   M`  U(       a  SR                  X%/5      nXV4v   M     g  N<7f)N	containerr;   )rK   ru   r+   )
r>   r@   strr   
Sequential
ModuleList
ModuleDictr   r,   r-   )r   rt   rK   ru   prefix_is_tupler*   r%   s          r#   r   r      s      !/O,$$;&MM2=="--HLMM+L%Z55&%%'	"1wt)	   'l"88VN3Dl" &s   B5C67C48=C6use_reentrantr~   c                |    Uc
  [        5       n[        R                  R                  R                  " U /UQ7SU0UD6$ )zqcheckpoint wrapper fn

A thin wrapper around torch.utils.checkpoint.checkpoint to default
use_reentrant to False
r~   )r   torchutilsr   )functionr~   argskwargss       r#   r   r      sN     *,;;!!,,	 $ 	 r0   everyflatten	skip_lastc                 J   Uc
  [        5       nS n[        U [        R                  R                  5      (       a  U R                  5       n U(       a  [        R                  " U 5      n [        U [        [        45      (       d  [        U 5      n [        U 5      nU(       a  US-  nSn[        SXr5       HG  n	[        X-   S-
  US-
  5      n[        R                  R                  R                  U" XU 5      UUS9nMI     U(       a  U" US-   [        U 5      S-
  U 5      " U5      $ U$ )a  A helper function for checkpointing sequential models.

Sequential models execute a list of modules/functions in order
(sequentially). Therefore, we can divide such a sequence into segments
and checkpoint each segment. All segments except run in :func:`torch.no_grad`
manner, i.e., not storing the intermediate activations. The inputs of each
checkpointed segment will be saved for re-running the segment in the backward pass.

See :func:`~torch.utils.checkpoint.checkpoint` on how checkpointing works.

.. warning::
    Checkpointing currently only supports :func:`torch.autograd.backward`
    and only if its `inputs` argument is not passed. :func:`torch.autograd.grad`
    is not supported.

.. warning:
    At least one of the inputs needs to have :code:`requires_grad=True` if
    grads are needed for model inputs, otherwise the checkpointed part of the
    model won't have gradients.

Args:
    functions: A :class:`torch.nn.Sequential` or the list of modules or functions to run sequentially.
    x: A Tensor that is input to :attr:`functions`
    every: checkpoint every-n functions (default: 1)
    flatten: flatten nn.Sequential of nn.Sequentials
    skip_last: skip checkpointing the last function in the sequence if True
    use_reentrant: Use re-entrant checkpointing

Returns:
    Output of running :attr:`functions` sequentially on :attr:`*inputs`

Example:
    >>> model = nn.Sequential(...)
    >>> input_var = checkpoint_seq(model, input_var, every=2)
c                    ^ ^^ UUU 4S jnU$ )Nc                 J   > [        TTS-   5       H  nTU   " U 5      n M     U $ )Nr;   )range)_xjend	functionsstarts     r#   forward5checkpoint_seq.<locals>.run_function.<locals>.forward  s+    5#'*q\"% +Ir0   rT   )r   r   r   r   s   ``` r#   run_function$checkpoint_seq.<locals>.run_function  s    	 r0   r;   rR   r   r}   )r   r>   r   r   ry   childrenr   rE   r@   r?   lenr   minr   r   )
r   rU   r   r   r   r~   r   num_checkpointedr   r   s
             r#   r   r      s   V *, )UXX0011&&(	''	2	i%//)$	9~A
Cq*2%-!#%5%9:KK""--Y/' . 
 3 C!GS^a%7CAFFHr0   in_chansconv_weightc                    UR                   nUR                  5       nUR                  u  p4pVU S:X  aV  US:  a?  UR                  S   S-  S:X  d   eUR                  X4S-  SXV5      nUR	                  SSS9nO~UR	                  SSS9nOmU S:w  ag  US:w  a  [        S5      e[        [        R                  " U S-  5      5      nUR                  SUSS5      S S 2S U 2S S 2S S 24   nUS[        U 5      -  -  nUR                  U5      nU$ )	Nr;      r      F)dimkeepdimTz*Weight format not supported by conversion.)dtyperD   shapereshapesumNotImplementedErrorintmathceilrepeatto)r   r   	conv_typeOIJKr   s           r#   r   r   !  s   !!I##%K""JA!1}q5$$Q'!+q000%--aaAAK%//a/?K%//a/>K	Q6%&RSS 8a<01F%,,Q1=a(Aq>PQKAh/0K..+Kr0   c                     / nU R                  5        HB  u  p#[        US5      (       d  M  UR                  5         UR                  U(       a  UOS5        MD     U$ )a  Walk model and call init_non_persistent_buffers() on modules that have it.

This reinitializes computed buffers (like RoPE frequencies, attention bias indices)
that are marked as non-persistent and thus not saved in checkpoints. These buffers
are typically computed from module configuration and need to be reinitialized after
loading a checkpoint.

Args:
    model: Model to reinitialize buffers for

Returns:
    List of module names that were reinitialized

Example:
    >>> model = create_model('vit_base', pretrained=True)
    >>> # After loading checkpoint or moving to new device
    >>> reinitialized = reinit_non_persistent_buffers(model)
    >>> print(f"Reinitialized {len(reinitialized)} modules")
init_non_persistent_buffersz(root))r   hasattrr   r]   )r   reinitializedr*   r%   s       r#   r   r   :  sR    ( M++-6899..0  8< . r0   )F) TF)FF)r;   r   
sequential)r;   FFN))collections.abcrG   r   r[   r   	itertoolsr   typingr   r   r   r   r	   r
   r   r   r   r   torch.utils.checkpointr   r   timm.layersr   __all__Moduleboolr   r   rx   r   r   r`   r   r   r   r   r   r   r   r   r   rT   r0   r#   <module>r      s     	 #  T T T     *,
"BII "T " !# "			  	
 YY$  "			  	$  "			  	    $	:c3h0:T8^,: : 	:@ $	`		` ` 	` $	h		h h 	h .0;G	#c299n 56## c5c?*+# CtBII!778	#B %) D>2 (,I I 	I
 I  ~IXs  F 2 tCy r0   