Ë
    T^(hD  ã                   ó®  — d Z ddlZddlZddlmZ ddlmZmZmZ erddl	m
Z
  G d„ dej                  «      Zd	eee      d
eee      deee      fd„Zd	eee      d
eeee   ee   f      dee   fd„Zd	eee      d
eeee   ee   f      dee   deee   ee   f   fd„Z G d„ d«      Z G d„ d«      Zd„ Zdededee   deeedf      dee   f
d„Zy)zACollection of utils to be used by backbones and their components.é    N)ÚIterable)ÚTYPE_CHECKINGÚOptionalÚUnioné   )ÚPretrainedConfigc                   ó   — e Zd ZdZdZy)ÚBackboneTypeÚtimmÚtransformersN)Ú__name__Ú
__module__Ú__qualname__ÚTIMMÚTRANSFORMERS© ó    ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/utils/backbone_utils.pyr
   r
      s   „ Ø€DØ!�Lr   r
   Úout_featuresÚout_indicesÚstage_namesc                 ó
  ‡— ‰€t        d«      ‚| �¦t        | t        f«      st        dt        | «      › �«      ‚t	        ˆfd„| D «       «      rt        d‰› d| › �«      ‚t        | «      t        t        | «      «      k7  rt        d| › �«      ‚| ‰D �cg c]	  }|| v sŒ|‘Œ c}x}k7  rt        d|› d| › �«      ‚|�òt        |t        «      st        d	t        |«      › �«      ‚t        ˆfd
„|D «       «      }t	        ˆfd„|D «       «      rt        d‰› d|› �«      ‚t        |«      t        t        |«      «      k7  r d|› �}|||k7  rd|› d�ndz  }t        |«      ‚|t        t        |«      «      k7  r;t        t        ||«      d„ ¬«      D ��cg c]  \  }}|‘Œ	 }	}}t        d|	› d|› �«      ‚| �G|�Dt        | «      t        |«      k7  rt        d«      ‚| |D �cg c]  }‰|   ‘Œ	 c}k7  rt        d«      ‚yyyc c}w c c}}w c c}w )zW
    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   ó&   •K  — | ]  }|‰v–— Œ
 y ­w©Nr   )Ú.0Úfeatr   s     €r   ú	<genexpr>z2verify_out_features_out_indices.<locals>.<genexpr>,   s   øè ø€ Ò@¨4ˆt˜;Ô&Ñ@ù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   óH   •K  — | ]  }|d k  r|t        ‰«      z  n|–— Œ y­w)r   N)Úlen©r   Úidxr   s     €r   r   z2verify_out_features_out_indices.<locals>.<genexpr>9   s'   øè ø€ Ò cÐPS¸3Àº7 ¤s¨;Ó'7Ò!7ÈÓ!KÑ cùs   ƒ"c              3   óP   •K  — | ]  }|t        t        ‰«      «      vsŒ|–— Œ y ­wr   )Úranger   r    s     €r   r   z2verify_out_features_out_indices.<locals>.<genexpr>:   s"   øè ø€ ÒU�s°#¼UÄ3À{ÓCSÓ=TÒ2TŒsÑUù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                 ó   — | d   S )Nr   r   )Úxs    r   ú<lambda>z1verify_out_features_out_indices.<locals>.<lambda>A   s   € ÐjkÐlmÑjn€ r   )Ú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_indicesr7       s_  ø€ ð ÐÜÐMÓNÐNàÐÜ˜,¬¨Ô0ÜÐ?ÄÀ\Ó@RÐ?SÐTÓUÐUÜÓ@°<Ô@Ô@ÜÐMÈkÈ]ÐZ_Ð`lÐ_mÐnÓoÐoÜˆ|Ó¤¤C¨Ó$5Ó 6Ò6ÜÐQÐR^ÐQ_Ð`ÓaÐaØ¸kÖ,b°dÈTÐUaÒMaªTÒ,bÐb˜LÒcÜØRÐS_ÐR`Ð`eÐfrÐesÐtóð ð ÐÜ˜+¤tÔ,ÜÐ?ÄÀ[Ó@QÐ?RÐSÓTÐTä Ó cÐWbÔ cÓcÐÜÓUÐ.ÔUÔUÜÐQÐR]ÐQ^Ð^dÐepÐdqÐrÓsÐsÜÐÓ ¤C¬Ð,<Ó(=Ó$>Ò>ØEÀkÀ]ÐSˆCØÐ=MÐQ\Ò=\�_Ð%5Ð$6°bÑ9ÐbdÑdˆCÜ˜S“/Ð!Øœu¤VÐ,<Ó%=Ó>Ò>Ü17¼Ð<LÈkÓ8ZÑ`nÔ1o×p¡v q¨#šsÐpˆOÑpÜØQÐRaÐQbÐbgÐhsÐgtÐuóð ð Ð KÐ$;Üˆ|Ó¤ KÓ 0Ò0ÜÐgÓhÐhØ¸ÖD°˜K¨Ó,ÒDÒDÜÐpÓqÐqð Eð %<Ðùò- -cùó" qùò Es   Â	G5ÂG5ÆG:ÇH c                 óÐ   — |€| €t        |«      dz
  g}|d   g} | |fS |€$| �"| D �cg c]  }|j                  |«      ‘Œ }}| |fS | €|�|D �cg c]  }||   ‘Œ	 } }| |fS c c}w c c}w )a‡  
    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_indicesr=   M   s«   € ð* Ð˜|Ð3Ü˜;Ó'¨!Ñ+Ð,ˆØ# B™Ð(ˆð
 ˜Ð$Ð$ð	 
