ó
    pyüi4B  ã            
       ó  • S r 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	  \R                  " \5      r " S S\R                  5      r " S	 S
5      rS r " S S5      r    SS\S-  S\S-  S\S-  S\S-  4S jjrS rg)zACollection of utils to be used by backbones and their components.é    N)Úrepo_existsé   )Úlogging)Úmaybe_install_capturing_hooksc                   ó   • \ 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   ó    ÚX/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/backbone_utils.pyr   r      s   † Ø€DØ!ƒLr   r   c                   óä   ^ • \ rS rSrSrS\S-  S\S-  4S jrS r\S 5       r	\	R                  S\\   4S	 j5       r	\S
 5       r\R                  S\\S4   \\   -  4S j5       rU 4S jrSrU =r$ )ÚBackboneConfigMixiné#   zn
A Mixin to support handling the `out_features` and `out_indices` attributes for the backbone configurations.
Úout_featuresNÚout_indicesc                 ó  • Xl         [        U[        5      (       a  [        U5      OUU l        U R                  5         U R                   U R                  p!Uc-  Uc*  [        U R                  5      S-
  /nU R                  S   /nOUUc.  Ub+  U Vs/ s H  o0R                  R                  U5      PM     nnO$Uc!  Ub  U Vs/ s H  o@R                  U   PM     nnXsU l         U l        U R                  5         gs  snf s  snf )a(  
Sets output indices and features to new values and aligns them with the given `stage_names`.
If one of the inputs is not given, find the corresponding `out_features` or `out_indices`
for the given `stage_names`.

Args:
    out_features (`list[str]`, *optional*):
        The names of the features for the backbone to output. Defaults to `config._out_features` if not provided.
    out_indices (`list[int]` or `tuple[int]`, *optional*):
        The indices of the features for the backbone to output. Defaults to `config._out_indices` if not provided.
Nr   éÿÿÿÿ)	Ú_out_featuresÚ
isinstanceÚtupleÚlistÚ_out_indicesÚverify_out_features_out_indicesÚlenÚstage_namesÚindex)Úselfr   r   ÚlayerÚidxs        r   Ú"set_output_features_output_indicesÚ6BackboneConfigMixin.set_output_features_output_indices(   sþ   € ð  *ÔÜ1;¸KÌ×1OÑ1OœD Ô-ÐU`ˆÔð 	×,Ñ,Ô.ð %)×$6Ñ$6¸×8IÑ8I�kØÑ <Ñ#7Ü˜t×/Ñ/Ó0°1Ñ4Ð5ˆKØ ×,Ñ,¨RÑ0Ð1‰LØÑ  \Ñ%=ÙFRÓSÂl¸U×+Ñ+×1Ñ1°%Ö8ÁlˆKÐSˆKØÑ! kÑ&=Ù=HÓIº[°c×,Ñ,¨SÔ1¹[ˆLÐIð 1=Ð-ˆÔ˜DÔ-Ø×,Ñ,Õ.ùò TùâIs   Â$C9Ã C>c                 óÌ  ^ • T R                   c  [        S5      eT R                  Gb0  [        T R                  [        45      (       d!  [        S[        T R                  5       35      e[        U 4S jT R                   5       5      (       a%  [        ST R                    ST R                   35      e[        T R                  5      [        [        T R                  5      5      :w  a  [        ST R                   35      eT R                  T R                    Vs/ s H  oT R                  ;   d  M  UPM     sn=n:w  a  [        SU ST R                   35      eT R                  GbY  [        T R                  [        5      (       d!  [        S	[        T R                  5       35      e[        U 4S
 jT R                   5       5      n[        U 4S jU 5       5      (       a%  [        ST R                    ST R                   35      e[        U5      [        [        U5      5      :w  a4  ST R                   3nXCT R                  :w  a  SU S3OS-  n[        U5      eU[        [        U5      5      :w  aN  [        [        UT R                  5      S S9 VVs/ s H  u  pVUPM	     nnn[        SU ST R                   35      eT R                  b‰  T R                  b{  [        T R                  5      [        T R                  5      :w  a  [        S5      eT R                  T R                   Vs/ s H  nT R                   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R                   ;  v •  M     g 7f©N)r$   )Ú.0Úfeatr&   s     €r   Ú	<genexpr>ÚFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>V   s   øé € ÐOÒ<N°D˜t×/Ñ/Ö/Ò<Nù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   ób   >#   • U  H$  oS :  a  U[        TR                  5      -  OUv •  M&     g7f)r   N)r#   r$   ©r.   r(   r&   s     €r   r0   r1   g   s.   øé € Ð$rÒ`qÐY\È!ÃG S¬3¨t×/?Ñ/?Ó+@Ò%@ÐQTÔ%TÒ`qùs   ƒ,/c              3   ón   >#   • U  H*  o[        [        TR                  5      5      ;  d  M&  Uv •  M,     g 7fr-   )Úranger#   r$   r3   s     €r   r0   r1   h   s*   øé € Ð^Ò"2˜3ÄÄsÈ4×K[ÑK[ÓG\ÓA]Ñ6]—3‘3Ò"2ùs   ƒ%5¬	5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>ÚEBackboneConfigMixin.verify_out_features_out_indices.<locals>.<lambda>r   s   € ÐefÐghÒeir   )Ú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)r$   Ú
ValueErrorr   r   r    ÚtypeÚanyr#   Úsetr!   r   ÚsortedÚzip)r&   r/   Úsorted_featsÚpositive_indicesÚmsgÚ_r(   Úsorted_negatives   `       r   r"   Ú3BackboneConfigMixin.verify_out_features_out_indicesL   sG  ø€ ð ×ÑÑ#ÜÐQÓRÐRà×ÑÒ)Ü˜d×0Ñ0´4°'×:Ñ:Ü Ð#CÄDÈ×I[ÑI[ÓD\ÐC]Ð!^Ó_Ð_ÜÔO¸D×<NÒ<NÓO×OÑOÜ ØDÀT×EUÑEUÐDVÐV[Ð\`×\nÑ\nÐ[oÐpóð ô �4×%Ñ%Ó&¬#¬c°$×2DÑ2DÓ.EÓ*FÓFÜ Ð#UÐVZ×VhÑVhÐUiÐ!jÓkÐkØ×!Ñ!Ø26×2BÒ2BÓ aÒ2B¨$Èd×N`ÑN`ÑF`§Ñ2BÑ aÐa�óô !ØVÐWcÐVdÐdiÐjn×j|Ñj|Ði}Ð~óð ð ×ÑÒ(Ü˜d×/Ñ/´×6Ñ6Ü Ð#CÄDÈ×IZÑIZÓD[ÐC\Ð!]Ó^Ð^ä$Ô$rÐ`d×`qÒ`qÓ$rÓrÐÜÔ^Ñ"2Ó^×^Ñ^Ü ØHÈ×IYÑIYÐHZÐZ`Ðae×arÑarÐ`sÐtóð ô Ð#Ó$¬¬CÐ0@Ó,AÓ(BÓBØIÈ$×J[ÑJ[ÐI\Ð]�ØÐUY×UfÑUfÓAf˜Ð)9Ð(:¸"Ñ=ÐlnÑn�Ü  “oÐ%Ø¤5¬Ð0@Ó)AÓ#BÓBä&,¬SÐ1AÀ4×CTÑCTÓ-UÑ[iÒ&jô#Ú&j™F˜A“CÑ&jð  ñ #ô !ØUÐVeÐUfÐfkÐlp×l}Ñl}Ðk~Ðóð ð ×ÑÑ)¨d×.?Ñ.?Ñ.KÜ�4×%Ñ%Ó&¬#¨d×.?Ñ.?Ó*@Ó@Ü Ð!kÓlÐlØ×!Ñ!Àt×GXÒGXÓ%YÒGXÀ d×&6Ñ&6°sÔ&;ÑGXÑ%YÓYÜ Ð!tÓuÐuð Zð /LÐ)ùò7 !bùó(#ùò &Zs   ÄMÄ/MÊMÌ)M!c                 ó   • U R                   $ r-   )r   ©r&   s    r   r   Ú BackboneConfigMixin.out_features~   s   € à×!Ñ!Ð!r   c                 ó$   • U R                  USS9  g)úp
Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
N©r   r   )r)   ©r&   r   s     r   r   rJ   ‚   s   € ð
 	×/Ñ/¸\ÐW[Ð/Ò\r   c                 ó   • U R                   $ r-   )r!   rI   s    r   r   ÚBackboneConfigMixin.out_indices‰   s   € à× Ñ Ð r   .c                 óD   • Ub  [        U5      OUnU R                  SUS9  g)úo
Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices.
NrM   )r    r)   ©r&   r   s     r   r   rP   �   s(   € ð
 ,7Ñ+B”d˜;Ô'ÈˆØ×/Ñ/¸TÈ{Ð/Ò[r   c                 óx   >• [         TU ]  5       nUR                  SS5      US'   UR                  S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.
r   Nr   r!   r   )ÚsuperÚto_dictÚpop)r&   ÚoutputÚ	__class__s     €r   rV   ÚBackboneConfigMixin.to_dict•   s@   ø€ ô
 ‘‘Ó"ˆØ!'§¡¨O¸TÓ!Bˆˆ~ÑØ &§
¡
¨>¸4Ó @ˆˆ}ÑØˆr   )r   r!   )r   r   r   r   Ú__doc__r    r)   r"   Úpropertyr   ÚsetterÚstrr   r   ÚintrV   r   Ú__classcell__©rY   s   @r   r   r   #   s¾   ø† ñð"/à˜T‘kð"/ð ˜D‘[ô"/òH0vðd ñ"ó ð"ð ×Ñð]¨¨c©ó ]ó ð]ð ñ!ó ð!ð ×Ñð\ u¨S°#¨X¡¸¸c¹Ñ'Bó \ó ð\÷ó r   r   c                 óF   ^ • [         R                  " T 5      U 4S j5       nU$ )aW  
Wrapper to filer out `hidden_states` as backbones tend to always use them to get their feature maps, i.e.
they also always output `hidden_states`. This controls for user-defined behavior again.

NOTE: We assume a `can_return_tuple` decorator to be applied before so that we always expect a dict like
      object to remove the hidden states.
c                 ó  >• UR                  S[        U R                  SS5      5      nT" U /UQ70 UD6nU(       d=  UR                  5        VVs0 s H  u  pVUS;  d  M  XV_M     nnn[	        U5      " S0 UD6nU$ s  snnf )NÚoutput_hidden_statesFÚhidden_statesr   )ÚgetÚgetattrÚconfigÚitemsr=   )	r&   ÚargsÚkwargsrd   rX   ÚkÚvÚfiltered_output_dataÚforward_functions	           €r   ÚwrapperÚ,filter_output_hidden_states.<locals>.wrapper©   s�   ø€ à%Ÿz™zÐ*@Ä'È$Ï+É+ÐWmÐotÓBuÓvÐÙ! $Ð8¨Ò8°Ñ8ˆÞ#Ø5;·\±\´^Ô#b²^©T¨QÀqÐQ`ÑGa£D A¢D±^Ð Ñ#bÜ˜&”\Ñ9Ð$8Ñ9ˆFØˆùó $cs   ÁA;ÁA;)Ú	functoolsÚwraps)ro   rp   s   ` r   Úfilter_output_hidden_statesrt       s(   ø€ ô ‡_‚_Ð%Ó&ôó 'ðð €Nr   c                   óX  ^ • \ rS rSr% Sr\S-  \S'   Sr\\S'   SU 4S jjr	U 4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S j5       r\S 5       r\S 5       rS r   SS\S-  S\S-  S\S-  4S jjrSrU =r$ )ÚBackboneMixinéµ   NÚbackbone_typeTÚhas_attentionsc                 ó�  >• [         TU ]  " U0 UD6  UR                  SS5      nUb  [        R                  U l        O[        R                  U l        U R
                  [        R                  :X  a  U R                  US9  gU R
                  [        R                  :X  a  U R                  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.
Útimm_backboneN)Úbackbonezbackbone_type z not supported.)
rU   Ú__init__rW   r   r   rx   r   Ú_init_timm_backboneÚ_init_transformers_backboner<   )r&   rj   rk   r{   rY   s       €r   r}   ÚBackboneMixin.__init__¼   s©   ø€ ô
 	‰Ò˜$Ð) &Ò)ØŸ
