Skip to content

Commit 1bb013f

Browse files
zhwesky2010liupluswei
authored andcommitted
cherry-pick PR#18671 , PR#19190 and PR#19225 into Release/1.5 (#19207)
* add function:error code,test=develop * add function:fix inference lib bug,test=develop * fix the bug that PYTHON_EXECUTABLE not exists
1 parent 1fd0ca8 commit 1bb013f

File tree

3 files changed

+120
-65
lines changed

3 files changed

+120
-65
lines changed

cmake/copyfile.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import sys
17+
import shutil
18+
import glob
19+
20+
21+
def main():
22+
src = sys.argv[1]
23+
dst = sys.argv[2]
24+
if os.path.isdir(src): #copy directory
25+
pathList = os.path.split(src)
26+
dst = os.path.join(dst, pathList[-1])
27+
if not os.path.exists(dst):
28+
shutil.copytree(src, dst)
29+
print("first copy directory: {0} --->>> {1}".format(src, dst))
30+
else:
31+
shutil.rmtree(dst)
32+
shutil.copytree(src, dst)
33+
print("overwritten copy directory: {0} --->>> {1}".format(src, dst))
34+
else: #copy file, wildcard
35+
if not os.path.exists(dst):
36+
os.makedirs(dst)
37+
srcFiles = glob.glob(src)
38+
for srcFile in srcFiles:
39+
shutil.copy(srcFile, dst)
40+
print("copy file: {0} --->>> {1}".format(srcFile, dst))
41+
42+
43+
if __name__ == "__main__":
44+
main()

cmake/inference_lib.cmake

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
# limitations under the License.
1414

