1 /** 2 * Copyright 2019 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_PIPELINE_JIT_PARSE_PARSE_BASE_H_ 18 #define MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_PARSE_BASE_H_ 19 #include <string> 20 #include <memory> 21 #include "pybind11/pybind11.h" 22 #include "ir/anf.h" 23 #include "ir/func_graph.h" 24 #include "ir/manager.h" 25 #include "pybind_api/export_flags.h" 26 27 namespace py = pybind11; 28 namespace mindspore { 29 namespace parse { 30 // define the node type 31 enum AstMainType : int64_t { 32 AST_MAIN_TYPE_STMT = 0, // ast.Stmt 33 AST_MAIN_TYPE_EXPR = 1, // ast.Expr 34 AST_MAIN_TYPE_SLICE = 2, // ast.Slice 35 AST_MAIN_TYPE_UNKNOWN = 0xFF // Error 36 }; 37 38 enum AstSubType : int64_t { 39 AST_SUB_TYPE_AND = 3, // ast.And 40 AST_SUB_TYPE_OR = 4, // ast.Or 41 AST_SUB_TYPE_NAME = 5, // ast.Name 42 AST_SUB_TYPE_TUPLE = 6, // ast.Tuple 43 AST_SUB_TYPE_SUBSCRIPT = 7, // ast.Subscript 44 AST_SUB_TYPE_STARRED = 8, // ast.Starred 45 AST_SUB_TYPE_ATTRIBUTE = 9, // ast.Attribute 46 AST_SUB_TYPE_UNKNOWN = 0xFF // Error 47 }; 48 49 // define the parse target type 50 enum ParseTargetTypeDef { 51 PARSE_TARGET_FUNCTION = 0, // function 52 PARSE_TARGET_METHOD = 1, // method 53 PARSE_TARGET_OBJECT_INSTANCE = 2, // object instance 54 PARSE_TARGET_UNKNOW = 0xFF // ERROR TYPE 55 }; 56 57 // define python module name 58 const char PYTHON_MOD_PARSE_MODULE[] = "mindspore._extends.parse"; 59 const char PYTHON_MOD_PARSE_OBJECT_FUNCTION[] = "parse_cb"; 60 const char PYTHON_MOD_RESOLVE_FUNCTION[] = "resolve_symbol"; 61 const char PYTHON_MOD_RESOLVE_GET_OBJ_KEY[] = "get_object_key"; 62 const char PYTHON_MOD_PARSE_CHECK_IS_CLASS_MEMBER[] = "is_class_member"; 63 const char PYTHON_MOD_RESOLVE_GET_OBJ_TYPE[] = "get_obj_type"; 64 const char PYTHON_MOD_GET_OBJ_ID[] = "get_obj_id"; 65 const char PYTHON_MOD_GET_CLASS_INSTANCE_TYPE[] = "get_class_instance_type"; 66 const char PYTHON_MOD_CREATE_INSTANCE[] = "create_instance"; 67 const char PYTHON_MOD_IS_SUPPORTED_CREATE_INSTANCE_TYPE[] = "is_supported_create_instance_type"; 68 const char PYTHON_MOD_GET_DATACLASS_ATTRS[] = "get_dataclass_attributes"; 69 const char PYTHON_MOD_GET_DATACLASS_METHODS[] = "get_dataclass_methods"; 70 const char PYTHON_MOD_GET_MODULE_NAMESPACE[] = "get_module_namespace"; 71 const char PYTHON_MOD_GET_MEMBER_NAMESPACE_SYMBOL[] = "get_class_member_namespace_symbol"; 72 const char PYTHON_MOD_GET_PARSE_METHOD[] = "get_parse_method_of_class"; 73 const char PYTHON_MOD_GET_BPROP_METHOD[] = "get_bprop_method_of_class"; 74 const char PYTHON_MOD_GET_OBJECT_DESCRIPTION[] = "get_object_description"; 75 const char PYTHON_MOD_CONVERT_TO_MS_TENSOR[] = "convert_to_ms_tensor"; 76 const char PYTHON_MOD_EVAL_PY_SCRIPT[] = "eval_script"; 77 78 const char PYTHON_PARSE_GET_ARGS[] = "get_args"; 79 const char PYTHON_PARSE_GET_ARGS_DEFAULT_VALUES[] = "get_args_default_values"; 80 const char PYTHON_PARSE_GET_NODE_TYPE[] = "get_node_type"; 81 const char PYTHON_PARSE_GET_AST_TYPE[] = "get_ast_type"; 82 const char PYTHON_PARSE_GET_NAMESPACE_SYMBOL[] = "get_namespace_symbol"; 83 const char PYTHON_PARSE_GET_AST_NAMESPACE_SYMBOL[] = "get_ast_namespace_symbol"; 84 const char PYTHON_PARSE_GET_OPERATION_NAMESPACE_SYMBOL[] = "get_operation_namespace_symbol"; 85 const char PYTHON_PARSE_GET_BUILTIN_NAMESPACE_SYMBOL[] = "get_builtin_namespace_symbol"; 86 const char PYTHON_PARSE_GET_LOCATION[] = "get_location"; 87 const char PYTHON_PARSE_EXPAND_EXPR_STATEMENT[] = "expand_expr_statement"; 88 const char PYTHON_PARSE_GENERATE_SCOPE[] = "generate_scope"; 89 const char PYTHON_PARSE_GET_SCOPE_NAME[] = "get_scope_name"; 90 const char PYTHON_PARSE_ANALYZE_SUPER[] = "analyze_super"; 91 92 const char PYTHON_PARSE_CLASS_SLICE[] = "create_slice_obj"; 93 const char PYTHON_PARSE_CLASS_ELLIPSIS[] = "create_ellipsis_obj"; 94 95 // define the common name 96 const char NAMED_PRIMITIVE_LEN[] = "len"; 97 const char NAMED_PRIMITIVE_BODY[] = "body"; 98 const char NAMED_PRIMITIVE_ASSIGN[] = "Assign"; 99 const char NAMED_PRIMITIVE_AUGASSIGN[] = "AugAssign"; 100 const char NAMED_PRIMITIVE_FOR[] = "For"; 101 const char NAMED_PRIMITIVE_IF[] = "If"; 102 const char NAMED_PRIMITIVE_WHILE[] = "While"; 103 const char NAMED_PRIMITIVE_VALUE[] = "value"; 104 const char NAMED_PRIMITIVE_FUNC[] = "func"; 105 const char NAMED_PRIMITIVE_TEST[] = "test"; 106 const char NAMED_PRIMITIVE_LEFT[] = "left"; 107 const char NAMED_PRIMITIVE_CALL[] = "Call"; 108 const char NAMED_PRIMITIVE_SUBSCRIPT[] = "Subscript"; 109 const char NAMED_PRIMITIVE_ATTRIBUTE[] = "Attribute"; 110 const char NAMED_PRIMITIVE_COMPARE[] = "Compare"; 111 const char NAMED_PRIMITIVE_NAMECONSTANT[] = "NameConstant"; 112 const char NAMED_PRIMITIVE_COMPARATORS[] = "comparators"; 113 const char NAMED_PRIMITIVE_TARGET[] = "target"; 114 const char NAMED_PRIMITIVE_SLICE[] = "slice"; 115 const char NAMED_PRIMITIVE_NAME[] = "Name"; 116 const char NAMED_PRIMITIVE_NUM[] = "Num"; 117 const char NAMED_PRIMITIVE_STR[] = "Str"; 118 const char NAMED_PRIMITIVE_ITER[] = "iter"; 119 const char NAMED_PRIMITIVE_NEXT[] = "next"; 120 const char NAMED_PRIMITIVE_GETITEM[] = "getitem"; 121 const char NAMED_PRIMITIVE_SETITEM[] = "setitem"; 122 const char NAMED_PRIMITIVE_HASNEXT[] = "hasnext"; 123 const char NAMED_PRIMITIVE_BOOL[] = "bool_"; // bool: P.identity 124 const char NAMED_PRIMITIVE_MAKETUPLE[] = "MakeTuple"; 125 const char NAMED_PRIMITIVE_MAKELIST[] = "make_list"; 126 const char NAMED_PRIMITIVE_MAKESLICE[] = "make_slice"; 127 const char NAMED_PRIMITIVE_MAKEDICT[] = "make_dict"; 128 const char NAMED_METAGRAPH_UNPACKCALL[] = "unpack_call"; 129 130 // define NAMED_PRIMITIVE_GETATTR "getattr" 131 // define python inline attr 132 const char PYTHON_GET_METHOD_LEN[] = "__len__"; 133 const char PYTHON_GET_METHOD_SELF_CLASS[] = "__self__"; 134 const char PYTHON_GET_OBJ_DESC[] = "__str__"; 135 136 const char PYTHON_EXTERN_PARSE_METHOD[] = "__parse_method__"; 137 const char PYTHON_EXTERN_MINDSPORE_FLAG[] = "_mindspore_flags"; 138 139 // define the parse constant 140 const int64_t MAX_COMPARISON_OPS_SUPPORTED = 1; 141 const char CUSTOM_BPROP_NAME[] = "bprop"; 142 const char STAGE_NAME[] = "_pipeline_stage"; 143 144 // define the Namespace name 145 const char RESOLVE_NAMESPACE_NAME_AST[] = "Ast"; // for ast type namespace 146 const char RESOLVE_NAMESPACE_NAME_CLASS_MEMBER[] = "ClassMember"; // for class member namespace 147 const char RESOLVE_NAMESPACE_NAME_SYMBOL_STR[] = "SymbolStr"; // for symbol str namespace 148 const char RESOLVE_NAMESPACE_NAME_COMMON_OPS[] = "CommonOPS"; // for common ops, eg: hasnext, next 149 const char RESOLVE_NAMESPACE_NAME_MODULE[] = "Module"; // fro Module namespace 150 151 // define Resolve type 152 enum ResolveTypeDef : int64_t { 153 RESOLVE_TYPE_NONE = 0, // resolve None 154 RESOLVE_TYPE_FUNCTION = 1, // resolve function 155 RESOLVE_TYPE_METHOD = 2, // resolve class method 156 RESOLVE_TYPE_CLASS_TYPE = 3, // resolve class type 157 RESOLVE_TYPE_CLASS_INSTANCE = 4, // resolve the class instance of common class 158 RESOLVE_TYPE_INVALID = 0xFF // resolve invalid 159 }; 160 161 // define the class instance detail type When the type is RESOLVE_TYPE_CLASS_INSTANCE 162 enum ClassInstanceTypeDef { 163 CLASS_INSTANCE_TYPE_CELL = 0, // class instance type is Cell 164 CLASS_INSTANCE_TYPE_PRIMITIVE = 1, // class instance type is Primitive 165 CLASS_INSTANCE_TYPE_INVALID = 0xFF 166 }; 167 168 // Convert python object to ValuePtr 169 bool ConvertData(const py::object &obj, ValuePtr *data, bool use_signature = false, const TypePtr &dtype = nullptr); 170 171 // Convert python obj to graph 172 FuncGraphPtr ConvertToFuncGraph(const py::object &obj, 173 const std::string &python_mod_get_parse_method = PYTHON_MOD_GET_PARSE_METHOD); 174 175 // Parse the python object to graph 176 FuncGraphPtr ParsePythonCode(const py::object &obj, 177 const std::string &python_mod_get_parse_method = PYTHON_MOD_GET_PARSE_METHOD); 178 // add wrap for cell top graph. 179 FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr); 180 } // namespace parse 181 } // namespace mindspore 182 183 #endif // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_PARSE_BASE_H_ 184