
    `i                         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dS )
    N)IndentedBufferpackage_namemodel_namescudaexample_inputs_mapreturnc                    t                      }|                    g d           |r|                    ddg           |D ]!}|                    d|  d| d| d           "|                                 |D ]}|                    d| d	           |                    g d
           |                                5  |                    d|rdnd d           |                    d           |                                5  |                    d           |rt          |          D ]\  }}||         }|                    d|            |                    d|dz    d	           |                    d| d           |                                5  |                    d| d           |                    d           |                    d           |                                5  |                    d           |                    d           ddd           n# 1 swxY w Y   |                    d           |                    d           |                    d           |                    d |dz    d!           ddd           n# 1 swxY w Y   |                    d           |                                 |                                 |                    d"           t          t          |                    D ]'}|                    d#|dz    d$d%|dz    d&g           (|                    d'           t          t          |                    D ]}|                    d(|dz    d	           |                    d)           t          |          D ]V\  }}|                    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           W|O|                    d9           t          t          |                    D ]L}|                    d:|dz    d;           |                    d7|dz    d<|dz    d=|dz    d>|dz    d?	           M|                    d@           t          t          |                    D ]'}|                    dA|dz    d$dB|dz    dCg           (|                    dD           t          t          |                    D ]F}|                    dE|dz    dF|dz    dG           |                    dH|dz    dI|dz    dJ           G|                    dK           ddd           n# 1 swxY w Y   |                    dLg           |                                5  |                    dM           |                    d           ddd           n# 1 swxY w Y   |                    d           ddd           n# 1 swxY w Y   |                    d           |                                S )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           t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/export/experimental/_utils.py_get_main_cpp_filer       s1	   * 
		BMM	
 	
 	

 
 
  

#/	
 	
 	
 " 
 


UUUUUjUUU	
 	
 	
 	
 JJLLL! R R

P:PPPQQQQMM	
 	
 	
   
 k k
OD2K&&eOOOPPP
WYY[[ \	& \	&LL:;;;!-%.{%;%; ! !MAz!3J!?JLL!PJ!P!PQQQLL!QQ!Q!Q!QRRRLL!M
!M!M!MNNN  hzhhh   %TUUU%;<<<YY[[ 6 6LL `   LL555	6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
 S)))   P   \AE\\\  %              * LL%%%JJLLLL

ABBBs;//00  AMM:Q:::tklopkpttt    CDDDs;//00 L LALL!J!a%!J!J!JKKKKLL6777!*;!7!7  :WQUWWWiq1uiii^QU^^PZ^^^?a!e???AAAAA)PPP
PPP:A:::	    "-2333s;//00  ALLYq1uYYY   LLvAvv1q5vvUVYZUZvvdehidivvv    EFFFs;//00  AMM:Q:::ymnqrmryyy    4555s;//00  ALLkAkkTUXYTYkkk   LL\a!e\\QQRU\\\    LL%%%y\	& \	& \	& \	& \	& \	& \	& \	& \	& \	& \	& \	& \	& \	& \	&| 	5	
 	
 	

 YY[[ 	& 	&LLKLLLLL%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	SWk k k k k k k k k k k k k k kX LL;;==s    AXBV AJ	6+H-!J	-H11J	4H15AJ	V J##V &J#'KV 4X V	XV	-X5+W, X,W0	0X3W0	4XXXc                 2   t                      }|                    g d           ddlm} |j        r|                    d           |r|                    d           |                                 |D ]}|                    d|  d| d           |                    d	           |r|                    d
           d                    |          }|                    d| d           |r|                    d           |                                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)      sN   			BMM	
 	
 	
   433333  5
3444 4
2333JJLLL! Y Y

WWWWWWXXXXLL2333 J
HIII+&&JLLJZJJJKKK S
QRRR;;==    )loggingtypingtorch._inductor.utilsr   __all__	getLogger__name__loggerstrlistboolOptionaldictintr    r)    r*   r   <module>r9      s      0 0 0 0 0 0 		8	$	$jjcj j S#X7	j
 	j j j jZ# #49 #D #S # # # # # #r*   