
    ¯wgg                    h   U 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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mZm Z  d d	l!m"Z"m#Z# d d
l$m%Z% e
jL                  dk(  Z'e
jL                  jQ                  d      Z)e
jL                  jQ                  d      Z*e'rdndZ+e'rdndZ,e'rdndZ-e'rdndZ.e'rdndZ/ej`                  jc                  e2      Z3ej`                  ji                  ej`                  ji                  e3            Z5ej`                  jm                  e5d      Z7e'rdndZ8dZ9dZ:e e e;df   e e;df   f   Z<ee=e<f   Z>e9dfe9dfe9dfe9dfdddddZ?e>e@d<   d ZAeAdfeAd!feAd!feAd"feAd"feAd#feAd#fd$ZBe>e@d%<   g d&ZCd'eee=      d(ee=   fd)ZDd(ee=   fd*ZEd(ee=   fd+ZFd(e=fd,ZGd-ZHd.ZId/ZJd0ZKd1ZL eF       ZMeMr eGd2      ndZNeMej                  j                  d3nd4ZQdZRej                  j                  5 eSd5 ej                  j                  j                  d6      dd7 D              ZRej                  j                         r eE       ndZWej                  j                  d8      xs ej                  j                  d9      ZZ ej                  d:      Z\g d;Z]g d<Z^g d=Z_g d>Z`g d?Za e       Zbd@dAdBZcdC Zdd(eefdDZfd(ee=   fdEZgdF Zhd(e=fdGZidHe=d(eefdIZjd(e eee"f   fdJZkdKe=dLe"d(dfdMZl G dN dOe%      ZmdP ZndQ ZodvdReed(ee=   fdSZpdvdReed(ee=   fdTZq	 	 	 	 	 	 	 	 	 	 dwdUee=ee=   f   dVeee   fdWZrdX ZsdY ZtdZ Zu	 dvd[Zvd\ Zw	 	 	 	 	 	 	 	 	 	 	 	 	 dxd]Zx	 dyd^e=d_eedVeee   d(dfd`ZydUee=   d^e=d_eedVeee   d(df
daZz	 dvdUee=   d^e=d_eedVeee   dbeed(dfdcZ{dd Z|de Z}df Z~dzdgeee=      d(ee=   fdhZdzdgeee=      d(ee=   fdiZdje=d_eed(e=fdkZd_eed(ee;   fdlZd^e=d_eedme=d(dfdnZdo Zdp Z	 	 d{dqZ	 	 d{drZd(e=fdsZdte=d(eefduZy)|    N)Path   )	FileBaton)ExtensionVersioner)hipify_python)GeneratedFileCleaner)DictListOptionalUnionTuple)TorchVersionVersion)	build_extwin32darwinlinuxz.pydz.soz.exe libz.dllz/DLLz-shared)oem )   r   r   )   r   i^  .)
   r      r   ))   r   r      r   )z11.011.111.211.311.411.511.611.7CUDA_GCC_VERSIONS)   r(   r   r   )   r   )   r   )r    r!   r"   r#   r$   r%   r&   CUDA_CLANG_VERSIONS)get_default_build_rootcheck_compiler_ok_for_platform*get_compiler_abi_compatibility_and_versionBuildExtensionCppExtensionCUDAExtensioninclude_pathslibrary_pathsloadload_inlineis_ninja_availableverify_ninja_availability&remove_extension_h_precompiler_headersget_cxx_compilercheck_compiler_is_gccargsreturnc                 H    | sg S | D cg c]  }d|v rd| dn| c}S c c}w )zQuote command-line arguments for DOS/Windows conventions.

    Just wraps every argument which contains blanks in double quotes, and
    returns a new argument list.
     "r   )r;   args     ^/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/utils/cpp_extension.py_nt_quote_argsrB   R   s3     	9=>##*auAJ#->>>s   c                  Z   t         j                  j                  d      xs t         j                  j                  d      } | t        j                  d      }|=t         j
                  j                  t         j
                  j                  |            } nUt        r,t        j                  d      }t        |      dk(  rd} n|d   } nd} t         j
                  j                  |       sd} | r=t        j                  j                         st        d	|  d
t        j                          | S )zFind the CUDA install path.	CUDA_HOME	CUDA_PATHNnvccz7C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v*.*r   r   z/usr/local/cudaz+No CUDA runtime is found, using CUDA_HOME=''file)osenvirongetshutilwhichpathdirname
IS_WINDOWSgloblenexiststorchcudais_availableprintsysstderr)	cuda_home	nvcc_path
cuda_homess      rA   _find_cuda_homer^   ]   s     

{+Jrzz~~k/JILL(	 	(BCI !YYMO
z?a' "I *1I-	77>>), 	002;I;aH::	    c                     t         j                  j                  d      xs t         j                  j                  d      } | t        j                  d      }|t         j
                  j                  t         j
                  j                  t         j
                  j                  |                  } t         j
                  j                  |       dk(  rCt         j
                  j                  |       } n#d}t         j
                  j                  |      r|} | r9t        j                  j                  t        d|  dt        j                         | S )	zFind the ROCm install path.	ROCM_HOME	ROCM_PATHhipcchipz	/opt/rocmz+No ROCm runtime is found, using ROCM_HOME='rG   rH   )rJ   rK   rL   rM   rN   rO   rP   realpathbasenamerT   rU   versionrd   rX   rY   rZ   )	rocm_home
hipcc_pathfallback_paths      rA   _find_rocm_homerk   x   s     

{+Jrzz~~k/JI\\'*
!  ,). /I ww	*e3GGOOI6	 (Mww~~m,)	U]]&&.;I;aH::	r_   c                      t         t        d      t        rt        d      t        j                  j
                  t         g|  S )z
    Join paths with ROCM_HOME, or raises an error if it ROCM_HOME is not set.

    This is basically a lazy way of raising an error for missing $ROCM_HOME
    only once we need to get any ROCm-specific path.
    zSROCM_HOME environment variable is not set. Please set it to your ROCm install root.zDBuilding PyTorch extensions using ROCm and Windows is not supported.)ra   OSErrorrQ   rJ   rO   joinpathss    rA   _join_rocm_homerq      sL      A B 	B	 ; < 	<77<<	*E**r_   a8  

                               !! WARNING !!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Your compiler ({}) may be ABI-incompatible with PyTorch!
Please use a compiler that is ABI-compatible with GCC 5.0 and above.
See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html.

See https://gist.github.com/goldsborough/d466f43e8ffc948ff92de7486c5216d6
for instructions on how to install GCC 5 or higher.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                              !! WARNING !!
a  

                               !! WARNING !!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Your compiler ({user_compiler}) is not compatible with the compiler Pytorch was
built with for this platform, which is {pytorch_compiler} on {platform}. Please
use {pytorch_compiler} to to compile your extension. Alternatively, you may
compile PyTorch from source using {user_compiler}, and then you can also use
{user_compiler} to compile your extension.

See https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md for help
with compiling PyTorch from source.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                              !! WARNING !!
z
The detected CUDA version ({0}) mismatches the version that was used to compile
PyTorch ({1}). Please make sure to use the same CUDA versions.
zThe detected CUDA version ({0}) has a minor version mismatch with the version that was used to compile PyTorch ({1}). Most likely this shouldn't be a problem.z
CUDA was not found on the system, please set the CUDA_HOME or the CUDA_PATH
environment variable or add NVCC to your system PATH. The extension compilation will fail.
rd   TFc              #   2   K   | ]  }t        |        y wN)int).0vs     rA   	<genexpr>rw      s     JAQJs   .   
CUDNN_HOME
CUDNN_PATHz\d+\.\d+\.\d+\w+\+\w+)z/MDz/wd4819z/wd4251z/wd4244z/wd4267z/wd4275z/wd4018z/wd4190z/wd4624z/wd4067z/wd4068z/EHsc)&base_class_has_different_dll_interfacefield_without_dll_interface#dll_interface_conflict_none_assumed(dll_interface_conflict_dllexport_assumed)z-D__CUDA_NO_HALF_OPERATORS__z-D__CUDA_NO_HALF_CONVERSIONS__z"-D__CUDA_NO_BFLOAT16_CONVERSIONS__z-D__CUDA_NO_HALF2_OPERATORS__z--expt-relaxed-constexpr)-fPICz-D__HIP_PLATFORM_AMD__=1z-DUSE_ROCM=1z-DHIPBLAS_V2)z-DCUDA_HAS_FP16=1z-D__HIP_NO_HALF_OPERATORS__=1z-D__HIP_NO_HALF_CONVERSIONS__=1x86	x86_amd64)r   z	win-amd64c                      t         r"t        j                  j                  dd      } | S t        j                  j                  dd      } | S )NCXXclc++)rQ   rJ   rK   rL   )compilers    rA   r9   r9      s;    ::>>%. O ::>>%/Or_   c                  ^    t         j                  t        j                  j                         S rs   )!BUILT_FROM_SOURCE_VERSION_PATTERNmatchrU   rg   __version__r   r_   rA   _is_binary_buildr     s     066u}}7P7PQQQr_   c                      t         rddgS g dS )Nclang++clang)zg++gcczgnu-c++zgnu-ccr   r   )IS_MACOSr   r_   rA    _accepted_compilers_for_platformr     s    #+Iwh1hhr_   c                    t         j                  j                  |       r*t        |       5 }|j	                         }ddd       |k(  ryt        | d      5 }|j                  |       ddd       y# 1 sw Y   6xY w# 1 sw Y   yxY w)z
    Equivalent to writing the content into the file but will not touch the file
    if it already had the right content (to avoid triggering recompile).
    Nw)rJ   rO   rT   openreadwrite)filenamenew_contentfcontentsource_files        rA   _maybe_writer     s~    
 
ww~~h(^ 	qffhG	 k!	h	 '+&' '	 	' 's   A1A=1A:=Bc                  |    t         j                  j                  t        j                  j                  d            S )a  
    Return the path to the root folder under which extensions will built.

    For each extension module built, there will be one folder underneath the
    folder returned by this function. For example, if ``p`` is the path
    returned by this function and ``ext`` the name of an extension, the build
    folder for the extension will be ``p/ext``.

    This directory is **user-specific** so that multiple users on the same
    machine won't meet permission issues.
    torch_extensions)appname)rJ   rO   re   rU   _appdirsuser_cache_dirr   r_   rA   r,   r,     s,     77ENN99BT9UVVr_   r   c                 ,   t         ryt        j                  |       yt        j                  j                        t        fdt               D              ryt        j                  j                         }d|d<    t        j                  | dgt        j                  |      j                  t         }t        rt!        j"                  dt         j$                        }t!        j&                  ||      }t)        |      d	k7  rd
|v S t        j                  j                  |d   j+                               t        j                  j-                        dk(  rd|v ryt        fdt               D              S t.        r|j1                  d      S y)a  
    Verify that the compiler is the expected one for the current platform.

    Args:
        compiler (str): The compiler executable to check.

    Returns:
        True if the compiler is gcc/g++ on Linux or clang/clang++ on macOS,
        and always True for Windows.
    TFc              3   &   K   | ]  }|v  
 y wrs   r   ru   namecompiler_paths     rA   rw   z1check_compiler_ok_for_platform.<locals>.<genexpr>=  s     
