
    Z jO                        S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
JrJr  SSKJr  SS	KJrJr  S
SKJr  \R(                  " \5      rS\S\4S jrS+S\\-  S\4S jjr " S S\R8                  5      r " S S\R<                  5      r " S S\R8                  5      r  " S S\R8                  5      r! " S S\R8                  5      r" " S S\R8                  5      r# " S S\R8                  5      r$ " S  S!\R8                  5      r%\ " S" S#\5      5       r&\ " S$ S%\&5      5       r'\" S&S'9 " S( S)\&5      5       r(/ S*Qr)g),zPyTorch EfficientNet model.    N)nn   )initialization)ACT2FN)BaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)auto_docstringlogging   )EfficientNetConfigconfignum_channelsc                     U R                   nXR                  -  n[        U[        XS-  -   5      U-  U-  5      nUSU-  :  a  X2-  n[        U5      $ )z4
Round number of filters based on depth multiplier.
   g?)depth_divisorwidth_coefficientmaxint)r   r   divisornew_dims       ڇ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/efficientnet/modeling_efficientnet.pyround_filtersr   $   s`     ""G,,,L'3|k9:gEOPG |##w<    kernel_sizeadjustc                     [        U [        5      (       a  X 4n U S   S-  U S   S-  4nU(       a  US   S-
  US   US   S-
  US   4$ US   US   US   US   4$ )a.  
Utility function to get the tuple padding value for the depthwise convolution.

Args:
    kernel_size (`int` or `tuple`):
        Kernel size of the convolution layers.
    adjust (`bool`, *optional*, defaults to `True`):
        Adjusts padding value to apply to right and bottom sides of the input.
r   r   r   )
isinstancer   )r   r   corrects      r   correct_padr!   3   s~     +s##"01~"KNa$78G
Q
GAJNGAJGG
GAJ
GAJ??r   c                   n   ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	EfficientNetEmbeddingsG   zD
A module that corresponds to the stem module of the original work.
r   c           	      |  > [         TU ]  5         [        US5      U l        [        R
                  " SS9U l        [        R                  " UR                  U R                  SSSSS9U l	        [        R                  " U R                  UR                  UR                  S	9U l        [        UR                     U l        g )
N    )r   r   r   r   paddingr   r   validFr   strider(   bias)epsmomentum)super__init__r   out_dimr   	ZeroPad2dr(   Conv2dr   convolutionBatchNorm2dbatch_norm_epsbatch_norm_momentum	batchnormr   
hidden_act
activationselfr   	__class__s     r   r0   EfficientNetEmbeddings.__init__L   s    $VR0||L9991QPW^c
 &:O:OZ`ZtZtu !2!23r   pixel_valuesreturnc                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ N)r(   r4   r8   r:   )r<   r?   featuress      r   forwardEfficientNetEmbeddings.forwardW   sA    <<-##H->>(+??8,r   )r:   r8   r4   r1   r(   )__name__
__module____qualname____firstlineno____doc__r   r0   torchTensorrD   __static_attributes____classcell__r=   s   @r   r#   r#   G   s5    	41 	4ELL U\\  r   r#   c                   :   ^  \ rS rSr       SU 4S jjrSrU =r$ )EfficientNetDepthwiseConv2d`   c	                 8   > X-  n	[         T
U ]  UU	UUUUUUUS9	  g )N)	in_channelsout_channelsr   r+   r(   dilationgroupsr,   padding_mode)r/   r0   )r<   rT   depth_multiplierr   r+   r(   rV   r,   rX   rU   r=   s             r   r0   $EfficientNetDepthwiseConv2d.__init__a   s:     #5#%#% 	 
	
r    )r   r   r   r   r   Tzeros)rF   rG   rH   rI   r0   rM   rN   rO   s   @r   rQ   rQ   `   s$     
 
r   rQ   c                   z   ^  \ rS rSrSrS\S\S\S\4U 4S jjrS\R                  S	\R                  4S
 jrSrU =r$ )EfficientNetExpansionLayerz   zW
This corresponds to the expansion phase of each block in the original implementation.
r   in_dimr1   r+   c                    > [         TU ]  5         [        R                  " UUSSSS9U l        [        R
                  " X1R                  S9U l        [        UR                     U l
        g )Nr   sameFrT   rU   r   r(   r,   )num_featuresr-   )r/   r0   r   r3   expand_convr5   r6   	expand_bnr   r9   
expand_act)r<   r   r`   r1   r+   r=   s        r   r0   #EfficientNetExpansionLayer.__init__   sX    99 
 WBWBWX !2!23r   hidden_statesr@   c                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ rB   )re   rf   rg   r<   ri   s     r   rD   "EfficientNetExpansionLayer.forward   s4    ((7}56r   )rg   rf   re   )rF   rG   rH   rI   rJ   r   r   r0   rK   FloatTensorrL   rD   rM   rN   rO   s   @r   r^   r^   z   sM    
