
    Vh,              	          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	m
Z
mZmZ d dlmZ d dlZd dlZd dlmc mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d	d
lmZmZm Z m!Z!  ejD                  e#      Z$ G d d      Z% G d d      Z&de'de(e'   de'de'fdZ)dedee(e'   e*e'e(e'   f   f   defdZ+ G d d      Z,	 ddede'de-de,fdZ.y)    N)Path)AnyIOOptionalUnion)Self)config)BuildOptionsBase
CppBuilder)reorder_kwargs)FileLike   )AOTINDUCTOR_DIRARCHIVE_VERSIONCONSTANTS_DIRCUSTOM_OBJ_FILENAME_PREFIXc                   b    e Zd ZdeddfdZdefdZddZdede	e
ef   ddfd	Zded
eddfdZy)PT2ArchiveWriterarchive_pathreturnNc                      || _         d | _        y Nr   archive_fileselfr   s     O/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/package/package.py__init__zPT2ArchiveWriter.__init__   s    &27;    c                     | j                   J t        j                  | j                  dt        j                        | _         | j                  dt        t                     | j                  dd       | S )Nwcompressionversionarchive_formatpt2)r   zipfileZipFiler   
ZIP_STOREDwritestrstrr   r   s    r   	__enter__zPT2ArchiveWriter.__enter__#   sa      (((#OOs0B0B
 	i_!56&.r   c                 b    | j                   J | j                   j                          d | _         y r   r   closer   argss     r   __exit__zPT2ArchiveWriter.__exit__,   s1      ,,,! r   namedatac                 X    | j                   J | j                   j                  ||       y r   )r   r*   )r   r4   r5   s      r   r*   zPT2ArchiveWriter.writestr2   s*      ,,,""4.r   	file_pathc                     t        |      j                         s
J | d       | j                  J | j                  j                  ||       y)z
        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   r4   r7   s      r   
write_filezPT2ArchiveWriter.write_file6   sR     I&&(QYK7P*QQ(  ,,,	48r   r   N)__name__
__module____qualname__r   r   r   r-   r3   r+   r   bytesr*   r<    r   r   r   r      sc    <X <$ <4 /S /eSj(9 /d /9s 9s 9t 9r   r   c                   v    e Zd ZdeddfdZdefdZddZdedefdZ	d	ed
edefdZ
d
eddfdZdee   fdZy)PT2ArchiveReaderr   r   Nc                      || _         d | _        y r   r   r   s     r   r   zPT2ArchiveReader.__init__B   s    !-7;r   c                 p    t        j                  | j                  dt         j                        | _        | S )Nrr"   )r'   r(   r   r)   r   r,   s    r   r-   zPT2ArchiveReader.__enter__F   s-    #OOs0B0B
 r   c                 P    | j                   | j                   j                          y r   r/   r1   s     r   r3   zPT2ArchiveReader.__exit__L   s#    (##%r   r4   c                 T    | j                   J | j                   j                  |      S r   )r   read)r   r4   s     r   rJ   zPT2ArchiveReader.readQ   s+      ,,,  %%d++r   memberpathc                 V    | j                   J | j                   j                  ||      S r   )r   extract)r   rK   rL   s      r   extract_to_pathz PT2ArchiveReader.extract_to_pathU   s-      ,,,  ((66r   c                 V    | j                   J | j                   j                  |       y r   )r   
extractall)r   rL   s     r   rQ   zPT2ArchiveReader.extractallY   s(      ,,,$$T*r   c                 R    | j                   J | j                   j                         S r   )r   namelistr,   s    r   get_file_nameszPT2ArchiveReader.get_file_names]   s)      ,,,  ))++r   r=   )r>   r?   r@   r+   r   r   r-   r3   rA   rJ   rO   rQ   listrT   rB   r   r   rD   rD   A   sv    <S <T <4 
, , ,7c 7 7 7+s +t +,S	 ,r   rD   aoti_dir
aoti_filesso_pathr   c                    dt         dt         ffd}t        j                  j                  |  |d            }t        j                  j                  |  |d            }t        j                  j	                  |      d   }t        |dz         5 }t        j                  |      }d d d        t        di dt        j                         i}	t        |||		      }
|
j                         }|
j                          t        |d
z         5 }t        j                  |      }d d d        t        di dt        j                         i}t        t        j                  j                  |      d   ||g||      }|j                         }|j                          |dz   }|v rwt        |d      5 }|j                         }d d d        t        |d      5 }|j!                         }|j#                  dd|dz  z
  z         |j#                         d d d        |S |S # 1 sw Y   yxY w# 1 sw Y   xY w# 1 sw Y   wxY w# 1 sw Y   |S xY w)Nsuffixr   c                 X    D ]  }|j                  |       s|c S  t        d|        )Nz Unable to find file with suffix )endswithRuntimeError)rZ   filerW   s     r   get_aoti_file_with_suffixz-compile_so.<locals>.get_aoti_file_with_suffixc   s8     	D}}V$	 =fXFGGr   .cppz.or   z_compile_flags.jsonuse_relative_path)r4   sourcesBuildOptionz_linker_flags.json)r4   rb   rc   
output_dirz_serialized_weights.binrbza+b    i @  rB   )r+   osrL   joinsplitextopenjsonloadr
   r	   	is_fbcoder   get_target_file_pathbuildsplitrJ   tellr;   )rV   rW   rX   r_   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   sA   H# H# H ww||H&?&GHHww||H&?&EFH  *1-I 
