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.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <vector>
  23. #include <unordered_map>
  24. #include <unordered_set>
  25. #include "utils/convert_utils_base.h"
  26. #include "utils/any.h"
  27. #include "base/base_ref.h"
  28. #include "base/base.h"
  29. #include "ir/anf.h"
  30. #include "ir/func_graph.h"
  31. namespace mindspore {
  32. namespace tensor {
  33. class Tensor;
  34. using TensorPtr = std::shared_ptr<Tensor>;
  35. } // namespace tensor
  36. bool BaseRefToBool(const BaseRef &in, bool *out);
  37. bool BaseRefToInt(const ValuePtr &v, int64_t *value);
  38. bool ValueToBool(const ValuePtr &in, bool *out);
  39. // Isomorphism
  40. struct PairHasher {
  41. template <class T1, class T2>
  42. std::size_t operator()(const std::pair<T1, T2> &p) const {
  43. auto h1 = std::hash<T1>{}(p.first);
  44. auto h2 = std::hash<T2>{}(p.second);
  45. return h1 ^ h2;
  46. }
  47. };
  48. enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
  49. using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
  50. using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
  51. bool Isomorphic(const FuncGraphPtr &g1, const FuncGraphPtr &g2, FuncGraphPairMapEquiv *equiv_func_graph,
  52. NodeMapEquiv *equiv_node);
  53. tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
  54. template <typename T>
  55. std::vector<T> TensorValueToVector(const tensor::TensorPtr &tensor) {
  56. MS_EXCEPTION_IF_NULL(tensor);
  57. std::vector<T> value;
  58. auto element_size = tensor->data().size();
  59. auto *data = static_cast<T *>(tensor->data_c());
  60. for (auto i = 0; i < element_size; i++) {
  61. value.push_back(data[i]);
  62. }
  63. return value;
  64. }
  65. void TensorValueToTensor(const ValuePtr &value, std::vector<tensor::TensorPtr> *tensors);
  66. size_t CountValueNum(const ValueTuplePtr &value_tuple);
  67. } // namespace mindspore
  68. #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_