
    sg]                        d Z ddlZddlmZmZmZ ddlZddl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mZ dd	lmZ dd
lmZmZmZmZ ddlmZ  ej8                  e      ZdZdZ g dZ!dZ"dZ#dZ$dZ%dede&fdZ'd1dee&ef   de(fdZ) G d dejT                        Z+ G d dejX                        Z- G d dejT                        Z. G d d ejT                        Z/ G d! d"ejT                        Z0 G d# d$ejT                        Z1 G d% d&ejT                        Z2 G d' d(ejT                        Z3 G d) d*e      Z4 ed+e$       G d, d-e4             Z5 ed.e$       G d/ d0e4             Z6y)2zPyTorch EfficientNet model.    N)OptionalTupleUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardlogging   )EfficientNetConfigr   zgoogle/efficientnet-b7)r   i      r   ztabby, tabby cataN  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`EfficientNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aB  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`AutoImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
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       i/var/www/html/venv/lib/python3.12/site-packages/transformers/models/efficientnet/modeling_efficientnet.pyround_filtersr"   P   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)   _   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!   r7   zEfficientNetEmbeddings.__init__x   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/   r;   r?   rA   )rC   rE   featuress      r!   forwardzEfficientNetEmbeddings.forward   sA    <<-##H->>(+??8,r#   )
__name__
__module____qualname____doc__r   r7   torchTensorrJ   __classcell__rD   s   @r!   r+   r+   s   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$   r2   r/   dilationgroupsr3   padding_mode)r6   r7   )rC   rV   depth_multiplierr$   r2   r/   rX   r3   rZ   rW   rD   s             r!   r7   z$EfficientNetDepthwiseConv2d.__init__   s=     #%55#%#% 	 
	
r#   )r   r
   r   r   r   Tzeros)rK   rL   rM   r7   rQ   rR   s   @r!   rT   rT      s$     
 
r#   rT   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_dimr8   r2   c                     t         |           t        j                  ||ddd      | _        t        j
                  ||j                        | _        t        |j                     | _
        y )Nr   sameFrV   rW   r$   r/   r3   )num_featuresr4   )r6   r7   r   r:   expand_convr<   r=   	expand_bnr   r@   
expand_act)rC   r   r_   r8   r2   rD   s        r!   r7   z#EfficientNetExpansionLayer.__init__   sZ    99 
 W&BWBWX !2!23r#   hidden_statesrF   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rH   )rd   re   rf   rC   rg   s     r!   rJ   z"EfficientNetExpansionLayer.forward   s4    ((7}56r#   )rK   rL   rM   rN   r   r   r7   rO   FloatTensorrP   rJ   rQ   rR   s   @r!   r^   r^      sH    
41 
43 
4 
4VY 
4U%6%6 5<< r#   r^   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   r_   r2   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   r0   ra   )r%   r.   Fr1   rc   r4   r5   )r6   r7   r2   r)   r   r9   depthwise_conv_padrT   depthwise_convr<   r=   r>   depthwise_normr   r@   depthwise_act)	rC   r   r_   r2   r$   rm   conv_padr/   rD   s	           r!   r7   z#EfficientNetDepthwiseLayer.__init__   s     	"kkQ.7Fk.A"$,,w"?9FHSX
 !nnV%:%:VE_E_
 $F$5$56r#   rg   rF   c                     | j                   dk(  r| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S )Nr   )r2   rp   rq   rr   rs   ri   s     r!   rJ   z"EfficientNetDepthwiseLayer.forward   sT    ;;! 33MBM++M:++M:**=9r#   rK   rL   rM   rN   r   r   boolr7   rO   rj   rP   rJ   rQ   rR   s   @r!   rl   rl      sZ    7"7 7 	7
 7 7,	U%6%6 	5<< 	r#   rl   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   r_   
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_sizera   )rV   rW   r$   r/   )r6   r7   dimr   r   squeeze_expansion_ratiodim_ser   AdaptiveAvgPool2dsqueezer:   reducer{   r   r@   
act_reduceSigmoid
act_expand)rC   r   r_   rz   r{   rD   s        r!   r7   z'EfficientNetSqueezeExciteLayer.__init__   s    !':V!S&*H*H!HIJ++:ii	
 ii	
 !!2!23**,r#   rg   rF   c                     |}| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  ||      }|S rH   )r   r   r   r{   r   rO   mul)rC   rg   inputss      r!   rJ   z&EfficientNetSqueezeExciteLayer.forward  sc    ]3M26M26		&-8r#   )Frv   rR   s   @r!   ry   ry      sH    '1 '3 'C 'Y] '*
U%6%6 
5<< 
r#   ry   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   r_   r8   r2   	drop_rateid_skipc                    t         |           |dk(  xr | | _        t        j                  ||ddd      | _        t        j                  ||j                  |j                        | _	        t        j                  |      | _        y )Nr   ra   Frb   ro   p)r6   r7   apply_dropoutr   r:   project_convr<   r=   r>   
project_bnDropoutdropout)rC   r   r_   r8   r2   r   r   rD   s          r!   r7   z$EfficientNetFinalBlockLayer.__init__  sz     	#q[8[II 
 .. f&;&;fF`F`
 zzI.r#   
