o
    Ih                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
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mZ d dlmZ d dlmZ d dlmZmZ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$ d d
l%m&Z& d dl'm(Z( e ) rd dl*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2 n(dededdfddZ/dededdfddZ0dededdfddZ1de3fddZ2dZ4ej56e7Z8ej59ej59e8Z:ej5;e:dZ<ej=dZ>ej=dZ?ejdkZ@e@rdnd ZAeBeCZDeEd!d"eFdeFfd#d$ZGdeFfd%d&ZHeEdd'eFddfd(d)ZIdeFfd*d+ZJeEdd,eFde3fd-d.ZKeEdd,eFde3fd/d0ZLeEdd,eFde3fd1d2ZMeEdd,eFde3fd3d4ZNeEdd,eFde3fd5d6ZOeEdde3fd7d8ZPeEdde3fd9d:ZQeEdde3fd;d<ZReEdde3fd=d>ZSeEdde3fd?d@ZTd'eFdeFfdAdBZUdCeVeF dDeVeF ddfdEdFZWdGeVeF deVeF fdHdIZXdJeFddfdKdLZYdJeFddfdMdNZZdOeFdPeFddfdQdRZ[dOeFdPeFddfdSdTZ\dUeFdeFfdVdWZ]G dXdY dYZ^dd[e3deVeF fd\d]Z_dd_eFdeVeF fd`daZ`d,eFdeVeF fdbdcZadeVeF fdddeZb	fdd,eFdge3deVeF fdhdiZcdje3deVeF fdkdlZd	Z	 	fdd,eFdje3d[e3dmeeF dge3deeeVeF eVeF eVeF eVeF eVeF eVeF eVeF f fdndoZfG dpdq dqe^ZgdeVeF fdrdsZhdeVeF fdtduZideVeF fdvdwZjdeVeF fdxdyZkd,eFdze3d{e3deeeVeF eVeF eVeF f fd|d}Zld~e$deeeVeF eVeF f fddZmde3dze3deeeVeF eVeF eVeF f fddZndeVeF fddZodeeeVeF eVeF f fddZpeEdde3fddZqeEddeee3eFf fddZreEdd,eFdeFddfddZseEdd,eFddfddZtd,eFdeeeVeF eVeF eVeF eVeF eVeF eVeF f fddZude3deVeF fddZvd,eFd~e$de3dze3dje3d{e3de3deeeVeF eVeF eVeF eVeF eVeF eVeF eVeF f fddZwG dd degZxdddZyeEddeFdee fddZzdeVeF ddfddZ{	f	fddeFdze3dje3deeeVeF eVeF eVeF eVeF eVeF eVeF eVeF f fddZ|G dd dexZ}deFdeeeFeFf fddZ~G dd dZdS )    N)Sequence)cdll)find_library)Path)AnyOptionalUnion)dynamo_timed)configexc)invalid_vec_isaVecISA)	cache_dir)TorchVersion)build_paths)_run_build_command)log_global_cache_errorslog_global_cache_statslog_global_cache_valsuse_global_cacheargskwargsreturnc                  O      d S N r   r   r   r   O/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_inductor/cpp_builder.pyr   .      r   c                  O   r   r   r   r   r   r   r   r   1   r   r   c                  O   r   r   r   r   r   r   r   r   4   r   r   c                   C   s   dS )NFr   r   r   r   r   r   7   r   r   CxxBuildz_inductor/script.ldlinuxdarwinwin32)utf-8r      searchc                 C   s   ddl m}m} | D ]V}zG|d u rHtjdkrW q
tdsW q
ddlm} | }|tj	
|d|d}| t }W d    n1 sCw   Y  t|dg |W   S  tjttfy`   Y q
w tj)	Nr   )get_lock_dirLOCK_TIMEOUTr    TORCH_INDUCTOR_INSTALL_GXX)FileLockzg++.lock)timeout	--version)torch._inductor.codecacher&   r'   sysplatformosgetenvtorch.utils._filelockr)   pathjoininstall_gcc_via_conda
subprocesscheck_outputSubprocessErrorFileNotFoundErrorImportErrorr   InvalidCxxCompiler)r%   r&   r'   cxxr)   lock_dirlockr   r   r   cpp_compiler_searchL   s,   


r>   c               	   C   s   t jt d} t j| dd}t j|sBtd t jdd}|du r+t	
d}|durBtj|dd	|  d
ddddgtjd |S )z>On older systems, this is a quick way to get a modern compilergccbinzg++zDownloading GCC via conda	CONDA_EXEcondaNcreatez	--prefix=z--channel=conda-forgez--quietz-yz
python=3.8gxx)stdout)r/   r2   r3   r   existsloginfoenvirongetshutilwhichr5   
check_callPIPE)prefixcxx_pathrB   r   r   r   r4   i   s*   


r4   compilerc              
   C   sZ   zt j| dgt jd W dS  ty" } z	td|  d|d}~w t jy,   Y dS w )zU
    Check if compiler is ready, in case end user not activate MSVC environment.
    /helpstderrz
Compiler: z is not found.N)r5   r6   STDOUTr8   RuntimeErrorr7   )rQ   r   r   r   r   check_compiler_exist_windows   s   rW   c                  C   sb   t rtjdd} t|  | S t rtjS t	tj
jttfr&ttj
j}ntj
jf}t|} | S )NCXXcl)_IS_WINDOWSr/   rI   rJ   rW   r
   	is_fbcoder   cc
isinstancecppr;   listtupler>   )rQ   r%   r   r   r   get_cpp_compiler   s   	
ra   cpp_compilerc                 C   s$   t | dgd}d| d v S )Nr+   utf8Appler   )r5   r6   decode
splitlines)rb   version_stringr   r   r   _is_apple_clang   s   rh   c                 C   sJ   t jdkr	t| S trtd| rtdttd| S ttd| S )Nr!   z((clang$)|(clang\+\+$))z]Please use clang-cl, due to torch.compile only support MSVC-like CLI (compiler flags syntax).z
(clang-cl)z(clang|clang\+\+))r-   r.   rh   rZ   rer%   rV   boolrb   r   r   r   	_is_clang   s   
rl   c                 C   s   t | rdS ttd| S )NFz(gcc|g\+\+))rl   rj   ri   r%   rk   r   r   r   _is_gcc   s   rm   c                 C   sN   t sdS ztj| dgtjd jt }d| d v W S  ty&   Y dS w )NFrR   rS   	Microsoftr   )	rZ   r5   r6   rU   stripre   SUBPROCESS_DECODE_ARGSrf   r8   )rb   
output_msgr   r   r   _is_msvc_cl   s   rr   c                 C   s   dt dd fdd}z=tj| dgtjd jt }d| d v }|rDtr/t	
d	| r/td
t	
d|}|d urD|d}|t | |W S  tyP   Y dS  tjyZ   Y dS w )Ncompiler_versionr   c                 S   s,   t rdnd}| t|k rtd| ddS )zw
        On Windows: early version icx has `-print-file-name` issue, and can't preload correctly for inductor.
        z2024.2.1z0.0.0z0Intel Compiler error: less than minimal version .N)rZ   r   rV   )rs   min_versionr   r   r   _check_minimal_version   s   
z2_is_intel_compiler.<locals>._check_minimal_versionr+   rS   Intelr   z((icx$)|(icx-cc$))z[Please use icx-cl, due to torch.compile only support MSVC-like CLI (compiler flags syntax).z(\d+[.]\d+[.]\d+[.]\d+)r$   F)r   r5   r6   DEVNULLro   re   rp   rf   rZ   ri   r%   rV   groupr8   r7   )rb   rv   rq   is_intel_compilericx_ver_searchicx_verr   r   r   _is_intel_compiler   s4   


r}   c                   C   
   t t S r   )rm   ra   r   r   r   r   is_gcc      
r   c                   C   r~   r   )rl   ra   r   r   r   r   is_clang   r   r   c                   C   r~   r   )r}   ra   r   r   r   r   rz     r   rz   c                   C   r~   r   )rh   ra   r   r   r   r   is_apple_clang
  r   r   c                   C   r~   r   )rr   ra   r   r   r   r   
is_msvc_cl  r   r   c                 C   s   t j }d|d< ztj| dgtj|djt }W n% ty>   ztj| dgtj|djt }W n ty;   Y Y dS w Y nw |	dd}|	d	d}|S )
NCLC_ALLz-v)rT   envr+    _
)
r/   rI   copyr5   r6   rU   re   rp   	Exceptionreplace)rQ   r   rg   r   r   r   get_compiler_version_info  s.   
r   	dest_listsrc_listc                 C   s   |  dd |D  d S )Nc                 s       | ]}t |V  qd S r   r   deepcopy).0itemr   r   r   	<genexpr>*      z_append_list.<locals>.<genexpr>)extend)r   r   r   r   r   _append_list)  s   r   	orig_listc                 C   s$   g }| D ]}||vr| | q|S r   append)r   new_listr   r   r   r   _remove_duplication_in_list-  s   
r   path_dirc              
   C   sj   t j| s3zt| jddd W d S  ty2 } z|jtjkr'td|  W Y d }~d S d }~ww d S )NT)parentsexist_okzFail to create path )	r/   r2   rF   r   mkdirOSErrorerrnoEEXISTrV   )r   r   r   r   r   _create_if_dir_not_exist5  s   r   c                 C   s|   t j| r<t j| ddD ]'\}}}|D ]}t j||}t | q|D ]}t j||}t | q%qt |  d S d S )NF)topdown)r/   r2   rF   walkr3   removermdir)r   rootdirsfilesname	file_pathdir_pathr   r   r   _remove_dir@  s   r   cmd_linecwdc              
   C   s   t | }ztj||tjd W d S  tjy@ } z"|jd}d|v p'd|v }|r5tj	dkr5d}||7 }t
|||d }~ww )N)r   r   rT   r#   z'omp.h' file not foundlibompr!   a  

OpenMP support not found. Please try one of the following solutions:
(1) Set the `CXX` environment variable to a compiler other than Apple clang++/g++ that has builtin OpenMP support;
(2) install OpenMP via conda: `conda install llvm-openmp`;
(3) install libomp via brew: `brew install libomp`;
(4) manually setup OpenMP and set the `OMP_PREFIX` environment variable to point to a path with `include/omp.h` under it.)shlexsplitr5   r6   rU   CalledProcessErroroutputre   r-   r.   r   CppCompileError)r   r   cmder   openmp_probleminstructionr   r   r   _run_compile_cmdL  s   
	r   c                 C   s8   t d t| | W d    d S 1 sw   Y  d S )Ncompile_file)r	   r   )r   r   r   r   r   run_compile_cmda  s   
"r   	orig_pathc                 C   s   t r	| tjdS | S )N/)rZ   r   r/   sep)r   r   r   r   normalize_path_separatorf  s   r   c                   @   s  e Zd ZdZ											d2dedeee  deee  deee  d	eee  d
eee  deee  deee  dedededdfddZd3ddZ	d3ddZ
d3ddZdefddZdee fddZdee fddZdee fdd Zdee fd!d"Zdee fd#d$Zdee fd%d&Zdee fd'd(Zdefd)d*Zdefd+d,Zdefd-d.Zd/eddfd0d1ZdS )4BuildOptionsBasez
    This is the Base class for store cxx build options, as a template.
    Acturally, to build a cxx shared library. We just need to select a compiler
    and maintains the suitable args.
    r   NFrQ   definitionsinclude_dirscflagsldflagslibraries_dirs	librariespassthrough_argsaot_modeuse_relative_pathcompile_onlyr   c                 C   sb   || _ |pg | _|pg | _|pg | _|pg | _|pg | _|pg | _|p$g | _|	| _|
| _	|| _
d S r   )	_compiler_definitions_include_dirs_cflags_ldflags_libraries_dirs
_libraries_passthrough_args	_aot_mode_use_relative_path_compile_only)selfrQ   r   r   r   r   r   r   r   r   r   r   r   r   r   __init__s  s   







zBuildOptionsBase.__init__c                 C   s   | j rg | _g | _d S d S r   )r   r   r   r   r   r   r   _process_compile_only_options  s   
z.BuildOptionsBase._process_compile_only_optionsc                 C   sX   t | j| _t | j| _t | j| _t | j| _t | j| _t | j| _t | j| _d S r   )r   r   r   r   r   r   r   r   r   r   r   r   _remove_duplicate_options  s   z*BuildOptionsBase._remove_duplicate_optionsc                 C   s   |    |   d S r   )r   r   r   r   r   r   _finalize_options  s   z"BuildOptionsBase._finalize_optionsc                 C      | j S r   )r   r   r   r   r   get_compiler     zBuildOptionsBase.get_compilerc                 C   r   r   )r   r   r   r   r   get_definitions  r   z BuildOptionsBase.get_definitionsc                 C   r   r   )r   r   r   r   r   get_include_dirs  r   z!BuildOptionsBase.get_include_dirsc                 C   r   r   )r   r   r   r   r   
get_cflags  r   zBuildOptionsBase.get_cflagsc                 C   r   r   )r   r   r   r   r   get_ldflags  r   zBuildOptionsBase.get_ldflagsc                 C   r   r   )r   r   r   r   r   get_libraries_dirs  r   z#BuildOptionsBase.get_libraries_dirsc                 C   r   r   )r   r   r   r   r   get_libraries  r   zBuildOptionsBase.get_librariesc                 C   r   r   )r   r   r   r   r   get_passthrough_args  r   z%BuildOptionsBase.get_passthrough_argsc                 C   r   r   )r   r   r   r   r   get_aot_mode  r   zBuildOptionsBase.get_aot_modec                 C   r   r   )r   r   r   r   r   get_use_relative_path  r   z&BuildOptionsBase.get_use_relative_pathc                 C   r   r   )r   r   r   r   r   get_compile_only  r   z!BuildOptionsBase.get_compile_onlyfilec                 C   s   |   |  |  |  |  |  |  |  |  | 	 | 
 d}t|d}t|| W d    d S 1 s;w   Y  d S )N)rQ   r   r   r   r   r   r   r   r   r   r   w)r   r   r   r   r   r   r   r   r   r   r   openjsondump)r   r   attrsfr   r   r   save_flags_to_json  s   "z#BuildOptionsBase.save_flags_to_json)r   NNNNNNNFFFr   N)__name__
__module____qualname____doc__strr   r_   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   l  sj    






	




	r   Twarning_allc                 C   s   t s	| rdgS g S g S )NWallrZ   )r   r   r   r   _get_warning_all_cflag  s   r   c++17std_numc                 C   s"   t r	 d} d|  gS d|  gS )Nzc++20zstd:zstd=r   )r  r   r   r   _get_cpp_std_cflag  s
   r  c                 C   s>   t rg d}|S ddg}t| rtjjrdnd}|| |S )N)wd4819wd4251wd4244wd4267wd4275wd4018wd4190wd4624wd4067wd4068EHsczWno-unused-variablezWno-unknown-pragmasz$Werror=ignored-optimization-argumentz!Wno-ignored-optimization-argument)rZ   rl   r
   aot_inductor#raise_error_on_ignored_optimizationr   )rb   r   ignored_optimization_argumentr   r   r   _get_os_related_cpp_cflags  s   
r  c                  C   s   g d} t  r| d | S )N)zfno-trapping-mathzfunsafe-math-optimizationszffinite-math-onlyzfno-signed-zeroszfno-math-errnozfexcess-precision=fast)r   r   )flagsr   r   r   _get_ffast_math_flags  s   
r  Fmin_optimizec                 C   s   t r
|rdgS dgS tjjrddgn|rdnddg}|t 7 }|d tjjs-|d |d	tjj  t	j
d
kr[t| rE|d t s[t
 dkrV|d |S |d |S )NO1O2O0gO3DNDEBUGzfno-finite-math-onlyzfno-unsafe-math-optimizationszffp-contract=r!   zfno-tree-loop-vectorizeppc64lezmcpu=nativezmarch=native)rZ   r
   r  debug_compiler  r   r^   enable_unsafe_math_opt_flag#enable_floating_point_contract_flagr-   r.   rm   r[   machine)rb   r  r   r   r   r   _get_optimization_cflags  s(   







r   r   c                 C   sB   t r		 ddg}|S | rdgS t dkrdt v rg dS ddgS )NDLLMDfPICDarwinclang)sharedr#  zundefined dynamic_lookupr&  )rZ   r.   systemra   )r   SHARED_FLAGr   r   r   _get_shared_cflag;  s   
r)  extra_flagsc                 C   sf   g }g }g }g }g }	g }
g }t |t| | t| t  t|  }|d| |||||	|
|fS )N )r)  r   r   r  r  r   r3   )rb   r   r   r*  r  r   r   r   r   r   r   r   r   r   r   get_cpp_optionsN  s4   r,  c                       sP   e Zd ZdZ						ddededee d	ed
ededdf fddZ  ZS )
CppOptionsaH  
    This class is inherited from BuildOptionsBase, and as cxx build options.
    This option need contains basic cxx build option, which contains:
    1. OS related args.
    2. Toolchains related args.
    3. Cxx standard related args.
    Note:
    1. This Options is good for assist modules build, such as x86_isa_help.
    FTr   r   r   r   r*  r   rQ   r  r   Nc                    s   t    |r	|nt | _|| _|| _t| j||||d\}}}	}
}}}t| j| t| j	| t| j
|	 t| j|
 t| j| t| j| t| j| |   d S )N)rb   r   r*  r   r  )superr   ra   r   r   r   r,  r   r   r   r   r   r   r   r   r   )r   r   r   r*  r   rQ   r  r   r   r   r   r   r   r   	__class__r   r   r   }  s6   
	
	zCppOptions.__init__)FTr   Fr   F)	r   r   r   r   rj   r   r   r   __classcell__r   r   r/  r   r-  r  s.    r-  c                   C   s   t sdtttjj gS g S )Nz-D_GLIBCXX_USE_CXX11_ABI=)rZ   r   inttorch_C_GLIBCXX_USE_CXX11_ABIr   r   r   r   _get_glibcxx_abi_build_flags  s   r6  c                   C   s   ddgS )NTORCH_INDUCTOR_CPP_WRAPPERSTANDALONE_TORCH_HEADERr   r   r   r   r   !_get_torch_cpp_wrapper_definition  s   r9  c                   C   s   dgS )Nz" C10_USING_CUSTOM_GENERATED_MACROSr   r   r   r   r   _use_custom_generated_macros  r   r:  c                  C   s    t st rg d} | S g S g S )N)C10_USE_GLOGC10_USE_MINIMAL_GLOG$C10_DISABLE_TENSORIMPL_EXTENSIBILITY)rZ   r
   r[   )fb_internal_macrosr   r   r   _use_fb_internal_macros  s   r?  r   r   c                 C   s  g }g }g }t r|||fS t r|d |tj |tj |tj |tj |tj	 |tj
 |tj |tj |tj |d |rX|sXt}ntjt}t| r|d |d |d|  |dtj  |dtj  |||fS )Nnostdincincludez --rtlib=compiler-rtz -fuse-ld=lldz -Wl,--script=z -Bz -L)rZ   r
   r[   r   r   sleef_includeopenmp_includepython_include
cc_includelibgcc_includelibgcc_arch_includelibgcc_backward_includeglibc_includelinux_kernel_include_LINKER_SCRIPTr/   r2   basenamerl   	glibc_lib)rb   r   r   r   r   r   linker_scriptr   r   r   _setup_standard_sys_libs  s6   





rO  vec_isac                 C   sl   g }g }| t kr2|dd |  D  |  g}t r2t|  }d| d| d| dg}||fS )Nc                 s   r   r   r   )r   xr   r   r   r     r   z0_get_build_args_of_chosen_isa.<locals>.<genexpr>zCPU_CAPABILITY=CPU_CAPABILITY_HAVE__CPU_DEFINITION)r   r   build_macrobuild_arch_flagsr
   r[   r   upper)rP  macrosbuild_flagscapr   r   r   _get_build_args_of_chosen_isa  s   

r[  include_pytorchc                 C   s`   ddl m}m} | }|g}g }tjdkr$t s$ddg}|s$|d tr+|d |||fS )Nr   )include_pathsTORCH_LIB_PATHr!   r3  	torch_cputorch_pythonsleef)	torch.utils.cpp_extensionr]  r^  r-   r.   r
   r[   r   rZ   )r\  r   r]  r^  r   r   r   r   r   r   _get_torch_related_args  s   


