9 Commits

Author SHA1 Message Date
Tianqi Chen adda179705 [REFACTOR][S-TIR] Lift dlight into s_tir namespace (#18734)
This PR migrates dlight into s_tir namespace, so s_tir related
components are closely grouped together.
2026-02-08 20:20:11 -05:00
Shushi Hong 2012d55caf [Relax] Add Python function support and BasePyModule for PyTorch integration (#18229)
### **Overview**

This PR implements native Python function support in TVM Relax through
the `@I.pyfunc` decorator and `BasePyModule`, which enable seamless
integration between TVM's compilation pipeline and Python/PyTorch runtime
environments. This enhancement allows users to write Python functions
directly in TVMScript that can interoperate with Relax and TIR functions
that provides enhanced debugging capabilities and leveraging existing
PyTorch operator libraries.

### **Key Features**
**TVMScript Parser Enhancement**
- `@I.pyfunc` decorator: Marks Python functions for integration into IRModules
- Dual storage format: Stores both raw string representation (for TVMScript
printing) and captured PackedFunc (for runtime execution)
- ExternFunc representation: Each Python function is represented as an
ExternFunc node with attributes storing source code and runtime wrapper

**Complete BasePyModule Implementation**
- DLPack-based tensor conversion: Seamless conversion between PyTorch
tensors and TVM NDArrays
- Cross-function interoperability: Python functions can call Relax/TIR
functions and vice versa
- JIT compilation: Delays compilation until module instantiation for flexible
late-stage modifications
- Dynamic function registration: Supports runtime addition of Python functions

### Future Work
- TVMScript printer for IRModules with Python functions: Print IRModules
in proper format with high-level operator mapping from Relax ops to PyTorch
ops, handling symbolic shapes
- R.call_py_func primitive: Introduce Relax primitive to invoke corresponding
PackedFunc of specified Python functions at runtime
2025-08-27 14:41:59 -04:00
Eric Lunderberg 732ae53653 [Unity][TVMScript] Produce var = R.ExternFunc("") statements (#15703)
* [Unity][TVMScript] Produce var = R.ExternFunc("") statements

Prior to this commit, any `ExternFunc` usage in a relax function would
print the string name of the function on its own line, omitting any
variable definition, and later use of the variable would occur without
a definition.  This commit updates the printing of `R.ExternFunc` to
appear as a normal relax variable.

* Preserve special handling as callee, test round-trip

* Updated parser to handle `var = R.ExternFunc(...)` in IRModule

Since this is now a representation that may be produced by the
TVMScript printer, it must also be handled at the parser.
2023-09-26 14:22:54 -07:00
Siyuan Feng 121e1e7a03 [TVMScript][Unity] Improve PyLint Compatibility (#14276)
The current cross-function calls in TVMScript will cause PyLint warnings,
since the GlobalVar will be marked as undefined vars, e.g.:

```python
@I.ir_module
class TestModule:
    @T.prim_func
    def tir_func(
        x: T.Buffer((T.int64(128),), "float32"), y: T.Buffer((T.int64(128),), "float32")
    ):
        T.evaluate(0)

    @R.function
    def foo(x: R.Tensor((128,), "float32")) -> R.Tensor((128,), "float32"):
        gv0 = R.call_tir(tir_func, x, R.Tensor((128,), dtype="float32"))  # <= `tir_func` is not defined in Python syntax.
        return gv0
```

This PR changes the behavior into `TestModule.tir_func` instead of direct `tir_func`
```python
@I.ir_module
class TestModule:
    @T.prim_func
    def tir_func(
        x: T.Buffer((T.int64(128),), "float32"), y: T.Buffer((T.int64(128),), "float32")
    ):
        T.evaluate(0)

    @R.function
    def foo(x: R.Tensor((128,), "float32")) -> R.Tensor((128,), "float32"):
        cls = TestModule  # Use `cls` to refer the current Module
        gv0 = R.call_tir(cls.tir_func, x, R.Tensor((128,), dtype="float32"))
        return gv0
```

NOTE: It's a breaking change, the old style is deprecated.

Additionally, this PR contains the following minor fixes:
- mark `R.function` as staticmethod as what we do for `T.prim_func`
- make `I`, `R`, `T`, `cls` be the builtin keywords for the printer
- define names for functions, modules to prevent naming conflict
- checking the var names is valid via regex expression
- fix typos
2023-04-01 15:31:44 -04:00
Siyuan Feng 3e03ca5abe [Unity][TVMScript] Enable Context-Aware Parsing (#14234)
This PR enables context-aware parsing for TVMScript. It means that the parser has full control of the statements in the specific context/namespace.

For example, we can override the global var `__call__` method in Relax function to make sure to generate Relax Calls instead of Relay Calls.
2023-04-01 15:31:44 -04:00
Hongyi Jin aaa457d304 [Unity] Add Global info (#14132) 2023-04-01 15:31:37 -04:00
Siyuan Feng 540ba28f5c [Unity] Relax TVMScript Parser. (#13932)
This PR adds the TVMScript parser/ir_builder support based on the blockbuilder.

Co-authored-by: Ruihang Lai <ruihangl@cs.cmu.edu>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tianqi.tchen@gmail.com>
Co-authored-by: Yuchen Jin <yuchenj@cs.washington.edu>
Co-authored-by: Steven S. Lyubomirsky <slyubomirsky@gmail.com>
Co-authored-by: Yong Wu <yongcale@gmail.com>
2023-04-01 15:31:36 -04:00
Junru Shao 09f38ac91c [TVMScript][Fix] Print Multi-line String as Metadata (#13965)
Multi-line strings might make less sense to be printed out by default,
as they could be LLVM snippets, CUDA source code and anything hard to
comprehend but easy to mess up with the TVMScript itself. Therefore,
this PR is introduced to print them as metadata by default.
2023-02-12 09:42:01 -05:00
Junru Shao b20b7c4ad4 [TVMScript] Reorganize the folder structure (#12496)
This PR introduces some minor restructuring of the `python/tvm/script`
folder structure to make it more convenient for future upstreaming.

Co-authored-by: Yaxing Cai <caiyaxing666@gmail.com>
2022-11-12 01:25:23 -05:00