
    ^jE                        d Z ddlZddlZddlm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 dd	lmZ dd
lmZmZmZmZ ddlmZ  ej0                  e      Z ed      e G d de                    Z ed      e G d de                    Z ed      e G d de                    Zd Zd Z G d dej@                        Z! G d dej@                        Z" G d dej@                        Z# G d d ej@                        Z$ G d! d"ej@                        Z% G d# d$ej@                        Z& G d% d&ej@                        Z' G d' d(ej@                        Z( G d) d*ej@                        Z) G d+ d,ej@                        Z* G d- d.e      Z+ G d/ d0ej@                        Z,e G d1 d2e             Z-e G d3 d4e-             Z. ed5       G d6 d7e-             Z/g d8Z0y)9zPyTorch Donut Swin Transformer model.

This implementation is identical to a regular Swin Transformer, without final layer norm on top of the final hidden
states.    N)	dataclass)nn   )initialization)ACT2FN)GradientCheckpointingLayer)PreTrainedModel)ModelOutputauto_docstringlogging	torch_int   )DonutSwinConfigzS
    DonutSwin encoder's outputs, with potential hidden states and attentions.
    )custom_introc                       e Zd ZU dZdZej                  dz  ed<   dZe	ej                  df   dz  ed<   dZ
e	ej                  df   dz  ed<   dZe	ej                  df   dz  ed<   y)DonutSwinEncoderOutputa  
    reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, hidden_size, height, width)`.

        Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
        include the spatial dimensions.
    Nlast_hidden_state.hidden_states
attentionsreshaped_hidden_states)__name__
__module____qualname____doc__r   torchFloatTensor__annotations__r   tupler   r        x/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/donut/modeling_donut_swin.pyr   r   %   s}     37u((4/6:>M5**C/047>7;Je'',-4;CGE%"3"3S"89D@Gr    r   z[
    DonutSwin model's outputs that also contains a pooling of the last hidden states.
    c                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	e
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   y)	DonutSwinModelOutputa  
    pooler_output (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*, returned when `add_pooling_layer=True` is passed):
        Average pooling of the last layer hidden-state.
    reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, hidden_size, height, width)`.

        Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
        include the spatial dimensions.
    Nr   pooler_output.r   r   r   )r   r   r   r   r   r   r   r   r$   r   r   r   r   r   r    r!   r#   r#   <   s    	 37u((4/6.2M5$$t+2:>M5**C/047>7;Je'',-4;CGE%"3"3S"89D@Gr    r#   z5
    DonutSwin outputs for image classification.
    c                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	e
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   y)	DonutSwinImageClassifierOutputa7  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Classification (or regression if config.num_labels==1) loss.
    logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`):
        Classification (or regression if config.num_labels==1) scores (before SoftMax).
    reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, hidden_size, height, width)`.

        Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
        include the spatial dimensions.
    Nlosslogits.r   r   r   )r   r   r   r   r'   r   r   r   r(   r   r   r   r   r   r    r!   r&   r&   V   s     &*D%

d
")'+FE$+:>M5**C/047>7;Je'',-4;CGE%"3"3S"89D@Gr    r&   c                     | j                   \  }}}}| j                  |||z  |||z  ||      } | j                  dd      j                         j                  d|||      }|S )z2
    Partitions the given input into windows.
       r   shapeview	transpose
contiguous)input_featurewindow_size
batch_sizeheightwidthnum_channelswindowss          r!   window_partitionr8   s   st     /<.A.A+J|!&&Fk);8Lk[gM %%a+668==b+{\hiGNr    c                     | j                   d   }| j                  d||z  ||z  |||      } | j                  dd      j                         j                  d|||      } | S )z?
    Merges windows to produce higher resolution features.
    r+   r*   r   r,   )r7   r2   r4   r5   r6   s        r!   window_reverser:      sg     ==$Lll2v4e{6JKYdfrsG1%00277FE<XGNr    c            
            e Zd ZdZd fd	Zdej                  dededej                  fdZ	 	 dd	ej                  dz  d
ej                  dz  dedeej                     fdZ xZS )DonutSwinEmbeddingszW
    Construct the patch and position embeddings. Optionally, also the mask token.
    c                 ~   t         |           t        |      | _        | j                  j                  }| j                  j
                  | _        |r4t        j                  t        j                  dd|j                              nd | _        |j                  r=t        j                  t        j                  d|dz   |j                              | _        nd | _        t        j                  |j                        | _        t        j"                  |j$                        | _        |j(                  | _        || _        y )Nr   )super__init__DonutSwinPatchEmbeddingspatch_embeddingsnum_patches	grid_size
