o
    Zh                     @   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   J/var/www/auris/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   Z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   ZTORCH_INDUCTOR_INSTALL_GXX)FileLockzg++.lock)timeout	--version)torch._inductor.codecacher%   r&   sysplatformosgetenvZtorch.utils._filelockr'   pathjoininstall_gcc_via_conda
subprocesscheck_outputSubprocessErrorFileNotFoundErrorImportErrorr   ZInvalidCxxCompiler)r$   r%   r&   cxxr'   Zlock_dirlockr   r   r   cpp_compiler_searchL   s,   


r9   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 compilerZgccbinzg++zDownloading GCC via condaZ	CONDA_EXEcondaNcreatez	--prefix=z--channel=conda-forgez--quietz-yz
python=3.8Zgxx)stdout)r-   r/   r0   r   existsloginfoenvirongetshutilwhichr2   
check_callPIPE)prefixZcxx_pathr;   r   r   r   r1   i   s*   


r1   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)r2   r3   STDOUTr5   RuntimeErrorr4   )rH   r   r   r   r   check_compiler_exist_windows   s   rN   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-   rA   rB   rN   r
   	is_fbcoder   cc
isinstancecppr7   listtupler9   )rH   r$   r   r   r   get_cpp_compiler   s   	
rX   cpp_compilerc                 C   s$   t | dgd}d| d v S )Nr)   utf8ZAppler   )r2   r3   decode
splitlines)rY   version_stringr   r   r   _is_apple_clang   s   r^   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,   r^   rQ   rer$   rM   boolrY   r   r   r   	_is_clang   s   
rb   c                 C   s   t | rdS ttd| S )NFz(gcc|g\+\+))rb   r`   r_   r$   ra   r   r   r   _is_gcc   s   rc   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 )NFrI   rJ   	Microsoftr   )	rQ   r2   r3   rL   stripr[   SUBPROCESS_DECODE_ARGSr\   r5   )rY   
output_msgr   r   r   _is_msvc_cl   s   rh   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)rQ   r   rM   )ri   min_versionr   r   r   _check_minimal_version   s   
z2_is_intel_compiler.<locals>._check_minimal_versionr)   rJ   Z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   r2   r3   DEVNULLre   r[   rf   r\   rQ   r_   r$   rM   groupr5   r4   )rY   rl   rg   is_intel_compilerZicx_ver_searchZicx_verr   r   r   _is_intel_compiler   s4   


rp   c                   C   
   t t S r   )rc   rX   r   r   r   r   is_gcc      
rr   c                   C   rq   r   )rb   rX   r   r   r   r   is_clang   rs   rt   c                   C   rq   r   )rp   rX   r   r   r   r   ro     rs   ro   c                   C   rq   r   )r^   rX   r   r   r   r   is_apple_clang
  rs   ru   c                   C   rq   r   )rh   rX   r   r   r   r   
is_msvc_cl  rs   rv   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)rK   envr)    _
)
r-   rA   copyr2   r3   rL   r[   rf   	Exceptionreplace)rH   ry   r]   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   Z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-   r/   r>   r   mkdirOSErrorerrnoEEXISTrM   )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-   r/   r>   walkr0   removermdir)r   rootdirsfilesname	file_pathZ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   rK   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splitr2   r3   rL   CalledProcessErroroutputr[   r+   r,   r   CppCompileError)r   r   cmder   Zopenmp_problemZ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/)rQ   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.
    rz   NFrH   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)selfrH   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)rH   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)rz   NNNNNNNFFFr   N)__name__
__module____qualname____doc__strr   rV   r`   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 )NZWallrQ   )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)Zwd4819Zwd4251Zwd4244Zwd4267Zwd4275Zwd4018Zwd4190Zwd4624Zwd4067Zwd4068ZEHsczWno-unused-variablezWno-unknown-pragmasz$Werror=ignored-optimization-argumentz!Wno-ignored-optimization-argument)rQ   rb   r
   aot_inductorZ#raise_error_on_ignored_optimizationr   )rY   r   Z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)rr   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 )NZO1ZO2ZO0gZO3ZDNDEBUGzfno-finite-math-onlyzfno-unsafe-math-optimizationszffp-contract=r    zfno-tree-loop-vectorizeppc64lezmcpu=nativezmarch=native)rQ   r
   r   Zdebug_compiler   r   rU   Zenable_unsafe_math_opt_flagZ#enable_floating_point_contract_flagr+   r,   rc   rR   machine)rY   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 )NZDLLZMDfPICDarwinZclang)sharedr   zundefined dynamic_lookupr   )rQ   r,   systemrX   )r   Z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   r0   )rY   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   rz   r   r   r   r   rH   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)rY   r   r   r   r   )superr   rX   r   r   r   r  r   r   r   r   r   r   r   r   r   )r   r   r   r   r   rH   r   r   r   r   r   r   r   r   	__class__r   r   r   }  s6   
	
	zCppOptions.__init__)FTr   Frz   F)	r   r   r   r   r`   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=)rQ   r   inttorchZ_CZ_GLIBCXX_USE_CXX11_ABIr   r   r   r   _get_glibcxx_abi_build_flags  s   r	  c                   C   s   ddgS )NZTORCH_INDUCTOR_CPP_WRAPPERZSTANDALONE_TORCH_HEADERr   r   r   r   r   !_get_torch_cpp_wrapper_definition  s   r
  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)ZC10_USE_GLOGZC10_USE_MINIMAL_GLOGZ$C10_DISABLE_TENSORIMPL_EXTENSIBILITY)rQ   r
   rR   )Z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 )NZnostdincincludez --rtlib=compiler-rtz -fuse-ld=lldz -Wl,--script=z -Bz -L)rQ   r
   rR   r   r   Zsleef_includeopenmp_includepython_includeZ
cc_includeZlibgcc_includeZlibgcc_arch_includeZlibgcc_backward_includeZglibc_includeZlinux_kernel_include_LINKER_SCRIPTr-   r/   basenamerb   Z	glibc_lib)rY   r   r   r   r   r   Zlinker_scriptr   r   r   _setup_standard_sys_libs  s6   





r  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=ZCPU_CAPABILITY_ZHAVE_Z_CPU_DEFINITION)r   r   Zbuild_macroZbuild_arch_flagsr
   rR   r   upper)r  macrosZ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    r  Z	torch_cpuZtorch_pythonZsleef)	Ztorch.utils.cpp_extensionr  r  r+   r,   r
   rR   r   rQ   )r  r   r  r  r   r   r   r   r   r   _get_torch_related_args  s   


r  c                  C   sj   t td} |  s t dkr t td}|jjd  } | d  s0t	dt
|   t
| gS )Nr  r   stdlibZHeaderszPython.hzCan't find Python.h in )r   	sysconfigget_pathr>   r,   r   parentabsolutewarningswarnr   )Zinclude_dirZstd_libr   r   r   _get_python_include_dirs  s   
r$  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 )Nr  ntposix_prefix)schemelibsLIBDIR)r$  r  r  rQ   r   r   r   r   r!  get_config_varr
   rR   r   r  )python_include_dirsZpython_include_pathZpython_lib_pathr   r   r   _get_python_related_args!  s    
r,  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 --jsonrZ   r   F)	r2   r3   r   r[   lenr   loadsr4   r5   )commandr   r   r   r   is_conda_llvm_openmp_installed:  s   r0  c                  C   sT   zt ddg t g dd } tj| }|| fW S  t jy)   Y dS w )NrD   brew)r1  z--prefixr   rZ   )Frz   )r2   r3   r[   re   r-   r/   r>   r4   )libomp_pathomp_availabler   r   r   homebrew_libompD  s   
r4  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=binrZ   TRUEKMP_DUPLICATE_LIB_OK)r2   r3   r[   r-   r/   r0   rstripisfilerA   r   LoadLibraryr4   )rY   r5  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 )NrY   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=rJ   r6  r7  TF)r2   r3   rm   r[   rf   r8  r-   r/   r9  rA   r   r:  r4   )rY   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   r`   )rY   r>  Z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 )NZXclangZfopenmpZ
OMP_PREFIXr  zomp.hlibz-environment variable `OMP_PREFIX` is invalid.ZompZCONDA_PREFIXx86_64zlibiomp5.dylibZiomp5Zopenmpr   z
libomp.dllZQiopenmpZ
libiomp5mdzopenmp:experimentalz-Wp,-fopenmp ZgompZfiopenmp)	_IS_MACOSr   r^   r-   r.   r/   r0   r>   r"  r#  r0  unamer   r4  rQ   rb   r<  rp   r?  r
   rR   r   r  Zopenmp_lib_so)rY   r   r   Zinclude_dir_pathsZlib_dir_pathsr(  r   r3  Z
omp_prefixheader_pathZ	valid_envZconda_prefixZconda_lib_pathr2  Z
openmp_libZfb_openmp_extra_flagsr   r   r   _get_openmp_args  s   























rE  use_mmap_weightsc                 C   s   g }| r	| d |S )Nz USE_MMAP_SELFr   )rF  r  r   r   r   get_mmap_self_macro  s   
rG  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   )
r
  r  r  r  r  r,  rE  r	  r  rG  )#rY   r  r  r   r   r   rF  r   r   r   r   r   r   r   Ztorch_cpp_wrapper_definitionsZ'use_custom_generated_macros_definitionsZsys_libs_cflagsZsys_libs_include_dirsZsys_libs_passthrough_argsZ
