From 23df6c447868e9c5d1fe2f0b04342ae377b7fafb Mon Sep 17 00:00:00 2001 From: Qiao Longfei Date: Fri, 12 Jan 2018 15:37:15 +0800 Subject: [PATCH] Add get lod for debug (#7375) * add GetLoD for debug * add LoDToString * optimize if * typo * add lod_tensor to operator's dependency --- paddle/framework/CMakeLists.txt | 2 +- paddle/framework/lod_tensor.cc | 6 ++++++ paddle/framework/lod_tensor.h | 2 ++ paddle/framework/operator.cc | 25 ++++++++++++++++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/paddle/framework/CMakeLists.txt b/paddle/framework/CMakeLists.txt index ed5f6310f4..597ea959f2 100644 --- a/paddle/framework/CMakeLists.txt +++ b/paddle/framework/CMakeLists.txt @@ -47,7 +47,7 @@ cc_test(op_proto_maker_test SRCS op_proto_maker_test.cc DEPS op_proto_maker) cc_library(op_info SRCS op_info.cc DEPS attribute framework_proto) cc_library(shape_inference SRCS shape_inference.cc DEPS ddim attribute device_context) cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope glog - shape_inference data_transform) + shape_inference data_transform lod_tensor) cc_test(operator_test SRCS operator_test.cc DEPS operator op_registry init) cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS shape_inference op_info operator glog) diff --git a/paddle/framework/lod_tensor.cc b/paddle/framework/lod_tensor.cc index 7ae94c6465..87a57d0951 100644 --- a/paddle/framework/lod_tensor.cc +++ b/paddle/framework/lod_tensor.cc @@ -69,6 +69,12 @@ std::ostream &operator<<(std::ostream &os, const LoDTensor &t) { return os; } +std::string LoDToString(const LoD &lod) { + std::ostringstream stream; + stream << lod; + return stream.str(); +} + LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin, size_t elem_end) { PADDLE_ENFORCE_LT(level, in.size()); diff --git a/paddle/framework/lod_tensor.h b/paddle/framework/lod_tensor.h index 37753f5f4d..88ea78f268 100644 --- a/paddle/framework/lod_tensor.h +++ b/paddle/framework/lod_tensor.h @@ -60,6 +60,8 @@ using LoD = std::vector>; std::ostream& operator<<(std::ostream& os, const LoD& lod); std::ostream& operator<<(std::ostream& os, const LoDTensor& t); +std::string LoDToString(const LoD& lod); + LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin, size_t elem_end); /* diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc index 7756a52ca9..be1373dc2a 100644 --- a/paddle/framework/operator.cc +++ b/paddle/framework/operator.cc @@ -80,7 +80,9 @@ static DDim GetDims(const Scope& scope, const std::string& name) { Variable* var = scope.FindVar(name); if (var == nullptr) { return DDim({-1}); - } else if (var->IsType()) { + } + + if (var->IsType()) { return var->Get().dims(); } else if (var->IsType()) { return var->Get().GetCompleteDims(); @@ -89,6 +91,21 @@ static DDim GetDims(const Scope& scope, const std::string& name) { } } +static LoD GetLoD(const Scope& scope, const std::string& name) { + Variable* var = scope.FindVar(name); + auto default_lod = LoD({{}}); + + if (var == nullptr) { + return default_lod; + } + + if (var->IsType()) { + return var->Get().lod(); + } else { + return default_lod; + } +} + std::string OperatorBase::Input(const std::string& name) const { auto& ins = Inputs(name); PADDLE_ENFORCE_LE(ins.size(), 1UL, @@ -130,7 +147,8 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const { for (size_t i = 0; i < input.second.size(); ++i) { ss << input.second[i]; if (scope) { - ss << "(" << GetDims(*scope, input.second[i]) << ")"; + ss << "[" << GetDims(*scope, input.second[i]) << "]"; + ss << "(" << GetLoD(*scope, input.second[i]) << ")"; } if (i != input.second.size() - 1) { ss << ", "; @@ -149,7 +167,8 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const { for (size_t i = 0; i < output.second.size(); ++i) { ss << output.second[i]; if (scope) { - ss << "(" << GetDims(*scope, output.second[i]) << ")"; + ss << "[" << GetDims(*scope, output.second[i]) << "]"; + ss << "(" << GetLoD(*scope, output.second[i]) << ")"; } if (i != output.second.size() - 1) { ss << ", "; -- GitLab