
    Vh                     x    d dl Z d dlmc mc mZ d dlmc mc mZ d dl	m
Z
 dgZ G d dej                        Zy)    N)_quantize_weightLinearc                   ~     e Zd ZdZdZdej                  f fd	Zd Zd Z	d Z
 fdZedd	       Zed
        Z xZS )r   a  
    A dynamic quantized linear module with floating point tensor as inputs and outputs.
    We adopt the same interface as `torch.nn.Linear`, please see
    https://pytorch.org/docs/stable/nn.html#torch.nn.Linear for documentation.

    Similar to :class:`torch.nn.Linear`, attributes will be randomly
    initialized at module creation time and will be overwritten later

    Attributes:
        weight (Tensor): the non-learnable quantized weights of the module which are of
                         shape :math:`(\text{out\_features}, \text{in\_features})`.
        bias (Tensor): the non-learnable floating point bias of the module of shape
                       :math:`(\text{out\_features})`. If :attr:`bias` is ``True``,
                       the values are initialized to zero.

    Examples::

        >>> # xdoctest: +SKIP
        >>> m = nn.quantized.dynamic.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
       Tc                 :    t         |   ||||       d| _        y )Ndtyper   )super__init__version)selfin_featuresout_featuresbias_r	   	__class__s        \/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/nn/quantized/dynamic/modules/linear.pyr   zLinear.__init__)   s"    lEG
     c                    | j                   j                  t        j                  k(  r| j                  | j                  dk  r?t        j
                  j                  j                  || j                   j                         }nt        j
                  j                  j                  || j                   j                   d      }nq| j                   j                  t        j                  k(  r?t        j
                  j                  j                  || j                   j                         }nt        d      |j                  |j                        S )Nr   T)reduce_rangez.Unsupported dtype on dynamic quantized linear!)_packed_paramsr	   torchqint8r   ops	quantizedlinear_dynamicfloat16linear_dynamic_fp16RuntimeErrorto)r   xYs      r   forwardzLinear.forward1   s    $$3||#t||a'7II''66t**99 II''66t**99 7    &&%--7		##774&&55A OPPttAGG}r   c                      y)NDynamicQuantizedLinear )r   s    r   	_get_namezLinear._get_nameD   s    'r   c                    d| j                    d| j                   d| j                  j                   }| j                  j                  t        j
                  k(  r$|d| j                         j                          z  }|S )Nzin_features=z, out_features=z, dtype=z
, qscheme=)r   r   r   r	   r   r   weightqscheme)r   extra_repr_strs     r   
extra_reprzLinear.extra_reprG   sv    '(8(8'9IZIZH[[cdhdwdwd}d}c~$$3
4;;=+@+@+B*CDDNr   c           	      b    |j                  dd       }|| _        t        	|   |||d|||       y )Nr   F)getr   r
   _load_from_state_dict)
r   
state_dictprefixlocal_metadatastrictmissing_keysunexpected_keys
error_msgsr   r   s
            r   r.   zLinear._load_from_state_dictM   s@     !$$Y5%	
r   c                    t         j                  j                  t         j                  j                  j                  j
                  t         j                  j                  j                  j                  j                  j                  t         j                  j                  j                  j                  j                  g}t        |      |v s+J dt        |D cg c]  }|j                   c}      z          t        |d      sJ d       t        |      t         j                  k(  r|d   }|j"                  1|j"                  j$                  |j"                  j%                         }nddlm} |j%                         }|j*                  }|t         j,                  t         j.                  fv s
J d|         ||j$                         |t         j,                  k(  r%t1        |j$                  j3                         |      }n9|t         j.                  k(  r|j$                  j3                         }nt5        d       | |j6                  |j8                  |      }	|	j;                  ||j<                         |	S c c}w )	zCreate a dynamic quantized module from a float module or qparams_dict

        Args:
            mod (Module): a float module, either produced by torch.ao.quantization
                          utilities or provided by the user
        z<nn.quantized.dynamic.Linear.from_float only works for one ofqconfigz,Input float module must have qconfig definedr   )default_dynamic_qconfigzRThe only supported dtypes for dynamic quantized linear are qint8 and float16 got: z9Unsupported dtype specified for dynamic quantized Linear!r   )r   nnr   moduleslinearNonDynamicallyQuantizableLinearao	intrinsicfused
LinearReLUqatdynamictypestr__name__hasattrnnir7   r(   torch.ao.quantization.qconfigr8   r	   r   r   r   floatr   r   r   set_weight_biasbias)
clsmoduse_precomputed_fake_quantfloat_modules	float_modweight_observerr8   r	   qweightqlinears
             r   
from_floatzLinear.from_floatc   s    HHOOHH##CCHHKK!!))//::HHKKOO##**	
 I&	
IC1>?IY?M
 
	
& sI&V(VV&9&a&C;;"s{{'9'9'E!kk002O
 N5<<>O%%emm44 	
CCH'K	
4 	

#EKK&szz'7'7'9?KGemm#jj&&(GK  coos'7'7uE2= @s   I8c                      | |j                   |j                  |j                        }|j                         }|j                  }|j                  ||       |S )a  Create a (fbgemm/qnnpack) dynamic quantized module from a reference quantized
        module
        Args:
            ref_qlinear (Module): a reference quantized  module, either produced by
            torch.ao.quantization functions or provided by the user
        r   )r   r   weight_dtypeget_quantized_weightrK   rJ   )rL   ref_qlinearrS   rR   rK   s        r   from_referencezLinear.from_reference   sY     ##$$**

 224.r   )F)rE   
__module____qualname____doc___versionr   r   r   r"   r&   r+   r.   classmethodrT   rY   __classcell__)r   s   @r   r   r      sZ    2 H8<EKK &(
, / /b  r   )r   torch.ao.nn.intrinsicr=   r9   r>   rG   torch.ao.nn.quantizedr   nnq#torch.ao.nn.quantized.modules.utilsr   __all__r   r%   r   r   <module>re      s5     # # # # @ 
XSZZ Xr   