3
I4W^n½  ã            
   @   sð  d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ d dlmZ ejdkZejdd	ƒjƒ d
kZdd„ ZdMZdNZdZ dZ!eƒ Z"ej#j$dƒ� pøej#j$dƒZ%e	j&dƒZ'dddgZ(ddddgZ)eƒ Z*dd„ Z+dd „ Z,d!d"„ Z-d#d$„ Z.d%d&„ Z/G d'd(„ d(ee0ƒZ1d)d*„ Z2d+d,„ Z3dOd.d/„Z4dPd0d1„Z5dQd3d4„Z6dRd5d6„Z7d7d8„ Z8d9d:„ Z9d;d<„ Z:d=d>„ Z;dSd?d@„Z<dAdB„ Z=dCdD„ Z>dEdF„ Z?dGdH„ Z@dIdJ„ ZAdKdL„ ZBdS )Té    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNé   )Ú	FileBaton)ÚExtensionVersioner)Ú	build_extÚwin32ÚBUILD_NAMEDTENSORÚ Ú1c              C   sÒ   t jjdƒpt jjdƒ} | dkr°y<tr*dnd}tj|dgƒjƒ jdƒ}t jj	t jj	|ƒƒ} W nR t
k
r®   tr–tjdƒ}t|ƒd	krŒd
} qš|d	 } nd} t jj| ƒsªd} Y nX | rÎtjjƒ  rÎtdj| ƒƒ | S )zFinds the CUDA install path.Ú	CUDA_HOMEZ	CUDA_PATHNÚwhereÚwhichÚnvccz
z7C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v*.*r   r   z/usr/local/cudaz.No CUDA runtime is found, using CUDA_HOME='{}')ÚosÚenvironÚgetÚ
IS_WINDOWSÚ
subprocessÚcheck_outputÚdecodeÚrstripÚpathÚdirnameÚ	ExceptionÚglobÚlenÚexistsÚtorchÚcudaZis_availableÚprintÚformat)Z	cuda_homer   r   Z
cuda_homes© r$   ú:/tmp/pip-build-e66j97b2/torch/torch/utils/cpp_extension.pyÚ_find_cuda_home   s(    

r&   é   é	   é   é—^  a:  

                               !! WARNING !!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Your compiler ({}) may be ABI-incompatible with PyTorch!
Please use a compiler that is ABI-compatible with GCC 4.9 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 4.9 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 !!
Ú
CUDNN_HOMEZ
CUDNN_PATHz\d+\.\d+\.\d+\w+\+\w+z/MDz/wd4819z/EHscz-D__CUDA_NO_HALF_OPERATORS__z-D__CUDA_NO_HALF_CONVERSIONS__z-D__CUDA_NO_HALF2_OPERATORS__z--expt-relaxed-constexprc               C   s   t jtjjƒ S )N)Ú!BUILT_FROM_SOURCE_VERSION_PATTERNÚmatchr    ÚversionÚ__version__r$   r$   r$   r%   Ú_is_binary_buildm   s    r0   c               C   s    t jjdƒrddgS ddddgS )NÚdarwinzclang++Zclangzg++Úgcczgnu-c++zgnu-cc)ÚsysÚplatformÚ
startswithr$   r$   r$   r%   Ú _accepted_compilers_for_platformq   s    r6   c               C   s   t jjt jjtjƒ dƒƒS )ad  
    Returns 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``.
    Ztorch_extensions)r   r   ÚrealpathÚjoinÚtempfileÚ
gettempdirr$   r$   r$   r%   Úget_default_build_rootv   s    
r;   c                sH   t rdS tjd| gtjd�}tjj|jƒ jƒ ƒ‰ t	‡ fdd„t
ƒ D ƒƒS )a  
    Verifies that the compiler is the expected one for the current platform.

    Arguments:
        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.
    Tr   )Ústderrc             3   s   | ]}|ˆ kV  qd S )Nr$   )Ú.0Úname)Úcompiler_pathr$   r%   ú	<genexpr>“   s    z1check_compiler_ok_for_platform.<locals>.<genexpr>)r   r   r   ÚSTDOUTr   r   r7   r   ÚstripÚanyr6   )Úcompilerr   r$   )r?   r%   Úcheck_compiler_ok_for_platformƒ   s
    rE   c             C   sB  t ƒ s
