/usr/local/lib64/python3.6/site-packages/torch/ao/ns/fx/__pycache__
NameSizeModeActions
graph_matcher.cpython-36.pyc120230644editdlrm
graph_passes.cpython-36.pyc153540644editdlrm
mappings.cpython-36.pyc72090644editdlrm
ns_types.cpython-36.pyc8090644editdlrm
pattern_utils.cpython-36.pyc37950644editdlrm
utils.cpython-36.pyc109420644editdlrm
weight_utils.cpython-36.pyc65730644editdlrm
__init__.cpython-36.pyc1500644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/ao/ns/fx/__pycache__/utils.cpython-36.pyc (10942B)
3 Eg\D @sddlZddlZddlZddljZddljjjZddl jjZ ej jZ ddl mZmZmZmZmZmZmZddlmZddlmZddlmZmZddlmZddlmZdd l m!Z!m"Z"Gd d d ej#Z$eeeee%ee!fee$e$fd d dZ&eeee%ee!feeeej'e(feej'e)ffdddZ*eeedddZ+eee)dddZ,eee)dddZ-eee%dddZ.e"e%e"dddZ/e"ddd d!Z0d"d#Z1e1ej'ej'ej'd$d%d&Z2e1ej'ej'ej'd$d'd(Z3e1ej'ej'ej'd$d)d*Z4dS)+N)TupleCallableDictSetListOptionalUnion) GraphModule)Node) ObserverBaseFakeQuantizeBase)getattr_from_fqn)is_activation_post_process)NSNodeTargetType NSResultsTypec@s4eZdZejZejZejZejZejZ dS)NodeInputOrOutputTypeN) __name__ __module__ __qualname__enumautoFP32INT8FP16UNKNOWN FP32_OR_INT8rr@/usr/local/lib64/python3.6/site-packages/torch/ao/ns/fx/utils.pyrs r)nodegm logger_clsnode_type_to_io_type_mapreturncs|d}|d}|d}|d}|d}|d} |d} |d} |jd kr|j|kr`tjtjfS|j|krvtjtjfS|j|krtjtjfS|j|krtjtjfStjtjfSn|jd kr|jd kstt |jt stt ||jt |t t fr.|jd } t | tstt| |||\} }||fStfd d |D}tfdd | D}tfdd | D}|rtjtjfS|rtjtjfS|rtjtjfStjtjfSn|jdkr|jdkr|jd }t |tstt||||\} }|tjfS|jdkrh|jd }t |ts(tt||||\} }|jd}|tjks^t|d|tjfS|j| krtjtjfStjtjfStjtjfSdS)NZfuns_io_type_fp32Zfuns_io_type_fp16Zfuns_io_type_int8Zfuns_io_type_fp32_or_int8Zmods_io_type_fp32Zmods_io_type_int8mods_io_type_fp32_or_int8Zmeths_io_type_fp32_or_int8 call_function call_modulerc3s|]}t|VqdS)N) isinstance).0 target_type)modrr Wsz7get_node_first_input_and_output_type..c3s|]}t|VqdS)N)r')r(r))r*rrr+Zsc3s|]}t|VqdS)N)r')r(r))r*rrr+]s call_method dequantizeZtorz handling needs to be added)optargetrrrrrrAssertionErrorr'strr r r argsr $get_node_first_input_and_output_typeanytorchZfloat16)rr r!r"ZFUNS_IO_TYPE_FP32ZFUNS_IO_TYPE_FP16ZFUNS_IO_TYPE_INT8ZFUNS_IO_TYPE_FP32_OR_INT8ZMODS_IO_TYPE_FP32ZMODS_IO_TYPE_INT8MODS_IO_TYPE_FP32_OR_INT8ZMETHS_IO_TYPE_FP32_OR_INT8Z first_argZ_prev_node_input_typeZprev_node_output_typeZis_known_fp32_input_moduleZis_known_int8_input_module"is_known_fp32_or_int8_input_module prev_nodeZcur_node_dtype_targetr)r*rr3&s                        r3)rr r"r#csD|jd}t|tsdS|d}dd}|jdkrx|jtjkrL|||ddS|jtjtj tj tj fkrt|||dd SdS|jd kr@t|jt st t||jttjtjtjtjtjtjtjtjtjtjtjtjtjtjtjtj tj!tj"tj#tj$tj%tjtj&tj'frj(j)fSt*fd d |D}|r@t+|||SdS) z{ Returns the qparams (scale, zero_point) of the first input to `node`, if they can be inferred from the graph. rNr$cSsj|j||j|}}t|tr,t|jts0tt|trFt|jtsJtt||j}t||j}||fS)N)r2r'r r/r1r0r )rr Z scale_arg_idxZ zp_arg_idxZ scale_nodeZzp_nodeZ scale_objZzp_objrrr _get_scale_zp_from_function_argss   z@get_node_input_qparams.._get_scale_zp_from_function_argsr%rr&c3s|]}t|VqdS)N)r')r(r)) module_objrrr+sz)get_node_input_qparams..),r2r'r r.r/r5Zquantize_per_tensortoqaddZadd_relumulZmul_relur1r0r nnqZLinearZConv1dZConv2dnniqZ ConvReLU2dZConv3dZ BatchNorm2dZ BatchNorm3dZConvTranspose1dZConvTranspose2dZELUZ GroupNormZInstanceNorm1dZInstanceNorm2dZInstanceNorm3dZ LayerNormZ HardswishZ LeakyReLUZReLU6ZBNReLU2dZBNReLU3dZ ConvReLU1dZ ConvReLU3dZ LinearReLUZscaleZ zero_pointr4get_node_input_qparams)rr r"r8r6r9r7r)r<rrBs\         rB)rr r#cCs|jdkrt||j}t|rt|jdks0tt|jdtsDt|jd}t|jt s^tt||j}t|rt|jdkstt|jdtst|jd}|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&rr) r.r r/rlenr2r0r'r r1)rr node_objrrrreturn_first_non_observer_nodes     rEcCs*|jdkr&t||j}t|tjr&dSdS)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)r.r r/r'nnZLSTM)rr rDrrrget_number_of_non_param_argss    rG)rr#cCst|jdkrgS|jdkr|jtjtjjjtjfksP|jtj tjjj tj fkrg}x,t dD] }t |j|t kr^|j |q^W|SdgS)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] rr%r:)rCr2r.r/r5r>ops quantizedoperatorr?rangetyper append)rresultirrr get_arg_indices_of_inputs_to_log"s  rPcCsPd}|jdkrtj|j}n0|jdkrLt|jts6tt||j}tj|}|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&)r%r,)r.r5typenamer/r'r1r0r )rr r)Z target_modrrrget_target_type_str=s    rS)results model_namer#c Csi}x|jD]v\}}d}xN|jD]B\}}x8|jD],\}} ||kr6t| sRt| dd}q6q6q6Wq$W|dk r||||<q|||<qW|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. NrZ ref_node_name)itemsrCr0) rTrUZ new_resultsZold_layer_nameresult_type_to_resultsZnew_layer_nameZ _result_typemodel_name_to_resultsZcur_model_nameZlist_of_resultsrrr'rekey_logger_info_on_node_name_of_modelLs    rY)rTr#c Csd}xf|jD]Z\}}xN|jD]B\}}x6|jD]*\}}t|dkr2|dddk r2|}Pq2WPq WPqW|rx|jD]t\}}xj|jD]^\}}||}xL|jD]@\}}||krqx,tt|D]} || d} | || d<qWqWqWqzWdS)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. Nrfqn)rVrCrK) rTZmodel_name_with_fqnsZ layer_namerWZ result_typerXrUZ model_resultsZref_model_resultsrOrZrrrmaybe_add_missing_fqnsss(   r[csfddS)Nc s|^}}}t|trt|ts2t|trnt|trng}x2t||D]$\}}||f|}|j||qBW|St|tjrt|tjr|jr|j}|jr|j}|j tj ks|j tj krdS||f|}||S)N) r'tuplelistziprMr5TensorZ is_quantizedr-Zdtypefloat) r2kwargsZa0Za1Za_otherrTZel0Zel1Znew_args)finnerrrrcs"   zGmaybe_dequantize_first_two_tensor_args_and_handle_tuples..innerr)rbr)rbrcr8maybe_dequantize_first_two_tensor_args_and_handle_tuplessrd)xyr#cCs*tj|}tj||}dtj||S)N)r5Znormlog10)rerfZPsZPnrrr compute_sqnrs ricCs"tj||dj|djS)Nr:)r5sqrtsum)rerfrrrcompute_normalized_l2_errorsrlcCs(|jdd}|jdd}tjjj||S)Nrrm)Zreshaper5rFZ functionalZcosine_similarity)rerfrrrcompute_cosine_similaritys  rn)5rrJr5Ztorch.nnrFZtorch.nn.intrinsic.quantizedZ intrinsicrIrAZtorch.nn.quantizedr@rHr=typingrrrrrrrZtorch.fxr Ztorch.fx.graphr Ztorch.quantizationr r Ztorch.quantization.utilsr Ztorch.quantization.quantizerZns_typesrrEnumrr1r3r_r`intrBrErGrPrSrYr[rdrirlrnrrrrsT  $    p.N$"