diff --git a/CMakeLists.txt b/CMakeLists.txt
index bbdba79d2f2ded208aa70c5d99a51b4d364992b7..265ddc9504167f21f54a1b1e7777147b3b6d37d9 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -218,6 +218,9 @@ endif(WITH_AMD_GPU)
if(WITH_ARM)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
+ set(WITH_XBYAK OFF CACHE STRING "Disable XBYAK when compiling WITH_ARM=ON" FORCE)
+ set(WITH_MKL OFF CACHE STRING "Disable MKL when compiling WITH_ARM=ON." FORCE)
+ set(WITH_GPU OFF CACHE STRING "Disable GPU when compiling WITH_ARM=ON." FORCE)
add_definitions(-DPADDLE_WITH_ARM)
endif()
diff --git a/README.md b/README.md
index 1805faeb11f03cc19764bdb6def172fe8b5cdc5a..b07709facd528114a1d69513a487d201f1dfc160 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,9 @@
-# PaddlePaddle
+
+
+
+
+--------------------------------------------------------------------------------
English | [简体中文](./README_cn.md)
@@ -29,7 +33,7 @@ pip install paddlepaddle
# Linux GPU cuda10cudnn7
pip install paddlepaddle-gpu
# Linux GPU cuda9cudnn7
-pip install paddlepaddle-gpu==1.8.2.post97
+pip install paddlepaddle-gpu==1.8.3.post97
```
It is recommended to read [this doc](https://www.paddlepaddle.org.cn/documentation/docs/en/beginners_guide/install/index_en.html) on our website.
diff --git a/README_cn.md b/README_cn.md
index dccd4f227b8d1d0974382b59c40be409edf4210f..93ad06d20010fcba1ff3382b169cb78328f2a375 100644
--- a/README_cn.md
+++ b/README_cn.md
@@ -1,5 +1,9 @@
-# PaddlePaddle
+
+
+
+
+--------------------------------------------------------------------------------
[English](./README.md) | 简体中文
@@ -26,7 +30,7 @@ pip install paddlepaddle
# Linux GPU cuda10cudnn7
pip install paddlepaddle-gpu
# Linux GPU cuda9cudnn7
-pip install paddlepaddle-gpu==1.8.2.post97
+pip install paddlepaddle-gpu==1.8.3.post97
```
更多安装信息详见官网 [安装说明](http://www.paddlepaddle.org.cn/documentation/docs/zh/1.8/beginners_guide/install/index_cn.html)
diff --git a/cmake/external/lite.cmake b/cmake/external/lite.cmake
index 9d7c21108601491744e81b15b48ec0cd31d9bf1d..b541d73bc6a633d8e6a77ff567d756f3b40bfce9 100644
--- a/cmake/external/lite.cmake
+++ b/cmake/external/lite.cmake
@@ -18,6 +18,15 @@ if(NOT LINUX OR NOT WITH_MKL)
return()
endif()
+if(XPU_SDK_ROOT)
+ set(LITE_WITH_XPU ON)
+ include_directories("${XPU_SDK_ROOT}/XTDK/include")
+ include_directories("${XPU_SDK_ROOT}/XTCL/include")
+ add_definitions(-DPADDLE_WITH_XPU)
+ LINK_DIRECTORIES("${XPU_SDK_ROOT}/XTDK/shlib/")
+ LINK_DIRECTORIES("${XPU_SDK_ROOT}/XTDK/runtime/shlib/")
+endif()
+
if (NOT LITE_SOURCE_DIR OR NOT LITE_BINARY_DIR)
include(ExternalProject)
set(LITE_PROJECT extern_lite)
@@ -25,7 +34,7 @@ if (NOT LITE_SOURCE_DIR OR NOT LITE_BINARY_DIR)
set(LITE_INSTALL_DIR ${THIRD_PARTY_PATH}/install/lite)
if(NOT LITE_GIT_TAG)
- set(LITE_GIT_TAG ab8af5c4b4dc5b40217633e0aa436315912d7b53)
+ set(LITE_GIT_TAG 42ab4d559f6659edfc35040fb30fdcec3dc3f8aa)
endif()
if(NOT CUDA_ARCH_NAME)
@@ -47,6 +56,8 @@ if (NOT LITE_SOURCE_DIR OR NOT LITE_BINARY_DIR)
-DCUDNN_ROOT=${CUDNN_ROOT}
-DLITE_WITH_STATIC_CUDA=OFF
-DCUDA_ARCH_NAME=${CUDA_ARCH_NAME}
+ -DLITE_WITH_XPU=${LITE_WITH_XPU}
+ -DXPU_SDK_ROOT=${XPU_SDK_ROOT}
-DLITE_WITH_ARM=OFF)
ExternalProject_Add(
@@ -83,7 +94,7 @@ message(STATUS "Paddle-lite SOURCE_DIR: ${LITE_SOURCE_DIR}")
include_directories(${LITE_SOURCE_DIR})
include_directories(${LITE_BINARY_DIR})
-function(external_lite_static_libs alias path)
+function(external_lite_libs alias path)
add_library(${alias} SHARED IMPORTED GLOBAL)
SET_PROPERTY(TARGET ${alias} PROPERTY IMPORTED_LOCATION
${path})
@@ -92,7 +103,7 @@ function(external_lite_static_libs alias path)
endif()
endfunction()
-external_lite_static_libs(lite_full_static ${LITE_BINARY_DIR}/inference_lite_lib/cxx/lib/libpaddle_full_api_shared.so)
+external_lite_libs(lite_full_static ${LITE_BINARY_DIR}/inference_lite_lib/cxx/lib/libpaddle_full_api_shared.so)
set(LITE_SHARED_LIB ${LITE_BINARY_DIR}/inference_lite_lib/cxx/lib/libpaddle_full_api_shared.so)
add_definitions(-DPADDLE_WITH_LITE)
diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake
index 5e47f268a36699b7e2310c5f5b2c20bcf6f18f1b..5bc7eaaff3abe65e1a12a923880960bbb4268f87 100644
--- a/cmake/external/openblas.cmake
+++ b/cmake/external/openblas.cmake
@@ -20,6 +20,8 @@ SET(CBLAS_INSTALL_DIR ${THIRD_PARTY_PATH}/install/openblas)
SET(CBLAS_REPOSITORY https://github.com/xianyi/OpenBLAS.git)
SET(CBLAS_TAG v0.3.7)
IF(WITH_ARM)
+ # Under the FT2000 architecture, the calculation result of blas.sgemm in openblas 0.3+ is wrong,
+ # so version 0.2 is used by default.
SET(CBLAS_TAG v0.2.18)
ENDIF()
cache_third_party(extern_openblas
diff --git a/cmake/external/protobuf.cmake b/cmake/external/protobuf.cmake
index 04f22d7fc87754eb5d2079575f77094cf25c54ac..82dd4fa2e8eae9ce6dbafa5f2d4acf47ce7ecd9f 100644
--- a/cmake/external/protobuf.cmake
+++ b/cmake/external/protobuf.cmake
@@ -145,9 +145,9 @@ if (NOT "${PROTOBUF_ROOT}" STREQUAL "")
find_program(PROTOBUF_PROTOC_EXECUTABLE protoc PATHS ${PROTOBUF_ROOT}/bin NO_DEFAULT_PATH)
if (PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY AND PROTOBUF_LITE_LIBRARY AND PROTOBUF_PROTOC_LIBRARY AND PROTOBUF_PROTOC_EXECUTABLE)
SET(PROTOBUF_FOUND true)
+ message(STATUS "Using custom protobuf library in ${PROTOBUF_ROOT}.")
SET_PROTOBUF_VERSION()
PROMPT_PROTOBUF_LIB()
- message(STATUS "Using custom protobuf library in ${PROTOBUF_ROOT}.")
endif()
endif()
diff --git a/cmake/flags.cmake b/cmake/flags.cmake
index e6a77c38ab5c0f5178669d9a4d18c571b638fb21..64878693518b686cc208c293c0ad0b410fa26058 100644
--- a/cmake/flags.cmake
+++ b/cmake/flags.cmake
@@ -8,6 +8,8 @@ function(CheckCompilerCXX11Flag)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 4.8)
message(FATAL_ERROR "Unsupported GCC version. GCC >= 4.8 required.")
+ elseif(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 8.2)
+ message(WARNING "Found GCC ${CMAKE_CXX_COMPILER_VERSION} which is too high, recommended to use GCC 8.2")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# cmake >= 3.0 compiler id "AppleClang" on Mac OS X, otherwise "Clang"
diff --git a/cmake/generic.cmake b/cmake/generic.cmake
index 69f4ccae88471dfd5caf1ef2410c5aeefab7db3c..8842e8e21c6df224bb6341a4f7f526e3d61e92e1 100644
--- a/cmake/generic.cmake
+++ b/cmake/generic.cmake
@@ -819,20 +819,18 @@ function(brpc_library TARGET_NAME)
cc_library("${TARGET_NAME}" SRCS "${brpc_library_SRCS}" DEPS "${TARGET_NAME}_proto" "${brpc_library_DEPS}")
endfunction()
-# copy_if_different from src_file to dst_file before barrier_target.
-function(copy_if_different src_file dst_file barrier_target)
- # this is a dummy target, should always be run to update ${dst_file}
- add_custom_target(before_${barrier_target} ALL
- DEPENDS before_${barrier_target}_custom_command
- )
- add_dependencies(${barrier_target} before_${barrier_target})
+# copy_if_different from src_file to dst_file At the beginning of the build.
+function(copy_if_different src_file dst_file)
+ get_filename_component(FILE_NAME ${dst_file} NAME_WE)
- add_custom_command(
- OUTPUT before_${barrier_target}_custom_command
+ # this is a dummy target for custom command, should always be run firstly to update ${dst_file}
+ add_custom_target(copy_${FILE_NAME}_command ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
COMMENT "copy_if_different ${dst_file}"
VERBATIM
)
+
+ add_dependencies(extern_glog copy_${FILE_NAME}_command)
endfunction()
# create a dummy source file, then create a static library.
diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake
index 6fc81f2387b78cce10f9c099a022b2372993c4f9..5a889dbc3143833ff48a972d17efc0aaf63f1810 100644
--- a/cmake/inference_lib.cmake
+++ b/cmake/inference_lib.cmake
@@ -19,9 +19,12 @@ set(FLUID_INSTALL_DIR "${CMAKE_BINARY_DIR}/fluid_install_dir" CACHE STRING
set(FLUID_INFERENCE_INSTALL_DIR "${CMAKE_BINARY_DIR}/fluid_inference_install_dir" CACHE STRING
"A path setting fluid inference shared and static libraries")
+# TODO(zhaolong)
+# At present, the size of static lib in Windows exceeds the system limit,
+# so the generation of static lib is temporarily turned off.
if(WIN32)
#todo: remove the option
- option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
+ option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." OFF)
if(NOT PYTHON_EXECUTABLE)
FIND_PACKAGE(PythonInterp REQUIRED)
endif()
@@ -187,21 +190,18 @@ copy(inference_lib_dist
SRCS ${CMAKE_BINARY_DIR}/../paddle/fluid/framework/io/crypto/cipher.h
DSTS ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include/crypto/)
include_directories(${CMAKE_BINARY_DIR}/../paddle/fluid/framework/io)
+
# CAPI inference library for only inference
set(FLUID_INFERENCE_C_INSTALL_DIR "${CMAKE_BINARY_DIR}/fluid_inference_c_install_dir" CACHE STRING
"A path setting CAPI fluid inference shared")
copy_part_of_thrid_party(inference_lib_dist ${FLUID_INFERENCE_C_INSTALL_DIR})
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
-if(WIN32)
- set(paddle_fluid_c_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/capi/${CMAKE_BUILD_TYPE}/paddle_fluid_c.*)
-else(WIN32)
- set(paddle_fluid_c_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/capi/libpaddle_fluid_c.*)
-endif(WIN32)
+set(paddle_fluid_c_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/capi/libpaddle_fluid_c.*)
copy(inference_lib_dist
- SRCS ${src_dir}/inference/capi/paddle_c_api.h ${paddle_fluid_c_lib}
- DSTS ${FLUID_INFERENCE_C_INSTALL_DIR}/paddle/include ${FLUID_INFERENCE_C_INSTALL_DIR}/paddle/lib)
+ SRCS ${src_dir}/inference/capi/paddle_c_api.h ${paddle_fluid_c_lib}
+ DSTS ${FLUID_INFERENCE_C_INSTALL_DIR}/paddle/include ${FLUID_INFERENCE_C_INSTALL_DIR}/paddle/lib)
# fluid library for both train and inference
set(fluid_lib_deps inference_lib_dist)
diff --git a/cmake/nccl.cmake b/cmake/nccl.cmake
index be84c54fd2fa1b1b16153000715ea453a10aeeef..9124fec0b856a6c46001da3c735454f9aff5493f 100644
--- a/cmake/nccl.cmake
+++ b/cmake/nccl.cmake
@@ -7,14 +7,14 @@ if(WIN32)
return()
endif()
-set(NCCL_ROOT "/usr" CACHE PATH "NCCL ROOT")
-find_path(NCCL_INCLUDE_DIR nccl.h
- PATHS ${NCCL_ROOT} ${NCCL_ROOT}/include ${NCCL_ROOT}/local/include
- $ENV{NCCL_ROOT} $ENV{NCCL_ROOT}/include $ENV{NCCL_ROOT}/local/include
- NO_DEFAULT_PATH
-)
-
if(WITH_NCCL)
+ set(NCCL_ROOT "/usr" CACHE PATH "NCCL ROOT")
+ find_path(NCCL_INCLUDE_DIR nccl.h
+ PATHS ${NCCL_ROOT} ${NCCL_ROOT}/include ${NCCL_ROOT}/local/include
+ $ENV{NCCL_ROOT} $ENV{NCCL_ROOT}/include $ENV{NCCL_ROOT}/local/include
+ NO_DEFAULT_PATH
+ )
+
file(READ ${NCCL_INCLUDE_DIR}/nccl.h NCCL_VERSION_FILE_CONTENTS)
string(REGEX MATCH "define NCCL_VERSION_CODE +([0-9]+)"
diff --git a/cmake/operators.cmake b/cmake/operators.cmake
index 961c1b554a5736c9292633791d39c53fd1a60299..5b03cbf8c7f844e163020ca17d25dc4b732fe636 100644
--- a/cmake/operators.cmake
+++ b/cmake/operators.cmake
@@ -114,7 +114,7 @@ function(op_library TARGET)
endif()
# Define operators that don't need pybind here.
- foreach(manual_pybind_op "compare_reduce_op" "compare_op" "logical_op" "nccl_op"
+ foreach(manual_pybind_op "compare_all_op" "compare_op" "logical_op" "nccl_op"
"tensor_array_read_write_op" "tensorrt_engine_op" "conv_fusion_op"
"fusion_transpose_flatten_concat_op" "fusion_conv_inception_op"
"sync_batch_norm_op" "dgc_op" "fused_fc_elementwise_layernorm_op"
diff --git a/doc/imgs/logo.png b/doc/imgs/logo.png
new file mode 100644
index 0000000000000000000000000000000000000000..3ed4cc8ec82ee3b843dea1644ac6ced246e8a6f6
Binary files /dev/null and b/doc/imgs/logo.png differ
diff --git a/paddle/fluid/framework/array.h b/paddle/fluid/framework/array.h
index 7424bae1ab865e7c82b676e5aca02a438dedc448..10abb83116624dfbf96d04799fb4cf77236997f3 100644
--- a/paddle/fluid/framework/array.h
+++ b/paddle/fluid/framework/array.h
@@ -63,7 +63,8 @@ class Array {
HOSTDEVICE inline const T &at(size_t i) const {
#ifndef __CUDA_ARCH__
- PADDLE_ENFORCE_LT(i, N, "Array index out of bounds");
+ PADDLE_ENFORCE_LT(
+ i, N, platform::errors::OutOfRange("Array index out of bounds."));
#endif
return (*this)[i];
}
@@ -106,7 +107,7 @@ class Array {
static T obj();
return obj;
#else
- PADDLE_THROW("Array has no element");
+ PADDLE_THROW(platform::errors::Unavailable("Array has no element."));
#endif
}
@@ -115,7 +116,7 @@ class Array {
static const T obj();
return obj;
#else
- PADDLE_THROW("Array has no element");
+ PADDLE_THROW(platform::errors::Unavailable("Array has no element."));
#endif
}
diff --git a/paddle/fluid/framework/async_executor.cc b/paddle/fluid/framework/async_executor.cc
index 9f8f17cd1ac68c0549e0927c30df2481d8ee2280..4c7ef2e600bc10141f55f99bd69e8a85177a7840 100644
--- a/paddle/fluid/framework/async_executor.cc
+++ b/paddle/fluid/framework/async_executor.cc
@@ -77,11 +77,13 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program,
for (auto var_name : fetch_var_names) {
auto var_desc = block.FindVar(var_name);
PADDLE_ENFORCE_NOT_NULL(
- var_desc, platform::errors::NotFound("%s is not found.", var_name));
+ var_desc, platform::errors::NotFound(
+ "Variable %s is not found in main program.", var_name));
auto shapes = var_desc->GetShape();
- PADDLE_ENFORCE(shapes[shapes.size() - 1] == 1,
- "var %s: Fetched var has wrong shape, "
- "only variables with the last dimension size 1 supported",
+ PADDLE_ENFORCE_EQ(shapes[shapes.size() - 1], 1,
+ platform::errors::InvalidArgument(
+ "Fetched variable %s has wrong shape, "
+ "only variables whose last dimension is 1 are supported",
var_name);
}
@@ -95,7 +97,7 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program,
actual_thread_num_ = thread_num;
int file_cnt = filelist.size();
PADDLE_ENFORCE_GT(file_cnt, 0,
- platform::errors::NotFound("Input file list is empty"));
+ platform::errors::NotFound("Input file list is empty."));
if (actual_thread_num_ > file_cnt) {
VLOG(1) << "Thread num = " << thread_num << ", file num = " << file_cnt
diff --git a/paddle/fluid/framework/attribute.cc b/paddle/fluid/framework/attribute.cc
index fabf2abfc803b8838edb48aa01ab8896799c97ac..9ca3fe31a33c78621b9e25acaf095e8240af7db6 100644
--- a/paddle/fluid/framework/attribute.cc
+++ b/paddle/fluid/framework/attribute.cc
@@ -72,7 +72,8 @@ Attribute GetAttrValue(const proto::OpDesc::Attr& attr_desc) {
return val;
}
default:
- PADDLE_THROW("Unsupport attr type %d", attr_desc.type());
+ PADDLE_THROW(platform::errors::Unavailable("Unsupport attribute type %d.",
+ attr_desc.type()));
}
return boost::blank();
}
diff --git a/paddle/fluid/framework/attribute.h b/paddle/fluid/framework/attribute.h
index 21bb39b0439876437136bdb0593f25a16677a0e1..e516ae1efdfc6a3fe5157dd65078c3bc67a8d005 100644
--- a/paddle/fluid/framework/attribute.h
+++ b/paddle/fluid/framework/attribute.h
@@ -37,9 +37,10 @@ struct ExtractAttribute {
try {
attr_value = &boost::get(attr);
} catch (boost::bad_get& bad_get) {
- PADDLE_THROW("Cannot get attribute %s by type %s, its type is %s",
- attr_name_, paddle::platform::demangle(typeid(T).name()),
- paddle::platform::demangle(attr.type().name()));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Cannot get attribute (%s) by type %s, its type is %s.", attr_name_,
+ paddle::platform::demangle(typeid(T).name()),
+ paddle::platform::demangle(attr.type().name())));
}
return attr_value;
}
@@ -70,8 +71,9 @@ struct ExtractAttribute {
try {
attr_value = &boost::get(attr);
} catch (boost::bad_get& bad_get) {
- PADDLE_THROW("Cannot get attribute %s by type bool, its type is %s",
- attr_name_, paddle::platform::demangle(attr.type().name()));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Cannot get attribute (%s) by type bool, its type is %s.", attr_name_,
+ paddle::platform::demangle(attr.type().name())));
}
return attr_value;
}
@@ -96,8 +98,9 @@ struct ExtractAttribute {
try {
attr_value = &boost::get(attr);
} catch (boost::bad_get& bad_get) {
- PADDLE_THROW("Cannot get attribute %s by type int64_t, its type is %s",
- attr_name_, paddle::platform::demangle(attr.type().name()));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Cannot get attribute (%s) by type int64_t, its type is %s.",
+ attr_name_, paddle::platform::demangle(attr.type().name())));
}
return attr_value;
}
@@ -124,8 +127,10 @@ struct ExtractAttribute> {
try {
attr_value = &boost::get>(attr);
} catch (boost::bad_get& bad_get) {
- PADDLE_THROW("Cannot get attribute %s by type int64_t, its type is %s",
- attr_name_, paddle::platform::demangle(attr.type().name()));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Cannot get attribute (%s) by type std::vector, its type is "
+ "%s.",
+ attr_name_, paddle::platform::demangle(attr.type().name())));
}
return attr_value;
}
@@ -150,8 +155,9 @@ struct ExtractAttribute {
try {
attr_value = &boost::get(attr);
} catch (boost::bad_get& bad_get) {
- PADDLE_THROW("Cannot get attribute %s by type float, its type is %s",
- attr_name_, paddle::platform::demangle(attr.type().name()));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Cannot get attribute (%s) by type float, its type is %s.",
+ attr_name_, paddle::platform::demangle(attr.type().name())));
}
return attr_value;
}
@@ -173,8 +179,9 @@ class AttrReader {
template
inline const T& Get(const std::string& name) const {
- PADDLE_ENFORCE(attrs_.count(name) != 0, "%s should be in AttributeMap",
- name);
+ PADDLE_ENFORCE_NE(attrs_.count(name), 0,
+ platform::errors::NotFound(
+ "Attribute (%s) should be in AttributeMap.", name));
Attribute& attr = const_cast(attrs_.at(name));
ExtractAttribute extract_attr(name);
@@ -192,8 +199,10 @@ class GreaterThanChecker {
public:
explicit GreaterThanChecker(T lower_bound) : lower_bound_(lower_bound) {}
void operator()(const T& value) const {
- PADDLE_ENFORCE_GT(value, lower_bound_,
- platform::errors::OutOfRange("larger_than check fails."));
+ PADDLE_ENFORCE_GT(
+ value, lower_bound_,
+ platform::errors::OutOfRange(
+ "Check for attribute value greater than a certain value failed."));
}
private:
@@ -205,7 +214,10 @@ class EqualGreaterThanChecker {
public:
explicit EqualGreaterThanChecker(T lower_bound) : lower_bound_(lower_bound) {}
void operator()(const T& value) const {
- PADDLE_ENFORCE_GE(value, lower_bound_, "equal_larger_than check fails.");
+ PADDLE_ENFORCE_GE(
+ value, lower_bound_,
+ platform::errors::OutOfRange("Check for attribute valur equal or "
+ "greater than a certain value failed."));
}
private:
@@ -231,9 +243,10 @@ class EnumInContainer {
public:
explicit EnumInContainer(const std::unordered_set& c) : container_(c) {}
void operator()(const T& val) const {
- PADDLE_ENFORCE(container_.find(val) != container_.end(),
- "Value %s is not in enum container %s", val,
- ContainerDebugString());
+ PADDLE_ENFORCE_NE(
+ container_.find(val), container_.end(),
+ platform::errors::NotFound("Value %s is not in enum container %s.", val,
+ ContainerDebugString()));
}
private:
@@ -284,8 +297,11 @@ class TypedAttrChecker {
// we can add more common limits, like LessThan(), Between()...
TypedAttrChecker& SetDefault(const T& default_value) {
- PADDLE_ENFORCE(default_value_setter_.empty(),
- "%s can't have more than one default value!", attr_name_);
+ PADDLE_ENFORCE_EQ(
+ default_value_setter_.empty(), true,
+ platform::errors::AlreadyExists(
+ "Attribute (%s) has a default value and cannot be set repeatedly.",
+ attr_name_));
default_value_setter_.push_back(DefaultValueSetter(default_value));
return *this;
}
@@ -308,8 +324,10 @@ class TypedAttrChecker {
auto it = attr_map->find(attr_name_);
if (it == attr_map->end()) {
// user do not set this attr
- PADDLE_ENFORCE(!default_value_setter_.empty(),
- "Attribute '%s' is required!", attr_name_);
+ PADDLE_ENFORCE_EQ(
+ default_value_setter_.empty(), false,
+ platform::errors::InvalidArgument(
+ "Attribute (%s) is not set correctly.", attr_name_));
// default_value_setter_ has no more than one element
attr_map->emplace(attr_name_, default_value_setter_[0]());
}
diff --git a/paddle/fluid/framework/data_device_transform.cc b/paddle/fluid/framework/data_device_transform.cc
index fee6ba40047053ed5662fe044eceb0c687bd4db9..7d005c9690b9486ff8c693d9c14f83853a016ced 100644
--- a/paddle/fluid/framework/data_device_transform.cc
+++ b/paddle/fluid/framework/data_device_transform.cc
@@ -23,11 +23,14 @@ void TransDataDevice(const Tensor &in, const platform::Place &dst_place,
PADDLE_ENFORCE_NE(
in.place().which(), dst_place.which(),
- "Currently, model parallelism is only supported between CPU and CUDA");
+ platform::errors::Unavailable("Currently, model parallelism is only "
+ "supported between CPU and CUDA."));
// NOTE(yy): TransDataDevice should wait for computation of input.
- platform::DeviceContextPool::Instance().Get(in.place())->Wait();
- platform::DeviceContextPool::Instance().Get(dst_place)->Wait();
+ if (!platform::is_cuda_pinned_place(in.place())) {
+ platform::DeviceContextPool::Instance().Get(in.place())->Wait();
+ platform::DeviceContextPool::Instance().Get(dst_place)->Wait();
+ }
// FIXME(zcd): TransDataDevice is used to transform data from GPU to CPU and
// the enforced checkings have been done in GetDeviceContext, so the
diff --git a/paddle/fluid/framework/data_feed.cc b/paddle/fluid/framework/data_feed.cc
index 566a08d8a2ad1c05750128e83924fb31aabb4462..96d54ec86917432837d61f681ece91da2ddcab10 100644
--- a/paddle/fluid/framework/data_feed.cc
+++ b/paddle/fluid/framework/data_feed.cc
@@ -133,11 +133,14 @@ bool DataFeed::PickOneFile(std::string* filename) {
}
void DataFeed::CheckInit() {
- PADDLE_ENFORCE(finish_init_, "Initialization did not succeed.");
+ PADDLE_ENFORCE_EQ(finish_init_, true, platform::errors::PreconditionNotMet(
+ "DataFeed initialization failed."));
}
void DataFeed::CheckSetFileList() {
- PADDLE_ENFORCE(finish_set_filelist_, "Set filelist did not succeed.");
+ PADDLE_ENFORCE_EQ(
+ finish_set_filelist_, true,
+ platform::errors::PreconditionNotMet("DataFeed set filelist failed."));
}
void DataFeed::CheckStart() {
@@ -160,14 +163,18 @@ void DataFeed::CopyToFeedTensor(void* dst, const void* src, size_t size) {
#ifdef PADDLE_WITH_CUDA
cudaMemcpy(dst, src, size, cudaMemcpyHostToDevice);
#else
- PADDLE_THROW("Not supported GPU, Please compile WITH_GPU option");
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Not supported GPU, please compile with option WITH_GPU=ON."));
#endif
}
}
template
void PrivateQueueDataFeed::SetQueueSize(int queue_size) {
- PADDLE_ENFORCE(queue_size > 0, "Illegal queue size: %d.", queue_size);
+ PADDLE_ENFORCE_GT(
+ queue_size, 0,
+ platform::errors::InvalidArgument(
+ "Queue size %d is illegal in PrivateQueueDataFeed.", queue_size));
queue_size_ = queue_size;
queue_ = paddle::framework::MakeChannel();
queue_->SetCapacity(queue_size);
@@ -418,8 +425,10 @@ void MultiSlotDataFeed::Init(
finish_set_filelist_ = false;
finish_start_ = false;
- PADDLE_ENFORCE(data_feed_desc.has_multi_slot_desc(),
- "Multi_slot_desc has not been set.");
+ PADDLE_ENFORCE_EQ(
+ data_feed_desc.has_multi_slot_desc(), true,
+ platform::errors::PreconditionNotMet(
+ "Multi_slot_desc has not been set in MultiSlotDataFeed."));
paddle::framework::MultiSlotDesc multi_slot_desc =
data_feed_desc.multi_slot_desc();
SetBatchSize(data_feed_desc.batch_size());
@@ -668,13 +677,14 @@ bool MultiSlotDataFeed::ParseOneInstance(std::vector* instance) {
for (size_t i = 0; i < use_slots_index_.size(); ++i) {
int idx = use_slots_index_[i];
int num = strtol(&str[pos], &endptr, 10);
- PADDLE_ENFORCE(
- num,
- "The number of ids can not be zero, you need padding "
- "it in data generator; or if there is something wrong with "
- "the data, please check if the data contains unresolvable "
- "characters.\nplease check this error line: %s",
- str);
+ PADDLE_ENFORCE_NE(
+ num, 0,
+ platform::errors::InvalidArgument(
+ "The number of ids can not be zero, you need padding "
+ "it in data generator; or if there is something wrong with "
+ "the data, please check if the data contains unresolvable "
+ "characters.\nplease check this error line: %s.",
+ str));
if (idx != -1) {
(*instance)[idx].Init(all_slots_type_[i]);
@@ -765,8 +775,10 @@ void MultiSlotInMemoryDataFeed::Init(
finish_set_filelist_ = false;
finish_start_ = false;
- PADDLE_ENFORCE(data_feed_desc.has_multi_slot_desc(),
- "Multi_slot_desc has not been set.");
+ PADDLE_ENFORCE_EQ(
+ data_feed_desc.has_multi_slot_desc(), true,
+ platform::errors::PreconditionNotMet(
+ "Multi_slot_desc has not been set in MultiSlotInMemoryDataFeed."));
paddle::framework::MultiSlotDesc multi_slot_desc =
data_feed_desc.multi_slot_desc();
SetBatchSize(data_feed_desc.batch_size());
@@ -898,13 +910,14 @@ bool MultiSlotInMemoryDataFeed::ParseOneInstanceFromPipe(Record* instance) {
for (size_t i = 0; i < use_slots_index_.size(); ++i) {
int idx = use_slots_index_[i];
int num = strtol(&str[pos], &endptr, 10);
- PADDLE_ENFORCE(
- num,
- "The number of ids can not be zero, you need padding "
- "it in data generator; or if there is something wrong with "
- "the data, please check if the data contains unresolvable "
- "characters.\nplease check this error line: %s",
- str);
+ PADDLE_ENFORCE_NE(
+ num, 0,
+ platform::errors::InvalidArgument(
+ "The number of ids can not be zero, you need padding "
+ "it in data generator; or if there is something wrong with "
+ "the data, please check if the data contains unresolvable "
+ "characters.\nplease check this error line: %s.",
+ str));
if (idx != -1) {
if (all_slots_type_[i][0] == 'f') { // float
for (int j = 0; j < num; ++j) {
@@ -963,13 +976,14 @@ bool MultiSlotInMemoryDataFeed::ParseOneInstance(Record* instance) {
for (size_t i = 0; i < use_slots_index_.size(); ++i) {
int idx = use_slots_index_[i];
int num = strtol(&str[pos], &endptr, 10);
- PADDLE_ENFORCE(
- num,
- "The number of ids can not be zero, you need padding "
- "it in data generator; or if there is something wrong with "
- "the data, please check if the data contains unresolvable "
- "characters.\nplease check this error line: %s",
- str);
+ PADDLE_ENFORCE_NE(
+ num, 0,
+ platform::errors::InvalidArgument(
+ "The number of ids can not be zero, you need padding "
+ "it in data generator; or if there is something wrong with "
+ "the data, please check if the data contains unresolvable "
+ "characters.\nplease check this error line: %s.",
+ str));
if (idx != -1) {
if (all_slots_type_[i][0] == 'f') { // float
@@ -1085,7 +1099,7 @@ void MultiSlotInMemoryDataFeed::PutToFeedVec(
PADDLE_ENFORCE_EQ(slot_offset.size(), 2,
platform::errors::InvalidArgument(
"In batch reader, the sparse tensor lod size "
- "must be 2, but received %d",
+ "must be 2, but received %d.",
slot_offset.size()));
const auto& max_size = slot_offset[1];
tmp_offset.reserve(max_size + 1);
@@ -1137,10 +1151,13 @@ void PrivateInstantDataFeed::PutToFeedVec() {
for (const auto e : use_slots_shape_[i]) {
total_dims *= e;
}
- PADDLE_ENFORCE(
- total_dims == total_instance,
- "The actual data size of slot[%s] doesn't match its declaration",
- use_slots_[i].c_str());
+ PADDLE_ENFORCE_EQ(
+ total_dims, total_instance,
+ platform::errors::InvalidArgument(
+ "The actual data size of slot[%s] doesn't match its declaration. "
+ "The actual data size of slot is %lld"
+ ", and its declaration is %lld.",
+ use_slots_[i].c_str(), total_dims, total_instance));
feed_vec_[i]->Resize(framework::make_ddim(use_slots_shape_[i]));
}
}
@@ -1162,7 +1179,9 @@ int PrivateInstantDataFeed::Next() {
return -1;
}
- PADDLE_ENFORCE(true == ParseOneMiniBatch(), "Fail to parse mini-batch data");
+ PADDLE_ENFORCE_EQ(
+ true, ParseOneMiniBatch(),
+ platform::errors::InvalidArgument("Fail to parse mini-batch data."));
PutToFeedVec();
return ins_vec_[0].GetBatchSize();
}
@@ -1173,8 +1192,10 @@ void PrivateInstantDataFeed::Init(const DataFeedDesc& data_feed_desc) {
finish_set_filelist_ = false;
finish_start_ = false;
- PADDLE_ENFORCE(data_feed_desc.has_multi_slot_desc(),
- "Multi_slot_desc has not been set.");
+ PADDLE_ENFORCE_EQ(
+ data_feed_desc.has_multi_slot_desc(), true,
+ platform::errors::PreconditionNotMet(
+ "Multi_slot_desc has not been set in PrivateInstantDataFeed."));
paddle::framework::MultiSlotDesc multi_slot_desc =
data_feed_desc.multi_slot_desc();
SetBatchSize(data_feed_desc.batch_size());
@@ -1217,7 +1238,10 @@ template class PrivateInstantDataFeed>;
bool MultiSlotFileInstantDataFeed::Preprocess(const std::string& filename) {
fd_ = open(filename.c_str(), O_RDONLY);
- PADDLE_ENFORCE(fd_ != -1, "Fail to open file: %s", filename.c_str());
+ PADDLE_ENFORCE_NE(
+ fd_, -1, platform::errors::Unavailable(
+ "Fail to open file: %s in MultiSlotFileInstantDataFeed.",
+ filename.c_str()));
struct stat sb;
fstat(fd_, &sb);
@@ -1225,7 +1249,11 @@ bool MultiSlotFileInstantDataFeed::Preprocess(const std::string& filename) {
buffer_ =
reinterpret_cast(mmap(NULL, end_, PROT_READ, MAP_PRIVATE, fd_, 0));
- PADDLE_ENFORCE(buffer_ != MAP_FAILED, strerror(errno));
+ PADDLE_ENFORCE_NE(
+ buffer_, MAP_FAILED,
+ platform::errors::Unavailable(
+ "Memory map failed when create shared memory, error number is %s.",
+ strerror(errno)));
offset_ = 0;
return true;
@@ -1257,12 +1285,13 @@ bool MultiSlotFileInstantDataFeed::ParseOneMiniBatch() {
char type = all_slots_type_[i][0];
uint16_t num = *reinterpret_cast(buffer_ + offset_);
- PADDLE_ENFORCE(
- num,
- "The number of ids can not be zero, you need padding "
- "it in data generator; or if there is something wrong with "
- "the data, please check if the data contains unresolvable "
- "characters.");
+ PADDLE_ENFORCE_NE(
+ num, 0,
+ platform::errors::InvalidArgument(
+ "The number of ids can not be zero, you need padding "
+ "it in data generator; or if there is something wrong with "
+ "the data, please check if the data contains unresolvable "
+ "characters."));
offset_ += sizeof(uint16_t);
if (idx != -1) {
@@ -1304,7 +1333,12 @@ bool MultiSlotFileInstantDataFeed::ParseOneMiniBatch() {
}
PADDLE_ENFORCE(batch_size_ == default_batch_size_ || offset_ == end_,
- "offset_ != end_");
+ platform::errors::InvalidArgument(
+ "The batch size id not equal to default batch size, or "
+ "the offset is not equal to end index."
+ "The batch size is %d, default batcch size is %d, offset "
+ "is %d, end index is %d.",
+ batch_size_, default_batch_size_, offset_, end_));
return true;
}
#endif
diff --git a/paddle/fluid/framework/data_feed.h b/paddle/fluid/framework/data_feed.h
index ef49b28cdbc8104c6f25d6c1f9d7fbd516b38b90..b48d152fe35826363a77104a5cbe39ad800b5eb1 100644
--- a/paddle/fluid/framework/data_feed.h
+++ b/paddle/fluid/framework/data_feed.h
@@ -116,7 +116,8 @@ class DataFeed {
virtual ~DataFeed() {}
virtual void Init(const DataFeedDesc& data_feed_desc) = 0;
virtual bool CheckFile(const char* filename) {
- PADDLE_THROW("This function(CheckFile) is not implemented.");
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "This function(CheckFile) is not implemented."));
}
// Set filelist for DataFeed.
// Pay attention that it must init all readers before call this function.
@@ -179,7 +180,8 @@ class DataFeed {
}
virtual int GetCurBatchSize() { return batch_size_; }
virtual void LoadIntoMemory() {
- PADDLE_THROW("This function(LoadIntoMemory) is not implemented.");
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "This function(LoadIntoMemory) is not implemented."));
}
virtual void SetPlace(const paddle::platform::Place& place) {
place_ = place;
@@ -438,14 +440,23 @@ class MultiSlotType {
private:
void CheckType(const std::string& type) const {
- PADDLE_ENFORCE((type == "uint64") || (type == "float"),
- "There is no this type<%s>.", type);
+ PADDLE_ENFORCE_EQ((type == "uint64" || type == "float"), true,
+ platform::errors::InvalidArgument(
+ "MultiSlotType error, expect type is uint64 or "
+ "float, but received type is %s.",
+ type));
}
void CheckFloat() const {
- PADDLE_ENFORCE(type_[0] == 'f', "Add %s value to float slot.", type_);
+ PADDLE_ENFORCE_EQ(
+ type_[0], 'f',
+ platform::errors::InvalidArgument(
+ "MultiSlotType error, add %s value to float slot.", type_));
}
void CheckUint64() const {
- PADDLE_ENFORCE(type_[0] == 'u', "Add %s value to uint64 slot.", type_);
+ PADDLE_ENFORCE_EQ(
+ type_[0], 'u',
+ platform::errors::InvalidArgument(
+ "MultiSlotType error, add %s value to uint64 slot.", type_));
}
std::vector float_feasign_;
std::vector uint64_feasign_;
diff --git a/paddle/fluid/framework/data_feed_test.cc b/paddle/fluid/framework/data_feed_test.cc
index 9a055765b8c91bedd38a1a5c23d4b3c21e8c80d5..2cc441bbd34cb1e199000a9130d57f39be403699 100644
--- a/paddle/fluid/framework/data_feed_test.cc
+++ b/paddle/fluid/framework/data_feed_test.cc
@@ -34,8 +34,10 @@ paddle::framework::DataFeedDesc load_datafeed_param_from_file(
const char* filename) {
paddle::framework::DataFeedDesc data_feed_desc;
int file_descriptor = open(filename, O_RDONLY);
- PADDLE_ENFORCE_NE(file_descriptor, -1, platform::errors::Unavailable(
- "Cannot open file %s.", filename));
+ PADDLE_ENFORCE_NE(
+ file_descriptor, -1,
+ platform::errors::Unavailable(
+ "Cannot open file %s c load datafeed param from file.", filename));
google::protobuf::io::FileInputStream fileInput(file_descriptor);
google::protobuf::TextFormat::Parse(&fileInput, &data_feed_desc);
close(file_descriptor);
@@ -45,8 +47,10 @@ paddle::framework::DataFeedDesc load_datafeed_param_from_file(
const std::vector load_filelist_from_file(const char* filename) {
std::vector filelist;
std::ifstream fin(filename);
- PADDLE_ENFORCE_EQ(fin.good(), true, platform::errors::Unavailable(
- "Cannot open file %s.", filename));
+ PADDLE_ENFORCE_EQ(
+ fin.good(), true,
+ platform::errors::Unavailable(
+ "Cannot open file %s when load filelist from file.", filename));
std::string line;
while (getline(fin, line)) {
filelist.push_back(line);
@@ -196,7 +200,8 @@ void GetElemSetFromReader(std::vector* reader_elem_set,
}
}
} else {
- PADDLE_THROW("Error type in proto file.");
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Error type in proto file."));
}
} else { // sparse branch
if (slot.type() == "uint64") {
@@ -218,7 +223,8 @@ void GetElemSetFromReader(std::vector* reader_elem_set,
}
}
} else {
- PADDLE_THROW("Error type in proto file.");
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Error type in proto file."));
}
} // end sparse branch
++index;
@@ -272,7 +278,10 @@ void GetElemSetFromFile(std::vector* file_elem_set,
file_elem_set->resize(used_slot_num);
for (const auto& file : filelist) {
std::ifstream fin(file.c_str());
- PADDLE_ENFORCE(fin.good(), "Can not open %s.", file.c_str());
+ PADDLE_ENFORCE_EQ(
+ fin.good(), true,
+ platform::errors::Unavailable(
+ "Can not open %s when get element set from file.", file.c_str()));
while (1) {
bool end_flag = false;
int index = 0;
@@ -298,7 +307,8 @@ void GetElemSetFromFile(std::vector* file_elem_set,
}
}
} else {
- PADDLE_THROW("Error type in proto file.");
+ PADDLE_THROW(
+ platform::errors::InvalidArgument("Error type in proto file."));
}
if (slot.is_used()) {
++index;
diff --git a/paddle/fluid/framework/data_layout.h b/paddle/fluid/framework/data_layout.h
index b611bb77b4e1ec05b8bd029ac37cefba346c6eb0..947f06408d02874f7c701f16b356df36012d0d0c 100644
--- a/paddle/fluid/framework/data_layout.h
+++ b/paddle/fluid/framework/data_layout.h
@@ -45,7 +45,8 @@ inline DataLayout StringToDataLayout(const std::string& str) {
} else if (s == "MKLDNNLAYOUT") {
return DataLayout::kMKLDNN;
} else {
- PADDLE_THROW("Unknown storage order string: %s", s);
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Unknown data layout type string: %s.", s));
}
}
@@ -60,7 +61,8 @@ inline std::string DataLayoutToString(const DataLayout& data_layout) {
case DataLayout::kMKLDNN:
return "MKLDNNLAYOUT";
default:
- PADDLE_THROW("unknown DataLayout %d", data_layout);
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Unknown Data Layout type %d.", data_layout));
}
}
diff --git a/paddle/fluid/framework/data_layout_transform.cc b/paddle/fluid/framework/data_layout_transform.cc
index 59a76ce103c0e30b1a927b14ae9b01bdb7a275ce..3cea7a66d01051824a1de01d62c237636771804b 100644
--- a/paddle/fluid/framework/data_layout_transform.cc
+++ b/paddle/fluid/framework/data_layout_transform.cc
@@ -25,14 +25,17 @@ namespace paddle {
namespace framework {
std::vector GetAxis(const DataLayout& from, const DataLayout& to) {
- PADDLE_ENFORCE_NE(from, to,
- "layout transform should transform different layout");
+ PADDLE_ENFORCE_NE(
+ from, to,
+ platform::errors::InvalidArgument(
+ "Layout transform should transform between different layout."));
if (from == DataLayout::kNCHW && to == DataLayout::kNHWC) {
return {0, 2, 3, 1};
} else if (from == DataLayout::kNHWC && to == DataLayout::kNCHW) {
return {0, 3, 1, 2};
} else {
- PADDLE_THROW("unsupported transform");
+ PADDLE_THROW(
+ platform::errors::InvalidArgument("Unsupported layout transform."));
}
}
@@ -55,7 +58,8 @@ struct CastDataLayout {
auto* context = static_cast(ctx_);
trans4(*context, in_, out_, axis_);
} else {
- PADDLE_THROW("Unsupport CPU <-> GPU!");
+ PADDLE_THROW(platform::errors::PreconditionNotMet(
+ "Unsupported data layout cast from CPU to GPU."));
}
}
};
@@ -66,9 +70,14 @@ void TransDataLayout(const OpKernelType& kernel_type_for_var,
PADDLE_ENFORCE(
platform::places_are_same_class(kernel_type_for_var.place_,
expected_kernel_type.place_),
- "TransDataLayout only support DataLayout transform on same place!");
+ platform::errors::PreconditionNotMet(
+ "TransDataLayout only support DataLayout transform on same place."));
- PADDLE_ENFORCE(arity(in.dims()) == 4, "Input Arity only support 4!");
+ PADDLE_ENFORCE_EQ(
+ arity(in.dims()), 4,
+ platform::errors::InvalidArgument(
+ "Input dimension arity only can be 4, the input dimension is %s.",
+ in.dims()));
auto& pool = platform::DeviceContextPool::Instance();
@@ -108,7 +117,8 @@ void* GetDataFromTensor(const Tensor& tensor, mkldnn::memory::data_type type) {
case mkldnn::memory::data_type::s32:
return platform::to_void_cast(tensor.data());
default:
- PADDLE_THROW("wrong mkldnn type provided");
+ PADDLE_THROW(
+ platform::errors::InvalidArgument("Wrong mkldnn type provided."));
}
}
@@ -121,8 +131,9 @@ void TransDataLayoutFromMKLDNN(const OpKernelType& kernel_type_for_var,
PADDLE_ENFORCE(
in_layout == DataLayout::kMKLDNN && out_layout != DataLayout::kMKLDNN,
- "TransDataLayoutFromMKLDNN only supports transform from MKLDNN to "
- "non-MKLDNN");
+ platform::errors::InvalidArgument(
+ "TransDataLayoutFromMKLDNN only supports transform from MKLDNN to "
+ "non-MKLDNN"));
innerTransDataLayoutFromMKLDNN(
in_layout,
@@ -155,7 +166,9 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout,
memory::data_type in_type = ToMKLDNNDataType(in.type());
PADDLE_ENFORCE_NE(in_type, memory::data_type::undef,
- "Input tensor type is not supported: %s", in.type());
+ platform::errors::InvalidArgument(
+ "Input tensor type (%s) is not supported.",
+ DataTypeToString(in.type())));
auto in_format = platform::MKLDNNFormatForSize(in_tz.size(), in.format());
auto out_format =
diff --git a/paddle/fluid/framework/data_layout_transform.h b/paddle/fluid/framework/data_layout_transform.h
index 711146efd267b80260c17dc89bb35932e534c9c6..6eb84ef9d7c01b589cc95a78ea9727a81f6dc36e 100644
--- a/paddle/fluid/framework/data_layout_transform.h
+++ b/paddle/fluid/framework/data_layout_transform.h
@@ -38,8 +38,9 @@ inline MKLDNNMemoryFormat ToMKLDNNFormat(const DataLayout& layout) {
case DataLayout::kNCHW:
return MKLDNNMemoryFormat::nchw;
default:
- PADDLE_THROW("Fail to convert layout %s to MKLDNN format",
- DataLayoutToString(layout));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Fail to convert layout %s to MKLDNN format.",
+ DataLayoutToString(layout)));
}
}
@@ -50,7 +51,8 @@ inline DataLayout ToPaddleLayout(const MKLDNNMemoryFormat& format) {
case MKLDNNMemoryFormat::nchw:
return DataLayout::kNCHW;
default:
- PADDLE_THROW("Fail to convert MKLDNN format to paddle layout");
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Fail to convert MKLDNN format to paddle layout."));
}
}
diff --git a/paddle/fluid/framework/data_transform.cc b/paddle/fluid/framework/data_transform.cc
index 76c53e82315773dfc2d9f1c073e055e35b1fee00..f54311eebfade312057224ddda075c03fdc0666d 100644
--- a/paddle/fluid/framework/data_transform.cc
+++ b/paddle/fluid/framework/data_transform.cc
@@ -45,9 +45,10 @@ void TransformData(const OpKernelType &expected_kernel_type,
if (NeedTransformLayout(lout, lin)) {
#ifdef PADDLE_WITH_MKLDNN
if (lin == DataLayout::kMKLDNN || lout == DataLayout::kMKLDNN) {
- PADDLE_ENFORCE(
- !(lin == DataLayout::kMKLDNN && lout == DataLayout::kMKLDNN),
- "No layout transform needed between two MKLDNN OPKernels");
+ PADDLE_ENFORCE_EQ(
+ !(lin == DataLayout::kMKLDNN && lout == DataLayout::kMKLDNN), true,
+ platform::errors::PreconditionNotMet(
+ "No layout transform needed between two MKLDNN OPKernels."));
if (lin != DataLayout::kMKLDNN && lout == DataLayout::kMKLDNN) {
// Case1 - transform from Non-MKLDNN OPKernel to MKLDNN OPKernel
@@ -96,7 +97,10 @@ void TransformData(const OpKernelType &expected_kernel_type,
PassTensorData(&out, &in);
}
- PADDLE_ENFORCE(transformed, "No transform is applied, please check!");
+ PADDLE_ENFORCE_EQ(
+ transformed, true,
+ platform::errors::PreconditionNotMet(
+ "No transform is applied for the data needs to be transformed."));
// get output data
output_tensor->ShareDataWith(in);
}
@@ -116,7 +120,10 @@ void SetTensorToVariable(const Variable &in_var, const Tensor &tensor,
trans_selected_rows->set_rows(in_selected_rows.rows());
trans_selected_rows->mutable_value()->ShareDataWith(tensor);
} else {
- PADDLE_THROW("unknown var type");
+ PADDLE_THROW(platform::errors::Unavailable(
+ "Unsupported variable type, only supports LoDTensor or SelectedRows, "
+ "but the input variable type is %s.",
+ ToTypeName(in_var.Type())));
}
}
diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc
index a0248cf3c75690fb9ec3fcc22596af245d042d80..f479d92483c1c39a0b43e0d8c514237bf89bcc00 100644
--- a/paddle/fluid/framework/data_type.cc
+++ b/paddle/fluid/framework/data_type.cc
@@ -65,7 +65,8 @@ proto::VarType::Type ToDataType(std::type_index type) {
if (it != gDataTypeMap().cpp_to_proto_.end()) {
return it->second;
}
- PADDLE_THROW("Not support %s as tensor type", type.name());
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Not support %s as tensor data type.", platform::demangle(type.name())));
}
std::type_index ToTypeIndex(proto::VarType::Type type) {
@@ -73,8 +74,9 @@ std::type_index ToTypeIndex(proto::VarType::Type type) {
if (it != gDataTypeMap().proto_to_cpp_.end()) {
return it->second;
}
- PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type",
- static_cast(type));
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Not support proto::VarType::Type(%d) as tensor type.",
+ static_cast(type)));
}
std::string DataTypeToString(const proto::VarType::Type type) {
@@ -82,8 +84,9 @@ std::string DataTypeToString(const proto::VarType::Type type) {
if (it != gDataTypeMap().proto_to_str_.end()) {
return it->second;
}
- PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type",
- static_cast(type));
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Not support proto::VarType::Type(%d) as tensor type.",
+ static_cast(type)));
}
size_t SizeOfType(proto::VarType::Type type) {
@@ -91,7 +94,8 @@ size_t SizeOfType(proto::VarType::Type type) {
if (it != gDataTypeMap().proto_to_size_.end()) {
return it->second;
}
- PADDLE_THROW("Not support %s as tensor type", DataTypeToString(type));
+ PADDLE_THROW(platform::errors::Unimplemented("Not support %s as tensor type.",
+ DataTypeToString(type)));
}
} // namespace framework
diff --git a/paddle/fluid/framework/data_type.h b/paddle/fluid/framework/data_type.h
index e3b45d05d85e9da0d1112fe7dabd06f10225166d..2c4a7b4d02727437742b19cc6d51e209e4346d03 100644
--- a/paddle/fluid/framework/data_type.h
+++ b/paddle/fluid/framework/data_type.h
@@ -78,7 +78,9 @@ inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
_ForEachDataType_(VisitDataTypeCallback);
#undef VisitDataTypeCallback
- PADDLE_THROW("Not supported %d", type);
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Not supported proto::VarType::Type(%d) as data type.",
+ static_cast(type)));
}
template
diff --git a/paddle/fluid/framework/data_type_transform.cc b/paddle/fluid/framework/data_type_transform.cc
index d79f8cacb5f4727defc77380371e57bcea65f068..44542f05d9d5c92f58a84dc2be59782bae2ff3aa 100644
--- a/paddle/fluid/framework/data_type_transform.cc
+++ b/paddle/fluid/framework/data_type_transform.cc
@@ -56,7 +56,8 @@ struct CastDataType {
context->Wait();
#endif
} else {
- PADDLE_THROW("Unsupported place!");
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Place type is not supported when casting data type."));
}
}
};
@@ -98,7 +99,9 @@ void TransDataType(const OpKernelType& kernel_type_for_var,
framework::VisitDataType(dst_type, CastDataType(in, out, ctx));
break;
default:
- PADDLE_THROW("Not support type %d", src_type);
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Data type (%s) is not supported when casting data type.",
+ DataTypeToString(src_type)));
}
}
diff --git a/paddle/fluid/framework/ddim.cc b/paddle/fluid/framework/ddim.cc
index 799deec1b6955ed4df534e3eec38081fbd345857..fe7d243066237d3fe4ef11b29532c9fbf72c9a75 100644
--- a/paddle/fluid/framework/ddim.cc
+++ b/paddle/fluid/framework/ddim.cc
@@ -81,9 +81,11 @@ bool contain_unknown_dim(const DDim& ddim) {
}
DDim slice_ddim(const DDim& dim, int begin, int end) {
- PADDLE_ENFORCE(begin >= 0 && end <= dim.size(),
- "[begin(%d), end(%d)) must be inside [0, %d) in ddim slice.",
- begin, end, dim.size());
+ PADDLE_ENFORCE_EQ(
+ (begin >= 0 && end <= dim.size()), true,
+ platform::errors::InvalidArgument(
+ "[begin(%d), end(%d)) must be inside [0, %d) in ddim slice.", begin,
+ end, dim.size()));
// Constructor of DDim would check whether end - begin is valid
return DDim(dim.Get() + begin, end - begin);
}
diff --git a/paddle/fluid/framework/ddim.h b/paddle/fluid/framework/ddim.h
index cbc8b0fb7cc7813a2bf1b309bc24a15d3af0f13e..29c4732f99118fe42f08317625ec07edf52ec217 100644
--- a/paddle/fluid/framework/ddim.h
+++ b/paddle/fluid/framework/ddim.h
@@ -29,20 +29,23 @@ namespace framework {
return (callback); \
}
-#define PADDLE_VISIT_DDIM(rank, callback) \
- switch (rank) { \
- PADDLE_VISIT_DDIM_BASE(0, callback); \
- PADDLE_VISIT_DDIM_BASE(1, callback); \
- PADDLE_VISIT_DDIM_BASE(2, callback); \
- PADDLE_VISIT_DDIM_BASE(3, callback); \
- PADDLE_VISIT_DDIM_BASE(4, callback); \
- PADDLE_VISIT_DDIM_BASE(5, callback); \
- PADDLE_VISIT_DDIM_BASE(6, callback); \
- PADDLE_VISIT_DDIM_BASE(7, callback); \
- PADDLE_VISIT_DDIM_BASE(8, callback); \
- PADDLE_VISIT_DDIM_BASE(9, callback); \
- default: \
- PADDLE_THROW("Invalid rank %d", rank); \
+#define PADDLE_VISIT_DDIM(rank, callback) \
+ switch (rank) { \
+ PADDLE_VISIT_DDIM_BASE(0, callback); \
+ PADDLE_VISIT_DDIM_BASE(1, callback); \
+ PADDLE_VISIT_DDIM_BASE(2, callback); \
+ PADDLE_VISIT_DDIM_BASE(3, callback); \
+ PADDLE_VISIT_DDIM_BASE(4, callback); \
+ PADDLE_VISIT_DDIM_BASE(5, callback); \
+ PADDLE_VISIT_DDIM_BASE(6, callback); \
+ PADDLE_VISIT_DDIM_BASE(7, callback); \
+ PADDLE_VISIT_DDIM_BASE(8, callback); \
+ PADDLE_VISIT_DDIM_BASE(9, callback); \
+ default: \
+ PADDLE_THROW(platform::errors::Unimplemented( \
+ "Invalid dimension to be accessed. Now only supports access to " \
+ "dimension 0 to 9, but received dimension is %d.", \
+ rank)); \
}
template
@@ -92,13 +95,31 @@ class DDim {
inline int64_t operator[](int idx) const { return dim_[idx]; }
- inline int64_t& at(int idx) {
- PADDLE_ENFORCE(idx >= 0 && idx < rank_, "Invalid idx %d", idx);
+ int64_t& at(int idx) {
+ PADDLE_ENFORCE_GE(idx, 0,
+ platform::errors::InvalidArgument(
+ "Invalid DDim index to be accessed. The valid index "
+ "is between 0 and %d, but received index is %d.",
+ rank_, idx));
+ PADDLE_ENFORCE_LT(idx, rank_,
+ platform::errors::InvalidArgument(
+ "Invalid DDim index to be accessed. The valid index "
+ "is between 0 and %d, but received index is %d.",
+ rank_, idx));
return dim_[idx];
}
- inline int64_t at(int idx) const {
- PADDLE_ENFORCE(idx >= 0 && idx < rank_, "Invalid idx %d", idx);
+ int64_t at(int idx) const {
+ PADDLE_ENFORCE_GE(idx, 0,
+ platform::errors::InvalidArgument(
+ "Invalid DDim index to be accessed. The valid index "
+ "is between 0 and %d, but received index is %d.",
+ rank_, idx));
+ PADDLE_ENFORCE_LT(idx, rank_,
+ platform::errors::InvalidArgument(
+ "Invalid DDim index to be accessed. The valid index "
+ "is between 0 and %d, but received index is %d.",
+ rank_, idx));
return dim_[idx];
}
diff --git a/paddle/fluid/framework/details/async_ssa_graph_executor.cc b/paddle/fluid/framework/details/async_ssa_graph_executor.cc
index 9615347d5478873aa000b6320f35040cc9537243..1cf4eb6c2989346c9e9acef648aa74615c7bcb10 100644
--- a/paddle/fluid/framework/details/async_ssa_graph_executor.cc
+++ b/paddle/fluid/framework/details/async_ssa_graph_executor.cc
@@ -42,53 +42,18 @@ inline void InitVarsInScope(const std::vector &var_infos, Scope *scope,
}
}
-// get RpcContext and remote send and recv op
+// get CommContext and remote send and recv op
void ProcessGraph(std::vector graphs, Scope *scope) {
#ifdef PADDLE_WITH_DISTRIBUTE
- using RpcCtxMap = operators::distributed::RpcCtxMap;
- VLOG(3) << "ProcessGraph";
- RpcCtxMap send_varname_to_ctx;
-
- for (auto &node : graphs[0]->Nodes()) {
- VLOG(3) << "node name " << node->Name();
- if (node && node->IsOp()) {
- if (node->Name() == "send") {
- auto send_var_name = node->Op()->Input("X")[0];
- auto send_varnames =
- BOOST_GET_CONST(std::vector,
- node->Op()->GetNullableAttr("send_varnames"));
- auto epmap = BOOST_GET_CONST(std::vector,
- node->Op()->GetNullableAttr("epmap"));
- auto height_section = BOOST_GET_CONST(
- std::vector, node->Op()->GetNullableAttr("sections"));
- auto trainer_id =
- BOOST_GET_CONST(int, node->Op()->GetNullableAttr("trainer_id"));
- auto merge_add =
- BOOST_GET_CONST(bool, node->Op()->GetNullableAttr("merge_add"));
- if (!merge_add) {
- merge_add = FLAGS_communicator_is_sgd_optimizer;
- }
- auto use_send_handler = BOOST_GET_CONST(
- bool, node->Op()->GetNullableAttr("use_send_handler"));
- send_varname_to_ctx[send_var_name] = operators::distributed::RpcContext(
- send_var_name, send_varnames, epmap, height_section, trainer_id,
- merge_add, use_send_handler);
- VLOG(3) << "find and init an send op: "
- << send_varname_to_ctx[send_var_name];
- }
- }
- }
-
// init communicator here
- if (send_varname_to_ctx.size() > 0) {
- auto *instance = operators::distributed::Communicator::GetInstance();
- auto initialized = instance ? true : false;
- PADDLE_ENFORCE_EQ(initialized, true,
- platform::errors::InvalidArgument(
- "Communicator is not Initialized, you may use "
- "FleetAPI(https://github.com/PaddlePaddle/Fleet/tree/"
- "develop/markdown_doc/transpiler)"));
- }
+ auto *instance = operators::distributed::Communicator::GetInstance();
+ auto initialized = instance ? true : false;
+ PADDLE_ENFORCE_EQ(initialized, true,
+ platform::errors::InvalidArgument(
+ "Communicator is not Initialized, you may use "
+ "FleetAPI(https://github.com/PaddlePaddle/Fleet/tree/"
+ "develop/markdown_doc/transpiler)"));
+
#endif
}
diff --git a/paddle/fluid/framework/device_worker.cc b/paddle/fluid/framework/device_worker.cc
index f7e64b4f659d800e2acb89c5680bfbde6441b1a8..aeec6161714028352da3628027864e8660dad774 100644
--- a/paddle/fluid/framework/device_worker.cc
+++ b/paddle/fluid/framework/device_worker.cc
@@ -111,6 +111,7 @@ void DeviceWorker::DumpParam(const Scope& scope, const int batch_id) {
writer_ << os.str();
}
}
+
void DeviceWorker::InitRandomDumpConfig(const TrainerDesc& desc) {
bool enable_random_dump = desc.enable_random_dump();
if (!enable_random_dump) {
diff --git a/paddle/fluid/framework/device_worker.h b/paddle/fluid/framework/device_worker.h
index d6d53a8858030734812587f6bbd03a108c5cf8ce..07470ef8532a0a0526d2e6228571716da37d78a8 100644
--- a/paddle/fluid/framework/device_worker.h
+++ b/paddle/fluid/framework/device_worker.h
@@ -335,6 +335,7 @@ class SectionWorker : public DeviceWorker {
void SetSkipVars(const std::vector& skip_vars) {
skip_vars_ = skip_vars;
}
+ static void ResetBatchId() { batch_id_ = 0; }
static std::atomic cpu_id_;
diff --git a/paddle/fluid/framework/dist_multi_trainer.cc b/paddle/fluid/framework/dist_multi_trainer.cc
index 6ed68bb09644b7b9984ebf0df656256622a332f4..e2a7375df9e46713aebe9f815f93809568b86c0f 100644
--- a/paddle/fluid/framework/dist_multi_trainer.cc
+++ b/paddle/fluid/framework/dist_multi_trainer.cc
@@ -99,7 +99,7 @@ void DistMultiTrainer::InitTrainerEnv(const ProgramDesc &main_program,
}
void DistMultiTrainer::InitOtherEnv(const ProgramDesc &main_program) {
- if (need_dump_field_) {
+ if (need_dump_field_ || need_dump_param_) {
InitDumpEnv();
}
pull_dense_worker_->SetRootScope(root_scope_);
@@ -158,7 +158,7 @@ void DistMultiTrainer::Finalize() {
}
}
- if (need_dump_field_) {
+ if (need_dump_field_ || need_dump_param_) {
FinalizeDumpEnv();
}
pull_dense_worker_->Stop();
diff --git a/paddle/fluid/framework/dist_multi_trainer_test.cc b/paddle/fluid/framework/dist_multi_trainer_test.cc
index f54029fd17f1c632e1a0bbbec69679241f26f379..75543b7b30e6f4ce6e5e8879c3d12b74d82a066d 100644
--- a/paddle/fluid/framework/dist_multi_trainer_test.cc
+++ b/paddle/fluid/framework/dist_multi_trainer_test.cc
@@ -49,7 +49,12 @@ TEST(DisMultiTrainerTest, test1) {
dataset->SetTrainerNum(1);
dataset->SetDataFeedDesc(str);
dataset->CreateReaders();
+ Scope root_scope;
+ tmp1->SetScope(&root_scope);
tmp1->Initialize(t, dataset.get());
+ ProgramDesc p;
+ tmp1->InitOtherEnv(p);
+ tmp1->Finalize();
#endif
}
} // namespace framework
diff --git a/paddle/fluid/framework/distributed_strategy.proto b/paddle/fluid/framework/distributed_strategy.proto
old mode 100644
new mode 100755
index 9bcd79cd34f07cb38ea28e1068bb6045cb82d27a..d17e68276cd1ce576029cf306a18469aef2ffdb0
--- a/paddle/fluid/framework/distributed_strategy.proto
+++ b/paddle/fluid/framework/distributed_strategy.proto
@@ -22,56 +22,104 @@ enum Mode {
HETER = 4; // support XPU and GPU computing server
}
-message DistributedStrategy {
- optional Mode mode = 1 [ default = COLLECTIVE ]; // just for serialization
- // collective training strategy
- optional bool amp = 2 [ default = false ];
- optional int32 amp_loss_scaling = 3 [ default = 32768 ];
- optional bool recompute = 4 [ default = false ];
- repeated string recompute_checkpoints = 5;
- optional bool localsgd = 6 [ default = false ];
- optional int32 localsgd_k_step = 7 [ default = 4 ];
- optional bool dgc = 8 [ default = false ];
- optional bool hierachical_allreduce = 9 [ default = false ];
- optional int32 nccl_comm_num = 10 [ default = 1 ];
- optional bool gradient_merge = 11 [ default = false ];
- optional int32 gradient_merge_k_step = 12 [ default = 1 ];
- optional bool sequential_execution = 13 [ default = false ];
- optional bool enable_backward_optimizer_op_deps = 14 [ default = true ];
- optional bool lars = 15 [ default = false ];
- optional bool lamb = 16 [ default = false ];
- optional bool fuse_elewise_add_act_ops = 17 [ default = false ];
- optional bool fuse_bn_act_ops = 18 [ default = false ];
- optional bool enable_auto_fusion = 19 [ default = false ];
- optional bool fuse_relu_depthwise_conv = 20 [ default = false ];
- optional bool enable_inplace = 21 [ default = false ];
- optional bool fuse_all_reduce_ops = 22 [ default = false ];
- optional int32 num_iteration_per_drop_scope = 23 [ default = 1 ];
- optional bool sync_batch_norm = 24 [ default = false ];
- optional bool fuse_all_optimizer_ops = 25 [ default = false ];
+message RecomputeConfig { repeated string checkpoints = 1; }
+
+message AMPConfig {
+ optional float init_loss_scaling = 1 [ default = 32768.0 ];
+ optional int32 incr_every_n_steps = 2 [ default = 1000 ];
+ optional int32 decr_every_n_nan_or_inf = 3 [ default = 2 ];
+ optional float incr_ratio = 4 [ default = 2.0 ];
+ optional float decr_ratio = 5 [ default = 0.8 ];
+ optional bool use_dynamic_loss_scaling = 6 [ default = true ];
+ repeated string custom_white_list = 7;
+ repeated string custom_black_list = 8;
+ repeated string custom_black_varnames = 9;
+}
+
+message LocalSGDConfig { optional int32 k_steps = 1 [ default = 4 ]; }
+
+message GradientMergeConfig {
+ optional int32 k_steps = 1 [ default = 1 ];
+ optional bool avg = 2 [ default = true ];
+}
+
+message LarsConfig {
+ optional float lars_coeff = 1 [ default = 0.001 ];
+ optional float lars_weight_decay = 2 [ default = 0.0005 ];
+}
- // pipeline training
- optional bool pipeline = 101 [ default = false ];
- optional int32 pipeline_micro_batch = 102;
+message LambConfig {
+ optional float beta1 = 1 [ default = 0.001 ];
+ optional float beta2 = 2 [ default = 0.999 ];
+ optional float epsilon = 3 [ default = 0.000001 ];
+}
- // parameter server training
- optional bool sync = 201 [ default = false ];
- optional bool async = 202 [ default = true ];
- optional int32 async_k_step = 203 [ default = -1 ];
- optional int32 max_merge_var_num = 204 [ default = 1 ];
- optional int32 send_queue_size = 205 [ default = 16 ];
- optional bool independent_recv_thread = 206 [ default = false ];
- optional int32 min_send_grad_num_before_recv = 207 [ default = 1 ];
- optional int32 thread_pool_size = 208 [ default = 1 ];
- optional int32 send_wait_times = 209 [ default = 1 ];
- optional bool runtime_split_send_recv = 210 [ default = false ];
- optional bool use_thread_barrier = 211 [ default = false ];
+message BuildStrategy {
+ optional bool enable_sequential_execution = 1 [ default = false ];
+ optional bool fuse_elewise_add_act_ops = 2 [ default = false ];
+ optional bool fuse_bn_act_ops = 3 [ default = false ];
+ optional bool fuse_relu_depthwise_conv = 4 [ default = false ];
+ optional bool fuse_broadcast_ops = 5 [ default = false ];
+ optional bool fuse_all_optimizer_ops = 6 [ default = false ];
+ optional bool enable_inplace = 7 [ default = false ];
+ optional bool enable_backward_optimizer_op_deps = 8 [ default = true ];
+ optional bool cache_runtime_context = 9 [ default = false ];
+}
- // elastic deep learning strategies
- optional bool elastic = 301 [ default = false ];
+message ExecutionStrategy {
+ optional int32 num_threads = 1 [ default = 1 ];
+ optional int32 num_iteration_per_drop_scope = 2 [ default = 10 ];
+ optional int32 num_iteration_per_run = 3 [ default = 1 ];
+ optional bool use_thread_barrier = 4 [ default = false ];
+}
+
+message AsyncConfig {
+ optional int32 k_steps = 1 [ default = 1 ];
+ optional int32 max_merge_var_num = 2 [ default = 1 ];
+ optional int32 send_queue_size = 3 [ default = 16 ];
+ optional bool independent_recv_thread = 4 [ default = false ];
+ optional int32 min_send_grad_num_before_recv = 5 [ default = 1 ];
+ optional int32 thread_pool_size = 6 [ default = 1 ];
+ optional int32 send_wait_times = 7 [ default = 1 ];
+ optional bool runtime_split_send_recv = 8 [ default = false ];
+}
+
+message PipelineConfig { optional int32 micro_batch = 1 [ default = 1 ]; }
+
+message DistributedStrategy {
+ // bool options
+ optional Mode mode = 1 [ default = COLLECTIVE ];
+ optional bool amp = 2 [ default = false ];
+ optional bool recompute = 3 [ default = false ];
+ optional bool localsgd = 4 [ default = false ];
+ optional bool dgc = 5 [ default = false ];
+ optional bool gradient_merge = 6 [ default = false ];
+ optional bool lars = 7 [ default = false ];
+ optional bool lamb = 8 [ default = false ];
+ optional bool pipeline = 9 [ default = false ];
+ optional bool elastic = 10 [ default = false ];
+ optional bool auto = 11 [ default = false ];
+ optional bool a_sync = 12 [ default = true ];
+ optional bool sync_nccl_allreduce = 13 [ default = true ];
+ optional int32 nccl_comm_num = 14 [ default = 1 ];
+ optional bool use_hierarchical_allreduce = 15 [ default = false ];
+ optional int32 hierarchical_allreduce_inter_nranks = 16 [ default = 1 ];
+ optional bool sync_batch_norm = 17 [ default = false ];
+ optional bool fuse_all_reduce_ops = 18 [ default = true ];
+ optional int32 fuse_grad_size_in_MB = 19 [ default = 32 ];
+ optional float fuse_grad_size_in_TFLOPS = 20 [ default = 50 ];
+ // optional bool enable_backward_optimizer_op_deps = 19 [ default = true ];
- // auto parallel
- optional bool auto = 401 [ default = false ];
+ optional RecomputeConfig recompute_configs = 101;
+ optional AMPConfig amp_configs = 102;
+ optional LocalSGDConfig localsgd_configs = 103;
+ optional GradientMergeConfig gradient_merge_configs = 104;
+ optional PipelineConfig pipeline_configs = 106;
+ optional AsyncConfig a_sync_configs = 107;
+ optional LarsConfig lars_configs = 108;
+ optional LambConfig lamb_configs = 109;
+ optional BuildStrategy build_strategy = 201;
+ optional ExecutionStrategy execution_strategy = 202;
}
message DistributedJobInfo {
diff --git a/paddle/fluid/framework/dlpack_tensor.cc b/paddle/fluid/framework/dlpack_tensor.cc
index 74e344cfebe36f0f9400d08a8b8e0527c4e5051e..f2421248e33f236b9fa861f22ce4848531cf1791 100644
--- a/paddle/fluid/framework/dlpack_tensor.cc
+++ b/paddle/fluid/framework/dlpack_tensor.cc
@@ -30,7 +30,10 @@ static ::DLDataType GetDLDataTypeCode() {
} else if (std::is_integral::value) {
dtype.code = kDLInt;
} else {
- PADDLE_THROW("Unsupported data type %s", typeid(T).name());
+ PADDLE_THROW(platform::errors::Unavailable(
+ "Unsupported data type (%s), only supports float16, float, unsigned "
+ "int and int.",
+ platform::demangle(typeid(T).name())));
}
dtype.bits = 8 * sizeof(T);
dtype.lanes = 1;
@@ -52,8 +55,9 @@ static DLDataType GetDLDataTypeFromTypeIndex(proto::VarType::Type type) {
static auto type_to_dtype_map = CreateDLDataTypeMap();
static auto type_to_dtype_map_end_it = type_to_dtype_map.end();
auto it = type_to_dtype_map.find(static_cast(type));
- PADDLE_ENFORCE(it != type_to_dtype_map_end_it, "Unsupported data type %d",
- type);
+ PADDLE_ENFORCE_NE(it, type_to_dtype_map_end_it,
+ platform::errors::InvalidArgument(
+ "Unsupported data type (%s).", DataTypeToString(type)));
return it->second;
#undef REG_DL_DATA_TYPE
}
@@ -73,7 +77,8 @@ struct DLContextVisitor : public boost::static_visitor<::DLContext> {
ctx.device_id = place.device;
return ctx;
#else
- PADDLE_THROW("platform::CUDAPlace is not supported in CPU only version");
+ PADDLE_THROW(platform::errors::Unavailable(
+ "platform::CUDAPlace is not supported in CPU only version."));
#endif
}
@@ -84,8 +89,8 @@ struct DLContextVisitor : public boost::static_visitor<::DLContext> {
ctx.device_id = 0;
return ctx;
#else
- PADDLE_THROW(
- "platform::CUDAPinnedPlace is not supported in CPU only version");
+ PADDLE_THROW(platform::errors::Unavailable(
+ "platform::CUDAPinnedPlace is not supported in CPU only version."));
#endif
}
};
@@ -136,7 +141,10 @@ DLPackTensor::DLPackTensor(const Tensor &tensor, LaneType lanes) {
// refer to cupy and cudf, the compact tensor first dim's strides need to be 1
// and second dim's strides need to be length of rows of cudf
// cudf now only support dim=2
- PADDLE_ENFORCE_LE(t_.ndim, 2, "cudf now only support dim=2.");
+ PADDLE_ENFORCE_LE(t_.ndim, 2, platform::errors::InvalidArgument(
+ "cudf now only supports dimension is 2, "
+ "but received dimension is %d.",
+ t_.ndim));
if (t_.ndim > 1)
t_.strides = new int64_t[2]{1, t_.shape[1]};
diff --git a/paddle/fluid/framework/downpour_worker.cc b/paddle/fluid/framework/downpour_worker.cc
index cbdfa00652abdedeb71b7961dc3ef1cabeca2f97..3f70835c9d312a652cd917ba53fb2f405ab401cc 100644
--- a/paddle/fluid/framework/downpour_worker.cc
+++ b/paddle/fluid/framework/downpour_worker.cc
@@ -556,9 +556,11 @@ void DownpourWorker::TrainFilesWithProfiler() {
continue;
}
PADDLE_ENFORCE_EQ(framework::TensorContainsInf(*tensor), false,
- "Tensor %s contains Inf", var_name);
+ platform::errors::InvalidArgument(
+ "Tensor %s contains Inf.", var_name));
PADDLE_ENFORCE_EQ(framework::TensorContainsNAN(*tensor), false,
- "Tensor %s contains NAN", var_name);
+ platform::errors::InvalidArgument(
+ "Tensor %s contains NAN.", var_name));
}
if (need_to_push_sparse_) {
@@ -829,9 +831,11 @@ void DownpourWorker::TrainFiles() {
continue;
}
PADDLE_ENFORCE_EQ(framework::TensorContainsInf(*tensor), false,
- "Tensor %s contains Inf", var_name);
+ platform::errors::InvalidArgument(
+ "Tensor %s contains Inf.", var_name));
PADDLE_ENFORCE_EQ(framework::TensorContainsNAN(*tensor), false,
- "Tensor %s contains NAN", var_name);
+ platform::errors::InvalidArgument(
+ "Tensor %s contains NAN.", var_name));
}
if (need_to_push_sparse_) {
diff --git a/paddle/fluid/framework/eigen.h b/paddle/fluid/framework/eigen.h
index 21adcb9948b20efe0169a9149b2afce1d485d12d..0e3edfb95cb9b37543ce84ba9a22227d2761734a 100644
--- a/paddle/fluid/framework/eigen.h
+++ b/paddle/fluid/framework/eigen.h
@@ -26,7 +26,11 @@ struct EigenDim {
using Type = Eigen::DSizes;
static Type From(const DDim& dims) {
- PADDLE_ENFORCE(arity(dims) == D, "D must match arity(DDim)");
+ PADDLE_ENFORCE_EQ(arity(dims), D,
+ platform::errors::InvalidArgument(
+ "Input dimension size should be equal to %d, but "
+ "received dimension size is %d.",
+ arity(dims), D));
Type ret;
for (int64_t d = 0; d < arity(dims); d++) {
ret[d] = dims[d];
@@ -69,8 +73,11 @@ struct EigenMatrix : public EigenTensor {
static typename EigenMatrix::Type Reshape(Tensor& tensor, // NOLINT
int num_col_dims) {
int rank = tensor.dims_.size();
- PADDLE_ENFORCE(num_col_dims > 0 && num_col_dims < rank,
- "`num_col_dims` must be between (0, rank_of_tensor).");
+ PADDLE_ENFORCE_EQ((num_col_dims > 0 && num_col_dims < rank), true,
+ platform::errors::InvalidArgument(
+ "Input dimension number(num_col_dims) must be "
+ "between 0 and %d, but received number is %d.",
+ rank, num_col_dims));
return EigenMatrix::From(tensor,
flatten_to_2d(tensor.dims(), num_col_dims));
}
@@ -78,8 +85,11 @@ struct EigenMatrix : public EigenTensor {
static typename EigenMatrix::ConstType Reshape(const Tensor& tensor,
int num_col_dims) {
int rank = tensor.dims_.size();
- PADDLE_ENFORCE(num_col_dims > 0 && num_col_dims < rank,
- "`num_col_dims` must be between (0, rank_of_tensor).");
+ PADDLE_ENFORCE_EQ((num_col_dims > 0 && num_col_dims < rank), true,
+ platform::errors::InvalidArgument(
+ "Input dimension number(num_col_dims) must be "
+ "between 0 and %d, but received number is %d.",
+ rank, num_col_dims));
return EigenMatrix::From(tensor,
flatten_to_2d(tensor.dims(), num_col_dims));
}
diff --git a/paddle/fluid/framework/executor.cc b/paddle/fluid/framework/executor.cc
index 68eca6e328da9510552f77760aea915c24292a49..8e2e1d38a66d1039519bab312f77bef6604d8ec1 100644
--- a/paddle/fluid/framework/executor.cc
+++ b/paddle/fluid/framework/executor.cc
@@ -37,9 +37,12 @@ limitations under the License. */
#include "paddle/fluid/operators/distributed/distributed.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
+#ifdef PADDLE_WITH_MKLDNN
+#include "paddle/fluid/platform/mkldnn_helper.h"
+#endif
DECLARE_bool(benchmark);
-DEFINE_bool(use_mkldnn, false, "Use MKLDNN to run");
+DECLARE_bool(use_mkldnn);
namespace paddle {
namespace framework {
@@ -83,14 +86,7 @@ Executor::~Executor() {
#ifdef PADDLE_WITH_MKLDNN
// Clear mkl-dnn cache,
// this is needed to have mkl-dnn unit tests working
- if (platform::is_cpu_place(place_)) {
- platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
- platform::MKLDNNDeviceContext* dev_ctx =
- (platform::MKLDNNDeviceContext*)pool.Get(place_);
- dev_ctx->ResetBlobMap();
- platform::MKLDNNDeviceContext::tls().set_cur_paddle_data_layout(
- paddle::framework::DataLayout::kNCHW);
- }
+ ClearMKLDNNCache(place_);
#endif
}
diff --git a/paddle/fluid/framework/executor_gc_helper.cc b/paddle/fluid/framework/executor_gc_helper.cc
index 1712d66cf4c99f0c01bf2ba2431bf41f457390db..706248229bc27e553fbc136116ab616f371eed5e 100644
--- a/paddle/fluid/framework/executor_gc_helper.cc
+++ b/paddle/fluid/framework/executor_gc_helper.cc
@@ -175,8 +175,9 @@ void DeleteUnusedTensors(
garbages.emplace_back(t.MoveMemoryHolder());
}
} else {
- PADDLE_THROW("Type %s of %s is not supported eager deletion",
- framework::ToTypeName(var->Type()), var_name);
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Type %s of variable %s is not supported eager deletion.",
+ framework::ToTypeName(var->Type()), var_name));
}
}
diff --git a/paddle/fluid/framework/garbage_collector.cc b/paddle/fluid/framework/garbage_collector.cc
index 08c3e6d7f592d1791739ac442ef186f374eab716..ac892443de36cf6d37d56da761fb3d60628a5e4a 100644
--- a/paddle/fluid/framework/garbage_collector.cc
+++ b/paddle/fluid/framework/garbage_collector.cc
@@ -79,15 +79,15 @@ StreamGarbageCollector::StreamGarbageCollector(const platform::CUDAPlace &place,
size_t max_memory_size)
: GarbageCollector(place, max_memory_size) {
platform::CUDADeviceGuard guard(place.device);
- PADDLE_ENFORCE(cudaStreamCreate(&stream_));
+ PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamCreate(&stream_));
callback_manager_.reset(new platform::StreamCallbackManager(stream_));
}
StreamGarbageCollector::~StreamGarbageCollector() {
auto place = BOOST_GET_CONST(platform::CUDAPlace, this->dev_ctx_->GetPlace());
platform::CUDADeviceGuard guard(place.device);
- PADDLE_ENFORCE(cudaStreamSynchronize(stream_));
- PADDLE_ENFORCE(cudaStreamDestroy(stream_));
+ PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamSynchronize(stream_));
+ PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamDestroy(stream_));
}
cudaStream_t StreamGarbageCollector::stream() const { return stream_; }
diff --git a/paddle/fluid/framework/grad_op_desc_maker.h b/paddle/fluid/framework/grad_op_desc_maker.h
index 7a3ba0863cf20d69a37d515dd17089c9f46cca26..27575878f2eedb6f3e30e2370a5717c313d58ff9 100644
--- a/paddle/fluid/framework/grad_op_desc_maker.h
+++ b/paddle/fluid/framework/grad_op_desc_maker.h
@@ -96,14 +96,14 @@ class GradOpDescMakerBase {
if (!drop_empty_grad) {
return ret_val;
}
- PADDLE_ENFORCE_LE(var_names.size(), 1UL,
- "BUG from operator developer:"
- " for input argument with a list of variables, "
- " drop_empty_grad is not allowed because it makes"
- " the correspondence bewteen a variable and its gradient"
- " ambiguous."
- " Op type %s",
- fwd_op_.Type());
+ PADDLE_ENFORCE_LE(
+ var_names.size(), 1UL,
+ platform::errors::Unavailable(
+ "BUG from operator developer:"
+ " for input argument with a list of variables, "
+ " drop_empty_grad is not allowed because it makes"
+ " the correspondence bewteen a variable and its gradient"
+ " ambiguous."));
std::vector dropped_ret_val;
dropped_ret_val.reserve(ret_val.size());
@@ -157,7 +157,8 @@ class GradOpDescMakerBase {
const Attribute& GetAttr(const std::string& name) const {
auto& map = fwd_op_.GetAttrMap();
auto it = map.find(name);
- PADDLE_ENFORCE(it != map.end(), "Cannot find attribute %s", name);
+ PADDLE_ENFORCE_NE(it, map.end(), platform::errors::NotFound(
+ "Cannot find attribute (%s).", name));
return it->second;
}
diff --git a/paddle/fluid/framework/hogwild_worker.cc b/paddle/fluid/framework/hogwild_worker.cc
index c51f091c54a98924a239f0e1ae717278863f7d6d..1117d676a5ece5b97a50b6290781f3bbc853cf7a 100644
--- a/paddle/fluid/framework/hogwild_worker.cc
+++ b/paddle/fluid/framework/hogwild_worker.cc
@@ -53,7 +53,9 @@ void HogwildWorker::CreateThreadScope(const ProgramDesc &program) {
auto &block = program.Block(0);
PADDLE_ENFORCE_NOT_NULL(
- root_scope_, "root_scope should be set before creating thread scope");
+ root_scope_,
+ platform::errors::NotFound(
+ "Root scope should be set before creating thread scope."));
thread_scope_ = &root_scope_->NewScope();
diff --git a/paddle/fluid/framework/io/shell.h b/paddle/fluid/framework/io/shell.h
index 5b3e9a4df1d11b957d656181844f17a06574556f..dc486275d6f58eaa7a360b8f17830acd664b11c7 100644
--- a/paddle/fluid/framework/io/shell.h
+++ b/paddle/fluid/framework/io/shell.h
@@ -17,6 +17,9 @@
#include
#include
#ifdef _WIN32
+#ifndef NOMINMAX
+#define NOMINMAX // msvc max/min macro conflict with std::min/max
+#endif
#include
#else
#include
diff --git a/paddle/fluid/framework/ir/CMakeLists.txt b/paddle/fluid/framework/ir/CMakeLists.txt
index 81bd8a4adf4c3fe584416b0ea834221e739ab4d4..8787aa8a94a44c2c36868fea4b88ede5f91b19f4 100644
--- a/paddle/fluid/framework/ir/CMakeLists.txt
+++ b/paddle/fluid/framework/ir/CMakeLists.txt
@@ -4,7 +4,7 @@ file(WRITE ${pass_file} "// Generated by the paddle/fluid/framework/ir/CMakeList
file(APPEND ${pass_file} "\#pragma once\n")
file(APPEND ${pass_file} "\#include \"paddle/fluid/framework/ir/pass.h\"\n")
-copy_if_different(${pass_file} ${pass_file_final} extern_glog)
+copy_if_different(${pass_file} ${pass_file_final})
add_subdirectory(fuse_optimizer_ops_pass)
add_subdirectory(memory_optimize_pass)
diff --git a/paddle/fluid/framework/ir/fc_gru_fuse_pass.cc b/paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
index 08dd0302b4b49e6b434beb0141abd974d2c7888d..a2185cdc5593cc36ed6ceda839fb13c28b45600c 100644
--- a/paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
@@ -26,15 +26,15 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
GraphPatternDetector gpd;
auto* pattern = gpd.mutable_pattern();
- // Create pattern.
- patterns::FC fc_pattern(pattern, name_scope);
- patterns::GRU gru_pattern(pattern, name_scope);
-
PDNode* x =
pattern->NewNode(patterns::UniqueKey("x"))->assert_var_not_persistable();
+ // Create pattern.
+ patterns::FC fc_pattern(pattern, name_scope);
auto* fc_out = fc_pattern(x, with_fc_bias, /* with_relu */ false);
fc_out->AsIntermediate(); // fc_out is a tmp var, will be removed after fuse.
+
+ patterns::GRU gru_pattern(pattern, name_scope);
gru_pattern(fc_out);
// Create New OpDesc
@@ -48,17 +48,18 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
SET_IN(X, x);
SET_IN(WeightX, weight_x);
SET_IN(WeightH, weight_h);
- if (with_fc_bias) {
- op_desc.SetInput("Bias", {NEW_NAME(bias) + bias->Name()});
- } else {
- SET_IN(Bias, bias);
- }
+ SET_IN(Bias, bias);
#undef SET_IN
+ // TODO(grygielski): Add H0 to the pass
op_desc.SetInput("H0", {});
op_desc.SetOutput("Hidden", {hidden->Name()});
op_desc.SetAttr("is_reverse", gru->Op()->GetAttr("is_reverse"));
+ op_desc.SetAttr("origin_mode",
+ gru->Op()->GetAttrIfExists("origin_mode"));
// TODO(TJ): This should be a option for infer
op_desc.SetAttr("use_seq", true);
+ op_desc.SetAttr("activation", gru->Op()->GetAttr("activation"));
+ op_desc.SetAttr("gate_activation", gru->Op()->GetAttr("gate_activation"));
#define SET_IMTERMEDIATE_OUT(key) op_desc.SetOutput(#key, {NEW_NAME(key)})
SET_IMTERMEDIATE_OUT(ReorderedH0);
@@ -68,35 +69,30 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
#undef SET_IMTERMEDIATE_OUT
auto* op = graph->CreateOpNode(&op_desc);
- PADDLE_ENFORCE_EQ(graph->Has(kParamScopeAttr), true,
- platform::errors::InvalidArgument(
- "Graph have no attr kParamScopeAttr."));
- auto& scope = graph->Get(kParamScopeAttr);
if (with_fc_bias) {
- // Fusion GRU bias = fcbias + grubias
- auto* fusion_bias_var = scope.Var(NEW_NAME(bias) + bias->Name());
- auto* out_bias_tensor =
- fusion_bias_var->GetMutable();
- PADDLE_ENFORCE_NOT_NULL(
- fusion_bias_var,
- platform::errors::InvalidArgument(
- "Fusion bias variable's pointer cannot be nullptr."));
- auto* gru_bias_var = scope.FindVar(bias->Name());
- auto* fc_bias_var = scope.FindVar(fc_bias->Name());
- PADDLE_ENFORCE_NOT_NULL(gru_bias_var,
- platform::errors::InvalidArgument(
- "Gru bias var ptr cannot be nullptr."));
- PADDLE_ENFORCE_NOT_NULL(fc_bias_var,
- platform::errors::InvalidArgument(
- "Fc bias var ptr cannot be nullptr."));
- const auto& gru_bias_tenosr = gru_bias_var->Get();
- const auto& fc_bias_tensor = fc_bias_var->Get();
- // new bias = fc bias + gru bias
- out_bias_tensor->Resize(gru_bias_tenosr.dims());
- auto* data = out_bias_tensor->mutable_data(platform::CPUPlace());
- for (int i = 0; i < out_bias_tensor->numel(); i++) {
- data[i] =
- fc_bias_tensor.data()[i] + gru_bias_tenosr.data()[i];
+ auto* gru_bias_var = scope->FindVar(bias->Name());
+ auto* fc_bias_var = scope->FindVar(fc_bias->Name());
+ PADDLE_ENFORCE_NE(
+ gru_bias_var, nullptr,
+ platform::errors::NotFound("GRU bias var has not been found."));
+ PADDLE_ENFORCE_NE(
+ fc_bias_var, nullptr,
+ platform::errors::NotFound("FC bias var has not been found."));
+
+ auto* gru_bias_tensor = gru_bias_var->GetMutable();
+ auto* fc_bias_tensor = fc_bias_var->GetMutable();
+ PADDLE_ENFORCE_EQ(
+ gru_bias_tensor->numel(), fc_bias_tensor->numel(),
+ platform::errors::PreconditionNotMet(
+ "GRU and FC biases have to have equal number of elements."));
+
+ auto gru_bias_data =
+ gru_bias_tensor->mutable_data(platform::CPUPlace());
+ auto* fc_bias_data = fc_bias_tensor->data();
+
+ // Recompute GRU bias
+ for (int i = 0; i < gru_bias_tensor->numel(); ++i) {
+ gru_bias_data[i] += fc_bias_data[i];
}
}
#undef GET_NODE
@@ -117,7 +113,7 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
IR_NODE_LINK_TO(x, op);
IR_NODE_LINK_TO(weight_x, op);
IR_NODE_LINK_TO(weight_h, op);
- IR_NODE_LINK_TO(bias, op); // actually should link to new bias if have
+ IR_NODE_LINK_TO(bias, op);
IR_NODE_LINK_TO(op, hidden);
// h0?
return op;
diff --git a/paddle/fluid/framework/ir/fuse_bn_act_pass.cc b/paddle/fluid/framework/ir/fuse_bn_act_pass.cc
index 7d6ef5b9023b017def332424b58e4a9629496992..54c05046a2c2f2f56c20a32b8ca32578abe7af31 100644
--- a/paddle/fluid/framework/ir/fuse_bn_act_pass.cc
+++ b/paddle/fluid/framework/ir/fuse_bn_act_pass.cc
@@ -320,7 +320,7 @@ std::vector FuseBatchNormActPass::ReplaceNode(
return node;
});
PADDLE_ENFORCE_EQ(has_replaced, true,
- platform::errors::NotFound("Not find %s in the node list.",
+ platform::errors::NotFound("Not found %s in the node list.",
cur_node->Name()));
return new_list;
}
diff --git a/paddle/fluid/framework/ir/fuse_elewise_add_act_pass.cc b/paddle/fluid/framework/ir/fuse_elewise_add_act_pass.cc
index 5c2c574fd681a642b950a9e6ddfa4166281f2234..b559d66fe74561e9f750dfd3da2a640ca1f74dfc 100644
--- a/paddle/fluid/framework/ir/fuse_elewise_add_act_pass.cc
+++ b/paddle/fluid/framework/ir/fuse_elewise_add_act_pass.cc
@@ -42,7 +42,8 @@ void FuseElewiseAddActPass::ApplyImpl(ir::Graph *graph) const {
// ele_add(x, act(y))
ir::Graph *FuseElewiseAddActPass::FuseElewiseAddAct(
ir::Graph *graph, const std::unordered_set &act_types) const {
- PADDLE_ENFORCE(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
FusePassBase::Init("elewise_add_act", graph);
GraphPatternDetector gpd;
@@ -93,7 +94,8 @@ ir::Graph *FuseElewiseAddActPass::FuseElewiseAddAct(
// act(ele_add(x,y))
ir::Graph *FuseElewiseAddActPass::FuseActElewiseAdd(
ir::Graph *graph, const std::unordered_set &act_types) const {
- PADDLE_ENFORCE(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
FusePassBase::Init("act_elewise_add", graph);
GraphPatternDetector gpd;
@@ -145,7 +147,8 @@ ir::Graph *FuseElewiseAddActPass::FuseActElewiseAdd(
// ele_add_grad: in["Y", "Out@GRAD"], out["X@GRAD", "Y@GRAD"]
ir::Graph *FuseElewiseAddActPass::FuseElewiseAddActInplaceGrad(
ir::Graph *graph, const std::unordered_set &act_types) const {
- PADDLE_ENFORCE(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
FusePassBase::Init("elewise_add_act_grad", graph);
GraphPatternDetector gpd;
@@ -252,10 +255,11 @@ void FuseElewiseAddActPass::RemoveIntermediateOut(Graph *graph) const {
bool save_intermediate_out = BOOST_GET_CONST(
bool, cur_node->Op()->GetAttr("save_intermediate_out"));
auto intermediate_out_args = cur_node->Op()->Output("IntermediateOut");
- PADDLE_ENFORCE(
- save_intermediate_out && !intermediate_out_args.empty(),
- "The %s should save the intermediate_out in the fusing stage.",
- cur_node->Name());
+ PADDLE_ENFORCE_EQ(
+ (save_intermediate_out && !intermediate_out_args.empty()), true,
+ platform::errors::InvalidArgument(
+ "The %s should save the intermediate out in the fusing stage.",
+ cur_node->Name()));
// If the intermediate_out's output is empty, it should be removed.
auto cur_node_outputs = cur_node->outputs;
@@ -271,10 +275,11 @@ void FuseElewiseAddActPass::RemoveIntermediateOut(Graph *graph) const {
} else if (cur_node->Name() == "fused_elemwise_activation_grad") {
auto intermediate_out_grad_args =
cur_node->Op()->Output(GradVarName("IntermediateOut"));
- PADDLE_ENFORCE(
- !intermediate_out_grad_args.empty(),
- "The %s should save the intermediate_out in the fusing stage.",
- cur_node->Name());
+ PADDLE_ENFORCE_EQ(
+ intermediate_out_grad_args.empty(), false,
+ platform::errors::InvalidArgument(
+ "The %s should save the intermediate out in the fusing stage.",
+ cur_node->Name()));
auto cur_node_outputs = cur_node->outputs;
// If the intermediate_out_g's output is empty, it should be removed.
for (auto &out : cur_node_outputs) {
@@ -312,7 +317,11 @@ void FuseElewiseAddActPass::ReLinkNodes(Graph *graph,
nodes2delete.emplace(out);
}
} else {
- PADDLE_ENFORCE(out == intermediate_out);
+ PADDLE_ENFORCE_EQ(
+ out, intermediate_out,
+ platform::errors::InvalidArgument(
+ "Output of op(%s) must be %s, but not %s.", op_1->Name(),
+ intermediate_out->Name(), out->Name()));
IR_OP_VAR_LINK(fused_op, out);
}
}
@@ -347,8 +356,9 @@ std::vector FuseElewiseAddActPass::ReplaceNode(
}
return node;
});
- PADDLE_ENFORCE(has_replaced, "Not find %s in the node list.",
- cur_node->Name());
+ PADDLE_ENFORCE_EQ(has_replaced, true,
+ platform::errors::NotFound("Not found %s in the node list.",
+ cur_node->Name()));
return new_list;
}
diff --git a/paddle/fluid/framework/ir/fuse_pass_base.cc b/paddle/fluid/framework/ir/fuse_pass_base.cc
index c7bf53f3d61194a770f345121f454b46980c95b8..e6fb1302e275fa2635542baf824c5e3333c2f5c8 100644
--- a/paddle/fluid/framework/ir/fuse_pass_base.cc
+++ b/paddle/fluid/framework/ir/fuse_pass_base.cc
@@ -25,14 +25,19 @@ void FusePassBase::Init(const std::string& repr, Graph* graph) const {
}
Scope* FusePassBase::param_scope() const {
- PADDLE_ENFORCE(graph_->Has(kParamScopeAttr));
+ PADDLE_ENFORCE_EQ(graph_->Has(kParamScopeAttr), true,
+ platform::errors::InvalidArgument(
+ "Graph must have kParamScopeAttr attribute."));
auto& scope = graph_->Get(kParamScopeAttr);
return &scope;
}
void FusePassBase::AddStatis(int count_of_fused) const {
- PADDLE_ENFORCE(graph_);
- PADDLE_ENFORCE(!repr_.empty());
+ PADDLE_ENFORCE_NOT_NULL(
+ graph_, platform::errors::InvalidArgument("Graph cannot be nullptr."));
+ PADDLE_ENFORCE_EQ(repr_.empty(), false,
+ platform::errors::InvalidArgument(
+ "Fuse pass must be initialized with a name."));
if (!graph_->Has(kFuseStatisAttr)) {
graph_->Set(kFuseStatisAttr, new std::unordered_map);
}
diff --git a/paddle/fluid/framework/ir/fuse_relu_depthwise_conv_pass.cc b/paddle/fluid/framework/ir/fuse_relu_depthwise_conv_pass.cc
index c4e6b6e6a52ec77c85c7c6162c4cbd006e47c502..56ca98b566070ce5ed49a96ec9aedc3276ae0499 100644
--- a/paddle/fluid/framework/ir/fuse_relu_depthwise_conv_pass.cc
+++ b/paddle/fluid/framework/ir/fuse_relu_depthwise_conv_pass.cc
@@ -31,7 +31,8 @@ void FuseReluDepthwiseConvPass::ApplyImpl(ir::Graph *graph) const {
ir::Graph *FuseReluDepthwiseConvPass::FuseReluDepthwiseConv(
ir::Graph *graph, bool only_forward) const {
- PADDLE_ENFORCE(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
if (only_forward)
FusePassBase::Init("relu_depthwise_conv_only_forward", graph);
else
@@ -110,23 +111,45 @@ ir::Graph *FuseReluDepthwiseConvPass::FuseReluDepthwiseConv(
xg_var = subgraph.at(xg)->Var();
}
- PADDLE_ENFORCE_EQ(layer_op->Input("Input").size(), 1UL);
- PADDLE_ENFORCE_EQ(layer_op->Input("Input")[0], y_var->Name());
+ PADDLE_ENFORCE_EQ(layer_op->Input("Input").size(), 1UL,
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input size(%d) must be 1.",
+ layer_op->Type(), layer_op->Input("Input").size()));
+ PADDLE_ENFORCE_EQ(
+ layer_op->Input("Input")[0], y_var->Name(),
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input name(%s) must be %s.", layer_op->Type(),
+ layer_op->Input("Input")[0], y_var->Name()));
layer_op->SetInput("Input", {x_var->Name()});
subgraph.at(layer)->inputs.push_back(subgraph.at(x));
subgraph.at(x)->outputs.push_back(subgraph.at(layer));
VLOG(4) << "replace " << y_var->Name() << " -> " << x_var->Name();
if (!only_forward) {
- PADDLE_ENFORCE_EQ(layer_g_op->Input("Input").size(), 1UL);
- PADDLE_ENFORCE_EQ(layer_g_op->Input("Input")[0], y_var->Name());
+ PADDLE_ENFORCE_EQ(
+ layer_g_op->Input("Input").size(), 1UL,
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input size(%d) must be 1.", layer_g_op->Type(),
+ layer_g_op->Input("Input").size()));
+ PADDLE_ENFORCE_EQ(
+ layer_g_op->Input("Input")[0], y_var->Name(),
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input name(%s) must be %s.", layer_g_op->Type(),
+ layer_g_op->Input("Input")[0], y_var->Name()));
layer_g_op->SetInput("Input", {x_var->Name()});
subgraph.at(layer_g)->inputs.push_back(subgraph.at(x));
subgraph.at(x)->outputs.push_back(subgraph.at(layer_g));
- PADDLE_ENFORCE_EQ(layer_g_op->Output(GradVarName("Input")).size(), 1UL);
- PADDLE_ENFORCE_EQ(layer_g_op->Output(GradVarName("Input"))[0],
- yg_var->Name());
+ PADDLE_ENFORCE_EQ(
+ layer_g_op->Output(GradVarName("Input")).size(), 1UL,
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input size(%d) must be 1.", layer_g_op->Type(),
+ layer_g_op->Output(GradVarName("Input")).size()));
+ PADDLE_ENFORCE_EQ(
+ layer_g_op->Output(GradVarName("Input"))[0], yg_var->Name(),
+ platform::errors::InvalidArgument(
+ "Op(%s)'s input name(%s) must be %s.", layer_g_op->Type(),
+ layer_g_op->Output(GradVarName("Input"))[0], yg_var->Name()));
layer_g_op->SetOutput(GradVarName("Input"), {xg_var->Name()});
subgraph.at(layer_g)->outputs.push_back(subgraph.at(xg));
subgraph.at(xg)->inputs.push_back(subgraph.at(layer_g));
diff --git a/paddle/fluid/framework/ir/graph_helper.cc b/paddle/fluid/framework/ir/graph_helper.cc
index b397216f0b4d15b0e71a3c3c7814439d75d59aee..ff0e0e65a297fd91834c85cb397bb98ba853f77d 100644
--- a/paddle/fluid/framework/ir/graph_helper.cc
+++ b/paddle/fluid/framework/ir/graph_helper.cc
@@ -136,7 +136,9 @@ bool FindCircleSubGraph(const Graph &graph,
std::vector TopologySortOperations(const Graph &graph) {
std::map, ir::NodeComp>
adj_list = BuildOperationAdjList(graph);
- PADDLE_ENFORCE(!HasCircleInternal(adj_list, nullptr));
+ PADDLE_ENFORCE_EQ(HasCircleInternal(adj_list, nullptr), false,
+ platform::errors::InvalidArgument(
+ "Generated graph shouldn't contain cycle."));
std::unordered_set visited;
std::vector ret;
for (auto adj : adj_list) {
@@ -161,7 +163,11 @@ BuildOperationAdjList(const Graph &graph) {
}
for (auto &var : n->inputs) {
for (auto &adj_n : var->inputs) {
- PADDLE_ENFORCE(adj_n->NodeType() == ir::Node::Type::kOperation);
+ PADDLE_ENFORCE_EQ(
+ adj_n->NodeType(), ir::Node::Type::kOperation,
+ platform::errors::InvalidArgument(
+ "Node(%s)'s type(%d) must be kOperation type.", adj_n->Name(),
+ static_cast(adj_n->NodeType())));
VLOG(4) << "adj " << adj_n->Name() << reinterpret_cast(adj_n)
<< " -> " << n->Name() << reinterpret_cast(n)
<< " via " << var->Name() << reinterpret_cast(var);
@@ -184,7 +190,11 @@ std::map> BuildOperationOutAdjList(
}
for (auto &var : n->outputs) {
for (auto &adj_n : var->outputs) {
- PADDLE_ENFORCE(adj_n->NodeType() == ir::Node::Type::kOperation);
+ PADDLE_ENFORCE_EQ(
+ adj_n->NodeType(), ir::Node::Type::kOperation,
+ platform::errors::InvalidArgument(
+ "Node(%s)'s type(%d) must be kOperation type.", adj_n->Name(),
+ static_cast(adj_n->NodeType())));
VLOG(40) << "adj " << adj_n->Name() << reinterpret_cast(adj_n)
<< " -> " << n->Name() << reinterpret_cast(n)
<< " via " << var->Name() << reinterpret_cast(var);
@@ -359,7 +369,10 @@ size_t GraphNum(const Graph &graph) {
}
std::unique_ptr fout(
new std::ofstream(FLAGS_print_sub_graph_dir));
- PADDLE_ENFORCE(fout->good());
+ PADDLE_ENFORCE_EQ(fout->good(), true,
+ platform::errors::Unavailable(
+ "Can not open file %s for printing the graph.",
+ FLAGS_print_sub_graph_dir));
*fout << out.str();
}
}
diff --git a/paddle/fluid/framework/ir/graph_traits.cc b/paddle/fluid/framework/ir/graph_traits.cc
index abcba32a6492b114193cfab6756ff87247956f6c..4b403c46260c6129451809f276aac67ccc17c4d4 100644
--- a/paddle/fluid/framework/ir/graph_traits.cc
+++ b/paddle/fluid/framework/ir/graph_traits.cc
@@ -37,12 +37,14 @@ NodesDFSIterator::NodesDFSIterator(const NodesDFSIterator &other)
: stack_(other.stack_), visited_(other.visited_) {}
Node &NodesDFSIterator::operator*() {
- PADDLE_ENFORCE(!stack_.empty());
+ PADDLE_ENFORCE_EQ(stack_.empty(), false, platform::errors::OutOfRange(
+ "The iterator exceeds range."));
return *stack_.top();
}
NodesDFSIterator &NodesDFSIterator::operator++() {
- PADDLE_ENFORCE(!stack_.empty(), "the iterator exceeds range");
+ PADDLE_ENFORCE_EQ(stack_.empty(), false, platform::errors::OutOfRange(
+ "The iterator exceeds range."));
visited_.insert(stack_.top());
auto *cur = stack_.top();
stack_.pop();
@@ -73,11 +75,18 @@ inline bool CheckNodeIndegreeEquals(const Node &node, size_t n) {
}
NodesTSIterator::NodesTSIterator(const std::vector &source) {
- PADDLE_ENFORCE(!source.empty(),
- "Start points of topological sorting should not be empty!");
+ PADDLE_ENFORCE_EQ(
+ source.empty(), false,
+ platform::errors::InvalidArgument(
+ "Start points of topological sorting should not be empty!"));
// CHECK all the inputs' in-degree is 0
for (auto *node : source) {
- PADDLE_ENFORCE(CheckNodeIndegreeEquals(*node, 0));
+ PADDLE_ENFORCE_EQ(
+ CheckNodeIndegreeEquals(*node, 0), true,
+ platform::errors::InvalidArgument(
+ "In start points of topological sorting, the indegree of each "
+ "point should be 0. Node(%s)'s indegree is not 0.",
+ node->Name()));
}
std::set to_visit{source.begin(), source.end()};
@@ -106,7 +115,11 @@ NodesTSIterator::NodesTSIterator(const NodesTSIterator &other)
: sorted_(other.sorted_), cursor_(other.cursor_) {}
Node &NodesTSIterator::operator*() {
- PADDLE_ENFORCE_LT(cursor_, sorted_.size());
+ PADDLE_ENFORCE_LT(
+ cursor_, sorted_.size(),
+ platform::errors::OutOfRange(
+ "The iterator exceeds range. Container size is %d, but index is %d.",
+ sorted_.size(), cursor_));
return *sorted_[cursor_];
}
@@ -128,7 +141,11 @@ bool NodesTSIterator::operator==(const NodesTSIterator &other) {
}
Node *NodesTSIterator::operator->() {
- PADDLE_ENFORCE_LT(cursor_, sorted_.size());
+ PADDLE_ENFORCE_LT(
+ cursor_, sorted_.size(),
+ platform::errors::OutOfRange(
+ "The iterator exceeds range. Container size is %d, but index is %d.",
+ sorted_.size(), cursor_));
return sorted_[cursor_];
}
diff --git a/paddle/fluid/framework/ir/graph_traits.h b/paddle/fluid/framework/ir/graph_traits.h
index f6772f9a37567c83c49bd44d551481edda1a74ae..bb4212bcd33d77cfe1c091b18387e18c4c3e5fa7 100644
--- a/paddle/fluid/framework/ir/graph_traits.h
+++ b/paddle/fluid/framework/ir/graph_traits.h
@@ -15,6 +15,8 @@
#pragma once
#include
+#include
+#include
#include
#include "paddle/fluid/framework/ir/graph.h"
@@ -66,7 +68,7 @@ struct NodesDFSIterator
struct NodesTSIterator
: public std::iterator {
NodesTSIterator() = default;
- NodesTSIterator(const std::vector &source);
+ explicit NodesTSIterator(const std::vector &source);
NodesTSIterator(NodesTSIterator &&other)
: sorted_(std::move(other.sorted_)), cursor_(other.cursor_) {
other.cursor_ = 0;
@@ -104,7 +106,10 @@ struct GraphTraits {
static iterator_range TS(const Graph &g) {
auto start_points = ExtractStartPoints(g);
- PADDLE_ENFORCE(!start_points.empty());
+ PADDLE_ENFORCE_EQ(
+ start_points.empty(), false,
+ platform::errors::InvalidArgument(
+ "Start points of topological sorting should not be empty!"));
NodesTSIterator x(start_points);
return iterator_range(NodesTSIterator(start_points),
NodesTSIterator());
diff --git a/paddle/fluid/framework/ir/graph_viz_pass.cc b/paddle/fluid/framework/ir/graph_viz_pass.cc
index 7f4519ad9919d7ad2a13c501e07b7ec92bd1eee1..64f5376a784c29eccadcfcf3021447e4655910c6 100644
--- a/paddle/fluid/framework/ir/graph_viz_pass.cc
+++ b/paddle/fluid/framework/ir/graph_viz_pass.cc
@@ -42,7 +42,10 @@ void GraphVizPass::ApplyImpl(ir::Graph* graph) const {
const std::string& graph_viz_path = Get(kGraphvizPath);
VLOG(3) << "draw IR graph viz to " << graph_viz_path;
std::unique_ptr fout(new std::ofstream(graph_viz_path));
- PADDLE_ENFORCE(fout->good());
+ PADDLE_ENFORCE_EQ(
+ fout->good(), true,
+ platform::errors::Unavailable(
+ "Can not open file %s for printing the graph.", graph_viz_path));
std::ostream& sout = *fout;
std::unordered_map node2dot;
diff --git a/paddle/fluid/framework/ir/identity_scale_op_clean_pass.cc b/paddle/fluid/framework/ir/identity_scale_op_clean_pass.cc
index a39901e63bf65f7c314595a5fb2cc31d00959bd5..c8dfa02f469a351a8d3495bf19238a723029bb4b 100644
--- a/paddle/fluid/framework/ir/identity_scale_op_clean_pass.cc
+++ b/paddle/fluid/framework/ir/identity_scale_op_clean_pass.cc
@@ -64,7 +64,11 @@ void IdentityScaleOpCleanPass::ApplyImpl(ir::Graph* graph) const {
for (auto& parameter : *pre_op_desc->Proto()->mutable_outputs()) {
auto* arguments = parameter.mutable_arguments();
auto it = std::find(arguments->begin(), arguments->end(), scale_in_name);
- PADDLE_ENFORCE(it != arguments->end());
+ PADDLE_ENFORCE_NE(
+ it, arguments->end(),
+ platform::errors::NotFound(
+ "Can not find input variable(%s) from scale op(%s).",
+ scale_in_name, pre_op_desc->Type()));
*it = scale_out_name;
}
diff --git a/paddle/fluid/framework/ir/lock_free_optimize_pass.cc b/paddle/fluid/framework/ir/lock_free_optimize_pass.cc
index a0cb7e93306d25276af415111faf441f2b43b614..864a0379988fabcb7006b6820fb80276dce6526d 100644
--- a/paddle/fluid/framework/ir/lock_free_optimize_pass.cc
+++ b/paddle/fluid/framework/ir/lock_free_optimize_pass.cc
@@ -33,7 +33,8 @@ const char kSumGradOpName[] = "sum";
const char kOptimizerType[] = "sgd";
void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const {
- PADDLE_ENFORCE(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
// We could collect all weights' name from SGD, where
// W1 <- SGD(W0, Grad0)
@@ -41,7 +42,10 @@ void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const {
for (auto* node : graph->Nodes()) {
if (IsOpNamed(node, kOptimizerType)) {
auto& param_out_vars = node->Op()->Output("ParamOut");
- PADDLE_ENFORCE(param_out_vars.size() == 1u);
+ PADDLE_ENFORCE_EQ(
+ param_out_vars.size(), 1u,
+ platform::errors::InvalidArgument(
+ "In op(%s), find output(ParamOut) failed.", node->Name()));
weight_var_set.insert(param_out_vars[0]);
}
}
@@ -95,12 +99,19 @@ void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const {
VLOG(3) << "Found forward_op " << forward_op->Name();
- PADDLE_ENFORCE(forward_op);
+ PADDLE_ENFORCE_NOT_NULL(
+ forward_op, platform::errors::NotFound(
+ "Can not find forward op for backword op(%s).",
+ backward_op->Name()));
Node* new_optimizer_node = CreateNewSGDNode(
graph, forward_op, backward_op, node, opt_node);
- PADDLE_ENFORCE(new_optimizer_node);
+ PADDLE_ENFORCE_NOT_NULL(
+ new_optimizer_node,
+ platform::errors::InvalidArgument(
+ "Create new SGD node failed, backward op is %s.",
+ backward_op->Name()));
}
}
}
@@ -144,11 +155,21 @@ void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const {
ir::Node* LockFreeOptimizePass::CreateNewSGDNode(
ir::Graph* graph, ir::Node* forward_node, ir::Node* backward_node,
ir::Node* grad_sum_node, ir::Node* optimize_node) const {
- PADDLE_ENFORCE(graph);
- PADDLE_ENFORCE(forward_node);
- PADDLE_ENFORCE(backward_node);
- PADDLE_ENFORCE(grad_sum_node);
- PADDLE_ENFORCE(optimize_node);
+ PADDLE_ENFORCE_NOT_NULL(graph,
+ platform::errors::InvalidArgument(
+ "Input argument graph cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ forward_node, platform::errors::InvalidArgument(
+ "Input argument forward_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ backward_node, platform::errors::InvalidArgument(
+ "Input argument backward_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ grad_sum_node, platform::errors::InvalidArgument(
+ "Input argument grad_sum_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ optimize_node, platform::errors::InvalidArgument(
+ "Input argument optimize_node cannot be nullptr."));
// find the grad var node between the grad sum node and backward_node
std::vector grad_vars =
@@ -159,7 +180,8 @@ ir::Node* LockFreeOptimizePass::CreateNewSGDNode(
grad_node = node;
}
}
- PADDLE_ENFORCE(grad_node);
+ PADDLE_ENFORCE_NOT_NULL(grad_node, platform::errors::NotFound(
+ "Can not find control dep variable."));
// create a new SGD node
OpDesc* old_desc = optimize_node->Op();
@@ -212,8 +234,14 @@ ir::Node* LockFreeOptimizePass::CreateNewSGDNode(
}
// SGD must have only one param and LR in
- PADDLE_ENFORCE(old_desc->Input("LearningRate").size() == 1u);
- PADDLE_ENFORCE(old_desc->Input("Param").size() == 1u);
+ PADDLE_ENFORCE_EQ(
+ old_desc->Input("LearningRate").size(), 1u,
+ platform::errors::InvalidArgument(
+ "In op(%s), find input(LearningRate) failed.", old_desc->Type()));
+ PADDLE_ENFORCE_EQ(
+ old_desc->Input("Param").size(), 1u,
+ platform::errors::InvalidArgument("In op(%s), find input(Param) failed.",
+ old_desc->Type()));
// LR and weight nodes should be copied
for (Node* upstream_node : optimize_node->inputs) {
@@ -245,9 +273,17 @@ std::vector LockFreeOptimizePass::FindConnectedNode(
void LockFreeOptimizePass::ReplaceUpstreamNode(
ir::Node* upstream_node, ir::Node* old_optimizer_node,
ir::Node* new_optimizer_node) const {
- PADDLE_ENFORCE(upstream_node);
- PADDLE_ENFORCE(old_optimizer_node);
- PADDLE_ENFORCE(new_optimizer_node);
+ PADDLE_ENFORCE_NOT_NULL(
+ upstream_node, platform::errors::InvalidArgument(
+ "Input argument upstream_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ old_optimizer_node,
+ platform::errors::InvalidArgument(
+ "Input argument old_optimizer_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ new_optimizer_node,
+ platform::errors::InvalidArgument(
+ "Input argument new_optimizer_node cannot be nullptr."));
// Remove the old_optimizer_node from upstream_node's outputs vector
auto& output_node_vec = upstream_node->outputs;
@@ -268,8 +304,14 @@ void LockFreeOptimizePass::ReplaceUpstreamNode(
void LockFreeOptimizePass::ReplaceAllDownstreamNode(
ir::Node* old_optimizer_node, ir::Node* new_optimizer_node) const {
- PADDLE_ENFORCE(old_optimizer_node);
- PADDLE_ENFORCE(new_optimizer_node);
+ PADDLE_ENFORCE_NOT_NULL(
+ old_optimizer_node,
+ platform::errors::InvalidArgument(
+ "Input argument old_optimizer_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ new_optimizer_node,
+ platform::errors::InvalidArgument(
+ "Input argument new_optimizer_node cannot be nullptr."));
for (ir::Node* downstream_node : old_optimizer_node->outputs) {
// Remove the old_optimizer_node from downstream_node's inputs vector
@@ -292,8 +334,12 @@ void LockFreeOptimizePass::ReplaceAllDownstreamNode(
ir::Node* LockFreeOptimizePass::FindForwardOpViaBackwardOp(
ir::Graph* graph, ir::Node* backward_node) const {
- PADDLE_ENFORCE(graph);
- PADDLE_ENFORCE(backward_node);
+ PADDLE_ENFORCE_NOT_NULL(graph,
+ platform::errors::InvalidArgument(
+ "Input argument graph cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(
+ backward_node, platform::errors::InvalidArgument(
+ "Input argument backward_node cannot be nullptr."));
// strip the suffix _grad of backward_node's name
std::string forward_op_name = backward_node->Name();
diff --git a/paddle/fluid/framework/ir/lock_free_optimize_pass.h b/paddle/fluid/framework/ir/lock_free_optimize_pass.h
index 9c923480bac26fb8c68768c8365b0f899959ec64..f38f48fcd92a6b672254b3d1dda44671652b8ddb 100644
--- a/paddle/fluid/framework/ir/lock_free_optimize_pass.h
+++ b/paddle/fluid/framework/ir/lock_free_optimize_pass.h
@@ -87,34 +87,46 @@ class LockFreeOptimizePass : public Pass {
ir::Node* downstream_node) const;
inline bool IsOpNamed(ir::Node* node, const std::string& name) const {
- PADDLE_ENFORCE(node);
+ PADDLE_ENFORCE_NOT_NULL(node,
+ platform::errors::InvalidArgument(
+ "Input argument node cannot be nullptr."));
return node->NodeType() == Node::Type::kOperation && node->Name() == name;
}
inline bool IsVarNamed(ir::Node* node, const std::string& name) const {
- PADDLE_ENFORCE(node);
+ PADDLE_ENFORCE_NOT_NULL(node,
+ platform::errors::InvalidArgument(
+ "Input argument node cannot be nullptr."));
return node->NodeType() == Node::Type::kVariable && node->Name() == name;
}
inline bool IsVarNameEndsWith(ir::Node* node, const std::string& name) const {
- PADDLE_ENFORCE(node);
+ PADDLE_ENFORCE_NOT_NULL(node,
+ platform::errors::InvalidArgument(
+ "Input argument node cannot be nullptr."));
return node->NodeType() == Node::Type::kVariable &&
boost::algorithm::ends_with(node->Name(), name);
}
inline bool IsVarNameContains(ir::Node* node, const std::string& name) const {
- PADDLE_ENFORCE(node);
+ PADDLE_ENFORCE_NOT_NULL(node,
+ platform::errors::InvalidArgument(
+ "Input argument node cannot be nullptr."));
return node->NodeType() == Node::Type::kVariable &&
node->Name().find(name) != std::string::npos;
}
inline bool IsControlDepFrom(ir::Node* ctrl_dep_node, ir::Node* node) const {
- PADDLE_ENFORCE(ctrl_dep_node);
- PADDLE_ENFORCE(node);
+ PADDLE_ENFORCE_NOT_NULL(
+ ctrl_dep_node, platform::errors::InvalidArgument(
+ "Input argument ctrl_dep_node cannot be nullptr."));
+ PADDLE_ENFORCE_NOT_NULL(node,
+ platform::errors::InvalidArgument(
+ "Input argument node cannot be nullptr."));
return IsControlDepVar(*ctrl_dep_node) &&
ctrl_dep_node->inputs.size() >= 1u &&
diff --git a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc
index d67f2274ebf1f0b57cf0e9c9fedd2f61eb1d5c9d..456e642ad86ab18d55df2d36650f04c4d6635876 100644
--- a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc
+++ b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc
@@ -85,7 +85,9 @@ void BatchMergePass::ApplyImpl(ir::Graph* graph) const {
// 1. record op nodes of different roles
for (auto node : nodes) {
if (!node->IsOp()) continue;
- PADDLE_ENFORCE(node->Op(), "must find opdesc");
+ PADDLE_ENFORCE_NOT_NULL(
+ node->Op(), platform::errors::InvalidArgument(
+ "Node(%s) must hold op description.", node->Name()));
int op_role = BOOST_GET_CONST(
int, node->Op()->GetAttr(
framework::OpProtoAndCheckerMaker::OpRoleAttrName()));
@@ -108,7 +110,9 @@ void BatchMergePass::ApplyImpl(ir::Graph* graph) const {
} else if (op_role & static_cast(framework::OpRole::kLRSched)) {
lr_ops.push_back(node);
} else { // NOLINT
- PADDLE_THROW("Invalid op_role: %d", static_cast(op_role));
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Invalid op role(%d), in node(%s).", static_cast(op_role),
+ node->Name()));
}
}
diff --git a/paddle/fluid/framework/ir/node.h b/paddle/fluid/framework/ir/node.h
index fbc0d7599eae12d32ccb6d7ea9546ce044037824..87e7e64acb71a5059b2f3bf1539ff281ac322774 100644
--- a/paddle/fluid/framework/ir/node.h
+++ b/paddle/fluid/framework/ir/node.h
@@ -66,12 +66,18 @@ class Node {
std::string Name() const { return name_; }
VarDesc* Var() const {
- PADDLE_ENFORCE_EQ(IsVar(), true);
+ PADDLE_ENFORCE_EQ(IsVar(), true,
+ platform::errors::InvalidArgument(
+ "Node(%s) must be kVariable type, but not %d.", name_,
+ static_cast(type_)));
return var_desc_.get();
}
OpDesc* Op() const {
- PADDLE_ENFORCE_EQ(IsOp(), true);
+ PADDLE_ENFORCE_EQ(IsOp(), true,
+ platform::errors::InvalidArgument(
+ "Node(%s) must be kOperation type, but not %d.",
+ name_, static_cast(type_)));
return op_desc_.get();
}
@@ -92,8 +98,9 @@ class Node {
try {
return *boost::any_cast(wrapper_);
} catch (boost::bad_any_cast&) {
- PADDLE_THROW("Invalid wrapper type error, expected %s, actual %s",
- typeid(T).name(), wrapper_type_.name());
+ PADDLE_THROW(platform::errors::InvalidArgument(
+ "Invalid wrapper type error, expected %s, actual %s.",
+ typeid(T).name(), wrapper_type_.name()));
}
}
@@ -114,8 +121,9 @@ class Node {
}
void RenameVar(const std::string& new_name) {
- PADDLE_ENFORCE(type_ == Type::kVariable && var_desc_,
- "Must be type of variable");
+ PADDLE_ENFORCE_EQ(
+ type_ == Type::kVariable && var_desc_, true,
+ platform::errors::InvalidArgument("Node must be type of variable."));
name_ = new_name;
var_desc_->SetName(new_name);
}
diff --git a/paddle/fluid/framework/ir/pass.cc b/paddle/fluid/framework/ir/pass.cc
index 78e8b1612648404743e6ba6725777e55d688e662..a5ca13f1ce252d2368e2fc765e49d397356660a7 100644
--- a/paddle/fluid/framework/ir/pass.cc
+++ b/paddle/fluid/framework/ir/pass.cc
@@ -19,6 +19,9 @@ limitations under the License. */
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/platform/device_context.h"
+#ifdef PADDLE_WITH_MKLDNN
+#include "paddle/fluid/platform/mkldnn_helper.h"
+#endif
namespace paddle {
namespace framework {
@@ -26,7 +29,8 @@ namespace ir {
Graph* Pass::Apply(Graph* graph) const {
CheckPrevPass();
- PADDLE_ENFORCE(graph, "graph passed to Pass::Apply() cannot be empty.");
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
for (const std::string& attr : required_pass_attrs_) {
PADDLE_ENFORCE_NE(
attrs_.find(attr), attrs_.end(),
@@ -40,11 +44,14 @@ Graph* Pass::Apply(Graph* graph) const {
}
ApplyImpl(graph);
// TODO(panyx0718): Add more verifications.
- PADDLE_ENFORCE(!HasCircle(*graph),
- "Illegal Pass %s. Generated graph shouldn't have cycle.",
- Type());
- PADDLE_ENFORCE(VarDescIsConsistency(*graph),
- "The VarDescs of persistable variable are not consistency.");
+ PADDLE_ENFORCE_EQ(
+ HasCircle(*graph), false,
+ platform::errors::InvalidArgument(
+ "Illegal pass %s. Generated graph shouldn't contain cycle.", Type()));
+ PADDLE_ENFORCE_EQ(
+ VarDescIsConsistency(*graph), true,
+ platform::errors::InvalidArgument(
+ "The VarDescs of persistable variable are not consistency."));
applied_ = true;
if (!graph->Has(kPassRecorder)) {
graph->Set(kPassRecorder, new PassRecorder);
@@ -53,10 +60,7 @@ Graph* Pass::Apply(Graph* graph) const {
#ifdef PADDLE_WITH_MKLDNN
// Clear mkl-dnn cache,
// Passes can change params, tensors, so caching need to be discarded
- platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
- platform::MKLDNNDeviceContext* dev_ctx =
- (platform::MKLDNNDeviceContext*)pool.Get(paddle::platform::CPUPlace());
- dev_ctx->ResetBlobMap();
+ ClearMKLDNNCache(paddle::platform::CPUPlace());
#endif
return graph;
}
diff --git a/paddle/fluid/framework/ir/pass.h b/paddle/fluid/framework/ir/pass.h
index b7b46085b9067b43a2613ea47043b8923da4c1b6..0f5ef551f044d9e53b04b6efad3954d1a48a0ac3 100644
--- a/paddle/fluid/framework/ir/pass.h
+++ b/paddle/fluid/framework/ir/pass.h
@@ -55,8 +55,9 @@ class Pass {
// Get a reference to the attributed previously set.
template
AttrType &Get(const std::string &attr_name) const {
- PADDLE_ENFORCE(attrs_.find(attr_name) != attrs_.end(),
- "%s attr not registered for pass.", attr_name);
+ PADDLE_ENFORCE_NE(attrs_.find(attr_name), attrs_.end(),
+ platform::errors::InvalidArgument(
+ "Attribute %s not registered for pass.", attr_name));
try {
return *boost::any_cast(attrs_.at(attr_name));
} catch (boost::bad_any_cast &) {
@@ -76,7 +77,7 @@ class Pass {
};
PADDLE_THROW(platform::errors::InvalidArgument(
- "Invalid type for attritube %s, expected: %s, actual: %s", attr_name,
+ "Invalid type for attritube %s, expected: %s, actual: %s.", attr_name,
TypeToString(typeid(AttrType *)),
TypeToString(attrs_.at(attr_name).type())));
}
@@ -101,9 +102,10 @@ class Pass {
template
void Set(const std::string &attr_name, AttrType *attr) {
if (default_pass_attrs_.count(attr_name) == 0) {
- PADDLE_ENFORCE_EQ(attrs_.count(attr_name), 0,
- platform::errors::InvalidArgument(
- "Attribute %s already set in the pass", attr_name));
+ PADDLE_ENFORCE_EQ(
+ attrs_.count(attr_name), 0,
+ platform::errors::AlreadyExists(
+ "Attribute %s already set in the pass.", attr_name));
} else {
VLOG(3) << "Setting the attribute " << attr_name << " for the pass "
<< type_;
@@ -119,15 +121,16 @@ class Pass {
// should delete the attribute.
template
void SetNotOwned(const std::string &attr_name, AttrType *attr) {
- PADDLE_ENFORCE(attrs_.count(attr_name) == 0, "%s already set in the pass",
- attr_name);
+ PADDLE_ENFORCE_EQ(attrs_.count(attr_name), 0,
+ platform::errors::AlreadyExists(
+ "Attribute %s already set in the pass.", attr_name));
attrs_[attr_name] = attr;
}
protected:
virtual void ApplyImpl(Graph *graph) const {
PADDLE_THROW(platform::errors::Unimplemented(
- "The virtual Pass called is not implemented."));
+ "The virtual pass called is not implemented."));
}
// Some Pass must be placed before this Pass, and some
@@ -198,8 +201,9 @@ class PassRegistry {
}
std::unique_ptr Get(const std::string &pass_type) const {
- PADDLE_ENFORCE(Has(pass_type), "Pass %s has not been registered",
- pass_type);
+ PADDLE_ENFORCE_EQ(Has(pass_type), true,
+ platform::errors::InvalidArgument(
+ "Pass %s has not been registered.", pass_type));
return map_.at(pass_type)();
}
@@ -213,8 +217,10 @@ class PassRegistry {
template
struct PassRegistrar : public Registrar {
explicit PassRegistrar(const char *pass_type) {
- PADDLE_ENFORCE(!PassRegistry::Instance().Has(pass_type),
- "'%s' is registered more than once.", pass_type);
+ PADDLE_ENFORCE_EQ(
+ PassRegistry::Instance().Has(pass_type), false,
+ platform::errors::AlreadyExists(
+ "Pass '%s' is registered more than once.", pass_type));
PassRegistry::Instance().Insert(
pass_type, [this, pass_type]() -> std::unique_ptr {
std::unique_ptr pass(new PassType());
diff --git a/paddle/fluid/framework/ir/pass_builder.cc b/paddle/fluid/framework/ir/pass_builder.cc
index 8355764aa6c983ace203906190e6cc6d86b500dd..6457bd230c59cfebd19ab7951b2c04a1890e3fce 100644
--- a/paddle/fluid/framework/ir/pass_builder.cc
+++ b/paddle/fluid/framework/ir/pass_builder.cc
@@ -28,13 +28,19 @@ std::shared_ptr PassBuilder::AppendPass(const std::string& pass_type) {
}
void PassBuilder::RemovePass(size_t idx) {
- PADDLE_ENFORCE(passes_.size() > idx);
+ PADDLE_ENFORCE_GT(
+ passes_.size(), idx,
+ platform::errors::InvalidArgument(
+ "Passes size is %d, %d is not a valid index.", passes_.size(), idx));
passes_.erase(passes_.begin() + idx);
}
std::shared_ptr PassBuilder::InsertPass(size_t idx,
const std::string& pass_type) {
- PADDLE_ENFORCE(passes_.size() >= idx);
+ PADDLE_ENFORCE_GE(
+ passes_.size(), idx,
+ platform::errors::InvalidArgument(
+ "Passes size is %d, %d is not a valid index.", passes_.size(), idx));
std::shared_ptr pass(
ir::PassRegistry::Instance().Get(pass_type).release());
passes_.insert(passes_.begin() + idx, std::move(pass));
diff --git a/paddle/fluid/framework/ir/pass_test.cc b/paddle/fluid/framework/ir/pass_test.cc
index 14e94a2bc5c51a7eb34cbe42890a6ab4572ef420..0c5286b3f77e10876b0240e1245ca343471770d5 100644
--- a/paddle/fluid/framework/ir/pass_test.cc
+++ b/paddle/fluid/framework/ir/pass_test.cc
@@ -119,7 +119,7 @@ TEST(PassTest, TestPassAttrCheck) {
} catch (paddle::platform::EnforceNotMet& e) {
exception = std::string(e.what());
}
- ASSERT_TRUE(exception.find("shouldn't have cycle") != exception.npos);
+ ASSERT_TRUE(exception.find("shouldn't contain cycle") != exception.npos);
pass = PassRegistry::Instance().Get("test_pass");
pass->Set("test_pass_attr", new int);
diff --git a/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc b/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
index 1f1a54f140b0d0fde18529708b0ea920a52ee466..4506c162fa743a3fcb5973a9f0ebd9e8f6cdcd36 100644
--- a/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
@@ -43,9 +43,11 @@ void DeleteQuant(ir::Graph* graph, Scope* scope,
// ops linked from it
auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph,
Graph* g) {
- PADDLE_ENFORCE_EQ(subgraph.count(input_act_node), true,
- platform::errors::NotFound(
- "Input act node not found in Delete Quant fusion."));
+ PADDLE_ENFORCE_EQ(
+ subgraph.count(input_act_node), true,
+ platform::errors::NotFound(
+ "Input act node(%s) not found in QuantDequantFuse pass.",
+ input_act_node->name()));
Node* input_act = subgraph.at(input_act_node);
Node* input_scale = subgraph.at(pattern.GetPDNode("input_scale_node"));
Node* quant = subgraph.at(pattern.GetPDNode("quant_node"));
@@ -58,7 +60,7 @@ void DeleteQuant(ir::Graph* graph, Scope* scope,
std::string input_scale_var_name = quant->Op()->Input("InScale").front();
PADDLE_ENFORCE_NOT_NULL(
scope, platform::errors::InvalidArgument(
- "scope in DeleteQuantOpFuse pass should not be null."));
+ "Scope in QuantDequantFuse pass should not be null."));
const LoDTensor& input_scale_tensor =
scope->FindVar(input_scale_var_name)->Get();
PADDLE_ENFORCE_EQ(
@@ -84,8 +86,8 @@ void DeleteQuant(ir::Graph* graph, Scope* scope,
} else if (quantized_op_type == "mul") {
op_desc->SetAttr("X_scale", scale_value);
} else {
- PADDLE_THROW(platform::errors::InvalidArgument(
- "Unsupported quantized op type %s", quantized_op_type));
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Unsupported quantized op type %s.", quantized_op_type));
}
op_desc->SetAttr("bit_length", bit_length);
op_desc->RenameInput(output_act_name, input_act_name);
@@ -119,9 +121,9 @@ void FuseDequant(ir::Graph* graph, Scope* scope,
weight_name = "W";
input_name = "Input";
} else {
- PADDLE_ENFORCE(
+ PADDLE_THROW(platform::errors::Unimplemented(
"QuantDequantFuse: We only support conv2d, conv2d_fusion, fc, mul for "
- "now.");
+ "now."));
}
const std::string pattern_name = "dequant_fuse";
GraphPatternDetector gpd;
@@ -141,8 +143,9 @@ void FuseDequant(ir::Graph* graph, Scope* scope,
Graph* g) {
PADDLE_ENFORCE_EQ(
subgraph.count(quantized_op_input), true,
- platform::errors::NotFound(
- "Quantized op input node not found in Delete Quant fusion."));
+ platform::errors::NotFound("Quantized op input node(%s) did not find "
+ "in QuantDequantFuse pass.",
+ quantized_op_input->name()));
Node* quantized_op_input_node = subgraph.at(quantized_op_input);
Node* quantized_op_weight_node =
subgraph.at(pattern.GetPDNode("quantized_op_weight"));
@@ -165,7 +168,7 @@ void FuseDequant(ir::Graph* graph, Scope* scope,
PADDLE_ENFORCE_EQ(
scales_name.size(), 2,
platform::errors::InvalidArgument(
- "Scales size in channel-wise dequantize op should be 2, got %d",
+ "Scales size in channel-wise dequantize op should be 2, got %d.",
scales_name.size()));
const LoDTensor& channel_scale_tensor =
scope->FindVar(scales_name[0])->Get();
@@ -193,9 +196,10 @@ void FuseDequant(ir::Graph* graph, Scope* scope,
bool valid_scale_size =
(weight_scale.size() == 1 ||
weight_scale.size() == static_cast(w_dims[0]));
- PADDLE_ENFORCE_EQ(valid_scale_size, true,
- platform::errors::InvalidArgument(
- "TRT int8 quant: invalid scale size"));
+ PADDLE_ENFORCE_EQ(
+ valid_scale_size, true,
+ platform::errors::InvalidArgument(
+ "TRT int8 quant: invalid scale size(%d).", weight_scale.size()));
float* quantized_weight_data =
weight_tensor->mutable_data(platform::CPUPlace());
for (int j = 0; j < weight_tensor->numel(); j++) {
diff --git a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc
index dddb2affbbad06e9f2f478985c604ded7a1953ce..2396a7f3c4f84f70c2f350e2121c4044c56b141a 100644
--- a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc
@@ -278,11 +278,12 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
auto retrieve_node = [](const std::string& name,
const GraphPatternDetector::subgraph_t& subgraph,
const PDPattern& pat) -> Node* {
- PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)),
- "pattern has no Node called %s", name.c_str());
+ PADDLE_ENFORCE_GT(subgraph.count(pat.RetrieveNode(name)), 0,
+ platform::errors::NotFound(
+ "Pattern has no node called %s.", name.c_str()));
Node* p = subgraph.at(pat.RetrieveNode(name));
- PADDLE_ENFORCE_NOT_NULL(
- p, platform::errors::NotFound("subgraph has no node %s", name.c_str()));
+ PADDLE_ENFORCE_NOT_NULL(p, platform::errors::NotFound(
+ "Subgraph has no node %s.", name.c_str()));
return p;
};
@@ -365,7 +366,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
}
void RepeatedFCReluFusePass::ApplyImpl(ir::Graph* graph) const {
- PADDLE_ENFORCE_NOT_NULL(graph);
+ PADDLE_ENFORCE_NOT_NULL(
+ graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
FusePassBase::Init(name_scope_, graph);
int fusion_count = 0;
diff --git a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass_tester.cc b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass_tester.cc
index 81d9476d409d9472518b14390492c3d9d1ab391c..283fe3797e454f92bea696fa97eaa744663f114c 100644
--- a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass_tester.cc
+++ b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass_tester.cc
@@ -55,9 +55,15 @@ void TestMain(int num_fc) {
VLOG(3) << DebugString(graph);
// Delete (num_fc_nodes_before - 1) fc ops
- PADDLE_ENFORCE_EQ(num_nodes_before - (num_fc_nodes_before - 1) + 1,
- num_nodes_after);
- PADDLE_ENFORCE_EQ(num_fused_nodes_after, 1);
+ PADDLE_ENFORCE_EQ(
+ num_nodes_before - (num_fc_nodes_before - 1) + 1, num_nodes_after,
+ platform::errors::InvalidArgument(
+ "num_nodes_before = %d, num_fc_nodes_before = %d, num_nodes_after = "
+ "%d.",
+ num_nodes_before, num_fc_nodes_before, num_nodes_after));
+ PADDLE_ENFORCE_EQ(num_fused_nodes_after, 1,
+ platform::errors::InvalidArgument(
+ "num_fused_nodes_after = %d.", num_fused_nodes_after));
}
TEST(RepeatedFCReluFusePass, basic_3) { TestMain(3); }
diff --git a/paddle/fluid/framework/ir/seq_concat_fc_fuse_pass.cc b/paddle/fluid/framework/ir/seq_concat_fc_fuse_pass.cc
index bd826709b1d88abefbfdf487603b5c157ca7bd95..19ec2d818a3db5140031287618f054f8468970fe 100644
--- a/paddle/fluid/framework/ir/seq_concat_fc_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/seq_concat_fc_fuse_pass.cc
@@ -185,11 +185,13 @@ void SeqConcatFcFusePass::ApplyImpl(ir::Graph* graph) const {
auto* concat_out = BuildSeqExpandConcatPattern(pattern);
BuildFCPattern(pattern, concat_out);
-#define GET_NODE(id, pattern) \
- PADDLE_ENFORCE(subgraph.count(pattern.RetrieveNode(#id)), \
- "pattern has no Node called %s", #id); \
- auto* id = subgraph.at(pattern.RetrieveNode(#id)); \
- PADDLE_ENFORCE_NOT_NULL(id, "subgraph has no node %s", #id);
+#define GET_NODE(id, pattern) \
+ PADDLE_ENFORCE_GT( \
+ subgraph.count(pattern.RetrieveNode(#id)), 0, \
+ platform::errors::NotFound("Pattern has no node called %s.", #id)); \
+ auto* id = subgraph.at(pattern.RetrieveNode(#id)); \
+ PADDLE_ENFORCE_NOT_NULL( \
+ id, platform::errors::NotFound("Subgraph has no node %s.", #id));
int fuse_count{0};
diff --git a/paddle/fluid/framework/ir/seqpool_concat_fuse_pass.cc b/paddle/fluid/framework/ir/seqpool_concat_fuse_pass.cc
index ea376b371f592e6aa21149e9c109595a0818581a..1c220ee4d571815eaf26255db2c519dc4821068c 100644
--- a/paddle/fluid/framework/ir/seqpool_concat_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/seqpool_concat_fuse_pass.cc
@@ -139,11 +139,12 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
auto retrieve_node = [](const std::string& name,
const GraphPatternDetector::subgraph_t& subgraph,
const PDPattern& pat) -> Node* {
- PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)),
- "pattern has no Node called %s", name.c_str());
+ PADDLE_ENFORCE_GT(subgraph.count(pat.RetrieveNode(name)), 0,
+ platform::errors::NotFound(
+ "Pattern has no node called %s.", name.c_str()));
Node* p = subgraph.at(pat.RetrieveNode(name));
- PADDLE_ENFORCE_NOT_NULL(
- p, platform::errors::NotFound("subgraph has no node %s", name.c_str()));
+ PADDLE_ENFORCE_NOT_NULL(p, platform::errors::NotFound(
+ "Subgraph has no node %s.", name.c_str()));
return p;
};
diff --git a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc
index 92d2a6acbb9f7aa5f267347151fa4f23f04c3e40..d9a65e71592ff464a2e6beaa2219a39103f6cae1 100644
--- a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc
+++ b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc
@@ -47,7 +47,9 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const {
Graph* g) {
GET_NODES;
- PADDLE_ENFORCE(subgraph.count(x));
+ PADDLE_ENFORCE_GT(
+ subgraph.count(x), 0,
+ platform::errors::NotFound("Detector did not find input X."));
auto* input_node = subgraph.at(x);
auto reshape1_desc = reshape1_op->Op();
auto reshape2_desc = reshape2_op->Op();
diff --git a/paddle/fluid/framework/ir/simplify_with_basic_ops_pass_tester.cc b/paddle/fluid/framework/ir/simplify_with_basic_ops_pass_tester.cc
index 324b9c0b7da248eb97f2fa46c112e36b49b1803b..80f387c442760db8217e152a9ae08ca3da7dc105 100644
--- a/paddle/fluid/framework/ir/simplify_with_basic_ops_pass_tester.cc
+++ b/paddle/fluid/framework/ir/simplify_with_basic_ops_pass_tester.cc
@@ -59,12 +59,25 @@ TEST(SimplifyWithBasicOpsPass, dropout) {
int num_scale_nodes_after = GetNumOpNodes(graph, "scale");
VLOG(3) << DebugString(graph);
- PADDLE_ENFORCE_EQ(num_dropout_nodes_after, 0);
+ PADDLE_ENFORCE_EQ(
+ num_dropout_nodes_after, 0,
+ platform::errors::InvalidArgument("num_dropout_nodes_after = %d.",
+ num_dropout_nodes_after));
if (dropout_implementation == "downgrade_in_infer") {
- PADDLE_ENFORCE_EQ(num_dropout_nodes_before,
- num_scale_nodes_after - num_scale_nodes_before);
+ PADDLE_ENFORCE_EQ(
+ num_dropout_nodes_before,
+ num_scale_nodes_after - num_scale_nodes_before,
+ platform::errors::InvalidArgument(
+ "num_dropout_nodes_before = %d, num_scale_nodes_after = %d, "
+ "num_scale_nodes_before = %d.",
+ num_dropout_nodes_before, num_scale_nodes_after,
+ num_scale_nodes_before));
} else {
- PADDLE_ENFORCE_EQ(num_scale_nodes_after - num_scale_nodes_before, 0);
+ PADDLE_ENFORCE_EQ(
+ num_scale_nodes_after - num_scale_nodes_before, 0,
+ platform::errors::InvalidArgument(
+ "num_scale_nodes_after = %d, num_scale_nodes_before = %d.",
+ num_scale_nodes_after, num_scale_nodes_before));
}
}
}
diff --git a/paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc b/paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
index 6d908b4362b80dfecaed23316e1ca8290f902acd..035b198bdcc51800be62acce58a538145413e92f 100644
--- a/paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
@@ -300,10 +300,12 @@ static int BuildFusion(Graph* graph, const std::string& name_scope) {
auto retrieve_node = [](const std::string& name,
const GraphPatternDetector::subgraph_t& subgraph,
const PDPattern& pat) -> Node* {
- PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)),
- "pattern has no Node called %s", name.c_str());
+ PADDLE_ENFORCE_GT(subgraph.count(pat.RetrieveNode(name)), 0,
+ platform::errors::NotFound(
+ "Pattern has no node called %s.", name.c_str()));
Node* p = subgraph.at(pat.RetrieveNode(name));
- PADDLE_ENFORCE_NOT_NULL(p, "subgraph has no node %s", name.c_str());
+ PADDLE_ENFORCE_NOT_NULL(p, platform::errors::NotFound(
+ "Subgraph has no node %s.", name.c_str()));
return p;
};
diff --git a/paddle/fluid/framework/ir/transpose_flatten_concat_fuse_pass.cc b/paddle/fluid/framework/ir/transpose_flatten_concat_fuse_pass.cc
index 90ffaada055a9d2b71ef4b89244d063e72a1a7cb..9a0a5f07a7080593d8f13e07788c703edb92c7ad 100644
--- a/paddle/fluid/framework/ir/transpose_flatten_concat_fuse_pass.cc
+++ b/paddle/fluid/framework/ir/transpose_flatten_concat_fuse_pass.cc
@@ -51,15 +51,25 @@ void RunTransposeFlattenConcatFuse(ir::Graph *graph, int times) {
std::vector nodes;
for (int i = 0; i < times; i++) {
- PADDLE_ENFORCE(
- subgraph.at(pattern.GetPDNode("transpose" + std::to_string(i))));
- PADDLE_ENFORCE(
- subgraph.at(pattern.GetPDNode("transpose_out" + std::to_string(i))));
- PADDLE_ENFORCE(
- subgraph.at(pattern.GetPDNode("flatten" + std::to_string(i))));
- PADDLE_ENFORCE(
- subgraph.at(pattern.GetPDNode("flatten_out" + std::to_string(i))));
- PADDLE_ENFORCE(subgraph.at(input_nodes[i]));
+ PADDLE_ENFORCE_NOT_NULL(
+ subgraph.at(pattern.GetPDNode("transpose" + std::to_string(i))),
+ platform::errors::NotFound("Can not find transpose%d in subgraph.",
+ i));
+ PADDLE_ENFORCE_NOT_NULL(
+ subgraph.at(pattern.GetPDNode("transpose_out" + std::to_string(i))),
+ platform::errors::NotFound(
+ "Can not find transpose_out%d in subgraph.", i));
+ PADDLE_ENFORCE_NOT_NULL(
+ subgraph.at(pattern.GetPDNode("flatten" + std::to_string(i))),
+ platform::errors::NotFound("Can not find flatten%d in subgraph.", i));
+ PADDLE_ENFORCE_NOT_NULL(
+ subgraph.at(pattern.GetPDNode("flatten_out" + std::to_string(i))),
+ platform::errors::NotFound("Can not find flatten_out%d in subgraph.",
+ i));
+ PADDLE_ENFORCE_NOT_NULL(
+ subgraph.at(input_nodes[i]),
+ platform::errors::NotFound("Can not find %s in subgraph.",
+ input_nodes[i]->name()));
nodes.push_back(subgraph.at(input_nodes[i]));
nodes.push_back(
diff --git a/paddle/fluid/framework/library_type.h b/paddle/fluid/framework/library_type.h
index 904cc013012b9c3ea8054816446844f6d2cda26b..d46f8a574c0d956dc0a90bc2741d2cb80313ab7f 100644
--- a/paddle/fluid/framework/library_type.h
+++ b/paddle/fluid/framework/library_type.h
@@ -37,7 +37,10 @@ inline std::string LibraryTypeToString(const LibraryType& library_type) {
case LibraryType::kCUDNN:
return "CUDNN";
default:
- PADDLE_THROW("unknown LibraryType %d", static_cast(library_type));
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Unknown LibraryType code (%d), only supports library type include "
+ "PLAIN(0), MKLDNN(1), CUDNN(2).",
+ static_cast(library_type)));
}
}
@@ -59,7 +62,10 @@ inline LibraryType StringToLibraryType(const char* ctype) {
} else if (s == std::string("CUDA")) {
return LibraryType::kPlain;
} else {
- PADDLE_THROW("Unknown LibraryType %s", s.c_str());
+ PADDLE_THROW(platform::errors::Unimplemented(
+ "Unknown LibraryType string (%s), only support library type string "
+ "include PLAIN, MKLDNN, CUDNN, CPU and CUDA.",
+ s.c_str()));
}
}
diff --git a/paddle/fluid/framework/load_op_lib.h b/paddle/fluid/framework/load_op_lib.h
index dd96137f02010ca2cf1e71597362d5f03e9fa008..16cffe119d63e0cb8bd6ff76f4ac5792127f480d 100644
--- a/paddle/fluid/framework/load_op_lib.h
+++ b/paddle/fluid/framework/load_op_lib.h
@@ -35,7 +35,10 @@ T *DynLoad(void *handle, std::string name) {
#else
auto errorno = GetLastError();
#endif // !_WIN32
- PADDLE_ENFORCE_NOT_NULL(func, errorno);
+ PADDLE_ENFORCE_NOT_NULL(
+ func,
+ platform::errors::NotFound(
+ "Failed to load dynamic operator library, error code(%s).", errorno));
return func;
}
@@ -63,9 +66,9 @@ void LoadOpLib(const std::string &dso_name) {
type == "conditional_block" || type == "conditional_block_grad") {
continue;
}
- if (info_map.Has(n.first)) {
- PADDLE_THROW("Op %s has been registered.");
- }
+ PADDLE_ENFORCE_NE(info_map.Has(n.first), true,
+ platform::errors::AlreadyExists(
+ "Operator (%s) has been registered.", type));
OpInfo info;
info.creator_ = n.second.creator_;
@@ -88,7 +91,8 @@ void LoadOpLib(const std::string &dso_name) {
for (auto &str : strs) {
proto::OpDesc proto_desc;
PADDLE_ENFORCE_EQ(proto_desc.ParseFromString(str), true,
- "Failed to parse OpDesc from string");
+ platform::errors::InvalidArgument(
+ "Failed to parse OpDesc from string."));
ret.emplace_back(new OpDesc(proto_desc, nullptr));
}
return ret;
diff --git a/paddle/fluid/framework/lod_rank_table.cc b/paddle/fluid/framework/lod_rank_table.cc
index 6bc795b642bf79b7556869c5ebe9b0323d3cc5fc..70df4f50ec910bfaa78924f834fa2c165ac1048d 100644
--- a/paddle/fluid/framework/lod_rank_table.cc
+++ b/paddle/fluid/framework/lod_rank_table.cc
@@ -19,9 +19,11 @@ namespace framework {
void LoDRankTable::Reset(const LoD& lod, size_t level) {
this->coarse_lod_.clear();
this->items_.clear();
- PADDLE_ENFORCE(level < lod.size(),
- "Cannot rank lod since the level %d is less than lod size %d",
- level, lod.size());
+ PADDLE_ENFORCE_LT(
+ level, lod.size(),
+ platform::errors::InvalidArgument(
+ "Cannot reset LoD since the level %d is less than lod size %d.",
+ level, lod.size()));
coarse_lod_.reserve(level);
for (size_t i = 0; i < level; ++i) {
coarse_lod_.push_back(lod[i]);
diff --git a/paddle/fluid/framework/lod_tensor.cc b/paddle/fluid/framework/lod_tensor.cc
index 2d1cba3b0f795cb1b65286adbf51d9bd2ddeb1f9..40615d772e555bb9e2ac44a6339de9f3be3c9562 100644
--- a/paddle/fluid/framework/lod_tensor.cc
+++ b/paddle/fluid/framework/lod_tensor.cc
@@ -65,9 +65,23 @@ std::string LoDToString(const LoD &lod) {
LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin,
size_t elem_end) {
- PADDLE_ENFORCE_LT(level, in.size());
- PADDLE_ENFORCE_LT(elem_begin, elem_end);
- PADDLE_ENFORCE_LT(elem_end, in[level].size());
+ PADDLE_ENFORCE_LT(level, in.size(),
+ platform::errors::InvalidArgument(
+ "The input LoDTensor's lod level should be less than "
+ "the LoD size, but received level is %d, LoD is %s.",
+ level, in));
+ PADDLE_ENFORCE_LT(
+ elem_begin, elem_end,
+ platform::errors::InvalidArgument(
+ "The index to start slicing should be less than the index to end "
+ "slicing, but received start index is %d, end index is %d.",
+ elem_begin, elem_end));
+ PADDLE_ENFORCE_LT(
+ elem_end, in[level].size(),
+ platform::errors::InvalidArgument(
+ "The index to end slicing should be less than the input LoD size, "
+ "but received end index is %d, LoD size is %d.",
+ elem_end, in[level].size()));
LoD res;
res.resize(in.size() - level);
@@ -185,8 +199,17 @@ LoDAndOffset GetSubLoDAndAbsoluteOffset(const LoD &lod, size_t start_idx,
LoD sub_lod;
for (size_t level_idx = start_level; level_idx < lod.size(); ++level_idx) {
- PADDLE_ENFORCE_LE(start_idx, end_idx);
- PADDLE_ENFORCE_LT(end_idx, lod[level_idx].size());
+ PADDLE_ENFORCE_LE(start_idx, end_idx,
+ platform::errors::InvalidArgument(
+ "The start index should be less than the end index, "
+ "but received start index is %d, end index is %d.",
+ start_idx, end_idx));
+ PADDLE_ENFORCE_LT(
+ end_idx, lod[level_idx].size(),
+ platform::errors::InvalidArgument(
+ "The end index should be less than the LoD level size, but "
+ "received end index is %d, LoD level size is %d.",
+ end_idx, lod[level_idx].size()));
std::vector level_lens;
for (size_t i = start_idx; i < end_idx; ++i) {
level_lens.push_back(lod[level_idx][i + 1] - lod[level_idx][i]);
@@ -202,7 +225,10 @@ LoDAndOffset GetSubLoDAndAbsoluteOffset(const LoD &lod, size_t start_idx,
void AppendLoD(LoD *lod, const LoD &lod_length) {
PADDLE_ENFORCE(
lod->empty() || lod->size() == lod_length.size(),
- "The lod_length should has the same size with the appended lod.");
+ platform::errors::InvalidArgument(
+ "The input LoD length should be equal to the appended LoD size, but "
+ "received input LoD length is %d, actual LoD size is %d.",
+ lod_length, lod->size()));
if (lod->empty()) {
for (size_t i = 0; i < lod_length.size(); ++i) {
lod->emplace_back(1, 0); // size = 1, value = 0;
@@ -254,11 +280,11 @@ void DeserializeFromStream(std::istream &is, LoDTensor *tensor,
is.read(reinterpret_cast(&version), sizeof(version));
PADDLE_ENFORCE_EQ(framework::IsTensorVersionSupported(version), true,
platform::errors::InvalidArgument(
- "tensor version %u is not supported.", version));
+ "Tensor version %u is not supported.", version));
PADDLE_ENFORCE_EQ(
version, 0U,
platform::errors::InvalidArgument(
- "tensor version %u is not supported, Only version 0 is supported",
+ "Tensor version %u is not supported, only version 0 is supported.",
version));
}
{
@@ -280,11 +306,11 @@ void DeserializeFromStream(std::istream &is, LoDTensor *tensor,
is.read(reinterpret_cast(&version), sizeof(version));
PADDLE_ENFORCE_EQ(framework::IsTensorVersionSupported(version), true,
platform::errors::InvalidArgument(
- "tensor version %u is not supported.", version));
+ "Tensor version %u is not supported.", version));
PADDLE_ENFORCE_EQ(
version, 0U,
platform::errors::InvalidArgument(
- "tensor version %u is not supported, Only version 0 is supported",
+ "Tensor version %u is not supported, only version 0 is supported.",
version));
}
{
@@ -310,7 +336,7 @@ std::vector LoDTensor::SplitLoDTensor(
const std::vector places) const {
PADDLE_ENFORCE_GT(places.size(), 0,
platform::errors::InvalidArgument(
- "place number cannot be empty when splitting"));
+ "Place number cannot be empty when splitting."));
check_memory_size();
size_t batch_size =
lod().empty() ? static_cast(dims()[0]) : lod()[0].size() - 1;
@@ -342,7 +368,9 @@ std::vector LoDTensor::SplitLoDTensor(
auto end = std::min((i + 1) * step_width, batch_size);
PADDLE_ENFORCE_LT(begin, end,
platform::errors::InvalidArgument(
- "begin must be less than end, this may be a bug"));
+ "The begin index must be less than the end index, "
+ "but received begin index is %d, end index is %d.",
+ begin, end));
LoDTensor dst;
if (lod().empty()) {
@@ -376,7 +404,9 @@ std::vector LoDTensor::SplitLoDTensor(
void LoDTensor::MergeLoDTensor(
const std::vector &lod_tensors,
platform::Place dst_place) {
- PADDLE_ENFORCE(!lod_tensors.empty());
+ PADDLE_ENFORCE_EQ(lod_tensors.empty(), false,
+ platform::errors::InvalidArgument(
+ "The LoDTensors to be merged are empty."));
framework::DDim new_dim = lod_tensors[0]->dims();
proto::VarType::Type new_type = proto::VarType::FP32;
@@ -395,15 +425,35 @@ void LoDTensor::MergeLoDTensor(
for (size_t i = 1; i < lod_tensors.size(); ++i) {
auto *t = lod_tensors[i];
if (t->numel() && t->IsInitialized()) {
- PADDLE_ENFORCE_EQ(new_type, t->type());
- PADDLE_ENFORCE_EQ(new_layout, t->layout());
- PADDLE_ENFORCE_EQ(framework::product(new_dim) / new_dim[0],
- framework::product(t->dims()) / t->dims()[0]);
+ PADDLE_ENFORCE_EQ(
+ new_type, t->type(),
+ platform::errors::InvalidArgument(
+ "LoDTensor data type does not match, expected type is %s, actual "
+ "type is %s.",
+ DataTypeToString(new_type), DataTypeToString(t->type())));
+ PADDLE_ENFORCE_EQ(
+ new_layout, t->layout(),
+ platform::errors::InvalidArgument(
+ "LoDTensor layout does not match, expected layout is %s, "
+ "actual layout is %s.",
+ DataLayoutToString(new_layout), DataLayoutToString(t->layout())));
+ PADDLE_ENFORCE_EQ(
+ framework::product(new_dim) / new_dim[0],
+ framework::product(t->dims()) / t->dims()[0],
+ platform::errors::InvalidArgument(
+ "LoDTensor dimension does not match, all dimensions except the "
+ "first dimension need to be equal,"
+ "but expected dimension is %s, actual dimension is %s.",
+ new_dim, t->dims()));
new_dim[0] += t->dims()[0];
}
auto &lod = t->lod();
- PADDLE_ENFORCE_EQ(new_lod.size(), lod.size());
+ PADDLE_ENFORCE_EQ(new_lod.size(), lod.size(),
+ platform::errors::InvalidArgument(
+ "The LoD information of LoDTensor does not match, "
+ "expected LoD is %s, actual LoD is %s.",
+ new_lod, lod));
for (size_t j = 0; j < lod.size(); ++j) {
auto &sub_lod = new_lod[j];
size_t offset = sub_lod.back();
diff --git a/paddle/fluid/framework/lod_tensor.h b/paddle/fluid/framework/lod_tensor.h
index 3ad873d1f6c500bf6135a521bfc846869b70f774..da97efb616840b6663677475c4ca5dab68d7ccfe 100644
--- a/paddle/fluid/framework/lod_tensor.h
+++ b/paddle/fluid/framework/lod_tensor.h
@@ -117,8 +117,19 @@ class LoDTensor : public Tensor {
* Get the start offset and end offset of an element from LoD.
*/
std::pair lod_element(size_t level, size_t elem) const {
- PADDLE_ENFORCE_LT(level, NumLevels());
- PADDLE_ENFORCE_LT(elem, NumElements(level));
+ PADDLE_ENFORCE_LT(
+ level, NumLevels(),
+ platform::errors::InvalidArgument(
+ "The input level of LoD is invalid, it should be less than LoD "
+ "size. The input level is %zu, the LoD size is %zu.",
+ level, NumLevels()));
+ PADDLE_ENFORCE_LT(elem, NumElements(level),
+ platform::errors::InvalidArgument(
+ "The input element of LoD is invalid, it should be "
+ "less than the number of elements in its level."
+ "The input element is %zu, the number of elements in "
+ "its level is %zu.",
+ elem, NumElements(level)));
return std::make_pair((lod_)[level][elem], (lod_)[level][elem + 1]);
}
@@ -131,7 +142,12 @@ class LoDTensor : public Tensor {
* Number of elements in a level.
*/
size_t NumElements(size_t level = 0) const {
- PADDLE_ENFORCE_LT(level, NumLevels());
+ PADDLE_ENFORCE_LT(
+ level, NumLevels(),
+ platform::errors::InvalidArgument(
+ "The input level of LoD is invalid, it should be less than LoD "
+ "size. The input level is %zu, the LoD size is %zu.",
+ level, NumLevels()));
// the last offset is the end of last element
return (lod_)[level].size() - 1;
}
@@ -172,7 +188,13 @@ LoDTensor LodExpand(const LoDTensor& source, const LoD& lod, size_t level,
tensor.Resize(dims);
tensor.mutable_data(place);
- PADDLE_ENFORCE_EQ(num_instances, lod_level.size() - 1);
+ PADDLE_ENFORCE_EQ(
+ num_instances, lod_level.size() - 1,
+ platform::errors::InvalidArgument(
+ "The input LoDTensor instance number should be equal to the LoD "
+ "level size minus 1."
+ "The input instance number is %zu, LoD level size is %zu.",
+ num_instances, lod_level.size()));
for (size_t ins = 0; ins < num_instances; ins++) {
for (size_t elem = lod_level[ins]; elem < lod_level[ins + 1]; elem++) {
auto slice = tensor.Slice(elem, elem + 1);
diff --git a/paddle/fluid/framework/mixed_vector.h b/paddle/fluid/framework/mixed_vector.h
index 185ebbcd3c88d7e8b7248e2af9cedc9974c86fd4..280996d34dd73e067e4e42848ea52dbbd6745caa 100644
--- a/paddle/fluid/framework/mixed_vector.h
+++ b/paddle/fluid/framework/mixed_vector.h
@@ -155,8 +155,10 @@ class Vector {
// get cuda ptr. immutable
const T *CUDAData(platform::Place place) const {
- PADDLE_ENFORCE(platform::is_gpu_place(place),
- "CUDA Data must on CUDA place");
+ PADDLE_ENFORCE_EQ(
+ platform::is_gpu_place(place), true,
+ platform::errors::Unavailable(
+ "Place mismatch, CUDA Data must be on CUDA place."));
ImmutableCUDA(place);
return reinterpret_cast(gpu_->ptr());
}
@@ -234,7 +236,8 @@ class Vector {
UnsetFlag(kDirty);
SetFlag(kDataInCUDA);
} else if (IsInCUDA() && !(place == gpu_->place())) {
- PADDLE_THROW("This situation should not happen");
+ PADDLE_THROW(
+ platform::errors::Unavailable("Unexpected data place mismatch."));
// Still dirty
} else {
// Dirty && DataInCUDA && Device is same
@@ -246,7 +249,8 @@ class Vector {
CopyCPUDataToCUDA(place);
SetFlag(kDataInCUDA);
} else if (!(place == gpu_->place())) {
- PADDLE_THROW("This situation should not happen.");
+ PADDLE_THROW(
+ platform::errors::Unavailable("Unexpected data place mismatch."));
} else {
// Not Dirty && DataInCUDA && Device is same
// Do nothing.
@@ -501,27 +505,29 @@ class CPUVector : public std::vector> {
}
const T *CUDAData(platform::Place place) const {
- PADDLE_THROW(
- "Vector::CUDAData() method is not supported in CPU-only version");
+ PADDLE_THROW(platform::errors::Unavailable(
+ "Vector::CUDAData() method is not supported in CPU-only version."));
}
T *CUDAMutableData(platform::Place place) {
- PADDLE_THROW(
+ PADDLE_THROW(platform::errors::Unavailable(
"Vector::CUDAMutableData() method is not supported in CPU-only "
- "version");
+ "version."));
}
const T *Data(platform::Place place) const {
- PADDLE_ENFORCE(
- platform::is_cpu_place(place),
- "Vector::Data() method is not supported when not in CPUPlace");
+ PADDLE_ENFORCE_EQ(
+ platform::is_cpu_place(place), true,
+ platform::errors::Unavailable(
+ "Vector::Data() method is not supported when not in CPUPlace."));
return this->data();
}
T *MutableData(platform::Place place) {
- PADDLE_ENFORCE(
- platform::is_cpu_place(place),
- "Vector::MutableData() method is not supported when not in CPUPlace");
+ PADDLE_ENFORCE_EQ(
+ platform::is_cpu_place(place), true,
+ platform::errors::Unavailable("Vector::MutableData() method is not "
+ "supported when not in CPUPlace."));
return this->data();
}
diff --git a/paddle/fluid/framework/multi_trainer.cc b/paddle/fluid/framework/multi_trainer.cc
index 4ffd9a2f9cbe036bb80512339cf832d1ea1c53bb..4ae26903e66c521f26eb3514622f03f7338c64e1 100644
--- a/paddle/fluid/framework/multi_trainer.cc
+++ b/paddle/fluid/framework/multi_trainer.cc
@@ -106,7 +106,7 @@ void MultiTrainer::InitTrainerEnv(const ProgramDesc& main_program,
}
void MultiTrainer::InitOtherEnv(const ProgramDesc& main_program) {
- if (need_dump_field_) {
+ if (need_dump_field_ || need_dump_param_) {
InitDumpEnv();
}
VLOG(3) << "init other env done.";
@@ -133,7 +133,7 @@ void MultiTrainer::Run() {
}
void MultiTrainer::Finalize() {
- if (need_dump_field_) {
+ if (need_dump_field_ || need_dump_param_) {
FinalizeDumpEnv();
}
root_scope_->DropKids();
diff --git a/paddle/fluid/framework/naive_executor.cc b/paddle/fluid/framework/naive_executor.cc
index a5de53e9d07d562c32885b1495981757f45cb5f9..be405a2cfb6b202e365aafbc46a9aea0c8e543e8 100644
--- a/paddle/fluid/framework/naive_executor.cc
+++ b/paddle/fluid/framework/naive_executor.cc
@@ -25,6 +25,9 @@
#include "paddle/fluid/framework/reader.h"
#include "paddle/fluid/framework/variable_helper.h"
#include "paddle/fluid/string/pretty_log.h"
+#ifdef PADDLE_WITH_MKLDNN
+#include "paddle/fluid/platform/mkldnn_helper.h"
+#endif
namespace paddle {
namespace framework {
@@ -51,12 +54,16 @@ void NaiveExecutor::Run() {
void NaiveExecutor::CreateVariables(const ProgramDesc &desc, int block_id,
bool persistable, Scope *scope) {
- PADDLE_ENFORCE_NOT_NULL(scope);
+ PADDLE_ENFORCE_NOT_NULL(scope,
+ platform::errors::InvalidArgument(
+ "The Scope to hold variables is nullptr."));
auto &global_block = desc.Block(block_id);
const auto *anc = scope;
- PADDLE_ENFORCE(anc->parent() != anc);
+ PADDLE_ENFORCE_NE(
+ anc->parent(), anc,
+ platform::errors::InvalidArgument("Input scope should be child scope."));
while (anc->parent()) {
anc = anc->parent();
}
@@ -101,9 +108,12 @@ void NaiveExecutor::CreateOps(const ProgramDesc &desc, int block_id,
}
LoDTensor *NaiveExecutor::FindTensor(const std::string &name) {
- PADDLE_ENFORCE(scope_, "Need to init scope first");
+ PADDLE_ENFORCE_NOT_NULL(scope_,
+ platform::errors::PreconditionNotMet(
+ "Need to init scope in NaiveExecutor firstly."));
auto *var = scope_->FindVar(name);
- PADDLE_ENFORCE(var, "No variable [%s] in the scope");
+ PADDLE_ENFORCE_NOT_NULL(var, platform::errors::NotFound(
+ "No variable [%s] in current scope.", name));
auto *tensor = const_cast(&var->Get());
return tensor;
}
@@ -122,14 +132,7 @@ NaiveExecutor::~NaiveExecutor() {
#ifdef PADDLE_WITH_MKLDNN
// Clear mkl-dnn cache,
// this is needed to have mkl-dnn unit tests working
- if (platform::is_cpu_place(place_)) {
- platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
- platform::MKLDNNDeviceContext *dev_ctx =
- (platform::MKLDNNDeviceContext *)pool.Get(place_);
- dev_ctx->ResetBlobMap();
- platform::MKLDNNDeviceContext::tls().set_cur_paddle_data_layout(
- paddle::framework::DataLayout::kNCHW);
- }
+ ClearMKLDNNCache(place_);
#endif
}
diff --git a/paddle/fluid/framework/no_need_buffer_vars_inference.cc b/paddle/fluid/framework/no_need_buffer_vars_inference.cc
index 07b84a151fe2595194e4ac536a500900e0f3b3e3..25f64838c6d39f45ecca41954f57f78f893be1ad 100644
--- a/paddle/fluid/framework/no_need_buffer_vars_inference.cc
+++ b/paddle/fluid/framework/no_need_buffer_vars_inference.cc
@@ -23,8 +23,9 @@ namespace framework {
const Attribute &InferNoNeedBufferVarsContext::GetAttr(
const std::string &name) const {
auto iter = attrs_.find(name);
- PADDLE_ENFORCE_EQ(iter != attrs_.end(), true, "Cannot find attribute %s",
- name);
+ PADDLE_ENFORCE_NE(
+ iter, attrs_.end(),
+ platform::errors::NotFound("Cannot find attribute (%s).", name));
return iter->second;
}
diff --git a/paddle/fluid/framework/no_need_buffer_vars_inference.h b/paddle/fluid/framework/no_need_buffer_vars_inference.h
index ace2b2371578789b50dc5957c2db0552c055bc6c..5d30f34090e230f1766a38992674dd9d0dc9a137 100644
--- a/paddle/fluid/framework/no_need_buffer_vars_inference.h
+++ b/paddle/fluid/framework/no_need_buffer_vars_inference.h
@@ -101,7 +101,10 @@ class InferNoNeedBufferVarsFN {
inline const std::unordered_set &operator()(
const VariableNameMap &inputs, const VariableNameMap &outputs,
const AttributeMap &attrs) const {
- PADDLE_ENFORCE_NOT_NULL(inferer_);
+ PADDLE_ENFORCE_NOT_NULL(
+ inferer_,
+ platform::errors::PreconditionNotMet(
+ "The `inferer_` of InferNoNeedBufferVarsFN is not initialized."));
StaticGraphInferNoNeedBufferVarsContext ctx(inputs, outputs, attrs);
return (*inferer_)(ctx);
}
@@ -110,7 +113,10 @@ class InferNoNeedBufferVarsFN {
const imperative::NameVarMap &inputs,
const imperative::NameVarMap &outputs,
const AttributeMap &attrs) const {
- PADDLE_ENFORCE_NOT_NULL(inferer_);
+ PADDLE_ENFORCE_NOT_NULL(
+ inferer_,
+ platform::errors::PreconditionNotMet(
+ "The `inferer_` of InferNoNeedBufferVarsFN is not initialized."));
DyGraphInferNoNeedBufferVarsContext ctx(inputs, outputs, attrs);
return (*inferer_)(ctx);
}
@@ -120,8 +126,14 @@ class InferNoNeedBufferVarsFN {
inline bool operator!() const { return inferer_ == nullptr; }
inline void Reset(const std::shared_ptr &inferer) {
- PADDLE_ENFORCE_NOT_NULL(inferer);
- PADDLE_ENFORCE_EQ(inferer_, nullptr);
+ PADDLE_ENFORCE_NOT_NULL(
+ inferer, platform::errors::InvalidArgument("The input inferer of "
+ "InferNoNeedBufferVarsFN::"
+ "Reset is nullptr."));
+ PADDLE_ENFORCE_EQ(
+ inferer_, nullptr,
+ platform::errors::AlreadyExists(
+ "The `inferer_` of InferNoNeedBufferVarsFN has been initialized."));
inferer_ = inferer;
}
diff --git a/paddle/fluid/framework/op_call_stack.cc b/paddle/fluid/framework/op_call_stack.cc
index 3a9b113ceac573c831ce39993d7e2f6df37ee5fe..80db35e0c391747cd5058cee3352fc496efa07f3 100644
--- a/paddle/fluid/framework/op_call_stack.cc
+++ b/paddle/fluid/framework/op_call_stack.cc
@@ -35,26 +35,14 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
}
std::ostringstream sout;
- std::ostringstream sout_py_trace;
// Step 1. Construct python call stack string
if (callstack) {
- sout_py_trace << "\n------------------------------------------\n";
- sout_py_trace << "Python Call Stacks (More useful to users):";
- sout_py_trace << "\n------------------------------------------\n";
+ sout << "\n\n Compile Traceback (most recent call last):";
for (auto &line : *callstack) {
- sout_py_trace << line;
+ sout << "\n " << line;
}
}
- // Step 2. Insert python traceback into err_str_
- std::size_t found = exception->err_str_.rfind(
- "\n----------------------\nError Message "
- "Summary:\n----------------------\n");
- if (found != std::string::npos) {
- exception->err_str_.insert(found, sout_py_trace.str());
- } else {
- exception->err_str_.append(sout_py_trace.str());
- }
- // Step 3. Construct final call stack & append error op name
+ // Step 2. Construct final call stack & append error op name
sout << exception->err_str_;
sout << " [operator < " << type << " > error]";
exception->err_str_ = sout.str();
diff --git a/paddle/fluid/framework/op_compatible_info.cc b/paddle/fluid/framework/op_compatible_info.cc
index 934f6828112fe72b4902a6a996af10c548c3f5ff..826e14dedb76d60c3f9f2cac5e537948c6b3c026 100644
--- a/paddle/fluid/framework/op_compatible_info.cc
+++ b/paddle/fluid/framework/op_compatible_info.cc
@@ -24,9 +24,10 @@ namespace framework {
inline std::vector ConvertStr2Int(const std::string& str_text) {
auto vec_text = string::split_string(str_text, ".");
- PADDLE_ENFORCE((vec_text.size() == 2 || vec_text.size() == 3),
- "Input[%s] is not a right version format [1.6 or 1.6.0]",
- str_text);
+ PADDLE_ENFORCE(
+ (vec_text.size() == 2 || vec_text.size() == 3),
+ platform::errors::InvalidArgument(
+ "Input[%s] is not a right version format [1.6 or 1.6.0].", str_text));
std::vector vec_res;
vec_res.reserve(3);
@@ -49,10 +50,11 @@ inline bool CompareVersion(const std::string& str_first,
auto vec_second_version = ConvertStr2Int(str_second);
// first version id
- PADDLE_ENFORCE_EQ(
- vec_first_version.size(), vec_second_version.size(),
- "version information size not equal, first is [%d] second is [%d]",
- vec_first_version.size(), vec_second_version.size());
+ PADDLE_ENFORCE_EQ(vec_first_version.size(), vec_second_version.size(),
+ platform::errors::InvalidArgument(
+ "Version information size is not equal, the first is "
+ "[%d], the second is [%d].",
+ vec_first_version.size(), vec_second_version.size()));
for (size_t i = 0; i < vec_first_version.size() - 1; ++i) {
if (vec_first_version[i] != vec_second_version[i]) {
diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc
index e490d571a699e38d4762cb1d1771fb15639e8e13..66fe71a80a7b0165a0d4afb38c89fc1fdb339190 100644
--- a/paddle/fluid/framework/op_desc.cc
+++ b/paddle/fluid/framework/op_desc.cc
@@ -700,7 +700,7 @@ void OpDesc::InferShape(const BlockDesc &block) const {
}
infer_shape(&ctx);
} catch (platform::EnforceNotMet &exception) {
- framework::InsertCallStackInfo(Type(), attrs_, &exception);
+ framework::AppendErrorOpHint(Type(), &exception);
throw std::move(exception);
} catch (...) {
std::rethrow_exception(std::current_exception());
diff --git a/paddle/fluid/framework/op_registry_test.cc b/paddle/fluid/framework/op_registry_test.cc
index c62835e51be0dca2f564fad1a9e4325cbadf5059..21d3454467603c58c9513351eba2c09ef6eeacba 100644
--- a/paddle/fluid/framework/op_registry_test.cc
+++ b/paddle/fluid/framework/op_registry_test.cc
@@ -117,7 +117,7 @@ TEST(OpRegistry, IllegalAttr) {
paddle::framework::OpRegistry::CreateOp(op_desc);
} catch (paddle::platform::EnforceNotMet& err) {
caught = true;
- std::string msg = "larger_than check fail";
+ std::string msg = "OutOfRangeError";
std::string err_msg = err.what();
ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
}
@@ -151,7 +151,7 @@ TEST(OpRegistry, CustomChecker) {
paddle::framework::OpRegistry::CreateOp(op_desc);
} catch (paddle::platform::EnforceNotMet& err) {
caught = true;
- std::string msg = "Attribute 'test_attr' is required!";
+ std::string msg = "InvalidArgumentError";
std::string err_msg = err.what();
ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
}
diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h
index 8cff6461863b21b71de1b67b3799172e54fd18c1..709f132813c7da23bc2ab77f7cfb586d4d11edbf 100644
--- a/paddle/fluid/framework/operator.h
+++ b/paddle/fluid/framework/operator.h
@@ -155,8 +155,9 @@ class OperatorBase {
bool HasAttr(const std::string& name) const { return attrs_.count(name); }
template
inline const T& Attr(const std::string& name) const {
- PADDLE_ENFORCE(attrs_.find(name) != attrs_.end(),
- "%s should be in AttributeMap", name);
+ PADDLE_ENFORCE_NE(
+ attrs_.find(name), attrs_.end(),
+ platform::errors::NotFound("(%s) is not found in AttributeMap.", name));
return BOOST_GET_CONST(T, attrs_.at(name));
}
const AttributeMap& Attrs() const { return attrs_; }
@@ -165,7 +166,9 @@ class OperatorBase {
const VariableNameMap& Outputs() const { return outputs_; }
const OpInfo& Info() const {
- PADDLE_ENFORCE_NOT_NULL(info_, "OpInfo of %s is not found", type_);
+ PADDLE_ENFORCE_NOT_NULL(
+ info_, platform::errors::NotFound(
+ "OpInfo of operator (%s) is not found.", type_));
return *info_;
}
@@ -369,7 +372,9 @@ class ExecutionContext {
#ifdef PADDLE_WITH_CUDA
const inline platform::CUDADeviceContext& cuda_device_context() const {
- PADDLE_ENFORCE_EQ(platform::is_gpu_place(device_context_.GetPlace()), true);
+ PADDLE_ENFORCE_EQ(platform::is_gpu_place(device_context_.GetPlace()), true,
+ platform::errors::PreconditionNotMet(
+ "Current device context place is not GPUPlace."));
return *reinterpret_cast(
&device_context_);
}
@@ -384,8 +389,12 @@ class ExecutionContext {
auto shared_allocation = std::shared_ptr(
allocation_ptr, deleter);
- PADDLE_ENFORCE_GE(allocation_ptr->size(),
- framework::product(dim) * sizeof(T));
+ PADDLE_ENFORCE_GE(
+ allocation_ptr->size(), framework::product(dim) * sizeof(T),
+ platform::errors::PreconditionNotMet(
+ "The data memory size(%d) is less than the tensor needed memory "
+ "size(%d).",
+ allocation_ptr->size(), framework::product(dim) * sizeof(T)));
paddle::framework::Tensor temp_tensor(
framework::ToDataType(std::type_index(typeid(T))));
diff --git a/paddle/fluid/framework/operator_test.cc b/paddle/fluid/framework/operator_test.cc
index b3ad316c9683e71440713ea26933c966842d7356..c4ce627ff1f940f1625b8650b243d64af2641612 100644
--- a/paddle/fluid/framework/operator_test.cc
+++ b/paddle/fluid/framework/operator_test.cc
@@ -16,6 +16,7 @@ limitations under the License. */
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
+#include "paddle/fluid/platform/errors.h"
#include "paddle/fluid/platform/init.h"
DECLARE_bool(enable_unused_var_check);
@@ -546,12 +547,13 @@ class GetLoDLevelTest : public OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext* ctx) const override {
- PADDLE_ENFORCE_EQ(ctx->HasInputs("X"), true,
- "Input(X) should not be null.");
- PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
- "Output(Out) should not be null.");
- PADDLE_ENFORCE_GT(ctx->GetLoDLevel("X"), 0,
- "The LoD level Input(X) should be larger than 0.");
+ OP_INOUT_CHECK(ctx->HasInputs("X"), "Input", "X", "GetLoDLevelTest");
+ OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "GetLoDLevelTest");
+
+ auto lod_level = ctx->GetLoDLevel("X");
+ PADDLE_ENFORCE_GT(lod_level, 0,
+ paddle::platform::errors::InvalidArgument(
+ "The LoD level Input(X) should be larger than 0."));
}
};
@@ -561,10 +563,8 @@ class SetLoDLevelTest : public OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext* ctx) const override {
- PADDLE_ENFORCE_EQ(ctx->HasInputs("X"), true,
- "Input(X) should not be null.");
- PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
- "Output(Out) should not be null.");
+ OP_INOUT_CHECK(ctx->HasInputs("X"), "Input", "X", "SetLoDLevelTest");
+ OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SetLoDLevelTest");
ctx->SetLoDLevel("Out", 1);
}
};
diff --git a/paddle/fluid/framework/pipeline_trainer.cc b/paddle/fluid/framework/pipeline_trainer.cc
index 379892ecfd1161fd5e5003552bc48b1153b2c412..758b728fd9cffff6867a46a6c22c86e496103b84 100644
--- a/paddle/fluid/framework/pipeline_trainer.cc
+++ b/paddle/fluid/framework/pipeline_trainer.cc
@@ -250,6 +250,7 @@ void PipelineTrainer::Finalize() {
}
}
root_scope_->DropKids();
+ SectionWorker::ResetBatchId();
}
Scope* PipelineTrainer::GetWorkerScope(int thread_id) {
diff --git a/paddle/fluid/framework/selected_rows.h b/paddle/fluid/framework/selected_rows.h
index f8a40a5d99a44bce11e4e952aaf958e9ac7823f4..5f733139419dbc1769d9eb4efe7e793f8fb2752f 100644
--- a/paddle/fluid/framework/selected_rows.h
+++ b/paddle/fluid/framework/selected_rows.h
@@ -122,7 +122,7 @@ class SelectedRows {
/*
* @brief Get the index of the key from id_to_index_ map.
*/
- inline int64_t GetIndexFromId(int64_t key) {
+ inline int64_t GetIndexFromId(int64_t key) const {
auto iter = id_to_index_.find(key);
if (iter == id_to_index_.end()) {
return -1;
diff --git a/paddle/fluid/framework/tensor.cc b/paddle/fluid/framework/tensor.cc
index df1e0fb6d5b48e0670b0bebb128578c467d19467..544c014eaf98a99b1737809f2cbad39b46fdb276 100644
--- a/paddle/fluid/framework/tensor.cc
+++ b/paddle/fluid/framework/tensor.cc
@@ -108,8 +108,15 @@ const DDim& Tensor::dims() const { return dims_; }
int64_t Tensor::numel() const { return product(dims_); }
void Tensor::ResetHolder(std::shared_ptr holder) {
+ PADDLE_ENFORCE_EQ(
+ offset_, 0,
+ platform::errors::Fatal(
+ "Only the offset is supported to zero when the holder is reset."));
if (holder_) {
- PADDLE_ENFORCE_EQ(numel() * SizeOfType(type()), holder->size());
+ PADDLE_ENFORCE_LE(
+ numel() * SizeOfType(type()) + offset_, holder->size(),
+ paddle::platform::errors::InvalidArgument(
+ "The size of Holder is not enough to store the Tensor."));
}
holder_ = holder;
}
diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc
index 853abda7345c573cee333eb69130dbefd8224845..50637a0c3d3f9c6975578e94e6ddc2c898c926e0 100644
--- a/paddle/fluid/framework/tensor_util.cc
+++ b/paddle/fluid/framework/tensor_util.cc
@@ -55,8 +55,13 @@ void TensorCopy(const Tensor& src, const platform::Place& dst_place,
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
- else if (platform::is_gpu_place(src_place) && // NOLINT
+ else if (platform::is_cuda_pinned_place(src_place) && // NOLINT
platform::is_cpu_place(dst_place)) {
+ memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
+ BOOST_GET_CONST(platform::CUDAPinnedPlace, src_place), src_ptr,
+ size);
+ } else if (platform::is_gpu_place(src_place) && // NOLINT
+ platform::is_cpu_place(dst_place)) {
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_cpu_place = BOOST_GET_CONST(platform::CPUPlace, dst_place);
auto ctx_place = ctx.GetPlace();
@@ -77,6 +82,28 @@ void TensorCopy(const Tensor& src, const platform::Place& dst_place,
auto stream =
reinterpret_cast(ctx).stream();
memory::Copy(dst_gpu_place, dst_ptr, src_cpu_place, src_ptr, size, stream);
+ } else if (platform::is_cuda_pinned_place(src_place) &&
+ platform::is_gpu_place(dst_place)) {
+ auto src_cuda_pinned_place =
+ BOOST_GET_CONST(platform::CUDAPinnedPlace, src_place);
+ auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
+ auto ctx_place = ctx.GetPlace();
+ PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx_place), true,
+ platform::errors::PreconditionNotMet(
+ "Device context place mismatch. When copying Tensor "
+ "data from CUDA Pinned memory to GPU memory, current "
+ "device context place should be GPU."));
+ auto ctx_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, ctx_place);
+ PADDLE_ENFORCE_EQ(dst_gpu_place, ctx_gpu_place,
+ platform::errors::PreconditionNotMet(
+ "The target GPU device and current device context do "
+ "not match. The target GPU device number is %d, but "
+ "device context GPU number is %d.",
+ dst_gpu_place.device, ctx_gpu_place.device));
+ auto stream =
+ reinterpret_cast(ctx).stream();
+ memory::Copy(dst_gpu_place, dst_ptr, src_cuda_pinned_place, src_ptr, size,
+ stream);
} else if (platform::is_gpu_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
@@ -148,8 +175,13 @@ void TensorCopySync(const Tensor& src, const platform::Place& dst_place,
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
- else if (platform::is_gpu_place(src_place) && // NOLINT
+ else if (platform::is_cuda_pinned_place(src_place) && // NOLINT
platform::is_cpu_place(dst_place)) {
+ memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
+ BOOST_GET_CONST(platform::CUDAPinnedPlace, src_place), src_ptr,
+ size);
+ } else if (platform::is_gpu_place(src_place) && // NOLINT
+ platform::is_cpu_place(dst_place)) {
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_cpu_place = BOOST_GET_CONST(platform::CPUPlace, dst_place);
memory::Copy(dst_cpu_place, dst_ptr, src_gpu_place, src_ptr, size, nullptr);
diff --git a/paddle/fluid/framework/trainer.cc b/paddle/fluid/framework/trainer.cc
index 99a1589200f72ef6fa33c03c0a72f27482e149e0..b033f9a99d6d9b031a9055414ea19538afc796da 100644
--- a/paddle/fluid/framework/trainer.cc
+++ b/paddle/fluid/framework/trainer.cc
@@ -22,6 +22,8 @@ void TrainerBase::SetScope(Scope* root_scope) { root_scope_ = root_scope; }
void TrainerBase::ParseDumpConfig(const TrainerDesc& desc) {
dump_fields_path_ = desc.dump_fields_path();
+ need_dump_field_ = false;
+ need_dump_param_ = false;
if (dump_fields_path_ == "") {
VLOG(2) << "dump_fields_path_ is empty";
return;
diff --git a/paddle/fluid/framework/variable_helper.cc b/paddle/fluid/framework/variable_helper.cc
index 34adbbc0abc879f305618bbd1f3a159600c3496c..67e17410a29aff435921f46eeb2691a025d5a9eb 100644
--- a/paddle/fluid/framework/variable_helper.cc
+++ b/paddle/fluid/framework/variable_helper.cc
@@ -79,5 +79,6 @@ void CopyVariable(const Variable &src_var, Variable *dst_var) {
PADDLE_THROW("unknown var type to copy");
}
}
+
} // namespace framework
} // namespace paddle
diff --git a/paddle/fluid/framework/variable_helper.h b/paddle/fluid/framework/variable_helper.h
index 5a2c267b7388f6c2de89054dc480fd74b4544bed..01a5d09e0728b2af6e9bf650f0d58af43a9a53ab 100644
--- a/paddle/fluid/framework/variable_helper.h
+++ b/paddle/fluid/framework/variable_helper.h
@@ -13,8 +13,11 @@ See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
+#include
+
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/variable.h"
+
namespace paddle {
namespace framework {
diff --git a/paddle/fluid/imperative/CMakeLists.txt b/paddle/fluid/imperative/CMakeLists.txt
index e0c2934ab32bb8135fcecf4577bae0f48bedf0ba..4d602d5c0211e221a99e0e87a3344c5a9c2a0142 100644
--- a/paddle/fluid/imperative/CMakeLists.txt
+++ b/paddle/fluid/imperative/CMakeLists.txt
@@ -1,4 +1,4 @@
-cc_library(imperative_flag SRCS flags.cc DEPS gflags)
+cc_library(imperative_flag SRCS flags.cc DEPS gflags)
cc_library(prepared_operator SRCS prepared_operator.cc DEPS proto_desc operator device_context lod_tensor selected_rows var_type_traits op_kernel_type data_transform)
cc_library(layer SRCS layer.cc DEPS prepared_operator math_function imperative_flag variable_helper op_registry)
diff --git a/paddle/fluid/imperative/basic_engine.cc b/paddle/fluid/imperative/basic_engine.cc
index 6c2f74e2712b0e7ccdce60e2b2c53ee529b52c5c..de1246883f1019bc3e6adabadbc9e071926eb772 100644
--- a/paddle/fluid/imperative/basic_engine.cc
+++ b/paddle/fluid/imperative/basic_engine.cc
@@ -33,8 +33,10 @@
namespace paddle {
namespace imperative {
-void BasicEngine::Init(VarBase* var, const detail::BackwardStrategy& strategy) {
+void BasicEngine::Init(VarBase* var, const detail::BackwardStrategy& strategy,
+ bool retain_graph) {
backward_strategy_ = strategy;
+ retain_graph_ = retain_graph;
init_node_ = var->GradVarBase()->GradNode();
var->GradVarBase()->ClearGradNode();
@@ -226,7 +228,9 @@ void BasicEngine::Execute() {
need_accu_var_list_.clear();
VLOG(3) << "Remove op after op " << cur_op.Type() << " runs";
- cur_op.ClearBackwardTrace();
+ if (!retain_graph_) {
+ cur_op.ClearBackwardTrace();
+ }
}
// Step 3: Collect ready ops
diff --git a/paddle/fluid/imperative/basic_engine.h b/paddle/fluid/imperative/basic_engine.h
index 2d517bb43d39f0321fe0a42718f20b9c457d01bb..4d25d81235098cca37491b1d8e43b481adc2fd0a 100644
--- a/paddle/fluid/imperative/basic_engine.h
+++ b/paddle/fluid/imperative/basic_engine.h
@@ -30,7 +30,8 @@ class OpBase;
class BasicEngine : public Engine {
public:
- void Init(VarBase* var, const detail::BackwardStrategy& strategy);
+ void Init(VarBase* var, const detail::BackwardStrategy& strategy,
+ bool retain_graph = false);
void Execute() override;
@@ -51,6 +52,7 @@ class BasicEngine : public Engine {
accumulators_;
std::vector>>
need_accu_var_list_;
+ bool retain_graph_;
};
} // namespace imperative
diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc
index 3e682863795724bcd3d521976c8b061b5602c8eb..ec76f58d77ed5dece46c53795b3cccfe8bfbd902 100644
--- a/paddle/fluid/imperative/layer.cc
+++ b/paddle/fluid/imperative/layer.cc
@@ -28,6 +28,11 @@
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/profiler.h"
+#ifdef PADDLE_WITH_MKLDNN
+#include "paddle/fluid/platform/mkldnn_helper.h"
+#endif
+
+DECLARE_bool(use_mkldnn);
namespace paddle {
namespace imperative {
@@ -192,6 +197,9 @@ void VarBase::ClearGradient() {
auto* grad_t =
grad_var_->MutableVar()->GetMutable();
if (grad_t->mutable_value()->IsInitialized()) {
+#ifdef PADDLE_WITH_MKLDNN
+ if (FLAGS_use_mkldnn) ClearMKLDNNCache(grad_t->place());
+#endif
grad_t->mutable_rows()->clear();
grad_t->mutable_value()->clear();
}
@@ -202,6 +210,9 @@ void VarBase::ClearGradient() {
auto* dev_ctx =
platform::DeviceContextPool::Instance().Get(grad_t->place());
operators::math::set_constant(*dev_ctx, grad_t, 0.0);
+#ifdef PADDLE_WITH_MKLDNN
+ if (FLAGS_use_mkldnn) ClearMKLDNNCache(grad_t->place());
+#endif
}
}
}
diff --git a/paddle/fluid/imperative/partial_grad_engine.cc b/paddle/fluid/imperative/partial_grad_engine.cc
index 342d046db73ea065c2605c98c06aa33d41b892e1..2bf1d2b72b2bb416d316a2dced604542059ece2e 100644
--- a/paddle/fluid/imperative/partial_grad_engine.cc
+++ b/paddle/fluid/imperative/partial_grad_engine.cc
@@ -36,6 +36,15 @@
namespace paddle {
namespace imperative {
+struct HashPair {
+ template
+ size_t operator()(const std::pair &p) const noexcept {
+ auto hash1 = std::hash{}(p.first);
+ auto hash2 = std::hash{}(p.second);
+ return hash1 ^ hash2;
+ }
+};
+
/**
* This function prunes the graph to get the ops between `output_targets`
* and `input_target_grads`.
@@ -152,8 +161,10 @@ static void GetGraphInfoBetweenTargets(
target_vars = *input_target_grads;
std::queue> op_queue;
+ std::unordered_set, HashPair> op_base_visited;
for (auto &endpoint_op : endpoint_ops) {
op_queue.emplace(endpoint_op, nullptr);
+ op_base_visited.emplace(endpoint_op, nullptr);
}
while (!op_queue.empty()) {
@@ -207,6 +218,7 @@ static void GetGraphInfoBetweenTargets(
if (pending_op) {
VLOG(10) << "Pending op of " << op->Type() << " is "
<< pending_op->Type();
+
pending_ops[op].insert(pending_op);
++op_deps[pending_op];
} else {
@@ -216,7 +228,10 @@ static void GetGraphInfoBetweenTargets(
auto iter = preceding_ops.find(op);
if (iter != preceding_ops.end()) {
for (auto &preceding_op : iter->second) {
- op_queue.emplace(preceding_op, op);
+ if (op_base_visited.count(std::make_pair(preceding_op, op)) == 0) {
+ op_queue.emplace(preceding_op, op);
+ op_base_visited.emplace(preceding_op, op);
+ }
}
}
}
diff --git a/paddle/fluid/inference/CMakeLists.txt b/paddle/fluid/inference/CMakeLists.txt
index 37ea3e5b40a65cbeb424c216aa74a75ace60ff64..9dc96fdfe8622e3e78673664637ab50970fe93c6 100644
--- a/paddle/fluid/inference/CMakeLists.txt
+++ b/paddle/fluid/inference/CMakeLists.txt
@@ -41,11 +41,16 @@ add_subdirectory(api)
# Create static inference library if needed
# All static libs in inference/api
-set(STATIC_INFERENCE_API paddle_inference_api analysis_predictor zero_copy_tensor reset_tensor_array
- analysis_config paddle_pass_builder activation_functions ${mkldnn_quantizer_cfg})
-create_static_lib(paddle_fluid ${fluid_modules} ${STATIC_INFERENCE_API})
+set(STATIC_INFERENCE_API paddle_inference_api analysis_predictor
+ zero_copy_tensor reset_tensor_array
+ analysis_config paddle_pass_builder activation_functions ${mkldnn_quantizer_cfg})
+if(WIN32)
+ cc_library(paddle_fluid DEPS ${fluid_modules} ${STATIC_INFERENCE_API})
+else()
+ create_static_lib(paddle_fluid ${fluid_modules} ${STATIC_INFERENCE_API})
+endif()
-if(NOT APPLE)
+if(NOT APPLE AND NOT WIN32)
# TODO(liuyiqu: Temporarily disable the link flag because it is not support on Mac.
set(LINK_FLAGS "-Wl,--retain-symbols-file ${CMAKE_CURRENT_SOURCE_DIR}/paddle_fluid.sym")
set_target_properties(paddle_fluid PROPERTIES LINK_FLAGS "${LINK_FLAGS}")
diff --git a/paddle/fluid/inference/analysis/argument.h b/paddle/fluid/inference/analysis/argument.h
index 2fc7f81bf8a59ca6dba3db36dfe7a9c074f03f9b..27bae7a71ea192ac08e4e87cb7bcdb8b84e29dc8 100644
--- a/paddle/fluid/inference/analysis/argument.h
+++ b/paddle/fluid/inference/analysis/argument.h
@@ -200,6 +200,10 @@ struct Argument {
DECL_ARGUMENT_FIELD(lite_ops_filter, LiteOpsFilter, std::vector);
DECL_ARGUMENT_FIELD(lite_precision_mode, LitePrecisionMode,
AnalysisConfig::Precision);
+ DECL_ARGUMENT_FIELD(lite_zero_copy, LiteZeroCopy, bool);
+
+ DECL_ARGUMENT_FIELD(use_xpu, UseXpu, bool);
+ DECL_ARGUMENT_FIELD(xpu_l3_workspace_size, XpuL3WorkspaceSize, int);
// Memory optimized related.
DECL_ARGUMENT_FIELD(enable_memory_optim, EnableMemoryOptim, bool);
diff --git a/paddle/fluid/inference/analysis/ir_pass_manager.cc b/paddle/fluid/inference/analysis/ir_pass_manager.cc
index 4a79a3cf3050380c920590355f10bb7a0d34f125..cd8d86d72938417112e17e86e5cc6dd12254a8d1 100644
--- a/paddle/fluid/inference/analysis/ir_pass_manager.cc
+++ b/paddle/fluid/inference/analysis/ir_pass_manager.cc
@@ -146,6 +146,10 @@ void IRPassManager::CreatePasses(Argument *argument,
pass->Set("predictor_id", new int(argument->predictor_id()));
pass->Set("enable_int8", new bool(enable_int8));
pass->Set("use_gpu", new bool(argument->use_gpu()));
+ pass->Set("zero_copy", new bool(argument->lite_zero_copy()));
+ pass->Set("use_xpu", new bool(argument->use_xpu()));
+ pass->Set("xpu_l3_workspace_size",
+ new int(argument->xpu_l3_workspace_size()));
}
disable_logs_ = argument->disable_logs();
if (pass_name == "fc_fuse_pass") {
diff --git a/paddle/fluid/inference/analysis/ir_passes/lite_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/lite_subgraph_pass.cc
index 91d0aec3f41fd90159958aa9035cfbf4d1c749fb..6b16a481ddedbad0956d1358de95842ea9a3a101 100644
--- a/paddle/fluid/inference/analysis/ir_passes/lite_subgraph_pass.cc
+++ b/paddle/fluid/inference/analysis/ir_passes/lite_subgraph_pass.cc
@@ -242,16 +242,33 @@ void LiteSubgraphPass::SetUpEngine(
bool use_gpu = Get("use_gpu");
bool enable_int8 = Get("enable_int8");
- lite_api::TargetType target_type = use_gpu ? TARGET(kCUDA) : TARGET(kX86);
+ bool use_xpu = Get("use_xpu");
+ int xpu_l3_workspace_size = Get("xpu_l3_workspace_size");
+
+ lite_api::TargetType target_type;
+ if (use_gpu) {
+ target_type = TARGET(kCUDA);
+ } else if (use_xpu) {
+ target_type = TARGET(kXPU);
+ } else {
+ target_type = TARGET(kX86);
+ }
+
paddle::lite_api::PrecisionType precision_type =
- enable_int8 ? PRECISION(kInt8) : PRECISION(kInt64);
+ enable_int8 ? PRECISION(kInt8) : PRECISION(kFloat);
+
serialize_params(&config.param, scope, repetitive_params);
config.model = program->Proto()->SerializeAsString();
config.valid_places = {
+ // Notice: The ordering here determines the device where the
+ // input tensor of the Lite engine is located, and then affects
+ // whether tensor sharing is feasible.
paddle::lite::Place({target_type, precision_type}),
+ paddle::lite::Place({target_type, PRECISION(kInt64)}),
paddle::lite::Place({target_type, PRECISION(kFloat)}),
paddle::lite::Place({TARGET(kHost), PRECISION(kFloat)}),
};
+ config.xpu_l3_workspace_size = xpu_l3_workspace_size;
if (dump_model) {
lite::StrToBinaryFile("./model.bin", config.model);
lite::StrToBinaryFile("./param.bin", config.param);
@@ -283,6 +300,7 @@ void LiteSubgraphPass::BuildOperator(
op_desc->SetAttr("engine_key", unique_key);
op_desc->SetAttr("enable_int8", Get("enable_int8"));
op_desc->SetAttr("use_gpu", Get("use_gpu"));
+ op_desc->SetAttr("zero_copy", Get("zero_copy"));
}
void LiteSubgraphPass::ApplyImpl(framework::ir::Graph* graph) const {
diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc
index 994f7c95352631b657edc3709f8f141cd68b3660..61886c225e6548413e6e2eb0415f596d016a988f 100644
--- a/paddle/fluid/inference/api/analysis_config.cc
+++ b/paddle/fluid/inference/api/analysis_config.cc
@@ -88,6 +88,12 @@ void AnalysisConfig::DisableFCPadding() {
Update();
}
+void AnalysisConfig::EnableXpu(int l3_workspace_size) {
+ use_xpu_ = true;
+ xpu_l3_workspace_size_ = l3_workspace_size;
+ Update();
+}
+
AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) {
#define CP_MEMBER(member__) member__ = other.member__;
@@ -132,6 +138,10 @@ AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) {
CP_MEMBER(lite_precision_mode_);
CP_MEMBER(lite_passes_filter_);
CP_MEMBER(lite_ops_filter_);
+ CP_MEMBER(lite_zero_copy_);
+
+ CP_MEMBER(use_xpu_);
+ CP_MEMBER(xpu_l3_workspace_size_);
// profile related.
CP_MEMBER(with_profile_);
@@ -344,6 +354,22 @@ void AnalysisConfig::Update() {
}
}
+ if (use_xpu_) {
+#ifndef PADDLE_WITH_XPU
+ PADDLE_THROW(platform::errors::Unavailable(
+ "You tried to use an XPU device, but Paddle was not compiled "
+ "with XPU-runtime."));
+#endif
+ if (!use_lite_) {
+ LOG(WARNING) << "Because XPU currently only works in Paddle-Lite "
+ "subgraph mode, please make sure you have enabled it.";
+ }
+ PADDLE_ENFORCE_EQ(use_gpu_, false,
+ platform::errors::Unavailable(
+ "Currently, XPU and GPU cannot be enabled in the "
+ "same analysis configuration."));
+ }
+
if (ir_debug_) {
pass_builder()->TurnOnDebug();
}
@@ -387,6 +413,8 @@ std::string AnalysisConfig::SerializeInfoCache() {
ss << cpu_math_library_num_threads_;
ss << use_lite_;
+ ss << use_xpu_;
+ ss << xpu_l3_workspace_size_;
ss << thread_local_stream_;
@@ -464,13 +492,14 @@ void AnalysisConfig::DisableGlogInfo() {
}
void AnalysisConfig::EnableLiteEngine(
- AnalysisConfig::Precision precision_mode,
+ AnalysisConfig::Precision precision_mode, bool zero_copy,
const std::vector &passes_filter,
const std::vector &ops_filter) {
use_lite_ = true;
lite_precision_mode_ = precision_mode;
lite_passes_filter_ = passes_filter;
lite_ops_filter_ = ops_filter;
+ lite_zero_copy_ = zero_copy;
Update();
}
diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc
index 1a15ecfda5d439db91d5dca3bd9e3bd8fd1a6507..67e3e237bbf2f08418dfcc072e806ae47ec32766 100644
--- a/paddle/fluid/inference/api/analysis_predictor.cc
+++ b/paddle/fluid/inference/api/analysis_predictor.cc
@@ -465,6 +465,9 @@ void AnalysisPredictor::PrepareArgument() {
argument_.SetLitePrecisionMode(config_.lite_precision_mode_);
argument_.SetLitePassesFilter(config_.lite_passes_filter_);
argument_.SetLiteOpsFilter(config_.lite_ops_filter_);
+ argument_.SetLiteZeroCopy(config_.lite_zero_copy_);
+ argument_.SetUseXpu(config_.use_xpu_);
+ argument_.SetXpuL3WorkspaceSize(config_.xpu_l3_workspace_size_);
LOG(INFO) << "Lite subgraph engine is enabled";
}
diff --git a/paddle/fluid/inference/api/demo_ci/run.sh b/paddle/fluid/inference/api/demo_ci/run.sh
index bfa273d4468dbb8e43995bfaadfa6dea932fd7c4..d8d9e2187815dcad78ad4ea6be10ad677940bf39 100755
--- a/paddle/fluid/inference/api/demo_ci/run.sh
+++ b/paddle/fluid/inference/api/demo_ci/run.sh
@@ -72,7 +72,7 @@ if [ $(echo `uname` | grep "Win") != "" ]; then
-DWITH_MKL=$TURN_ON_MKL \
-DDEMO_NAME=simple_on_word2vec \
-DWITH_GPU=$TEST_GPU_CPU \
- -DWITH_STATIC_LIB=ON
+ -DWITH_STATIC_LIB=OFF
msbuild /maxcpucount /property:Configuration=Release cpp_inference_demo.sln
Release/simple_on_word2vec.exe \
--dirname=$DATA_DIR/word2vec/word2vec.inference.model \
@@ -88,7 +88,7 @@ if [ $(echo `uname` | grep "Win") != "" ]; then
-DWITH_MKL=$TURN_ON_MKL \
-DDEMO_NAME=vis_demo \
-DWITH_GPU=$TEST_GPU_CPU \
- -DWITH_STATIC_LIB=ON
+ -DWITH_STATIC_LIB=OFF
msbuild /maxcpucount /property:Configuration=Release cpp_inference_demo.sln
for vis_demo_name in $vis_demo_list; do
Release/vis_demo.exe \
diff --git a/paddle/fluid/inference/api/paddle_analysis_config.h b/paddle/fluid/inference/api/paddle_analysis_config.h
index ec7b08b306707484619d126d4983633aeec9b601..6a31ff281c68e3675d35c14059a453455ef398df 100644
--- a/paddle/fluid/inference/api/paddle_analysis_config.h
+++ b/paddle/fluid/inference/api/paddle_analysis_config.h
@@ -176,6 +176,8 @@ struct PD_INFER_DECL AnalysisConfig {
///
///
void DisableGpu();
+
+ void EnableXpu(int l3_workspace_size = 0xfffc00);
///
/// \brief A boolean state telling whether the GPU is turned on.
///
@@ -319,6 +321,7 @@ struct PD_INFER_DECL AnalysisConfig {
///
void EnableLiteEngine(
AnalysisConfig::Precision precision_mode = Precision::kFloat32,
+ bool zero_copy = false,
const std::vector& passes_filter = {},
const std::vector& ops_filter = {});
@@ -579,8 +582,11 @@ struct PD_INFER_DECL AnalysisConfig {
std::vector lite_passes_filter_;
std::vector lite_ops_filter_;
Precision lite_precision_mode_;
+ bool lite_zero_copy_;
bool thread_local_stream_{false};
+ bool use_xpu_{false};
+ int xpu_l3_workspace_size_;
// mkldnn related.
int mkldnn_cache_capacity_{0};
diff --git a/paddle/fluid/inference/lite/CMakeLists.txt b/paddle/fluid/inference/lite/CMakeLists.txt
index decf2d830fe0a77c69c80c071248b04c7fdb2f9d..fd513b59588f82716900d4d48e9aac036085baa9 100644
--- a/paddle/fluid/inference/lite/CMakeLists.txt
+++ b/paddle/fluid/inference/lite/CMakeLists.txt
@@ -1,6 +1,9 @@
+if(XPU_SDK_ROOT)
+ set(XPU_DEPS xpuapi xpurt)
+endif()
+
cc_library(lite_op_teller SRCS op_teller.cc DEPS lite_full_static framework_proto device_context boost xxhash)
-cc_library(lite_engine SRCS engine.cc DEPS lite_full_static framework_proto)
+cc_library(lite_engine SRCS engine.cc DEPS lite_full_static framework_proto ${XPU_DEPS})
cc_library(lite_tensor_utils SRCS tensor_utils.cc DEPS memcpy lite_full_static framework_proto boost device_context)
-
cc_test(test_lite_engine SRCS test_engine.cc DEPS lite_engine protobuf framework_proto glog gtest analysis)
cc_test(test_lite_tensor_utils SRCS test_tensor_utils.cc DEPS lite_engine lite_tensor_utils)
diff --git a/paddle/fluid/inference/lite/engine.cc b/paddle/fluid/inference/lite/engine.cc
index fb3b6e460d5bb23133de1d6a8a106530043cd99a..8e88c94493952ff257ef69bf73f8edebb6ba2eee 100644
--- a/paddle/fluid/inference/lite/engine.cc
+++ b/paddle/fluid/inference/lite/engine.cc
@@ -16,8 +16,11 @@
#define LITE_WITH_CUDA 1
#endif
-#include "paddle/fluid/inference/lite/engine.h"
+#ifdef PADDLE_WITH_XPU
+#define LITE_WITH_XPU 1
+#endif
+#include "paddle/fluid/inference/lite/engine.h"
#include "lite/api/paddle_use_passes.h"
namespace paddle {
@@ -39,10 +42,17 @@ paddle::lite::Predictor* EngineManager::Get(const std::string& name) const {
paddle::lite::Predictor* EngineManager::Create(const std::string& name,
const EngineConfig& cfg) {
- auto* p = new paddle::lite::Predictor();
+ if (cfg.valid_places.front().target == TARGET(kCUDA)) {
#ifdef PADDLE_WITH_CUDA
- paddle::lite::Env::Init();
+ paddle::lite::Env::Init();
#endif
+ } else if (cfg.valid_places.front().target == TARGET(kXPU)) {
+#ifdef PADDLE_WITH_XPU
+ paddle::lite::TargetWrapper::workspace_l3_size_per_thread =
+ cfg.xpu_l3_workspace_size;
+#endif
+ }
+ auto* p = new paddle::lite::Predictor();
p->Build("", cfg.model, cfg.param, cfg.valid_places, cfg.neglected_passes,
cfg.model_type, cfg.model_from_memory);
engines_[name].reset(p);
diff --git a/paddle/fluid/inference/lite/engine.h b/paddle/fluid/inference/lite/engine.h
index 5f11c51952bd3ce0bb0e09121dbd5e633c6fd3ae..345eb682e9fe81d4ec67a31082c1d347a694fd96 100644
--- a/paddle/fluid/inference/lite/engine.h
+++ b/paddle/fluid/inference/lite/engine.h
@@ -26,6 +26,7 @@
#include "lite/api/paddle_place.h"
#include "lite/core/context.h"
#include "lite/core/device_info.h"
+#include "lite/core/memory.h"
#include "lite/core/op_registry.h"
#include "lite/core/tensor.h"
#pragma GCC diagnostic pop
@@ -42,6 +43,7 @@ struct EngineConfig {
std::vector neglected_passes;
lite_api::LiteModelType model_type{lite_api::LiteModelType::kProtobuf};
bool model_from_memory{true};
+ size_t xpu_l3_workspace_size;
};
class EngineManager {
diff --git a/paddle/fluid/inference/lite/tensor_utils.cc b/paddle/fluid/inference/lite/tensor_utils.cc
index 59087c6fec20360ef4a8f8a34aa810c3328d6e0d..d79a041ccf8a1611247b65b034c03940eabfcccd 100644
--- a/paddle/fluid/inference/lite/tensor_utils.cc
+++ b/paddle/fluid/inference/lite/tensor_utils.cc
@@ -14,8 +14,10 @@
#include "paddle/fluid/inference/lite/tensor_utils.h"
#include