
    fThD                        S r SSKrSSKrSSKJr  SSKJrJrJr  \(       a  SSK	J
r
   " S S\R                  5      rS	\\\      S
\\\      S\\\      4S jrS	\\\      S
\\\\   \\   4      S\\   4S jrS	\\\      S
\\\\   \\   4      S\\   S\\\   \\   4   4S jr " S S5      r " S S5      rS rS\S\S\\   S\\\S4      S\\   4
S jrg)zACollection of utils to be used by backbones and their components.    N)Iterable)TYPE_CHECKINGOptionalUnion   )PretrainedConfigc                       \ rS rSrSrSrSrg)BackboneType   timmtransformers N)__name__
__module____qualname____firstlineno__TIMMTRANSFORMERS__static_attributes__r       Y/var/www/auris/envauris/lib/python3.13/site-packages/transformers/utils/backbone_utils.pyr
   r
      s    D!Lr   r
   out_featuresout_indicesstage_namesc                 X  ^ Tc  [        S5      eU b  [        U [        45      (       d  [        S[        U 5       35      e[	        U4S jU  5       5      (       a  [        ST SU  35      e[        U 5      [        [        U 5      5      :w  a  [        SU  35      eU T Vs/ s H  o3U ;   d  M
  UPM     sn=n:w  a  [        SU SU  35      eUb  [        U[        5      (       d  [        S	[        U5       35      e[        U4S
 jU 5       5      n[	        U4S jU 5       5      (       a  [        ST SU 35      e[        U5      [        [        U5      5      :w  a   SU 3nXeU:w  a  SU S3OS-  n[        U5      eU[        [        U5      5      :w  a9  [        [        XQ5      S S9 VVs/ s H  u  pxUPM	     n	nn[        SU	 SU 35      eU bL  UbH  [        U 5      [        U5      :w  a  [        S5      eX Vs/ s H  nTU   PM
     sn:w  a  [        S5      egggs  snf s  snnf s  snf )zO
Verify that out_indices and out_features are valid for the given stage_names.
Nz2Stage_names must be set for transformers backbonesz out_features must be a list got c              3   ,   >#    U  H	  oT;  v   M     g 7fNr   ).0featr   s     r   	<genexpr>2verify_out_features_out_indices.<locals>.<genexpr>,   s     @<4;&<s   z.out_features must be a subset of stage_names: z got z2out_features must not contain any duplicates, got z@out_features must be in the same order as stage_names, expected z out_indices must be a list, got c              3   N   >#    U  H  oS :  a  U[        T5      -  OUv   M     g7f)r   N)lenr   idxr   s     r   r    r!   9   s'      cWbPS7s;'7!7!KWbs   "%c              3   Z   >#    U  H   o[        [        T5      5      ;  d  M  Uv   M"     g 7fr   )ranger#   r$   s     r   r    r!   :   s$     U.sU3{CS=T2Tss.s   +	+z2out_indices must be valid indices for stage_names z, got z1out_indices must not contain any duplicates, got z(equivalent to z)) c                     U S   $ )Nr   r   )xs    r   <lambda>1verify_out_features_out_indices.<locals>.<lambda>A   s    jklmjnr   )keyz?out_indices must be in the same order as stage_names, expected zHout_features and out_indices should have the same length if both are setzQout_features and out_indices should correspond to the same stages if both are set)

ValueError
isinstancelisttypeanyr#   settuplesortedzip)
r   r   r   r   sorted_featspositive_indicesmsg_r%   sorted_negatives
     `       r   verify_out_features_out_indicesr<       sm    MNN,00?\@R?STUU@<@@@Mk]Z_`l_mnoo|C$5 66QR^Q_`aak,bkdUaMaTk,bbLcRS_R``efrest  +t,,?[@Q?RSTT  cWb ccU.UUUQR]Q^^depdqrss C,<(=$>>Ek]SCQ\=\_%5$6b9bddCS/!uV,<%=>>17<L8Z`n1op1ovqs1oOpQRaQbbghsgtu  K$;|K 00ghhDK,DDpqq E %<- -c" q Es   	H,H*H!9H'c                     Uc  U c  [        U5      S-
  /nUS   /n X4$ Uc&  U b#  U  Vs/ s H  o2R                  U5      PM     nnX4$ U c  Ub  U Vs/ s H  oBU   PM	     n nX4$ s  snf s  snf )aS  
Finds the corresponding `out_features` and `out_indices` for the given `stage_names`.

The logic is as follows:
    - `out_features` not set, `out_indices` set: `out_features` is set to the `out_features` corresponding to the
    `out_indices`.
    - `out_indices` not set, `out_features` set: `out_indices` is set to the `out_indices` corresponding to the
    `out_features`.
    - `out_indices` and `out_features` not set: `out_indices` and `out_features` are set to the last stage.
    - `out_indices` and `out_features` set: input `out_indices` and `out_features` are returned.

Args:
    out_features (`List[str]`): The names of the features for the backbone to output.
    out_indices (`List[int]` or `Tuple[int]`): The indices of the features for the backbone to output.
    stage_names (`List[str]`): The names of the stages of the backbone.
   )r#   index)r   r   r   layerr%   s        r   %_align_output_features_output_indicesrB   M   s    * |3;'!+,#B(
 $$	 
	!9=IJ\E((/\J $$ 
	+"94?@KSC(K@$$ K@s   A%A*returnc                 h    Ub  [        U5      OSn[        XUS9  [        XUS9u  p4[        X4US9  X44$ )a,  
Get the `out_features` and `out_indices` so that they are aligned.

The logic is as follows:
    - `out_features` not set, `out_indices` set: `out_features` is set to the `out_features` corresponding to the
    `out_indices`.
    - `out_indices` not set, `out_features` set: `out_indices` is set to the `out_indices` corresponding to the
    `out_features`.
    - `out_indices` and `out_features` not set: `out_indices` and `out_features` are set to the last stage.
    - `out_indices` and `out_features` set: they are verified to be aligned.

Args:
    out_features (`List[str]`): The names of the features for the backbone to output.
    out_indices (`List[int]` or `Tuple[int]`): The indices of the features for the backbone to output.
    stage_names (`List[str]`): The names of the stages of the backbone.
Nr   r   r   )r0   r<   rB   )r   r   r   output_featuresoutput_indicess        r   *get_aligned_output_features_output_indicesrH   l   sH    * (3'>${#DK#dop&K!'#O $juv**r   c                   H  ^  \ rS rSr% Sr\\   \S'   SS jrSS jr	SS jr
\S 5       r\R                  S\\   4S	 j5       r\S
 5       r\R                  S\\\   \\   4   4S j5       r\S 5       r\S 5       rS r   SS\\   S\\   S\\   4S jjrU 4S jrSrU =r$ )BackboneMixin   Nbackbone_typec                    [        U SS5      c  [        S5      eU R                  R                  R                   Vs/ s H  o"S   PM	     snU l        U R                  R                  R                   Vs/ s H  o"S   PM	     snU l        [        U R                  R                  R                  5      nU R                  R                  R                  5       n[        XCU R
                  S9  XCsU l        U l        gs  snf s  snf )z_
Initialize the backbone model from timm The backbone must already be loaded to self._backbone
	_backboneNz=self._backbone must be set before calling _init_timm_backbonemodulenum_chsrE   )getattrr.   rN   feature_infoinfor   num_featuresr0   r   module_namer<   _out_features_out_indices)selfconfigstager   r   s        r   _init_timm_backbone!BackboneMixin._init_timm_backbone   s     4d+3\]]
 :>9T9T9Y9YZ9Y(O9YZ;?>>;V;V;[;[\;[%9-;[\ 4>>66BBC~~22>>@ 	(%DL\L\	
 1=-D- [\s   C:4C?c                     [        US5      n[        USS 5      n[        USS 5      nX l        [        X4US9u  U l        U l        S U l        g )Nr   r   r   rE   )rQ   r   rH   rV   rW   rT   )rX   rY   r   r   r   s        r   _init_transformers_backbone)BackboneMixin._init_transformers_backbone   sT    fm4v~t<fmT:&0Z%K1
-D- !r   c                    Xl         [        USS5      U l        U R                  (       a  [        R                  O[        R
                  U l        U R                  [        R                  :X  a  U R                  U5        gU R                  [        R
                  :X  a  U R                  U5        g[        SU R                   S35      e)z
Method to initialize the backbone. This method is called by the constructor of the base class after the
pretrained model weights have been loaded.
use_timm_backboneFzbackbone_type z not supported.N)
rY   rQ   ra   r
   r   r   rL   r[   r^   r.   )rX   rY   s     r   _init_backboneBackboneMixin._init_backbone   s    
 !(1De!L262H2H\..lNgNg!2!22$$V,<#<#<<,,V4~d.@.@-AQRRr   c                     U R                   $ r   rV   rX   s    r   r   BackboneMixin.out_features       !!!r   r   c                 H    [        USU R                  S9u  U l        U l        gzp
Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
NrE   rH   r   rV   rW   rX   r   s     r   r   rg      '    
 1[%4TEUEU1
-D-r   c                     U R                   $ r   rW   rf   s    r   r   BackboneMixin.out_indices          r   r   c                 F    [        SXR                  S9u  U l        U l        gzo
Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices.
NrE   rk   rX   r   s     r   r   rp      %    
 1[;DTDT1
-D-r   c                 |    [        U R                  5       VVs0 s H  u  pX R                  U   _M     snn$ s  snnf r   )	enumerater   rT   )rX   irZ   s      r   out_feature_channels"BackboneMixin.out_feature_channels   s:     =FdFVFV<WX<W((++<WXXXs   8c                 ^    U R                    Vs/ s H  oR                  U   PM     sn$ s  snf r   )r   ry   )rX   names     r   channelsBackboneMixin.channels   s+    <@<M<MN<MD))$/<MNNNs   *c                     [        [        R                  " U R                  5      R                  5      nUR                  5        VVs0 s H  u  pEXC;   d  M  XE_M     nnnU " U0 UD6$ s  snnf r   )dictinspect	signatureforward
parametersitems)rX   argskwargsr   kvfiltered_kwargss          r   forward_with_filtered_kwargs*BackboneMixin.forward_with_filtered_kwargs   s[    **4<<8CCD	,2LLNMNDAan414NMT-_-- Ns   A'A'output_hidden_statesoutput_attentionsreturn_dictc                     [        S5      e)Nz7This method should be implemented by the derived class.)NotImplementedError)rX   pixel_valuesr   r   r   s        r   r   BackboneMixin.forward   s     ""[\\r   c                 t   > [         TU ]  5       nUR                  S5      US'   UR                  S5      US'   U$ z
Serializes this instance to a Python dictionary. Override the default `to_dict()` from `PretrainedConfig` to
include the `out_features` and `out_indices` attributes.
rV   r   rW   r   superto_dictpoprX   output	__class__s     r   r   BackboneMixin.to_dict   <    
 "!'O!<~ &

> :}r   )rV   rW   rL   rY   rT   r   ra   )rC   N)NNN)r   r   r   r   rL   r   r
   __annotations__r[   r^   rb   propertyr   setterr0   strr   r   r4   intry   r}   r   boolr   r   r   __classcell__r   s   @r   rJ   rJ      s   ,0M8L)0J0
!S" " " 
c 
 
 ! ! 
uU3Zc-B'C 
 
 Y Y
 O O. 04,0&*] 'tn] $D>	]
 d^] r   rJ   c                      ^  \ rS rSrSr\S 5       r\R                  S\\	   4S j5       r\S 5       r
\
R                  S\\\   \\   4   4S j5       r
U 4S	 jrS
rU =r$ )BackboneConfigMixini  zn
A Mixin to support handling the `out_features` and `out_indices` attributes for the backbone configurations.
c                     U R                   $ r   re   rf   s    r   r    BackboneConfigMixin.out_features  rh   r   r   c                 H    [        USU R                  S9u  U l        U l        grj   rk   rl   s     r   r   r   
  rm   r   c                     U R                   $ r   ro   rf   s    r   r   BackboneConfigMixin.out_indices  rq   r   r   c                 F    [        SXR                  S9u  U l        U l        grs   rk   rt   s     r   r   r     ru   r   c                 t   > [         TU ]  5       nUR                  S5      US'   UR                  S5      US'   U$ r   r   r   s     r   r   BackboneConfigMixin.to_dict   r   r   )rV   rW   )r   r   r   r   __doc__r   r   r   r0   r   r   r   r4   r   r   r   r   r   s   @r   r   r     s     " " 
c 
 
 ! ! 
uU3Zc-B'C 
 
 r   r   c                 R   SSK JnJn  [        U SS5      n[        U SS5      n[        U SS5      n[        U SS5      n[        U SS5      nUc  0 OUnU(       a  Ub  [	        S	5      eUb  Ub  Ub  [	        S
5      eUc  Uc  Uc  Uc  UR
                  " SSU 0UD6$ U(       a&  Uc  [	        S5      eUR                  " U4UUS.UD6nU$ U(       a#  Uc  [	        S5      eUR                  " U40 UD6nU$ Uc  Uc  [	        S5      eUc  UR                  " U40 UD6nUR                  US9nU$ )a&  
Loads the backbone model from a config object.

If the config is from the backbone model itself, then we return a backbone model with randomly initialized
weights.

If the config is from the parent model of the backbone model itself, then we load the pretrained backbone weights
if specified.
r   )AutoBackbone
AutoConfigbackbone_configNra   use_pretrained_backbonebackbonebackbone_kwargs?You can't specify both `backbone_kwargs` and `backbone_config`.z>Cannot specify both config.backbone_config and config.backbonerY   z8config.backbone must be set if use_timm_backbone is True)ra   r   z>config.backbone must be set if use_pretrained_backbone is Truez<Either config.backbone_config or config.backbone must be set)rY   r   )r   r   r   rQ   r.   from_configfrom_pretrained)	rY   r   r   r   ra   r   backbone_checkpointr   r   s	            r   load_backboner   +  s    6f&7>O(;TB%f.GN!&*d;f&7>O+3bO?6Z[[ "':'FKbKnYZZ 	%''''IvIII &WXX  //
/$;
 	
  O 
!&]^^//0CWW O "':'B[\\"(889L`P_`O++?+COr   ra   r   r   r   r   r   c                     Ub  Ub  [        S5      eUb  U (       a  [        S5      eUb  U(       a  Ub  [        S5      eggg)zJ
Verify that the config arguments to be passed to load_backbone are valid
Nz8You can't specify both `backbone` and `backbone_config`.zAYou can't specify both `backbone_config` and `use_timm_backbone`.r   )r.   )ra   r   r   r   r   s        r    verify_backbone_config_argumentsr   h  sT     "x';STT"'8\]]"?;VZ[[ <W"r   )r   enumr   collections.abcr   typingr   r   r   configuration_utilsr   Enumr
   r   r   r<   r0   r4   rB   rH   rJ   r   r   r   r   r   r   r   r   <module>r      s   H   $ 1 1 6"499 "
*r8C=)*r8@#8O*r^fgopsgt^u*rZ%49%%%S	5: 567% c%>+49%+%S	5: 567+ c+ 49d3i 	+@r rj' 'T:z\\!\ sm\ eD*<$<=>	\
 d^\r   