U
    d                     @   s  d dl 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	 ddl
mZ ddlmZ ddlmZmZ dd	lmZmZmZ d
dlmZmZmZ d
dlmZ d dlmZmZ d
dlmZm Z  d
dl!m"Z"m#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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4m5Z5 ddl)m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@ d
dlAmBZB d dlCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZK d dlLmMZM eNeOe jPdgZQeeFeRe jSjTf ePdddZUeeDePddd ZVeeDePdd!d"ZWe	eeFeReFeReHeJe jXeYf  f f eFeRe jXf ePd#d$d%ZZeeFeReFeReHeJe jXeYf  f f eFeRe jXf ePd&d'd(Z[d)d* Z\eHe eHe eFeReFeReHeJe jXeYf  f f eHeFeReDf  ePd+d,d-Z]eeFeRe jSjTf eFeReDf eeHeFeReDf  eIeFeReDf eFeReDf eFeReDf f d.d/d0Z^e jSjTeFeEeEf dd1d2d3Z_eeKeR d4d5d6Z`d7d8 Zaeee jSjTeFeRe jSjTf eed9d:d;ZbeeeNeNeGeN eGeN eHe eFeRe jSjTf eFeePf eFeReHeJe jXeYf  f d<
d=d>ZceeFeRe jSjTf eFeReFeReHeJe jXeYf  f f eHeJe jXeYf  d?d@dAZdeeeFeRe jSjTf eFeReFeReHeJe jXeYf  f f eHeJe jXeYf  dBdCdDZeeeeFeRe jSjTf eFeReFeReJe jXeYdf f f eJe jXeYdf dBdEdFZfeJeeDf e	ee jSjTeFeRe jSjTf eeFeReFeReHeJe jXeYf  f f eHe eFeReDf eHeFeReDf  e	dGdHdIZgeee jSjTeFeRe jSjTf eeFeReFeReHeJe jXeYf  f f eHe eFeReDf eHeFeReDf  ddJ
dKdLZheeDe jSjTeFeRe jSjTf eeFeReFeReHeJe jXeYf  f f ePddMdNdOZiee jSjTeFeRe jSjTf eeFeRe%f eFeReFeReHeJe jXeYf  f f eDeHe ePeHe dP
dQdRZjeeGeN eFeReFeReHeJe jXeYf  f f eFeRef e jSjTeFeRe jSjTf eddSdTdUZkeeJe jXeYf eFeReFeReHeJe jXeYf  f f eFeRe%f ddVdWdXZleeFeReFeReHeJe jXeYf  f f eFeRe%f ddYdZd[Zmee jSjTeFeRe jSjTf ePd\d]d^Znee jSjTeFeRe jSjTf d_d`daZoeeeFeRe jSjTf eFeReDf dbdcddZpeeFeRe jSjTf eFeRe%f eFeRef eeFeReDf eFeReDf eGeN eGeN eHeFeReDf  eKeR ePeHe dedfdgZqe jSjTePeFeRe jSjTf eDeFeReDf eHeFeReDf  ddhdidjZreeFeRef eFeReIeReYf f eFeReDf eFeReDf eFeReFeDeDf f ePeKeR ddk	dldmZsdreeDePeFeReIeReYf f eHeFeReDf  eHeFeReDf  eHeFeReDf  ePe"do	dpdqZtdS )s    N)GraphModule)GraphNode)Argument   )propagate_qconfig_)ObserverBase)
QConfigAnyis_reuse_input_qconfig)get_flattened_qconfig_dictconvert_dict_to_ordered_dictupdate_qconfig_for_qat   )generate_qconfig_mapupdate_qconfig_for_fusionget_standalone_module_configs)QuantizeHandler)PatternNodePattern)is_equalization_observernode_supports_equalization)ObservedGraphModuleObservedStandaloneGraphModule)MatchResultsorted_patterns_dict)find_matches)_parent_name)get_custom_module_class_keysall_node_args_have_no_tensorsassert_and_get_unique_device(get_non_observable_arg_indexes_and_typesget_new_attr_name_with_prefixNON_QUANTIZABLE_WEIGHT_OPSWEIGHT_INDEX_DICTBIAS_INDEX_DICT)is_activation_post_processconvert)get_qconfig_dtypesget_swapped_custom_module_class"activation_is_statically_quantizedactivation_is_int8_quantized)get_pattern_to_dtype_configs"get_pattern_to_input_type_to_indexget_module_to_qat_module&get_fusion_pattern_to_root_node_getter)get_native_backend_config_dict) get_pattern_to_quantize_handlers)AnyCallableDictListOptionalTupleUnionSet)defaultdict)nodemodulesreturnc                 C   s*   t | tjjo(| jdko(t|t| j S )Ncall_module)
isinstancetorchfxr   opr%   strtarget)r:   r;    rD   D/tmp/pip-unpacked-wheel-ua33x9lu/torch/ao/quantization/fx/prepare.pyis_activation_post_process_nodef   s    rF   )r:   argr<   c                 C   s|   t | trx| jdkrx| jtkrxt| jD ]$\}}||kr(|t| j kr( dS q(| j D ]\}}|dkrX||krX dS qXdS )Ncall_functionTweightF)	r>   r   rA   rC   r#   	enumerateargskwargsitems)r:   rG   inode_argZ
kwarg_nameZkwarg_valuerD   rD   rE   node_arg_is_weightj   s    
rP   c                 C   sd   t | tr| jdks| jtkr"dS t| jD ]$\}}||kr,|t| j kr, dS q,| jdd |kS )NrH   FTbias)	r>   r   rA   rC   r$   rJ   rK   rL   get)r:   rG   rN   rO   rD   rD   rE   node_arg_is_biasv   s    
rS   )rG   r:   node_name_to_target_dtypedtype_configr<   c                    s  t | ttfr(tt fdd| S t | ts6dS t| }t| }| oT| }|r dd}|r dd}j	 dd}	|dkp|	|kS  dd}|dkpj	 d	 |kS nR|r d
d}
|
dkpj	 d
 |
kS  dd}|dkpj	 d |kS dS )z] Check if the configured qconfig for the argument
    is supported by the backend or not
    c                    s   t |  S N)'is_input_arg_dtype_supported_by_backend)arU   r:   rT   rD   rE   <lambda>       z9is_input_arg_dtype_supported_by_backend.<locals>.<lambda>T
is_dynamicFZinput_dtypeNinput_activation_compute_dtypeinput_activation_dtypeweight_dtype
bias_dtype)
r>   listtupleallmapr   rP   rS   rR   name)rG   r:   rT   rU   	is_weightis_biasis_activationr\   r^   Zcompute_dtyper_   r`   rD   rY   rE   rW      s0    	


