o
    ,ib1                  	   @   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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 d dlmZ eeeedeeeeef	Zdd ZdZd	d
 Z G dd de
Z!G dd de j"Z"G dd dZ#dd Z$dS )    N)FunctionType
ModuleType)AnyDictOptionalSetUnion)
deprecated)mockc                    sx   G dd dt } fddt  t t}d  __t  _|_|_	d_
d_dS )z
    Converts a module-level config into a `ConfigModule()`.

    See _config_typing.pyi for instructions on how to get the converted module to typecheck.
    c                   @   s   e Zd ZeddhZdS )z3install_config_module.<locals>.ConfigModuleInstance	_is_dirty_hash_digestN)__name__
__module____qualname__set_bypass_keys r   r   X/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/utils/_config_module.pyConfigModuleInstance   s    r   c              	      s   t | j D ]l\}}|ds!t|ttfs!t|dr"|jdkr"q| | }t|t	r?| |< ||< |u r>t
| qt|trd|jjksLJ t| d}||| d t||| qtd| d| dt| dd	S )
z?Walk the module structure and move everything to module._config__r   typing.zUnhandled config =z ()N)list__dict__items
startswith
isinstancer   r   hasattrr   CONFIG_TYPESdelattrtyper   SubConfigProxysetattrAssertionError)sourcedestprefixkeyvaluenameproxyconfigdefaultmodulevisitr   r   r1      s.   



 z$install_config_module.<locals>.visit TN)ConfigModuledict-get_assignments_with_compile_ignored_comments_config_defaultr   keys_allowed_keys_compile_ignored_keys	__class__r   r   )r0   r   compile_ignored_keysr   r-   r   install_config_module   s   
r=   z@compile_ignoredc                 C   s   t | }t }tt|dj}d}d}|D ]W}|jtj	kr@d}|j
 }t|v r?|dks8J dt ||jd f}q|jtjkrL|sK|j
}q|jtjkrq|j
dkrqt|d v ro|d |jd d kro|| d}d}q|dks}J dt |S )Nutf-8)r2   r2   zunconsumed r   r      )inspect	getsourcer   tokenizeioBytesIOencodereadliner"   COMMENTstringstripCOMPILE_IGNORED_MARKERstartNAMEOPadd)r0   source_codeassignmentstokenscurrent_comment	prev_nametokenmaybe_currentr   r   r   r5   J   s8   



r5   c                       sr  e Zd ZU eeef ed< eeef ed< ee ed< ee ed< ee ed< eed< e	e
 ed< dd	 Z fd
dZdd Zdd Zde
fddZdeeef fddZdefddZde
fddZededdeeef fddZdeeef fddZdee
eeef f dd fd!d"Zdeeef fd#d$Z	 	 d+d%e	eeeeef f  d&efd'd(Zd)d* Z  ZS ),r3   r7   r6   r9   r   r:   r   r   c                 C   s   t dt d)Nzuse z-.install_config_module(sys.modules[__name__]))NotImplementedErrorr   selfr   r   r   __init__   s   
zConfigModule.__init__c                    sJ   || j v rt || d S || jvrt| j d| d|| j|< d S Nr   z does not exist)r   super__setattr__r9   AttributeErrorr   r6   rY   r+   r*   r;   r   r   r]      s
   

zConfigModule.__setattr__c              
   C   s>   z| j | W S  ty } zt| j d| d|d }~ww r[   )r6   KeyErrorr^   r   )rY   r+   er   r   r   __getattr__   s   zConfigModule.__getattr__c                 C   s   | j |= d S Nr6   rY   r+   r   r   r   __delattr__   s   zConfigModule.__delattr__returnc                 C   s4   t | j}|ddD ]}|| qtj|ddS )z Convert config to a pickled blob_save_config_ignorer      )protocol)r4   r6   getpoppickledumps)rY   r.   r)   r   r   r   save_config   s   
zConfigModule.save_configc                    sP   i }t | jD ]  drqt fdd| jd D rq| j  | < q|S )z!Convert config to portable format_c                 3   s    | ]}  |V  qd S rd   )r   ).0rb   r)   r   r   	<genexpr>   s    

z4ConfigModule.save_config_portable.<locals>.<genexpr>_cache_config_ignore_prefix)sortedr6   r   any)rY   r.   r   rs   r   save_config_portable   s   
z!ConfigModule.save_config_portablec                 C   sf   g }| j }| j D ]#\}}|| jddv rq
|| j| kr q
|| d| d| q
d|S )zConvert config to Python statements that replicate current config.
        This does NOT include config settings that are at default values.
        ri   r   r   z = 
)r   r6   r   rl   r7   appendjoin)rY   linesmodkvr   r   r   codegen_config   s   
zConfigModule.codegen_configc                    sZ    j s jdu r* fdd j D }tt| }t|d	  _d _  jS )z/Hashes the configs that are not compile_ignoredNc                    s    i | ]\}}| j vr||qS r   )r:   )rr   r~   r   rX   r   r   
<dictcomp>   s
    
z)ConfigModule.get_hash.<locals>.<dictcomp>r>   F)
r   r   r6   r   reprrv   hashlibmd5rF   digest)rY   dict_to_hashstring_to_hashr   rX   r   get_hash   s   
zConfigModule.get_hashz`config.to_dict()` has been deprecated. It may no longer change the underlying config. use `config.shallow_copy_dict()` or `config.get_config_copy()` instead)categoryc                 C   s   |   S rd   )shallow_copy_dictrX   r   r   r   to_dict   s   zConfigModule.to_dictc                 C   s
   i | j S rd   re   rX   r   r   r   r      s   