™
 ?°DÓ9ˆØÑ$Ü!-×!2Ñ!2ˆDÕä!-×!:Ñ!:ˆDÔà×Ñ¤×!2Ñ!2Ó2Ø×$Ñ$¨mÐ$Ò<Ø×Ñ¤<×#<Ñ#<Ó<Ø×,Ñ,Õ.ä˜~¨d×.@Ñ.@Ð-AÀÐQÓRÐRr   c                 ó8   >• [         TU ]  5         [        U 5        g)am  
Override `post_init` to always install capturing hooks, as backbone will ALWAYS capture outputs. We need to do
it in `post_init`, as modules need to be already instantiated.
It avoids some mixups with `torch.compile`, as the first hook installation will need/create a graph break,
which can clash with external user call such as `model = torch.compile(model...)`.
N)rU   Ú	post_initr   )r&   rY   s    €r   r‚   ÚBackboneMixin.post_initÏ   s   ø€ ô 	‰ÑÔÜ% dÕ+r   c                 óz  • [        U R                  SS5      n[        U R                  SS5      nUR                  R                   Vs/ s H  oDS   PM	     snU l        UR                  R                   Vs/ s H  oDS   PM	     snU l        [        UR                  R                  5      nUR                  R                  5       nUb  X&:w  a  [        SU S35      eUb  X0R                  :w  a  [        SU S	35      eU R                  U R                  l        U R                  R                  Xe5        gs  snf s  snf )
zZ
Initialize the backbone model from timm. The backbone must already be loaded to backbone
r   Nr$   ÚmoduleÚnum_chsz!Config has `out_features` set to z‰ which doesn't match `out_features` from backbone's feature_info. Please check if your checkpoint has correct out features/indices saved.z Config has `stage_names` set to z� which doesn't match `stage_names` from backbone's feature_info. Please check if your checkpoint has correct `stage_names` saved.)rg   rh   Úfeature_infoÚinfor$   Únum_featuresr    r   Úmodule_namer<   r)   )r&   r|   Úout_features_from_configÚstage_names_from_configÚstager   r   s          r   r~   Ú!BackboneMixin._init_timm_backboneÛ   s;  € ô
 $+¨4¯;©;¸ÈÓ#MÐ Ü")¨$¯+©+°}ÀdÓ"KÐð
 :B×9NÑ9N×9SÒ9SÓTÒ9S° (œOÑ9SÑTˆÔØ;C×;PÑ;P×;UÒ;UÓVÒ;U°% 9Ô-Ñ;UÑVˆÔä˜8×0Ñ0×<Ñ<Ó=ˆØ×,Ñ,×8Ñ8Ó:ˆà#Ñ/Ð4LÓ4\ÜØ3Ð4LÐ3Mð Nxð xóð ð
 #Ñ.Ð3J×N^ÑN^Ó3^ÜØ2Ð3JÐ2Kð Lqð qóð ð #'×"2Ñ"2ˆ�‰ÔØ�‰×6Ñ6°|ÕQùò) UùÚVs   ÁD3Á5D8c                 ó|   • U R                   R                  U l        U R                   R                  5         S U l        g r-   )rh   r$   r"   r‰   rI   s    r   r   Ú)BackboneMixin._init_transformers_backboneü   s,   € ØŸ;™;×2Ñ2ˆÔØ�‰×3Ñ3Ô5à ˆÕr   c                 ó.   • U R                   R                  $ r-   )rh   r   rI   s    r   r   ÚBackboneMixin.out_features  s   € à�{‰{×(Ñ(Ð(r   r   c                 ó$   • XR                   l        g)rL   N)rh   r   rN   s     r   r   r’     s   € ð
 $0�‰Õ r   c                 ó.   • U R                   R                  $ r-   )rh   r!   rI   s    r   r   ÚBackboneMixin.out_indices  s   € à�{‰{×'Ñ'Ð'r   r   c                 ó$   • XR                   l        g)rR   N)rh   r   rS   s     r   r   r•     s   € ð
 #.�‰Õ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$   r‰   )r&   Úir�   s      r   Úout_feature_channelsÚ"BackboneMixin.out_feature_channels  s:   € ô =FÀd×FVÑFVÔ<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   rš   )r&   Únames     r   ÚchannelsÚBackboneMixin.channels  s+   € à<@×<MÒ<MÓNÒ<M°D×)Ñ)¨$Ô/Ñ<MÑNÐNùÒNs   �*c                 ó\  • U R                   (       d  UR                  SS 5        U R                  [        R                  :X  a^  [        [        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 )NÚoutput_attentions)ry   rW   rx   r   r   ÚdictÚinspectÚ	signatureÚforwardÚ
parametersri   )r&   rj   rk   r¤   rl   rm   s         r   Úforward_with_filtered_kwargsÚ*BackboneMixin.forward_with_filtered_kwargs"  s‡   € Ø×"×"Ø�J‰JÐ*¨DÔ1Ø×Ñ¤×!2Ñ!2Ó2ÜœW×.Ò.¨t¯|©|Ó<×GÑGÓHˆIØ'-§|¡|¤~ÔH¢~™t˜q¸¹“d�a’d¡~ˆFÑHÙ�TÐ$˜VÑ$Ð$ùó Is   ÂB(ÂB(rd   r¡   Úreturn_dictc                 ó   • [        S5      e)Nz7This method should be implemented by the derived class.)ÚNotImplementedError)r&   Úpixel_valuesrd   r¡   r©   s        r   r¥   ÚBackboneMixin.forward*  s   € ô "Ð"[Ó\Ð\r   )rx   r‰   r$   )ÚreturnN)NNN)r   r   r   r   rx   r   Ú__annotations__ry   Úboolr}   r‚   r~   r   r\   r   r]   r    r^   r   r   r_   rš   rž   r§   r¥   r   r`   ra   s   @r   rv   rv   µ   s%  ø‡ Ø)-€M�< $Ñ&Ó-ð  €N�DÓ÷Sõ&
,ôRôB!ð ñ)ó ð)ð ×Ñð0¨¨c©ó 0ó ð0ð ñ(ó ð(ð ×Ñð. u¨S¡z°D¸±IÑ'=ó .ó ð.ð ñYó ðYð
 ñOó ðOò%ð -1Ø)-Ø#'ñ]ð # T™kð]ð   $™;ð	]ð
 ˜D‘[÷]ó ]r   rv   Údefault_backboneÚdefault_config_typeÚdefault_config_kwargsÚtimm_default_kwargsc                 óÂ  • SSK Jn  SSKJn  UR	                  SS5      nUR	                  S0 5      n	UR                  S5      b  UR	                  S5      OUn
