
     `i:E                     ^   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df         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dS )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dS )BackboneTypetimmtransformersN)__name__
__module____qualname__TIMMTRANSFORMERS     u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/utils/backbone_utils.pyr
   r
      s        D!LLLr   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                               k    rt          d              fdD             x}k    rt          d	| d            |0t          |t                    st          d
t          |                     t          fd|D                       }t	          fd|D                       rt          d d|           t          |          t          t          |                    k    r%d| }|||k    rd| dndz  }t          |          |t          t          |                    k    r?d t          t          ||          d           D             }t          d| d|            R|Rt                     t          |          k    rt          d           fd|D             k    rt          d          dS dS dS )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V  	d S Nr   ).0featr   s     r   	<genexpr>z2verify_out_features_out_indices.<locals>.<genexpr>,   s(      @@4t;&@@@@@@r   z.out_features must be a subset of stage_names: z got z2out_features must not contain any duplicates, got c                     g | ]}|v |	S r   r   )r   r   r   s     r   
<listcomp>z3verify_out_features_out_indices.<locals>.<listcomp>0   s$    ,b,b,bdTUaMaMaTMaMaMar   z@out_features must be in the same order as stage_names, expected z out_indices must be a list, got c              3   L   K   | ]}|d k     r|t                    z  n|V  dS )r   Nlenr   idxr   s     r   r   z2verify_out_features_out_indices.<locals>.<genexpr>9   s>       c cPS377s;'7'7!7!7 c c c c c cr   c              3   X   K   | ]$}|t          t                              v |V  %d S r   )ranger"   r#   s     r   r   z2verify_out_features_out_indices.<locals>.<genexpr>:   s=      UUs#U3{CSCS=T=T2T2Ts2T2T2T2TUUr   z2out_indices must be valid indices for stage_names z, got z1out_indices must not contain any duplicates, got z(equivalent to z)) c                     g | ]\  }}|S r   r   )r   _r$   s      r   r   z3verify_out_features_out_indices.<locals>.<listcomp>A   s    pppvq#spppr   c                     | d         S )Nr   r   )xs    r   <lambda>z1verify_out_features_out_indices.<locals>.<lambda>A   s    jklmjn 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 setc                      g | ]
}|         S r   r   r#   s     r   r   z3verify_out_features_out_indices.<locals>.<listcomp>I   s    DDDK,DDDr   zQout_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   sorted_featspositive_indicesmsgsorted_negatives   ` `    r   verify_out_features_out_indicesr<       s    MNNN,00 	VT\@R@RTTUUU@@@@<@@@@@ 	pnknn`lnnooo|C$5$5 6 666`R^``aaa,b,b,b,bk,b,b,bbLcctS_ttfrtt   +t,, 	US[@Q@QSSTTT  c c c cWb c c cccUUUU.UUUUU 	trR]rreprrsss  C,<(=(=$>$>>>SkSSC=MQ\=\=\9%59999bddCS//!uV,<%=%=>>>>pp<Lk8Z8Z`n`n1o1o1opppOuRauuhsuu   K$;|K 0 000ghhhDDDDDDDDDpqqq	  $;$; EDr   .c                     || t                    dz
  g}d         g} n%|| fd| D             }n| |fd|D             } | |fS )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.
    N   c                 :    g | ]}                     |          S r   )index)r   layerr   s     r   r   z9_align_output_features_output_indices.<locals>.<listcomp>f   s'    JJJE{((//JJJr   c                      g | ]
}|         S r   r   r#   s     r   r   z9_align_output_features_output_indices.<locals>.<listcomp>h   s    @@@SC(@@@r   r!   r   r   r   s     `r   %_align_output_features_output_indicesrE   M   s    * |3;''!+,#B(		!9JJJJ\JJJ		+"9@@@@K@@@$$r   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.
    NrD   )r1   r<   rE   )r   r   r   output_featuresoutput_indicess        r   *get_aligned_output_features_output_indicesrJ   l   st    * (3'>${###DK#;dopppp&K!{' ' '#O^ $njuvvvvN**r   c                   ~    e Zd ZU dZee         ed<   dZ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_typeThas_attentionsrF   c                    t          | dd          t          d          d | j        j        j        D             | _        d | j        j        j        D             | _        t          | j        j        j                  }| j        j        	                                }t          ||| j                   ||c| _        | _        dS )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_backbonec                     g | ]
}|d          S )moduler   r   stages     r   r   z5BackboneMixin._init_timm_backbone.<locals>.<listcomp>   s    ZZZE(OZZZr   c                     g | ]
}|d          S )num_chsr   rS   s     r   r   z5BackboneMixin._init_timm_backbone.<locals>.<listcomp>   s    \\\%U9-\\\r   rD   )getattrr/   rP   feature_infoinfor   num_featuresr1   r   module_namer<   _out_features_out_indices)selfconfigr   r   s       r   _init_timm_backbonez!BackboneMixin._init_timm_backbone   s     4d++3\]]]
 [Z9T9YZZZ\\4>;V;[\\\ 4>6BCC~2>>@@ 	(%;DL\	
 	
 	
 	
 1=k-D---r   c                     t          |d          }t          |dd           }t          |dd           }|| _        t          |||          \  | _        | _        d | _        d S )Nr   r   r   rD   )rW   r   rJ   r\   r]   rZ   )r^   r_   r   r   r   s        r   _init_transformers_backbonez)BackboneMixin._init_transformers_backbone   sq    fm44v~t<<fmT::&0Z%;K1
 1
 1
