
    ^j5                        d dl Z d dlmZ d dlZd dlmZ d dlmc mZ d dl	m
Z
 ddlmZ ddlmZmZ ddlm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mZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(  ejR                  e*      Z+ ed      e
 G d de$                    Z, G d de'      Z-ee G d de                    Z. G d de(      Z/ G d de!      Z0 G d de"      Z1 G d d ejd                        Z3 G d! d"ejd                        Z4 G d# d$ejd                        Z5 G d% d&e-      Z6 ed'(       G d) d*e&             Z7g d+Z8y),    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                       e Zd ZU dZdeiZ e       ZdZe	e
z  dz  ed<    e       ZdZeed<   dZeed<   dZeed	<   d
Zeed<   dZeed<   d Zy)SLANetConfiga  
    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           
          t        d| j                  ddg dg dddd|\  | _        }t        j                  di | y )	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     t/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/slanet/modular_slanet.pyr6   zSLANetConfig.__post_init__L   sS    'L 
(
 00 * H+	#
(
 
(
$f 	&&00    )__name__
__module____qualname____doc__r   sub_configsAttributeErrorvision_configr   dictr	   __annotations__post_conv_in_channelsr   intr    r"   strr$   r&   r6   r5   r:   r9   r   r   ,   sq      %j1K"$M6:OT,,t3:*,"$C$K!J!OSNC1r:   r   c                   >    e Zd Zg Z ej
                         d        Zy)SLANetPreTrainedModelc                    t        j                  |       t        |t        j                        r|j
                  dkD  r"dt        j                  |j
                        z  nd}t        j                  |j                  | |       t        j                  |j                  | |       |j                  "t        j                  |j                  | |       |j                  "t        j                  |j                  | |       t        |t              rdt        j                  | j                  j
                  dz        z  }|j                   fD ]  }|j#                         D ]n  }t        |t        j$                        st        j                  |j&                  | |       |j(                  Mt        j                  |j(                  | |       p  yy)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)r7   modulestd	generatorlayers        r9   rJ   z#SLANetPreTrainedModel._init_weights^   sf    	%%f- fbjj)9?9K9Ka9O#		&"4"455UVCMM&**SD#6MM&**SD#6~~)fnnsdC8~~)fnnsdC8 fm,		$++"9"9C"?@@C$88: A	&//1 AE!%3ellSD#> ::1 MM%**sdC@	AA -r:   N)r;   r<   r=   _keep_in_fp32_modules_stricttorchno_gradrJ   r5   r:   r9   rH   rH   [   s$    #% U]]_A Ar:   rH   c                   b    e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   y)SLANetForTableRecognitionOutputak  
    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_attentions)	r;   r<   r=   r>   rf   rb   FloatTensorrC   rg   r5   r:   r9   re   re   y   s4     48))D0704OU&&-4r:   re   c                       e Zd Zy)rV   Nr;   r<   r=   r5   r:   r9   rV   rV          r:   rV   c                       e Zd Zy)SLANetConvLayerNrj   r5   r:   r9   rm   rm      rk   r:   rm   c                   "     e Zd ZdZ fdZ xZS )!SLANetDepthwiseSeparableConvLayerz
    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                 T    t         |           t        j                         | _        y N)super__init__rL   Identitysqueeze_excitation_module)r7   in_channelsout_channelsstridekernel_sizerW   	__class__s         r9   rs   z*SLANetDepthwiseSeparableConvLayer.__init__   s     	)+&r:   )r;   r<   r=   r>   rs   __classcell__rz   s   @r9   ro   ro      s    	7 	7r:   ro   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )SLANetBottleneckc                 t    t         |           t        ||d|      | _        t	        |||d|      | _        y )Nr%   rv   rw   ry   
activation)rv   rw   ry   rx   rW   )rr   rs   rm   conv1ro   conv2)r7   rv   rw   ry   r   rW   rz   s         r9   rs   zSLANetBottleneck.__init__   sC     	$#,AZd

 7$%#

r:   hidden_statesreturnc                 J    | j                  |      }| j                  |      }|S rq   )r   r   )r7   r   s     r9   forwardzSLANetBottleneck.forward   s$    

=1