PT4= 
P   CLC_ALL-vrZ   env^COLLECT_GCC=(.*)$r   zclang versionr   r   gcc versionc              3   &   K   | ]  }|v  
 y wrs   r   r   s     rA   rw   z1check_compiler_ok_for_platform.<locals>.<genexpr>O  s     XT4=(Xr   zApple clang)rQ   rM   rN   rJ   rO   re   anyr   rK   copy
subprocesscheck_outputSTDOUTdecodeSUBPROCESS_DECODE_ARGSIS_LINUXrecompile	MULTILINEfindallrS   striprf   r   
startswith)r   r   version_stringpatternresultsr   s        @rA   r-   r-   *  sH    LL*MGG$$]3M

P-M-O
PP
**//
CCMhZ,,h-=jFWFW]`ahh  kA  BN**12<<@**Wn5w<1 #n44(()9)9);<77M*e38WX5U5WXXX((77r_   c                    t               sdt        d      fS t        j                  j	                  d      dv rdt        d      fS t        |       sQt        j                  t        j                  | t               d   t        j                               dt        d      fS t        rdt        d      fS 	 t        rOt        }t!        j"                  | dd	g      } |j$                  t&         j)                         j+                  d
      }nt,        }t!        j"                  | t         j.                        }t1        j2                  d |j$                  t&         j)                               }|g dnt5        |j7                               }t=        t?        t@        |            |k\  rdt        d
jC                  |            fS |  dd
jC                  |       } t        j                  tD        j                  |              dt        d
jC                  |            fS # t8        $ rC t        j:                         \  }}}t        j                  d|  d|        dt        d      fcY S w xY w)a  
    Determine if the given compiler is ABI-compatible with PyTorch alongside its version.

    Args:
        compiler (str): The compiler executable name to check (e.g. ``g++``).
            Must be executable in a shell process.

    Returns:
        A tuple that contains a boolean that defines if the compiler is (likely) ABI-incompatible with PyTorch,
        followed by a `TorchVersion` string that contains the compiler version separated by dots.
    Tz0.0.0TORCH_DONT_CHECK_COMPILER_ABION1YESTRUEYr   )user_compilerpytorch_compilerplatformFz-dumpfullversionz-dumpversionrx   )rZ   z(\d+)\.(\d+)\.(\d+))0r   r   z$Error checking compiler version for : r>   )#r   r   rJ   rK   rL   r-   warningswarnWRONG_COMPILER_WARNINGformatr   rY   r   r   r   MINIMUM_GCC_VERSIONr   r   r   r   r   splitMINIMUM_MSVC_VERSIONr   r   searchlistgroups	Exceptionexc_infotuplemaprt   rn   ABI_INCOMPATIBILITY_WARNING)r   minimum_required_version
versionstrrg   compiler_infor   _errors           rA   r.   r.   V  s    l7+,,	zz~~56:YYl7+,, *(3,33"=?B\\ 4 # 	$ |G,--l7+,,.':$#00(<NP^1_`J'j'')?@FFHNNsSG';$&33HZEVEVWMII46Jm6J6JLb6c6i6i6klE).oD<PG Sg#;;l388G#45661SXXg./0HMM-44X>?< 1233  .lln5!<XJbPQ|G,--.s   6CH A	I('I(compiler_namecompiler_versionc                    t         st        t              t        j                  j                  t         dd      } t        j                  |dg      j                         j                  t         }t        j                  d|      }|y |j                  d      }t        |      }t        j                   j"                  y t        t        j                   j"                        }||k7  rt%        |dd       t'        d      |j(                  |j(                  k7  r7t        t*        j-                  |t        j                   j"                              t/        j0                  t2        j-                  |t        j                   j"                               t4        j6                  j9                  d      r+t        j:                  j=                  d	      d
vr
t?               sy | j9                  d      rt@        ntB        }||vrt/        j0                  d|  d|        y ||   \  }	}
d|v r|tB        k(  rd}
dj                  tE        tF        |	            }dj                  tE        tF        |
            }d| d| }|tI        |      k  rt        d|  d| d| d| d|  d| d      |tI        |      k\  rt        d|  d| d| d|  d| d      y )NbinrF   	--versionzrelease (\d+[.]\d+)r   majorzsetuptools>=49.4.0 is requiredr   r   r   r   zThere are no z) version bounds defined for CUDA version zV11.4.48r   rx   z>=z, <z!The current installed version of z (z4) is less than the minimum required version by CUDA z2). Please make sure to use an adequate version of z).z7) is greater than the maximum required version by CUDA z1. Please make sure to use an adequate version of )%rD   RuntimeErrorCUDA_NOT_FOUND_MESSAGErJ   rO   rn   r   r   r   r   r   r   r   groupr   rU   rg   rV   getattr
ValueErrorr   CUDA_MISMATCH_MESSAGEr   r   r   CUDA_MISMATCH_WARNrY   r   r   rK   rL   r   r+   r'   r   strr   )r   r   rF   cuda_version_strcuda_versioncuda_str_versioncuda_vertorch_cuda_versioncuda_compiler_boundsmin_compiler_versionmax_excl_compiler_versionmin_compiler_version_strmax_excl_compiler_version_strversion_bound_strs                 rA   _check_cuda_versionr     s   12277<<	5&1DRz..k/BCIIKRRTjk9935EFL#))!,'(H}}! !3!34%%8Wd+3=>>>>/5554;;<LemmN`N`abb(//0@%--BTBTUVLL##G,JJNN:;Cbb>K>V>VW^>_':ev33m_4]^n]opq:NO_:`77)).BFW.W(/%#&88C5I,J#K (+S:S1T(U% !9 :#>[=\]l+CDD3M?"EUDV W==M<NbQiPj kBBOPRSdReegi 
 |,IJJ3M?"EUDV W==M<N OBBOPRSdReegi  Kr_   c                   z     e Zd ZdZed        Zd fdZd fdZddZ fdZ	de
eef   fdZd	 Zd
 Zd Z xZS )r/   a  
    A custom :mod:`setuptools` build extension .

    This :class:`setuptools.build_ext` subclass takes care of passing the
    minimum required compiler flags (e.g. ``-std=c++17``) as well as mixed
    C++/CUDA compilation (and support for CUDA files in general).

    When using :class:`BuildExtension`, it is allowed to supply a dictionary
    for ``extra_compile_args`` (rather than the usual list) that maps from
    languages (``cxx`` or ``nvcc``) to a list of additional compiler flags to
    supply to the compiler. This makes it possible to supply different flags to
    the C++ and CUDA compiler during mixed compilation.

    ``use_ninja`` (bool): If ``use_ninja`` is ``True`` (default), then we
    attempt to build using the Ninja backend. Ninja greatly speeds up
    compilation compared to the standard ``setuptools.build_ext``.
    Fallbacks to the standard distutils backend if Ninja is not available.

    .. note::
        By default, the Ninja backend uses #CPUS + 2 workers to build the
        extension. This may use up too many resources on some systems. One
        can control the number of workers by setting the `MAX_JOBS` environment
        variable to a non-negative number.
    c                 "     G fdd|       }|S )zReturn a subclass with alternative constructor that extends any original keyword arguments to the original constructor with the given options.c                   "     e Zd Z fdZ xZS )5BuildExtension.with_options.<locals>.cls_with_optionsc                 F    |j                         t        |   |i | y rs   )updatesuper__init__)selfr;   kwargs	__class__optionss      rA   r  z>BuildExtension.with_options.<locals>.cls_with_options.__init__  s!    g& $1&1r_   )__name__
__module____qualname__r  __classcell__)r  r  s   @rA   cls_with_optionsr     s    2 2r_   r  r   )clsr  r  s    ` rA   with_optionszBuildExtension.with_options  s    	2s 	2
  r_   r<   c                 
   t        |   |i | |j                  dd      | _        |j                  dd      | _        | j                  r9d}t               s,t        j                  |j                  d             d| _        y y y )Nno_python_abi_suffixF	use_ninjaTznAttempted to use ninja as the BuildExtension backend but {}. Falling back to using the slow distutils backend.zwe could not find ninja.)	r  r  rL   r  r  r6   r   r   r   )r  r;   r  msgr  s       rA   r  zBuildExtension.__init__  sw    $)&)$*JJ/Eu$M!K6>>KC%'cjj)CDE!& (	 r_   c                 J    t         |           | j                  rd| _        y y )NT)r  finalize_optionsr  force)r  r  s    rA   r  zBuildExtension.finalize_options  s!     ">>DJ r_   c           
           j                         \  }}d}t         j                        }t        |d       }|sO|rM|j                  D ]-  }t
        j                  j                  |      \  }}|dk(  s+d} n t        |d       }|s|rM|rt        st        ||        j                  D ]  }t        |j                  t              r%dD ]   }||j                  vsg |j                  |<   "  j                  |d       dD ]B  }	t        t        j                   d|	       }
|
#t"        r* j                  |d|	 d	|
 d
       D  j%                  |        j'                  |       d|j                  v s̉ j(                  rJ d|j*                   d         j,                  xj.                  g dz  c_        t        j0                  j2                  j5                         r  j,                  xj.                  dgz  c_         j,                  j6                  dk(  rN j,                  xj8                  ddgz  c_         j,                  j:                   j,                  j<                  n j,                  j>                  d fdd d d fd}	 	 	 	 	 	 	 d fd	}d 	 	 	 	 	 	 	 d fd	}	 	 	 	 	 	 	 d fd	} j,                  j6                  dk(  r0 j(                  r| j,                  _        nA| j,                  _        n/ j(                  r| j,                  _        n| j,                  _        tA        jB                          y )NF.cuT)cxxrF   -DTORCH_API_INCLUDE_EXTENSION_HCOMPILER_TYPESTDLIB	BUILD_ABI
_PYBIND11_-DPYBIND11_z="r?   
nvcc_dlinkz;With dlink=True, ninja is required to build cuda extension rx   )r  .cuh.hipz.mmmsvcr"  c                     j                   j                  dk(  rdnd}|j                  d      dz   }t        fd| D              s| j	                  |       y y )Nr$  z/{}:z-{}=stdzc++17c              3   @   K   | ]  }|j                          y wrs   r   )ru   flagcpp_flag_prefixs     rA   rw   zZBuildExtension.build_extensions.<locals>.append_std17_if_no_std_present.<locals>.<genexpr>5  s     KDt7K   )r   compiler_typer   r   append)cflagscpp_format_prefixcpp_flagr*  r  s      @rA   append_std17_if_no_std_presentzGBuildExtension.build_extensions.<locals>.append_std17_if_no_std_present/  sY     +/--*E*E*OU[/66u=O&0HKFKKh' Lr_   c                     t         ddgz   | z   t        |       z   } t        j                  d      }|%t	        d | D              s| j                  d|g       | S )N--compiler-options'-fPIC'CCc              3   >   K   | ]  }|j                  d         yw))-ccbinz--compiler-bindirNr(  ru   r)  s     rA   rw   zKBuildExtension.build_extensions.<locals>.unix_cuda_flags.<locals>.<genexpr>B  s     `QUDOO,KL`   r7  )COMMON_NVCC_FLAGS_get_cuda_arch_flagsrJ   getenvr   extend)r.  _ccbins     rA   unix_cuda_flagsz8BuildExtension.build_extensions.<locals>.unix_cuda_flags8  sc    '+Y783F;<F YYt_F"`Y_``x01Mr_   c                     | bt        t        |             D ]J  }t        j                  j	                  | |         r&t        j                  j                  | |         | |<   L y y rs   )rangerS   rJ   rO   isabsabspath)rp   is     rA   !convert_to_absolute_paths_inplacezJBuildExtension.build_extensions.<locals>.convert_to_absolute_paths_inplaceH  sS     s5z* =A77==q2#%77??58#<a= !r_   c                 J   t        j                  |      }	 j                  j                  }t	        |      rut
        rt        dd      nt        dd      g}j                  j                  d|       t        |t              r|d   }t
        rt        |z   t        |      z   }n |      }nt        |t              r|d   }t
        r	t        |z   } 	|        
