o
    ZhsP                     @   sh  d dl Z d dlZd dlmZmZmZ d dlZd dlm  m	  m
  mZ d dlm  m	  m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	lmZmZ ejjZG d
d de j Z!dededede"e#e$e f de%e!e!f f
ddZ&dedede"e#e$e f dee%eej'e(f eej'e)f f  fddZ*dededefddZ+dedede)fddZ,dede-e) fddZ.dedede#fddZ/dede#defdd Z0deddfd!d"Z1d#d$ Z2e2d%ej'd&ej'dej'fd'd(Z3e2d%ej'd&ej'dej'fd)d*Z4e2d%ej'd&ej'dej'fd+d,Z5dede6fd-d.Z7deded/e)defd0d1Z8dS )2    N)CallableOptionalUnion)FakeQuantizeBaseObserverBase)_is_activation_post_process)getattr_from_fqn)GraphModule)Node   )NSNodeTargetTypeNSResultsTypec                   @   s4   e Zd Ze Ze Ze Ze Ze Z	dS )NodeInputOrOutputTypeN)
__name__
__module____qualname__enumautoFP32INT8FP16UNKNOWNZFP32_OR_INT8 r   r   C/var/www/auris/lib/python3.10/site-packages/torch/ao/ns/fx/utils.pyr      s    r   nodegm
logger_clsnode_type_to_io_type_mapreturnc                    s  |d }|d }|d }|d }|d }|d }	|d }
|d }| j d	krk| j|v r0tjtjfS | j|v r;tjtjfS | j|v rFtjtjfS | j|v ret| |d
}t|tsXJ t	||||\}}||fS tj
tj
fS | j dkr| j dkswJ t| jtsJ t|| j t fdd|
D }t |ttfs|rt| |d
}t|tsJ t	||||\}}||fS t fdd|D }t fdd|	D }|rtjtjfS |rtjtjfS tj
tj
fS | j dkrc| jdkrt| |d
}t|tsJ t	||||\}}|tjfS | jdkr<t| |d
}t|tsJ t	||||\}}t| |d}|tju s7J | d|tjfS | j|v r]t| |d
}t|tsPJ t	||||\}}||fS tj
tj
fS tj
tj
fS )NZfuns_io_type_fp32Zfuns_io_type_fp16Zfuns_io_type_int8Zfuns_io_type_fp32_or_int8Zmods_io_type_fp32Zmods_io_type_int8mods_io_type_fp32_or_int8Zmeths_io_type_fp32_or_int8call_functionr   call_modulec                 3       | ]}t  |V  qd S N
isinstance.0target_typemodr   r   	<genexpr>N       

z7get_node_first_input_and_output_type.<locals>.<genexpr>c                 3   r"   r#   r$   r&   r)   r   r   r+   `   r,   c                 3   r"   r#   r$   r&   r)   r   r   r+   c   r,   call_method
dequantizetor   z handling needs to be added)optargetr   r   r   r   get_normalized_nth_inputr%   r
   $get_node_first_input_and_output_typer   strr   anyr   r   torchZfloat16)r   r   r   r   ZFUNS_IO_TYPE_FP32ZFUNS_IO_TYPE_FP16ZFUNS_IO_TYPE_INT8ZFUNS_IO_TYPE_FP32_OR_INT8ZMODS_IO_TYPE_FP32ZMODS_IO_TYPE_INT8MODS_IO_TYPE_FP32_OR_INT8ZMETHS_IO_TYPE_FP32_OR_INT8Z	first_argZ_prev_node_input_typeZprev_node_output_type"is_known_fp32_or_int8_input_moduleZis_known_fp32_input_moduleZis_known_int8_input_module	prev_nodeZcur_node_dtype_targetr   r)   r   r3   &   s   







r3   c                    s@  t | |d}t|tsdS |d }dd }|jdkr=|jtjkr'|||ddS |jtjtj	tj
tjfv r;|||dd	S dS |jd
krt|jtsJJ t||j t tjtjtjtjtjtjtjtjtjtjtjtjtjtjtjtjtj tj!tj"tj#tj$tjtj%tj&fr j' j(fS t) fdd|D }|rt*|||S dS )z{
    Returns the qparams (scale, zero_point) of the first input to `node`,
    if they can be inferred from the graph.
    r   Nr   c                 S   sl   t | ||}t | ||}t|trt|jtsJ t|tr$t|jts&J t||j}t||j}||fS r#   )r2   r%   r
   r1   r4   r   )r   r   Zscale_arg_idxZ
