/usr/local/lib64/python3.6/site-packages/torch/quantization/fx/__pycache__
NameSizeModeActions
convert.cpython-36.pyc130810644editdlrm
fuse.cpython-36.pyc25980644editdlrm
fusion_patterns.cpython-36.pyc49420644editdlrm
graph_module.cpython-36.pyc55130644editdlrm
lower_to_fbgemm.cpython-36.pyc5060644editdlrm
lower_to_qnnpack.cpython-36.pyc5090644editdlrm
match_utils.cpython-36.pyc43240644editdlrm
pattern_utils.cpython-36.pyc17540644editdlrm
prepare.cpython-36.pyc259380644editdlrm
qconfig_utils.cpython-36.pyc73400644editdlrm
quantization_patterns.cpython-36.pyc453210644editdlrm
quantization_types.cpython-36.pyc3150644editdlrm
quantized_fusion_patterns_and_replacements.cpython-36.pyc12480644editdlrm
utils.cpython-36.pyc146600644editdlrm
_equalize.cpython-36.pyc249460644editdlrm
_lower_to_native_backend.cpython-36.pyc9260644editdlrm
__init__.cpython-36.pyc3800644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/quantization/fx/__pycache__/prepare.cpython-36.pyc (25938B)
3 EgJ @sddlZddlZddlZddlmZddlmZmZddlm Z m Z ddl m Z ddl mZmZdd lmZmZmZdd lmZmZmZdd lmZdd lmZmZdd lmZmZddl m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4m5Z5ddl%m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mm?Z?ddl>m@Z@ddlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHe eDeIejJjKfeLdddZMe eBeLdddZNejJjOjPejJjOjQejJjOjRhZSdZTe eBeLdddZUe eDeIejJjKfeDeIeBfeeGeDeIeBfeDeIeBffd d!d"ZVejJjKeDeCeCfdd#d$d%ZWeBeDeCeCfeBd&d'd(ZXeeBeBd)d*d+ZYe ejZjejJjKeDeIejJjKfe e d,d-d.Z[e ee\e\eEe\eEe\eFeeDeIejJjKfeDe eLfeFej]d/ d0d1Z^eHe eBfe eejJjKeDeIejJjKfe eDeIeBfeFeeDeIeBfe d2 d3d4Z_e eejJjKeDeIejJjKfe eDeIeBfeFeeDeIeBfdd5 d6d7Z`e eBejJjKeDeIejJjKfe eDeIeBfeLdd8d9d:Zae ejJjKeDeIejJjKfe eDeIe!feDeIeBfeBeFeeFe d; dd?d@Zce ej]eDeIej]feDeIe!fddAdBdCZde eDeIej]feDeIe!fddDdEdFZee ejJjKeDeIejJjKfeLdGdHdIZfe ejJjKeDeIejJjKfdJdKdLZge eeDeIejJjKfeDeIeBfdMdNdOZheeDeIejJjKfeDeIe!feDeIefe eDeIeBfeDeIeBfeEe\eEe\eFe dP dQdRZiejJjKeDeIejJjKfeBeDeIeBfddSdTdUZjeeDeIefeDeIeGeIekffeDeefeDeIeBfeDeIeBfddVdWdXZld]eeBeDeIeGeIekffeFeDeIeBfeFeDeIeBfeFeDeIeBfeLedZd[d\ZmdS)^N) GraphModule)propagate_qconfig_ ObserverBase)GraphNode)Argument) QConfigAnyqconfig_equals)convert_dict_to_ordered_dictgenerate_qconfig_mapget_flattened_qconfig_dict)QuantizeHandlerCustomModuleQuantizeHandlerStandaloneModuleQuantizeHandler)Pattern)is_equalization_observernode_supports_equalization)ObservedGraphModuleObservedStandaloneGraphModule) MatchResult.get_default_output_activation_post_process_map) find_matches) _parent_nameget_custom_module_class_keysall_node_args_have_no_tensorsassert_and_get_unique_devicenode_bool_tensor_arg_indexesget_new_attr_name_with_prefixNON_QUANTIZABLE_WEIGHT_OPSWEIGHT_INDEX_DICTFUNCTIONAL_OPS_WITH_BIAS)DEFAULT_OP_LIST_TO_FUSER_METHOD)get_default_qat_module_mappings)is_activation_post_processconvert)get_combined_dictget_qconfig_dtypesget_swapped_custom_module_classweight_is_quantized"activation_is_statically_quantizedactivation_is_int8_quantizedactivation_dtype weight_dtype)get_fbgemm_backend_config_dict)validate_backend_config_dict)AnyCallableDictListOptionalTupleUnion)nodemodulesreturncCs*t|tjjo(|jdko(t|t|jS)N call_module) isinstancetorchZfxropr%strtarget)r8r9rAI/usr/local/lib64/python3.6/site-packages/torch/quantization/fx/prepare.pyis_activation_post_process_node[srC)r8argr:cCst|tr||jdkr||jtkr|x0t|jD]"\}}||kr*|t|jkr*dSq*Wx*|jjD]\}}|dkr\||kr\dSq\WdS)N call_functionTweightF) r<rr>r@r! enumerateargskwargsitems)r8rDinode_arg kwarg_name kwarg_valuerArArBnode_arg_is_weight_s   rOcCst|tr|jdkr|jtkrLxbt|jD]\}}||kr*|tkr*dSq*Wn6|jtkrx*|j j D]\}}|dkrb||krbdSqbWdS)NrETZbiasF) r<rr>r@CONV_OPS_WITH_BIASrGrHCONV_BIAS_ARG_INDEXr"rIrJ)r8rDrKrLrMrNrArArBnode_arg_is_biasrs   rR)r8r9prepare_custom_config_dictqconfigr:c Cs||j}|jdg}|jdg}dd|D}dd|D}|jt|d } |j|j| } | ddkrpd|in| d} | d dkrin| d } | | fS) 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. standalone_module_namestandalone_module_classcSs"i|]}|d|df|dqS)r rrrA).0xrArArB sz1get_standalone_module_configs..cSs"i|]}|d|df|dqS)r rrrA)rWrXrArArBrYsNrr )NN)r@gettype) r8r9rSrTstandalone_modulestandalone_module_name_configsstandalone_module_class_configsZclass_config_mapZname_config_mapconfigsm_qconfig_dictsm_prepare_config_dictrArArBget_standalone_module_configs~s   rc)rootadditional_qat_module_mappingr:cCs tt|}t||ddddS)NTF)mappingZinplaceZremove_qconfig)r'r$r&)rdreZ all_mappingsrArArBqat_swap_moduless rg) qconfig_dictrer:cCsNtt|}|jdd}|j}x(|jD]\}}||kr*||||<q*W|S)z Update the qconfig_dict to account for module swaps during QAT. During QAT we perform a module swap on the nn.Module types to the corresponding nn.qat.modules types. object_typeN)r'r$r[copyrJ)rhreZall_qat_mappingsobject_type_dictZnew_object_type_dictkvrArArBupdate_qconfig_for_qats  rn)modelrhr:c Cs|jdd}|dkr|St|j}x|jjD]}|jdkr.t|t|j}|t t j kr`q.xpt j D]d\}}||krj|j|dd}x2|D]*} t |j| d|stdd|dqW|dk rj|||<qjWq.W|S)zR Update the qconfig_dict to account for fused modules such as LinearReLU. riNr;rz+During fusion, we need to specify the same zqconfigs for both modules in .)r[dict named_modulesgraphnodesr>r\r?r@listr#valuesrJr LookupError) rorhrkr9r8 module_typeopsZfuserZ fused_qconfigr>rArArBupdate_qconfig_for_fusions&    rz)r8observerror9rsr:c Cst|}|r|j|t|r*|jd}n |jd}t|}||}t||||||<|j||jd||fi} WdQRX| 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;N)rZtornamersetattrZinserting_afterZ create_node) r8r{ror9rsZ model_deviceprefixZget_new_observer_nameZ observer_namenew_obsrArArBinsert_observers      r) r8rTinputs_seen_counteroutputs_seen_counterinput_quantized_idxsoutput_quantized_idxsqhandlerr9cache_for_no_tensor_checkr:c Cs|jdkr ||krtjStjSn|jd krt|||} | r>dS|jdkoR|jtjk} | r^tjS|dk r|dk r|jr|j |drt |\} } } | StjSqtjSnB|jdkrtjS|jdkr||krtjStjSnt d |j dS) 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. placeholderr; call_methodrENFZget_attroutputzneed to handle )r;rrE) r>r=quint8floatrr@operatorgetitemZinput_output_observedZis_output_quantizedr(AssertionErrorZ format_node)r8rTrrrrrr9rZargs_have_no_tensors is_getitemZ act_dtyper.Zact_compute_dtyperArArB$get_target_activation_dtype_for_nodes4       r) r8rDrTror9rsnode_name_to_target_dtyperrSr:c $ Cszt|ttfrNg} x.|D]&} t|| ||||||| } | j| qWt|| St|ts\|St|tsjt|} |dk o~t|t} | sPt ||}|dk st|r|j n|j }t ||}|p| }|ot |o|jtk}|ot|tjkot|tjk}||j}||j}||ko<|tjko<|tjdfko<|or)$r8rDrTror9rsrrrSZnew_arg_to_returnZ inner_argZ new_inner_argnew_argis_standalone_module is_weightact_post_process_ctrZis_biasZ is_activationZweight_needs_obsZbias_needs_obsZ arg_dtypeZ node_dtypeZ(dtype_changes_and_second_dtype_not_floatZ needs_obsZ_sm_qconfig_dictrbZsm_input_quantized_idxsZ cur_input_idxZarg_idxZ arg_to_checkZ new_obs_modZexisting_obs_nodeZmaybe_obs_node_Z maybe_obs_modZ new_obs_noderArArBr0s                        r) r8rTror9rsrrrSr:c Cs|dkr dS|dk stg}x0|jD]&} t|| ||||||| } |j| q$Wi} x6|jjD](\} } t|| ||||||| }|| | <q^Wt||_| |_dS)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)rrHrrrIrJr)r8rTror9rsrrrSnew_argsrDrZ new_kwargsrlkwargZ new_kwargrArArB%maybe_insert_input_observers_for_nodes&      r)r8equalization_qconfigror9rsr is_branchr:c Cs|dkst|| rdS|r2tjd|ddSg}xz|jD]p}t|t sXt||rd|j|q>t||} | rx|j n|j } | } t || |||} ||j || j <|j| q>Wt ||_dS)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. NzCannot equalize z because it is part of a branch.)rwarningswarnrHr<rrRrrOrFZinput_activationrr|r) r8rror9rsrrrrDrZact_eq_process_ctrZnew_eq_obs_modZnew_eq_obs_noderArArB2maybe_insert_input_equalization_observers_for_nodes(    r) r8ror9rsmatchesrmatched_patternrr:cCs|j|jd\}} } }} |dkr$dS| dk s0t|jdksBtd|dk oRt|t} ||j} |j| |jo|| tj dtj fk}|ot | }|o| }|r| j }t | rtj||}|}t|||||}||j||j<|SdSdS)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. Nrz3observer insertion for outputs is handled elsewhere)NNNNN)r[r|rr>r<rZ!should_insert_observer_for_outputtrainingr=rrr+rr,rr)r8ror9rsrrrr root_node matched_nodespatternrTrdtypeZshould_insert_observerrr{rrArArB%maybe_insert_output_observer_for_nodes4   r)graph_output_noderr qconfig_mapror9rsr:c s|dgks|gkstd|gkr&dStj}ttjtttjftttftjj tttjj ft tdfdd g}x(|j D]} |j | ||||||q|W||_ dS)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. rz,unrecognized format of output_quantized_idxsN) maybe_node target_dtyperrror9rsr:c st|trZ||j}||krT|j|j}|dk s8td|j} t|| |||} | S|Snt|ttfrg} x&|D]} | j | ||||||qrWt|tr| St| SnDt|t ri} x,|j D] \}}|||||||| |<qW| S| SdS)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<rr|r[rrrrurrrqrJ)rrrrror9rsthis_node_dtyperTZ observer_modZ observer_noderesultsZ inner_nodeZ results_dictrlZinner_v)&_recursive_maybe_replace_node_with_obsrArBrls8        zZmaybe_insert_observers_before_graph_output.._recursive_maybe_replace_node_with_obs) rr=rrrr3r?r nnModulerrHr) rrrrror9rsZoutput_target_dtyperZold_argrA)rrB*maybe_insert_observers_before_graph_outputOs(  5 r)r8rrrr:c CsX|||j<|j|jd\}}}}}|dk rT|jrT|jd} t| trTt| |||dS)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. Nr)NNNNN)r|r[is_general_tensor_shape_oprHr<rmaybe_propagate_dtype_for_node) r8rrrrrrrrTZ prev_noderArArBrs   r)rsrrr:cCsDx>|jD]4}t|}x&|D]}|j|}t|tj||qWqWdS)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)rtrrHrr=r)rsrrr8Z bool_arg_idxsZ bool_arg_idxZcur_noderArArB propagate_dtypes_for_known_nodess    r)r8ror9r:cCs d}x8tt|jD]&}t|j|tttfr|j|}PqW|dkrJdSt|ttfrb|d}nt|trr|}ndSd}xt||st|tsdS|jdkrdSd}x,tt|jD]}|j|}t|trPqW|dkrdS|}|d7}|dkr|t dq|Wt|tst |j }t|t s(t ||} t|ttfrx~t |D]r\} } | dkr`qJd}x6t| |s| jd} |d7}|dkrft dqfWt | j \} } t|| | | qJWxF|jjD]8\}}t||st t |j \} } t|| | | qWdS) 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 NFrrr i'z(Unable to find observer of previous nodeT)rangelenrHr<rrurrCr>rr@r?rGrr}rrJ)r8ror9Z first_argrKZ first_arg_argZiteration_guardZtrace_back_nodeZ target_to_useZobs_mod_to_useZ input_idxZ input_arg parent_namer|output_obs_noderrArArB'maybe_make_input_output_share_observerssd           r)r8ror9cCsHt|jj}x4|D],\}}t||s*t|j||jj|qWdS)N)rurrJrCrZreplace_all_uses_withrsZ erase_node)r8ror9rJrrrArArBremove_output_observer9s  r)r8rTr9rSc CsN||j}|jdi}t|||}|j|}t|j\}} t||| |dS)N%float_to_observed_custom_module_class)r@r[r) from_floatrr}) r8rTr9rSZ custom_moduleZcustom_module_class_mappingZobserved_custom_module_classZobserved_custom_modulerr|rArArBswap_custom_module_to_observedCs   r) ror9rrrsrSequalization_config_maprrr:c # Cs i} t} d} d} d} t|jdd}xF|jjD]:}|j|jd \}}}}}t||| | ||||| | |j<q0Wt|j| |t|jj}x|D]}|j dkrnH|j d kr|j|jd\}}}}}|j|jd}| |j}|dk}|j dko|j t j k}|dks|s|o |j d k }|st|jdd}|j d krd}t |jdkrt|jdtrt |jdjd krx^|jdjD]N}|j|jddk p|j dkot|t|j t}||kr|rd }qWt|||||| ||t|||||| |||k}|dk o"|j}|dk o4|j}|r| rt|||||| ||} | dk rt|jj}!x(|!D] }"|"| krqx|"j|| qxW|rt|||st|||t|trt||||nt||| |||||j dkr| d 7} q|j d kr| d 7} |} qW| 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. rNF)Zremove_duplicaterr;rrErr T)NNNNN)r;rrEr)NNNNN) rqrrrsrtr[r|rrrur>r@rrrrHr<rrr?rrris_general_tensor_value_oprrkeysZreplace_input_withrrrrr)#ror9rrrsrSrrrrrrrZ results_noder8rrrrrTZnodes_before_observationrrZoutput_not_a_tensorrZskip_inserting_observersZis_quantized_branchuserZis_user_quantizedZis_last_node_of_patternrrZmaybe_output_obs_nodeZ orig_usersZ user_noderArArBinsert_observers_for_modelSs)                       r)ror9rrSr:cCsx|jD]\}\}}}}} |dkr(q n t|ts4q t|||| \} } ||j} tjjj} | | | | }t | j dg}t ||j |}t |j\}}t|||||||j<q WdS)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. Npreserved_attributes)rJr<rrcr@r= quantizationZ quantize_fxZ_prepare_standalone_module_fxsetr[rrsrr})ror9rrSZ node_namerrrrrTrarbr]prepareZobserved_standalone_modulerrr|rArArB$run_prepare_fx_on_standalone_modules#s&      r)observedrnode_name_to_scopepatternsrSequalization_qconfig_mapr:cCs"||_||_||_||_||_dS)N)Z _patternsZ _qconfig_mapZ_prepare_custom_config_dictZ_node_name_to_scopeZ_equalization_qconfig_map)rrrrrSrrArArB save_stateJs rF)rorhrrSequalization_qconfig_dictbackend_config_dictrr:c Cs|dkr i}|dkri}|dkr&t}t||jdi}|d}t||} t|t|t|} t|| |jr|jdi} t|| t || }t ||}t ||}t |j } t || |j||} t || |j||}|jdg}|jdg}dd|D}d d|D}t|d }t|j| | ||||}|jd g}|jd g}t|| ||t|| |||j|| || }t|||| || t|jd g}t||j|}|r|dk stt|jdtstdtj||_tj||_|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 NZadditional_quant_patternquant_patternsrerUrVcSsg|] }|dqS)rrA)rWr`rArArB szprepare..cSsg|] }|dqS)rrA)rWr`rArArBrsrrrrrzVstandalone module only supports returning simple value currently(not tuple, dict etc.))r/r0r[r'r rrrrgrnrzrqrrr rsrrrrrrrrr<rHrr=ZtensorZ'_standalone_module_input_quantized_idxsZ(_standalone_module_output_quantized_idxs)rorhrrSrrrZadditional_quant_patternsrrZflattened_qconfig_dictrer9rrr^r_Zstandalone_module_namesZstandalone_module_classesZcustom_module_classesrrrZ result_noderrArArBrYst           r)NNNF)nr=rrZtorch.fxrZtorch.quantizationrrZtorch.fx.graphrrZ torch.fx.noderrTr r Z qconfig_utilsr r rZquantization_patternsrrrZquantization_typesrZ _equalizerrZ graph_modulerrZ pattern_utilsrrZ match_utilsrutilsrrrrrrr r!r"Zfuser_method_mappingsr#Zquantization_mappingsr$Ztorch.ao.quantization.quantizer%r&r'r(r)r*r+r,r-r.rr/r0typingr1r2r3r4r5r6r7r?rrrrCrOZ functionalZconv1dZconv2dZconv3drPrQrRrcrgrnrzrrintrrrrrrrrrrrrrrr\rrrArArArBs*    ,  (  $    & ! :   {  * (  2  ]    W     H #