dS tjjdƒdkrdS t| ƒsHtjtj| t	ƒ d t
jd	�ƒ d
S t
jjdƒrXdS ytt
jjdƒrŽt}tj| ddgƒ}|jƒ jƒ jdƒ}n<t}tj| tjd�}tjd|jƒ jƒ ƒ}|dkrÂdn|jƒ }W n6 tk
�r   t
jƒ \}}}tjdj| |ƒƒ d
S X ttt|ƒƒ|k�rdS dj| dj|ƒƒ} tjtj| ƒƒ d
S )aE  
    Verifies that the given compiler is ABI-compatible with PyTorch.

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

    Returns:
        False if the compiler is (likely) ABI-incompatible with PyTorch,
        else True.
    TZTORCH_DONT_CHECK_COMPILER_ABIÚONr   ÚYESÚTRUEÚYr   )Zuser_compilerZpytorch_compilerr4   Fr1   Úlinuxz-dumpfullversionz-dumpversionÚ.)r<   z(\d+)\.(\d+)\.(\d+)Nz*Error checking compiler version for {}: {}z{} {})rF   r   rG   rH   rI   )r   r   r   )r0   r   r   r   rE   ÚwarningsÚwarnÚWRONG_COMPILER_WARNINGr#   r6   r3   r4   r5   ÚMINIMUM_GCC_VERSIONr   r   r   rB   ÚsplitÚMINIMUM_MSVC_VERSIONrA   ÚreÚsearchÚgroupsr   Úexc_infoÚtupleÚmapÚintr8   ÚABI_INCOMPATIBILITY_WARNING)rD   Zminimum_required_versionr.   Zcompiler_infor-   Ú_Úerrorr$   r$   r%   Ú check_compiler_abi_compatibility–   s<    r\   c                   s`   e Zd ZdZedd„ ƒZ‡ fdd„Zdd„ Z‡ fdd	„Zd
d„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚBuildExtensiona}  
    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++11``) 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.
    c                s   ‡ ‡fdd„}|S )zœ
        Returns an alternative constructor that extends any original keyword
        arguments to the original constructor with the given options.
        c                 s   |j ƒ }|jˆƒ ˆ | |ŽS )N)ÚcopyÚupdate)ÚargsÚkwargs)ÚclsÚoptionsr$   r%   Úinit_with_optionsã   s    
