
    j                         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dS )    N)AnyOptional)Module)QQLinearQuantizedLinearc                   T     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                 H    t                                                       d S N)super__init__)selfr
   r   	__class__s      ^/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/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      "( rx        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``.
    T
input_dimsoutput_dimsbiasr   Nc                 $   t                                                       t          j        d|z            }t          j                            | |||f          | _        |r+t          j                            | ||f          | _        d S d S Ng      ?)lowhighshape	r   r   mathsqrtr   randomuniformweightr&   )r   r$   r%   r&   scaler   s        r   r   zLinear.__init__0   s    	#
*++i''
+ ( 
 

  		))F"n *  DIII	 	r   c                 \    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?   s<    nT[.q1nnARSTAUnn^dhl^lnnnr   r   c                 ~    d| v r(t          j        | d         || d         j                  }n|| d         j        z  }|S )Nr&   r1   )r   addmmTr   s     r   r   zLinear.__call__B   sA    T>>fq$x.*:;;AADN$$Ar   affineF
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   sj    :  	F--- jcgjjj   'j$EEE*4T4HHHr   T)NNr<   F)r   r   r   r   intboolr   strr8   r   r   r   r   rG   r    r!   s   @r   r#   r#      s        * 3 S  PT      oS o o o o"( rx     %)"$#I #ISM#I sm#I 	#I
 #I #I #I #I #I #I #I #Ir   r#   c                   z     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``.
    Tinput1_dimsinput2_dimsr%   r&   r   Nc                 &   t                                                       t          j        d|z            }t          j                            | ||||f          | _        |r+t          j                            | ||f          | _        d S d S r(   r,   )r   rN   rO   r%   r&   r2   r   s         r   r   zBilinear.__init__   s     		#+,,i''[9 ( 
 

  		))F"n *  DIII	 	r   c           	      B    | 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   sQ    )S#%3 % %c % % % %dN% %	
r   x1x2c                    | j         j        \  }}}|j        d d         }|                    d|          }|                    dd|          }| j                             ||z  |          }||j        z  }|                    d||                              dd          }||z  }|                    d          } |j        g ||R  }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## Kc	3//HIIb#s##,,R44FIIaLL AI#v#s### T>>DIAr   rH   )r   r   r   r   rI   rJ   r   rK   r8   r   r   r   r    r!   s   @r   rM   rM   o   s         0 RV -0?BJN	     "
S 
 
 
 
28  bh        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      s                          % % % % % % = = = = = = = =    v   RI RI RI RI RIV RI RI RIjE E E E Ev E E E E Er   