Skip to content

Commit 5a86891

Browse files
silingtong123liupluswei
authored andcommitted
cherry pick #18761 (#19199)
* fix warpctc dynamic library not found issue on mac and windows platform
1 parent de5dec8 commit 5a86891

File tree

8 files changed

+56
-34
lines changed

8 files changed

+56
-34
lines changed

paddle/fluid/platform/dynload/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ if (CUPTI_FOUND)
1616
list(APPEND CUDA_SRCS cupti.cc)
1717
endif(CUPTI_FOUND)
1818
nv_library(dynload_cuda SRCS ${CUDA_SRCS} DEPS dynamic_loader)
19-
configure_file(warpctc_lib_path.h.in ${CMAKE_CURRENT_BINARY_DIR}/warpctc_lib_path.h)
2019
cc_library(dynload_warpctc SRCS warpctc.cc DEPS dynamic_loader warpctc)
2120
if (WITH_MKLML)
2221
cc_library(dynload_mklml SRCS mklml.cc DEPS dynamic_loader mklml)

paddle/fluid/platform/dynload/dynamic_loader.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ limitations under the License. */
2020
#include "gflags/gflags.h"
2121
#include "glog/logging.h"
2222
#include "paddle/fluid/platform/dynload/cupti_lib_path.h"
23-
#include "paddle/fluid/platform/dynload/warpctc_lib_path.h"
2423
#include "paddle/fluid/platform/enforce.h"
2524
#include "paddle/fluid/platform/port.h"
2625

@@ -34,8 +33,6 @@ DEFINE_string(cuda_dir, "",
3433
"libcurand. For instance, /usr/local/cuda/lib64. If default, "
3534
"dlopen will search cuda from LD_LIBRARY_PATH");
3635

