
    GJj                         d dl Z d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ  G d de      Z G d de      Z G d	 d
e      Zy)    N)AnyOptional)Module)QQLinearQuantizedLinearc                   h     e Zd ZdZdededdf fdZdej                  dej                  fdZ xZ	S )	IdentityzA placeholder identity operator that is argument-insensitive.

    Args:
        args: any argument (unused)
        kwargs: any keyword argument (unused)
    argskwargsreturnNc                 "    t         |           y N)super__init__)selfr
   r   	__class__s      ^/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx/nn/layers/linear.pyr   zIdentity.__init__   s        xc                     |S r    r   r   s     r   __call__zIdentity.__call__   s    r   )
__name__
__module____qualname____doc__r   r   mxarrayr   __classcell__r   s   @r   r	   r	      s=    c S T "(( rxx r   r	   c            	            e Zd ZdZddedededdf fdZdefdZd	e	j                  de	j                  fd
Z	 	 	 	 ddee   dee   dedefdZ xZS )Lineara  Applies an affine transformation to the input.

    Concretely:

    .. math::

        y = x W^\top + b

    where:
    where :math:`W` has shape ``[output_dims, input_dims]`` and :math:`b` has shape ``[output_dims]``.

    The values are initialized from the uniform distribution :math:`\mathcal{U}(-{k}, {k})`,
    where :math:`k = \frac{1}{\sqrt{D_i}}` and :math:`D_i` is equal to ``input_dims``.

    Args:
        input_dims (int): The dimensionality of the input features
        output_dims (int): The dimensionality of the output features
        bias (bool, optional): If set to ``False`` then the layer will
          not use a bias. Default is ``True``.
    
input_dimsoutput_dimsbiasr   Nc                     t         |           t        j                  d|z        }t        j
                  j                  | |||f      | _        |r*t        j
                  j                  | ||f      | _        y y Ng      ?)lowhighshape	r   r   mathsqrtr   randomuniformweightr&   )r   r$   r%   r&   scaler   s        r   r   zLinear.__init__0   s~    		#
*+ii''
+ ( 

 		))F"n * DI r   c                 x    d| j                   j                  d    d| j                   j                  d    dd| v  S )Nzinput_dims=   , output_dims=r   , bias=r&   r1   r+   )r   s    r   _extra_reprzLinear._extra_repr?   sE    T[[..q12.ARARSTAU@VV]^dhl^l]mnnr   r   c                     d| v r)t        j                  | d   || d   j                        }|S || d   j                  z  }|S )Nr&   r1   )r   addmmTr   s     r   r   zLinear.__call__B   sK    T>fq$x.*:*:;A  DN$$$Ar   
group_sizebitsmodequantize_inputc                     |r+|dvrt        d| d      t        j                  | |||      S t        j                  | |||      S )a  Return a quantized approximation of this layer.

        If ``quantize_input`` is ``False``, returns a :obj:`QuantizedLinear`
        (weights are quantized). If ``quantize_input`` is ``True``, returns
        a :obj:`QQLinear` (weights and activations are quantized).

        Args:
            group_size (Optional[int]): The quantization group size (see
                :func:`mlx.core.quantize`). Default: ``None``.
            bits (Optional[int]): The number of bits per parameter (see
                :func:`mlx.core.quantize`). Default: ``None``.
            mode (str): The quantization method to use (see
                :func:`mlx.core.quantize`). Default: ``"affine"``.
            quantize_input (bool): Whether to quantize input. Default: ``False``.

        Returns:
            QuantizedLinear or QQLinear: A quantized version of this layer.

        Notes:
            Quantized input is only supported for ``"nvfp4"`` and ``"mxfp8"``
            modes.
        )nvfp4mxfp8zLQuantized activations are only supported for 'nvfp4' and 'mxfp8' modes, got .)
ValueErrorr   from_linearr   )r   r<   r=   r>   r?   s        r   to_quantizedzLinear.to_quantizedI   s\    : -- bcgbhhij  ''j$EE**4T4HHr   T)NNaffineF)r   r   r   r   intboolr   strr8   r   r   r   r   rF   r    r!   s   @r   r#   r#      s    *3 S  PT oS o"(( rxx  %)"$#ISM#I sm#I 	#I
 #Ir   r#   c                        e Zd ZdZ	 ddededededdf
 fdZdefd	Zd
e	j                  de	j                  de	j                  fdZ xZS )Bilineara!  Applies a bilinear transformation to the inputs.

    Concretely:

    .. math::

        y_i = x_1^\top W_i x_2 + b_i

    where:
    :math:`W` has shape ``[output_dims, input1_dims, input2_dims]``, :math:`b` has shape ``[output_dims ]``,
    and :math:`i` indexes the output dimension.

    The values are initialized from the uniform distribution :math:`\mathcal{U}(-{k}, {k})`,
    where :math:`k = \frac{1}{\sqrt{D_1}}` and :math:`D_1` is ``input1_dims``.

    Args:
        input1_dims (int): The dimensionality of the input1 features
        input2_dims (int): The dimensionality of the input2 features
        output_dims (int): The dimensionality of the output features
        bias (bool, optional): If set to ``False`` then the layer will
          not use a bias. Default is ``True``.
    input1_dimsinput2_dimsr%   r&   r   Nc                     t         |           t        j                  d|z        }t        j
                  j                  | ||||f      | _        |r*t        j
                  j                  | ||f      | _        y y r(   r,   )r   rN   rO   r%   r&   r2   r   s         r   r   zBilinear.__init__   s     			#+,ii''[9 ( 

 		))F"n * DI r   c           	      V    | j                   j                  \  }}}d| d| d| dd| v  S )Nzinput1_dims=z, input2_dims=r5   r6   r&   r7   )r   outin2in1s       r   r8   zBilinear._extra_repr   sD    ))S#3%~cU. FdN#%	
r   x1x2c                    | j                   j                  \  }}}|j                  d d }|j                  d|      }|j                  dd|      }| j                   j                  ||z  |      }||j                  z  }|j                  d||      j	                  dd      }||z  }|j                  d      } |j                  g || }d| v r|| j                  z   }|S )Nr4   r&   )r1   r+   reshaper;   swapaxessqueezer&   )	r   rU   rV   rR   rS   rT   xshapewys	            r   r   zBilinear.__call__   s    ))S##2ZZC ZZAs# KKc	3/HIIb#s#,,R4FIIaL AII#v#s# T>DIIAr   rG   )r   r   r   r   rI   rJ   r   rK   r8   r   r   r   r    r!   s   @r   rM   rM   o   sn    0 RV-0?BJN	"
S 
288  bhh r   rM   )r-   typingr   r   mlx.corecorer   mlx.nn.layers.baser   mlx.nn.layers.quantizedr   r   r	   r#   rM   r   r   r   <module>re      sA        % =v RIV RIjEv Er   