o
    ei                     @   sB   d Z ddlZddlmZ ddlmZ eeZG dd dejZ	dS )zHLibrary implementing transducer_joint.

Author
    Abdelwahab HEBA 2020
    N)
get_loggerc                       s<   e Zd ZdZddejjf fdd	Zdd Zdd	 Z	  Z
S )
Transducer_jointa  Computes joint tensor between Transcription network (TN) & Prediction network (PN)

    Arguments
    ---------
    joint_network : torch.class (neural network modules)
        if joint == "concat", we call this network after the concatenation of TN and PN
        if None, we don't use this network.
    joint : str
        join the two tensors by ("sum",or "concat") option.
    nonlinearity : torch class
        Activation function used after the joint between TN and PN
        Type of nonlinearity (tanh, relu).

    Example
    -------
    >>> from speechbrain.nnet.transducer.transducer_joint import Transducer_joint
    >>> from speechbrain.nnet.linear import Linear
    >>> input_TN = torch.rand(8, 200, 1, 40)
    >>> input_PN = torch.rand(8, 1, 12, 40)
    >>> joint_network = Linear(input_size=80, n_neurons=80)
    >>> TJoint = Transducer_joint(joint_network, joint="concat")
    >>> output = TJoint(input_TN, input_PN)
    >>> output.shape
    torch.Size([8, 200, 12, 80])
    Nsumc                    s"   t    || _|| _| | _d S )N)super__init__joint_networkjointnonlinearity)selfr   r   r	   	__class__ j/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/nnet/transducer/transducer_joint.pyr   *   s   
zTransducer_joint.__init__c                 C   s   |  | dS )z
        Arguments
        ---------
        first_input : tensor
            A first input used for initializing the parameters.
        N)r   )r
   first_inputr   r   r   init_params2   s   zTransducer_joint.init_paramsc                 C   s6  t |jt |jkrtdt |jdks t |jdks td| jdkr)|| }| jdkrt |jdkr|t |jd }|}|}dd t| d	d
 | d	d
 D }|t||jd
 g }|t||jd
 g }tj	||f|d}nt |jdkrtj	||fdd}| j
d	ur| 
|}| |S )a,  Returns the fusion of inputs tensors.

        Arguments
        ---------
        input_TN : torch.Tensor
           Input from Transcription Network.
        input_PN : torch.Tensor
           Input from Prediction Network.

        Returns
        -------
        fusion of input tensors.
        z"Arg 1 and 2 must be have same size      z(Tensors 1 and 2 must have dim=1 or dim=4r   concatc                 S   s   g | ]	\}}t ||qS r   )max).0ijr   r   r   
<listcomp>W   s    z,Transducer_joint.forward.<locals>.<listcomp>N)dimr   )lenshape
ValueErrorr   zipsizeexpandtorchSizecatr   r	   )r
   input_TNinput_PNr   r   xsymatszr   r   r   forward;   s,   

 


zTransducer_joint.forward)__name__
__module____qualname____doc__r!   nn	LeakyReLUr   r   r)   __classcell__r   r   r   r   r      s    
	r   )
r-   r!   torch.nnr.   speechbrain.utils.loggerr   r*   loggerModuler   r   r   r   r   <module>   s    