isa_macrosZisa_ps_args_build_flagstorch_include_dirstorch_libraries_dirstorch_librariesr+  Zpython_libraries_dirsZ
omp_cflagsZomp_ldflagsZomp_include_dir_pathsZomp_lib_dir_pathsZomp_libZomp_passthrough_argsZcxx_abi_passthrough_argsZfb_macro_passthrough_argsZmmap_self_macrosr   r   r   get_cpp_torch_options  s   	


	rK  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   rz   r  r  r   r   r   r   rF  r   r   rH   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   rH   r   )rY   r  r  r   r   r   rF  )r  r   r   rK  r   r   r   r   r   r   r   r   r   r   )r   r  r  r   r   r   r   rF  r   r   rH   r   Ztorch_definitionsrH  Ztorch_cflagsZtorch_ldflagsrI  rJ  Ztorch_passthrough_argsr  r   r   r   X  sD   	
zCppTorchOptions.__init__)r   r   r   r   r   r   r`   r   r   r   r  r   r   r  r   rL  L  sL    	
rL  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
   rR   r  versionhipr-   rA   r   sdk_homer   r   r   r   _set_gpu_runtime_env  s   

rR     r/   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 )rV   r   rglobresolver   r?   r@   )r/   lib_dirsZlog_msgr   r   r   _find_libcudart_static  s   

rW  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 )NrM  Zstubs)	enumerater-   rA   
startswithrW  r   r>   r   )rX  ir/   lib_dirZ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 )NrM  rN  r   )cpp_extensioncudaz	 USE_ROCMz	 USE_CUDAZamdhip64Zc10_hipZ	torch_hipz __HIP_PLATFORM_AMD__)Zc10_cudar`  Z
torch_cudaZxpuz USE_XPUzWno-comment)Zc10_xpuZsycl	ze_loaderZ	torch_xpura  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
   rR   r-   rA   r   rQ  rR  Ztorch.utilsr_  r  Zlibrary_pathsr   r  rO  rP  r]  r   r   r*   rc  r/   dirnameZsdk_include)r^  r   r   r   r   r   r   r   r   r   r_  rc  Zcpp_prefix_include_dirr   r   r   get_cpp_torch_device_options  sl   





