o
    h                     @   s   d dl mZ d dlZd dlm  m  mZ 	 zedgZ	edgZ
eje	e
ddZdd ZW n ey>   d	d ZY nw G d
d deZdS )    )divisionNg      ?g       @truncrounding_modec                 C   s   t j| |ddS )Nr   r   )torchdivab r   T/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/beam.pytrunc_division"      r   c                 C   s   | | S Nr   r   r   r   r   r   %   s   c                   @   sH   e Zd ZdddZdd Zdd Zddd	Zd
d Zdd Zdd Z	dS )BeamNc                 C   sd   || _ d| _tj|tj|d| _g | _g | _tj|tj|d	t
jg| _t
j| jd d< g | _d S )NF)dtypedevicer   )sizedoner   zerosfloat32scores	allScoresprevKsint64fill_constantPAD_IDnextYsSOS_IDcopy)selfr   r   r   r   r   __init__)   s   
zBeam.__init__c                 C   
   | j d S )z)Get the outputs for the current timestep.)r   r!   r   r   r   get_current_state;      
zBeam.get_current_statec                 C   r#   )z.Get the backpointers for the current timestep.r$   )r   r%   r   r   r   get_current_origin?   r'   zBeam.get_current_originc           	      C   s   | j rdS |d}t| jdkr|| jd| }n|d }|d}|| jddd\}}| j	
| j || _t||}| j
| | j
|||   |dur^| j
|d| | jd d tjkrrd| _ | j	
| j | j S )aN  
        Given prob over words for every last beam `wordLk` and attention
        `attnOut`: Compute and update the beam search.

        Parameters:

        * `wordLk`- probs of advancing from the last step (K x words)
        * `copy_indices` - copy indices (K x ctx_len)

        Returns: True if beam search is complete.
        T   r   r$   N)r   r   lenr   r   	unsqueeze	expand_asviewtopkr   appendr   r   r    index_selectr   EOS_ID)	r!   wordLkcopy_indicesnumWordsbeamLk
flatBeamLk
bestScoresbestScoresIdprevKr   r   r   advanceC   s&   


zBeam.advancec                 C   s   t | jddS )Nr   T)r   sortr   r%   r   r   r   	sort_bestq   r   zBeam.sort_bestc                 C   s   |   \}}|d |d fS )z&Get the score of the best in the beam.r)   )sortBest)r!   r   idsr   r   r   get_bestt   s   zBeam.get_bestc                 C   s   g }g }t t| jd ddD ]&}|| j|d  |  t| jdkr.|| j| |  | j| | }q|ddd }|ddd }t|D ]\}}|dkrW|d  ||< qH|S )z
        Walk back to construct the full hypothesis.

        Parameters:

             * `k` - the position in the beam to construct.

         Returns: The hypothesis
        r)   r$   r   N)ranger*   r   r/   r   r    	enumerate)r!   khypcpyjicidxr   r   r   get_hypy   s   
zBeam.get_hypr   )
__name__
__module____qualname__r"   r&   r(   r:   r<   r?   rH   r   r   r   r   r   (   s    

.r   )
__future__r   r   %stanza.models.common.seq2seq_constantmodelscommonseq2seq_constantr   tensorr	   r
   r   cr   	TypeErrorobjectr   r   r   r   r   <module>   s    