rW   )r:   rT   rU   r<   c                 C   s&   | dd}|dkp$||| j d kS )z[ Check if the configured qconfig for the output
    is supported by the backend or not
    output_dtypeNoutput_activation_dtype)rR   re   )r:   rT   rU   ri   rD   rD   rE   $is_output_dtype_supported_by_backend   s    rk   c                 C   sL   t | ||}t| jdkrHt| jd trH|tjkrH| jd jdkrHdS dS )z Check if observer in same graph
    when the node output is not fp32 and input is 'placeholder'
    the input is assumed to be quantized, so it is observed
    in a different place rather than not observed.
    r   placeholderFT)get_arg_target_dtype_as_outputlenrK   r>   r   r?   quint8rA   )r:   r;   rT   Znode_output_dtyperD   rD   rE   is_observer_in_same_graph   s
    rp   )patternmatched_node_patternrT   backend_config_dictr<   c                 C   s   |dks| dkrdS |dk	r(t |dks,tt|}|| g }t|}|}|d }|D ]f}	d}
|jD ]}|
ozt||||	}
qf|j D ]\}}|
ot||||	}
q|
ot	|||	}
|
rX dS qXdS )zZ Check is the dtype configuration of a pattern is supported by
    the backend or not
    NTr   r   F)
rn   AssertionErrorr+   rR   _default_root_node_getterrK   rW   rL   rM   rk   )rq   rr   rT   rs   Zpattern_to_dtype_configsZdtype_configs	root_nodeZ
input_nodeZoutput_noderU   	supportedrG   krD   rD   rE   ,is_pattern_dtype_config_supported_by_backend   sH    	
        ry   )r:   r;   prepare_custom_config_dictparent_qconfigparent_backend_config_dictr<   c           
      C   sZ   t | j}t|| }t|||\}}}	|dkr8d|i}|dkrDi }|	dkrP|}	|||	fS )z
    Returns the standalone module qconfig_dict and prepare_config_dict
    for `node`, assuming that the module pointed to by `node` is
    a standalone modules.
    N )rB   rC   typer   )
