U
    dB                     @   sV   d dl mZ d dlmZ d dlm  m  m  mZ d dl	m
Z
 G dd deZdS )    )
namedtuple)coreN)Seq2SeqModelHelperc                   @   sb   e Zd ZdZeddddgZedddd	gZejd
fddZ	dd Z
dd Zdd ZdddZd
S )BeamSearchForwardOnlya  
    Class generalizing forward beam search for seq2seq models.

    Also provides types to specify the recurrent structure of decoding:

    StateConfig:
        initial_value: blob providing value of state at first step_model
        state_prev_link: LinkConfig describing how recurrent step receives
            input from global state blob in each step
        state_link: LinkConfig describing how step writes (produces new state)
            to global state blob in each step

    LinkConfig:
        blob: blob connecting global state blob to step application
        offset: offset from beginning of global blob for link in time dimension
        window: width of global blob to read/write in time dimension
    
LinkConfigbloboffsetwindowStateConfiginitial_valuestate_prev_link
state_linkNc                 C   s   || _ || _td| jd| _|| _|| _|| _| jjddddd\| _	| _
| _| _| _| jjj| jdtjjd	}| jjj|g|d
gddgd\| _}d S )N
step_model)nameZparam_modeltimestepscores_t_prevtokens_t_prevhypo_t_prevattention_t_prevtokens_t_prev_int32toZinput_t_int32_old_shape   shape)	beam_sizemodelr   r   go_token_ideos_token_idpost_eos_penaltynetZAddExternalInputsr   r   r   r   r   Castr   DataTypeINT32Reshapetokens_t_prev_int32_flattened)selfr   r   r   r   r    r   _ r)   L/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/models/seq2seq/beam_search.py__init__(   s@    zBeamSearchForwardOnly.__init__c                 C   s   | j S N)r   r'   r)   r)   r*   get_step_modelR   s    z$BeamSearchForwardOnly.get_step_modelc                 C   s   | j S r,   )r&   r-   r)   r)   r*   get_previous_tokensU   s    z)BeamSearchForwardOnly.get_previous_tokensc                 C   s   | j S r,   )r   r-   r)   r)   r*   get_timestepX   s    z"BeamSearchForwardOnly.get_timestepc
           ?         s  j jjg ddgdtjjd}
jj|
j	gd}j
d k	rBj jjg djgjtjjd}j jjg ddgtj
tjjd}j jjg ddgd	tjjd}jj|||gd
}jjjd}jjj|dtjjd}jj||gd}jjj|dtjjd}jjj||gddd}jjj||gdddd}jjj|ddgjd\}}|r|jj||gdg}jjjjddgjgd\}}jjj||gdddd}|d k	rjj||gd}jj||gd}jjj|g|dgdgd\}}j jjg dddgtjjd }j jjg d!dgjtjjd}jj|||gd"g}jj||
|gd#}jjj||d$gddgd\}}jjj|d%d&gjd\}} j jj|d'tjjd}!jjj| |!gd(ddjjjd)tjjd}"jj|gd*}#jjj|#|#d+gdjdgd\}#}jjj|d,d-gdgd\}$}jj|$| gd.}%jjj|%d/tjjd}&fd0d1fd2d3|D }j jjg d4dgd	tjjd}'|	rֈj j|	gd5}(n$j jjg d5dgtjtjjd}(j jjg d6dgd	tjjd})j jj|d7d8gdgd\}*}j jj|*d9d	tjjd:}+|j|'jdd|ddd;j|(jdd|&ddd;j|)j dd|"ddd;j|+j!dd|#ddd;g }j jj|d<d=jdgd	tjjd>},|,gjj" d?d3 |D  |  g }-g }.|D ]\}/t#|/j$j%d@ }0|.&|0 |-&|/j$j%|0|/j$j'|/j$j(f |-&|/j)j%|0|/j)j'|/j)j(f q8t*|- \}1}2}3}4dAd3 ||&|"|#fD }5j jj+ |5dBg  fdCd3jj"D dDd3 jjj j!fD |5dgdE |. fdFd3|D dGd3 |1D dHd3 |2D |3|4g g g jj, t#j	g dddI}6|6d dE \}7}8}9}:j jj|8dJtjjd};j jj|9dKtjjd}<j j-|7dL}=j j-|:dM}>|;|<|=|>fS )NNZEROr   r   )r   valuedtypeon_initial_step	eos_tokenfinished_penalty
ZERO_FLOATg        Zpossible_finished_penaltytokens_t_flattokens_t_flat_intr   predecessor_is_eospredecessor_is_eos_floatpredecessor_is_eos_penalty)	broadcastZlog_probs_penalized)r=   Zaxisbest_scores_per_hypoZbest_tokens_per_hypo_indices)kbest_tokens_per_hyposcores_t_prev_squeezedZscores_t_prev_old_shaper   output_scores%word_rewards_for_best_tokens_per_hypoZoutput_scores_old_shaper   MINUS_ONE_INT32)r2   r   r3   r   	slice_endoutput_scores_flattened_sliceZ'output_scores_flattened_slice_old_shapescores_tbest_indicesBEAM_SIZE_64hypo_t_int32hypo_tattention_tZattention_t_old_shapebest_tokens_per_hypo_flattenZbest_tokens_per_hypo_old_shapetokens_t_int32tokens_tc                    sz   j jj| jj| jj| jjd gjdgd\}}j j| gt| jjd }j| j	| j
j|| jj| jjddS )NZ
_old_shaper   r   Z_chosen_per_hypo)r   r   r	   r   r   r   )r   r!   r%   r   r   r   Gatherstrr
   r   r   r   r   r	   )state_configZstate_flattenedr(   Zstate_chosen_per_hypo)rJ   r'   r)   r*   choose_state_per_hypo8  s(    

