
    gD                     Z   d Z ddlZddlZddlmZmZmZ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dS )zACollection of utils to be used by backbones and their components.    N)TYPE_CHECKINGIterableListOptionalTupleUnion   )PretrainedConfigc                       e Zd ZdZdZdS )BackboneTypetimmtransformersN)__name__
__module____qualname__TIMMTRANSFORMERS     ]/var/www/html/ai-engine/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_indicesrG   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.
    NrF   )r3   r>   rG   )r   r   r   output_featuresoutput_indicess        r   *get_aligned_output_features_output_indicesrL   l   st    * (3'>${###DK#;dopppp&K!{' ' '#O^ $njuvvvvN**r   c                   p    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_typerH   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   rT   s     r   r!   z5BackboneMixin._init_timm_backbone.<locals>.<listcomp>   s    \\\%U9-\\\r   rF   )getattrr1   rQ   feature_infoinfor   num_featuresr3   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   rF   )rX   r   rL   r]   r^   r[   )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`   rX   re   r   r   r   rO   ra   rc   r1   )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.
        NrF   rL   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^   ri   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.
        NrF   rm   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   )r[   )r   irU   r_   s      r   
<dictcomp>z6BackboneMixin.out_feature_channels.<locals>.<dictcomp>   s'    XXX5t(+XXXr   )	enumerater   ri   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   ri   s   `r   channelszBackboneMixin.channels   s     NNNND<MNNNNr   c                     t          t          j        | j                  j                  fd|                                D             } | |i |S )Nc                 $    i | ]\  }}|v 	||S r   r   )r   kv	signatures      r   rz   z>BackboneMixin.forward_with_filtered_kwargs.<locals>.<dictcomp>   s$    MMMDAqa9nn1annnr   )dictinspectr   forward
parametersitems)r_   argskwargsfiltered_kwargsr   s       @r   forward_with_filtered_kwargsz*BackboneMixin.forward_with_filtered_kwargs   sV    *4<88CDD	MMMMFLLNNMMMtT-_---r   output_hidden_statesoutput_attentions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_dictpopr_   output	__class__s     r   r   zBackboneMixin.to_dict   I    
 ""!'O!<!<~ &

> : :}r   )rH   N)NNN)r   r   r   rO   r   r   __annotations__ra   rc   rf   propertyr   setterr   strr   r   r   intr|   r   r   boolr   r   __classcell__r   s   @r   rN   rN      s        ,0M8L)000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   rN   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   rh   ri   s    r   r   z BackboneConfigMixin.out_features  rj   r   r   c                 N    t          |d| j                  \  | _        | _        dS rl   rm   rn   s     r   r   z BackboneConfigMixin.out_features
  ro   r   c                     | j         S r   rq   ri   s    r   r   zBackboneConfigMixin.out_indices  rr   r   r   c                 N    t          d|| j                  \  | _        | _        dS rt   rm   ru   s     r   r   zBackboneConfigMixin.out_indices  rv   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   r   r   r   r   r   r   r   r   r   s   @r   r   r     s          " " X" 
c 
 
 
 
 ! ! X! 
uU3Zc-B'C 
 
 
 
        r   r   c                 J   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_configNre   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)re   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   rX   r1   from_configfrom_pretrained)	r`   r   r   r   re   r   backbone_checkpointr   r   s	            r   load_backboner   +  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 	%'''|'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   re   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   )r1   )re   r   r   r   r   s        r    verify_backbone_config_argumentsr   h  sl     "x';STTT"'8"\]]]""?;VZ[[[ #""";V;Vr   )r   enumr   typingr   r   r   r   r   r   configuration_utilsr
   Enumr   r   r   r>   rG   rL   rN   r   r   r   r   r   r   r   r   <module>r      s    H G   H H H H H H H H H H H H H H H H  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: 567% c% % % %>+49%+%S	5: 567+ c+ 49d3i 	+ + + +@r r r r r r r rj' ' ' ' ' ' ' 'T: : :z\\!\ sm\ eD*<$<=>	\
 d^\ \ \ \ \ \r   