
    pi                         d dl Z d dlZd dlmZ g Z e j
                  e      Zdede	e   de
dej                  eeef      def
dZdede	e   de
defd	Zy)
    N)IndentedBufferpackage_namemodel_namescudaexample_inputs_mapreturnc                 
   t               }|j                  g d       |r|j                  ddg       |D ]  }|j                  d|  d| d| d        |j                          |D ]  }|j                  d| d	        |j                  g d
       |j	                         5  |j                  d|rdnd d       |j                  d       |j	                         5  |j                  d       |t        |      D ]C  \  }}||   }|j                  d|        |j                  d|dz    d	       |j                  d| d       |j	                         5  |j                  d| d       |j                  d       |j                  d       |j	                         5  |j                  d       |j                  d       ddd       |j                  d       |j                  d       |j                  d       |j                  d |dz    d!       ddd       |j                  d       |j                          F |j                          |j                  d"       t        t        |            D ]#  }|j                  d#|dz    d$d%|dz    d&g       % |j                  d'       t        t        |            D ]  }|j                  d(|dz    d	        |j                  d)       t        |      D ]R  \  }}|j                  d*|dz    d+d,|dz    d-d.|dz    d/| d0d1|dz    d2d3|dz    d2d4d5|  d| d6d7|dz    d8g       T ||j                  d9       t        t        |            D ]D  }|j                  d:|dz    d;       |j                  d7|dz    d<|dz    d=|dz    d>|dz    d?	       F |j                  d@       t        t        |            D ]#  }|j                  dA|dz    d$dB|dz    dCg       % |j                  dD       t        t        |            D ]>  }|j                  dE|dz    dF|dz    dG       |j                  dH|dz    dI|dz    dJ       @ |j                  dK       ddd       |j                  dLg       |j	                         5  |j                  dM       |j                  d       ddd       |j                  d       ddd       |j                  d       |j                         S # 1 sw Y   OxY w# 1 sw Y   	xY w# 1 sw Y   xY w# 1 sw Y   ixY w# 1 sw Y   \xY w)Na}  
    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 <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   ib
model_namei
num_inputss           b/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/export/experimental/_utils.py_get_main_cpp_filer       s   * 
	BMM	

 
#/	
 " 


&8Aj\QTU	


 JJL! R

B:,aPQR MM	
 
 k
1D&e1LBOP
WYY[ \	&LL:;!-%.{%; !MAz!3J!?JLL#CJ<!PQLL#HQq!QRLL#7
|9!MN 6zlBgh %TU%;<YY[ 6LL ` LL5	6
 S) P +AE72[\%* LL%JJL9!< 

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

 YY[ 	&LLKLLL%	& 	SWkX LL;;=s6 6 \	& \	&F	& 	&Ok ksp   $;UA=T.AT!	$#TAT!	I+T.*U/#T:UTT!	!T+&T..T7	3U:U	?UUc                    t               }|j                  g d       ddlm} |j                  r|j                  d       |r|j                  d       |j                          |D ]  }|j                  d|  d| d        |j                  d	       |r|j                  d
       dj                  |      }|j                  d| d       |r|j                  d       |j                         S )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(CUDA REQUIRED)zadd_subdirectory(r
   z/)z
add_executable(main main.cpp)z1target_compile_definitions(main PRIVATE USE_CUDA) z)target_link_libraries(main PRIVATE torch )z:target_link_libraries(main PRIVATE cuda ${CUDA_LIBRARIES}))	r   r   torch._inductor.configr"   use_libtorchr   r   joinr   )r   r   r   r   r"   r   
model_libss          r   _get_make_filer)      s    		BMM	
 4  
34
23JJL! Y

(6HTVWXY LL23
HI+&JLL<ZLJK
QR;;=    )loggingtypingtorch._inductor.utilsr   __all__	getLogger__name__loggerstrlistboolOptionaldictintr    r)    r*   r   <module>r9      s      0 			8	$jjcj j S#X7	j
 	jZ# #49 #D #S #r*   