patch_gridr   	Parameterr   zeros	embed_dim
mask_tokenuse_absolute_embeddingsposition_embeddings	LayerNormnormDropouthidden_dropout_probdropout
patch_sizeconfig)selfrQ   use_mask_tokenrB   	__class__s       r!   r?   zDonutSwinEmbeddings.__init__   s     8 @++77//99O]",,u{{1a9I9I'JKcg))')||EKK;QR?TZTdTd4e'fD$'+D$LL!1!12	zz&"<"<= ++r    
embeddingsr4   r5   returnc                    |j                   d   dz
  }| j                  j                   d   dz
  }t        j                  j	                         s||k(  r||k(  r| j                  S | j                  ddddf   }| j                  ddddf   }|j                   d   }|| j
                  z  }	|| j
                  z  }
t        |dz        }|j                  d|||      }|j                  dddd      }t        j                  j                  ||	|
fdd	
      }|j                  dddd      j                  dd|      }t        j                  ||fd      S )a   
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
        images. This method is also adapted to support torch.jit tracing.

        Adapted from:
        - https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
        - https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
        r   Nr+   g      ?r   r   r*   bicubicF)sizemodealign_cornersdim)r-   rJ   r   jit
is_tracingrP   r   reshapepermuter   
functionalinterpolater.   cat)rR   rU   r4   r5   rB   num_positionsclass_pos_embedpatch_pos_embedr]   
new_height	new_widthsqrt_num_positionss               r!   interpolate_pos_encodingz,DonutSwinEmbeddings.interpolate_pos_encoding   s`    !&&q)A-0066q9A= yy##%+*F6UZ?+++221bqb59221ab59r"t.
T__,	&}c'9:)11!5GI[]`a)11!Q1=--33i(	 4 
 *11!Q1=BB1b#Nyy/?;CCr    Npixel_valuesbool_masked_posrk   c                    |j                   \  }}}}| j                  |      \  }}	| j                  |      }|j                         \  }
}}|K| j                  j                  |
|d      }|j                  d      j                  |      }|d|z
  z  ||z  z   }| j                  (|r|| j                  |||      z   }n|| j                  z   }| j                  |      }||	fS )Nr+   g      ?)r-   rA   rL   rY   rH   expand	unsqueezetype_asrJ   rk   rO   )rR   rl   rm   rk   _r6   r4   r5   rU   output_dimensionsr3   seq_lenmask_tokensmasks                 r!   forwardzDonutSwinEmbeddings.forward   s     *6););&<(,(=(=l(K%
%YYz*
!+!2
GQ&//00WbIK",,R088ED#sTz2[45GGJ##/''$*G*G
TZ\a*bb
'$*B*BB
\\*-
,,,r    )FNF)r   r   r   r   r?   r   Tensorintrk   r   
BoolTensorboolr   rw   __classcell__rT   s   @r!   r<   r<      s    &&D5<< &D &DUX &D]b]i]i &DV 48).	-''$.- ))D0- #'	-
 
