o
    }oi P                     @   s  d dl Z d dlmZmZmZmZ d dlZd dlZd dl	m
Z
 								 d-dededed	ee d
ee dee dee dee dedededejjfddZG dd dejjZG dd dejjZG dd dejjZG dd dejjZG dd dejjZ		d.dedededee d
ee dee dee dee dejjfddZG d d! d!ejjZG d"d# d#ejjZded$ejjd%ed&edejjf
d'd(ZG d)d* d*ejjZd/d+d,ZdS )0    N)ListOptionalTupleUnion)logging      ?        
input_sizehidden_size
num_layersnormforget_gate_biasdropoutnorm_first_rnnt_maxweights_init_scalehidden_hidden_bias_scale	proj_sizereturnc                 C   s   |dvrt d| |du rt| |||||||	|
d	S |dkr/t| ||d||||||	|
dS |dkrCtjt| |||||||	d	S dS )
a  
    Utility function to provide unified interface to common LSTM RNN modules.

    Args:
        input_size: Input dimension.

        hidden_size: Hidden dimension of the RNN.

        num_layers: Number of RNN layers.

        norm: Optional string representing type of normalization to apply to the RNN.
            Supported values are None, batch and layer.

        forget_gate_bias: float, set by default to 1.0, which constructs a forget gate
                initialized to 1.0.
                Reference:
                [An Empirical Exploration of Recurrent Network Architectures](http://proceedings.mlr.press/v37/jozefowicz15.pdf)

        dropout: Optional dropout to apply to end of multi-layered RNN.

        norm_first_rnn: Whether to normalize the first RNN layer.

        t_max: int value, set to None by default. If an int is specified, performs Chrono Initialization
            of the LSTM network, based on the maximum number of timesteps `t_max` expected during the course
            of training.
            Reference:
            [Can recurrent neural networks warp time?](https://openreview.net/forum?id=SJcKhk-Ab)

        weights_init_scale: Float scale of the weights after initialization. Setting to lower than one
            sometimes helps reduce variance between runs.

        hidden_hidden_bias_scale: Float scale for the hidden-to-hidden bias scale. Set to 0.0 for
            the default behaviour.

    Returns:
        A RNN module
    )Nbatchlayerzunknown norm=N	r	   r
   r   r   r   r   r   r   r   r   T)r	   r
   
rnn_layers
batch_normr   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   )
ValueErrorLSTMDropoutBNRNNSumtorchjitscriptln_lstm)r	   r
   r   r   r   r   r   r   r   r   r    r"   U/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/common/parts/rnn.pyrnn   sT   2r$   c                       s@   e Zd ZdZdejjf fddZdejdejfddZ	  Z
S )	OverLastDimaa  Collapses a tensor to 2D, applies a module, and (re-)expands the tensor.
    An n-dimensional tensor of shape (s_1, s_2, ..., s_n) is first collapsed to
    a tensor with shape (s_1*s_2*...*s_n-1, s_n). The module is called with
    this as input producing (s_1*s_2*...*s_n-1, s_n') --- note that the final
    dimension can change. This is expanded to (s_1, s_2, ..., s_n-1, s_n') and
    returned.
    Args:
        module (torch.nn.Module): Module to apply. Must accept a 2D tensor as
            input and produce a 2D tensor as output, optionally changing the
            size of the last dimension.
    modulec                    s   t    || _d S N)super__init__r&   )selfr&   	__class__r"   r#   r)      s   

zOverLastDim.__init__xr   c                 C   sR   |  ^ }}d}|D ]}||9 }q||d}| |}|jg |dR  }|S )N   )sizeviewr&   )r*   r-   dims_reduced_dimsdimr"   r"   r#   forward   s   

