
    qi!                         d dl Z d dlmZ g Z e j                  e      Zdedee   de	de
eef   dz  de	defd	Zdedee   de	de	def
d
Zy)    N)IndentedBufferpackage_namemodel_namescudaexample_inputs_mapis_hipreturnc                 
   t               }|j                  g d       |r(|r|j                  dg       n|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    d0| d1d2|dz    d3d4|dz    d3d5d6|  d| d7d8|dz    d9g       T ||j                  d:       t        t        |            D ]D  }|j                  d;|dz    d<       |j                  d8|dz    d=|dz    d>|dz    d?|dz    d@	       F |j                  dA       t        t        |            D ]#  }|j                  dB|dz    d%dC|dz    dDg       % |j                  dE       t        t        |            D ]>  }|j                  dF|dz    dG|dz    dH       |j                  dI|dz    dJ|dz    dK       @ |j                  dL       ddd       |j                  dMg       |j	                         5  |j                  dN       |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)Oa}  
    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	            f/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/export/experimental/_utils.py_get_main_cpp_filer!   
   s   , 
	BMM	

 MM2 MM'3 " 


&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   9;U4A=U1AT6	9#T)AT6	/I+U*U#U'U)T3.T6	6U ;UU	UU	UU$c                 v   t               }|j                  g d       ddlm} |j                  r|j                  d       |r%|r|j                  d       n|j                  d       |j                          |D ]  }|j                  d|  d| d	        |j                  d
       |r%|r|j                  d       n|j                  d       dj                  |      }|j                  d| d       |r4|r!|j                  d       |j                         S |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(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! Y

(6HTVWXY 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+   