37-
DEFINE_string(warpctc_dir, "", "Specify path for loading libwarpctc.so.");
38-
3936
DEFINE_string(nccl_dir, "",
4037
"Specify path for loading nccl library, such as libcublas, "
4138
"libcurand. For instance, /usr/local/cuda/lib64. If default, "
@@ -52,8 +49,15 @@ DEFINE_string(mklml_dir, "", "Specify path for loading libmklml_intel.so.");
5249
namespace paddle {
5350
namespace platform {
5451
namespace dynload {
52+
53+
struct PathNode {
54+
PathNode() {}
55+
std::string path = "";
56+
};
57+
5558
static constexpr char cupti_lib_path[] = CUPTI_LIB_PATH;
56-
static constexpr char warpctc_lib_path[] = WARPCTC_LIB_PATH;
59+
60+
static PathNode s_py_site_pkg_path;
5761

5862
#if defined(_WIN32) && defined(PADDLE_WITH_CUDA)
5963
static constexpr char* win_cublas_lib = "cublas64_" PADDLE_CUDA_BINVER ".dll";
@@ -78,6 +82,11 @@ static inline std::string join(const std::string& part1,
7882
return ret;
7983
}
8084

85+
void SetPaddleLibPath(const std::string& py_site_pkg_path) {
86+
s_py_site_pkg_path.path = py_site_pkg_path;
87+
VLOG(3) << "Set paddle lib path : " << py_site_pkg_path;
88+
}
89+
8190
static inline void* GetDsoHandleFromDefaultPath(const std::string& dso_path,
8291
int dynload_flags) {
8392
VLOG(3) << "Try to find library: " << dso_path
@@ -214,9 +223,9 @@ void* GetCurandDsoHandle() {
214223
}
215224

216225
void* GetWarpCTCDsoHandle() {
217-
std::string warpctc_dir = warpctc_lib_path;
218-
if (!FLAGS_warpctc_dir.empty()) {
219-
warpctc_dir = FLAGS_warpctc_dir;
226+
std::string warpctc_dir = "";
227+
if (!s_py_site_pkg_path.path.empty()) {
228+
warpctc_dir = s_py_site_pkg_path.path;
220229
}
221230
#if defined(__APPLE__) || defined(__OSX__)
222231
return GetDsoHandleFromSearchPath(warpctc_dir, "libwarpctc.dylib");

paddle/fluid/platform/dynload/dynamic_loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#pragma once
16+
#include <string>
1617

1718
namespace paddle {
1819
namespace platform {
@@ -33,6 +34,7 @@ void* GetNCCLDsoHandle();
3334
void* GetTensorRtDsoHandle();
3435
void* GetMKLMLDsoHandle();
3536

37+
void SetPaddleLibPath(const std::string&);
3638
} // namespace dynload
3739
} // namespace platform
3840
} // namespace paddle

paddle/fluid/platform/dynload/warpctc_lib_path.h.in

Lines changed: 0 additions & 17 deletions
This file was deleted.

paddle/fluid/pybind/pybind.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ limitations under the License. */
4747
#include "paddle/fluid/operators/reader/lod_tensor_blocking_queue.h"
4848
#include "paddle/fluid/platform/cpu_helper.h"
4949
#include "paddle/fluid/platform/cpu_info.h"
50+
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
5051
#include "paddle/fluid/platform/enforce.h"
5152
#include "paddle/fluid/platform/init.h"
5253
#include "paddle/fluid/platform/place.h"
@@ -197,6 +198,8 @@ PYBIND11_MODULE(core_noavx, m) {
197198
m.def("print_mem_usage",
198199
[]() { return memory::allocation::GPUMemMonitor.PrintMemUsage(); });
199200

201+
m.def("_set_paddle_lib_path", &paddle::platform::dynload::SetPaddleLibPath);
202+
200203
BindImperative(&m);
201204

202205
py::class_<Tensor>(m, "Tensor", py::buffer_protocol())

paddle/scripts/paddle_build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ EOF
418418
#remove proxy here to fix dist error on mac
419419
export http_proxy=
420420
export https_proxy=
421-
# TODO: jiabin need to refine this part when these tests fixed on mac
422-
ctest --output-on-failure -j $2
423421
# make install should also be test when unittest
424422
make install -j 8
425423

@@ -447,6 +445,9 @@ EOF
447445
pip3.7 install --user ${INSTALL_PREFIX:-/paddle/build}/opt/paddle/share/wheels/*.whl
448446
fi
449447

448+
# TODO: jiabin need to refine this part when these tests fixed on mac
449+
ctest --output-on-failure -j $2
450+
450451
paddle version
451452
fi
452453
}

python/paddle/fluid/core.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import print_function
1616

17+
import site
1718
import sys
1819
import os
1920

@@ -34,8 +35,8 @@
3435
try:
3536
if os.name == 'nt':
3637
third_lib_path = current_path + os.sep + '..' + os.sep + 'libs'
37-
os.environ['path'] += ';' + third_lib_path
38-
sys.path.append(third_lib_path)
38+
os.environ['path'] = third_lib_path + ';' + os.environ['path']
39+
sys.path.insert(0, third_lib_path)
3940

4041
except ImportError as e:
4142
from .. import compat as cpt
@@ -175,6 +176,7 @@ def asm_func(code_str, restype=ctypes.c_uint32, argtypes=()):
175176
from .core_avx import _set_fuse_parameter_memory_size
176177
from .core_avx import _is_dygraph_debug_enabled
177178
from .core_avx import _dygraph_debug_level
179+
from .core_avx import _set_paddle_lib_path
178180
except Exception as e:
179181
if has_avx_core:
180182
raise e
@@ -203,9 +205,29 @@ def asm_func(code_str, restype=ctypes.c_uint32, argtypes=()):
203205
from .core_noavx import _set_fuse_parameter_memory_size
204206
from .core_noavx import _is_dygraph_debug_enabled
205207
from .core_noavx import _dygraph_debug_level
208+
from .core_noavx import _set_paddle_lib_path
206209
except Exception as e:
207210
if has_noavx_core:
208211
sys.stderr.write(
209212
'Error: Can not import noavx core while this file exists ' +
210213
current_path + os.sep + 'core_noavx.' + core_suffix + '\n')
211214
raise e
215+
216+
217+
# set paddle lib path
218+
def set_paddle_lib_path():
219+
site_dirs = site.getsitepackages() if hasattr(
220+
site,
221+
'getsitepackages') else [x for x in sys.path if 'site-packages' in x]
222+
for site_dir in site_dirs:
223+
lib_dir = os.path.sep.join([site_dir, 'paddle', 'libs'])
224+
if os.path.exists(lib_dir):
225+
_set_paddle_lib_path(lib_dir)
226+
return
227+
if hasattr(site, 'USER_SITE'):
228+
lib_dir = os.path.sep.join([site.USER_SITE, 'paddle', 'libs'])
229+
if os.path.exists(lib_dir):
230+
_set_paddle_lib_path(lib_dir)
231+
232+
233+
set_paddle_lib_path()

python/setup.py.in

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import subprocess
33
import os
44
import re
55
import shutil
6+
import sys
67
class BinaryDistribution(Distribution):
78
def has_ext_modules(foo):
89
return True
910

1011
RC = 0
1112

12-
ext_name = '.dll' if os.name == 'nt' else '.so'
13+
ext_name = '.dll' if os.name == 'nt' else ('.dylib' if sys.platform == 'darwin' else '.so')
1314

1415
def git_commit():
1516
try:
@@ -145,9 +146,9 @@ if '${CMAKE_SYSTEM_PROCESSOR}' not in ['arm', 'armv7-a', 'aarch64']:
145146
paddle_bins = ''
146147
if not '${WIN32}':
147148
paddle_bins = ['${PADDLE_BINARY_DIR}/paddle/scripts/paddle']
148-
package_data={'paddle.fluid': ['${FLUID_CORE_NAME}' + (ext_name if os.name != 'nt' else '.pyd')]}
149+
package_data={'paddle.fluid': ['${FLUID_CORE_NAME}' + ('.so' if os.name != 'nt' else '.pyd')]}
149150
if '${HAS_NOAVX_CORE}' == 'ON':
150-
package_data['paddle.fluid'] += ['core_noavx' + (ext_name if os.name != 'nt' else '.pyd')]
151+
package_data['paddle.fluid'] += ['core_noavx' + ('.so' if os.name != 'nt' else '.pyd')]
151152

152153
package_dir={
153154
'': '${PADDLE_BINARY_DIR}/python',
@@ -223,9 +224,9 @@ if '${CMAKE_BUILD_TYPE}' == 'Release':
223224
if os.name != 'nt':
224225
# only change rpath in Release mode, since in Debug mode, ${FLUID_CORE_NAME}.xx is too large to be changed.
225226
if "@APPLE@" == "1":
226-
command = "install_name_tool -id \"@loader_path/../libs/\" ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + ext_name
227+
command = "install_name_tool -id \"@loader_path/../libs/\" ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'
227228
else:
228-
command = "patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + ext_name
229+
command = "patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'
229230
if os.system(command) != 0:
230231
raise Exception("patch ${FLUID_CORE_NAME}.%s failed, command: %s" % (ext_name, command))
231232

@@ -237,6 +238,8 @@ if os.name == 'nt':
237238
fix_package_dir[k] = v.replace('/', '\\')
238239
package_dir = fix_package_dir
239240
ext_modules = []
241+
elif sys.platform == 'darwin':
242+
ext_modules = []
240243

241244
setup(name='${PACKAGE_NAME}',
242245
version='${PADDLE_VERSION}',

0 commit comments

Comments
 (0)