r:   r;   rz   r{   r|   standalone_module_nameZstandalone_module_typesm_qconfig_dictsm_prepare_config_dictsm_backend_config_dictrD   rD   rE   %prepare_get_standalone_module_configs   s    

r   )rootmodule_to_qat_moduler<   c                 C   s   t | |ddd d S )NTF)mappingZinplaceZremove_qconfig)r&   )r   r   rD   rD   rE   qat_swap_modules   s    r   )rr   sc                 C   s>   t | tr|| j n"t | ttfr:| D ]}t|| q*d S rV   )r>   r   addre   ra   rb   add_matched_node_name_to_set)rr   r   
maybe_noderD   rD   rE   r     s
    
r   c                 C   s   t | ts| d } q | S )N)r>   r   )Znode_patternrD   rD   rE   ru     s    

ru   )r:   observermodelr;   graphr<   c           
   	   C   s~   t |}|r|| t|r*| jd }nd}t|}||}t||| |||< ||  |d|| fi }	W 5 Q R X |	S )zp
    Attaches `observer` to `model`, and creates a node which calls
    `observer` on the output of `node`.
    Z_equalization_process_Zactivation_post_process_r=   )r   tor   re   r!   setattrZinserting_afterZcreate_node)
r:   r   r   r;   r   Zmodel_deviceprefixZget_new_observer_nameZobserver_namenew_obsrD   rD   rE   insert_observer  s$    
   r   )
r:   qconfiginputs_seen_counteroutputs_seen_counterinput_quantized_idxsoutput_quantized_idxsqhandlerr;   cache_for_no_tensor_checkr<   c	                 C   sF  | j dkr2||kr tjtjdS tjtjdS n| j dkrt| ||}	|	rVdddS | j dkoj| jtjk}
|
r~tjtjdS |dk	r|dk	r| rt	|\}}}|tj
kr|tj
krtj
ntj}|||||dS tjtjdS | j dk rtjtjdS | j dkr0||kr tjtjdS tjtjdS ntd	|   dS )
ay  
    Returns the expected dtype of the input and output of this node after
    convert. If the value is not None, it represents the dtype of the
    Tensor. If the value is None, it means the value is not a Tensor.

    Note: this is for activations only, weight dtypes are not handled here.

    TODO(future PR, if needed): explicitly spell out the non-Tensor
    dtypes.
    rl   )r^   rj   )r=   call_methodrH   NrH   )r^   r]   r_   r`   rj   Zget_attroutputzneed to handle )rA   r?   ro   floatr   rC   operatorgetitemZinput_output_observedr'   float16rt   Zformat_node)r:   r   r   r   r   r   r   r;   r   Zargs_have_no_tensors
is_getitemZ	act_dtyper_   Zact_compute_dtyper`   rD   rD   rE   $get_target_activation_dtype_for_node-  st    


  


