Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions python/paddle/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,7 @@ def set_paddle_custom_device_lib_path(lib_path):

# set paddle lib path
def set_paddle_lib_path():
site_dirs = (
site.getsitepackages()
if hasattr(site, 'getsitepackages')
else [x for x in sys.path if 'site-packages' in x]
)
site_dirs = site.getsitepackages()
for site_dir in site_dirs:
lib_dir = os.path.sep.join([site_dir, 'paddle', 'libs'])
if os.path.exists(lib_dir):
Expand Down
26 changes: 18 additions & 8 deletions python/paddle/utils/cpp_extension/extension_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,23 +821,33 @@ def find_rocm_includes():
return [os.path.join(rocm_home, 'include')]


def find_paddle_includes(use_cuda=False):
def _get_all_paddle_includes_from_include_root(include_root: str) -> list[str]:
"""
Return Paddle necessary include dir path.
Get all paddle include directories from include root (packaged in wheel)
Comment on lines +824 to +826
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is missing a docstring that explains its parameters and return value. Consider adding documentation for the include_root parameter to clarify its expected format and purpose.

Copilot uses AI. Check for mistakes.
"""
# pythonXX/site-packages/paddle/include
paddle_include_dir = get_include()
third_party_dir = os.path.join(paddle_include_dir, 'third_party')
include_dirs = [paddle_include_dir, third_party_dir]
third_party_dir = os.path.join(include_root, 'third_party')
include_dirs = [include_root, third_party_dir]
if not IS_WINDOWS:
compat_dir_root = os.path.join(
paddle_include_dir, 'paddle/phi/api/include/compat'
include_root, 'paddle/phi/api/include/compat'
)
compat_dir_api_include = os.path.join(
paddle_include_dir,
include_root,
'paddle/phi/api/include/compat/torch/csrc/api/include',
)
include_dirs.extend([compat_dir_root, compat_dir_api_include])
return include_dirs


def find_paddle_includes(use_cuda=False):
"""
Return Paddle necessary include dir path.
"""
# pythonXX/site-packages/paddle/include
paddle_include_dir = get_include()
include_dirs = _get_all_paddle_includes_from_include_root(
paddle_include_dir
)

if use_cuda:
if core.is_compiled_with_rocm():
Expand Down
19 changes: 6 additions & 13 deletions test/auto_parallel/custom_op/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from pathlib import Path
from site import getsitepackages

from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
_get_all_paddle_includes_from_include_root,
)

# Test for extra compile args
extra_cc_args = ['-w', '-g']
Expand All @@ -38,18 +40,9 @@ def get_paddle_includes():

for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)

return paddle_includes

Expand Down
22 changes: 9 additions & 13 deletions test/auto_parallel/semi_auto_parallel_for_custom_relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import paddle
import paddle.distributed as dist
from paddle.utils.cpp_extension import get_build_directory, load
from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS, run_cmd
from paddle.utils.cpp_extension.extension_utils import (
IS_WINDOWS,
_get_all_paddle_includes_from_include_root,
run_cmd,
)

# Note(Aurelius84): We use `add_test` in Cmake to config how to run unittest in CI.
# `PYTHONPATH` will be set as `build/python/paddle` that will make no way to find
Expand All @@ -30,18 +34,10 @@
paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)


# Test for extra compile args
extra_cc_args = ['-w', '-g'] if not IS_WINDOWS else ['/w']
Expand Down
22 changes: 9 additions & 13 deletions test/auto_parallel/semi_auto_parallel_simple_net_custom_relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import paddle.nn.functional as F
from paddle import nn
from paddle.utils.cpp_extension import get_build_directory, load
from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS, run_cmd
from paddle.utils.cpp_extension.extension_utils import (
IS_WINDOWS,
_get_all_paddle_includes_from_include_root,
run_cmd,
)

# Note(Aurelius84): We use `add_test` in Cmake to config how to run unittest in CI.
# `PYTHONPATH` will be set as `build/python/paddle` that will make no way to find
Expand All @@ -32,18 +36,10 @@
paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)


# Test for extra compile args
extra_cc_args = ['-w', '-g'] if not IS_WINDOWS else ['/w']
Expand Down
20 changes: 7 additions & 13 deletions test/cpp_extension/cpp_extension_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,17 @@

import paddle
from paddle.utils.cpp_extension import CppExtension, CUDAExtension, setup
from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
_get_all_paddle_includes_from_include_root,
)

paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)

# Add current dir, search custom_power.h
paddle_includes.append(os.path.dirname(os.path.abspath(__file__)))

