From f73b47d00c814765a79acba0808e7b2f2c5d2180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E8=B1=AA=E6=9D=B0?= Date: Mon, 20 Oct 2025 13:41:58 +0000 Subject: [PATCH] =?UTF-8?q?!3037=20move=20some=20dynamic=20dims=20option?= =?UTF-8?q?=20from=20session=20option=20to=20graph=20option=20Merge=20pull?= =?UTF-8?q?=20request=20!3037=20from=20=E5=94=90=E8=B1=AA=E6=9D=B0/ge=5Fde?= =?UTF-8?q?v?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 8 ++++- tf_adapter/kernels/geop_npu.cc | 9 ++--- tf_adapter/util/npu_attrs.cc | 34 ++++++++++++------- tf_adapter/util/npu_attrs.h | 1 + tf_adapter/util/session_manager.cc | 53 +++++++++++++++--------------- 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 5501fa92c..dd9453cf9 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ build tools -.idea \ No newline at end of file +.idea + +# IDE +.vscode +.idea +cmake-build-debug +cmake-build-gcov diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 7266804a8..e278857ac 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -512,6 +512,7 @@ void GeOp::Initialize(OpKernelConstruction *ctx) { } sess_options_ = NpuAttrs::GetSessOptions(ctx); + graph_options_ = NpuAttrs::GetGraphOptions(ctx); input_shapes_vec_.resize(ctx->num_inputs() + 1, absl::nullopt); init_flag_ = true; @@ -914,8 +915,8 @@ void GeOp::GetExecGraphId(uint32_t &cache_graph_id, const std::vectortype_string() == "IteratorGetNext") { @@ -1992,7 +1993,7 @@ Status GeOp::ChangeInputsShapeDesc() { return Status::OK(); } std::vector result; - std::string input_shapes = sess_options_["ge.inputShape"]; + std::string input_shapes = graph_options_["ge.inputShape"]; Split(input_shapes, result, ";"); // e.g. result:["data:2,3", "data1:3,4"] if (dynamic_shape_nodes_.size() == 1U && dynamic_shape_nodes_[0]->type_string() == "IteratorGetNext") { diff --git a/tf_adapter/util/npu_attrs.cc b/tf_adapter/util/npu_attrs.cc index 77d9fbcf1..bfbb0063a 100644 --- a/tf_adapter/util/npu_attrs.cc +++ b/tf_adapter/util/npu_attrs.cc @@ -443,6 +443,20 @@ std::string ConvertToGeJitValue(const std::string jit_compile) { return ge_jit_compile; } +std::map NpuAttrs::GetGraphOptions(const OpKernelConstruction *ctx) { + std::map graph_options; + std::string input_shape; + std::string dynamic_dims; + std::string dynamic_node_type; + (void) ctx->GetAttr("_input_shape", &input_shape); + (void) ctx->GetAttr("_dynamic_dims", &dynamic_dims); + (void) ctx->GetAttr("_dynamic_node_type", &dynamic_node_type); + graph_options["ge.inputShape"] = input_shape; + graph_options["ge.dynamicDims"] = dynamic_dims; + graph_options["ge.dynamicNodeType"] = dynamic_node_type; + return graph_options; +} + std::map NpuAttrs::GetSessOptions(const OpKernelConstruction *ctx) { std::map sess_options; std::string variable_format_optimize; @@ -468,9 +482,6 @@ std::map NpuAttrs::GetSessOptions(const OpKernelConstr std::string fusion_switch_file; std::string enable_compress_weight = "0"; std::string compress_weight_conf; - std::string input_shape; - std::string dynamic_dims; - std::string dynamic_node_type; std::string session_device_id; std::string modify_mixlist; std::string op_precision_mode; @@ -539,14 +550,11 @@ std::map NpuAttrs::GetSessOptions(const OpKernelConstr (void) ctx->GetAttr("_is_tailing_optimization", &is_tailing_optimization); (void) ctx->GetAttr("_op_select_implmode", &op_select_implmode); (void) ctx->GetAttr("_optypelist_for_implmode", &optypelist_for_implmode); - (void) ctx->GetAttr("_input_shape", &input_shape); - (void) ctx->GetAttr("_dynamic_dims", &dynamic_dims); (void) ctx->GetAttr("_buffer_optimize", &buffer_optimize); (void) ctx->GetAttr("_enable_small_channel", &enable_small_channel); (void) ctx->GetAttr("_fusion_switch_file", &fusion_switch_file); (void) ctx->GetAttr("_enable_compress_weight", &enable_compress_weight); (void) ctx->GetAttr("_compress_weight_conf", &compress_weight_conf); - (void) ctx->GetAttr("_dynamic_node_type", &dynamic_node_type); (void) ctx->GetAttr("_session_device_id", &session_device_id); (void) ctx->GetAttr("_modify_mixlist", &modify_mixlist); (void) ctx->GetAttr("_op_precision_mode", &op_precision_mode); @@ -604,8 +612,6 @@ std::map NpuAttrs::GetSessOptions(const OpKernelConstr sess_options[ge::OP_SELECT_IMPL_MODE] = op_select_implmode; sess_options[ge::OPTYPELIST_FOR_IMPLMODE] = optypelist_for_implmode; sess_options[ge::GRAPH_MAX_PARALLEL_MODEL_NUM] = graph_max_parallel_model_num; - sess_options["ge.inputShape"] = input_shape; - sess_options["ge.dynamicDims"] = dynamic_dims; // 如果compile_dynamic_mode为0, jit_compile=1, shape_generalization_mode!=STRICT, 需要将ge.compile_dynamic_mode设置为1 if ((compile_dynamic_mode == "0" && jit_compile != "1") || (compile_dynamic_mode == "0" && jit_compile == "1" && shape_generalization_mode != "STRICT")) { @@ -618,7 +624,6 @@ std::map NpuAttrs::GetSessOptions(const OpKernelConstr sess_options["ge.fusionSwitchFile"] = fusion_switch_file; sess_options["ge.enableCompressWeight"] = enable_compress_weight; sess_options["compress_weight_conf"] = compress_weight_conf; - sess_options["ge.dynamicNodeType"] = dynamic_node_type; if (std::atoi(session_device_id.c_str()) >= 0) { sess_options["ge.session_device_id"] = session_device_id; } @@ -1839,6 +1844,7 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options return errors::Internal("node is null."); } std::map sess_options; + std::map graph_options; bool hcom_parallel = true; std::string graph_memory_max_size; std::string variable_memory_max_size; @@ -2578,9 +2584,6 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options sess_options["is_tailing_optimization"] = std::to_string(static_cast(is_tailing_optimization)); sess_options["op_select_implmode"] = op_select_implmode; sess_options["optypelist_for_implmode"] = optypelist_for_implmode; - sess_options["input_shape"] = input_shape; - sess_options["dynamic_dims"] = dynamic_dims; - sess_options["dynamic_node_type"] = std::to_string(dynamic_node_type); sess_options["buffer_optimize"] = buffer_optimize; sess_options["enable_small_channel"] = std::to_string(enable_small_channel); sess_options["fusion_switch_file"] = fusion_switch_file; @@ -2612,6 +2615,9 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options sess_options["ge.aicoreNum"] = aicore_num; sess_options["all_tensor_not_empty"] = std::to_string(all_tensor_not_empty); sess_options[ge::OPTION_ALL_TENSOR_NOT_EMPTY] = std::to_string(all_tensor_not_empty); + graph_options["input_shape"] = input_shape; + graph_options["dynamic_dims"] = dynamic_dims; + graph_options["dynamic_node_type"] = std::to_string(dynamic_node_type); init_options_["profiling_mode"] = std::to_string(static_cast(profiling_mode)); init_options_[ge::OPTION_EXEC_PROFILING_MODE] = std::to_string(static_cast(profiling_mode)); init_options_["profiling_options"] = profiling_options; @@ -2712,6 +2718,10 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options std::string attr_name = std::string("_") + option.first; node->AddAttr(attr_name, option.second); } + for (const auto &option : graph_options) { + std::string attr_name = std::string("_") + option.first; + node->AddAttr(attr_name, option.second); + } for (const auto &option : pass_options) { std::string attr_name = std::string("_") + option.first; node->AddAttr(attr_name, option.second); diff --git a/tf_adapter/util/npu_attrs.h b/tf_adapter/util/npu_attrs.h index 0506cf804..e281232d6 100644 --- a/tf_adapter/util/npu_attrs.h +++ b/tf_adapter/util/npu_attrs.h @@ -52,6 +52,7 @@ class NpuAttrs { static std::map GetInitOptions(const OpKernelConstruction *ctx); static std::map GetDefaultInitOptions(); static std::map GetSessOptions(const OpKernelConstruction *ctx); + static std::map GetGraphOptions(const OpKernelConstruction *ctx); static std::map GetPassOptions(const GraphOptimizationPassOptions &options); static std::map GetPassOptions(const OpKernelConstruction *ctx); static std::map GetPassOptions(const AttrSlice &attrs); diff --git a/tf_adapter/util/session_manager.cc b/tf_adapter/util/session_manager.cc index f4d504592..b81551ad2 100644 --- a/tf_adapter/util/session_manager.cc +++ b/tf_adapter/util/session_manager.cc @@ -19,6 +19,12 @@ #include "tf_adapter/common/adapter_logger.h" using namespace tensorflow; +namespace { +std::string GetOptionVal(const std::map &options, const std::string option_key) { + auto it = options.find(option_key); + return (it != options.end() ? it->second : ""); +} +} /** * @brief: get instance */ @@ -103,59 +109,54 @@ bool SessionManager::IsGeSessionExist() const { void SessionManager::PrintGeSessionOptions(std::map &sess_options) const { // variable acceleration configuration - ADP_LOG(INFO) << "[GEOP] variable_acceleration :" << sess_options["ge.exec.variable_acc"]; + ADP_LOG(INFO) << "[GEOP] variable_acceleration :" << GetOptionVal(sess_options, "ge.exec.variable_acc"); // hcom parallel - ADP_LOG(INFO) << "[GEOP] hcom_parallel :" << sess_options[ge::HCOM_PARALLEL]; + ADP_LOG(INFO) << "[GEOP] hcom_parallel :" << GetOptionVal(sess_options, ge::HCOM_PARALLEL); // stream max parallel num - ADP_LOG(INFO) << "[GEOP] stream_max_parallel_num :" << sess_options[ge::STREAM_MAX_PARALLEL_NUM]; + ADP_LOG(INFO) << "[GEOP] stream_max_parallel_num :" << GetOptionVal(sess_options, ge::STREAM_MAX_PARALLEL_NUM); // ac parallel enable - ADP_LOG(INFO) << "[GEOP] ac_parallel_enable :" << sess_options[ge::AC_PARALLEL_ENABLE]; + ADP_LOG(INFO) << "[GEOP] ac_parallel_enable :" << GetOptionVal(sess_options, ge::AC_PARALLEL_ENABLE); // quant dumpable - ADP_LOG(INFO) << "[GEOP] quant_dumpable :" << sess_options[ge::QUANT_DUMPABLE]; + ADP_LOG(INFO) << "[GEOP] quant_dumpable :" << GetOptionVal(sess_options, ge::QUANT_DUMPABLE); // graph memory configuration - if (!sess_options[ge::GRAPH_MEMORY_MAX_SIZE].empty()) { + if (!GetOptionVal(sess_options, ge::GRAPH_MEMORY_MAX_SIZE).empty()) { ADP_LOG(INFO) << "[GEOP] set graph_memory_max_size: " << sess_options[ge::GRAPH_MEMORY_MAX_SIZE]; } else { (void)sess_options.erase(ge::GRAPH_MEMORY_MAX_SIZE); } // variable memory configuration - if (!sess_options[ge::VARIABLE_MEMORY_MAX_SIZE].empty()) { + if (!GetOptionVal(sess_options, ge::VARIABLE_MEMORY_MAX_SIZE).empty()) { ADP_LOG(INFO) << "[GEOP] set variable_memory_max_size: " << sess_options[ge::VARIABLE_MEMORY_MAX_SIZE]; } else { (void)sess_options.erase(ge::VARIABLE_MEMORY_MAX_SIZE); } // tailing optimization - ADP_LOG(INFO) << "[GEOP] is_tailing_optimization : " << sess_options["ge.exec.isTailingOptimization"]; + ADP_LOG(INFO) << "[GEOP] is_tailing_optimization : " << GetOptionVal(sess_options, "ge.exec.isTailingOptimization"); - ADP_LOG(INFO) << "[GEOP] op_select_implmode : " << sess_options[ge::OP_SELECT_IMPL_MODE]; + ADP_LOG(INFO) << "[GEOP] op_select_implmode : " << GetOptionVal(sess_options, ge::OP_SELECT_IMPL_MODE); - ADP_LOG(INFO) << "[GEOP] optypelist_for_implmode : " << sess_options[ge::OPTYPELIST_FOR_IMPLMODE]; + ADP_LOG(INFO) << "[GEOP] optypelist_for_implmode : " << GetOptionVal(sess_options, ge::OPTYPELIST_FOR_IMPLMODE); // dump configuration - string dump_step = sess_options[ge::OPTION_EXEC_DUMP_STEP]; - ADP_LOG(INFO) << "[GEOP] enable_dump :" << sess_options[ge::OPTION_EXEC_ENABLE_DUMP] - << ", dump_path :" << sess_options[ge::OPTION_EXEC_DUMP_PATH] + string dump_step = GetOptionVal(sess_options, ge::OPTION_EXEC_DUMP_STEP); + ADP_LOG(INFO) << "[GEOP] enable_dump :" << GetOptionVal(sess_options, ge::OPTION_EXEC_ENABLE_DUMP) + << ", dump_path :" << GetOptionVal(sess_options, ge::OPTION_EXEC_DUMP_PATH) << ", dump_step :" << (dump_step.empty() ? "NA" : dump_step) - << ", dump_mode :" << sess_options[ge::OPTION_EXEC_DUMP_MODE] - << ", enable_dump_debug :" << sess_options[ge::OPTION_EXEC_ENABLE_DUMP_DEBUG] - << ", dump_debug_mode :" << sess_options[ge::OPTION_EXEC_DUMP_DEBUG_MODE]; - - // dynamic input config - ADP_LOG(INFO) << "[GEOP] input_shape :" << sess_options["ge.inputShape"] - << ", dynamic_dims :" << sess_options["ge.dynamicDims"] - << ", dynamic_node_type :" << sess_options["ge.dynamicNodeType"]; + << ", dump_mode :" << GetOptionVal(sess_options, ge::OPTION_EXEC_DUMP_MODE) + << ", enable_dump_debug :" << GetOptionVal(sess_options, ge::OPTION_EXEC_ENABLE_DUMP_DEBUG) + << ", dump_debug_mode :" << GetOptionVal(sess_options, ge::OPTION_EXEC_DUMP_DEBUG_MODE); - ADP_LOG(INFO) << "[GEOP] buffer_optimize :" << sess_options["ge.bufferOptimize"]; + ADP_LOG(INFO) << "[GEOP] buffer_optimize :" << GetOptionVal(sess_options, "ge.bufferOptimize"); - ADP_LOG(INFO) << "[GEOP] enable_small_channel :" << sess_options["ge.enableSmallChannel"]; + ADP_LOG(INFO) << "[GEOP] enable_small_channel :" << GetOptionVal(sess_options, "ge.enableSmallChannel"); - ADP_LOG(INFO) << "[GEOP] fusion_switch_file :" << sess_options["ge.fusionSwitchFile"]; + ADP_LOG(INFO) << "[GEOP] fusion_switch_file :" << GetOptionVal(sess_options, "ge.fusionSwitchFile"); - ADP_LOG(INFO) << "[GEOP] enable_compress_weight :" << sess_options["ge.enableCompressWeight"]; + ADP_LOG(INFO) << "[GEOP] enable_compress_weight :" << GetOptionVal(sess_options, "ge.enableCompressWeight"); - ADP_LOG(INFO) << "[GEOP] compress_weight_conf :" << sess_options["compress_weight_conf"]; + ADP_LOG(INFO) << "[GEOP] compress_weight_conf :" << GetOptionVal(sess_options, "compress_weight_conf"); } \ No newline at end of file -- Gitee