
    ȅi!                         S SK r S SKJr  / r\ R                  " \5      rS\S\\   S\	S\
\\4   S-  S\	S\4S	 jrS\S\\   S\	S\	S\4
S
 jrg)    N)IndentedBufferpackage_namemodel_namescudaexample_inputs_mapis_hipreturnc                 @   [        5       nUR                  / SQ5        U(       a-  U(       a  UR                  S/5        OUR                  SS/5        U H  nUR                  SU  SU SU S35        M      UR                  5         U H  nUR                  S	U S
35        M     UR                  / SQ5        UR	                  5          UR                  SU(       a  SOS S35        UR                  S5        UR	                  5          UR                  S5        UGb  [        U5       GHB  u  pvX6   nUR                  SU 35        UR                  SUS-    S
35        UR                  SU S35        UR	                  5          UR                  SU S35        UR                  S5        UR                  S5        UR	                  5          UR                  S5        UR                  S5        SSS5        UR                  S5        UR                  S5        UR                  S 5        UR                  S!US-    S"35        SSS5        UR                  S5        UR                  5         GME     UR                  5         UR                  S#5        [        [        U5      5       H$  nUR                  S$US-    S%3S&US-    S'3/5        M&     UR                  S(5        [        [        U5      5       H  nUR                  S)US-    S
35        M     UR                  S*5        [        U5       HR  u  pvUR                  S+US-    S,3S-US-    S.3S/US-    S0U S13S2US-    S33S4US-    S33S5S6U  SU S73S8US-    S93/5        MT     UGb#  UR                  S:5        [        [        U5      5       HE  nUR                  S;US-    S<35        UR                  S8US-    S=US-    S>US-    S?US-    S@3	5        MG     UR                  SA5        [        [        U5      5       H$  nUR                  SBUS-    S%3SCUS-    SD3/5        M&     UR                  SE5        [        [        U5      5       H?  nUR                  SFUS-    SGUS-    SH35        UR                  SIUS-    SJUS-    SK35        MA     UR                  SL5        SSS5        UR                  SM/5        UR	                  5          UR                  SN5        UR                  S5        SSS5        UR                  S5        SSS5        UR                  S5        UR                  5       $ ! , (       d  f       GNb= f! , (       d  f       GN!= f! , (       d  f       N= f! , (       d  f       N}= f! , (       d  f       Nu= f)OaQ  
Generates a main.cpp file for AOTInductor standalone models in the specified package.

Args:
    package_name (str): Name of the package containing the models.
    model_names (List[str]): List of model names to include in the generated main.cpp.
    cuda (bool): Whether to generate code with CUDA support.
    example_inputs_map (Optional[Dict[str, List[Tensor]]]): A mapping from model name to
        its list of example input tensors. If provided, the generated main.cpp will
        load and run these inputs.

Returns:
    str: The contents of the generated main.cpp file as a string.
)z#include <dlfcn.h>z#include <fstream>z#include <iostream>z#include <memory>z#include <torch/torch.h>z#include <vector>z<#include <torch/csrc/inductor/aoti_torch/tensor_converter.h>z#include <hip/hip_runtime.h>z#include <cuda.h>z#include <cuda_runtime_api.h>z
#include "/data/aotinductor//z.h"z+using torch::aot_inductor::AOTInductorModel;)z*using torch::aot_inductor::ConstantHandle;z'using torch::aot_inductor::ConstantMap; z"int main(int argc, char* argv[]) {zstd::string device_str = "r   cpuz";ztry {zc10::Device device(device_str);Nz // Load input tensors for model z%std::vector<at::Tensor> input_tensors   zfor (int j = 0; j < z; ++j) {zstd::string filename = "z%_input_" + std::to_string(j) + ".pt";z-std::ifstream in(filename, std::ios::binary);zif (!in.is_open()) {z>std::cerr << "Failed to open file: " << filename << std::endl;z	return 1;}zastd::vector<char> buffer((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());z2torch::IValue ivalue = torch::pickle_load(buffer);input_tensorsz).push_back(ivalue.toTensor().to(device));z!
// Create array of input handleszauto input_handlesz =zL    torch::aot_inductor::unsafe_alloc_new_handles_from_tensors(input_tensorsz);z#
// Create array for output handleszAtenTensorHandle output_handlez
// Create and load modelszauto constants_mapz# = std::make_shared<ConstantMap>();zauto constants_arrayz3 = std::make_shared<std::vector<ConstantHandle>>();z
auto modelz$ = std::make_unique<AOTInductorModelz>(z    std::move(constants_mapz),z    std::move(constants_arrayz    device_str,z    "z/");modelz->load_constants();z
// Run the modelsz,torch::aot_inductor::DeviceStreamType streamz = nullptr;z->run(&input_handlesz[0], &output_handlez, streamz, nullptr);z%
// Convert output handles to tensorszauto output_tensorzN    torch::aot_inductor::alloc_tensors_by_stealing_from_handles(&output_handlez, 1);z
// Validate outputszstd::cout << "output_tensorz\n" << output_tensorz << std::endl;ztorch::save(output_tensorz, "output_tensorz.pt");z	return 0;z#} catch (const std::exception &e) {z0std::cerr << "Error: " << e.what() << std::endl;)	r   
writelines	writelinenewlineindent	enumeraterangelengetvalue)	r   r   r   r   r   ib
model_namei
num_inputss	            Z/home/james-whalen/.local/lib/python3.13/site-packages/torch/export/experimental/_utils.py_get_main_cpp_filer!   
   s   , 
	BMM	

 MM2 MM'3 "

&8Aj\QTU	
 "
 JJL!

B:,aPQ " MM	
 

1D&e1LBOP
WYY[LL:;!-%.{%;MA!3!?JLL#CJ<!PQLL#HQq!QRLL#7
|9!MN6zlBgh %TU%;<YY[LL ` LL5	 )
 S) P +AE72[\% %* LL%JJL9 &<< 

ABs;/0AMM0Qr:jklopkpjqqst 1 CDs;/0ALL#A!a%!JK 1 LL67!*;!7,QUG3VW.q1ug5hi$QUG+OPZ|[]^5a!eWB?7AwbA)~-?
|4PAw&9:	 "8 "-23s;/0ALLFq1ug[Y LLAw&:1q5'ATUVYZUZT[[cdehidicjjuv	 1 EFs;/0AMM0Qr:lmnqrmrlssxy 1 45s;/0ALL7Aw>STUXYTYSZZhk LL5a!eW<LQQRUGSY\	 1 LL%y | 	5	

 YY[LLKLLL%  	SW 
X LL;;=s )[ % [F [O 
sq   A VA<U-AU	#U	.AU	I9U-:*V$#U>V	
UU	
U*%U--
U;	7V>
V	V
Vc                    [        5       nUR                  / SQ5        SSKJn  UR                  (       a  UR                  S5        U(       a*  U(       a  UR                  S5        OUR                  S5        UR                  5         U H  nUR                  SU  SU S	35        M     UR                  S
5        U(       a*  U(       a  UR                  S5        OUR                  S5        SR                  U5      nUR                  SU S35        U(       a9  U(       a!  UR                  S5        UR                  5       $ UR                  S5        UR                  5       $ )N)z$cmake_minimum_required(VERSION 3.10)zproject(TestProject)r   zset(CMAKE_CXX_STANDARD 17)r   r   )test_configszfind_package(Torch REQUIRED)zfind_package(hip REQUIRED)zfind_package(CUDA REQUIRED)zadd_subdirectory(r   z/)z
add_executable(main main.cpp)z0target_compile_definitions(main PRIVATE USE_HIP)z1target_compile_definitions(main PRIVATE USE_CUDA) z)target_link_libraries(main PRIVATE torch )z-target_link_libraries(main PRIVATE hip::host)z:target_link_libraries(main PRIVATE cuda ${CUDA_LIBRARIES}))	r   r   torch._inductor.configr#   use_libtorchr   r   joinr   )r   r   r   r   r   r#   r   
model_libss           r    _get_make_filer*      s    
	BMM	
 4  
34LL56LL67JJL!

(6HTVWX " LL23LLKLLLLM+&JLL<ZLJKLLHI ;;= LLUV;;=    )loggingtorch._inductor.utilsr   __all__	getLogger__name__loggerstrlistbooldictintr!   r*    r+   r    <module>r8      s     0 			8	$sscs s S#X-	s
 s 	sl..$(I.59.CG..r+   