
    9ib              
          S SK JrJrJrJr  S SKrS SKJs  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  S SKJr  S S	KJrJrJrJr  S S
KJr  \R6                  " \R8                  \R:                  S9\R6                  " \R<                  \R:                  S9\R6                  " \R>                  \R:                  S9\R6                  " \R@                  \R:                  S9 " S S\5      5       5       5       5       r!\R6                  " \RD                  \RD                  S9\R6                  " \R8                  \RD                  S9\R6                  " \R<                  \RD                  S9\R6                  " \R>                  \RD                  S9\R6                  " \R@                  \RD                  S9 " S S\5      5       5       5       5       5       r# " S S\RH                  5      r%g)    )AnyDictListOptionalN)nn)CrossEntropyLoss)ACT2FN)Heads)	TorchHead)HEADS)'AttentionTokenClassificationModelOutputModelOutputBase
OutputKeysTokenClassificationModelOutput)Tasks)module_namec                   L   ^  \ rS rSrSU 4S jjr    SS\4S jjrS rSrU =r	$ )	LSTMCRFHead#   c                    > [         TU ]  XS9  Uc   e[        R                  " US-  U5      U l        [        USS9U l        g )Nhidden_size
num_labels   Tbatch_first)super__init__r   LinearffnCRFcrfselfr   r   kwargs	__class__s       d/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/nlp/heads/crf_head.pyr   LSTMCRFHead.__init__*   sC    [H%%%99[1_j9zt4    inputsc                 L    U R                  UR                  5      n[        S US9$ )N)losslogits)r    last_hidden_stater   )r$   r*   attention_masklabel
label_maskoffset_mappingr%   r-   s           r'   forwardLSTMCRFHead.forward0   s+     &223-
 	
r)   c                    UR                  S5      R                  5       n[        R                  " UR                  S   UR
                  S9S S S 24   US S 2S 4   :  nU R                  R                  X5      R                  S5      nU$ N   )devicer   	sumlongtorcharangeshaper9   r"   decodesqueezer$   r-   r1   seq_lensmaskpredictss         r'   r@   LSTMCRFHead.decode>   ~    >>"%**,||Q??$$(!G-/74/@A 88??6088;r)   )r"   r    )d   NNNNN
__name__
__module____qualname____firstlineno__r   r   r3   r@   __static_attributes____classcell__r&   s   @r'   r   r   #   s-    5  $#
'
 r)   r   c                   H   ^  \ rS rSrU 4S jr    SS\4S jjrS rSrU =r	$ )TransformersCRFHeadG   c                 ~   > [         TU ]  " SXS.UD6  [        R                  " X5      U l        [        USS9U l        g )Nr   Tr    )r   r   r   r   linearr!   r"   r#   s       r'   r   TransformersCRFHead.__init__R   sA     	F#	F>D	Fii8zt4r)   r*   c                    U R                  UR                  5      nUb  UnUR                  S5      R                  5       n	[        R
                  " U5      n
[        UR                  S   5       HE  nX{   R                  X   R                  S5      5      R                  X   S5      U
US X   2S S 24'   MG     U
n[        S UUR                  UR                  S9$ )Nr7   r   )r,   r-   hidden_states
attentions)rW   r.   r;   r<   r=   
zeros_likeranger?   masked_select	unsqueezeviewr   rZ   r[   )r$   r*   r/   r0   r1   r2   r%   r-   rD   masked_lengthsmasked_logitsis               r'   r3   TransformersCRFHead.forwardX   s     V556!D!XXb\..0N!,,V4M4::a=)06	0G0G))"-1//3tN4Er/J )))1,. * #F6 ..((	
 	
r)   c                    UR                  S5      R                  5       n[        R                  " UR                  S   UR
                  S9S S S 24   US S 2S 4   :  nU R                  R                  X5      R                  S5      nU$ r6   r:   rB   s         r'   r@   TransformersCRFHead.decodeq   rG   r)   )r"   rW   rI   rJ   rQ   s   @r'   rS   rS   G   s-    5  $#
'
2 r)   rS   c                   ,  ^  \ rS rSrSrSS\S\SS4U 4S jjjrSS jrS\	4S	 jr
  SS
