o
    i                     @   sl   d Z ddlZddlmZmZmZ ddlZG dd dZG dd deZG dd	 d	eZ	G d
d dee	Z
dS )zScorer interface module.    N)AnyListTuplec                
   @   sz   e Zd ZdZdejdefddZddeded	edefd
dZ	dejdedejde
ejef fddZdedefddZdS )ScorerInterfacea  Scorer interface for beam search.

    The scorer performs scoring of the all tokens in vocabulary.

    Examples:
        * Search heuristics
            * :class:`espnet.nets.scorers.length_bonus.LengthBonus`
        * Decoder networks of the sequence-to-sequence models
            * :class:`espnet.nets.pytorch_backend.nets.transformer.decoder.Decoder`
            * :class:`espnet.nets.pytorch_backend.nets.rnn.decoders.Decoder`
        * Neural language models
            * :class:`espnet.nets.pytorch_backend.lm.transformer.TransformerLM`
            * :class:`espnet.nets.pytorch_backend.lm.default.DefaultRNNLM`
            * :class:`espnet.nets.pytorch_backend.lm.seq_rnn.SequentialRNNLM`

    xreturnc                 C      dS )Get an initial state for decoding (optional).

        Args:
            x (torch.Tensor): The encoded feature tensor

        Returns: initial state

        N selfr   r
   r
   P/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/scorer_interface.py
init_state   s   	zScorerInterface.init_stateNstateinew_idc                 C   s   |du rdS || S )aC  Select state with relative ids in the main beam search.

        Args:
            state: Decoder state for prefix tokens
            i (int): Index to select a state in the main beam search
            new_id (int): New label index to select a state if necessary

        Returns:
            state: pruned state

        Nr
   )r   r   r   r   r
   r
   r   select_state&   s   zScorerInterface.select_stateyc                 C      t )a  Score new token (required).

        Args:
            y (torch.Tensor): 1D torch.int64 prefix tokens.
            state: Scorer state for prefix tokens
            x (torch.Tensor): The encoder feature that generates ys.

        Returns:
            tuple[torch.Tensor, Any]: Tuple of
                scores for next token that has a shape of `(n_vocab)`
                and next state for ys

        NotImplementedError)r   r   r   r   r
   r
   r   score4   s   zScorerInterface.scorec                 C   r   )zScore eos (optional).

        Args:
            state: Scorer state for prefix tokens

        Returns:
            float: final score

        g        r
   )r   r   r
   r
   r   final_scoreF   s   
zScorerInterface.final_score)N)__name__
__module____qualname____doc__torchTensorr   r   intr   r   r   floatr   r
   r
   r
   r   r   	   s    
r   c                   @   sT   e Zd ZdZdejdefddZdejdee dejde	ejee f fd	d
Z
dS )BatchScorerInterfacezBatch scorer interface.r   r   c                 C   s
   |  |S )r	   )r   r   r
   r
   r   batch_init_stateV   s   
	z%BatchScorerInterface.batch_init_stateysstatesxsc                 C   s   t d| jj t }t }tt|||D ]\}\}}}	| |||	\}
}|	| |	|
 qt
|d|jd d}||fS )a  Score new token batch (required).

        Args:
            ys (torch.Tensor): torch.int64 prefix tokens (n_batch, ylen).
            states (List[Any]): Scorer states for prefix tokens.
            xs (torch.Tensor):
                The encoder feature that generates ys (n_batch, xlen, n_feat).

        Returns:
            tuple[torch.Tensor, List[Any]]: Tuple of
                batchfied scores for next token with shape of `(n_batch, n_vocab)`
                and next state list for ys.

        z?{} batch score is implemented through for loop not parallelizedr   )warningswarnformat	__class__r   list	enumeratezipr   appendr   catviewshape)r   r#   r$   r%   scores	outstatesr   r   r   r   r   outstater
   r
   r   batch_scorea   s   
z BatchScorerInterface.batch_scoreN)r   r   r   r   r   r   r   r"   r   r   r5   r
   r
   r
   r   r!   S   s    r!   c                   @   s>   e Zd ZdZdejdejdedejdeejef f
ddZd	S )
PartialScorerInterfacea  Partial scorer interface for beam search.

    The partial scorer performs scoring when non-partial scorer finished scoring,
    and receives pre-pruned next tokens to score because it is too heavy to score
    all the tokens.

    Examples:
         * Prefix search for connectionist-temporal-classification models
             * :class:`espnet.nets.scorers.ctc.CTCPrefixScorer`

    r   next_tokensr   r   r   c                 C   r   )a  Score new token (required).

        Args:
            y (torch.Tensor): 1D prefix token
            next_tokens (torch.Tensor): torch.int64 next token to score
            state: decoder state for prefix tokens
            x (torch.Tensor): The encoder feature that generates ys

        Returns:
            tuple[torch.Tensor, Any]:
                Tuple of a score tensor for y that has a shape `(len(next_tokens),)`
                and next state for ys

        r   )r   r   r7   r   r   r
   r
   r   score_partial   s   z$PartialScorerInterface.score_partialN)	r   r   r   r   r   r   r   r   r8   r
   r
   r
   r   r6      s    r6   c                   @   sB   e Zd ZdZdejdejdee dejdeejef f
ddZ	d	S )
BatchPartialScorerInterfacez/Batch partial scorer interface for beam search.r#   r7   r$   r%   r   c                 C   r   )a8  Score new token (required).

        Args:
            ys (torch.Tensor): torch.int64 prefix tokens (n_batch, ylen).
            next_tokens (torch.Tensor): torch.int64 tokens to score (n_batch, n_token).
            states (List[Any]): Scorer states for prefix tokens.
            xs (torch.Tensor):
                The encoder feature that generates ys (n_batch, xlen, n_feat).

        Returns:
            tuple[torch.Tensor, Any]:
                Tuple of a score tensor for ys that has a shape `(n_batch, n_vocab)`
                and next states for ys
        r   )r   r#   r7   r$   r%   r
   r
   r   batch_score_partial   s   z/BatchPartialScorerInterface.batch_score_partialN)
r   r   r   r   r   r   r   r   r   r:   r
   r
   r
   r   r9      s    r9   )r   r'   typingr   r   r   r   r   r!   r6   r9   r
   r
   r
   r   <module>   s    J.!