
    #j4B              
          d Z ddlZddlZddlZddlm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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   )logging)maybe_install_capturing_hooksc                       e Zd ZdZdZdS )BackboneTypetimmtransformersN)__name__
__module____qualname__TIMMTRANSFORMERS     e/lsinfo/ai/hellotax_ai/base_platform/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>D   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>F   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>V   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   r1   zFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>g   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   r1   zFBackboneConfigMixin.verify_out_features_out_indices.<locals>.<genexpr>h   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>q   s)     # # #"AsC# # #r   c                     | d         S )Nr   r   )xs    r   <lambda>zEBackboneConfigMixin.verify_out_features_out_indices.<locals>.<lambda>r   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>{   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_indicesL   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.   r3   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(   rJ   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.
        NrM   )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   rU   zBackboneConfigMixin.to_dict   sM    
 ""!'OT!B!B~ &

>4 @ @}r   )r   r   r   __doc__r'   r+   r)   propertyr   setterstrr   r&   intrU   __classcell__rX   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                 F     t          j                    fd            }|S )ak  
    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                     |                     dt          | j        dd                    } | g|R i |}|s3d |                                D             } t	          |          di |}|S )Noutput_hidden_statesFc                 "    i | ]\  }}|d v	||S )hidden_statesr   )r   kvs      r   
<dictcomp>z@filter_output_hidden_states.<locals>.wrapper.<locals>.<dictcomp>   s)    #b#b#bTQqQ`GaGaAqGaGaGar   r   )getgetattrconfigitemsr@   )r   argskwargsrb   rW   filtered_output_dataforward_functions         r   wrapperz,filter_output_hidden_states.<locals>.wrapper   s    %zz*@'$+WmotBuBuvv!!$888888# 	:#b#bV\\^^#b#b#b !T&\\99$899Fr   )	functoolswraps)ro   rp   s   ` r   filter_output_hidden_statesrs      s;     _%&&    '& Nr   c                   `    e Zd ZU dZedz  ed<   dZeed<   d fdZ 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.)
rT   __init__rV   r   r   rv   r   _init_timm_backbone_init_transformers_backboner?   )r   rl   rm   rz   rX   s       r   r|   zBackboneMixin.__init__   s    
 	$)&)))

?D99$!-!2D!-!:D!222$$m$<<<<<<#<<<,,.....Qd.@QQQRRRr   c                 f    t                                                       t          |            dS )a  
        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)rT   	post_initr   )r   rX   s    r   r   zBackboneMixin.post_init   s/     	%d+++++r   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   r   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.)ri   rj   feature_infoinfor   num_featuresr'   r   module_namer?   r+   )r   r{   out_features_from_configstage_names_from_configr   r   s         r   r}   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.   )rj   r   r)   r   rJ   s    r   r~   z)BackboneMixin._init_transformers_backbone   s1    ;233555 r   c                     | j         j        S r.   )rj   r$   rJ   s    r   r   zBackboneMixin.out_features  s    {((r   r   c                     || j         _        dS )rL   N)rj   r   rN   s     r   r   zBackboneMixin.out_features  s    
 $0   r   c                     | j         j        S r.   )rj   r(   rJ   s    r   r   zBackboneMixin.out_indices  s    {''r   r   c                     || j         _        dS )rQ   N)rj   r   rR   s     r   r   zBackboneMixin.out_indices  s    
 #.r   c                 D      fdt           j                  D             S )Nc                 2    i | ]\  }}|j         |         S r   )r   )r   ir   r   s      r   rg   z6BackboneMixin.out_feature_channels.<locals>.<dictcomp>  s'    XXX5t(+XXXr   )	enumerater   rJ   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   rJ   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   re   rf   	signatures      r   rg   z>BackboneMixin.forward_with_filtered_kwargs.<locals>.<dictcomp>'  s$    HHHtq!iar   )rw   rV   rv   r   r   dictinspectr   forward
parametersrk   )r   rl   rm   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   rb   r   return_dictc                      t          d          )Nz7This method should be implemented by the derived class.)NotImplementedError)r   pixel_valuesrb   r   r   s        r   r   zBackboneMixin.forward*  s     ""[\\\r   )rx   N)NNN)r   r   r   rv   r   __annotations__rw   boolr|   r   r}   r~   rZ   r   r[   r'   r\   r   r&   r]   r   r   r   r   r^   r_   s   @r   ru   ru      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   ru   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_kwargsr{   use_pretrained_backbonerz   
model_typezG`backbone_config` is `None`. Initializing the config with the default `z` vision config.r   )configuration_utilsr   models.autor   rV   rh   r   get_config_dictupdateloggerr   r%   r   	from_dict)backbone_configr   r   r   r   rm   r   r   r   r   r{   config_dictr9   config_classbackbone_model_types                  r   %consolidate_backbone_kwargs_to_configr   4  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)rj   )r
   r   ri   from_config)rj   r   r   r{   s       r   load_backboner   f  sa     *)))))f&7>>O++6+::++?+CCOr   )NNNN)rY   enumrq   r   huggingface_hubr   utilsr   utils.output_capturingr   
get_loggerr   r   Enumr   r   rs   ru   r\   r   r   r   r   r   r   <module>r      s   H G       ' ' ' ' ' '       A A A A A A 
	H	%	%" " " " "49 " " "
z z z z z z z zz  *|] |] |] |] |] |] |] |]B $(&*)-'+/# /#Dj/# t/#  $;	/#
 /# /# /# /#d    r   