zOverLastDim.forward)__name__
__module____qualname____doc__r   nnModuler)   Tensorr6   __classcell__r"   r"   r+   r#   r%   y   s    r%   c                       s   e Zd Z				ddedededee d	ee d
ee dededef fddZ	ddejdee	ejejf  de	eje	ejejf f fddZ
  ZS )r   Nr   r   r   r	   r
   r   r   r   r   r   r   r   c
              	      s  t t|   tjj|||||	d| _|durc| j D ]E\}
}d|
v rat| j|
}|	 }|d }|j
d ttjj|j
d| d|d |j
|d| < |j
|d|   |j
d|< qn=|dur| j D ]3\}
}d|
v rt| j|
}|j
|d|  | d	|
v rt| j|
}|j
|d|   t|9  < ql|rtj|nd| _|  D ]\}
}d
|
v sd|
v r| j
t|9  _
qdS )a  Returns an LSTM with forget gate bias init to `forget_gate_bias`.
        Args:
            input_size: See `torch.nn.LSTM`.
            hidden_size: See `torch.nn.LSTM`.
            num_layers: See `torch.nn.LSTM`.
            dropout: See `torch.nn.LSTM`.

            forget_gate_bias: float, set by default to 1.0, which constructs a forget gate
                initialized to 1.0.
                Reference:
                [An Empirical Exploration of Recurrent Network Architectures](http://proceedings.mlr.press/v37/jozefowicz15.pdf)

            t_max: int value, set to None by default. If an int is specified, performs Chrono Initialization
                of the LSTM network, based on the maximum number of timesteps `t_max` expected during the course
                of training.
                Reference:
                [Can recurrent neural networks warp time?](https://openreview.net/forum?id=SJcKhk-Ab)

            weights_init_scale: Float scale of the weights after initialization. Setting to lower than one
                sometimes helps reduce variance between runs.

            hidden_hidden_bias_scale: Float scale for the hidden-to-hidden bias scale. Set to 0.0 for
                the default behaviour.

        Returns:
            A `torch.nn.LSTM`.
        )r	   r
   r   r   r   Nbias   r   r.      bias_ihbias_hhweight)r(   r   r)   r   r;   LSTMlstmnamed_parametersgetattrnelementdatafill_loginituniform_floatDropoutr   )r*   r	   r
   r   r   r   r   r   r   r   namevpnr?   r+   r"   r#   r)      s@   '
zLSTMDropout.__init__r-   hr   c                 C   s(   |  ||\}}| jr| |}||fS r'   )rF   r   )r*   r-   rU   r"   r"   r#   r6      s   
zLSTMDropout.forward)Nr   r   r   r'   )r7   r8   r9   intr   rO   r)   r   r=   r   r6   r>   r"   r"   r+   r#   r      s@    	
Lr   c                       s   e Zd ZdZejjddddddfdeded	ejjd
e	de
e de
e dededef fddZ	ddejde
eeejejf   deejeejejf f fddZdd Z  ZS )RNNLayerz+A single RNNLayer with optional batch norm.Tr   Nr   r   r	   r
   rnn_typer   r   r   r   r   r   c
           
         sh   t    |rttj|| _t|tjjr)|s)t	||dd|||||	d	| _
d S |||| d| _
d S )Nr.   r   r   )r	   r
   r?   )r(   r)   r%   r   r;   BatchNorm1dbn
isinstancerE   r   r$   )
r*   r	   r
   rX   r   r   r   r   r   r   r+   r"   r#   r)      s    
zRNNLayer.__init__r-   hxr   c                 C   s6   t | dr| }| |}| j||d\}}||fS )NrZ   r\   )hasattr
contiguousrZ   r$   )r*   r-   r\   rU   r"   r"   r#   r6     s
   

zRNNLayer.forwardc                 C   s   | j   d S r'   )r$   flatten_parameters)r*   r"   r"   r#   _flatten_parameters  s   zRNNLayer._flatten_parametersr'   )r7   r8   r9   r:   r   r;   rE   rV   r<   boolr   rO   r)   r=   r   r   r6   ra   r>   r"   r"   r+   r#   rW      sJ    	
"
	rW   c                       s  e Zd ZdZejjdddddddddf
d	ed
edejjdede	de
e de
e de	de
e dededef fddZ	d dejde
eeejejf   deejeejejf f fddZde
eejejf  deed eeejejf  f fddZdd Z  ZS )!r   aN  RNN wrapper with optional batch norm.
    Instantiates an RNN. If it is an LSTM it initialises the forget gate
    bias =`lstm_gate_bias`. Optionally applies a batch normalisation layer to
    the input with the statistics computed over all time steps.  If dropout > 0
    then it is applied to all layer outputs except the last.
    r.   Tr   r   FNr   r	   r
   rX   r   r   r   r   r   r   r   r   r   c                    s   t    || _tj | _t|D ]5}|d |k}| jt	||||o(|p(|dk||	|
||d	 |d urE|dkrE|sE| jtj
| |}qd S )Nr.   r   )rX   r   r   r   r   r   r   r   )r(   r)   r   r   r;   
ModuleListlayersrangeappendrW   rP   )r*   r	   r
   rX   r   r   r   r   r   r   r   r   r   ifinal_layerr+   r"   r#   r)   '  s,   
