Skip to content

Commit

Permalink
Unify core avx and core_noavx to libpaddle (#46095)
Browse files Browse the repository at this point in the history
* unify  core_avx and core_noavx

* fix except error

* revert mac compile logic

* revert dylib to so

* add core_noavx branch

* remove core_noavx

* replace paddle_core by lib paddle

* polish var name

* replace paddle_core by libpaddle

* update custom device commit

* polish code by comments
  • Loading branch information
chenwhql authored Sep 16, 2022
1 parent fa97e5b commit 267d71a
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 244 deletions.
14 changes: 7 additions & 7 deletions paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,24 @@ if(WITH_PYTHON)
endif()

cc_library(
paddle_pybind SHARED
libpaddle SHARED
SRCS ${PYBIND_SRCS}
DEPS ${PYBIND_DEPS} ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS})

if(NOT ((NOT WITH_PYTHON) AND ON_INFER))
add_dependencies(paddle_pybind legacy_eager_codegen)
add_dependencies(paddle_pybind eager_legacy_op_function_generator_cmd)
add_dependencies(libpaddle legacy_eager_codegen)
add_dependencies(libpaddle eager_legacy_op_function_generator_cmd)
endif()

if(NOT APPLE AND NOT WIN32)
target_link_libraries(paddle_pybind rt)
target_link_libraries(libpaddle rt)
endif()

if(WITH_ROCM)
target_link_libraries(paddle_pybind ${ROCM_HIPRTC_LIB})
target_link_libraries(libpaddle ${ROCM_HIPRTC_LIB})
endif()

get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES)
target_link_libraries(paddle_pybind ${os_dependency_modules})
add_dependencies(paddle_pybind op_function_generator_cmd)
target_link_libraries(libpaddle ${os_dependency_modules})
add_dependencies(libpaddle op_function_generator_cmd)
endif()
16 changes: 10 additions & 6 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ PyTypeObject *g_framework_scope_pytype = nullptr;
PyTypeObject *g_framework_lodtensorarray_pytype = nullptr;
PyTypeObject *g_custom_op_kernel_ctx_pytype = nullptr;

bool IsCompiledWithAVX() {
#ifndef PADDLE_WITH_AVX
return false;
#else
return true;
#endif
}

bool IsCompiledWithCUDA() {
#if !defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
return false;
Expand Down Expand Up @@ -576,12 +584,7 @@ static int GetNCCLVersion() {
}
#endif

#ifdef PADDLE_WITH_AVX
PYBIND11_MODULE(core_avx, m) {
#else
PYBIND11_MODULE(core_noavx, m) {
#endif

PYBIND11_MODULE(libpaddle, m) {
BindImperative(&m);
BindEager(&m);
BindEagerStringTensor(&m);
Expand Down Expand Up @@ -1706,6 +1709,7 @@ All parameter, weight, gradient are variables in Paddle.
});
m.def("init_default_kernel_signatures",
[]() { framework::InitDefaultKernelSignatureMap(); });
m.def("is_compiled_with_avx", IsCompiledWithAVX);
m.def("is_compiled_with_cuda", IsCompiledWithCUDA);
m.def("is_compiled_with_ascend", IsCompiledWithAscend);
m.def("is_compiled_with_rocm", IsCompiledWithROCM);
Expand Down
41 changes: 26 additions & 15 deletions paddle/scripts/paddle_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ EOF
-DWITH_DISTRIBUTE=${distibuted_flag} \
-DWITH_MKL=${WITH_MKL:-ON} \
-DWITH_AVX=${WITH_AVX:-OFF} \
-DNOAVX_CORE_FILE=${NOAVX_CORE_FILE:-""} \
-DCUDA_ARCH_NAME=${CUDA_ARCH_NAME:-All} \
-DNEW_RELEASE_PYPI=${NEW_RELEASE_PYPI:-OFF} \
-DNEW_RELEASE_ALL=${NEW_RELEASE_ALL:-OFF} \
Expand Down Expand Up @@ -546,23 +545,26 @@ EOF
}


function combine_avx_noavx_build() {
mkdir -p ${PADDLE_ROOT}/build.noavx
cd ${PADDLE_ROOT}/build.noavx
WITH_AVX=OFF
function avx_build() {
mkdir -p ${PADDLE_ROOT}/build
cd ${PADDLE_ROOT}/build
WITH_AVX=ON

cmake_base ${PYTHON_ABI:-""}
build_base
}

# build combined one

function noavx_build() {
mkdir -p ${PADDLE_ROOT}/build
cd ${PADDLE_ROOT}/build
NOAVX_CORE_FILE=`find ${PADDLE_ROOT}/build.noavx/python/paddle/fluid/ -name "core_noavx.*"`
WITH_AVX=ON
WITH_AVX=OFF

cmake_base ${PYTHON_ABI:-""}
build_base
}


function mac_m1_arm_build() {
mkdir -p ${PADDLE_ROOT}/build
cd ${PADDLE_ROOT}/build
Expand Down Expand Up @@ -2887,12 +2889,12 @@ EOF
local LIB_TYPE=$1
case $LIB_TYPE in
full)
# Build full Paddle Python module. Will timeout without caching 'copy_paddle_pybind' first
make -j `nproc` framework_py_proto copy_paddle_pybind paddle_python
# Build full Paddle Python module. Will timeout without caching 'copy_libpaddle' first
make -j `nproc` framework_py_proto copy_libpaddle paddle_python
;;
pybind)
# Build paddle pybind library. Takes 49 minutes to build. Might timeout
make -j `nproc` copy_paddle_pybind
make -j `nproc` copy_libpaddle
;;
proto)
# Even smaller library.
Expand Down Expand Up @@ -3485,16 +3487,25 @@ function main() {
gen_dockerfile ${PYTHON_ABI:-""}
assert_api_spec_approvals
;;
combine_avx_noavx)
combine_avx_noavx_build
avx_build)
avx_build
gen_dockerfile ${PYTHON_ABI:-""}
;;
noavx_build)
noavx_build
gen_dockerfile ${PYTHON_ABI:-""}
;;
mac_m1_arm)
mac_m1_arm_build
gen_dockerfile ${PYTHON_ABI:-""}
;;
combine_avx_noavx_build_and_test)
combine_avx_noavx_build
avx_build_and_test)
avx_build
gen_dockerfile ${PYTHON_ABI:-""}
parallel_test_base
;;
noavx_build_and_test)
noavx_build
gen_dockerfile ${PYTHON_ABI:-""}
parallel_test_base
;;
Expand Down
6 changes: 3 additions & 3 deletions paddle/scripts/windows_build/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ echo Current directory : %cd%