u||	-r    r<   c                   v     e Zd ZdZ fdZd Zdej                  dz  deej                  ee
   f   fdZ xZS )r@   z
    This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
    `hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer.
    c                    t         |           |j                  |j                  }}|j                  |j
                  }}t        |t        j                  j                        r|n||f}t        |t        j                  j                        r|n||f}|d   |d   z  |d   |d   z  z  }|| _        || _
        |d   |d   z  |d   |d   z  f| _        t        j                  ||||      | _        y )Nr   r   )kernel_sizestride)r>   r?   
image_sizerP   r6   rG   
isinstancecollectionsabcIterablerB   rC   r   Conv2d
projection)rR   rQ   r   rP   r6   hidden_sizerB   rT   s          r!   r?   z!DonutSwinPatchEmbeddings.__init__   s    !'!2!2F4E4EJ
$*$7$79I9Ik#-j+//:R:R#SZZdfpYq
#-j+//:R:R#SZZdfpYq
!!}
15*Q-:VW=:XY$&$Q-:a=8*Q-:VW=:XY))L+:^hir    c                 n   || j                   d   z  dk7  rDd| j                   d   || j                   d   z  z
  f}t        j                  j                  ||      }|| j                   d   z  dk7  rFddd| j                   d   || j                   d   z  z
  f}t        j                  j                  ||      }|S )z9Pad pixel_values to be divisible by patch_size if needed.r   r   )rP   r   rb   pad)rR   rl   r4   r5   
pad_valuess        r!   	maybe_padz"DonutSwinPatchEmbeddings.maybe_pad   s    4??1%%*T__Q/%$//!:L2LLMJ==,,\:FLDOOA&&!+Q4??1#5QRAS8S#STJ==,,\:FLr    rl   NrV   c                     |j                   \  }}}}| j                  |||      }| j                  |      }|j                   \  }}}}||f}|j                  d      j	                  dd      }||fS )Nr*   r   )r-   r   r   flattenr/   )rR   rl   rr   r6   r4   r5   rU   rs   s           r!   rw   z DonutSwinPatchEmbeddings.forward  s}    )5););&<~~lFEB__\2
(..1fe#UO''*44Q:
,,,r    )r   r   r   r   r?   r   r   r   r   ry   rz   rw   r}   r~   s   @r!   r@   r@      sF    j	-E$5$5$< 	-u||UZ[^U_G_A` 	-r    r@   c                        e Zd ZdZdeddf fdZdej                  dededej                  fd	Zdej                  d
e	eef   dej                  fdZ
 xZS )DonutSwinPatchMergingzd
    Patch Merging Layer.

    Args:
        dim (`int`):
            Number of input channels.
    r]   rV   Nc                     t         |           t        j                  d|z  d|z  d      | _        t        j
                  d|z        | _        y )N   r*   Fbias)r>   r?   r   Linear	reductionrK   rL   )rR   r]   rT   s     r!   r?   zDonutSwinPatchMerging.__init__  s>    1s7AG%@LLS)	r    r1   r4   r5   c           
      ~    |dz  dk(  s|dz  dk(  r,t         j                  j                  |ddd|dz  d|dz  f      }|S )zPPad input feature map to be divisible by 2 in both spatial dimensions if needed.r*   r   r   )r   rb   r   )rR   r1   r4   r5   s       r!   r   zDonutSwinPatchMerging.maybe_pad!  sL    QJ!OaMM--maAuqyRSU[^_U_=`aMr    input_dimensionsc                    |\  }}|j                   \  }}}|j                  ||||      }| j                  |||      }t        j                  t        d      D 	cg c]%  }t        d      D ]  }	|d d |	d d|d dd d f    ' c}	}d      }|j                  |dd|z        }| j                  |      }| j                  |      }|S c c}	}w )Nr*   r+   r\   r   )r-   r.   r   r   rd   rangerL   r   )
rR   r1   r   r4   r5   r3   r]   r6   colrows
             r!   rw   zDonutSwinPatchMerging.forward'  s    ((5(;(;%
C%**:vulS}feD		<A!HYSPUVWPXY]1cf1fcf1fa/0Y0Y_a
 &**:r1|;KL		-0}5 Zs   *C
)r   r   r   r   rz   r?   r   ry   r   r   rw   r}   r~   s   @r!   r   r     su    *C *D *
u|| S  QVQ]Q] U\\ U3PS8_ Y^YeYe r    r   c            
            e Zd Z fdZ	 	 d	dej
                  dej                  dz  dedz  deej
                     fdZ	d Z
 xZS )
DonutSwinSelfAttentionc                    t         |           ||z  dk7  rt        d| d| d      || _        t	        ||z        | _        | j                  | j
                  z  | _        t        |t        j                  j                        r|n||f| _        t        j                  t        j                  d| j                  d   z  dz
  d| j                  d   z  dz
  z  |            | _        | j#                  d| j%                                t        j&                  | j                  | j                  |j(                        | _        t        j&                  | j                  | j                  |j(                        | _        t        j&                  | j                  | j                  |j(                        | _        t        j0                  |j2                        | _        y )	Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()r*   r   relative_position_indexr   )r>   r?   
ValueErrornum_attention_headsrz   attention_head_sizeall_head_sizer   r   r   r   r2   r   rE   r   rF   relative_position_bias_tableregister_buffercreate_relative_position_indexr   qkv_biasquerykeyvaluerM   attention_probs_dropout_probrO   rR   rQ   r]   	num_headsr2   rT   s        r!   r?   zDonutSwinSelfAttention.__init__=  s   ?a#C5(^_h^iijk  $- #&sY#7 !558P8PP%k;??3K3KLKS^`kRl 	 -/LLKKT--a0014T=M=Ma=P9PST9TUW`a-
) 	68[8[8]^YYt1143E3EFOO\
99T//1C1C&//ZYYt1143E3EFOO\
zz&"E"EFr    Nr   attention_maskoutput_attentionsrV   c                    |j                   \  }}}||d| j                  f}| j                  |      j                  |      j	                  dd      }| j                  |      j                  |      j	                  dd      }	| j                  |      j                  |      j	                  dd      }
t        j                  ||	j	                  dd            }|t        j                  | j                        z  }| j                  | j                  j                  d         }|j                  | j                  d   | j                  d   z  | j                  d   | j                  d   z  d      }|j                  ddd      j                         }||j!                  d      z   }|r|j                   d   }|j                  ||z  || j"                  ||      }||j!                  d      j!                  d      z   }|j                  d| j"                  ||      }t$        j&                  j)                  |d      }| j+                  |      }t        j                  ||
      }|j                  dddd      j                         }|j-                         d d | j.                  fz   }|j                  |      }|r||f}|S |f}|S )Nr+   r   r*   r   r\   r   )r-   r   r   r.   r/   r   r   r   matmulmathsqrtr   r   r2   ra   r0   rp   r   r   rb   softmaxrO   rY   r   )rR   r   r   r   r3   r]   r6   hidden_shapequery_layer	key_layervalue_layerattention_scoresrelative_position_bias
mask_shapeattention_probscontext_layernew_context_layer_shapeoutputss                     r!   rw   zDonutSwinSelfAttention.forwardW  s    )6(;(;%
C"CT-E-EFjj/44\BLLQPQRHH]+00>HHAN	jj/44\BLLQPQR !<<Y5H5HR5PQ+dii8P8P.QQ!%!B!B4C_C_CdCdegCh!i!7!<!<Q$"2"21"55t7G7G7JTM]M]^_M`7`bd"
 "8!?!?1a!H!S!S!U+.D.N.Nq.QQ%'--a0J/44j(*d6N6NPSUX   0.2J2J12M2W2WXY2ZZ/44R9Q9QSVX[\ --//0@b/I ,,7_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2 O\M]r    c                    t        j                  | j                  d         }t        j                  | j                  d         }t        j                  t        j                  ||gd            }t        j
                  |d      }|d d d d d f   |d d d d d f   z
  }|j                  ddd      j                         }|d d d d dfxx   | j                  d   dz
  z  cc<   |d d d d dfxx   | j                  d   dz
  z  cc<   |d d d d dfxx   d| j                  d   z  dz
  z  cc<   |j                  d      }|S )Nr   r   ij)indexingr*   r+   )	r   aranger2   stackmeshgridr   ra   r0   sum)rR   coords_hcoords_wcoordscoords_flattenrelative_coordsr   s          r!   r   z5DonutSwinSelfAttention.create_relative_position_index  s-   << 0 0 34<< 0 0 34U^^Xx,@4PQvq1(At4~aqj7QQ)11!Q:EEG1a D$4$4Q$7!$;; 1a D$4$4Q$7!$;; 1a A(8(8(;$;a$?? "1"5"5b"9&&r    rx   )r   r   r   r?   r   ry   r   r|   r   rw   r   r}   r~   s   @r!   r   r   <  s^    G: 48).	1||1 ))D01  $;	1
 
u||	1f'r    r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )DonutSwinSelfOutputc                     t         |           t        j                  ||      | _        t        j
                  |j                        | _        y N)r>   r?   r   r   denserM   r   rO   rR   rQ   r]   rT   s      r!   r?   zDonutSwinSelfOutput.__init__  s6    YYsC(
zz&"E"EFr    r   input_tensorrV   c                 J    | j                  |      }| j                  |      }|S r   r   rO   )rR   r   r   s      r!   rw   zDonutSwinSelfOutput.forward  s$    

=1]3r    r   r   r   r?   r   ry   rw   r}   r~   s   @r!   r   r     s2    G
U\\  RWR^R^ r    r   c            
            e Zd Z fdZ	 	 ddej
                  dej                  dz  dedz  deej
                     fdZ	 xZ
S )	DonutSwinAttentionc                 j    t         |           t        ||||      | _        t	        ||      | _        y r   )r>   r?   r   rR   r   outputr   s        r!   r?   zDonutSwinAttention.__init__  s.    *63	;O	)&#6r    Nr   r   r   rV   c                 h    | j                  |||      }| j                  |d   |      }|f|dd  z   }|S )Nr   r   )rR   r   )rR   r   r   r   self_outputsattention_outputr   s          r!   rw   zDonutSwinAttention.forward  sE     yy@QR;;|AF#%QR(88r    rx   )r   r   r   r?   r   ry   r   r|   r   rw   r}   r~   s   @r!   r   r     sW    7 48).		||	 ))D0	  $;		
 
u||		r    r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )DonutSwinIntermediatec                    t         |           t        j                  |t	        |j
                  |z              | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y r   )r>   r?   r   r   rz   	mlp_ratior   r   
hidden_actstrr   intermediate_act_fnr   s      r!   r?   zDonutSwinIntermediate.__init__  sa    YYsC(8(83(>$?@
f''-'-f.?.?'@D$'-'8'8D$r    r   rV   c                 J    | j                  |      }| j                  |      }|S r   )r   r   rR   r   s     r!   rw   zDonutSwinIntermediate.forward  s&    

=100?r    r   r~   s   @r!   r   r     s#    9U\\ ell r    r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )DonutSwinOutputc                     t         |           t        j                  t	        |j
                  |z        |      | _        t        j                  |j                        | _	        y r   )
r>   r?   r   r   rz   r   r   rM   rN   rO   r   s      r!   r?   zDonutSwinOutput.__init__  sF    YYs6#3#3c#9:C@
zz&"<"<=r    r   rV   c                 J    | j                  |      }| j                  |      }|S r   r   r   s     r!   rw   zDonutSwinOutput.forward  s$    

=1]3r    r   r~   s   @r!   r   r     s#    >
U\\ ell r    r   c                   r     e Zd ZdZd	deddf fdZdej                  dej                  fdZde	fdZ
 xZS )
DonutDropPathzStochastic depth (DropPath) per sample, for residual blocks.

    Identity when ``drop_prob`` is 0 or outside training. See `Deep Networks with Stochastic Depth
    <https://arxiv.org/abs/1603.09382>`_.
    	drop_probrV   Nc                 0    t         |           || _        y r   )r>   r?   r   )rR   r   rT   s     r!   r?   zDonutDropPath.__init__  s    "r    r   c                 P   | j                   dk(  s| j                  s|S d| j                   z
  }|j                  d   fd|j                  dz
  z  z   }t	        j
                  ||j                  |j                        }t	        j                  ||z         }|j                  |      |z  S )N        r   r   )r   dtypedevice)
