/usr/local/lib64/python3.6/site-packages/torch/ao/quantization/__pycache__
NameSizeModeActions
fake_quantize.cpython-36.pyc139970644editdlrm
fuser_method_mappings.cpython-36.pyc45210644editdlrm
fuse_modules.cpython-36.pyc50120644editdlrm
observer.cpython-36.pyc392270644editdlrm
qconfig.cpython-36.pyc85840644editdlrm
quantization_mappings.cpython-36.pyc68670644editdlrm
quantize.cpython-36.pyc189010644editdlrm
quantize_jit.cpython-36.pyc86310644editdlrm
quant_type.cpython-36.pyc6340644editdlrm
stubs.cpython-36.pyc27160644editdlrm
utils.cpython-36.pyc66890644editdlrm
_correct_bias.cpython-36.pyc44490644editdlrm
_equalize.cpython-36.pyc57040644editdlrm
_learnable_fake_quantize.cpython-36.pyc64550644editdlrm
__init__.cpython-36.pyc2530644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/ao/quantization/__pycache__/quantize.cpython-36.pyc (18901B)
3 Eg^@sJddlZddlZddlZddlZddljZddljjZddl m Z ddl m Z m Z mZmZmZmZmZddlmZmZddlmZmZmZmZddZd/d d Zd0d d Zd dZddZd1ddZ ddZ!ddZ"d2ddZ#ddZ$ddZ%d3ddZ&dej'ddfd d!Z(d4d"d#Z)d5d$d%Z*d6d'd(Z+d7d)d*Z,d+d,Z-d8d-d.Z.dS)9N) _FusedModule))get_default_dynamic_quant_module_mappings(get_default_static_quant_module_mappingsget_default_qat_module_mappings$get_default_qconfig_propagation_listno_observer_set_has_special_act_post_process_get_special_act_post_process) DeQuantStub QuantWrapper)add_module_to_qconfig_obs_ctrdefault_dynamic_qconfigfloat16_dynamic_qconfig!float_qparams_weight_only_qconfigcCst|tjjpt|tjjS)N) isinstancetorch quantizationZ ObserverBaseZFakeQuantizeBase)modulerJ/usr/local/lib64/python3.6/site-packages/torch/ao/quantization/quantize.pyis_activation_post_processsrc Cs|dkrt}|jt||}|j||}t|d|}tjjj||t||}||_x8|j D],\}}|rx|d|n|} t ||||| q`WdS)ayThis is a helper function for `propagate_qconfig_` Args: module: input module qconfig_dict: dictionary that maps from name of submodule to quantization configuration allow_list: list of quantizable modules qconfig_parent: quantization config of parent module, we will fallback to this config when there is no specified config for current module prefix: corresponding prefix of the current module, used as key in qconfig_dict Return: None, module is modified inplace with qconfig attached Nqconfig.) rgettypegetattrrrrZassert_valid_qconfigr named_children_propagate_qconfig_helper) r qconfig_dict allow_listZqconfig_parentprefixZmodule_qconfigZqconfig_with_device_checknamechild module_prefixrrrrs   rcCs|dkr i}t|||dS)aPropagate qconfig through the module hierarchy and assign `qconfig` attribute on each leaf module Args: module: input module qconfig_dict: dictionary that maps from name or type of submodule to quantization configuration, qconfig applies to all submodules of a given module unless qconfig for the submodules are specified (when the submodule already has qconfig attribute) Return: None, module is modified inplace with qconfig attached N)r)rrr rrrpropagate_qconfig_Dsr%cCs |j|S)z3Forward hook that calls observer on the output )activation_post_process)selfinputoutputrrr_observer_forward_hookVsr*cCst|dstd|jtS)Nr&zFExpect activation_post_process attribut already attached to the module)hasattrAssertionErrorregister_forward_hookr*)rrrr%register_activation_post_process_hook[s r.c s|dkrt}|dkri}dkr`t|}t|dksDtdj|t|dkr\tt|ndd ddddd fd d }x |jD]\}}t|t j t j gkrĈ|rˆ|j |_ qt|tr|r||qt|rt|} ||| q|dk r*t||kr*|r||q|r~t||kr~|t|j|} t||| |t|tkr|| qt||||qWt|jdkrt|tjj rt||kr||dS) aAdd observer for the leaf child of the module. This function insert observer module to all leaf child module that has a valid qconfig attribute. Args: module: input module with qconfig attributes for all the leaf modules that we want to quantize device: parent device, if any non_leaf_module_list: list of non-leaf modules we want to add observer Return: None, module is modified inplace with added observer modules and forward_hooks NzSadd_observer_ only works with cpu or single-device CUDA modules, but got devices {}rcSs,|dkr|jn|}|dk r(|j||S)N) activationto)rdevicespecial_act_post_processr0rrrget_activation_post_process}s z2add_observer_..get_activation_post_processcSst|do|jdk S)Nr)r+r)mrrrneeds_observationsz(add_observer_..needs_observationcsH|rDt|t rD|jd|j|t|}|jj|jdddS)zg Adds an activation post process module and register a post hook that calls the module r&F)lastN)rr Z add_modulerr._forward_hooks move_to_endid)r5r3handle)r2r4r6rrinsert_activation_post_processs z5add_observer_..insert_activation_post_process)N)N)rget_unique_devices_lenr,formatnextiterrrnnqZFloatFunctionalZ QFunctionalrr&rrrr from_floatsetattrr add_observer__modulesrnnZ Sequential) rqconfig_propagation_listZnon_leaf_module_listr2custom_module_class_mappingdevicesr<r"r#r3Zobserved_childr)r2r4r6rrE`sF         "rEcCs$dd|jDdd|jDBS)NcSsh|] }|jqSr)r2).0prrr sz&get_unique_devices_..cSsh|] }|jqSr)r2)rKrLrrrrMs) parametersbuffers)rrrrr=sr=cCsNt|jdkr&t|dr&|jr&t|Sx"|jD]\}}t||j|<q0W|S)a{Wrap the leaf child module in QuantWrapper if it has a valid qconfig Note that this function will modify the children of module inplace and it can return a new module which wraps the input module as well. Args: module: input module with qconfig attributes for all the leaf modules that we want to quantize Return: Either the inplace modified module with submodules wrapped in `QuantWrapper` based on qconfig or a new `QuantWrapper` module which wraps the input module, the latter case only happens when the input module is a leaf module and we want to quantize it. rr)r>rFr+rr radd_quant_dequant)rr"r#rrrrPs rPFcCstjjd|dkri}|jdi}|s2tj|}|}|dkrDt}t|ddtdd|j Dspt j dt ||||d|S) aPrepares a copy of the model for quantization calibration or quantization-aware training. Quantization configuration should be assigned preemptively to individual submodules in `.qconfig` attribute. The model will be attached with observer or fake quant modules, and qconfig will be propagated. Args: `model`: input model to be modified in-place `inplace`: carry out model transformations in-place, the original module is mutated `allow_list`: list of quantizable modules `observer_non_leaf_module_list`: list of non-leaf modules we want to add observer `prepare_custom_config_dict`: customization configuration dictionary for prepare function .. code-block:: python # Example of prepare_custom_config_dict: prepare_custom_config_dict = { # user will manually define the corresponding observed # module class which has a from_float class method that converts # float custom module to observed custom module "float_to_observed_custom_module_class": { CustomModule: ObservedCustomModule } } z!quantization_api.quantize.prepareNZ%float_to_observed_custom_module_class)rcss|]}t|do|jVqdS)rN)r+r)rKr5rrr szprepare..zNone of the submodule got qconfig applied. Make sure you passed correct configuration through `qconfig_dict` or by assigning the `.qconfig` attribute directly on submodules)rI) r_C_log_api_usage_oncercopydeepcopyrr%anymoduleswarningswarnrE)modelinplacer observer_non_leaf_module_listZprepare_custom_config_dictrIrHrrrprepares      r]cCslt|drt|jrt|dt}x(|jjD]\}}|tkr0|j|q0Wx|D]}|jj |qTWdS)Nr&) r+rr&delattrsetr8itemsr*addpop)rZhandle_ids_to_removeZ handle_idhook_fnrrr_remove_activation_post_processs    rdcCs4x|jD] }t|q Wt|dr(|`t|dS)zClean up the qconfig left in the module so that new qconfig can be propagated. Args: module: module to be cleaned up rN)children_remove_qconfigr+rrd)rr#rrrrfs   rfcCs\tjjd|dkrt}|s(tj|}|jt|dd||f|t||dd|S)aQuantize the input float model with post training static quantization. First it will prepare the model for calibration, then it calls `run_fn` which will run the calibration step, after that we will convert the model to a quantized model. Args: model: input float model run_fn: a calibration function for calibrating the prepared model run_args: positional arguments for `run_fn` inplace: carry out model transformations in-place, the original module is mutated mapping: correspondence between original module types and quantized counterparts Return: Quantized model. z"quantization_api.quantize.quantizeNT)r[) rrRrSrrTrUevalr]convert)rZrun_fnrun_argsmappingr[rrrquantize#s   rlc CsBtjjd|dkr|tjkrHtjttjttjttj ttj ttj ti}q|tj kr|tjt tjt tjt tj t tj t tj t i}q|tjkrtjti}qtdj|n\t|tr|tjkrt}n.|tj krt }n|tjkrt}ntdt|tt|tj|}|dkrt}|stj|}|jt||t ||dd|S)a}Converts a float model to dynamic (i.e. weights-only) quantized model. Replaces specified modules with dynamic weight-only quantized versions and output the quantized model. For simplest usage provide `dtype` argument that can be float16 or qint8. Weight-only quantization by default is performed for layers with large weights size - i.e. Linear and RNN variants. Fine grained control is possible with `qconfig` and `mapping` that act similarly to `quantize()`. If `qconfig` is provided, the `dtype` argument is ignored. Args: model: input model qconfig_spec: Either: - A dictionary that maps from name or type of submodule to quantization configuration, qconfig applies to all submodules of a given module unless qconfig for the submodules are specified (when the submodule already has qconfig attribute). Entries in the dictionary need to be QConfigDynamic instances. - A set of types and/or submodule names to apply dynamic quantization to, in which case the `dtype` argument is used to specify the bit-width inplace: carry out model transformations in-place, the original module is mutated mapping: maps type of a submodule to a type of corresponding dynamically quantized version with which the submodule needs to be replaced z*quantization_api.quantize.quantize_dynamicNzTDon't know how to quantize with default settings for {}. Provide full qconfig pleasez.Unknown dtype specified for quantize_dynamic: T)r[)!rrRrSqint8rGZLinearr ZLSTMZGRUZLSTMCellZRNNCellZGRUCellZfloat16rZquint8Z EmbeddingBagr ValueErrorr?rr_ RuntimeErrorstrdictzip itertoolsrepeatrrTrUrgr%rh)rZZ qconfig_specZdtyperkr[Zdefault_qconfigrrrquantize_dynamic?sJ              rucCs^tjjd|dkrt}|s(tj|}t|ddt||dddt|t |j dd|S)a Prepares a copy of the model for quantization calibration or quantization-aware training and converts it to quantized version. Quantization configuration should be assigned preemptively to individual submodules in `.qconfig` attribute. Args: model: input model to be modified in-place mapping: dictionary that maps float modules to quantized modules to be replaced. inplace: carry out model transformations in-place, the original module is mutated z%quantization_api.quantize.prepare_qatN)rTF)rkr[remove_qconfig)r\r[) rrRrSrrTrUr%rhr]r_values)rZrkr[rrr prepare_qats   rxcCsLtjjd|stj|}|jt|dd||f|t|dd|S)agDo quantization aware training and output a quantized model Args: model: input model run_fn: a function for evaluating the prepared model, can be a function that simply runs the prepared model or a training loop run_args: positional arguments for `run_fn` Return: Quantized model. z&quantization_api.quantize.quantize_qatT)r[)rrRrSrTrUZtrainrxrh)rZrirjr[rrr quantize_qats    ryTcCs:tjjd|stj|}t||d|d|r6t||S)agConverts submodules in input module to a different module according to `mapping` by calling `from_float` method on the target module class. And remove qconfig at the end if remove_qconfig is set to True. Args: `module`: prepared and calibrated module `mapping`: a dictionary that maps from source module type to target module type, can be overwritten to allow swapping user defined Modules `inplace`: carry out model transformations in-place, the original module is mutated `convert_custom_config_dict`: custom configuration dictionary for convert function .. code-block:: python # Example of convert_custom_config_dict: convert_custom_config_dict = { # user will manually define the corresponding quantized # module class which has a from_observed class method that converts # observed custom module to quantized custom module "observed_to_quantized_custom_module_class": { ObservedCustomModule: QuantizedCustomModule } } z!quantization_api.quantize.convertT)r[convert_custom_config_dict)rrRrSrTrU_convertrf)rrkr[rvrzrrrrhs  rhc Cs|dkrt}|dkri}|jdi}|s4tj|}i}xJ|jD]>\}}t|t rpt||krpt||d|t |||||<qBWx|j D]\}} | |j |<qW|S)aConverts submodules in input module to a different module according to `mapping` by calling `from_float` method on the target module class Args: module: input module mapping: a dictionary that maps from source module type to target module type, can be overwritten to allow swapping user defined Modules inplace: carry out model transformations in-place, the original module is mutated NZ)observed_to_quantized_custom_module_classT) rrrTrUrrrrr{ swap_moduler`rF) rrkr[rzrIZreassignr"modkeyvaluerrrr{s"    r{c Cs|}t|dr|jdk rd}t||kr@|t|j|}d}n"t||krb|t|j|}d}|rx|jjD]}|j|qrWx$|jjD]}|t k r|j |qWt |}t |dkst dj|t |dkrtt|nd}|r|j||S)a Swaps the module if it has a quantized counterpart and it has an `observer` attached. Args: mod: input module mapping: a dictionary that maps from nn module to nnq module Return: The corresponding quantized module of `mod` rNFTr/zQswap_module only works with cpu or single-device CUDA modules, but got devices {}r)r+rrZ from_observedrCZ_forward_pre_hooksrwZregister_forward_pre_hookr8r*r-r=r>r,r?r@rAr1) r}rkrInew_modZswappedZ pre_hook_fnrcrJr2rrrr| s.    r|cCs^dd}t|dr$|j|||d<x4|jD](\}}|rF|||n|}t|||q.WdS)a,Traverse the modules and save all observers into dict. This is mainly used for quantization accuracy debug Args: mod: the top module we want to save all observers prefix: the prefix for the current module target_dict: the dictionary used to save all the observers cSs|dkr |S|dS)Nrrr)r!rrr get_prefix=sz%get_observer_dict..get_prefixr&N)r+r&rget_observer_dict)r} target_dictr!rr"r#r$rrrr5s  r)NNr)NN)NNNN)FNNN)NF)NF)F)NFTN)NFN)r)/rTrsrXrZtorch.nnrGZtorch.nn.quantizedZ quantizedrBZtorch.nn.intrinsicrZ(torch.quantization.quantization_mappingsrrrrrrr Ztorch.ao.quantization.stubsr r Ztorch.quantization.qconfigr r rrrrr%r*r.rEr=rPr]rdrfrlrmrurxryrhr{r|rrrrrsD   $  $  R 6 M   & $*