Register Python functions with the VM runtime for call_py_func support.
(self)
| 190 | setattr(self, func_name, _create_relax_wrapper(func_name)) |
| 191 | |
| 192 | def _register_python_functions(self): |
| 193 | """Register Python functions with the VM runtime for call_py_func support.""" |
| 194 | if not hasattr(self.ir_mod, "pyfuncs") or not self.ir_mod.pyfuncs: |
| 195 | return |
| 196 | |
| 197 | try: |
| 198 | register_py_func = tvm.get_global_func("vm.builtin.register_py_func") |
| 199 | except ValueError: |
| 200 | return |
| 201 | |
| 202 | for func_name, py_func in self.ir_mod.pyfuncs.items(): |
| 203 | |
| 204 | def create_py_func_wrapper(name, original_func): |
| 205 | def wrapper(*args, **kwargs): |
| 206 | converted_args = [self._convert_tvm_to_pytorch(arg) for arg in args] |
| 207 | converted_kwargs = { |
| 208 | k: self._convert_tvm_to_pytorch(v) for k, v in kwargs.items() |
| 209 | } |
| 210 | |
| 211 | result = original_func(self, *converted_args, **converted_kwargs) |
| 212 | |
| 213 | return self._convert_pytorch_to_tvm(result) |
| 214 | |
| 215 | wrapper.__name__ = name |
| 216 | return wrapper |
| 217 | |
| 218 | wrapped_func = create_py_func_wrapper(func_name, py_func) |
| 219 | register_py_func(func_name, wrapped_func) |
| 220 | |
| 221 | def call_tir(self, tir_func, args, out_sinfo): |
| 222 | """Call a TIR function with PyTorch tensors.""" |
no test coverage detected