41 
43 
4 
4VY 
4U%6%6 5<<  r   r^   c            
       ~   ^  \ rS rSrSrS\S\S\S\S\4
U 4S jjrS	\	R                  S
\	R                  4S jrSrU =r$ )EfficientNetDepthwiseLayer   zc
This corresponds to the depthwise convolution phase of each block in the original implementation.
r   r`   r+   r   adjust_paddingc                 F  > [         TU ]  5         X0l        U R                  S:X  a  SOSn[        XES9n[        R
                  " US9U l        [        X$X6SS9U l        [        R                  " X!R                  UR                  S9U l        [        UR                     U l        g )	Nr   r)   rb   )r   r'   Fr*   rd   r-   r.   )r/   r0   r+   r!   r   r2   depthwise_conv_padrQ   depthwise_convr5   r6   r7   depthwise_normr   r9   depthwise_act)	r<   r   r`   r+   r   rq   conv_padr(   r=   s	           r   r0   #EfficientNetDepthwiseLayer.__init__   s     	"kkQ.7FkA"$,,w"?9FSX
 !nn%:%:VE_E_
 $F$5$56r   ri   r@   c                     U R                   S:X  a  U R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU$ )Nr   )r+   rt   ru   rv   rw   rk   s     r   rD   "EfficientNetDepthwiseLayer.forward   sT    ;;! 33MBM++M:++M:**=9r   )rw   ru   rt   rv   r+   rF   rG   rH   rI   rJ   r   r   boolr0   rK   rm   rL   rD   rM   rN   rO   s   @r   ro   ro      s_    7"7 7 	7
 7 7,	U%6%6 	5<< 	 	r   ro   c            	       ~   ^  \ rS rSrSrSS\S\S\S\4U 4S jjjrS\	R                  S	\	R                  4S
 jrSrU =r$ )EfficientNetSqueezeExciteLayer   zd
This corresponds to the Squeeze and Excitement phase of each block in the original implementation.
r   r`   
expand_dimexpandc                   > [         TU ]  5         U(       a  UOUU l        [        S[	        X!R
                  -  5      5      U l        [        R                  " SS9U l	        [        R                  " U R                  U R                  SSS9U l        [        R                  " U R                  U R                  SSS9U l        [        UR                     U l        [        R                   " 5       U l        g )Nr   )output_sizerb   )rT   rU   r   r(   )r/   r0   dimr   r   squeeze_expansion_ratiodim_ser   AdaptiveAvgPool2dsqueezer3   reducer   r   r9   
act_reduceSigmoid
act_expand)r<   r   r`   r   r   r=   s        r   r0   'EfficientNetSqueezeExciteLayer.__init__   s    !':V!S*H*H!HIJ++:ii	
 ii	
 !!2!23**,r   ri   r@   c                     UnU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      n[
        R                  " X!5      nU$ rB   )r   r   r   r   r   rK   mul)r<   ri   inputss      r   rD   &EfficientNetSqueezeExciteLayer.forward   sa    ]3M26M26		&8r   )r   r   r   r   r   r   r   )Fr|   rO   s   @r   r   r      sR    '1 '3 'C 'Y] ' '*
U%6%6 
5<< 
 
r   r   c                      ^  \ rS rSrSrS\S\S\S\S\S\4U 4S	 jjr	S
\
R                  S\
R                  S\
R                  4S jrSrU =r$ )EfficientNetFinalBlockLayer   zS
This corresponds to the final phase of each block in the original implementation.
r   r`   r1   r+   	drop_rateid_skipc                   > [         TU ]  5         US:H  =(       a    U(       + U l        [        R                  " UUSSSS9U l        [        R                  " X1R                  UR                  S9U l	        [        R                  " US9U l        g )Nr   rb   Frc   rs   p)r/   r0   apply_dropoutr   r3   project_convr5   r6   r7   
project_bnDropoutdropout)r<   r   r`   r1   r+   r   r   r=   s          r   r0   $EfficientNetFinalBlockLayer.__init__   sx     	#q[8[II 
 .. &;&;fF`F`
 zzI.r   
