
    ɯwgX                     	   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mZmZ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jL                         rd d
l'm(Z( d dl)m*Z*m+Z+m,Z,m-Z- n$dededdfdZ*dededdfdZ+dededdfdZ,de.fdZ-dZ/ej                  ja                  d      Z1ej                  ja                  d      Z2ej                  dk(  Z3e3rdndZ4 ejj                  e6      Z7 ejp                  d      de9de9fd       Z:de9fdZ; ejp                  d      de9ddfd       Z<de9fdZ= ejp                  d      d e9de.fd!       Z>d e9de.fd"Z?d e9de.fd#Z@ ejp                  d      d e9de.fd$       ZA ejp                  d      d e9de.fd%       ZB ejp                  d      de.fd&       ZC ejp                  d      de.fd'       ZD ejp                  d      de.fd(       ZE ejp                  d      de.fd)       ZF ejp                  d      de.fd*       ZGde9de9fd+ZHd,ee9   d-ee9   ddfd.ZId/ee9   dee9   fd0ZJd1e9ddfd2ZKd1e9ddfd3ZLd4e9d5e9deMfd6ZNd4e9d5e9deMfd7ZOd8e9de9fd9ZP G d: d;      ZQdkd<e.dee9   fd=ZRdld>e9dee9   fd?ZSd e9dee9   fd@ZTdee9   fdAZUdBe.dee9   fdCZV	 	 dmd e9dBe.d<e.dDee9   deee9   ee9   ee9   ee9   ee9   ee9   ee9   f   f
dEZW G dF dGeQ      ZXdee9   fdHZYdee9   fdIZZdee9   fdJZ[dee9   fdKZ\d e9dLe.dMe.deee9   ee9   ee9   f   fdNZ]dOe!deee9   ee9   f   fdPZ^dQe.dLe.deee9   ee9   ee9   f   fdRZ_dee9   fdSZ`deee9   ee9   f   fdTZa ejp                  d      de.fdU       Zb ejp                  d      dee.e9f   fdV       Zc ejp                  d      d e9dWe9ddfdX       Zd ejp                  d      d e9ddfdY       Zed e9deee9   ee9   ee9   ee9   ee9   ee9   f   fdZZfd[e.dee9   fd\Zgd e9dOe!dQe.dLe.dBe.dMe.d[e.deee9   ee9   ee9   ee9   ee9   ee9   ee9   f   fd]Zh G d^ d_eX      Zidnd`Zjdaee9   ddfdbZk	 	 dodce.dLe.dBe.deee9   ee9   ee9   ee9   ee9   ee9   ee9   f   fddZl G de dfei      Zmdge9dee9e9f   fdhZn G di dj      Zoy)p    N)cdll)Path)AnyListOptionalSequenceTupleUnion)dynamo_timed)configexc)invalid_vec_isaVecISA)	cache_dir)TorchVersion)build_paths)log_global_cache_errorslog_global_cache_statslog_global_cache_valsuse_global_cacheargskwargsreturnc                       y N r   r   s     `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_inductor/cpp_builder.pyr   r   )           c                       y r   r   r   s     r   r   r   ,   r   r    c                       y r   r   r   s     r   r   r   /   r   r    c                       y)NFr   r   r    r   r   r   2   s    r    CxxBuildlinuxdarwinwin32)utf-8r      searchc                    ddl m}m} | D ]  }	 |tt        j                  dk7  rt        j                  d      s0ddlm}  |       } |t
        j                  j                  |d      |      }|5  t               }d d d        t        j                  |dg       |c S  t"        j$                  # 1 sw Y   5xY w# t        j                  t        t         f$ r Y w xY w)	Nr   )get_lock_dirLOCK_TIMEOUTr%   TORCH_INDUCTOR_INSTALL_GXX)FileLockzg++.lock)timeout	--version)torch._inductor.codecacher,   r-   sysplatformosgetenvfilelockr/   pathjoininstall_gcc_via_conda
subprocesscheck_outputSubprocessErrorFileNotFoundErrorImportErrorr   InvalidCxxCompiler)r*   r,   r-   cxxr/   lock_dirlocks          r   cpp_compiler_searchrD   D   s    D 	{ <<7*yy!=>-'>GGLL:6  2/1C2##S+$67J', 
 
  2 2 **,={K 		s3   B=B=7B=2B1= B=1B:	6B==CCc            
         t         j                  j                  t               d      } t         j                  j                  | dd      }t         j                  j	                  |      s~t
        j                  d       t         j                  j                  dd      }|t        j                  d      }|0t        j                  |dd|  d	d
dddgt        j                         |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condacreatez	--prefix=z--channel=conda-forgez--quietz-yz
python=3.8gxx)stdout)r5   r8   r9   r   existsloginfoenvirongetshutilwhichr;   
check_callPIPE)prefixcxx_pathrI   s      r   r:   r:   a   s    WW\\)+u-Fww||FE51H77>>(#,-

{G4=LL)E!!x(+ 	 " Or    compilerc                     	  t        j                  | dgt         j                        j                         j                  t
         }y# t        $ r}t        d|  d      |d}~wt         j                  $ r Y yw xY w)zU
    Check if compiler is ready, in case end user not activate MSVC environment.
    /helpstderrz
Compiler: z is not found.N)	r;   r<   STDOUTstripdecodeSUBPROCESS_DECODE_ARGSr>   RuntimeErrorr=   )rX   
output_msgr   s      r   check_compiler_exist_windowsrc   {   sy    

J##Xw$7
@Q@QRUWV+- 	
  KZz@AsJ%% s   AA	 		A;A""A;:A;c                     t         r-t        j                  j                  dd      } t	        |        | S t        j                         rBt        j                  j                  t        j                         S 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_WINDOWSr5   rP   rQ   rc   r   	is_fbcodetorchversionhipr   ccclang
isinstancecpprA   listtuplerD   )rX   r*   s     r   get_cpp_compilerrr      s    ::>>%.$X. O $)MM$5$5$= CNCTCTCV fjjnntUm46::>>*Fjjnn&F&v.Or    cpp_compilerc                 x    t        j                  | dg      j                  d      }d|j                         d   v S )Nr1   utf8Appler   )r;   r<   r_   
splitlines)rs   version_strings     r   _is_apple_clangry      s<    ,,lK-HIPPQWXNn//1!444r    c                    t         j                  dk(  rt        |       S t        r@t	        j
                  d|       rt        d      t        t	        j
                  d|             S t        t	        j
                  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\+\+))r3   r4   ry   rg   rer*   ra   boolrs   s    r   	_is_clangr~      sj    
||x|,,	99/>o  BIIm\:;;		.=>>r    c                 ~    t         j                  dk(  rt        |       ryt        t	        j
                  d|             S )Nr&   Fz(gcc|g\+\+))r3   r4   ry   r|   r{   r*   r}   s    r   _is_gccr      s.    
||xOL$A		.,788r    c                     t         sy	  t        j                  | dgt        j                        j	                         j
                  t         }d|j                         d   v S # t        $ r
}Y d }~yd }~ww xY w)NFrZ   r[   	Microsoftr   )	rg   r;   r<   r]   r^   r_   r`   rw   r>   )rs   rb   r   s      r   _is_msvc_clr      ss    J##\7$;JDUDUVUWV+- 	
 j335a888 s   AA$ $	A72A7c                    dt         dd fd}	  t        j                  | dgt        j                        j	                         j
                  t         }d|j                         d   v }|rat        r!t        j                  d|       rt        d	      t        j                  d
