U
    KcK2                     @  sD  U d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
Z
ddlmZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZ d	d
dgZdZdZdZdZdZe
j j!ee
j j"j#ededee
j j$ee
j%ededee
j j&ee
j j'ee
j(ededeiZ)de*d< dddddddZ+d$dd d!d	Z,d%dd d"d
Z-G d#d dZ.dS )&    )annotations)OrderedDict)AnyCallableDictTupleUnionN   )default_weight_fake_quantFixedQParamsFakeQuantize)_PartialWrapper)default_fixed_qparams_range_0to1_observer,default_fixed_qparams_range_neg1to1_observerdefault_placeholder_observerdefault_weight_observer)default_reuse_input_qconfigget_default_qconfigget_default_qat_qconfigQConfig
QConfigAnyget_default_qconfig_mappingget_default_qat_qconfig_mappingQConfigMapping object_typemodule_name_regexmodule_nameZmodule_name_object_type_orderhardsigmoidZhardsigmoid_sigmoidZsigmoid_tanhZtanh_z+Dict[Union[Callable, str], _PartialWrapper]_FIXED_QPARAMS_OP_TO_OBSERVERboolstrint)is_qatbackendversionreturnc                 C  s  | rt ||}n
t||}| r"tnt}|dkr>t|j|d}n|}t|jtd}t |	dt
	tjj|	tjj|	tjj|	tjj|	tjj|	tjj|	tjj|	tjjj|	tjjj|	tjjj|	tjjj|	tjjj|	tjjj|	tjjj|	tjj|	tjjj|	tj|	tjj|	tjj|	tjj |	tjjj!|	tjj"|}i }t#$ D ]V\}	}
|
|kr||
 }n,| rt%j&|
d}n|
}t||d}|||
< |	|	| q|S )zX
    Return the default QConfigMapping for the given quantization type and backend.
    )fbgemmx86)
activationZweightZreshape)observer)'r   r   r
   r   r   r*   r   r   
set_globalset_object_typer   torchnnZConv1dZConv2dZConv3dZConvTranspose1dZConvTranspose2dZConvTranspose3dZLinear
functionalZconv1dZconv2dZconv3dZconv_transpose1dZconv_transpose2dZconv_transpose3dZlinearZReLUZreluZBatchNorm1dZBatchNorm2dZBatchNorm3dZ
layer_normZ	LayerNormr    itemsr   Z	with_args)r$   r%   r&   qconfigZdefault_weightZqconfig_transposeZqconfig_layernormZqconfig_mappingZ!fixed_qparams_observer_to_qconfigZfixed_qparams_opr+   Zfixed_qparams_qconfigr*    r3   I/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/qconfig_mapping.py_get_default_qconfig_mapping:   s    
       	 
               

r5   r(   r'   c                 C  s   t d| |S )a3  
    Return the default QConfigMapping for post training quantization.

    Args:
      * ``backend`` : the quantization backend for the default qconfig mapping, should be
         one of ["x86", "fbgemm" (default), "qnnpack", "onednn"]
      * ``version`` : the version for the default qconfig mapping
    Fr5   r%   r&   r3   r3   r4   r   {   s    
c                 C  s   t d| |S )a4  
    Return the default QConfigMapping for quantization aware training.

    Args:
      * ``backend`` : the quantization backend for the default qconfig mapping, should be
         one of ["x86", "fbgemm" (default), "qnnpack", "onednn"]
      * ``version`` : the version for the default qconfig mapping
    Tr7   r8   r3   r3   r4   r      s    	c                   @  s   e Zd ZdZdd Zdd dddZddd d	d
dZddd dddZddd dddZddddd dddZ	ddddZ
edd dddZdS ) r   a  
    Mapping from model ops to :class:`torch.ao.quantization.QConfig` s.

    The user can specify QConfigs using the following methods (in increasing match priority):

        ``set_global`` : sets the global (default) QConfig

        ``set_object_type`` : sets the QConfig for a given module type, function, or method name

        ``set_module_name_regex`` : sets the QConfig for modules matching the given regex string

        ``set_module_name`` : sets the QConfig for modules matching the given module name

        ``set_module_name_object_type_order`` : sets the QConfig for modules matching a combination
        of the given module name, object type, and the index at which the module appears

    Example usage::

        qconfig_mapping = QConfigMapping()
            .set_global(global_qconfig)
            .set_object_type(torch.nn.Linear, qconfig1)
            .set_object_type(torch.nn.ReLU, qconfig1)
            .set_module_name_regex("foo.*bar.*conv[0-9]+", qconfig1)
            .set_module_name_regex("foo.*", qconfig2)
            .set_module_name("module1", qconfig1)
            .set_module_name("module2", qconfig2)
            .set_module_name_object_type_order("foo.bar", torch.nn.functional.linear, 0, qconfig3)

    c                 C  s*   d | _ t | _t | _t | _t | _d S )N)global_qconfigr   object_type_qconfigsmodule_name_regex_qconfigsmodule_name_qconfigs&module_name_object_type_order_qconfigsselfr3   r3   r4   __init__   s    zQConfigMapping.__init__r   )r9   r'   c                 C  s
   || _ | S )z3
        Set the global (default) QConfig.
        )r9   )r?   r9   r3   r3   r4   r,      s    zQConfigMapping.set_globalzUnion[Callable, str])r   r2   r'   c                 C  s   || j |< | S )z
        Set the QConfig for a given module type, function, or method name.
        If the QConfig for an existing object type was already set, the new QConfig will override the old one.
        )r:   )r?   r   r2   r3   r3   r4   r-      s    
