• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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_CORE_MINDAPI_IR_UTILS_H_
18 #define MINDSPORE_CORE_MINDAPI_IR_UTILS_H_
19 
20 #include "mindapi/base/base.h"
21 #include "mindapi/base/shared_ptr.h"
22 #include "mindapi/base/type_traits.h"
23 #include "mindapi/ir/anf.h"
24 #include "mindapi/ir/value.h"
25 #include "mindapi/ir/func_graph.h"
26 
27 namespace mindspore::api::utils {
28 /// \brief Check whether the given object is an instance of the given class.
29 ///
30 /// \param[in] ptr The pointer to the given object.
31 ///
32 /// \return True if the pointer is not null and the object is an instance of the given class, false otherwise.
33 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<Base, T> || is_wrapper_ptr<T>::value>>
isa(const BasePtr & ptr)34 inline bool isa(const BasePtr &ptr) {
35   if (ptr == nullptr) {
36     return false;
37   }
38   if constexpr (is_wrapper_ptr<T>::value) {
39     return ptr->isa<typename T::element_type>();
40   } else {
41     return ptr->isa<T>();
42   }
43 }
44 
45 /// \brief Check whether the given object is an value of the given c++ type T.
46 ///
47 /// \param[in] ptr The pointer to the given value object.
48 ///
49 /// \return True if the pointer is not null and it is an value of the given c++ type, false otherwise.
50 template <typename T, typename U = typename ImmTrait<T>::type::element_type>
isa(const ValuePtr & ptr)51 inline bool isa(const ValuePtr &ptr) {
52   if (ptr == nullptr) {
53     return false;
54   }
55   return ptr->isa<U>();
56 }
57 
58 /// \brief Cast the given object pointer to a pointer with the given class.
59 ///
60 /// \param[in] ptr The pointer to the object to casted.
61 ///
62 /// \return A non-null pointer if the input pointer is not null and cast success, nullptr otherwise.
63 template <typename T, typename = typename std::enable_if_t<is_wrapper_ptr<T>::value, T>>
cast(const BasePtr & ptr)64 inline T cast(const BasePtr &ptr) {
65   if (ptr == nullptr) {
66     return nullptr;
67   }
68   return ptr->cast<T>();
69 }
70 
71 /// \brief Cast the given value to a C++ value.
72 ///
73 /// \param[in] ptr The pointer to the value to be casted.
74 ///
75 /// \return The C++ value according the input value.
76 template <typename T, typename U = typename ImmTrait<T>::type>
cast(const ValuePtr & ptr)77 inline T cast(const ValuePtr &ptr) {
78   return GetValue<T>(ptr);
79 }
80 
81 /// \brief Make a copy from the given function graph.
82 ///
83 /// \param[in] func_graph The graph to be cloned.
84 ///
85 /// \return The cloned graph.
86 MIND_API FuncGraphPtr CloneGraph(const FuncGraphPtr &func_graph);
87 
88 /// \brief Get pad mode id from a value holds the pad mode name or id.
89 ///
90 /// \param[in] value The value holds the pad mode name or id.
91 /// \param[in] is_upper Indicates whether the name is uppercase or lowercase, default is false for lowercase.
92 ///
93 /// \return The pad mode id.
94 MIND_API int64_t GetPadMode(const ValuePtr &value, bool is_upper = false);
95 }  // namespace mindspore::api::utils
96 
97 #endif  // MINDSPORE_CORE_MINDAPI_IR_UTILS_H_
98