i//	0 %A		!% ' 
+1+;+;+=O  #N
 224H 
i..	/ $1yy|$ & 
*0*:*:*<N WW]]7#B'8$"	J //1I #,.G"G"j0-t4 	2	!*!1	2 )U# 	+tiikGJJtuw678JJ)*		+ 9U% %$ $$	2 	2	+ s0   HH*0H7<IH'*H47I Ir   c                 ~   t        |t              rd|i}t        |t              sJ d       t        | t        j                  t
        f      r | j                         r| j                         sNt        | t        t        j                  f      r$t        j                  |       j                  d      s
J d|         t        |       5 }|j                         D ]  \  }}d}d}|D ]  }|dk(  r
|j                  d      r|dz  }|dkD  rt        d	| d
      |j                  d      r|dz  }|dkD  rt        d| d
      t        j                   j#                  |      }|j%                  t&              r%t        j                   j)                  t*        |      }	n%t        j                   j)                  t,        ||      }	t.        j1                  d||	       |j3                  t        |	      |          	 ddd       t        | t        j                  t
        f      r| j5                  d       | S # 1 sw Y   <xY w)a_  
    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.r`   zMultiple .cpp files found in z.Saving AOTI generated file %s to archive in %sN)
isinstancerU   dictioIOBaser   writableseekabler+   rh   PathLikefspathr\   r   itemsr]   rL   basename
startswithr   ri   r   r   logdebugr<   seek)
r   rW   archive_writer
model_namefilesnum_so_filesnum_cpp_filesr^   filenamenew_filepaths
             r   package_aotir      s1    *d#z*
j$' 	R' 	<"))R1!!#!!#<#r{{!34IIl#,,V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#%77<<x#HL#%77<<X#VL		DdL ))%; 	$	%N ,B0!S% %s   D2H33H<c                       e Zd ZdZdej
                  j                  j                  ddfdZd Z	de
eef   fdZde
eej                  f   d	eddfd
Zdee   fdZdee
eef      dd fdZy)AOTICompiledModelz8
    Callable AOT Inductor loaded model from a .pt2
    loaderr   Nc                     || _         y r   )r   )r   r   s     r   r   zAOTICompiledModel.__init__   s	    r   c                    | j                   j                         }t        j                  |d         }t        j                  |d         }t        j                  |t        ||      f      d   }|D cg c]  }t        |t        j                        s|! }}| j                   j                  |      }t        j                  ||      S c c}w )Nr   r   )r   get_call_specpytreetreespec_loadstree_flattenr   r   torchTensor	boxed_runtree_unflatten)	r   r2   kwargs	call_specin_specout_specflat_inputsxflat_outputss	            r   __call__zAOTICompiledModel.__call__   s    KK--/	''	!5((16))41P*QRSTU"-MQAu||1LqMM{{,,[9$$\8<< Ns   3C
C
c                 6    | j                   j                         S r   )r   get_metadatar,   s    r   r   zAOTICompiledModel.get_metadata   s    {{''))r   constants_mapcheck_full_updatec                >    | j                   j                  |d|       y)a  
        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   z AOTICompiledModel.load_constants  s      	""=%9JKr   c                 6    | j                   j                         S r   )r   get_constant_fqnsr,   s    r   r   z#AOTICompiledModel.get_constant_fqns  s    {{,,..r   memoc                 V    t         j                  d       t        | j                        S )NzOAOTICompiledModel deepcopy warning: AOTICompiledModel.loader is not deepcopied.)r   warningr   r   )r   r   s     r   __deepcopy__zAOTICompiledModel.__deepcopy__  s"    ]	
 !--r   )r>   r?   r@   __doc__r   _C_aotiAOTIModelPackageLoaderr   r   r   r+   r   r   boolr   rU   r   r   r   r   rB   r   r   r   r      s    uxx~~DD  =*d38n *LC-.L  	L
 
L$/49 /.$sCx.!9 .>Q .r   r   rL   r   run_single_threadedc                 l   t        | t        j                  t        f      r | j	                         r| j                         sNt        | t        t        j                  f      r$t        j                  |       j                  d      s
J d|         t        | t        j                  t        f      rt        j                  d      5 }|j                  | j                                | j                  d       t         j#                  d|j$                         t&        j(                  j*                  j-                  |j$                  ||      }t/        |      cd d d        S t        j                  |       } t&        j(                  j*                  j-                  | ||      }t/        |      S # 1 sw Y   TxY w)Nr   zTUnable to load package. Path must be a buffer or a file ending in .pt2. Instead got )rZ   r   z)Writing buffer to tmp file located at %s.)r   r   r   r   readabler   r+   rh   r   r   r\   tempfileNamedTemporaryFiler;   rJ   r   r   r   r4   r   r   r   r   r   )rL   r   r   rv   r   s        r   load_packager     sG    	4"))R)dmmo$--/
TC-
.299T?3K3KF3S __c^de U $B(((7 		-1 GGDIIK IIaLIIA166JXX^^::
$7F %V,		- 		- 99T?DXX^^22j-F V$$		- 		-s   BF**F3)r   F)/r   rl   loggingrh   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   	getLoggerr>   r   r   rD   r+   rU   r   r   r   r   r   r   rB   r   r   <module>r      s   	   	    + + "   $ $ " D 3    g! 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   