|      }|"|j                  d      } |t        |             |S # t        $ r
}Y d }~yd }~wt        j                  $ r Y yw xY w)Ncompiler_versionr   c                 R    t         rdnd}| t        |      k  rt        d| d      y)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)rg   r   ra   )r   min_versions     r   _check_minimal_versionz2_is_intel_compiler.<locals>._check_minimal_version   s8     %0jWl;77B;-qQ  8r    r1   r[   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   r;   r<   DEVNULLr^   r_   r`   rw   rg   r{   r*   ra   groupr>   r=   )rs   r   rb   is_intel_compilericx_ver_searchicx_verr   s          r   _is_intel_compilerr      s     $ J##{+J4F4F UWV+	- 	 $z'<'<'>q'AA992LA&u 
  YY'A:NN)(..q1&|G'<=   %% s   B?C 	C5C54C5c                  (    t        t                     S r   )r   rr   r   r    r   is_gccr      s    #%&&r    c                  (    t        t                     S r   )r~   rr   r   r    r   is_clangr      s    %'((r    c                  (    t        t                     S r   )r   rr   r   r    r   r   r      s    .011r    c                  (    t        t                     S r   )ry   rr   r   r    r   is_apple_clangr     s    +-..r    c                  (    t        t                     S r   )r   rr   r   r    r   
is_msvc_clr   
  s    ')**r    c                    t         j                  j                         }d|d<   	  t        j                  | dgt        j
                  |      j                  t         }|j                  dd      }|j                  d	d      }|S # t        $ r_}	  t        j                  | dgt        j
                  |      j                  t         }n# t        $ r}Y d }~Y d }~yd }~ww xY wY d }~d }~ww xY w)
NCLC_ALLz-v)r\   envr1    _
)
r5   rP   copyr;   r<   r]   r_   r`   	Exceptionreplace)rX   r   rx   es       r   get_compiler_version_infor     s    
**//
CCM
00tZ%6%6C

&(* $++D#6N#++D#6N  	Z44;'
0A0Asf,.N  	2	 s5   9B 	C,9CC'	CC'CC''C,	dest_listsrc_listc                 Z    |D ]&  }| j                  t        j                  |             ( y r   )appendr   deepcopy)r   r   items      r   _append_listr   $  s(     .t,-.r    	orig_listc                 D    g }| D ]  }||vs|j                  |        |S r   r   )r   new_listr   s      r   _remove_duplication_in_listr   )  s1    H "xOOD!" Or    path_dirc                     t         j                  j                  |       s	 t        |       j	                  dd       y y # t
        $ r5}|j                  t        j                  k7  rt        d|        Y d }~y d }~ww xY w)NT)parentsexist_okzFail to create path )	r5   r8   rM   r   mkdirOSErrorerrnoEEXISTra   )r   r   s     r   _create_if_dir_not_existr   1  sq    77>>(#	N   = $  	yyELL("*8*5  )	s   ? 	A=+A88A=c                    t         j                  j                  |       rt        j                  | d      D ]~  \  }}}|D ]7  }t         j                  j	                  ||      }t        j
                  |       9 |D ]7  }t         j                  j	                  ||      }t        j                  |       9  t        j                  |        y y )NF)topdown)r5   r8   rM   walkr9   removermdir)r   rootdirsfilesname	file_pathdir_paths          r   _remove_dirr   <  s    	ww~~h!#5!A 	#D$ %GGLLt4			)$%  #77<<d3"#		# 	  r    cmd_linecwdc                 d   t        j                  |       }	 t        j                  ||t        j                        }|S # t        j
                  $ r]}|j                  j                  d      }d|v xs d|v }|rt        j                  dk(  rd}||z  }t        j                  ||      |d }~ww xY w)N)r   r   r\   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splitr;   r<   r]   CalledProcessErroroutputr_   r3   r4   r   CppCompileError)r   r   cmdstatusr   r   openmp_probleminstructions           r   _run_compile_cmdr   H  s    