\R                  S\R                  S\\R                     S\	S\R                  4
S jjr   SS
\R                  S\\R                     S\\   S\\   S\\\\         4
S jjr  SS
\R                  S\\R                     S\\R                     SS4S jjrS
\R                  S\R                  S\R                  S\R                  4S jrS
\R                  S\R                  S\R                  4S jr SS
\R,                  S\R                  S\\   S\\\      4S jjr SS
\R,                  S\R                  S\S\\   S\\\\         4
S jjrSrU =r$ )r!   z   u^  Conditional random field.
This module implements a conditional random field [LMP01]_. The forward computation
of this class computes the log likelihood of the given sequence of tags and
emission score tensor. This class also has `~CRF.decode` method which finds
the best tag sequence given an emission score tensor using `Viterbi algorithm`_.
Args:
    num_tags: Number of tags.
    batch_first: Whether the first dimension corresponds to the size of a minibatch.
Attributes:
    start_transitions (`~torch.nn.Parameter`): Start transition score tensor of size
        ``(num_tags,)``.
    end_transitions (`~torch.nn.Parameter`): End transition score tensor of size
        ``(num_tags,)``.
    transitions (`~torch.nn.Parameter`): Transition score tensor of size
        ``(num_tags, num_tags)``.
.. [LMP01] Lafferty, J., McCallum, A., Pereira, F. (2001).
   "Conditional random fields: Probabilistic models for segmenting and
   labeling sequence data". *Proc. 18th International Conf. on Machine
   Learning*. Morgan Kaufmann. pp. 282–289.
.. _Viterbi algorithm: https://en.wikipedia.org/wiki/Viterbi_algorithm

num_tagsr   returnNc                   > US::  a  [        SU 35      e[        TU ]	  5         Xl        X l        [
        R                  " [        R                  " U5      5      U l	        [
        R                  " [        R                  " U5      5      U l
        [
        R                  " [        R                  " X5      5      U l        U R                  5         g )Nr   zinvalid number of tags: )
ValueErrorr   r   ri   r   r   	Parameterr=   emptystart_transitionsend_transitionstransitionsreset_parameters)r$   ri   r   r&   s      r'   r   CRF.__init__   s    q=7zBCC &!#ekk(.C!D!||EKK,AB<<H(GHr)   c                    [         R                  R                  U R                  SS5        [         R                  R                  U R                  SS5        [         R                  R                  U R
                  SS5        g)zInitialize the transition parameters.
The parameters will be initialized randomly from a uniform distribution
between -0.1 and 0.1.
gg?N)r   inituniform_ro   rp   rq   r$   s    r'   rr   CRF.reset_parameters   sX    
 	//s;