z6BuildExtension.with_options.<locals>.init_with_optionsr$   )rb   rc   rd   r$   )rb   rc   r%   Úwith_optionsÝ   s    zBuildExtension.with_optionsc                s$   t t| ƒj||Ž |jddƒ| _d S )NÚno_python_abi_suffixF)Úsuperr]   Ú__init__r   rf   )Úselfr`   ra   )Ú	__class__r$   r%   rh   é   s    zBuildExtension.__init__c                sâ   ˆj ƒ  x>ˆjD ]4}ˆj|dƒ tr0ˆj|dƒ ˆj|ƒ ˆj|ƒ qW ˆj jddg7  _ˆjjdkrŽˆj j	ddg7  _	ˆjj
‰ ˆjj‰nˆjj‰ ‡ ‡fdd„}d‡ ‡‡fd	d
„	}ˆjjdkrÌ|ˆj_
n|ˆj_tjˆƒ d S )Nz-DTORCH_API_INCLUDE_EXTENSION_Hz-DBUILD_NAMEDTENSORz.cuz.cuhZmsvcc       	         sÈ   t j|ƒ}z¨ˆjj}t|ƒrptddƒ}t|tƒs6|g}ˆjjd|ƒ t|t	ƒrV|d }t
ddg | t|ƒ }nt|t	ƒr‚|d }tdd„ |D ƒƒsž|jd	ƒ ˆ | |||||ƒ W d ˆjjd|ƒ X d S )
NÚbinr   Úcompiler_soz--compiler-optionsz'-fPIC'Úcxxc             s   s   | ]}|j d ƒV  qdS )z-std=N)r5   )r=   Úflagr$   r$   r%   r@     s    zMBuildExtension.build_extensions.<locals>.unix_wrap_compile.<locals>.<genexpr>z
-std=c++11)r^   ÚdeepcopyrD   rl   Ú_is_cuda_fileÚ_join_cuda_homeÚ
isinstanceÚlistZset_executableÚdictÚCOMMON_NVCC_FLAGSÚ_get_cuda_arch_flagsrC   Úappend)	ÚobjÚsrcÚextZcc_argsÚextra_postargsZpp_optsÚcflagsZoriginal_compilerr   )Úoriginal_compileri   r$   r%   Úunix_wrap_compile   s"    





z:BuildExtension.build_extensions.<locals>.unix_wrap_compiler   c       	         sL   t j|ƒˆ_d }‡‡fdd„}z|ˆj_ˆ | |||||||ƒS ˆˆj_X d S )Nc       	         sp  t jdƒ‰dd„ ‡fdd„| D ƒD ƒ}t jdƒ‰dd„ ‡fdd„| D ƒD ƒ}t jd	ƒ‰ d
d„ ‡ fdd„| D ƒD ƒ}t|ƒdkoˆt|ƒdk�rh|d }|d }t|ƒ�r"tddƒ}tˆjtƒrÈˆjd }ntˆjtƒrÜˆj}ng }t	| t
|ƒ }xtD ]}d|g| }qöW |d|d|g| | } nFtˆjtƒ�rHtˆjd  }| |7 } n tˆjtƒ�rhtˆj }| |7 } ˆ| ƒS )Nz/T(p|c)(.*)c             S   s   g | ]}|r|j d ƒ‘qS )é   )Úgroup)r=   Úmr$   r$   r%   ú
<listcomp>*  s    z\BuildExtension.build_extensions.<locals>.win_wrap_compile.<locals>.spawn.<locals>.<listcomp>c             3   s   | ]}ˆ j |ƒV  qd S )N)r-   )r=   Úelem)Ú	src_regexr$   r%   r@   *  s    z[BuildExtension.build_extensions.<locals>.win_wrap_compile.<locals>.spawn.<locals>.<genexpr>z/Fo(.*)c             S   s   g | ]}|r|j d ƒ‘qS )r   )r€   )r=   r�   r$   r$   r%   r‚   0  s    c             3   s   | ]}ˆ j |ƒV  qd S )N)r-   )r=   rƒ   )Ú	obj_regexr$   r%   r@   0  s    z((\-|\/)I.*)c             S   s   g | ]}|r|j d ƒ‘qS )r   )r€   )r=   r�   r$   r$   r%   r‚   6  s   c             3   s   | ]}ˆ j |ƒV  qd S )N)r-   )r=   rƒ   )Úinclude_regexr$   r%   r@   7  s    r   r   rk   r   z
-Xcompilerz-cz-orm   )rR   Úcompiler   rp   rq   rr   r|   rt   rs   ru   rv   ÚCOMMON_MSVC_FLAGS)	ÚcmdZsrc_listZobj_listZinclude_listry   rx   r   r|   rn   )Úoriginal_spawnri   )r†   r…   r„   r%   Úspawn&  s8    







zHBuildExtension.build_extensions.<locals>.win_wrap_compile.<locals>.spawn)r^   ro   r|   rD   r‹   )	ÚsourcesÚ
output_dirÚmacrosÚinclude_dirsÚdebugZextra_preargsr{   Údependsr‹   )r}   rŠ   ri   r$   r%   Úwin_wrap_compile  s    	-z9BuildExtension.build_extensions.<locals>.win_wrap_compile)NNNr   NNN)Ú
_check_abiÚ
extensionsÚ_add_compile_flagr   Ú_define_torch_extension_nameÚ_add_gnu_cpp_abi_flagrD   Zsrc_extensionsÚcompiler_typeZ_cpp_extensionsr‡   r‹   Ú_compiler	   Úbuild_extensions)ri   Ú	extensionr~   r’   r$   )r}   rŠ   ri   r%   rš   í   s2    

      ;
zBuildExtension.build_extensionsc                sP   t t| ƒj|ƒ}| jrLtjdkrL|jdƒ}|d d… |dd …  }dj|ƒ}|S )	Né   r   rK   r   r   )rœ   r   éþÿÿÿéÿÿÿÿ)rg   r]   Úget_ext_filenamerf   r3   Úversion_inforP   r8   )ri   Zext_nameZext_filenameZext_filename_partsZwithout_abi)rj   r$   r%   rŸ   c  s    

zBuildExtension.get_ext_filenamec             C   sH   t | jdƒr| jjd }n"tr.tjjddƒ}ntjjddƒ}t|ƒ d S )NÚcompiler_cxxr   ÚCXXÚclzc++)ÚhasattrrD   r¡   r   r   r   r   r\   )ri   rD   r$   r$   r%   r“   t  s    zBuildExtension._check_abic             C   sJ   t j|jƒ|_t|jtƒr:x*|jjƒ D ]}|j|ƒ q&W n|jj|ƒ d S )N)r^   ro   Úextra_compile_argsrr   rt   Úvaluesrw   )ri   r›   rn   r`   r$   r$   r%   r•   ~  s
    z BuildExtension._add_compile_flagc             C   s.   |j jdƒ}|d }dj|ƒ}| j||ƒ d S )NrK   r   z-DTORCH_EXTENSION_NAME={}rž   )r>   rP   r#   r•   )ri   r›   Únamesr>   Zdefiner$   r$   r%   r–   †  s    
