o
    }oi                  
   @   sH   d dl Z d dlm  mZ de jde jdedee je jf fddZdS )    Nlogitstargetsblank_idreturnc                 C   s   | j }| jd }tj| dd}|d|f }tj|tj|g|j|ddfdd}tj	|d|d
|jdd ddd}d	|dddddf< ||fS )
a  
    Given logits, calculate log probabilities for blank and target labels needed for transducer loss calculation.
    Naive implementation in PyTorch, for testing and prototyping purposes.

    Args:
        logits: Joint tensor of size [B, T, U+1, D]
        targets: Targets of size [B, U]
        blank_id: id of the blank output

    Returns:
        Tuple of tensors with log probabilities for targets and blank labels, both of size [B, T, U+1].
        For the last non-existent target (U+1) output is zero.
    r   )dim.)dtypedevice   N)r   indexg        )r	   shapeFlog_softmaxtorchcatzerosr   	unsqueezegatherexpandsqueeze)r   r   r   r	   
batch_size	log_probsblank_scorestarget_scores r   _/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/k2/rnnt_logprobs.pyrnnt_logprobs_torch   s   
("r   )	r   torch.nn.functionalnn
functionalr   Tensorinttupler   r   r   r   r   <module>   s   