UR	                  SS 5        Ub$  U(       a  U
b  U c  U	(       d  US	   " SSU
0UD6n X4$ U
bZ  U cW  [        U
5      (       a6  UR                  U
5      u  p¼X{S
      nUR                  U	5        U" S0 UD6n X4$ US	   " SSU
0U	D6n  X4$ U c4  Ub1  [        R                  SU S35        U=(       d    0 nXr   " S0 UD6n X4$ [        U [        5      (       a&  U R                  S
5      nX~   nUR                  U 5      n X4$ )Nr   )ÚPreTrainedConfig)ÚCONFIG_MAPPINGÚuse_timm_backboneTÚbackbone_kwargsr|   Úuse_pretrained_backboner{   Ú
model_typezG`backbone_config` is `None`. Initializing the config with the default `z` vision config.r   )Úconfiguration_utilsr¶   Úmodels.autor·   rW   rf   r   Úget_config_dictÚupdateÚloggerrˆ   r   r¢   Ú	from_dict)Úbackbone_configr±   r²   r³   r´   rk   r¶   r·   r¸   r¹   r|   Úconfig_dictrE   Úconfig_classÚbackbone_model_types                  r   Ú%consolidate_backbone_kwargs_to_configrÆ   4  s­  € õ 6Ý+àŸ
™
Ð#6¸Ó=ÐØ—j‘jÐ!2°BÓ7€OØ)/¯©°JÓ)?Ñ)Kˆv�z‰z˜*Ô%ÐQa€HØ
‡J�JÐ(¨$Ô/ð
 	Ñ'ÞØÑ ØÑ#Þà(¨Ò9ÑcÀ8ÐcÐObÑcˆð( Ð"Ð"ð' 
Ñ	 /Ñ"9Ü�x× Ñ Ø-×=Ñ=¸hÓG‰NˆKØ)°lÑ*CÑDˆLØ×Ñ˜Ô/Ù*Ñ9¨[Ñ9ˆOð Ð"Ð"ð -¨_Ò=ÑcÀxÐcÐSbÑc‰Oð Ð"Ð"ð 
Ñ	 Ð%8Ñ%DÜ�‰ØUÐViÐUjÐjzÐ{ô	
ð !6× ;¸ÐØ(Ò=ÑVÐ@UÑVˆð Ð"Ð"ô 
�O¤T×	*Ñ	*Ø-×1Ñ1°,Ó?ÐØ%Ñ:ˆØ&×0Ñ0°ÓAˆàÐ"Ð"r   c                 ór   • SSK Jn  [        U SS5      nUc  UR                  U S9nU$ 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   )ÚAutoBackbonerÂ   N)rh   )r   rÈ   rg   Úfrom_config)rh   rÈ   rÂ   r|   s       r   Úload_backbonerÊ   f  sP   € õ *ä˜fÐ&7¸Ó>€OàÑØ×+Ñ+°6Ð+Ð:ˆð €Oð  ×+Ñ+°?Ð+ÐCˆØ€Or   )NNNN)r[   Úenumrr   r£   Úhuggingface_hubr   Úutilsr   Úutils.output_capturingr   Ú
get_loggerr   rÀ   ÚEnumr   r   rt   rv   r^   r¢   rÆ   rÊ   r   r   r   Ú<module>rÑ      s¯   ðñ Hã Û Û å 'å Ý Að 
×	Ò	˜HÓ	%€ô"�4—9‘9ô "÷
zñ zòz÷*|]ñ |]ðB $(Ø&*Ø)-Ø'+ñ/#à˜D‘jð/#ð ˜t™ð/#ð   $™;ð	/#ð
  ™õ/#ódr   