Declare a Python function as an ExternFunc in the IRModule.
(self: Parser, node: doc.FunctionDef)
| 142 | |
| 143 | @dispatch.register(token="pyfunc", type_name="tvm_declare_function") |
| 144 | def visit_tvm_declare_function(self: Parser, node: doc.FunctionDef) -> GlobalVar: |
| 145 | """Declare a Python function as an ExternFunc in the IRModule.""" |
| 146 | # Check if Python functions are allowed in this context |
| 147 | # We need to check if we're in a class that inherits from BasePyModule |
| 148 | current_class = self._get_current_class_context() |
| 149 | if current_class and not self._is_base_py_module_context(): |
| 150 | self.report_error( |
| 151 | node, |
| 152 | "@I.pyfunc are only allowed in classes that inherit from BasePyModule. " |
| 153 | f"Class '{current_class}' does not inherit from BasePyModule.", |
| 154 | ) |
| 155 | |
| 156 | # Lazy import: tvm.relax cannot be imported at module level in tvm.script.parser |
| 157 | # because tvm.script is loaded before tvm.relax during tvm initialization. |
| 158 | from tvm.relax import ExternFunc # pylint: disable=import-outside-toplevel |
| 159 | |
| 160 | # Create ExternFunc with proper attributes for Python functions |
| 161 | func = ExternFunc(node.name) |
| 162 | func = func.with_attr("is_pyfunc", True) |
| 163 | func = func.with_attr("function_type", "python") |
| 164 | func = func.with_attr("python_function_name", node.name) |
| 165 | |
| 166 | # Add placeholder attributes that will be filled in later |
| 167 | func = func.with_attr("python_source", f"# Source will be filled for {node.name}") |
| 168 | func = func.with_attr("python_packed_func", None) # Will be filled in entry.py |
| 169 | |
| 170 | # Store the function name for later retrieval |
| 171 | return I.decl_function(node.name, func) |
| 172 | |
| 173 | |
| 174 | @dispatch.register(token="pyfunc", type_name="FunctionDef") |
nothing calls this directly
no test coverage detected
searching dependent graphs…