o
    %ݫia                     @   s.   d Z ddlZddlZG dd dejjjZdS )zdLibrary implementing quaternion-valued max and average pooling layers.

Authors
 * Drew Wagner 2024
    Nc                       s8   e Zd ZdZ					d fdd	Z fd	d
Z  ZS )
QPooling2da  This class implements the quaternion average pooling and max pooling
    by magnitude as described in: "Geometric methods of perceptual organisation for
    computer vision", Altamirano G.

    Arguments
    ---------
    pool_type : str
        It is the type of pooling function to use ('avg','max').
    kernel_size : int
        It is the kernel size that defines the pooling dimension.
        For instance, kernel size=3,3 performs a 2D Pooling with a 3x3 kernel.
    pool_axis : tuple
        It is a list containing the axis that will be considered
        during pooling.
    ceil_mode : bool
        When True, will use ceil instead of floor to compute the output shape.
    padding : int
        It is the number of padding elements to apply.
    dilation : int
        Controls the dilation factor of pooling.
    stride : int
        It is the stride size.

    Example
    -------
    >>> pool = QPooling2d('max',(5,3))
    >>> inputs = torch.rand(10, 15, 12)
    >>> output=pool(inputs)
    >>> output.shape
    torch.Size([10, 3, 4])
          Fr   r   Nc              	      s4   t  j|||||||d | jdkrd| j_d S d S )N)	pool_axis	ceil_modepaddingdilationstridemaxT)super__init__	pool_type
pool_layerreturn_indices)selfr   kernel_sizer   r   r   r	   r
   	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/nnet/quaternion_networks/q_pooling.pyr   -   s   


zQPooling2d.__init__c           
         s  t j|ddd\}}}}| jdkr*t |}t |}t |}t |}n| jdkr|d |d  |d  |d  }|ddd| jd d| jd	 | jd	 | jd }| 	|\}}|| jd | jd	 d| jd d| jd	 dd}|
 }	|
 |	 |j}|
 |	 |j}|
 |	 |j}|
 |	 |j}t j||||fddS )
zPerforms 2d pooling to the input tensor.

        Arguments
        ---------
        x : torch.Tensor
            It represents a tensor for a mini-batch.

        Returns
        -------
        The pooled tensor.
           )dimavgr   r   r   r   )torchchunkr   r   forward	unsqueeze	transposer   squeezer   flattenreshapeshapeconcat)
r   xx_rx_ix_jx_km_idxidx_flatr   r   r   r   D   s:   

 	
zQPooling2d.forward)r   Fr   r   N)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    $r   )r2   r   speechbrainsbnnetpooling	Pooling2dr   r   r   r   r   <module>   s    