--tS9
))45r)   c                 N    U R                   R                   SU R                   S3$ )Nz
(num_tags=))r&   rK   ri   rw   s    r'   __repr__CRF.__repr__   s$    ..))**T]]O1EEr)   	emissionstagsrD   	reductionc                    US;  a  [        SU 35      eUc.  [        R                  " U[        R                  UR                  S9nUR
                  [        R                  :w  a  UR                  5       nU R                  XUS9  U R                  (       a6  UR                  SS5      nUR                  SS5      nUR                  SS5      nU R                  XU5      nU R                  X5      nXV-
  nUS:X  a  U$ US:X  a  UR                  5       $ US	:X  a  UR                  5       $ UR                  5       UR                  5       R                  5       -  $ )
a?  Compute the conditional log likelihood of a sequence of tags given emission scores.
Args:
    emissions (`~torch.Tensor`): Emission score tensor of size
        ``(seq_length, batch_size, num_tags)`` if ``batch_first`` is ``False``,
        ``(batch_size, seq_length, num_tags)`` otherwise.
    tags (`~torch.LongTensor`): Sequence of tags tensor of size
        ``(seq_length, batch_size)`` if ``batch_first`` is ``False``,
        ``(batch_size, seq_length)`` otherwise.
    mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
        if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
    reduction: Specifies  the reduction to apply to the output:
        ``none|sum|mean|token_mean``. ``none``: no reduction will be applied.
        ``sum``: the output will be summed over batches. ``mean``: the output will be
        averaged over batches. ``token_mean``: the output will be averaged over tokens.
Returns:
    `~torch.Tensor`: The log likelihood. This will have size ``(batch_size,)`` if
    reduction is ``none``, ``()`` otherwise.
)noner;   mean
token_meanzinvalid reduction: dtyper9   )r~   rD   r   r8   r   r;   r   )rl   r=   	ones_likeuint8r9   r   byte	_validater   	transpose_compute_score_compute_normalizerr;   r   float)r$   r}   r~   rD   r   	numeratordenominatorllhs           r'   r3   CRF.forward   s&   . AA29+>??<??4u{{4;;OD::$99;Dy$7!++Aq1I>>!Q'D>>!Q'D ''	>	..y?%J77988:wwy4::<++---r)   nbestpad_tagc                    Uc  SnUc;  [         R                  " UR                  SS [         R                  UR                  S9nUR
                  [         R                  :w  a  UR                  5       nU R                  XS9  U R                  (       a$  UR                  SS5      nUR                  SS5      nUS:X  a!  U R                  XU5      R                  S5      $ U R                  XX45      $ )aP  Find the most likely tag sequence using Viterbi algorithm.
Args:
    emissions (`~torch.Tensor`): Emission score tensor of size
        ``(seq_length, batch_size, num_tags)`` if ``batch_first`` is ``False``,
        ``(batch_size, seq_length, num_tags)`` otherwise.
    mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
        if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
    nbest (`int`): Number of most probable paths for each sequence
    pad_tag (`int`): Tag at padded positions. Often input varies in length and
        the length will be padded to the maximum length in the batch. Tags at
        the padded positions will be assigned with a padding tag, i.e. `pad_tag`
Returns:
    A PyTorch tensor of the best tag sequence for each batch of shape
    (nbest, batch_size, seq_length)
Nr8   r   r   )rD   r   )r=   onesr?   r   r9   r   r   r   r   r   _viterbi_decoder_   _viterbi_decode_nbest)r$   r}   rD   r   r   s        r'   r@   
CRF.decode   s    ( =E<::#kk '')D ::$99;Dy,!++Aq1I>>!Q'DA:''	AKKANN)))5JJr)   c                 >   UR                  5       S:w  a  [        SUR                  5        35      eUR                  S5      U R                  :w  a*  [        SU R                   SUR                  S5       35      eUbW  UR                  S S UR                  :w  a:  [        S[        UR                  S S 5       S[        UR                  5       35      eUb  UR                  S S UR                  :w  a:  [        S[        UR                  S S 5       S[        UR                  5       35      eU R                  (       + =(       a    US	   R                  5       nU R                  =(       a    US S 2S	4   R                  5       nU(       d  U(       d  [        S
5      eg g g )N   z(emissions must have dimension of 3, got r   z(expected last dimension of emissions is z, got z?the first two dimensions of emissions and tags must match, got z and z?the first two dimensions of emissions and mask must match, got r   z)mask of the first timestep must all be on)dimrl   sizeri   r?   tupler   all)r$   r}   r~   rD   no_empty_seqno_empty_seq_bfs         r'   r   CRF._validate  s    ==?a:9==?:KLN N>>!-:4==/ J ~~a()+, , r"djj0  !!456eE$**<M;NP 
 r"djj0  !!456eE$**<M;NP   $///ADGKKML"..C41:>>3CO !LMM )8< r)   c                    UR                   u  pEUR                  5       nU R                  US      nXaS[        R                  " U5      US   4   -  n[        SU5       HG  nX`R                  X'S-
     X'   4   X7   -  -  nXaU[        R                  " U5      X'   4   X7   -  -  nMI     UR                  5       R                  SS9S-
  nX([        R                  " U5      4   n	X`R                  U	   -  nU$ )Nr   r8   r   )
r?   r   ro   r=   r>   r]   rq   r<   r;   rp   )
r$   r}   r~   rD   
seq_length
batch_sizescorerc   seq_ends	last_tagss
             r'   r   CRF._compute_score"  s    
 "&
