U
    9%etb                     @   sb  d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZmZ ddlZddlmZ ddlmZ ddlmZ eeeef eeeeef f e	f Zd	e_e	Zee
ee
e
f ee
ee
e
f f e	f Zd	e_G d
d dZejjejjejjejjejj ejj!ejj"ejj#ejj$ejj%ejj&ejj'ejj(ejj)ejj*hZ+ejj,j-ejj,j.ejj,j/ejj,j0ejj,j1ejj,j2ejj,j3ejj,j4ejj,j5ejj,j6ejj,j7ejj,j8ejj,j9ejj,j:ejj,j;ejj,j<ejj,j=ejj,j>ejj,j?ej@ejAej?ejBejCejDejEejFejGhZHejIddddddddddddddddddddd d!d"d#hZJd$d% ZKd&d' ZLd(d) ZMd*d+ ZNe	eOe	d,d-d.ZPd/d0 ZQd1d2 ZRd3d4 ZSd5d6 ZTd7d8 ZUd9d: ZVd;d< ZWd=d> ZXd?d@ ZYdAdB ZZdCdD Z[e\dEdFdGZ]dHdI Z^dJdK Z_ej`ej`e\dLdMdNZaebebe\ejce\eebebf dOdPdQZddRdS ZedTdU ZfejjgejjgeeO dVdWdXZhe
eeOe	f eeOe	f dYdZd[Zie
d\d]d^d_Zje
eeOe	f d\d`dadbZkebebddcdddeZlejmfej`ej`ebebejcej`e\ejneej`ej`f df	dgdhZoe
ebd]didjZpejjgee	dkf eeOee	dkf f dldmdnZqdodpdd%d'd)d+d.d2d4d6d8d:d<d>d@dBdDdGdIdKdNdQdUdnd0dhdegZrdS )qz?
Utils shared by different modes of quantization (eager/graph)
    N)OrderedDict)getfullargspec	signature)AnyCallableDictOptionalTupleUnion)	QuantType)Node)is_parametrizedztorch.ao.quantization.utilsc                   @   s   e Zd ZdZdS )MatchAllNodezo A node pattern that matches all nodes, used in defining
    fusion patterns in FX Graph Mode Quantization
    N)__name__
__module____qualname____doc__ r   r   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/ao/quantization/utils.pyr   "   s   r   reluZrelu_
contiguousdetachZdetach_hardsigmoidZhardsigmoid_Zpermuterepeatrepeat_interleaveZreshapeZresize_shapesigmoidZsigmoid_sizesqueezeZsqueeze_tanhZtanh_	transpose	unsqueezeZ
unsqueeze_viewc                 C   sR   | j dko| jtk}| j dko&| jtk}| j dkoFt|t| j tk}|||fS )NZcall_functionZcall_methodZcall_module)optarget	func_listmethod_listtypestrmodule_type_list)nodemodulesZis_call_functionZis_call_methodZis_call_moduler   r   r   
check_nodet   s     r,   c                 C   s   |   }|| |S N)copyupdate)Zdefault_dictZadditional_dictdr   r   r   get_combined_dict{   s    
r1   c                 C   s   | t jkp| t jkS r-   )torchper_tensor_affineper_tensor_symmetricqschemer   r   r   is_per_tensor   s    
r7   c                 C   s   | t jt jt jfkS r-   )r2   per_channel_affine per_channel_affine_float_qparamsper_channel_symmetricr5   r   r   r   is_per_channel   s    r;   )objfqnreturnc                 C   s   t t|d| S )zO
    Given an obj and a fqn such as "foo.bar.baz", returns gm.foo.bar.baz.
    .)	functoolsreducegetattrsplit)r<   r=   r   r   r   getattr_from_fqn   s    rD   c              
   C   sH   t jt jt jt jt jt jt jt jt jt ji}| |ks@t	d|  ||  S )NzUnsupported dtype: )
r2   quint8uint8qint8Zint8qint32Zint32quint4x2Zquint2x4AssertionError)ZqdtypeZDTYPE_MAPPINGr   r   r   to_underlying_dtype   s         rK   c                 C   s   ddl m} t| dd }| j}||d}|r6t| |r@d |dS t|rPtj}n2t|rt|tj	krhtj
}| j|d< ntd| ||d< |  \}}||d< ||d< t| d	r| j|d	< t| d
r| j|d
< |S )Nr   )PlaceholderObserverr6   )r6   dtypeZaxiszUnrecognized qscheme: scale
zero_point	quant_min	quant_max)Ztorch.ao.quantization.observerrL   rB   rM   
isinstancer7   r2   r3   r;   r:   r8   Zch_axisRuntimeErrorZcalculate_qparamshasattrrP   rQ   )Zobserver_or_fake_quantrL   r6   rM   ZqparamsrN   rO   r   r   r   get_qparam_dict   s,    






