diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc index 359bab844303f365b3370a56f34795007af4cc57..4c1538a28fedb23f39e6cd94b3d69d36d1e611bf 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc @@ -31,6 +31,7 @@ #include "cinn/frontend/syntax.h" #include "cinn/hlir/framework/graph.h" #include "cinn/hlir/framework/graph_compiler.h" +#include "cinn/hlir/framework/visualize_helper.h" #include "gflags/gflags.h" #include "paddle/fluid/framework/framework.pb.h" #include "paddle/fluid/framework/ir/graph.h" @@ -49,6 +50,7 @@ DECLARE_bool(enable_pe_launch_cinn); DECLARE_bool(enable_cinn_auto_tune); +DECLARE_string(cinn_subgraph_graphviz_dir); namespace paddle { namespace framework { namespace paddle2cinn { @@ -73,7 +75,6 @@ const CinnCompiledObject &CinnCompiler::Compile( const std::map &input_tensors, const Target &target, void *stream) { - VLOG(4) << "-- The graph to be compiled is:\n" << VizGraph(graph); CinnCacheKeyByAddress cur_key_by_address( graph, input_tensors, target.arch_str()); CinnCacheKeyByStructure cur_key_by_struct; @@ -85,6 +86,26 @@ const CinnCompiledObject &CinnCompiler::Compile( if (!cache_by_struct_.count(cur_key_by_struct)) { VLOG(4) << "Not found CinnCompiledObject in cache_by_struct_."; std::int64_t compiled_num = real_compiled_num_.fetch_add(1); + + if (!FLAGS_cinn_subgraph_graphviz_dir.empty()) { + const std::string &viz_path = FLAGS_cinn_subgraph_graphviz_dir + + "/fusion_groups_" + + std::to_string(compiled_num) + "/"; + if (!::cinn::hlir::framework::MakeDirectory( + viz_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { + LOG_IF(WARNING, compiled_num == 0) + << "Failed to make directory: \"" << viz_path + << "\", the CINN subgraph's graphviz dot file will not print."; + } else { + LOG_IF(INFO, compiled_num == 0) + << "The CINN subgraph's graphviz dot file will writing into " + "path: \"" + << FLAGS_cinn_subgraph_graphviz_dir << "\""; + ::cinn::hlir::framework::WriteToFile(viz_path + "cinn_subgraph.dot", + VizGraph(graph)); + } + } + auto compiled_res = CompileGraph(graph, input_tensors, target, compiled_num, stream); std::unique_lock guard(lock_); diff --git a/paddle/phi/core/flags.cc b/paddle/phi/core/flags.cc index 9cff3acccbd416e3e515322ab2a4f6b39a6a3074..ad2e38b70d0ed654df26aef51227e6cf9e08bd16 100644 --- a/paddle/phi/core/flags.cc +++ b/paddle/phi/core/flags.cc @@ -1001,6 +1001,20 @@ PADDLE_DEFINE_EXPORTED_bool(enable_cinn_auto_tune, "It controls whether to use cinn with " "its auto-tune feature enabled"); +/* + * CINN related FLAG + * Name: FLAGS_cinn_subgraph_graphviz_dir + * Since Version: 2.3 + * Value Range: string, default="" + * Example: FLAGS_cinn_subgraph_graphviz_dir="./cinn_graph/" will save the + * CINN sub-graph into "./cinn_graph/", and each sub-graph will save into + * "fusion_groups_*"" directory + */ +PADDLE_DEFINE_EXPORTED_string(cinn_subgraph_graphviz_dir, + "", + "Specify the directory path of dot file of " + "graph, which is used for debug."); + #endif /*