-D- !r   c                 d   || _         t          |dd          | _        | j        rt          j        nt          j        | _        | j        t          j        k    r|                     |           dS | j        t          j        k    r|                     |           dS 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)
r_   rW   rd   r
   r   r   rM   r`   rb   r/   )r^   r_   s     r   _init_backbonezBackboneMixin._init_backbone   s    
 !(1De!L!L262Hg\..lNg!222$$V,,,,,<#<<<,,V44444Qd.@QQQRRRr   c                     | j         S r   r\   r^   s    r   r   zBackboneMixin.out_features       !!r   r   c                 N    t          |d| j                  \  | _        | _        dS z
        Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
        NrD   rJ   r   r\   r]   r^   r   s     r   r   zBackboneMixin.out_features   4    
 1[%4TEU1
 1
 1
-D---r   c                     | j         S r   r]   rh   s    r   r   zBackboneMixin.out_indices         r   r   c                 N    t          d|| j                  \  | _        | _        dS z
        Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices.
        NrD   rl   r^   r   s     r   r   zBackboneMixin.out_indices   4    
 1[;DDT1
 1
 1
-D---r   c                 D      fdt           j                  D             S )Nc                 2    i | ]\  }}|j         |         S r   )rZ   )r   irT   r^   s      r   
<dictcomp>z6BackboneMixin.out_feature_channels.<locals>.<dictcomp>   s'    XXX5t(+XXXr   )	enumerater   rh   s   `r   out_feature_channelsz"BackboneMixin.out_feature_channels   s+     YXXXIdFV<W<WXXXXr   c                 *      fd j         D             S )Nc                 *    g | ]}j         |         S r   )r{   )r   namer^   s     r   r   z*BackboneMixin.channels.<locals>.<listcomp>   s!    NNND)$/NNNr   )r   rh   s   `r   channelszBackboneMixin.channels   s     NNNND<MNNNNr   c                    | j         s|                    dd            | j        t          j        k    rKt          t          j        | j                  j	                  fd|
                                D             } | |i |S )Noutput_attentionsc                 $    i | ]\  }}|v 	||S r   r   )r   kv	signatures      r   ry   z>BackboneMixin.forward_with_filtered_kwargs.<locals>.<dictcomp>   s$    HHHtq!iar   )rN   poprM   r
   r   dictinspectr   forward
