
    rhzS                        d Z ddlZddl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 ddlmZ  ej2                  e      ZdedefdZd,deeef   defdZ  G d dejB                        Z" G d dejF                        Z$ G d dejB                        Z% G d dejB                        Z& G d dejB                        Z' G d dejB                        Z( G d d ejB                        Z) G d! d"ejB                        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)OptionalUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )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       /var/www/html/ai-insurance-compliance-backend/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%   6   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   r3   zEfficientNetEmbeddings.__init__O   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+   r7   r;   r=   )r?   rA   featuress      r   forwardzEfficientNetEmbeddings.forwardZ   sA    <<-##H->>(+??8,r   )
__name__
__module____qualname____doc__r   r3   torchTensorrF   __classcell__r@   s   @r   r'   r'   J   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)r2   r3   )r?   rR   depth_multiplierr    r.   r+   rT   r/   rV   rS   r@   s             r   r3   z$EfficientNetDepthwiseConv2d.__init__d   s=     #%55#%#% 	 
	
r   )r   r	   r   r   r   Tzeros)rG   rH   rI   r3   rM   rN   s   @r   rP   rP   c   s$     
 
r   rP   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_dimr4   r.   c                     t         |           t        j                  ||ddd      | _        t        j
                  ||j                        | _        t        |j                     | _
        y )Nr   sameFrR   rS   r    r+   r/   )num_featuresr0   )r2   r3   r   r6   expand_convr8   r9   	expand_bnr
   r<   
expand_act)r?   r   r[   r4   r.   r@   s        r   r3   z#EfficientNetExpansionLayer.__init__   sZ    99 
 W&BWBWX !2!23r   hidden_statesrB   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rD   )r`   ra   rb   r?   rc   s     r   rF   z"EfficientNetExpansionLayer.forward   s4    ((7}56r   )rG   rH   rI   rJ   r   r   r3   rK   FloatTensorrL   rF   rM   rN   s   @r   rZ   rZ   }   sH    
41 
43 
4 
4VY 
4U%6%6 5<< r   rZ   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[   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,   r]   )r!   r*   Fr-   r_   r0   r1   )r2   r3   r.   r%   r   r5   depthwise_conv_padrP   depthwise_convr8   r9   r:   depthwise_normr
   r<   depthwise_act)	r?   r   r[   r.   r    ri   conv_padr+   r@   s	           r   r3   z#EfficientNetDepthwiseLayer.__init__   s     	"kkQ.7Fk.A"$,,w"?9FHSX
 !nnV%:%:VE_E_
 $F$5$56r   rc   rB   c                     | j                   dk(  r| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S )Nr   )r.   rl   rm   rn   ro   re   s     r   rF   z"EfficientNetDepthwiseLayer.forward   sT    ;;! 33MBM++M:++M:**=9r   rG   rH   rI   rJ   r   r   boolr3   rK   rf   rL   rF   rM   rN   s   @r   rh   rh      sZ    7"7 7 	7
 7 7,	U%6%6 	5<< 	r   rh   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_sizer]   )rR   rS   r    r+   )r2   r3   dimr   r   squeeze_expansion_ratiodim_ser   AdaptiveAvgPool2dsqueezer6   reducerw   r
   r<   
act_reduceSigmoid
act_expand)r?   r   r[   rv   rw   r@   s        r   r3   z'EfficientNetSqueezeExciteLayer.__init__   s    !':V!S&*H*H!HIJ++:ii	
 ii	
 !!2!23**,r   rc   rB   c                     |}| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  ||      }|S rD   )r~   r   r   rw   r   rK   mul)r?   rc   inputss      r   rF   z&EfficientNetSqueezeExciteLayer.forward   sc    ]3M26M26		&-8r   )Frr   rN   s   @r   ru   ru      sH    '1 '3 'C 'Y] '*
U%6%6 
5<< 
r   ru   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[   r4   r.   	drop_rateid_skipc                    t         |           |dk(  xr | | _        t        j                  ||ddd      | _        t        j                  ||j                  |j                        | _	        t        j                  |      | _        y )Nr   r]   Fr^   rk   p)r2   r3   apply_dropoutr   r6   project_convr8   r9   r:   
project_bnDropoutdropout)r?   r   r[   r4   r.   r   r   r@   s          r   r3   z$EfficientNetFinalBlockLayer.__init__   sz     	#q[8[II 
 .. f&;&;fF`F`
 zzI.r   
embeddingsrc   rB   c                     | j                  |      }| j                  |      }| j                  r| j                  |      }||z   }|S rD   )r   r   r   r   )r?   r   rc   s      r   rF   z#EfficientNetFinalBlockLayer.forward   sG    ))-86 LL7M)J6Mr   rG   rH   rI   rJ   r   r   floatrs   r3   rK   rf   rL   rF   rM   rN   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[   r4   r.   expand_ratior    r   r   ri   c
                 l   t         |           || _        | j                  dk7  | _        ||z  }
| j                  rt	        |||
|      | _        t        || j                  r|
n||||	      | _        t        |||
| j                        | _	        t        || j                  r|
n|||||      | _        y )Nr   )r   r[   r4   r.   )r   r[   r.   r    ri   )r   r[   rv   rw   )r   r[   r4   r.   r   r   )r2   r3   r   rw   rZ   	expansionrh   rm   ru   squeeze_exciter   
projection)r?   r   r[   r4   r.   r   r    r   r   ri   expand_in_dimr@   s              r   r3   zEfficientNetBlock.__init__!  s     	(''1,-;;7fmFDN 9$(KK=V#)
 =&]4;;
 6$(KK=V
r   rc   rB   c                     |}| j                   dk7  r| j                  |      }| j                  |      }| j                  |      }| j	                  ||      }|S )Nr   )r   r   rm   r   r   )r?   rc   r   s      r   rF   zEfficientNetBlock.forwardJ  s[    "
! NN=9M++M: ++M:
MBr   r   rN   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 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 rD   )r   mathceildepth_coefficient)repeatsr?   s    r   round_repeatsz3EfficientNetEncoder.__init__.<locals>.round_repeatse  s"    tyy!7!7'!ABCCr   c              3   .   K   | ]  } |        y wrD    ).0nr   s     r   	<genexpr>z/EfficientNetEncoder.__init__.<locals>.<genexpr>j  s     Laq)Ls   r   r   )	r   r[   r4   r.   r    r   r   r   ri   i   r]   Fr^   rk   )r2   r3   r   r   lenrR   sumnum_block_repeatsranger   rS   strideskernel_sizesexpand_ratiosdepthwise_paddingdrop_connect_rater   appendr   
ModuleListblocksr6   top_convr8   
hidden_dimr9   r:   top_bnr
   r<   top_activation)r?   r   num_base_blocks
num_blockscurr_block_numr   ir[   r4   r.   r    r   jr   ri   r   blockr   r@   s   `                @r   r3   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   rc   output_hidden_statesreturn_dictrB   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rD   r   )r   vs     r   r   z.EfficientNetEncoder.forward.<locals>.<genexpr>  s     Xq!-Xs   )last_hidden_staterc   )r   r   r   r   tupler   )r?   rc   r   r   all_hidden_statesr   s         r   rF   zEfficientNetEncoder.forward  s     1E],$[[ 	6E!-0M#!m%55!	6
 m4M2++M:X]4E$FXXX-++
 	
r   )FT)rG   rH   rI   rJ   r   r3   rK   rf   r   rs   r   rF   rM   rN   s   @r   r   r   W  sW    581 58t 05&*	
((
 'tn
 d^	

 
(
r   r   c                   D    e Zd ZU eed<   dZdZg Zdej                  fdZ
y)EfficientNetPreTrainedModelr   efficientnetrA   modulec                 P   t        |t        j                  t        j                  t        j                  f      rm|j
                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyy)zInitialize the weightsg        )meanstdN)r#   r   Linearr6   r8   weightdatanormal_r   initializer_ranger/   zero_)r?   r   s     r   _init_weightsz)EfficientNetPreTrainedModel._init_weights  ss    fryy"))R^^DE MM&&CT[[5R5R&S{{&  &&( '	 Fr   N)rG   rH   rI   r   __annotations__base_model_prefixmain_input_name_no_split_modulesr   Moduler   r   r   r   r   r     s)    &$O)BII )r   r   c                   ~     e Zd Zdef fdZe	 	 	 dde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 )r2   r3   r   r'   r   r   encoderpooling_typer   	AvgPool2dr   pooler	MaxPool2d