| |||||       j                  j                  d|       y # j                  j                  d       w xY w)Nr   rc   rF   compiler_sor  )r   deepcopyr   rG  _is_cuda_fileIS_HIP_EXTENSIONrq   _join_cuda_homeset_executable
isinstancedictCOMMON_HIPCC_FLAGS_get_rocm_arch_flagsCOMMON_HIP_FLAGS)objsrcextcc_argsextra_postargspp_optsr.  original_compilerrF   r1  original_compiler  r?  s            rA   unix_wrap_single_compilezABuildExtension.build_extensions.<locals>.unix_wrap_single_compileO  s    ]]>2FO$(MM$=$=! %?OOE7;UdejlrUstDMM00E!&$/!''!3f!<?STZ?[![!0!8-#E]F#-6F.v6 c3I ,,]<MN,,]<MNs   CD D"c                 <   t         j                  j                  |      } j                  j                         j                  j                  |||| ||      \  }}	}}
}j                  j                  |
||      }j                  j                  dd }t        t        t        |             }t        |t              r|d   }nt        |      }t        r	t        |z   } |       d}d}|r|}t        |t              r|d   }nt        |      }t        r|t!        |      z   }t        t"        z   |z   }n |      } |       |D cg c]  }t%        j&                  |       }}|D cg c]  }t%        j&                  |       }}t        |t              rd|v r |d         }nd}t)        | |	||z   D cg c]  }t%        j&                  |       c}|D cg c]  }t%        j&                  |       c}||||d|
       |	S c c}w c c}w c c}w c c}w )z;Compiles sources by outputting a ninja file and running it.r   Nr  rF   r!  T
sourcesobjectsr.  post_cflagscuda_cflagscuda_post_cflagscuda_dlink_post_cflagsbuild_directoryverbose	with_cuda)rJ   rO   rC  r   include_dirs_setup_compile_get_cc_argsrG  r   r   rI  rM  rN  r   rJ  rQ  rP  rO  shlexquote%_write_ninja_file_and_compile_objects)r]  
output_dirmacrosrf  debugextra_preargsrV  dependsr   r^  rW  common_cflagsextra_cc_cflagsre  r_  ra  r`  r   rb  r1  rE  r  r?  s                      rA   unix_wrap_ninja_compilez@BuildExtension.build_extensions.<locals>.unix_wrap_ninja_compileh  s
   ( 4J .dmm.H.HI ,,Z-97-4nF 3Aw !MM66w}UM"mm77;OCw78I
 .$/,U3">2.<*;7#K+nd3'5f'=$'+N';$#'7:NO_:`'`$'7:L'LO_'_$'67G'H$./?@7BC!u{{1~CC<L#MqEKKN#M #M.$/LN4R)89U)V&)-&10?-0OP1AP5@AU[[^A'!1'= *#
% N) D#M QAs   H
.H<HHc                 ,    t         | z   t        |       z   S rs   )r:  r;  )r.  s    rA   win_cuda_flagsz7BuildExtension.build_extensions.<locals>.win_cuda_flags  s    %1&9: ;r_   c           
          t        j                  |      _        d }	fd}	 |j                  _         
| |||||||      j                  _        S # j                  _        w xY w)Nc                 D   t        j                  d      fd| D        D cg c]  }|r|j                  d       }}t        j                  d      fd| D        D cg c]  }|r|j                  d       }}t        j                  d      fd| D        D cg c]  }|r|j                  d       }}t        |      dk\  r=t        |      dk\  r.|d	   }|d	   }t	        |      rt        d
d      }t        j                  t              rj                  d   }n)t        j                  t              rj                  }ng } |      ddgz   }t        D ]	  }	d|	g|z   } t        D ]  }
dd|
z   g|z   } |d|d|g|z   |z   } nxt        j                  t              r$t        j                  d   z   } |       | |z  } n:t        j                  t              r t        j                  z   } |       | |z  }  |       S c c}w c c}w c c}w )Nz/T(p|c)(.*)c              3   @   K   | ]  }j                  |        y wrs   r   )ru   elem	src_regexs     rA   rw   zbBuildExtension.build_extensions.<locals>.win_wrap_single_compile.<locals>.spawn.<locals>.<genexpr>       (O4)>(Or+  ry   z/Fo(.*)c              3   @   K   | ]  }j                  |        y wrs   ry  )ru   rz  	obj_regexs     rA   rw   zbBuildExtension.build_extensions.<locals>.win_wrap_single_compile.<locals>.spawn.<locals>.<genexpr>  r|  r+  r   z((\-|\/)I.*)c              3   @   K   | ]  }j                  |        y wrs   ry  )ru   rz  include_regexs     rA   rw   zbBuildExtension.build_extensions.<locals>.win_wrap_single_compile.<locals>.spawn.<locals>.<genexpr>  s     HDm11$7Hr+  r   r   rF   
-std=c++17--use-local-env
-Xcompiler-Xcudafe--diag_suppress=z-cz-or  )r   r   r   rS   rI  rK  rM  r.  rN  r   COMMON_MSVC_FLAGSMSVC_IGNORE_CUDAFE_WARNINGS)cmdmsrc_listobj_listinclude_listrS  rR  rF   r.  r)  ignore_warningr  r~  r{  r1  original_spawnr  ru  s              @@@rA   spawnzOBuildExtension.build_extensions.<locals>.win_wrap_single_compile.<locals>.spawn  s!   JJ}5	(O3(O#$ GGAJ 
 JJy1	(O3(O#$ GGAJ 
 !#

? ; ICH A GGAJ   
 x=A%#h-1*<"1+C"1+C$S).uf=%dkk48%)[[%8F'T:%)[[F%'F!/!7<IZ:[![$5 CD&2D%9F%BFC.I `N&02D~2U%VY_%_F`#T3c:\IFR#DKK6!2T[[5G!G6v>v#DKK6!2T[[!@6v>v%c**Y s   H%H#H)r   rH  r.  r   r  )r]  rl  rm  rf  rn  ro  rV  rp  r  r1  rY  r  r  ru  s            rA   win_wrap_single_compilez@BuildExtension.build_extensions.<locals>.win_wrap_single_compile  sg     --7DK!N/+b5&+#'V(4e](6A '5#n#s   A A+c                    j                   j                  sj                   j                          t        j                  j                  |      } j                   j                         j                   j                  |||| ||      \  }}	}}
}|xs g }g }|r&|j                  j                   j                         n%|j                  j                   j                         |j                  t               ||z   |
z   }t        t        t        |             }t        |t               r|d   }nt#        |      } |       d }d }|rddg}|D ]$  }|j%                  d       |j%                  |       & t&        D ]'  }|j%                  d       |j%                  d|z          ) |j                  |
       t        |t               r|d   }nt#        |      } |      }t)        |      }t)        |      }|rt)        |      }t)        |      }t        |t               rd|v r |d         }nd }t+        | |	||||||d	|

       |	S )Nr  r  r  r  r  r  rF   r!  Tr\  )r   initialized
initializerJ   rO   rC  rf  rg  r=  compile_options_debugcompile_optionsr  r   r   rI  rM  rN  r   r-  r  rB   rk  )r]  rl  rm  rf  rn  ro  rV  rp  r   r^  rW  rq  r.  re  r_  ra  r`  common_cflagr  rb  r1  rE  r  ru  s                       rA   win_wrap_ninja_compilez?BuildExtension.build_extensions.<locals>.win_wrap_ninja_compile  sH    ==,,((*4J .dmm.H.HI ,,Z-97-4nF 3Aw */RMFdmmAABdmm;;<  !23m+g5FCw78I
 .$/,U3">2*;7#K+->?$1 5L&&|4&&|45 'B LN&&z2&&'9N'JKL ""7+nd3'5f'=$'+N';$#12B#C #F+F(5K,[9#12B#C .$/LN4R)7|8T)U&)-&1''!1'= *#
% Nr_   r<   N)NNNr   NNN)"
_check_abiiter
extensionsnextr]  rJ   rO   splitextrJ  r   rM  extra_compile_argsrN  _add_compile_flagr   rU   _CrQ   _define_torch_extension_name_add_gnu_cpp_abi_flagr  r   r   src_extensionsbackendsmpsis_builtr,  _cpp_extensionsr   r  _compiler   build_extensions)r  r   r   cuda_extextension_iter	extensionsourcer   rT  r   valrZ  rs  r  r  r1  rE  rY  r  r?  ru  s   `              @@@@@@rA   r  zBuildExtension.build_extensions  s.   *.//*;''doo..	y#++ ))&13%<#H	
 ^T2I y ,/?@ 	wI )66=* ?C)">">><>	44S9? ""9.OP@ Tehh*TF(;<?:**9D6C5PQ6RST --i8&&y1y;;;~~v)denesesdttu'vv~/	w4 	$$(??$>>&&(MM((UG3(==&&&0MM))eV_<)#}}44!]]00N#}}55	(	 	=	O 	O4 04+/15*+2637,0N	 N	`	;
 04+/15*+2637,0C	5 C	5L /3*.04)*1526+/T	 T	p ==&&&0~~(>%(?%~~(?%)A&""4(r_   c                     t         |   |      }| j                  r-|j                  d      }|d d |dd  z   }dj	                  |      }|S )Nrx   )r  get_ext_filenamer  r   rn   )r  ext_nameext_filenameext_filename_partswithout_abir  s        rA   r  zBuildExtension.get_ext_filenamef  s]     w/9 $$!-!3!3C!8,Sb14Frs4KKK88K0Lr_   c                    t        | j                  d      r| j                  j                  d   }n