z+BuildExtension._define_torch_extension_namec             C   s    | j |dtttjjƒƒ ƒ d S )Nz-D_GLIBCXX_USE_CXX11_ABI=)r•   ÚstrrX   r    Ú_CÚ_GLIBCXX_USE_CXX11_ABI)ri   r›   r$   r$   r%   r—   �  s    z$BuildExtension._add_gnu_cpp_abi_flag)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodre   rh   rš   rŸ   r“   r•   r–   r—   Ú__classcell__r$   r$   )rj   r%   r]   Î   s   v

r]   c             O   s˜   |j dg ƒ}|tƒ 7 }||d< tr||j dg ƒ}|tƒ 7 }||d< |j dg ƒ}|jdƒ |jdƒ |jdƒ |jdƒ ||d< d|d	< tj| |f|ž|ŽS )
a(  
    Creates 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.

    Example:
        >>> 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']),
                ],
                cmdclass={
                    'build_ext': BuildExtension
                })
    r�   Úlibrary_dirsÚ	librariesÚc10r    Útorch_pythonr©   zc++Úlanguage)r   Úinclude_pathsr   Úlibrary_pathsrw   Ú
setuptoolsÚ	Extension)r>   rŒ   r`   ra   r�   r±   r²   r$   r$   r%   ÚCppExtension•  s    





rº   c             O   s´   |j dg ƒ}|tdd�7 }||d< |j dg ƒ}|jdƒ trn|jdƒ |jdƒ |jdƒ |jd	ƒ |jd
ƒ ||d< |j dg ƒ}|tdd�7 }||d< d|d< tj| |f|ž|ŽS )aù  
    Creates 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.

    Example:
        >>> 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']})
                ],
                cmdclass={
                    'build_ext': BuildExtension
                })
    r±   T)r!   r²   Zcudartr³   Zc10_cudar    r´   r©   r�   zc++rµ   )r   r·   rw   r   r¶   r¸   r¹   )r>   rŒ   r`   ra   r±   r²   r�   r$   r$   r%   ÚCUDAExtensionÂ  s"    





r»   Fc             C   sœ   t jjtƒ}t jjt jj|ƒƒ}t jj|dƒ}|t jj|ddddƒt jj|dƒt jj|dƒg}| r˜tdƒ}|dkr||j|ƒ tdk	r˜|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.
    Úincluder    ZcsrcÚapiZTHZTHCz/usr/includeN)	r   r   ÚabspathÚ__file__r   r8   rq   rw   r+   )r!   ÚhereÚ
torch_pathZlib_includeÚpathsZcuda_home_includer$   r$   r%   r¶   õ  s    

r¶   c             C   s¦   g }t r@tjjtƒ}tjjtjj|ƒƒ}tjj|dƒ}|j|ƒ | r¢t rNd}n*d}tjjt	|ƒƒ rxtjjt	dƒƒrxd}|jt	|ƒƒ t
dk	r¢|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.
    Úlibzlib/x64Úlib64N)r   r   r   r¾   r¿   r   r8   rw   r   rq   r+   )r!   rÂ   rÀ   rÁ   Úlib_pathÚlib_dirr$   r$   r%   r·     s"    

r·   Tc
       
      C   s4   t | t|tƒr|gn||||||p*t| |ƒ|||	ƒ