++h
C6((cs:CTCTU  M (( 6)1V;Qx6?Qcllh62  k!F!!#v.A56s   &? B/AB**B/c                 \    t        d      5  t        | |      cd d d        S # 1 sw Y   y xY w)Ncompile_file)r   r   )r   r   s     r   run_compile_cmdr   ^  s)    	n	% /#./ / /s   "+	orig_pathc                 R    t         r | j                  t        j                  d      S | S )N/)rg   r   r5   sep)r   s    r   normalize_path_separatorr   c  s"      --r    c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 d d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Zd!dZ	d!dZ
d!dZdefdZdee   fdZdee   fdZdee   fdZdee   fdZdee   fdZdee   fdZdee   fdZdefdZdefdZdefdZdeddfdZy)"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.
    NrX   definitionsinclude_dirscflagsldflagslibraries_dirs	librariespassthrough_argsaot_modeuse_absolute_pathcompile_onlyr   c                     || _         |xs g | _        |xs g | _        |xs g | _        |xs g | _        |xs g | _        |xs g | _        |xs g | _        |	| _        |
| _	        || _
        y r   )	_compiler_definations_include_dirs_cflags_ldflags_libraries_dirs
_libraries_passthough_args	_aot_mode_use_absolute_path_compile_only)selfrX   r   r   r   r   r   r   r   r   r   r   s               r   __init__zBuildOptionsBase.__init__p  sw     "'2'8b(4(:"(,B#*=b*8*>B%._"+;+Ar'(9#/r    c                 :    | j                   rg | _        g | _        y y r   )r   r   r   r   s    r   _process_compile_only_optionsz.BuildOptionsBase._process_compile_only_options  s    #%D  DO r    c                 p   t        | j                        | _        t        | j                        | _        t        | j                        | _        t        | j                        | _        t        | j
                        | _        t        | j                        | _        t        | j                        | _        y r   )r   r   r   r   r   r   r   r   r   s    r   _remove_duplicate_optionsz*BuildOptionsBase._remove_duplicate_options  s~    78I8IJ89K9KL24<<@3DMMB:4;O;OP5dooF ;D<Q<Q Rr    c                 4    | j                    | j                   y r   )r   r   r   s    r   _finalize_optionsz"BuildOptionsBase._finalize_options  s    **&&r    c                     | j                   S r   )r   r   s    r   get_compilerzBuildOptionsBase.get_compiler      ~~r    c                     | j                   S r   )r   r   s    r   get_definationsz BuildOptionsBase.get_definations  s       r    c                     | j                   S r   )r   r   s    r   get_include_dirsz!BuildOptionsBase.get_include_dirs      !!!r    c                     | j                   S r   )r   r   s    r   
get_cflagszBuildOptionsBase.get_cflags  s    ||r    c                     | j                   S r   )r   r   s    r   get_ldflagszBuildOptionsBase.get_ldflags  s    }}r    c                     | j                   S r   )r   r   s    r   get_libraries_dirsz#BuildOptionsBase.get_libraries_dirs  s    ###r    c                     | j                   S r   )r   r   s    r   get_librarieszBuildOptionsBase.get_libraries  s    r    c                     | j                   S r   )r   r   s    r   get_passthough_argsz$BuildOptionsBase.get_passthough_args  s    $$$r    c                     | j                   S r   )r   r   s    r   get_aot_modezBuildOptionsBase.get_aot_mode  r  r    c                     | j                   S r   )r   r   s    r   get_use_absolute_pathz&BuildOptionsBase.get_use_absolute_path  s    &&&r    c                     | j                   S r   )r   r   s    r   get_compile_onlyz!BuildOptionsBase.get_compile_only  r  r    filec                    | j                         | j                         | j                         | j                         | j	                         | j                         | j                         | j                         | j                         | j                         | j                         d}t        |d      5 }t        j                  ||       d d d        y # 1 sw Y   y xY w)N)rX   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fs       r   save_flags_to_filez#BuildOptionsBase.save_flags_to_file  s    ))+//1 113oo''')"557++- $ 8 8 :))+!%!;!;!= 113
 $_ 	 IIeQ	  	  	 s   5CC)r   NNNNNNNFFFr   N)__name__
__module____qualname____doc__strr   r   r|   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r  r   r    r   r   r   i  s    +/,0&*'+.2)-04"'"00 d3i(0 tCy)	0
 c#0 $s)$0 !c+0 DI&0 #49-0 0  0 0 
