
    ёi                    B   S SK Jr  S SKJrJr  S SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKrS SKJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S	S
KJrJrJrJrJrJ r   S	SKJ!r!J"r"J#r#  S	SKJ$r$J%r%J&r&  S	SKJ'r'J(r(J)r)J*r*  S	SKJ+r+J,r,  S	SKJ-r-J.r.J/r/  S	SKJ0r0J1r1J2r2  S	SKJ3r3J4r4  SSK5J6r6  S SK7J8r8  \(       a  S SK9J:r:  S SK;J<r<  \0(       a  S SK=Jr>  S SK?J@r@  \@" SS9\>lA        \" 5       rB\6R                  " 5       (       a	  \" 5       rD\DrB\" 5       rES*S jrF        S+S jrG        S+S jrHS rI " S S\5      rJ " S S \5      rK " S! S"\5      rL " S# S$\5      rM       S,                   S-S% jjrNS.S/S& jjrOS' rPS0S( jrQ        S1S) jrRg)2    )annotations)TYPE_CHECKINGAnyN)DistutilsExecError	LinkError)easy_install)	build_ext)build)install   )add_compile_flagfind_cuda_homefind_ccache_homefind_rocm_homenormalize_extension_kwargsdefine_paddle_extension_name)is_cuda_fileprepare_unix_cudaflagsprepare_win_cudaflags)_import_module_from_library_write_setup_file_jit_compile)check_abi_compatibilitylog_vCustomOpInfoparse_op_name_from)_reset_so_rpathclean_object_if_change_cflags)get_build_directoryadd_std_without_repeatcustom_write_stub)
IS_WINDOWSOS_NAMEMSVC_COMPILE_FLAGS)CLANG_COMPILE_FLAGSCLANG_LINK_FLAGS   )core)ThreadPoolExecutor)Sequence)
ModuleType)Mock)return_valuec                    U R                  S0 5      n[        U[        5      (       d   eSU;  a  [        R	                  SS9US'   XS'   SnSU ;  a  [        U5      eU S   R                  S5      (       a   S5       e U R                  S	/ 5      n[        U[        5      (       d  U/n[        U5      S
:X  d   S[        U5       S35       eU H  nU S   Ul	        M     X0S	'   SU;  d   e[        US'   SU;  d   e[        US'   SU;  d   e[        R                  R                  SU S   5      n[        R	                  US9US'   SU S'   [         R"                  " S0 U D6  g)ag  
The interface is used to config the process of compiling customized operators,
mainly includes how to compile shared library, automatically generate python API
and install it into site-package. It supports using customized operators directly with
``import`` statement.

It encapsulates the python built-in ``setuptools.setup`` function and keeps arguments
and usage same as the native interface. Meanwhile, it hides Paddle inner framework
concepts, such as necessary compiling flags, included paths of head files, and linking
flags. It also will automatically search and valid local environment and versions of
``cc(Linux)`` , ``cl.exe(Windows)`` and ``nvcc`` , then compiles customized operators
supporting CPU or GPU device according to the specified Extension type.

Moreover, `ABI compatibility <https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html>`_
will be checked to ensure that compiler version from ``cc(Linux)`` , ``cl.exe(Windows)``
on local machine is compatible with pre-installed Paddle whl in python site-packages.

For Linux, GCC version will be checked . For example if Paddle with CUDA 10.1 is built with GCC 8.2,
then the version of user's local machine should satisfy GCC >= 8.2.
For Windows, Visual Studio version will be checked, and it should be greater than or equal to that of
PaddlePaddle (Visual Studio 2017).
If the above conditions are not met, the corresponding warning will be printed, and a fatal error may
occur because of ABI compatibility.

Note:

    1. Currently we support Linux, MacOS and Windows platform.
    2. On Linux platform, we recommend to use GCC 8.2 as soft linking candidate of ``/usr/bin/cc`` .
       Then, Use ``which cc`` to ensure location of ``cc`` and using ``cc --version`` to ensure linking
       GCC version.
    3. On Windows platform, we recommend to install `` Visual Studio`` (>=2017).


Compared with Just-In-Time ``load`` interface, it only compiles once by executing
``python setup.py install`` . Then customized operators API will be available everywhere
after importing it.

A simple example of ``setup.py`` as followed:

.. code-block:: text

    # setup.py

    # Case 1: Compiling customized operators supporting CPU and GPU devices
    from paddle.utils.cpp_extension import CUDAExtension, setup

    setup(
        name='custom_op',  # name of package used by "import"
        ext_modules=CUDAExtension(
            sources=['relu_op.cc', 'relu_op.cu', 'tanh_op.cc', 'tanh_op.cu']  # Support for compilation of multiple OPs
        )
    )

    # Case 2: Compiling customized operators supporting only CPU device
    from paddle.utils.cpp_extension import CppExtension, setup

    setup(
        name='custom_op',  # name of package used by "import"
        ext_modules=CppExtension(
            sources=['relu_op.cc', 'tanh_op.cc']  # Support for compilation of multiple OPs
        )
    )


Applying compilation and installation by executing ``python setup.py install`` under source files directory.
Then we can use the layer api as followed:

.. code-block:: text

    import paddle
    from custom_op import relu, tanh

    x = paddle.randn([4, 10], dtype='float32')
    relu_out = relu(x)
    tanh_out = tanh(x)


Args:
    name(str): Specify the name of shared library file and installed python package.
    ext_modules(Extension): Specify the Extension instance including customized operator source files, compiling flags et.al.
                            If only compile operator supporting CPU device, please use ``CppExtension`` ; If compile operator
                            supporting CPU and GPU devices, please use ``CUDAExtension`` .
    include_dirs(list[str], optional): Specify the extra include directories to search head files. The interface will automatically add
                             ``site-package/paddle/include`` . Please add the corresponding directory path if including third-party
                             head files. Default is None.
    extra_compile_args(list[str] | dict, optional): Specify the extra compiling flags such as ``-O3`` . If set ``list[str]`` , all these flags
                            will be applied for ``cc`` and ``nvcc`` compiler. It supports specify flags only applied ``cc`` or ``nvcc``
                            compiler using dict type with ``{'cxx': [...], 'nvcc': [...]}`` . Default is None.
    **attr(dict, optional): Specify other arguments same as ``setuptools.setup`` .

Returns:
    None

cmdclassr	   T)no_python_abi_suffixa  
    Required to specific `name` argument in paddle.utils.cpp_extension.setup.
    It's used as `import XXX` when you want install and import your custom operators.

    For Example:
        # setup.py file
        from paddle.utils.cpp_extension import CUDAExtension, setup
        setup(name='custom_module',
              ext_modules=CUDAExtension(
              sources=['relu_op.cc', 'relu_op.cu'])

        # After running `python setup.py install`
        from custom_module import relu
    namemodulez8Please don't use 'module' as suffix in `name` argument, ext_modulesr   z*Required only one Extension, but received zf. If you want to compile multi operators, you can include all necessary source files in one Extension.r   r   r
   )
build_baseFzip_safeN )get
isinstancedictBuildExtensionwith_options
ValueErrorendswithlistlenr1   EasyInstallCommandInstallCommandospathjoinBuildCommand
setuptoolssetup)attrr/   	error_msgr3   
ext_moduler4   s         h/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/utils/cpp_extension/cpp_extension.pyrG   rG   e   s   ~ xx
B'Hh%%%%(" . ; ;!% !< !
 $ZI T##F|$$X.. B. J((="-Kk4(("m{q  
4S5E4F  Gm  	n  "
v,
 " & )))1H^ H$$$(HY
 ("""gtF|4J$11Z1HHW Dt    c                    [        USS9nUR                  SS5      nUc  [        U 5      n[        R                  " X0/UQ70 UD6$ )a  
The interface is used to config source files of customized operators and complies
Op Kernel only supporting CPU device. Please use ``CUDAExtension`` if you want to
compile Op Kernel that supports both CPU and GPU devices.

It further encapsulates python built-in ``setuptools.Extension`` .The arguments and
usage are same as the native interface, except for no need to explicitly specify
``name`` .

**A simple example:**

.. code-block:: text

    # setup.py

    # Compiling customized operators supporting only CPU device
    from paddle.utils.cpp_extension import CppExtension, setup

    setup(
        name='custom_op',
        ext_modules=CppExtension(sources=['relu_op.cc'])
    )


Note:
    It is mainly used in ``setup`` and the name of built shared library keeps same
    as ``name`` argument specified in ``setup`` interface.


Args:
    sources(list[str]): Specify the C++/CUDA source files of customized operators.
    *args(list[options], optional): Specify other arguments same as ``setuptools.Extension`` .
    **kwargs(dict[option], optional): Specify other arguments same as ``setuptools.Extension`` .

Returns:
    setuptools.Extension: An instance of ``setuptools.Extension``
Fuse_cudar1   Nr   pop_generate_extension_namerF   	Extensionsourcesargskwargsr1   s       rK   CppExtensionrX     sM    P (?F
 ::fd#D|'0????rL   c                    [        USS9nUR                  SS5      nUc  [        U 5      n[        R                  " X0/UQ70 UD6$ )a  
The interface is used to config source files of customized operators and complies
Op Kernel supporting both CPU and GPU devices. Please use ``CppExtension`` if you want to
compile Op Kernel that supports only CPU device.

It further encapsulates python built-in ``setuptools.Extension`` .The arguments and
usage are same as the native interface, except for no need to explicitly specify
``name`` .

**A simple example:**

.. code-block:: text

    # setup.py

    # Compiling customized operators supporting CPU and GPU devices
    from paddle.utils.cpp_extension import CUDAExtension, setup

    setup(
        name='custom_op',
        ext_modules=CUDAExtension(
            sources=['relu_op.cc', 'relu_op.cu']
        )
    )


Note:
    It is mainly used in ``setup`` and the name of built shared library keeps same
    as ``name`` argument specified in ``setup`` interface.


Args:
    sources(list[str]): Specify the C++/CUDA source files of customized operators.
    *args(list[options], optional): Specify other arguments same as ``setuptools.Extension`` .
    **kwargs(dict[option], optional): Specify other arguments same as ``setuptools.Extension`` .

Returns:
    setuptools.Extension: An instance of setuptools.Extension.
TrN   r1   NrP   rT   s       rK   CUDAExtensionrZ   :  sM    T (>F
 ::fd#D|'0????rL   c                   [        U 5      S:  d   S5       e/ nU  H[  n[        R                  R                  U5      n[        R                  R	                  U5      u  p4X1;  d  MJ  UR                  U5        M]     SR                  U5      $ )z*
Generate extension name by source files.
r   zsource files is empty_)r?   rB   rC   basenamesplitextappendrD   )rU   file_prefixsourcefilenamer\   s        rK   rR   rR   p  sz     w<!444K!!&)gg&&v.&x(  88K  rL   c                     ^  \ rS rSrSr\SS j5       rSU 4S jjrSU 4S jjrSU 4S jjr	SS jr
SU 4S jjrSS	 jrSS
 jrSS jrS rSS jrSS jrU 4S jrSrU =r$ )r:   i  zo
Inherited from setuptools.command.build_ext to customize how to apply
compilation process with share library.
c                (   ^ ^  " U U4S jST 5      nU$ )zC
Returns a BuildExtension subclass containing use-defined options.
c                  $   > \ rS rSrU U4S jrSrg)5BuildExtension.with_options.<locals>.cls_with_optionsi  c                T   > UR                  T5        TR                  " U /UQ70 UD6  g Nupdate__init__selfrV   rW   clsoptionss      rK   rk   >BuildExtension.with_options.<locals>.cls_with_options.__init__  %    g&T3D3F3rL   r6   N__name__
__module____qualname____firstlineno__rk   __static_attributes__rn   ro   s   rK   cls_with_optionsrf         4 4rL   ry   r6   rn   ro   ry   s   `` rK   r;   BuildExtension.with_options      	4 	4s 	4
  rL   c                   > [         TU ]  " U0 UD6  UR                  SS5      U l        UR                  SS5      U l        SU l        g)z
Attributes is initialized with following order:

    1. super().__init__()
    2. initialize_options(self)
    3. the reset of current __init__()
    4. finalize_options(self)

So, it is recommended to set attribute value in `finalize_options`.
r0   T
output_dirNF)superrk   r7   r0   r   contain_cuda_filerm   rV   rW   	__class__s      rK   rk   BuildExtension.__init__  sE     	$)&)$*JJ/Et$L! **\48!&rL   c                "   > [         TU ]  5         g rh   )r   initialize_optionsrm   r   s    rK   r   !BuildExtension.initialize_options  s    "$rL   c                `   > [         TU ]  5         U R                  b  U R                  U l        g g rh   )r   finalize_optionsr   	build_libr   s    rK   r   BuildExtension.finalize_options  s*     " ??&!__DN 'rL   c                  ^ ^	^
^^^^ [         R                  " S5      (       a  T R                  5         T R                  5         T m
T R                  S   n[        UR                  [        5      (       a=  SUR                  ;   a-  [        [        R                  " UR                  S   5      5      m	OS m	T R                  T R                  S   R                  5      n[        [        R                  R!                  U5      T R                  S   5        T R"                  =R$                  SS/-  sl        S mS mT R"                  R&                  S:X  aN  T R"                  =R(                  SS/-  sl        T R"                  R*                  mT R"                  R,                  mO T R"                  R.                  R*                  mT R                   H  n[1        U5        M     U
4S jm          S                       SU	U4S	 jjjn       SU4S
 jjn       SUUU 4S jjnU 4S jnT R"                  R&                  S:X  a  UT R"                  l        OUT R"                  R.                  l        U" T R"                  R2                  T R4                  5      T R"                  l        T R7                  5          T	(       aU  T R"                  R&                  S:w  a;  T R"                  R.                  R8                  mUT R"                  R.                  l        [;        S5        [<        R>                  " T 5        T R"                  R&                  S:X  a  TT R"                  l        O=TT R"                  R.                  l        T(       a  TT R"                  R.                  l        T R                  T R                  S   R@                  5      n[C        U5        g ! T R"                  R&                  S:X  a  TT R"                  l        f TT R"                  R.                  l        T(       a  TT R"                  R.                  l        f f = f)Ndarwinr   
nvcc_dlinkz.cuz.cu.ccmsvcz.cuhc                v  > [         R                  R                  U5      n[        R                  " U5      n U R
                  n[        U5      (       Ga  [        R                  " 5       (       a  [        c   S5       e[        b1  [         R                  R                  [        SS5      n	[         SU	 3n	O%[         R                  R                  [        SS5      n	U R                  SU	5        [        U[        5      (       a  US   nGOD[        R                  " S5      (       a  [         R                  R                  [         R                   " SS	5      SS
5      n
[         R                  R#                  U
5      (       d  [%        S5      eU R                  SU
5        [        U[        5      (       a  US   nO[&        c   S5       e[        b1  [         R                  R                  [&        SS5      n[         SU 3nO%[         R                  R                  [&        SS5      nU R                  SU5        [        U[        5      (       a  US   n[)        U5      nOD[        b#  U R                  S[        /U R
                  Q5        [        U[        5      (       a  US   n[        R                  " 5       (       a"  UR+                  S5        UR+                  S5        [-        US/5        [        U5      (       dN  TR.                  (       a=  [        R                  " 5       (       a  UR+                  S5        OUR+                  S5        [1        XpR2                  SS9  U R5                  XX4Xv5        U R                  SW5        g! [6         a  n[9        U SU 35         SnAN2SnAff = f! U R                  SW5        f = f)z^
Monkey patch mechanism to replace inner compiler to custom compile process on Unix platform.
NzeNot found ROCM runtime,                             please use `export ROCM_PATH= XXX` to specify it.binhipcc compiler_soiluvatar_gpu
COREX_HOMEz/usr/local/corex/zclang++zECorex compiler is unavailable, please set `COREX_HOME` to specify it.nvcczeNot found CUDA runtime,                             please use `export CUDA_HOME= XXX` to specify it.cxxz-D__HIP_PLATFORM_HCC__z/-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_HIPz-D_GLIBCXX_USE_CXX11_ABI=1z-DPADDLE_WITH_HIP-DPADDLE_WITH_CUDAT)	use_std17z compile failed, )rB   rC   abspathcopydeepcopyr   r   r(   is_compiled_with_rocm	ROCM_HOMECCACHE_HOMErD   set_executabler8   r9   is_compiled_with_custom_devicegetenvisfiler<   	CUDA_HOMEr   r_   r   r   r    compiler_type_compile	Exceptionprint)rm   objsrcextcc_argsextra_postargspp_optscflagsoriginal_compiler	hipcc_cmdixcc_cmdnvcc_cmdecurrent_extension_builders                rK   unix_custom_compile_single_fileHBuildExtension.build_extensions.<locals>.unix_custom_compile_single_file  s    ''//#&C]]>2FZF$($4$4!$$1133(4 O4 '2(*Yw(OI+6-q(DI(*Yw(OI++M9E%fd33%+G_F<<^LL#%77<<IIl4GH!%$
  "ww~~h77", g#  ++M8D%fd33%+F^F(4 O4 '2')ww||Iuf'MH*5az'BH')ww||Iuf'MH++M8D%fd33%+F^F3F;F #.++)K+K$:J:J+K "&$//!' --//MM":;MMI !*F)GH %S))1CC1133&9:&:;&..$ cfF
 ##M3DE	  4.qc2334 ##M3DEs*   L2M= =
N!NN$ N!!N$ $N8c                j  > [         R                  R                  US   5      n[         R                  R                  US5      n[        c  [        S5      e[         R                  R                  [        SS5      n/ n[        (       a  UR                  [        5        UR                  U5        UR                  U5        UR                  SU/5        UR                  T5         U R                  U5        / [        U5      QUPnT" U UUUUUUUUU	U
UU5      $ ! [         a  n[        U5      eS nAff = f)Nr   zdlink.oz&CUDA_HOME is not found, please set it.r   r   -o)rB   rC   dirnamerD   r   RuntimeErrorr   r_   extendspawnr   r   r>   )rm   objectsoutput_filenamer   	librarieslibrary_dirsruntime_library_dirsexport_symbolsdebugextra_preargsr   
build_temptarget_lang	dlink_dirdlink_objectr   cmdmsgcuda_dlink_post_cflagsoriginal_links                     rK   unix_custom_link_shared_objectGBuildExtension.build_extensions.<locals>.unix_custom_link_shared_objectA  s     
3I77<<	9=L
  "#KLLww||Iuf=HC{

;'JJx JJwJJl+,JJ-.%

3
 5W4|4G $  & %n$%s   $D 
D2"D--D2c	                  > U R                  UUUUUU5      u  p9pznU R                  XU5      n[        [        U R                  5      S9n[        U[        R                  " 5       [        U	5      5      n[        SU S35        [        US9 nU	 Vs0 s H<  nUR                  T[        R                  " U 5      UUU   S   UU   S   UUU
5      U_M>     nn[        R                  R                  U5       H'  nUU   n UR!                  5         [        U S35        M)     S S S 5        U	$ s  snf ! ["         a  n[        U< SU 35         S nAM[  S nAff = f! , (       d  f       U	$ = f)	N)verbosezUsing zN workers for compilation. HINT: export MAX_JOBS=n to set the number of workers)max_workersr   r   z is compiledz generated an exception: )_setup_compile_get_cc_args_get_num_workersboolr   _compute_worker_numberrB   	cpu_countr?   r   r)   submitr   
concurrentfuturesas_completedresultr   )rm   rU   r   macrosinclude_dirsr   r   r   dependsr   r   r
   r   requested_workersworker_numberexecutorr   r   futureexcr   s                       rK   unix_custom_single_compilerDBuildExtension.build_extensions.<locals>.unix_custom_single_compiler  s    ## " <F^e ''FG 0dll9K L2!2<<>3w<M 'uv $>(  '  ' OO7		$c
1c
1&	 	  '   )00==gFF!&/C4 \23 G! ?2 N/$ % H'@FGGH) ?>2 NsI   EAD))E=D.E)E.
E8E	EEE
E&c           
        > [         R                  " U5      Tl        S nU
U4S jn UTR                  l        T	" U UUUUUUU5      T
TR                  l        $ ! T
TR                  l        f = f)Nc                  >^^^ TR                   R                  n[        [        U 5      5       HA  n[        R
                  " SX   5      b  SX'   [        R
                  " SX   5      c  M=  SX'   MC     [        R                  " S5      mU4S jU  5        Vs/ s H  nU(       d  M  UR                  S5      PM     nn[        R                  " S5      mU4S	 jU  5        Vs/ s H  nU(       d  M  UR                  S
5      PM     nn[        R                  " S5      mU4S jU  5        Vs/ s H  nU(       d  M  UR                  S
5      PM     nn[        U5      S
:X  a  [        U5      S
:X  d   eUS   nUS   n[        U5      (       a  [        c   S5       e[        R                  R                  [        SS5      n	[        TR                  [        5      (       a  TR                  S   n
O.[        TR                  [         5      (       a  TR                  n
O/ n
/ [#        U
5      QSPn
[$         H	  nSU/U
Qn
M     U	SUSU/UQU
Qn Op[        TR                  [        5      (       a  [$        TR                  S   -   n
X
-  n O6[        TR                  [         5      (       a  [$        TR                  -   n
X
-  n [        U5      (       d"  TR&                  (       a  U R)                  S5        T" U 5      $ s  snf s  snf s  snf )Nz/MDz/MTz/W[1-4]z/W0z/T(p|c)(.*)c              3  F   >#    U  H  nTR                  U5      v   M     g 7frh   match).0elem	src_regexs     rK   	<genexpr>pBuildExtension.build_extensions.<locals>.win_custom_single_compiler.<locals>.win_custom_spawn.<locals>.<genexpr>       Diood33   !   z/Fo(.*)c              3  F   >#    U  H  nTR                  U5      v   M     g 7frh   r   )r   r   	obj_regexs     rK   r   r     r   r   r   z((\-|\/)I.*)c              3  F   >#    U  H  nTR                  U5      v   M     g 7frh   r   )r   r   include_regexs     rK   r   r     s     HCDm11$77Cr   r   zaNot found CUDA runtime,                         please use `export CUDA_HOME= XXX` to specify it.r   r   z--use-local-envz
-Xcompilerz-cr   r   r   )compilercompile_optionsranger?   researchcompilegroupr   r   rB   rC   rD   r8   r   r9   r>   r   r$   r   r_   )r   r   imsrc_listobj_listinclude_listr   r   r   r   flagr   r   r   original_spawnrm   s               @@@rK   win_custom_spawn]BuildExtension.build_extensions.<locals>.win_custom_single_compiler.<locals>.win_custom_spawn  s   "&--"?"?s3xAyy/;!&yyCF3?!&	 ) JJ}5	 EDD AGGAJD   JJy1	 EDD AGGAJD   !#

? ; ICH H AGGAJH    8})c(mq.@@@qkqk$$$0 K0
  "ww||IufEH!$++t44!%V!4#DKK66!%!#P4V<P>OPF 2".!>v!> !3 ! &  C  T22/$++e2DDFMCT22/$++=FMC#C((T-C-CJJ34%c**w s$   K4'K4"K91K9,K>;K>)r   r   r   r   r   )rU   r   r   r   r   r   r   r   r	  original_compiler  rm   s            rK   win_custom_single_compilerCBuildExtension.build_extensions.<locals>.win_custom_single_compiler  sm     --7DK!NF+P5&6#' !"	 '5#n#s   A A+c                    >^ ^ SUU U4S jjnU$ )z
Decorated the function to add customized naming mechanism.
Originally, both .cc/.cu will have .o object output that will
bring file override problem. Use .cu.o as CUDA object suffix.
c                  >  T	" XU5      n[        U 5       HK  u  pE[        U5      (       d  M  X4   nT
R                  R                  S:X  a  US S S-   X4'   MA  US S S-   X4'   MM     Tb0  U Vs/ s H#  n[        R
                  R                  TU5      PM%     nnU Vs/ s H"  n[        R
                  R                  U5      PM$     nnT	T
R                  l        U$ s  snf s  snf ! T	T
R                  l        f = f)Nr   zcu.objzcu.o)		enumerater   r   r   rB   rC   rD   r   object_filenames)source_filenames	strip_dirr   r   r  ra   old_objr   build_directoryoriginal_funcrm   s           rK   wrapperTBuildExtension.build_extensions.<locals>.object_filenames_with_cuda.<locals>.wrapper%  s    C+(ZG &//?%@	'//&-jG#}}::fD-4Sb\H-D
-4Sb\F-B
 &A '2 (/#'. GGLL#>'.   #
 @GGwrwws3wGG5BDMM2#
 H5BDMM2s.   'C% ?C% -*CC% )C C% 
C% %C8)r    r6   )r  r  r  rm   s   `` rK   object_filenames_with_cudaCBuildExtension.build_extensions.<locals>.object_filenames_with_cuda  s     4 NrL   z9Compiling user custom op, it will cost a few seconds.....)
NNNNNFNNNN)r   zlist[str] | tuple[str, ...]r   strr   
str | Noner   "list[str] | tuple[str, ...] | Noner   r   r   r   r   z
Any | Noner   r   r   list[str] | Noner   r!  r   zstr | os.PathLike[str] | Noner   r  )NNNFNNN)NNNr   NNN)"r#   
startswith_valid_clang_compiler
_check_abi
extensionsr8   extra_compile_argsr9   r   r   r   get_ext_fullpathr1   r   rB   rC   r   r   src_extensionsr   _cpp_extensionsr   r   r   r   r  r   _record_op_infolink_shared_objectr   r	   build_extensions
_full_namer   )rm   r   so_name	extensionr   r   r  r  so_pathr   r   r  r   r  r   s   `        @@@@@@rK   r,  BuildExtension.build_extensions  s   h''&&($(! ooa s--t44 6 66%;c44\BC&" &*"
 ''(:(?(?@%GGOOG$dooa&8	

 	$$(99$ ==&&&0MM))eV_<)#}}44!]]00N#}}66>>I(3 )d	FT &*<@?CGK)-.2/38<&*<	0<	 !<	 #	<	
 :<	 =<	 #E<	 '<	 <	 ,<	 -<	 6<	 $<	 <	B 9	z b	5 b	5H!	H ==&&&0$>DMM!.IDMM##+)CMM**DNN*
& 		O%$--*E*E*O $ 7 7 J J2 '': MN&&t,}}**f4(8%2B''/ ANDMM++> ''(:(E(EF  }}**f4(8%2B''/ ANDMM++> !s   A=O A,Qc                X  > [         TU ]  U5      nSnUR                  U5      nU R                  (       aD  [	        U5      S:  d   S[	        U5       35       eUR                  S5        UR                  U5      n[        R                  " S5      (       a  SUS'   UR                  U5      nU$ )N.r   z+Expected len(name_items) > 2, but received r   dylibr  )	r   get_ext_filenamesplitr0   r?   rQ   rD   r#   r"  )rm   fullnameext_name	split_str
name_itemsr   s        rK   r6  BuildExtension.get_ext_filenamea  s    7+H5	^^I.
$$z?Q& =c*o=NO& NN2 ~~j1H h''$JrN ~~j1HrL   c                f    S/[         QnS/[        QnU R                  R                  UUS/S/US9  g)z4
Make sure to use Clang as compiler on Mac platform
clang)r   r   compiler_cxx
linker_exe	linker_soN)r%   r&   r   set_executables)rm   compiler_infoslinker_infoss      rK   r#  $BuildExtension._valid_clang_compilers  sJ     "8$783"23%%#&!y" 	& 	
rL   c                   [        U R                  S5      (       a  U R                  R                  S   nOL[        (       a!  [        R
                  R                  SS5      nO [        R
                  R                  SS5      n[        U5        [        (       a7  S[        R
                  ;   a"  S[        R
                  ;  a  Sn[        U5      eg	g	g	)
z
Check ABI Compatibility.
r?  r   CXXclzc++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 run `set DISTUTILS_USE_SDK=1` and try again.N)	hasattrr   r?  r"   rB   environr7   r   UserWarning)rm   r   r   s      rK   r$  BuildExtension._check_abi  s     4==.11}}11!4HZzz~~eT2Hzz~~eU3H) J$

2#2::5F 
 c"" 6 3 rL   c                >   U R                  5       n[        U5      S:X  d   e[        R                  R	                  US   5      n[        R                  R                  U5      n[        U R                  5       H  u  pEUR                   Vs/ s H"  n[        R                  R	                  U5      PM$     nnU R                  (       d  [        S U 5       5      U l	        [        U5      nU H&  n	[        R                  " 5       R                  XUS9  M(     M     gs  snf )z
Record custom op information.
r   r   c              3  8   #    U  H  n[        U5      v   M     g 7frh   )r   )r   ss     rK   r   1BuildExtension._record_op_info.<locals>.<genexpr>  s     ,Ng\!__gs   )r.  r0  N)get_outputsr?   rB   rC   r   r]   r  r%  rU   r   anyr   r   instanceadd)
rm   outputsr0  r.  r  r/  rQ  rU   op_namesop_names
             rK   r*  BuildExtension._record_op_info  s    
 ""$7|q   ''//'!*-''""7+%doo6LA3<3D3DE3Darwwq)3DGE))),,Ng,N)N&)'2H#%%'++g ,  $ 7Es   )Dc           
        U R                    H  n[        R                  R                  U R	                  UR
                  5      5      n[        R                  " U5       H  u  p4nU H  nUR                  S5      (       d  UR                  S5      (       d  M1  [        R                  " [        R                  R                  X65      5        [        S[        R                  R                  X65       35        M     M     M     g )Nz.cu.oz.oz	Removed: )r%  rB   rC   r   r'  r1   walkr=   removerD   r   )rm   r   	build_dirrootr\   filesfiles          rK   _clean_intermediate_files(BuildExtension._clean_intermediate_files  s    ??C(=(=chh(GHI"$'')"4!D}}W--t1D1D		"'',,t":;	"'',,t*B)CDE " #5 #rL   c                     U R                   (       d  gU R                   S   n[        R                  R                  U R	                  UR
                  5      5      n[        R                  R                  U5      n[        R                  R                  U5      nUR
                  nSU;   a  UR                  S5      S   OUn[        R                  R                  XF S35      n[        X75        g! [         a  n[        SU 35      UeSnAff = f)z
Generate the top-level python api file (package stub) alongside the
built shared library in build_lib. This replaces the legacy bdist_egg
write_stub mechanism that is no longer triggered in setuptools >= 80.
Nr   r3  r  .pyz$Failed to generate python api file: )r%  rB   rC   r   r'  r1   r]   r   r7  rD   r!   r   r   )	rm   r   r0  r.  r^  r9  lib_namepyfiler   s	            rK   _generate_python_api_file(BuildExtension._generate_python_api_file  s    	?? //!$Cggood&;&;CHH&EFGgg&&w/G0I xxH 36/x~~c*2.xHWW\\)z-=>Fg. 	6qc:	s   C/ CC/ /
D9DDc                j   U R                   (       d  gU R                   S   nU R                  UR                  5      n[        R                  R                  U5      n[        R                  R                  U5      n[        R                  R                  U5      u  pVSn[        R                  " S5      (       a	  US:X  a  SnO=[        R                  " S5      (       a  US:X  d  US:X  a  SnO[        (       a  US	:X  a  SnU(       a  U S
U 3n[        R                  R                  XH5      n	[        R                  R                  U5      (       ac  [        R                  R                  U	5      (       a  [        R                  " U	5        [        R                  " X)5        [        SU SU	 S35        ggg)z
Rename the shared library to *_pd_.so if it is an inplace build.
This is necessary for editable installs to work correctly with the python stub.
Nr   Flinux.soTr   .dylib.pyd_pd_z	Renaming z to z# for editable install compatibility)r%  r'  r1   rB   rC   r]   r   r^   r#   r"  r"   rD   existsr]  renamer   )
rm   r   fullpathrb   r   r1   
ext_suffixwill_renamenew_namenew_paths
             rK   _rename_inplace_shared_library-BuildExtension._rename_inplace_shared_library  sM    ooa ((277##H-''//(+77++H5g&&:+>K))("jE&9KZJ&0KtJ<0Hww||G6Hww~~h''77>>(++IIh'		(-zhZ7Z[	 (	 rL   c                   > [         TU ]  5         U R                  5         U R                  (       a  U R	                  5         U R                  5         g rh   )r   runrh  inplacerw  rb  r   s    rK   rz  BuildExtension.run   s9     	&&(<<//1&&(rL   )r   r   r   r0   r   )ro   r   returnztype[BuildExtension]rV   r   rW   r   r}  Noner}  r  )r8  r  r}  r  )rs   rt   ru   rv   __doc__classmethodr;   rk   r   r   r,  r6  r#  r$  r*  rb  rh  rw  rz  rw   __classcell__r   s   @rK   r:   r:     sb    
 
  
 '"%-p!d$
#2,FB$L	) 	)rL   r:   c                  @   ^  \ rS rSrSrSU 4S jjrSU 4S jjrSrU =r$ )r@   i  z
Extend easy_install Command to control the behavior of naming shared library
file.

NOTE(Aurelius84): This is a hook subclass inherited Command used to rename shared
                library file after extracting egg-info into site-packages.
c                &   > [         TU ]  " U0 UD6  g rh   )r   rk   r   s      rK   rk   EasyInstallCommand.__init__  s    $)&)rL   c                0  > [         TU ]  " U0 UD6  U R                   H  n[        R                  R                  U5      u  pESn[        R                  " S5      (       a	  US:X  a  SnO7[        R                  " S5      (       a	  US:X  a  SnO[        (       a  US:X  a  SnU(       d  M  US-   U-   n[        R                  R                  U5      (       d  [        R                  " U U 5        [        R                  R                  U5      (       a  M   e   g )	NFrk  rl  Tr   rm  rn  ro  )r   rz  rW  rB   rC   r^   r#   r"  r"   rp  rq  )	rm   rV   rW   egg_filerb   r   rt  new_so_pathr   s	           rK   rz  EasyInstallCommand.run  s    T$V$ HGG,,X6MHK!!'**se|"##H--#/"v"{&/#5ww~~k22II(+@ww~~k2222 %rL   r6   r~  )	rs   rt   ru   rv   r  rk   rz  rw   r  r  s   @rK   r@   r@     s    *3 3rL   r@   c                  T   ^  \ rS rSrSr\SS j5       rSU 4S jjrS	U 4S jjrSr	U =r
$ )
rE   i/  z
Extend build Command to control the behavior of specifying `build_base` root directory.

NOTE(Aurelius84): This is a hook subclass inherited Command used to specify customized
                  build_base directory.
c                (   ^ ^  " U U4S jST 5      nU$ )zA
Returns a BuildCommand subclass containing use-defined options.
c                  $   > \ rS rSrU U4S jrSrg)3BuildCommand.with_options.<locals>.cls_with_optionsi=  c                T   > UR                  T5        TR                  " U /UQ70 UD6  g rh   ri   rl   s      rK   rk   <BuildCommand.with_options.<locals>.cls_with_options.__init__>  rq   rL   r6   Nrr   rx   s   rK   ry   r  =  rz   rL   ry   r6   r{   s   `` rK   r;   BuildCommand.with_options7  r}   rL   c                T   > UR                  SS 5      U l        [        TU ]  " U0 UD6  g )Nr4   )r7   _specified_build_baser   rk   r   s      rK   rk   BuildCommand.__init__D  s(    %+ZZd%C"$)&)rL   c                `   > [         TU ]  5         U R                  b  U R                  U l        gg)z}
build_base is root directory for all sub-command, such as
build_lib, build_temp. See `distutils.command.build` for details.
N)r   r   r  r4   r   s    rK   r   BuildCommand.initialize_optionsJ  s.    
 	"$%%1"88DO 2rL   )r  r4   )ro   r   r}  ztype[BuildCommand]r~  r  )rs   rt   ru   rv   r  r  r;   rk   r   rw   r  r  s   @rK   rE   rE   /  s+     
  
 *9 9rL   rE   c                  ^   ^  \ rS rSrSrS	S jrS
U 4S jjrSU 4S jjrS
S jrS
S jr	Sr
U =r$ )rA   iT  aH  
Extend install Command to:
  1) choose an install dir that is actually importable (on sys.path)
  2) ensure a single top-level entry for the package in site/dist-packages so
     legacy tests that expect a sole artifact (egg/package) keep working
  3) rename the compiled library to *_pd_.so to avoid shadowing the python stub
c                H    U R                   R                  S   R                  $ )a   
Get the extension name from the extension module, not the distribution name.
This ensures we use the correct package name from setup.py.

Note: This assumes there is only one extension module (len(ext_modules) == 1).

Returns:
    str: The extension name
r   )distributionr3   r1   )rm   s    rK   _get_extension_name"InstallCommand._get_extension_name]  s!       ,,Q/444rL   c                  >^ [         TU ]  5         [        U SS 5      =(       d!    [        U SS 5      =(       d    [        U SS 5      nU(       a$  [        R                  R                  U5      (       d  g U R                  5       nSU;   a  UR                  S5      S   OUm[        U4S j[        R                  " U5       5       5      nU(       a  g / nUR                  [        R                  " 5       5        [        R                  " 5       nU(       a  UR                  U5        [        R                   HC  n[!        U["        5      (       d  M  UR%                  S5      (       d  M2  UR                  U5        ME     ['        5       n/ nU H5  n	U	(       d  M  X;  d  M  UR)                  U	5        UR                  U	5        M7     S n
U HA  n	U	[        R                  ;   d  M  [        R                  R                  U	5      (       d  M?  U	n
  O   U
c1  U H+  n	[        R                  R                  U	5      (       d  M)  U	n
  O   U
(       aA  U R*                  R-                  S5      nSU;  a  Xl        SU;  a  Xl        SU;  a  Xl        g g g )	Ninstall_libinstall_purelibinstall_platlibr3  r   c              3  v   >#    U  H.  nUR                  S 5      =(       a    UR                  T5      v   M0     g7f)z
.dist-infoNr=   r"  r   r1   pkg_names     rK   r   2InstallCommand.finalize_options.<locals>.<genexpr>|  s2      
/ MM,'EDOOH,EE/   69)zsite-packageszdist-packagesr   )r   r   getattrrB   rC   isdirr  r7  rT  listdirr   sitegetsitepackagesgetusersitepackagesr_   sysr8   r  r=   setrV  r  get_option_dictr  r  r  )rm   install_dirr9  has_dist_info
candidatesuspspseenorderedctargetoption_dictr  r   s               @rK   r   InstallCommand.finalize_optionsi  s    " D-. 6t.56t.5 	
 "''--"<"< ++- .1H_8>>#&q)(  


;/
 

  
$..01&&(c"((B"c""r{{2( ( !!"%	  uAqQ]q! 
 ACHH}q!1!1 
 >77==##F  ++;;IFKK/#)  3'-$ 3'-$ 4 rL   c                  >^ [         TU ]  " U0 UD6  [        U SS 5      =(       d!    [        U SS 5      =(       d    [        U SS 5      nU(       a$  [        R                  R                  U5      (       d  g U R                  5       nSU;   a  UR                  S5      S   OUm[        U4S j[        R                  " U5       5       5      nU(       a!  U R                  5         U R                  5         g g )Nr  r  r  r3  r   c              3  v   >#    U  H.  nUR                  S 5      =(       a    UR                  T5      v   M0     g7f)z	.egg-infoNr  r  s     rK   r   %InstallCommand.run.<locals>.<genexpr>  s2      
/ MM+&D4??8+DD/r  )r   rz  r  rB   rC   r  r  r7  rT  r  _rename_shared_library_single_entry_layout)rm   rV   rW   r  r9  has_egg_infor  r   s         @rK   rz  InstallCommand.run  s    T$V$ D-. 6t.56t.5 	
 "''--"<"< ++- .1H_8>>#&q)(  


;/
 
 '')%%'	 rL   c                <   [        U SS 5      =(       d!    [        U SS 5      =(       d    [        U SS 5      nU(       a$  [        R                  R                  U5      (       d  g U R	                  5       nSU;   a  UR                  S5      OU/nUS   n[        (       a  SO[        R                  " S5      (       a  SOS	n[        R                  R                  " U/US S Q76 n[        R                  R                  Xd U 35      n[        R                  R                  Xd S
U 35      n[        R                  R                  U5      (       aQ  [        R                  R                  U5      (       a  [        R                  " U5        [        R                  " Xx5        g g )Nr  r  r  r3  r  rn  r   rm  rl  ro  )r  rB   rC   r  r  r7  r"   r#   r"  rD   rp  r]  rq  )	rm   r  r9  namesrf  suffixdir_patholdnews	            rK   r  %InstallCommand._rename_shared_library  sC   D-. 6t.56t.5 	
 "''--"<"< ++- (+hs#XJ9 z %00::( 	 77<<9eCRj9ggll8z&%:;ggll8zfX%>?77>>#ww~~c""		#IIc rL   c                .   [        U SS5      =(       d!    [        U SS5      =(       d    [        U SS5      nU(       a$  [        R                  R                  U5      (       d  gU R	                  5       nSU;   a  UR                  S5      SS OU/n[        R                  R                  " U6 nSU;   a  UR                  S5      S   OUn[        R                  R                  X5      n[        R                  R                  X S35      n[        (       a  SO[        R                  " S	5      (       a  S
OSn[        R                  R                  X SU 35      [        R                  R                  X U 35      /n	[        S U	 5       S5      n
[        R                  R                  U5      (       d  [        R                  " USS9  [        R                  R                  U5      (       ap  [        R                  R                  US5      n[        R                  R                  U5      (       a  [        R                  " U5        [        R                  " X{5        U
(       a  [        R                  R                  U
5      (       a  [        R                  R                  U[        R                  R                  U
5      5      n[        R                  R                  U5      (       a  [        R                  " U5        [        R                  " X5        ggg)a:  
Ensure only one top-level item in install_dir contains the package name by:
  - moving {pkg}.py -> {pkg}/__init__.py
  - moving {pkg}_pd_.so -> {pkg}/{pkg}_pd_.so
  - removing any {pkg}-*.egg-info left by setuptools install (only if dist-info exists)
This keeps legacy tests that scan os.listdir(site_dir) happy.
r  Nr  r  r3  r  re  rn  r   rm  rl  ro  c              3  r   #    U  H-  n[         R                  R                  U5      (       d  M)  Uv   M/     g 7frh   )rB   rC   rp  )r   ps     rK   r   6InstallCommand._single_entry_layout.<locals>.<genexpr>  s!     E-Q277>>!3Dqq-s   (7	7T)exist_okz__init__.py)r  rB   rC   r  r  r7  rD   r"   r#   r"  nextmakedirsrp  r]  replacer]   )rm   r  r9  pkg_path_partspkg_pathrf  pkg_dirpy_srcsuf_soso_candidatesso_srcpy_dstso_dsts                rK   r  #InstallCommand._single_entry_layout  s9    D-. 6t.56t.5 	
 "''--"<"< ++-
 ),xHNN3$hZ 	 77<<0 /2Xo8>>#&r*8 '',,{5kZs+;< z %00::( 	 GGLL
$vh&?@GGLL
6(&;<
 E-EtLww}}W%%KK$/77>>&!!WW\\'=9Fww~~f%%		&!JJv&bggnnV,,WW\\'277+;+;F+CDFww~~f%%		&!JJv&	 -6rL   )r  r  r  )r}  r  r  r~  )rs   rt   ru   rv   r  r  r   rz  r  r  rw   r  r  s   @rK   rA   rA   T  s)    
5C.J(>  D:' :'rL   rA   c	                   Uc  [        U5      n[        R                  R                  U5      n[	        SU 3U5        [        R                  R                  Xp S35      n	U V
s/ s H"  n
[        R                  R                  U
5      PM$     nn
Uc  / nUc  / n[        U[        5      (       d
   SU 35       e[        U[        5      (       d
   SU 35       e[	        SR                  SR                  U5      SR                  U5      5      U5        [        R                  R                  Xp5      n[        U UU	UUUUUUU5
        [        X5        [        XU5      nU$ s  sn
f )a8  
An Interface to automatically compile C++/CUDA source files Just-In-Time
and return callable python function as other Paddle layers API. It will
append user defined custom operators in background while building models.

It will perform compiling, linking, Python API generation and module loading
processes under a individual subprocess. It does not require CMake or Ninja
environment. On Linux platform, it requires GCC compiler whose version is
greater than 5.4 and it should be soft linked to ``/usr/bin/cc`` . On Windows
platform, it requires Visual Studio whose version is greater than 2017.
On MacOS, clang++ is requited. In addition, if compiling Operators supporting
GPU device, please make sure ``nvcc`` compiler is installed in local environment.

Moreover, `ABI compatibility <https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html>`_
will be checked to ensure that compiler version from ``cc(Linux)`` , ``cl.exe(Windows)``
on local machine is compatible with pre-installed Paddle whl in python site-packages.

For Linux, GCC version will be checked . For example if Paddle with CUDA 10.1 is built with GCC 8.2,
then the version of user's local machine should satisfy GCC >= 8.2.
For Windows, Visual Studio version will be checked, and it should be greater than or equal to that of
PaddlePaddle (Visual Studio 2017).
If the above conditions are not met, the corresponding warning will be printed, and a fatal error may
occur because of ABI compatibility.

Compared with ``setup`` interface, it doesn't need extra ``setup.py`` and execute
``python setup.py install`` command. The interface contains all compiling and installing
process underground.

Note:

    1. Currently we support Linux, MacOS and Windows platform.
    2. On Linux platform, we recommend to use GCC 8.2 as soft linking candidate of ``/usr/bin/cc`` .
       Then, Use ``which cc`` to ensure location of ``cc`` and using ``cc --version`` to ensure linking
       GCC version.
    3. On Windows platform, we recommend to install `` Visual Studio`` (>=2017).


**A simple example:**

.. code-block:: text

    import paddle
    from paddle.utils.cpp_extension import load

    custom_op_module = load(
        name="op_shared_library_name",                # name of shared library
        sources=['relu_op.cc', 'relu_op.cu'],        # source files of customized op
        extra_cxx_cflags=['-g', '-w'],               # optional, specify extra flags to compile .cc/.cpp file
        extra_cuda_cflags=['-O2'],                   # optional, specify extra flags to compile .cu file
        verbose=True                                 # optional, specify to output log information
    )

    x = paddle.randn([4, 10], dtype='float32')
    out = custom_op_module.relu(x)


Args:
    name(str): Specify the name of generated shared library file name, not including ``.so`` and ``.dll`` suffix.
    sources(list[str]): Specify source files name of customized operators.  Supporting ``.cc`` , ``.cpp`` for CPP file
                        and ``.cu`` for CUDA file.
    extra_cxx_cflags(list[str]|None, optional): Specify additional flags used to compile CPP files. By default
                           all basic and framework related flags have been included.
    extra_cuda_cflags(list[str]|None, optional): Specify additional flags used to compile CUDA files. By default
                           all basic and framework related flags have been included.
                           See `Cuda Compiler Driver NVCC <https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html>`_
                           for details. Default is None.
    extra_ldflags(list[str]|None, optional): Specify additional flags used to link shared library. See
                            `GCC Link Options <https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html>`_ for details.
                            Default is None.
    extra_include_paths(list[str]|None, optional): Specify additional include path used to search header files. By default
                            all basic headers are included implicitly from ``site-package/paddle/include`` .
                            Default is None.
    extra_library_paths(list[str]|None, optional): Specify additional library path used to search library files. By default
                            all basic libraries are included implicitly from ``site-packages/paddle/libs`` .
                            Default is None.
    build_directory(str|None, optional): Specify root directory path to put shared library file. If set None,
                        it will use ``PADDLE_EXTENSION_DIR`` from os.environ. Use
                        ``paddle.utils.cpp_extension.get_build_directory()`` to see the location. Default is None.
    verbose(bool, optional): whether to verbose compiled log information. Default is False.

Returns:
    Module: A callable python module contains all CustomOp Layer APIs.

zbuild_directory: z	_setup.pyz;Required type(extra_cxx_cflags) == list[str], but received z<Required type(extra_cuda_cflags) == list[str], but received z:additional extra_cxx_cflags: [{}], extra_cuda_cflags: [{}]r   )r   rB   rC   r   r   rD   r8   r>   formatr   r   r   )r1   rU   extra_cxx_cflagsextra_cuda_cflagsextra_ldflagsextra_include_pathsextra_library_pathsr  r   	file_pathra   build_base_dircustom_op_apis                rK   loadr  ,  sr   @ -g6 ggooo6O	o.
/9_i.@AI5<=W6rwwv&WG= &-- 
EFVEWX- '.. 
FGXFYZ. 
DKKHH%&1B(C	
 		 WW\\/8N $ 0gNMQ >s   #)Ec                x  ^ U b/  U  H)  m[        U4S jS 5       5      (       a  M  ST;   d  M'  / s  $    [        R                  " / SQ5      n/ SQnUU Vs/ s H  o3S-   PM	     sn-   n[        R                  R                  S5      nU(       Gd  [        R                  " S5        / n[        R                  " 5       n[        R                  " 5       (       a  [        [        R                  R                  R                  5       5       HQ  n[        R                  R                  R!                  U5      n	U	S	    S
U	S    3n
X;  d  M@  UR#                  U
5        MS     [%        U5      nU(       a  US==   S-  ss'   GOU(       a  [        R&                  " US	   5      (       a  [        [        R                  R                  5       5       HK  n[        R                  R!                  US	   U5      n	U	S	    S
U	S    3n
X;  d  M:  UR#                  U
5        MM     [%        U5      nU(       a  US==   S-  ss'   OX[)        S5      eUR+                  SS5      nUR-                  5        H  u  pUR+                  X5      nM     UR/                  S5      n/ nU H  n
X;  a  [1        SU
 S35      eU
R/                  S5      S	   nUR/                  S
5      u  nnU U 3nUR#                  SU SU 35        U
R3                  S5      (       d  Mt  UR#                  SU SU 35        M     [%        [5        U5      5      $ s  snf )z
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.
c              3  ,   >#    U  H	  oT;   v   M     g 7frh   r6   )r   xr  s     rK   r   '_get_cuda_arch_flags.<locals>.<genexpr>  s     @&?9&?s   )PADDLE_EXTENSION_NAMEarch)
)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Blackwell+Tegra10.1)	Blackwellz10.0;12.0+PTX)z6.0z6.1z6.2z7.0r  z7.5z8.0z8.6r  z8.9z9.0z9.0az10.0z10.0ar  z10.1az12.0z12.0az+PTXPADDLE_CUDA_ARCH_LISTzPADDLE_CUDA_ARCH_LIST are not set, all archs for visible cards are included for compilation. 
If this is not desired, please set os.environ['PADDLE_CUDA_ARCH_LIST'].r   r3  r   r  zNPaddle is not compiled with CUDA or Custom Device, cannot determine CUDA arch.r   ;zUnknown CUDA arch (z) or GPU not supported+z-gencode=arch=compute_z	,code=sm_z,code=compute_)rT  collectionsOrderedDictrB   rL  r7   warningswarnr(   get_all_custom_device_typeis_compiled_with_cudar   paddledevicecudadevice_countget_device_capabilityr_   sortedr   r   r  itemsr7  r<   r=   r  )r   named_archessupported_archesrQ  valid_arch_strings
_arch_list	arch_list	dev_typesdev_id
capabilityr  
named_archarchivalflagsversionmajorminornumr  s                     @rK   _get_cuda_arch_flagsr    s    D@&?@@@~		  **	
L( *,-,qF
,-   78JV	
 	335	%%'' 2 2 ? ? AB#]]//EE
 %Q-*Q-9($$T* C y)I"'4>>y|LL : : <=#]]@@aL&
 %Q-*Q-9($$T* > y)I"'`   ''S1
$0$6$6$8 J#++JAJ %9$$S)	E)24&8NOPP**S/!$}}S)uw-cU)C5AB==  LL1#nSEJK  #e*o-s   L7c                     / n S HJ  n[        [        R                  SU 35      nUc  M%  [        (       a  M2  U R	                  SU SU S35        ML     U $ )N)COMPILER_TYPESTDLIB	BUILD_ABI
_PYBIND11_z-DPYBIND11_z=\"z\")r  r  _Cr"   r_   )
abi_cflagspnamepvals      rK   _get_pybind11_abi_build_flagsr  .  sW    J9vyyJug"67JJE7$tfC@A : rL   c                   [         R                  R                  S5      nUbC  UR                  5       (       a.  U (       a  [	        SU S3[
        R                  S9  [        U5      $ U (       a  [	        S[
        R                  S9  g )NMAX_JOBSzUsing envvar MAX_JOBS (z) as the number of workers...)ra  zqAllowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N))rB   rL  r7   isdigitr   r  stderrint)r   max_jobss     rK   r   r   7  sp    zz~~j)H 0 0 2 2)(3PQZZ 8}K	

 rL   c                h    U=(       d    SnU c  [        X5      nU$ [        S[        XU5      5      nU$ )Nr   )minmax)r   r   num_objectsr   s       rK   r   r   I  sA     QI I3  As#4MNrL   )rH   r   r}  r  )rU   Sequence[str]rV   r   rW   r   r}  zsetuptools.Extension)NNNNNNF)r1   r  rU   r*  r  Sequence[str] | Noner  r+  r  r+  r  r+  r  r+  r  r  r   r   r}  r+   rh   )r   r!  r}  z	list[str])r   r   r}  
int | None)r   r,  r   r,  r)  r$  r}  r$  )S
__future__r   typingr   r   rB   r   r   r   r  r  rF   r  r  r  distutils.errorsr   r   setuptools.command.easy_installr   setuptools.command.build_extr	   distutils.command.buildr
   setuptools.command.installr   extension_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   baser(   concurrent.futuresr)   collections.abcr*   typesr+   distutils.command.build_ext_du_build_extunittest.mockr,   get_export_symbolsr   r   r   r   rG   rX   rZ   rR   r:   r@   rE   rA   r  r  r  r   r   r6   rL   rK   <module>r=     s/  " # % 	   	    
   : 8 2 ) .  
 
  L  
 C  1 ( 
 F"'+'>M$	 II ^B1@1@#&1@251@1@h3@3@#&3@253@3@l! I
)Y I
)X 3  3F"95 "9JU'W U'v .2.2*.0404"&Q
QQ +Q ,	Q
 (Q .Q .Q  Q Q Qhk\$!.8GJrL   