S )að  
    Loads 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``, loads it into the process
            as a plain dynamic library.

    Returns:
        If ``is_python_module`` is ``True``, returns the loaded PyTorch
        extension as a Python module. If ``is_python_module`` is ``False``
        returns nothing (the shared library is loaded into the process as a side
        effect).

    Example:
        >>> from torch.utils.cpp_extension import load
        >>> module = load(
                name='extension',
                sources=['extension.cpp', 'extension_kernel.cu'],
                extra_cflags=['-O2'],
                verbose=True)
    )Ú_jit_compilerr   r¨   Ú_get_build_directory)
r>   rŒ   Úextra_cflagsÚextra_cuda_cflagsÚextra_ldflagsÚextra_include_pathsÚbuild_directoryÚverboseÚ	with_cudaÚis_python_moduler$   r$   r%   Úload;  s    PrÑ   c             C   sŒ  |pt | |	ƒ}t|tƒr|g}|p$g }t|tƒr6|g}|jddƒ |dk	rÐ|jdƒ t|tƒrd|g}t|tƒr‚tdd„ |D ƒƒ}nt|tƒsžtdjt	|ƒƒƒ‚x&|j
ƒ D ]\}}|jdj||ƒƒ q¨W |jd	ƒ tjj|d
ƒ}t|dƒ�}|jdj|ƒƒ W dQ R X |g}|�rr|jddƒ |jddƒ |jddƒ tjj|dƒ}t|dƒ�}|jdj|ƒƒ W dQ R X |j|ƒ t| |||||||	|
|ƒ
S )aJ  
    Loads 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.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.

    Example:
        >>> 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'])
    r   z#include <torch/extension.h>Nz*PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {c             s   s   | ]}||fV  qd S )Nr$   )r=   Úfr$   r$   r%   r@   ñ  s    zload_inline.<locals>.<genexpr>z5Expected 'functions' to be a list or dict, but was {}zm.def("{0}", &{0}, "{1}");Ú}zmain.cppÚwÚ
z#include <torch/types.h>r   z#include <cuda.h>r   z#include <cuda_runtime.h>zcuda.cu)rÈ   rr   r¨   Úinsertrw   rs   rt   Ú
ValueErrorr#   ÚtypeÚitemsr   r   r8   ÚopenÚwriterÇ   )r>   Zcpp_sourcesZcuda_sourcesZ	functionsrÉ   rÊ   rË   rÌ   rÍ   rÎ   rÏ   rÐ   Zfunction_nameZ	docstringZcpp_source_pathZcpp_source_filerŒ   Zcuda_source_pathZcuda_source_filer$   r$   r%   Úload_inline˜  sX    F







rÜ   c
             C   sø   t j| ƒ}
t j| |||||g||d�}|dkr`||
krT|rTtdj| ƒdj|| ƒ ƒ dj| |ƒ} ||
krÈttjj|dƒƒ}|j	ƒ r¾z.t
| ||p�g |p–g |pœg |p¢g |||d�	 W d |jƒ  X qÚ|jƒ  n|rÚtdj| ƒƒ |rìtd	j| ƒƒ t| ||	ƒS )
N)Zbuild_argumentsrÍ   rÏ   r   z;The input conditions for extension module {} have changed. z5Bumping to version {0} and re-building as {1}_v{0}...z{}_v{}Úlock)	r>   rŒ   rÉ   rÊ   rË   rÌ   rÍ   rÎ   rÏ   zSNo modifications detected for re-loaded extension module {}, skipping build step...zLoading extension module {}...)ÚJIT_EXTENSION_VERSIONERÚget_versionZbump_version_if_changedr"   r#   r   r   r   r8   Ztry_acquireÚ_write_ninja_file_and_buildÚreleaseÚwaitÚ_import_module_from_library)r>   rŒ   rÉ   rÊ   rË   rÌ   rÍ   rÎ   rÏ   rÐ   Zold_versionr.   Zbatonr$   r$   r%   rÇ     sD    




rÇ   c	          
   C   sÀ   t ƒ  trtjjddƒ}	ntjjddƒ}	t|	ƒ |d krFttt|ƒƒ}t	|pNg ||ƒ}tj
j|dƒ}
|rvtdj|
ƒƒ t|
| ||p„g |pŠg |p�g |p–g |d� |r°tdj| ƒƒ t| ||ƒ d S )Nr¢   r£   zc++zbuild.ninjazEmitting ninja build file {}...)r   r>   rŒ   rÉ   rÊ   rË   rÌ   rÏ   zBuilding extension module {}...)Úverify_ninja_availabilityr   r   r   r   r\   rC   rW   rp   Ú_prepare_ldflagsr   r8   r"   r#   Ú_write_ninja_fileÚ_build_extension_module)r>   rŒ   rÉ   rÊ   rË   rÌ   rÍ   rÎ   rÏ   rD   Zbuild_file_pathr$   r$   r%   rà   L  s6    	rà   c              C   sT   t tjdƒ�>} ytjdjƒ | d� W n tk
r@   tdƒ‚Y nX dS W dQ R X dS )zr
    Returns ``True`` if the `ninja <https://ninja-build.org/>`_ build system is
    available on the system.
    Úwbzninja --version)Ústdoutz(Ninja is required to load C++ extensionsTN)rÚ   r   Údevnullr   Ú