08!
S'c !c !"$s) "DI T#Y $DI $tCy %T#Y %d 't '"$ " s  t  r    r   warning_allc                      t         s| rdgS g S g S )NWallrg   )r&  s    r   _get_warning_all_cflagr*    s    &x.B.	r    std_numc                 ,    t         r		 d} d|  gS d|  gS )Nzc++20zstd:zstd=r)  )r+  s    r   _get_cpp_std_cflagr-    s1    	 wi !!wi !!r    c                 ^    t         rg d}|S ddg}t        |       r|j                  d       |S )N)wd4819wd4251wd4244wd4267wd4275wd4018wd4190wd4624wd4067wd4068EHsczWno-unused-variablezWno-unknown-pragmasz$Werror=ignored-optimization-argument)rg   r~   r   )rs   r   s     r   _get_os_related_cpp_cflagsr:    s:    
" M ()>?\"MM@AMr    c                     t         rdgS t        j                  j                  rddgnddg} | j	                  d       | j	                  d       t        j
                  j                  s| j	                  d       t        j
                  j                  s| j	                  d	       t        j                  d
k7  rOt        j                         s;t        j                         dk(  r| j	                  d       | S | j	                  d       | S )NO2O0gO3DNDEBUGz
ffast-mathzfno-finite-math-onlyzfno-unsafe-math-optimizationszffp-contract=offr&   ppc64lezmcpu=nativezmarch=native)rg   r   aot_inductordebug_compiler   ro   enable_unsafe_math_opt_flag#enable_floating_point_contract_flagr3   r4   rh   machine)r   s    r   _get_optimization_cflagsrG    s    v & 3 3 A A$iGXl#,-zz55MM9:zz==MM,-<<8# ##%##%2MM-0  MM.1r    r   c                 |    t         r	 ddg}|S | rdgS t        j                         dk(  rdt               v rg dS ddgS )NDLLMDfPICDarwinrm   )sharedrK  zundefined dynamic_lookuprM  )rg   r4   systemrr   )r   SHARED_FLAGs     r   _get_shared_cflagrP    sT    	 dm  8O??(W8H8J-JAAf%%r    extra_flagsc                     g }g }g }g }g }g }	g }
t        |      t               z   t        |      z   t               z   t	        |       z   }|
j                  dj                  |             ||||||	|
fS )N )rP  rG  r*  r-  r:  r   r9   )rs   r   r&  rQ  definationsr   r   r   r   r   passthough_argss              r   get_cpp_optionsrV  *  s      K LFG "NI!#O 	,'
"
$	%
 
-	. 
	 %\
2		3  388K01 	 r    c                   H     e Zd ZdZ	 	 	 	 d	dededee   deddf
 fdZ x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.
    r   r&  rQ  r   r   Nc                    t         |           t               | _        || _        || _        t        | j                  |||      \  }}}}}	}
}t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |	       t        | j                  |
       t        | j                  |       | j                          y )N)rs   r   rQ  r&  )superr   rr   r   r   r   rV  r   r   r   r   r   r   r   r   r   )r   r   r&  rQ  r   rT  r   r   r   r   r   rU  	__class__s               r   r   zCppOptions.__init__X  s     	)+"3) %##	
	
 	T&&4T''6T\\6*T]]G,T))>:T__i0T**O< r    )FTr   F)	r!  r"  r#  r$  r|   r   r%  r   __classcell__r[  s   @r   rX  rX  M  sT     # %'"'"!"! "! c]	"!
  "! 
"! "!r    rX  c                  r    t         s0dt        t        t        j                  j
                              z   gS g S )Nz-D_GLIBCXX_USE_CXX11_ABI=)rg   r%  intri   _C_GLIBCXX_USE_CXX11_ABIr   r    r   _get_glibcxx_abi_build_flagsrb  }  s,    +c#ehh6U6U2V.WWXX	r    c                      dgS )NTORCH_INDUCTOR_CPP_WRAPPERr   r   r    r   !_get_torch_cpp_wrapper_definationre    s    ())r    c                      dgS )Nz" C10_USING_CUSTOM_GENERATED_MACROSr   r   r    r   _use_custom_generated_macrosrg    s    011r    c                  p    t         s/t        j                         rg d} d}| j                  |       | S g S g S )N)C10_USE_GLOGC10_USE_MINIMAL_GLOG$C10_DISABLE_TENSORIMPL_EXTENSIBILITY#AOTI_USE_CREATE_TENSOR_FROM_BLOB_V1)rg   r   rh   r   )fb_internal_macroscreate_tensor_from_blob_v1s     r   _use_fb_internal_macrosro    sA    " *O&%%&@A%%I	r    r   r   c                    ddl m} g }g }g }t        r|||fS t        j                         r|j                  d       t        j                  j                  #|j                  t        j                                |j                  t        j                                |j                  t        j                                t        j                  j                  j|j                  t        j                                |j                  t        j                                |j                  t        j                                ni|j                  t        j                                 |j                  t        j"                                |j                  t        j$                                |j                  t        j&                                |j                  t        j(                                |j                  t        j*                                |j                  d       |r|s|}nt,        j.                  j1                  |      }t3        |       r|j                  d       |j                  d       |j                  d|        |j                  dt        j4                         z          |j                  d	t        j4                         z          |||fS )
Nr   )_LINKER_SCRIPTnostdincincludez --rtlib=compiler-rtz -fuse-ld=lldz -Wl,--script=z -Bz -L)r2   rq  rg   r   rh   r   ri   rj   rk   r   sleefopenmppythonclang_includegcc_includegcc_install_tools_include
cc_includelibgcclibgcc_archlibgcc_backwardglibclinux_kernelr5   r8   basenamer~   	glibc_lib)rs   r   r   rq  r   r   rU  linker_scripts           r   _setup_standard_sys_libsr    s    
 9F L!#O|_44j! ==$ 1 1 34K..01K..01==( 9 9 ;< 7 7 9: E E GH 6 6 89 2 2 45 7 7 9:K779:K--/0K4467I&-*MGG,,^<M\"""#9:""?3""^M?#CD""5;+@+@+B#BC""5;+@+@+B#BC<00r    vec_isac                 0   g }g }| t         k7  r| j                         D ]&  }|j                  t        j                  |             ( | j                         g}t        j                         r(t        |       j                         }d| d| d| dg}||fS )NzCPU_CAPABILITY=CPU_CAPABILITY_HAVE__CPU_DEFINITION)
