ó —Àv]c @@sDdZddlmZyddlmZWnek rCdZnXddlZddlZddl Z ddl Z ddl m Z mZmZddl mZmZmZmZddl mZmZdd lmZdd lmZdd lmZdd lmZdd lmZdd lmZddl m!Z!m"Z"ddl#m$Z$d„Z%dddd„Z&de'fd„ƒYZ(de'fd„ƒYZ)d„Z*ddd„Z+dddd„Z,dddd„Z-dd„Z.ddd„Z/dddd „Z0ed!„Z1ed"„Z2d'd(e!ƒdd%dddded&„ Z3dS()sLQuantization module for generating quantized (INT8) models from FP32 models.i(tabsolute_import(tstatsNi(t_LIBt check_calltpy_str(tc_arraytc_strtmx_uintt c_str_array(t NDArrayHandlet SymbolHandle(tSymbol(tload(tndarray(tNDArray(tDataIter(tcputContext(tModulec C@sy|jƒ}i}x`|D]X}|jd ƒr´|tdƒ }||}tjjd|dtj|ƒdtj|ƒddƒ\}} } |||<| ||d <| ||d NDArray th_dict: dict of min/max pairs of layers' output tweight_quantizet bias_quantizet _quantizetdatat min_ranget max_rangetout_typetint8t_mint_maxii(RR( tlist_argumentstendswithtlenR tcontribtquantizetmintmaxtarray( tqsymtparamstth_dictt inputs_nametquantized_paramstnamet original_nametparamtvaltvmintvmaxtoutput((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pyt_quantize_params+s0      $ (Rc C@sûd}|dk r6t|tƒs't‚t|ƒ}ng}d}g}|dk r‡t|ƒ}x$|D]}|jt|ƒƒqgWntƒ}tt j |j t j |ƒt|ƒt|ƒt|ƒtt j|ƒt|ƒt jtƒƒƒt|ƒS(s©Given a symbol object representing a neural network of data type FP32, quantize it into a INT8 network. Parameters ---------- sym : Symbol FP32 neural network symbol. excluded_sym_names : list of strings A list of strings representing the names of the symbols that users want to excluding from being quantized. offline_params : list of strs Names of the parameters that users want to quantize offline. It's always recommended to quantize parameters offline so that quantizing parameters during the inference can be avoided. quantized_dtype: str The quantized destination type for input data. iN(tNonet isinstancetlisttAssertionErrorRtappendRR RRtMXQuantizeSymbolthandletctypestbyrefRRRtc_char_ptc_booltTrueR ( tsymtexcluded_symbolstoffline_paramstquantized_dtypetnum_excluded_symbolst num_offlinetofflinetktout((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pyt_quantize_symbolTs*          t_LayerOutputCollectorcB@s&eZdZddd„Zd„ZRS(sÜSaves layer output NDArray in a dict with layer names as keys and lists of NDArrays as values. The collected NDArrays will be used for calculating the optimal thresholds for quantization using KL divergence. cC@si|_||_||_dS(N(tnd_dictt include_layertlogger(tselfRJRK((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pyt__init__…s  cC@sÈt|ƒ}|jdk r/|j|ƒ r/dStj|tƒ}t|dtƒjt ƒƒ}|j dk rŽ|j j d||j fƒn||j kr´|j |j|ƒn|g|j |RKtcur_pathtsymbol_file_path((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pyt _load_syms c C@s%t|tƒrÜtjjtjjtƒƒ}tjj||ƒ}|jd|ƒt |ƒ}i}i}xd|j ƒD]V\}}|j ddƒ\} } | dkrµ||| RÏRÐRÖR×tctxtexcluded_sym_namest calib_modet calib_datatnum_calib_examplest calib_layerRARKR%R'RrRIRvt qarg_params((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pytquantize_model§s`C       !          (R(RÓ(4RXt __future__RtscipyRR¸R2R9tloggingR½tnumpyR|tbaseRRRRRRRR R RÕR R RÃtR RÊRtioRRØRRtmoduleRR1RGtobjectRHRYRfRxRyRzR‰R·R¼RÆRÒRè(((s;/tmp/pip-install-Qvdv_2/mxnet/mxnet/contrib/quantization.pytsL      " ),   ^