zConfigModule.shallow_copy_dictmaybe_pickled_configNc                 C   s*   t |tst|}n|}| j| dS )zARestore from a prior call to save_config() or shallow_copy_dict()N)r   r4   rn   loadsr6   update)rY   r   r.   r   r   r   load_config   s   
zConfigModule.load_configc                 C   s   t | jS rd   )copydeepcopyr6   rX   r   r   r   get_config_copy   s   zConfigModule.get_config_copyarg1arg2c                    s   |dur"|durt |tsJ ||i n	t |tsJ | |r!J n| |du s*J t  ts8J dt  i | dG  fdddt}| S )a  
        Decorator and/or context manager to make temporary changes to a config.

        As a decorator:

            @config.patch("name", val)
            @config.patch(name1=val1, name2=val2)
            @config.patch({"name1": val1, "name2", val2})
            def foo(...):
                ...

        As a context manager:

            with config.patch("name", val):
                ...
        Nzexpected `dict` got Fc                       s.   e Zd Z fddZfddZdS )z'ConfigModule.patch.<locals>.ConfigPatchc                    sD   rJ    D ]}j| |< |jvqj  _d S rd   )r8   r6   r:   r   r   )rY   r)   changesr.   dirtypriorr   r   	__enter__	  s   
z1ConfigModule.patch.<locals>.ConfigPatch.__enter__c                    s    j   _  d S rd   )r6   r   r   clearrY   exc_typeexc_valexc_tb)r.   r   r   r   r   __exit__  s   z0ConfigModule.patch.<locals>.ConfigPatch.__exit__N)r   r   r   r   r   r   r   r   r   ConfigPatch  s    
r   )r   strr4   r"   ContextDecorator)rY   r   r   kwargsr   r   r   r   patch   s   

zConfigModule.patchc                    s   | j  fdd}|S )aV  
        A lower-overhead version of patch() for things on the critical path.

        Usage:

            # do this off the critical path
            change_fn = config.make_closure_patcher(foo=True)

            ...

            revert = change_fn()
            try:
              ...
            finally:
                revert()

        c                     s.   fddD     fdd} | S )Nc                    s   i | ]}| | qS r   r   )rr   r~   )r.   r   r   r   0  s    zFConfigModule._make_closure_patcher.<locals>.change.<locals>.<dictcomp>c                      s      d S rd   r   r   )r.   r   r   r   revert3  s   zBConfigModule._make_closure_patcher.<locals>.change.<locals>.revertr   )r   r   r.   )r   r   change/  s   
z2ConfigModule._make_closure_patcher.<locals>.changere   )rY   r   r   r   r   r   _make_closure_patcher  s   	z"ConfigModule._make_closure_patcher)NN)r   r   r   r   r   r   __annotations__r   boolr   bytesrZ   r]   rc   rg   rp   rx   r   r   r	   FutureWarningr   r   r   r   r   r   r   __classcell__r   r   r`   r   r3   p   s@   
 "
=r3   c                       s0   e Zd ZdZdd Zdd Z fddZ  ZS )r   z[
    Same as contextlib.ContextDecorator, but with support for
    `unittest.TestCase`
    c                 C      t dNNYIrW   rX   r   r   r   r   A     zContextDecorator.__enter__c                 C   r   r   r   r   r   r   r   r   D  r   zContextDecorator.__exit__c                    sR   t |tr#t|tjr#G  fddd|}|j|_|j|_|j|_|S t 	|S )Nc                       s4   e Zd Ze fddZe fddZ  ZS )z,ContextDecorator.__call__.<locals>._TestCasec                    s8      zt   W d S  ty   d d d   w rd   )r   r\   
setUpClass	Exceptionr   clsr;   rY   r   r   r   K  s   z7ContextDecorator.__call__.<locals>._TestCase.setUpClassc                    s0   zt    W d d d  d S d d d  w rd   )r\   tearDownClassr   r   r   r   r   r   T  s   "z:ContextDecorator.__call__.<locals>._TestCase.tearDownClass)r   r   r   classmethodr   r   r   r   rX   r`   r   	_TestCaseJ  s
    r   )
r   r"   
issubclassunittestTestCaser   r   r   r\   __call__)rY   funcr   r`   rX   r   r   G  s   zContextDecorator.__call__)r   r   r   __doc__r   r   r   r   r   r   r`   r   r   ;  s
    r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
r#   zn
    Shim to redirect to main config.
    `config.triton.cudagraphs` maps to _config["triton.cudagraphs"]
    c                    s    t  d| t  d| d S )Nr6   _prefix)r\   r]   )rY   r.   r(   r`   r   r   rZ   j  s   zSubConfigProxy.__init__c                 C   s   | j | j| |S rd   )r6   r]   r   r_   r   r   r   r]   o  s   zSubConfigProxy.__setattr__c                 C      | j | j| S rd   )r6   rc   r   rf   r   r   r   rc   r     zSubConfigProxy.__getattr__c                 C   r   rd   )r6   rg   r   rf   r   r   r   rg   u  r   zSubConfigProxy.__delattr__)	r   r   r   r   rZ   r]   rc   rg   r   r   r   r`   r   r#   d  s    r#   c                 C   s&   t | tr| ||S tj| ||S )z@
    Workaround `mock.patch.object` issue with ConfigModule
    )r   r3   r   r
   object)objr+   r*   r   r   r   patch_objecty  s   
r   )%
contextlibr   r   rA   rD   rn   rC   r   typesr   r   r   r   r   r   r   r   typing_extensionsr	   r
   intfloatr   r"   r   r   r   tupler4   r    r=   rK   r5   r3   r   r#   r   r   r   r   r   <module>   s*   2& L)