check_callrP   ÚOSErrorÚRuntimeError)rê   r$   r$   r%   rä   v  s    rä   c             C   s.  t r–tjjtjƒ}tjj|dƒ}tjjtƒ}tjjtjj|ƒƒ}tjj|dƒ}| j	dƒ | j	dƒ | j	dƒ | j	dƒ | j	dj
|ƒƒ | j	dj
|ƒƒ |�r*|r¨tdƒ t rè| j	dj
td	ƒƒƒ | j	d
ƒ td k	ræ| j	tjjtd	ƒƒ nB| j	dj
tdƒƒƒ | j	dƒ td k	�r*| j	dj
tjjtdƒƒƒ | S )NZlibsrÃ   zc10.libz	torch.libztorch_python.libz_C.libz/LIBPATH:{}z%Detected CUDA files, patching ldflagszlib/x64z
cudart.libz-L{}rÄ   z-lcudart)r   r   r   r   r3   Ú
executabler8   r¾   r¿   rw   r#   r"   rq   r+   )rË   rÏ   rÎ   Zpython_pathZpython_lib_pathrÀ   rÁ   rÅ   r$   r$   r%   rå   „  s4    






rå   c             C   sH  | dk	r"x| D ]}d|krg S qW t jd'd(d)d*d+d,d-gƒ}dddddddddddg}|dd„ |D ƒ }tjjddƒ}|sštjjƒ }dj|d |d ƒg}n8|j	d d!ƒ}x |j
ƒ D ]\}}|j	||ƒ}q°W |jd!ƒ}g }	xd|D ]\}
|
|k� rútd"j|
ƒƒ‚qÜ|
d |
d#  }|	jd$j||ƒƒ |
jd%ƒrÜ|	jd&j||ƒƒ qÜW tt|	ƒƒS ).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ÚarchúKepler+Teslaú3.7ÚKeplerú3.5+PTXúMaxwell+Tegraú5.3ÚMaxwellú5.0;5.2+PTXÚPascalú6.0;6.1+PTXÚVoltaú7.0+PTXÚTuringú7.5+PTXz3.5z5.0z5.2z6.0z6.1z6.2z7.0z7.2z7.5c             S   s   g | ]}|d  ‘qS )z+PTXr$   )r=   Úsr$   r$   r%   r‚   Ç  s    z(_get_cuda_arch_flags.<locals>.<listcomp>ZTORCH_CUDA_ARCH_LISTz{}.{}r   r   ú ú;z+Unknown CUDA arch ({}) or GPU not supportedr   z#-gencode=arch=compute_{},code=sm_{}z+PTXz(-gencode=arch=compute_{},code=compute_{})rð   rñ   )rò   ró   )rô   rõ   )rö   r÷   )rø   rù   )rú   rû   )rü   rý   )ÚcollectionsÚOrderedDictr   r   r   r    r!   Zget_device_capabilityr#   ÚreplacerÙ   rP   r×   rw   Úendswithrs   Úset)r|   rn   Znamed_archesZsupported_archesZvalid_arch_stringsZ	arch_listZ
capabilityZ
named_archZarchvalÚflagsrï   Únumr$   r$   r%   rv   ¦  s@    






rv   c             C   sf   t jjdƒ}|d krtƒ }|r,tdj|ƒƒ t jj|| ƒ}t jj|ƒsb|rXtdj|ƒƒ t j	|ƒ |S )NZTORCH_EXTENSIONS_DIRz&Using {} as PyTorch extensions root...z"Creating extension directory {}...)
r   r   r   r;   r"   r#   r   r8   r   Úmakedirs)r>   rÎ   Zroot_extensions_directoryrÍ   r$   r$   r%   rÈ   é  s    
rÈ   c             C   s¾   y^t jjƒ  t jjƒ  t jdkrFtjddg|r2d ntjtj|dd� ntj	ddgtj|d� W nZ tj
k
r¸   t jƒ \}}}dj| ƒ}t|d	ƒr¬|jr¬|d
j|jjƒ ƒ7 }t|ƒ‚Y nX d S )Nrœ   é   Zninjaz-vT)ré   r<   ÚcwdÚcheck)r<   r
  zError building extension '{}'Úoutputz: {})rœ   r	  )r3   ré   Úflushr<   r    r   ÚrunÚPIPErA   r   ÚCalledProcessErrorrU   r#   r¤   r  r   rí   )r>   rÍ   rÎ   rZ   r[   Úmessager$   r$   r%   rç   ü  s(    




