From 407f883f5adb4aa462fe5ff25a64dd419ba3d974 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 29 Nov 2019 11:22:41 +0800 Subject: [PATCH] Add SelectedRows support for dygraph DebugString (#21415) * add selected rows support for debug string, test=develop * refactor unittest of debug string, test=develop * polish unittest name, test=develop --- paddle/fluid/imperative/layer.cc | 16 +++++ paddle/fluid/imperative/tests/test_layer.cc | 78 ++++++++++++++------- 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index 8a25b4220c4..c309c671b0d 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -140,6 +140,22 @@ static std::string DebugString( ss << "NOT_INITED"; } ss << ">"; + } else if (var.IsType()) { + ss << "SelectedRows<"; + auto& selected_rows = var.Get(); + auto& tensor = selected_rows.value(); + auto& rows = selected_rows.rows(); + if (tensor.IsInitialized()) { + ss << framework::DataTypeToString(tensor.type()) << ", "; + ss << tensor.place() << ", "; + ss << "height(" << selected_rows.height() << "), rows("; + std::for_each(rows.cbegin(), rows.cend(), + [&ss](const int64_t r) { ss << r << " "; }); + ss << "), dims(" << tensor.dims() << ")"; + } else { + ss << "NOT_INITED"; + } + ss << ">"; } else { ss << "UNRESOLVED_TYPE"; } diff --git a/paddle/fluid/imperative/tests/test_layer.cc b/paddle/fluid/imperative/tests/test_layer.cc index be7e6fe160d..e2f26ea8acc 100644 --- a/paddle/fluid/imperative/tests/test_layer.cc +++ b/paddle/fluid/imperative/tests/test_layer.cc @@ -61,34 +61,62 @@ std::string LayerDebugString(const std::string& op_type, const NameVarBaseMap& ins, const NameVarBaseMap& outs); -TEST(test_layer, test_debug_string_test_debug_Test) { +TEST(test_layer, test_debug_string) { + platform::CPUPlace place; std::shared_ptr vin( new imperative::VarBase(false, "vin")); - std::shared_ptr vin_error( - new imperative::VarBase(false, "vin_error")); - std::shared_ptr vout( - new imperative::VarBase(false, "vout")); - std::shared_ptr vout_error( - new imperative::VarBase(false, "vout_error")); - vin_error->MutableVar()->GetMutable(); - vout->MutableVar()->GetMutable(); - vout_error->MutableVar()->GetMutable(); var_pair in_pair = var_pair("X", vb_vector(1, vin)); - vb_vector vb_in_error = {vin_error, nullptr}; - var_pair vin_error_pair = var_pair("X", vb_in_error); - var_pair out_pair = var_pair("Out", vb_vector(1, vout)); - var_pair vout_error_pair = var_pair("Out2", vb_vector(1, vout_error)); - imperative::NameVarBaseMap ins = {in_pair}; - imperative::NameVarBaseMap ins_error = {vin_error_pair}; - imperative::NameVarBaseMap outs = {out_pair}; - imperative::NameVarBaseMap outs_error = {vout_error_pair}; - ASSERT_NO_FATAL_FAILURE(LayerDebugString("test_op", ins, outs)); - std::string res = LayerDebugString("test_op", ins, outs_error); - ASSERT_TRUE(res.find("UNRESOLVED_TYPE") != std::string::npos); - std::string res2 = LayerDebugString("test_op", ins_error, outs_error); - VLOG(3) << res2; - ASSERT_TRUE(res2.find("NOT_INITED") != std::string::npos); - ASSERT_TRUE(res2.find("NULL") != std::string::npos); + + auto test_func = [&](std::shared_ptr& vout) { + var_pair out_pair = var_pair("Out", vb_vector(1, vout)); + imperative::NameVarBaseMap ins = {in_pair}; + imperative::NameVarBaseMap outs = {out_pair}; + return LayerDebugString("test_op", ins, outs); + }; + + // 1. test null + std::shared_ptr null_out(nullptr); + std::string res_null = test_func(null_out); + ASSERT_TRUE(res_null.find("NULL") != std::string::npos); + + // 2. test uninit var + std::shared_ptr un_init_out( + new imperative::VarBase(false, "un_init_out")); + std::string res_un_init = test_func(un_init_out); + ASSERT_TRUE(res_un_init.find("NOT_INITED_VAR") != std::string::npos); + + // 3. test unresolved type + std::shared_ptr ut_out( + new imperative::VarBase(false, "ut_out")); + ut_out->MutableVar()->GetMutable(); + std::string res_ut = test_func(ut_out); + ASSERT_TRUE(res_ut.find("UNRESOLVED_TYPE") != std::string::npos); + + // 4. test uninit lod tensor + std::shared_ptr lod_tensor( + new imperative::VarBase(false, "lod_tensor")); + auto tensor_l = lod_tensor->MutableVar()->GetMutable(); + std::string res_ui_lod_t = test_func(lod_tensor); + ASSERT_TRUE(res_ui_lod_t.find("NOT_INITED") != std::string::npos); + + // 5. test init lod tensor + tensor_l->mutable_data(place); + std::string res_lod_t = test_func(lod_tensor); + ASSERT_TRUE(res_lod_t.find("LoDTensor") != std::string::npos); + + // 6. test uninit selected rows + std::shared_ptr selected_rows( + new imperative::VarBase(false, "selected_rows")); + auto tensor_sr = selected_rows->MutableVar() + ->GetMutable() + ->mutable_value(); + std::string res_ui_sr = test_func(selected_rows); + ASSERT_TRUE(res_ui_sr.find("NOT_INITED") != std::string::npos); + + // 7. test init selected rows + tensor_sr->mutable_data(place); + std::string res_sr = test_func(selected_rows); + ASSERT_TRUE(res_sr.find("SelectedRows") != std::string::npos); } TEST(test_layer, test_clear_backward_info) { -- GitLab