U
    •Ê¹j)  ã                   @   sÊ   d dl Z d dlmZ d dlZd dlmZ d dlmZmZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddddgZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deeƒZdS )é    N)ÚAny)ÚTensor)Ú	ParameterÚUninitializedParameteré   )Ú
functional)Úinité   )ÚModule)ÚLazyModuleMixinÚBilinearÚIdentityÚ
LazyLinearÚLinearc                       s:   e Zd ZdZeeddœ‡ fdd„Zeedœdd„Z‡  ZS )	r   a  A placeholder identity operator that is argument-insensitive.

    Args:
        args: any argument (unused)
        kwargs: any keyword argument (unused)

    Shape:
        - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
        - Output: :math:`(*)`, same shape as the input.

    Examples::

        >>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 20])

    N)ÚargsÚkwargsÚreturnc                    s   t ƒ  ¡  d S ©N©ÚsuperÚ__init__)Úselfr   r   ©Ú	__class__© úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/nn/modules/linear.pyr   )   s    zIdentity.__init__©Úinputr   c                 C   s   |S r   r   ©r   r   r   r   r   Úforward,   s    zIdentity.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú__classcell__r   r   r   r   r      s   c                       s|   e Zd ZU dZddgZeed< eed< eed< deeeddœ‡ fdd	„Z	dd
œdd„Z
eedœdd„Zed
œdd„Z‡  ZS )r   a&  Applies a linear transformation to the incoming data: :math:`y = xA^T + b`

    This module supports :ref:`TensorFloat32<tf32_on_ampere>`.

    On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.

    Args:
        in_features: size of each input sample
        out_features: size of each output sample
        bias: If set to ``False``, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input: :math:`(*, H_{in})` where :math:`*` means any number of
          dimensions including none and :math:`H_{in} = \text{in\_features}`.
        - Output: :math:`(*, H_{out})` where all but the last dimension
          are the same shape as the input and :math:`H_{out} = \text{out\_features}`.

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in\_features})`. The values are
            initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
                :math:`k = \frac{1}{\text{in\_features}}`

    Examples::

        >>> m = nn.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
    Úin_featuresÚout_featuresÚweightTN©r%   r&   Úbiasr   c                    sj   ||dœ}t ƒ  ¡  || _|| _ttj||ff|Žƒ| _|rRttj|f|Žƒ| _n|  	dd ¡ |  
¡  d S ©N©ÚdeviceÚdtyper)   )r   r   r%   r&   r   ÚtorchÚemptyr'   r)   Úregister_parameterÚreset_parameters)r   r%   r&   r)   r,   r-   Úfactory_kwargsr   r   r   r   Z   s    

zLinear.__init__©r   c                 C   s`   t j| jt d¡d� | jd k	r\t  | j¡\}}|dkrFdt |¡ nd}t  | j| |¡ d S )Né   )Úar   r	   )r   Zkaiming_uniform_r'   ÚmathÚsqrtr)   Z_calculate_fan_in_and_fan_outÚuniform_)r   Zfan_inÚ_Úboundr   r   r   r1   g   s
    
