Skip to content

Commit 1943d97

Browse files
authored
[XPU]add support for XPUFFT (#72477)
* add cmake * fix * fix * fix setup.in
1 parent 4285943 commit 1943d97

File tree

6 files changed

+125
-2
lines changed

6 files changed

+125
-2
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ option(WITH_XPU_KP "Compile PaddlePaddle with BAIDU XPU compiler " OFF)
5656
option(WITH_XPU_XFT "Compile PaddlePaddle with BAIDU XPU-XFT" OFF)
5757
option(WITH_XPU_PLUGIN "Compile PaddlePaddle with BAIDU XPU plugin" OFF)
5858
option(WITH_XPU_XRE5 "Compile PaddlePaddle with BAIDU XPU XRE 5" OFF)
59+
option(WITH_XPU_FFT "Compile PaddlePaddle with BAIDU XPU FFT" OFF)
5960
option(WITH_WIN_DUMP_DBG "Compile with windows core dump debug mode" OFF)
6061
option(WITH_ROCM "Compile PaddlePaddle with ROCM platform" OFF)
6162
option(WITH_IPU "Compile PaddlePaddle with Graphcore IPU" OFF)
@@ -95,6 +96,9 @@ endif()
9596
if(WITH_GPU AND WITH_XPU_XRE5)
9697
message(FATAL_ERROR "Error when compile GPU and XPU-XRE5 at the same time")
9798
endif()
99+
if(WITH_GPU AND WITH_XPU_FFT)
100+
message(FATAL_ERROR "Error when compile GPU and XPU-FFT at the same time")
101+
endif()
98102
if(WITH_GPU AND WITH_ROCM)
99103
message(FATAL_ERROR "Error when compile CUDA and ROCM at the same time")
100104
endif()
@@ -478,6 +482,14 @@ if(NOT WITH_XPU AND WITH_XPU_XRE5)
478482
CACHE STRING "Disable XRE5 when compiling without XPU" FORCE)
479483
endif()
480484

485+
if(NOT WITH_XPU AND WITH_XPU_FFT)
486+
message((WARNING
487+
"Disable FFT when compiling without XPU. Force WITH_XPU_FFT=OFF"))
488+
set(WITH_XPU_FFT
489+
OFF
490+
CACHE STRING "Disable FFT when compiling without XPU" FORCE)
491+
endif()
492+
481493
if(WITH_NCCL)
482494
add_definitions("-DPADDLE_WITH_NCCL")
483495
include(nccl)

cmake/external/xpu.cmake

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(XPU_XPTI_LIB_NAME "libxpti.so")
2828
set(XPU_XBLAS_LIB_NAME "libxpu_blas.so")
2929
set(XPU_XFA_LIB_NAME "libxpu_flash_attention.so")
3030
set(XPU_XPUDNN_LIB_NAME "libxpu_dnn.so")
31+
set(XPU_FFT_LIB_NAME "libcufft.so")
3132

3233
if(NOT DEFINED XPU_XHPC_BASE_DATE)
3334
set(XPU_XHPC_BASE_DATE "dev/20250417")
@@ -51,6 +52,10 @@ else()
5152
set(XPU_XPTI_BASE_VERSION "0.0.1")
5253
endif()
5354

55+
if(NOT DEFINED XPU_FFT_BASE_DATE)
56+
set(XPU_FFT_BASE_DATE "20250425")
57+
endif()
58+
5459
set(XPU_XRE_BASE_URL
5560
"https://klx-sdk-release-public.su.bcebos.com/xre/release/${XPU_XRE_BASE_VERSION}"
5661
)
@@ -81,6 +86,13 @@ if(WITH_XPU_XRE5)
8186
)
8287
endif()
8388

89+
if(WITH_XPU_FFT)
90+
set(XPU_FFT_BASE_URL
91+
"https://klx-sdk-release-public.su.bcebos.com/xpufft/kl3/${XPU_FFT_BASE_DATE}"
92+
)
93+
set(XPU_FFT_DIR_NAME "xpufft_ubuntu2004-x86_64")
94+
endif()
95+
8496
if(WITH_AARCH64)
8597
set(XPU_XRE_DIR_NAME "xre-kylin_aarch64")
8698
set(XPU_XCCL_DIR_NAME "") # TODO: xccl has no kylin output now.
@@ -130,6 +142,10 @@ if(WITH_XPTI)
130142
set(XPU_XPTI_URL "${XPU_XPTI_BASE_URL}/${XPU_XPTI_DIR_NAME}.tar.gz")
131143
endif()
132144