r   build_macror   r   r   build_arch_flagsr   rh   r%  upper)r  macrosbuild_flagsxcaps        r   _get_build_args_of_chosen_isar    s    FK/!$$& 	,AMM$--*+	, //12g,$$&C!#'!#'uO,F ;r    include_pytorchc           	      <   ddl m}m} t        j                  j                  |d      t        j                  j                  |ddddd      t        j                  j                  |dd      t        j                  j                  |dd      g}|g}g }t        j                  d	k7  r+t        j                         sdd
g}|s|j                  d       t        r|j                  d       t        j                  s"|j                  d       |j                  |       |||fS )Nr   )_TORCH_PATHTORCH_LIB_PATHrs  ri   csrcapiTHTHCr&   	torch_cputorch_pythonrt  c10)torch.utils.cpp_extensionr  r  r5   r8   r9   r3   r4   r   rh   r   rg   abi_compatible)r  r   r  r  r   r   r   s          r   _get_torch_related_argsr    s     F 	[),
[)WfeYO 	[)T2
[)U3L %%NI
||x(8(8(:k*	^,!   n-22r    c                     t        t        j                  d            } | j                         s\t	        j
                         dk(  rEt        t        j                  d            }|j                  j                  dz  j                         } | dz  j                         s!t        j                  dt        |               t        |       gS )Nrs  rL  stdlibHeaderszPython.hzCan't find Python.h in )r   	sysconfigget_pathrM   r4   rN  parentabsolutewarningswarnr%  )include_dirstd_libs     r   _get_python_include_dirsr    s    y)))45K HOO$5$Ay))(34~~,,y8BBD*$,,./K0@/ABCr    c                     t               } t        j                  dt        rdnd      }|| j	                  |       t        rOt
        j                  j                  t        j                        }t
        j                  j                  |d      g}nt        j                  d      g}t        j                         r#| j	                  t        j                                | |fS )Nrs  ntposix_prefix)schemelibsLIBDIR)r  r  r  rg   r   r5   r8   dirnamer3   
executabler9   get_config_varr   rh   r   rv  )python_include_dirspython_include_pathpython_pathpython_lib_paths       r   _get_python_related_argsr    s    24#,,+$> &""#67ggoocnn577<<V<=$33H=>"";#5#5#78//r    c                      	 d} t        j                  | j                               j                  d      }t	        t        j                  |            dkD  S # t         j                  $ r Y yw xY w)Nzconda list llvm-openmp --jsonru   r   F)r;   r<   r   r_   lenr  loadsr=   )commandr   s     r   is_conda_llvm_openmp_installedr  '  s^    1((9@@H4::f%&**%% s   AA A-,A-c                     	 t        j                  ddg       t        j                  g d      j                  d      j                         } t        j
                  j                  |       }|| fS # t         j                  $ r Y yw xY w)NrS   brew)r  z--prefixr   ru   )Fr   )r;   r<   r_   r^   r5   r8   rM   r=   )libomp_pathomp_availables     r   homebrew_libompr  1  sw    & 12
 ##$BCVF^UW 	 {3k))%% s   A-A0 0BBomp_namec                 p   	 t        j                  | dg      j                  d      }t        j                  j                  |j                         |      }t        j                  j                  |      r)dt        j                  d<   t        j                  |      }y y # t         j                  $ r Y y w xY w)Nz-print-file-name=binru   TRUEKMP_DUPLICATE_LIB_OK)r;   r<   r_   r5   r8   r9   rstripisfilerP   r   LoadLibraryr=   )rs   r  r   omp_path
omp_modules        r   perload_clang_libomp_winr  E  s    	((,8N)OPWW
 77<<:77>>(#17BJJ-.))(3J $ %% s   BB B54B5c                 Z    dt         dt         dt        fd}	 g d}|D ]  } || |        y )Nrs   lib_namer   c                 `   	  t        j                  | d| gt         j                        j                  t         }|j                         }t        j                  j                  |      r)dt        j                  d<   t        j                  |      }y	 y# t         j                  $ r Y yw xY w)Nz-print-file-name=r[   r  r  TF)r;   r<   r   r_   r`   r  r5   r8   r  rP   r   r  r=   )rs   r  r   r  r  s        r   _load_icx_built_in_lib_by_namez>perload_icx_libomp_win.<locals>._load_icx_built_in_lib_by_nameU  s    	Z,,!28*=>!)) f,.F }}Hww~~h'5;

12!--h7
 (  )) 		s   BB B-,B-)zlibiomp5md.dllzsvml_dispmd.dllz
libmmd.dll)r%  r|   )rs   r  preload_listr  s       r   perload_icx_libomp_winr  S  sC    S C D L ! ?&|X>?r    c                 ,   g }g }g }g }g }g }t         r{|j                  d       |j                  d       t        |        }t        j                  d      }|t        j
                  j                  |dd      }	t        j
                  j                  |	      }
