(mod)
| 52 | outer_stack = inspect.stack() |
| 53 | |
| 54 | def decorator_wrapper(mod): |
| 55 | if not inspect.isclass(mod): |
| 56 | raise TypeError(f"Expect a class, but got: {mod}") |
| 57 | |
| 58 | # Check BasePyModule inheritance |
| 59 | base_py_module_inherited = any(base.__name__ == "BasePyModule" for base in mod.__bases__) |
| 60 | |
| 61 | extra_vars = utils.inspect_class_capture(mod) |
| 62 | # Resolve closure variables hidden by PEP 563 (annotation-only names) |
| 63 | utils.resolve_closure_vars(mod, extra_vars, outer_stack) |
| 64 | m = parse(mod, extra_vars, check_well_formed=check_well_formed, s_tir=s_tir) |
| 65 | |
| 66 | if base_py_module_inherited: |
| 67 | # Lazy import: tvm.relax cannot be imported at module level in tvm.script.parser |
| 68 | # because tvm.script is loaded before tvm.relax during tvm initialization. |
| 69 | from tvm.relax.base_py_module import BasePyModule |
| 70 | from tvm.relax.expr import ExternFunc # pylint: disable=import-outside-toplevel |
| 71 | |
| 72 | # Collect pyfunc methods |
| 73 | pyfunc_methods = [ |
| 74 | name |
| 75 | for name, attr in mod.__dict__.items() |
| 76 | if hasattr(attr, "dispatch_token") and attr.dispatch_token == "pyfunc" |
| 77 | ] |
| 78 | |
| 79 | mod._pyfunc_methods = pyfunc_methods |
| 80 | |
| 81 | # Create ExternFunc nodes |
| 82 | |
| 83 | for method_name in pyfunc_methods: |
| 84 | try: |
| 85 | existing_gvars = [ |
| 86 | global_var |
| 87 | for global_var in m.get_global_vars() |
| 88 | if global_var.name_hint == method_name |
| 89 | ] |
| 90 | |
| 91 | extern_func = ExternFunc(method_name) |
| 92 | extern_func = extern_func.with_attr("is_pyfunc", True) |
| 93 | extern_func = extern_func.with_attr("function_type", "python") |
| 94 | extern_func = extern_func.with_attr("python_function_name", method_name) |
| 95 | extern_func = extern_func.with_attr( |
| 96 | "python_source", f"# Source for {method_name}" |
| 97 | ) |
| 98 | extern_func = extern_func.with_attr("python_packed_func", None) |
| 99 | |
| 100 | if existing_gvars: |
| 101 | m[existing_gvars[0]] = extern_func |
| 102 | else: |
| 103 | m[GlobalVar(method_name)] = extern_func |
| 104 | |
| 105 | except Exception: # pylint: disable=broad-exception-caught |
| 106 | continue |
| 107 | |
| 108 | class ModuleFactory: |
| 109 | """Factory class for creating BasePyModule instances with Python functions.""" |
| 110 | |
| 111 | def __init__(self, module, pyfunc_methods, original_class): |
no test coverage detected
searching dependent graphs…