
    9j                     j   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	Z	d dl
mZ d dlmZ d dlm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 d dlmZ eee  e!d      e"e#e$e%e&f	Z'eee  e!d      e"e%fZ( ed	eez  e z  e"z  e#z  e$z  e%z  e&z  dz  
      Z) e*       Z+ ed       G d dee)                Z,erAe+dddddddfde)e*z  de"dz  de"e#e"   z  dz  de"e#e"   z  dz  de!dz  de"dz  de de"dz  de)fdZ-nCe+dddddddfde)e*z  de"dz  de"e#e"   z  dz  de"e#e"   z  dz  de!dz  de"dz  de de"dz  de,e)   fdZ-de"de e"z  dz  fdZ.deddfdZ/dZ0dede$e"   fd Z1e G d! d"             Z2 G d# d$e      Z3 G d% d&e jh                        Z4 G d' d(      Z5d+de"dede dz  fd)Z6d* Z7y),    N)Callable)
ContextVar)	dataclass)FunctionType
ModuleType)AnyGenericNoReturnOptionalTYPE_CHECKINGTypeVar)
deprecated)justknobs_checkT)boundT)kw_onlyc                       e Zd ZU dZeez  ed<   dZedz  ed<   dZ	e
e   dz  ed<   dZe
e   dz  ed<   dZedz  ed<   dZedz  ed<   d	Zeed
<   dZedz  ed<   ddZedee
e   z  dz  de
e   dz  fd       Zy)_Configa+  Represents a config with richer behaviour than just a default value.
    ::
        i.e.
        foo = Config(justknob="//foo:bar", default=False)
        install_config_module(...)

    This configs must be installed with install_config_module to be used

    Precedence Order:
        alias: If set, the directly use the value of the alias.
        env_name_force: If set, this environment variable has precedence over
            everything after this.
            If multiple env variables are given, the precedence order is from
            left to right.
        user_override: If a user sets a value (i.e. foo.bar=True), that
            has precedence over everything after this.  User overrides are thread-local.
        env_name_default: If set, this environment variable will override everything
            after this.
            If multiple env variables are given, the precedence order is from
            left to right.
        justknob: If this pytorch installation supports justknobs, that will
            override defaults, but will not override the user_override precedence.
        default: This value is the lowest precedence, and will be used if nothing is
            set.

    Environment Variables:
        These are interpreted to be either "0" or "1" to represent true and false.

    Arguments:
        justknob: the name of the feature / JK. In OSS this is unused.
        default: is the value to default this knob to in OSS.
        alias: The alias config to read instead.
        env_name_force: The environment variable, or list of, to read that is a FORCE
            environment variable. I.e. it overrides everything except for alias.
        env_name_default: The environment variable, or list of, to read that changes the
            default behaviour. I.e. user overrides take preference.
    defaultNjustknobenv_name_defaultenv_name_force
value_typealiasFr   deprecation_messagereturnc                 0   t         j                  | j                        | _        t         j                  | j                        | _        | j                  B| j
                  t        us$| j                  | j                  | j                  t        d      y y )Nztif alias is set, none of {default, justknob,                         env_name_default and env_name_force} can be set)	r    string_or_list_of_string_to_listr   r   r   r   _UNSET_SENTINELr   AssertionErrorselfs    Z/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/utils/_config_module.py__post_init__z_Config.__post_init__V   s     ' H H!!!
 &FF
 ::!O3==,((4&&2$I  3 "    valc                     | y t        | t              r| gS t        | t              st        dt	        |              | S )Nzval is not a list, got )
isinstancestrlistr    type)r&   s    r#   r   z(_Config.string_or_list_of_string_to_listj   sC     ;c35L#t$ #:49+!FGG
r%   r   N)__name__
__module____qualname____doc__r   object__annotations__r   r)   r   r*   r   r   r+   r   r   boolr   r$   staticmethodr    r%   r#   r   r   $   s    $L ZHcDj)-d3i$&-'+NDI$+"Jt"E3:J&*t*( 	49_t#		cT		 	r%   r   Fr   r   r   r   r   r   r   r   r   c                      y Nr5   r   r   r   r   r   r   r   r   s           r#   Configr9   }   s     r%   c           
      (    t        | |||||||      S )Nr8   )r   r8   s           r#   r9   r9      s)     -)!! 3

 
	
