
    iO                        d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ dd	lmZmZ d
dlmZ  ej(                  e      ZdedefdZd+deez  defdZ G d dej8                        Z G d dej<                        Z G d dej8                        Z  G d dej8                        Z! G d dej8                        Z" G d dej8                        Z# G d dej8                        Z$ G d  d!ej8                        Z%e G d" d#e             Z&e G d$ d%e&             Z' ed&'       G d( d)e&             Z(g d*Z)y),zPyTorch EfficientNet model.    N)nn   )initialization)ACT2FN)BaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)auto_docstringlogging   )EfficientNetConfigconfignum_channelsc                     | j                   }|| j                  z  }t        |t        ||dz  z         |z  |z        }|d|z  k  r||z  }t        |      S )z<
    Round number of filters based on depth multiplier.
       g?)depth_divisorwidth_coefficientmaxint)r   r   divisornew_dims       r/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/efficientnet/modeling_efficientnet.pyround_filtersr   $   sf     ""GF,,,L'3|gk9:gEOPG |##7w<    kernel_sizeadjustc                     t        | t              r| | f} | d   dz  | d   dz  f}|r|d   dz
  |d   |d   dz
  |d   fS |d   |d   |d   |d   fS )aJ  
    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#"K01~"KNa$78G
Q
GAJNGAJGG
GAJ
GAJ??r   c                   `     e Zd ZdZdef fdZdej                  dej                  fdZ xZ	S )EfficientNetEmbeddingszL
    A module that corresponds to the stem module of the original work.
    r   c                    t         |           t        |d      | _        t	        j
                  d      | _        t	        j                  |j                  | j                  dddd      | _	        t	        j                  | j                  |j                  |j                  	      | _        t        |j                     | _        y )
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   r/   zEfficientNetEmbeddings.__init__L   s    $VR0||L9991QPW^c
 &:O:OZ`ZtZtu !2!23r   pixel_valuesreturnc                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S N)r'   r3   r7   r9   )r;   r=   featuress      r   forwardzEfficientNetEmbeddings.forwardW   sA    <<-##H->>(+??8,r   )
__name__
__module____qualname____doc__r   r/   torchTensorrB   __classcell__r<   s   @r   r#   r#   G   s0    	41 	4ELL U\\ r   r#   c                   .     e Zd Z	 	 	 	 	 	 	 d fd	Z xZS )EfficientNetDepthwiseConv2dc	                 @    ||z  }	t         
|   ||	|||||||	       y )N)	in_channelsout_channelsr   r*   r'   dilationgroupsr+   padding_mode)r.   r/   )r;   rN   depth_multiplierr   r*   r'   rP   r+   rR   rO   r<   s             r   r/   z$EfficientNetDepthwiseConv2d.__init__a   s=     #%55#%#% 	 
	
