taichi.lang.struct

Module Contents

Classes

Struct

The Struct type class.

StructField

Taichi struct field with SNode implementation.

StructType

class taichi.lang.struct.Struct(*args, **kwargs)

Bases: taichi.lang.common_ops.TaichiOperations

The Struct type class. :param entries: keys and values for struct members. :type entries: Dict[str, Union[Dict, Expr, Matrix, Struct]]

is_taichi_class = True
property keys(self)
property members(self)
property items(self)
register_members(self)
set_entries(self, value)
static make_getter(key)
static make_setter(key)
element_wise_unary(self, foo)
element_wise_binary(self, foo, other)
broadcast_copy(self, other)
element_wise_writeback_binary(self, foo, other)
element_wise_ternary(self, foo, other, extra)
fill(self, val)

Fills the Struct with a specific value in Taichi scope.

Parameters

val (Union[int, float]) – Value to fill.

to_dict(self)

Converts the Struct to a dictionary.

Args:

Returns

The result dictionary.

Return type

Dict

classmethod field(cls, members, shape=None, name='<Struct>', offset=None, needs_grad=False, layout=Layout.AOS)
class taichi.lang.struct.StructField(field_dict, name=None)

Bases: taichi.lang.field.Field

Taichi struct field with SNode implementation.

Instead of directly contraining Expr entries, the StructField object directly hosts members as Field instances to support nested structs.

Parameters
  • field_dict (Dict[str, Field]) – Struct field members.

  • name (string, optional) – The custom name of the field.

property name(self)
property keys(self)
property members(self)
property items(self)
static make_getter(key)
static make_setter(key)
register_fields(self)
get_field_members(self)

Get A flattened list of all struct elements.

Returns

A list of struct elements.

property snode(self)

Gets representative SNode for info purposes.

Returns

Representative SNode (SNode of first field member).

Return type

SNode

loop_range(self)

Gets representative field member for loop range info.

Returns

Representative (first) field member.

Return type

taichi_core.Expr

copy_from(self, other)

Copies all elements from another field.

The shape of the other field needs to be the same as self.

Parameters

other (Field) – The source field.

fill(self, val)

Fills self with a specific value.

Parameters

val (Union[int, float]) – Value to fill.

initialize_host_accessors(self)
get_member_field(self, key)

Creates a ScalarField using a specific field member. Only used for quant.

Parameters

key (str) – Specified key of the field member.

Returns

The result ScalarField.

Return type

ScalarField

from_numpy(self, array_dict)
from_torch(self, array_dict)
to_numpy(self)
Converts the Struct field instance to a dictionary of NumPy arrays. The dictionary may be nested when converting

nested structs.

Args: :returns: The result NumPy array. :rtype: Dict[str, Union[numpy.ndarray, Dict]]

to_torch(self, device=None)
Converts the Struct field instance to a dictionary of PyTorch tensors. The dictionary may be nested when converting

nested structs.

Parameters

device (torch.device, optional) – The desired device of returned tensor.

Returns

The result PyTorch tensor.

Return type

Dict[str, Union[torch.Tensor, Dict]]

class taichi.lang.struct.StructType(**kwargs)

Bases: taichi.types.CompoundType

cast(self, struct)
filled_with_scalar(self, value)
field(self, **kwargs)