taichi.lang.impl

Module Contents

Functions

static_print(*args, __p=print, **kwargs)

static_assert(cond, msg=None)

deactivate_all_snodes()

Recursively deactivate all SNodes.

field(dtype, shape=None, name='', offset=None, needs_grad=False)

Defines a Taichi field

ndarray(dtype, shape)

Defines a Taichi ndarray with scalar elements.

zero(x)

Fill the input field with zero.

one(x)

Fill the input field with one.

axes(*x: Iterable[int])

Defines a list of axes to be used by a field.

static(x, *xs)

Evaluates a Taichi-scope expression at compile time.

grouped(x)

Groups a list of independent loop indices into a Vector().

stop_grad(x)

Attributes

root

Root of the declared Taichi :func:`~taichi.lang.impl.field`s.

taichi.lang.impl.static_print(*args, __p=print, **kwargs)
taichi.lang.impl.static_assert(cond, msg=None)
taichi.lang.impl.deactivate_all_snodes()

Recursively deactivate all SNodes.

taichi.lang.impl.root

Root of the declared Taichi :func:`~taichi.lang.impl.field`s.

See also https://docs.taichi.graphics/lang/articles/advanced/layout

Example:

>>> x = ti.field(ti.f32)
>>> ti.root.pointer(ti.ij, 4).dense(ti.ij, 8).place(x)
taichi.lang.impl.field(dtype, shape=None, name='', offset=None, needs_grad=False)

Defines a Taichi field

A Taichi field can be viewed as an abstract N-dimensional array, hiding away the complexity of how its underlying SNode are actually defined. The data in a Taichi field can be directly accessed by a Taichi kernel().

See also https://docs.taichi.graphics/lang/articles/basic/field

Parameters
  • dtype (DataType) – data type of the field.

  • shape (Union[int, tuple[int]], optional) – shape of the field

  • name (str, optional) – name of the field

  • offset (Union[int, tuple[int]], optional) – offset of the field domain

  • needs_grad (bool, optional) – whether this field participates in autodiff and thus needs an adjoint field to store the gradients.

Example

The code below shows how a Taichi field can be declared and defined:

>>> x1 = ti.field(ti.f32, shape=(16, 8))
>>>
>>> # Equivalently
>>> x2 = ti.field(ti.f32)
>>> ti.root.dense(ti.ij, shape=(16, 8)).place(x2)
taichi.lang.impl.ndarray(dtype, shape)

Defines a Taichi ndarray with scalar elements.

Parameters
  • dtype (DataType) – Data type of each value.

  • shape (Union[int, tuple[int]]) – Shape of the ndarray.

Example

The code below shows how a Taichi ndarray with scalar elements can be declared and defined:

>>> x = ti.ndarray(ti.f32, shape=(16, 8))
taichi.lang.impl.zero(x)

Fill the input field with zero.

Parameters

x (DataType) – The input field to fill.

Returns

The output field, which keeps the shape but filled with zero.

Return type

DataType

taichi.lang.impl.one(x)

Fill the input field with one.

Parameters

x (DataType) – The input field to fill.

Returns

The output field, which keeps the shape but filled with one.

Return type

DataType

taichi.lang.impl.axes(*x: Iterable[int])

Defines a list of axes to be used by a field.

Parameters

*x – A list of axes to be activated

Note that Taichi has already provided a set of commonly used axes. For example, ti.ij is just axes(0, 1) under the hood.

taichi.lang.impl.static(x, *xs)

Evaluates a Taichi-scope expression at compile time.

static() is what enables the so-called metaprogramming in Taichi. It is in many ways similar to constexpr in C++11.

See also https://docs.taichi.graphics/lang/articles/advanced/meta.

Parameters
  • x (Any) – an expression to be evaluated

  • *xs (Any) – for Python-ish swapping assignment

Example

The most common usage of static() is for compile-time evaluation:

>>> @ti.kernel
>>> def run():
>>>     if ti.static(FOO):
>>>         do_a()
>>>     else:
>>>         do_b()

Depending on the value of FOO, run() will be directly compiled into either do_a() or do_b(). Thus there won’t be a runtime condition check.

Another common usage is for compile-time loop unrolling:

>>> @ti.kernel
>>> def run():
>>>     for i in ti.static(range(3)):
>>>         print(i)
>>>
>>> # The above is equivalent to:
>>> @ti.kernel
>>> def run():
>>>     print(0)
>>>     print(1)
>>>     print(2)
taichi.lang.impl.grouped(x)

Groups a list of independent loop indices into a Vector().

Parameters

x (Any) – does the grouping only if x is a ndrange.

Example:

>>> for I in ti.grouped(ndrange(8, 16)):
>>>     print(I[0] + I[1])
taichi.lang.impl.stop_grad(x)