r   )r   r   r   r   r   Tzeros)rC   rD   rE   r/   rI   rJ   s   @r   rL   rL   `   s$     
 
r   rL   c                   l     e Zd ZdZdedededef fdZdej                  dej                  fd	Z
 xZS )
EfficientNetExpansionLayerz_
    This corresponds to the expansion phase of each block in the original implementation.
    r   in_dimr0   r*   c                     t         |           t        j                  ||ddd      | _        t        j
                  ||j                        | _        t        |j                     | _
        y )Nr   sameFrN   rO   r   r'   r+   )num_featuresr,   )r.   r/   r   r2   expand_convr4   r5   	expand_bnr   r8   
expand_act)r;   r   rW   r0   r*   r<   s        r   r/   z#EfficientNetExpansionLayer.__init__   sZ    99 
 W&BWBWX !2!23r   hidden_statesr>   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r@   )r\   r]   r^   r;   r_   s     r   rB   z"EfficientNetExpansionLayer.forward   s4    ((7}56r   )rC   rD   rE   rF   r   r   r/   rG   FloatTensorrH   rB   rI   rJ   s   @r   rV   rV   z   sH    
41 
43 
4 
4VY 
4U%6%6 5<< r   rV   c            
       p     e Zd ZdZdededededef
 fdZdej                  d	ej                  fd
Z xZS )EfficientNetDepthwiseLayerzk
    This corresponds to the depthwise convolution phase of each block in the original implementation.
    r   rW   r*   r   adjust_paddingc                 b   t         |           || _        | j                  dk(  rdnd}t        ||      }t	        j
                  |      | _        t        ||||d      | _        t	        j                  ||j                  |j                        | _        t        |j                     | _        y )	Nr   r(   rY   )r   r&   Fr)   r[   r,   r-   )r.   r/   r*   r!   r   r1   depthwise_conv_padrL   depthwise_convr4   r5   r6   depthwise_normr   r8   depthwise_act)	r;   r   rW   r*   r   re   conv_padr'   r<   s	           r   r/   z#EfficientNetDepthwiseLayer.__init__   s     	"kkQ.7Fk.A"$,,w"?9FHSX
 !nnV%:%:VE_E_
 $F$5$56r   r_   r>   c                     | j                   dk(  r| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S )Nr   )r*   rh   ri   rj   rk   ra   s     r   rB   z"EfficientNetDepthwiseLayer.forward   sT    ;;! 33MBM++M:++M:**=9r   rC   rD   rE   rF   r   r   boolr/   rG   rb   rH   rB   rI   rJ   s   @r   rd   rd      sZ    7"7 7 	7
 7 7,	U%6%6 	5<< 	r   rd   c            	       n     e Zd ZdZd
dedededef fdZdej                  dej                  fd	Z xZS )EfficientNetSqueezeExciteLayerzl
    This corresponds to the Squeeze and Excitement phase of each block in the original implementation.
    r   rW   
expand_dimexpandc                    t         |           |r|n|| _        t        dt	        ||j
                  z              | _        t        j                  d      | _	        t        j                  | j                  | j                  dd      | _        t        j                  | j                  | j                  dd      | _        t        |j                     | _        t        j                          | _        y )Nr   )output_sizerY   )rN   rO   r   r'   )r.   r/   dimr   r   squeeze_expansion_ratiodim_ser   AdaptiveAvgPool2dsqueezer2   reducers   r   r8   
act_reduceSigmoid
act_expand)r;   r   rW   rr   rs   r<   s        r   r/   z'EfficientNetSqueezeExciteLayer.__init__   s    !':V!S&*H*H!HIJ++:ii	
 ii	
 !!2!23**,r   r_   r>   c                     |}| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  ||      }|S r@   )rz   r{   r|   rs   r~   rG   mul)r;   r_   inputss      r   rB   z&EfficientNetSqueezeExciteLayer.forward   sc    ]3M26M26		&-8r   )Frn   rJ   s   @r   rq   rq      sH    '1 '3 'C 'Y] '*
U%6%6 
5<< 
r   rq   c                        e Zd ZdZdedededededef fdZd	e	j                  d
e	j                  de	j                  fdZ xZS )EfficientNetFinalBlockLayerz[
    This corresponds to the final phase of each block in the original implementation.
    r   rW   r0   r*   	drop_rateid_skipc                    t         |           |dk(  xr | | _        t        j                  ||ddd      | _        t        j                  ||j                  |j                        | _	        t        j                  |      | _        y )Nr   rY   FrZ   rg   p)r.   r/   apply_dropoutr   r2   project_convr4   r5   r6   
project_bnDropoutdropout)r;   r   rW   r0   r*   r   r   r<   s          r   r/   z$EfficientNetFinalBlockLayer.__init__   sz     	#q[8[II 
 .. f&;&;fF`F`
 zzI.r   
embeddingsr_   r>   c                     | j                  |      }| j                  |      }| j                  r| j                  |      }||z   }|S r@   )r   r   r   r   )r;   r   r_   s      r   rB   z#EfficientNetFinalBlockLayer.forward   sG    ))-86 LL7M)J6Mr   rC   rD   rE   rF   r   r   floatro   r/   rG   rb   rH   rB   rI   rJ   s   @r   r   r      sj    /(/25/@C/MP/]b/mq/"%"3"3 EDUDU Z_ZfZf r   r   c                        e Zd ZdZdededededededed	ed
ef fdZde	j                  de	j                  fdZ xZS )EfficientNetBlocka  
    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   rW   r0   r*   expand_ratior   r   r   re   c
                 l   t         |           || _        | j                  dk7  | _        ||z  }
| j                  rt	        |||
|      | _        t        || j                  r|
n||||	      | _        t        |||
| j                        | _	        t        || j                  r|
n|||||      | _        y )Nr   )r   rW   r0   r*   )r   rW   r*   r   re   )r   rW   rr   rs   )r   rW   r0   r*   r   r   )r.   r/   r   rs   rV   	expansionrd   ri   rq   squeeze_exciter   
projection)r;   r   rW   r0   r*   r   r   r   r   re   expand_in_dimr<   s              r   r/   zEfficientNetBlock.__init__  s     	(''1,-;;7fmFDN 9$(KK=V#)
 =&]4;;
 6$(KK=V
r   r_   r>   c                     |}| j                   dk7  r| j                  |      }| j                  |      }| j                  |      }| j	                  ||      }|S )Nr   )r   r   ri   r   r   )r;   r_   r   s      r   rB   zEfficientNetBlock.forwardG  s[    "
! NN=9M++M: ++M:
MBr   r   rJ   s   @r   r   r     s    4'
"'
 '
 	'

 '
 '
 '
 '
 '
 '
R
U%6%6 
5<< 
r   r   c            	       f     e Zd ZdZdef fdZ	 	 d
dej                  dedz  dedz  de	fd	Z
 xZS )EfficientNetEncoderz
    Forward propagates the embeddings through each EfficientNet block.

    Args:
        config ([`EfficientNetConfig`]):
            Model configuration class.
    r   c                     t                    | _        |j                   _         fdt	        |j
                        }t        fd|j                  D              }d}g }t        |      D ]  }t        ||j
                  |         }t        ||j                  |         }|j                  |   }	|j                  |   }
|j                  |   }t         |j                  |               D ]c  }|dk(  }|dkD  rdn|	}	|dkD  r|n|}||j                  v}|j                  |z  |z  }t!        ||||	|
||||	      }|j#                  |       |dz  }e  t%        j&                  |       _        t%        j*                  t        |d      ddd	       _        t%        j.                  |j0                  |j2                  |j4                  
       _        t8        |j:                      _        y )Nc                 Z    t        t        j                  j                  | z              S r@   )r   mathceildepth_coefficient)repeatsr;   s    r   round_repeatsz3EfficientNetEncoder.__init__.<locals>.round_repeatsb  s"    tyy!7!7'!ABCCr   c              3   .   K   | ]  } |        y wr@    ).0nr   s     r   	<genexpr>z/EfficientNetEncoder.__init__.<locals>.<genexpr>g  s     Laq)Ls   r   r   )	r   rW   r0   r*   r   r   r   r   re   i   rY   FrZ   rg   )r.   r/   r   r   lenrN   sumnum_block_repeatsranger   rO   strideskernel_sizesexpand_ratiosdepthwise_paddingdrop_connect_rater   appendr   
ModuleListblocksr2   top_convr4   
hidden_dimr5   r6   top_bnr   r8   top_activation)r;   r   num_base_blocks
num_blockscurr_block_numr   irW   r0   r*   r   r   jr   re   r   blockr   r<   s   `                @r   r/   zEfficientNetEncoder.__init__]  s   !'!9!9	D f001L63K3KLL
' 	$A"66+=+=a+@AF#FF,?,?,BCG^^A&F --a0K!//2L=)A)A!)DEF $q&!e$%Ev!/v7O7O!O"44~E
R	)!!#! +!-'##1
 e$!#'$	$8 mmF+		&vt4
 nn**0E0EPVPjPj
 %V%6%67r   r_   output_hidden_statesNreturn_dictr>   c                     |r|fnd }| j                   D ]  } ||      }|s||fz  } | j                  |      }| j                  |      }| j                  |      }|st	        d ||fD              S t        ||      S )Nc              3   &   K   | ]	  }||  y wr@   r   )r   vs     r   r   z.EfficientNetEncoder.forward.<locals>.<genexpr>  s     Xq!-Xs   )last_hidden_stater_   )r   r   r   r   tupler   )r;   r_   r   r   all_hidden_statesr   s         r   rB   zEfficientNetEncoder.forward  s     1E],$[[ 	6E!-0M#!m%55!	6
 m4M2++M:X]4E$FXXX-++
 	
r   )FT)rC   rD   rE   rF   r   r/   rG   rb   ro   r   rB   rI   rJ   s   @r   r   r   T  sW    581 58t -2#'	
((
 #Tk
 D[	

 
(
r   r   c                   r    e Zd ZU eed<   dZdZdZdgZ e	j                         dej                  fd       Zy)	EfficientNetPreTrainedModelr   efficientnetr=   )imager   modulec                    t        |t        j                  t        j                  t        j                  f      rt        j                  |j                  d| j                  j                         |j                  t        j                  |j                         t        |dd      ^t        j                  |j                         t        j                  |j                         t        j                  |j                          yyy)zInitialize the weightsg        )meanstdNrunning_mean)r   r   Linearr2   r4   initnormal_weightr   initializer_ranger+   zeros_getattrr   ones_running_varnum_batches_tracked)r;   r   s     r   _init_weightsz)EfficientNetPreTrainedModel._init_weights  s     fryy"))R^^DELLSdkk6S6ST{{&FKK(v~t4@F//0