r   )rG   r;   rT   r<   c                 C   sT   t | tstt| |rB| jd }t |ts4td||j d S || j d S dS )a   Get the target output activation dtype for
    the argumnet in the original graph, skipping inserted observers
    We are assuming that the observers are inserted correctly, and the dtype for
    argument in quantized graph will match what is specified by the qconfig
    r   z(Currently we only support observing Noderj   N)r>   r   rt   rF   rK   re   )rG   r;   rT   Zobserved_argrD   rD   rE   rm     s    


rm   )rG   r:   r;   rT   r<   c                 C   st   t | tstt|| }t|| }| o,| }|r@||j d S |rb|jtkrRdS ||j d S n||j d S dS )W Get the target argument dtype for the argument `arg`, as input
    to node `node`
    r^   Nr_   r`   )r>   r   rt   rP   rS   re   rC   r"   rG   r:   r;   rT   rf   rg   rh   rD   rD   rE   %get_arg_target_dtype_as_input_to_node  s    	


r   c                 C   sV   t | tstt|| }t|| }| o,| }|rNd||j krN||j d S dS dS )r   r]   N)r>   r   rt   rP   rS   re   r   rD   rD   rE   -get_arg_target_compute_dtype_as_input_to_node  s    	

r   )r:   rG   r   r   r;   r   rT   r   rz   rs   r<   c
           #      C   sH  t |ttfrLg }
|D ](}t| |||||||||	
}|
| qt||
S t |tsZ|S t |tsht|}|dk	oz| }|dk	st|s t	| |}t
|}|r|jn|j}t|||}t|| ||}t|| ||}||kr|tjkr|tkr| p|tjtjtjfko|| jd k}nt| ||||	\}}}|dg }d}t| jD ]\}}||krP|} qpqP|dkrd}n6t|||}||krtjntj}||ko|tjk}|j}|rD| }d}|j D ]H\}} |jdkr||j }!t|!t|kr|!j|kr|} q q|dkr@t|||||}"|"}n|}|S )zk
    Given a `node` and an `arg`, inserts an input observer between
    `node` and `arg` if necessary.
    Nr   r   Fr=   ) r>   ra   rb   ,maybe_insert_input_observer_for_arg_or_kwargappendr~   r   rt   is_standalone_modulerP   r
   rI   
activationrm   r   r   r?   r   DO_NOT_OBS_DTYPE_LISTro   Zint8r   rK   r   rR   rJ   usersrM   rA   rC   dtyper   )#r:   rG   r   r   r;   r   rT   r   rz   rs   Znew_arg_to_returnZ	inner_argZnew_inner_argnew_argr   rf   is_reuse_input_qconfig_act_post_process_ctrZarg_as_output_target_dtypeZarg_as_input_target_dtypeZ!arg_as_input_target_compute_dtypeZ	needs_obsZ_sm_qconfig_dictr   Z_sm_backend_config_dictZsm_input_quantized_idxsZcur_input_idxZarg_idxZarg_to_checkZnew_obs_modZexisting_obs_nodeZmaybe_obs_node_Zmaybe_obs_modZnew_obs_noderD   rD   rE   r     s         


   	"    








    r   )
r:   r   r   r;   r   rT   r   rz   rs   r<   c	                 C   s   |dkrdS |dk	st g }	| jD ](}
t| |
||||||||
}|	| q"i }| j D ]*\}}t| |||||||||
}|||< qZt|	| _|| _dS )a
  
    If needed, inserts observers to the input args and kwargs of `node`.
    Note: modifies `node` inplace.

    For example, if cur_node needs an observer after prev_node, we change from

      prev_node -> cur_node

    To

      prev_node -> obs -> cur_node
    N)rt   rK   r   r   rL   rM   rb   )r:   r   r   r;   r   rT   r   rz   rs   new_argsrG   r   Z
new_kwargsrx   kwargZ	new_kwargrD   rD   rE   %maybe_insert_input_observers_for_nodeQ  sF    
          

r   )r:   equalization_qconfigr   r;   r   rT   	is_branchr<   c                 C   s   |dkst | |sdS |r0td|  d dS g }| jD ]^}t|trRt| |r^|| q:t| |}	|	rr|j	n|j
}
|
 }t|||||}|| q:t|| _dS )z
    If `node` needs to be equalized, find the input/weight observers it needs in
    `equalization_qconfig`, creates them, and inserts it into `graph`.

    If `node` does not need an equalization observer, returns None.
    NzCannot equalize z  because it is part of a branch.)r   warningswarnrK   r>   r   rS   r   rP   rI   Zinput_activationr   rb   )r:   r   r   r;   r   rT   r   r   rG   rf   Zact_eq_process_ctrZnew_eq_obs_modZnew_eq_obs_noderD   rD   rE   2maybe_insert_input_equalization_observers_for_node  s4    




    r   )
r:   r   r;   r   matchesrT   matched_patternr   is_qatr<   c	                 C   s   | | jd\}	}
}}}|dkr$dS |dk	s0t| jdksBtd|dk	oP| }|| j d }|ttjg k}|ozt|}|o| }|r|j	}t
|r||||}| }t| ||||}|S dS dS )z
    If `node` needs an output observer, creates it, inserts it into `graph`
    and returns it.

    If `node` does not need an output observer, returns None.
    NNNNNNr   z3observer insertion for outputs is handled elsewhererj   )rR   re   rt   rA   r   r   r?   r   r)   r   r*   Zget_activation_ctrr   )r:   r   r;   r   r   rT   r   r   r   rv   r   rq   r   r   r   Zshould_insert_observerr   r   r   rD   rD   rE   %maybe_insert_output_observer_for_node  s:     r   )graph_output_noder   rT   qconfig_mapr   r;   r   r<   c           
         s   |dgks|g kst d|g kr&dS tj}ttjtttttttjt	f  f f ttt
f tjjtttjjf ttd fdd g }| jD ]}	| |	|||||| qt|| _dS )z
    If the output needs to be quantized and there are any nodes
    in the output which are not already observed, inserts observers
    for those nodes.
    r   z,unrecognized format of output_quantized_idxsN)r   target_dtyperT   r   r   r;   r   r<   c                    s   t | tr\t| ||}||krV|| j}|dk	s:td| }	t| |	|||}
|
S | S nt | tt	frg }| D ]}|
 ||||||| qrt | tr|S t	|S n@t | tri }|  D ] \}} |||||||||< q|S |S dS )a`  
        Navigate an arbitrary data structure of lists, tuples, dicts.
        For each container type, recurse on all inputs. Once any Node
        is found, insert an observer if needed and do not recurse further.

        For example, given a structure of

          {'foo1': [[bar1]], 'foo2': {'foo3': [[[bar3]]]}}

        we recurse down to bar1 and bar3, observe them if necessary,
        and if we inserted an observer then replace the original node
        with its observer.

        Returns the data structure with all nodes needing observation being
        replaced by their observers.
        Nz=Quantizing the output node without a qconfig is not supported)r>   r   rm   rR   re   rt   r   r   ra   rb   r   dictrM   )r   r   rT   r   r   r;   r   this_node_dtyper   Zobserver_modZobserver_noderesultsZ
