U
    Kºc  ã                   @   sÐ   d dl Z d dlmZmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ dd	d
dddgZeeeef eedœdd	„Zdd„ Zdd
„ Zdd„ Zeeeeef ef dœdd„Zeeeef dœdd„ZdS )é    N)ÚDictÚCallableÚUnioné   )Úget_combined_dictÚ_parent_name)Úget_default_qat_module_mappings)Ú
QConfigAny)ÚQConfigMappingÚget_flattened_qconfig_dictÚget_object_type_qconfigÚget_module_name_qconfigÚget_module_name_regex_qconfigÚ,maybe_adjust_qconfig_for_module_type_or_nameÚupdate_qconfig_for_qat)Úqconfig_mappingÚobject_typeÚfallback_qconfigÚreturnc                 C   s   | j  ||¡S ©N)Úobject_type_qconfigsÚget)r   r   r   © r   úO/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/qconfig_mapping_utils.pyr      s    c                 C   s,   | j  ¡ D ]\}}t ||¡r
|  S q
|S r   )Zmodule_name_regex_qconfigsÚitemsÚreÚmatch)r   Úmodule_namer   Zregex_patternÚqconfigr   r   r   r   !   s    
c                 C   s<   |dkr|S || j kr | j | S t|ƒ\}}t| ||ƒS d S )NÚ )Úmodule_name_qconfigsr   r   )r   r   r   ÚparentÚ_r   r   r   r   )   s    

c                 C   s(   t | ||ƒ}t| ||ƒ}t| ||ƒ}|S r   )r   r   r   )r   Úmodule_typer   Úglobal_qconfigZmodule_type_qconfigZmodule_name_regex_qconfigZmodule_name_qconfigr   r   r   r   4   s       ÿ  ÿ  ÿ)r   r   c                 C   sF   d| j i}| j ¡ D ]\}}|||< q| j ¡ D ]\}}|||< q0|S )a   flatten the global, object_type and module_name qconfig
    to the same qconfig_dict so that it can be used by
    propagate_qconfig_ function.
    "module_name_regex" is ignored for now since it's not supported
    in propagate_qconfig_, but it can be fixed later.

    For example:
    Input: {
      "": qconfig,
      "object_type": [
        (torch.add, qconfig)
      ],
      "module_name": [
        ("conv", qconfig)
      ]
    }

    Output: {
      "": qconfig,
      torch.add: qconfig,
      "conv": qconfig
    }
    r   )r$   r   r   r    )r   Z	flattenedÚobjr   r   r   r   r   A   s    


)r   Úadditional_qat_module_mappingc                 C   sD   t tƒ |ƒ}| j}| ¡ }| ¡ D ]\}}||kr"|||| < q"dS )z³
    Update the qconfig_dict to account for module swaps during QAT.
    During QAT we perform a module swap on the nn.Module types to the corresponding nn.qat.modules types.
    N)r   r   r   Úcopyr   )r   r&   Zall_qat_mappingsZobject_type_dictZnew_object_type_dictÚkÚvr   r   r   r   a   s     ÿ)r   Útypingr   r   r   Úutilsr   r   Zquantization_mappingsr   r   r	   r   r
   Ú__all__Ústrr   r   r   r   r   r   r   r   r   r   Ú<module>   s0   ú
 ý !
þ