Skip to content

Commit 23df6c4

Browse files
authored
Add get lod for debug (#7375)
* add GetLoD for debug * add LoDToString * optimize if * typo * add lod_tensor to operator's dependency
1 parent 3423022 commit 23df6c4

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

paddle/framework/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ cc_test(op_proto_maker_test SRCS op_proto_maker_test.cc DEPS op_proto_maker)
4747
cc_library(op_info SRCS op_info.cc DEPS attribute framework_proto)
4848
cc_library(shape_inference SRCS shape_inference.cc DEPS ddim attribute device_context)
4949
cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope glog
50-
shape_inference data_transform)
50+
shape_inference data_transform lod_tensor)
5151
cc_test(operator_test SRCS operator_test.cc DEPS operator op_registry init)
5252
cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS shape_inference op_info operator glog)
5353

paddle/framework/lod_tensor.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
6969
return os;
7070
}
7171

72+
std::string LoDToString(const LoD &lod) {
73+
std::ostringstream stream;
74+
stream << lod;
75+
return stream.str();
76+
}
77+
7278
LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin,
7379
size_t elem_end) {
7480
PADDLE_ENFORCE_LT(level, in.size());

paddle/framework/lod_tensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ using LoD = std::vector<Vector<size_t>>;
6060
std::ostream& operator<<(std::ostream& os, const LoD& lod);
6161
std::ostream& operator<<(std::ostream& os, const LoDTensor& t);
6262

63+
std::string LoDToString(const LoD& lod);
64+
6365
LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
6466
size_t elem_end);
6567
/*

paddle/framework/operator.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ static DDim GetDims(const Scope& scope, const std::string& name) {
8080
Variable* var = scope.FindVar(name);
8181
if (var == nullptr) {
8282
return DDim({-1});
83-
} else if (var->IsType<LoDTensor>()) {
83+
}
84+
85+
if (var->IsType<LoDTensor>()) {
8486
return var->Get<LoDTensor>().dims();
8587
} else if (var->IsType<SelectedRows>()) {
8688
return var->Get<SelectedRows>().GetCompleteDims();
@@ -89,6 +91,21 @@ static DDim GetDims(const Scope& scope, const std::string& name) {
8991
}
9092
}
9193

94+
static LoD GetLoD(const Scope& scope, const std::string& name) {
95+
Variable* var = scope.FindVar(name);
96+
auto default_lod = LoD({{}});
97+
98+
if (var == nullptr) {
99+
return default_lod;
100+
}
101+
102+
if (var->IsType<LoDTensor>()) {
103+
return var->Get<LoDTensor>().lod();
104+
} else {
105+
return default_lod;
106+
}
107+
}
108+
92109
std::string OperatorBase::Input(const std::string& name) const {
93110
auto& ins = Inputs(name);
94111
PADDLE_ENFORCE_LE(ins.size(), 1UL,
@@ -130,7 +147,8 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const {
130147
for (size_t i = 0; i < input.second.size(); ++i) {
131148
ss << input.second[i];
132149
if (scope) {
133-
ss << "(" << GetDims(*scope, input.second[i]) << ")";
150+
ss << "[" << GetDims(*scope, input.second[i]) << "]";
151+
ss << "(" << GetLoD(*scope, input.second[i]) << ")";
134152
}
135153
if (i != input.second.size() - 1) {
136154
ss << ", ";
@@ -149,7 +167,8 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const {
149167
for (size_t i = 0; i < output.second.size(); ++i) {
150168
ss << output.second[i];
151169
if (scope) {
152-
ss << "(" << GetDims(*scope, output.second[i]) << ")";
170+
ss << "[" << GetDims(*scope, output.second[i]) << "]";
171+
ss << "(" << GetLoD(*scope, output.second[i]) << ")";
153172
}
154173
if (i != output.second.size() - 1) {
155174
ss << ", ";

0 commit comments

Comments
 (0)