6--.F667 A	 Fr   N)rC   rD   rE   r   __annotations__base_model_prefixmain_input_nameinput_modalities_no_split_modulesrG   no_gradr   Moduler   r   r   r   r   r     sG    &$O!,-U]]_	8BII 	8 	8r   r   c                   z     e Zd Zdef fdZe	 	 	 d	dej                  dz  dedz  dedz  de	e
z  fd       Z xZS )
EfficientNetModelr   c                    t         |   |       || _        t        |      | _        t        |      | _        |j                  dk(  r't        j                  |j                  d      | _        nN|j                  dk(  r't        j                  |j                  d      | _        nt        d|j                         | j                          y )Nr   T)	ceil_moder   z2config.pooling must be one of ['mean', 'max'] got )r.   r/   r   r#   r   r   encoderpooling_typer   	AvgPool2dr   pooler	MaxPool2d
ValueErrorpooling	post_initr:   s     r   r/   z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                 ~   ||n| j                   j                  }||n| j                   j                  }|t        d      | j	                  |      }| j                  |||      }|d   }| j                  |      }|j                  |j                  d d       }|s
||f|dd  z   S t        |||j                        S )Nz You have to specify pixel_valuesr   r   r   r   r   )r   pooler_outputr_   )r   r   use_return_dictr   r   r   r   reshapeshaper   r_   )	r;   r=   r   r   kwargsembedding_outputencoder_outputsr   pooled_outputs	            r   rB   zEfficientNetModel.forward  s     %9$D $++JjJj 	 &1%<k$++B]B]?@@??<8,,!5# ' 
 ,A.$56%--m.A.A"1.EF%}58KKK7/')77
 	