t               }t	        |      \  }}t
        r1dt        j                  v rdt        j                  vrd}t        |      ||fS )Ncompiler_cxxr   VSCMD_ARG_TGT_ARCHDISTUTILS_USE_SDKzIt seems that the VC environment is activated but DISTUTILS_USE_SDK is not set.This may lead to multiple activations of the VC env.Please set `DISTUTILS_USE_SDK=1` and try again.)	hasattrr   r  r9   r.   rQ   rJ   rK   UserWarning)r  r   r   rg   r  s        rA   r  zBuildExtension._check_abiv  sx    4==.1}}11!4H')H?I
7."**<AT\^\f\fAfEC c""  r_   c                    t        j                  |j                        |_        t        |j                  t              r1|j                  j                         D ]  }|j                  |        y |j                  j                  |       y rs   )r   rH  r  rM  rN  valuesr-  )r  r  r)  r;   s       rA   r  z BuildExtension._add_compile_flag  si    '+}}Y5Q5Q'R	$i22D9!44;;= "D!" ((//5r_   c                 r    |j                   j                  d      }|d   }d| }| j                  ||       y )Nrx   r  -DTORCH_EXTENSION_NAME=)r   r   r  )r  r  namesr   defines        rA   r  z+BuildExtension._define_torch_extension_name  s=    
 $$S)Ry*4&1y&1r_   c           	          | j                  |dt        t        t        j                  j
                              z          y Nz-D_GLIBCXX_USE_CXX11_ABI=)r  r   rt   rU   r  _GLIBCXX_USE_CXX11_ABI)r  r  s     rA   r  z$BuildExtension._add_gnu_cpp_abi_flag  s/    y*ECPUPXPXPoPoLpHq*qrr_   r  )r	  r
  r  __doc__classmethodr  r  r  r  r  r   r   r   r  r  r  r  r  )r  s   @rA   r/   r/     sX    2    '
k)Z !E#|"34 !62sr_   r/   c                    |j                  dg       }|t               z  }||d<   |j                  dg       }|t               z  }||d<   |j                  dg       }|j                  d       |j                  d       |j                  d       |j                  d       t        r|j                  d       ||d<   d	|d
<   t        j                  | |g|i |S )aH  
    Create a :class:`setuptools.Extension` for C++.

    Convenience method that creates a :class:`setuptools.Extension` with the
    bare minimum (but often sufficient) arguments to build a C++ extension.

    All arguments are forwarded to the :class:`setuptools.Extension`
    constructor. Full list arguments can be found at
    https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#extension-api-reference

    Example:
        >>> # xdoctest: +SKIP
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
        >>> from setuptools import setup
        >>> from torch.utils.cpp_extension import BuildExtension, CppExtension
        >>> setup(
        ...     name='extension',
        ...     ext_modules=[
        ...         CppExtension(
        ...             name='extension',
        ...             sources=['extension.cpp'],
        ...             extra_compile_args=['-g'],
        ...             extra_link_args=['-Wl,--no-as-needed', '-lm'])
        ...     ],
        ...     cmdclass={
        ...         'build_ext': BuildExtension
        ...     })
    rf  library_dirs	librariesc10rU   	torch_cputorch_pythonsleefr   language)rL   r2   r3   r-  rQ   
setuptools	Extension)r   r]  r;   r  rf  r  r  s          rA   r0   r0     s    : ::nb1LMO#L)F>::nb1LMO#L)F>

;+IUW[!^$!#F;F:g????r_   c                 F   |j                  dg       }|t        d      z  }||d<   |j                  dg       }|j                  d       |j                  d       |j                  d       |j                  d       t        r4|j                  d	       |j                  d
       |j                  d       n3|j                  d       |j                  d       |j                  d       ||d<   |j                  dg       }t        r	t	        j
                         }t        j                  |||t        j                  j                  |d      g|D cg c]!  }t        j                  j                  |      # c}ddd      }	t               }
|D ]t  }t        j                  j                  |      }||	v r|	|   j                  |	|   j                  n|}|
j                  t        j                  j                  ||             v t        |
      }|t!        d      z  }||d<   d|d<   |j                  dg       }|j                  dd      xs |}|r|j                  di       }|j                  dg       }|dgz  }||D cg c]  }d| 	 c}z  }||D cg c]  }d| 	 c}z  }t"        j$                  j&                  ,t)        t"        j$                  j&                        dk\  r|dgz  }||d<   ||d<   t+        j,                  | |g|i |S c c}w c c}w c c}w )a  
    Create a :class:`setuptools.Extension` for CUDA/C++.

    Convenience method that creates a :class:`setuptools.Extension` with the
    bare minimum (but often sufficient) arguments to build a CUDA/C++
    extension. This includes the CUDA include path, library path and runtime
    library.

    All arguments are forwarded to the :class:`setuptools.Extension`
    constructor. Full list arguments can be found at
    https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#extension-api-reference

    Example:
        >>> # xdoctest: +SKIP
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
        >>> from setuptools import setup
        >>> from torch.utils.cpp_extension import BuildExtension, CUDAExtension
        >>> setup(
        ...     name='cuda_extension',
        ...     ext_modules=[
        ...         CUDAExtension(
        ...                 name='cuda_extension',
        ...                 sources=['extension.cpp', 'extension_kernel.cu'],
        ...                 extra_compile_args={'cxx': ['-g'],
        ...                                     'nvcc': ['-O2']},
        ...                 extra_link_args=['-Wl,--no-as-needed', '-lcuda'])
        ...     ],
        ...     cmdclass={
        ...         'build_ext': BuildExtension
        ...     })

    Compute capabilities:

    By default the extension will be compiled to run on all archs of the cards visible during the
    building process of the extension, plus PTX. If down the road a new card is installed the
    extension may need to be recompiled. If a visible card has a compute capability (CC) that's
    newer than the newest version for which your nvcc can build fully-compiled binaries, Pytorch
    will make nvcc fall back to building kernels with the newest version of PTX your nvcc does
    support (see below for details on PTX).

    You can override the default behavior using `TORCH_CUDA_ARCH_LIST` to explicitly specify which
    CCs you want the extension to support:

    ``TORCH_CUDA_ARCH_LIST="6.1 8.6" python build_my_extension.py``
    ``TORCH_CUDA_ARCH_LIST="5.2 6.0 6.1 7.0 7.5 8.0 8.6+PTX" python build_my_extension.py``

    The +PTX option causes extension kernel binaries to include PTX instructions for the specified
    CC. PTX is an intermediate representation that allows kernels to runtime-compile for any CC >=
    the specified CC (for example, 8.6+PTX generates PTX that can runtime-compile for any GPU with
    CC >= 8.6). This improves your binary's forward compatibility. However, relying on older PTX to
    provide forward compat by runtime-compiling for newer CCs can modestly reduce performance on
    those newer CCs. If you know exact CC(s) of the GPUs you want to target, you're always better
    off specifying them individually. For example, if you want your extension to run on 8.0 and 8.6,
    "8.0+PTX" would work functionally because it includes PTX that can runtime-compile for 8.6, but
    "8.0 8.6" would be better.

    Note that while it's possible to include all supported archs, the more archs get included the
    slower the building process will be, as it will build a separate kernel image for each arch.

    Note that CUDA-11.5 nvcc will hit internal compiler error while parsing torch/extension.h on Windows.
    To workaround the issue, move python binding logic to pure C++ file.

    Example use:
        #include <ATen/ATen.h>
        at::Tensor SigmoidAlphaBlendForwardCuda(....)

    Instead of:
        #include <torch/extension.h>
        torch::Tensor SigmoidAlphaBlendForwardCuda(...)

    Currently open issue for nvcc bug: https://github.com/pytorch/pytorch/issues/69460
    Complete workaround code example: https://github.com/facebookresearch/pytorch3d/commit/cb170ac024a949f1f9614ffe6af1c38d972f7d48

    Relocatable device code linking:

    If you want to reference device symbols across compilation units (across object files),
    the object files need to be built with `relocatable device code` (-rdc=true or -dc).
    An exception to this rule is "dynamic parallelism" (nested kernel launches)  which is not used a lot anymore.
    `Relocatable device code` is less optimized so it needs to be used only on object files that need it.
    Using `-dlto` (Device Link Time Optimization) at the device code compilation step and `dlink` step
    help reduce the protentional perf degradation of `-rdc`.
    Note that it needs to be used at both steps to be useful.

    If you have `rdc` objects you need to have an extra `-dlink` (device linking) step before the CPU symbol linking step.
    There is also a case where `-dlink` is used without `-rdc`:
    when an extension is linked against a static lib containing rdc-compiled objects
    like the [NVSHMEM library](https://developer.nvidia.com/nvshmem).

    Note: Ninja is required to build a CUDA Extension with RDC linking.

    Example:
        >>> # xdoctest: +SKIP
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
        >>> CUDAExtension(
        ...        name='cuda_extension',
        ...        sources=['extension.cpp', 'extension_kernel.cu'],
        ...        dlink=True,
        ...        dlink_libraries=["dlink_lib"],
        ...        extra_compile_args={'cxx': ['-g'],
        ...                            'nvcc': ['-O2', '-rdc=true']})
    r  T)rV   r  r  rU   r  r  amdhip64c10_hip	torch_hipcudartc10_cuda
torch_cudarf  *)project_directoryoutput_directoryheader_include_dirsincludesextra_filesshow_detailedis_pytorch_extensionhipify_extra_files_onlyr   r  dlink_librariesdlinkFr  r!  z-dlink-Lz-lr!   z-dlto)rL   r3   r-  rJ  rJ   getcwdr   hipifyrO   rn   rC  sethipified_pathaddrelpathr   r2   rU   rg   rV   r   r  r  )r   r]  r;   r  r  r  rf  	build_dirshipify_resulthipified_sourcesr  s_abshipified_s_absr  r  r  extra_compile_args_dlinkxs                      rA   r1   r1     s   L ::nb1LMt,,L)F>

;+IUW[!^$$#%"$&#F;::nb1LIIK	%,,'& ,ggll9c235<=+=!%$(	
 5 	MFGGOOF+EEJmE[+E2@@L ,E2@@SX    !KL	M '(Mt,,L)F>F:jj!2B7OJJw&9/E#ZZ(<bA#5#9#9,#K  XJ.  |$D!r!X$DD  $G!r!X$GG MM*U]]=O=O0PTZ0Z$	1$+C<('9#$g????O >: %E$Gs   &LLLrV   c                    t         j                  j                  t        d      }|t         j                  j                  |dddd      t         j                  j                  |d      t         j                  j                  |d      g}| rQt        rK|j                  t         j                  j                  |d             |j                  t        d             |S | rt        d      }|dk7  r|j                  |       t         j                  j                  d	d
      x}r|dk7  r|j                  |       t        3|j                  t         j                  j                  t        d             |S )z
    Get the include paths required to build a C++ or CUDA extension.

    Args:
        cuda: If `True`, includes CUDA-specific include paths.

    Returns:
        A list of include path strings.
    includerU   csrcapiTHTHCTHHz/usr/includeCUDA_INC_PATHN)rJ   rO   rn   _TORCH_PATHrJ  r-  rq   rK  rK   rL   rz   )rV   lib_includerp   cuda_home_includecuda_inc_paths        rA   r2   r2   {  s    '',,{I6K
['65)D 	[$'
[%(E  RWW\\+u56_Y/0 L 
+I6 .LL*+  ZZ^^OTBBMB/LL'!LLj)<=Lr_   c                 r   t         g}| r]t        rWd}|j                  t        |             t        3|j                  t
        j                  j                  t        d             |S | rt        r!t
        j                  j                  dd      }nTd}t
        j                  j                  t        |            s*t
        j                  j                  t        d            rd}|j                  t        |             t        3|j                  t
        j                  j                  t        |             |S )z
    Get the library paths required to build a C++ or CUDA extension.

    Args:
        cuda: If `True`, includes CUDA-specific library paths.

    Returns:
        A list of library path strings.
    r   x64lib64)TORCH_LIB_PATHrJ  r-  rq   HIP_HOMErJ   rO   rn   rQ   rT   rK  rz   )rV   rp   lib_dirs      rA   r3   r3     s     E _W-.LLh67  L 