Ð	 Ð!9Ø=IÖJ°E�{×(Ñ(¨Õ/ÐJˆÐJð ˜Ð$Ð$ð 
Ð	 +Ð"9Ø4?Ö@¨S˜ CÓ(Ð@ˆÐ@Ø˜Ð$Ð$ùò Kùâ@s   ¦AÁA#Úreturnc                 ó‚   — |�t        |«      nd}t        | ||¬«       t        | ||¬«      \  }}t        |||¬«       ||fS )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   )r+   r7   r=   )r   r   r   Úoutput_featuresÚoutput_indicess        r   Ú*get_aligned_output_features_output_indicesrC   l   sR   € ð* (3Ð'>”$�{Ô#ÀD€Kä#°È;ÐdoÕpÜ&KØ!¨{Èô'Ñ#€O�^ô $°ÈnÐjuÕvØ˜NÐ*Ð*r   c                   ó0  ‡ — e Zd ZU dZee   ed<   dd„Zdd„Zdd„Z	e
d„ «       Zej                  dee   fd„«       Ze
d	„ «       Zej                  d
eee   ee   f   fd„«       Ze
d„ «       Ze
d„ «       Zd„ Z	 	 	 ddee   dee   dee   fd„Zˆ fd„Zˆ xZS )ÚBackboneMixinNÚbackbone_typec                 ó  — t        | dd«      €t        d«      ‚| j                  j                  j                  D �cg c]  }|d   ‘Œ	 c}| _        | j                  j                  j                  D �cg c]  }|d   ‘Œ	 c}| _        t        | j                  j                  j                  «      }| j                  j                  j                  «       }t        ||| j
                  ¬«       ||c| _        | _        yc c}w c c}w )zo
        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_chsr@   )Úgetattrr)   rH   Úfeature_infoÚinfor   Únum_featuresr+   r   Úmodule_namer7   Ú_out_featuresÚ_out_indices)ÚselfÚconfigÚstager   r   s        r   Ú_init_timm_backbonez!BackboneMixin._init_timm_backbone�   sØ   € ô �4˜ dÓ+Ð3ÜÐ\Ó]Ð]ð
 :>¿¹×9TÑ9T×9YÑ9YÖZ°˜E (›OÒZˆÔØ;?¿>¹>×;VÑ;V×;[Ñ;[Ö\°%˜U 9Ó-Ò\ˆÔô ˜4Ÿ>™>×6Ñ6×BÑBÓCˆØ—~‘~×2Ñ2×>Ñ>Ó@ˆô 	(Ø%°;ÈD×L\ÑL\õ	
ð 1=¸kÐ-ˆÔ˜DÕ-ùò [ùÚ\s   »C:Á1C?c                 ó¢   — t        |d«      }t        |dd «      }t        |dd «      }|| _        t        |||¬«      \  | _        | _        d | _        y )Nr   r   r   r@   )rK   r   rC   rP   rQ   rN   )rR   rS   r   r   r   s        r   Ú_init_transformers_backbonez)BackboneMixin._init_transformers_backbone§   sX   € Ü˜f mÓ4ˆÜ˜v ~°tÓ<ˆÜ˜f m°TÓ:ˆà&ˆÔÜ0ZØ%°;ÈKô1
Ñ-ˆÔ˜DÔ-ð !ˆÕr   c                 ó„  — || _         t        |dd«      | _        | j                  rt        j                  nt        j
                  | _        | j                  t        j                  k(  r| j                  |«       y| j                  t        j
                  k(  r| j                  |«       yt        d| j                  › d�«      ‚)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)