145+
if(WITH_XPU_FFT)
146+
set(XPU_FFT_URL "${XPU_FFT_BASE_URL}/${XPU_FFT_DIR_NAME}.tar.gz")
147+
endif()
148+
133149
set(XPU_XHPC_URL
134150
"https://klx-sdk-release-public.su.bcebos.com/xhpc/${XPU_XHPC_BASE_DATE}/${XPU_XHPC_DIR_NAME}.tar.gz"
135151
CACHE STRING "" FORCE)
@@ -205,6 +221,12 @@ if(DEFINED ENV{XPU_LIB_ROOT})
205221
set(XFT_COMMAND
206222
"${CMAKE_SOURCE_DIR}/tools/xpu/get_xft_dependence_from_custom_path.sh")
207223
endif()
224+
225+
# FFT
226+
if(DEFINED ENV{XPU_FFT_DIR_NAME})
227+
set(XPU_FFT_URL "${XPU_LIB_ROOT}/$ENV{XPU_FFT_DIR_NAME}")
228+
set(XPU_FFT_DIR_NAME "$ENV{XPU_FFT_DIR_NAME}")
229+
endif()
208230
endif()
209231

210232
if(WITH_XPU_XRE5)
@@ -219,7 +241,9 @@ if(WITH_XPU_XRE5)
219241
${XPU_XCCL_URL} ${XPU_XCCL_DIR_NAME} 1 && wget ${XPU_XFT_GET_DEPENCE_URL}
220242
&& bash ${XFT_COMMAND} ${XPU_XFT_URL} ${XPU_XFT_DIR_NAME} && bash
221243
${CMAKE_SOURCE_DIR}/tools/xpu/get_xpti_dependence.sh ${XPU_XPTI_URL}
222-
${XPU_XPTI_DIR_NAME}
244+
${XPU_XPTI_DIR_NAME} && bash
245+
${CMAKE_SOURCE_DIR}/tools/xpu/get_xpufft_dependence.sh ${XPU_FFT_URL}
246+
${XPU_FFT_DIR_NAME}
223247
DOWNLOAD_NO_PROGRESS 1
224248
UPDATE_COMMAND ""
225249
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${XPU_INSTALL_ROOT}
@@ -244,7 +268,9 @@ else()
244268
${XPU_XCCL_URL} ${XPU_XCCL_DIR_NAME} 0 && wget ${XPU_XFT_GET_DEPENCE_URL}
245269
&& bash get_xft_dependence.sh ${XPU_XFT_URL} ${XPU_XFT_DIR_NAME} && bash
246270
${CMAKE_SOURCE_DIR}/tools/xpu/get_xpti_dependence.sh ${XPU_XPTI_URL}
247-
${XPU_XPTI_DIR_NAME}
271+
${XPU_XPTI_DIR_NAME} && bash
272+
${CMAKE_SOURCE_DIR}/tools/xpu/get_xpufft_dependence.sh ${XPU_FFT_URL}
273+
${XPU_FFT_DIR_NAME}
248274
DOWNLOAD_NO_PROGRESS 1
249275
UPDATE_COMMAND ""
250276
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${XPU_INSTALL_ROOT}
@@ -272,6 +298,16 @@ if(WITH_XPU_XFT)
272298
target_link_libraries(xpulib ${XPU_XFT_LIB})
273299
endif()
274300

301+
if(WITH_XPU_FFT)
302+
message(STATUS "Compile with XPU FFT!")
303+
add_definitions(-DPADDLE_WITH_XPU_FFT)
304+
305+
set(XPU_FFT_INC_DIR "${XPU_INC_DIR}/fft")
306+
include_directories(${XPU_FFT_INC_DIR})
307+
set(XPU_FFT_LIB "${XPU_LIB_DIR}/${XPU_FFT_LIB_NAME}")
308+
target_link_libraries(xpulib ${XPU_FFT_LIB})
309+
endif()
310+
275311
set(XPU_XHPC_INC_DIR "${XPU_INC_DIR}/xhpc")
276312
include_directories(${XPU_XHPC_INC_DIR})
277313
set(XPU_XRE_INC_DIR "${XPU_INC_DIR}/xre")

