Decorator for inline function definitions with Python LEGB scoping. @T.inline follows Python's lexical scoping with late binding: - At definition time, record which scopes are visible. - At call time, read current values from those scopes. Example:: import tvm from
(*args, definition_depth: int | None = None, defining_var_table=None)
| 162 | |
| 163 | |
| 164 | def inline(*args, definition_depth: int | None = None, defining_var_table=None) -> Callable: |
| 165 | """Decorator for inline function definitions with Python LEGB scoping. |
| 166 | |
| 167 | @T.inline follows Python's lexical scoping with late binding: |
| 168 | - At definition time, record which scopes are visible. |
| 169 | - At call time, read current values from those scopes. |
| 170 | |
| 171 | Example:: |
| 172 | |
| 173 | import tvm |
| 174 | from tvm.script import tirx as T |
| 175 | |
| 176 | x_value = 128 |
| 177 | |
| 178 | @T.inline |
| 179 | def capture(A, B): |
| 180 | B[()] = A[x_value] # x_value resolved from enclosing scope |
| 181 | |
| 182 | @T.prim_func(s_tir=True) |
| 183 | def use(A: T.Buffer((1024,), "int32"), B: T.Buffer((), "int32")) -> None: |
| 184 | capture(A, B) # Produces B[()] = A[128] |
| 185 | """ |
| 186 | |
| 187 | def _decorator(func: Callable) -> Callable: |
| 188 | source, closure_vars = scan_macro(func, utils.inspect_function_capture(func)) |
| 189 | obj = TIRInline( |
| 190 | source, |
| 191 | closure_vars, |
| 192 | func, |
| 193 | definition_depth=definition_depth, |
| 194 | defining_var_table=defining_var_table, |
| 195 | ) |
| 196 | |
| 197 | def wrapper(*args, **kwargs): |
| 198 | return obj(*args, **kwargs) |
| 199 | |
| 200 | return wrapper |
| 201 | |
| 202 | if len(args) == 0: |
| 203 | setattr(_decorator, "dispatch_token", "tir.inline") |
| 204 | return _decorator |
| 205 | if len(args) == 1 and inspect.isfunction(args[0]): |
| 206 | return _decorator(args[0]) |
| 207 | |
| 208 | raise ValueError("Invalid use of T.inline. Usage: @T.inline or @T.inline()") |
| 209 | |
| 210 | |
| 211 | setattr(inline, "dispatch_token", "tir.inline") |
no test coverage detected
searching dependent graphs…