Ë
    ÛÓ^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Ð[\ÌtÐTUÏwÑNqùs   ‚ú.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ÐØÐ(Ñ(ÜØ& y kÐ1DÀTÇYÁYÑNqÐ`pÔNqÓEqÐDrÐrsÐtó
ð 	
ð $×&Ñ& v§}¡}Ó5€NÜ×"Ñ"×,Ñ,Ø�|‰|‹~×(Ñ(Ó*Ø—
‘
˜6Ÿ=™=Ó)×-Ñ-Ó/Ø#×&Ñ& v§}¡}Ó5×9Ñ9Ó;Ø%×)Ñ)Ó+ØØð -ó €Fð �MÒ!Ø�z‰z‹|˜n×2Ñ2Ó4×8Ñ8Ó:Ñ:Ð:Ø	�lÒ	"Ø�{‰{‹}ÐØ	�fÒ	Ø˜×-Ñ-Ó/Ñ/×5Ñ5Ó7Ð7Ø	�eÒ	Ø�z‰z‹|ÐØ€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Ó	%€áÔÛð #ñ?Ø�L‰Lð?à�\‰\ð?ð —<‘<ð?ð —L‘Lð	?ð
 ð?ð ð?ð ‡\�\ó?ðP ôr(   