r%   namec                 \    t         j                  j                  |       }|dk(  ry|dk(  ry|S N1T0Fosenvironget)r;   values     r#   _read_env_variablerE      s-    JJNN4 E||Lr%   modulec                       G d dt               }dt        t        z  dt        t        z  dt        ddf fdi t               }   d	        _        | _        | _        d
 _	        d _
        y)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                        e Zd Z eh d      Zy)3install_config_module.<locals>.ConfigModuleInstance>   	_is_dirty_hash_digestr2   N)r-   r.   r/   set_bypass_keysr5   r%   r#   ConfigModuleInstancerI      s    KLr%   rN   sourcedestprefixr   Nc                    t        j                  |       }t        | j                  j	                               D ]  \  }}|j                  d      slt        |t        t        f      sVt        |d      r*|j                  dk(  s;|j                  j                  d      s t        |t              rt        |t              r| | }|j                  |d      }t        |t              r,t!        t        ||      |      	|<   |
u st#        
|       t        |t              r8||j$                  ||_        t!        ||      	|<   |
u st#        
|       !t        |t              rt|j                  
j&                  k7  rt)        d| d
j&                         t+        
| d	      } ||| d	       |
u rt-        |||       ||j                  |<   t)        d
| d| dt        |       d       y)z?Walk the module structure and move everything to module._config__r.   typingzcollections.abcN)r   r   zsubconfig class z must be defined in module .zUnhandled config =z ())inspectget_annotationsr*   __dict__items
startswithr(   r   r   hasattrr.   r+   
issubclassr   rC   CONFIG_TYPES_ConfigEntrydelattrr   r-   r    SubConfigProxysetattr)rO   rP   rQ   
type_hintskeyrD   r;   annotated_typeproxyconfigrF   visits            r#   ri   z$install_config_module.<locals>.visit   s    ,,V4
v4467 -	XJCt$ej,%?@E<0((H4 ++667HI ud+
5'0JXcU#D'^^C6N%.+EnEt t 6>FC(E7+!-%2B2B2J'5E$+E48t6>FC(E4(##v6(*5'1LV__L]^  'v$qz:eUtfAJ/6>D#u-).DMM#&$'8QugRU}TU%VWW[-	Xr%    T)ConfigModuler   r+   rb   r)   -get_assignments_with_compile_ignored_comments_config_compile_ignored_keys	__class__rJ   rK   )rF   rN   compile_ignored_keysrh   ri   s   `  @@r#   install_config_modulerq      s    M| M4XT!4X>)4X 4X 
	4Xl ')FHP	&&"FN#7F +FFFr%   z@compile_ignoredc                 
   t        j                  |       }t               }t        j                  t	        j
                  |j                  d            j                        }d}d}|D ]  }|j                  t        j                  k(  rNd}|j                  j                         }t        |v sF|dk7  rt        dt               ||j                  d   f}o|j                  t        j                  k(  r|r|j                  }|j                  t        j                   k(  s|j                  dk(  st        |d   v r+|d   |j                  d   dz
  k(  r|j#                  |       d}d} |dk7  rt        dt               |S )Nutf-8)rj   rj   zunconsumed r   rV      )rX   	getsourcerL   tokenizeioBytesIOencodereadliner+   COMMENTstringstripCOMPILE_IGNORED_MARKERr    startNAMEOPadd)rF   source_codeassignmentstokenscurrent_comment	prev_nametokenmaybe_currents           r#   rl   rl      sY   ##F+K%K rzz+*<*<W*EFOOPFOI ::)))I!LL..0M%6"h.(;7M6N)OPP"/Q"?ZZ8==( !LL	ZZ8;;&5<<3+> '/!*<<#A&%++a.1*<<	*"(I-. ("{+A*BCDDr%   c                       e Zd ZU eed<   eed<   ee   ed<   dZe	dz  ed<   e
Zeed<   e
Zeed<   dZeed	<   dZe	dz  ed
<   dZeed<   dZe	dz  ed<   dZeed<   dede	ddfdZy)r`   r   r   user_overrideNr   env_value_forceenv_value_defaultFhider   r   r   _deprecation_warnedrh   r;   r   c                    |j                   | _         |j                  |j                  nt        | j                         | _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        d| _        t        |t              | _
        |j                  '|j                  D ]  }t        |      x}|| _         n |j                  '|j                  D ]  }t        |      x}|| _         n | j                  ?| j                   3t!        | j                   t"              st%        d| j                    d      | j                  c|j                  |j                  J| j                  t"        t&        t(        t"           t(        t&           fvrt%        d| j                   d      y y y )NFr   z!justknobs only support booleans, z is not a booleanz<envvar configs only support (optional) booleans or strings, z is neither)r   r   r+   r   r   r   r   r   r   r   r   r   rE   r   r   r   r(   r3   r    r)   r   )r"   rh   r;   r&   	env_values        r#   __init__z_ConfigEntry.__init__B  s   ~~!'!2!2!>FDDV 	 \\
 ++#)#=#= #( 'oF""... !3C!88IE-6D*   ,,, !3C!88IE+4D( ==$)AdllD1$7~EVW  ??&##/63H3H3T	'  %RSWSbSbRccno  4U 'r%   )r-   r.   r/   r   r2   r+   r   r1   r   r)   r   r   r   r   r3   r   r   r   r   r   r   r5   r%   r#   r`   r`   "  s     L f%%HcDj*OS*,s, D$E3:J&*t* %%)w )c )d )r%   r`   c                       e Zd ZU eeef   ed<   ee   ed<   ee   ed<   eed<   e	dz  ed<   d3dZ
d	ed
eddfdZd	ededdf fdZd	edefdZd	eddfdZdedeeef   dz  fdZdedefdZdededdfdZd	edefdZ	 	 	 d4dee   dz  dee   dz  dedeeef   fdZdedefdZde	fdZdddedeeef   fd Zdefd!Zde	fd"Z ed#e $      deeef   fd%       Z! ed&e $      deeef   fd'       Z"d(e	eeef   z  ddfd)Z#deeef   fd*Z$deeef   fd+Z%	 	 d5d,eeeef   z  dz  d-ed.eeef   dd/fd0Z&d1eeef   defd2Z' xZ(S )6rk   rm   rM   rn   rJ   NrK   r   c                 (    t        dt         d      )Nzuse z-.install_config_module(sys.modules[__name__]))NotImplementedErrorr-   r!   s    r#   r   zConfigModule.__init__z  s    !8*IJ
 	
r%   r;   rh   c                     |j                   rf|j                  sYddl}| j                   d| d}|j                  r|d|j                   z  }|dz  }|j                  |t        d       d	|_        yyy)
z;Issue deprecation warning for config if not already warned.r   NrU   z is deprecatedz and z4. It will be removed in a future version of PyTorch.   )
stacklevelT)r   r   warningsr-   r   warnFutureWarning)r"   r;   rh   r   msgs        r#   _warn_if_deprecatedz ConfigModule._warn_if_deprecated  s{    V%?%?]]O1TF.9C))v99:;;IICMM#}M;)-F& &@r%   rD   c                 h   || j                   v rt        | 	  ||       y || j                  vrt	        | j
                   d| d      | j                  |   }| j                  ||       |j                  | j                  ||       y |j                  j                  |       d| _        d|_        y )NrU    does not existTF)rM   super__setattr__rm   AttributeErrorr-   r   r   _set_alias_valr   rL   rJ   r   )r"   r;   rD   rh   ro   s       r#   r   zConfigModule.__setattr__  s    4$$$Ge,% DMM?!D6!IJJ \\$'F$$T62||'##FE2$$((/!%#r%   c                 .   	 | j                   |   }|j                  rt        | j                   d| d      | j	                  ||       | j                  |      }|t        ur|S |j                  t        ur|j                  S |j                  j                         }|t        ur|S |j                  t        ur|j                  S |j                  !t        |j                  |j                        S t        |j                  t              sR|j                  j!                  t#        j$                  |j                               |j                  j                         S |j                  S # t&        $ r!}t        | j                   d| d      |d }~ww xY w)NrU   r   r;   r   )rm   r   r   r-   r   _get_alias_valr   r   r   rC   r   r   r   r   r(   _IMMUTABLE_CONFIG_TYPESrL   copydeepcopyKeyError)r"   r;   rh   	alias_valr   es         r#   __getattr__zConfigModule.__getattr__  s`   $	R\\$'F{{$av_%MNN $$T62++F3I/  %%_<---"00446MO3$$''>///*&FOOV^^TT fnn.EF$$((v~~)FG++//11>>! 	R DMM?!D6!IJPQQ	Rs=   A"E* %E* #E* 'E* ,E* 2A+E* E* *	F3FFc                     d| _         | j                  |   j                  j                  t               d| j                  |   _        y NT)rJ   rm   r   rL   r   r   r"   r;   s     r#   __delattr__zConfigModule.__delattr__  s9     	T((,,_="&Tr%   entryc                     |j                   }|y |j                  dd      \  }}	 t        j                  |      }||fS # t        $ r}t        d| d      |d }~ww xY w)NrU   ru   zconfig alias r   )r   rsplit	importlibimport_moduleImportErrorr   )r"   r   r   module_nameconstant_namerF   r   s          r#   _get_alias_module_and_namez'ConfigModule._get_alias_module_and_name  sw     =%*\\#q%9"]	P,,[9F }$$  	P =!GHaO	Ps   ? 	AAAc                 Z    | j                  |      }|t        S |\  }}t        ||      }|S r7   )r   r   getattr)r"   r   datarF   r   constant_values         r#   r   zConfigModule._get_alias_val  s9    ..u5<"" $ 7r%   r&   c                 d    | j                  |      }|t        d      |\  }}t        |||       y )Nz6alias data should not be None when setting alias value)r   r    rc   )r"   r   r&   r   rF   r   s         r#   r   zConfigModule._set_alias_val  s?    ..u5< H  !%s+r%   c                    | j                   |   }|j                  t        u xs |j                  |j                  k(  }|j                  t        u xs |j                  |j                  k(  }|j
                  j                         t        u }t        |j                  t              s+|xs' |j
                  j                         |j                  k(  }|xr |xr |S )z
        Returns true if the config is at its default value.
        configs overridden by the env are not considered default.
        )	rm   r   r   r   r   r   rC   r(   r   )r"   r;   