r   trainingr-   ndimr   randr   r   floordiv)rR   r   	keep_probr-   random_tensors        r!   rw   zDonutDropPath.forward  s    >>S   &	$$Q')DM4F4F4J,KK

50C0CML`L`aMI$=>  +m;;r    c                      d| j                    S )Nzp=)r   rR   s    r!   
extra_reprzDonutDropPath.extra_repr  s    DNN#$$r    )r   )r   r   r   r   floatr?   r   ry   rw   r   r  r}   r~   s   @r!   r   r     sB    #% #$ #<U\\ <ell <%C %r    r   c                       e Zd Zd fd	Zd Zdededej                  dej                  dej                  dz  f
d	Z
d
 Z	 	 ddej                  deeef   dedz  dedz  deej                  ej                  f   f
dZ xZS )DonutSwinLayerc                    t         |           |j                  | _        || _        |j                  | _        || _        t        j                  ||j                        | _	        t        |||| j                        | _        |dkD  rt        |      nt        j                         | _        t        j                  ||j                        | _        t!        ||      | _        t%        ||      | _        y )N)eps)r2   r   )r>   r?   chunk_size_feed_forward
shift_sizer2   input_resolutionr   rK   layer_norm_epslayernorm_beforer   	attentionr   Identity	drop_pathlayernorm_afterr   intermediater   r   )rR   rQ   r]   r	  r   drop_path_rater  rT   s          r!   r?   zDonutSwinLayer.__init__  s    '-'E'E$$!-- 0 "Sf6K6K L+FCPTP`P`a:H3:N~6TVT_T_Ta!||CV5J5JK1&#>%fc2r    c                    t        |      | j                  k  rgt        d      | _        t        j
                  j                         r(t	        j                   t	        j                  |            n
t        |      | _        y y Nr   )minr2   r   r  r   r^   r_   tensor)rR   r	  s     r!   set_shift_and_window_sizez(DonutSwinLayer.set_shift_and_window_size  s\     D$4$44'lDO=BYY=Q=Q=S		%,,'789Y\]mYn  5r    r4   r5   r   r   rV   Nc                    | j                   dk  ryt        j                  ||      }t        j                  ||      }||| j                  z
  k\  j	                         ||| j                   z
  k\  j	                         z   }||| j                  z
  k\  j	                         ||| j                   z
  k\  j	                         z   }|dddddf   dz  |dddddf   z   j                  |      }	t        |	| j                        }
