Attach Python functions to IRModule with reduced nesting.
(irmodule, all_pyfuncs)
| 178 | |
| 179 | |
| 180 | def _attach_pyfuncs_to_irmodule(irmodule, all_pyfuncs): |
| 181 | """Attach Python functions to IRModule with reduced nesting.""" |
| 182 | if not all_pyfuncs: |
| 183 | return |
| 184 | |
| 185 | if not hasattr(irmodule, "pyfuncs"): |
| 186 | irmodule.pyfuncs = {} |
| 187 | |
| 188 | for global_var, func in irmodule.functions_items(): |
| 189 | if not isinstance(func, tvm.relax.ExternFunc): |
| 190 | continue |
| 191 | if not func.attrs.get("is_pyfunc", False): |
| 192 | continue |
| 193 | |
| 194 | pyfunc_name = global_var.name_hint |
| 195 | if pyfunc_name not in all_pyfuncs: |
| 196 | continue |
| 197 | |
| 198 | pyfunc = all_pyfuncs[pyfunc_name] |
| 199 | irmodule.pyfuncs[pyfunc_name] = pyfunc |
| 200 | |
| 201 | try: |
| 202 | source_code = inspect.getsource(pyfunc) |
| 203 | func = func.with_attr("python_source", source_code) |
| 204 | except (OSError, TypeError): |
| 205 | func = func.with_attr("python_source", f"# Source unavailable for {pyfunc_name}") |
| 206 | |
| 207 | packed_func = _create_python_packed_func(pyfunc) |
| 208 | func = func.with_attr("python_packed_func", packed_func) |
| 209 | |
| 210 | irmodule[global_var] = func |
no test coverage detected
searching dependent graphs…