o
    }oi6                     @   s@   d dl mZmZmZmZmZmZ d dlZG dd dejj	Z
dS )    )AnyDictListOptionalTupleUnionNc                       sF   e Zd ZdZ fddZ	d	deej deeej  fddZ	  Z
S )
StatelessNeta  
    Helper class used in transducer models with stateless decoders. This stateless
    simply outputs embedding or concatenated embeddings for the input label[s],
    depending on the configured context size.

    Args:
        context_size: history context size for the stateless decoder network. Could be any positive integer. We recommend setting this as 2.
        vocab_size: total vocabulary size.
        emb_dim: total embedding size of the stateless net output.
        blank_idx: index for the blank symbol for the transducer model.
        normalization_mode: normalization run on the output embeddings. Could be either 'layer' or None. We recommend using 'layer' to stabilize training.
        dropout: dropout rate on the embedding outputs.
    c                    s   t    |dksJ || _|| _|| _tj|| _tj	 | _
|dkr.tjj|dd| _
g }t| jD ],}|dkrC|d | j }	n||d | j | jd   }	tjj|d |	|d}
||
 q5tj|| _|| _d S )Nr   layerF)elementwise_affine      )padding_idx)super__init__context_size
vocab_sizeemb_dimtorchnnDropoutdropoutIdentitynorm	LayerNormrange	Embeddingappend
ModuleListembeds	blank_idx)selfr   r   r   r   normalization_moder   r   i
embed_sizeembed	__class__ g/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/submodules/stateless_net.pyr   #   s$   
	
zStatelessNet.__init__Nystatec                 C   s  g }|j \}}|}|dkrytj|d |gdd}|j d }|| jk r@tj|| jgtj|jd| j }||dd| j| df< n|| jd krR|ddddf }n|}t| jD ]}	| j	|	 |dd| jd |	 | j|	 f }
|
|
 qYnDt| jD ]>}	| j	|	 |}
|	dkr|
ddd|	 ddf  |
dd|	dddf< |
ddd|	ddf  d9  < |
|
 q~| tj|dd}
| |
}
d}|dur|dd|j d | j d df g}|
|fS )aA  
        Although this is a *stateless* net, we use the "state" parameter to
        pass in the previous labels, unlike LSTMs where state would represent
        hidden activations of the network.

        Args:
            y: a Integer tensor of shape B x U.
            state: a list of 1 tensor in order to be consistent with the stateful
                   decoder interface, and the element is a tensor of shape [B x context-length].

        Returns:
            The return dimension of this function's output is B x U x D, with D being the total embedding dim.
        Nr   r   )axis)dtypedeviceg        )shaper   concatr   oneslongr-   r   r   r   r   cloner   r   )r    r)   r*   outsBU
appended_yr   padded_stater"   outr'   r'   r(   forwardC   s@   


 ."
&zStatelessNet.forward)NN)__name__
__module____qualname____doc__r   r   r   Tensorr   r:   __classcell__r'   r'   r%   r(   r      s    !r   )typingr   r   r   r   r   r   r   r   Moduler   r'   r'   r'   r(   <module>   s    