rc  c                  C   sj   t td} |  s t dkr t td}|jjd  } | d  s0t	dt
|   t
| gS )NrA  r$  stdlibHeaderszPython.hzCan't find Python.h in )r   	sysconfigget_pathrF   r.   r'  parentabsolutewarningswarnr   )include_dirstd_libr   r   r   _get_python_include_dirs  s   
rn  c                  C   s~   t  } tjdtr
dndd}|d ur| | tr+tttjdddjd  g}nt	dg}t
 r;| tj | |fS )NrA  ntposix_prefix)schemelibsLIBDIR)rn  rf  rg  rZ   r   r   r   rh  ri  get_config_varr
   r[   r   rD  )python_include_dirspython_include_pathpython_lib_pathr   r   r   _get_python_related_args!  s    
rx  c               	   C   sH   zd} t |  d}tt|dkW S  t jtfy#   Y dS w )Nzconda list llvm-openmp --jsonrc   r   F)	r5   r6   r   re   lenr   loadsr7   r8   )commandr   r   r   r   is_conda_llvm_openmp_installed:  s   r|  c                  C   sT   zt ddg t g dd } tj| }|| fW S  t jy)   Y dS w )NrL   brew)r}  z--prefixr   rc   )Fr   )r5   r6   re   ro   r/   r2   rF   r7   )libomp_pathomp_availabler   r   r   homebrew_libompD  s   
r  omp_namec                 C   sj   z)t | dgd}tj| |}tj|r'dtjd< t	
| W d S W d S  t jy4   Y d S w )Nz-print-file-name=binrc   TRUEKMP_DUPLICATE_LIB_OK)r5   r6   re   r/   r2   r3   rstripisfilerI   r   LoadLibraryr7   )rb   r  r   omp_pathr   r   r   perload_clang_libomp_winX  s   
r  c                 C   s8   dt dt dtfdd}	 g d}|D ]}|| | qd S )Nrb   lib_namer   c                 S   sl   z*t j| d| gt jdjt }| }tj|r(dtj	d< t
| W dS W dS  t jy5   Y dS w )Nz-print-file-name=rS   r  r  TF)r5   r6   rx   re   rp   r  r/   r2   r  rI   r   r  r7   )rb   r  r   r  r   r   r   _load_icx_built_in_lib_by_nameh  s$   

