MCPcopy Create free account
hub / github.com/apache/tvm / _wrap_relax_functions

Method _wrap_relax_functions

python/tvm/relax/base_py_module.py:164–190  ·  view source on GitHub ↗

Wrap Relax functions to be callable from Python with auto conversion.

(self)

Source from the content-addressed store, hash-verified

162 setattr(self, func_name, func)
163
164 def _wrap_relax_functions(self):
165 """Wrap Relax functions to be callable from Python with auto conversion."""
166 for func_name in self.relax_func_names:
167
168 def _create_relax_wrapper(name):
169 def wrapper(*args, **kwargs):
170 """Wrapper for Relax function with automatic tensor conversion."""
171 if hasattr(self.ir_mod, "pyfuncs") and name in self.ir_mod.pyfuncs:
172 return self.ir_mod.pyfuncs[name](*args, **kwargs)
173
174 if self.relax_vm is not None:
175 converted_args = self._convert_pytorch_to_tvm(list(args))
176 converted_kwargs = {
177 k: self._convert_pytorch_to_tvm(v) for k, v in kwargs.items()
178 }
179 result = self.relax_vm[name](*converted_args, **converted_kwargs)
180 return self._convert_tvm_to_pytorch(result)
181
182 raise RuntimeError(
183 f"Neither converted Python function nor Relax VM available for {name}"
184 )
185
186 wrapper.__name__ = name
187 wrapper.__doc__ = f"Wrapped Relax function: {name}"
188 return wrapper
189
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."""

Callers 1

__init__Method · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected