1From e9a9786e9619b6fcb8637eaf1331829793ccc351 Mon Sep 17 00:00:00 2001
2From: chengfeng27 <chengfeng27@huawei.com>
3Date: Thu, 12 Sep 2024 10:38:08 +0800
4Subject: [PATCH] context nullptr return, remote std::cout
5
6---
7 .../nnacl/infer/sparse_segment_sum_infer.c    |  1 -
8 .../src/common/hi_app_event/handler_thread.cc |  5 ++--
9 mindspore/lite/src/litert/c_api/context_c.cc  |  5 ++--
10 .../delegate/nnrt/extension_options_parser.cc |  3 ++-
11 mindspore/lite/src/litert/inner_allocator.cc  |  2 +-
12 .../kernel/cpu/base/scatter_nd_binary.cc      | 24 +++++++++++++++++--
13 mindspore/lite/src/litert/lite_session.cc     |  1 -
14 7 files changed, 31 insertions(+), 10 deletions(-)
15
16diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/sparse_segment_sum_infer.c b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/sparse_segment_sum_infer.c
17index 34069304..c8160bf6 100644
18--- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/sparse_segment_sum_infer.c
19+++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/sparse_segment_sum_infer.c
20@@ -32,7 +32,6 @@ int SparseSegmentSumInferShape(const TensorC *const *inputs, size_t inputs_size,
21   }
22 
23   return NNACL_INFER_INVALID;
24-  // return NNACL_OK;
25 }
26 
27 REG_INFER(SparseSegmentSum, PrimType_SparseSegmentSum, SparseSegmentSumInferShape)
28diff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.cc b/mindspore/lite/src/common/hi_app_event/handler_thread.cc
29index a84504c0..1cf32bca 100644
30--- a/mindspore/lite/src/common/hi_app_event/handler_thread.cc
31+++ b/mindspore/lite/src/common/hi_app_event/handler_thread.cc
32@@ -15,6 +15,7 @@
33  */
34 
35 #include "handler_thread.h"
36+#include "src/common/log.h"
37 
38 namespace mindspore {
39 namespace lite {
40@@ -121,8 +122,8 @@ public:
41 
42 bool HandlerThread::Post(std::string key, Runnable && aRunnable)
43 {
44-  if (not Running()) {
45-    std::cout << "Denying insertion, as the looper is not running.\n";
46+  if (!Running()) {
47+    MS_LOG(ERROR) << "Denying insertion, as the looper is not running.";
48     return false;
49   }
50 
51diff --git a/mindspore/lite/src/litert/c_api/context_c.cc b/mindspore/lite/src/litert/c_api/context_c.cc
52index 5418c46a..92085960 100644
53--- a/mindspore/lite/src/litert/c_api/context_c.cc
54+++ b/mindspore/lite/src/litert/c_api/context_c.cc
55@@ -41,6 +41,7 @@ OH_AI_ContextHandle OH_AI_ContextCreate() {
56   if (impl->context_ == nullptr) {
57     MS_LOG(ERROR) << "memory allocation failed.";
58     delete impl;
59+    return nullptr;
60   }
61   impl->owned_by_model_ = false;
62   return static_cast<OH_AI_ContextHandle>(impl);
63@@ -536,8 +537,8 @@ OH_AI_API OH_AI_Status OH_AI_DeviceInfoAddExtension(OH_AI_DeviceInfoHandle devic
64     return OH_AI_STATUS_LITE_ERROR;
65   }
66   static std::vector<std::string> extension_keys = {"CachePath", "CacheVersion", "ModelName", "QuantBuffer",
67-                                                     "isProfiling", "opLayout", "InputDims", "DynamicDims", "BandMode",
68-                                                     "NPU_FM_SHARED"};
69+                                                    "QuantConfigData", "isProfiling", "opLayout", "InputDims",
70+                                                    "DynamicDims", "BandMode", "NPU_FM_SHARED"};
71   auto it = std::find(extension_keys.begin(), extension_keys.end(), std::string(name));
72   if (it == extension_keys.end()) {
73     MS_LOG(ERROR) << "The name of the extension is not allowable, only can be one of {CachePath, CacheVersion,"
74diff --git a/mindspore/lite/src/litert/delegate/nnrt/extension_options_parser.cc b/mindspore/lite/src/litert/delegate/nnrt/extension_options_parser.cc
75index 6e4c57a4..b2a01592 100644
76--- a/mindspore/lite/src/litert/delegate/nnrt/extension_options_parser.cc
77+++ b/mindspore/lite/src/litert/delegate/nnrt/extension_options_parser.cc
78@@ -30,6 +30,7 @@ const std::string kCachePath = "CachePath";
79 const std::string kCacheVersion = "CacheVersion";
80 const std::string kBandMode = "BandMode";
81 const std::string kQuantBuffer = "QuantBuffer";
82+const std::string kQuantConfigData = "QuantConfigData";
83 const std::string kModelName = "ModelName";
84 }  // namespace
85 
86@@ -83,7 +84,7 @@ void ExtensionOptionsParser::DoParseQuantConfig(const std::vector<Extension> &ex
87   MS_CHECK_TRUE_RET_VOID(quant_config != nullptr);
88   MS_CHECK_TRUE_RET_VOID(num != nullptr);
89   auto iter_config = std::find_if(extensions.begin(), extensions.end(), [](const Extension &extension) {
90-    return extension.name == kQuantBuffer;
91+    return extension.name == kQuantBuffer || extension.name == kQuantConfigData;
92   });
93   if (iter_config != extensions.end()) {
94     *quant_config = static_cast<void *>(const_cast<uint8_t *>(iter_config->value.data()));
95diff --git a/mindspore/lite/src/litert/inner_allocator.cc b/mindspore/lite/src/litert/inner_allocator.cc
96index 9bd0c017..c80c7de9 100644
97--- a/mindspore/lite/src/litert/inner_allocator.cc
98+++ b/mindspore/lite/src/litert/inner_allocator.cc
99@@ -54,11 +54,11 @@ void *DefaultAllocator::Malloc(size_t size) {
100     MS_LOG(ERROR) << "MallocData out of max_size, size: " << size;
101     return nullptr;
102   }
103+  Lock();
104   if (this->total_size_ >= max_malloc_size_) {
105     MS_LOG(ERROR) << "Memory pool is exhausted";
106     return nullptr;
107   }
108-  Lock();
109   auto iter = freeList_.lower_bound(size);
110   if (iter != freeList_.end() && ReuseMemory(iter->second->size, size)) {
111     auto membuf = iter->second;
112diff --git a/mindspore/lite/src/litert/kernel/cpu/base/scatter_nd_binary.cc b/mindspore/lite/src/litert/kernel/cpu/base/scatter_nd_binary.cc
113index 0c98fedc..23e6b4cd 100644
114--- a/mindspore/lite/src/litert/kernel/cpu/base/scatter_nd_binary.cc
115+++ b/mindspore/lite/src/litert/kernel/cpu/base/scatter_nd_binary.cc
116@@ -31,6 +31,14 @@ int ScatterNDBinaryCPUKernel::Prepare() {
117   return ReSize();
118 }
119 
120+/**
121+ * ScatterND equation is like follows:
122+ *
123+ *    output = np.copy(input)
124+ *    update_indices = indices.shape[:-1]
125+ *    for idx in np.ndindex(update_indices):
126+ *        output[indices[idx]] = updates[idx]
127+ */
128 int ScatterNDBinaryCPUKernel::ReSize() {
129   auto input = in_tensors_.at(kScatterUpdateInputIndex);
130   auto indices = in_tensors_.at(kScatterIndicesIndex);
131@@ -90,7 +98,13 @@ int ScatterNDBinaryCPUKernel::ReSize() {
132     for (int i = 0; i < param_->num_unit; i++) {
133       int tmp_stride = 0;
134       for (int j = 0; j < indices_unit_rank; j++) {
135-        tmp_stride += indices_data[i * indices_unit_rank + j] * out_strides.at(j) * param_->unit_size;
136+        int index = indices_data[i * indices_unit_rank + j];
137+        if ((index < -input_shape[j]) || (index >= input_shape[j])) {
138+          MS_LOG(ERROR) << "illegal index: " << index << ", should in [" << -input_shape[j] << ", " << input_shape[j]
139+                        << ")";
140+          return RET_ERROR;
141+        }
142+        tmp_stride += index * out_strides.at(j) * param_->unit_size;
143       }
144       output_unit_offsets_.push_back(tmp_stride);
145     }
146@@ -99,7 +113,13 @@ int ScatterNDBinaryCPUKernel::ReSize() {
147     for (int i = 0; i < param_->num_unit; i++) {
148       int tmp_stride = 0;
149       for (int j = 0; j < indices_unit_rank; j++) {
150-        tmp_stride += indices_data[i * indices_unit_rank + j] * out_strides.at(j) * param_->unit_size;
151+        int64_t index = indices_data[i * indices_unit_rank + j];
152+        if ((index < -input_shape[j]) || (index >= input_shape[j])) {
153+          MS_LOG(ERROR) << "illegal index: " << index << ", should in [" << -input_shape[j] << ", " << input_shape[j]
154+                        << ")";
155+          return RET_ERROR;
156+        }
157+        tmp_stride += index * out_strides.at(j) * param_->unit_size;
158       }
159       output_unit_offsets_.push_back(tmp_stride);
160     }
161diff --git a/mindspore/lite/src/litert/lite_session.cc b/mindspore/lite/src/litert/lite_session.cc
162index 7502ec27..69c292e9 100644
163--- a/mindspore/lite/src/litert/lite_session.cc
164+++ b/mindspore/lite/src/litert/lite_session.cc
165@@ -1104,7 +1104,6 @@ int LiteSession::CreateNNRTDelegate() {
166     MS_LOG(ERROR) << "New NNRT delegate failed";
167     return RET_ERROR;
168   }
169-//  ((NNRTDelegate *)(delegate_.get()))->SetMetaGraph(this->model_->buf);
170   delegate_device_type_ = DT_NNRT;
171   this->context_->delegate = delegate_;
172 #endif
173-- 
1742.17.1
175
176