|
r_|j                  t        j
                  j                  |d             |j                  t        j
                  j                  |d             nt        j                  d       |xs |
}|s|j                  d       t        j                  d	      }|s|t               }|rt        j
                  j                  |d      }|j                  t        j
                  j                  |d             |j                  |       t        j                         j                  d
k(  rNt        j
                  j                  t        j
                  j                  |d            r|j                  d       |st               \  }}|r|j                  t        j
                  j                  |d             |j                  t        j
                  j                  |d             nt        r	 t        |       r0|j                  d       |j                  d       t!        | d       n=t#        |       r/|j                  d       |j                  d       t%        |        n|j                  d       |j                  d       nt'        j(                         r_|j                  t+        j,                                t+        j.                         }d| }|j                  |       |j                  d       nmt        |       r#|j                  d       |j                  d       n?t#        |       r|j                  d       n"|j                  d       |j                  d       ||||||fS )NXclangfopenmp
OMP_PREFIXrs  zomp.hlibz-environment variable `OMP_PREFIX` is invalid.ompCONDA_PREFIXx86_64zlibiomp5.dylibiomp5ru  r   z
libomp.dllQiopenmp
libiomp5mdzopenmp:experimentalz-Wp,-fopenmp gompfiopenmp)	_IS_MACOSr   ry   r5   r6   r8   r9   rM   r  r  r  unamerF  r  rg   r~   r  r   r  r   rh   r   ru  
openmp_lib)rs   r   r   include_dir_pathslib_dir_pathsr  rU  r  
omp_prefixheader_path	valid_envconda_prefixconda_lib_pathr  r  fb_openmp_extra_flagss                   r   _get_openmp_argsr  r  s    FG#%!MD!#Ohi  ,L99 YY|,
!'',,z9gFK{3I!((j))LM$$RWW\\*e%DEMN)6YMKK yy0!9:<M!#lE!B!((lI)NO$$^488:%%1bggnnGGLL1AB7 KK( )8):&M;!((k9)MN$$RWW\\+u%EF 
	 \"MM(#KK!$\<@-MM*%KK%"<0
 MM(#MM/0$$[%7%7%9:$//1J&3J<$@!""#89KK&i(F##L1j)i(F#7-}dOSSr    use_mmap_weightsc                 0    g }| r|j                  d       |S )Nz USE_MMAP_SELFr   )r  r  s     r   get_mmap_self_macror    s    F&'Mr    c                    g }g }g }	g }
g }g }g }t               }t               }t        | ||      \  }}}t        |      \  }}t	        ||      \  }}}t               \  }}t        |       \  }}}}}}t               } t               }!t        |      }"||z   |z   |!z   |"z   }||z   |z   |z   }||z   }	|}
||z   |z   }||z   }||z   | z   |z   }|||	|
|||fS )N)r  r   )
re  rg  r  r  r  r  r  rb  ro  r  )#rs   r  r  r   r   r   r  rT  r   r   r   r   r   rU  torch_cpp_wrapper_definations'use_custom_generated_macros_definationssys_libs_cflagssys_libs_include_dirssys_libs_passthough_args
isa_macrosisa_ps_args_build_flagstorch_include_dirstorch_libraries_dirstorch_librariesr  python_libraries_dirs
omp_cflagsomp_ldflagsomp_include_dir_pathsomp_lib_dir_pathsomp_libomp_passthough_argscxx_abi_passthough_argsfb_macro_passthough_argsmmap_self_macross#                                      r   get_cpp_torch_optionsr    s     K LFG "NI!#O$E$G!.J.L+ 	!x9JK	  +H*P'J' 	 (S	 2J1K.. 	& ;<68*+;< 	&
1	2
	 #	# 		  	
	
	  	   z)FG*-AADUUN')I 
!	"
!	" 	  	 r    c                   f     e Zd ZdZe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df fdZ	 x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  r  r&  r   r   r   r  rM  rQ  r   Nc
           	         t         |   |||	|       || _        t        | j                  ||||||      \  }
}}}}}}t        | j                  |
       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       | j                          y )N)r   r&  rQ  r   )rs   r  r  r   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  rM  rQ  torch_definationsr  torch_cflagstorch_ldflagsr  r  torch_passthough_argsr[  s                    r   r   zCppTorchOptions.__init__E  s     	%##/	 	 	
 " "+%/-
	
 ! 	T&&(9:T'');<T\\<0T]]M2T))+?@T__o6T**,AB r    r!  r"  r#  r$  r   r   r|   r   r%  r   r\  r]  s   @r   r  r  9  s    	 * % ""'!&%'.!.! .! 	.!
 .! .!  .! .! .! c].! 
.! .!r    r  c                      t        j                         rgt        j                  j                  Ldt
        j                  vr9dt
        j                  vr&t        j                         t
        j                  d<   y y y y y )N	CUDA_HOME	CUDA_PATH)	r   rh   ri   rj   rk   r5   rP   r   cudar   r    r   _set_gpu_runtime_envr  v  sa    MM%rzz)rzz)"-"2"2"4

; * * & 	r    lpathsc                    t        |       D ]  \  }}dt        j                  v s|j                  t        j                  d         s<t        j                  j                  | d      r_t        j                  |      D ]a  \  }}}d|v st        j                  j                  ||      | |<   | j                  t        j                  j                  | |   d                y )Nr  z/libcudart_static.azlibcudart_static.astubs)		enumerater5   rP   
startswithr8   rM   r   r9   r   )r  ir8   r   r   r   s         r   _transform_cuda_pathsr    s     V$ 
42::%

