o
    }oi                     @   sN   d dl mZ d dlZd dlmZmZ d dlmZmZ dgZ	G dd deZ
dS )    )ListN)Loss	typecheck)LossType
NeuralTypeAggregatorLossc                       sV   e Zd ZdZedd Zedd Zdded	ee	 f fd
dZ
e dd Z  ZS )r   z
    Sums several losses into one.

    Args:
        num_inputs: number of input losses
        weights: a list of coefficient for merging losses
    c                 C   s4   i }t | jD ]}tt d|dt|d  < q|S )z3Returns definitions of module input ports.
        elements_typeloss_   )range_num_lossesr   r   str)selfinput_typesi r   ]/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/common/losses/aggregator.pyr   "   s   zAggregatorLoss.input_typesc                 C   s   dt t diS )z4Returns definitions of module output ports.
        lossr   )r   r   )r   r   r   r   output_types,   s   zAggregatorLoss.output_types   N
num_inputsweightsc                    s6   t    || _|d urt||krtd|| _d S )NzFLength of weights should be equal to the number of inputs (num_inputs))super__init__r   len
ValueError_weights)r   r   r   	__class__r   r   r   2   s
   

zAggregatorLoss.__init__c                    sh    fddt   D }t|d }t|D ]\}}| jd ur,|j|| j| d}q||}q|S )Nc                    s   g | ]} | qS r   r   ).0xkwargsr   r   
<listcomp><   s    z*AggregatorLoss.forward.<locals>.<listcomp>r   )alpha)sortedkeystorch
zeros_like	enumerater   add)r   r#   valuesr   loss_idx
loss_valuer   r"   r   forward:   s   
zAggregatorLoss.forward)r   N)__name__
__module____qualname____doc__propertyr   r   intr   floatr   r   r/   __classcell__r   r   r   r   r      s    
	
)typingr   r(   nemo.core.classesr   r   nemo.core.neural_typesr   r   __all__r   r   r   r   r   <module>   s   