inner_nodeZresults_dictrx   Zinner_v&_recursive_maybe_replace_node_with_obsrD   rE   r     sd    
  
         


     
zZmaybe_insert_observers_before_graph_output.<locals>._recursive_maybe_replace_node_with_obs)rt   r?   ro   r   r   r3   rB   r5   r7   r~   r	   nnModuler   rK   r   rb   )
r   r   rT   r   r   r;   r   Zoutput_target_dtyper   Zold_argrD   r   rE   *maybe_insert_observers_before_graph_output  s:     
>
     r   )r:   r   rT   r   r<   c           
      C   sj   ||| j  d< ||| j  d< || j d\}}}}}|dk	rf| rf| jd }	t|	trft|	||| dS )a  
    Assigns `target_dtype` to `node`. If `node` is a general tensor shape op
    (see GeneralTensorShapeOpQuantizeHandler in quantization_patterns.py for more details)
    also call this function recursively on
    the first argument, to propagate the dtype to the caller.
    r^   rj   r   Nr   )re   rR   is_general_tensor_value_oprK   r>   r   maybe_propagate_dtype_for_node)
r:   r   rT   r   rv   r   rq   r   r   Z	prev_noderD   rD   rE   r   N  s     

   r   )r   rT   r   r<   c              	   C   s   | j D ]}t|}|D ]p}|| |}|D ]Z}|j| }t|tsLt|trVt|}	n|g}	|	D ]"}
