o
    wi/                     @   s`   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZ G dd deZdS )    )OptionalN)
DictConfig)Loss	typecheck)
LabelsTypeLengthsTypeLogprobsTypeLossType
NeuralTypec                       s~   e Zd ZdZedd Zedd Z						
	ddedededededede	e
 f fddZdd Ze dd Z  ZS )LatticeLossaD  Family of loss functions based on various lattice scores.

    Note:
        Requires k2 v1.14 or later to be installed to use this loss function.

    Losses can be selected via the config, and optionally be passed keyword arguments as follows.

    Examples:
        .. code-block:: yaml

            model:  # Model config
                ...
                graph_module_cfg:  # Config for graph modules, e.g. LatticeLoss
                    criterion_type: "map"
                    loss_type: "mmi"
                    split_batch_size: 0
                    backend_cfg:
                        topo_type: "default"       # other options: "compact", "shared_blank", "minimal"
                        topo_with_self_loops: true
                        token_lm: <token_lm_path>  # must be provided for criterion_type: "map"

    Args:
        num_classes: Number of target classes for the decoder network to predict.
            (Excluding the blank token).

        reduction: Type of reduction to perform on loss. Possible values are `mean_batch`, `mean`, `sum`, or None.
            None will return a torch vector comprising the individual loss values of the batch.

        backend: Which backend to use for loss calculation. Currently only `k2` is supported.

        criterion_type: Type of criterion to use. Choices: `ml` and `map`, 
            with `ml` standing for Maximum Likelihood and `map` for Maximum A Posteriori Probability.

        loss_type: Type of the loss function to use. Choices: `ctc` and `rnnt` for `ml`, and `mmi` for `map`.

        split_batch_size: Local batch size. Used for memory consumption reduction at the cost of speed performance.
            Effective if complies 0 < split_batch_size < batch_size.

        graph_module_cfg: Optional Dict of (str, value) pairs that are passed to the backend loss function.
    c                 C   s@   t | jrdndt t dt t tdt t tdt dS )z1Input types definitions for LatticeLoss.
        )BTD)r   r   r   r   )r   r   r   	log_probstargetsinput_lengthstarget_lengths)r
   	_3d_inputr   r   tupler   self r   g/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/asr/losses/lattice_losses.pyinput_typesC   s
   
zLatticeLoss.input_typesc                 C   s   dt t diS )z]Output types definitions for LatticeLoss.
        loss:
            NeuralType(None)
        loss)elements_type)r
   r	   r   r   r   r   output_typesN   s   zLatticeLoss.output_types
mean_batchk2mlctcr   Nnum_classes	reductionbackendcriterion_type	loss_typesplit_batch_sizegraph_module_cfgc                    sP  t    || _|| _d }|dkrd}d| _n	|dv r |}d| _|dkrs|dkrF|dkr3d	d
lm}	 n2|dkr>d	dlm}	 n'td| d|dkr]|dkrUd	dl	m
}	 ntd| dtd| d|	| jd | j||d| _n|dkrtd| dtd| d|| _|| _| jdk| _| jd	krd	dlm}
 |
| j| _d S d S )Nr   noneT)summeanr)   Fr   r    r!   r   )CtcLossrnnt)RnntLosszUnsupported `loss_type`: .map)
CtcMmiLosszUnsupported `criterion_type`:    )r"   blankr#   cfggtnzBackend z is not supported.zInvalid value of `backend`: )PartialGrad)super__init___blankr'   _apply_batch_mean%nemo.collections.asr.parts.k2.ml_lossr,   r.   
ValueError&nemo.collections.asr.parts.k2.map_lossr1   _lossNotImplementedErrorr%   r&   r   (nemo.collections.asr.parts.k2.grad_utilsr6   _partial_loss)r   r"   r#   r$   r%   r&   r'   r(   inner_reductionK2Lossr6   	__class__r   r   r8   V   sF   



zLatticeLoss.__init__c                 C   s   | j dkr| j| dS dS )z4Updates graph of the backend loss function.
        r    N)r%   r>   update_graph)r   graphr   r   r   rF      s   
zLatticeLoss.update_graphc                 C   s\  t | st | rJ | }| }| }| }|jd }| jdkr| j|krg }td|| jD ]N}|}t	|| j |}	|||	 }
|||	d |

 f }|||	 }|||	d |
 f }|jrs| |||
|n| |||
|\}}~~~
~|| q8t |d}n| j||||d\}}| jr| dkrt |nt |}|S )Nr   r   )torchisnananyisinffloatlongshaper'   rangeminmaxrequires_gradrA   r>   appendcatr:   nelementr+   r*   )r   r   r   r   r   
batch_size	loss_list	batch_idxbeginendinput_lengths_partlog_probs_parttarget_lengths_parttargets_part	loss_part_r   r   r   r   forward   s8    

 zLatticeLoss.forward)r   r   r    r!   r   N)__name__
__module____qualname____doc__propertyr   r   intstrr   r   r8   rF   r   ra   __classcell__r   r   rD   r   r      s<    )



8r   )typingr   rH   	omegaconfr   nemo.core.classesr   r   nemo.core.neural_typesr   r   r   r	   r
   r   r   r   r   r   <module>   s   