U
    Mhb1                  	   @   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   L/var/www/html/venv/lib/python3.8/site-packages/torch/utils/_config_module.pyConfigModuleInstance   s   r   c              	      s   t | j D ]\}}|dst|ttfst|drD|jdkrDq| | }t|t	r~| |< ||< |krt
| qt|tr|jjkstt| d}||| d t||| qtd| d| dt| dq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   AssertionErrorSubConfigProxysetattr)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 ]}|jtj	krd}|j
 }t|kr|dksptdt ||jd f}q4|jtjkr|s|j
}q4|jtjkr4|j
dkr4t|d kr|d |jd d kr|| d}d}q4|dkstdt |S )Nutf-8)r2   r2   zunconsumed r   r      )inspect	getsourcer   tokenizeioBytesIOencodereadliner"   COMMENTstringstripCOMPILE_IGNORED_MARKERr#   startNAMEOPadd)r0   Zsource_codeZassignmentstokensZcurrent_commentZ	prev_nametokenZmaybe_currentr   r   r   r5   J   s6    




r5   c                       sj  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e
dddZeeef dddZedddZe
dddZededeeef dddZeeef dddZee
eeef f dd d!d"Zeeef dd#d$Zd*e	eeeeef f  ed%d&d'Zd(d) Z  ZS )+r3   r7   r6   r9   r   r:   r   r   c                 C   s   t dt dd S )Nzuse z-.install_config_module(sys.modules[__name__]))NotImplementedErrorr   selfr   r   r   __init__   s    
zConfigModule.__init__c                    sJ   || j krt || n,|| jkr<t| j d| dn
|| 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   sL   z| j | W S  tk
rF } zt| j d| d|W 5 d }~X Y nX d S 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                    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 r^   )r   ).0r\   r)   r   r   	<genexpr>   s    z4ConfigModule.save_config_portable.<locals>.<genexpr>Z_cache_config_ignore_prefix)sortedr6   r   any)rS   r.   r   rm   r   save_config_portable   s    
z!ConfigModule.save_config_portablec                 C   sf   g }| j }| j D ]F\}}|| jddkr0q|| 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_configc                    sZ    j s jdkrT fdd j D }tt| }t|d	  _d _  jS )z/Hashes the configs that are not compile_ignoredNc                    s    i | ]\}}| j kr||qS r   )r:   )rl   rw   rx   rR   r   r   
<dictcomp>   s   
 z)ConfigModule.get_hash.<locals>.<dictcomp>r=   F)
r   r   r6   r   reprro   hashlibmd5rE   digest)rS   Zdict_to_hashZstring_to_hashr   rR   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 r^   )shallow_copy_dictrR   r   r   r   to_dict   s    zConfigModule.to_dictc                 C   s   | j S r^   r_   rR   r   r   r   r      s    zConfigModule.shallow_copy_dictN)maybe_pickled_configrb   c                 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   update)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k	rD|dk	r(t |tst||i nt |ts6t| |rTtn| |dksTtt  tspt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t   D ]}j| |< |jkqj  _d S r^   )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   strr#   r4   r"   ContextDecorator)rS   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   )rl   rw   )r.   r   r   rz   0  s      zFConfigModule._make_closure_patcher.<locals>.change.<locals>.<dictcomp>c                      s      d S r^   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>.changer_   )rS   r   r   r   r   r   _make_closure_patcher  s    	z"ConfigModule._make_closure_patcher)NN)r   r   r   r   r   r   __annotations__r   boolr   bytesrT   rW   r]   ra   rj   rq   ry   r   r	   FutureWarningr   r   r   r   r   r   r   __classcell__r   r   rZ   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   s   t dd S NZNYIrQ   rR   r   r   r   r   A  s    zContextDecorator.__enter__c                 C   s   t dd S r   r   r   r   r   r   r   D  s    zContextDecorator.__exit__c                    sR   t |trFt|tjrFG  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                    s@      zt   W n$ tk
r:   d d d   Y nX d S r^   )r   rV   
setUpClass	Exceptionr   clsr;   rS   r   r   r   K  s    z7ContextDecorator.__call__.<locals>._TestCase.setUpClassc                    s$   zt   W 5  d d d  X d S r^   )r   rV   tearDownClassr   r   r   r   r   T  s    z:ContextDecorator.__call__.<locals>._TestCase.tearDownClass)r   r   r   classmethodr   r   r   r   rR   rZ   r   	_TestCaseJ  s   r   )
r   r"   
issubclassunittestZTestCaser   r   r   rV   __call__)rS   funcr   rZ   rR   r   r   G  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   j  s    zSubConfigProxy.__init__c                 C   s   | j | j| |S r^   )r6   rW   r   rY   r   r   r   rW   o  s    zSubConfigProxy.__setattr__c                 C   s   | j | j| S r^   )r6   r]   r   r`   r   r   r   r]   r  s    zSubConfigProxy.__getattr__c                 C   s   | j | j| S r^   )r6   ra   r   r`   r   r   r   ra   u  s    zSubConfigProxy.__delattr__)	r   r   r   r   rT   rW   r]   ra   r   r   r   rZ   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|   r@   rC   rh   rB   r   typesr   r   r   r   r   r   r   r   typing_extensionsr	   r
   intfloatr   r"   r   r   r   tupler4   r    r<   rJ   r5   r3   r   r$   r   r   r   r   r   <module>   s(   2& L)