config_valnot_set_env_defaultnot_set_env_forceunsets         r#   _is_defaultzConfigModule._is_default  s    
 \\$'

 ((O; B++z/A/AA 	
 &&/9 @))Z-?-?? 	
 ((,,./A*,,.EFQZ5599;z?Q?QQEB,B1BBr%   ignored_keysignored_prefixesskip_defaultc                 `   i }| j                   j                         D ]  \  }|j                  |r|v r|rt        fd|D              r2|r| j	                        rF|j
                  j                         }|j                  t        ur|j                  }nd|t        ur|}nY|j                  t        ur|j                  }n:|j                  "t        |j                  |j                        }n|j                  }t        |t              st        j                   |      }||<    |S )a  Export a dictionary of current configuration keys and values.

        This function is design to provide a single point which handles
        accessing config options and exporting them into a dictionary.
        This is used by a number of different user facing export methods
        which all have slightly different semantics re: how and what to
        skip.
        If a config is aliased, it skips this config.

        Arguments:
            ignored_keys are keys that should not be exported.
            ignored_prefixes are prefixes that if a key matches should
                not be exported
            skip_default does two things. One if a key has not been modified
                it skips it.
        c              3   @   K   | ]  }j                  |        y wr7   )r\   ).0rQ   re   s     r#   	<genexpr>z)ConfigModule._get_dict.<locals>.<genexpr>  s     M&s~~f-Ms   r   )rm   r[   r   anyr   r   rC   r   r   r   r   r   r   r(   r   r   r   )	r"   r   r   r   rh   r   r   r&   re   s	           @r#   	_get_dictzConfigModule._get_dict   s   , "$,,,,. 	JC{{&| 3M<LMM 0 0 5 "//335M$$O;++o5#((?--+%5>>5==Qmmc#:;mmC(F3K5	8 r%   config_namec                 4    | j                   |   j                  S r7   )rm   r   )r"   r   s     r#   get_typezConfigModule.get_type5  s    ||K(333r%   c                 j    t        | dg       }t        j                  | j                  |      d      S )z Convert config to a pickled blob_save_config_ignorer      )protocol)r   pickledumpsr   )r"   r   s     r#   save_configzConfigModule.save_config8  s3    t%:B?||NNN5
 	
r%   T)ignore_private_configsr   c                B   g }|r|j                  d       |j                  t        | dg              | j                  |      }t        | dg       D ]K  }||v s||    ||          }t	        |d      r|j                         ||<   8t        d| d||    d       |S )	z!Convert config to portable format__cache_config_ignore_prefix)r   _cache_config_factory_keysuuidzConfig 'z' is set to zO which does not implement uuid(). Implement uuid() for cache key participation.)appendextendr   r   r]   r   RuntimeError)r"   r   prefixesrh   re   instances         r#   save_config_portablez!ConfigModule.save_config_portable@  s     !OOC &CRHI:4!=rB 
	Cf}!8&6#;=8V,"*--/F3K&"3%|F3K= A) * 
	 r%   c                   	
 t               
dt        dt        dt        fddt        ddf
fddt        t         z  dt        t           ffddt
        dt        fd		dt        f	fd
}g }| j                  }| j                  t        | dg       d      j                         D ]  \  }}|j                   ||||               
D ]  }|j                  dd|         dj                  |      S )zConvert config to Python statements that replicate current config.
        This does NOT include config settings that are at default values.
        funcadd_dotr   c                 D    | j                   }|dk(  rd}|r
|dk7  r|dz  }|S )Nbuiltinsrj   rU   )r.   )r   r   r   s      r#   get_module_namez4ConfigModule.codegen_config.<locals>.get_module_name^  s2    //Kj( ;",s"r%   Nc                 @     | d      }|rj                  |       y y )NF)r   )r   r   r   importss     r#   
add_importz/ConfigModule.codegen_config.<locals>.add_importf  s#    )$6KK( r%   vc                 X    | D cg c]  } |d       |j                     c}S c c}w r   )r-   )r   itemr   s     r#   list_of_callables_to_stringz@ConfigModule.codegen_config.<locals>.list_of_callables_to_stringk  s,    PQRtT23DMM?CRRRs   'c                 P    t        |       xr t        | d      xr t        | d      S )Nr.   r-   )callabler]   )r   s    r#   importable_callablez8ConfigModule.codegen_config.<locals>.importable_callablen  s$    A;V71l#;V:@VVr%   c                 \    |      r% |       |  d| d |d       |j                    S t        |t        t        f      r_t	        fd|D              rK|D ]
  } |         |      }t        |t              r
|  d| d| S |  d| ddj                  |       dS |  d| d|S )a  
            Return a string version of the config line.
            Handle v when v is a callable, or a list/dict of callables. Add import statements for callables if necessary.
            We assume that the value of a single config won't be a mix of callables and non-callables.

            Example output:
                import logging
                import _warnings
                torch._dynamo.config.reorderable_logging_functions = { _warnings.warn, logging.warn, print }
            rU   z = Tc              3   .   K   | ]  } |        y wr7   r5   )r   r   r   s     r#   r   zGConfigModule.codegen_config.<locals>.get_config_line.<locals>.<genexpr>  s      4.2#D)4s   z = { z, z })r-   r(   r*   rL   alljoin)	modkr   r   v_listr   r   r   r   s	        r#   get_config_linez4ConfigModule.codegen_config.<locals>.get_config_liner  s     #1%1as#oa&>%?

|LLAc{+ 4674 1  %Dt$%4Q7a&!U!A3c&22!U!A3fTYYv->,?sCCas#aU++r%   r   T)r   r   r   zimport 
)rL   r   r3   r)   r*   r   r-   r   r   r[   r   insertr   )r"   r   linesr   r   r   import_namer   r   r   r   r   s          @@@@@r#   codegen_configzConfigModule.codegen_configV  s    %	( 	T 	c 		)X 	)$ 	)
	S4#: 	S$s) 	S	W3 	W4 	W	,# 	, 	,6 mmNN '<bAPT # 

%'	5DAq LLa34	5 # 	5KLLgk]34	5yyr%   c                 X   | j                   s| j                  | j                  t        | j                              }t        t        |j                                     }t        j                  |j                  d      d      j                         | _        d| _         | j                  S )z/Hashes the configs that are not compile_ignoredr   rs   F)usedforsecurity)rJ   rK   r   r*   rn   reprsortedr[   hashlibmd5rz   digest)r"   dict_to_hashstring_to_hashs      r#   get_hashzConfigModule.get_hash  s    >>T..6>>tD<V<V7W>XL!&););)=">?N '%%g.!fh  #DN   r%   z`config.to_dict()` has been deprecated. It no longer changes the underlying config. use `config.get_config_copy()` instead if you just want a copy of the config, or config.load_config if you need mutable access)categoryc                 "    | j                         S r7   get_config_copyr!   s    r#   to_dictzConfigModule.to_dict       ##%%r%   z`config.shallow_copy_dict()` has been deprecated. It no longer changes the underlying config. use `config.get_config_copy()` instead if you just want a copy of the config, or config.load_config if you need mutable accessc                 "    | j                         S r7   r  r!   s    r#   shallow_copy_dictzConfigModule.shallow_copy_dict  r  r%   maybe_pickled_configc                     t        |t              st        j                  |      }n|}|j	                         D ]6  \  }}|| j
                  v rt        | ||       "ddlm}  |d| d| d       8 y)zARestore from a prior call to save_config() or shallow_copy_dict()r   )	warn_oncezkey z with value z! is not understood by this configN)	r(   dictr   loadsr[   rm   rc   torch._dynamo.utilsr  )r"   r  rh   r   r   r  s         r#   load_configzConfigModule.load_config  so    .5\\"67F)FLLN 	VDAqDLL a#9D<s2STU	Vr%   c                 "    | j                         S r7   )r   r!   s    r#   r  zConfigModule.get_config_copy  s    ~~r%   c                 <    | j                  t        | dg             S )Nr   r   )r   r   r!   s    r#   get_serializable_config_copyz)ConfigModule.get_serializable_config_copy  s    ~~749NPR+S~TTr%   arg1arg2kwargsContextDecoratorc                 D   |L| t        |t              st        d      ||int        |t              st        d      ||rt        d      ||t        d      t        t              st        dt	                     |  G fddt
              } |       S )a  
        Decorator and/or context manager to make temporary changes to a config.  Note that patched settings are thread-local.

        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):
                ...
        zGfirst argument must be a string when passing 2 positional args to patchzKfirst argument must be a dict when passing a single positional arg to patchz:cannot pass both positional and keyword arguments to patchzLsecond positional argument is only valid when first argument is a key stringzexpected `dict` got c                   2    e Zd Zd fdZdfdZfdZy)'ConfigModule.patch.<locals>.ConfigPatchNc                 j    | _         t        j                   dt        |        dd      | _        y )Nz.ConfigPatch[]r5   r   )changesr   r-   id_prior)r"   r*  rh   s    r#   r   z0ConfigModule.patch.<locals>.ConfigPatch.__init__  s3    &FP'}RXJa@Gr%   c                    i }| j                   D ]  }j                  |      ||<    | j                  j                         }| j                  j	                  g ||       	 | j                   j                         D ]  \  }}j                  ||        y # t        $ r | j                  j	                  |        w xY wr7   )r*  r   r,  rC   rL   r[   r   	Exception)r"   priorre   prior_stackr   r   rh   s         r#   	__enter__z1ConfigModule.patch.<locals>.ConfigPatch.__enter__  s    (*<< 9C!'!3!3C!8E#J9 #kkoo/ 5+ 5u 56 $ 2 2 4 11**1a01  KKOOK0s   $4B &B?c                     | j                   j                         }|st        d      |d   }| j                   j                  |d d        |j	                         D ]  \  }}j                  ||        y )Nz2prior should not be empty when exiting ConfigPatchrt   )r,  rC   r    rL   r[   r   )	r"   exc_typeexc_valexc_tbr0  r/  r   r   rh   s	           r#   __exit__z0ConfigModule.patch.<locals>.ConfigPatch.__exit__  sq    "kkoo/"(L  $BCR 01!KKM -DAq&&q!,-r%   r,   )r-   r.   r/   r   r1  r6  )r*  rh   s   r#   ConfigPatchr'    s    	-r%   r7  )r(   r)   r    r  r+   r$  )r"   r!  r"  r#  r7  r*  rh   s        @@r#   patchzConfigModule.patch  s    . !$,(a   ,!$-(e  $P 
 G$b  '4( #7W!GHH	-* 	-B }r%   r*  c                 J      j                   dt        g df   f f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()

        r   Nc                      D  ci c]   } | |    j                   j                         " c} j                         D ]-  \  } }j                  |    j                   j	                  |       / dfd}|S c c} w )Nc                      j                         D ]-  \  } }j                  |    j                  j                  |       / y r7   )r[   rm   r   rL   )r   r   r/  r"   s     r#   revertzBConfigModule._make_closure_patcher.<locals>.change.<locals>.revert9  s8    !KKM 9DAqLLO1155a89r%   r,   )r   rC   r[   rm   rL   )r   r   r<  r/  r*  rh   r"   s      @r#   changez2ConfigModule._make_closure_patcher.<locals>.change4  sr    ?FG!Qq	//3355GE 51Q--11!459 M Hs   %A8)rm   r   )r"   r*  r=  rh   s   `` @r#   _make_closure_patcherz"ConfigModule._make_closure_patcher   s*    $ 		T* 		 r%   r,   )NNF)NN))r-   r.   r/   r  r)   r`   r2   rL   r3   bytesr   r   r1   r   r   r   r   tupler   r   r   r   r   r*   r   r+   r   r   r   r  r  r   r   r  r  r  r  r   r8  r>  __classcell__ro   s   @r#   rk   rk   n  s    #|#$$c(s8#O$,


. 
.\ 
.d 
.$ $F $t $"%R %R %RN' ' '%!%	z3	$	&%L S ,L ,s ,t ,C C C4 *.-1"	33i$&3 s)d*3 	3
 
c3h3j4C 4D 4
U 
 15)-	c3h,?  ? B	!% 	! 	8 	&c3h && 	8 	&4S> &&VS#X0F V4 V c3h  Ud38n U
 -1VDcN"T)V V sCx.	V
 