call:rest_env

echo cmake %dst_path%\..\Paddle -G "Visual Studio 15 2017 Win64" -T host=x64 -DWITH_GPU=%WITH_GPU% -DWITH_MKL=%WITH_MKL% -DWITH_AVX=ON -DPYTHON_INCLUDE_DIR=%PYTHON_DIR%\include\ -DPYTHON_LIBRARY=%PYTHON_DIR%\libs\ -DPYTHON_EXECUTABLE=%PYTHON_DIR%\python.exe -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=OFF -DWITH_PYTHON=ON -DCUDA_TOOLKIT_ROOT_DIR=%CUDA_DIR% -DCUDA_ARCH_NAME=All -DNOAVX_CORE_FILE=%dst_path%_noavx\python\paddle\fluid\core_noavx.pyd
cmake %dst_path%\..\Paddle -G "Visual Studio 15 2017 Win64" -T host=x64 -DWITH_GPU=%WITH_GPU% -DWITH_MKL=%WITH_MKL% -DWITH_AVX=ON -DPYTHON_INCLUDE_DIR=%PYTHON_DIR%\include\ -DPYTHON_LIBRARY=%PYTHON_DIR%\libs\ -DPYTHON_EXECUTABLE=%PYTHON_DIR%\python.exe -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=OFF -DWITH_PYTHON=ON -DCUDA_TOOLKIT_ROOT_DIR=%CUDA_DIR% -DCUDA_ARCH_NAME=All -DNOAVX_CORE_FILE=%dst_path%_noavx\python\paddle\fluid\core_noavx.pyd
echo cmake %dst_path%\..\Paddle -G "Visual Studio 15 2017 Win64" -T host=x64 -DWITH_GPU=%WITH_GPU% -DWITH_MKL=%WITH_MKL% -DWITH_AVX=ON -DPYTHON_INCLUDE_DIR=%PYTHON_DIR%\include\ -DPYTHON_LIBRARY=%PYTHON_DIR%\libs\ -DPYTHON_EXECUTABLE=%PYTHON_DIR%\python.exe -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=OFF -DWITH_PYTHON=ON -DCUDA_TOOLKIT_ROOT_DIR=%CUDA_DIR% -DCUDA_ARCH_NAME=All
cmake %dst_path%\..\Paddle -G "Visual Studio 15 2017 Win64" -T host=x64 -DWITH_GPU=%WITH_GPU% -DWITH_MKL=%WITH_MKL% -DWITH_AVX=ON -DPYTHON_INCLUDE_DIR=%PYTHON_DIR%\include\ -DPYTHON_LIBRARY=%PYTHON_DIR%\libs\ -DPYTHON_EXECUTABLE=%PYTHON_DIR%\python.exe -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=OFF -DWITH_PYTHON=ON -DCUDA_TOOLKIT_ROOT_DIR=%CUDA_DIR% -DCUDA_ARCH_NAME=All

set MSBUILDDISABLENODEREUSE=1

Expand Down Expand Up @@ -184,4 +184,4 @@ exit /b 1
:END
echo BUILD SUCCESSFULLY

ENDLOCAL
ENDLOCAL
71 changes: 11 additions & 60 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,7 @@ endif()

set(SETUP_LOG_FILE "setup.py.log")