python/env_dict.py.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ env_dict={
7171
'XPU_CUDA_LIB':'@XPU_CUDA_LIB@',
7272
'XPU_CUDA_LIB_NAME':'@XPU_CUDA_LIB_NAME@',
7373
'WITH_XPU_XRE5':'@WITH_XPU_XRE5@',
74+
'WITH_XPU_FFT':'@WITH_XPU_FFT@',
75+
'XPU_FFT_LIB':'@XPU_FFT_LIB@',
76+
'XPU_FFT_LIB_NAME':'@XPU_FFT_LIB_NAME@',
7477
'XPU_CUDA_RT_LIB':'@XPU_CUDA_RT_LIB@',
7578
'XPU_CUDA_RT_LIB_NAME':'@XPU_CUDA_RT_LIB_NAME@',
7679
'XPU_ML_LIB':'@XPU_ML_LIB@',

python/setup.py.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,12 @@ if '${WITH_XPU_BKCL}' == 'ON':
11551155
shutil.copy('${XPU_BKCL_LIB}', libs_path)
11561156
package_data['paddle.libs']+=['${XPU_BKCL_LIB_NAME}']
11571157

1158+
if '${WITH_XPU_FFT}' == 'ON':
1159+
xpu_fft_lib_list = glob.glob('${XPU_FFT_LIB}*')
1160+
for xpu_fft_lib_file in xpu_fft_lib_list:
1161+
shutil.copy(xpu_fft_lib_file, libs_path)
1162+
package_data['paddle.libs']+=[os.path.basename(xpu_fft_lib_file)]
1163+
11581164
if '${WITH_XPU_XFT}' == 'ON':
11591165
shutil.copy('${XPU_XFT_LIB}', libs_path)
11601166
package_data['paddle.libs']+=['${XPU_XFT_LIB_NAME}']

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,12 @@ def get_package_data_and_package_dir():
16191619
shutil.copy(env_dict.get("XPU_BKCL_LIB"), libs_path)
16201620
package_data['paddle.libs'] += [env_dict.get("XPU_BKCL_LIB_NAME")]
16211621

1622+
if env_dict.get("WITH_XPU_FFT") == 'ON':
1623+
xpu_fft_lib_list = glob.glob(env_dict.get("XPU_FFT_LIB") + '*')
1624+
for xpu_fft_lib_file in xpu_fft_lib_list:
1625+
shutil.copy(xpu_fft_lib_file, libs_path)
1626+
package_data['paddle.libs'] += [os.path.basename(xpu_fft_lib_file)]
1627+
16221628
if env_dict.get("WITH_XPU_XFT") == 'ON':
16231629
shutil.copy(env_dict.get("XPU_XFT_LIB"), libs_path)
16241630
package_data['paddle.libs'] += [env_dict.get("XPU_XFT_LIB_NAME")]

tools/xpu/get_xpufft_dependence.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -ex
18+
19+
FFT_URL=$1
20+
FFT_DIR_NAME=$2
21+
22+
if ! [ -n "$FFT_URL" ]; then
23+
exit 0
24+
fi
25+
26+
mkdir -p xpu/include/fft
27+
28+
function download_from_bos() {
29+
local url=$1
30+
wget --no-check-certificate ${url} -q -O tmp.tar.gz
31+
if [[ $? -ne 0 ]]; then
32+
echo "downloading failed: ${url}"
33+
exit 1
34+
fi
35+
tar xvf tmp.tar.gz
36+
rm -f tmp.tar.gz
37+
}
38+
39+
function check_files() {
40+
local files=("$@")
41+
for file in "${files[@]}";
42+
do
43+
echo "checking $file"
44+
if [[ ! -f $file ]]; then
45+
echo "checking failed: $file"
46+
exit 1
47+
else
48+
echo "checking ok: $file"
49+
fi
50+
done
51+
}
52+
53+
download_from_bos ${FFT_URL}
54+
check_files ${FFT_DIR_NAME}/include/cufft.h ${FFT_DIR_NAME}/lib64/libcufft.so
55+
cp -r ${FFT_DIR_NAME}/include/* xpu/include/xpu/
56+
cp -r ${FFT_DIR_NAME}/lib64/* xpu/lib/
57+
cp -r ${FFT_DIR_NAME}/include/* xpu/include/fft
58+
patchelf --set-rpath '$ORIGIN/' xpu/lib/libcufft.so
59+
patchelf --set-rpath '$ORIGIN/' xpu/lib/libcufft.so.10
60+
patchelf --set-rpath '$ORIGIN/' xpu/lib/libcufft.so.10.7.2.91

0 commit comments

Comments
 (0)