
    Z j5                        S SK r S SKJr  S SKrS SKJr  S SKJs  Jr  S SK	J
r
  SSKJr  SSKJrJr  SSKJ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Jr  SSKJr  SSK J!r!J"r"  SSK#J$r$  SSK%J&r&J'r'J(r(  \RR                  " \*5      r+\" SS9\
 " S S\$5      5       5       r, " S S\'5      r-\\ " S S\5      5       5       r. " S S\(5      r/ " S S\!5      r0 " S S\"5      r1 " S S \Rd                  5      r3 " S! S"\Rd                  5      r4 " S# S$\Rd                  5      r5 " S% S&\-5      r6\" S'S(9 " S) S*\&5      5       r7/ S+Qr8g),    N)	dataclass)strict   )initialization)%consolidate_backbone_kwargs_to_configload_backbone)PreTrainedConfig)BaseModelOutputWithNoAttention)PreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tuplelogging   )
AutoConfig)PPLCNetConvLayer"PPLCNetDepthwiseSeparableConvLayer)SLANeXtConfig)SLANeXtForTableRecognitionSLANeXtPreTrainedModelSLANeXtSLAHeadz$PaddlePaddle/SLANet_plus_safetensors)
checkpointc                       \ rS rSr% SrS\0r\" 5       rSr	\
\-  S-  \S'   \" 5       rSr\\S'   Sr\\S'   S	r\\S
'   Sr\\S'   Sr\\S'   S rSrg)SLANetConfig,   aa  
post_conv_out_channels (`int`, *optional*, defaults to 96):
    Number of output channels for the post-encoder convolution layer.
out_channels (`int`, *optional*, defaults to 50):
    Vocabulary size for the table structure token prediction head, i.e., the number of distinct structure
    tokens the model can predict.
hidden_size (`int`, *optional*, defaults to 256):
    Dimensionality of the hidden states in the attention GRU cell and the structure/location prediction heads.
max_text_length (`int`, *optional*, defaults to 500):
    Maximum number of autoregressive decoding steps (tokens) for the structure and location decoder.
csp_kernel_size (`int`, *optional*, defaults to 5):
    The kernel size of the Cross Stage Partial (CSP) layer.
csp_num_blocks (`int`, *optional*, defaults to 1):
    Number of blocks within the Cross Stage Partial (CSP) layer.