=1r:   )r;   r<   r=   rs   rb   rh   r   r{   r|   s   @r9   r~   r~      s'    
(U%6%6 5;L;L r:   r~   c                   d     e Zd ZdZ	 	 	 	 d fd	Zdej                  dej                  fdZ xZS )SLANetCSPLayerz
    Cross Stage Partial (CSP) network layer. Similar in structure to DFineCSPRepLayer, but with a different forward computation.
    c                 B   t         
|           t        ||z        }t        ||d|      | _        t        ||d|      | _        t        d|z  |d|      | _        t        j                  t        |      D 	cg c]  }	t        |||||       c}	      | _        y c c}	w )Nr%   )r   r   )rr   rs   rE   rm   r   r   conv3rL   
ModuleListranger~   bottlenecks)r7   rW   rv   rw   ry   	expansion
num_blocksr   hidden_channels_rz   s             r9   rs   zSLANetCSPLayer.__init__   s     	lY67$[/1Q[\
$[/1Q[\
$Q%8,V`a
== z* !/;PZ\bc
s   9Br   r   c                     | j                  |      }| j                  |      }| j                  D ]
  } ||      } t        j                  ||fd      }| j                  |      }|S )Nr%   dim)r   r   r   rb   catr   )r7   r   residual
bottlenecks       r9   r   zSLANetCSPLayer.forward   sh    ::m,

=1** 	6J&}5M	6 		=(";C

=1r:   )r   g      ?r%   r!   	r;   r<   r=   r>   rs   rb   rh   r   r{   r|   s   @r9   r   r      s:     
,
U%6%6 
5;L;L 
r:   r   c                   Z     e Zd ZdZ fdZdej                  dej                  fdZ xZS )SLANetCSPPANz;
    CSP-PAN: Path Aggregation Network with CSP layers
    c                 L   t         	|           |j                  }|j                  }|j                  }|j
                  }t        j                  t        t        |            D cg c]  }t        ||   |d|       c}      | _        t        j                  dd      | _        t        j                  t        t        |      dz
  dd      D cg c]  }t        ||dz  ||||       c}      | _        t        j                  t        t        |      dz
        D cg c]  }t!        |||d|	       c}      | _        t        j                  t        t        |      dz
        D cg c]  }t        ||dz  ||||       c}      | _        y c c}w c c}w c c}w c c}w )
Nr%   r   r   nearest)scale_factormoder   )ry   r   r   )ry   rx   rW   )rr   rs   r   r"   r$   r&   rL   r   r   lenrm   channel_projectorUpsampleupsampler   top_down_blocksro   downsamplesbottom_up_blocks)
r7   rW   in_channel_listrw   r   ry   r&   ir   rz   s
            r9   rs   zSLANetCSPPAN.__init__   s   
 	44&&
,,..!#
 s?34	    / 2[\is"
 C!}} s?3a7B?
   1$  +-)
 
 == s?3a78	  2   +!	
 !# s?3a78
   1$  +-)
!
K
	
s   %FFF*F!r   r   c                    g }t        t        | j                              D ])  }|j                   | j                  |   ||                + |d   g}t	        | j
                  t        |d d             D ]_  \  }}|d   }t        j                  ||j                  dd  d      } |t        j                  ||gd            }	|j                  |	       a t        t        |            }
|
d   }t	        | j                  | j                  |
dd        D ]-  \  }}} ||      } |t        j                  ||gd            }/ |j                  d      j!                  dd      }|S )	Nr   r   )sizer   r%   r   r   r   )r   r   r   appendzipr   reversedFinterpolateshaperb   r   listr   r   flatten	transpose)r7   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                  r9   r   zSLANetCSPPAN.forward*  s   T3345 	WC%%&Ad&<&<S&A-PSBT&UV	W 03414T5I5I8TfgjhjTkKl1m 	4-N-!22!6 !"&,,RS1!
 +5996GIZ5[ab+cdM$$]3	4  ): ;<)!,EHd335Eab5IF
 	jAo/A #3>"B,UYY8KM_7`fg-hiN		j '..q1;;AqAr:   r   r|   s   @r9   r   r      s-    =
~U%6%6 5;L;L r:   r   c            	            e Zd Zdef fdZeedej                  de	e
   deej                     ez  fd              Z xZS )SLANetBackbonerW   c                     t         |   |       t        |      | _        t	        || j                  j
                  dd        | _        | j                          y )Nr   )rr   rs   r   vision_backboner   num_featurespost_csp_pan	post_init)r7   rW   rz   s     r9   rs   zSLANetBackbone.__init__G  sK     ,V4(1E1E1R1RSTSU1VWr:   r   r8   r   c                      | j                   |fi |}| j                  |j                        }t        ||j                        S )N)last_hidden_stater   )r   r   feature_mapsr
   r   )r7   r   r8   outputss       r9   r   zSLANetBackbone.forwardN  sJ    
 '$&&}??))'*>*>?-+!//
 	
r:   )r;   r<   r=   r   rs   r   r   rb   rh   r   r   tupler
   r   r{   r|   s   @r9   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            	       t    e Zd ZdgZeedej                  dee	   de
ej                     ez  fd              Zy)SLANetForTableRecognitionnum_batches_trackedpixel_valuesr8   r   c                      | j                   |fi |} | j                  |j                  fi |}t        |j                  |j                  |j                  |j
                        S )N)r   r   rf   rg   )backboneheadr   re   r   
attentions)r7   r   r8   r   head_outputss        r9   r   z!SLANetForTableRecognition.forwardd  se    
  $--77 tyy!:!:EfE.*<<!//+99(33	
 	
r:   N)r;   r<   r=   _keys_to_ignore_on_load_missingr   r   rb   rh   r   r   r   re   r   r5   r:   r9   r   r   [  s\     (=&=#
!--
9?@R9S
	u  	!$C	C
  
r:   r   )r   r   rH   rV   r   )9rN   dataclassesr   rb   torch.nnrL   torch.nn.functional
functionalr   huggingface_hub.dataclassesr    r   rP   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   rH   re   rV   rm   ro   Moduler~   r   r   r   r   __all__r5   r:   r9   <module>r      sN     !     . & 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:   