rS   rK   rY   r
   r   r   rF   rU   rW   r)   )rR   rS   s     r   Ú_init_backbonezBackboneMixin._init_backbone³   sš   € ð
 ˆŒä!(¨Ð1DÀeÓ!LˆÔØ26×2HÒ2Hœ\×.Ò.Ìl×NgÑNgˆÔà×Ñ¤×!2Ñ!2Ò2Ø×$Ñ$ VÕ,Ø×Ñ¤<×#<Ñ#<Ò<Ø×,Ñ,¨VÕ4ä˜~¨d×.@Ñ.@Ð-AÀÐQÓRÐRr   c                 ó   — | j                   S r   ©rP   ©rR   s    r   r   zBackboneMixin.out_featuresÄ   ó   € à×!Ñ!Ð!r   r   c                 óN   — t        |d| j                  ¬«      \  | _        | _        y©z€
        Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
        Nr@   ©rC   r   rP   rQ   ©rR   r   s     r   r   zBackboneMixin.out_featuresÈ   ó'   € ô
 1[Ø%°4ÀT×EUÑEUô1
Ñ-ˆÔ˜DÕ-r   c                 ó   — | j                   S r   ©rQ   r]   s    r   r   zBackboneMixin.out_indicesÑ   ó   € à× Ñ Ð r   r   c                 óN   — t        d|| j                  ¬«      \  | _        | _        y©z
        Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices.
        Nr@   ra   ©rR   r   s     r   r   zBackboneMixin.out_indicesÕ   ó'   € ô
 1[Ø¨;ÀD×DTÑDTô1
Ñ-ˆÔ˜DÕ-r   c                 óz   — t        | j                  «      D ��ci c]  \  }}|| j                  |   “Œ c}}S c c}}w r   )Ú	enumerater   rN   )rR   ÚirT   s      r   Úout_feature_channelsz"BackboneMixin.out_feature_channelsÞ   s8   € ô =FÀd×FVÑFVÓ<W×X±°°5��t×(Ñ(¨Ñ+Ñ+ÓXÐXùÓXs   ™7c                 óZ   — | j                   D �cg c]  }| j                  |   ‘Œ c}S c c}w r   )r   rn   )rR   Únames     r   ÚchannelszBackboneMixin.channelsä   s'   € à<@×<MÑ<MÖN°D�×)Ñ)¨$Ó/ÒNÐNùÒNs   �(c                 óÒ   — t        t        j                  | j                  «      j                  «      }|j                  «       D ��ci c]  \  }}||v sŒ||“Œ }}} | |i |¤ŽS c c}}w r   )ÚdictÚinspectÚ	signatureÚforwardÚ
parametersÚitems)rR   ÚargsÚkwargsru   ÚkÚvÚfiltered_kwargss          r   Úforward_with_filtered_kwargsz*BackboneMixin.forward_with_filtered_kwargsè   s^   € Üœ×*Ñ*¨4¯<©<Ó8×CÑCÓDˆ	Ø,2¯L©L«N×M¡D A q¸aÀ9ºn˜1˜a™4ÐMˆÑMÙ�TÐ-˜_Ñ-Ð-ùó Ns   ÁA#ÁA#Úoutput_hidden_statesÚoutput_attentionsÚreturn_dictc                 ó   — t        d«      ‚)Nz7This method should be implemented by the derived class.)ÚNotImplementedError)rR   Úpixel_valuesr   r€   r�   s        r   rv   zBackboneMixin.forwardí   s   € ô "Ð"[Ó\Ð\r   c                 ót   •— t         ‰| �  «       }|j                  d«      |d<   |j                  d«      |d<   |S ©z¿
        Serializes this instance to a Python dictionary. Override the default `to_dict()` from `PretrainedConfig` to
        include the `out_features` and `out_indices` attributes.
        rP   r   rQ   r   ©ÚsuperÚto_dictÚpop©rR   ÚoutputÚ	__class__s     €r   r‰   zBackboneMixin.to_dictö   ó<   ø€ ô
 ‘‘Ó"ˆØ!'§¡¨OÓ!<ˆˆ~ÑØ &§