t|
tjjj	r`t
|
||| q`q*qqdS )a  
    Currently we assume that inputs to the graph are either `torch.float` or
    `torch.quint8`, which is not always correct. For ops such as
    `x.masked_fill(mask, value)`, we know that the dtype of  `mask` is a
    `BoolTensor`. Propagate this information throughout the graph.

    Note: not all dtypes in the graph will be correct after this pass, but a
    higher percentage of them will be correct. Hopefully in the future we can
    replace this with a better way to reason about dtypes of tensors.
    N)nodesr    rK   r>   rb   ra   r?   r@   r:   r   r   )r   rT   r   r:   Znon_observable_arg_dictZarg_typeZnon_observable_indicesindexrG   arg_listZcur_argrD   rD   rE    propagate_dtypes_for_known_nodese  s"    


   r   )r:   r   r;   r<   c                 C   s  d}t t| jD ](}t| j| tttfr| j| } q<q|dkrHdS t|ttfr`|d }nt|trp|}ndS d}t||st|tsdS |jdkrdS d}t t|jD ]}|j| }t|tr qq|dkrdS |}|d7 }|dkrxt	dqxt|tst	|j
}t|tst	|| }	t|ttfrt|D ]\}
}|
dkrTq>d}t||st|jdk rz dS |jd }|d7 }|dkrXt	dqXt|j
\}}t|| ||	 q>| j D ]8\}}t||st	t|j
\}}t|| ||	 qdS )	a  
    Ensures that we share an observer
    for all input arguments as well as the output argument. In detail, given
    a graph of

      x0 -> obs0 -> op -> x2
                  /
      x1 -> obs1 /

    where node obs0 points to observer instance observer0,
    obs1 points to observer1 and obs2 points to observer2, we make nodes obs1
    and ob2 point to observer0.
    Returns: whether the operation succeeded or not
    NFr   rl   r   i'  z(Unable to find observer of previous nodeT)rangern   rK   r>   r   ra   rb   rF   rA   rt   rC   rB   rJ   r   r   r   rM   )r:   r   r;   Z	first_argrN   Zfirst_arg_argZiteration_guardZtrace_back_nodeZtarget_to_useZobs_mod_to_useZ	input_idxZ	input_argparent_namere   output_obs_noder   rD   rD   rE   'maybe_make_input_output_share_observers  sh    











r   )r:   r   r;   c                 C   sD   t | j }|D ],\}}t||s(t||  |j| qd S rV   )ra   r   rM   rF   rt   Zreplace_all_uses_withr   Z
erase_node)r:   r   r;   rM   r   r   rD   rD   rE   remove_output_observer  s
    
r   )r:   r   r;   rz   c           
      C   sN   || j  }|di }t|||}||}t| j \}}	t|| |	| d S )N%float_to_observed_custom_module_class)rC   rR   r(   
from_floatr   r   )
r:   r   r;   rz   Zcustom_moduleZcustom_module_class_mappingZobserved_custom_module_classZobserved_custom_moduler   re   rD   rD   rE   swap_custom_module_to_observed  s    
   r   )r   r;   r   r   r   rz   equalization_config_mapr   r   rs   observed_node_namesr   r<   c           +      C   s  t t}t }d}d}t| jdd}| jjD ]^}||jd\}}}}}t|||||||||	||j< |jdkrz|d7 }|jdkr.|d7 }q.t	| j|| t
| jj}d}d}d}|D ]}|jdkr̐n|jd	kr`||jd\}}}}}||jd}||j }|dk}|jd
ko&|jtjk}|dks>|s>|oH|jdk }t||||	}|s`|r`t| jdd}|jdkrL|dk	stt||
 d} t|jdkr0t|jd tr0t|jd jdkr0|jd jD ]N}!||!jddk	p|!jdkot|t|!j t}"|!|kr|"rd} qt|}||k}#|#rrt||| ||||||		 t||| ||||  ||k}$|dk	o| }%t|}&|$r`t|| |||||||	}'|'dk	r`t
