1From 551353a01bc269ac3a509b916bfe28499d1db714 Mon Sep 17 00:00:00 2001
2From: chengfeng27 <chengfeng27@huawei.com>
3Date: Thu, 1 Aug 2024 20:49:49 +0800
4Subject: [PATCH] remove recursive lock, which conflict with ffrt
5
6---
7 .../src/litert/cxx_api/model/model_impl.cc    | 26 -------------------
8 .../src/litert/cxx_api/model/model_impl.h     |  1 -
9 2 files changed, 27 deletions(-)
10
11diff --git a/mindspore/lite/src/litert/cxx_api/model/model_impl.cc b/mindspore/lite/src/litert/cxx_api/model/model_impl.cc
12index cacbf86e..6a73a927 100644
13--- a/mindspore/lite/src/litert/cxx_api/model/model_impl.cc
14+++ b/mindspore/lite/src/litert/cxx_api/model/model_impl.cc
15@@ -161,7 +161,6 @@ bool ModelImpl::IsEnablePreInference() {
16 #endif
17 Status ModelImpl::Build(const void *model_data, size_t data_size, ModelType model_type,
18                         const std::shared_ptr<Context> &ms_context) {
19-  std::lock_guard<std::recursive_mutex> lock(mutex_);
20   if (session_ != nullptr) {
21     MS_LOG(ERROR) << "Model has been called Build";
22     return kLiteModelRebuild;
23@@ -207,7 +206,6 @@ Status ModelImpl::Build(const void *model_data, size_t data_size, ModelType mode
24 
25 Status ModelImpl::Build(const std::string &model_path, ModelType model_type,
26                         const std::shared_ptr<Context> &ms_context) {
27-  std::lock_guard<std::recursive_mutex> lock(mutex_);
28   if (session_ != nullptr) {
29     MS_LOG(ERROR) << "Model has been called Build";
30     return kLiteModelRebuild;
31@@ -243,7 +241,6 @@ Status ModelImpl::Build(const std::string &model_path, ModelType model_type,
32 }
33 
34 Status ModelImpl::Build() {
35-  std::lock_guard<std::recursive_mutex> lock(mutex_);
36   if (session_ != nullptr) {
37     MS_LOG(ERROR) << "Model has been called Build";
38     return kLiteModelRebuild;
39@@ -357,7 +354,6 @@ Status ModelImpl::RunGraph(const MSKernelCallBack &before, const MSKernelCallBac
40 bool ModelImpl::IsTrainModel() { return (graph_ && graph_->graph_data_ && graph_->graph_data_->IsTrainModel()); }
41 
42 Status ModelImpl::LoadConfig(const std::string &config_path) {
43-  std::lock_guard<std::recursive_mutex> lock(mutex_);
44   if (session_ != nullptr) {
45     MS_LOG(ERROR) << "Model has been called Build, please call LoadConfig before Build.";
46     return kLiteError;
47@@ -380,7 +376,6 @@ Status ModelImpl::LoadConfig(const std::string &config_path) {
48 }
49 
50 Status ModelImpl::UpdateConfig(const std::string &section, const std::pair<std::string, std::string> &config) {
51-  std::lock_guard<std::recursive_mutex> lock(mutex_);
52   auto iter = config_info_.find(section);
53   if (iter == config_info_.end()) {
54     if (config_info_.size() >= kMaxSectionNum) {
55@@ -400,7 +395,6 @@ Status ModelImpl::UpdateConfig(const std::string &section, const std::pair<std::
56 
57 Status ModelImpl::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs,
58                           const MSKernelCallBack &before, const MSKernelCallBack &after) {
59-  std::lock_guard<std::recursive_mutex> lock(mutex_);
60   if (session_ == nullptr) {
61     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
62     return kLiteNullptr;
63@@ -565,7 +559,6 @@ Status ModelImpl::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTen
64 }
65 
66 Status ModelImpl::Predict(const MSKernelCallBack &before, const MSKernelCallBack &after) {
67-  std::lock_guard<std::recursive_mutex> lock(mutex_);
68   if (session_ == nullptr) {
69     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
70     return kLiteNullptr;
71@@ -592,7 +585,6 @@ Status ModelImpl::Predict(const MSKernelCallBack &before, const MSKernelCallBack
72 }
73 
74 std::vector<MSTensor> ModelImpl::GetInputs() {
75-  std::lock_guard<std::recursive_mutex> lock(mutex_);
76   if (session_ == nullptr) {
77     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
78     return {};
79@@ -617,7 +609,6 @@ std::vector<MSTensor> ModelImpl::GetInputs() {
80 }
81 
82 std::vector<MSTensor> ModelImpl::GetOutputs() {
83-  std::lock_guard<std::recursive_mutex> lock(mutex_);
84   if (session_ == nullptr) {
85     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
86     return {};
87@@ -655,7 +646,6 @@ std::vector<MSTensor> ModelImpl::GetOutputs() {
88 }
89 
90 std::vector<MSTensor> ModelImpl::GetGradients() const {
91-  std::lock_guard<std::recursive_mutex> lock(mutex_);
92   if (session_ == nullptr) {
93     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
94     return {};
95@@ -670,7 +660,6 @@ std::vector<MSTensor> ModelImpl::GetGradients() const {
96 }
97 
98 Status ModelImpl::ApplyGradients(const std::vector<MSTensor> &gradients) {
99-  std::lock_guard<std::recursive_mutex> lock(mutex_);
100   if (session_ == nullptr) {
101     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
102     return kLiteNullptr;
103@@ -699,7 +688,6 @@ Status ModelImpl::ApplyGradients(const std::vector<MSTensor> &gradients) {
104 }
105 
106 std::vector<MSTensor> ModelImpl::GetFeatureMaps() const {
107-  std::lock_guard<std::recursive_mutex> lock(mutex_);
108   if (session_ == nullptr) {
109     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
110     return {};
111@@ -714,7 +702,6 @@ std::vector<MSTensor> ModelImpl::GetFeatureMaps() const {
112 }
113 
114 std::vector<MSTensor> ModelImpl::GetTrainableParams() const {
115-  std::lock_guard<std::recursive_mutex> lock(mutex_);
116   if (session_ == nullptr) {
117     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
118     return {};
119@@ -729,7 +716,6 @@ std::vector<MSTensor> ModelImpl::GetTrainableParams() const {
120 }
121 
122 Status ModelImpl::UpdateFeatureMaps(const std::vector<MSTensor> &new_weights) {
123-  std::lock_guard<std::recursive_mutex> lock(mutex_);
124   if (session_ == nullptr) {
125     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
126     return kLiteNullptr;
127@@ -758,7 +744,6 @@ Status ModelImpl::UpdateFeatureMaps(const std::vector<MSTensor> &new_weights) {
128 }
129 
130 std::vector<MSTensor> ModelImpl::GetOptimizerParams() const {
131-  std::lock_guard<std::recursive_mutex> lock(mutex_);
132   if (session_ == nullptr) {
133     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
134     return {};
135@@ -773,7 +758,6 @@ std::vector<MSTensor> ModelImpl::GetOptimizerParams() const {
136 }
137 
138 Status ModelImpl::SetOptimizerParams(const std::vector<MSTensor> &params) {
139-  std::lock_guard<std::recursive_mutex> lock(mutex_);
140   if (session_ == nullptr) {
141     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
142     return kLiteNullptr;
143@@ -802,7 +786,6 @@ Status ModelImpl::SetOptimizerParams(const std::vector<MSTensor> &params) {
144 }
145 
146 MSTensor ModelImpl::GetInputByTensorName(const std::string &name) {
147-  std::lock_guard<std::recursive_mutex> lock(mutex_);
148   if (session_ == nullptr) {
149     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
150     return MSTensor(nullptr);
151@@ -822,7 +805,6 @@ MSTensor ModelImpl::GetInputByTensorName(const std::string &name) {
152 }
153 
154 std::vector<std::string> ModelImpl::GetOutputTensorNames() {
155-  std::lock_guard<std::recursive_mutex> lock(mutex_);
156   if (session_ == nullptr) {
157     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
158     return {};
159@@ -831,7 +813,6 @@ std::vector<std::string> ModelImpl::GetOutputTensorNames() {
160 }
161 
162 MSTensor ModelImpl::GetOutputByTensorName(const std::string &name) {
163-  std::lock_guard<std::recursive_mutex> lock(mutex_);
164   if (session_ == nullptr) {
165     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
166     return MSTensor(nullptr);
167@@ -851,7 +832,6 @@ MSTensor ModelImpl::GetOutputByTensorName(const std::string &name) {
168 }
169 
170 std::vector<MSTensor> ModelImpl::GetOutputsByNodeName(const std::string &name) {
171-  std::lock_guard<std::recursive_mutex> lock(mutex_);
172   if (session_ == nullptr) {
173     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
174     return {};
175@@ -881,7 +861,6 @@ std::vector<MSTensor> ModelImpl::GetOutputsByNodeName(const std::string &name) {
176 
177 Status ModelImpl::BindGLTexture2DMemory(const std::map<std::string, unsigned int> &inputGLTexture,
178                                         std::map<std::string, unsigned int> *outputGLTexture) {
179-  std::lock_guard<std::recursive_mutex> lock(mutex_);
180   MS_LOG(INFO) << "Bind GLTexture2D to Input MsTensors and Output MsTensors";
181   if (session_ == nullptr) {
182     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
183@@ -896,7 +875,6 @@ Status ModelImpl::BindGLTexture2DMemory(const std::map<std::string, unsigned int
184 }
185 
186 Status ModelImpl::Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) {
187-  std::lock_guard<std::recursive_mutex> lock(mutex_);
188   if (session_ == nullptr) {
189     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
190     return kLiteNullptr;
191@@ -950,7 +928,6 @@ Status ModelImpl::Resize(const std::vector<MSTensor> &inputs, const std::vector<
192 }
193 
194 Status ModelImpl::UpdateWeights(const std::vector<MSTensor> &new_weights) {
195-  std::lock_guard<std::recursive_mutex> lock(mutex_);
196   if (session_ == nullptr) {
197     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
198     return kLiteNullptr;
199@@ -982,7 +959,6 @@ Status ModelImpl::UpdateWeights(const std::vector<MSTensor> &new_weights) {
200 }
201 
202 Status ModelImpl::SetupVirtualBatch(int virtual_batch_multiplier, float lr, float momentum) {
203-  std::lock_guard<std::recursive_mutex> lock(mutex_);
204   if (session_ == nullptr) {
205     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
206     return kLiteNullptr;
207@@ -992,7 +968,6 @@ Status ModelImpl::SetupVirtualBatch(int virtual_batch_multiplier, float lr, floa
208 }
209 
210 Status ModelImpl::SetLearningRate(float learning_rate) {
211-  std::lock_guard<std::recursive_mutex> lock(mutex_);
212   if (session_ == nullptr) {
213     MS_LOG(ERROR) << "Model has not been called Build, or Model Build has failed";
214     return kLiteNullptr;
215@@ -1002,7 +977,6 @@ Status ModelImpl::SetLearningRate(float learning_rate) {
216 }
217 
218 float ModelImpl::GetLearningRate() {
219-  std::lock_guard<std::recursive_mutex> lock(mutex_);
220   if (session_ == nullptr) {
221     MS_LOG(WARNING) << "Model has not been called Build, or Model Build has failed";
222     return 0.0;
223diff --git a/mindspore/lite/src/litert/cxx_api/model/model_impl.h b/mindspore/lite/src/litert/cxx_api/model/model_impl.h
224index 8e11ee55..17cafba8 100644
225--- a/mindspore/lite/src/litert/cxx_api/model/model_impl.h
226+++ b/mindspore/lite/src/litert/cxx_api/model/model_impl.h
227@@ -131,7 +131,6 @@ class ModelImpl {
228   std::shared_ptr<Context> context_ = nullptr;
229   std::shared_ptr<TrainCfg> cfg_ = nullptr;
230   std::vector<Metrics *> metrics_;
231-  mutable std::recursive_mutex mutex_;
232   void SetGraph(const std::shared_ptr<Graph> &graph) { graph_ = graph; }
233   void SetContext(const std::shared_ptr<Context> &context) { context_ = context; }
234   void SetConfig(const std::shared_ptr<TrainCfg> cfg) { cfg_ = cfg; }
235-- 
2362.17.1
237
238