L i+ddlZddlZddlZddlmZmZddlmZddlm Z ddl m Z ddl m Z ddlmZmZddlmZmZmZdd lmZej.eZej4d Gd d eZGddZej4GddZdeddddfdZdeddfdZe e!e"efZ#GddZ$y)N)ABCabstractmethod) defaultdict) Generator)contextmanager)chain)AnyOptional)AppendingByteSerializer BytesReader BytesWriter) OrderedSetT)frozenceZdZUdZeed<ejdZe ed<e de dddd fd Z e d ed e ddfd Ze dede fdZeddZdefdZe defdZy ) CacheArtifactzO Data for each cache artifact that will be serialized and deserialized keyF)reprcontentwriterclsreturnNcp|j|j|j|jyN) write_strr write_bytesr)rrs [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/compiler/_cache.py serializezCacheArtifact.serializes&!3;;' artifact_typereadercp|j}|j}tj|||Sr)read_str read_bytesCacheArtifactFactorycreate)rr rrs r deserializezCacheArtifact.deserialize$s1oo##%#**=#wGGrcLt|tsJdt||S)NzExpected bytes, got ) isinstancebytestype)rs rencodezCacheArtifact.encode*s('5)Q-A$w-+QQ)rcyrselfs rpopulate_cachezCacheArtifact.populate_cache/s rcy)NFr-r.s rprecompile_compatiblez#CacheArtifact.precompile_compatible3srctd)a  Returns the type of the artifact. Must be unique across all CacheArtifact classes. CacheArtifactFactory.register will add property method to CacheInfo based on this (def {type}_artifacts) that returns all artifacts for specific cache. z9CacheArtifact is an abstract class, please use a subclass) RuntimeErrorr-rrr*zCacheArtifact.type6sVWWrrN)__name__ __module__ __qualname____doc__str__annotations__ dataclassesfieldrr) staticmethodr rr r&r r+rr0boolr2r*r-rrrrs H&[&&E2GU2(+(O(((H3H HHH   tX#XXrrc eZdZUdZiZeeeefe d<e deedeefdZ e dedeefdZ e deded e defd Ze deded edefd Zy ) r$zH Factory for creating CacheArtifact objects based on their type _artifact_types artifact_clsrc|j|j|jvs Jdd||j<ttdt fd|S)NArtifact of type=z2 already registered in mega-cache artifact factory _artifactsc"|jSr artifacts)r/artifact_type_keys rz/CacheArtifactFactory.register..Rs$..1B"Cr)r*rAsetattr CacheInfoproperty)rrBrIs @rregisterzCacheArtifactFactory.registerHs(--/  "#*=*==  122d e =2>-.  ! , C D rrIcR||jvs Jd|d|j|S)NrDz. not registered in mega-cache artifact factory)rA)rrIs r_get_artifact_typez'CacheArtifactFactory._get_artifact_typeVs@ C$7$77  122` a 7""#455rrrc6|j|}|||Sr)rPrrIrrrBs rr%zCacheArtifactFactory.create]s --.?@ C))rcT|j|}|||j|Sr)rPr+rRs r encode_createz"CacheArtifactFactory.encode_createbs---.?@ C!4!4W!=>>rN)r6r7r8r9rAdictr:r*rr; classmethodrNrPr)r%r rTr-rrr$r$As79OT#tM2238 D$7 Drs D 1r)default_factoryrHrcyrr-r.s rinductor_artifactszCacheInfo.inductor_artifactsv rcyrr-r.s rautotune_artifactszCacheInfo.autotune_artifactszr]rcyrr-r.s raot_autograd_artifactsz CacheInfo.aot_autograd_artifacts~r]rcyrr-r.s r pgo_artifactszCacheInfo.pgo_artifactsr]rcyrr-r.s r!precompile_aot_autograd_artifactsz+CacheInfo.precompile_aot_autograd_artifactsr]rcyrr-r.s rprecompile_dynamo_artifactsz%CacheInfo.precompile_dynamo_artifactsr]rartifactNcp|j|jj|jyr)rHr*appendr)r/rhs raddz CacheInfo.adds# x}}'..x||>!!rr5)r6r7r8r9r<r=rHrr:rYr;rMr\r_rarcrergrrkrmr?ror-rrrLrLjs .?[->->1.I{3S >*  DI   DI   S    tCy   49   T#Y  =M=d="t"rrLrrztuple[str, list[CacheArtifact]]rc|j|d|jt|d|dD]}tj ||y)Nr)r write_uint64lenrr)rrrhs r_serialize_single_cachertsO SV CF $F212rr cg}|j}|j}t|D]'}|jtj ||)||fSr)r" read_uint64rangerjrr&)r rHrI num_artifacts_s r_deserialize_single_cacherzscI)&&(M = !O223DfMNO i ''rc veZdZUdZeeZeed<e Z e e ed<e e Ze eeee fed<eZeed<edd Zeeded fd Zed ed ededdfdZedefdZedeeeeffdZededeefdZ ededefdZ!eddZ"y)CacheArtifactManageraQ Lightweight manager class for collecting and processing cache artifacts for hot loading Intended Lifecycle: - Execute code via torch.compile, this will call CacheArtifactManager.record_artifact on each cache artifact - Call CacheArtifactManager.serialize to convert all the cache artifacts to portable format - Call CacheArtifactManager.deserialize to hot load the cache artifacts on a potentially different process NOTE: There's no FB/FC guarantees, results of cache artifacts will not be used unless code version matches. _new_cache_artifacts_seen_artifacts serialize_fn _serializer _cache_inforNc|jj|jj|jj|jjyr)r}rmr~rrrs rrmzCacheArtifactManager.clearsF   &&( !!#  r)NNNc#K|j}|j}|j}|j}t t |_t |_tt|_|jj|_ d||_||_||_||_y#||_||_||_||_wxYww)Nr) r}r~rrrrYrr rt __class__)roriginal_new_cache_artifactsoriginal_seen_artifactsoriginal_serializeroriginal_cache_infos rwith_fresh_cachez%CacheArtifactManager.with_fresh_caches(+'?'?$"%"5"5!oo!oo#.t#4 (l1?VW//335 2 'CC $"9C 1CO1CO(DC $"9C 1CO1COsBC  B,C ,C  C rrrctj|||}||jvrytj dt ||j |j||jj|y)zg Called from each caching operation to record the artifact in this "mega" list Nz Recording %s) r$rTr~logdebugr:r}rjrk)rrrrrhs rrecord_artifactz$CacheArtifactManager.record_artifactsh(55mS'R s** *  .#h-0   /66x@ )rc2t|jdk7S)zG Have we seen new artifacts since last serialize call? r)rsr}rs rneed_serializez#CacheArtifactManager.need_serializes 3++,11rcFt|jjD]3}tj d||j j |5|j jry tj|j }|jj|jj|jj}|jj||fS#t$rtj!ddYywxYw)z? Converts the "mega" list into portable format z saving: %sNz Failed to pickle cache artifactsTexc_info)rr}valuesrrrrkrocopydeepcopyrextenditemsto_bytesrm Exceptionwarning)rrhinfoartifact_bytess rrzCacheArtifactManager.serializes s77>>@A *H IIlH - OO   ) * ?? " K==1D OO " "3#;#;#A#A#C D __557N  $ $ * * ,!4' ' K KK:TK J Ks3B C== D D serialized_artifactsc tjttj|t }|S#t $rtjddYywxYw)zG Converts the portable format back into CacheArtifacts )deserialize_fnz#Failed to un-pickle cache artifactsTrN) r|"_ensure_cache_artifacts_registeredrUr to_listrzrrr)rrHs rr&z CacheArtifactManager.deserializes^  C C E'//(#<I   KK=K M s8< AArHct}t|jD]9}tj d||j ||j ;|S)Nz writing: %s)rLrrrrrkr0)rHrrhs rpopulate_cachesz$CacheArtifactManager.populate_caches/sS{y//12 &H IImX . HHX   # # % &  rc4ddlm}ddlm}ddlm}ddlm}y)zWhen deserializing caches in fresh process, we need to ensure that all cache artifacts are registered in the cache registry. This is done by simply importing all the cache artifacts already wrapped with register call. r)PGOCacheArtifact)AOTAutogradCacheArtifact)InductorCacheArtifact)AutotuneCacheArtifactN)torch._dynamo.pgor-torch._functorch._aot_autograd.autograd_cachertorch._inductor.codecacher&torch._inductor.runtime.autotune_cacher)rrrrrs rrz7CacheArtifactManager._ensure_cache_artifacts_registered9s 7  D rr5)#r6r7r8r9rrYr}CacheArtifactsResultr;rr~rr rtrtupler:rLrrVrmrrrr rr?rr r)rr>r&rrr-rrr|r|s"2=T1B.B2<OZ .= -DE(sD4G/G)HI'[K(  2+;!<22$*** *  **"2t22 (5 )9#:;2%H=Q4R$#7I    rr|)%rr<loggingabcrr collectionsrcollections.abcr contextlibr itertoolsrtypingr r &torch.utils._appending_byte_serializerr r r torch.utils._ordered_setr getLoggerr6r dataclassrr$rLrtrzrUr:rYrr|r-rrrs ##%%  0g!d#'XC'X$'XT&?&?R *"*"*"Z2 2?2 2 (  (& (Cm!445V V r