
    h+                         S SK r S SKrS SKrS SKrS SKrS SKrS SKJrJr  S SK	r	/ SQr
Sr " S S\ R                  5      r " S S\R                  5      rS	\S
\\\4   4S jr1 SkrS\\\4   S
\4S jrg)    N)AnyDict)AOBaseConfigconfig_from_dictconfig_to_dictALLOWED_AO_MODULES   c                   *    \ rS rSr% Sr \r\\S'   Sr	g)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_.

version N)
__name__
__module____qualname____firstlineno____doc___DEFAULT_VERSIONr   int__annotations____static_attributes__r       M/home/james-whalen/.local/lib/python3.13/site-packages/torchao/core/config.pyr   r      s    0 $GS#r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )ConfigJSONEncoderF   z,Custom JSON encoder for AOBaseConfig objectsc           	      0  > [        U[        5      (       a  0 nUR                  R                  5        H8  u  p4UR	                  S5      (       a  M  US:w  d  M%  U R                  U5      X#'   M:     UR                  R                  [        US[        5      US.$ [        US5      (       a}  [        US5      (       al  UR                  5       nUR                  5        VVs0 s H  u  p4X0R                  U5      _M     nnnUR                  R                  [        US[        5      US.$ [        R                  " U5      (       a  [        U[        5      (       d  0 n[        R                  " U5       HE  nUR                   S:w  d  M  U R                  [        XR                   5      5      X'R                   '   MG     UR                  R                  [        US[        5      US.$ [        US5      (       aE  UR"                  S:X  a5  [        U[        5      (       a   S[%        U5      R'                  S	5      S
   S.$ [        US5      (       a  SUR                  R                  ;   am  UR                  R                  UR                  R                  5        VVs0 s H.  u  p4UR	                  S5      (       a  M  X0R                  U5      _M0     snnS.$ [        U[(        R*                  5      (       a$  UR                  R                   UR                   S.$ [        U[,        R.                  5      (       a   S[%        U5      R'                  S	5      S
   S.$ [        U[0        5      (       a!  U Vs/ s H  oR                  U5      PM     sn$ [        U[2        5      (       a  [5        SU 35      e[        U[6        5      (       a4  UR                  5        VVs0 s H  u  p4X0R                  U5      _M     snn$ [8        T	U ]u  U5      $ s  snnf s  snnf s  snf s  snnf )N_r   )_type_version_data_fields_asdictr   torchtorch.dtype.)r   r    	__class__LayoutiTuples will be serialized as List in JSON, so we recommend to use Lists instead to avoid surprises. got: )
isinstancer   __dict__items
startswithencode_valuer'   r   getattrr   hasattrr"   dataclassesis_dataclasstypefieldsnamer   strsplitenumEnumr#   dtypelisttupleNotImplementedErrordictsuperdefault)
selfo	data_dictkvasdict_dataprocessed_datafitemr'   s
            r   r@   ConfigJSONEncoder.defaultI   si   a&&I

((*||C((Q)^#'#4#4Q#7IL + --#Ay2BC"	  1i  Wy&
 &
 ))+KBMBSBSBUVBU$!a!2!21!55BUNV --#Ay2BC'  ##A&&z!T/B/BI ''*66Y&(,(9(9'!VV:L(MIff% + --#Ay2BC"	  1l##(?JqRVDWDW*SV\\#5Fr5JKK 1k""x1;;3G3G'G-- !"

 0 0 2 2<<, ,A((++ 2  a## ! 4 45HHa%%*SV\\#5Fr5JKK a89:%%d+::5!!%::;> 
 a89	B	A((++	BB wq!!y W>" ; Cs   PP3P)PPc                 L     U R                  U5      nU$ ! [         a     U$ f = f)z+Helper method to recursively encode a value)r@   	TypeError)rA   valueresults      r   r.   ConfigJSONEncoder.encode_value   s5    	 \\%(FM 	 	s    
##r   )	r   r   r   r   r   r@   r.   r   __classcell__)r'   s   @r   r   r   F   s    6S"j r   r   configreturnc                     [        U [        5      (       d  [        S[        U 5       35      e[        R
                  " [        R                  " U [        S95      $ )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)r*   r   rL   r3   jsonloadsdumpsr   )rQ   s    r   r   r      sE     fl++>tF|nMNN ::djj->?@@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           
         [        U [        5      (       d  [        S[        U 5       35      eSU ;  d  SU ;  a  [	        S5      eU S   nU R                  S[        5      nU S   nUS:X  a  SSKn[        XC5      $ Sn[         H%  n [        R                  " U5      n[        Xq5      n  O   Uc&  S	R                  [        5      n[	        S
U SU 35      e[        US[        5      n	X):w  a  [        R                   " SU< SU	< S35        [        U[        5      (       d3  [#        U[$        R&                  5      (       a  [        XS5      $  U" U5      $ X):w  a  SU0n
O0 n
UR)                  5        GH!  u  p[        U[        5      (       a  SU;   a  SU;   a  [+        U5      X'   M6  [        U[,        5      (       aC  U Vs/ s H1  n[        U[        5      (       a  SU;   a  SU;   a  [+        U5      OUPM3     snX'   M  [        U[.        5      (       a  [1        SU 35      e[        U[        5      (       aW  UR)                  5        VVs0 s H4  u  pU[        U[        5      (       a  SU;   a  SU;   a  [+        U5      OU_M6     snnX'   GM  XU'   GM$      U" S0 U
D6$ ! [        [        4 a     GM.  f = f!   Us $ = fs  snf s  snnf ! [2         a   n[	        SUR4                   SU 35      eSnAff = f)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 warningr)   zFailed to create instance of z: r   )r*   r>   rL   r3   
ValueErrorgetr   r#   r/   r   	importlibimport_moduleImportErrorAttributeErrorjoinwarningswarn
issubclassr8   r9   r,   r   r;   r<   r=   	Exceptionr   )rb   	type_pathstored_versionobj_datar#   rT   module_pathmoduleallowed_modules_strcurrent_default_versionrG   keyrM   rI   rD   rE   es                    r   r   r      s-    dD!!3DJ<@AAdgT1VWWWIXXj*:;NG}H M!u''
C)	,,[9F&,C	 * {"ii(:;#I;.NObNcd
 	
 &c96FG0gXfWh  iD  lC  kE  Ek  l	

 h%%c499%%3)) 8}$
 0#^4nn&
eT""w%'7Gu<L"25"9Nt$$ "	# "D dD))go'T/ !& "	#N u%%%::?B  t$$ "KKM	# *DA a&&7a<GqL $A& *	#N #(35 ':N$^$$A ^, 		2 ##  N8bLMMNsB   !JJ3 ;8J<;KK J0/J03J9
K1K,,K1)abcr1   r8   rf   rU   rk   typingr   r   r#   __all__r   ABCr   JSONEncoderr   r6   r   r   r   r   r   r   <module>r}      s             ($377 ($Ve(( ePA< ADcN A" eN4S> eNl eNr   