~L iddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl mZddlmZddlmZddlmZddlmZmZmZmZmZmZmZmZddlZddlmZdd l m!Z!dd l"m#Z#m$Z$dd l%m&Z&ejNe(Z)er*dd l*m+Z+m,Z,dd l-m.Z.ddl/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7 ejpdZ9ejpdZ:edGddZ;GddeZ<GddejzZ> GddZ?ej$dGddZ@Gd d!eZAe!ej$Gd"d#ZBed$ZC ej$dGd%d&ZD ej$dGd'd(eDZE ej$dGd)d*eDZF Gd+d,eeCZGGd-d.ZHGd/d0ZIGd1d2eGeIZJGd3d4ZKGd5d6eGeKZLGd7d8ZM Gd9d:eGeHZNGd;d<ZOGd=d>eOZPGd?d@ZQejZS GdAdBZTGdCdDZUe dMdEZVe dNdFZWej$dGdGdHZXej$dGdIdJeXZYdOdPdKZZdQdLZ[y)R) annotationsN)abstractmethod) defaultdict)contextmanager) dataclass)AnyCallableGeneric NamedTupleOptional TYPE_CHECKINGTypeVarUnion)_pytree)dataclass_slots)CapturedTraceback format_frame)WeakTensorKeyDictionary) GeneratorIterator)CodeType)DDPOptimizerContext) PyCodegen)ViewAndMutationMetaFakeTensorModez-^(?P\d+)/(?P\d+)$zQ^!(?P\d+)(?:/(?P\d+)/(?P\d+))?$T)frozencJeZdZUded<ded<dZded<ddZed dZy) CompileId Optional[int]frame_idframe_compile_idNcompiled_autograd_idc8|jZ|jdu|jduk(sJd}|jd|jd|j}d|j|S|j |jJ|jd|jS)N/!)r#r!r")self frame_strs S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/_guards.py__str__zCompileId.__str__[s  $ $ 0MMT)t/D/D/LM MMI}}( a0E0E/FG t001)= ===,1F1F1R RRmm_Ad&;&;%<= =c&|y ttfD]X}|j|x}s|j}|j D]\}}| t |||<|di|cSt #t$r}t d|d|d}~wwxYw)z Factory method that creates a CompileId from its string representation. Keep this in sync with the __str__ method. NzInvalid compile_id '')COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch groupdictitemsint ValueError Exception)cls compile_idpatternr2groupskves r* from_stringzCompileId.from_stringhs   J.0EF !#MM*5555"__.F & /1=(+AF1I/==(  !!  J3JJJr,rc*eZdZUded<ded<ddZy)TraceIdrr9r5attemptc|jdk(rt|jS|jd|jS)Nr_)rLrBr9r(s r*r+zTraceId.__str__s7 <<1 t' 'oo&a ~6 6r,Nr@rErFrGrHr+r/r,r*rKrKsL7r,rKcteZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZddZddZddZddZddZy) GuardSourcer cF|tjtjfvSN)rRGLOBAL_FSDP_MODULELOCAL_FSDP_MODULErOs r*is_fsdp_modulezGuardSource.is_fsdp_modules 66 8U8UVVVr,cddlmcm}|jr4|tj tj fvxs|jS|tj tj fvSNr)torch._dynamo.config_dynamoconfig_unsafe_skip_fsdp_module_guardsrRGLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErf)r(rks r*is_specialized_nn_modulez$GuardSource.is_specialized_nn_modulesn--  1 1<<;;) &&(    4 4  3 3   r,c|tjtjtjtjfvSrc)rRGLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErOs r*is_unspecialized_nn_modulez&GuardSource.is_unspecialized_nn_modules8  6 6  5 5  > >  = =    r,cF|tjtjfvSrc)rRrsrtrOs r*"is_unspecialized_builtin_nn_modulez.GuardSource.is_unspecialized_builtin_nn_modules&  > >  = =   r,c|tjtjtjtjtj fvSrc)rRLOCALrnrerrrtrOs r*is_localzGuardSource.is_localsA     3 3  ) )  5 5  = =    r,NrAbool)rErFrGryGLOBALrnrmCONSTANT RANDOM_VALUE SHAPE_ENVrerdBACKWARD_STATE EPHEMERALSYNTHETIC_LOCALrrrqrtrsrfrorurwrzr/r,r*rRrRst E F"##$ HLINIO$&!%'",.)-/*W "   r,rRc eZdZy)GuardBuilderBaseNrErFrGr/r,r*rrsr,rc*eZdZUded<ded<ddZy)SLocz,Optional[Union[traceback.FrameSummary, str]] framework_locrCmaybe_user_locct|jtr |jnt|j}|j|jd|dSd|dS)Nz ()() isinstancerrBrr)r(flocs r*r+z SLoc.__str__sh$,,c2   d001    *))*"TF!4 4tfA; r,Nr@rPr/r,r*rrs??!! r,rc,eZdZUded<ded<ded<y) ShapeGuardzsympy.logic.boolalg.Booleanexprrslocr|size_obliviousNrErFrGrHr/r,r*rrs %% Jr,rc<eZdZUded<ded<dZded<dZded<dZd ed <dZd ed <dZd ed<dZ ded<dZ ded<dZ ded<d%dZ d&dZ d'dZd(dZed)dZed*dZed+dZd)dZd)dZd,dZd-d Zd-d!Zd-d"Zd)d#Z d.d$Zy)/GuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None] create_fnNzOptional[list[str]] guard_types code_listzOptional[object] obj_weakref$Optional[weakref.ReferenceType[Any]]guarded_class_weakrefzOptional[CapturedTraceback]stackz Optional[traceback.StackSummary] user_stackr _hashFr|_unserializablec|j:t|j|jt |j f|_|jSrc)rhashnamesourceidrrOs r*__hash__zGuard.__hash__s: :: tyy$++r$..7IJKDJzzr,ct|jtjxrD|jjt j jjju}||jr|jjndt|j|j|jjj fS)N)rr functoolspartialfunctorchrjguards GuardBuilderDUPLICATE_INPUTrvaluelenrinner_create_fn__code__co_firstlineno)r(is_duplicate_inputs r*sort_keyzGuard.sort_keys t~~y'8'8 9 Y##u}}';';'H'H'X'XX  !%DKK  "  N II  " + + : :   r,cD|j|jkSrc)rr(others r*__lt__z Guard.__lt__+s}}!111r,ct|jtjr|jjS|jSrc)rrrrrrOs r*rzGuard.inner_create_fn.s0 dnni&7&7 8>>&& &>> !r,c6|jjSrc)rrrOs r*rz Guard.name4s&&++--r,c6|jjSrc)r guard_sourcerOs r*rz Guard.source8s&&3355r,c t|tjre|}|Ddtt |d|j j dtt |dSdtt |dSt|S)a This is a workaround of a Python weakref bug. `obj_weakref` is instance returned by `weakref.ref`, `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g: class MyConfig(dict): def __getattr__(self, x): return self[x] obj = MyConfig(offset=5) obj_weakref = weakref.ref(obj) str(obj_weakref) # raise error: KeyError: '__name__' z z; dead>)rweakref ReferenceTypehexr __class__rErB)robjs r*weakref_to_strzGuard.weakref_to_str<s k7#8#8 9-C%c"[/&:%;6#--BXBXAYY^_bceficj_k^llmnn%c"[/&:%;7CC{# #r,cZd|jr$|jjjnddt|jd|j j d|j d|jd|j|jd|jd}|S) Nz r% z& { 'guard_types': z, 'code': z, 'obj_weakref': z 'guarded_class': z } ) rrlowerreprrrErrrrr)r(ss r*__repr__zGuard.__repr__Us %)[[     !b94 ?:K1TMaMaMcMlMlLmn ,,-.^^$% //0@0@ABC"889:  r,cdt|jd}|jr$|jjjnd}|d|dz }|d|j j dz }|d|j dz }|d|jdz }|d|j|jdz }|d |jdz }|S) NzName:  r%z Source: z Create Function: z Guard Types: z Code List: z Object Weakref: z Guarded Class Weakref: ) rrrrrrErrrrr)r(outputrs r*r+z Guard.__str__as$tyy/*"--1[[!!'')bL++)$*>*>*@*I*I)J"MM%d&6&6%7r::ODNN#3266()<)>'40 0  MM;SY=M=M=O Pzz +RWWTZZ5F5F5H5M-N-U-U-WX   s BB&c6|jjSrc)rrorOs r*rozGuard.is_specialized_nn_moduleus{{3355r,c6|jjSrc)rrfrOs r*rfzGuard.is_fsdp_modulexs{{))++r,c6|jjSrc)rrzrOs r*rzzGuard.is_local{s{{##%%r,ct|jtjr"|jj}|j S|j}|j Src)rrrrrrE)r(rs r*create_fn_namezGuard.create_fn_name~sJ dnni&7&7 8++I!!!I!!!r,c|jsg|_|jj||j|dfvsJd||_|js||_n|jj ||j |dfvs,t |j r|j Jd||_y)Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))rappendrrextendrcallable)r( guard_type guarded_classrrs r*set_export_infozGuard.set_export_infos!D   +))  .   9 9 9 &3"~~&DN NN ! !) ,   d 3 3(()  "* P P  P +'r,rAr5)rAztuple[bool, int, int, str, int])rrrAr|)rAz(Callable[[GuardBuilderBase, Guard], Any]r@rArR)robjectrArB)rrrArr{) rrBrrr list[str]rrrANone)rErFrGrHrrrrrrrrrrrrpropertyrr staticmethodrrr+rrorfrzrrr/r,r*rrs(88(,K$+%)I")$(K!(BF?F)-E &-37J07E=!OT!  2" ..66$$0  6,&" ' '< ' '  '  'r,rTc eZdZy) GuardEnvExprNrr/r,r*rrsr,rc*eZdZUded<ded<ddZy)DuplicateInputsrinput_source_ainput_source_bc:|j|jk7sJyrc)rrrOs r* __post_init__zDuplicateInputs.__post_init__s""d&9&9999r,NrAr)rErFrGrHrr/r,r*rrs:r,rc"eZdZUded<ded<y)StorageOverlapz list[Source]overlapping_sourcesnon_overlapping_sourcesNrr/r,r*rrs%%))r,rc0eZdZeddZeddZy)Checkpointablecyrcr/rOs r*copy_graphstatezCheckpointable.copy_graphstates$'r,cyrcr/r(states r*restore_graphstatez!Checkpointable.restore_graphstates47r,N)rAr)rrrAr)rErFrGrrrr/r,r*rrs''77r,rcBeZdZUdZeZded<ddZd dZd dZ y) GuardsCheckpointStatezW The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext set[Guard] dynamo_guardsc||_yrc)r )r(r s r*__init__zGuardsCheckpointState.__init__s *r,cn|jj|j}t|dk(ry|S)z Produces a delta against another GuardsCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched Guard type objects. rN)r  differencerr(rrs r*diffzGuardsCheckpointState.diffs3    ) )%*=*= > q6Q;r,cJt|tsy|j|duSNF)rrrrs r*__eq__zGuardsCheckpointState.__eq__s$%!67yy4''r,N)r rrAr)rrrAOptional[set[Guard]]rrrAr|) rErFrG__doc__setr rHr rrr/r,r*rrs$!$M:%+ (r,rc4eZdZUiZded<ddZddZd dZy) ModuleContextCheckpointStatedict[str, torch.nn.Module] nn_modulesc||_yrcr)r(rs r*r z%ModuleContextCheckpointState.__init__s $r,ct|jjjt|jj}t |dk(ry|S)z Produces a delta against another ModuleContextCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched module key names. rN)rrkeysr rrs r*rz!ModuleContextCheckpointState.diffsM $$& ' 2 23u7G7G7L7L7N3O P q6Q;r,cJt|tsy|j|duSr)rrrrs r*rz#ModuleContextCheckpointState.__eq__$%!=>yy4''r,N)rrrAr)rrrAOptional[set[str]]r)rErFrGrrHr rrr/r,r*rr s-/J*/% (r,rc$eZdZddZddZddZy) ModuleContextci|_yrcrrOs r*r zModuleContext.__init__$s *,r,c>tt|jSrc)rdictrrOs r*rzModuleContext.copy_graphstate's+D,ABBr,cJt|tsJ|j|_yrc)rrrrs r*rz ModuleContext.restore_graphstate*s %!=>>>**r,Nr)rAr)rrrArrErFrGr rrr/r,r*r$r$#s-C+r,r$c4eZdZUiZded<ddZddZd dZy) GlobalContextCheckpointStatedict[str, tuple[Callable, Any]] global_statec||_yrcr-)r( global_statess r*r z%GlobalContextCheckpointState.__init__2s )r,ct|jjjt|jj}t |dk(ry|S)z Produces a delta against another GlobalContextCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched global key names. rN)rr-rr rrs r*rz!GlobalContextCheckpointState.diff5sO !!&&( ) 4 4S9K9K9P9P9R5S T q6Q;r,cJt|tsy|j|duSr)rr+rrs r*rz#GlobalContextCheckpointState.__eq__Ar!r,N)r0r,rAr)rr+rAr"r)rErFrGr-rHr rrr/r,r*r+r+/s46L16* (r,r+c0eZdZdZhdZddZddZd dZy) GlobalContextzz This keeps track of the global torch state during tracing of a function. For example, torch.is_grad_enabled. > grad_enabledautocast_enabledautocast_cpu_dtypeautocast_gpu_dtypeautocast_cpu_enabledautocast_cache_enabledci|_yrcr/rOs r*r zGlobalContext.__init__Vs =?r,c,t|jSrc)r+r-rOs r*rzGlobalContext.copy_graphstateYs+D,=,=>>r,cbt|tsJ|j|_t|jt|jk(r0t |jj |jk(sJd|jjD] \}}||y)NzGlobal state mismatch)rr+r-r_supported_global_statesrrvalues)r(rrargss r*rz GlobalContext.restore_graphstate\s%!=>>>!.. !! "c$*G*G&H HD%%**,-1N1NN # # # O++224 JD$ J r,Nr)rAr+)rr+rAr)rErFrGrr>r rrr/r,r*r4r4Gs  @?r,r4cdeZdZd ddZddZddZddZddZddd  dd Zdd Z dd Z y) GuardsSetNc*| t}||_yrc)rinner)r(rDs r*r zGuardsSet.__init__js =EE r,c,t|jSrc)iterrDrOs r*__iter__zGuardsSet.__iter__oDJJr,c,t|jSrc)rrDrOs r*__len__zGuardsSet.__len__rs4::r,cFt|j|jz Src)rBrDrs r*__sub__zGuardsSet.__sub__wsekk122r,c,t|jSrc)r|rDrOs r*__bool__zGuardsSet.__bool__zrHr,Tr)collect_debug_stackskipc||jvry|r*|jtjd|z|_|jt j |_|jj|yNrS)rP)rDrrextractrTracingContext extract_stackadd)r(guardrOrPs r*rVz GuardsSet.add}sc DJJ   {{"/77QXF    #-;;=E  ur,cF|D]}|D]}|j|dyrR)rV)r(othersogs r*updatezGuardsSet.updates0 $A $# $ $r,cddlm}|jDchc]}||j|r|c}|_ycc}w)z.Delete all guards that contains a given sourcerS)is_from_sourceN)_dynamo.sourcer^rDr)r(rr^r[s r*remove_guards_with_sourcez#GuardsSet.remove_guards_with_sources62zz 8L8Lf)UA   s;;rc)rDrrAr)rAzIterator[Guard]r)rrBrArBr{)rWrrOr|rPr5rAr)rYrrAr)rrrAr) rErFrGr rGrJrLrNrVr\r`r/r,r*rBrBisR   3 <@Q  48 GJ   $  r,rBc$eZdZddZddZddZy) GuardsContextc0t|_g|_yrc)rBr aotautograd_guardsrOs r*r zGuardsContext.__init__s(1 68r,cRtt|jjSrc)rrr rDrOs r*rzGuardsContext.copy_graphstates$S););)A)A%BCCr,c\t|tsJt|j|_yrc)rrrBr rs r*rz GuardsContext.restore_graphstates&%!6777&u':':;r,Nr)rAr)rrrArr)r/r,r*rbrbs9DC =CCr,rhcpeZdZd dZd dZd dZddZddZddZddZ ddZ dd Z y )InvokeSubgraphCachecpi|_i|_tt|_tt |_yrc)autograd_cacheproxy_dispatch_cacherlistdynamo_installed_submodulesr'lazy_bwd_cacherOs r*r zInvokeSubgraphCache.__init__s1359;!ALTAR(   r,c@|j|j|yrc)rrrjs r*rmz2InvokeSubgraphCache.add_dynamo_installed_submodules ((/66zBr,c:|jj|gSrc)rgetros r*rpz3InvokeSubgraphCache.get_dynamo_installed_submoduless//33E2>>r,c"||j|<yrc)rrrs r*rtz*InvokeSubgraphCache.add_autograd_key_entrys*-J'r,c:|jj|dSrc)rrrvs r*rwz*InvokeSubgraphCache.get_autograd_key_entrys""&&z488r,c"||j|<yrc)rrrs r*ryz,InvokeSubgraphCache.add_proxy_dispatch_entrys03!!*-r,c:|jj|dSrc)rrrvs r*r{z,InvokeSubgraphCache.get_proxy_dispatch_entrys((,,Z>>r,c^t|j|}||f|j||<|Src)rr)r(rlr}r~ num_gmodss r*rz&InvokeSubgraphCache.add_lazy_bwd_entrys:++J78 =A9S =Sr,rc$eZdZddZ ddZy)HopDispatchSetCachec2ddlm}|ti|_y)Nr)invoke_subgraph)'torch._higher_order_ops.invoke_subgraphrr hop_cache_map)r(rs r*r zHopDispatchSetCache.__init__sK-/B/DEr,c>||jvry|j|Src)r)r(ops r* get_cachezHopDispatchSetCache.get_caches& T'' '!!"%%r,Nr)rztorch._ops.HigherOrderOperatorrAzOptional[HopSubgraphCache])rErFrGr rr/r,r*rrsF &0& #&r,rc\eZdZeddZeddZd dZed dZed dZy) CompileContextcFtjJtjSrc)_TLScompile_contextr/r,r*rzCompileContext.get"s ##///###r,c$ttddSNrgetattrrr/r,r*try_getzCompileContext.try_get't.55r,cV|t|tsJ||_d|_g|_yrh)rrr9rLshape_env_guards)r(r9s r*r zCompileContext.__init__+s-!Z I%FFF/9 +-r,cHtj}|y|jSrc)rrr9rOs r*current_compile_idz!CompileContext.current_compile_id2s"%%' <r,ctj}|y|jyt|j|jSrc)rrr9rKrLrOs r*current_trace_idzCompileContext.current_trace_id9s:%%' < ?? "t 55r,N)rAr)rAOptional[CompileContext])r9rDrAr)rArD)rAzOptional[TraceId]) rErFrGrrrr rrr/r,r*rr!sU$$66. 66r,rc6eZdZdZeddZeddZddZddZee ddZ eddZ ddZ ee jdd Zee j dd Zee jdd Zedd Zedd Zy)rTz Provides the currently installed TracingContext, or None. Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but will return None. c$ttddS)Ntracing_contextrr/r,r*rzTracingContext.try_getKrr,cHtjx}r|Std)Nz@D3704BF*CG+JN 490!8!: "'&9&;#+-r,ci|j_|jj|jjyrc)rr-rclearrrOs r*rzTracingContext.clears6,.( ))//1 ,,224r,c+rKi}tj}|jD]}t||||<|j D]\}}t ||| d|j D]\}}t |||y#|j D]\}}t |||wxYwwrc)rTrrrr4setattr)kwargspriorrrsvals r*patchzTracingContext.patchs  ";;= +C c*E#J +  #HC Cc " # ' !KKM 'SS#& 'EKKM 'SS#& 'sA B7#B '&B7 'B44B7ctj}|tjS|j}|j ||j gz}tjj|Src)rTr traceback StackSummaryrr_populate_loc_in_frame_summary from_list)r(rs r*rUzTracingContext.extract_stacksh%%' <))+ +((    (T@@BCCE%%//66r,cp|jJ|j\}}}tj|||dS)NF) lookup_line)rr FrameSummary)r(filenamelineno frame_names r*rz-TracingContext._populate_loc_in_frame_summarys>  ,,,'+'8'8$&*%%h PUVVr,c#Ktj}tjjj |dg5tjjj |dd5 d ddddddy#t $r}t|dsd|_d}~wwxYw#1swY7xYw#1swYyxYww)Nrr real_stack) rTrunittestmockrrr7hasattrr)tcr>s r* clear_framezTracingContext.clear_frames   ! MM   & &r+@" E  MM   & &r>4 @        $q,/#'AL)      sTAC,B7.B+0B5B7= C B(B##B((B++B4 0B77C<Cc#Ktj}||jj||j}d|_ d ||jj||_y#t $r'}t |ds|j|_d}~wwxYw#||jj||_wxYww)Nr) rTrrrrr7rrUrpop) frame_summaryroldr>s r* current_framezTracingContext.current_frames   !  $  " " ) )- 8oo "  (&&**,!BO 1l+!//1   (&&**,!BOs6AC A1 $C 1 B!:"BB!!B$$%C  C c#Ktj}|dy|j}g|_ |j||_y#||_wxYwwrc)rTrr)rold_output_stridess r*report_output_stridesz$TracingContext.report_output_stridessY  # # % :J .. 3## # 2B  2B s/AAA AAc<|||ftj_yrc)rTrr)rrrs r*set_current_loczTracingContext.set_current_locs.6vz,J)r,cHtj}|y|jSrc)rTrr)rs r*get_traced_codezTracingContext.get_traced_codes"  # # % :~~r,N)rAOptional[TracingContext])rArT)rOptional[FakeTensorMode]rArr)rrrAGenerator[None, None, None])rAztraceback.StackSummary)rAztraceback.FrameSummary)rAr)rz Optional[traceback.FrameSummary]rAr)rAz@Generator[Optional[list[Optional[tuple[int, ...]]]], None, None])rrBrr5rrBrAr)rAzOptional[list[CodeType]])rErFrGrrrrr rrrrUr contextlibrrrrrr/r,r*rTrTCs66  1.f5 ' '77W<"7" $""* 3 3KK r,rTc#Kttdd}|t_ ||t_y#|t_wxYwwr)rrr)context old_contexts r*rrs9$ 148K"D+ *{sA0 A =Ac#Kttdd}|t_ | |F|j:|jj$|jjj|t_y#t$r)}t |ds||j |_d}~wwxYw#|F|j:|jj$|jjj|t_wxYww)z This function installs the passed in tracing context as a dynamic scoped global variable. Calls to TracingContext.get() while not under a `with tracing()` context will return None. rNr) rrrr7rrUrr shape_envcleanup)rrr>s r*tracingrs$ 148K"D +   !!-!!++7    ' ' / / 1* q,'G,?"002AL   !!-!!++7    ' ' / / 1*s5DA9AD9 B+$B&&B++B..ADDcLeZdZd dZd dZd dZd dZd dZddZd dZ d dZ y )rcyrr/rOs r* is_dict_keyzSource.is_dict_key5r,cyrr/rOs r* is_ephemeralzSource.is_ephemeral8rr,ctrcNotImplementedError)r(codegens r* reconstructzSource.reconstruct;!!r,ctrcr rOs r*rzSource.guard_source>rr,ctrcr rOs r*rz Source.nameArr,cf|jtjurtt ||Src)rrRr~r r)r(fns r* make_guardzSource.make_guardDs+    +"6"6 6% %T2r,c>|jjSrc)rrorOs r*rozSource.is_specialized_nn_moduleIs  ";;==r,cD|jtjk7S)z+True if you can guard on attributes of this)rrRrrOs r*subguards_allowedzSource.subguards_allowedLs  "k&A&AAAr,Nr{)r rrArrr@)rzCallable[..., Any]rAr) rErFrGrrr rrrrorr/r,r*rr3s+""" >Br,rc0eZdZUded<ddZddZddZy) ChainedSourcerbasec6|jjSrc)rrrOs r*rzChainedSource.is_dict_keyVsyy$$&&r,c6|jjSrc)rrrOs r*rzChainedSource.is_ephemeralZsyy%%''r,cd|}t|tr|j}t|tr|Src)rrr)r(currents r*get_basezChainedSource.get_base]s+-0llG-0r,Nr{)rAr)rErFrGrHrrrr/r,r*rrRs L'(r,rczddlm}m}g}tj x}r"|j }||j |ddfddlm}tt|D]&\}}t||s|j |d|f(tj|} t| D]0\}} t| |s|j | j d|f2|r\|d\}} } |ddD]I\}} }||ur Jd |d | d | d |d | d |d | d | d|jd| d |d|j|Sy)a Attempts to "detect" what the current fake mode is. If there is one ambiently available from TracingContext, we preferentially use that. Otherwise, we heuristically detect the fake mode via the following sources, in order of priority: - Currently active fake mode on stack - Fake mode associated with passed in tensors (inputs does not have to be flattened) r) FakeTensorrNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor inputrSz fake mode (z) from rz doesn't match mode (z fake mode from z allocated at: z fake mode from )torch._subclasses.fake_tensorr rrTrrrtorch.utils._python_dispatchr" enumeratereversedrpytree tree_leavesr)inputsr r fake_modesrrr"im flat_inputs flat_inputdesc1i1desc2i2s r*detect_fake_moder3dsIJ ((**w*%%    y*;Q? @M(#C#EFG:1 a (   q"4a8 9:$$V,K";/N : j* -   z335H!L MN)!} 5"&qrN LAub> i[wat;PQRPSSZ[`Zaabcebfg""'"-=ioo=NO""'"-=aggYH >  r,c~ddlm}ddlm}t t |D]\}}t ||s|cSy)z~ Inspects the dispatch mode stack for an active fake mode and returns it. Returns None if no fake mode is active. rrr!N)r#rr$r"r%r&r)rr"rNr,s r*active_fake_moder5s? =M(#C#EFG1 a (H r,)rrrAz/Generator[Optional[CompileContext], None, None])rrrAz/Generator[Optional[TracingContext], None, None]rc)r)rrAr)rAr)\ __future__rr dataclassesenumrloggingre threadingr unittest.mockrrabcr collectionsrrrtypingrr r r r r rrr torch.utilsrr'torch.utils._backport_slotsrtorch.utils._tracebackrrtorch.utils.weakr getLoggerrErcollections.abcrrtypesrsympy"torch._dynamo.backends.distributedrtorch._dynamo.codegenr&torch._functorch._aot_autograd.schemasrr#rcompiler0r1rrKEnumrRrrrrrrrrrrrr$r+r4rBrbrhrrlocalrrrTrrrrr3r5r/r,r*rNsZ"  #%!    )7B4g!3F/J< RZZ PQ" X$ $/J/J/Jd 7j 7; $)); |   d#  $    o'o'o'd CLd#  $  d#:l:$: d#*\*$* 8WQZ8((8((0 +N#?@ +((0N#?@D+ + \