zBNRNNSum.__init__r-   r\   r   c           
      C   s   |  |}g }g }d}| jD ]+}t|tjjr||}q|||| d\}}||d  ||d  |d7 }~qtj|dd}tj|dd}	|||	ffS )Nr   r]   r.   r5   )_parse_hidden_staterd   r[   r   r;   rP   rf   stack)
r*   r-   r\   hscsrnn_idxr   h_outh_0c_0r"   r"   r#   r6   P  s   


zBNRNNSum.forwardc                    sR   |du r
dg| j  S |\ jd | j krtd fddtjd D S )z
        Dealing w. hidden state:
        Typically in pytorch: (h_0, c_0)
            h_0 = ``[num_layers * num_directions, batch, hidden_size]``
            c_0 = ``[num_layers * num_directions, batch, hidden_size]``
        Nr   zgProvided initial state value `h_0` must be of shape : [num_layers * num_directions, batch, hidden_size]c                    s   g | ]
}|  | fqS r"   r"   ).0rg   rq   rp   r"   r#   
<listcomp>z  s    z0BNRNNSum._parse_hidden_state.<locals>.<listcomp>)r   shaper   re   )r*   r\   r"   rs   r#   rj   f  s   	zBNRNNSum._parse_hidden_statec                 C   s4   | j D ]}t|tjjtjjtjjfr|  qd S r'   )rd   r[   r   r;   rE   GRURNNra   )r*   r   r"   r"   r#   ra   |  s
   
zBNRNNSum._flatten_parametersr'   )r7   r8   r9   r:   r   r;   rE   rV   r<   rb   r   rO   r)   r=   r   r   r6   r   rj   ra   r>   r"   r"   r+   r#   r     sf    	
*

r   c                       sN   e Zd ZdZdef fddZdeeej	  deej	ej	f fddZ
  ZS )		StackTimezZ
    Stacks time within the feature dim, so as to behave as a downsampling operation.
    factorc                    s   t    t|| _d S r'   )r(   r)   rV   ry   )r*   ry   r+   r"   r#   r)     s   
zStackTime.__init__r-   r   c                 C   s   |\}}|g}t d| jD ]%}t|}||d d d d d f |d | d d d d f< || qt| | j  }tj|ddd d | jd d d d f |fS )Nr.   rA   ri   )	re   ry   r   
zeros_likerf   ceilrO   rV   cat)r*   r-   x_lensseqrg   tmpr"   r"   r#   r6     s   
2,zStackTime.forward)r7   r8   r9   r:   rV   r)   r   r   r   r=   r6   r>   r"   r"   r+   r#   rx     s    0rx   c                 C   sn   |dur|dkrt d|durtd |durtd |dur'td t|tt| ||gt|||gdS )z9Returns a ScriptModule that mimics a PyTorch native LSTM.Nr   z*`dropout` not supported with LayerNormLSTMz6LayerNormLSTM does not support chrono init via `t_max`z1`weights_init_scale` is ignored for LayerNormLSTMz7`hidden_hidden_bias_scale` is ignored for LayerNormLSTM)first_layer_argsother_layer_args)r   r   warningStackedLSTM	LSTMLayerLayerNormLSTMCellr   r"   r"   r#   r!     s   




r!   c                
       sV   e Zd Z fddZdejdeejejf deejeejejf f fddZ  ZS )r   c                    s   t t|   || | _d S r'   )r(   r   r)   cell)r*   r   	cell_argsr+   r"   r#   r)     s   zLSTMLayer.__init__inputstater   c                 C   sL   | d}g }tt|D ]}| || |\}}||g7 }qt||fS )Nr   )unbindre   lenr   r   rk   )r*   r   r   inputsoutputsrg   outr"   r"   r#   r6     s   
