From 55dbf76a2a36f7b241d7ff885ceb9bb65a06c04d Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Fri, 28 Oct 2022 11:16:01 +0800 Subject: [PATCH 1/5] jit 1.x --- tf_adapter/kernels/geop_npu.cc | 20 ++++++++++++++++++++ tf_adapter/kernels/geop_npu.h | 1 + 2 files changed, 21 insertions(+) diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 87dd62d71..58d9124d2 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -1734,9 +1734,29 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectornum_inputs(); std::string cur_input_shapes; + input_shapes_vec_.resize(num_inputs, std::vector{}); // populate inputs for (int i = 0; i < num_inputs; i++) { Tensor tensor(ctx->input(i)); + + auto &shape_dim = input_shapes_vec_[i]; + auto &value_shape_dim = tensor.GetTensorDesc().GetShape().GetDims(); + auto &shape = tensor.GetTensorDesc().GetShape(); + if (shape_dim.empty()) { + shape_dim = value_shape_dim; + } else { + if (shape_dim == value_shape_dim) { + continue; + } else { + for (int i = 0; i < value_shape_dim.size(); i++) { + if (shape_dim[i] != value_shape_dim[i]) { + shape.SetDim(i, -1); + } + } + ADP_LOG(INFO) << "Refresh input " << i << " shape to " << value_shape_dim.DebugString(); + build_flag_ = false; + } + } bool is_equal = false; if (GraphCheckInputEqualConstOp(tensor, i, is_equal) != Status::OK()) { return errors::Internal("Const op value not equal with tensor :", i); diff --git a/tf_adapter/kernels/geop_npu.h b/tf_adapter/kernels/geop_npu.h index 50c38544a..1ac64f2f3 100644 --- a/tf_adapter/kernels/geop_npu.h +++ b/tf_adapter/kernels/geop_npu.h @@ -180,6 +180,7 @@ private: std::string recompute_mode_; std::string enable_graph_parallel_; std::string graph_parallel_option_path_; + std::vector> input_shapes_vec_; SessionId session_id_; AoeInitializeFunc aoe_initialize_; -- Gitee From 9c5d82fd1561448173134d0c91361a1f175c07e1 Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Thu, 3 Nov 2022 10:08:58 +0800 Subject: [PATCH 2/5] 11.3 --- tf_adapter/kernels/geop_npu.cc | 31 ++++++++++++++++--------------- tf_adapter/kernels/geop_npu.h | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 58d9124d2..0f7d5cf81 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -1734,27 +1734,28 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectornum_inputs(); std::string cur_input_shapes; - input_shapes_vec_.resize(num_inputs, std::vector{}); + input_shapes_vec_.resize(num_inputs, absl::nullopt); // populate inputs for (int i = 0; i < num_inputs; i++) { Tensor tensor(ctx->input(i)); - auto &shape_dim = input_shapes_vec_[i]; - auto &value_shape_dim = tensor.GetTensorDesc().GetShape().GetDims(); - auto &shape = tensor.GetTensorDesc().GetShape(); - if (shape_dim.empty()) { - shape_dim = value_shape_dim; - } else { - if (shape_dim == value_shape_dim) { - continue; + for (int i = 0; i < num_inputs; i++) { + auto &shape = input_shapes_vec_[i]; + auto &value_shape = ctx->input(i).shape(); + if (!shape.has_value()) { + build_flag_ = false; + shape = value_shape; + //DLOG() << "Init input " << i << " shape " << shape.value().DebugString(); } else { - for (int i = 0; i < value_shape_dim.size(); i++) { - if (shape_dim[i] != value_shape_dim[i]) { - shape.SetDim(i, -1); - } + if (shape.value().IsCompatibleWith(value_shape)) { + continue; + } else { + //DLOG() << "Compat input " << i << " shape " << shape.value().DebugString() << " vs. " + //<< value_shape.DebugString(); + shape = MakeCompatShape(shape.value(), value_shape); + //DLOG() << "Refresh input " << i << " shape to " << shape.value().DebugString(); + build_flag_ = false; } - ADP_LOG(INFO) << "Refresh input " << i << " shape to " << value_shape_dim.DebugString(); - build_flag_ = false; } } bool is_equal = false; diff --git a/tf_adapter/kernels/geop_npu.h b/tf_adapter/kernels/geop_npu.h index 1ac64f2f3..5ee6a94cb 100644 --- a/tf_adapter/kernels/geop_npu.h +++ b/tf_adapter/kernels/geop_npu.h @@ -127,6 +127,19 @@ private: void ChangeChannelNameAttr(NodeDef &node_def) const; bool IsDynamicConfig(); + + TensorShape MakeCompatShape(const PartialTensorShape &a, PartialTensorShape &b) { + TensorShape shape; + static constexpr int64 kUnknownDim = -1; + std::vector dims; + for (int i = 0; i < a.dims(); i++) { + dims.push_back((a.dim_size(i) != b.dim_size(i)) ? kUnknownDim : a.dim_size(i)); + } + auto status = TensorShapeUtils::MakeShape(dims.data(), static_cast(dims.size()), &shape); + PartialTensorShape::MakePartialShape(dims.data(), static_cast(dims.size()), &b); + //NPU_LOG_IF_ERROR(status); + return shape; + } static const std::string INPUT_DESC; static const std::string OUTPUT_DESC; @@ -180,7 +193,7 @@ private: std::string recompute_mode_; std::string enable_graph_parallel_; std::string graph_parallel_option_path_; - std::vector> input_shapes_vec_; + std::vector> input_shapes_vec_; SessionId session_id_; AoeInitializeFunc aoe_initialize_; -- Gitee From c2cac14758da5fe66b8cb657eeb7eab1f52559c6 Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Thu, 3 Nov 2022 11:18:47 +0800 Subject: [PATCH 3/5] jit=false --- tf_adapter/kernels/geop_npu.cc | 28 +++++++--------------------- tf_adapter/kernels/geop_npu.h | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 0f7d5cf81..22b805483 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -1734,30 +1734,15 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectornum_inputs(); std::string cur_input_shapes; - input_shapes_vec_.resize(num_inputs, absl::nullopt); + input_shapes_vec_.resize(num_inputs, TensorShape()); + + if(init_options_["ge.jit_compile"] == "0") { + FuzzAllShape(ctx); + } // populate inputs for (int i = 0; i < num_inputs; i++) { Tensor tensor(ctx->input(i)); - for (int i = 0; i < num_inputs; i++) { - auto &shape = input_shapes_vec_[i]; - auto &value_shape = ctx->input(i).shape(); - if (!shape.has_value()) { - build_flag_ = false; - shape = value_shape; - //DLOG() << "Init input " << i << " shape " << shape.value().DebugString(); - } else { - if (shape.value().IsCompatibleWith(value_shape)) { - continue; - } else { - //DLOG() << "Compat input " << i << " shape " << shape.value().DebugString() << " vs. " - //<< value_shape.DebugString(); - shape = MakeCompatShape(shape.value(), value_shape); - //DLOG() << "Refresh input " << i << " shape to " << shape.value().DebugString(); - build_flag_ = false; - } - } - } bool is_equal = false; if (GraphCheckInputEqualConstOp(tensor, i, is_equal) != Status::OK()) { return errors::Internal("Const op value not equal with tensor :", i); @@ -1807,7 +1792,8 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vector dims; - for (int i = 0; i < a.dims(); i++) { - dims.push_back((a.dim_size(i) != b.dim_size(i)) ? kUnknownDim : a.dim_size(i)); + + void FuzzAllShape(OpKernelContext *const ctx) { + for (size_t i = 0UL; i < static_cast(ctx->num_inputs()); i++) { + auto &value_shape = ctx->input(static_cast(i)).shape(); + TensorShape shape; + std::vector dims; + for (int i = 0; i < value_shape.dims(); i++) { + dims.push_back(-1); + } + auto status = TensorShapeUtils::MakeShape(dims.data(), static_cast(dims.size()), &shape); + input_shapes_vec_[i] = shape; } - auto status = TensorShapeUtils::MakeShape(dims.data(), static_cast(dims.size()), &shape); - PartialTensorShape::MakePartialShape(dims.data(), static_cast(dims.size()), &b); - //NPU_LOG_IF_ERROR(status); - return shape; } static const std::string INPUT_DESC; @@ -193,7 +193,7 @@ private: std::string recompute_mode_; std::string enable_graph_parallel_; std::string graph_parallel_option_path_; - std::vector> input_shapes_vec_; + std::vector input_shapes_vec_; SessionId session_id_; AoeInitializeFunc aoe_initialize_; -- Gitee From ed4d12cc30cd4107b960334f3fd82714d74bfb1e Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Thu, 3 Nov 2022 15:57:10 +0800 Subject: [PATCH 4/5] jit = true --- tf_adapter/kernels/geop_npu.cc | 1 + tf_adapter/kernels/geop_npu.h | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 22b805483..6ad051361 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -1739,6 +1739,7 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectorinput(i)); diff --git a/tf_adapter/kernels/geop_npu.h b/tf_adapter/kernels/geop_npu.h index 625077b6a..26f3c9e2a 100644 --- a/tf_adapter/kernels/geop_npu.h +++ b/tf_adapter/kernels/geop_npu.h @@ -20,6 +20,7 @@ #include #include +#include "tf_adapter/common/adapter_logger.h" #include "tensorflow/core/common_runtime/function.h" #include "tensorflow/core/framework/op_kernel.h" #include "tensorflow/core/platform/mutex.h" @@ -141,6 +142,45 @@ private: } } + void MaybeUpdateShape(OpKernelContext *const ctx) { + for (size_t i = 0UL; i < static_cast(ctx->num_inputs()); i++) { + auto &shape = input_shapes_vec_[i]; + auto &value_shape = ctx->input(static_cast(i)).shape(); + if (shape.dims() == 0) { + build_flag_ = false; + shape = value_shape; + ADP_LOG(INFO) << "Init input " << i << " shape " << shape.DebugString(); + } else { + PartialTensorShape sub_shape = shape; + if (sub_shape.IsCompatibleWith(value_shape)) { + continue; + } else { + build_flag_ = false; + ADP_LOG(INFO) << "Compat input " << i << " shape " << shape.DebugString() << " vs. " + << value_shape.DebugString(); + shape = MakeCompatShape(shape, value_shape); + ADP_LOG(INFO) << "Refresh input " << i << " shape to " << shape.DebugString(); + } + } + } + } + + TensorShape MakeCompatShape(const TensorShape &a, const TensorShape &b) { + const static auto kUnknownRankShape = TensorShape(); + if (a.dims() != b.dims()) { + return kUnknownRankShape; + } + TensorShape shape; + static constexpr int64 kUnknownDim = -1; + std::vector dims; + for (int i = 0; i < a.dims(); i++) { + dims.push_back((a.dim_size(i) != b.dim_size(i)) ? kUnknownDim : a.dim_size(i)); + } + auto status = TensorShapeUtils::MakeShape(dims.data(), static_cast(dims.size()), &shape); + //NPU_LOG_IF_ERROR(status); + return status.ok() ? shape : kUnknownRankShape; + } + static const std::string INPUT_DESC; static const std::string OUTPUT_DESC; static const std::string SERIALIZE_FORMAT; -- Gitee From 045ff2681b261646d1c4a6be3c8aba02189e69d9 Mon Sep 17 00:00:00 2001 From: zhao-lupeng Date: Thu, 3 Nov 2022 17:13:47 +0800 Subject: [PATCH 5/5] ut test --- tf_adapter/kernels/geop_npu.cc | 17 ++++++----------- tf_adapter/kernels/geop_npu.h | 9 ++++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index 6ad051361..d866b6753 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -1736,13 +1736,16 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectorinput(i)); + ADP_LOG(INFO) << "Refresh input tensor " << i << " tensor1 to " << tensor.DebugString(); bool is_equal = false; if (GraphCheckInputEqualConstOp(tensor, i, is_equal) != Status::OK()) { @@ -1794,17 +1797,9 @@ Status GeOp::BuildInputTensorInfo(OpKernelContext *const ctx, std::vectorop_kernel().name(), - " is dynamic, please ensure that npu option[dynamic_input] is set" - " correctly, for more details please refer to the migration guide."); - } - } return Status::OK(); } Status GeOp::BuildOutTensorInfo(OpKernelContext *ctx) { diff --git a/tf_adapter/kernels/geop_npu.h b/tf_adapter/kernels/geop_npu.h index 26f3c9e2a..385b43848 100644 --- a/tf_adapter/kernels/geop_npu.h +++ b/tf_adapter/kernels/geop_npu.h @@ -134,10 +134,17 @@ private: auto &value_shape = ctx->input(static_cast(i)).shape(); TensorShape shape; std::vector dims; - for (int i = 0; i < value_shape.dims(); i++) { + ADP_LOG(INFO) << "value_shape dims " << value_shape.dims(); + for (int j = 0; j < value_shape.dims(); j++) { dims.push_back(-1); } + ADP_LOG(INFO) << "dims size is " << dims.size(); + for (auto dim : dims) { + ADP_LOG(INFO) << "dim is : " << dim; + } auto status = TensorShapeUtils::MakeShape(dims.data(), static_cast(dims.size()), &shape); + ADP_LOG(INFO) << "FuzzAllShape Init input " << i << " shape " << shape.DebugString() << "status is" + << status.ok(); input_shapes_vec_[i] = shape; } } -- Gitee