• 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 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_H_
17 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_H_
18 
19 #include <limits>
20 #include <memory>
21 #include <utility>
22 #include <stack>
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <set>
27 #include "utils/hash_map.h"
28 #include "utils/hash_set.h"
29 #include "utils/convert_utils_base.h"
30 #include "utils/any.h"
31 #include "base/base_ref.h"
32 #include "base/base.h"
33 #include "ir/anf.h"
34 #include "ir/func_graph.h"
35 #include "ir/kernel_tensor_value.h"
36 #include "include/common/visible.h"
37 #include "mindspore/core/utils/simple_info.h"
38 
39 namespace mindspore {
40 namespace tensor {
41 class Tensor;
42 }  // namespace tensor
43 
44 COMMON_EXPORT bool BaseRefToBool(const BaseRef &in, bool *out);
45 COMMON_EXPORT bool BaseRefToInt(const ValuePtr &v, int64_t *value);
46 COMMON_EXPORT bool ValueToBool(const ValuePtr &in, bool *out);
47 
48 // Isomorphism
49 struct PairHasher {
50   template <class T1, class T2>
operatorPairHasher51   std::size_t operator()(const std::pair<T1, T2> &p) const {
52     auto h1 = std::hash<T1>{}(p.first);
53     auto h2 = std::hash<T2>{}(p.second);
54     return h1 ^ h2;
55   }
56 };
57 
58 enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
59 
60 using FuncGraphPairMapEquiv = mindspore::HashMap<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
61 using NodeMapEquiv = mindspore::HashMap<AnfNodePtr, AnfNodePtr>;
62 
63 COMMON_EXPORT bool Isomorphic(const FuncGraphPtr &g1, const FuncGraphPtr &g2, FuncGraphPairMapEquiv *equiv_func_graph,
64                               NodeMapEquiv *equiv_node);
65 
66 COMMON_EXPORT tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
67 
68 COMMON_EXPORT tensor::TensorPtr SequenceToTensor(const ValueSequencePtr &sequence);
69 
70 COMMON_EXPORT ValuePtr CreateValueFromTensor(const tensor::TensorPtr &tensor);
71 
72 template <typename T>
TensorValueToVector(const tensor::TensorPtr & tensor)73 std::vector<T> TensorValueToVector(const tensor::TensorPtr &tensor) {
74   MS_EXCEPTION_IF_NULL(tensor);
75   std::vector<T> value;
76   auto element_size = tensor->data().size();
77   auto *data = static_cast<T *>(tensor->data_c());
78   for (auto i = 0; i < element_size; i++) {
79     value.push_back(data[i]);
80   }
81   return value;
82 }
83 
84 COMMON_EXPORT void TensorValueToTensor(const ValuePtr &value, std::vector<tensor::BaseTensorPtr> *tensors);
85 
86 COMMON_EXPORT size_t CountValueNum(const ValueSequencePtr &value_sequence);
87 
88 COMMON_EXPORT bool IsAKGSparseOP(const AnfNodePtr &cnode);
89 
90 COMMON_EXPORT KernelTensorValuePtr ConvertValueToKernelTensorValue(const ValuePtr &value);
91 
92 COMMON_EXPORT tensor::MetaSparseTensorPtr TensorListToSparseTensor(const abstract::AbstractBasePtr &abs_sparse,
93                                                                    const tensor::TensorPtrList &tensor_list);
94 // Convert base shape to shape vector, support the tuple shape.
95 COMMON_EXPORT std::vector<ShapeVector> BaseShapeToShapeVector(const abstract::BaseShapePtr &base_shape);
96 // Convert base shape to shape, not support the tuple shape.
97 COMMON_EXPORT ShapeVector BaseShapeToShape(const abstract::BaseShapePtr &base_shape);
98 
99 COMMON_EXPORT ValuePtr UpdateValueByAttrDataType(const ValuePtr &value, const std::string &attr_data_type);
100 
101 COMMON_EXPORT std::map<SignatureEnumDType, std::pair<TypeId, bool>> GetSignatureTypeMap(
102   const std::vector<SignatureEnumDType> &dtypes, const std::vector<TypeId> &args_type_id,
103   const std::vector<bool> &args_is_tensor, const std::set<size_t> &write_indices = {});
104 
105 COMMON_EXPORT std::string ValueSimpleInfoToString(const ValueSimpleInfo &value_simple_info);
106 
107 COMMON_EXPORT abstract::AbstractBasePtr TransformValueSimpleInfoToAbstract(const ValueSimpleInfo &value_simple_info);
108 }  // namespace mindspore
109 
110 #endif  // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CONVERT_UTILS_H_
111