embeddingsrg   rF   c                     | j                  |      }| j                  |      }| j                  r| j                  |      }||z   }|S rH   )r   r   r   r   )rC   r   rg   s      r!   rJ   z#EfficientNetFinalBlockLayer.forward$  sG    ))-86 LL7M)J6Mr#   rK   rL   rM   rN   r   r   floatrw   r7   rO   rj   rP   rJ   rQ   rR   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   r_   r8   r2   expand_ratior$   r   r   rm   c
                 t   t         |           || _        | j                  dk7  rdnd| _        ||z  }
| j                  rt	        |||
|      | _        t        || j                  r|
n||||	      | _        t        |||
| j                        | _	        t        || j                  r|
n|||||      | _        y )Nr   TF)r   r_   r8   r2   )r   r_   r2   r$   rm   )r   r_   rz   r{   )r   r_   r8   r2   r   r   )r6   r7   r   r{   r^   	expansionrl   rq   ry   squeeze_exciter   
projection)rC   r   r_   r8   r2   r   r$   r   r   rm   expand_in_dimrD   s              r!   r7   zEfficientNetBlock.__init__J  s     	("//14d%-;;7fmFDN 9$(KK=V#)
 =&]4;;
 6$(KK=V
r#   rg   rF   c                     |}| j                   dk7  r| j                  |      }| j                  |      }| j                  |      }| j	                  ||      }|S )Nr   )r   r   rq   r   r   )rC   rg   r   s      r!   rJ   zEfficientNetBlock.forwards  s[    "
! NN=9M++M: ++M:
MBr#   r   rR   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e	   dee	   de
fdZ xZS )
EfficientNetEncoderz
    Forward propogates 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 ]k  }|dk(  rdnd}|dkD  rdn|	}	|dkD  r|n|}||j                  v rdnd}|j                  |z  |z  }t!        ||||	|