ggll5%0GGGGNN?7#;<GGNN?5#9:  _W-.!LLj':;Lr_   r]  re  c                 r    t        | t        |t              r|gn||||||xs t        | |      |||	|
|      S )a  
    Load a PyTorch C++ extension just-in-time (JIT).

    To load an extension, a Ninja build file is emitted, which is used to
    compile the given sources into a dynamic library. This library is
    subsequently loaded into the current Python process as a module and
    returned from this function, ready for use.

    By default, the directory to which the build file is emitted and the
    resulting library compiled to is ``<tmp>/torch_extensions/<name>``, where
    ``<tmp>`` is the temporary folder on the current platform and ``<name>``
    the name of the extension. This location can be overridden in two ways.
    First, if the ``TORCH_EXTENSIONS_DIR`` environment variable is set, it
    replaces ``<tmp>/torch_extensions`` and all extensions will be compiled
    into subfolders of this directory. Second, if the ``build_directory``
    argument to this function is supplied, it overrides the entire path, i.e.
    the library will be compiled into that folder directly.

    To compile the sources, the default system compiler (``c++``) is used,
    which can be overridden by setting the ``CXX`` environment variable. To pass
    additional arguments to the compilation process, ``extra_cflags`` or
    ``extra_ldflags`` can be provided. For example, to compile your extension
    with optimizations, pass ``extra_cflags=['-O3']``. You can also use
    ``extra_cflags`` to pass further include directories.

    CUDA support with mixed compilation is provided. Simply pass CUDA source
    files (``.cu`` or ``.cuh``) along with other sources. Such files will be
    detected and compiled with nvcc rather than the C++ compiler. This includes
    passing the CUDA lib64 directory as a library directory, and linking
    ``cudart``. You can pass additional flags to nvcc via
    ``extra_cuda_cflags``, just like with ``extra_cflags`` for C++. Various
    heuristics for finding the CUDA install directory are used, which usually
    work fine. If not, setting the ``CUDA_HOME`` environment variable is the
    safest option.

    Args:
        name: The name of the extension to build. This MUST be the same as the
            name of the pybind11 module!
        sources: A list of relative or absolute paths to C++ source files.
        extra_cflags: optional list of compiler flags to forward to the build.
        extra_cuda_cflags: optional list of compiler flags to forward to nvcc
            when building CUDA sources.
        extra_ldflags: optional list of linker flags to forward to the build.
        extra_include_paths: optional list of include directories to forward
            to the build.
        build_directory: optional path to use as build workspace.
        verbose: If ``True``, turns on verbose logging of load steps.
        with_cuda: Determines whether CUDA headers and libraries are added to
            the build. If set to ``None`` (default), this value is
            automatically determined based on the existence of ``.cu`` or
            ``.cuh`` in ``sources``. Set it to `True`` to force CUDA headers
            and libraries to be included.
        is_python_module: If ``True`` (default), imports the produced shared
            library as a Python module. If ``False``, behavior depends on
            ``is_standalone``.
        is_standalone: If ``False`` (default) loads the constructed extension
            into the process as a plain dynamic library. If ``True``, build a
            standalone executable.

    Returns:
        If ``is_python_module`` is ``True``:
            Returns the loaded PyTorch extension as a Python module.

        If ``is_python_module`` is ``False`` and ``is_standalone`` is ``False``:
            Returns nothing. (The shared library is loaded into the process as
            a side effect.)

        If ``is_standalone`` is ``True``.
            Return the path to the executable. (On Windows, TORCH_LIB_PATH is
            added to the PATH environment variable as a side effect.)

    Example:
        >>> # xdoctest: +SKIP
        >>> from torch.utils.cpp_extension import load
        >>> module = load(
        ...     name='extension',
        ...     sources=['extension.cpp', 'extension_kernel.cu'],
        ...     extra_cflags=['-O2'],
        ...     verbose=True)
    keep_intermediates)_jit_compilerM  r   _get_build_directory)r   r]  extra_cflagsextra_cuda_cflagsextra_ldflagsextra_include_pathsrc  rd  re  is_python_moduleis_standaloner  s               rA   r4   r4     sP    x -	7>/g>-/ /r_   c                      g } dD ]A  }t        t        j                  d|       }|#t        r*| j	                  d| d| d       C | S )Nr  r  r   z=\"z\")r   rU   r  rQ   r-  )
abi_cflagspnamepvals      rA   _get_pybind11_abi_build_flagsr  0  s^     J9 Buxx:eW!56JE7$tfC@AB r_   c                  f    dt        t        t        j                  j                              z   g} | S r  )r   rt   rU   r  r  )glibcxx_abi_cflagss    rA   _get_glibcxx_abi_build_flagsr  D  s*    5C@_@_<`8aabr_   c                    t         syt        j                  j                         }d|d<   	  t	        j
                  | dgt        j                  |      j                  t         }t        j                  dt        j                        }t        j                  ||      }t        |      dk7  ryt        j                  j!                  |d	   j#                               }t        j                  j%                  |      d
k(  rd|v ryy# 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)NFr   r   r   r   r   r   r   r   r   r   T)r   rJ   rK   r   r   r   r   r   r   r   r   r   r   r   rS   rO   re   r   rf   )r   r   r   er   r   r   s          rA   r:   r:   H  s=   
**//
CCMl00(D1A*J[J[adell  oE  F jj-r||<Gjj.1G
7|qGG$$WQZ%5%5%78M	ww&%/M^4S  	wZ44h5LU_UfUflopww  zP  QN 	5	 s5   9D 	E/9E
	E*
	E!E*E!!E**E/c                 R   t         syt               }t        |      }|du ryt        j                  j                  t        ddd      }t        j                  j                  t        ddd      }t        j                  j                  t        ddd      }d }d	 }	d
 }
d }d fd}d } ||       }dj                  |r|D cg c]  }d| 	 c}ng       }t        j                  j                  t        d      }d| dj                  t        j                  d            dj                  t        j                  j                  |dddd            g} ||      }g }|s|dgz  }|ddgz  }|t               D cg c]  }|  c}z  }|t               D cg c]  }|  c}z  } ||      } |	|||||||      } |
|      }t        j                  j                  |      dur ||        |||       y |||      }|du r ||        |||       yyc c}w c c}w c c}w )aP  
    Precompiled Headers(PCH) can pre-build the same headers and reduce build time for pytorch load_inline modules.
    GCC offical manual: https://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Precompiled-Headers.html
    PCH only works when built pch file(header.h.gch) and build target have the same build parameters. So, We need
    add a signature file to record PCH file parameters. If the build parameters(signature) changed, it should rebuild
    PCH file.

    Note:
    1. Windows and MacOS have different PCH mechanism. We only support Linux currently.
    2. It only works on GCC/G++.
    NFr  rU   zextension.hextension.h.gchextension.h.signc                 0    d}| |S | D ]
  }||dz   z  } |S )Nr   r>   r   )r  stringelements      rA   listToStringzF_check_and_build_extension_h_precompiler_headers.<locals>.listToString|  s7    9M  	&Gw}%F	& r_   c                 x    t        j                  ddd|  d| d| d| d| d| d| d      j                         S )Nz[ \n]+r>   z
                z -x c++-header z -o z
            )r   subr   )r   	head_filehead_file_pchrq  torch_include_dirsr
  r  s          rA   format_precompiler_header_cmdzW_check_and_build_extension_h_precompiler_headers.<locals>.format_precompiler_header_cmd  sz    vv
/)DqI[H\\]^q]rrs  uA  tB  BC  DQ  CR R
 %'	r_   c                 *    | j                  dd      }|S )Nr>   r   )replace)r  	signatures     rA   command_to_signaturezN_check_and_build_extension_h_precompiler_headers.<locals>.command_to_signature  s    KKS)	r_   c                     t         j                  j                  |       }|du ryt        |       5 }|j	                         }||k(  cd d d        S # 1 sw Y   y xY w)NF)rJ   rO   isfiler   r   )	file_pathr)  b_existrI   r   s        rA   check_pch_signature_in_filezU_check_and_build_extension_h_precompiler_headers.<locals>.check_pch_signature_in_file  sP    ''..+e)_ 	(iikG'		( 	( 	(s   AAc                    t         j                  j                  |       s	 t        |       j	                  dd       y y # t
        $ r6}|j                  t        j                  k7  rt        d|        |Y d }~y d }~ww xY w)NT)parentsexist_okzFail to create path )	rJ   rO   rT   r   mkdirrm   errnoEEXISTr   )path_direxcs     rA   _create_if_not_existzN_check_and_build_extension_h_precompiler_headers.<locals>._create_if_not_exist  sr    ww~~h'SX$$TD$A (  S99,&)=hZ'HIsR -Ss   ? 	A>,A99A>c                      t         j                  j                  |              t        | d      5 }|j	                  |       |j                          d d d        y # 1 sw Y   y xY w)Nr   )rJ   rO   rP   r   r   close)r-  pch_signr   r8  s      rA   write_pch_signature_to_filezU_check_and_build_extension_h_precompiler_headers.<locals>.write_pch_signature_to_file  sN    RWW__Y78)S! 	QGGHGGI	 	 	s   "AA'c                     	 t        j                  | dt         j                         y # t         j                  $ r}t	        d|        |d }~ww xY w)NT)shellrZ   z)Compile PreCompile Header fail, command: )r   r   r   CalledProcessErrorr   )pch_cmdr  s     rA   build_precompile_headerzQ_check_and_build_extension_h_precompiler_headers.<locals>.build_precompile_header  sN    	]##G4
@Q@QR,, 	]!J7)TU[\\	]s   &) AAAr>   -Iz-I z-I {}r  r  r  r  r   T)r   r9   r:   rJ   rO   rn   r  r   	sysconfigget_pathr  r  r,  )r
  r  r  r   b_is_gccr#  r$  head_file_signaturer   r&  r*  r/  r<  rA  extra_cflags_strr  extra_include_paths_strr  r%  torch_include_dirs_strrq  r  common_cflags_strr@  r;  b_same_signr8  s                             @rA   0_check_and_build_extension_h_precompiler_headersrL  `  s8    !H$X.H5[)WmLIGGLLiBSTM'',,{IwHZ[
	(S] $L1!hh?R':;G2gY;XZ '',,{I6K