; 78GGNNdV+>#?@%'WWT] !dE'50 "T4 8F1IMM"'',,vay'"BC	
r    r  c                 l   g }g }g }g }g }g }g }	t        j                         rwdt        j                  vredt        j                  vrSt        j
                  j                  rt        j                         nt        j                         t        j                  d<   t                ddlm}
 |
j                  |       }|
j                  |       }| r|j                  t        j
                  j                  rdnd       t        j
                  j                  4t        j                         r|dgz  }n|dd	gz  }|j                  d
       n"t        j                         r|dgz  }n|g dz  }|rlt        j                         r1ddlm} t        j$                  j'                   |              g}||z  }| r%t        j
                  j                  t)        |       t        j                         rt        j
                  j                  B|j                  t        j$                  j+                  t        j                         d             nA|j                  t        j$                  j+                  t        j                         d             |r!| rt        j
                  j                  |sdg}	|||||||	fS )Nr  r  r   )cpp_extensionz	 USE_ROCMz	 USE_CUDAamdhip64c10_hip	torch_hipz __HIP_PLATFORM_AMD__r  )c10_cudar  
torch_cuda)cpp_prefix_pathrs  z*-Wl,-Bstatic -lcudart_static -Wl,-Bdynamic)r   rh   r5   rP   ri   rj   rk   r   rocmr  r  torch.utilsr  include_pathslibrary_pathsr   r2   r   r8   r  r  r9   )r  r   r   rT  r   r   r   r   r   rU  r  r   cpp_prefix_include_dirs                r   get_cpp_torch_cuda_optionsr&    s-   
  K LFG "NI!#Orzz)rzz) #(--"3"3K9I9I9K 	

; ) ..t4L"006N%--*;*;;M==(!j\)	i55	67!fX%	??	A)+9J)K(L%N"22LEMM%%-!.1==([-=-=-? KL[-=-=-? KL}}  (#'S&TO 	 r    c                   f     e Zd ZdZe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df fdZ	 xZ
S )CppTorchCudaOptionsz
    This class is inherited from CppTorchOptions, which automatic contains
    base cxx build options and torch common build options. And then it will
    maintains cuda device related build args.
    FTr   r  r  r  r   r   r   r  rM  rQ  r   Nc
           	         t         |   |||||||	       g }
g }g }g }g }g }g }t        |||      \  }
}}}}}}t        | j                  |
       t        | j
                  |       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       t        | j                  |       | j                          y )N)r  r  r   r   r   r  rQ  )r  r   r   )rZ  r   r&  r   r   r   r   r   r   r   r   r   )r   r  r  r  r   r   r   r  rM  rQ  cuda_definationscuda_include_dirscuda_cflagscuda_ldflagscuda_libraries_dirscuda_librariescuda_passthough_argsr[  s                    r   r   zCppTorchCudaOptions.__init__  s     	+%/-# 	 	
 ')')!#"$)+$&*, '|
	
  	T&&(89T''):;T\\;/T]]L1T))+>?T__n5T**,@A r    r  r]  s   @r   r(  r(    s     * %""'!&%'0!0! 0! 	0!
 0! 0!  0! 0! 0! c]0! 
0! 0!r    r(  r   c                     t         j                  j                  |       }t         j                  j                  |      \  }}t         j                  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
    )r5   r8   r  splitextr  )r   name_and_extr   extdirs        r   &get_name_and_dir_from_output_file_pathr6    sL    " 77##I.L  .ID#
''//)
$C9r    c                       e Zd ZdZdefdZdefdZ	 ddedeeee   f   de	dedd	f
d
Z
defdZdefdZdeeef   fdZy	)
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                     t         rd}|S d}|S )Nz.pydz.sor)  )r   SHARED_LIB_EXTs     r   __get_python_module_extz"CppBuilder.__get_python_module_ext?  s    #. 5:r    c                     t         rd}|S d}|S )Nz.objz.or)  )r   EXTs     r   __get_object_extzCppBuilder.__get_object_extC  s    #f
 *.
r    r   sourcesBuildOption
output_dirNc                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        || _        |j                         | _         |j                         | _
        |j                         | _        || _        |j!                         | _        | j"                  r| j%                         n| j'                         }t(        j*                  j-                  | j                  | j                   |       | _	        t/        |t0              r|g}t3        j4                         r| j                  r| j                  s|}nZ|D cg c]!  }t(        j*                  j7                  |      # }}t(        j*                  j7                  | j                        | _	        dj-                  |      | _        ndj-                  |      | _        |j;                         D ];  }t<        r| xj                  d| dz  c_        #| xj                  d| dz  c_        = |j?                         D ];  }	t<        r| xj                  d|	 dz  c_        #| xj                  d|	 dz  c_        = |jA                         D ];  }
t<        r| xj                  d|
 dz  c_        #| xj                  d	|
 dz  c_        = |jC                         D ];  }t<        r| xj                  d| dz  c_        #| xj                  d| dz  c_        = |jE                         D ];  }t<        r| xj
                  d
| dz  c_        #| xj
                  d| dz  c_        = |jG                         D ];  }t<        r| xj                  d| dz  c_        #| xj                  d| dz  c_        = |jI                         D ]  }| xj                  | dz  c_         y c c}w )Nr   FrS  r   -z/D z-D z/I z-Iz