||||	      }|j#                  |       |dz  }m  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 rH   )r   mathceildepth_coefficient)repeatsrC   s    r!   round_repeatsz3EfficientNetEncoder.__init__.<locals>.round_repeats  s"    tyy!7!7'!ABCCr#   c              3   .   K   | ]  } |        y wrH    ).0nr   s     r!   	<genexpr>z/EfficientNetEncoder.__init__.<locals>.<genexpr>  s     Laq)Ls   r   TFr   )	r   r_   r8   r2   r$   r   r   r   rm   i   ra   rb   ro   )r6   r7   r   r   lenrV   sumnum_block_repeatsranger"   rW   strideskernel_sizesexpand_ratiosdepthwise_paddingdrop_connect_rater   appendr   
ModuleListblocksr:   top_convr<   
hidden_dimr=   r>   top_bnr   r@   top_activation)rC   r   num_base_blocks
num_blockscurr_block_numr   ir_   r8   r2   r$   r   jr   rm   r   blockr   rD   s   `                @r!   r7   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$%Ev*8F<T<T*TZ^"44~E
R	)!!#! +!-'##1
 e$!#'$	$8 mmF+		&vt4
 nn**0E0EPVPjPj
 %V%6%67r#   rg   output_hidden_statesreturn_dictrF   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rH   r   )r   vs     r!   r   z.EfficientNetEncoder.forward.<locals>.<genexpr>  s     Xq!-Xs   )last_hidden_staterg   )r   r   r   r   tupler   )rC   rg   r   r   all_hidden_statesr   s         r!   rJ   zEfficientNetEncoder.forward  s     1E],$[[ 	6E!-0M#!m%55!	6
 m4M2++M:X]4E$FXXX-++
 	
r#   )FT)rK   rL   rM   rN   r   r7   rO   rj   r   rw   r   rJ   rQ   rR   s   @r!   r   r     sW    581 58t 05&*	
((
 'tn
 d^	

 
(
r#   r   c                   &    e Zd ZdZeZdZdZg Zd Z	y)EfficientNetPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    efficientnetrE   c                    t        |t        j                  t        j                  f      rm|j                  j
                  j                  d| j                  j                         |j                  %|j                  j
                  j                          yyt        |t        j                        rJ|j                  j
                  j                          |j                  j
                  j                  d       yy)zInitialize the weightsg        )meanstdNg      ?)r'   r   Linearr:   weightdatanormal_r   initializer_ranger3   zero_	LayerNormfill_)rC   modules     r!   _init_weightsz)EfficientNetPreTrainedModel._init_weights  s    fryy"))45 MM&&CT[[5R5R&S{{&  &&( '-KK""$MM$$S) .r#   N)
rK   rL   rM   rN   r   config_classbase_model_prefixmain_input_name_no_split_modulesr   r   r#   r!   r   r     s$    
 &L&$O
*r#   r   zUThe bare EfficientNet model outputting raw features without any specific head on top.c                        e Zd Zdef fdZ ee       eee	e
de      	 	 	 d
dej                  dee   dee   deee	f   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 )r6   r7   r   r+   r   r   encoderpooling_typer   	AvgPool2dr   pooler	MaxPool2d
ValueErrorpooling	post_initrB   s     r!   r7   zEfficientNetModel.__init__  s     08*62 &(,,v'8'8DIDK  E),,v'8'8DIDKQRXR`R`Qabcc 	r#   vision)
checkpointoutput_typer   modalityexpected_outputrE   r   r   rF   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_outputrg   )r   r   use_return_dictr   r   r   r   reshapeshaper   rg   )rC   rE   r   r   embedding_outputencoder_outputsr   pooled_outputs           r!   rJ   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)rK   rL   rM   r   r7   r   EFFICIENTNET_INPUTS_DOCSTRINGr   _CHECKPOINT_FOR_DOCr   _CONFIG_FOR_DOC_EXPECTED_OUTPUT_SHAPErO   rj   r   rw   r   r   rJ   rQ   rR   s   @r!   r   r     s    
1 " ++HI&<$. +//3&*	"
''"
 'tn"
 d^	"

 
u>>	?"
 J"
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.
    c                        e Zd Z fdZ ee       eeee	e
      	 	 	 	 d	dej                  deej                     dee   dee   deeef   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   )r6   r7   
num_labelsr   r   r   r   r   dropout_rater   r   r   Identity
classifierr   rB   s     r!   r7   z+EfficientNetForImageClassification.__init__=  s      ++-f5zzF$7$78KO??]^K^"))F$5$5tGdfdododq 	r#   )r   r   r   r   rE   labelsr   r   rF   c                 :   ||n| j                   j                  }| j                  |||      }|r|j                  n|d   }| j	                  |      }| j                  |      }d}|| j                   j                  | j                  dk(  rd| j                   _        nl| j                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _        nd| j                   _        | j                   j                  dk(  rIt               }	| j                  dk(  r& |	|j                         |j                               }n |	||      }n| j                   j                  dk(  r=t               }	 |	|j                  d| j                        |j                  d            }n,| j                   j                  dk(  rt!               }	 |	||      }|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   
regressionsingle_label_classificationmulti_label_classificationr   )losslogitsrg   )r   r   r   r   r   r  problem_typer  dtyperO   longr   r	   r   r   viewr   r   rg   )rC   rE   r  r   r   outputsr   r  r  loss_fctoutputs              r!   rJ   z*EfficientNetForImageClassification.forwardI  s   ( &1%<k$++B]B]##LG[it#u1<--'!*]3/{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE3!//
 	
r#   )NNNN)rK   rL   rM   r7   r   r   r   _IMAGE_CLASS_CHECKPOINTr   r   _IMAGE_CLASS_EXPECTED_OUTPUTrO   rj   r   
LongTensorrw   r   r   rJ   rQ   rR   s   @r!   r  r  5  s    
 ++HI*8$4	 +/-1/3&*4
''4
 ))*4
 'tn	4

 d^4
 
u::	;4
 J4
r#   r  )T)7rN   r   typingr   r   r   rO   torch.utils.checkpointr   torch.nnr   r   r	   activationsr   modeling_outputsr   r   r   modeling_utilsr   utilsr   r   r   r   configuration_efficientnetr   
get_loggerrK   loggerr   r   r   r  r  EFFICIENTNET_START_DOCSTRINGr   r   r"   rw   r)   Moduler+   r:   rT   r^   rl   ry   r   r   r   r   r   r  r   r#   r!   <module>r&     s   "  ) )    A A ! 
 .  ; 
		H	% ' / '  3 1 	  ! , C @U3:. @ @(RYY 2
")) 
4 4$ $N$RYY $N")) BN		 NbW
")) W
t*/ *0 [ <
3 <
	<
~  !H
)D H
H
r#   