o
    ‡ZŽhÙ9  ã                   @  sb  U d dl mZ d dlmZ d dl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mZmZmZ g d	¢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< d2dd „Z,d3d4d"d#„Z-d5d4d$d%„Z.d4d&d'„Z/d4d(d)„Z0d6d,d-„Z1g d.¢Z2d/e+d0< G d1d„ dƒZ3dS )7é    )Úannotations)ÚOrderedDict)ÚAnyÚCallableÚ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_quint8_weight_qconfigÚdefault_reuse_input_qconfigÚ%default_symmetric_qnnpack_qat_qconfigÚ!default_symmetric_qnnpack_qconfigÚget_default_qat_qconfigÚget_default_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Úis_qatÚboolÚbackendÚstrÚversionÚintÚreturnr   c                 C  s:  | rt ||ƒ}nt||ƒ}| rtnt}|dv rt|j|d}n|}t|jtd}tƒ  |¡ 	dt
¡ 	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|¡ 	tjjt¡}i }t ¡ D ](\}	}
|
|v r||
 }n| rˆtj|
d}n|
}t||d}|||
< | 	|	|¡ qr|S )zX
    Return the default QConfigMapping for the given quantization type and backend.
    )ZfbgemmÚx86)Ú
activationÚweightZreshape)Úobserver)r   r   r   r   r   r*   r   r   Ú
set_globalÚset_object_typer   ÚtorchÚnnZConvTranspose1dZConvTranspose2dZConvTranspose3dÚ
functionalZconv_transpose1dZconv_transpose2dZconv_transpose3dZ
layer_normZ	LayerNormZPReLUr   r!   Úitemsr	   Z	with_args)r"   r$   r&   ÚqconfigZdefault_weightZqconfig_transposeZqconfig_layernormÚqconfig_mappingZ!fixed_qparams_observer_to_qconfigZfixed_qparams_opr,   Zfixed_qparams_qconfigr*   © r5   úT/var/www/auris/lib/python3.10/site-packages/torch/ao/quantization/qconfig_mapping.pyÚ_get_default_qconfig_mapping=   sL   
ÿÿô
ÿr7   r)   c                 C  ó   t d| |ƒS )a?  
    Return the default QConfigMapping for post training quantization.

    Args:
      * ``backend`` (str) : the quantization backend for the default qconfig mapping, should be
         one of ["x86" (default), "fbgemm", "qnnpack", "onednn"]
      * ``version`` (int) : the version for the default qconfig mapping
    F©r7   ©r$   r&   r5   r5   r6   r   ~   s   
r   c                 C  r8   )a@  
    Return the default QConfigMapping for quantization aware training.

    Args:
      * ``backend`` (str) : the quantization backend for the default qconfig mapping, should be
         one of ["x86" (default), "fbgemm", "qnnpack", "onednn"]
      * ``version`` (int) : the version for the default qconfig mapping
    Tr9   r:   r5   r5   r6   r   ‹   s   	r   c                  C  ó   t } tdd| ƒS )z
    Return a QConfigMapping that uses `torch.ao.quantization.default_symmetric_qnnpack_qconfig`
    as the default QConfig.
    FÚqnnpack)r   Ú1_get_default_qconfig_mapping_with_default_qconfig©Údefault_qconfigr5   r5   r6   Ú&_get_symmetric_qnnpack_qconfig_mapping—   ó   ÿr@   c                  C  r;   )z…
    Return a QConfigMapping that uses `torch.ao.quantization.default_symmetric_qnnpack_qat_qconfig`
    as the default QConfig.
    Tr<   )r   r=   r>   r5   r5   r6   Ú*_get_symmetric_qnnpack_qat_qconfig_mapping¢   rA   rB   r?   r   c                 C  sH   | rt |ƒ}nt|ƒ}| |¡ |j ¡ D ]}|tvr!| ||¡ q|S )zX
    Return a QConfigMapping that uses the provided qconfig as the default QConfig.
    )r   r   r-   Úobject_type_qconfigsÚkeysr!   r.   )r"   r$   r?   r4   Úpatternr5   r5   r6   r=   ­   s   

