o
    h                  	   @   s   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
 d dlmZ eeeedeeeeef	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Set)mockc                    s^   G dd dt } fddt  t d  __t  _|_dS )z@
    Converts a module-level config into a `ConfigModule()`
    c                   @   s   e Zd Ze ZdS )z3install_config_module.<locals>.ConfigModuleInstanceN)__name__
__module____qualname__set_bypass_keys r   r   P/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/config_utils.pyConfigModuleInstance   s    
r   c              	      s   t | j D ]b\}}|dst|ttfrq| | }t|tr5| |< ||< |u r4t| qt|t	rZ|j
jksBJ t| d}||| d t||| qtd| d| dt	| ddS )z?Walk the module structure and move everything to module._config__.zUnhandled config =z ()N)list__dict__items
startswith
isinstancer   r   CONFIG_TYPESdelattrtyper	   r   SubConfigProxysetattrAssertionError)sourcedestprefixkeyvaluenameproxyconfigdefaultmodulevisitr   r   r*      s"   


 z$install_config_module.<locals>.visit N)ConfigModuledict_config_defaultr   keys_allowed_keys	__class__)r)   r   r   r&   r   install_config_module   s   
r3   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< dd Z fddZ	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd ZdddZ  ZS )r,   r/   r.   r1   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__r1   AttributeErrorr   r.   r6   r$   r#   r2   r   r   r:   D   s
   

zConfigModule.__setattr__c                 C   s2   z| j | W S  ty   t| j d| dw r8   )r.   KeyErrorr;   r   r6   r$   r   r   r   __getattr__L   s
   zConfigModule.__getattr__c                 C   s   | j |= d S Nr.   r?   r   r   r   __delattr__S   s   zConfigModule.__delattr__c                 C   s4   t | j}|ddD ]}|| qtj|ddS )z Convert config to a pickled blob_save_config_ignorer      )protocol)r-   r.   getpoppickledumps)r6   r'   r"   r   r   r   save_configX   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.
        rD   r   r   z = 
)r   r.   r   rG   r/   appendjoin)r6   linesmodkvr   r   r   codegen_config_   s   
zConfigModule.codegen_configc                 C   s   |   t| dS )z*Restore from a prior call to save_config()N)to_dictupdaterI   loads)r6   datar   r   r   load_configm   s   zConfigModule.load_configc                 C   s   | j S rA   rB   r5   r   r   r   rT   q   s   zConfigModule.to_dictc                 C   s   t | jS rA   )copydeepcopyr.   r5   r   r   r   get_config_copyt   s   zConfigModule.get_config_copyNc                    sz   |dur|dur||i n| |rJ n| |du sJ t  ts*J dt  i | 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 c                       s*   e Zd Z fddZfddZdS )z'ConfigModule.patch.<locals>.ConfigPatchc                    s4   rJ    D ]	}j| |< qj  d S rA   )r0   r.   rU   )r6   r"   changesr'   priorr   r   	__enter__   s   z1ConfigModule.patch.<locals>.ConfigPatch.__enter__c                    s    j    d S rA   )r.   rU   clear)r6   exc_typeexc_valexc_tb)r'   r^   r   r   __exit__   s   z0ConfigModule.patch.<locals>.ConfigPatch.__exit__N)r   r	   r
   r_   rd   r   r\   r   r   ConfigPatch   s    re   )r   r-   r   ContextDecorator)r6   arg1arg2kwargsre   r   r\   r   patchw   s   

zConfigModule.patch)NN)r   r	   r
   r   strr   __annotations__r   r7   r:   r@   rC   rK   rS   rX   rT   r[   rj   __classcell__r   r   r=   r   r,   4   s   
 r,   c                       s    e Zd ZdZ fddZ  ZS )rf   z[
    Same as contextlib.ContextDecorator, but with support for
    `unittest.TestCase`
    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 rA   )r_   r9   
setUpClass	Exceptionrd   clsr2   r6   r   r   rn      s   z7ContextDecorator.__call__.<locals>._TestCase.setUpClassc                    s0   zt    W d d d  d S d d d  w rA   )r9   tearDownClassrd   rp   rr   r   r   rs      s   "z:ContextDecorator.__call__.<locals>._TestCase.tearDownClass)r   r	   r
   classmethodrn   rs   rm   r   r5   r=   r   	_TestCase   s
    ru   )
r   r   
issubclassunittestTestCaser   r
   r	   r9   __call__)r6   funcru   r=   r5   r   ry      s   zContextDecorator.__call__)r   r	   r
   __doc__ry   rm   r   r   r=   r   rf      s    rf   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 )Nr.   _prefix)r9   r:   )r6   r'   r!   r=   r   r   r7      s   zSubConfigProxy.__init__c                 C   s   | j | j| |S rA   )r.   r:   r|   r<   r   r   r   r:      s   zSubConfigProxy.__setattr__c                 C      | j | j| S rA   )r.   r@   r|   r?   r   r   r   r@         zSubConfigProxy.__getattr__c                 C   r}   rA   )r.   rC   r|   r?   r   r   r   rC      r~   zSubConfigProxy.__delattr__)	r   r	   r
   r{   r7   r:   r@   rC   rm   r   r   r=   r   r      s    r   c                 C   s&   t | tr| ||S tj| ||S )z@
    Workaround `mock.patch.object` issue with ConfigModule
    )r   r,   rj   r   object)objr$   r#   r   r   r   patch_object   s   
r   )
contextlibrY   rI   rw   typesr   r   typingr   r   r   r   intfloatboolr   rk   r   r   tupler-   r   r3   r,   rf   r   r   r   r   r   r   <module>   s    &s#