
    ^j                         d dl Z ddlmZmZ  ej                  e      Z e       rd dlZ	 dde j                  de j                  de j                  de j                  de	d	e
d
e j                  fdZ	 ddZy)    N   )is_torchaudio_availablelogginglogitstargetslogit_lengthstarget_lengthsblank_token_id	reductionreturnc           	         t               st        d      d}||vr(t        d| ddj                  d |D               d      |j	                  | j
                        }t        j                  j                  | j                         j                         |j	                  | j
                        j                         |j	                  | j
                        j                         |j                         |d	      }|d
k(  r/|j                         |j                         j                         z  S |dk(  r|j                         S |dk(  r!||j                         z  j                         S |dk(  r|j                         S |S )a  
    Compute standard RNN-T (RNN Transducer) loss (https://huggingface.co/papers/1211.3711).

    Thin wrapper around [`torchaudio.functional.rnnt_loss`]. torchaudio is queried with `reduction="none"` to get
    the per-sample negative log-likelihoods, and the requested reduction is applied here. The reduction names and
    formulas mirror NeMo's `RNNTLoss` (the reference implementation used to train/finetune Parakeet), so that loss
    magnitudes and gradient scaling match when finetuning other RNNT models like Parakeet:

    - `"mean_volume"`: sum of per-sample losses divided by the sum of target lengths (per-token average over the
      whole batch). This is what `nvidia/parakeet-rnnt-0.6b` is trained with (`rnnt_reduction: mean_volume`).
    - `"mean_batch"`: plain average of per-sample losses over the batch (NeMo's default).
    - `"mean"`: per-sample loss divided by its own target length, then averaged over the batch.
    - `"sum"`: sum of per-sample losses.
    - `"none"`: per-sample losses, unreduced.

    Args:
        logits: Joint token logits of shape `(batch, T, U+1, vocab_size)`.
        targets: Target labels of shape `(batch, U)`.
        logit_lengths: Encoder output lengths of shape `(batch,)`.
        target_lengths: Target lengths of shape `(batch,)`.
        blank_token_id: Blank token id.
        reduction: Loss reduction method. One of `"mean_volume"`, `"mean_batch"`, `"mean"`, `"sum"`, or `"none"`.

    Returns:
        Scalar loss tensor (or per-example losses if `reduction="none"`).

    zWComputing the RNN-T loss requires torchaudio. Install it with `pip install torchaudio`.)mean_volume
mean_batchmeansumnonezInvalid reduction mode "z". Expected one of z, c              3   2   K   | ]  }t        |        y w)N)repr).0rs     f/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/loss/loss_rnnt.py	<genexpr>zrnnt_loss.<locals>.<genexpr>D   s     Nq[\tTUwNqs   .r   )r   r   r   r	   blankr   r   r   r   r   )r   ImportError
ValueErrorjointodevice
torchaudio
functional	rnnt_lossfloat
contiguousintr   r   )r   r   r   r	   r
   r   valid_reductionslossess           r   r"   r"      s`   H #$sttK((&yk1DTYYNq`pNqEqDrrst
 	
 $&&v}}5N"",,||~((*

6==)--/#&&v}}599;%))+ - F M!zz|n22488:::	l	"{{}	f	--//5577	e	zz|M    c                 $    t        | |||||      S )N)r   r   r   r	   r
   r   )r"   )r   labelsr   label_lengthsr
   r   kwargss          r   ParakeetForRNNTLossr-   \   s#     #$% r(   )r   )torchutilsr   r   
get_logger__name__loggerr    Tensorr%   strr"   r-    r(   r   <module>r6      s     4 
		H	% #?LL?\\? <<? LL	?
 ? ? \\?P r(   