€r=   )Úglobal_qconfigrC   Úmodule_name_regex_qconfigsÚmodule_name_qconfigsÚ&module_name_object_type_order_qconfigsz	list[str]Ú_QCONFIG_STYLE_ORDERc                   @  sn   e Zd ZdZd$dd„Zd%dd	„Zd&dd„Zd'dd„Zd(dd„Zd)dd„Z	d*dd„Z
d+dd„Ze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)

    r(   ÚNonec                 C  s*   d | _ tƒ | _tƒ | _tƒ | _tƒ | _d S )N)rF   r   rC   rG   rH   rI   ©Úselfr5   r5   r6   Ú__init__è   s   þþzQConfigMapping.__init__rF   r   c                 C  s
   || _ | S )z3
        Set the global (default) QConfig.
        )rF   )rM   rF   r5   r5   r6   r-   ô   s   zQConfigMapping.set_globalr   úUnion[Callable, str]r3   c                 C  ó   || 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.
        )rC   )rM   r   r3   r5   r5   r6   r.   û   s   
zQConfigMapping.set_object_typer   r%   c                 C  rP   )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.
        )rG   )rM   r   r3   r5   r5   r6   Úset_module_name_regex  s   
z$QConfigMapping.set_module_name_regexr   c                 C  rP   )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.
        )rH   )rM   r   r3   r5   r5   r6   Úset_module_name  s   
zQConfigMapping.set_module_namer   Úindexr'   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.
        )rI   )rM   r   r   rS   r3   r5   r5   r6   Ú!set_module_name_object_type_order$  s
   þÿz0QConfigMapping.set_module_name_object_type_orderc                 C  s‚   | j jd }tD ]4}|d|› 7 }t| |ƒ}t|tƒr5t|ƒdkr5| ¡ D ]\}}|d|› d|› 7 }q%q|d|› 7 }q|d S )Nz (z
 r   z
  z: z
))Ú	__class__Ú__name__rJ   ÚgetattrÚ
isinstancer   Úlenr2   )rM   ÚoutputÚ
style_nameZqconfigsÚkeyr3   r5   r5   r6   Ú__repr__3  s   
ÿzQConfigMapping.__repr__údict[str, Any]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 | ]\}}g |¢|‘R ‘qS r5   r5   )Ú.0ÚkÚvr5   r5   r6   Ú
<listcomp>U  s    ÿz*QConfigMapping.to_dict.<locals>.<listcomp>)Ú_GLOBAL_DICT_KEYrF   Ú_OBJECT_TYPE_DICT_KEYÚlistrC   r2   Ú_MODULE_NAME_REGEX_DICT_KEYrG   Ú_MODULE_NAME_DICT_KEYrH   Ú'_MODULE_NAME_OBJECT_TYPE_ORDER_DICT_KEYrI   rL   r5   r5   r6   Úto_dict@  s   ÿûzQConfigMapping.to_dictÚqconfig_dictc                 C  s°   | ƒ }t |v r| |t  ¡ | tg ¡D ]
\}}| ||¡ q| tg ¡D ]
\}}| ||¡ q%| tg ¡D ]
\}}| ||¡ q6| t	g ¡D ]\}}}}| 
||||¡ qG|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.
        )rc   r-   Úgetrd   r.   rf   rQ   rg   rR   rh   rT   )Úclsrj   Úconfr   r3   r   r   rS   r5   r5   r6   Ú	from_dict[  s$   ÿÿÿzQConfigMapping.from_dictN)r(   rK   )rF   r   r(   r   )r   rO   r3   r   r(   r   )r   r%   r3   r   r(   r   )r   r%   r3   r   r(   r   )
r   r%   r   r   rS   r'   r3   r   r(   r   )r(   r%   )r(   r^   )rj   r^   r(   r   )rV   Ú
__module__Ú__qualname__Ú__doc__rN   r-   r.   rQ   rR   rT   r]   ri   Úclassmethodrn   r5   r5   r5   r6   r   É   s    








)r"   r#   r$   r%   r&   r'   r(   r   )r)   r   )r(   r   )r)   r   )r"   r#   r$   r%   r?   r   r(   r   )4Ú
__future__r   Úcollectionsr   Útypingr   r   r   r/   Zfake_quantizer   r	   r,   r
   r   r   r   r   r3   r   r   r   r   r   r   r   r   Ú__all__rc   rd   rf   rg   rh   r0   ZHardsigmoidr1   r   ZSigmoidr   ZSoftmaxZTanhr    r!   Ú__annotations__r7   r   r   r@   rB   r=   rJ   r   r5   r5   r5   r6   Ú<module>   sF   (
ó
A


	