tvm.script
tvm.script
TVMScript public namespace.
Dialect resolution mechanism
tvm.script is a virtual namespace: dialect names like tirx and
relax are not bound as static attributes here. Instead:
register_dialect(name, module_path)writes an entry to_DIALECT_REGISTRY: dict[str, str]. Each in-tree dialect’s__init__.pycalls this on import (e.g.,tvm.tirx.__init__.pycallstvm.script.register_dialect("tirx", "tvm.tirx.script")). Out-of-tree dialects can register themselves the same way.__getattr__(name)(PEP 562) fires on missing attribute access. Ifnameis in_DIALECT_REGISTRY, the listed module is imported and cached as a normal module attribute. Subsequent accesses skip__getattr__(cached inglobals()).Subpackages
tvm.script.parser,tvm.script.ir_builder, etc. each define their own__getattr__that consults the SAME_DIALECT_REGISTRYand appends their suffix. Sotvm.script.parser.tirxresolves totvm.tirx.script.parservia the dialect registry +.parsersuffix.For deep statement-form imports like
from tvm.script.parser.tirx.entry import ObjectProxy, PEP 562’s__getattr__is not enough — it only handles one-levelfrom X import Y. Asys.meta_pathfinder (see_DialectRedirectFinder) intercepts the import machinery to register the real module under the legacy name insys.modules, so subsequent attribute walks resolve correctly.
Each dialect’s tvm.<dialect>.script package MUST expose parser,
ir_builder, and (where applicable) printer as submodules. This
convention is what makes the suffix-append redirect work uniformly.
IR is foundational (script depends on ir) and is NOT a dialect; its
script handlers live in the shared core, not via this registry.
Bootstrap order
python/tvm/__init__.py imports tvm.script BEFORE importing any
dialect package (tvm.tirx, tvm.relax, …). This guarantees that
tvm.script.register_dialect is reachable the moment a dialect’s own
__init__.py runs and calls it. The tvm.script module itself
stays dialect-agnostic at load time (no dialect submodules are eagerly
imported here), so there is no circular dependency.
- tvm.script.register_dialect(name: str, module_path: str) None
Register a dialect’s script package path.
Writes
name -> module_pathinto_DIALECT_REGISTRY. After registration,tvm.script.<name>resolves tomodule_pathvia__getattr__, andtvm.script.parser.<name>/tvm.script.ir_builder.<name>resolve tomodule_path + ".parser"/module_path + ".builder"etc. via each subpackage’s own__getattr__. Deep statement-form imports (e.g.,from tvm.script.parser.<name>.entry import X) are handled by_DialectRedirectFinderonsys.meta_path.This function is idempotent — re-registering the same name with the same path is harmless.
Each in-tree dialect calls this from its own
__init__.py:import tvm.script tvm.script.register_dialect("tirx", "tvm.tirx.script")
Out-of-tree dialects do the same in their own package init without editing any in-tree file.
- Parameters:
name (str) – The short name exposed under
tvm.script.<name>(e.g."tirx").module_path (str) – The full dotted module path of the dialect’s script package, e.g.
"tvm.tirx.script". That package must exposeparserandir_builderas submodules (andprinterwhere applicable) so that the suffix-append redirect works uniformly.
tvm.script.relax
Relax-layer TVMScript pieces (parser, builder).
After the per-dialect TVMScript restructure, the Relax layer owns its own
script/{parser,builder} subpackages. tvm.script.relax resolves to
this module via the dialect registry, so the public parser surface
(function, Tensor, match_cast, etc.) is re-exported here.
tvm.script.tirx
TIRX-layer TVMScript pieces (parser, builder).
After the per-dialect TVMScript restructure, the TIRX layer owns its own
script/{parser,builder} subpackages. tvm.script.tirx resolves to
this module via the dialect registry, so the public parser surface
(prim_func, Buffer, Ptr, etc.) is re-exported here.