
    GJjw                     r    d dl Z d dlmZ d dlmZ d dlmZ  G d de      Z G d de      Z	 G d d	e      Z
y)
    N)Union)Modulec                   \     e Zd ZdZ	 	 	 	 	 ddededededededed	ef fd
Zd Zd Z xZ	S )Conv1da  Applies a 1-dimensional convolution over the multi-channel input sequence.

    The channels are expected to be last i.e. the input shape should be ``NLC`` where:

    * ``N`` is the batch dimension
    * ``L`` is the sequence length
    * ``C`` is the number of input channels

    Args:
        in_channels (int): The number of input channels
        out_channels (int): The number of output channels
        kernel_size (int): The size of the convolution filters
        stride (int, optional): The stride when applying the filter.
            Default: ``1``.
        padding (int, optional): How many positions to 0-pad the input with.
            Default: ``0``.
        dilation (int, optional): The dilation of the convolution.
        groups (int, optional): The number of groups for the convolution.
            Default: ``1``.
        bias (bool, optional): If ``True`` add a learnable bias to the output.
            Default: ``True``
    in_channelsout_channelskernel_sizestridepaddingdilationgroupsbiasc	                 Z   t         
|           ||z  dk7  rt        d| d| d      t        j                  d||z  z        }	t
        j                  j                  |	 |	||||z  f      | _        |rt        j                  |f      | _
        || _        || _        || _        || _        y )Nr   The number of input channels (-) must be divisible by the number of groups ()   lowhighshape)super__init__
ValueErrormathsqrtmxrandomuniformweightzerosr   r   r   r
   r   selfr   r   r	   r
   r   r   r   r   scale	__class__s             c/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx/nn/layers/convolution.pyr   zConv1d.__init__"   s     	1$0 >66<XQ@ 
 		!{[89:ii''kV.CD ( 

 ,1DI     c                 ,   | j                   j                  d   | j                  z   d| j                   j                  d    d| j                   j                  d    d| j                   d| j                   d| j
                   d	| j                   d
d| v  S )N, r   , kernel_size=r   	, stride=
, padding=, dilation=	, groups=, bias=r   r    r   r   r
   r   r   r#   s    r&   _extra_reprzConv1d._extra_reprC   s    {{  $t{{232dkk6G6G6J5K L;;,,Q/0	$++ G||nK ?kk] #dN#	%	
r'   c                     t        j                  || j                  | j                  | j                  | j
                  | j                        }d| v r|| j                  z   }|S Nr   )r   conv1dr    r
   r   r   r   r   r#   xys      r&   __call__zConv1d.__call__L   L    IIt{{DKKt}}dkk
 T>DIIAr'   r   r   r   r   T)
__name__
__module____qualname____doc__intboolr   r3   r:   __classcell__r%   s   @r&   r   r   
   sw    8   	
     B
r'   r   c                        e Zd ZdZ	 	 	 	 	 ddededeeef   deeef   deeef   deeef   ded	ef fd
Zd Z	d Z
 xZS )Conv2da  Applies a 2-dimensional convolution over the multi-channel input image.

    The channels are expected to be last i.e. the input shape should be ``NHWC`` where:

    * ``N`` is the batch dimension
    * ``H`` is the input image height
    * ``W`` is the input image width
    * ``C`` is the number of input channels

    Args:
        in_channels (int): The number of input channels.
        out_channels (int): The number of output channels.
        kernel_size (int or tuple): The size of the convolution filters.
        stride (int or tuple, optional): The size of the stride when
            applying the filter. Default: ``1``.
        padding (int or tuple, optional): How many positions to 0-pad
            the input with. Default: ``0``.
        dilation (int or tuple, optional): The dilation of the convolution.
        groups (int, optional): The number of groups for the convolution.
            Default: ``1``.
        bias (bool, optional): If ``True`` add a learnable bias to the
            output. Default: ``True``
    r   r   r	   r
   r   r   r   r   c	                    t         
|           ||z  dk7  rt        d| d| d      t        d |||f      \  }}}t	        j
                  d||d   z  |d   z  z        }	t        j                  j                  |	 |	|g|||z        | _	        |rt        j                  |f      | _        || _        || _        || _        || _        y )Nr   r   r   r   c                 .    t        | t              r| | fS | S N