embeddingsri   r@   c                     U R                  U5      nU R                  U5      nU R                  (       a  U R                  U5      nX!-   nU$ rB   )r   r   r   r   )r<   r   ri   s      r   rD   #EfficientNetFinalBlockLayer.forward   sE    ))-86 LL7M)6Mr   )r   r   r   r   rF   rG   rH   rI   rJ   r   r   floatr}   r0   rK   rm   rL   rD   rM   rN   rO   s   @r   r   r      so    /(/25/@C/MP/]b/mq/"%"3"3 EDUDU Z_ZfZf  r   r   c                      ^  \ rS rSrSrS\S\S\S\S\S\S	\S
\S\4U 4S jjr	S\
R                  S\
R                  4S jrSrU =r$ )EfficientNetBlocki  aA  
This corresponds to the expansion and depthwise convolution phase of each block in the original implementation.

Args:
    config ([`EfficientNetConfig`]):
        Model configuration class.
    in_dim (`int`):
        Number of input channels.
    out_dim (`int`):
        Number of output channels.
    stride (`int`):
        Stride size to be used in convolution layers.
    expand_ratio (`int`):
        Expand ratio to set the output dimensions for the expansion and squeeze-excite layers.
    kernel_size (`int`):
        Kernel size for the depthwise convolution layer.
    drop_rate (`float`):
        Dropout rate to be used in the final phase of each block.
    id_skip (`bool`):
        Whether to apply dropout and sum the final hidden states with the input embeddings during the final phase
        of each block. Set to `True` for the first block of each stage.
    adjust_padding (`bool`):
        Whether to apply padding to only right and bottom side of the input kernel before the depthwise convolution
        operation, set to `True` for inputs with odd input sizes.
r   r`   r1   r+   expand_ratior   r   r   rq   c
           	      f  > [         TU ]  5         XPl        U R                  S:g  U l        X%-  n
U R                  (       a  [	        XXS9U l        [        UU R                  (       a  U
OUUUU	S9U l        [        XXR                  S9U l	        [        UU R                  (       a  U
OUUUUUS9U l        g )Nr   )r   r`   r1   r+   )r   r`   r+   r   rq   )r   r`   r   r   )r   r`   r1   r+   r   r   )r/   r0   r   r   r^   	expansionro   ru   r   squeeze_exciter   
projection)r<   r   r`   r1   r+   r   r   r   r   rq   expand_in_dimr=   s              r   r0   EfficientNetBlock.__init__  s     	(''1,-;;7mDN 9$(KK=V#)
 =];;
 6$(KK=V
r   ri   r@   c                     UnU R                   S:w  a  U R                  U5      nU R                  U5      nU R                  U5      nU R	                  X!5      nU$ )Nr   )r   r   ru   r   r   )r<   ri   r   s      r   rD   EfficientNetBlock.forwardG  sY    "
! NN=9M++M: ++M:
Br   )ru   r   r   r   r   r   r   rO   s   @r   r   r     s    4'
"'
 '
 	'

 '
 '
 '
 '
 '
 '
R
U%6%6 
5<< 
 
r   r   c            	       v   ^  \ rS rSrSrS\4U 4S jjr  SS\R                  S\	S-  S\	S-  S	\
4S
 jjrSrU =r$ )EfficientNetEncoderiT  z
Forward propagates the embeddings through each EfficientNet block.

Args:
    config ([`EfficientNetConfig`]):
        Model configuration class.