re  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   r  r  r^  r   r   r   rF  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)r  r  r   r   r   rF  r   r   )r^  r   r   )r  r   re  r   r   r   r   r   r   r   r   r   )r   r  r  r^  r   r   r   rF  r   r   r   Zdevice_definitionsZdevice_include_dirsZdevice_cflagsZdevice_ldflagsZdevice_libraries_dirsZdevice_librariesZ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
   rR   r,  r-  r   r   r   )r   r|   Zpython_lib_dirsr  r   r   r   9  s   

z'CppTorchDeviceOptions._finalize_optionsr   )r   r   r   r   r   r   r`   r   r   r   r   r  r   r   r  r   rf    sH    	
4rf  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-   r/   r  splitextrd  )r   Zname_and_extr   Z_extdirr   r   r   &get_name_and_dir_from_output_file_pathE  s   ri  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   ZSHARED_LIB_EXTr   r   r   Z__get_python_module_extn     z"CppBuilder.__get_python_module_extc                 C   rk  )Nz.obj.or   )r   ZEXTr   r   r   Z__get_object_extr  rm  zCppBuilder.__get_object_extrz   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 )Nrz   Fc                 S   s   g | ]}t j|qS r   )r-   r/   r  )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-   r/   r0   rT   r   r
   rR   _sources_argsr   rQ   r   r   r   r   r   r   )r   r   ro  rp  rq  Zfile_extZinp_nameZcflagZ
