o
    h                     @   s   d Z ddlmZ ddlZddlZddlZddlm  m  m	Z
 dd Zdd Zdd	 Zd
d ZdddZdddZdd Zdd Zdd Zdd ZdddZdd ZdS )z
Utils for seq2seq models.
    )CounterNc                 C   sf   | dkrt jj||dS | dkrt jj||dS | dkr"t j|S | dkr,t j|S td| )Nsgd)lradagradadamadamaxzUnsupported optimizer: {})torchoptimSGDAdagradAdamAdamax	Exceptionformat)name
parametersr    r   ]/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/seq2seq_utils.pyget_optimizer   s   r   c                 C   s   | j D ]}||d< qd S )Nr   )param_groups)	optimizernew_lrparam_groupr   r   r   	change_lr   s   

r   c                 C   s:   g }t | D ]\}}t|D ]}||| |  qq|S N)	enumeraterangeappend)seq_lenswidthflatiljr   r   r   flatten_indices   s   r$   c                 C   s(   ||  dk s	J | j|d   | S )z+
    Keep only the topk rows of grads.
    r   N)sizedatazero_)gradtopkr   r   r   keep_partial_grad#   s   r*   Tc                 C   sP   t |d}tj| |dd W d    n1 sw   Y  |r&td| | S )Nw   )indentzConfig saved to file {})openjsondumpprintr   )configpathverboseoutfiler   r   r   save_config,   s   r6   c                 C   sH   t | }t|}W d    n1 sw   Y  |r"td|  |S )NzConfig loaded from file {})r.   r/   loadr1   r   )r3   r4   fr2   r   r   r   load_config3   s   
r9   c                 C   sf   g }t | |D ])\}}g }|D ]}|dkr||j|  q| d }|||  q||g7 }q|S )zQ
    Unmap a list of list of indices, by optionally copying from src_tokens.
    r      )zipr   id2word)indices
src_tokensvocabresultindtokenswordsidxr   r   r   unmap_with_copy:   s   
rE   c                 C   sF   g }| D ]}t j|v r|t j}||d| g7 }q||g7 }q|S )z2
    Prune decoded sequences after EOS token.
    N)constantEOSindex	EOS_TOKEN)seqsoutsrD   r   r   r   prune_decoded_seqsJ   s   
rM   c                 C   s&   t j| v r| t j}| d| S | S )z$
    Prune a decoded hypothesis
    N)rF   EOS_IDrH   )hyprD   r   r   r   	prune_hypW   s   
rP   c                 C   sB   t | t |ks
J g }t| |D ]\}}||d |  q|S r   )lenr;   r   )	data_listlensnldr"   r   r   r   prunea   s
   rV   c                 C   sl   t | ts
t | trt |tsJ |gtt|g t|  } dd ttt|  |d D }t|dd S )zt
    Sort a series of packed list, according to a ref list.
    Also return the original index before the sort.
    c                 S      g | ]}t |qS r   list.0tr   r   r   
<listcomp>o       zsort.<locals>.<listcomp>)reverser:   N)
isinstancetuplerY   r   rQ   r;   sorted)packedrefr_   sorted_packedr   r   r   sorth   s   "rf   c                 C   s<   t | t |ksJ ddd ttt||  D \}}|S )z:
    Unsort a sorted list, based on the original idx.
    z9Number of list elements must match with original indices.c                 S   rW   r   rX   rZ   r   r   r   r]   w   r^   zunsort.<locals>.<listcomp>)rQ   r;   rb   )sorted_listoidx_unsortedr   r   r   unsortr   s    rk   )T)__doc__collectionsr   randomr/   r   %stanza.models.common.seq2seq_constantmodelscommonseq2seq_constantrF   r   r   r$   r*   r6   r9   rE   rM   rP   rV   rf   rk   r   r   r   r   <module>   s$    
	



