~L i "dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZmZddlmZmZmZmZddlmZddlZddlZddlmZddlmZm Z ddl!m"Z"dd l#m$Z$d d l%m&Z&d d l'm(Z(m)Z)e jTe+Z,ejZd GddZ.ejZGddZ/ede0Z1ede0Z2ejZd GddZ3ejZGddZ4ejZGdde4Z5de5dejlfdZ7dejldedefd Z8dejlde9e0e0ffd!Z:ejZGd"d#Z;e$jxGd$d%ee;Z=d&e0de0fd'Z>d(ej~d)e@d*e@deAe0fd+ZBd(ej~d)e@d*e@de0fd,ZCdejldejdfd-ZEGd.d/ZFe$jxGd0d1eeZGeHe1eefZIGd2d3ejZKGd4d5eKZLGd6d7eKZMGd8d9eMZNeNe jje"d:ZQy);a This module provides the infrastructure for creating and managing compile package for torch.compile. We mainly have two abstractions here: - CompilePackage: Overarching data structure for store and lookup a list of compiled codes. - CodeCacheEntry: Data structure for a single code being compiled by torch.compile. The caching behavior is always under user control explicitly so that a stronger guarantee can be provided about cache hit for a specific compiled model. Users can load the compile package from a different process or host. N) GeneratorIterator)AnyCallableNewTypeOptional)Never PackageError)PrecompileCacheArtifactPrecompileContext) cache_dir)CacheArtifactFactory) get_code_keys) dynamo_timedincrement_frameT)frozenceZdZUeed<eed<eed<eed<eed<eed<eed<eedfed <eedfed <eedfed <eed <eed <eed<eedfed<eedfed<dZ e eed<dZ e eed<dZ e eed<dZ e eed<eej dej$ddfdZeej dddej$fdZy)SerializedCode co_argcountco_posonlyargcountco_kwonlyargcount co_nlocals co_stacksizeco_flagsco_code. co_constsco_names co_varnames co_filenameco_nameco_firstlineno co_cellvars co_freevarsN co_linetable co_qualnameco_exceptiontable co_lnotabcodereturnctDcic]}|t||}}tfd|dD|d<di|Scc}w)Nc3xK|]1}t|tjrj|n|3ywN) isinstancetypesCodeTypefrom_code_object.0cclss [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/_dynamo/package.py z2SerializedCode.from_code_object..Fs6$ (2!U^^'DC  #! K$ s7:r)rgetattrtuple)r6r*keykwargss` r7r2zSerializedCode.from_code_objectBs\6C_Ec#wtS))EE#$ K($  {}V} FsAserialized_codectDcic]}|t||}}tfd|dD|d<tj|j Scc}w)Nc3dK|]'}t|trj|n|)ywr.)r/rto_code_objectr3s r7r8z0SerializedCode.to_code_object..Ps2$ &0>%BC  q ! I$ s-0r)rr:r;r0r1values)r6r>r<r=s` r7rAzSerializedCode.to_code_objectLskANP#w44PP#$ K($  {~~ ]]_  QsA)__name__ __module__ __qualname__int__annotations__bytesr;rstrr&rr'r(r) classmethod functoolscacher0r1r2rAr9r7rr,sOM NS#XCHosCx  LsCx sCx $(L(5/(!%K#%)-x-#Ix}#__ENN7G__ -= %..  rMrc&eZdZUdZeed<eed<y)_GuardedCodeCacheEntrya  Contains the serializable information associated with a single compilation in dynamo. To restore an execution of compiled code, we will need to serialize the following data: - Dynamo bytecode for mapping Python inputs/outputs. - Dynamo guards. guards_state dynamo_codeN)rCrDrE__doc__rHrGrr9rMr7rOrOYsrMrO _BackendId _FunctionIdc6eZdZUeed<eed<eed<eed<y) InlinedSourcemodule firstlineno lastlinenochecksumN)rCrDrErIrGrFr9rMr7rVrVjs KOMrMrVc2eZdZUdZeeed<eeed<y)DynamoCaptureOutputzD Core information generated from Dynamo for fullgraph=True. guarded_codes backend_idsN)rCrDrErRlistrOrGrSr9rMr7r\r\rs .//j!!rMr\ceZdZUdZeed<eed<eeed<e eefed<e eed<e ed<dZ e ed <dZ e ed <y ) _DynamoCodeCacheEntrya Contains the serializable information associated with a single code object in dynamo. To restore an execution of compiled code, we will need the following ingredients: 1. The "original" code object, which serves as the entry point for eager execution, i.e. the code only executed when there's no cache entry hit. 2. The python module name this code object belongs to, for identifying the enclosing global scope to inject compiled and resume functions. 3. A list of function names that pointing to this code object. There could be multiple function objects pointing to the same code such as recursive functions. 4. A list of guarded code that eval frame dispatches to. 5. A list of imported module objects unioned from all compiled branches. 6. A list of "backends" (compiled fx graph) unioned from all compield branches. 7. A string path used to access the original code object users defined. A code object can be accessed by "{python_module}.{function_name}.{code_source}" . 8. A boolean flag indicating whether the function is installed to global scope. 9. A boolean flag indicating whether the function has a compile id. 10. Whether or not this code entry was bypassed python_code python_modulefunction_namesimport_sources code_sourceinstall_to_globalFhas_compile_idbypassedN)rCrDrErRrrGrIr_rTdictrboolrhrir9rMr7rara|sR( %%cN"# ND HdrMraentryr+ct|jdk(sJtj|j}|jdj d}|D]}t ||}|jr|jj d}|D]}|jdrg|jd}t|tr|dk\sJt ||d|d}|td||tj||dzd}{t ||}ntd|t|tj sJ|S)Nrr.][z"Cannot find source for code entry )lenrdsysmodulesrcsplitr:rfendswithrfindr/rFr ast literal_evalr0r1)rlfnpartspart index_beginattrs r7 _lookup_codersJ u## $ )) )kk%--.B   # ) )# .E R  !!'', 'D}}S!"jjo !+s3 q8HHHr4 #5t<<&)KE7'STT#**4 a"+EFGR& '?wGHH b%.. )) ) IrMr*scopec$td|d|)Nz*Cannot resolve a fully qualified name for z. Lookup scope: r )r*rs r7_raise_resolution_errorrs  4TF:J5'R rMctj}|td|tjdk\r]j j d}|D]=}t|s tt|tjs=ntdtdttffd }| tj|j!dfS)aS Given a code object, return a fully qualified name which will be used as a serialized handle to access the code object from the new process. This is normally a straightforward process, but there are some corner cases: 1. When a function is defined with decorator, then this function will be captured inside a closure with the wrapper object. 2. When a function is defined as a nested function, then the code object will be stored on the co_consts field of the parent code object by Python compiler. This function handles all of the corner cases above. Cannot find module for code  rnobjr+cf| vry j|tj|r6| uryt|jD]\}}|x}d|d|cStj |r|j x}| d|S|jkt|jD]S\}} |j}tj |stj|s>|x}I| d|d|cStjdkrtj|rX|jjD];}tj |stj|s.|x}9|cStj|r|jj!D]e\}}t#||}tj |stj|s=|x}H|j$|k7r t' |cSy#t$rY{wxYw)Nz .co_consts[roz .__code__z .__closure__[z].cell_contentsr)addinspectiscode enumerater isfunction__code__ __closure__ cell_contents ValueErrorrs version_infoismodule__dict__rBisclassitemsr:rCr) riconstrescellrvaluename_find_code_sourcer*seentoplevels r7rz+_get_code_source.._find_code_sources) $;  >># d{%cmm4 35,U33C@(1SE22 3   c "(66C"3%((*(9 GGAt!(,(:(:  **=9">>-8 0??L#&!.qcFF G   g %$ \\002#E#..u59O 077D" # s##&<<#5#5#7#KD%#C.E#..u59O 077D >>T13D(C" #9&! !s; H## H0/H0)r getmoduler rsrr'ruhasattrrr:rsetrrrIrEstrip)r*rWr{r|rfrrrs` @@@r7_get_code_sourcers  t $F ~9$@AAH 7"  &&s+ D8T*'h7x.H!!(+   5D4s4x}44l$H-Kh/  +"3"3C"8 88rMceZdZUeeed<eeed<ejZ e ed<e jZ e ed<edeefdZy)_DynamoCacheEntrycodesinlined_sourcespython_version torch_versionr+ch|jDchc]}|jD]}|c}}Scc}}wr.)rr^)selfr* backend_ids r7r^z_DynamoCacheEntry.backend_idss,'+zzUtDDTDTUj U UUUs.N)rCrDrEr_rarGrrVplatformrrItorch __version__rpropertyrSr^r9rMr7rrs^ % &&''1(113NC3**M3* VS_VVrMrc.eZdZedefdZdefdZy)_DynamoCacheArtifactr+cy)Nprecompile_dynamor9r9rMr7typez_DynamoCacheArtifact.type s"rMc@tj|jSr.pickleloadscontentrs r7after_deserializationz*_DynamoCacheArtifact.after_deserialization$||DLL))rMN)rCrDrE staticmethodrIrrrr9rMr7rrs(####*'8*rMrsourcectj}|j|j|j Sr.)hashlibsha256updateencode hexdigest)r sha256_hashs r7 _hash_sourcer(s1.."Kv}}'  ""rMmrXrYcDtj|d|dz |dz S)Nrr)rgetsourcelinesrrXrYs r7_get_sourcelinesr.s)  ! !! $Q ' a*q. IIrMc Ltdjt|||S)Nr)rjoinrrs r7_hash_sourcelinesr4s  0K LM NNrMcddlmmddlmmt jdtdffd }|S)Nr)get_compile_idlog_dynamo_start)compile_contextCompileContextr+c 3vKti}|5tdddttjj j jjjd5dddddddy#1swYxYw#1swYyxYww)N) frame_statez_compile.compile_innerentire_frame_compile!dynamo_cumulative_compile_time_us) frame_keyr"r!r#) phase_namedynamo_compile_column_usmetadata) rrrIr_dynamoutils curr_framer"r!r#) compile_idrr*rrrs r7_ctxz$_compile_frame_context.._ctxCs#3 N:6 7  (1)L"%U]]%8%8%C%C!D#||#'#3#3&*&9&9    T " !      s5"B9AB- B!B- B9!B* &B--B62B9) torch._dynamo.convert_framerr torch._guardsrr contextlibcontextmanagerr)r*rrrrrs` @@@@r7_compile_frame_contextr8s?M=(4.* 6MrMc eZdZdZ d.deedefdeededdfdZ defd Z d.dedeededdfd Z d/d e jd ed eedeededdf dZedeeeffdZej,defdZde jddfdZej4de jdedfdZdede jddfdZdee jddfdZ d0dZ!d e jd ed eeddfdZ"dededdfd Z#d1d!ed"eeddfd#Z$d0d$Z%d%e jLd&ed'eddfd(Z'd0d)Z(d*eeefddfd+Z)defd,Z*e+dedefdefd-Z,y)2CompilePackagea CompilePackage is considered a low level component and should not be directly exposed to end users. It has the following interface: 1. `CompilePackage.__init__()` which optionally takes previously serialized dynamo states. a. when `dynamo` argument is None, it will construct a brand new CompilePackage object. b. when `dynamo` argument is not None, it will load a pre-compiled dynamo state. 2. `package.save()` which dumps the dynamo and backend states to a DynamoCacheEntry object. 3. `package.install(backends) which will handle all the side-effectful global scope updates with compiled functions and resume functions. Nrz.dynamoignore_inlined_sourcesr+cd|_i|_d|_i|_i|_t |_t |_d|_|4|j||||j|jyyNF) _innermost_fn_codes_current_entry_installed_globals_cached_backendsr_inlined_sources _resume_codes _initialized initialize uninstallvalidate)rrzrrs r7__init__zCompilePackage.__init__isw "CE ?CEG8:47E25%! > OOB(> ? NN  MMO rMc|jSr.)rrs r7is_initializedzCompilePackage.is_initializeds   rMc  ddlm}|jrJt|_|||_|j J|t |tsJ|jtjk7rtd|j|jtjk7rtd|j|s|jD]}tj |j"}t%||j&|j(}||j*k7sStd|j"d|j&d|j(d|j|_|j,^}} |j j.|i|_| D].}||j0t2j5|j6<0d |_y|j9|j j.|j j:d |_y) Nr innermost_fnz=Compile package was created with a different Python version: z>Compile package was created with a different PyTorch version: z!Source code changes detected for z (line z - line )T) eval_framerrrrrr/rrr RuntimeErrorrrrr importlib import_modulerWrrXrYrZrrrrrArb _add_functionrD) rrzrrrr*rrZmainrs r7rzCompilePackage.initializes -$$$$ #)"-!!---  f&78 88$$(?(?(AA"STZTiTiSjk##u'8'88"TU[UiUiTjk*"22D!// (>%!<> >>##{2 22  $    & &} 5 %rMc|jSr.)rrs r7cached_backendszCompilePackage.cached_backendss$$$rMc\|jJtj|jSr.)rrsource_id_from_fnrs r7 source_idzCompilePackage.source_ids+!!---//0B0BCCrMr*ct|\}}tj|}|td||j ||j t ||y)Nr)rrf)rrrr rrCrT)rr*rrfrWs r7_add_user_functionz!CompilePackage._add_user_functionsa%5d%;" {""4( >!=dVDE E   OO%m4#  rM)NNNc#BK|jJ||jvr|j||j|}||_ d|jr |j|=d|_d|_y#|jr |j|=d|_d|_wxYwwNT)rrrrirh)rr*rls r7 code_contextzCompilePackage.code_contexts""*** t{{ "  # #D ) D!# ' KK%#'E "&D  KK%#'E "&D sABA3 (B3)BBrPrQc|jJ|jjryt|tj |}|jj j |y)N)rPrQ)rrirOrr2r]r )rrPrQguarded_code_entrys r7add_guarded_codezCompilePackage.add_guarded_codesa ""...    ' ' 3%&77 D  ))001CDrMsourcesc |jJ|jjry|D]}||jvrtj|}|*tj |\}}|t |z}dj|}|djt|||k(sJ|jjt|j||t|y)Nr)rWrXrYrZ)rrirrrrrrrrrrrVrCr)rrr*rW sourcelinesrXrYrs r7add_inlined_sourcez!CompilePackage.add_inlined_sources""...    ' '  Dt)))&&t,F~'.'='=d'C $K$s;'77JWW[)FRWW%5fk:%VWW WW  ! ! % %!?? +))&1   rMcB|jJd|j_yr)rrirs r7bypass_current_entryz#CompilePackage.bypass_current_entrys"""...'+$rMc~|j|||r t|ndd|jj|y)NT)rrg)rrTrr)rrbrcrs r7add_resume_functionz"CompilePackage.add_resume_functionsB   8E+m44"  {+rMalias module_namecR|jJ||jj|<yr.)rre)rr r!s r7add_import_sourcez CompilePackage.add_import_source+s*""...4?**51rMrbackendc|jJ|jdsJt|}|jjj ||||j |<yy)N__compiled_fn_)r startswithrSr^r r)rrr$s r7add_backend_idzCompilePackage.add_backend_id/sh""...$$%5666 +  ''..z:  07D ! !* - rMc|jJ|jJ|jsJtt |j |jj usJyr.)rrrnextiterrrrs r7rzCompilePackage.validate7s]""***!!---    D%&$*<*<*E*EEEErMrWrrcx||j|<|jj|gj|yr.)rr setdefaultr )rrWrrs r7_install_globalzCompilePackage._install_global=s1 % **626==dCrMcddlm}|jJ|jj D]'\}}|D]}|j j |)i|_||jjy)Nr)_reset_precompile_entries)torch._C._dynamo.eval_framer0rrrrpopr)rr0rWnamesrs r7rzCompilePackage.uninstallAsxI!!---!44::< *MFE *##D) * *#%!$"4"4"="=>rMbackendsc ddlm}ddlm}|j |j j D]\}}|jr t|ntj}|5tj|j}|jj D]+\}} |j||t!j"| -|} |j$rE|j&D]6} t)j*||j,| } |j|| | 8|j.r t1|} |j2D]n} | |vrt5d| dt7dd 5|| j9}|j|| t:j<j?|d d d ptA|jBdk(r)t:j<jDjG| |jBD]}tIjJ|jL}tj|jj,}|jjNx}r||}||vr |||usJ|||<tQ|t:j<jRjTsJt:j<jRjW| |j|jX| }|| |jZt\j_|j`  d d d y #1swYxYw#1swYxYw) a3 Sync the package states to the compiled function. This includes the following actions: 1. Clean up the previously installed states. 2. Install the compiled functions to global scopes. 3. Install the precompiled cache entries to ExtraStates on the code object. r)_load_precompile_entryr)get_builtins_dictBackend # is not found in the given backendsrbackend_compile)rN)shape_code_partsruntime_global_scope)1r1r6 output_graphr7rrrrhrr nullcontextrsrtrcrer.rrrgrdr0 FunctionTyperrfrr^rrrrrdisablerrr]r skip_coderrrP%name_of_builtins_dict_key_in_fglobalsr/guards GuardsStateCheckFunctionManagerr; guard_managerrrArQ)rr4r6r7r*rlcontextrWr r! target_coderrzrr$ guarded_coderPr<builtin_dict_name builtins_dictcheck_fn_managers r7installzCompilePackage.installMs( G3 ;;,,.C KD%'''t,++-  = U%8%89*/*>*>*D*D*F&E;((y'>'>{'K# **).)=)=H "//foo}U,,V]BGH$$".u"5K"'"3"3 J!1*&zl2UV&/S>S  rMc0ddlm}||}tj}|j |j j |j t|jjj |jS)Nrr) rrrrrrErrIrr#r)rzr innermost_fn_rs r7rz CompilePackage.source_id_from_fnso,$R( nn& =55<<>?3}55DDELLNO$$&&rMr)NNFr+Nr.)-rCrDrErRrrrrrkrrrr0r1rIrTrrrjrSr rKcached_propertyrrrrrrrHrr_rrrr#r(r ModuleTyper.rrMrOrrr9rMr7rr\s /3',  Xc3h' (*+!%   ,!! /3', )! )!*+)!!% )!  )!^04%)"' 6^^66 , 6 c] 6  6 6:%j#o!6%%D3DD  u~~  $  ''I>N4O''( E E^^ E  E$u~~*>4., ,^^ , , } ,  ,@s@@@88x}8PT8F De&6&6DcD#DRVD ?OZ_ 5O$Ob . 'hsCx0'S''rMrc.eZdZedefdZdefdZy)EagerCacheArtifactr+cy)Nprecompile_eagerr9r9rMr7rzEagerCacheArtifact.types!rMc@tj|jSr.rrs r7rz(EagerCacheArtifact.after_deserializationrrMN)rCrDrErrIrrrr9rMr7rVrVs'"#""*s*rMrVc <eZdZdZdeddfdZdededdfdZe jdd Z e jd e d e d eddfd Zde deddfdZdededdfdZe jd edee e ffdZdedee eeefffdZdededeeeeefffdZy) DynamoStorez A DynamoStore tracks active CompilePackages, and provides methods to store and retrieve them. This is an abstract base class for different storage implementations. packager+Nc|j}tj|}tjt j |j|y)z] Records a package to PrecompileContext, so that it can be serialized later. r<rN)rOrdumpsr record_artifactrrr)rr\rOpickled_results r7record_packagezDynamoStore.record_packagesD))+ k2)) % % 'W->-> rMrr$ctj|}tjtj ||y)zT Records eager fx graphs to PrecompileContext for testing purposes. r^N)rr_r r`rVr)rrr$ras r7record_eager_backendz DynamoStore.record_eager_backends0 g.))  # # %:~ rMcyr.r9rs r7clearzDynamoStore.clears rMrr4pathcy)a Abstract method to write dynamo cache entry and backends to storage. Args: dynamo: The dynamo cache entry to write backends: Dictionary of backend content to write path: Path or key to identify where to write the data Nr9rrr4rgs r7writezDynamoStore.writes rMrOr<ci}|jD]?}tj|}|td|dt |t sJ|||<A|j |||y)Y Saves a package to a given path. Grabs backends from PrecompileContext. Nr8r9)r^r serialize_artifact_by_keyrr/r rj)rrOr<backend_contentrserialized_backends r7save_cache_entryzDynamoStore.save_cache_entrys&(%11 =J!2!L!LZ!X !)"zl*MN02IJ JJ*|jr*tjj|j|n|} tj|dt tjj|dd5}t j||dddt tjj|dd5}t j||dddy#1swYSxYw#1swYyxYw#t$r}td|d||d}~wwxYw) z@ Write dynamo cache entry and backends to disk. T)exist_okrwbNr4zFailed to save package to : ) rosrgrmakedirsopenrdump Exceptionr)rrr4rg dynamo_path backend_pathes r7rjzDiskDynamoStore.write\s8<7G7Grww||D,,d3T P KKt ,bggll42D9 1[ FK0 1bggll44d; 4| Hl3 4 4 1 1 4 4 P!;D6A3GHa O PsNAC<;C$2C<C0C<$C-)C<0C95C<9C<< DDDc|jr*tjj|j|n|} t tjj|dd5}t j |}dddt tjj|dd5}t j |}dddfS#1swYUxYw#1swYxYw#t$r}td|d||d}~wwxYw)zA Read dynamo cache entry and backends from disk. rrbNr4z!Failed to load package from path r) rrrgrrrloadrr)rrgrrOrrnrs r7ruzDiskDynamoStore.reados8<7G7Grww||D,,d3T Wbggll42D9 7[$kk+6  7bggll44d; <|"(++l"; </ /  7 7 < < W!B4&1#NOUV V WsG*C&$C:2C&,C C&CC&C#C&& D/DD)rrR) rCrDrErRrIrrfrr}rjr;rur9rMr7rrGsj'C'@P!PP P  P& W W'8)'C!D WrMrc eZdZdZdeddffd Zdedefdee e e e effffd Z dedefdeefd ZxZS) DiskDynamoCachezs Special DiskDynamoStore which adds some helper functions for automatically tracking paths of packages r\r+Nc~|j}tjd|jt|||y)rlzSaving CompilePackage for %sN)rloggerinfosuperrr)rr\r< __class__s r7savezDiskDynamoCache.saves4 2G4E4EF Wc*rMrz.ctj|}tjd|tj j |j|}tj j|r t|)|}|Stjd|y#t$r*}tjd|t|Yd}~yd}~wwxYw)ryzLoading CompilePackage for %sz'Failed to load package from path %s: %sNzNo package found for %s)rrrrrrgrrexistsrrwrwarningrI)rrzr<rgresultrrs r7rzDiskDynamoCache.loads ..r2 3S9ww||D,,c2 77>>$  1#6   -s3  H$PSTUPVW s7B C( C  Ccr|j|}|y|\}}t||}|j||S)zC Load directly into a package and install backends N)rrrM)rrzresultsrlr4r\s r7load_and_install_packagez(DiskDynamoCache.load_and_install_packages@ ))B- ? ' UH$R/G OOH %NrM)rCrDrErRrrrrrr;rrjrSrr __classcell__)rs@r7rr~su +N+t+38$ %)4 C+@@A B& 38$  . ! rMrr)RrRr{rxr dataclassesrKrrrloggingrrrrrsr0collections.abcrrtypingrrrrtyping_extensionsr rtorch._inductor.packagetorch._dynamo.excr torch._dynamo.precompile_contextr r 'torch._inductor.runtime.cache_dir_utilsrtorch.compiler._cacherbytecode_transformationrrrr getLoggerrCr dataclassrrOrIrSrTrVr\rar1rrr;rrregisterrrrTrFr_rrAbstractContextManagerrrrVrjr}ABCr[rrrrgr DynamoCacher9rMr7rs   /33# *W=620   8 $d#) ) $) X      \3 ' mS) d#$ """ />-%..0%.. U95>>U9eCHoU9p VVV*23DE**### J J&)J7:J #YJ O))OOOQTO! ..!&&t,!HQ'Q'h *05** 4S99 : g(#''g(T#+#@4Wk4Wn.o.bbggll9;AB rM