From 172a1c377629fb06e42f1998ae6c9c044ac8edf9 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Tue, 17 Jan 2023 17:25:49 +0000 Subject: [PATCH 1/5] constant folding/trt subgrash pass debug --- .../framework/ir/constant_folding_pass.cc | 40 ++++++++++++++++++- .../ir_passes/tensorrt_subgraph_pass.cc | 35 ++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/framework/ir/constant_folding_pass.cc b/paddle/fluid/framework/ir/constant_folding_pass.cc index feba2242ad68e..2bf099694cd3e 100644 --- a/paddle/fluid/framework/ir/constant_folding_pass.cc +++ b/paddle/fluid/framework/ir/constant_folding_pass.cc @@ -119,6 +119,7 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { local_x_tensor->Resize(global_persis_x_tensor->dims()); *local_x_tensor = *global_persis_x_tensor; } + std::cout << "#####" << std::endl; op = paddle::framework::OpRegistry::CreateOp(*op_node->Op()); remove_nodes.emplace(op_node); @@ -135,20 +136,55 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { auto out_desc = out_node->Var(); auto out_name = out_desc->Name(); auto *local_out_tensor = - local_scope->FindVar(out_name)->GetMutable(); + local_scope->FindVar(out_name)->GetMutable(); std::vector out_shape; for (int64_t i = 0; i < local_out_tensor->dims().size(); i++) { out_shape.push_back(local_out_tensor->dims()[i]); } out_desc->SetShape(out_shape); out_desc->SetPersistable(true); - auto *global_out_tensor = scope->Var(out_name)->GetMutable(); + auto *var_desc_out = op_node->Op()->Block()->Var(out_name); + var_desc_out->SetShape(out_shape); + var_desc_out->SetPersistable(true); + var_desc_out->Flush(); + auto *global_out_tensor = + scope->Var(out_name)->GetMutable(); *global_out_tensor = *local_out_tensor; } GraphSafeRemoveNodes(graph, remove_nodes); } delete local_scope; } + for (auto *node : graph->Nodes()) { + if (!node->IsOp() || node->Op()->Type() != "conv2d") continue; + std::cout << "node: " << node->Name() << ", isVar: " << node->IsVar() + << ", isOP: " << node->IsOp() << std::endl; + for (auto *x : node->inputs) { + std::cout << "## trtvar input " << x->Name() << std::endl; + if (x->IsVar() && x->Var()->Persistable()) { + std::cout << "## trtvar input " << x->Name() << " " + << x->Var()->Persistable() << std::endl; + } + } + for (auto *x : node->outputs) { + std::cout << "## trtvar output " << x->Name() << std::endl; + if (x->IsVar() && x->Var()->Persistable()) { + std::cout << "## trtvar output " << x->Name() << " " + << x->Var()->Persistable() << std::endl; + } + } + std::cout << "=========" << std::endl; + // auto* desc = node->Op() + const framework::OpDesc desc = *node->Op(); + auto *block = desc.Block(); + if (block) { + auto *var_desc = block->FindVar("tmp_0"); + // Can't get feed op's TensorDesc + if (var_desc && !var_desc->Persistable()) { + std::cout << "tmp_0 is not persisable" << std::endl; + } + } + } } } // namespace ir diff --git a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc index 5021336df490d..b48f480091e1a 100644 --- a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc +++ b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc @@ -78,6 +78,13 @@ void OutputProcess(framework::ir::Graph *graph, } for (auto *op_node : framework::ir::TopologySortOperations(*graph)) { + for (auto *var_node : op_node->outputs) { + std::cout << "trtvar " << var_node->Name() << std::endl; + if (var_node->Var()->Persistable()) { + std::cout << "trtvar " << var_node->Name() << " " + << var_node->Var()->Persistable() << std::endl; + } + } if (!op_node->IsOp()) continue; auto op_type = op_node->Op()->Type(); if (op_type == "feed") block_desc = op_node->Op()->Block(); @@ -138,6 +145,15 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( auto with_dynamic_shape = Get("with_dynamic_shape"); auto teller = [&](const framework::ir::Node *node) { if (!node->IsOp() || !node->Op()) return false; + if (node->Op()->Type() == "conv2d") { + for (auto *x : node->inputs) { + std::cout << "## teller var input " << x->Name() << std::endl; + if (x->IsVar() && x->Var()->Persistable()) { + std::cout << "## teller var input " << x->Name() << " " + << x->Var()->Persistable() << std::endl; + } + } + } if (find(trt_disabled_ops.begin(), trt_disabled_ops.end(), node->Op()->Type()) != trt_disabled_ops.end()) { @@ -151,6 +167,25 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( VLOG(3) << node->Op()->Type().c_str() << " op is not in TensorRT"; return is_ok; }; + for (auto *node : graph->Nodes()) { + std::cout << "node: " << node->Name() << ", isVar: " << node->IsVar() + << ", isOP: " << node->IsOp() << std::endl; + for (auto *x : node->inputs) { + std::cout << "## trtvar input " << x->Name() << std::endl; + if (x->IsVar() && x->Var()->Persistable()) { + std::cout << "## trtvar input " << x->Name() << " " + << x->Var()->Persistable() << std::endl; + } + } + for (auto *x : node->outputs) { + std::cout << "## trtvar output " << x->Name() << std::endl; + if (x->IsVar() && x->Var()->Persistable()) { + std::cout << "## trtvar output " << x->Name() << " " + << x->Var()->Persistable() << std::endl; + } + } + std::cout << "=========" << std::endl; + } framework::ir::SubGraphFuser fuser( graph, From fbf74596a31ecab813484eb493a0d04904909381 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Tue, 17 Jan 2023 17:30:41 +0000 Subject: [PATCH 2/5] constant folding set persistalbe var in OP block, and remove unsed log --- .../framework/ir/constant_folding_pass.cc | 31 ---------------- .../ir_passes/tensorrt_subgraph_pass.cc | 35 ------------------- 2 files changed, 66 deletions(-) diff --git a/paddle/fluid/framework/ir/constant_folding_pass.cc b/paddle/fluid/framework/ir/constant_folding_pass.cc index 2bf099694cd3e..f6c098f18b17c 100644 --- a/paddle/fluid/framework/ir/constant_folding_pass.cc +++ b/paddle/fluid/framework/ir/constant_folding_pass.cc @@ -119,7 +119,6 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { local_x_tensor->Resize(global_persis_x_tensor->dims()); *local_x_tensor = *global_persis_x_tensor; } - std::cout << "#####" << std::endl; op = paddle::framework::OpRegistry::CreateOp(*op_node->Op()); remove_nodes.emplace(op_node); @@ -155,36 +154,6 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { } delete local_scope; } - for (auto *node : graph->Nodes()) { - if (!node->IsOp() || node->Op()->Type() != "conv2d") continue; - std::cout << "node: " << node->Name() << ", isVar: " << node->IsVar() - << ", isOP: " << node->IsOp() << std::endl; - for (auto *x : node->inputs) { - std::cout << "## trtvar input " << x->Name() << std::endl; - if (x->IsVar() && x->Var()->Persistable()) { - std::cout << "## trtvar input " << x->Name() << " " - << x->Var()->Persistable() << std::endl; - } - } - for (auto *x : node->outputs) { - std::cout << "## trtvar output " << x->Name() << std::endl; - if (x->IsVar() && x->Var()->Persistable()) { - std::cout << "## trtvar output " << x->Name() << " " - << x->Var()->Persistable() << std::endl; - } - } - std::cout << "=========" << std::endl; - // auto* desc = node->Op() - const framework::OpDesc desc = *node->Op(); - auto *block = desc.Block(); - if (block) { - auto *var_desc = block->FindVar("tmp_0"); - // Can't get feed op's TensorDesc - if (var_desc && !var_desc->Persistable()) { - std::cout << "tmp_0 is not persisable" << std::endl; - } - } - } } } // namespace ir diff --git a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc index b48f480091e1a..5021336df490d 100644 --- a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc +++ b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc @@ -78,13 +78,6 @@ void OutputProcess(framework::ir::Graph *graph, } for (auto *op_node : framework::ir::TopologySortOperations(*graph)) { - for (auto *var_node : op_node->outputs) { - std::cout << "trtvar " << var_node->Name() << std::endl; - if (var_node->Var()->Persistable()) { - std::cout << "trtvar " << var_node->Name() << " " - << var_node->Var()->Persistable() << std::endl; - } - } if (!op_node->IsOp()) continue; auto op_type = op_node->Op()->Type(); if (op_type == "feed") block_desc = op_node->Op()->Block(); @@ -145,15 +138,6 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( auto with_dynamic_shape = Get("with_dynamic_shape"); auto teller = [&](const framework::ir::Node *node) { if (!node->IsOp() || !node->Op()) return false; - if (node->Op()->Type() == "conv2d") { - for (auto *x : node->inputs) { - std::cout << "## teller var input " << x->Name() << std::endl; - if (x->IsVar() && x->Var()->Persistable()) { - std::cout << "## teller var input " << x->Name() << " " - << x->Var()->Persistable() << std::endl; - } - } - } if (find(trt_disabled_ops.begin(), trt_disabled_ops.end(), node->Op()->Type()) != trt_disabled_ops.end()) { @@ -167,25 +151,6 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( VLOG(3) << node->Op()->Type().c_str() << " op is not in TensorRT"; return is_ok; }; - for (auto *node : graph->Nodes()) { - std::cout << "node: " << node->Name() << ", isVar: " << node->IsVar() - << ", isOP: " << node->IsOp() << std::endl; - for (auto *x : node->inputs) { - std::cout << "## trtvar input " << x->Name() << std::endl; - if (x->IsVar() && x->Var()->Persistable()) { - std::cout << "## trtvar input " << x->Name() << " " - << x->Var()->Persistable() << std::endl; - } - } - for (auto *x : node->outputs) { - std::cout << "## trtvar output " << x->Name() << std::endl; - if (x->IsVar() && x->Var()->Persistable()) { - std::cout << "## trtvar output " << x->Name() << " " - << x->Var()->Persistable() << std::endl; - } - } - std::cout << "=========" << std::endl; - } framework::ir::SubGraphFuser fuser( graph, From 1366a29561aea23e7f5748a6fc0d7f3c0525bd26 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Thu, 19 Jan 2023 06:15:33 +0000 Subject: [PATCH 3/5] set node var persistalbe From 2b0b87685ccec7d28764627aabda375b577c378e Mon Sep 17 00:00:00 2001 From: zhangjun Date: Tue, 31 Jan 2023 03:30:48 +0000 Subject: [PATCH 4/5] update --- paddle/fluid/framework/ir/constant_folding_pass.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/ir/constant_folding_pass.cc b/paddle/fluid/framework/ir/constant_folding_pass.cc index f6c098f18b17c..b16694265bd5d 100644 --- a/paddle/fluid/framework/ir/constant_folding_pass.cc +++ b/paddle/fluid/framework/ir/constant_folding_pass.cc @@ -135,7 +135,7 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { auto out_desc = out_node->Var(); auto out_name = out_desc->Name(); auto *local_out_tensor = - local_scope->FindVar(out_name)->GetMutable(); + local_scope->FindVar(out_name)->GetMutable(); std::vector out_shape; for (int64_t i = 0; i < local_out_tensor->dims().size(); i++) { out_shape.push_back(local_out_tensor->dims()[i]); @@ -146,8 +146,7 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { var_desc_out->SetShape(out_shape); var_desc_out->SetPersistable(true); var_desc_out->Flush(); - auto *global_out_tensor = - scope->Var(out_name)->GetMutable(); + auto *global_out_tensor = scope->Var(out_name)->GetMutable(); *global_out_tensor = *local_out_tensor; } GraphSafeRemoveNodes(graph, remove_nodes); From 96892e90a1d06cea394555b0310bc1369cff7e5e Mon Sep 17 00:00:00 2001 From: zhangjun Date: Tue, 31 Jan 2023 04:49:07 +0000 Subject: [PATCH 5/5] update --- paddle/fluid/framework/ir/constant_folding_pass.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/framework/ir/constant_folding_pass.cc b/paddle/fluid/framework/ir/constant_folding_pass.cc index b16694265bd5d..ac82c6b73f37d 100644 --- a/paddle/fluid/framework/ir/constant_folding_pass.cc +++ b/paddle/fluid/framework/ir/constant_folding_pass.cc @@ -145,7 +145,6 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { auto *var_desc_out = op_node->Op()->Block()->Var(out_name); var_desc_out->SetShape(out_shape); var_desc_out->SetPersistable(true); - var_desc_out->Flush(); auto *global_out_tensor = scope->Var(out_name)->GetMutable(); *global_out_tensor = *local_out_tensor; }