set(FLUID_CORE_NAME "core")
if(WITH_AVX AND AVX_FOUND)
set(FLUID_CORE_NAME "${FLUID_CORE_NAME}_avx")
if(NOT DEFINED NOAVX_CORE_FILE OR NOAVX_CORE_FILE STREQUAL "")
message(
STATUS
"MESSAGE: This is just a message for publishing release.
You are building AVX version without NOAVX core.
So the wheel package may fail on NOAVX machine.
You can add -DNOAVX_CORE_FILE=/path/to/your/core_noavx.* in cmake command
to get a full wheel package to resolve this warning.
While, this version will still work on local machine.")
endif()

if(NOAVX_CORE_FILE AND NOT EXISTS "${NOAVX_CORE_FILE}")
message(FATAL_ERROR "The file ${NOAVX_CORE_FILE} does not exist!")
endif()

set(HAS_NOAVX_CORE ON)
else()
set(FLUID_CORE_NAME "${FLUID_CORE_NAME}_noavx")
endif()
set(FLUID_CORE_NAME "libpaddle")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
${CMAKE_CURRENT_BINARY_DIR}/setup.py)
Expand All @@ -55,48 +34,20 @@ if(WIN32)

add_custom_command(
OUTPUT ${FLUID_CORE}
COMMAND cmake -E copy $<TARGET_FILE:paddle_pybind> ${FLUID_CORE}
COMMAND cmake -E copy $<TARGET_LINKER_FILE:paddle_pybind> ${FLUID_CORE_LIB}
DEPENDS paddle_pybind)

set(FLUID_NOAVX_CORE ${FLUID_DST_DIR}/core_noavx.pyd)
COMMAND cmake -E copy $<TARGET_FILE:libpaddle> ${FLUID_CORE}
COMMAND cmake -E copy $<TARGET_LINKER_FILE:libpaddle> ${FLUID_CORE_LIB}
DEPENDS libpaddle)
else()
set(FLUID_CORE ${FLUID_DST_DIR}/${FLUID_CORE_NAME}.so)
add_custom_command(
OUTPUT ${FLUID_CORE}
COMMAND cmake -E copy $<TARGET_FILE:paddle_pybind> ${FLUID_CORE}
DEPENDS paddle_pybind)

set(FLUID_NOAVX_CORE ${FLUID_DST_DIR}/core_noavx.so)
COMMAND cmake -E copy $<TARGET_FILE:libpaddle> ${FLUID_CORE}
DEPENDS libpaddle)
endif()

set(FLUID_CORE_DEPS ${FLUID_CORE})

if(HAS_NOAVX_CORE AND EXISTS "${NOAVX_CORE_FILE}")
get_filename_component(NOAVX_CORE_NAME ${NOAVX_CORE_FILE} NAME)
get_filename_component(NOAVX_CORE_EXT ${NOAVX_CORE_FILE} EXT)
if(WIN32)
if(NOT NOAVX_CORE_EXT STREQUAL ".pyd")
message(
FATAL_ERROR
"Wrong file ${NOAVX_CORE_NAME}, the ext does not match windows *.pyd!"
)
endif()
else()
if(NOT NOAVX_CORE_EXT STREQUAL ".so")
message(
FATAL_ERROR
"Wrong file ${NOAVX_CORE_NAME}, the ext does not match *.so!")
endif()
endif()
add_custom_command(
OUTPUT ${FLUID_NOAVX_CORE}
COMMAND cmake -E copy ${NOAVX_CORE_FILE} ${FLUID_NOAVX_CORE}
DEPENDS paddle_pybind)
list(APPEND FLUID_CORE_DEPS ${FLUID_NOAVX_CORE})
endif()

add_custom_target(copy_paddle_pybind ALL DEPENDS ${FLUID_CORE_DEPS})
add_custom_target(copy_libpaddle ALL DEPENDS ${FLUID_CORE_DEPS})

if(WIN32)
add_custom_command(
Expand All @@ -107,17 +58,17 @@ if(WIN32)
COMMAND ${CMAKE_COMMAND} -E env ${py_env} ${PYTHON_EXECUTABLE} setup.py
bdist_wheel
COMMENT "Packing whl packages------>>>"
DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto
profiler_py_proto pass_desc_py_proto ${PY_FILES})
DEPENDS copy_libpaddle ${FLUID_CORE} framework_py_proto profiler_py_proto
pass_desc_py_proto ${PY_FILES})
else()
add_custom_command(
OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp
COMMAND touch stub.cc
COMMAND cp -r ${PADDLE_SOURCE_DIR}/python/paddle ${PADDLE_BINARY_DIR}/python
COMMAND env ${py_env} ${PYTHON_EXECUTABLE} setup.py bdist_wheel
COMMENT "Packing whl packages------>>>"
DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto
profiler_py_proto pass_desc_py_proto ${PY_FILES})
DEPENDS copy_libpaddle ${FLUID_CORE} framework_py_proto profiler_py_proto
pass_desc_py_proto ${PY_FILES})
endif()

add_custom_target(paddle_python ALL
Expand Down
Loading

0 comments on commit 267d71a

Please sign in to comment.