z>perload_icx_libomp_win.<locals>._load_icx_built_in_lib_by_name)zlibiomp5md.dllzsvml_dispmd.dllz
libmmd.dll)r   rj   )rb   r  preload_listr  r   r   r   perload_icx_libomp_winf  s   r  c                 C   sl  g }g }g }g }g }g }t r|d |d t|  }td}|d urTtj|dd}	tj|	}
|
rK|tj|d |tj|d nt	d |pS|
}|s[|d td	}|s|d urt
 }|rtj|d}|tj|d || t jd
krtjtj|dr|d |st \}}|r|tj|d |tj|d nxtr	 t| r|d |d t| d nat| r|d |d t|  nN|d |d nCt r	|tj tj}d| }|| |d n%t| r|d |d nt| r$|d n
|d |d ||||||fS )NXclangfopenmp
OMP_PREFIXrA  zomp.hlibz-environment variable `OMP_PREFIX` is invalid.ompCONDA_PREFIXx86_64zlibiomp5.dylibiomp5openmpr   z
libomp.dllQiopenmp
libiomp5mdzopenmp:experimentalz-Wp,-fopenmp gompfiopenmp)	_IS_MACOSr   rh   r/   r0   r2   r3   rF   rj  rk  r|  unamer  r  rZ   rl   r  r}   r  r
   r[   r   rC  openmp_lib_so)rb   r   r   include_dir_pathslib_dir_pathsrr  r   r  