|
j                  d| j                  | j                  z        }
|
j                  d      |
j                  d      z
  }|j                  |dk7  d      j                  |dk(  d	      }|S )
u  Build the cyclic-shift attention mask for shifted-window MSA; returns None when shift_size is 0.

        Each (h, w) position belongs to one of 9 cyclic-shift regions (3 along each axis), encoded
        as ``h_region * 3 + w_region``. Regions per axis:
        - 0: indices ``[0, axis - window_size)``
        - 1: indices ``[axis - window_size, axis - shift_size)``
        - 2: indices ``[axis - shift_size, axis)``
        Implementation note: a single arithmetic pass on `torch.arange` (two comparisons +
        broadcast add) replaces the original 9-iteration nested-Python-loop slice-assignment —
        fully vectorised, no per-cell host-side scatter, no GPU↔host sync.
        r   Nr   r   r+   r   r*   g      Yr   )
r  r   r   r2   longtor8   r.   rp   masked_fill)rR   r4   r5   r   r   h_idxw_idxh_regionw_regionimg_maskmask_windows	attn_masks               r!   get_attn_maskzDonutSwinLayer.get_attn_mask  si    ??aVF3U62Vd&6&666<<>%6TXTcTcKcBcAiAiAkkUT%5%555;;=%RVRaRaJaAa@g@g@iiT1dD01A5tQPTAT8UUYYZ_`'$2B2BC#((T-=-=@P@P-PQ **1-0F0Fq0II	)))q.&AMMi[\n^ab	r    c                     | j                   || j                   z  z
  | j                   z  }| j                   || j                   z  z
  | j                   z  }ddd|d|f}t        j                  j                  ||      }||fS r  )r2   r   rb   r   )rR   r   r4   r5   	pad_right
pad_bottomr   s          r!   r   zDonutSwinLayer.maybe_pad!  s    %%0@0@(@@DDTDTT	&&$2B2B)BBdFVFVV
Ay!Z8
))-Dj((r    r   r   r   always_partitionc                    |s| j                  |       n	 |\  }}|j                         \  }}}	|}
| j                  |      }|j                  ||||	      }| j	                  |||      \  }}|j
                  \  }}}}| j                  dkD  r1t        j                  || j                   | j                   fd      }n|}t        || j                        }|j                  d| j                  | j                  z  |	      }| j                  |||j                  |j                        }| j                  |||      }|d   }|j                  d| j                  | j                  |	      }t        || j                  ||      }| j                  dkD  r/t        j                  || j                  | j                  fd      }n|}|d   dkD  xs |d   dkD  }|r|d d d |d |d d f   j!                         }|j                  |||z  |	      }|
| j#                  |      z   }| j%                  |      }| j'                  |      }|| j)                  |      z   }|r	||d	   f}|S |f}|S )
Nr   )r   r*   )shiftsdimsr+   r   )r   r      r   )r  rY   r  r.   r   r-   r  r   rollr8   r2   r#  r   r   r  r:   r0   r  r  r  r   )rR   r   r   r   r'  r4   r5   r3   rr   channelsshortcutr   
height_pad	width_padshifted_hidden_stateshidden_states_windowsr"  attention_outputsr   attention_windowsshifted_windows
was_paddedlayer_outputlayer_outputss                           r!   rw   zDonutSwinLayer.forward(  s     **+;<("/"4"4"6
Ax --m<%**:vuhO %)NN=&%$P!z&3&9&9#:y!??Q$)JJ}tFVY]YhYhXhEipv$w!$1! !11FHXHX Y 5 : :2t?O?ORVRbRb?bdl m&&	)<)<EZEaEa ' 
	 !NN+@)_pNq,Q/,11"d6F6FHXHXZbc():D<L<LjZcd ??Q %

?DOOUYUdUdCelr s /]Q&;*Q-!*;
 1!WfWfufa2G H S S U-22:v~xX 4>>2C#DD++M:((6$t{{<'@@@Q'8';< YeWfr    )r   r   FF)r   r   r   r?   r  rz   r   r   r   ry   r#  r   r   r|   rw   r}   r~   s   @r!   r  r    s    3C  EKK QVQ]Q] bgbnbnqubu 2) */(->||>  S/>  $;	>
 +> 
u||U\\)	*>r    r  c                        e Zd Z fdZ	 	 d	dej
                  deeef   dedz  dedz  deej
                     f
dZ	 xZ
S )
DonutSwinStagec                 H   t         	|           || _        || _        t	        j
                  t        |      D cg c]-  }t        ||||||   |dz  dk(  rdn|j                  dz        / c}      | _	        | ||      | _
        d| _        y d | _
        d| _        y c c}w )Nr*   r   )rQ   r]   r	  r   r  r  r\   F)r>   r?   rQ   r]   r   
ModuleListr   r  r2   blocks
downsamplepointing)
rR   rQ   r]   r	  depthr   r  r?  irT   s
            r!   r?   zDonutSwinStage.__init__k  s    mm u
  !%5'#,Q<%&UaZqf6H6HA6M

 !(S1DO  #DO'
s   2Br   r   r   Nr'  rV   c                    |\  }}t        | j                        D ]  \  }} |||||      }	|	d   } |}
| j                  )|dz   dz  |dz   dz  }}||||f}| j                  |
|      }n||||f}||
|f}|r|	dd  z  }|S )Nr   r   r*   )	enumerater>  r?  )rR   r   r   r   r'  r4   r5   rB  layer_moduler8  !hidden_states_before_downsamplingheight_downsampledwidth_downsampledrs   stage_outputss                  r!   rw   zDonutSwinStage.forward  s     )(5 	-OA|(8HJ[]mnM)!,M	-
 -:)??&5;aZA4EPQ	VWGW 1!'0BDU V OO,MO_`M!' >&(IK\]]12..Mr    r9  )r   r   r   r?   r   ry   r   rz   r|   rw   r}   r~   s   @r!   r;  r;  j  sb    < */(-||  S/  $;	
 + 