ValueErrorpooling	post_initr>   s     r   r3   zEfficientNetModel.__init__  s     08*62 &(,,v'8'8DIDK  E),,v'8'8DIDKQRXR`R`Qabcc 	r   rA   r   r   rB   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_outputrc   )r   r   use_return_dictr   r   r   r   reshapeshaper   rc   )r?   rA   r   r   embedding_outputencoder_outputsr   pooled_outputs           r   rF   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)rG   rH   rI   r   r3   r   r   rK   rf   rs   r   r   r   rF   rM   rN   s   @r   r   r     ss    1 "  59/3&*	"
u001"
 'tn"
 d^	"

 
u>>	?"
 "
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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   )r2   r3   
num_labelsr   r   r   r   r   dropout_rater   r   r   Identity
classifierr   r>   s     r   r3   z+EfficientNetForImageClassification.__init__  s      ++-f5zzF$7$78KO??]^K^"))F$5$5tGdfdododq 	r   rA   labelsr   r   rB   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logitsrc   )r   r   r   r   r   r   problem_typer   dtyperK   longr   r   r~   r   viewr   r   rc   )r?   rA   r   r   r   outputsr   r  r  loss_fctoutputs              r   rF   z*EfficientNetForImageClassification.forward  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)rG   rH   rI   r3   r   r   rK   rf   
LongTensorrs   r   r   r   rF   rM   rN   s   @r   r   r     s    
  59-1/3&*4
u0014
 ))*4
 'tn	4

 d^4
 
u::	;4
 4
r   r   )r   r   r   )T)/rJ   r   typingr   r   rK   torch.utils.checkpointr   torch.nnr   r   r   activationsr
   modeling_outputsr   r   r   modeling_utilsr   utilsr   r   configuration_efficientnetr   
get_loggerrG   loggerr   r   r   rs   r%   r   r'   r6   rP   rZ   rh   ru   r   r   r   r   r   r   __all__r   r   r   <module>r     sq   "  "    A A ! 
 . , : 
		H	%, C @U3:. @ @(RYY 2
")) 
4 4$ $N$RYY $N")) BN		 NbW
")) W
t )/ ) )  5
3 5
 5
p B
)D B
B
J er   