k]y)))45rww||K&%ST **<=M;<<lG,,M&C&EFfFFM&B&DEfEEM$]3+HiPacy  |L  Ne  fG#G,H	ww~~m$D0(#$7B12ExP%#G,'(;XF  ? 	<& GEs   H>
H
H$c                      d } t         j                  j                  t        ddd      }t         j                  j                  t        ddd      } | |        | |       y )Nc                 n    t         j                  j                  |       rt        j                  |        y y rs   )rJ   rO   rT   remove)	path_files    rA   _remove_if_file_existszFremove_extension_h_precompiler_headers.<locals>._remove_if_file_exists  s#    77>>)$IIi  %r_   r  rU   r  r  )rJ   rO   rn   r  )rQ  r$  rF  s      rA   r8   r8     sM    ! GGLLiBSTM'',,{IwHZ[=)./r_   c                 >   |xs t        | |	      }t        |t              r|g}|xs g }t        |t              r|g}|j                  dd       |du rt	        ||       n
t                |g }|j                  d       t        |t              r|g}t        |t              r|D ci c]  }|| }}n't        |t              st        dt        |             |j                         D ]>  \  }}|r|j                  d| d| d| d	       $|j                  d| d
| d| d	       @ |j                  d       ||z  }t        j                  j                  |d      }t        |dj                  |             |g}|r|j                  dd       |j                  dd       |j                  dd       t        j                  j                  |d      }t        |dj                  |             |j                  |       t!        | |||||||	|
|d|      S c c}w )aE  
    Load a PyTorch C++ extension just-in-time (JIT) from string sources.

    This function behaves exactly like :func:`load`, but takes its sources as
    strings rather than filenames. These strings are stored to files in the
    build directory, after which the behavior of :func:`load_inline` is
    identical to :func:`load`.

    See `the
    tests <https://github.com/pytorch/pytorch/blob/master/test/test_cpp_extensions_jit.py>`_
    for good examples of using this function.

    Sources may omit two required parts of a typical non-inline C++ extension:
    the necessary header includes, as well as the (pybind11) binding code. More
    precisely, strings passed to ``cpp_sources`` are first concatenated into a
    single ``.cpp`` file. This file is then prepended with ``#include
    <torch/extension.h>``.

    Furthermore, if the ``functions`` argument is supplied, bindings will be
    automatically generated for each function specified. ``functions`` can
    either be a list of function names, or a dictionary mapping from function
    names to docstrings. If a list is given, the name of each function is used
    as its docstring.

    The sources in ``cuda_sources`` are concatenated into a separate ``.cu``
    file and  prepended with ``torch/types.h``, ``cuda.h`` and
    ``cuda_runtime.h`` includes. The ``.cpp`` and ``.cu`` files are compiled
    separately, but ultimately linked into a single library. Note that no
    bindings are generated for functions in ``cuda_sources`` per  se. To bind
    to a CUDA kernel, you must create a C++ function that calls it, and either
    declare or define this C++ function in one of the ``cpp_sources`` (and
    include its name in ``functions``).

    See :func:`load` for a description of arguments omitted below.

    Args:
        cpp_sources: A string, or list of strings, containing C++ source code.
        cuda_sources: A string, or list of strings, containing CUDA source code.
        functions: A list of function names for which to generate function
            bindings. If a dictionary is given, it should map function names to
            docstrings (which are otherwise just the function names).
        with_cuda: Determines whether CUDA headers and libraries are added to
            the build. If set to ``None`` (default), this value is
            automatically determined based on whether ``cuda_sources`` is
            provided. Set it to ``True`` to force CUDA headers
            and libraries to be included.
        with_pytorch_error_handling: Determines whether pytorch error and
            warning macros are handled by pytorch instead of pybind. To do
            this, each function ``foo`` is called via an intermediary ``_safe_foo``
            function. This redirection might cause issues in obscure cases
            of cpp. This flag should be set to ``False`` when this redirect
            causes issues.

    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
        >>> from torch.utils.cpp_extension import load_inline
        >>> source = """
        at::Tensor sin_add(at::Tensor x, at::Tensor y) {
          return x.sin() + y.sin();
        }
        """
        >>> module = load_inline(name='inline_extension',
        ...                      cpp_sources=[source],
        ...                      functions=['sin_add'])

    .. note::
        By default, the Ninja backend uses #CPUS + 2 workers to build the
        extension. This may use up too many resources on some systems. One
        can control the number of workers by setting the `MAX_JOBS` environment
        variable to a non-negative number.
    r   z#include <torch/extension.h>Tz*PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {z3Expected 'functions' to be a list or dict, but was zm.def("z", torch::wrap_pybind_function(z), "z");z", z, "}zmain.cpp
z#include <torch/types.h>r   z#include <cuda.h>ry   z#include <cuda_runtime.h>zcuda.cuF)r  r  )r	  rM  r   insertrL  r8   r-  r   rN  r   typeitemsrJ   rO   rn   r   r  )r   cpp_sourcescuda_sources	functionsr
  r  r  r  rc  rd  re  r  with_pytorch_error_handlingr  use_pch
module_defr   function_name	docstringcpp_source_pathr]  cuda_source_paths                         rA   r5   r5     s9   l &L)=dG)LO+s#"m%2L,$$~q89$8GZ[.0
 
FGi%"Ii&'01!A1I1It,RSWXaSbRcdee(1(9 	`$M9*!!GM?:YZgYhhlmvlwwz"{|!!GM?#m_CPY{Z]"^_		`
 	#z!ggll?J?O$))K"89GA9:A23A:;77<<C%tyy'>?'(-/ /5 2s   &
Hrc  rd  c                 p   |	r|
rt        d      |t        t        t        |            }t        d |xs g D              }t        j                  |       }t        j                  | |||||g|||	|
      }|dkD  r:||k7  r.|r,t        d|  dd| d|  d	| d
z   t        j                         |  d	| } t        t        j                  j                  |d            }|j                         rW	 ||k7  rt        |      5 }t         r|s|rt#        j$                  ||||ng |D cg c]!  }t        j                  j'                  |      # c}t)        d      t        j                  j                  t*        d      g||d|	      }t-               }|D ]E  }t        j                  j'                  |      }|j/                  ||v r||   j0                  n|       G t3        |      }t5        | ||xs g |xs g |xs g |xs g ||||

       d d d        n!|rt        d|  dt        j                         |j7                          n|j9                          |rt        d|  d
t        j                         |
rt;        | |      S t=        | ||	      S c c}w # 1 sw Y   kxY w# |j7                          w xY w)Nz>`is_python_module` and `is_standalone` are mutually exclusive.c              3   $   K   | ]  }d |v  
 yw)cudnnNr   )ru   r   s     rA   rw   z_jit_compile.<locals>.<genexpr>  s     ?aW\?s   )build_argumentsrc  re  r  r  r   z*The input conditions for extension module z have changed. zBumping to version z and re-building as _v...rH   lockr  r  T)	r  r  r  r  ignoresr  show_progressr  	clean_ctx)
r   r]  r
  r  r  r  rc  rd  re  r  z9No modifications detected for re-loaded extension module z, skipping build step...zLoading extension module )r   r   r   rI  JIT_EXTENSION_VERSIONERget_versionbump_version_if_changedrX   rY   rZ   r   rJ   rO   rn   try_acquirer   rJ  r   r  rC  rq   r  r  r  r  r   #_write_ninja_file_and_build_libraryreleasewait_get_exec_path_import_module_from_library)r   r]  r
  r  r  r  rc  rd  re  r  r  r  
with_cudnnold_versionrg   batonrk  r  r  r  r  r  s                         rA   r  r  }  s    MYZZM734	?=+>B??J)55d;K%==%'8-I\]')# > G {k!g>tfOT'y0DTF"WIUXYZzz# r'#bggll?F;<E&	+%)=OP 5T]'Y*(5(<(<.=-<H[Hg1DmoEL(M);(M%4S%9277<<UX;Y$Z*1*115&/
) ,/5(&- zF$&GGOOF$;E,00W\`mWmu1E1S1Ssxyz #''7"87! '%1%7R*;*Ar&3&9r,?,E2(7 '"+&3
5+5 5@    $v%=?EHZZQ MMO

)$s3#**EdO44&t_>NOOQ )N5 5H MMOs7   0J# %J'&JB>J*J# JJ J# #J5c
                 |   t                t               }
t        |
       |	t        t	        t
        |             }	t        j                  j                  |d      }|rt        d| dt        j                         t        ||||||| |d d |	       |rt        dt        j                         t        ||d       y )	Nbuild.ninjaEmitting ninja build file rg  rH   rO   r.  r_  r`  ra  rb  r]  r^  ldflagslibrary_targetre  zCompiling objects...z%Error compiling objects for extensionerror_prefix)r7   r9   r.   r   r   rI  rJ   rO   rn   rX   rY   rZ   _write_ninja_file_run_ninja_build)r]  r^  r.  r_  r`  ra  rb  rc  rd  re  r   build_file_paths               rA   rk  rk    s     !H.x8M734	ggll?MBO*?*;3?cjjQ)5 $3::6 =>r_   r  c
                    t                t               }
t        |
       |t        t	        t
        |            }t        |xs g |||	      }t        j                  j                  |d      }|rt        d| dt        j                         t        || ||xs g |xs g |xs g |xs g ||		       |rt        d|  dt        j                         t        ||d|  d	       y )
Nry  rz  rg  rH   )	rO   r   r]  r
  r  r  r  re  r  zBuilding extension module zError building extension 'rG   r~  )r7   r9   r.   r   r   rI  _prepare_ldflagsrJ   rO   rn   rX   rY   rZ   "_write_ninja_file_to_build_libraryr  )r   r]  r
  r  r  r  rc  rd  re  r  r   r  s               rA   rp  rp     s     !H.x8M734	$	M
 ggll?MBO*?*;3?cjjQ '!'R+1r#)r/52#	% *4&43::F1$q9;r_   c                  j    	 t        j                  dj                                y# t        $ r Y yw xY w)zxReturn ``True`` if the `ninja <https://ninja-build.org/>`_ build system is available on the system, ``False`` otherwise.zninja --versionTF)r   r   r   r   r   r_   rA   r6   r6   /  s8     1 7 7 9:   s   #& 	22c                  .    t               st        d      y)zRaise ``RuntimeError`` if `ninja <https://ninja-build.org/>`_ build system is not available on the system, does nothing otherwise.z(Ninja is required to load C++ extensionsN)r6   r   r   r_   rA   r7   r7   9  s    EFF  r_   c                 :   t         rt        j                  j                  t        j
                  d      }| j                  d       |r| j                  d       | j                  d       |r"| j                  d       | j                  d       | j                  d       | j                  dt                |s| j                  d	       | j                  d|        n| j                  d