omp_prefixheader_path	valid_envconda_prefixconda_lib_pathr~  
openmp_libfb_openmp_extra_flagsr   r   r   _get_openmp_args  s   























r  use_mmap_weightsc                 C   s   g }| r	| d |S )Nz USE_MMAP_SELFr   )r  rX  r   r   r   get_mmap_self_macro  s   
r  c           #      C   s   g }g }g }	g }
g }g }g }t  }t }t| ||\}}}t|\}}t||d\}}}t \}}t| \}}}}}}t } t }!t	|}"|| | |! |" }|| | | }|| }	|}
|| | }|| }|| |  | }|||	|
|||fS )N)r\  r   )
r9  r:  rO  r[  rc  rx  r  r6  r?  r  )#rb   rP  r\  r   r   r   r  r   r   r   r   r   r   r   torch_cpp_wrapper_definitions'use_custom_generated_macros_definitionssys_libs_cflagssys_libs_include_dirssys_libs_passthrough_args
isa_macrosisa_ps_args_build_flagstorch_include_dirstorch_libraries_dirstorch_librariesru  python_libraries_dirs
omp_cflagsomp_ldflagsomp_include_dir_pathsomp_lib_dir_pathsomp_libomp_passthrough_argscxx_abi_passthrough_argsfb_macro_passthrough_argsmmap_self_macrosr   r   r   get_cpp_torch_options  s   	


	r  c                       sn   e Zd ZdZeddddddddddfdededed	ed