backbone_configN`   post_conv_out_channels   hidden_size	hardswish
hidden_act   csp_kernel_size   csp_num_blocksc           
          [        SU R                  SS/ SQ/ SQSS.S.UD6u  U l        n[        R                  " S0 UD6  g )	Npp_lcnetr&   )stage2stage3stage4stage5)r   r      r$      )scaleout_featuresout_indicesdivisor)r   default_config_typedefault_config_kwargs )r   r   r	   __post_init__)selfkwargss     z/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/slanet/modular_slanet.pyr7   SLANetConfig.__post_init__L   sS    'L 
(
 00 * H+	#
(
 
(
$f 	&&00    )r   )__name__
__module____qualname____firstlineno____doc__r   sub_configsAttributeErrorvision_configr   dictr	   __annotations__post_conv_in_channelsr   intr!   r#   strr%   r'   r7   __static_attributes__r6   r<   r:   r   r   ,   sq      %j1K"$M6:OT,,t3:*,"$C$K!J!OSNC1r<   r   c                   F    \ rS rSr/ r\R                  " 5       S 5       rSrg)SLANetPreTrainedModel[   c                    [         R                  " U5        [        U[        R                  5      (       a  UR
                  S:  a#  S[        R                  " UR
                  5      -  OSn[        R                  " UR                  U* U5        [        R                  " UR                  U* U5        UR                  b#  [        R                  " UR                  U* U5        UR                  b#  [        R                  " UR                  U* U5        [        U[        5      (       a  S[        R                  " U R                  R
                  S-  5      -  nUR                   4 H  nUR#                  5        Hy  n[        U[        R$                  5      (       d  M$  [        R                  " UR&                  U* U5        UR(                  c  MV  [        R                  " UR(                  U* U5        M{     M     gg)zInitialize the weightsr   g      ?N)r   _init_weights
isinstancennGRUCellr!   mathsqrtinituniform_	weight_ih	weight_hhbias_ihbias_hhSLANetSLAHeadconfigstructure_generatorchildrenLinearweightbias)r8   modulestd	generatorlayers        r:   rO   #SLANetPreTrainedModel._init_weights^   sa    	%%f- fbjj))9?9K9Ka9O#		&"4"455UVCMM&**SD#6MM&**SD#6~~)fnnsdC8~~)fnnsdC8 fm,,		$++"9"9C"?@@C$88:	&//1E!%33ellSD#> ::1 MM%**sdC@	 2 ; -r<   r6   N)	r=   r>   r?   r@   _keep_in_fp32_modules_stricttorchno_gradrO   rJ   r6   r<   r:   rL   rL   [   s"    #% 
]]_A Ar<   rL   c                   j    \ rS rSr% SrSr\R                  S-  \S'   Sr	\R                  S-  \S'   Sr
g)SLANetForTableRecognitionOutputy   aW  
head_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
    Hidden-states of the SLANetSLAHead at each prediction step, varies up to max `self.config.max_text_length` states (depending on early exits).
head_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
    Attentions of the SLANetSLAHead at each prediction step, varies up to max `self.config.max_text_length` attentions (depending on early exits).
Nhead_hidden_stateshead_attentionsr6   )r=   r>   r?   r@   rA   rm   rh   FloatTensorrF   rn   rJ   r6   r<   r:   rk   rk   y   s4     48))D0704OU&&-4r<   rk   c                       \ rS rSrSrg)r[      r6   Nr=   r>   r?   r@   rJ   r6   r<   r:   r[   r[          r<   r[   c                       \ rS rSrSrg)SLANetConvLayer   r6   Nrr   r6   r<   r:   ru   ru      rs   r<   ru   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )!SLANetDepthwiseSeparableConvLayer   z
Depthwise Separable Convolution Layer: Depthwise Conv -> Pointwise Conv
Core component of lightweight models (e.g., MobileNet, PP-LCNet) that significantly reduces
the number of parameters and computational cost.
c                 V   > [         TU ]  5         [        R                  " 5       U l        g N)super__init__rQ   Identitysqueeze_excitation_module)r8   in_channelsout_channelsstridekernel_sizer\   	__class__s         r:   r}   *SLANetDepthwiseSeparableConvLayer.__init__   s     	)+&r<   )r   )r=   r>   r?   r@   rA   r}   rJ   __classcell__r   s   @r:   rx   rx      s    	7 	7r<   rx   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )SLANetBottleneck   c                 f   > [         TU ]  5         [        XSUS9U l        [	        UUUSUS9U l        g )Nr&   r   r   r   
activation)r   r   r   r   r\   )r|   r}   ru   conv1rx   conv2)r8   r   r   r   r   r\   r   s         r:   r}   SLANetBottleneck.__init__   sA     	$#AZd

 7$%#

r<   hidden_statesreturnc                 J    U R                  U5      nU R                  U5      nU$ r{   r   r   )r8   r   s     r:   forwardSLANetBottleneck.forward   s$    

=1

=1r<   r   )
r=   r>   r?   r@   r}   rh   ro   r   rJ   r   r   s   @r:   r   r      s,    
(U%6%6 5;L;L  r<   r   c                   r   ^  \ rS rSrSr    SU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	SLANetCSPLayer   z~
Cross Stage Partial (CSP) network layer. Similar in structure to DFineCSPRepLayer, but with a different forward computation.
c                 .  > [         T
U ]  5         [        X5-  5      n[        X(SUS9U l        [        X(SUS9U l        [        SU-  USUS9U l        [        R                  " [        U5       V	s/ s H  n	[        XXGU5      PM     sn	5      U l        g s  sn	f )Nr&   )r   r   )r|   r}   rH   ru   r   r   conv3rQ   
ModuleListranger   bottlenecks)r8   r\   r   r   r   	expansion
num_blocksr   hidden_channels_r   s             r:   r}   SLANetCSPLayer.__init__   s     	l67$[1Q[\
$[1Q[\
$Q%8,V`a
== z**A !;\bc*
s   .Br   r   c                     U R                  U5      nU R                  U5      nU R                   H  nU" U5      nM     [        R                  " X4SS9nU R                  U5      nU$ )Nr&   dim)r   r   r   rh   catr   )r8   r   residual
bottlenecks       r:   r   SLANetCSPLayer.forward   sa    ::m,

=1**J&}5M + 		=";C

=1r<   )r   r   r   r   )r   g      ?r&   r"   r=   r>   r?   r@   rA   r}   rh   ro   r   rJ   r   r   s   @r:   r   r      s?     
,
U%6%6 
5;L;L 
 
r<   r   c                   f   ^  \ rS rSrSrU 4S jrS\R                  S\R                  4S jrSr	U =r
$ )SLANetCSPPAN   z3
CSP-PAN: Path Aggregation Network with CSP layers
c                 N  > [         T	U ]  5         UR                  nUR                  nUR                  nUR
                  n[        R                  " [        [        U5      5       Vs/ s H  n[        X'   USUS9PM     sn5      U l        [        R                  " SSS9U l        [        R                  " [        [        U5      S-
  SS5       Vs/ s H  n[        UUS-  UUUUS9PM     sn5      U l        [        R                  " [        [        U5      S-
  5       Vs/ s H  n[!        UUUSUS	9PM     sn5      U l        [        R                  " [        [        U5      S-
  5       Vs/ s H  n[        UUS-  UUUUS9PM     sn5      U l        g s  snf s  snf s  snf s  snf )
Nr&   r   r   nearest)scale_factormoder   )r   r   r   )r   r   r\   )r|   r}   r   r#   r%   r'   rQ   r   r   lenru   channel_projectorUpsampleupsampler   top_down_blocksrx   downsamplesbottom_up_blocks)
r8   r\   in_channel_listr   r   r   r'   ir   r   s
            r:   r}   SLANetCSPPAN.__init__   s   
 	44&&
