
    rhe                         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Zd dlm	Z	 d dl
mZmZ d dlmZmZmZmZ d dlmZ  ej(                  e      Zdedee   d	ed
efdZdeded
efdZ	 	 	 	 ddededededed
efdZy)    N)IO)config)BuildOptionsBase
CppBuilder)
AOTI_FILESAOTICompiledModelload_pt2package_pt2)FileLikeaoti_dir
aoti_filesso_pathreturnc                    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)r   filer   s     r/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_inductor/package/package.pyget_aoti_file_with_suffixz-compile_so.<locals>.get_aoti_file_with_suffix   s8     	D}}V$	 =fXFGG    z.cppz.or   z_compile_flags.jsonuse_relative_path)namesourcesBuildOptionz_linker_flags.json)r   r   r   
output_dirz_serialized_weights.binrbza+b    i @   )strospathjoinsplitextopenjsonloadr   r   	is_fbcoder   get_target_file_pathbuildsplitreadtellwrite)r   r   r   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_sorB      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archive_filec                     t        | |      S )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.
    )r   )r
   )rC   r   s     r   package_aotirE   R   s      r   r$   
model_namerun_single_threadednum_runnersdevice_indexc                    	 t        | |||      }||j                  vrt        d| d      |j                  |   S # t        $ r t        j	                  d       Y nw xY wt        | t        j                  t        f      rt        j                  d      5 }| j                  d       |j                  | j                                t        j                  d|j                         t         j"                  j$                  j'                  |j                  ||||      }t)        |      cd d d        S # 1 sw Y   nxY wt+        j,                  |       } t         j"                  j$                  j'                  | ||||      }t)        |      S )	N)rG   rH   rI   zModel z not found in packagez:Loading outdated pt2 file. Please regenerate your package.z.pt2)r   r   z)Writing buffer to tmp file located at %s.)r	   aoti_runnersr   logwarning
isinstanceioIOBaser   tempfileNamedTemporaryFileseekr0   r.   debugr   torch_C_aotiAOTIModelPackageLoaderr   r#   fspath)r$   rF   rG   rH   rI   pt2_contentsr4   loaders           r   load_packager\   f   sO   R 3#%	
 \666
|3HIJJ((44 RPQR $B(((7 		-1 IIaLGGDIIK IIA166JXX^^::
$7lF %V,		- 		- 		- 99T?DXX^^22j-{LF V$$s   := AABD33D<)modelF   r   )rO   r(   loggingr#   rQ   typingr   rU   torch._inductorr   torch._inductor.cpp_builderr   r   !torch.export.pt2_archive._packager   r   r	   r
   torch.typesr   	getLogger__name__rL   r"   listrB   rE   boolintr\   r!   r   r   <module>rj      s    	   	    " D  ! g!8 8$s) 8c 8c 8v ,  %$%
$%$% $% 	$%
 $% $%r   