zLSTMLayer.forward)	r7   r8   r9   r)   r   r=   r   r6   r>   r"   r"   r+   r#   r     s    r   c                
       s^   e Zd Z fddZdd Zdejdeejejf deejeejejf f fdd	Z  Z	S )
r   c                    s   t    || _|| _tjtd| || _tjtd| || _	tj
d| | _tj
d| | _tj
|| _|   | jjj|d|  d | jjj|d|  | d S )Nr@   rA   r   )r(   r)   r	   r
   r   r;   	Parameterrandn	weight_ih	weight_hh	LayerNormlayernorm_ilayernorm_hlayernorm_creset_parametersr?   rJ   rK   )r*   r	   r
   r   r+   r"   r#   r)     s   
 zLayerNormLSTMCell.__init__c                 C   s6   dt | j }|  D ]}tjj|| | qd S )Nr   )mathsqrtr
   
parametersr   r;   rM   rN   )r*   stdvrD   r"   r"   r#   r     s   z"LayerNormLSTMCell.reset_parametersr   r   r   c                 C   s   |\}}|  t|| j }| t|| j }|| }|dd\}}	}
}t|}t|	}	t	|
}
t|}| 
|	| ||
  }|t	| }|||ffS )Nr@   r.   )r   r   mmr   tr   r   chunksigmoidtanhr   )r*   r   r   r\   cxigateshgatesgatesingate
forgetgatecellgateoutgatecyhyr"   r"   r#   r6     s   



zLayerNormLSTMCell.forward)
r7   r8   r9   r)   r   r   r=   r   r6   r>   r"   r"   r+   r#   r     s    r   r   r   r   c                    s2    | g fddt | d D  }tj|S )Nc                    s   g | ]}  qS r"   r"   )rr   r3   r   r   r"   r#   rt     s    z%init_stacked_lstm.<locals>.<listcomp>r.   )re   r   r;   rc   )r   r   r   r   rd   r"   r   r#   init_stacked_lstm  s   &r   c                       sx   e Zd Zdedejjdedef fddZdej	de
eeej	ej	f   d	eej	eeej	ej	f  f fd
dZ  ZS )r   r   r   r   r   c                    s"   t t|   t||||| _d S r'   )r(   r   r)   r   rd   )r*   r   r   r   r   r+   r"   r#   r)     s   zStackedLSTM.__init__r   statesr   c              
   C   s   |d u r0g }| d}| jD ]}|tj||jj|j|jdtj||jj|j|jdf q|}g }|}t	| jD ]\}}	|| }
|	||
\}}|| |d7 }q9||fS )Nr.   dtypedevice)
r0   rd   rf   r   zerosr   r
   r   r   	enumerate)r*   r   r   temp_statesr   r   output_statesoutputrg   	rnn_layerr   	out_stater"   r"   r#   r6     s$   



zStackedLSTM.forward)r7   r8   r9   rV   r   r;   r<   r   r)   r=   r   r   r6   r>   r"   r"   r+   r#   r     s    "r   c                 C   s   t | tjr| tjS t | ttfstdt|  t| }t	dd | D }t
j||fdt
jd}t| D ]\}}|||dt|f< q8tj|tj|d} | S )a\  Collates the label inputs for the rnn-t prediction network.
    If `labels` is already in torch.Tensor form this is a no-op.

    Args:
        labels: A torch.Tensor List of label indexes or a torch.Tensor.
        device: Optional torch device to place the label on.

    Returns:
        A padded torch.Tensor of shape (batch, max_seq_len).
    z(`labels` should be a list or tensor not c                 s   s    | ]}t |V  qd S r'   )r   )rr   labelr"   r"   r#   	<genexpr>*  s    z label_collate.<locals>.<genexpr>r   )
fill_valuer   Nr   )r[   r   r=   typeint64listtupler   r   maxnpfullint32r   tensor)labelsr   
batch_sizemax_len
cat_labelselr"   r"   r#   label_collate  s   r   )Nr   r   NNr   r   r   )NNr'   )r   typingr   r   r   r   numpyr   r   
nemo.utilsr   rV   strrO   rb   r;   r<   r$   r%   r   rW   r   rx   r!   r   r   rc   r   r   r   r"   r"   r"   r#   <module>   s   	

`W1c	
 +
