• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
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 #ifndef MINDSPORE_LITE_PYTHON_SRC_COMMON_PYBIND_H_
17 #define MINDSPORE_LITE_PYTHON_SRC_COMMON_PYBIND_H_
18 
19 #include <vector>
20 #include <algorithm>
21 #include <memory>
22 #include "include/api/types.h"
23 #include "src/common/log_adapter.h"
24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
26 
27 namespace mindspore::lite {
28 namespace py = pybind11;
29 using MSTensorPtr = std::shared_ptr<MSTensor>;
MSTensorPtrToMSTensor(const std::vector<MSTensorPtr> & tensors_ptr)30 static inline std::vector<MSTensor> MSTensorPtrToMSTensor(const std::vector<MSTensorPtr> &tensors_ptr) {
31   std::vector<MSTensor> tensors;
32   for (auto &item : tensors_ptr) {
33     if (item == nullptr) {
34       MS_LOG(ERROR) << "Tensor object cannot be nullptr";
35       return {};
36     }
37     tensors.push_back(*item);
38   }
39   return tensors;
40 }
41 
MSTensorToMSTensorPtr(const std::vector<MSTensor> & tensors)42 static inline std::vector<MSTensorPtr> MSTensorToMSTensorPtr(const std::vector<MSTensor> &tensors) {
43   std::vector<MSTensorPtr> tensors_ptr;
44   std::transform(tensors.begin(), tensors.end(), std::back_inserter(tensors_ptr),
45                  [](auto &item) { return std::make_shared<MSTensor>(item); });
46   return tensors_ptr;
47 }
48 }  // namespace mindspore::lite
49 #endif  // MINDSPORE_LITE_PYTHON_SRC_COMMON_PYBIND_H_
50