From a5b32637825e19f7527c09878ba2994314929d54 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 25 Sep 2020 20:51:21 +0800 Subject: [PATCH] Refine error msg in paddle/fluid/imperative (#27521) * refine err msg * follow comments --- .../fluid/imperative/gradient_accumulator.cc | 12 +++- .../imperative/jit/program_desc_tracer.cc | 13 ++-- paddle/fluid/imperative/nccl_context.cc | 59 +++++++++++++------ 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/imperative/gradient_accumulator.cc b/paddle/fluid/imperative/gradient_accumulator.cc index 7caeb4378c..07f1868b7f 100644 --- a/paddle/fluid/imperative/gradient_accumulator.cc +++ b/paddle/fluid/imperative/gradient_accumulator.cc @@ -13,9 +13,11 @@ // limitations under the License. #include "paddle/fluid/imperative/gradient_accumulator.h" + #include #include #include + #include "paddle/fluid/framework/framework.pb.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/selected_rows.h" @@ -136,9 +138,13 @@ void TensorAdd(const framework::Variable& src, framework::Variable* dst) { return; } - PADDLE_ENFORCE_EQ(dst_tensor->numel() == numel, true, - "dst_numel %d vs. src_numel %d", dst_tensor->numel(), - numel); + PADDLE_ENFORCE_EQ( + dst_tensor->numel(), numel, + platform::errors::PreconditionNotMet( + "The number of elements of source tensor and destination tensor " + "should be equal, but got the number of elements of source tensor is " + "%zu and the number of elements of destination tensor is %zu.", + numel, dst_tensor->numel())); auto data_type = src_tensor.type(); auto place = src_tensor.place(); diff --git a/paddle/fluid/imperative/jit/program_desc_tracer.cc b/paddle/fluid/imperative/jit/program_desc_tracer.cc index 9f4cf713f7..59ff5b4eae 100644 --- a/paddle/fluid/imperative/jit/program_desc_tracer.cc +++ b/paddle/fluid/imperative/jit/program_desc_tracer.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/fluid/imperative/jit/program_desc_tracer.h" + #include #include @@ -203,7 +204,8 @@ TracedProgramTuple ProgramDescTracer::CreateProgramDesc( void ProgramDescTracer::InsertVarIfNotExist( const std::shared_ptr &new_var, bool is_input) { - PADDLE_ENFORCE_NOT_NULL(new_var); + PADDLE_ENFORCE_NOT_NULL(new_var, platform::errors::InvalidArgument( + "The variable to insert is NULL.")); if (vars_.count(new_var) != 0) return; auto new_var_desc = new framework::VarDesc(""); @@ -220,7 +222,9 @@ void ProgramDescTracer::InsertVarIfNotExist( } const auto &inner_var = new_var->Var(); - PADDLE_ENFORCE_EQ(inner_var.IsInitialized(), true); + PADDLE_ENFORCE_EQ(inner_var.IsInitialized(), true, + platform::errors::InvalidArgument( + "The variable to insert is not initialized.")); if (inner_var.IsType()) { const auto &tensor = inner_var.Get(); new_var_desc->SetType(framework::proto::VarType::LOD_TENSOR); @@ -232,8 +236,9 @@ void ProgramDescTracer::InsertVarIfNotExist( new_var_desc->SetDataType(framework::proto::VarType::FP32); } } else { - PADDLE_THROW("Not support variable type %s", - framework::ToTypeName(inner_var.Type())); + PADDLE_THROW(platform::errors::InvalidArgument( + "Not support variable type %s.", + framework::ToTypeName(inner_var.Type()))); } } diff --git a/paddle/fluid/imperative/nccl_context.cc b/paddle/fluid/imperative/nccl_context.cc index 115078e7ea..c8fd31fcbf 100644 --- a/paddle/fluid/imperative/nccl_context.cc +++ b/paddle/fluid/imperative/nccl_context.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/fluid/imperative/nccl_context.h" + #include "paddle/fluid/platform/collective_helper.h" namespace paddle { @@ -21,8 +22,10 @@ namespace imperative { void NCCLParallelContext::RecvNCCLID(const std::string &ep, ncclUniqueId *nccl_id) { auto addr = paddle::string::Split(ep, ':'); - PADDLE_ENFORCE_EQ(addr.size(), 2UL, - "The endpoint should contain host and port: %s", ep); + PADDLE_ENFORCE_EQ( + addr.size(), 2UL, + platform::errors::InvalidArgument( + "The endpoint should contain host and port, but got %s.", ep)); std::string host = addr[0]; int port = std::stoi(addr[1]); @@ -32,27 +35,41 @@ void NCCLParallelContext::RecvNCCLID(const std::string &ep, char buffer[1024] = {0}; int opt = 0; // creating socket fd - if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) - PADDLE_THROW("create server fd failed"); - if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) - PADDLE_THROW("set socket opt failed"); + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + PADDLE_THROW( + platform::errors::Unavailable("Create server file descriptor failed.")); + } + + if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { + PADDLE_THROW(platform::errors::Unavailable("Set socket options failed.")); + } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(port); - if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) - PADDLE_THROW("binding failed on ep: %s", ep); + if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { + PADDLE_THROW( + platform::errors::Unavailable("Bind on endpoint %s failed.", ep)); + } + VLOG(3) << "listening on: " << ep; - if (listen(server_fd, 3) < 0) PADDLE_THROW("listen on server fd failed"); + if (listen(server_fd, 3) < 0) { + PADDLE_THROW(platform::errors::Unavailable( + "Listen on server file descriptor failed.")); + } if ((new_socket = accept(server_fd, reinterpret_cast(&address), - reinterpret_cast(&addrlen))) < 0) - PADDLE_THROW("accept the new socket fd failed"); + reinterpret_cast(&addrlen))) < 0) { + PADDLE_THROW(platform::errors::Unavailable( + "Accept the new socket file descriptor failed.")); + } + + if (read(new_socket, buffer, 1024) < 0) { + PADDLE_THROW(platform::errors::Unavailable("Read from socket failed.")); + } - if (read(new_socket, buffer, 1024) < 0) - PADDLE_THROW("reading the ncclUniqueId from socket failed"); VLOG(3) << "recevived the ncclUniqueId"; memcpy(nccl_id, buffer, NCCL_UNIQUE_ID_BYTES); @@ -63,8 +80,10 @@ void NCCLParallelContext::RecvNCCLID(const std::string &ep, void NCCLParallelContext::SendNCCLID(const std::string &ep, ncclUniqueId *nccl_id) { auto addr = paddle::string::Split(ep, ':'); - PADDLE_ENFORCE_EQ(addr.size(), 2UL, - "The endpoint should contain host and port: %s", ep); + PADDLE_ENFORCE_EQ( + addr.size(), 2UL, + platform::errors::InvalidArgument( + "The endpoint should contain host and port, but got %s.", ep)); std::string host = addr[0]; int port = std::stoi(addr[1]); // struct sockaddr_in address; @@ -73,15 +92,17 @@ void NCCLParallelContext::SendNCCLID(const std::string &ep, char buffer[1024] = {0}; memcpy(buffer, nccl_id, NCCL_UNIQUE_ID_BYTES); - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) - PADDLE_THROW("create socket failed"); + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + PADDLE_THROW(platform::errors::Unavailable("Create socket failed.")); + } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); - if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) - PADDLE_THROW("invalied address: %s", ep); + if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) { + PADDLE_THROW(platform::errors::Unavailable("Open address %s failed.", ep)); + } int try_times = 0; while (true) { -- GitLab