o
    i
                     @   s   d dl Z G dd deZdS )    Nc                   @   s4   e Zd ZdZdddZdd Zddd	Zd
d ZdS )WindowStreamingE2EzWindowStreamingE2E constructor.

    :param E2E e2e: E2E ASR object
    :param recog_args: arguments for "recognize" method of E2E
    Nc                 C   sZ   || _ || _|j| _|| _| j   d| _d | _g | _g | _	d | _
| jjdks+J dd S )Nr   g        zGWindowStreamingE2E works only with combined CTC and attention decoders.)_e2e_recog_args	char_list
_char_list_rnnlmeval_offset!_previous_encoder_recurrent_state_encoder_states_ctc_posteriors_last_recognition
ctc_weight)selfe2e
recog_argsrnnlm r   `/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/pytorch_backend/streaming/window.py__init__   s   
zWindowStreamingE2E.__init__c                 C   sb   | j |\}}| j |d|| j\}}| _| j|d | j| j j	
|d dS )z=Call this method each time a new batch of input is available.r   N)r   subsample_framesenc	unsqueezer
   r   appendsqueezer   ctclog_softmax)r   xhilen_r   r   r   accept_input   s    zWindowStreamingE2E.accept_inputFc                    sD   |rt j jddt j jddfS  fdd}| j| jfS )Nr   dimc                    sH    j }d}g }| D ]}||kr|| q	||d7 }q	tj|ddS )Nr      r"   )r	   r   sizetorchcat)window_tensorslast_offsetoffset_traversedselected_windowsesr   r   r   select_unprocessed_windows4   s   
zPWindowStreamingE2E._input_window_for_decoder.<locals>.select_unprocessed_windows)r&   r'   r   r   )r   use_allr.   r   r-   r   _input_window_for_decoder-   s   z,WindowStreamingE2E._input_window_for_decoderc                 C   s,   | j dd\}}| jj||| j| j| jS )aa  Run the attention decoder offline.

        Works even if the previous layers (encoder and CTC decoder) were
        being run in the online mode.
        This method should be run after all the audio has been consumed.
        This is used mostly to compare the results between offline
        and online implementation of the previous layers.
        T)r/   )r0   r   decrecognize_beamr   r   r   )r   r   lpzr   r   r   decode_with_attention_offlineD   s   	z0WindowStreamingE2E.decode_with_attention_offline)N)F)__name__
__module____qualname____doc__r   r!   r0   r4   r   r   r   r   r      s    

r   )r&   objectr   r   r   r   r   <module>   s    