
    sh,              	          S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
JrJr  S SKJr  S SKrS SKrS SKJs  Jr  S SKJr  S SKJrJr  S SKJr  S SKJr  S	S
KJrJrJ r J!r!  \RD                  " \#5      r$ " S S5      r% " S S5      r&S\'S\(\'   S\'S\'4S jr)S\S\\(\'   \*\'\(\'   4   4   S\4S jr+ " S S5      r, SS\S\'S\-S\,4S jjr.g)    N)Path)AnyIOOptionalUnion)Self)config)BuildOptionsBase
CppBuilder)reorder_kwargs)FileLike   )AOTINDUCTOR_DIRARCHIVE_VERSIONCONSTANTS_DIRCUSTOM_OBJ_FILENAME_PREFIXc                   t    \ rS rSrS\SS4S jrS\4S jrSS jrS\	S	\
\\	4   SS4S
 jrS\	S\	SS4S jrSrg)PT2ArchiveWriter   archive_pathreturnNc                     Xl         S U l        g Nr   archive_fileselfr   s     s/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_inductor/package/package.py__init__PT2ArchiveWriter.__init__   s    &27;    c                     U R                   b   e[        R                  " U R                  S[        R                  S9U l         U R                  S[        [        5      5        U R                  SS5        U $ )Nwcompressionversionarchive_formatpt2)r   zipfileZipFiler   
ZIP_STOREDwritestrstrr   r   s    r   	__enter__PT2ArchiveWriter.__enter__#   sa      (((#OOs0B0B
 	i_!56&.r!   c                 d    U R                   c   eU R                   R                  5         S U l         g r   r   closer   argss     r   __exit__PT2ArchiveWriter.__exit__,   s1      ,,,! r!   namedatac                 X    U R                   c   eU R                   R                  X5        g r   )r   r,   )r   r8   r9   s      r   r,   PT2ArchiveWriter.writestr2   s(      ,,,""4.r!   	file_pathc                     [        U5      R                  5       (       d
   U S35       eU R                  c   eU R                  R                  X!S9  g)zr
Copy a file into the archive.
name: The destination file inside the archive.
file_path: The source file on disk.
z is not a valid file pathN)arcname)r   is_filer   write)r   r8   r<   s      r   
write_filePT2ArchiveWriter.write_file6   sS     I&&((QYK7P*QQ(  ,,,	8r!   r   r   r   N)__name__
__module____qualname____firstlineno__r   r   r   r/   r6   r-   r   bytesr,   rA   __static_attributes__ r!   r   r   r      sc    <X <$ <4 /S /eSj(9 /d /9s 9s 9t 9r!   r   c                       \ rS rSrS\SS4S jrS\4S jrSS jrS\S\	4S	 jr
S
\S\S\4S jrS\SS4S jrS\\   4S jrSrg)PT2ArchiveReaderA   r   r   Nc                     Xl         S U l        g r   r   r   s     r   r   PT2ArchiveReader.__init__B   s    !-7;r!   c                 l    [         R                  " U R                  S[         R                  S9U l        U $ )Nrr$   )r)   r*   r   r+   r   r.   s    r   r/   PT2ArchiveReader.__enter__F   s-    #OOs0B0B
 r!   c                 R    U R                   b  U R                   R                  5         g r   r2   r4   s     r   r6   PT2ArchiveReader.__exit__L   s#    (##%r!   r8   c                 V    U R                   c   eU R                   R                  U5      $ r   )r   read)r   r8   s     r   rW   PT2ArchiveReader.readQ   s+      ,,,  %%d++r!   memberpathc                 V    U R                   c   eU R                   R                  X5      $ r   )r   extract)r   rY   rZ   s      r   extract_to_path PT2ArchiveReader.extract_to_pathU   s+      ,,,  ((66r!   c                 X    U R                   c   eU R                   R                  U5        g r   )r   
extractall)r   rZ   s     r   r`   PT2ArchiveReader.extractallY   s(      ,,,$$T*r!   c                 T    U R                   c   eU R                   R                  5       $ r   )r   namelistr.   s    r   get_file_namesPT2ArchiveReader.get_file_names]   s)      ,,,  ))++r!   rC   rD   )rE   rF   rG   rH   r-   r   r   r/   r6   rI   rW   r]   r`   listrd   rJ   rK   r!   r   rM   rM   A   sv    <S <T <4 
, , ,7c 7 7 7+s +t +,S	 ,r!   rM   aoti_dir
aoti_filesso_pathr   c                   ^ S[         S[         4U4S jjn[        R                  R                  X" S5      5      n[        R                  R                  X" S5      5      n[        R                  R	                  U5      S   n[        US-   5       n[        R                  " U5      nS S S 5        [        S0 WDS[        R                  " 5       0D6n	[        UUU	S	9n
U
R                  5       nU
R                  5         [        US
-   5       n[        R                  " U5      nS S S 5        [        S0 WDS[        R                  " 5       0D6n[        [        R                  R                  U5      S   X/UUS9nUR                  5       nUR                  5         US-   nUT;   aw  [        US5       nUR                  5       nS S S 5        [        US5       nUR!                  5       nUR#                  SSUS-  -
  -  5        UR#                  W5        S S S 5        U$ U$ ! , (       d  f       GN|= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       U$ = f)Nsuffixr   c                 f   > T H  nUR                  U 5      (       d  M  Us  $    [        SU  35      e)Nz Unable to find file with suffix )endswithRuntimeError)rk   filerh   s     r   get_aoti_file_with_suffix-compile_so.<locals>.get_aoti_file_with_suffixc   s6    D}}V$$  =fXFGGr!   .cppz.or   z_compile_flags.jsonuse_relative_path)r8   sourcesBuildOptionz_linker_flags.json)r8   rt   ru   
output_dirz_serialized_weights.binrbza+b    i @  rK   )r-   osrZ   joinsplitextopenjsonloadr
   r	   	is_fbcoder   get_target_file_pathbuildsplitrW   tellr@   )rg   rh   ri   rp   cpp_fileconsts_o	file_namefcompile_flagscompile_optionsobject_builderoutput_olinker_flagslinker_options
so_builder	output_soserialized_weights_filename	f_weightsserialized_weightsf_soso_sizes    `                   r   
compile_sor   b   s(   H# H# H ww||H&?&GHHww||H&?&EFH  *1-I 
i//	0A		! 
1 ' 
+1+;+;+=O  #N
 224H 
i..	/1yy| 
0 & 
*0*:*:*<N WW]]7#B'$"	J //1I #,.G"G"j0-t4	!*!1 5 )U#tiikGJJtuw678JJ)*	 $ 9U 
1	0 
0	/$ 54 $# s0   H
H./I <I
H+.
H= 
I
I r   c                     [        U[        5      (       a  SU0n[        U[        5      (       d   S5       e[        U [        R                  [
        45      (       a*  U R                  5       (       a  U R                  5       (       dY  [        U [        [        R                  45      (       a*  [        R                  " U 5      R                  S5      (       d
   SU  35       e[        U 5       nUR                  5        GH0  u  p4SnSnU GH  nUS:X  a  M  UR                  S5      (       a  US-  nUS:  a  [        S	U S
35      eUR                  S5      (       a  US-  nUS:  a  [        SU S
35      e[        R                   R#                  U5      nUR%                  [&        5      (       a%  [        R                   R)                  [*        U5      n	O$[        R                   R)                  [,        X85      n	[.        R1                  SXy5        UR3                  [        U	5      U5        GM"     GM3     SSS5        [        U [        R                  [
        45      (       a  U R5                  S5        U $ ! , (       d  f       NF= f)aC  
Saves the AOTInductor generated files to the PT2Archive format.

Args:
    archive_file: The file name to save the package to.
    aoti_files: This can either be a singular path to a directory containing
    the AOTInductor files, or a dictionary mapping the model name to the
    path to its AOTInductor generated files.
modelzPlease pass a list of AOTI generated files to be packaged or a dictionary mapping model names to their list of AOTI generated files. You can get this list of files through calling `torch._inductor.aot_compile(..., options={aot_inductor.package=True})`.pt2zMExpect archive file to be a file ending in .pt2, or is a buffer. Instead got r    z.sor   zMultiple .so files found in zQ. You might need to clear your cache directory before calling aoti_compile again.rr   zMultiple .cpp files found in z.Saving AOTI generated file %s to archive in %sN)
isinstancerf   dictioIOBaser   writableseekabler-   rz   PathLikefspathrm   r   itemsrn   rZ   basename
startswithr   r{   r   r   logdebugrA   seek)
r   rh   archive_writer
model_namefilesnum_so_filesnum_cpp_filesro   filenamenew_filepaths
             r   package_aotir      s;    *d##z*
j$'' 	R' 	<"))R11!!##!!##<#r{{!344IIl#,,V44	 XXdWef	  
,	'>!+!1!1!3JLM2:=='' A%L#a'*:5' BK K 
 ==((!Q&M#a'*;E7 CK K  77++D1&&'ABB#%77<<x#HL#%77<<#VL		Dd ))%; 	 "4 
(N ,B00!S 
(	's   )EI//
I=c                       \ rS rSrSrS\R                  R                  R                  SS4S jr	S r
S\\\4   4S jrS	\\\R                  4   S
\SS4S jrS\\   4S jrS\\\\4      SS 4S jrSrg)AOTICompiledModel   z0
Callable AOT Inductor loaded model from a .pt2
loaderr   Nc                     Xl         g r   r   )r   r   s     r   r   AOTICompiledModel.__init__   s    r!   c                    U R                   R                  5       n[        R                  " US   5      n[        R                  " US   5      n[        R                  " U[        X$5      45      S   nU Vs/ s H&  n[        U[        R                  5      (       d  M$  UPM(     nnU R                   R                  U5      n[        R                  " X5      $ s  snf )Nr   r   )r   get_call_specpytreetreespec_loadstree_flattenr   r   torchTensor	boxed_runtree_unflatten)	r   r5   kwargs	call_specin_specout_specflat_inputsxflat_outputss	            r   __call__AOTICompiledModel.__call__   s    KK--/	''	!5((16))41P*QRSTU"-M+QAu||1Lq+M{{,,[9$$\<< Ns   5#CCc                 6    U R                   R                  5       $ r   )r   get_metadatar.   s    r   r   AOTICompiledModel.get_metadata   s    {{''))r!   constants_mapcheck_full_updatec                >    U R                   R                  USU5        g)ae  
Given a mapping of constant fqns to tensors, load the constants into the model.
You can use ``get_constant_fqns`` to get the list of constant fqns that
are needed in the compiled model.

Args:
    constants_map: A mapping of constant fqns to tensors.
    check_full_update: Whether to add check to see if all the constants
    are updated and have values.
FN)r   load_constants)r   r   r   s      r   r    AOTICompiledModel.load_constants  s      	""=%9JKr!   c                 6    U R                   R                  5       $ r   )r   get_constant_fqnsr.   s    r   r   #AOTICompiledModel.get_constant_fqns  s    {{,,..r!   memoc                 V    [         R                  S5        [        U R                  5      $ )NzOAOTICompiledModel deepcopy warning: AOTICompiledModel.loader is not deepcopied.)r   warningr   r   )r   r   s     r   __deepcopy__AOTICompiledModel.__deepcopy__  s"    ]	
 !--r!   r   )rE   rF   rG   rH   __doc__r   _C_aotiAOTIModelPackageLoaderr   r   r   r-   r   r   boolr   rf   r   r   r   r   rJ   rK   r!   r   r   r      s    uxx~~DD  =*d38n *LC-.L  	L
 
L$/49 /.$sCx.!9 .>Q .r!   r   rZ   r   run_single_threadedc                    [        U [        R                  [        45      (       a*  U R	                  5       (       a  U R                  5       (       dY  [        U [        [        R                  45      (       a*  [        R                  " U 5      R                  S5      (       d
   SU  35       e[        U [        R                  [        45      (       a  [        R                  " SS9 nUR                  U R                  5       5        U R                  S5        [         R#                  SUR$                  5        [&        R(                  R*                  R-                  UR$                  X5      n[/        U5      sS S S 5        $ [        R                  " U 5      n [&        R(                  R*                  R-                  XU5      n[/        U5      $ ! , (       d  f       NY= f)Nr   zTUnable to load package. Path must be a buffer or a file ending in .pt2. Instead got )rk   r   z)Writing buffer to tmp file located at %s.)r   r   r   r   readabler   r-   rz   r   r   rm   tempfileNamedTemporaryFiler@   rW   r   r   r   r8   r   r   r   r   r   )rZ   r   r   r   r   s        r   load_packager     sI    	4"))R))dmmoo$--//
TC-
.
.299T?3K3KF3S3S __c^de U $B((((71 GGDIIK IIaLIIA166JXX^^::
F %V, 87 99T?DXX^^22-F V$$ 87s   "BG
G)r   F)/r   r~   loggingrz   r   r)   pathlibr   typingr   r   r   r   typing_extensionsr   r   torch._inductortorch.utils._pytreeutils_pytreer   r	   torch._inductor.cpp_builderr
   r   torch.export._tree_utilsr   torch.typesr   pt2_archive_constantsr   r   r   r   	getLoggerrE   r   r   rM   r-   rf   r   r   r   r   r   r   rK   r!   r   <module>r      s	   	   	    + + "   $ $ " D 3    ! 9  9F, ,B8 8$s) 8c 8c 8vMMd3ic49n!556M M`-. -.b LQ%
% #%DH%%r!   