u||	r    r;  c                        e Zd Z fdZ	 	 	 	 	 ddej
                  deeef   dedz  dedz  dedz  dedz  d	edz  d
ee	z  fdZ
 xZS )DonutSwinEncoderc                    t         |           t        |j                        | _        || _        t        j                  d|j                  t        |j                        d      D cg c]  }|j                          }}t        j                  t        | j                        D cg c]  }t        |t        |j                   d|z  z        |d   d|z  z  |d   d|z  z  f|j                  |   |j"                  |   |t        |j                  d |       t        |j                  d |dz           || j                  dz
  k  rt$        nd        c}      | _        d| _        y c c}w c c}w )Nr   cpur  r*   r   )rQ   r]   r	  rA  r   r  r?  F)r>   r?   lendepths
num_layersrQ   r   linspacer  r   itemr   r=  r   r;  rz   rG   r   r   layersgradient_checkpointing)rR   rQ   rC   xdpri_layerrT   s         r!   r?   zDonutSwinEncoder.__init__  sM   fmm,!&63H3H#fmmJ\ej!klAqvvxllmm  %T__5  !F,,q'z9:&/lq'z&BIaLUVX_U_D`%a --0$..w7!#fmmHW&=">V]]S`U\_`U`EaAbc9@4??UVCV9V4]a
 ',#! ms   )E&(B*E+r   r   r   Noutput_hidden_states(output_hidden_states_before_downsamplingr'  return_dictrV   c                    |rdnd }|rdnd }	|rdnd }