r   c                   >^ ^ [         TT ]  5         UT l        UR                  T l        U 4S jm[	        UR
                  5      n[        U4S jUR                   5       5      nSn/ n[        U5       H  n[        XR
                  U   5      n[        XR                  U   5      nUR                  U   n	UR                  U   n
UR                  U   n[        T" UR                  U   5      5       Hc  nUS:H  nUS:  a  SOU	n	US:  a  UOUnXAR                  ;  nUR                  U-  U-  n[!        UUUU	U
UUUUS9	nUR#                  U5        US-  nMe     M     [$        R&                  " U5      T l        [$        R*                  " W[        US5      SSSS	9T l        [$        R.                  " UR0                  UR2                  UR4                  S
9T l        [8        UR:                     T l        g )Nc                 \   > [        [        R                  " TR                  U -  5      5      $ rB   )r   mathceildepth_coefficient)repeatsr<   s    r   round_repeats3EfficientNetEncoder.__init__.<locals>.round_repeatsb  s"    tyy!7!7'!ABCCr   c              3   4   >#    U  H  nT" U5      v   M     g 7frB   r[   ).0nr   s     r   	<genexpr>/EfficientNetEncoder.__init__.<locals>.<genexpr>g  s     L3Kaq))3Ks   r   r   )	r   r`   r1   r+   r   r   r   r   rq   i   rb   Frc   rs   )r/   r0   r   r   lenrT   sumnum_block_repeatsranger   rU   strideskernel_sizesexpand_ratiosdepthwise_paddingdrop_connect_rater   appendr   
ModuleListblocksr3   top_convr5   
hidden_dimr6   r7   top_bnr   r9   top_activation)r<   r   num_base_blocks
num_blockscurr_block_numr   ir`   r1   r+   r   r   jr   rq   r   blockr   r=   s   `                @r   r0   EfficientNetEncoder.__init__]  s   !'!9!9	D f001L63K3KLL
'A"6+=+=a+@AF#F,?,?,BCG^^A&F --a0K!//2L=)A)A!)DEFq&!e$%Ev!/7O7O!O"44~E
R	)!!#! +!-'##1
 e$!#' G (8 mmF+		&vt4
 nn**0E0EPVPjPj
 %V%6%67r   ri   output_hidden_statesNreturn_dictr@   c                    U(       a  U4OS nU R                    H  nU" U5      nU(       d  M  XA4-  nM     U R                  U5      nU R                  U5      nU R                  U5      nU(       d  [	        S X4 5       5      $ [        UUS9$ )Nc              3   .   #    U  H  oc  M  Uv   M     g 7frB   r[   )r   vs     r   r   .EfficientNetEncoder.forward.<locals>.<genexpr>  s     X$Fq$Fs   	)last_hidden_stateri   )r   r   r   r   tupler   )r<   ri   r   r   all_hidden_statesr   s         r   rD   EfficientNetEncoder.forward  s     1E],$[[E!-0M##!%55! !
 m4M2++M:X]$FXXX-++
 	
r   )r   r   r   r   r   r   )FT)rF   rG   rH   rI   rJ   r   r0   rK   rm   r}   r   rD   rM   rN   rO   s   @r   r   r   T  s\    581 58t -2#'	
((
 #Tk
 D[	

 
(
 
r   r   c                   |    \ rS rSr% \\S'   SrSrSrS/r	\
R                  " 5       S\R                  4S j5       rS	rg
)EfficientNetPreTrainedModeli  r   efficientnetr?   )imager   modulec                 "   [        U[        R                  [        R                  [        R                  45      (       a  [
        R                  " UR                  SU R                  R                  S9  UR                  b   [
        R                  " UR                  5        [        USS5      ba  [
        R                  " UR                  5        [
        R                  " UR                  5        [
        R                  " UR                   5        ggg)zInitialize the weightsg        )meanstdNrunning_mean)r   r   Linearr3   r5   initnormal_weightr   initializer_ranger,   zeros_getattrr   ones_running_varnum_batches_tracked)r<   r   s     r   _init_weights)EfficientNetPreTrainedModel._init_weights  s     fryy"))R^^DEELLSdkk6S6ST{{&FKK(v~t4@F//0

6--.F667 A	 Fr   r[   N)rF   rG   rH   rI   r   __annotations__base_model_prefixmain_input_nameinput_modalities_no_split_modulesrK   no_gradr   Moduler   rM   r[   r   r   r   r     sE    &$O!,-
]]_	8BII 	8 	8r   r   c                      ^  \ rS rSrS\4U 4S jjr\   SS\R                  S-  S\	S-  S\	S-  S\
\-  4S	 jj5       rS
rU =r$ )EfficientNetModeli  r   c                   > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        UR                  S:X  a%  [        R                  " UR                  SS9U l        OMUR                  S:X  a%  [        R                  " UR                  SS9U l        O[        SUR                   35      eU R                  5         g )Nr   T)	ceil_moder   z2config.pooling must be one of ['mean', 'max'] got )r/   r0   r   r#   r   r   encoderpooling_typer   	AvgPool2dr   pooler	MaxPool2d
ValueErrorpooling	post_initr;   s     r   r0   EfficientNetModel.__init__  s     08*62 &(,,v'8'8DIDK  E),,v'8'8DIDKQRXR`R`Qabcc 	r   Nr?   r   r   r@   c                    Ub  UOU R                   R                  nUb  UOU R                   R                  nUc  [        S5      eU R	                  U5      nU R                  UUUS9nUS   nU R                  U5      nUR                  UR                  S S 5      nU(       d	  Xx4USS  -   $ [        UUUR                  S9$ )Nz You have to specify pixel_valuesr   r   r   r   r   )r   pooler_outputri   )r   r   r   r  r   r  r  reshapeshaper   ri   )	r<   r?   r   r   kwargsembedding_outputencoder_outputsr   pooled_outputs	            r   rD   EfficientNetModel.forward  s     %9$D $++JjJj 	 &1%<k$++BYBY?@@??<8,,!5# ' 
 ,A.$56%--m.A.A"1.EF%58KKK7/')77
 	
