1be168c0dSopenharmony_ciFrom c87618bc9c440082b7ed6f804539b499ea2263ed Mon Sep 17 00:00:00 2001
2be168c0dSopenharmony_ciFrom: chengfeng27 <chengfeng27@huawei.com>
3be168c0dSopenharmony_ciDate: Thu, 30 May 2024 19:32:52 +0800
4be168c0dSopenharmony_ciSubject: add model version check
5be168c0dSopenharmony_ci
6be168c0dSopenharmony_ci---
7be168c0dSopenharmony_ci .../plugin/device/cpu/kernel/nnacl/kernel.c   | 13 ++++++++++
8be168c0dSopenharmony_ci mindspore/lite/src/common/utils.cc            | 26 +++++++++++++++++++
9be168c0dSopenharmony_ci mindspore/lite/src/common/utils.h             |  7 +++++
10be168c0dSopenharmony_ci mindspore/lite/src/litert/c_api/model_c.cc    | 13 +---------
11be168c0dSopenharmony_ci mindspore/lite/src/litert/lite_model.cc       |  9 ++++---
12be168c0dSopenharmony_ci mindspore/lite/src/litert/scheduler.cc        |  1 +
13be168c0dSopenharmony_ci 6 files changed, 54 insertions(+), 15 deletions(-)
14be168c0dSopenharmony_ci
15be168c0dSopenharmony_cidiff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/kernel.c b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/kernel.c
16be168c0dSopenharmony_ciindex b86ab817..86a5d163 100644
17be168c0dSopenharmony_ci--- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/kernel.c
18be168c0dSopenharmony_ci+++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/kernel.c
19be168c0dSopenharmony_ci@@ -38,12 +38,22 @@ void Init_MSC_VER_kernels(void) {
20be168c0dSopenharmony_ci   return;
21be168c0dSopenharmony_ci }
22be168c0dSopenharmony_ci 
23be168c0dSopenharmony_ci+bool checkOpValid(int opType) {
24be168c0dSopenharmony_ci+  if (opType < PrimType_MIN || opType >= PrimType_MAX) {
25be168c0dSopenharmony_ci+    return false;
26be168c0dSopenharmony_ci+  }
27be168c0dSopenharmony_ci+  return true;
28be168c0dSopenharmony_ci+}
29be168c0dSopenharmony_ci+
30be168c0dSopenharmony_ci bool SupportKernelC(int opType, int dataType) {
31be168c0dSopenharmony_ci   Init_MSC_VER_kernels();
32be168c0dSopenharmony_ci   const int length = 16;
33be168c0dSopenharmony_ci   if (REGIST_DT(dataType) < 0 || REGIST_DT(dataType) >= length) {
34be168c0dSopenharmony_ci     return false;
35be168c0dSopenharmony_ci   }
36be168c0dSopenharmony_ci+  if (!checkOpValid(opType)) {
37be168c0dSopenharmony_ci+    return false;
38be168c0dSopenharmony_ci+  }
39be168c0dSopenharmony_ci   KernelCreator creator = g_kernelCreatorRegistry[opType][REGIST_DT(dataType)];
40be168c0dSopenharmony_ci   return creator != NULL;
41be168c0dSopenharmony_ci }
42be168c0dSopenharmony_ci@@ -77,6 +87,9 @@ int NNACLCheckKernelBase(KernelBase *kernel_base) {
43be168c0dSopenharmony_ci KernelBase *CreateKernel(OpParameter *param, TensorC **ins, size_t in_size, TensorC **outs, size_t out_size,
44be168c0dSopenharmony_ci                          int data_type, ExecEnv *env) {
45be168c0dSopenharmony_ci   Init_MSC_VER_kernels();
46be168c0dSopenharmony_ci+  if (!checkOpValid(param->type_)) {
47be168c0dSopenharmony_ci+    return NULL;
48be168c0dSopenharmony_ci+  }
49be168c0dSopenharmony_ci   KernelCreator creator = g_kernelCreatorRegistry[param->type_][REGIST_DT(data_type)];
50be168c0dSopenharmony_ci   if (creator == NULL) {
51be168c0dSopenharmony_ci     return NULL;
52be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/utils.cc b/mindspore/lite/src/common/utils.cc
53be168c0dSopenharmony_ciindex c8509976..e1699687 100644
54be168c0dSopenharmony_ci--- a/mindspore/lite/src/common/utils.cc
55be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/utils.cc
56be168c0dSopenharmony_ci@@ -195,6 +195,32 @@ std::vector<std::string> Tokenize(const std::string &src, const std::string &del
57be168c0dSopenharmony_ci   return tokens;
58be168c0dSopenharmony_ci }
59be168c0dSopenharmony_ci 
60be168c0dSopenharmony_ci+std::string GetShortVersionStr(const std::string &s) {
61be168c0dSopenharmony_ci+  std::string match_str = "";
62be168c0dSopenharmony_ci+  std::regex e("\\d+(\\.\\d+){2}");
63be168c0dSopenharmony_ci+  auto words_begin = std::sregex_iterator(s.begin(), s.end(), e);
64be168c0dSopenharmony_ci+  auto words_end = std::sregex_iterator();
65be168c0dSopenharmony_ci+  if (words_begin != words_end) {
66be168c0dSopenharmony_ci+    std::smatch match = *words_begin;
67be168c0dSopenharmony_ci+    match_str = match.str();
68be168c0dSopenharmony_ci+  }
69be168c0dSopenharmony_ci+  return match_str;
70be168c0dSopenharmony_ci+}
71be168c0dSopenharmony_ci+
72be168c0dSopenharmony_ci+bool IsVersionGreaterThan(const std::string& str1, const std::string& str2) {
73be168c0dSopenharmony_ci+  auto str1_splits = StrSplit(str1, ".");
74be168c0dSopenharmony_ci+  auto str2_splits = StrSplit(str2, ".");
75be168c0dSopenharmony_ci+  size_t len1 = str1_splits.size();
76be168c0dSopenharmony_ci+  size_t len2 = str2_splits.size();
77be168c0dSopenharmony_ci+  size_t len = std::min(len1, len2);
78be168c0dSopenharmony_ci+  for (size_t i = 0; i < len; ++i) {
79be168c0dSopenharmony_ci+    if (str1_splits[i] != str2_splits[i]) {
80be168c0dSopenharmony_ci+      return std::stoi(str1_splits[i]) > std::stoi(str2_splits[i]);
81be168c0dSopenharmony_ci+    }
82be168c0dSopenharmony_ci+  }
83be168c0dSopenharmony_ci+  return len1 > len2;
84be168c0dSopenharmony_ci+}
85be168c0dSopenharmony_ci+
86be168c0dSopenharmony_ci #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
87be168c0dSopenharmony_ci uint32_t getHwCap(int hwcap_type) {
88be168c0dSopenharmony_ci   uint32_t ret = getauxval(hwcap_type);
89be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/utils.h b/mindspore/lite/src/common/utils.h
90be168c0dSopenharmony_ciindex c3f1d069..ecbe4af2 100644
91be168c0dSopenharmony_ci--- a/mindspore/lite/src/common/utils.h
92be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/utils.h
93be168c0dSopenharmony_ci@@ -25,6 +25,7 @@
94be168c0dSopenharmony_ci #include <cmath>
95be168c0dSopenharmony_ci #include <string>
96be168c0dSopenharmony_ci #include <utility>
97be168c0dSopenharmony_ci+#include <regex>
98be168c0dSopenharmony_ci #include "src/common/log_adapter.h"
99be168c0dSopenharmony_ci #include "tools/common/option.h"
100be168c0dSopenharmony_ci #include "include/errorcode.h"
101be168c0dSopenharmony_ci@@ -213,6 +214,12 @@ enum RemoveSubStrMode { PREFIX, SUFFIX, ANY };
102be168c0dSopenharmony_ci // remove redundant character
103be168c0dSopenharmony_ci std::string RemoveSubStr(const std::string &from, const std::string &sub_str, RemoveSubStrMode mode = ANY);
104be168c0dSopenharmony_ci 
105be168c0dSopenharmony_ci+// match version: x.y.z
106be168c0dSopenharmony_ci+std::string GetShortVersionStr(const std::string &s);
107be168c0dSopenharmony_ci+
108be168c0dSopenharmony_ci+// compare string
109be168c0dSopenharmony_ci+bool IsVersionGreaterThan(const std::string& str1, const std::string& str2);
110be168c0dSopenharmony_ci+
111be168c0dSopenharmony_ci template <typename T>
112be168c0dSopenharmony_ci inline Option<T> GenericParseValue(const std::string &value) {
113be168c0dSopenharmony_ci   T ret;
114be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/c_api/model_c.cc b/mindspore/lite/src/litert/c_api/model_c.cc
115be168c0dSopenharmony_ciindex cbbe2dbb..4f40b3d3 100644
116be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/c_api/model_c.cc
117be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/c_api/model_c.cc
118be168c0dSopenharmony_ci@@ -259,8 +259,6 @@ OH_AI_Status OH_AI_ModelPredict(OH_AI_ModelHandle model, const OH_AI_TensorHandl
119be168c0dSopenharmony_ci 
120be168c0dSopenharmony_ci   std::vector<mindspore::MSTensor> ms_tensor_outputs;
121be168c0dSopenharmony_ci 
122be168c0dSopenharmony_ci-  bool all_has_data = false;
123be168c0dSopenharmony_ci-
124be168c0dSopenharmony_ci   size_t output_num;
125be168c0dSopenharmony_ci   (void)impl->GetOutputs(&output_num);
126be168c0dSopenharmony_ci   auto handle_num = outputs->handle_num;
127be168c0dSopenharmony_ci@@ -273,15 +271,6 @@ OH_AI_Status OH_AI_ModelPredict(OH_AI_ModelHandle model, const OH_AI_TensorHandl
128be168c0dSopenharmony_ci       }
129be168c0dSopenharmony_ci       ms_tensor_outputs.push_back(*static_cast<mindspore::MSTensor *>(outputs->handle_list[i]));
130be168c0dSopenharmony_ci     }
131be168c0dSopenharmony_ci-
132be168c0dSopenharmony_ci-    all_has_data = std::all_of(ms_tensor_outputs.begin(), ms_tensor_outputs.end(), [](const mindspore::MSTensor &t) {
133be168c0dSopenharmony_ci-      return t.Data() != nullptr;
134be168c0dSopenharmony_ci-    });
135be168c0dSopenharmony_ci-
136be168c0dSopenharmony_ci-    if (!all_has_data) {
137be168c0dSopenharmony_ci-      ms_tensor_outputs.clear();
138be168c0dSopenharmony_ci-    }
139be168c0dSopenharmony_ci-
140be168c0dSopenharmony_ci   }
141be168c0dSopenharmony_ci 
142be168c0dSopenharmony_ci   auto ret = impl->model_->Predict(ms_tensor_inputs, &ms_tensor_outputs, before_call_back, after_call_back);
143be168c0dSopenharmony_ci@@ -290,7 +279,7 @@ OH_AI_Status OH_AI_ModelPredict(OH_AI_ModelHandle model, const OH_AI_TensorHandl
144be168c0dSopenharmony_ci     return static_cast<OH_AI_Status>(ret.StatusCode());
145be168c0dSopenharmony_ci   }
146be168c0dSopenharmony_ci 
147be168c0dSopenharmony_ci-  if (handle_num == output_num && all_has_data) {
148be168c0dSopenharmony_ci+  if (handle_num == output_num) {
149be168c0dSopenharmony_ci     return OH_AI_STATUS_SUCCESS;
150be168c0dSopenharmony_ci   }
151be168c0dSopenharmony_ci 
152be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/lite_model.cc b/mindspore/lite/src/litert/lite_model.cc
153be168c0dSopenharmony_ciindex d32db7c8..006bc02c 100644
154be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/lite_model.cc
155be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/lite_model.cc
156be168c0dSopenharmony_ci@@ -29,6 +29,7 @@
157be168c0dSopenharmony_ci #include "src/common/prim_util.h"
158be168c0dSopenharmony_ci #include "src/common/graph_util.h"
159be168c0dSopenharmony_ci #include "src/common/file_utils.h"
160be168c0dSopenharmony_ci+#include "src/common/utils.h"
161be168c0dSopenharmony_ci #include "src/tensor.h"
162be168c0dSopenharmony_ci #include "extendrt/mindir_loader/model_loader.h"
163be168c0dSopenharmony_ci #include "src/common/mmap_utils.h"
164be168c0dSopenharmony_ci@@ -434,9 +435,11 @@ int LiteModel::GenerateModelByVersion() {
165be168c0dSopenharmony_ci   if(DeObfRegister::deobf_handle != nullptr) {
166be168c0dSopenharmony_ci     dlclose(DeObfRegister::deobf_handle);
167be168c0dSopenharmony_ci   }
168be168c0dSopenharmony_ci-  if (this->graph_.version_ != Version()) {
169be168c0dSopenharmony_ci-    MS_LOG(INFO) << "model version is " << this->graph_.version_ << ", inference version is " << Version()
170be168c0dSopenharmony_ci-                 << " not equal";
171be168c0dSopenharmony_ci+  if (IsVersionGreaterThan(GetShortVersionStr(this->graph_.version_), GetShortVersionStr(Version()))) {
172be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "The current model version "<< this->graph_.version_
173be168c0dSopenharmony_ci+                    << " is later than the inference engine version " << Version()
174be168c0dSopenharmony_ci+                    << ". Use a converter tool whose version is earlier than or equal to "
175be168c0dSopenharmony_ci+                    << "the inference engine version to convert the model.";
176be168c0dSopenharmony_ci   }
177be168c0dSopenharmony_ci   MS_LOG(INFO) << "MindSpore Lite inference version: " << Version();
178be168c0dSopenharmony_ci   return status;
179be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/scheduler.cc b/mindspore/lite/src/litert/scheduler.cc
180be168c0dSopenharmony_ciindex d6749471..bc2cf881 100644
181be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/scheduler.cc
182be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/scheduler.cc
183be168c0dSopenharmony_ci@@ -1021,6 +1021,7 @@ int Scheduler::FindCpuKernel(const std::vector<Tensor *> &in_tensors, const std:
184be168c0dSopenharmony_ci   MS_CHECK_TRUE_MSG(op_parameter != nullptr, RET_ERROR, "op parameter is nullptr.");
185be168c0dSopenharmony_ci   auto op_type = op_parameter->type_;
186be168c0dSopenharmony_ci   if (!KernelRegistry::GetInstance()->SupportKernel(desc)) {
187be168c0dSopenharmony_ci+    MS_LOG(INFO) << "unsupport op_type: " << PrimitiveCurVersionTypeName(op_type) << ", data_type: " << desc.data_type;
188be168c0dSopenharmony_ci     return RET_NOT_SUPPORT;
189be168c0dSopenharmony_ci   }
190be168c0dSopenharmony_ci   kernel::KernelKey cpu_desc = desc;
191be168c0dSopenharmony_ci-- 
192be168c0dSopenharmony_ci2.17.1
193be168c0dSopenharmony_ci
194