From 8ee45abed9c23bbd5d11bf9a0f64dc63c9fc5bd1 Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Thu, 12 Jan 2023 15:59:25 +0800 Subject: [PATCH] fixed 03896b1 from https://gitee.com/zhao-lupeng/tensorflow/pulls/1980 fixed 3f483fa from https://gitee.com/dengtao24/tensorflow/pulls/1937 add option check --- .../tests/st/util/testcase/npu_attrs_test.cc | 16 ++++++++++ .../tests/ut/util/testcase/npu_attrs_test.cc | 16 ++++++++++ tf_adapter/util/npu_attrs.cc | 11 +++++-- tf_adapter/util/npu_attrs.h | 29 +++++++++++++++++++ tf_adapter_2.x/npu_device/core/npu_device.cpp | 2 +- tf_adapter_2.x/npu_device/core/npu_micros.h | 4 +-- .../python/npu_device/configs/npu_config.py | 2 +- 7 files changed, 73 insertions(+), 7 deletions(-) diff --git a/tf_adapter/tests/st/util/testcase/npu_attrs_test.cc b/tf_adapter/tests/st/util/testcase/npu_attrs_test.cc index aff1db6be..1b9b91e13 100644 --- a/tf_adapter/tests/st/util/testcase/npu_attrs_test.cc +++ b/tf_adapter/tests/st/util/testcase/npu_attrs_test.cc @@ -84,6 +84,22 @@ TEST_F(NpuAttrTest, CheckAoeMode) { EXPECT_EQ(s.ok(), false); } +TEST_F(NpuAttrTest, CheckPrecisionMode ) { + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options()->mutable_optimizer_options()->set_do_function_inlining(true); + auto *custom_config = + session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + options.session_options = &session_options; + + AttrValue precision_mode = AttrValue(); + precision_mode.set_s("force_Dp32"); + (*custom_config->mutable_parameter_map())["precision_mode"] = precision_mode; + Status s = NpuAttrs::SetNpuOptimizerAttr(options, nullptr); + EXPECT_EQ(s.ok(), false); +} + TEST_F(NpuAttrTest, GetDumpPath) { setenv("DUMP_GRAPH_PATH", "./", 1); string path = GetDumpPath(); diff --git a/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc b/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc index cf6b30c70..dc796242b 100644 --- a/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc +++ b/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc @@ -85,6 +85,22 @@ TEST_F(NpuAttrTest, CheckAoeMode) { EXPECT_EQ(s.ok(), false); } +TEST_F(NpuAttrTest, CheckPrecisionMode ) { + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options()->mutable_optimizer_options()->set_do_function_inlining(true); + auto *custom_config = + session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + options.session_options = &session_options; + + AttrValue precision_mode = AttrValue(); + precision_mode.set_s("force_Dp32"); + (*custom_config->mutable_parameter_map())["precision_mode"] = precision_mode; + Status s = NpuAttrs::SetNpuOptimizerAttr(options, nullptr); + EXPECT_EQ(s.ok(), false); +} + TEST_F(NpuAttrTest, GetDumpPath) { setenv("DUMP_GRAPH_PATH", "./", 1); string path = GetDumpPath(); diff --git a/tf_adapter/util/npu_attrs.cc b/tf_adapter/util/npu_attrs.cc index 630da0290..fa51334db 100644 --- a/tf_adapter/util/npu_attrs.cc +++ b/tf_adapter/util/npu_attrs.cc @@ -30,6 +30,7 @@ #include "mmpa/mmpa_api.h" #include "tf_adapter/util/ge_plugin.h" #include "ge/ge_api.h" +#include "tf_adapter_2.x/npu_device/core/npu_micros.h" namespace tensorflow { namespace { bool kIsNewDataTransfer = true; @@ -726,7 +727,7 @@ std::map NpuAttrs::GetPassOptions(const GraphOptimizat if (dynamic_input) { if (params.count("dynamic_graph_execute_mode") > 0) { dynamic_graph_execute_mode = params.at("dynamic_graph_execute_mode").s(); - if (dynamic_graph_execute_mode != "lazy_recompile" && dynamic_graph_execute_mode != "dynamic_execute") { + if ((dynamic_graph_execute_mode != "lazy_recompile") && (dynamic_graph_execute_mode != "dynamic_execute")) { ADP_LOG(ERROR) << "dynamic_graph_execute_mode should be lazy_recompile or dynamic_execute."; LOG(FATAL) << "dynamic_graph_execute_mode should be lazy_recompile or dynamic_execute."; } @@ -1732,6 +1733,10 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options } if (params.count("precision_mode") > 0) { precision_mode = params.at("precision_mode").s(); + const static std::vector kPrecisionModeList = {"force_fp32", "allow_fp32_to_fp16", + "force_fp16", "must_keep_origin_dtype", + "allow_mix_precision", "cube_fp16in_fp32out"}; + NPU_REQUIRES_OK(CheckValueAllowed(precision_mode, kPrecisionModeList)); } else { if (static_cast(graph_run_mode)) { precision_mode = "allow_fp32_to_fp16"; @@ -1863,7 +1868,7 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options } if (params.count("buffer_optimize") > 0) { buffer_optimize = params.at("buffer_optimize").s(); - if (buffer_optimize != "l2_optimize" && buffer_optimize != "off_optimize") { + if ((buffer_optimize != "l2_optimize") && (buffer_optimize != "off_optimize")) { ADP_LOG(FATAL) << "buffer_optimize is valid, should be one of [l2_optimize, off_optimize]"; LOG(FATAL) << "buffer_optimize is valid, should be one of [l2_optimize, off_optimize]"; } @@ -1880,7 +1885,7 @@ Status NpuAttrs::SetNpuOptimizerAttr(const GraphOptimizationPassOptions &options if (params.count("fusion_switch_file") > 0) { fusion_switch_file = params.at("fusion_switch_file").s(); } - if (params.count("enable_compress_weight") > 0 && params.count("compress_weight_conf") > 0) { + if ((params.count("enable_compress_weight") > 0) && (params.count("compress_weight_conf") > 0)) { ADP_LOG(FATAL) << "enable_compress_weight can not use with compress_weight_conf."; LOG(FATAL) << "enable_compress_weight can not use with compress_weight_conf."; } diff --git a/tf_adapter/util/npu_attrs.h b/tf_adapter/util/npu_attrs.h index 048bbeb3f..7e73c693a 100644 --- a/tf_adapter/util/npu_attrs.h +++ b/tf_adapter/util/npu_attrs.h @@ -17,6 +17,9 @@ #ifndef TENSORFLOW_NPU_ATTRS_H_ #define TENSORFLOW_NPU_ATTRS_H_ +#include +#include +#include #include #include #include "ge/ge_api_types.h" @@ -58,6 +61,32 @@ class NpuAttrs { static bool GetNewDataTransferFlag(); // only use for ut/st static void SetNewDataTransferFlag(bool flag); + template + static std::string VectorToString(const std::vector &values) { + std::stringstream ss; + ss << '['; + const auto size = values.size(); + for (size_t i = 0U; i < size; ++i) { + ss << values[i]; + if (i != (size - 1U)) { + ss << ", "; + } + } + ss << ']'; + return ss.str(); + } + template + static Status CheckValueAllowed(const T &v, const std::vector &allowed_values) { + if (find(allowed_values.begin(), allowed_values.end(), v) != allowed_values.cend()) { + return Status::OK(); + } else { + std::stringstream ss; + ss << v << " is invalid, it should be one of the list:"; + ss << VectorToString(allowed_values); + return errors::InvalidArgument(ss.str()); + } + } + private: static bool CheckIsNewDataTransfer(); static std::map turn_on_tdt_info_; diff --git a/tf_adapter_2.x/npu_device/core/npu_device.cpp b/tf_adapter_2.x/npu_device/core/npu_device.cpp index f1d6f0f8e..1849e34dd 100644 --- a/tf_adapter_2.x/npu_device/core/npu_device.cpp +++ b/tf_adapter_2.x/npu_device/core/npu_device.cpp @@ -808,7 +808,7 @@ void NpuDevice::RunGeGraphAsync(TFE_Context *context, uint64_t graph_id, int num if (err_msg.empty()) { err_msg = " code:" + std::to_string(s); } - done(tensorflow::errors::Internal("Graph engine process graph failed: ", err_msg)); + done(tensorflow::errors::Internal("Graph engine process graph failed:\n", err_msg)); return; } else if (ge_outputs.size() != static_cast(num_outputs)) { done(tensorflow::errors::Internal("Graph engine process graph succeed but output num ", ge_outputs.size(), diff --git a/tf_adapter_2.x/npu_device/core/npu_micros.h b/tf_adapter_2.x/npu_device/core/npu_micros.h index cfeeb4ee0..933ef44a8 100644 --- a/tf_adapter_2.x/npu_device/core/npu_micros.h +++ b/tf_adapter_2.x/npu_device/core/npu_micros.h @@ -79,7 +79,7 @@ if (err_msg.empty()) { \ err_msg = " code:" + std::to_string(_status); \ } \ - CTX->status = tensorflow::errors::Internal(PREFIX, ":", err_msg); \ + CTX->status = tensorflow::errors::Internal(PREFIX, ":\n", err_msg); \ LOG(ERROR) << CTX->status.ToString(); \ return; \ } \ @@ -93,7 +93,7 @@ if (err_msg.empty()) { \ err_msg = " code:" + std::to_string(_status); \ } \ - (CTX)->status = tensorflow::errors::Internal(PREFIX, ":", err_msg); \ + (CTX)->status = tensorflow::errors::Internal(PREFIX, ":\n", err_msg); \ LOG(ERROR) << (CTX)->status.ToString(); \ return RET; \ } \ diff --git a/tf_adapter_2.x/python/npu_device/configs/npu_config.py b/tf_adapter_2.x/python/npu_device/configs/npu_config.py index da5492e08..30791404b 100644 --- a/tf_adapter_2.x/python/npu_device/configs/npu_config.py +++ b/tf_adapter_2.x/python/npu_device/configs/npu_config.py @@ -37,7 +37,7 @@ class NpuConfig(NpuBaseConfig): self.fusion_switch_file = OptionValue(None, None) self.precision_mode = OptionValue('allow_fp32_to_fp16', ['force_fp32', 'allow_fp32_to_fp16', 'force_fp16', 'must_keep_origin_dtype', - 'allow_mix_precision']) + 'allow_mix_precision', 'cube_fp16in_fp32out']) self.op_select_implmode = DeprecatedValue(['high_performance', 'high_precision'], replacement='op_precision_mode') self.optypelist_for_implmode = DeprecatedValue(None, replacement='op_precision_mode') -- Gitee