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
17 #ifndef MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
18 #define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
19
20 #include <limits>
21 #include <memory>
22 #include <utility>
23 #include <stack>
24 #include <vector>
25 #include <unordered_map>
26 #include <unordered_set>
27
28 #include "utils/convert_utils_base.h"
29 #include "utils/any.h"
30 #include "base/base_ref.h"
31 #include "base/base.h"
32 #include "ir/anf.h"
33 #include "ir/func_graph.h"
34
35 namespace mindspore {
36 namespace tensor {
37 class Tensor;
38 using TensorPtr = std::shared_ptr<Tensor>;
39 } // namespace tensor
40
41 bool BaseRefToBool(const BaseRef &in, bool *out);
42 bool BaseRefToInt(const ValuePtr &v, int64_t *value);
43 bool ValueToBool(const ValuePtr &in, bool *out);
44
45 // Isomorphism
46 struct PairHasher {
47 template <class T1, class T2>
operatorPairHasher48 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
55 enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
56
57 using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
58 using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
59
60 bool Isomorphic(const FuncGraphPtr &g1, const FuncGraphPtr &g2, FuncGraphPairMapEquiv *equiv_func_graph,
61 NodeMapEquiv *equiv_node);
62
63 tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
64
65 template <typename T>
TensorValueToVector(const tensor::TensorPtr & tensor)66 std::vector<T> TensorValueToVector(const tensor::TensorPtr &tensor) {
67 MS_EXCEPTION_IF_NULL(tensor);
68 std::vector<T> value;
69 auto element_size = tensor->data().size();
70 auto *data = static_cast<T *>(tensor->data_c());
71 for (auto i = 0; i < element_size; i++) {
72 value.push_back(data[i]);
73 }
74 return value;
75 }
76
77 void TensorValueToTensor(const ValuePtr &value, std::vector<tensor::TensorPtr> *tensors);
78
79 size_t CountValueNum(const ValueTuplePtr &value_tuple);
80 } // namespace mindspore
81
82 #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
83