
    bi;              
           d Z ddlZddlZddlmZ ddlmZ  ej        e          Z	 G d dej
                  Z G d d	          Z G d
 d          Z	 	 	 	 ddedz  dedz  dedz  dedz  fdZd ZdS )zACollection of utils to be used by backbones and their components.    N)repo_exists   )loggingc                       e Zd ZdZdZdS )BackboneTypetimmtransformersN)__name__
__module____qualname__TIMMTRANSFORMERS     V/root/projects/butler/venv/lib/python3.11/site-packages/transformers/backbone_utils.pyr   r      s        D!LLLr   r   c                        e Zd ZdZdedz  dedz  fdZd Zed             Zej	        dee
         fd            Zed	             Zej	        deed
f         ee         z  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.
    out_featuresNout_indicesc                     | _         t          |t                    rt          |          n| _                                           j          j        }}|)|'t           j                  dz
  g} j        d         g}n%|| fd|D             }n|| fd|D             }||c _          _                                          dS )ap  
        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   c                 D    g | ]}j                             |          S r   )stage_namesindex).0layerselfs     r   
<listcomp>zJBackboneConfigMixin.set_output_features_output_indices.<locals>.<listcomp>B   s*    SSSU4+11%88SSSr   c                 *    g | ]}j         |         S r   r   r   idxr   s     r   r   zJBackboneConfigMixin.set_output_features_output_indices.<locals>.<listcomp>D   s!    IIIcD,S1IIIr   )_out_features
isinstancetuplelist_out_indicesverify_out_features_out_indiceslenr   )r   r   r   s   `  r   "set_output_features_output_indicesz6BackboneConfigMixin.set_output_features_output_indices&   s     *1;K1O1O`D---U` 	,,... %)$68Ik<#7t/00145K ,R01LL \%=SSSSlSSSKK!k&=IIII[IIIL 1=k-D-,,.....r   c                      j         t          d           j        t           j        t          f          s$t          dt           j                             t           fd j        D                       rt          d j          d j                   t           j                  t          t           j                            k    rt          d j                    j         fd j         D             x}k    rt          d	| d j                    j	        ]t           j	        t                    s$t          d
t           j	                             t           fd j	        D                       }t           fd|D                       rt          d j          d j	                   t          |          t          t          |                    k    r/d j	         }|| j	        k    rd| dndz  }t          |          |t          t          |                    k    rId t          t          | j	                  d           D             }t          d| d j	                    j        k j	        ft           j                  t           j	                  k    rt          d           j         fd j	        D             k    rt          d          dS dS dS )z_
        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   | ]}|j         vV  d S Nr    r   featr   s     r   	<genexpr>zFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>T   s+      OOD4t//OOOOOOr   z.out_features must be a subset of stage_names: z got z2out_features must not contain any duplicates, got c                 &    g | ]}|j         v |S r   r#   r.   s     r   r   zGBackboneConfigMixin.verify_out_features_out_indices.<locals>.<listcomp>[   s&     a a a$ddN`F`F`F`F`F`r   z@out_features must be in the same order as stage_names, expected z out_indices must be a list, got c              3   V   K   | ]#}|d k     r|t          j                  z  n|V  $dS )r   N)r)   r   r!   s     r   r0   zFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>e   sB      $r$rY\C!GGS3t/?+@+@%@%@QT$r$r$r$r$r$rr   c              3   b   K   | ])}|t          t          j                            v%|V  *d S r-   )ranger)   r   r!   s     r   r0   zFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>f   s@      ^^3cs4K[G\G\A]A]6]6]36]6]6]6]^^r   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   zGBackboneConfigMixin.verify_out_features_out_indices.<locals>.<listcomp>o   s)     # # #"AsC# # #r   c                     | d         S )Nr   r   )xs    r   <lambda>zEBackboneConfigMixin.verify_out_features_out_indices.<locals>.<lambda>p   s    efghei 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 | ]}j         |         S r   r    r!   s     r   r   zGBackboneConfigMixin.verify_out_features_out_indices.<locals>.<listcomp>y   s!    %Y%Y%Yd&6s&;%Y%Y%Yr   zQout_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   sorted_featspositive_indicesmsgsorted_negatives   `    r   r(   z3BackboneConfigMixin.verify_out_features_out_indicesJ   s    #QRRR)d04':: ` !^DI[D\D\!^!^___OOOOD<NOOOOO  pTEUpp\`\npp   4%&&#c$2D.E.E*F*FFF !jVZVh!j!jkkk! a a a a$2B a a aa  !~Wc~~jnj|~~   (d/66 _ !]DIZD[D[!]!]^^^$$r$r$r$r`d`q$r$r$rrr^^^^"2^^^^^  tIYttaeartt   #$$C0@,A,A(B(BBB]$J[]]AQUYUfAfAf=)9====lnn oo%50@)A)A#B#BBB# #&,S1A4CT-U-U[i[i&j&j&j# # # !Velpl}   )d.?.K4%&&#d.?*@*@@@ !klll!%Y%Y%Y%YtGX%Y%Y%YYY !tuuu	 *).K.K ZYr   c                     | j         S r-   r2   r   s    r   r   z BackboneConfigMixin.out_features|   s    !!r   c                 4    |                      |d           dS )
        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   z BackboneConfigMixin.out_features   s$    
 	//\W[/\\\\\r   c                     | j         S r-   )r'   rI   s    r   r   zBackboneConfigMixin.out_indices   s      r   .c                 Z    |t          |          n|}|                     d|           dS )
        Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices.
        NrL   )r&   r*   r   r   s     r   r   zBackboneConfigMixin.out_indices   s:    
 ,7+Bd;'''//T{/[[[[[r   c                     t                                                      }|                    dd          |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#   Nr   r'   r   )superto_dictpop)r   output	__class__s     r   rT   zBackboneConfigMixin.to_dict   sM    
 ""!'OT!B!B~ &

>4 @ @}r   )r
   r   r   __doc__r&   r*   r(   propertyr   setterstrr   r%   intrT   __classcell__rW   s   @r   r   r   !   s/        "/Tk"/ D["/ "/ "/ "/H0v 0v 0vd " " X" ]c ] ] ] ] ! ! X! \uS#Xc'B \ \ \ \        r   r   c                   V    e Zd ZU dZedz  ed<   dZeed<   d f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         z  fd            Zed             Zed             Zd Z	 	 	 ddedz  dedz  dedz  fdZ xZS )BackboneMixinNbackbone_typeThas_attentionsreturnc                     t                      j        |i | |                    dd          }|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.
        timm_backboneN)backbonezbackbone_type z not supported.)
rS   __init__rU   r   r   ra   r   _init_timm_backbone_init_transformers_backboner>   )r   argskwargsre   rW   s       r   rg   zBackboneMixin.__init__   s    
 	$)&)))

?D99$!-!2D!-!:D!222$$m$<<<<<<#<<<,,.....Qd.@QQQRRRr   c                    t          | j        dd          }t          | j        dd          }d |j        j        D             | _        d |j        j        D             | _        t          |j        j                  }|j                                        }|||k    rt          d| d          ||| j        k    rt          d| d	          | j        | j        _        | j        
                    ||           dS )
zj
        Initialize the backbone model from timm. The backbone must already be loaded to backbone
        r   Nr   c                     g | ]
}|d          S )moduler   r   stages     r   r   z5BackboneMixin._init_timm_backbone.<locals>.<listcomp>   s    TTTE(OTTTr   c                     g | ]
}|d          S )num_chsr   ro   s     r   r   z5BackboneMixin._init_timm_backbone.<locals>.<listcomp>   s    VVV%U9-VVVr   z!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.)getattrconfigfeature_infoinfor   num_featuresr&   r   module_namer>   r*   )r   rf   out_features_from_configstage_names_from_configr   r   s         r   rh   z!BackboneMixin._init_timm_backbone   s=   
 $+4;#M#M ")$+}d"K"K
 UT9N9STTTVV8;P;UVVV80<==,88::#/4LP\4\4\x4L x x x  
 #.3JdN^3^3^q3J q q q   #'"266|[QQQQQr   c                 h    | j         j        | _        | j                                          d | _        d S r-   )rt   r   r(   rw   rI   s    r   ri   z)BackboneMixin._init_transformers_backbone   s1    ;233555 r   c                     | j         j        S r-   )rt   r#   rI   s    r   r   zBackboneMixin.out_features   s    {((r   r   c                     || j         _        dS )rK   N)rt   r   rM   s     r   r   zBackboneMixin.out_features   s    
 $0   r   c                     | j         j        S r-   )rt   r'   rI   s    r   r   zBackboneMixin.out_indices   s    {''r   r   c                     || j         _        dS )rP   N)rt   r   rQ   s     r   r   zBackboneMixin.out_indices   s    
 #.r   c                 D      fdt           j                  D             S )Nc                 2    i | ]\  }}|j         |         S r   )rw   )r   irp   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                    | 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   r   z>BackboneMixin.forward_with_filtered_kwargs.<locals>.<dictcomp>  s$    HHHtq!iar   )rb   rU   ra   r   r   dictinspectr   forward
parametersitems)r   rj   rk   r   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   )rc   N)NNN)r
   r   r   ra   r   __annotations__rb   boolrg   rh   ri   rY   r   rZ   r&   r[   r   r%   r\   r   r   r   r   r]   r^   s   @r   r`   r`      s        )-M<$&---  NDS S S S S S&R R R RB! ! ! ! ) ) X) 0c 0 0 0 0 ( ( X( .uSzDI'= . . . . Y Y XY
 O O XO% % % -1)-#'] ] #Tk]  $;	]
 D[] ] ] ] ] ] ] ]r   r`   default_backbonedefault_config_typedefault_config_kwargstimm_default_kwargsc                    ddl m} ddlm} |                    dd          }|                    di           }	|                    d          |                    d          n|}
|                    dd            ||r|
| |	s |d	         dd|
i|} n|
f| dt          |
          rD|                    |
          \  }}||d
                  }|                    |	            |di |} n |d	         dd|
i|	} n|| 3|1t          
                    d| d           |pi } ||         di |} nGt          | t                    r2|                     d
          }||         }|                    |           } | |fS )Nr   )PreTrainedConfig)CONFIG_MAPPINGuse_timm_backboneTbackbone_kwargsrf   use_pretrained_backbonere   
model_typezG`backbone_config` is `None`. Initializing the config with the default `z` vision config.r   )configuration_utilsr   models.autor   rU   getr   get_config_dictupdateloggerrv   r$   r   	from_dict)backbone_configr   r   r   r   rk   r   r   r   r   rf   config_dictr8   config_classbackbone_model_types                  r   %consolidate_backbone_kwargs_to_configr     s    655555++++++

#6==jj!2B77O)/J)?)?)Kvzz*%%%QaH
JJ($///
 	' 	( # $ :.9cc8cObcc		/"9x   	d-==hGGNK)+l*CDL///*l99[99OO=n_=ccxcSbccOO		 %8%D{Vi{{{	
 	
 	
 !6 ;=.)<=VV@UVV	OT	*	* B-11,??%&9:&00AAF""r   c                     ddl m} t          | dd          }||                    |           }n|                    |          }|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   )AutoBackboner   N)rt   )r	   r   rs   from_config)rt   r   r   rf   s       r   load_backboner   C  sa     *)))))f&7>>O++6+::++?+CCOr   )NNNN)rX   enumr   huggingface_hubr   utilsr   
get_loggerr
   r   Enumr   r   r`   r[   r   r   r   r   r   r   <module>r      se   H G   ' ' ' ' ' '       
	H	%	%" " " " "49 " " "
z z z z z z z zzp] p] p] p] p] p] p] p]j $(&*)-'+/# /#Dj/# t/#  $;	/#
 /# /# /# /#d    r   