t                | j                  d       |r| j                  t        rdnd       | j                  d       |r| j                  t        rdnd       | j                  d       |s| j                  d       |r| j                  dt                |r|rt        dt        j                         t         rn| j                  dt        dd              | j                  d       t        7| j                  dt        j                  j                  t        dd              | S t        sd}t        j                  j                  t        |            s*t        j                  j                  t        d            rd}| j                  d
t        |              | j                  d       t        6| j                  d
t        j                  j                  t        d              | S t        r.| j                  d
t        d              | j                  d       | S )Nlibszc10.libzc10_cuda.libztorch_cpu.libztorch_cuda.libz"-INCLUDE:?warp_size@cuda@at@@YAHXZz	torch.libz	/LIBPATH:ztorch_python.libr  z-lc10z	-lc10_hipz
-lc10_cudaz-ltorch_cpuz-ltorch_hipz-ltorch_cudaz-ltorchz-ltorch_pythonz-Wl,-rpath,z%Detected CUDA files, patching ldflagsrH   r   r   z
cudart.libr  z-lcudartz
-lamdhip64)rQ   rJ   rO   rn   rY   base_exec_prefixr-  r  rJ  rX   rZ   rK  rz   rT   rq   )r  re  rd  r  python_lib_pathextra_lib_dirs         rA   r  r  ?  s   '',,s';';VDY'  0_-  !12   !EF[)y(89:  !34  9_,=!>? 	r.!123W%  0@lS]+  2BWY'  !12  ;~.>!?@9

K  9_UE-J,K!LM  .%$$yj%QV1W0X%YZ  "#MGGNN?=#ABGGNN?5#9: !&  2om&D%E!FG  ,%$$r"'',,z7*K)L%MN    2oe&<%=!>?  .r_   r.  c           	         | | D ]  }d|v rd|v sg c S  t        j                  g d      }g d}||D cg c]  }|dz   	 c}z   }t        j                  j	                  dd      }|st        j                  d       g }t        t        j                  j                               D ]  }t        j                  j                  |      }	t        j                  j                         D 
cg c]#  }
d	|
v rt        |
j                  d
      d         % }}
t        d |D              }t!        ||	      }	|	d    d|	d    }
|
|vs|j#                  |
        t%        |      }|dxx   dz  cc<   nM|j'                  dd      }|j)                         D ]  \  }}|j'                  ||      } |j                  d      }g }|D ]r  }
|
|vrt+        d|
 d      |
d   |
dd j                  d      d   z   }|j#                  d| d|        |
j-                  d      s\|j#                  d| d|        t t%        t/        |            S c c}w c c}
w )ad  
    Determine CUDA arch flags to use.

    For an arch, say "6.1", the added compile flag will be
    ``-gencode=arch=compute_61,code=sm_61``.
    For an added "+PTX", an additional
    ``-gencode=arch=compute_xx,code=compute_xx`` is added.

    See select_compute_arch.cmake for corresponding named and supported arches
    when building with CMake.
    NTORCH_EXTENSION_NAMEarch))zKepler+Tesla3.7)Keplerz3.5+PTX)zMaxwell+Tegra5.3)Maxwellz5.0;5.2+PTX)Pascalz6.0;6.1+PTX)zVolta+Tegra7.2)Voltaz7.0+PTX)Turingz7.5+PTX)zAmpere+Tegra8.7)Amperez8.0;8.6+PTX)Adaz8.9+PTX)Hopperz9.0+PTX)z3.5r  z5.0z5.2r  z6.0z6.1z6.2z7.0r  z7.5z8.0z8.6r  z8.9z9.0z9.0az+PTXTORCH_CUDA_ARCH_LISTzTORCH_CUDA_ARCH_LIST is not set, all archs for visible cards are included for compilation. 
If this is not desired, please set os.environ['TORCH_CUDA_ARCH_LIST'].sm_r   r   c              3   0   K   | ]  }|d z  |d z  f  yw)r   Nr   )ru   sms     rA   rw   z'_get_cuda_arch_flags.<locals>.<genexpr>  s     "N2B"Hb2g#6"Ns   r   rx   r  r>   ;zUnknown CUDA arch (z) or GPU not supportedry   +z-gencode=arch=compute_z	,code=sm_z,code=compute_)collectionsOrderedDictrJ   rK   rL   r   r   rA  rU   rV   device_countget_device_capabilityget_arch_listrt   r   maxminr-  sortedr(  rW  r   endswithr  )r.  r)  named_archessupported_archesr  valid_arch_strings
_arch_list	arch_listrD  
capabilityr  supported_smmax_supported_sm
named_archarchvalflagsnums                    rA   r;  r;  z  s     	D%-~			 ** , LX)AQ,RAQZ,RR  6=J U	V 	 uzz..01 	'A99!<J(-

(@(@(BU $etm  

3 23 UL U""N"NN
 -z:J m_Ajm_5D9$  &	' 9%	"  ''S1
#/#5#5#7 	AJ#++J@J	A $$S)	E P))24&8NOPPq'DHNN3/22CLL1#iuEF}}V$5cU.NOP #e*e -S$Us   I+(Ic                 Z   | | D ]  }d|v sd|v sdgc S  t         j                  j                  dd       }|s4t        j                  j                         }|r|j                         }n$g }n!|j                  dd      j                  d      }|D cg c]  }d| 	 }}|dgz  }|S c c}w )Nzamdgpu-targetzoffload-archz-fno-gpu-rdcPYTORCH_ROCM_ARCHr>   r  z--offload-arch=)rJ   rK   rL   rU   r  _cuda_getArchFlagsr   r(  )r.  r)  _archs	archFlagsarchsr  r  s          rA   rP  rP    s      	(D$&.D*@&''	(
 ZZ^^/6FHH//1	OO%EEsC(..s3278$tf%8E8	nEL 9s   B(r   c                    t         j                  j                  d      }|t               }t        j
                  j                  dn,dt        j
                  j                  j                  dd       }dt        j                  j                   t        j                  j                   }| d| }t         j                  j                  ||      }|rt        d| d	t        j                  
       t         j                  j                  ||       }t         j                  j!                  |      s8|rt        d| dt        j                  
       t        j"                  |d       |S )NTORCH_EXTENSIONS_DIRcpucurx   r   pyr   zUsing z as PyTorch extensions root...rH   zCreating extension directory rg  T)r2  )rJ   rK   rL   r,   rU   rg   rV   r(  rY   version_infor   minorrO   rn   rX   rZ   rT   makedirs)r   rd  root_extensions_directorycu_strpython_versionbuild_folderrc  s          rA   r	  r	    s(    "

/E F ($:$<! ==--5%u}}))11#r:;< 	c..445c6F6F6L6L5MN()6(3$&GGLL%|%5! 011OPWZWaWabggll#<dCO77>>/*1/1B#FSZZX
Od3r_   c                     t         j                  j                  d      }|<|j                         r,| rt	        d| dt
        j                         t        |      S | rt	        dt
        j                         y )NMAX_JOBSzUsing envvar MAX_JOBS (z) as the number of workers...rH   zqAllowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N))rJ   rK   rL   isdigitrX   rY   rZ   rt   )rd  max_jobss     rA   _get_num_workersr    sk    zz~~j)H 0 0 2+H:5RSzz#8} M::	 r_   r  c                    ddg}t        |      }||j                  dt        |      g       t        j                  j                         }t        rd|vrddlm} |j                  j                         }t        |   }|j                  j                  |      }	|	j                         D 
ci c]  \  }
}|
j                         | }	}
}|j                         D ]  \  }
}|
j                         }||	vs||	|<   ! |	}	 t         j"                  j%                          t         j&                  j%                          d}t)        j*                  ||r|nt(        j,                  t(        j.                  | d|	       y c c}}
w # t(        j0                  $ rf}t!        j2                         \  }}}|}t5        |d
      r/|j6                  r#|d |j6                  j8                  t:          z  }t=        |      |d }~ww xY w)Nninjar   z-jr  r   )	distutilsr   T)stdoutrZ   cwdcheckr   outputr   )r  r=  r   rJ   rK   r   rQ   r  r  utilget_platformPLAT_TO_VCVARS_msvccompiler_get_vc_envrW  upperrY   r  flushrZ   r   runPIPEr   r?  r   r  r  r   r   r   )rc  rd  r  commandnum_workersr   r  	plat_name	plat_specvc_envkrv   ukstdout_filenor  r   r   messages                     rA   r  r    s   oG"7+Kc+./0
**//
C*#5(NN//1	"9-	((44Y?+1<<>:41a!'')Q,::IIK 	DAqBr
	  +



 $+=$$	- ;: (( 	+lln5! 5(#/ELL//1GHIJJG7#*	+s    -E: A9F   G9A!G44G9c                    t         rt        t        j                  dd      j	                  d      vrgt        d t        j                  dd      j	                  d      D              }|s0t         dt        j                  dd       t        j                  d<   t        j                  j                  ||  t               S )NPATHr   r  c              3      K   | ]I  }t         j                  j                  |      xr$ t         j                  j                  |t               K y wrs   )rJ   rO   rT   samefiler  )ru   ps     rA   rw   z!_get_exec_path.<locals>.<genexpr>M  s>       
 GGNN1E"''"2"21n"EE 
s   AA)
rQ   r  rJ   r<  r   r   rK   rO   rn   EXEC_EXT)module_namerO   torch_lib_in_paths      rA   rs  rs  K  s    nBIIfb,A,G,G,LL  
YYvr*005 
 
 !$2#31RYYvr5J4K!LBJJv77<<hZ899r_   c                    t         j                  j                  ||  t               }|rt        j
                  j                  | |      }|J t        j
                  j                  |      }t        |j                  t        j                  j                        sJ |j                  j                  |       |S t        j                  j                  |       |S rs   )rJ   rO   rn   LIB_EXT	importlibr  spec_from_file_locationmodule_from_specrM  loaderabcLoaderexec_modulerU   opsload_library)r  rO   r  filepathspecmodules         rA   rt  rt  V  s    ww||D[M'";<H~~55k8L006$++y}}';';<<<'		x(r_   c	                    |D 	cg c]  }	|	j                          }}	|D 	cg c]  }	|	j                          }}	|D 	cg c]  }	|	j                          }}	|D 	cg c]  }	|	j                          }}	|D 
cg c]!  }
t        j                  j                  |
      # }}
t	              }t        j                  dt        rdnd      }||j                  |       g }|s%|j                  d|        |j                  d       |t               D cg c]  }|  c}z  }t        r|||z   D cg c]  }d| 	 c}z  }nR||D cg c]  }dt        j                  |        c}z  }||D cg c]  }dt        j                  |        c}z  }|t               D cg c]  }|  c}z  }t        r|t        z   d	gz   |z   }t        |      }n
|d
dgz   |z   }r.t        r(dg|z   t         z   t"        z   }||z  }|t%        |      z  }nr|t&        z   t)               z   }t        rGt        D ]	  }	d|	g|z   } t*        D ]  }dd|z   g|z   } |dgz   }t        |      }|t        |      z  }nP|ddgz  }||z  }t-        d |D              s|j                  d       t        j.                  d      }|
d|g|z   }nd }dt0        dt0        ffd}|D cg c]
  } ||       }}|rg nt2        g|z   }t4        r|j                  d       nt        rt        |      }|rt6        nt8        }| | }t;        | |d |d d ||||       y c c}	w c c}	w c c}	w c c}	w c c}