VptCH~ # r%   rk   c                   L     e Zd ZdZddZdefdZdeegef   def fdZ	 xZ
S )r$  z[
    Same as contextlib.ContextDecorator, but with support for
    `unittest.TestCase`
    r   c                     t        d      NNYIr   r!   s    r#   r1  zContextDecorator.__enter__H      !%((r%   c                     t        d      rE  rG  )r"   r3  r4  r5  s       r#   r6  zContextDecorator.__exit__K  rH  r%   r   c                      t        |t              r\t        |t        j                        rB G  fdd|      }|j
                  |_        |j                  |_        |j                  |_        |S t         %  |      S )Nc                   F     e Zd Zed fd       Zed fd       Z xZS ),ContextDecorator.__call__.<locals>._TestCasec                     j                          	 t        | 	          y # t        $ r j	                  d d d         w xY wr7   )r1  r   
setUpClassr.  r6  clsro   r"   s    r#   rN  z7ContextDecorator.__call__.<locals>._TestCase.setUpClassR  s?    NN$*,$ dD$7s	   " A c                 z    	 t         |           j                  d d d        y # j                  d d d        w xY wr7   )r   tearDownClassr6  rO  s    r#   rR  z:ContextDecorator.__call__.<locals>._TestCase.tearDownClass[  s4    8-/dD$7dD$7s   % :r,   )r-   r.   r/   classmethodrN  rR  rA  )ro   r"   s   @r#   	_TestCaserL  Q  s)      8 8r%   rT  )
r(   r+   r^   unittestTestCaser-   r/   r.   r   __call__)r"   r   rT  ro   s   `  r#   rW  zContextDecorator.__call__N  sf    dD!jx7H7H&I8D 8" "&I%)%6%6I"#'??I w%%r%   r,   )r-   r.   r/   r0   r1  r
   r6  r   r   rW  rA  rB  s   @r#   r$  r$  B  s<    
))X )&XseSj1 &c & &r%   r$  c                   d     e Zd ZdZdededdf fdZdededdfd	Zdedefd
Z	deddfdZ
 xZS )rb   zn
    Shim to redirect to main config.
    `config.triton.cudagraphs` maps to _config["triton.cudagraphs"]
    rh   rQ   r   Nc                 F    t         |   d|       t         |   d|       y )Nrm   _prefix)r   r   )r"   rh   rQ   ro   s      r#   r   zSubConfigProxy.__init__q  s!    Iv.Iv.r%   r;   rD   c                 T    | j                   j                  | j                  |z   |      S r7   )rm   r   rZ  )r"   r;   rD   s      r#   r   zSubConfigProxy.__setattr__v  s"    ||''t(;UCCr%   c                 R    | j                   j                  | j                  |z         S r7   )rm   r   rZ  r   s     r#   r   zSubConfigProxy.__getattr__y       ||''t(;<<r%   c                 R    | j                   j                  | j                  |z         S r7   )rm   r   rZ  r   s     r#   r   zSubConfigProxy.__delattr__|  r]  r%   )r-   r.   r/   r0   r1   r)   r   r   r   r   r   rA  rB  s   @r#   rb   rb   k  sf    