rç   c             C   sH   t j| |gƒ\}}}|�& |r.t j| |||ƒS tjj|ƒ W d Q R X d S )N)ÚimpÚfind_moduleÚload_moduler    ÚopsZload_library)Úmodule_namer   rÐ   ÚfileÚdescriptionr$   r$   r%   rã     s
    rã   c       &      C   sv  dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}t rLtjjddƒ}ntjjddƒ}d	g}	|	jd
j|ƒƒ |rŠ|	jdjtddƒƒƒ dd„ |D ƒ}dd„ |D ƒ}
t|ƒ}|jtj	ƒ d ƒ t rÔ|
|7 }
|j
ƒ  dj|ƒg}|jdƒ trø|jdƒ |dd„ |
D ƒ7 }|dd„ |D ƒ7 }|dtttjjƒƒ g7 }t �r^|t | }ddlm} ||ƒ}n|ddg | }djdj|ƒƒg}|�r|t tƒ  }t �rÌxtD ]}d|g| }�q W ||ƒ}|||ƒ7 }n2|ddg7 }||7 }td d!„ |D ƒƒ�sþ|jdƒ |jd"jdj|ƒƒƒ t �r&d#g| }n
d$g| }tjjd%ƒ�rJ|jd&ƒ nt �rX||ƒ}|jd'jdj|ƒƒƒ d(g}t �r�|jd)ƒ |jd*ƒ n|jd+ƒ |jd,ƒ |jd-ƒ |�rÄd.g}|jd/ƒ d0g}t �r*tjd1dgƒjƒ jd2ƒ}t|ƒd3k�rtj j!|d ƒj"d4d5ƒ}nt#d6ƒ‚|jd7j|ƒƒ n
|jd8ƒ g }g }x�|D ]ˆ}tj j$tj j%|ƒƒd }t&|ƒ�r~|�r~d9}d:j|ƒ}nd;}d<j|ƒ}|j|ƒ t �r¨|j"d4d5ƒ}|j"dd=ƒ}|jd>j|||ƒƒ �qBW t �rØd?nd@}dAj||ƒ}dBj|dj|ƒƒg} dCj|ƒg}!|	||g}"|�r"|"j|ƒ |"||| |!g7 }"t'| dDƒ�0}#x(|"D ] }$dEj|$ƒ}%|#j(dFj|%ƒƒ �qDW W d Q R X d S )GNc             S   s   g | ]}|j ƒ ‘qS r$   )rB   )r=   rn   r$   r$   r%   r‚   )  s    z%_write_ninja_file.<locals>.<listcomp>c             S   s   g | ]}|j ƒ ‘qS r$   )rB   )r=   rn   r$   r$   r%   r‚   *  s    c             S   s   g | ]}|j ƒ ‘qS r$   )rB   )r=   rn   r$   r$   r%   r‚   +  s    c             S   s   g | ]}|j ƒ ‘qS r$   )rB   )r=   rn   r$   r$   r%   r‚   ,  s    r¢   r£   zc++zninja_required_version = 1.3zcxx = {}z	nvcc = {}rk   r   c             S   s   g | ]}t jj|ƒ‘qS r$   )r   r   r¾   )r=   r  r$   r$   r%   r‚   ;  s    c             S   s   g | ]}t jj|ƒ‘qS r$   )r   r   r¾   )r=   r  r$   r$   r%   r‚   <  s    r¼   z-DTORCH_EXTENSION_NAME={}z-DTORCH_API_INCLUDE_EXTENSION_Hz-DBUILD_NAMEDTENSORc             S   s   g | ]}d j |ƒ‘qS )z-I{})r#   )r=   r¼   r$   r$   r%   r‚   L  s    c             S   s   g | ]}d j |ƒ‘qS )z-isystem {})r#   )r=   r¼   r$   r$   r%   r‚   M  s    z-D_GLIBCXX_USE_CXX11_ABI=r   )Ú_nt_quote_argsz-fPICz
-std=c++11zcflags = {}rÿ   z
-Xcompilerz--compiler-optionsz'-fPIC'c             s   s   | ]}|j d ƒV  qdS )z-std=N)r5   )r=   rn   r$   r$   r%   r@   c  s    z$_write_ninja_file.<locals>.<genexpr>zcuda_flags = {}z/DLLz-sharedr1   z-undefined dynamic_lookupzldflags = {}zrule compilez3  command = cl /showIncludes $cflags -c $in /Fo$outz  deps = msvcz7  command = $cxx -MMD -MF $out.d $cflags -c $in -o $outz  depfile = $out.dz  deps = gcczrule cuda_compilez,  command = $nvcc $cuda_flags -c $in -o $outz	rule linkr   z
r   ú:z$:z'MSVC is required to load C++ extensionsz8  command = "{}/link.exe" $in /nologo $ldflags /out:$outz%  command = $cxx $in $ldflags -o $outZcuda_compilez	{}.cuda.or‡   z{}.oz$ zbuild {}: {} {}ZpydÚsoz{}.{}zbuild {}: link {}z
default {}rÔ   rÕ   z{}

))r   r   r   r   rw   r#   rq   r¶   Ú	sysconfigÚ	get_pathsÚclearr   r¨   rX   r    r©   rª   rˆ   Údistutils.spawnr  r8   ru   rv   rC   r3   r4   r5   r   r   r   rP   r   r   r   r  rí   ÚsplitextÚbasenamerp   rÚ   rÛ   )&r   r>   rŒ   rÉ   rÊ   rË   rÌ   rÏ   rD   ÚconfigZuser_includesZsystem_includesZcommon_cflagsr|   r  r  Z
cuda_flagsrn   ZldflagsZcompile_ruleZcuda_compile_ruleZ	link_ruleZcl_pathsZcl_pathZobject_filesÚbuildZsource_fileÚ	file_nameZruleÚtargetrz   Zlibrary_targetÚlinkÚdefaultÚblocksZ
build_fileÚblockÚlinesr$   r$   r%   ræ   !  sÊ    
















