taichi.lang.impl

Module Contents

Classes

PyTaichi

Functions

expr_init_local_tensor(shape, element_type, elements)

expr_init(rhs)

expr_init_list(xs, expected)

expr_init_func(rhs)

begin_frontend_struct_for(group, loop_range)

begin_frontend_if(cond)

wrap_scalar(x)

subscript(value, *_indices, skip_reordered=False)

make_tensor_element_expr(_var, _indices, shape, stride)

insert_expr_stmt_if_ti_func(func, *args, **kwargs)

This method is used only for real functions. It inserts a

get_runtime()

make_constant_expr_i32(val)

make_constant_expr(val)

reset()

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

static_assert(cond, msg=None)

inside_kernel()

index_nd(dim)

deactivate_all_snodes()

Recursively deactivate all SNodes.

create_field_member(dtype, name)

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

Defines a Taichi field

ndarray(dtype, shape)

Defines a Taichi ndarray with scalar elements.

ti_print(*_vars, sep=' ', end='\n')

ti_format(*args, **kwargs)

ti_assert(cond, msg, extra_args)

ti_int(_var)

ti_float(_var)

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)

current_cfg()

default_cfg()

call_internal(name, *args)

mesh_relation_access(mesh, from_index, to_element_type)

Attributes

pytaichi

root

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

Axis

taichi.lang.impl.expr_init_local_tensor(shape, element_type, elements)
taichi.lang.impl.expr_init(rhs)
taichi.lang.impl.expr_init_list(xs, expected)
taichi.lang.impl.expr_init_func(rhs)
taichi.lang.impl.begin_frontend_struct_for(group, loop_range)
taichi.lang.impl.begin_frontend_if(cond)
taichi.lang.impl.wrap_scalar(x)
taichi.lang.impl.subscript(value, *_indices, skip_reordered=False)
taichi.lang.impl.make_tensor_element_expr(_var, _indices, shape, stride)
taichi.lang.impl.insert_expr_stmt_if_ti_func(func, *args, **kwargs)

This method is used only for real functions. It inserts a FrontendExprStmt to the C++ AST to hold the function call if func is a Taichi function.

Parameters
  • func – The function to be called.

  • args – The arguments of the function call.

  • kwargs – The keyword arguments of the function call.

Returns

The return value of the function call if it’s a non-Taichi function. Returns None if it’s a Taichi function.

class taichi.lang.impl.PyTaichi(kernels=None)
get_num_compiled_functions(self)
set_default_fp(self, fp)
set_default_ip(self, ip)
create_program(self)
static materialize_root_fb(is_first_call)
materialize(self)
clear(self)
get_tape(self, loss=None)
sync(self)
taichi.lang.impl.pytaichi
taichi.lang.impl.get_runtime()
taichi.lang.impl.make_constant_expr_i32(val)
taichi.lang.impl.make_constant_expr(val)
taichi.lang.impl.reset()
taichi.lang.impl.static_print(*args, __p=print, **kwargs)
taichi.lang.impl.static_assert(cond, msg=None)
taichi.lang.impl.inside_kernel()
taichi.lang.impl.index_nd(dim)
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.create_field_member(dtype, name)
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.ti_print(*_vars, sep=' ', end='\n')
taichi.lang.impl.ti_format(*args, **kwargs)
taichi.lang.impl.ti_assert(cond, msg, extra_args)
taichi.lang.impl.ti_int(_var)
taichi.lang.impl.ti_float(_var)
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.Axis
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)
taichi.lang.impl.current_cfg()
taichi.lang.impl.default_cfg()
taichi.lang.impl.call_internal(name, *args)
taichi.lang.impl.mesh_relation_access(mesh, from_index, to_element_type)