• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2022 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 
17 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_PY_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_PY_H_
19 
20 #include <memory>
21 #include <utility>
22 #include <string>
23 #include <vector>
24 #include <unordered_set>
25 
26 #include "pybind11/pybind11.h"
27 #include "utils/convert_utils_base.h"
28 #include "utils/any.h"
29 #include "base/base_ref.h"
30 #include "base/base.h"
31 #include "ir/anf.h"
32 #include "ir/tensor.h"
33 #include "include/common/visible.h"
34 
35 namespace py = pybind11;
36 
37 namespace mindspore {
38 // Detect recursion for python object sych as a = [..., 1].
39 class PyRecursionScope {
40  public:
PyRecursionScope(const py::object & obj)41   explicit PyRecursionScope(const py::object &obj) {
42     address_ = obj.ptr();
43     if (!recursion_set_.insert(address_).second) {
44       MS_LOG(EXCEPTION) << "Detect recursion when converting python object.";
45     }
46   }
47 
~PyRecursionScope()48   ~PyRecursionScope() { recursion_set_.erase(address_); }
49 
50  private:
51   PyObject *address_;
52   static inline std::unordered_set<PyObject *> recursion_set_;
53 };
54 
55 py::object AnyToPyData(const Any &value);
56 COMMON_EXPORT py::object BaseRefToPyData(const BaseRef &value, const AbstractBasePtr &abs = nullptr);
57 COMMON_EXPORT py::object ValueToPyData(const ValuePtr &value, const AbstractBasePtr &abs = nullptr);
58 COMMON_EXPORT bool IsStubTensor(const py::handle &obj);
59 COMMON_EXPORT tensor::TensorPtr ConvertStubTensor(const py::handle &obj);
60 COMMON_EXPORT ValuePtr PyStubNodeCast(const py::handle &obj);
61 COMMON_EXPORT std::pair<ShapeVector, TypePtr> GetStubTensorInfo(const py::handle &obj);
62 COMMON_EXPORT bool IsGraphOutputValueNodeOrParameter(const AnfNodePtr &output, const py::tuple &args,
63                                                      const std::shared_ptr<py::object> &ret_val);
64 COMMON_EXPORT ValuePtr ShallowCopyTensorValue(const ValuePtr &value);
65 COMMON_EXPORT void ConvertPyObjectToTensor(const py::object &input_object, std::vector<ValuePtr> *tensors);
66 COMMON_EXPORT void ConvertCTensorToPyTensor(const py::tuple &input_args, py::tuple *convert_args);
67 COMMON_EXPORT std::string ConvertPyObjToString(const py::object &obj);
68 bool IsStubTensor(const py::handle &obj);
69 tensor::TensorPtr ConvertStubTensor(const py::handle &obj);
70 }  // namespace mindspore
71 
72 #endif  // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_PY_H_
73