From e867ef4af4ea909930fc4f4b901e91c6975ab046 Mon Sep 17 00:00:00 2001 From: guopeian Date: Tue, 21 May 2024 20:13:18 +0800 Subject: [PATCH] new geop --- .vscode/settings.json | 70 ++++++++++ tf_adapter/graph/attr_manager.cc | 200 +++++++++++++++++++++++++++++ tf_adapter/graph/attr_manager.h | 41 ++++++ tf_adapter/kernels/new_geop_npu.cc | 58 +++++++++ tf_adapter/kernels/new_geop_npu.h | 42 ++++++ 5 files changed, 411 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 tf_adapter/graph/attr_manager.cc create mode 100644 tf_adapter/graph/attr_manager.h create mode 100644 tf_adapter/kernels/new_geop_npu.cc create mode 100644 tf_adapter/kernels/new_geop_npu.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..e4fef01fb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,70 @@ +{ + "files.associations": { + "numeric": "cpp", + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "cfenv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "any": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "set": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp", + "*.inc": "cpp" + } +} \ No newline at end of file diff --git a/tf_adapter/graph/attr_manager.cc b/tf_adapter/graph/attr_manager.cc new file mode 100644 index 000000000..377a38c7e --- /dev/null +++ b/tf_adapter/graph/attr_manager.cc @@ -0,0 +1,200 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "attr_manager.h" +#include "tf_adapter/common/adapter_logger.h" + +namespace tensorflow { +namespace { + const std::map kGlobalConfigOptions = { + {"_precision_mode", ge::PRECISION_MODE}, + {"_precision_mode_v2", "ge.exec.precision_mode_v2"}, + {"_auto_tune_mode", "ge.autoTuneMode"}, + {"_graph_run_mode", ge::OPTION_GRAPH_RUN_MODE}, + {"_op_debug_level", ge::OP_DEBUG_LEVEL}, + {"_enable_scope_fusion_passes", ge::OPTION_EXEC_ENABLE_SCOPE_FUSION_PASSES}, + {"_enable_exception_dump", "ge.exec.enable_exception_dump"}, + {"_aoe_mode", "ge.jobType"}, + {"_work_path", "ge.tuningPath"}, + {"_op_compiler_cache_mode", "ge.op_compiler_cache_mode"}, + {"_op_compiler_cache_dir", "ge.op_compiler_cache_dir"}, + {"_debug_dir", "ge.debugDir"}, + {"_hcom_multi_mode", "ge.hcomMultiMode"}, + {"_distribute_config", "distribute_config"}, + {"_modify_mixlist", ge::MODIFY_MIXLIST}, + {"_fusion_switch_file", "ge.fusionSwitchFile"}, + {"_op_precision_mode", ge::OP_PRECISION_MODE}, + {"_op_select_implmode", ge::OP_SELECT_IMPL_MODE}, + {"_device_type", "ge.deviceType"}, + {"_soc_config", "ge.socVersion"}, + {"_hccl_timeout", "ge.exec.hcclExecuteTimeOut"}, + {"_HCCL_algorithm", "HCCL_algorithm"}, + {"_customize_dtypes", "ge.customizeDtypes"}, + {"_op_debug_config", "ge.exec.opDebugConfig"}, + {"_graph_exec_timeout", "ge.exec.graphExecTimeout"}, + {"_static_memory_policy", "ge.exec.staticMemoryPolicy"}, + {"_logical_device_cluster_deploy_mode", "ge.exec.logicalDeviceClusterDeployMode"}, + {"_logical_device_id", "ge.exec.logicalDeviceId"}, + {"_model_deploy_mode", "ge.exec.modelDeployMode"}, + {"_model_deploy_devicelist", "ge.exec.modelDeployDevicelist"}, + {"_dump_data", "ge.exec.dumpData"}, + {"_aoe_config_file", "ge.aoe_config_file"}, + {"_stream_sync_timeout", "stream_sync_timeout"}, + {"_event_sync_timeout", "event_sync_timeout"}, + {"_es_cluster_config", "ge.esClusterConfig"} + }; + // session options + const std::map kSessionConfigOptions = { + {"_variable_format_optimize", "ge.exec.variable_acc"}, + {"_hcom_parallel", ge::HCOM_PARALLEL}, + {"_graph_memory_max_size", ge::GRAPH_MEMORY_MAX_SIZE}, + {"_enable_dump", ge::OPTION_EXEC_ENABLE_DUMP}, + {"_variable_memory_max_size", ge::VARIABLE_MEMORY_MAX_SIZE}, + {"_enable_dump_debug", ge::OPTION_EXEC_ENABLE_DUMP_DEBUG}, + {"_input_fusion_size", "ge.exec.input_fusion_size"}, + {"_dump_path", ge::OPTION_EXEC_DUMP_PATH}, + {"_dump_step", ge::OPTION_EXEC_DUMP_STEP}, + {"_dump_mode", ge::OPTION_EXEC_DUMP_MODE}, + {"_dump_debug_mode", ge::OPTION_EXEC_DUMP_DEBUG_MODE}, + {"_stream_max_parallel_num", ge::STREAM_MAX_PARALLEL_NUM}, + {"_ac_parallel_enable", ge::AC_PARALLEL_ENABLE}, + {"_quant_dumpable", ge::QUANT_DUMPABLE}, + {"_is_tailing_optimization", "ge.exec.isTailingOptimization"}, + {"_op_select_implmode", ge::OP_SELECT_IMPL_MODE}, + {"_optypelist_for_implmode", ge::OPTYPELIST_FOR_IMPLMODE}, + {"_input_shape", "ge.inputShape"}, + {"_dynamic_dims", "ge.dynamicDims"}, + {"_buffer_optimize", "ge.bufferOptimize"}, + {"_enable_small_channel", "ge.enableSmallChannel"}, + {"_fusion_switch_file", "ge.fusionSwitchFile"}, + {"_enable_compress_weight", "ge.enableCompressWeight"}, + {"_compress_weight_conf", "compress_weight_conf"}, + {"_dynamic_node_type", "ge.dynamicNodeType"}, + {"_session_device_id", "ge.session_device_id"}, + {"_modify_mixlist", ge::MODIFY_MIXLIST}, + {"_op_precision_mode", "ge.exec.op_precision_mode"}, + {"_graph_run_mode", ge::OPTION_GRAPH_RUN_MODE}, + {"_hccl_timeout", "ge.exec.hcclExecuteTimeOut"}, + {"_HCCL_algorithm", "HCCL_algorithm"}, + {"_atomic_clean_policy", "ge.exec.atomicCleanPolicy"}, + {"_memory_optimization_policy", "ge.exec.memoryOptimizationPolicy"}, + {"_topo_sorting_mode", "ge.topoSortingMode"}, + {"_insert_op_file", "ge.insertOpFile"}, + {"_resource_config_path", "ge.resourceConfigPath"}, + {"_dump_layer", "ge.exec.dumpLayer"}, + {"_external_weight", "ge.externalWeight"}, + {"_graph_parallel_option_path", "ge.graphParallelOptionPath"}, + {"_enable_graph_parallel", "ge.enableGraphParallel"}, + {"_graph_slice", "ge.graphSliceMode"}, + {"_compile_dynamic_mode", "ge.compile_dynamic_mode"}, + {"_jit_compile", "ge.jit_compile"}, + {"_graph_compiler_cache_dir", "ge.graph_compiler_cache_dir"} + }; + + const std::map kGraphConfigOptions = { + {"_recompute_mode", "ge.recompute"}, + {"_max_key_num", "ge.max_key_num"}, + {"_use_counter_filter", "ge.use_counter_filter"}, + {"_embedding_dim", "ge.embedding_dim"}, + {"_execute_times", "ge.execute_times"}, + {"_padding_key", "ge.padding_key"}, + {"_embedding_flags", "ge.embedding_flags"}, + {"_jit_compile", "ge.jit_compile"}, + {"_is_var_init_graph", "ge.exec.isVarInitGraph"} + }; + const std::map kTfConfigOptions = { + {"data_format", "data_format"}, + {"_placeholder_index", "_placeholder_index"}, + {"_is_dynamic_getnext", "_is_dynamic_getnext"}, + {"job", "job"}, + {"mix_compile_mode", "mix_compile_mode"}, + {"accelerate_train_mode", "accelerate_train_mode"}, + {"_dynamic_input", "ge.exec.dynamicInput"}, + {"_dynamic_graph_execute_mode", "ge.exec.dynamicGraphExecuteMode"}, + {"_getnext_inputs_shape_range", "_getnext_inputs_shape_range"}, + {"_data_inputs_shape_range", "ge.exec.dataInputsShapeRange"}, + {"iterations_per_loop", "iterations_per_loop"} + }; +} +void AttrManager::ParserOptionFromConfigFile(OpKernelConstruction *ctx, + const std::map &config_map) { + for (const auto &iter : config_map) { + std::string value; + (void)ctx->GetAttr(iter.first, value); + if (!value.empty()) { + (void)all_attr_map_.emplace(std::make_pair(iter.first, value)); + } + } +} +void AttrManager::Init(OpKernelConstruction *ctx) { + ParserOptionFromConfigFile(ctx, kGlobalConfigOptions); + ParserOptionFromConfigFile(ctx, kSessionConfigOptions); + ParserOptionFromConfigFile(ctx, kGraphConfigOptions); + ParserOptionFromConfigFile(ctx, kTfConfigOptions); +} + +std::map AttrManager::GetOptionMapFromConfig( + const std::map &config_map) { + std::map option_map; + for (const auto &iter : all_attr_map_) { + const auto it = config_map.find(iter->first); + if (it != kGraphConfigOptions.cend()) { + (void)graph_option.emplace(std::make_pair(it->second, iter->second)); + } + } + return option_map; +} +std::map AttrManager::GetGraphOption() { + auto graph_option = GetOptionMapFromConfig(kGraphConfigOptions); + if (all_attr_map_["_dynamic_input"] == "1") { + (void)graph_option.emplace(std::make_pair("ge.exec.dynamicInput", all_attr_map_["_dynamic_input"])); + (void)graph_option.emplace(std::make_pair("ge.exec.dynamicGraphExecuteMode", + all_attr_map_["_dynamic_graph_execute_mode"])); + (void)graph_option.emplace(std::make_pair("ge.exec.dataInputsShapeRange", + all_attr_map_["_data_inputs_shape_range"])); + if (all_attr_map_["_dynamic_graph_execute_mode"] == "dynamic_execute" && + all_attr_map_["_data_inputs_shape_range"].empty() && + all_attr_map_["_getnext_inputs_shape_range"].empty()) { + (void)graph_option.emplace(std::make_pair("ge.shape_generalized_build_mode", "shape_generalized")); + } + } + if (std::atoi(pass_options["iterations_per_loop"].c_str()) > 1) { + (void)graph_option.emplace(std::make_pair("iterations_per_loop", all_attr_map_["iterations_per_loop"])); + } + const std::string graph_level_sat_value = (all_attr_map_["mix_compile_mode"] == "0") ? "1" : "0"; + (void)graph_option.emplace(std::make_pair("ge.graphLevelSat", graph_level_sat_value)); + return graph_option; +} +std::map AttrManager::GetInitOption() { + return GetOptionMapFromConfig(kGlobalConfigOptions); +} + +std::map AttrManager::GetSessionOption() { + return GetOptionMapFromConfig(kSessionConfigOptions); +} + +std::string AttrManager::GetOption(std::string &key) { + const auto it = all_attr_map_.find(key); + if (it != all_attr_map_.cend()) { + ADP_LOG(INFO) << "Key: " << it->first << ", value: " << it->second; + return iter->second; + } + return ""; +} +void AttrManager::SetOption(std::string &key, std::string &value) { + all_attr_map_[key] = value; +} +} \ No newline at end of file diff --git a/tf_adapter/graph/attr_manager.h b/tf_adapter/graph/attr_manager.h new file mode 100644 index 000000000..62a24b750 --- /dev/null +++ b/tf_adapter/graph/attr_manager.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TENSORFLOW_TFADAPTER_GRAPH_ATTR_MANAGER_H_ +#define TENSORFLOW_TFADAPTER_GRAPH_ATTR_MANAGER_H_ +#include +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { +class AttrManager { + public: + void Init(OpKernelConstruction *ctx); + std::map GetGraphOption(); + std::map GetInitOption(); + std::map GetSessionOption(); + std::map GetAoeOption(); + std::map GetTfOption(); + std::string GetOption(std::string &key); + bool SetOption(std::string &key, std::string &value); + private: + void ParserOptionFromConfigFile(OpKernelConstruction *ctx, + const std::map config_map); + std::map GetOptionMapFromConfig( + const std::map &config_map); + std::map all_attr_map_; +}; +} +#endif // TENSORFLOW_TFADAPTER_GRAPH_ATTR_MANAGER_H_ \ No newline at end of file diff --git a/tf_adapter/kernels/new_geop_npu.cc b/tf_adapter/kernels/new_geop_npu.cc new file mode 100644 index 000000000..a1dcabbc3 --- /dev/null +++ b/tf_adapter/kernels/new_geop_npu.cc @@ -0,0 +1,58 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tf_adapter/kernels/new_geop_npu.h" +#include "tf_adapter/util/infershape_util.h" +#include "tf_adapter/common/common.h" +#include "tf_adapter/common/adapter_logger.h" + +namespace tensorflow { +bool NewGeOp::IsDynamicConfig() { + const bool result = !attr_manager_.GetOption("_input_shape").empty() && + !attr_manager_.GetOption("_dynamic_dims").empty() && + !attr_manager_.GetOption("_dynamic_node_type").empty(); + ADP_LOG(INFO) << "[GEOP] IsDynamicConfig result is: " << result; + return result; +} + +NewGeOp::NewGeOp(OpKernelConstruction *ctx) + : AsyncOpKernel(ctx) { + mutex_lock lock{mu_}; + int64 startTime = InferShapeUtil::GetCurrentTimestap(); + ADP_LOG(INFO) << "[GEOP] Begin GeOp initialize."; + CHECK_NOT_NULL(ctx); + OP_REQUIRES_OK(ctx, ctx->GetAttr("function", &function_)); + OP_REQUIRES_OK(ctx, ctx->GetAttr("data_format", &data_format_)); + ADP_LOG(INFO) << "Attr 'data_format' of " << ctx->def().name() << " is " << data_format_; + OP_REQUIRES_OK(ctx, ctx->GetAttr("_session", &tf_session_)); + attr_manager_.Init(ctx); + if (attr_manager_.GetOption("_dynamic_input") == "1") { + attr_manager_.SetOption("_jit_compile", "1"); + is_dynamic_input_ = true; + } + is_multi_batch_ = IsDynamicConfig(); + +} + +NewGeOp::~NewGeOp() { + +} + +void NewGeOp::ComputeAsync(OpKernelContext *ctx, DoneCallback done) { + +} + +} \ No newline at end of file diff --git a/tf_adapter/kernels/new_geop_npu.h b/tf_adapter/kernels/new_geop_npu.h new file mode 100644 index 000000000..cb6be9f1f --- /dev/null +++ b/tf_adapter/kernels/new_geop_npu.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TENSORFLOW_KERNELS_NEW_GEOP_NPU_H_ +#define TENSORFLOW_KERNELS_NEW_GEOP_NPU_H_ +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/platform/mutex.h" +#include "tf_adapter/graph/attr_manager.h" + +namespace tensorflow { +class NewGeop : public AsyncOpKernel { + public: + explicit GeOp(OpKernelConstruction *ctx); + ~GeOp() override; + void ComputeAsync(OpKernelContext *ctx, DoneCallback done) override; + private: + bool IsDynamicConfig(); + private: + static mutex mu_; + NameAttrList function_; + std::string data_format_; + std::string tf_session_; + AttrManager attr_manager_; + bool is_dynamic_input_{false}; + bool is_multi_batch_{false}; +}; +} + +#endif // TENSORFLOW_KERNELS_NEW_GEOP_NPU_H_ \ No newline at end of file -- Gitee