[ML][single] SingleShot.invoke implementation added
[platform/core/api/webapi-plugins.git] / src / ml / ml_single_manager.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "ml_single_manager.h"
18 #include "common/tools.h"
19
20 using common::PlatformResult;
21 using common::ErrorCode;
22
23 namespace extension {
24 namespace ml {
25
26 SingleManager::SingleManager(TensorsInfoManager* tim) : nextId_{0}, tim_{tim} {
27   ScopeLogger();
28 }
29
30 SingleManager::~SingleManager() {
31   ScopeLogger();
32 }
33
34 // MachineLearningSingle::openModel()
35 PlatformResult SingleManager::OpenModel(const std::string& modelPath, TensorsInfo* inTensorsInfo,
36                                         TensorsInfo* outTensorsInfo, ml_nnfw_type_e nnfw_e,
37                                         ml_nnfw_hw_e hw_e, bool isDynamicMode, int* res_id) {
38   ScopeLogger();
39
40   ml_single_h handle = nullptr;
41
42   ml_tensors_info_h in_info = inTensorsInfo ? inTensorsInfo->Handle() : nullptr;
43   ml_tensors_info_h out_info = outTensorsInfo ? outTensorsInfo->Handle() : nullptr;
44
45   int ret = ml_single_open(&handle, modelPath.c_str(), in_info, out_info, nnfw_e, hw_e);
46   if (ML_ERROR_NONE != ret) {
47     LoggerE("ml_single_open failed: %d (%s)", ret, get_error_message(ret));
48     return util::ToPlatformResult(ret, "Failed to open model");
49   }
50
51   std::lock_guard<std::mutex> singles_lock(singles_mutex_);
52   int id = nextId_++;
53   singles_[id] = std::make_unique<SingleShot>(id, handle, isDynamicMode);
54   *res_id = id;
55   return PlatformResult{};
56 }
57
58 // SingleShot input
59 SingleShot* SingleManager::GetSingleShot(int id) {
60   ScopeLogger("id: %d", id);
61
62   std::lock_guard<std::mutex> singles_lock(singles_mutex_);
63   if (singles_.end() != singles_.find(id)) {
64     return singles_[id].get();
65   }
66
67   return nullptr;
68 }
69
70 PlatformResult SingleManager::GetNativeTensorsInfo(int id, bool get_input_mode, int* res_id) {
71   ScopeLogger();
72
73   SingleShot* single = GetSingleShot(id);
74   if (!single) {
75     LoggerE("Could not find singleShot handle");
76     return PlatformResult(ErrorCode::ABORT_ERR);
77   }
78
79   ml_tensors_info_h in_info = nullptr;
80   PlatformResult ret = single->GetTensorsInfo(get_input_mode, &in_info);
81   if (!ret) {
82     return ret;
83   }
84
85   auto tensor_info = tim_->CreateTensorsInfo(in_info);
86   *res_id = tensor_info->Id();
87   return PlatformResult{};
88 }
89
90 PlatformResult SingleManager::SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo) {
91   ScopeLogger();
92
93   SingleShot* single = GetSingleShot(id);
94   if (!single) {
95     LoggerE("Could not find singleShot handle");
96     return PlatformResult(ErrorCode::ABORT_ERR);
97   }
98
99   ml_tensors_info_h in_info = inTensorsInfo ? inTensorsInfo->Handle() : nullptr;
100
101   PlatformResult ret = single->SetInputInfo(in_info);
102   if (!ret) {
103     return ret;
104   }
105
106   return PlatformResult{};
107 }
108
109 PlatformResult SingleManager::Invoke(int id, TensorsData* in_tensors_data,
110                                      TensorsData** out_tensors_data) {
111   ScopeLogger();
112
113   SingleShot* single = GetSingleShot(id);
114   if (!single) {
115     LoggerE("Could not find SingleShot handle");
116     return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
117   }
118
119   ml_tensors_info_h out_tensors_info_h = nullptr;
120   ml_tensors_data_h out_tensors_data_h = nullptr;
121   bool should_copy_data = false;
122   PlatformResult result =
123       single->Invoke(in_tensors_data->Handle(), in_tensors_data->GetTensorsInfo()->Handle(),
124                      &out_tensors_data_h, &out_tensors_info_h, &should_copy_data);
125   if (!result) {
126     return result;
127   }
128   if (should_copy_data) {
129     *out_tensors_data = tim_->CloneNativeTensorWithData(out_tensors_info_h, out_tensors_data_h);
130   } else {
131     *out_tensors_data = tim_->CreateTensorsData(out_tensors_info_h, out_tensors_data_h);
132   }
133
134   if (*out_tensors_data == nullptr) {
135     LoggerE("out_tensors_data creation failed");
136     result = single->CleanUpAfterInvoke();
137     if (!result) {
138       LoggerE("CleanUpAfterInvoke failed");
139     }
140     return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
141   }
142
143   return PlatformResult{};
144 }
145
146 PlatformResult SingleManager::GetValue(int id, const std::string& name, std::string& value) {
147   ScopeLogger();
148
149   SingleShot* single = GetSingleShot(id);
150   if (!single) {
151     LoggerE("Could not find SingleShot handle");
152     return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
153   }
154
155   return single->GetValue(name, value);
156 }
157
158 PlatformResult SingleManager::SetValue(int id, const std::string& name, const std::string& value) {
159   ScopeLogger();
160
161   SingleShot* single = GetSingleShot(id);
162   if (!single) {
163     LoggerE("Could not find SingleShot handle");
164     return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
165   }
166
167   return single->SetValue(name, value);
168 }
169
170 // SingleShot::setTimeout()
171 // SingleShot::close()
172
173 }  // namespace ml
174 }  // namespace extension