o
    <&i1                  	   @   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Zd dl	m
Z
mZ d dlmZmZmZmZmZ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TupleUnion)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   EC:\wamp64\www\opt\env\Lib\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   Zcompile_ignored_keysr   r-   r   install_config_module   s   
r<   z@compile_ignoredc           	      C   s(  t | }t }tt|dj}d}d}d}|D ]j}|jtj	krR|j
 }t|v rQ|dks8J dt ||jd f}|jd |d krQ||d  d}q|jtjkr\|j
}q|jtjkr|j
dkr||jd f}t|d v r|d |jd d kr|| d}q|dksJ 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addNAMEOP)	r0   Zsource_codeZassignmentstokensZcurrent_commentZ	prev_nameZprev_assignedtokenZmaybe_currentr   r   r   r5   I   s:   



r5   c                       s  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fddZdeeef deeeef e
f fddZdeeef de
fddZde
fddZ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  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   rS   r+   r*   r;   r   r   rW      s
   

zConfigModule.__setattr__c              
   C   s>   z| j | W S  ty } zt| j d| d|d }~ww rU   )r6   KeyErrorrX   r   )rS   r+   er   r   r   __getattr__   s   zConfigModule.__getattr__c                 C   s   | j |= d S Nr6   rS   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)rS   r.   r)   r   r   r   save_config   s   
zConfigModule.save_configc                 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.
        rc   r   r   z = 
)r   r6   r   rf   r7   appendjoin)rS   linesmodkvr   r   r   codegen_config   s   
zConfigModule.codegen_configupdatesc                    sR   t  fdd|D rtd fdd j D }||  |}||fS )zCHashes the configs that are not compile_ignored, along with updatesc                 3   s    | ]}| j v V  qd S r^   r:   ).0rp   rR   r   r   	<genexpr>   s    z@ConfigModule.get_config_and_hash_with_updates.<locals>.<genexpr>z&update keys cannot be @compile_ignoredc                        i | ]\}}| j vr||qS r   rt   ru   rp   rq   rR   r   r   
<dictcomp>   s    zAConfigModule.get_config_and_hash_with_updates.<locals>.<dictcomp>)any
ValueErrorr6   r   update	_get_hash)rS   rs   cfghashedr   rR   r    get_config_and_hash_with_updates   s   


z-ConfigModule.get_config_and_hash_with_updatesr.   c                 C   s$   t t| }t|d S )Nr=   )reprsortedr   hashlibmd5rE   digest)rS   r.   Zstring_to_hashr   r   r   r}      s   zConfigModule._get_hashc                    s@    j s jdu r fdd j D } | _d _  jS )z/Hashes the configs that are not compile_ignoredNc                    rw   r   rt   rx   rR   r   r   ry      s
    
z)ConfigModule.get_hash.<locals>.<dictcomp>F)r   r   r6   r   r}   )rS   Zdict_to_hashr   rR   r   get_hash   s   
zConfigModule.get_hashc                 C   s   t dt |  S )Nzconfig.to_dict() has been deprecated. It may no longer change the underlying config. use config.shallow_copy_dict() or config.get_config_copy() instead)warningswarnDeprecationWarningshallow_copy_dictrR   r   r   r   to_dict   s
   zConfigModule.to_dictc                 C   s
   i | j S r^   r_   rR   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   rh   loadsr6   r|   )rS   r   r.   r   r   r   load_config   s   
zConfigModule.load_configc                 C   s   t | jS r^   )copydeepcopyr6   rR   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 r^   )r8   r6   r:   r|   r   )rS   r)   changesr.   dirtypriorr   r   	__enter__  s   
z1ConfigModule.patch.<locals>.ConfigPatch.__enter__c                    s    j   _  d S r^   )r6   r|   r   clearrS   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)rS   r   r   kwargsr   r   r   r   patch   s   

zConfigModule.patch)NN)r   r   r   r   r   r   __annotations__r   boolr   bytesrT   rW   r]   ra   rj   rr   r   r   r}   r   r   r   r	   r   r   r   __classcell__r   r   rZ   r   r3   q   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NZNYIrQ   rR   r   r   r   r   &     zContextDecorator.__enter__c                 C   r   r   r   r   r   r   r   r   )  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 r^   )r   rV   
setUpClass	Exceptionr   clsr;   rS   r   r   r   0  s   z7ContextDecorator.__call__.<locals>._TestCase.setUpClassc                    s0   zt    W d d d  d S d d d  w r^   )rV   tearDownClassr   r   r   r   r   r   9  s   "z:ContextDecorator.__call__.<locals>._TestCase.tearDownClass)r   r   r   classmethodr   r   r   r   rR   rZ   r   	_TestCase/  s
    r   )
r   r"   
issubclassunittestZTestCaser   r   r   rV   __call__)rS   funcr   rZ   rR   r   r   ,  s   zContextDecorator.__call__)r   r   r   __doc__r   r   r   r   r   r   rZ   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)rV   rW   )rS   r.   r(   rZ   r   r   rT   O  s   zSubConfigProxy.__init__c                 C   s   | j | j| |S r^   )r6   rW   r   rY   r   r   r   rW   T  s   zSubConfigProxy.__setattr__c                 C      | j | j| S r^   )r6   r]   r   r`   r   r   r   r]   W     zSubConfigProxy.__getattr__c                 C   r   r^   )r6   ra   r   r`   r   r   r   ra   Z  r   zSubConfigProxy.__delattr__)	r   r   r   r   rT   rW   r]   ra   r   r   r   rZ   r   r#   I  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_object^  s   
r   )%
contextlibr   r   r@   rC   rh   rB   r   r   typesr   r   r   r   r   r   r   r   r	   r
   intfloatr   r"   r   r   r   tupler4   r    r<   rJ   r5   r3   r   r#   r   r   r   r   r   <module>   s*     2( 0)