The parsing method for ir module, by using `@ir_module` as decorator. Parameters ---------- mod : Type The class to be parsed as ir module. check_well_formed : bool Whether to check well-formedness during parsing. Returns ------- ir_module : IRModule
(
mod: type | None = None, check_well_formed: bool = True, s_tir: bool = False
)
| 30 | # appear as a decorator by itself or to have optional arguments |
| 31 | # like @I.ir_module(check_well_formed=False) |
| 32 | def ir_module( |
| 33 | mod: type | None = None, check_well_formed: bool = True, s_tir: bool = False |
| 34 | ) -> IRModule: |
| 35 | """The parsing method for ir module, by using `@ir_module` as decorator. |
| 36 | |
| 37 | Parameters |
| 38 | ---------- |
| 39 | mod : Type |
| 40 | The class to be parsed as ir module. |
| 41 | |
| 42 | check_well_formed : bool |
| 43 | Whether to check well-formedness during parsing. |
| 44 | |
| 45 | Returns |
| 46 | ------- |
| 47 | ir_module : IRModule |
| 48 | The parsed ir module. |
| 49 | """ |
| 50 | |
| 51 | # Capture stack outside wrapper (wrapper adds to the stack) |
| 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 | ] |
nothing calls this directly
no test coverage detected
searching dependent graphs…