
    jM                         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
dS )
    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``
       r   Tin_channelsout_channelskernel_sizestridepaddingdilationgroupsbiasc	                    t                                                       ||z  dk    rt          d| d| d          t          j        d||z  z            }	t
          j                            |	 |	||||z  f          | _        |rt          j	        |f          | _
        || _        || _        || _        || _        d S )Nr   The number of input channels (-) must be divisible by the number of groups ()r   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/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/mlx/nn/layers/convolution.pyr   zConv1d.__init__"   s     	1$$@ @ @6<@ @ @  
 	!{[89::i''kV.CD ( 
 

  	2,11DI     c                     | j         j        d          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    { $ % %(9!(< % %;,Q/% %:>+% %|% %04% % k% % 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   E    It{DKt}dk
 
 T>>DIAr'   r   r   r   r   T)
__name__
__module____qualname____doc__intboolr   r3   r:   __classcell__r%   s   @r&   r   r   
   s         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   Tr   r	   r
   r   r   r   r   r   c	                    t                                                       ||z  dk    rt          d| d| d          t          d |||f          \  }}}t	          j        d||d         z  |d         z  z            }	t          j                            |	 |	|g|||z  R           | _	        |rt          j
        |f          | _        || _        || _        || _        || _        d S )Nr   r   r   r   c                 8    t          | t                    r| | fn| S N
isinstancerA   r8   s    r&   <lambda>z!Conv2d.__init__.<locals>.<lambda>   s    
1c 2 29q!ff r'   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   s1    	1$$@ @ @6<@ @ @  
 (+99&'*(
 (
$VW 	!{[^;k!nLMNNi''E+E{f/DEE ( 
 

  	2,11DI r'   c                     | j         j        d          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    { $ % %(9!(< % %;,QqS1% %<@K% %|% %04% % k% % 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   Tr   r	   r
   r   r   r   r   c                    t                                                       t          d |||f          \  }}}t          j        d||d         z  |d         z  |d         z  z            }t
          j                            | ||g||R           | _        |rt          j	        |f          | _
        || _        || _        || _        d S )Nc                 :    t          | t                    r| | | fn| S rI   rJ   rL   s    r&   rM   z!Conv3d.__init__.<locals>.<lambda>   s    :a#5#5<q!Qii1 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
 
 i'';+;{;; ( 
 

  	2,11DI r'   c                     | j         j        d          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   )r    r   r   r   r   r2   s    r&   r3   zConv3d._extra_repr   s    { $ % %(9!(< % %;,QqS1% %<@K% %|% %04% % dN% %	
r'   c                 z    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__   s:    Iadk4<OOT>>DI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 H H H HV H H HVM M M M MV M M M`C C C C CV C C C C Cr'   