zp_arg_idxZ
scale_nodeZzp_nodeZ	scale_objZzp_objr   r   r    _get_scale_zp_from_function_args   s   z@get_node_input_qparams.<locals>._get_scale_zp_from_function_argsr    r         r!   c                 3   r"   r#   r$   r&   Z
module_objr   r   r+      r,   z)get_node_input_qparams.<locals>.<genexpr>)+r2   r%   r
   r0   r1   r6   Zquantize_per_tensortoqaddZadd_relumulZmul_relur4   r   nnqZLinearZConv1dZConv2dnniqZ
ConvReLU2dZConv3dZBatchNorm2dZBatchNorm3dZConvTranspose1dZConvTranspose2dZELUZ	GroupNormZInstanceNorm1dZInstanceNorm2dZInstanceNorm3dZ	LayerNormZ	HardswishZ	LeakyReLUZReLU6ZBNReLU2dZBNReLU3dZ
ConvReLU1dZ
ConvReLU3dZ
LinearReLUscaleZ
zero_pointr5   get_node_input_qparams)r   r   r   r9   r7   r:   r8   r   r=   r   rD      sb   	

	
rD   c                 C   s   | j dkrQt|| j}t|rQt| jdksJ t| jd ts"J | jd } t| jts/J t|| j}t|rQt| jdksBJ t| jd tsLJ | jd } | S )a  
    If node is not an observer, returns it.  If node is an observer,
    navigates up the graph and returns the first parent which is not an
    observer.  For example,

    graph: (node_non_obs), node = node_non_obs : returns node_non_obs
    graph: (node_non_obs -> obs0), node = obs0 : returns node_non_obs
    graph: (node_non_obs -> obs0 -> fq0), node = fq0 : returns node_non_obs
    r!   r   r   )	r0   r   r1   r   lenargsr%   r
   r4   r   r   Znode_objr   r   r   return_first_non_observer_node   s   


rH   c                 C   s*   | j dkrt|| j}t|tjrdS dS )aO  
    Assumes that all non-param args occur first. Returns the number of
    non-param args expected for a node.  For example, for

      F.linear(x, weight, bias)

    Returns 1, because x is a non-param arg and weight and bias are params.
    For

      lstm_mod(x, hid)

    Returns 2, because both x and hid are non-param args.
    r!   r;   r   )r0   r   r1   r%   nnZLSTMrG   r   r   r   get_number_of_non_param_args  s
   
rJ   c                    sp   t  jdkr	g S  jdkr5 jtjtjjjtjfv s( jtj	tjjj	tj	fv r5 fddt
dD }|S dgS )a-  
    Returns the indices of args of the node which we should attach
    loggers to, if input logging is enabled.

    For example,
    * for (x + y), returns [0, 1]
    * for (1 + y), returns [1]
    * for (x + 1), returns [0]
    * for (linear(x, w, b)) returns [0]
    * by default, returns [0]
    r   r    c                    s"   g | ]}t  j| tkr|qS r   )typerF   r
   )r'   ir   r   r   
<listcomp>;  s   " z4get_arg_indices_of_inputs_to_log.<locals>.<listcomp>r;   )rE   rF   r0   r1   r6   r?   ops	quantizedoperatorr@   range)r   resultr   rM   r    get_arg_indices_of_inputs_to_log(  s   
rT   c                 C   sR   d}| j dv rt| j}|S | j dkr't| jtsJ t|| j}t|}|S )z
    Returns a string representation of the type of the function or module
    pointed to by this node, or '' for other node types.
     )r    r-   r!   )r0   r6   typenamer1   r%   r4   r   )r   r   r(   Z
target_modr   r   r   get_target_type_str@  s   


rW   results
model_namec           	      C   sz   i }|   D ]4\}}d}| D ]}|  D ]\}}||kr+t|s$J |d d }qqq|dur6|||< q|||< q|S )a	  
    Rekeys the layer name of a results dictionary to use node names
    from `model_name`.

    For example, transforms

        {'base_op_1_0': {'node_output': {'model_a':
          [{'ref_node_name': 'linear1', ...}]}}}

    into

        {'linear1': {'node_output': {'model_a':
          [{'ref_node_name': 'linear1', ...}]}}}

    Note: we cannot use these node names directly because they are not
    guaranteed to be consistent across models. This is why we extract
    the results first and rekey afterwards.
    Nr   Zref_node_name)itemsvaluesrE   )	rX   rY   Znew_resultsZold_layer_nameresult_type_to_resultsZnew_layer_namemodel_name_to_resultsZcur_model_nameZlist_of_resultsr   r   r   'rekey_logger_info_on_node_name_of_modelO  s   