1515
# make package for paddle fluid shared and static library
16+
17+
if(WIN32)
18+
if(NOT PYTHON_EXECUTABLE)
19+
FIND_PACKAGE(PythonInterp REQUIRED)
20+
endif()
21+
endif()
22+
23+
set(COPY_SCRIPT_DIR ${PADDLE_SOURCE_DIR}/cmake)
1624
function(copy TARGET)
1725
set(options "")
1826
set(oneValueArgs "")
@@ -26,42 +34,16 @@ function(copy TARGET)
2634
message(FATAL_ERROR "${TARGET} source numbers are not equal to destination numbers")
2735
endif ()
2836
math(EXPR len "${copy_lib_SRCS_len} - 1")
29-
3037
add_custom_target(${TARGET} DEPENDS ${copy_lib_DEPS})
3138
foreach (index RANGE ${len})
3239
list(GET copy_lib_SRCS ${index} src)
3340
list(GET copy_lib_DSTS ${index} dst)
34-
if (WIN32)
35-
if(IS_DIRECTORY ${src})
36-
get_filename_component(last_path ${src} NAME)
37-
string(APPEND dst "/" ${last_path})
38-
add_custom_command(TARGET ${TARGET} PRE_BUILD
39-
COMMAND ${CMAKE_COMMAND} -E make_directory "${dst}"
40-
)
41-
if(EXISTS ${src})
42-
add_custom_command(TARGET ${TARGET} PRE_BUILD
43-
COMMAND cmake -E copy_directory "${src}" "${dst}"
44-
COMMENT "copying ${src} -> ${dst}")
45-
else()
46-
message(WARNING "${src} not exist!")
47-
endif()
48-
else()
49-
# windows cmd shell will not expand wildcard automatically.
50-
# below expand the files, and copy them by rules.
51-
file(GLOB src_files ${src})
52-
if (NOT "${src_files}" STREQUAL "")
53-
list(REMOVE_DUPLICATES src_files)
54-
endif ()
55-
add_custom_command(TARGET ${TARGET} PRE_BUILD
56-
COMMAND ${CMAKE_COMMAND} -E make_directory "${dst}"
57-
)
58-
foreach (src_file ${src_files})
59-
add_custom_command(TARGET ${TARGET} PRE_BUILD
60-
COMMAND ${CMAKE_COMMAND} -E copy "${src_file}" "${dst}"
61-
COMMENT "copying ${src_file} -> ${dst}")
62-
endforeach ()
63-
endif()
64-
else (WIN32) # not windows
41+
if (WIN32) #windows
42+
file(TO_NATIVE_PATH ${src} native_src)
43+
file(TO_NATIVE_PATH ${dst} native_dst)
44+
add_custom_command(TARGET ${TARGET} POST_BUILD
45+
COMMAND ${PYTHON_EXECUTABLE} ${COPY_SCRIPT_DIR}/copyfile.py ${native_src} ${native_dst})
46+
else (WIN32) #not windows
6547
add_custom_command(TARGET ${TARGET} PRE_BUILD
6648
COMMAND mkdir -p "${dst}"
6749
COMMAND cp -r "${src}" "${dst}"
@@ -189,13 +171,12 @@ copy(zlib_lib
189171
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
190172
set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
191173
set(module "framework")
192-
if (NOT WIN32)
193-
set(framework_lib_deps framework_py_proto)
194-
endif (NOT WIN32)
174+
set(framework_lib_deps framework_py_proto)
175+
195176
copy(framework_lib DEPS ${framework_lib_deps}
196-
SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
197-
${src_dir}/${module}/ir/*.h
198-
DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module}/ir
177+
SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/data_feed.pb.h ${src_dir}/${module}/ir/memory_optimize_pass/*.h
178+
${src_dir}/${module}/ir/*.h ${src_dir}/${module}/fleet/*.h
179+
DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}/ir/memory_optimize_pass ${dst_dir}/${module}/ir ${dst_dir}/${module}/fleet
199180
)
200181

201182
set(module "memory")

paddle/fluid/platform/gpu_info.cc

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ DEFINE_string(selected_gpus, "",
7979
namespace paddle {
8080
namespace platform {
8181

82+
inline std::string CudaErrorWebsite() {
83+
return "Please see detail in https://docs.nvidia.com/cuda/cuda-runtime-api"
84+
"/group__CUDART__TYPES.html#group__CUDART__TYPES_1g3f51e3575c217824"
85+
"6db0a94a430e0038";
86+
}
87+
8288
static int GetCUDADeviceCountImpl() {
8389
const auto *cuda_visible_devices = std::getenv("CUDA_VISIBLE_DEVICES");
8490
if (cuda_visible_devices != nullptr) {
@@ -92,9 +98,12 @@ static int GetCUDADeviceCountImpl() {
9298
}
9399

94100
int count;
101+
auto error_code = cudaGetDeviceCount(&count);
95102
PADDLE_ENFORCE(
96-
cudaGetDeviceCount(&count),
97-
"cudaGetDeviceCount failed in paddle::platform::GetCUDADeviceCount");
103+
error_code,
104+
"cudaGetDeviceCount failed in "
105+
"paddle::platform::GetCUDADeviceCountImpl, error code : %d, %s",
106+
error_code, CudaErrorWebsite());
98107
return count;
99108
}
100109

@@ -106,9 +115,12 @@ int GetCUDADeviceCount() {
106115
int GetCUDAComputeCapability(int id) {
107116
PADDLE_ENFORCE_LT(id, GetCUDADeviceCount(), "id must less than GPU count");
108117
cudaDeviceProp device_prop;
109-
PADDLE_ENFORCE(cudaGetDeviceProperties(&device_prop, id),
110-
"cudaGetDeviceProperties failed in "
111-
"paddle::platform::GetCUDAComputeCapability");
118+
auto error_code = cudaGetDeviceProperties(&device_prop, id);
119+
PADDLE_ENFORCE(
120+
error_code,
121+
"cudaGetDeviceProperties failed in "
122+
"paddle::platform::GetCUDAComputeCapability, error code : %d, %s",
123+
error_code, CudaErrorWebsite());
112124
return device_prop.major * 10 + device_prop.minor;
113125
}
114126

@@ -143,20 +155,25 @@ bool TensorCoreAvailable() {
143155
int GetCUDAMultiProcessors(int id) {
144156
PADDLE_ENFORCE_LT(id, GetCUDADeviceCount(), "id must less than GPU count");
145157
int count;
146-
PADDLE_ENFORCE(
147-
cudaDeviceGetAttribute(&count, cudaDevAttrMultiProcessorCount, id),
148-
"cudaDeviceGetAttribute failed in "
149-
"paddle::platform::GetCUDAMultiProcessors");
158+
auto error_code =
159+
cudaDeviceGetAttribute(&count, cudaDevAttrMultiProcessorCount, id);
160+
PADDLE_ENFORCE(error_code,
161+
"cudaDeviceGetAttribute failed in "
162+
"paddle::platform::GetCUDAMultiProcess, error code : %d, %s",
163+
error_code, CudaErrorWebsite());
150164
return count;
151165
}
152166

153167
int GetCUDAMaxThreadsPerMultiProcessor(int id) {
154168
PADDLE_ENFORCE_LT(id, GetCUDADeviceCount(), "id must less than GPU count");
155169
int count;
156-
PADDLE_ENFORCE(cudaDeviceGetAttribute(
157-
&count, cudaDevAttrMaxThreadsPerMultiProcessor, id),
158-
"cudaDeviceGetAttribute failed in "
159-
"paddle::platform::GetCUDAMaxThreadsPerMultiProcessor");
170+
auto error_code = cudaDeviceGetAttribute(
171+
&count, cudaDevAttrMaxThreadsPerMultiProcessor, id);
172+
PADDLE_ENFORCE(
173+
error_code,
174+
"cudaDeviceGetAttribute failed in paddle::"
175+
"platform::GetCUDAMaxThreadsPerMultiProcessor, error code : %d, %s",
176+
error_code, CudaErrorWebsite());
160177
return count;
161178
}
162179

@@ -266,37 +283,50 @@ size_t GpuMaxChunkSize() {
266283

267284
void GpuMemcpyAsync(void *dst, const void *src, size_t count,
268285
enum cudaMemcpyKind kind, cudaStream_t stream) {
269-
PADDLE_ENFORCE(cudaMemcpyAsync(dst, src, count, kind, stream),
286+
auto error_code = cudaMemcpyAsync(dst, src, count, kind, stream);
287+
PADDLE_ENFORCE(error_code,
270288
"cudaMemcpyAsync failed in paddle::platform::GpuMemcpyAsync "
271-
"(%p -> %p, length: %d)",
272-
src, dst, static_cast<int>(count));
289+
"(%p -> %p, length: %d) error code : %d, %s",
290+
src, dst, static_cast<int>(count), error_code,
291+
CudaErrorWebsite());
273292
}
274293

275294
void GpuMemcpySync(void *dst, const void *src, size_t count,
276295
enum cudaMemcpyKind kind) {
277-
PADDLE_ENFORCE(cudaMemcpy(dst, src, count, kind),
278-
"cudaMemcpy failed in paddle::platform::GpuMemcpySync (%p -> "
279-
"%p, length: %d)",
280-
src, dst, static_cast<int>(count));
296+
auto error_code = cudaMemcpy(dst, src, count, kind);
297+
PADDLE_ENFORCE(error_code,
298+
"cudaMemcpy failed in paddle::platform::GpuMemcpySync "
299+
"(%p -> %p, length: %d) error code : %d, %s",
300+
src, dst, static_cast<int>(count), error_code,
301+
CudaErrorWebsite());
281302
}
282303

283304
void GpuMemcpyPeerAsync(void *dst, int dst_device, const void *src,
284305
int src_device, size_t count, cudaStream_t stream) {
306+
auto error_code =
307+
cudaMemcpyPeerAsync(dst, dst_device, src, src_device, count, stream);
285308
PADDLE_ENFORCE(
286-
cudaMemcpyPeerAsync(dst, dst_device, src, src_device, count, stream),
287-
"cudaMemcpyPeerAsync failed in paddle::platform::GpuMemcpyPeerAsync");
309+
error_code,
310+
"cudaMemcpyPeerAsync failed in paddle::platform::GpuMemcpyPeerAsync "
311+
"error code : %d, %s",
312+
error_code, CudaErrorWebsite());
288313
}
289314

290315
void GpuMemcpyPeerSync(void *dst, int dst_device, const void *src,
291316
int src_device, size_t count) {
292-
PADDLE_ENFORCE(
293-
cudaMemcpyPeer(dst, dst_device, src, src_device, count),
294-
"cudaMemcpyPeer failed in paddle::platform::GpuMemcpyPeerSync");
317+
auto error_code = cudaMemcpyPeer(dst, dst_device, src, src_device, count);
318+
PADDLE_ENFORCE(error_code,
319+
"cudaMemcpyPeer failed in paddle::platform::GpuMemcpyPeerSync "
320+
"error code : %d, %s",
321+
error_code, CudaErrorWebsite());
295322
}
296323

297324
void GpuMemsetAsync(void *dst, int value, size_t count, cudaStream_t stream) {
298-
PADDLE_ENFORCE(cudaMemsetAsync(dst, value, count, stream),
299-
"cudaMemsetAsync failed in paddle::platform::GpuMemsetAsync");
325+
auto error_code = cudaMemsetAsync(dst, value, count, stream);
326+
PADDLE_ENFORCE(error_code,
327+
"cudaMemsetAsync failed in paddle::platform::GpuMemsetAsync "
328+
"error code : %d, %s",
329+
error_code, CudaErrorWebsite());
300330
}
301331
} // namespace platform
302332
} // namespace paddle

0 commit comments

Comments
 (0)