parametersitems)r^   argskwargsr   s      @r   forward_with_filtered_kwargsz*BackboneMixin.forward_with_filtered_kwargs   s    " 	2JJ*D111!222W.t|<<GHHIHHHHv||~~HHHFtT$V$$$r   output_hidden_statesr   return_dictc                      t          d          )Nz7This method should be implemented by the derived class.)NotImplementedError)r^   pixel_valuesr   r   r   s        r   r   zBackboneMixin.forward   s     ""[\\\r   c                     t                                                      }|                    d          |d<   |                    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.
        r\   r   r]   r   superto_dictr   r^   output	__class__s     r   r   zBackboneMixin.to_dict   I    
 ""!'O!<!<~ &

> : :}r   )rF   N)NNN)r   r   r   rM   r   r
   __annotations__rN   boolr`   rb   re   propertyr   setterr1   strr   r   r5   intr{   r   r   r   r   __classcell__r   s   @r   rL   rL      s        ,0M8L)000  NDJ J J J0
! 
! 
! 
!S S S S" " " X" 
c 
 
 
 
 ! ! X! 
uU3Zc-B'C 
 
 
 
 Y Y XY
 O O XO% % % 04,0&*] ] 'tn] $D>	]
 d^] ] ] ]        r   rL   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df         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   rg   rh   s    r   r   z BackboneConfigMixin.out_features  ri   r   r   c                 N    t          |d| j                  \  | _        | _        dS rk   rl   rm   s     r   r   z BackboneConfigMixin.out_features  rn   r   c                     | j         S r   rp   rh   s    r   r   zBackboneConfigMixin.out_indices  rq   r   r   .c                 N    t          d|| j                  \  | _        | _        dS rs   rl   rt   s     r   r   zBackboneConfigMixin.out_indices  ru   r   c                     t                                                      }|                    d          |d<   |                    d          |d<   |S r   r   r   s     r   r   zBackboneConfigMixin.to_dict'  r   r   )r   r   r   __doc__r   r   r   r1   r   r   r   r5   r   r   r   r   s   @r   r   r     s          " " X" 
c 
 
 
 
 ! ! X! 
uU38_d3i-G'H 
 
 
 
        r   r   c                 F   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|}n[|r |t	          d           |j        |fi |}n9||t	          d          | |j        |fi |}|                    |          }|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_configNrd   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.backboner_   z8config.backbone must be set if use_timm_backbone is True)rd   r   z>config.backbone must be set if use_pretrained_backbone is Truez<Either config.backbone_config or config.backbone must be set)r_   r   )r   r   r   rW   r/   from_configfrom_pretrained)	r_   r   r   r   rd   r   backbone_checkpointr   r   s	            r   load_backboner   2  s    65555555f&7>>O(;TBB%f.GNN!&*d;;f&7>>O+3bbO \?6Z[[[ "':'FKbKnYZZZ #4#<ATA\'|'IIvIIII  D&WXXX 0</
/$;
 
 	
 
 
! 	D&]^^^/</0CWWWW"':'B[\\\"8j89L``P_``O++?+CCOr   rd   r   r   r   r   r   c                     ||t          d          || rt          d          ||r|t          d          dS dS dS )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/   )rd   r   r   r   r   s        r    verify_backbone_config_argumentsr   j  sl     "x';STTT"'8"\]]]""?;VZ[[[ #""";V;Vr   )r   enumr   collections.abcr   typingr   r   r   configuration_utilsr   Enumr
   r   r   r<   r1   r5   rE   rJ   rL   r   r   r   r   r   r   r   r   <module>r      s   H G   $ $ $ $ $ $ 1 1 1 1 1 1 1 1 1 1  7666666" " " " "49 " " "
*r8C=)*r8@#8O*r^fgopsgt^u*r *r *r *rZ%49%%%S	5c? :;<% c% % % %>+49%+%S	5: 567+ c+ 49d3i 	+ + + +@y y y y y y y yx' ' ' ' ' ' ' 'T5 5 5p\\!\ sm\ eD*<$<=>	\
 d^\ \ \ \ \ \r   