/v /s /t /
D DF Dt D= = == = =r%   rb   c                 \    t         j                  j                  |       }|dk(  ry|dk(  ry|S r=   r@   )r;   r   rD   s      r#   get_tristate_envr`    s-    JJNN4 E||Nr%   c                       fd}|S )Nc                     j                   j                         D ]3  \  }}|j                  d      r|| j                   vs't        | ||       5 | S )Nr   )rZ   r[   r\   rc   )	child_clsr   r   
parent_clss      r#   wrapperz$inherit_fields_from.<locals>.wrapper  sR    ''--/ 	)DAq<<$)2D2D)D	1a(	) r%   r5   )rd  re  s   ` r#   inherit_fields_fromrf    s     Nr%   r7   )8
contextlibr   r	  r   rX   rx   rA   r   rw   rU  collections.abcr   contextvarsr   dataclassesr   typesr   r   rT   r   r	   r
   r   r   r   typing_extensionsr   torch._utils_internalr   intfloatr3   r+   r)   r*   rL   r@  r  r_   r   r   r1   r   r   r9   rE   rq   r   rl   r`   rk   r$  rb   r`  rf  r5   r%   r#   <module>rp     s        	 	    $ " ! * K K ( 1 UD$t*c4eTJ tT$ZeD  CsU{T)C/$6<uDtKdRS ( 4Ogaj O Ol  .#3715"&  *.
V
*
 S	/D0
 d3i$.	

 4K
 Tz
 
 !4Z
 

 .#3715"&  *.
V
*
 S	/D0
 d3i$.	

 4K
 Tz
 
 !4Z
 

0S TCZ$%6 J* J JZ , "* "S "J H H HVQ: Qh&&z22 &&R= =*3  t r%   