ededededee dededdf fddZ	  Z
S )CppTorchOptionsaf  
    This class is inherited from CppTorchOptions, which automatic contains
    base cxx build options. And then it will maintains torch related build
    args.
    1. Torch include_directories, libraries, libraries_directories.
    2. Python include_directories, libraries, libraries_directories.
    3. OpenMP related.
    4. Torch MACROs.
    5. MISC
    FTr   r   rP  r\  r   r   r   r   r  r&  r*  rQ   r  r   Nc              	      s   t  j|||	||
|d || _t| j||||||d\}}}}}}}t| j| t| j| t| j| t| j	| t| j
| t| j| t| j| |   d S )N)r   r   r*  r   rQ   r  )rb   rP  r\  r   r   r   r  )r.  r   r   r  r   r   r   r   r   r   r   r   r   r   )r   rP  r\  r   r   r   r   r  r&  r*  rQ   r  torch_definitionsr  torch_cflagstorch_ldflagsr  r  torch_passthrough_argsr/  r   r   r   X  sD   	
zCppTorchOptions.__init__)r   r   r   r   r   r   rj   r   r   r   r1  r   r   r/  r   r  L  sL    	
r  c                   C   sH   t  rtjjd u rdtjvr dtjvr"tjtjd< d S d S d S d S d S )N	CUDA_HOME	CUDA_PATH)	r
   r[   r3  versionhipr/   rI   r   sdk_homer   r   r   r   _set_gpu_runtime_env  s   

r     r2   c                 C   s<   t t| d}|r|d  jS d|  }t| d S )Nzlibcudart_static.ar   z%"libcudart_static.a" not found under )r_   r   rglobresolverh  rG   rH   )r2   lib_dirslog_msgr   r   r   _find_libcudart_static  s   