r^   c           	      C   s   d}|   D ]$}|  D ]}| D ]\}}t|dkr(|d d dur(|} nq  |rb|   D ]2}|  D ])}|| }| D ]\}}||krJqAtt|D ]}|| d }||| d< qPqAq7q1dS dS )ay  
    If `fqn` entries are filled in for one of the models in `results`, copies
    them over to any models which do not have them filled out.

    A common use case benefitting from this is comparing a model prepared by
    quantization to a quantized model. In this case, the model prepared by
    quantization would have `fqn` entries, and the quantized model would not.
    Nr   fqn)r[   rZ   rE   rR   )	rX   Zmodel_name_with_fqnsr\   r]   rY   Zmodel_resultsZref_model_resultsrL   r_   r   r   r   maybe_add_missing_fqnsv  s4   r`   c                    s    fddS )Nc            	         s   | ^}}}t |trt |tst |tr8t |tr8g }t||D ]\}}||g|R }||i | q |S t |tjrRt |tjrR|jrK| }|jrR| }|j	tj
ks^|j	tj
kr`d S ||g|R } |i |S r#   )r%   tuplelistzipappendr6   TensorZis_quantizedr.   Zdtypefloat)	rF   kwargsZa0Za1Za_otherrX   Zel0Zel1new_argsfinnerr   r   rk     s(   
zGmaybe_dequantize_first_two_tensor_args_and_handle_tuples.<locals>.innerr   )rj   r   ri   r   8maybe_dequantize_first_two_tensor_args_and_handle_tuples  s   rl   xyc                 C   s*   t | }t | | }dt ||  S )z
    Computes the SQNR between `x` and `y`.

    Args:
        x: Tensor or tuple of tensors
        y: Tensor or tuple of tensors

    Return:
        float or tuple of floats
       )r6   Znormlog10)rm   rn   ZPsZPnr   r   r   compute_sqnr  s   
rq   c                 C   s"   t | | d  | d   S )z
    Computes the normalized L2 error between `x` and `y`.

    Args:
        x: Tensor or tuple of tensors
        y: Tensor or tuple of tensors

    Return:
        float or tuple of floats
    r;   )r6   sqrtsumrm   rn   r   r   r   compute_normalized_l2_error  s   "ru   c                 C   s(   |  dd} | dd}tjj| |S )z
    Computes the cosine similarity between `x` and `y`.

    Args:
        x: Tensor or tuple of tensors
        y: Tensor or tuple of tensors

    Return:
        float or tuple of floats
    r   )Zreshaper6   rI   Z
functionalZcosine_similarityrt   r   r   r   compute_cosine_similarity  s   rw   c                 C   s4   | j dkr| jtjtjtjtjtjtjfv rdS dS )Nr    FT)r0   r1   r6   r?   r@   rQ   catstackrM   r   r   r   op_type_supports_shadowing  s   
	rz   idxc                 C   s"  z[| j |dd}|dur0|\}}t|t| |ksJ |t|k r'|| W S t| | W S t| jt| j |ks>J |t| jk rK| j| W S |t| j }t| j | W S  ty   t| jt| j |kspJ |t| jk r~| j|  Y S |t| j }t| j |  Y S w )zu
    Given a node, gets the n'th input to that node, normalizing
    args and kwargs to the best of its ability.
    T)Znormalize_to_only_use_kwargsN)Znormalized_argumentsrE   rb   r[   rF   rg   RuntimeError)r   r   r{   Znorm_args_and_kwargsZ	norm_argsZnorm_kwargsZ
kwargs_idxr   r   r   r2     s,   
r2   )9r   rQ   typingr   r   r   r6   Ztorch.ao.nn.intrinsic.quantizedZaorI   Z	intrinsicrP   rB   Ztorch.ao.nn.quantizedrA   Ztorch.nnZtorch.ao.quantizationr   r   Ztorch.ao.quantization.observerr   Ztorch.ao.quantization.utilsr   Ztorch.fxr	   Ztorch.fx.graphr
   Zns_typesr   r   rO   r>   Enumr   dictr4   setra   r3   re   rf   intrD   rH   rJ   rb   rT   rW   r^   r`   rl   rq   ru   rw   boolrz   r2   r   r   r   r   <module>   s   

{"
P


'"