z:BeamSearchForwardOnly.apply.<locals>.choose_state_per_hypoc                    s   g | ]} |qS r)   r)   ).0c)rT   r)   r*   
<listcomp>N  s     z/BeamSearchForwardOnly.apply.<locals>.<listcomp>initial_scoresinitial_tokensinitial_hypoencoder_inputs_flattenedZencoder_inputs_old_shapeinit_attention)r2   r3   rP   Zbeam_search_fake_inputT)Zinput_as_shapeZextra_shaper2   r3   c                 S   s   g | ]
}|j qS r)   )r   rU   rS   r)   r)   r*   rW     s     _statesc                 S   s   g | ]}t |d  qS )Z_allrR   rU   sr)   r)   r*   rW     s   Zstep_workspacesc                    s   g | ]}  |qS r)   )index)rU   p
all_inputsr)   r*   rW     s     c                 S   s   g | ]}t |d  qS )r^   r_   r`   r)   r)   r*   rW     s      c                    s   g | ]}  |jqS r)   )rb   r   r]   rd   r)   r*   rW     s   c                 S   s   g | ]}t |qS r)   r_   rU   lr)   r)   r*   rW     s     c                 S   s   g | ]}t |qS r)   r_   rg   r)   r)   r*   rW     s     )paramZ	alias_srcZ	alias_dstZalias_offsetrecurrent_statesZinitial_recurrent_state_idslink_internallink_externallink_offsetlink_windowZbackward_link_internalZbackward_link_externalZbackward_link_offsetZstep_netr   Zoutputs_with_gradsZenable_rnn_executorZrnn_executor_debugoutput_token_beam_listoutput_prev_index_beam_listoutput_score_beam_list"output_attention_weights_beam_list).r   Zparam_init_netZConstantFillr   r#   r$   r   r!   ZEQr   r    r   r   floatFLOATZConditionalZFlattenToVecr   r"   ZMulZAddZTopKrQ   r%   r   ZSliceZINT64ZDivZCopyr   r
   r   r   r   paramsrR   r   r   appendr   r	   r   zipZRecurrentNetworkZProtoZAlias)?r'   inputslengthZ	log_probsZ
attentionsZstate_configsZdata_dependenciesZword_rewardsZpossible_translation_tokensr   r1   r4   r5   r6   r7   r8   r9   r:   r;   r<   r>   r@   rA   r(   rB   rC   Zoutput_scores_flattenedrD   Z	BEAM_SIZErE   rF   rG   rH   rI   rK   rL   rM   rN   rO   rX   rY   rZ   r[   r\   Z
fake_inputZforward_linksrj   rS   Z
state_namerk   rl   rm   rn   Zall_outputsresultsZscore_t_allZtokens_t_allZ
hypo_t_allZattention_t_allro   rp   rq   rr   r)   )re   rT   rJ   r'   r*   apply_   s   




	




	



	

!zBeamSearchForwardOnly.apply)NNN)__name__
__module____qualname____doc__r   r   r
   seq2seq_utilZGO_IDr+   r.   r/   r0   r{   r)   r)   r)   r*   r      s    

*   r   )collectionsr   Zcaffe2.pythonr   Z)caffe2.python.models.seq2seq.seq2seq_utilpythonmodelsZseq2seqr   Z1caffe2.python.models.seq2seq.seq2seq_model_helperr   objectr   r)   r)   r)   r*   <module>   s   