zQConfigMapping.set_object_typer"   )r   r2   r'   c                 C  s   || j |< | S )aO  
        Set the QConfig for modules matching the given regex string.

        Regexes will be matched in the order in which they are registered through this method.
        Thus, the caller should register more specific patterns first, e.g.::

            qconfig_mapping = QConfigMapping()
                .set_module_name_regex("foo.*bar.*conv[0-9]+", qconfig1)
                .set_module_name_regex("foo.*bar.*", qconfig2)
                .set_module_name_regex("foo.*", qconfig3)

        In this example, "foo.bar.conv0" would match qconfig1, "foo.bar.linear" would match qconfig2,
        and "foo.baz.relu" would match qconfig3.

        If the QConfig for an existing module name regex was already set, the new QConfig will override the
        old one while preserving the order in which the regexes were originally registered.
        )r;   )r?   r   r2   r3   r3   r4   set_module_name_regex   s    
z$QConfigMapping.set_module_name_regex)r   r2   r'   c                 C  s   || j |< | S )z
        Set the QConfig for modules matching the given module name.
        If the QConfig for an existing module name was already set, the new QConfig will override the old one.
        )r<   )r?   r   r2   r3   r3   r4   set_module_name   s    
zQConfigMapping.set_module_namer   r#   )r   r   indexr2   r'   c                 C  s   || j |||f< | S )a-  
        Set the QConfig for modules matching a combination of the given module name, object type,
        and the index at which the module appears.

        If the QConfig for an existing (module name, object type, index)  was already set, the new QConfig
        will override the old one.
        )r=   )r?   r   r   rC   r2   r3   r3   r4   !set_module_name_object_type_order   s    z0QConfigMapping.set_module_name_object_type_orderzDict[str, Any]r6   c                 C  sH   t | jtt| j tt| j tt| j	 t
dd | j D iS )a?  
        Convert this ``QConfigMapping`` to a dictionary with the following keys:

            "" (for global QConfig)

            "object_type"

            "module_name_regex"

            "module_name"

            "module_name_object_type_order"

        The values of this dictionary are lists of tuples.
        c                 S  s   g | ]\}}||fqS r3   r3   ).0kvr3   r3   r4   
<listcomp>  s    z*QConfigMapping.to_dict.<locals>.<listcomp>)GLOBAL_DICT_KEYr9   OBJECT_TYPE_DICT_KEYlistr:   r1   MODULE_NAME_REGEX_DICT_KEYr;   MODULE_NAME_DICT_KEYr<   &MODULE_NAME_OBJECT_TYPE_ORDER_DICT_KEYr=   r>   r3   r3   r4   to_dict   s         zQConfigMapping.to_dict)qconfig_dictr'   c                 C  s   |  }t |kr||t   |tg D ]\}}||| q(|tg D ]\}}||| qJ|tg D ]\}}||| ql|t	g D ]\}}}}|
|||| q|S )a[  
        Create a ``QConfigMapping`` from a dictionary with the following keys (all optional):

            "" (for global QConfig)

            "object_type"

            "module_name_regex"

            "module_name"

            "module_name_object_type_order"

        The values of this dictionary are expected to be lists of tuples.
        )rI   r,   getrJ   r-   rL   rA   rM   rB   rN   rD   )clsrP   confr   r2   r   r   rC   r3   r3   r4   	from_dict  s    zQConfigMapping.from_dictN)__name__
__module____qualname____doc__r@   r,   r-   rA   rB   rD   rO   classmethodrT   r3   r3   r3   r4   r      s   	)r(   r   )r(   r	   )/
__future__r   collectionsr   typingr   r   r   r   r   r.   Zfake_quantizer
   r   r+   r   r   r   r   r   r2   r   r   r   r   r   __all__rI   rJ   rL   rM   rN   r/   ZHardsigmoidr0   r   ZSigmoidr   ZSoftmaxZTanhr   r    __annotations__r5   r   r   r   r3   r3   r3   r4   <module>   s^    
             A