w c c}w c c}w c c}w c c}w c c}w c c}w )Nr  ntposix_prefix)schemer  r  rB  z	-isystem z
/std:c++17r   r  z
-DWITH_HIPr  r  r  r3  r4  c              3   >   K   | ]  }|j                  d         yw)z-std=Nr(  r8  s     rA   rw   z5_write_ninja_file_to_build_library.<locals>.<genexpr>  s     GDtw/Gr9  r5  r7  r   r<   c                     t         j                  j                  t         j                  j                  |             d   }t	        |       r	r| d}|S | d}|S )Nr   z.cuda.oz.o)rJ   rO   r  rf   rI  )r   	file_nametargetre  s      rA   object_file_pathz<_write_ninja_file_to_build_library.<locals>.object_file_path  s]    GG$$RWW%5%5k%BCAF	%) "{'*F  "{"%Fr_   z-undefined dynamic_lookupr{  )r   rJ   rO   rC  r2   rC  rD  rQ   r-  r  ri  rj  r  r  rB   rJ  rQ  rO  rP  r:  r;  r  r   r<  r   SHARED_FLAGr   r  r  r  )rO   r   r]  r
  r  r  r  re  r  r)  rI   user_includessystem_includespython_include_pathrq  r  r  r.  
cuda_flagsr  cc_envr  rS  r^  r|  rT  r}  s          `                   rA   r  r  e  s    .::TDJJL:L:2CD$DD.;<dTZZ\<M<4GHD4::<HH 8KKtRWW__T*KMK $I.O $,,YztWef&23M6tf=>>?&C&EFfFFM 8WXWBwi.XX]S'Bu{{7345SSO\Iekk'&:%;<\\&B&DEfEEM!22l^ClR''<!88<G%"^f,/??BTT
''
*:66
	"%669M9OO
) ?*D1J>
?"= \(*<~*MNQ[[
\#|n4J'
3J.):;;J/;;J++JGJGG!!,/YYt_F!&/*<

	c 	c 	 188$8G8"rFG 23	 )#(CvcU^N#%q ;D<H L  G YS\EX 9s@   M
M MM:&M
M#2M(	M-2M2#
M7M<c           	      	   d } ||      } ||      } ||      } ||      } ||      } ||      }t        |      t        |      k(  sJ t        |      dkD  sJ t               }dg}|j                  d|        |
s|r[dt        j                  v rt        j
                  d      }nt        rt        dd      }nt        dd      }|j                  d	|        t        r	t        |z   }d
dj                  |       g}|j                  ddj                  |              |
rF|j                  ddj                  |              |j                  ddj                  |              |j                  ddj                  |              |j                  ddj                  |              |D cg c]!  }t        j                  j                  |      # }}dg}t        r#|j                  d       |j                  d       n3|j                  d       |j                  d       |j                  d       |
rqdg}d}t        j                  j                   =t        j
                  dd      dk7  r$|j                  d       |j                  d       d}|j                  d| d       g }t#        ||      D ]  \  }}t%        |      xr |
}|rd nd!}t        r$|j'                  d"d#      }|j'                  d"d#      }|j'                  dd$      }|j'                  dd$      }|j                  d%| d&| d|         |rst        j                  j                  t        j                  j)                  |d         d'      }d(g}|j                  d)       d%| d*dj                  |       g}||gz  }ng g }}|	d+g}t        r t+        j,                  d,d-g      j.                  t0         j3                  d.      }t        |      d/k\  r3t        j                  j)                  |d         j'                  d"d#      }nt5        d0      |j                  d1| d2       n|j                  d3       d%|	 d4dj                  |       g}d5|	 g} ng g g } }}|||g}!|
r|!j                         |!|||||| gz  }!d6j                  d7 |!D              }"|"d8z  }"t7        | |"       yc c}w )9a  Write a ninja file that does the desired compiling and linking.

    `path`: Where to write this file
    `cflags`: list of flags to pass to $cxx. Can be None.
    `post_cflags`: list of flags to append to the $cxx invocation. Can be None.
    `cuda_cflags`: list of flags to pass to $nvcc. Can be None.
    `cuda_postflags`: list of flags to append to the $nvcc invocation. Can be None.
    `sources`: list of paths to source files
    `objects`: list of desired paths to objects, one per source.
    `ldflags`: list of flags to pass to linker. Can be None.
    `library_target`: Name of the output library. Can be None; in that case,
                      we do no linking.
    `with_cuda`: If we should be compiling with CUDA.
    c                 P    | g S | D cg c]  }|j                          c}S c c}w rs   )r   )r  r)  s     rA   sanitize_flagsz)_write_ninja_file.<locals>.sanitize_flags  s&    =I-23TDJJL333s   #r   zninja_required_version = 1.3zcxx = PYTORCH_NVCCr   rc   rF   znvcc = z	cflags = r>   zpost_cflags = zcuda_cflags = zcuda_post_cflags = zcuda_dlink_post_cflags = z
ldflags = zrule compilez@  command = cl /showIncludes $cflags -c $in /Fo$out $post_cflagsz  deps = msvczD  command = $cxx -MMD -MF $out.d $cflags -c $in -o $out $post_cflagsz  depfile = $out.dz  deps = gcczrule cuda_compiler   N*TORCH_EXTENSION_SKIP_NVCC_GEN_DEPENDENCIESr   r   z?--generate-dependencies-with-compile --dependency-output $out.dz  command = $nvcc z. $cuda_cflags -c $in -o $out $cuda_post_cflagscuda_compiler   :z$:z$ zbuild r   zdlink.ozrule cuda_devlinkz5  command = $nvcc $in -o $out $cuda_dlink_post_cflagsz: cuda_devlink z	rule linkwherer   z
r   z'MSVC is required to load C++ extensionsz  command = "z)/link.exe" $in /nologo $ldflags /out:$outz%  command = $cxx $in $ldflags -o $outz: link zdefault z

c              3   >   K   | ]  }d j                  |        yw)rT  N)rn   )ru   bs     rA   rw   z$_write_ninja_file.<locals>.<genexpr>c	  s     71$))A,7r9  rT  )rS   r9   r-  rJ   rK   r<  rJ  rq   rK  rQ  rn   rO   rC  rQ   rU   rg   rV   ziprI  r(  rP   r   r   r   r   r   r   r   )#rO   r.  r_  r`  ra  rb  r]  r^  r|  r}  re  r  r   configrF   r  rI   compile_rulecuda_compile_rulenvcc_gendepsbuildr   object_fileis_cuda_sourceruledevlink_outdevlink_ruledevlink	link_rulecl_pathscl_pathlinkdefaultblocksr   s#                                      rA   r  r    s   24 F#F -K -K%&67+,BCW%G w<3w<'''w<!!H --F
MMF8*%&*RZZ'99^,D&ug6&uf5v&'&4&)*+,E	LL>#((;"7!89:~chh{&;%<=>*3884D+E*FGH	LL,SXX6L-M,NOP	LL:chhw/012 299rwwt$9G9 ##LN	PO,R	T01N+01 ==)bii8dfi.jnq.q$$%9:$$^4 ]L   .\]	_ E$'$9 C [&{3A	!/~Y%--c48K%--c48K!))#t4!))#t4vk]"TF!K=ABC ggll277??71:#>	J+,STK=8I7JKLK=  "Bg! M	>z..0406 77=v?UWW\W\]cWd 8}!''//(1+6>>sDI"#LMM}WI5^_`DE(0A/BCDn-./#%r24	 e\*F'(
|YwgFFFkk777GtOGw[ :s   6&S#c                  l    t         t        d      t        j                  j                  t         g|  S )z
    Join paths with CUDA_HOME, or raises an error if it CUDA_HOME is not set.

    This is basically a lazy way of raising an error for missing $CUDA_HOME
    only once we need to get any CUDA-specific path.
    zSCUDA_HOME environment variable is not set. Please set it to your CUDA install root.)rD   rm   rJ   rO   rn   ro   s    rA   rK  rK  h	  s7      A B 	B77<<	*E**r_   rO   c                     ddg}t         r|j                  d       t        j                  j	                  |       d   |v S )Nr  r"  r#  r   )rJ  r-  rJ   rO   r  )rO   	valid_exts     rA   rI  rI  u	  s;    I 77D!!$	11r_   )F)
NNNNNFNTFT)NNNNNNNFNTTTF)Trs   r  )r   rR   r  importlib.abcrJ   r   ri  rM   r  r   rY   rC  r   r  pathlibr   r4  rU   torch._appdirs
file_batonr   _cpp_extension_versionerr   r  r   hipify.hipify_pythonr   typingr	   r
   r   r   r   torch.torch_versionr   r   setuptools.command.build_extr   r   rQ   r   r   r   r  r  CLIB_PREFIXCLIB_EXTr  rO   rC  __file___HERErP   r  rn   r  r   r   r   rt   VersionRanger   
VersionMapr'   __annotations__MINIMUM_CLANG_VERSIONr+   __all__rB   r^   rk   rq   r   r   r   r   r   ra   r  rg   rd   rJ  ROCM_VERSIONr   r   rV   _is_compiledrD   rK   rL   rz   r   r   r  r  r:  rQ  rO  rl  r  r9   boolr   r   r   r,   r-   r.   r   r/   r0   r1   r2   r3   r4   r  r  r:   rL  r8   r5   r  rk  rp  r6   r7   r  r;  rP  r	  r  r  rs  rt  r  r  rK  rI  r   r_   rA   <module>r@     s       	 	     
        ! 8 ! 6 5 5 5 2\\W$
<<""8,<<""7+&E6RbE6U"f	
!ggoobggooe45k51 &0R  % U38_eCHo56#|#$
 !'* '* '* '*    	! : 	 " "G,"G,"G,"G,"G,"G,"G,# Z 
	?$s), 	?c 	?# 6# 0+s +   "  v   	%.?5!D&29J9J9V4^c == J):):)@)@)Ebq)IJJL!&!8!8!:O	ZZ^^L)IRZZ^^L-I
 %/BJJ/G$H ! S      -.  
R$ Ri$s) i' W W)S )T )X14E$BT<U 14h6s 6l 6t 6rVsY Vsr0@fi@X$ $c $N! !c !L !%) h/T#Y'h/ TNh/T(6 wGr	0 "!"&"$( $!%,0#'V/H %)UP #&UP UP %TNUP .2UPp(>c(> (> (> D>(> '+(>j $,;c,; ,; ,; D>,; ,; )-,;^G8vZ$s)!4 ZS	 Zz$s)!4 S	 ,s T c 2d x} 4+c 4+D 4+ 4+PT 4+n:l :>l^R  %)R h
+s 
+2 2 2r_   