|rE|j                   \  }}} |j                  |g|| }|j                  dddd      }||fz  }|	|fz  }	t        | j                        D ]  \  }} |||||      }|d   }|d   }|d   }|d   |d   f}|rP|rN|j                   \  }}} |j                  |g|d   |d   f| }|j                  dddd      }||fz  }|	|fz  }	nI|rG|sE|j                   \  }}} |j                  |g|| }|j                  dddd      }||fz  }|	|fz  }	|s|
|dd  z  }
 |st        d |||
fD              S t        |||
|		      S )
Nr   r   r   r   r*   r   r+   c              3   &   K   | ]	  }||  y wr   r   ).0vs     r!   	<genexpr>z+DonutSwinEncoder.forward.<locals>.<genexpr>  s     mq_`_lms   )r   r   r   r   )r-   r.   ra   rD  rS  r   r   )rR   r   r   r   rX  rY  r'  rZ  all_hidden_statesall_reshaped_hidden_statesall_self_attentionsr3   rr   r   reshaped_hidden_staterB  rE  r8  rF  rs   s                       r!   rw   zDonutSwinEncoder.forward  s0    #7BD+?RT"$5b4)6)<)<&J;$6M$6$6z$bDT$bVa$b!$9$A$A!Q1$M!-!11&+@*BB&(5 	9OA|(8HJ[]mnM)!,M0=a0@- -a 0 1" 57H7LM#(P-N-T-T*
A{ )O(I(N(N)"3A"68I!8L!M)OZ)% )>(E(EaAq(Q%!&G%II!*/D.FF*%.V-:-@-@*
A{(:(:(::(fHX(fZe(f%(=(E(EaAq(Q%!m%55!*/D.FF* #}QR'88#9	9< m]4EGZ$[mmm%++*#=	
 	
r    )FFFFT)r   r   r   r?   r   ry   r   rz   r|   r   rw   r}   r~   s   @r!   rK  rK    s    ,4 */,1@E(-#'<
||<
  S/<
  $;	<

 #Tk<
 37+<
 +<
 D[<
 
'	'<
r    rK  c                   h     e Zd ZU eed<   dZdZdZdZdgZ	 e
j                          fd       Z xZS )DonutSwinPreTrainedModelrQ   donutrl   )imageTr;  c                    t         |   |       t        |t              rX|j                  t        j                  |j                         |j                   t        j                  |j                         yyt        |t              rNt        j                  |j                         t        j                  |j                  |j                                yy)zInitialize the weightsN)r>   _init_weightsr   r<   rH   initzeros_rJ   r   r   copy_r   r   )rR   modulerT   s     r!   ri  z&DonutSwinPreTrainedModel._init_weights  s     	f%f12  ,F--.))5F667 6 67KK;;<JJv55v7\7\7^_ 8r    )r   r   r   r   r   base_model_prefixmain_input_nameinput_modalitiessupports_gradient_checkpointing_no_split_modulesr   no_gradri  r}   r~   s   @r!   re  re    sG     $O!&*#)*U]]_
` 
`r    re  c                        e Zd Zd fd	Zd Ze	 	 	 	 	 	 ddej                  dz  dej                  dz  de	dz  de	dz  de	d	e	dz  d
e
ez  fd       Z xZS )DonutSwinModelc                    t         |   |       || _        t        |j                        | _        t        |j                  d| j
                  dz
  z  z        | _        t        ||      | _
        t        || j                  j                        | _        |rt        j                  d      nd| _        | j#                          y)z
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        use_mask_token (`bool`, *optional*, defaults to `False`):
            Whether to use a mask token for masked image modeling.
        r*   r   )rS   N)r>   r?   rQ   rN  rO  rP  rz   rG   num_featuresr<   rU   rK  rD   encoderr   AdaptiveAvgPool1dpooler	post_init)rR   rQ   add_pooling_layerrS   rT   s       r!   r?   zDonutSwinModel.__init__  s     	 fmm, 0 0119L3M MN-f^T'0J0JK1Bb**1- 	r    c                 .    | j                   j                  S r   )rU   rA   r   s    r!   get_input_embeddingsz#DonutSwinModel.get_input_embeddings&  s    ///r    Nrl   rm   r   rX  rk   rZ  rV   c                    ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }|t	        d      | j                  |||      \  }}	| j                  ||	|||      }
|
d   }d}| j                  7| j                  |j                  dd            }t        j                  |d      }|s||f|
dd z   }|S t        |||
j                  |
j                  |
j                        S )	z
        bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`):
            Boolean masked positions. Indicates which patches are masked (1) and which aren't (0).
        Nz You have to specify pixel_values)rm   rk   )r   rX  rZ  r   r   r*   )r   r$   r   r   r   )rQ   r   rX  rZ  r   rU   rx  rz  r/   r   r   r#   r   r   r   )rR   rl   rm   r   rX  rk   rZ  kwargsembedding_outputr   encoder_outputssequence_outputpooled_outputr   s                 r!   rw   zDonutSwinModel.forward)  s<    2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY?@@-1__/Tl .= .