r   )NNN)rC   rD   rE   r   r/   r   rG   rb   ro   r   r   rB   rI   rJ   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                        e Zd Z fdZe	 	 	 	 d	dej                  dz  dej                  dz  dedz  dedz  de	e
z  f
d       Z xZS )
"EfficientNetForImageClassificationc                    t         |   |       |j                  | _        || _        t	        |      | _        t        j                  |j                        | _	        | j                  dkD  r*t        j                  |j                  | j                        nt        j                         | _        | j                          y )Nr   r   )r.   r/   
num_labelsr   r   r   r   r   dropout_rater   r   r   Identity
classifierr   r:   s     r   r/   z+EfficientNetForImageClassification.__init__  s      ++-f5zzF$7$78KO??]^K^"))F$5$5tGdfdododq 	r   Nr=   labelsr   r   r>   c                 d   ||n| j                   j                  }| j                  |||      }|r|j                  n|d   }| j	                  |      }| j                  |      }d}	|| j                  ||| j                         }	|s|f|dd z   }
|	|	f|
z   S |
S t        |	||j                        S )a  
        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logitsr_   )	r   r   r   r   r   r   loss_functionr	   r_   )r;   r=   r   r   r   r   outputsr   r  r  outputs              r   rB   z*EfficientNetForImageClassification.forward  s     &1%<k$++B]B]##LG[it#u1<--'!*]3/%%ffdkkBDY,F)-)9TGf$EvE3!//
 	
r   )NNNN)rC   rD   rE   r/   r   rG   rb   
LongTensorro   r   r	   rB   rI   rJ   s   @r   r   r     s~    
  26*.,0#'"
''$."
   4'"
 #Tk	"

 D["
 
5	5"
 "
r   r   )r   r   r   )T)*rF   r   rG   r    r   r   activationsr   modeling_outputsr   r   r	   modeling_utilsr
   utilsr   r   configuration_efficientnetr   
get_loggerrC   loggerr   r   r   ro   r!   r   r#   r2   rL   rV   rd   rq   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   