¡
¨>Ó :ˆˆ}ÑØˆr   )r>   N)NNN)r   r   r   rF   r   r
   Ú__annotations__rU   rW   rZ   Úpropertyr   Úsetterr+   Ústrr   r   r/   Úintrn   rq   r~   Úboolrv   r‰   Ú__classcell__©r�   s   @r   rE   rE   Œ   s  ø… Ø,0€M�8˜LÑ)Ó0óJó0
!óSð" ñ"ó ð"ð ×Ñð
¨¨c©ò 
ó ð
ð ñ!ó ð!ð ×Ñð
 u¨U°3©Z¸¸c¹Ð-BÑ'Cò 
ó ð
ð ñYó ðYð
 ñOó ðOò.ð 04Ø,0Ø&*ñ]ð ' t™nð]ð $ D™>ð	]ð
 ˜d‘^ó]÷ð r   rE   c                   ó²   ‡ — e Zd ZdZed„ «       Zej                  dee   fd„«       Zed„ «       Z	e	j                  de
ee   ee   f   fd„«       Z	ˆ fd„Zˆ xZS )	ÚBackboneConfigMixinzv
    A Mixin to support handling the `out_features` and `out_indices` attributes for the backbone configurations.
    c                 ó   — | j                   S r   r\   r]   s    r   r   z BackboneConfigMixin.out_features  r^   r   r   c                 óN   — t        |d| j                  ¬«      \  | _        | _        yr`   ra   rb   s     r   r   z BackboneConfigMixin.out_features
  rc   r   c                 ó   — | j                   S r   re   r]   s    r   r   zBackboneConfigMixin.out_indices  rf   r   r   c                 óN   — t        d|| j                  ¬«      \  | _        | _        yrh   ra   ri   s     r   r   zBackboneConfigMixin.out_indices  rj   r   c                 ót   •— t         ‰| �  «       }|j                  d«      |d<   |j                  d«      |d<   |S r†   r‡   r‹   s     €r   r‰   zBackboneConfigMixin.to_dict   rŽ   r   )r   r   r   Ú__doc__r�   r   r‘   r+   r’   r   r   r/   r“   r‰   r•   r–   s   @r   r˜   r˜     s’   ø„ ñð ñ"ó ð"ð ×Ñð
¨¨c©ò 
ó ð
ð ñ!ó ð!ð ×Ñð
 u¨U°3©Z¸¸c¹Ð-BÑ'Cò 
ó ð
÷ð r   r˜   c                 ó  — ddl m}m} t        | dd«      }t        | dd«      }t        | dd«      }t        | dd«      }t        | dd«      }|€i n|}|r|�t	        d	«      ‚|�|�|�t	        d
«      ‚|€|€|€|€ |j
                  dd| i|¤ŽS |r%|€t	        d«      ‚ |j                  |f||dœ|¤Ž}|S |r"|€t	        d«      ‚ |j                  |fi |¤Ž}|S |€|€t	        d«      ‚|€ |j                  |fi |¤Ž}|j                  |¬«      }|S )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_configNrY   Ú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.backbonerS   z8config.backbone must be set if use_timm_backbone is True)rY   r£   z>config.backbone must be set if use_pretrained_backbone is Truez<Either config.backbone_config or config.backbone must be set)rS   r   )r   r    r¡   rK   r)   Úfrom_configÚfrom_pretrained)	rS   r    r¡   r¢   rY   r£   Úbackbone_checkpointr¥   r¤   s	            r   Úload_backbonerª   +  sš  € ÷ 6ä˜fÐ&7¸Ó>€OÜ Ð(;¸TÓBÐÜ% fÐ.GÈÓNÐÜ! &¨*°dÓ;ÐÜ˜fÐ&7¸Ó>€OØ+Ð3‘b¸€Oá˜?Ð6ÜÐZÓ[Ð[ð Ð"Ð':Ð'FÐKbÐKnÜÐYÓZÐZð 	ÐØÐ%ØÐ'ØÐ'à'ˆ|×'Ñ'ÑI¨vÐI¸ÑIÐIñ ØÐ&ÜÐWÓXÐXð 0�<×/Ñ/Øð
à/Ø$;ñ
ð ñ	
ˆð  €Oñ 
!ØÐ&ÜÐ]Ó^Ð^Ø/�<×/Ñ/Ð0CÑWÀÑWˆð €Oð Ð"Ð':Ð'BÜÐ[Ó\Ð\ØÐ"Ø8˜j×8Ñ8Ð9LÑ`ÐP_Ñ`ˆOØ×+Ñ+°?Ð+ÓCˆØ€Or   rY   r£   r¤   r¢   r   r¥   c                 óf   — |�|�t        d«      ‚|�| rt        d«      ‚|�|r|�t        d«      ‚yyy)zR
    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)   )rY   r£   r¤   r¢   r¥   s        r   Ú verify_backbone_config_argumentsr¬   h  sT   € ð Ð" xÐ';ÜÐSÓTÐTàÐ"Ñ'8ÜÐ\Ó]Ð]àÐ"¡¸?Ð;VÜÐZÓ[Ð[ð <W Ð"r   )rž   Úenumrt   Úcollections.abcr   Útypingr   r   r   Úconfiguration_utilsr   ÚEnumr
   r’   r“   r7   r+   r/   r=   rC   rE   r˜   rª   r”   rs   r¬   r   r   r   ú<module>r²      s„  ðñ Hã Û Ý $ß 1Ñ 1ñ Ý6ô"�4—9‘9ô "ð
*rØ˜8 C™=Ñ)ð*rØ8@ÀÈ#ÁÑ8Oð*rØ^fÐgoÐpsÑgtÑ^uó*rðZ%Ø˜4 ™9Ñ%ð%à˜%  S¡	¨5°©:Ð 5Ñ6Ñ7ð%ð �c‘ó%ð>+Ø˜4 ™9Ñ%ð+à˜%  S¡	¨5°©:Ð 5Ñ6Ñ7ð+ð �c‘ð+ð ˆ4�‰9�d˜3‘iÐÑ ó	+÷@rñ r÷j'ñ 'òT:ðz\Øð\à!ð\ð �s‰mð\ð ˜e DÐ*<Ð$<Ñ=Ñ>ð	\ð
 ˜d‘^ô\r   