Skip to main content


Taichi’s AOT (ahead of time) module.

Users can use Taichi as a GPU compute shader/kernel compiler by compiling their Taichi kernels into an AOT module.

class taichi.aot.GfxRuntime140(metadata_json: Any, graphs_json: Any)#
static from_module(module_path: str) GfxRuntime140#
to_graphs_json(self) List[Any]#
to_metadata_json(self) Any#
class taichi.aot.Module(arch=None, caps=None)#

An AOT module to save and load Taichi kernels.

This module serializes the Taichi kernels for a specific arch. The serialized module can later be loaded to run on that backend, without the Python environment.



m = ti.aot.Module(ti.metal)

# Now the module file '/path/to/module' contains the Metal kernels
# for running ``foo`` and ``bar``.
add_field(self, name, field)#

Add a taichi field to the AOT module.

  • name – name of taichi field

  • field – taichi field


>>> a = ti.field(ti.f32, shape=(4,4))
>>> b = ti.field("something")
>>> m.add_field(a)
>>> m.add_field(b)
>>> # Must add in sequence
add_graph(self, name, graph)#
add_kernel(self, kernel_fn, template_args=None, name=None)#

Add a taichi kernel to the AOT module.

  • kernel_fn (Function) – the function decorated by taichi kernel.

  • template_args (Dict[str, Any]) – a dict where key is the template parameter name, and value is the instantiating arg. Note that this works for both template and for :class:`~taichi.types.ndarray.

  • name (str) – Name to identify this kernel in the module. If not provided, uses the built-in __name__ attribute of kernel_fn.

add_kernel_template(self, kernel_fn)#

Add a taichi kernel (with template parameters) to the AOT module.


kernel_fn (Function) – the function decorated by taichi kernel.


>>> @ti.kernel
>>> def bar_tmpl(a: ti.template()):
>>>   x = a
>>>   # or y = a
>>>   # do something with `x` or `y`
>>> m = ti.aot.Module(arch)
>>> with m.add_kernel_template(bar_tmpl) as kt:
>>>   kt.instantiate(a=x)
>>>   kt.instantiate(a=y)
>>> @ti.kernel
>>> def bar_tmpl_multiple_args(a: ti.template(), b: ti.template())
>>>   x = a
>>>   y = b
>>>   # do something with `x` and `y`
>>> with m.add_kernel_template(bar_tmpl) as kt:
>>>   kt.instantiate(a=x, b=y)
archive(self, filepath: str)#

filepath (str) – path to the stored archive of aot artifacts, MUST end with .tcm.

save(self, filepath)#

filepath (str) – path to a folder to store aot files.

taichi.aot.export_as(name: str, *, template_types: Dict[str, Any] | None = None)#