1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2023 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 #ifndef MINDSPORE_PI_JIT_COMPILER_UTILS_H_ 19 #define MINDSPORE_PI_JIT_COMPILER_UTILS_H_ 20 21 #include <string> 22 #include "include/common/utils/python_adapter.h" 23 24 namespace mindspore { 25 namespace pijit { 26 class GraphUtils { 27 public: 28 // object is const when it has attr(const_arg) and the value of attr(const_arg) is true IsConst(const py::object & obj)29 static bool IsConst(const py::object &obj) { return IsAttrEnabled(obj, "const_arg"); } 30 31 // object is mutable when it has attr(__ms_mutable__) and the value of attr(__ms_mutable__) is true IsMutable(const py::object & obj)32 static bool IsMutable(const py::object &obj) { return IsAttrEnabled(obj, "__ms_mutable__"); } 33 34 // object is dynamic length when it has attr(__ms_dynamic_len__) and the value of attr(__ms_dynamic_len__) is true IsDynamicLength(const py::object & obj)35 static bool IsDynamicLength(const py::object &obj) { return IsAttrEnabled(obj, "__ms_dynamic_len__"); } 36 37 // object is enable_tuple_broaden when it has attr(enable_tuple_broaden) and the value of attr(enable_tuple_broaden) 38 // is true IsTupleBroadenEnable(const py::object & obj)39 static bool IsTupleBroadenEnable(const py::object &obj) { return IsAttrEnabled(obj, "enable_tuple_broaden"); } 40 41 // object is has init when it has attr(has_init) and the value of attr(has_init) is true HasInit(const py::object & obj)42 static bool HasInit(const py::object &obj) { return IsAttrEnabled(obj, "has_init"); } 43 44 static bool IsTupleCanBroaden(const py::object &obj); 45 46 static bool IsGradForScalar(const py::object &obj); 47 48 static bool IsTensor(const py::object &obj); 49 50 static bool IsEmptyContainer(const py::object &obj); 51 52 static AbstractBasePtr ArgsToAbstract(const py::object &arg, const ValuePtr &value, bool enable_tuple_broaden = true); 53 54 static PrimitivePtr GetPrimitive(int op_code); 55 56 static AnfNodePtr GetPrimOrMetaFuncGraph(int op_code); 57 58 static std::string OpCompareArgToGraphName(int oparg); 59 60 static std::string OpCodeToGraphName(int op_code); 61 62 static AnfNodePtr GetMetaFuncGraph(int op_code); 63 64 static AnfNodePtr GetMetaFuncGraph(const std::string &name); 65 66 static AnfNodePtr ConvertPythonObjectToAnfNode(const py::object &object); 67 68 private: 69 // object has the attr and the value of attr is true IsAttrEnabled(const py::object & obj,const std::string & attr)70 static bool IsAttrEnabled(const py::object &obj, const std::string &attr) { 71 return py::hasattr(obj, common::SafeCStr(attr)) && py::cast<bool>(py::getattr(obj, common::SafeCStr(attr))); 72 } 73 }; 74 } // namespace pijit 75 } // namespace mindspore 76 #endif // MINDSPORE_PI_JIT_COMPILER_UTILS_H_ 77