From 72a48dab840f25eb8d50aa7c0d065a1393690c54 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 29 Jun 2022 11:54:16 +0800 Subject: [PATCH 1/5] fix device context init error (#43910) --- paddle/fluid/platform/device_context.h | 4 +- paddle/phi/api/lib/CMakeLists.txt | 314 +++++++++++++++++-------- paddle/phi/api/lib/context_pool.cc | 5 + 3 files changed, 228 insertions(+), 95 deletions(-) diff --git a/paddle/fluid/platform/device_context.h b/paddle/fluid/platform/device_context.h index 2c5f24d28c6d6..dde7dc5761b63 100644 --- a/paddle/fluid/platform/device_context.h +++ b/paddle/fluid/platform/device_context.h @@ -58,7 +58,7 @@ limitations under the License. */ #endif #ifdef PADDLE_WITH_MKLDNN -#include "dnnl.hpp" +#include "dnnl.hpp" // NOLINT #include "paddle/fluid/framework/data_layout.h" #endif @@ -902,6 +902,8 @@ class DeviceContextPool { return *pool; } + static bool IsInitialized() { return pool != nullptr; } + static void SetPool(DeviceContextPool* dev_pool) { pool = dev_pool; } /*! \brief Return handle of single device context. */ diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index e10ae8254a79e..2bbd875158148 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -1,11 +1,20 @@ add_subdirectory(utils) -if (WITH_GPU) - nv_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) -elseif (WITH_ROCM) - hip_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) +if(WITH_GPU) + nv_library( + phi_tensor_raw + SRCS tensor.cc + DEPS tensor_base dense_tensor phi_api_utils phi_enforce) +elseif(WITH_ROCM) + hip_library( + phi_tensor_raw + SRCS tensor.cc + DEPS tensor_base dense_tensor phi_api_utils phi_enforce) else() - cc_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) + cc_library( + phi_tensor_raw + SRCS tensor.cc + DEPS tensor_base dense_tensor phi_api_utils phi_enforce) endif() set(api_gen_base ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/api_base.py) @@ -19,51 +28,71 @@ set(api_header_file_tmp ${api_header_file}.tmp) set(api_source_file_tmp ${api_source_file}.tmp) # backward api file -set(bw_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward_api_gen.py) -set(bw_api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward.yaml) -set(bw_api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/backward_api.h) +set(bw_api_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward_api_gen.py) +set(bw_api_yaml_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward.yaml) +set(bw_api_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/backward_api.h) set(bw_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/backward_api.cc) set(bw_api_header_file_tmp ${bw_api_header_file}.tmp) set(bw_api_source_file_tmp ${bw_api_source_file}.tmp) # dygraph(intermediate) api file -set(im_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/intermediate_api_gen.py) -set(dygraph_api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.h) -set(dygraph_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.cc) +set(im_api_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/intermediate_api_gen.py) +set(dygraph_api_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.h) +set(dygraph_api_source_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.cc) set(dygraph_api_header_file_tmp ${dygraph_api_header_file}.tmp) set(dygraph_api_source_file_tmp ${dygraph_api_source_file}.tmp) # sparse api file -set(sparse_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_api_gen.py) -set(sparse_api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_api.yaml) -set(sparse_api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/sparse_api.h) +set(sparse_api_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_api_gen.py) +set(sparse_api_yaml_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_api.yaml) +set(sparse_api_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/sparse_api.h) set(sparse_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_api.cc) set(sparse_api_header_file_tmp ${sparse_api_header_file}.tmp) set(sparse_api_source_file_tmp ${sparse_api_source_file}.tmp) # sparse bw api file -set(sparse_bw_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_bw_api_gen.py) -set(sparse_bw_api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_bw_api.yaml) -set(sparse_bw_api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/sparse_bw_api.h) -set(sparse_bw_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_bw_api.cc) +set(sparse_bw_api_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_bw_api_gen.py) +set(sparse_bw_api_yaml_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/sparse_bw_api.yaml) +set(sparse_bw_api_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/sparse_bw_api.h) +set(sparse_bw_api_source_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_bw_api.cc) set(sparse_bw_api_header_file_tmp ${sparse_bw_api_header_file}.tmp) set(sparse_bw_api_source_file_tmp ${sparse_bw_api_source_file}.tmp) # strings api file -set(strings_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/strings_api_gen.py) -set(strings_api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/strings_api.yaml) -set(strings_api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/strings_api.h) -set(strings_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/strings_api.cc) +set(strings_api_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/strings_api_gen.py) +set(strings_api_yaml_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/strings_api.yaml) +set(strings_api_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/strings_api.h) +set(strings_api_source_file + ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/strings_api.cc) set(strings_api_header_file_tmp ${strings_api_header_file}.tmp) set(strings_api_source_file_tmp ${strings_api_source_file}.tmp) # wrapped infermeta file -set(wrapped_infermeta_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/wrapped_infermeta_gen.py) +set(wrapped_infermeta_gen_file + ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/wrapped_infermeta_gen.py) set(api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/api.yaml) -set(wrapped_infermeta_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.h) -set(wrapped_infermeta_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.cc) +set(wrapped_infermeta_header_file + ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.h) +set(wrapped_infermeta_source_file + ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.cc) -if (NOT PYTHON_EXECUTABLE) +if(NOT PYTHON_EXECUTABLE) find_package(PythonInterp REQUIRED) endif() @@ -71,25 +100,30 @@ endif() add_custom_command( OUTPUT ${api_header_file} ${api_source_file} COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml - COMMAND ${PYTHON_EXECUTABLE} ${api_gen_file} - --api_yaml_path ${api_yaml_file} - --api_header_path ${api_header_file_tmp} - --api_source_path ${api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_header_file_tmp} ${api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_source_file_tmp} ${api_source_file} + COMMAND + ${PYTHON_EXECUTABLE} ${api_gen_file} --api_yaml_path ${api_yaml_file} + --api_header_path ${api_header_file_tmp} --api_source_path + ${api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_header_file_tmp} + ${api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_source_file_tmp} + ${api_source_file} COMMENT "copy_if_different ${api_header_file} ${api_source_file}" DEPENDS ${api_yaml_file} ${api_gen_file} ${api_gen_base} VERBATIM) # generate backward api add_custom_command( - OUTPUT ${bw_api_header_file} ${bw_api_source_file} ${bw_api_header_file_tmp} ${bw_api_source_file_tmp} - COMMAND ${PYTHON_EXECUTABLE} ${bw_api_gen_file} - --backward_yaml_path ${bw_api_yaml_file} - --backward_header_path ${bw_api_header_file_tmp} - --backward_source_path ${bw_api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_header_file_tmp} ${bw_api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_source_file_tmp} ${bw_api_source_file} + OUTPUT ${bw_api_header_file} ${bw_api_source_file} ${bw_api_header_file_tmp} + ${bw_api_source_file_tmp} + COMMAND + ${PYTHON_EXECUTABLE} ${bw_api_gen_file} --backward_yaml_path + ${bw_api_yaml_file} --backward_header_path ${bw_api_header_file_tmp} + --backward_source_path ${bw_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_header_file_tmp} + ${bw_api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_source_file_tmp} + ${bw_api_source_file} COMMENT "copy_if_different ${bw_api_header_file} ${bw_api_source_file}" DEPENDS ${bw_api_yaml_file} ${bw_api_gen_file} ${api_gen_base} VERBATIM) @@ -97,81 +131,173 @@ add_custom_command( # generate sparse api add_custom_command( OUTPUT ${sparse_api_header_file} ${sparse_api_source_file} - COMMAND ${PYTHON_EXECUTABLE} ${sparse_api_gen_file} - --api_yaml_path ${sparse_api_yaml_file} - --api_header_path ${sparse_api_header_file_tmp} - --api_source_path ${sparse_api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_header_file_tmp} ${sparse_api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_source_file_tmp} ${sparse_api_source_file} - COMMENT "copy_if_different ${sparse_api_header_file} ${sparse_sparse_api_source_file}" - DEPENDS ${sparse_api_yaml_file} ${sparse_api_gen_file} ${api_gen_base} ${api_gen_file} + COMMAND + ${PYTHON_EXECUTABLE} ${sparse_api_gen_file} --api_yaml_path + ${sparse_api_yaml_file} --api_header_path ${sparse_api_header_file_tmp} + --api_source_path ${sparse_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_header_file_tmp} + ${sparse_api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_source_file_tmp} + ${sparse_api_source_file} + COMMENT + "copy_if_different ${sparse_api_header_file} ${sparse_sparse_api_source_file}" + DEPENDS ${sparse_api_yaml_file} ${sparse_api_gen_file} ${api_gen_base} + ${api_gen_file} VERBATIM) # generate backward sparse api add_custom_command( OUTPUT ${sparse_bw_api_header_file} ${sparse_bw_api_source_file} - COMMAND ${PYTHON_EXECUTABLE} ${sparse_bw_api_gen_file} - --api_yaml_path ${sparse_bw_api_yaml_file} - --api_header_path ${sparse_bw_api_header_file_tmp} - --api_source_path ${sparse_bw_api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_header_file_tmp} ${sparse_bw_api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_source_file_tmp} ${sparse_bw_api_source_file} - COMMENT "copy_if_different ${sparse_bw_api_header_file} ${sparse_bw_sparse_api_source_file}" - DEPENDS ${sparse_bw_api_yaml_file} ${sparse_bw_api_gen_file} ${api_gen_base} ${api_gen_file} ${sparse_api_gen_file} ${bw_api_gen_file} + COMMAND + ${PYTHON_EXECUTABLE} ${sparse_bw_api_gen_file} --api_yaml_path + ${sparse_bw_api_yaml_file} --api_header_path + ${sparse_bw_api_header_file_tmp} --api_source_path + ${sparse_bw_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_header_file_tmp} + ${sparse_bw_api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_source_file_tmp} + ${sparse_bw_api_source_file} + COMMENT + "copy_if_different ${sparse_bw_api_header_file} ${sparse_bw_sparse_api_source_file}" + DEPENDS ${sparse_bw_api_yaml_file} ${sparse_bw_api_gen_file} ${api_gen_base} + ${api_gen_file} ${sparse_api_gen_file} ${bw_api_gen_file} VERBATIM) # generate strings api add_custom_command( OUTPUT ${strings_api_header_file} ${strings_api_source_file} - COMMAND ${PYTHON_EXECUTABLE} ${strings_api_gen_file} - --api_yaml_path ${strings_api_yaml_file} - --api_header_path ${strings_api_header_file_tmp} - --api_source_path ${strings_api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_header_file_tmp} ${strings_api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_source_file_tmp} ${strings_api_source_file} - COMMENT "copy_if_different ${strings_api_header_file} ${strings_strings_api_source_file}" - DEPENDS ${strings_api_yaml_file} ${strings_api_gen_file} ${api_gen_base} ${api_gen_file} + COMMAND + ${PYTHON_EXECUTABLE} ${strings_api_gen_file} --api_yaml_path + ${strings_api_yaml_file} --api_header_path ${strings_api_header_file_tmp} + --api_source_path ${strings_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_header_file_tmp} + ${strings_api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_source_file_tmp} + ${strings_api_source_file} + COMMENT + "copy_if_different ${strings_api_header_file} ${strings_strings_api_source_file}" + DEPENDS ${strings_api_yaml_file} ${strings_api_gen_file} ${api_gen_base} + ${api_gen_file} VERBATIM) # generate dygraph(intermediate) api add_custom_command( OUTPUT ${dygraph_api_header_file} ${dygraph_api_source_file} - COMMAND ${PYTHON_EXECUTABLE} ${im_api_gen_file} - --api_yaml_path ${api_yaml_file} - --sparse_api_yaml_path ${sparse_api_yaml_file} - --dygraph_api_header_path ${dygraph_api_header_file_tmp} - --dygraph_api_source_path ${dygraph_api_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_header_file_tmp} ${dygraph_api_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_source_file_tmp} ${dygraph_api_source_file} - DEPENDS ${api_yaml_file} ${sparse_api_yaml_file} ${im_api_gen_file} ${api_gen_base} ${api_gen_file} + COMMAND + ${PYTHON_EXECUTABLE} ${im_api_gen_file} --api_yaml_path ${api_yaml_file} + --sparse_api_yaml_path ${sparse_api_yaml_file} --dygraph_api_header_path + ${dygraph_api_header_file_tmp} --dygraph_api_source_path + ${dygraph_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_header_file_tmp} + ${dygraph_api_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_source_file_tmp} + ${dygraph_api_source_file} + DEPENDS ${api_yaml_file} ${sparse_api_yaml_file} ${im_api_gen_file} + ${api_gen_base} ${api_gen_file} VERBATIM) # generate wrapped infermeta add_custom_command( OUTPUT ${wrapped_infermeta_header_file} ${wrapped_infermeta_source_file} - COMMAND ${PYTHON_EXECUTABLE} ${wrapped_infermeta_gen_file} - --api_yaml_path ${api_yaml_file} - --wrapped_infermeta_header_path ${wrapped_infermeta_header_file} - --wrapped_infermeta_source_path ${wrapped_infermeta_source_file} + COMMAND + ${PYTHON_EXECUTABLE} ${wrapped_infermeta_gen_file} --api_yaml_path + ${api_yaml_file} --wrapped_infermeta_header_path + ${wrapped_infermeta_header_file} --wrapped_infermeta_source_path + ${wrapped_infermeta_source_file} DEPENDS ${api_yaml_file} ${wrapped_infermeta_gen_file} ${api_gen_base} VERBATIM) -cc_library(op_meta_info SRCS op_meta_info.cc DEPS phi_tensor_raw) -cc_library(wrapped_infermeta SRCS ${wrapped_infermeta_source_file} DEPS phi) -cc_library(context_pool SRCS context_pool.cc DEPS phi_context phi_enforce place) - -cc_library(kernel_dispatch SRCS kernel_dispatch.cc DEPS phi_tensor_raw phi_context kernel_factory context_pool) -cc_library(api_gen_utils SRCS api_gen_utils.cc DEPS phi_tensor_raw selected_rows sparse_csr_tensor sparse_coo_tensor) -cc_library(phi_data_transform SRCS data_transform.cc DEPS phi_tensor_raw transfer_layout_kernel cast_kernel data_device_transform) -cc_library(api_custom_impl SRCS api_custom_impl.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta phi_data_transform) -cc_library(sparse_api_custom_impl SRCS sparse_api_custom_impl.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform tensor_copy) - -cc_library(phi_function_api SRCS ${api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform api_custom_impl) -cc_library(phi_bw_function_api SRCS ${bw_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta phi_data_transform phi_function_api api_custom_impl global_utils) -cc_library(sparse_api SRCS ${sparse_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl) -cc_library(sparse_bw_api SRCS ${sparse_bw_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api sparse_api_custom_impl) -cc_library(phi_dygraph_api SRCS ${dygraph_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform phi_function_api sparse_api) -cc_library(strings_api SRCS ${strings_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils) -cc_library(phi_tensor SRCS tensor_method.cc DEPS phi_tensor_raw phi_function_api api_gen_utils kernel_dispatch infermeta sparse_api strings_api) -cc_library(tensor_copy SRCS tensor_copy.cc DEPS phi_tensor_raw copy_kernel kernel_dispatch api_gen_utils) -cc_library(api_scalar SRCS scalar.cc DEPS tensor_copy) +cc_library( + op_meta_info + SRCS op_meta_info.cc + DEPS phi_tensor_raw) +cc_library( + wrapped_infermeta + SRCS ${wrapped_infermeta_source_file} + DEPS phi) +cc_library( + context_pool + SRCS context_pool.cc + DEPS phi_context phi_enforce place init) + +cc_library( + kernel_dispatch + SRCS kernel_dispatch.cc + DEPS phi_tensor_raw phi_context kernel_factory context_pool) +cc_library( + api_gen_utils + SRCS api_gen_utils.cc + DEPS phi_tensor_raw selected_rows sparse_csr_tensor sparse_coo_tensor) +cc_library( + phi_data_transform + SRCS data_transform.cc + DEPS phi_tensor_raw transfer_layout_kernel cast_kernel data_device_transform) +cc_library( + api_custom_impl + SRCS api_custom_impl.cc + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta + phi_data_transform) +cc_library( + sparse_api_custom_impl + SRCS sparse_api_custom_impl.cc + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform + tensor_copy) + +cc_library( + phi_function_api + SRCS ${api_source_file} + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform + api_custom_impl) +cc_library( + phi_bw_function_api + SRCS ${bw_api_source_file} + DEPS phi_tensor_raw + phi + kernel_dispatch + api_gen_utils + backward_infermeta + phi_data_transform + phi_function_api + api_custom_impl + global_utils) +cc_library( + sparse_api + SRCS ${sparse_api_source_file} + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl) +cc_library( + sparse_bw_api + SRCS ${sparse_bw_api_source_file} + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api + sparse_api_custom_impl) +cc_library( + phi_dygraph_api + SRCS ${dygraph_api_source_file} + DEPS phi_tensor_raw + phi + kernel_dispatch + api_gen_utils + phi_data_transform + phi_function_api + sparse_api) +cc_library( + strings_api + SRCS ${strings_api_source_file} + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils) +cc_library( + phi_tensor + SRCS tensor_method.cc + DEPS phi_tensor_raw + phi_function_api + api_gen_utils + kernel_dispatch + infermeta + sparse_api + strings_api) +cc_library( + tensor_copy + SRCS tensor_copy.cc + DEPS phi_tensor_raw copy_kernel kernel_dispatch api_gen_utils) +cc_library( + api_scalar + SRCS scalar.cc + DEPS tensor_copy) diff --git a/paddle/phi/api/lib/context_pool.cc b/paddle/phi/api/lib/context_pool.cc index 07ac9822d3310..f3b148fb7bc9d 100644 --- a/paddle/phi/api/lib/context_pool.cc +++ b/paddle/phi/api/lib/context_pool.cc @@ -17,6 +17,8 @@ limitations under the License. */ #include "paddle/phi/backends/all_context.h" #include "paddle/phi/core/enforce.h" +#include "paddle/fluid/platform/init.h" + namespace paddle { namespace experimental { @@ -28,6 +30,9 @@ DeviceContextPool& DeviceContextPool::Instance() { const phi::DeviceContext* DeviceContextPool::Get(const Place& place) { auto it = context_map_.find(place); if (it == context_map_.end()) { + if (!paddle::platform::DeviceContextPool::IsInitialized()) { + paddle::framework::InitDevices(); + } // only when we need the specific DeviceContext, get and cache it auto* dev_ctx = paddle::platform::DeviceContextPool::Instance().Get(place); { From 45aa9651b2779b255652d16cd509154ebd029a15 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 1 Jul 2022 11:00:47 +0800 Subject: [PATCH 2/5] Fix core so name mismatch error (#43977) * fix core avx soname error * remove print info --- python/setup.py.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/setup.py.in b/python/setup.py.in index ee9e9b7e8ec40..aef902f942c8d 100755 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -570,7 +570,8 @@ if '${CMAKE_BUILD_TYPE}' == 'Release': commands = ["install_name_tool -id '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] commands.append("install_name_tool -add_rpath '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so') else: - commands = ["patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] + commands = ["patchelf --set-soname '${FLUID_CORE_NAME}.so' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] + commands.append("patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so') # The sw_64 not suppot patchelf, so we just disable that. if platform.machine() != 'sw_64' and platform.machine() != 'mips64': for command in commands: From 366b20a47885e8b65f462ef95455b81ee70166d7 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 1 Jul 2022 19:35:28 +0800 Subject: [PATCH 3/5] add clip_extra (#44008) --- python/paddle/fluid/dygraph/jit.py | 83 ++++++++++++++++-------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/python/paddle/fluid/dygraph/jit.py b/python/paddle/fluid/dygraph/jit.py index e0e259215c509..b0e4e4bb3f7b1 100644 --- a/python/paddle/fluid/dygraph/jit.py +++ b/python/paddle/fluid/dygraph/jit.py @@ -64,8 +64,8 @@ def _extract_vars(inputs, result_list, err_tag='inputs'): _extract_vars(var, result_list, err_tag) else: raise TypeError( - "The type of 'each element of {}' in fluid.dygraph.jit.TracedLayer.trace must be fluid.Variable, but received {}.". - format(err_tag, type(inputs))) + "The type of 'each element of {}' in fluid.dygraph.jit.TracedLayer.trace must be fluid.Variable, but received {}." + .format(err_tag, type(inputs))) def extract_vars(inputs, err_tag='inputs'): @@ -211,20 +211,19 @@ def decorated(python_func): _, python_func = unwrap_decorators(python_func) # Step 2. copy some attributes from original python function. - static_layer = copy_decorator_attrs( - original_func=python_func, - decorated_obj=StaticFunction( - function=python_func, - input_spec=input_spec, - build_strategy=build_strategy)) + static_layer = copy_decorator_attrs(original_func=python_func, + decorated_obj=StaticFunction( + function=python_func, + input_spec=input_spec, + build_strategy=build_strategy)) return static_layer build_strategy = build_strategy or BuildStrategy() if not isinstance(build_strategy, BuildStrategy): raise TypeError( - "Required type(build_strategy) shall be `paddle.static.BuildStrategy`, but received {}". - format(type(build_strategy).__name__)) + "Required type(build_strategy) shall be `paddle.static.BuildStrategy`, but received {}" + .format(type(build_strategy).__name__)) # for usage: `declarative(foo, ...)` if function is not None: @@ -232,8 +231,8 @@ def decorated(python_func): if isinstance(function.forward, StaticFunction): class_name = function.__class__.__name__ logging_utils.warn( - "`{}.forward` has already been decorated somewhere. It will be redecorated to replace previous one.". - format(class_name)) + "`{}.forward` has already been decorated somewhere. It will be redecorated to replace previous one." + .format(class_name)) function.forward = decorated(function.forward) return function else: @@ -284,6 +283,7 @@ def func(x): class _SaveLoadConfig(object): + def __init__(self): self._output_spec = None self._model_filename = None @@ -371,7 +371,7 @@ def keep_name_table(self, value): def _parse_save_configs(configs): - supported_configs = ['output_spec', "with_hook"] + supported_configs = ['output_spec', "with_hook", "clip_extra"] # input check for key in configs: @@ -384,6 +384,7 @@ def _parse_save_configs(configs): inner_config = _SaveLoadConfig() inner_config.output_spec = configs.get('output_spec', None) inner_config.with_hook = configs.get('with_hook', False) + inner_config.clip_extra = configs.get("clip_extra", False) return inner_config @@ -622,6 +623,7 @@ def _remove_save_pre_hook(hook): def _run_save_pre_hooks(func): + def wrapper(layer, path, input_spec=None, **configs): global _save_pre_hooks for hook in _save_pre_hooks: @@ -775,8 +777,8 @@ def fun(inputs): "The paddle.jit.save doesn't work when setting ProgramTranslator.enable to False." ) - if not (isinstance(layer, Layer) or inspect.isfunction(layer) or isinstance( - layer, StaticFunction)): + if not (isinstance(layer, Layer) or inspect.isfunction(layer) + or isinstance(layer, StaticFunction)): raise TypeError( "The input of paddle.jit.save should be 'Layer' or 'Function', but received input type is %s." % type(layer)) @@ -837,7 +839,7 @@ def fun(inputs): # parse configs configs = _parse_save_configs(configs) # whether outermost layer has pre/post hook, if does, we need also save - # these operators in program. + # these operators in program. with_hook = configs.with_hook scope = core.Scope() @@ -848,7 +850,9 @@ def fun(inputs): with_hook = True else: # layer is function - functions = [layer, ] + functions = [ + layer, + ] for attr_func in functions: if isinstance(layer, Layer): static_func = getattr(inner_layer, attr_func, None) @@ -862,8 +866,8 @@ def fun(inputs): if inner_input_spec: inner_input_spec = pack_sequence_as(input_spec, inner_input_spec) - static_forward = declarative( - inner_layer.forward, input_spec=inner_input_spec) + static_forward = declarative(inner_layer.forward, + input_spec=inner_input_spec) concrete_program = static_forward.concrete_program_specify_input_spec( with_hook=with_hook) # the input_spec has been used in declarative, which is equal to @@ -882,14 +886,14 @@ def fun(inputs): if inner_input_spec: inner_input_spec = pack_sequence_as(input_spec, inner_input_spec) - static_function = declarative( - attr_func, input_spec=inner_input_spec) + static_function = declarative(attr_func, + input_spec=inner_input_spec) concrete_program = static_function.concrete_program if static_function._class_instance is None: warnings.warn( - '`jit.save` will only save the `Program`, not the parameters. If you have to save the parameters, please make sure that {} is a member function of `paddle.nn.Layer` and the saved parameters are in `state_dict`'. - format(layer)) + '`jit.save` will only save the `Program`, not the parameters. If you have to save the parameters, please make sure that {} is a member function of `paddle.nn.Layer` and the saved parameters are in `state_dict`' + .format(layer)) dygraph_state_dict = None if isinstance(inner_layer, Layer): @@ -922,8 +926,8 @@ def fun(inputs): param_or_buffer_tensor = scope.var( param_or_buffer.name).get_tensor() #src_tensor = param_or_buffer.value().get_tensor() - src_tensor = state_var_dict[param_or_buffer.name].value( - ).get_tensor() + src_tensor = state_var_dict[ + param_or_buffer.name].value().get_tensor() param_or_buffer_tensor._share_data_with(src_tensor) # record var info if param_or_buffer.name not in extra_var_info: @@ -986,7 +990,7 @@ def fun(inputs): params_filename=params_filename, export_for_deployment=configs._export_for_deployment, program_only=configs._program_only, - clip_extra=False) + clip_extra=configs.clip_extra) # NOTE(chenweihang): [ Save extra variable info ] # save_inference_model will lose some important variable information, including: @@ -1534,14 +1538,16 @@ def forward(self, input): "fluid.dygraph.jit.TracedLayer.save_inference_model") if isinstance(feed, list): for f in feed: - check_type(f, "each element of feed", int, - "fluid.dygraph.jit.TracedLayer.save_inference_model") + check_type( + f, "each element of feed", int, + "fluid.dygraph.jit.TracedLayer.save_inference_model") check_type(fetch, "fetch", (type(None), list), "fluid.dygraph.jit.TracedLayer.save_inference_model") if isinstance(fetch, list): for f in fetch: - check_type(f, "each element of fetch", int, - "fluid.dygraph.jit.TracedLayer.save_inference_model") + check_type( + f, "each element of fetch", int, + "fluid.dygraph.jit.TracedLayer.save_inference_model") clip_extra = kwargs.get('clip_extra', False) # path check file_prefix = os.path.basename(path) @@ -1575,12 +1581,11 @@ def get_feed_fetch(all_vars, partial_vars): model_filename = file_prefix + INFER_MODEL_SUFFIX params_filename = file_prefix + INFER_PARAMS_SUFFIX - save_inference_model( - dirname=dirname, - feeded_var_names=feeded_var_names, - target_vars=target_vars, - executor=self._exe, - main_program=self._program.clone(), - model_filename=model_filename, - params_filename=params_filename, - clip_extra=clip_extra) + save_inference_model(dirname=dirname, + feeded_var_names=feeded_var_names, + target_vars=target_vars, + executor=self._exe, + main_program=self._program.clone(), + model_filename=model_filename, + params_filename=params_filename, + clip_extra=clip_extra) From ee4065ccb04bdb1a10c093e38488efa846c3b160 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 21 Jul 2022 02:50:24 -0500 Subject: [PATCH 4/5] fix tensor stream error in custom op (#44500) --- paddle/phi/api/lib/CMakeLists.txt | 6 +++--- paddle/phi/api/lib/tensor.cc | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 2bbd875158148..0ed3dd6e51085 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -4,17 +4,17 @@ if(WITH_GPU) nv_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce) + DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool) elseif(WITH_ROCM) hip_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce) + DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool) else() cc_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce) + DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool) endif() set(api_gen_base ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/api_base.py) diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index fb81092ffee94..8e1f5ee3f44b6 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -20,7 +20,9 @@ limitations under the License. */ #include "glog/logging.h" +#include "paddle/phi/api/include/context_pool.h" #include "paddle/phi/api/lib/utils/allocator.h" +#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/dense_tensor.h" @@ -32,8 +34,7 @@ limitations under the License. */ #include "paddle/phi/core/tensor_base.h" #include "paddle/phi/core/tensor_meta.h" #include "paddle/phi/core/tensor_utils.h" - -#include "paddle/fluid/platform/stream/cuda_stream.h" +// clang-format off namespace paddle { namespace experimental { @@ -305,7 +306,10 @@ void Tensor::set_impl(std::shared_ptr &&impl) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) gpuStream_t Tensor::stream() const { - return platform::stream::get_current_stream(-1)->raw_stream(); + int device_id = phi::backends::gpu::GetCurrentDeviceId(); + auto* gpu_context = DeviceContextPool::Instance() + .Get(GPUPlace(device_id)); + return gpu_context->stream(); } #endif From 6bce8a4996e711eda38439e92e17a2e38754fd9e Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Sun, 7 Aug 2022 22:04:09 -0500 Subject: [PATCH 5/5] fix custom op attr names size error (#44938) --- paddle/fluid/pybind/eager_functions.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index 4d7b50943d084..352fdbd274f09 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -305,8 +305,7 @@ static std::vector CastAttrsToTragetType( attrs_names.size(), src.size())); for (size_t i = 0; i < src.size(); i++) { size_t end = attrs_names[i].find(": "); - std::string type_name = - attrs_names[i].substr(end + 2, attrs_names.size() - end - 2); + std::string type_name = attrs_names[i].substr(end + 2); if (type_name == "int") { if (src[i].type() == typeid(bool)) { res.emplace_back(static_cast(paddle::any_cast(src[i])));