zLinear.reset_parametersr   c                 C   s   t  || j| j¡S r   )ÚFZlinearr'   r)   r   r   r   r   r   q   s    zLinear.forwardc                 C   s    d| j › d| j› d| jd k	› �S )Nzin_features=z, out_features=z, bias=)r%   r&   r)   ©r   r   r   r   Ú
extra_reprt   s    zLinear.extra_repr)TNN©r    r!   r"   r#   Z__constants__ÚintÚ__annotations__r   Úboolr   r1   r   Ústrr=   r$   r   r   r   r   r   0   s   
$    ÿÿ
c                       s*   e Zd Zdeeeddœ‡ fdd„Z‡  ZS )ÚNonDynamicallyQuantizableLinearTNr(   c                    s   t ƒ j|||||d� d S )N)r)   r,   r-   r   )r   r%   r&   r)   r,   r-   r   r   r   r   ~   s     ÿz(NonDynamicallyQuantizableLinear.__init__)TNN)r    r!   r"   r?   rA   r   r$   r   r   r   r   rC   }   s       ÿÿrC   c                       sŠ   e Zd ZU dZdddgZeed< eed< eed< eed< deeeeddœ‡ fd	d
„Z	ddœdd„Z
eeedœdd„Zedœdd„Z‡  ZS )r   a¡  Applies a bilinear transformation to the incoming data:
    :math:`y = x_1^T A x_2 + b`

    Args:
        in1_features: size of each first input sample
        in2_features: size of each second input sample
        out_features: size of each output sample
        bias: If set to False, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input1: :math:`(*, H_{in1})` where :math:`H_{in1}=\text{in1\_features}` and
          :math:`*` means any number of additional dimensions including none. All but the last dimension
          of the inputs should be the same.
        - Input2: :math:`(*, H_{in2})` where :math:`H_{in2}=\text{in2\_features}`.
        - Output: :math:`(*, H_{out})` where :math:`H_{out}=\text{out\_features}`
          and all but the last dimension are the same shape as the input.

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in1\_features}, \text{in2\_features})`.
            The values are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in1\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
                :math:`k = \frac{1}{\text{in1\_features}}`

    Examples::

        >>> m = nn.Bilinear(20, 30, 40)
        >>> input1 = torch.randn(128, 20)
        >>> input2 = torch.randn(128, 30)
        >>> output = m(input1, input2)
        >>> print(output.size())
        torch.Size([128, 40])
    Úin1_featuresÚin2_featuresr&   r'   TN)rD   rE   r&   r)   r   c                    sr   ||dœ}t ƒ  ¡  || _|| _|| _ttj|||ff|Žƒ| _|rZttj|f|Žƒ| _	n|  
dd ¡ |  ¡  d S r*   )r   r   rD   rE   r&   r   r.   r/   r'   r)   r0   r1   )r   rD   rE   r&   r)   r,   r-   r2   r   r   r   r   °   s    

zBilinear.__init__r3   c                 C   sH   dt  | j d¡¡ }t | j| |¡ | jd k	rDt | j| |¡ d S )Nr	   )r6   r7   r'   Úsizer   r8   r)   )r   r:   r   r   r   r1   ¿   s    
zBilinear.reset_parameters)Úinput1Úinput2r   c                 C   s   t  ||| j| j¡S r   )r;   Zbilinearr'   r)   )r   rG   rH   r   r   r   r   Å   s    zBilinear.forwardc                 C   s   d  | j| j| j| jd k	¡S )Nz:in1_features={}, in2_features={}, out_features={}, bias={})ÚformatrD   rE   r&   r)   r<   r   r   r   r=   È   s       ÿzBilinear.extra_repr)TNNr>   r   r   r   r   r   „   s   
%
    ÿ
ÿc                       sb   e Zd ZU dZeZeed< eed< dee	ddœ‡ fdd„Z
dd	œ‡ fd
d„Zdd	œdd„Z‡  ZS )r   a  A :class:`torch.nn.Linear` module where `in_features` is inferred.

    In this module, the `weight` and `bias` are of :class:`torch.nn.UninitializedParameter`
    class. They will be initialized after the first call to ``forward`` is done and the
    module will become a regular :class:`torch.nn.Linear` module. The ``in_features`` argument
    of the :class:`Linear` is inferred from the ``input.shape[-1]``.

    Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
    on lazy modules and their limitations.

    Args:
        out_features: size of each output sample
        bias: If set to ``False``, the layer will not learn an additive bias.
            Default: ``True``

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in\_features})`. The values are
            initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
                :math:`k = \frac{1}{\text{in\_features}}`


    r'   r)   TN)r&   r)   r   c                    s@   ||dœ}t ƒ  ddd¡ tf |Ž| _|| _|r<tf |Ž| _d S )Nr+   r   F)r   r   r   r'   r&   r)   )r   r&   r)   r,   r-   r2   r   r   r   r   ï   s    
zLazyLinear.__init__r3   c                    s    |   ¡ s| jdkrtƒ  ¡  d S )Nr   )Úhas_uninitialized_paramsr%   r   r1   r<   r   r   r   r1   ú   s    zLazyLinear.reset_parametersc              	   C   sb   |   ¡ r^t ¡ �H |jd | _| j | j| jf¡ | jd k	rL| j | jf¡ |  	¡  W 5 Q R X d S )Néÿÿÿÿ)
rJ   r.   Zno_gradÚshaper%   r'   Zmaterializer&   r)   r1   r   r   r   r   Úinitialize_parametersþ   s    

z LazyLinear.initialize_parameters)TNN)r    r!   r"   r#   r   Zcls_to_becomer   r@   r?   rA   r   r1   rM   r$   r   r   r   r   r   Î   s   
    ÿÿ)r6   Útypingr   r.   r   Ztorch.nn.parameterr   r   Ú r   r;   r   Úmoduler
   Zlazyr   Ú__all__r   r   rC   r   r   r   r   r   r   Ú<module>   s$   üMJ