definitioninc_dirZldflagr\  r@  Z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 )NrH   ro  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-crz   z[ \n]+z
                    z -o )rQ   r   r   r_   subre   )rH   ro  r  r  r  r  r  r  r   r  r   Zcompile_only_argr   r   r   format_build_command  s\   z9CppBuilder.get_command_line.<locals>.format_build_command)
rH   ro  r  r  r  r  r  r  r   r  )r   r   r  rw  rv  ru  rx  rz  ry  r{  r   r-   r/   r  r~  )r   r  Z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   rb  r   r  z	script.ldrn  i  rl  i  r"   )r*   rc  r	   r  r   r-   r/   r  r~  r0   _TORCH_PATHtempfileTemporaryDirectoryrC   r~   r  r|  copytreer   r>   r   endswithchmodr2   r   r   r[   r   r   )r   rc  r/  rD  header_nameZoutput_pathZtorch_includes_pathZtmp_dirsrcZdest_include_pathZ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   r  r   r}  r-   r/   r0   r  _BUILD_TEMP_DIRr  r   r   )r   Z_build_tmp_dirZ	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   )	r0   r  r   textwrapdedentru  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   r  )r   r  r  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 existr  )r0   r  r   r   r  r  r-   r/   r>   r   r  )r   r  Zlflagsr(  r  r   r   r   r   save_link_cmd_to_cmakeh  s   

"z!CppBuilder.save_link_cmd_to_cmake)rz   r   )r   r   r   r   r   r  r  r   rV   r   r   r  r  r  r  r  r  r  r   r   r   r   rj  ]  s:    	
`1

(
rj  )T)r   )F)Tr   Fr   )FF)r~   r   	functoolsr   loggingr-   r,   r_   r   rC   r2   r+   r  r  r  r"  collections.abcr   ctypesr   Zctypes.utilr   pathlibr   typingr   r   r   r  Ztorch._dynamo.utilsr	   Ztorch._inductorr
   r   Ztorch._inductor.cpu_vec_isar   r   Z%torch._inductor.runtime.runtime_utilsr   Ztorch.torch_versionr   rR   Z	triton.fbr   Ztriton.fb.buildr   Ztorch._inductor.fb.utilsr   r   r   r   r`   r  r/   abspath__file__Z_HERErd  r  r0   r  rZ  Z	_IS_LINUXrB  rQ   rf   	getLoggerr   r?   	lru_cacher   r9   r1   rN   rX   r^   rb   rc   rh   rp   rr   rt   ro   ru   rv   r   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   r  r  r	  r
  r  r  r  r  r  r$  r,  r0  r4  r<  r?  rE  rG  rK  rL  rR  rW  r]  re  rf  ri  rj  r   r   r   r   <module>   st  

+i
 0
$3
"*
	*
k0
U
A
	0
NG