zz| &&tAw/1ell:6Q?@@q*%A %%dq5k47&:;dgEEE q%,,z":DGCDtwNNE & 99;??q?)A-5<<
#;;<	%%i00r)   c                    UR                  S5      nU R                  US   -   n[        SU5       Hu  nUR                  S5      nX   R                  S5      nX`R                  -   U-   n[
        R                  " USS9n[
        R                  " X%   R                  S5      X5      nMw     X@R                  -  n[
        R                  " USS9$ )Nr   r8   r   r   )	r   ro   r]   r_   rq   r=   	logsumexpwhererp   )	r$   r}   rD   r   r   rc   broadcast_scorebroadcast_emissions
next_scores	            r'   r   CRF._compute_normalizerB  s     ^^A&
 &&15q*%A $ooa0O #,,"8"8"; )+;+;;>QQJ ;J KK 1 1! 4jHE1 &8 	%%% u!,,r)   c           	         Uc  SnUR                   nUR                  u  pVU R                  US   -   n[        R                  " XVU R
                  4[        R                  US9n[        R                  " X`R
                  4[        R                  US9n	[        R                  " XV4U[        R                  US9n
[        SU5       H  nUR                  S5      nX   R                  S5      nXR                  -   U-   nUR                  SS9u  p[        R                  " X+   R                  S5      R                  5       UU5      n[        R                  " X+   R                  S5      R                  5       UU	5      nXUS-
  '   M     XpR                  -   nUR                  SS9u  nnUR                  5       R                  SS9S-
  nUR!                  SS5      R#                  5       nUR%                  SUR'                  SSS5      R)                  SSU R
                  5      UR'                  SSS5      R)                  SSU R
                  5      5        UR!                  SS5      R#                  5       n[        R                  " XV4[        R                  US9n[        R                  " US[        R                  US9n[        US-
  SS5       H<  n[        R*                  " UU   SU5      nUR,                  R'                  U5      UU'   M>     [        R                  " UR                  5       UU
5      R!                  SS5      $ )Nr   r   r8   r   r   r7   )r9   r?   ro   r=   zerosri   r<   fullr]   r_   rq   maxr   boolrp   r;   r   
contiguousscatter_r`   expandgatherdata)r$   r}   rD   r   r9   r   r   r   history_idxoor_idxoor_tagrc   r   broadcast_emissionr   indices	end_score_end_tagr   best_tags_arr	best_tagsidxs                          r'   r   CRF._viterbi_decodep  s    ?G!!!%
 &&15kk:4=="I(-

)/1 ++z==9$)JJ%+- **j5$#(::$*, q*%A $ooa0O "+!7!7!: )+;+;;>PPJ #-..Q."7J
 KK 1 1" 5 : : <j %'Ekk$'"3"3B"7"<"<">")+G!(A5 &< 000	]]q])
7 99;??q?)A- "++Aq1<<>MM"a#**2q$--@LLQ"))"a?	A "++Aq1<<> Z$<*/**+13 KK
AUZZO	aR0C[%5q)DI!*!4!4Z!@M# 1 {{499;w?II!QOOr)   c           
      	   Uc  SnUR                   nUR                  u  pgU R                  US   -   n[        R                  " XgU R
                  U4[        R                  US9n	[        R                  " XpR
                  U4[        R                  US9n
[        R                  " XgU4U[        R                  US9n[        SU5       GH  nUS:X  a6  UR                  S5      nX   R                  S5      nXR                  -   U-   nOSUR                  S5      nX   R                  S5      R                  S5      nXR                  R                  S5      -   U-   nUR                  USU R
                  5      R                  USS9u  nnUS:X  a'  UR                  S5      R                  SSU5      nUU-  nUR                  SS5      nUR                  SS5      n[        R                  " X,   R                  S5      R!                  5       R                  S5      X5      n[        R                  " X,   R                  S5      R                  S5      R!                  5       UU
5      nUXS-
  '   GM     XR"                  R                  S5      -   nUR                  US5      R                  USS9u  nnUR                  5       R%                  SS9S-
  nU	R                  SS5      R'                  5       n	U	R)                  SUR                  SSSS5      R                  SSU R
                  U5      UR                  SSSU5      R                  SSU R
                  U5      5        U	R                  SS5      R'                  5       n	[        R                  " XgU4[        R                  US9n[        R*                  " U[        R                  US9R                  SS5      R                  US5      n[        US-
  SS5       HP  n[        R,                  " U	U   R                  US5      SU5      nUR.                  R                  US5      U-  UU'   MR     [        R                  " UR                  S5      UU5      R1                  SSS5      $ )Nr   r   r8   r7   r   r   )r9   r?   ro   r=   r   ri   r<   r   r]   r_   rq   r`   topkr   r   r   r   rp   r;   r   r   r>   r   r   permute)r$   r}   rD   r   r   r9   r   r   r   r   r   r   rc   r   r   r   r   r   r   r   r   r   r   r   s                           r'   r   CRF._viterbi_decode_nbest  s,    ?G!!!%
 &&15kkT]]E:** ++z==%@$)JJ%+- **je<$#(::$*, q*%AAv"'//""5%.\%;%;A%>",/?/??BTT