r  lpathsc                 C   sl   t | D ]/\}}dtjv r3|tjd r3t|}|d u rqt|| |< |d }| r3| t| qd S )Nr  stubs)	enumerater/   rI   
startswithr  r   rF   r   )r  ir2   lib_dirstub_dirr   r   r   _transform_cuda_paths  s   r  device_typec                 C   s  g }g }g }g }g }g }g }	t  r"dtjvr"dtjvr"tjtjd< t  ddlm}
 |
	| }|

| }| dkrt|tjjrAdnd tjjd ur`t  rT|dg7 }n|d	d
g7 }|d nt  rj|dg7 }n|g d7 }t| | dkr|d |dg7 }|g d7 }tdstd|rt  rddlm} tj|  g}||7 }t  r|tj |r| dkrtjjd u r|sdg}	|||||||	fS )Nr  r  r   )cpp_extensioncudaz	 USE_ROCMz	 USE_CUDAamdhip64c10_hip	torch_hipz __HIP_PLATFORM_AMD__)c10_cudar  
torch_cudaxpuz USE_XPUzWno-comment)c10_xpusycl	ze_loader	torch_xpur  zIntel GPU driver is not properly installed, please follow the instruction in https://github.com/pytorch/pytorch?tab=readme-ov-file#intel-gpu-support.cpp_prefix_pathz*-Wl,-Bstatic -lcudart_static -Wl,-Bdynamic)r
   r[   r/   rI   r   r  r  torch.utilsr  r]  library_pathsr   r3  r  r  r  r   r   r,   r  r2   dirnamesdk_include)r  r   r   r   r   r   r   r   r   r   r  r  cpp_prefix_include_dirr   r   r   get_cpp_torch_device_options  sl   





r  c                       sv   e Zd ZdZedddddddddf
dededed	ed
ededededee deddf fddZ	d fddZ
  ZS )CppTorchDeviceOptionsz
    This class is inherited from CppTorchOptions, which automatic contains
    base cxx build options and torch common build options. And then it will
    maintains cuda/xpu device related build args.
    Fr  Tr   rP  r\  r  r   r   r   r  r&  r*  r  r   Nc              
      s   t  j|||||||	|
d g }g }g }g }g }g }g }t|||d\}}}}}}}t| j| t| j| t| j| t| j| t| j| t| j	| t| j
| |   d S )N)rP  r\  r   r   r   r  r*  r  )r  r   r   )r.  r   r  r   r   r   r   r   r   r   r   r   )r   rP  r\  r  r   r   r   r  r&  r*  r  device_definitionsdevice_include_dirsdevice_cflagsdevice_ldflagsdevice_libraries_dirsdevice_librariesdevice_passthrough_argsr/  r   r   r     sH   
zCppTorchDeviceOptions.__init__c                    sp   t    t r4t \}}t|dksJ d| |d | jv r6| j|d  | j|d  d S d S d S )Nr$   zPython lib dirs: r   )	r.  r   r
   r[   rx  ry  r   r   r   )r   r   python_lib_dirsr/  r   r   r   9  s   

z'CppTorchDeviceOptions._finalize_optionsr   )r   r   r   r   r   r   rj   r   r   r   r   r1  r   r   r/  r   r    sH    	
4r  r   c                 C   s0   t j| }t j|\}}t j| }||fS )a  
    This function help prepare parameters to new cpp_builder.
    Example:
        input_code: /tmp/tmpof1n5g7t/5c/c5crkkcdvhdxpktrmjxbqkqyq5hmxpqsfza4pxcf3mwk42lphygc.cpp
        name, dir = get_name_and_dir_from_output_file_path(input_code)
    Run result:
        name = c5crkkcdvhdxpktrmjxbqkqyq5hmxpqsfza4pxcf3mwk42lphygc
        dir = /tmp/tmpof1n5g7t/5c/

    put 'name' and 'dir' to CppBuilder's 'name' and 'output_dir'.
    CppBuilder --> get_target_file_path will format output path accoding OS:
    Linux: /tmp/tmppu87g3mm/zh/czhwiz4z7ca7ep3qkxenxerfjxy42kehw6h5cjk6ven4qu4hql4i.so
    Windows: [Windows temp path]/tmppu87g3mm/zh/czhwiz4z7ca7ep3qkxenxerfjxy42kehw6h5cjk6ven4qu4hql4i.dll
    )r/   r2   rL  splitextr  )r   name_and_extr   _extdirr   r   r   &get_name_and_dir_from_output_file_pathE  s   r  c                   @   s   e Zd ZdZdefddZdefddZ	dded	eeee f d
e	deddf
ddZ
defddZdefddZ		d ddZd ddZdeddfddZdededdfddZdeddfddZdS )!
CppBuildera  
    CppBuilder is a cpp jit builder, and it supports both Windows, Linux and MacOS.
    Args:
        name:
            1. Build target name, the final target file will append extension type automatically.
            2. Due to the CppBuilder is supports mutliple OS, it will maintains ext for OS difference.
        sources:
            Source code file list to be built.
        BuildOption:
            Build options to the builder.
        output_dir:
            1. The output_dir the taget file will output to.
            2. The default value is empty string, and then the use current dir as output dir.
            3. Final target file: output_dir/name.ext
    r   c                 C      t rd}|S d}|S )Nz.pyd.sor   )r   SHARED_LIB_EXTr   r   r   __get_python_module_extn     z"CppBuilder.__get_python_module_extc                 C   r  )Nz.obj.or   )r   EXTr   r   r   __get_object_extr  r  zCppBuilder.__get_object_extr   r   sourcesBuildOption
output_dirNc                 C   s  d| _ d| _d| _d| _d| _d| _d| _d| _g | _d| _	d| _
d| _d| _|| _|| _| | _ | | _| | _|| _	| | _| jrK|  n|  }tj| j	| j | | _
t|tre|g}t r| jrr| jsr|}n
dd |D }|| _d|| _nd|| _| D ]}tr|  jd| d7  _q|  jd| d7  _q|  D ]}tr|  jd| d7  _q|  jd	| d7  _q|! D ]}	tr|  jd
|	 d7  _q|  jd|	 d7  _q|" D ]}
tr|  jd|
 d7  _q|  jd|
 d7  _q|# D ]}tr|  jd| d7  _q|  jd| d7  _q|$ D ]}trB|  jd| d7  _q0|  jd| d7  _q0|% D ]}|  j| d7  _qSd S )Nr   Fc                 S   s   g | ]}t j|qS r   )r/   r2   rL  )r   r  r   r   r   
<listcomp>  s    z'CppBuilder.__init__.<locals>.<listcomp>r+  r   -z/D z-D z/I "z" z-Iz
/LIBPATH:"z-L"z.lib" z-l)&r   _cflags_args_definitions_args_include_dirs_args_ldflags_args_libraries_dirs_args_libraries_args_passthrough_parameters_args_orig_source_paths_output_dir_target_filer   r   _name_build_optionr   r   r   r   r   _CppBuilder__get_object_ext"_CppBuilder__get_python_module_extr/   r2   r3   r]   r   r
   r[   _sources_argsr   rZ   r   r   r   r   r   r   )r   r   r  r  r  file_extinp_namecflag
definitioninc_dirldflagr  r  passthrough_argr   r   r   r   v  sz   





zCppBuilder.__init__c                    s   dt dt dt dt dt dt dt dt d	t d
t dt f fdd}| j j j j j j j j j	 j
r:tj jn jd
}|S )NrQ   r  include_dirs_argsdefinitions_argscflags_argsldflags_argslibraries_argslibraries_dirs_argsr   target_filer   c
                    s   t r(|  d| d| d| d| d| d|	 d| d| d| d}
t|
}
|
S  jr-dnd}tddd|  d| d| d| d| d| d| d| d| d| d|	 d }
|
S )	Nr+  z /LD /Fez /link z-cr   z[ \n]+z
                    z -o )rZ   r   r   ri   subro   )rQ   r  r  r   r!  r"  r#  r$  r   r%  r   compile_only_argr   r   r   format_build_command  s\   z9CppBuilder.get_command_line.<locals>.format_build_command)
rQ   r  r  r   r!  r"  r#  r$  r   r%  )r   r   r  r  r
  r	  r  r  r  r  r   r/   r2   rL  r  )r   r(  command_liner   r   r   get_command_line  sL   	
 zCppBuilder.get_command_linec                 C   s
   t | jS r   )r   r  r   r   r   r   get_target_file_path  s   
zCppBuilder.get_target_file_pathc                 C   s  ddl m} td |   }z| }tj|}| j}tj	t
d}t r}t|tj	|| tttj	|d | jD ]}t|tj	|tj| qCtj	|d}	t||	 t||tj|}
tj|rwt| t|
| |drt|d n|drt|d	 W d    n1 sw   Y  W n tjy } z|jd
}t|||d }~ww W d    d S 1 sw   Y  d S )Nr   r  r   rA  z	script.ldr   i  r  i  r#   )r,   r  r	   r*  r   r/   r2   rL  r  r3   _TORCH_PATHtempfileTemporaryDirectoryrK   r   rK  r  copytreer   rF   r   endswithchmodr5   r   r   re   r   r   )r   r  r{  r  header_nameoutput_pathtorch_includes_pathtmp_dirsrcdest_include_pathtmp_output_pathr   r   r   r   r   build_fbcode_re
  sF   


 


"zCppBuilder.build_fbcode_rec                 C   s\   | j r|  S t| j tj| j| j dt }t| | 	 }t
||d t| dS )z
        It is must need a temperary directory to store object files in Windows.
        After build completed, delete the temperary directory to save disk space.
        r   )r   N)r   r9  r   r  r/   r2   r3   r  _BUILD_TEMP_DIRr*  r   r   )r   _build_tmp_dir	build_cmdr   r   r   build2  s   
zCppBuilder.build
cmake_pathc              	   C   sl   d | j }td| d| j d| j d}t|d}|| W d    d S 1 s/w   Y  d S )Nr+  a  
            cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
            project(aoti_model LANGUAGES CXX)
            set(CMAKE_CXX_STANDARD 17)

            # May need to point CMAKE_PREFIX_PATH to the right torch location
            find_package(Torch REQUIRED)

            # Set a shared library target
            add_library(aoti_model SHARED)

            # Add macro definitions
            target_compile_definitions(aoti_model PRIVATE zY)

            # Add compile flags
            target_compile_options(aoti_model PRIVATE z])
            # Backend specific flags
            target_compile_options(aoti_model PRIVATE z -c)

            r   )	r3   r  r   textwrapdedentr	  r  r   write)r   r>  r   contentsr   r   r   r   save_compile_cmd_to_cmakeD  s   "z$CppBuilder.save_compile_cmd_to_cmakesrc_pathc                 C   sP   dt |j }t|d}|d| d W d    d S 1 s!w   Y  d S )Nz${CMAKE_CURRENT_SOURCE_DIR}/az"target_sources(aoti_model PRIVATE z)
)r   r   r   rA  )r   r>  rD  r   r   r   r   save_src_to_cmakeb  s   "zCppBuilder.save_src_to_cmakec                 C   s   d | j }d | j }td| d| d}tj|s*J d| dt	|d}|
| W d    d S 1 s@w   Y  d S )Nr+  zS
            # Add linker flags
            target_link_options(aoti_model PRIVATE zT)

            # Add libraries
            target_link_libraries(aoti_model PRIVATE z)
         z#save_link_cmd_to_cmakefile expects z to already existrE  )r3   r  r   r   r?  r@  r/   r2   rF   r   rA  )r   r>  lflagsrr  rB  r   r   r   r   save_link_cmd_to_cmakeh  s   

"z!CppBuilder.save_link_cmd_to_cmake)r   r   )r   r   r   r   r   r  r  r   r_   r   r   r*  r+  r9  r=  rC  rF  rH  r   r   r   r   r  ]  s:    	
`1

(
r  )T)r   )F)Tr   Fr   )FF)r   r   	functoolsr   loggingr/   r.   ri   r   rK   r5   r-   rf  r-  r?  rj  collections.abcr   ctypesr   ctypes.utilr   pathlibr   typingr   r   r   r3  torch._dynamo.utilsr	   torch._inductorr
   r   torch._inductor.cpu_vec_isar   r   %torch._inductor.runtime.runtime_utilsr   torch.torch_versionr   r[   	triton.fbr   triton.fb.buildr   torch._inductor.fb.utilsr   r   r   r   rj   r:  r2   abspath__file___HEREr  r,  r3   rK  r  	_IS_LINUXr  rZ   rp   	getLoggerr   rG   	lru_cacher   r>   r4   rW   ra   rh   rl   rm   rr   r}   r   r   rz   r   r   r   r_   r   r   r   r   r   r   r   r   r   r  r  r  r   r)  r`   r,  r-  r6  r9  r:  r?  rO  r[  rc  rn  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   <module>   st  

+i
 0
$3
"*
	*
k0
U
A
	0
NG