** ,,/!5# ' 
 *!,;;" KK(A(A!Q(GHM!MM-;M%}58KKFM#-')77&11#2#I#I
 	
r    )TFNNNNFN)r   r   r   r?   r~  r   r   r   r{   r|   r   r#   rw   r}   r~   s   @r!   ru  ru    s    (0  2637)-,0).#'5
''$.5
 ))D05
  $;	5

 #Tk5
 #'5
 D[5
 
%	%5
 5
r    ru  a  
    DonutSwin Model transformer with an image classification head on top (a linear layer on top of the final hidden state of
    the [CLS] token) e.g. for ImageNet.

    <Tip>

        Note that it's possible to fine-tune DonutSwin on higher resolution images than the ones it has been trained on, by
        setting `interpolate_pos_encoding` to `True` in the forward of the model. This will interpolate the pre-trained
        position embeddings to the higher resolution.

    </Tip>
    c                        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dedz  d	e	e
z  fd
       Z xZS )DonutSwinForImageClassificationc                 >   t         |   |       |j                  | _        t        |      | _        |j                  dkD  r4t        j                  | j                  j                  |j                        nt        j                         | _	        | j                          y r  )r>   r?   
num_labelsru  rf  r   r   rw  r  
classifierr{  )rR   rQ   rT   s     r!   r?   z(DonutSwinForImageClassification.__init__r  sx      ++#F+
 FLEVEVYZEZBIIdjj--v/@/@A`b`k`k`m 	
 	r    Nrl   labelsr   rX  rk   rZ  rV   c                 V   ||n| j                   j                  }| j                  |||||      }|d   }	| j                  |	      }
d}|| j	                  ||
| j                         }|s|
f|dd z   }||f|z   S |S t        ||
|j                  |j                  |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).
        N)r   rX  rk   rZ  r   r*   )r'   r(   r   r   r   )	rQ   rZ  rf  r  loss_functionr&   r   r   r   )rR   rl   r  r   rX  rk   rZ  r  r   r  r(   r'   r   s                r!   rw   z'DonutSwinForImageClassification.forward  s    " &1%<k$++BYBY**/!5%=#  
  
/%%ffdkkBDY,F)-)9TGf$EvE-!//))#*#A#A
 	
r    r  )r   r   r   r?   r   r   r   
LongTensorr|   r   r&   rw   r}   r~   s   @r!   r  r  b  s       26*.)-,0).#',
''$.,
   4',
  $;	,

 #Tk,
 #',
 D[,
 
/	/,
 ,
r    r  )ru  re  r  )1r   collections.abcr   r   dataclassesr   r   r    r   rj  activationsr   modeling_layersr   modeling_utilsr	   utilsr
   r   r   r   configuration_donut_swinr   
get_loggerr   loggerr   r#   r&   r8   r:   Moduler<   r@   r   r   r   r   r   r   r   r  r;  rK  re  ru  r  __all__r   r    r!   <module>r     s  
   !   & ! 9 - D D 5 
		H	% 
 H[ H H  
 H; H H& 
 H[ H H,	Y-")) Y-z'-ryy '-V%BII %RZ'RYY Z'|
")) 
 &BII  	bii 	%BII %2uRYY ur4/ 4pS
ryy S
l ` ` `, N
- N
 N
b <
&> <
<
~ \r    