rU   c                 C   sD   t |}||i }t| |ks8tdt|  d| |t|  S )a   Get the observed/quantized custom module class that we need
    to swap `custom_module` to
    Input:
        custom_module: input, can be an instance of either a float or observed custom module
        custom_module_class_mapping: the float to observed or observed to quantized custom module class mapping
        qconfig: qconfig configured for the custom module

    Output:
        corresponding observed/quantized custom module class for input custom module instance
    z5did not find corresponding observed module class for z in mapping: )get_quant_typegetr'   rJ   )Zcustom_moduleZcustom_module_class_mappingqconfigZ
quant_typeZclass_mappingr   r   r   get_swapped_custom_module_class   s    $rY   c                 C   s   | d k	st |  }|jS r-   )rJ   
activationrM   )rX   rZ   r   r   r   activation_dtype   s    r[   c                 C   s   | d k	st |  }|jS r-   )rJ   weightrM   )rX   r\   r   r   r   weight_dtype   s    r]   c                 C   s&   t | tjtjtjtjfko$t|  S )z Given a qconfig, decide if the activation needs to be
    quantized or not, this includes quantizing to quint8, qint8 and qint32 and float16
    )r[   r2   rE   rG   rH   float16#activation_is_dynamically_quantizedrX   r   r   r   "activation_is_statically_quantized   s    ra   c                 C   s   t | \}}}|S )z Given a qconfig, decide if the activation needs to be
    dynamically quantized or not, this includes dynamically quantizing to
    quint8, qint8 and float16
    )get_qconfig_dtypes)rX   r[   _activation_is_dynamicr   r   r   r_      s    r_   c                 C   s   t | tjtjfkS )z Given a qconfig, decide if the activation needs to be
    quantized to int8 or not, this includes quantizing to quint8, qint8
    )r[   r2   rE   rG   r`   r   r   r   activation_is_int8_quantized   s    re   c                 C   s   t | tjkS )zY Given a qconfig, decide if the activation needs to be
    quantized to int32 or not
    )r[   r2   rH   r`   r   r   r   activation_is_int32_quantized   s    rf   c                 C   s   t | tjtjtjtjfkS )zL Given a qconfig, decide if the weight needs to be
    quantized or not
    )r]   r2   rE   rG   r^   rI   r`   r   r   r   weight_is_quantized   s    rg   c                 C   s   t | tjtjfkS )zW Given a qconfig, decide if the weight needs to be statically
    quantized or not
    )r]   r2   rE   rG   r`   r   r   r   weight_is_statically_quantized   s    rh   )r>   c                 C   s&   t | \}}}|tjko$|tjko$|S )zU Given a qconfig, returns True if this op is using int8 dynamic
    quantization
    )rb   r2   rE   rG   )rX   r[   r]   rd   r   r   r    op_is_int8_dynamically_quantized  s    
ri   c                 C   s6   | dk	st |  }|  }t|dd}|j|j|fS )zh returns the qconfig tuple for qconfig:
    (activation_dtype, weight_dtype, activation_is_dynamic)
    N
is_dynamicF)rJ   rZ   r\   rB   rM   )rX   rZ   r\   Zact_is_dynamicr   r   r   rb     s
    rb   c                 C   s   | d k	st |  }|  }tjtjtjtjg}|j|krft	|drP|j
