o
    }oiX                     @   sD   d dl Z d dlmZmZ d dlmZmZmZmZ G dd deZ	dS )    N)NeuralModule	typecheck)LengthsTypeLogprobsType
NeuralTypePredictionsTypec                       sV   e Zd ZdZedd Zedd Z	d fd	d
	Zedde	
 dd Z  ZS )BeamSearchDecoderWithLMa  Neural Module that does CTC beam search with a N-gram language model.
    It takes a batch of log_probabilities. Note the bigger the batch, the
    better as processing is parallelized. Outputs a list of size batch_size.
    Each element in the list is a list of size beam_search, and each element
    in that list is a tuple of (final_log_prob, hyp_string).
    Args:
        vocab (list): List of characters that can be output by the ASR model. For English, this is the 28 character set
            {a-z '}. The CTC blank symbol is automatically added.
        beam_width (int): Size of beams to keep and expand upon. Larger beams result in more accurate but slower
            predictions
        alpha (float): The amount of importance to place on the N-gram language model. Larger alpha means more
            importance on the LM and less importance on the acoustic model.
        beta (float): A penalty term given to longer word sequences. Larger beta will result in shorter sequences.
        lm_path (str): Path to N-gram language model
        num_cpus (int): Number of CPUs to use
        cutoff_prob (float): Cutoff probability in vocabulary pruning, default 1.0, no pruning
        cutoff_top_n (int): Cutoff number in pruning, only top cutoff_top_n characters with highest probs in
            vocabulary will be used in beam search, default 40.
        input_tensor (bool): Set to True if you intend to pass PyTorch Tensors, set to False if you intend to pass
            NumPy arrays.
    c                 C   s   t dt t tdt dS )z3Returns definitions of module input ports.
        )BTDr	   )	log_probslog_probs_length)r   r   tupler   self r   d/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/modules/beam_search_decoder.pyinput_types,   s   
z#BeamSearchDecoderWithLM.input_typesc                 C   s   dt dt iS )z4Returns definitions of module output ports.
        predictions)r	   r
   )r   r   r   r   r   r   output_types5   s   z$BeamSearchDecoderWithLM.output_types      ?(   Fc
                    s   z
ddl m}
m} W n ty   tdw t   |d ur)|
||||d| _nd | _|| _|| _|| _	|| _
|| _|| _|	| _d S )Nr   )Scorerctc_beam_search_decoder_batchzBeamSearchDecoderWithLM requires the installation of ctc_decoders from scripts/asr_language_modeling/ngram_lm/install_beamsearch_decoders.sh)
model_path
vocabulary)ctc_decodersr   r   ModuleNotFoundErrorsuper__init__scorerbeam_search_funcvocab
beam_widthnum_cpuscutoff_probcutoff_top_ninput_tensor)r   r"   r#   alphabetalm_pathr$   r%   r&   r'   r   r   	__class__r   r   r   ;   s$   

z BeamSearchDecoderWithLM.__init__T)ignore_collectionsc              	   C   sp   |}| j r$t|}g }t|D ]\}}||d || d d f  q| j|| j| j| j| j	| j
| jd}|S )N)	beam_sizenum_processesext_scoring_funcr%   r&   )r'   torchexp	enumerateappendr!   r"   r#   r$   r    r%   r&   )r   r   r   
probs_listprobsiprobresr   r   r   forwardU   s    
 	zBeamSearchDecoderWithLM.forward)r   r   F)__name__
__module____qualname____doc__propertyr   r   r   r   r1   no_gradr:   __classcell__r   r   r+   r   r      s    

r   )
r1   nemo.core.classesr   r   nemo.core.neural_typesr   r   r   r   r   r   r   r   r   <module>   s   