/LIBPATH:"z" z-L"z.lib" z-l)%r   _cflags_args_definations_args_include_dirs_args_ldflags_args_libraries_dirs_args_libraries_args_passthough_parameters_args_output_dir_target_filer   r   _namer   r  r  r  r   _CppBuilder__get_object_ext"_CppBuilder__get_python_module_extr5   r8   r9   rn   r%  r   rh   r  _sources_argsr  rg   r  r  r
  r  r  r  )r   r   r?  r@  rA  file_extinp_namer  cflag
definationinc_dirldflaglib_dirr  passthough_args                  r   r   zCppBuilder.__init__G  s    !#"$$&!!+-((-$
 %113"-"C"C"E$113%(99; !! !!#--/ 	
 GGLL)9)9djj\(;TUgs#iG~~d&=&=" :AAABGG,,Q/AA$&GG$4$4T5F5F$G!!$(!3D!$'!2D ++- 	2E!!qq\1!!!qq\1!		2 &557 	>J&&C
|1*==&&&C
|1*==&		> #335 	;G''S	+;;'''Ry?:'		; "--/ 	4F""&m3"""&m3"		4 #557 	=G))z'"-EE)))r'!_<)		= ,,. 	4C$$!C57$$$"SE3$		4 *==? 	EN,,>2B!0DD,	EW Bs   &&O+c                     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} | j                   j                   j                   j                   j
                   j                   j                   j                   j                   j                  
      }|S )NrX   r?  include_dirs_argsdefinations_argscflags_argsldflags_argslibraries_argslibraries_dirs_argspassthougn_argstarget_filer   c
                    t         r-|  d| d| d| d| d| d|	 d| d| d| d}
t        |
      }
|
S j                  rdnd}t        j                  ddd|  d| d| d| d| d| d| d| d| d| d|	 d      j                         }
|
S )	NrS  z /LD /Fez /link z-cr   z[ \n]+z
                    z -o )rg   r   r   r{   subr^   )rX   r?  r[  r\  r]  r^  r_  r`  ra  rb  r   compile_only_argr   s               r   format_build_commandz9CppBuilder.get_command_line.<locals>.format_build_command  s      j"3!4A6F5GqUVW^V__`&'x}GDWCXXYZhYiijkwjxxy{  /s3 J ,0+=+=42 ffJay*:);1[MK\J] ^$%Q|nAn5EQGZF[[\]m\nnrs~r @ %'  Jr    )
rX   r?  r[  r\  r]  r^  r_  r`  ra  rb  )r%  r   rQ  rG  rF  rE  rH  rJ  rI  rK  rM  )r   rf  command_lines   `  r   get_command_linezCppBuilder.get_command_line  s    			  #	 "		
 	 	  	 "%	 !	 	 	@ ,^^&&"55!33))++// $ 9 9 <<))
 r    c                 ,    t        | j                        S r   )r   rM  r   s    r   get_target_file_pathzCppBuilder.get_target_file_path  s    '(9(9::r    c                 (   t        | j                         t        j                  j	                  | j                  | j
                   dt               }t        |       | j                         }t        ||      }t        |       || j                  f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   )r   rL  r5   r8   r9   rN  _BUILD_TEMP_DIRrh  r   r   rM  )r   _build_tmp_dir	build_cmdr   s       r   buildzCppBuilder.build  s|    
 	!!1!12Ao->?
 	!0))+	 ?N#t((((r    )r   )r!  r"  r#  r$  r%  rP  rO  r
   r   r   r   rh  rj  r	   bytesro  r   r    r   r8  r8  .  s      #  \E\E sDI~&\E &	\E
 \E 
\E|-# -^;c ;)uUCZ( )r    r8  )T)zc++17)Tr   r   )FF)pr   r   	functoolsr  loggingr5   r4   r{   r   rR   r;   r3   r  r  ctypesr   pathlibr   typingr   r   r   r   r	   r
   ri   torch._dynamo.utilsr   torch._inductorr   r   torch._inductor.cpu_vec_isar   r   %torch._inductor.runtime.runtime_utilsr   torch.torch_versionr   rh   	triton.fbr   torch._inductor.fb.utilsr   r   r   r   r|   rl  r  	_IS_LINUXr  rg   r`   	getLoggerr!  rN   	lru_cacher%  rD   r:   rc   rr   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   rp  r   r   r   r   r*  r-  r:  rG  rP  rV  rX  rb  re  rg  ro  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  r(  r6  r8  r   r    r   <module>r     s@        	  	    
     > >  , ' ? ; , 6% s c d c S T S C D d 
  LL##G,	LL##H-	llg%'2 g! Q! ! ! !8s 4 T3 4  "# " T5# 5$ 5 5
?C ?D ?9# 9$ 9 Tc d  " T(S (T ( (V T' ' ' T)$ ) ) T24 2 2 T/ / / T+D + +  *.DI .c .t .
49 c s t 	# 	$ 	s   ,/c / / /
  f  f R S	 " "$s) "S T#Y ,$s) 2D T#Y , !#	      #	 
 49d3icDItCy$s)TRUYVW F-!! -!`d3i *49 *2d3i 2c *.1.1.1 .1 49d3ic*+	.1b6 eDItCy<P6Q *33%)3
49d3ic*+3>
$s) 
0%S	49(<"= 0( T   TtSy)  & T
3 
# 
$ 
 
 T? ? ? ?<hThT
49d3icDItCy$s)KLhTV$ 49 RRR R 	R
 R R R 49d3icDItCy$s)TRUYVWRj:!j :!z5$s)  & H
HH H 49d3icDItCy$s)TRUYVW	HV7!/ 7!t
38_0y) y)r    