Skip to content

Commit d0b7134

Browse files
authored
enhance DebugStringEx (#12949)
1 parent eca4563 commit d0b7134

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

paddle/fluid/framework/operator.cc

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ static DDim GetDims(const Scope& scope, const std::string& name,
7474
}
7575
}
7676

77+
static bool VarInited(const Scope& scope, const std::string& name) {
78+
Variable* var = scope.FindVar(name);
79+
if (var == nullptr) return false;
80+
return var->IsInitialized();
81+
}
82+
7783
static std::string GetDtype(const Scope& scope, const std::string& name) {
7884
Variable* var = scope.FindVar(name);
7985
if (var == nullptr) {
@@ -87,8 +93,12 @@ static std::string GetDtype(const Scope& scope, const std::string& name) {
8793
}
8894
return DataTypeToString(ToDataType(tensor.type()));
8995
} else if (var->IsType<SelectedRows>()) {
90-
return DataTypeToString(
91-
ToDataType(var->Get<SelectedRows>().value().type()));
96+
auto tensor = var->Get<SelectedRows>().value();
97+
if (UNLIKELY(!tensor.IsInitialized())) {
98+
return "uninited";
99+
} else {
100+
return DataTypeToString(ToDataType(tensor.type()));
101+
}
92102
} else {
93103
return "";
94104
}
@@ -197,16 +207,21 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const {
197207
auto& input = *it;
198208
ss << input.first << "[";
199209
for (size_t i = 0; i < input.second.size(); ++i) {
200-
ss << input.second[i];
210+
auto var_name = input.second[i];
211+
ss << var_name;
201212
if (scope) {
202-
int row_size = GetRowSize(*scope, input.second[i]);
203-
if (row_size >= 0) {
204-
ss << "[row_size=" << row_size << "]";
213+
if (!VarInited(*scope, var_name)) {
214+
ss << "[uninited]";
215+
} else {
216+
int row_size = GetRowSize(*scope, var_name);
217+
if (row_size >= 0) {
218+
ss << "[row_size=" << row_size << "]";
219+
}
220+
std::string dtype = GetDtype(*scope, var_name);
221+
ss << ":" << dtype;
222+
ss << "[" << GetDims(*scope, var_name, true) << "]";
223+
ss << "(" << GetLoD(*scope, var_name) << ")";
205224
}
206-
std::string dtype = GetDtype(*scope, input.second[i]);
207-
ss << ":" << dtype;
208-
ss << "[" << GetDims(*scope, input.second[i], true) << "]";
209-
ss << "(" << GetLoD(*scope, input.second[i]) << ")";
210225
}
211226
if (i != input.second.size() - 1) {
212227
ss << ", ";
@@ -223,14 +238,19 @@ std::string OperatorBase::DebugStringEx(const Scope* scope) const {
223238
auto& output = *it;
224239
ss << output.first << "[";
225240
for (size_t i = 0; i < output.second.size(); ++i) {
226-
ss << output.second[i];
241+
auto var_name = output.second[i];
242+
ss << var_name;
227243
if (scope) {
228-
int row_size = GetRowSize(*scope, output.second[i]);
229-
if (row_size >= 0) {
230-
ss << "[row_size=" << row_size << "]";
244+
if (!VarInited(*scope, var_name)) {
245+
ss << "[uninited]";
246+
} else {
247+
int row_size = GetRowSize(*scope, output.second[i]);
248+
if (row_size >= 0) {
249+
ss << "[row_size=" << row_size << "]";
250+
}
251+
ss << "[" << GetDims(*scope, var_name, true) << "]";
252+
ss << "(" << GetLoD(*scope, var_name) << ")";
231253
}
232-
ss << "[" << GetDims(*scope, output.second[i], true) << "]";
233-
ss << "(" << GetLoD(*scope, output.second[i]) << ")";
234254
}
235255
if (i != output.second.size() - 1) {
236256
ss << ", ";

0 commit comments

Comments
 (0)