|j }(|(D ] })|)|'krq|)||' qt |||}*|%r|*s|&r(t!|| |s(t"|| | |dk	r`|# r`t$|||| nt%||||| || |jdkrv|d7 }q|jdkr|d7 }|}q|S )a$  
    Inserts observers, using the following high level algorithm:

    For each node in the graph:
      1. determine the target dtype of this node in the quantized graph, and save
           it for future steps
      2. determine the target dtype or all args and kwargs of this node
      3. if any arg or kwarg's target dtype does not match the current node's
           dtype, insert an observer
      4. if the current node needs an output observer, insert it

    For example:

    - starting graph:
        x0 -> linear -> x1

    - observed graph after processing x0:
        x0(fp32)

    - observed graph after processing linear:
        x0(fp32) -> x0_obs0(int8) -> linear(int8) -> linear_obs0(int8)

    - observed graph after processing x1:
        x0(fp32) -> x0_obs0(int8) -> linear(int8) -> linear_obs0(int8) -> x1

    After a node is processed, the naive observer placement is guaranteed to be
    complete for that node and all of its predecessors. There can be future
    passes which optimize the graph by deduplicating observers, etc.
    r   FZremove_duplicater   rl   r   r   N)r=   r   rH   r   rH   r=   T)&r9   r   named_modulesr   r   rR   re   r   rA   r   ra   rC   r   r   ry   rt   r   rn   rK   r>   r   r   rB   r   ru   r   r   r   r
   r   keysZreplace_input_withrp   r   r   Zis_custom_moduler   r   )+r   r;   r   r   r   rz   r   r   r   rs   r   r   rT   r   r   r   r:   rv   r   rq   r   r   Znodes_before_observationZresults_nodeZ	last_noderr   r   r   Zoutput_not_a_tensorr   Zskip_inserting_observersZis_supported_by_backendZis_quantized_branchuserZis_user_quantizedZis_input_node_of_the_patternZis_last_node_of_patternr   r   Zmaybe_output_obs_nodeZ
orig_usersZ	user_nodeZis_observer_in_same_graph_rD   rD   rE   insert_observers_for_model  s&   B       



  


 



   
                

    

r   )r   r   r;   r   rz   rs   r<   c                 C   s   |  D ]\}\}}}	}
}|
dkr&qn
|
 s0qt|||||\}}}||j }tjjjj}||||||d}t	|
dg }t||j|}t|j\}}t|| || |||j< qdS )z
    Runs prepare_fx on each standalone module. Note: this does
    not modify the graph, it just replaces the unobserved modules with
    their observed versions.
    N)rs   preserved_attributes)rM   r   r   rC   r?   ZaoZquantizationZquantize_fxZ_prepare_standalone_module_fxsetrR   r   r   r   r   )r   r   r;   r   rz   rs   Z	node_namerv   r   rq   r   r   r   r   r   Zstandalone_moduleprepareZobserved_standalone_moduler   r   re   rD   rD   rE   $run_prepare_fx_on_standalone_modules  sN        

 
r   )	observedr   node_name_to_scoperz   equalization_qconfig_mapqconfig_dictr   r   r<   c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S rV   )Z_qconfig_mapZ_prepare_custom_config_dictZ_node_name_to_scopeZ_equalization_qconfig_mapZ_qconfig_dictZ_is_qatZ_observed_node_names)r   r   r   rz   r   r   r   r   rD   rD   rE   
save_state0  s    
r   F)	r   r   r   r   rz   equalization_qconfig_dictrs   r   r<   c           !      C   sz  |dkri }|dkri }i }|dkr*t  }t|}t|}t|}	|	 D ]t\}
}| D ]b\}}tti d}|| kstd|  d| || }|
|kr||
 	| qZ|g||