ræ   c              G   s"   t dkrtdƒ‚tjjt f| žŽ S )zÖ
    Joins 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.
    NzSCUDA_HOME environment variable is not set. Please set it to your CUDA install root.)r   ÚEnvironmentErrorr   r   r8   )rÂ   r$   r$   r%   rq   ¸  s    rq   c             C   s   t jj| ƒd dkS )Nr   ú.cuú.cuh)r,  r-  )r   r   r   )r   r$   r$   r%   rp   Å  s    rp   )r'   r(   r   )r)   r   r*   )F)F)NNNNNFNT)
NNNNNNNFNT)N)CÚ
__future__r   r   r   r   r^   r   r  r   rR   r¸   r   r3   r  r9   rL   r  r    Z
file_batonr   Z_cpp_extension_versionerr   Zsetuptools.command.build_extr	   r4   r   ÚgetenvÚupperr   r&   rO   rQ   rY   rN   r   r   r   r+   r‡   r,   rˆ   ru   rÞ   r0   r6   r;   rE   r\   Úobjectr]   rº   r»   r¶   r·   rÑ   rÜ   rÇ   rà   rä   rå   rv   rÈ   rç   rã   ræ   rq   rp   r$   r$   r$   r%   Ú<module>   s’   


8 H-3
!
'       
V         
v3*"
C 