r   )r   r   r  r  )NNN)rF   rG   rH   rI   r   r0   r   rK   rm   r}   r   r   rD   rM   rN   rO   s   @r   r   r     sn    1 "  26,0#'	#
''$.#
 #Tk#
 D[	#
 
9	9#
 #
r   r   z
    EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g.
    for ImageNet.
    )custom_introc                      ^  \ rS rSrU 4S jr\    SS\R                  S-  S\R                  S-  S\	S-  S\	S-  S\
\-  4
S	 jj5       rS
rU =r$ )"EfficientNetForImageClassificationi  c                   > [         TU ]  U5        UR                  U l        Xl        [	        U5      U l        [        R                  " UR                  S9U l	        U R                  S:  a+  [        R                  " UR                  U R                  5      O[        R                  " 5       U l        U R                  5         g )Nr   r   )r/   r0   
num_labelsr   r   r   r   r   dropout_rater   r   r   Identity
classifierr  r;   s     r   r0   +EfficientNetForImageClassification.__init__  s      ++-f5zzF$7$78KO??]^K^"))F$5$5tGdfdododq 	r   Nr?   labelsr   r   r@   c                 n   Ub  UOU R                   R                  nU R                  XUS9nU(       a  UR                  OUS   nU R	                  U5      nU R                  U5      nSn	Ub  U R                  X(U R                   5      n	U(       d  U4USS -   n
U	b  U	4U
-   $ U
$ [        U	UUR                  S9$ )ab  
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
    Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
    `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
Nr  r   r   )losslogitsri   )	r   r   r   r  r   r  loss_functionr	   ri   )r<   r?   r  r   r   r  outputsr  r   r  outputs              r   rD   *EfficientNetForImageClassification.forward  s     &1%<k$++BYBY##Lit#u1<--'!*]3/%%fdkkBDY,F)-)9TGf$EvE3!//
 	
r   )r  r   r   r   r  )NNNN)rF   rG   rH   rI   r0   r   rK   rm   
LongTensorr}   r   r	   rD   rM   rN   rO   s   @r   r  r    s~    
  26*.,0#'"
''$."
   4'"
 #Tk	"

 D["
 
5	5"
 "
r   r  )r  r   r   )T)*rJ   r   rK   r    r   r   activationsr   modeling_outputsr   r   r	   modeling_utilsr
   utilsr   r   configuration_efficientnetr   
get_loggerrF   loggerr   r   r   r}   r!   r   r#   r3   rQ   r^   ro   r   r   r   r   r   r   r  __all__r[   r   r   <module>r/     s^   "    & ! 
 . , : 
		H	%, C @S5[ @$ @(RYY 2
")) 
4 4$ $N$RYY $N")) BN		 NbW
")) W
t 8/ 8 8( 6
3 6
 6
r 0
)D 0
0
f er   