o
    Qe                     @   sv   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
 ddlmZmZ ddgZ			
dddZG dd deZd
S )   )Layer)core_non_static_modein_dygraph_mode)LayerHelper)check_variable_and_dtype
check_type    )_C_ops_legacy_C_opsviterbi_decodeViterbiDecoderTNc           	      C   s   t  rt| |||S t rt| ||d|S t| dddgd t|dddgd t|ddd t|d	td tdi t	 }d|i}|
| j}|
d}|jd| ||d
||d|d ||fS )a  
    Decode the highest scoring sequence of tags computed by transitions and potentials and get the viterbi path.
 
    Args:
        potentials (Tensor): The input tensor of unary emission. This is a 3-D
            tensor with shape of [batch_size, sequence_length, num_tags]. The data type is float32 or float64. 
        transition_params (Tensor): The input tensor of transition matrix. This is a 2-D
            tensor with shape of [num_tags, num_tags]. The data type is float32 or float64. 
        lengths (Tensor):  The input tensor of length of each sequence. This is a 1-D tensor with shape of [batch_size]. The data type is int64. 
        include_bos_eos_tag (`bool`, optional): If set to True, the last row and the last column of transitions will be considered
            as start tag, the second to last row and the second to last column of transitions will be considered as stop tag. Defaults to ``True``.
        name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please
            refer to :ref:`api_guide_Name`.

    Returns:
        scores(Tensor): The output tensor containing the score for the Viterbi sequence. The shape is [batch_size]
            and the data type is float32 or float64.
        paths(Tensor): The output tensor containing the highest scoring tag indices.  The shape is [batch_size, sequence_length]
            and  the data type is int64.

    Example:
        .. code-block:: python

            import paddle
            paddle.seed(102)
            batch_size, seq_len, num_tags = 2, 4, 3
            emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
            length = paddle.randint(1, seq_len + 1, [batch_size])
            tags = paddle.randint(0, num_tags, [batch_size, seq_len])
            transition = paddle.rand((num_tags, num_tags), dtype='float32')
            scores, path = paddle.text.viterbi_decode(emission, transition, length, False) # scores: [3.37089300, 1.56825531], path: [[1, 0, 0], [1, 1, 0]]
    include_bos_eos_taginputZfloat32Zfloat64r   transitionslengthZint64Zinclude_tag)ZInputZ
TransitionZLength)ZScoresPath)typeZinputsZoutputsattrsN)r   )r   r
   r   r   r   r   r   boolr   localsZ"create_variable_for_type_inferenceZdtypeZ	append_op)	
potentialsZtransition_paramslengthsr   namehelperr   Zscorespath r   JD:\Projects\ConvertPro\env\Lib\site-packages\paddle/text/viterbi_decode.pyr      s@   %

c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	r   a&   
    Decode the highest scoring sequence of tags computed by transitions and potentials and get the viterbi path. 

    Args:
        transitions (`Tensor`): The transition matrix.  Its dtype is float32 and has a shape of `[num_tags, num_tags]`.
        include_bos_eos_tag (`bool`, optional): If set to True, the last row and the last column of transitions will be considered
            as start tag, the second to last row and the second to last column of transitions will be considered as stop tag. Defaults to ``True``.
        name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please
            refer to :ref:`api_guide_Name`.

    Shape:
        potentials (Tensor): The input tensor of unary emission. This is a 3-D tensor with shape of 
            [batch_size, sequence_length, num_tags]. The data type is float32 or float64. 
        lengths (Tensor):  The input tensor of length of each sequence. This is a 1-D tensor with shape of
            [batch_size]. The data type is int64. 

    Returns:
        scores(Tensor): The output tensor containing the score for the Viterbi sequence. The shape is [batch_size]
            and the data type is float32 or float64.
        paths(Tensor): The output tensor containing the highest scoring tag indices.  The shape is [batch_size, sequence_length]
            and the data type is int64.

    Example:
        .. code-block:: python

            import paddle
            paddle.seed(102)
            batch_size, seq_len, num_tags = 2, 4, 3
            emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
            length = paddle.randint(1, seq_len + 1, [batch_size])
            tags = paddle.randint(0, num_tags, [batch_size, seq_len])
            transition = paddle.rand((num_tags, num_tags), dtype='float32')
            decoder = paddle.text.ViterbiDecoder(transition, include_bos_eos_tag=False)
            scores, path = decoder(emission, length) # scores: [3.37089300, 1.56825531], path: [[1, 0, 0], [1, 1, 0]]
    TNc                    s$   t t|   || _|| _|| _d S N)superr   __init__r   r   r   )selfr   r   r   	__class__r   r   r       s   
zViterbiDecoder.__init__c                 C   s   t || j|| j| jS r   )r   r   r   r   )r!   r   r   r   r   r   forward   s   
zViterbiDecoder.forwardTN)__name__
__module____qualname____doc__r    r$   __classcell__r   r   r"   r   r   ]   s    $r%   )nnr   Zfluid.frameworkr   r   r   Zfluid.layer_helperr   Zfluid.data_feederr   r   Zpaddler
   r   __all__r   r   r   r   r   r   <module>   s   
E