rPtjS |j|kr`tjS tjS |jtjkrt	|dr|j
rtjS |jtjkrtjS td|j d|j dd S )Nrj   z=Unrecognized dtype combination in get_quant_type: activation(z	),weight())rJ   rZ   r\   r2   rE   rG   rI   rH   rM   rT   rj   r   ZDYNAMICZSTATICZWEIGHT_ONLYr^   	Exception)rX   rZ   r\   Zstatic_dtypesr   r   r   rV     s     

rV   )min_valmax_valr>   c                 C   s   |   dks|  dkr&td dS |  dks>| dkr| tdkrd|tdkrdtd dS | |kstd|  d| n"t| |kstd|  d| dS )	z Checks if the given minimum and maximum values are valid, meaning that
    they exist and the min value is less than the max value.
    r   zMmust run observer before calling calculate_qparams. Returning default values.Finfz-infzmin z should be less than max T)ZnumelwarningswarndimfloatrJ   r2   all)rm   rn   r   r   r   check_min_max_valid0  s$    ru   )rP   rQ   has_customized_qrangerM   reduce_ranger>   c           
      C   s&  |r|t jkrd\}}nd\}}| | }}|dk	rD|dk	rD|| }}|| d }	|t jkrzd|	  k rndksn tdn(|t jkrd|	  k rdksn td	|r| d
 |d
  } }nd|t jkr|rd\} }nd\} }nB|t jkr |rd\} }nd\} }n|t jkrd\} }nd\} }| |fS )ztCalculates actual qmin and qmax based on the quantization range,
    observer datatype and if range is reduced.
    )r   l    )r      N   r      zRquantization range should be positive and not exceed the maximum bit range (=256).l        zYquantization range should be positive and not exceed the maximum bit range (=4294967296).   )i?   )i   )r   r}   )i   i)r      )r2   rH   rG   rJ   rE   )
rP   rQ   rv   rM   rw   Zinitial_quant_minZinitial_quant_maxZcustom_quant_minZcustom_quant_maxZ
qrange_lenr   r   r   calculate_qmin_qmaxM  sP    



 

 






r   c                 C   s8   |  dd}t|dkr$d|d fS |d |d fS dS )z,
    Turn 'foo.bar' into ['foo', 'bar']
    r?   ry    r   N)rsplitlen)r$   rr   r   r   _parent_name  s    r   c                 C   s:   t | jdkrdS t| r2t | jdko0d| jkS dS dS )z
    Checks if module._modules is empty or
    if module is a parametrization, checks that module._modules only has
    the 'parametrizations' module
    r   Try   ZparametrizationsFN)r   Z_modulesr   )moduler   r   r   )has_no_children_ignoring_parametrizations  s
    r   )root	submoduler>   c                 C   s&   |   D ]\}}||kr|  S qdS )aS   Get the path (fully qualified name) of a submodule

    Example::

    >> class M(torch.nn.Module):
           def __init__(self):
               self.linear = torch.nn.Linear(5, 5)
           def forward(self, x):
               return self.linear(x)

    >> m = M()
    >> l = m.linear
    >> _get_path_of_module(m, l)
    "linear"
    N)Znamed_modules)r   r   npr   r   r   _get_path_of_module  s    
r   )flocr>   c                    s    fdd|  D S )z Get local keyword arguments

    Example::

    >> def f(self, a, b=9):
           pass
    >> loc = {"a": 6, "c": 7}
    >> _get_signature_locals(f, loc)
    {"a": 6}
    c                    s$   i | ]\}}|t  jkr||qS r   )r   
parameters).0kvr   r   r   
<dictcomp>  s       z)_get_signature_locals.<locals>.<dictcomp>)items)r   r   r   r   r   _get_signature_locals  s    r   zOrderedDict[str, Any])r   r>   c                 C   sf   i }t | j D ]J\}}|j|jk	r2|j||< q|j|jkrHd||< q|j|jkri ||< qt|S )z Get all default keyword arguments from function signature

    Example::

    >> def f(self, a, b=9):
           pass
    >> _get_default_kwargs(f)
    {"b": 9}
    r   )	r   r   r   defaultemptykindVAR_POSITIONALVAR_KEYWORDr   )r   kwargsnameparamr   r   r   _get_default_kwargs  s    


r   )funcr   r>   c                 C   s@   t | }t| |}| }| D ]\}}||kr"|||< q"|S )a)   Given a function and local function arguments, normalize the keyword
    arguments by filling in default arguments from function signature

    Example::

    >> def f(self, key1=3, key2=3):
           pass
    >> loc = {"key2": 6}
    >> _normalize_kwargs(f, loc)
    {"key1": 3, "key2": 6}
    )r   r   r.   r   )r   r   Zdefault_kwargsZlocal_kwargsnormalized_kwargsattrvalr   r   r   _normalize_kwargs  s    

r   )rP   rQ   r>   c                 C   s2   | d  kr|ksn t d| |k s.t ddS )ae  Validates that the user-specified quantization range is properly initialized
    and within the given bound supported by the observer dtype.

    To accommodate lower-bit quantization with respect to the existing torch.qint8 and
    torch.quint8 datatypes, the user can choose to use dynamic quantization range by passing
    in a tuple of initial qmin and qmax values. One use case is these customized qmin and qmax
    values are used to calculate static estimates of the scale and zero point for aggressive lower-bit
    fake quantization. These estimates are compared against parameters learned through backpropagation.
    The related literatures for scale and zero point via backpropagation are as follows:

    Learned Step Size Quantization: https://openreview.net/pdf?id=rkgO66VKDS
    Trained Quantization Thresholds: https://arxiv.org/pdf/1903.08066.pdf
    r   z1Used-specified quantization range must include 0.zKqmin must be strictly less than qmax for user-specified quantization range.N)rJ   )rP   rQ   r   r   r   validate_qmin_qmax  s     
r   )	rm   rn   rP   rQ   rM   epsrv   r6   r>   c                 C   s.  t | |s2tjdg| jjdtjdg| jjdfS t| t| }t|t|}	|j}
tj|	 tj
|
d}tj|	 tj|
d}|tjks|tjkrt| |	}	|	t|| d  }t||}|tjks|tjkr|r||	 || d }n||	 d}n|tjkrZ||  t||  }t||k|t|}d|  | }nH|	| t||  }t||}|t|| tj }t|||}t|jdkrtjt|g|j|
d}t|jdkrtjt|g|j|
d}|tjkrtjt|g|j|
d}|tj
|tjfS )ad  Calculates the quantization parameters, given min and max
    value tensors. Works for both per tensor and per channel cases

    Args:
        min_val: Minimum values per channel
        max_val: Maximum values per channel

    Returns:
        scales: Scales tensor of shape (#channels,)
        zero_points: Zero points tensor of shape (#channels,)
    g      ?)devicer   )rM   r   r{      )ru   r2   Ztensorr   r'   minZ
zeros_likemaxZonesr   doubleZzerosZint64r4   r:   rs   rF   rE   Znew_fullr9   whereZ	ones_likeroundtointclampr   r   rM   )rm   rn   rP   rQ   rM   r   rv   r6   Zmin_val_negZmax_val_posr   rN   rO   r   r   r   determine_qparams   sX    
( 
    r   c                 C   s   t t| jS )z Get number of positional args for a function

    Example::

    >> def f(self, key1=3, key2=3):
           pass
    >> _get_num_pos_args(f)
    3
    )r   r   argsr   r   r   r   _get_num_pos_argsG  s    
r   .)modelexample_inputsr>   c                    sJ   | i   fdd}t jjj|t jj_z| |  W 5 t jj_X  S )a   Given a model and its example inputs, return a dictionary from
    fully qualified name of submodules to example_inputs for that submodule,
    e.g. {"linear1": (tensor1,), "linear2": (tensor2,), "sub": (tensor3,),
          "sub.linear1": (tensor4,), ...}

    Used to make quantizing submodules easier now that FX Graph Mode Quantization requires
    example inputs.

    Also works for keyword arguments with default values, we would flatten keyword
    arguments as positional arguments and fill in the missing keyword args with default
    values, e.g. if we have a forward function:
    def forward(self, x, key1=3, key2=3):
        ...

    and we call it with self.submodule(x, key2=6)
    we'll get example_inputs: (x, 3, 6)

    user can also override `key1` with positional arguments as well:
    for self.submodule(x, 5, key2=6)
    we'll get: (x, 5, 6)

    variable positional arguments and variable positional keyword arguments in forward
    function are not supported currently, so please make sure no submodules is using
    them.
    c           	         s   t | }t| j|}t| jd }|t| }|rP|rP|jdd |d8 }q2||  t	|}t
| }|d k	r| |< | f||S )Nry   F)last)listr.   r   forwardr   r   popitemextendvaluestupler   )	selfr   r   Zsubmodule_example_inputsr   num_argsZ
num_to_popZsubmodule_example_inputs_tupler=   Zfqn_to_example_inputsZorig_module_callr   r   r   _patched_module_calls  s    

z7get_fqn_to_example_inputs.<locals>._patched_module_call)r2   nnModule__call__)r   r   r   r   r   r   get_fqn_to_example_inputsS  s    

r   NodePatternPattern)sr   r@   rp   collectionsr   inspectr   r   typingr   r   r   r   r	   r
   r2   Z torch.ao.quantization.quant_typer   Ztorch.fxr   Ztorch.nn.utils.parametrizer   r   r   ZQuantizerClsr   r   r   ZReLUZReLU6ZAdaptiveAvgPool1dZAdaptiveAvgPool2dZAdaptiveAvgPool3dZ	AvgPool1dZ	AvgPool2dZ	AvgPool3dZ	MaxPool1dZ	MaxPool2dZ	MaxPool3dZIdentityZHardsigmoidZSigmoidZTanhr)   Z
functionalZadaptive_avg_pool1dZadaptive_avg_pool2dZadaptive_avg_pool3dZeluZ	hardswishZinstance_normZ
layer_normZ
leaky_reluZsiluZmishZdropoutZ
max_pool1dZ
max_pool2dZ
max_pool3dr   ZhardtanhZ	hardtanh_r   r   r    r   r   stacksumr   r!   catr%   Zmeanr&   r,   r1   r7   r;   r(   rD   rK   rU   rY   r[   r]   ra   r_   re   rf   rg   rh   boolri   rb   rV   ZTensorru   r   rM   r   r   r   r   r   r   r   r   r   r3   r6   r   r   r   __all__r   r   r   r   <module>   sJ   &"$		

 
5
"      G
: