You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_utils.h 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright 2019-2020 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_CCSRC_UTILS_CONVERT_UTILS_H_
  17. #define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
  18. #include <limits>
  19. #include <memory>
  20. #include <utility>
  21. #include <stack>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include "pybind11/pybind11.h"
  25. #include "utils/convert_utils_base.h"
  26. #include "utils/any.h"
  27. #include "utils/base_ref.h"
  28. #include "ir/base.h"
  29. #include "ir/anf.h"
  30. namespace py = pybind11;
  31. namespace mindspore {
  32. namespace tensor {
  33. class Tensor;
  34. using TensorPtr = std::shared_ptr<Tensor>;
  35. } // namespace tensor
  36. py::object AnyToPyData(const Any &value);
  37. py::object BaseRefToPyData(const BaseRef &value);
  38. bool BaseRefToBool(const BaseRef &in, bool *out);
  39. bool BaseRefToInt(const ValuePtr &v, int *value);
  40. bool ValueToBool(const ValuePtr &in, bool *out);
  41. py::object ValuePtrToPyData(const ValuePtr &value);
  42. AbstractBasePtr PyListDtype2AbstractTensor(const py::object &shape_obj, const py::object &type_obj);
  43. bool IsGraphOutputValueNodeOrParameter(const AnfNodePtr &output, const py::tuple &args,
  44. const std::shared_ptr<py::object> &ret_val);
  45. // Isomorphism
  46. struct PairHasher {
  47. template <class T1, class T2>
  48. std::size_t operator()(const std::pair<T1, T2> &p) const {
  49. auto h1 = std::hash<T1>{}(p.first);
  50. auto h2 = std::hash<T2>{}(p.second);
  51. return h1 ^ h2;
  52. }
  53. };
  54. enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
  55. using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
  56. using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
  57. bool Isomorphic(FuncGraphPtr g1, FuncGraphPtr g2, FuncGraphPairMapEquiv *equiv_func_graph, NodeMapEquiv *equiv_node);
  58. tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
  59. } // namespace mindspore
  60. #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_