isinstancerA   r8   s    r&   <lambda>z!Conv2d.__init__.<locals>.<lambda>   s    
1c 2q!f99r'   r   r   )r   r   r   mapr   r   r   r   r   r    r!   r   r   r
   r   r   r"   s             r&   r   zConv2d.__init__n   s     	1$0 >66<XQ@ 
 (+9&'*(
$VW 		!{[^;k!nLMNii''E+E{f/DE ( 

 ,1DI r'   c                 ,   | j                   j                  d   | j                  z   d| j                   j                  d    d| j                   j                  dd  d| j                   d| j                   d	| j
                   d
| j                   dd| v  S )Nr)   r*   r   r+   r      r,   r-   r.   r/   r0   r   r1   r2   s    r&   r3   zConv2d._extra_repr   s    {{  $t{{232dkk6G6G6J5K L;;,,Qq12)DKK= I||nK ?kk] #dN#	%	
r'   c                     t        j                  || j                  | j                  | j                  | j
                  | j                        }d| v r|| j                  z   }|S r5   )r   conv2dr    r
   r   r   r   r   r7   s      r&   r:   zConv2d.__call__   r;   r'   r<   r=   r>   r?   r@   rA   r   tuplerB   r   r3   r:   rC   rD   s   @r&   rF   rF   U   s    : %&%&&'## # 3:&	#
 c5j!# sEz"# U
## # #J
r'   rF   c                   ~     e Zd ZdZ	 	 	 	 ddededeeef   deeef   deeef   deeef   def fd	Zd
 Z	d Z
 xZS )Conv3da  Applies a 3-dimensional convolution over the multi-channel input image.

    The channels are expected to be last i.e. the input shape should be ``NDHWC`` where:

    * ``N`` is the batch dimension
    * ``D`` is the input image depth
    * ``H`` is the input image height
    * ``W`` is the input image width
    * ``C`` is the number of input channels

    Args:
        in_channels (int): The number of input channels.
        out_channels (int): The number of output channels.
        kernel_size (int or tuple): The size of the convolution filters.
        stride (int or tuple, optional): The size of the stride when
            applying the filter. Default: ``1``.
        dilation (int or tuple, optional): The dilation of the convolution.
        padding (int or tuple, optional): How many positions to 0-pad
            the input with. Default: ``0``.
        bias (bool, optional): If ``True`` add a learnable bias to the
            output. Default: ``True``
    r   r   r	   r
   r   r   r   c                 ^   t         	|           t        d |||f      \  }}}t        j                  d||d   z  |d   z  |d   z  z        }t
        j                  j                  | ||g||      | _        |rt        j                  |f      | _
        || _        || _        || _        y )Nc                 0    t        | t              r| | | fS | S rI   rJ   rL   s    r&   rM   z!Conv3d.__init__.<locals>.<lambda>   s    :a#5q!Qi<1<r'   r   r      r   )r   r   rN   r   r   r   r   r   r    r!   r   r   r
   r   )
r#   r   r   r	   r
   r   r   r   r$   r%   s
            r&   r   zConv3d.__init__   s     	'*<&'*(
$VW 		{1~-A>QOP
 ii'';+;{; ( 

 ,1DI r'   c                    | j                   j                  d   | j                  z   d| j                   j                  d    d| j                   j                  dd  d| j                   d| j                   d	| j
                   d
d| v  S )Nr)   r*   r   r+   r      r,   r-   r.   r0   r   r1   r2   s    r&   r3   zConv3d._extra_repr   s    {{  $t{{232dkk6G6G6J5K L;;,,Qq12)DKK= I||nK ?dN#%	
r'   c                     t        j                  || j                  | j                  | j                  | j
                        }d| v r|| j                  z   }|S r5   )r   conv3dr    r
   r   r   r   r7   s      r&   r:   zConv3d.__call__   sA    IIadkk4<<OT>DIIAr'   )r   r   r   TrS   rD   s   @r&   rV   rV      s    8 %&%&&'!! ! 3:&	!
 c5j!! sEz"! U
#! !>
r'   rV   )r   typingr   mlx.corecorer   mlx.nn.layers.baser   r   rF   rV    r'   r&   <module>rc      s?       %HV HVMV M`CV Cr'   