"'//""5%.\%;%;A%>%H%H%K",/?/?/I/I0 +,

 #-//*b26--#AAE6; BF BD  J Av+222r5A!E/ $--a3J''1-G
 KK 1 1" 5 : : < F Fr J *3Ekk$'"3"3B"7"A"A""E"J"J"L")74G!(KAE &J 00::2>>	^^J388A8F
7 99;??q?)A- "++Aq1<<>MM"aA&--b!T]]EJLLQ5)00QuM	O "++Aq1<<> ZU$C*/**+13 LLejjH$q"+ffZ&< 	aR0C[%5%:%::r%JA%.0I!*!4!4Z!D!MM# 1
 {{4>>"-}"$$+GAq!$4	5r)   )r   rp   ri   ro   rq   )F)rj   N)Nr   )NNN)NN)N)rK   rL   rM   rN   __doc__intr   r   rr   strr{   r=   Tensor
LongTensorr   
ByteTensorr3   r   r@   r   r   r   FloatTensorr   r   rO   rP   rQ   s   @r'   r!   r!   z   s`   .
  
 4 
 D 
  
 6F# F 48!'	1. <<1.&&1. u//01. 	1. -2LL	1.j 37&*(,	%K,,%Ke../%K sm%K !	%K 26d49o1F	%KR 6:59N"\\N !1!12N !!1!12N ?CN< E<L<L "--27,,@,-U\\ ,-"'"2"2,-7<||,-b 26ZP#(#4#4ZP#..ZP "*#ZP ;?tCy/ZPB &*g5((g5 ""g5 	g5
 c]g5
 /34S	?.Cg5 g5r)   r!   )&typingr   r   r   r   r=   torch.nn.functionalr   
functionalFtorch.nnr   transformers.activationsr	   modelscope.metainfor
   modelscope.models.baser   modelscope.models.builderr   modelscope.outputsr   r   r   r   modelscope.utils.constantr   register_moduletoken_classificationlstm_crfnamed_entity_recognitionword_segmentationpart_of_speechr   transformer_crfrS   Moduler!   rV   r)   r'   <module>r      s  $ - ,     % + % , +@ @ , u11u~~N	""@u..ENNKu++H)  I L@ O
> 	u'<'<>	E,A,AC	""0E0EG	)>)>@u++9N9NO') ' P@GC>'Ty5")) y5r)   