,,..!#
 s?34	 5A   / 2[\is 5	"
 C!}} s?3a7B?
 @A  1$  +-) @
 
 == s?3a78	 9A 2   +! 9	
 !# s?3a78
 9A  1$  +-) 9
!
K
	
s   &FFF+F"r   r   c                    / n[        [        U R                  5      5       H)  nUR                  U R                  U   " X   5      5        M+     US   /n[	        U R
                  [        US S 5      5       HZ  u  pVUS   n[        R                  " UUR                  SS  SS9nU" [        R                  " X/SS95      n	UR                  U	5        M\     [        [        U5      5      n
U
S   n[	        U R                  U R                  U
SS  5       H*  u  pnU" U5      nU" [        R                  " X/SS95      nM,     UR                  S5      R!                  SS5      nU$ )	Nr   r   )sizer   r&   r   r   r   )r   r   r   appendzipr   reversedFinterpolateshaperh   r   listr   r   flatten	transpose)r8   r   projected_featuresidxtop_down_featurestop_down_blocklow_level_featurehigh_level_featureupsampled_featurefused_featurepyramid_featuresoutput_featuredownsample_layerbottom_up_blockdownsampled_features                  r:   r   SLANetCSPPAN.forward*  ss   T3345C%%d&<&<S&A-BT&UV 6 03414T5I5I8TfgjhjTkKl1m-N!22!6 !"&,,RS1!
 +5996G5[ab+cdM$$]3 2n  ): ;<)!,EHd335Eab5IF
A/A #3>"B,UYY8K7`fg-hiN	F
 '..q1;;AqAr<   )r   r   r   r   r   r   r   s   @r:   r   r      s2    =
~U%6%6 5;L;L  r<   r   c            	          ^  \ rS rSrS\4U 4S jjr\\S\R                  S\
\   S\\R                     \-  4S j5       5       rSrU =r$ )	SLANetBackboneiF  r\   c                    > [         TU ]  U5        [        U5      U l        [	        XR                  R
                  SS  5      U l        U R                  5         g )Nr   )r|   r}   r   vision_backboner   num_featurespost_csp_pan	post_init)r8   r\   r   s     r:   r}   SLANetBackbone.__init__G  sI     ,V4(1E1E1R1RSTSU1VWr<   r   r9   r   c                     U R                   " U40 UD6nU R                  UR                  5      n[        UUR                  S9$ )N)last_hidden_stater   )r   r   feature_mapsr
   r   )r8   r   r9   outputss       r:   r   SLANetBackbone.forwardN  sH    
 &&}??))'*>*>?-+!//
 	
r<   )r   r   )r=   r>   r?   r@   r   r}   r   r   rh   ro   r   r   tupler
   r   rJ   r   r   s   @r:   r   r   F  s`    |  
"..
:@AS:T
	u  	!$B	B
  
r<   r   z
    SLANet Table Recognition model for table recognition tasks. Wraps the core SLANetPreTrainedModel
    and returns outputs compatible with the Transformers table recognition API.
    )custom_introc            	       ~    \ rS rSrS/r\\S\R                  S\	\
   S\\R                     \-  4S j5       5       rSrg)	SLANetForTableRecognitioni[  num_batches_trackedpixel_valuesr9   r   c                     U R                   " U40 UD6nU R                  " UR                  40 UD6n[        UR                  UR                  UR                  UR
                  S9$ )N)r   r   rm   rn   )backboneheadr   rk   r   
attentions)r8   r   r9   r   head_outputss        r:   r   !SLANetForTableRecognition.forwardd  sa    
 --77yy!:!:EfE.*<<!//+99(33	
 	
r<   r6   N)r=   r>   r?   r@   _keys_to_ignore_on_load_missingr   r   rh   ro   r   r   r   rk   r   rJ   r6   r<   r:   r   r   [  s\     (=&=#
!--
9?@R9S
	u  	!$C	C
  
r<   r   )r   r   rL   r[   r   )9rS   dataclassesr   rh   torch.nnrQ   torch.nn.functional
functionalr   huggingface_hub.dataclassesr    r   rU   backbone_utilsr   r   configuration_utilsr	   modeling_outputsr
   modeling_utilsr   processing_utilsr   utilsr   r   r   r   autor   pp_lcnet.modeling_pp_lcnetr   r   slanext.configuration_slanextr   slanext.modeling_slanextr   r   r   
get_loggerr=   loggerr   rL   rk   r[   ru   rx   Moduler   r   r   r   r   __all__r6   r<   r:   <module>r     sL     !     . & R 3 > - & R R  ] 9  
		H	% AB*1= *1  C*1ZA2 A< 
	5&D 	5  	5	N 		& 	7(J 7&ryy 8%RYY %P]299 ]@
* 
* 
 : 

& tr<   