Expand Down
20 changes: 7 additions & 13 deletions test/cpp_extension/test_cpp_extension_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import paddle
from paddle.utils.cpp_extension import load
from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
_get_all_paddle_includes_from_include_root,
)

if os.name == 'nt' or sys.platform.startswith('darwin'):
# only support Linux now
Expand All @@ -37,18 +39,10 @@
paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)

# include "custom_power.h"
paddle_includes.append(os.path.dirname(os.path.abspath(__file__)))

Expand Down
21 changes: 8 additions & 13 deletions test/cpp_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import numpy as np

from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
IS_WINDOWS,
_get_all_paddle_includes_from_include_root,
)

IS_MAC = sys.platform.startswith('darwin')

Expand All @@ -29,18 +32,10 @@
paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)


# Test for extra compile args
extra_cc_args = ['-w', '-g'] if not IS_WINDOWS else ['/w']
Expand Down
6 changes: 1 addition & 5 deletions test/custom_kernel/test_custom_kernel_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ def setUp(self):

# get paddle lib path and place so
paddle_lib_path = ''
site_dirs = (
site.getsitepackages()
if hasattr(site, 'getsitepackages')
else [x for x in sys.path if 'site-packages' in x]
)
site_dirs = site.getsitepackages()
for site_dir in site_dirs:
lib_dir = os.path.sep.join([site_dir, 'paddle', 'libs'])
if os.path.exists(lib_dir):
Expand Down
24 changes: 9 additions & 15 deletions test/custom_op/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
from pathlib import Path
from site import getsitepackages

import numpy as np

from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
IS_WINDOWS,
_get_all_paddle_includes_from_include_root,
)

IS_MAC = sys.platform.startswith('darwin')

Expand All @@ -31,19 +33,11 @@
paddle_libraries = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_libraries.append(os.path.join(site_packages_path, 'paddle', 'libs'))
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)

paddle_libraries.append(str(Path(site_packages_path) / 'paddle' / 'libs'))

# Test for extra compile args
extra_cc_args = ['-w', '-g'] if not IS_WINDOWS else ['/w']
Expand Down
19 changes: 7 additions & 12 deletions test/custom_runtime/test_custom_op_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import numpy as np

from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
_get_all_paddle_includes_from_include_root,
Comment on lines +24 to +25
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function _get_all_paddle_includes_from_include_root is marked as private with a leading underscore but is being imported and used across multiple test files. Consider making it a public API by removing the underscore prefix or keeping it private and accessing it through a public interface.

Suggested change
from paddle.utils.cpp_extension.extension_utils import (
_get_all_paddle_includes_from_include_root,
get_all_paddle_includes_from_include_root,

Copilot uses AI. Check for mistakes.
)


def custom_relu_dynamic(func, device, dtype, np_x, use_func=True):
Expand Down Expand Up @@ -140,18 +142,11 @@ def setUp(self):
paddle_includes = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(
str(paddle_include_dir)
)
)

custom_module = paddle.utils.cpp_extension.load(
name='custom_device',
Expand Down
24 changes: 9 additions & 15 deletions test/deprecated/custom_op/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
from pathlib import Path
from site import getsitepackages

import numpy as np

from paddle.utils.cpp_extension.extension_utils import IS_WINDOWS
from paddle.utils.cpp_extension.extension_utils import (
IS_WINDOWS,
_get_all_paddle_includes_from_include_root,
)

IS_MAC = sys.platform.startswith('darwin')

Expand All @@ -31,19 +33,11 @@
paddle_libraries = []
for site_packages_path in getsitepackages():
paddle_include_dir = Path(site_packages_path) / "paddle/include"
paddle_includes.append(str(paddle_include_dir))
paddle_includes.append(str(paddle_include_dir / 'third_party'))
if not IS_WINDOWS:
paddle_includes.append(
str(paddle_include_dir / 'paddle/phi/api/include/compat')
)
paddle_includes.append(
str(
paddle_include_dir
/ 'paddle/phi/api/include/compat/torch/csrc/api/include'
)
)
paddle_libraries.append(os.path.join(site_packages_path, 'paddle', 'libs'))
paddle_includes.extend(
_get_all_paddle_includes_from_include_root(str(paddle_include_dir))
)

paddle_libraries.append(str(Path(site_packages_path) / 'paddle' / 'libs'))

# Test for extra compile args
extra_cc_args = ['-w', '-g'] if not IS_WINDOWS else ['/w']
Expand Down