< qZqJt
|}t| t| t| |}t| |}t|}t| || |r"t|}t| | t|i }t| jdd}t| || j||}t| || j||}|dg }|dg }d	d
 |D }dd
 |D }t|d}t| j|||||||}|dg }|dg }t| ||||| t }t| |||| j|||||||}t| ||||||| t|dg } t| | j| } |rv|dk	sDtt|jd t s^tdt!"|| _#t!"|| _$| S )aQ   standalone_module means it a submodule that is not inlined in
    parent module, and will be quantized separately as one unit.

    How the standalone module is observed is specified by `input_quantized_idxs` and
    `output_quantized_idxs` in the prepare_custom_config for the standalone module
    Args:
        node_name_to_scope: mapping from node name to the scope of the module which contains the node.
        The scope is a tuple of fully qualified path of the module and the type of the module
    Returns:
        model(GraphModule): prepared standalone module
        attributes:
            _standalone_module_input_quantized_idxs(List[Int]): a list of
                indexes for the graph input that is expected to be quantized,
                same as input_quantized_idxs configuration provided
                for the standalone module
            _standalone_module_output_quantized_idxs(List[Int]): a list of
                indexs for the graph output that is quantized
                same as input_quantized_idxs configuration provided
                for the standalone module
    N)rI   rQ   inputzinput type must be one of z
 but got: Fr   r   Zstandalone_module_classc                 S   s   g | ]}|d  qS r   rD   .0configrD   rD   rE   
<listcomp>  s     zprepare.<locals>.<listcomp>c                 S   s   g | ]}|d  qS r   rD   r   rD   rD   rE   r     s     r   r   r   r   r   zVstandalone module only supports returning simple value currently(not tuple, dict etc.))%r/   r0   r   r,   rM   r#   r$   r   rt   r   r.   r   r   r   r   r-   r   r   r   r   r   r   rR   r   r   r   r   r   r   r   r>   rK   r   r?   ZtensorZ'_standalone_module_input_quantized_idxsZ(_standalone_module_output_quantized_idxs)!r   r   r   r   rz   r   rs   r   patternsZpattern_to_input_type_to_indexrq   Zinput_type_to_indexZ
input_typer   Zindex_dictsZ
index_dictZroot_node_getter_mappingZflattened_qconfig_dictr   r;   r   r   Zstandalone_module_name_configsZstandalone_module_class_configsZstandalone_module_namesZstandalone_module_classesZcustom_module_classesr   r   r   r   Zresult_noder   rD   rD   rE   r   C  s    



	                    
    r   )NNNF)ur?   r   r   Ztorch.fxr   Ztorch.fx.graphr   r   Ztorch.fx.noder   quantizer   r   r   r   r	   r
   Zqconfig_dict_utilsr   r   r   Zqconfig_utilsr   r   r   Zquantization_patternsr   Z(torch.ao.quantization.quantization_typesr   r   Z	_equalizer   r   Zgraph_moduler   r   Zpattern_utilsr   r   Zmatch_utilsr   utilsr   r   r   r   r    r!   r"   r#   r$   Ztorch.ao.quantization.quantizer%   r&   r'   r(   r)   r*   Zbackend_config.utilsr+   r,   r-   r.   Zbackend_configr/   Zbackend_config_utilsr0   typingr1   r2   r3   r4   r5   r6   r7   r8   collectionsr9   intr   boolr   rB   r   r   rF   rP   rS   r   r~   rW   rk   rp   ry   r   r   r   ru   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   rD   rD   rE   <module>   s  (( %  '
$
 
b  
 
 	 
8 -
 6 
f 
 
&^




  
/


     