o
    i*+                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	Z	g dZ
dZG dd de jZG dd dejZd	ed
eeef fddZh dZdeeef d
efddZdS )    N)AnyDict)AOBaseConfigconfig_from_dictconfig_to_dictALLOWED_AO_MODULES   c                   @   s    e Zd ZU dZ	 eZeed< dS )r   a  
    If a workflow config inherits from this then `quantize_` knows
    how to a apply it to a model. For example::

        # user facing code
        class WorkflowFooConfig(AOBaseConfig): ...
            # configuration for workflow `Foo` is defined here
            bar = 'baz'

        # non user facing code
        @register_quantize_module_handler(WorkflowFooConfig)
        def _transform(
            mod: torch.nn.Module,
            config: WorkflowFooConfig,
        ) -> torch.nn.Module:
            # the transform is implemented here, usually a tensor sublass
            # weight swap or a module swap
            ...

        # then, the user calls `quantize_` with a config, and `_transform` is called
        # under the hood by `quantize_.

    versionN)__name__
__module____qualname____doc___DEFAULT_VERSIONr	   int__annotations__ r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/torchao/core/config.pyr      s   
 r   c                       s(   e Zd ZdZ fddZdd Z  ZS )ConfigJSONEncoderz,Custom JSON encoder for AOBaseConfig objectsc                    s  t |tr-i }|j D ]\}}|ds |dkr  |||< q|jjt|dt	|dS t
|drRt
|drR| } fdd| D }|jjt|dt	|dS t|rt |tsi }t|D ]}|jdkrv t||j||j< qc|jjt|dt	|dS t
|dr|jd	krt |trd
t|dd dS t
|drd|jjv r|jj fdd|j D dS t |tjr|jj |jdS t |tjrd
t|dd dS t |tr fdd|D S t |trtd| t |tr fdd| D S t |S )N_r	   )_type_version_data_fields_asdictc                       i | ]
\}}|  |qS r   encode_value.0kvselfr   r   
<dictcomp>`       z-ConfigJSONEncoder.default.<locals>.<dictcomp>r   torchtorch.dtype.r   r   	__class__Layoutc                    s&   i | ]\}}| d s| |qS )r   )
startswithr   r   r!   r   r   r#      s    
c                    s   g | ]}  |qS r   r   r   itemr!   r   r   
<listcomp>   s    z-ConfigJSONEncoder.default.<locals>.<listcomp>iTuples will be serialized as List in JSON, so we recommend to use Lists instead to avoid surprises. got: c                    r   r   r   r   r!   r   r   r#      r$   )
isinstancer   __dict__itemsr,   r   r*   r
   getattrr   hasattrr   dataclassesis_dataclasstypefieldsnamer   strsplitenumEnumr%   dtypelisttupleNotImplementedErrordictsuperdefault)r"   o	data_dictr   r    asdict_dataprocessed_datafr*   r!   r   rE   I   sh   








zConfigJSONEncoder.defaultc                 C   s&   z|  |}|W S  ty   Y |S w )z+Helper method to recursively encode a value)rE   	TypeError)r"   valueresultr   r   r   r      s   
zConfigJSONEncoder.encode_value)r
   r   r   r   rE   r   __classcell__r   r   rK   r   r   F   s    Ur   configreturnc                 C   s0   t | tstdt|  ttj| tdS )z
    Convert an AOBaseConfig instance to a dictionary suitable for serialization.

    Args:
        config: An instance of AOBaseConfig subclass

    Returns:
        Dict representation of the config
    z$Expected AOBaseConfig instance, got )cls)r1   r   rL   r8   jsonloadsdumpsr   )rP   r   r   r   r      s   

r   >   torchao.dtypestorchao.quantizationtorchao.prototype.awqtorchao.prototype.parqtorchao.sparsity.sparse_apitorchao.prototype.mx_formatstorchao.prototype.parq.quanttorchao.prototype.smoothquanttorchao.prototype.quantization%torchao.quantization.quantize_.common(torchao.quantization.quantize_.workflowsdatac              
   C   s&  t | tstdt|  d| vsd| vrtd| d }| dt}| d }|dkr5ddl}t||S d}t	D ]}zt
|}t||}W  n ttfyS   Y q9w |du rgd	t	}td
| d| t|dt}	||	kr}td|d|	d t |tst|tjrt||S z||W S    | Y S ||	krd|i}
ni }
| D ]H\}}t |trd|v rd|v rt||
|< qt |trdd |D |
|< qt |trtd| t |trdd | D |
|< q||
|< qz|di |
W S  ty } ztd|j d| d}~ww )a"  
    Create an AOBaseConfig subclass instance from a dictionary.

    Args:
        data: Dictionary containing serialized config data

    Returns:
        An instance of the appropriate AOBaseConfig subclass

    Raises:
        ValueError: If deserialization fails for other reasons
    zExpected dictionary, got r   r   z;Input dictionary missing required '_type' or '_data' fieldsr   r&   r   Nz, zFailed to find class z  in any of the allowed modules: r	   zXStored version is not the same as current default version of the config: stored_version=z, current_default_version=z&, please check the deprecation warningc                 S   s2   g | ]}t |trd |v rd|v rt|n|qS r)   r1   rC   r   r-   r   r   r   r/     s    z$config_from_dict.<locals>.<listcomp>r0   c                 S   s8   i | ]\}}|t |trd |v rd|v rt|n|qS r)   rb   r   r   r   r   r#   &  s    z$config_from_dict.<locals>.<dictcomp>zFailed to create instance of z: r   )r1   rC   rL   r8   
ValueErrorgetr   r%   r4   r   	importlibimport_moduleImportErrorAttributeErrorjoinwarningswarn
issubclassr=   r>   r3   r   r@   rA   rB   	Exceptionr
   )ra   	type_pathstored_versionobj_datar%   rR   module_pathmoduleallowed_modules_strcurrent_default_versionrI   keyrM   er   r   r   r      s~   












r   )abcr6   r=   re   rS   rj   typingr   r   r%   __all__r   ABCr   JSONEncoderr   r;   r   r   r   r   r   r   r   <module>   s   +h