o
    }oi:	                     @   sP   d dl Z d dlmZmZmZ d dlmZmZmZm	Z	 dgZ
G dd deeZdS )    N)LossTyping	typecheck)
LabelsType
LogitsTypeLossType
NeuralTypeAngularSoftmaxLossc                       sH   e Zd ZdZedd Zedd Zd fdd		Ze d
d Z	  Z
S )r	   a5  
    Computes ArcFace Angular softmax angle loss
    reference: https://openaccess.thecvf.com/content_CVPR_2019/papers/Deng_ArcFace_Additive_Angular_Margin_Loss_for_Deep_Face_Recognition_CVPR_2019_paper.pdf
    args:
    scale: scale value for cosine angle
    margin: margin value added to cosine angle 
    c                 C   s   t dt t dt dS )z0Input types definitions for AnguarLoss.
        )BD)r
   )logitslabels)r   r   r   self r   [/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/losses/angularloss.pyinput_types!   s   

zAngularSoftmaxLoss.input_typesc                 C   s   dt t diS )z]Output types definitions for AngularLoss.
        loss:
            NeuralType(None)
        loss)elements_type)r   r   r   r   r   r   output_types*   s   zAngularSoftmaxLoss.output_types      4@皙?c                    s    t    d| _|| _|| _d S )NgHz>)super__init__epsscalemargin)r   r   r   	__class__r   r   r   2   s   

zAngularSoftmaxLoss.__init__c                    s   | j tttt dd| d| j d| j | j  }tj	 fddt
|D dd}t|tjt| j | dd }|t| }t| S )Nr      g      c              	      s@   g | ]\}}t  |d |f  ||d d f fdqS )Nr   r   )torchcat	unsqueeze).0iyr   r   r   
<listcomp>@   s   @ z.AngularSoftmaxLoss.forward.<locals>.<listcomp>)dim)r   r    cosacosclampdiagonal	transposer   r   r!   	enumerateexpsumlogmean)r   r   r   	numeratorexcldenominatorLr   r&   r   forward9   s   0$zAngularSoftmaxLoss.forward)r   r   )__name__
__module____qualname____doc__propertyr   r   r   r   r7   __classcell__r   r   r   r   r	      s    

)r    nemo.core.classesr   r   r   nemo.core.neural_typesr   r   r   r   __all__r	   r   r   r   r   <module>   s
   