
    Pi+                         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e j                  Z G d dej                  Zd	ed
eeef         fdZh dZdeeef         d
efdZdS )    N)AnyDict)AOBaseConfigconfig_from_dictconfig_to_dictALLOWED_AO_MODULES   c                   $    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__     g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/core/config.pyr   r      s3          0 $GS#####r   r   c                   (     e Zd ZdZ fdZd Z xZS )ConfigJSONEncoderz,Custom JSON encoder for AOBaseConfig objectsc                     t          |t                    rxi }|j                                        D ]8\  }}|                    d          s|dk    r                     |          ||<   9|j        j        t          |dt                    |dS t          |d          rht          |d          rX|                                } fd|                                D             }|j        j        t          |dt                    |dS t          j        |          rt          |t                    sxi }t          j        |          D ]=}|j        dk    r0                     t          ||j                            ||j        <   >|j        j        t          |dt                    |dS t          |d          rK|j        dk    r@t          |t                    r+d	t%          |                              d
          d         dS t          |d          r@d|j        j        v r2|j        j         fd|j                                        D             dS t          |t(          j                  r|j        j         |j        dS t          |t,          j                  r+d	t%          |                              d
          d         dS t          |t0                    r fd|D             S t          |t2                    rt5          d|           t          |t6                    r  fd|                                D             S t9                                          |          S )N_r   )_type_version_data_fields_asdictc                 B    i | ]\  }}|                     |          S r   encode_value.0kvselfs      r   
<dictcomp>z-ConfigJSONEncoder.default.<locals>.<dictcomp>`   s-    VVV$!Qa!2!21!5!5VVVr   r   torchtorch.dtype.r   r   	__class__Layoutc                 l    i | ]0\  }}|                     d           |                    |          1S )r   )
startswithr!   r"   s      r   r'   z-ConfigJSONEncoder.default.<locals>.<dictcomp>   sO       1<<,,t((++  r   c                 :    g | ]}                     |          S r   r    )r#   itemr&   s     r   
<listcomp>z-ConfigJSONEncoder.default.<locals>.<listcomp>   s'    :::D%%d++:::r   iTuples will be serialized as List in JSON, so we recommend to use Lists instead to avoid surprises. got: c                 B    i | ]\  }}|                     |          S r   r    r"   s      r   r'   z-ConfigJSONEncoder.default.<locals>.<dictcomp>   s-    BBB1At((++BBBr   )
isinstancer   __dict__itemsr0   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fr-   s	   `       r   rJ   zConfigJSONEncoder.defaultI   s   a&& 	I
((** 8 81||C(( 8Q)^^#'#4#4Q#7#7IaL -#Ay2BCC"	   1i   	Wy&
 &
 	 ))++KVVVV+BSBSBUBUVVVN -#Ay2BCC'   #A&& 	z!T/B/B 	I '** N N6Y&&(,(9(9'!QV:L:L(M(MIaf% -#Ay2BCC"	   1l## 	L(?(?JqRVDWDW(?*SVV\\#5F5Fr5JKKK 1k"" 	x1;3G'G'G-    !
 0 0 2 2     a## 	I ! 46HHHa%% 	L*SVV\\#5F5Fr5JKKK a 	::::::::5!! 	%>:;> >  
 a 	CBBBB		BBBB wwq!!!r   c                 V    	 |                      |          }|S # t          $ r Y nw xY w|S )z+Helper method to recursively encode a value)rJ   	TypeError)r&   valueresults      r   r!   zConfigJSONEncoder.encode_value   sF    	 \\%((FM 	 	 	D	
 s    
&&)r   r   r   r   rJ   r!   __classcell__)r-   s   @r   r   r   F   sS        66S" S" S" S" S"j      r   r   configreturnc                     t          | t                    st          dt          |                      t	          j        t	          j        | t                              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)r6   r   rQ   r=   jsonloadsdumpsr   )rU   s    r   r   r      sV     fl++ OMtF||MMNNN :dj->???@@@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.quantization%torchao.quantization.quantize_.common(torchao.quantization.quantize_.workflowsdatac                 T   t          | t                    st          dt          |                      d| vsd| vrt	          d          | d         }|                     dt                    }| d         }|dk    rddl}t          ||          S d}t          D ]?}	 t          j        |          }t          ||          } n# t          t          f$ r Y <w xY w|/d	                    t                    }t	          d
| d|           t          |dt                    }	||	k    rt          j        d|d|	d           t          |t                    s?t#          |t$          j                  rt          ||          S 	  ||          S #  |cY S xY w||	k    rd|i}
ni }
|                                D ]\  }}t          |t                    rd|v rd|v rt+          |          |
|<   5t          |t,                    rd |D             |
|<   Zt          |t.                    rt1          d|           t          |t                    r"d |                                D             |
|<   ||
|<   	  |di |
S # t2          $ r}t	          d|j         d|           d}~ww xY 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                 j    g | ]0}t          |t                    rd |v rd|v rt          |          n|1S r,   r6   rH   r   )r#   r2   s     r   r3   z$config_from_dict.<locals>.<listcomp>  s]     # # #  dD)).5oo'T// !&&&# # #r   r4   c                 r    i | ]4\  }}|t          |t                    rd |v rd|v rt          |          n|5S r,   ri   )r#   r$   r%   s      r   r'   z$config_from_dict.<locals>.<dictcomp>%  sd     # # # Aq a&&+2a<<GqLL $A&&&# # #r   zFailed to create instance of z: r   )r6   rH   rQ   r=   
ValueErrorgetr   r(   r9   r   	importlibimport_moduleImportErrorAttributeErrorjoinwarningswarn
issubclassrB   rC   r8   r   rE   rF   rG   	Exceptionr   )rf   	type_pathstored_versionobj_datar(   rX   module_pathmoduleallowed_modules_strcurrent_default_versionrN   keyrR   es                 r   r   r      s    dD!! B@DJJ@@AAAdgT11VWWWWIXXj*:;;NG}H M!!uh'''
C)  	,[99F&),,CE^, 	 	 	H	 {"ii(:;;dIddObdd
 
 	
 &c96FGG000 lXf  l  l  lC  l  l  l	
 	
 	

 h%% 	 c49%% 	 3))) s8}}$  000#^4nn&& ( (
UeT"" 	(w%'7'7Gu<L<L"25"9"9N3t$$ 	(# # "	# # #N3 u%% 	(%B:?B B   t$$ 		(# # "KKMM	# # #N3 #(N3Ns$$^$$$ N N NLLLLLMMMNs6   $CCC
F F6I> >
J'J""J')abcr;   rB   rm   rY   rr   typingr   r   r(   __all__r   ABCr   JSONEncoderr   r@   r   r   r   r   r   r   <module>r      sf   


                        ($ ($ ($ ($ ($37 ($ ($ ($Ve e e e e( e e ePA< ADcN A A A A"   eN4S> eNl eN eN eN eN eN eNr   