Skip to main content

Mojo struct

AnyTensor

A type erased tensor representation that is useful for situations where we need variadics of tensors.

Implemented traits

AnyType, CollectionElement, Copyable, Movable, Writable

Methods

__init__

__init__(out self)

Default constructor for AnyTensor.

__init__(out self, owned device_tensor: DeviceTensor)

Creates AnyTensor from a DeviceTensor.

Args:

  • device_tensor (DeviceTensor): DeviceTensor to construct AnyTensor from.

__init__[type: DType, rank: Int](out self, owned tensor: Tensor[type, rank])

Creates AnyTensor from a Tensor.

Args:

  • tensor (Tensor[type, rank]): Tensor to construct AnyTensor from.

__moveinit__

__moveinit__(out self, owned existing: Self)

Move constructor for AnyTensor.

Args:

  • existing (Self): Instance to move from.

__del__

__del__(owned self)

Destructor for AnyTensor.

get_rank

get_rank(self) -> Int

Gets rank of the tensor.

Returns:

Rank of the tensor.

spec

spec(self) -> TensorSpec

Gets the spec of the tensor.

Returns:

Spec of the tensor.

to_device_tensor

to_device_tensor(owned self) -> DeviceTensor

Consumes this AnyTensor and converts it into a device tensor.

Returns:

DeviceTensor representation of AnyTensor.

to_tensor

to_tensor[type: DType, rank: Int](owned self) -> Tensor[$0, $1]

Consumes this anytensor and convert it into a tensor.

Parameters:

  • type (DType): Type of tensor.
  • rank (Int): Rank of tensor.

Returns:

Tensor representation of AnyTensor.

take

take(inout self) -> Self

The returned value takes self's resources and replaces them with default initialized values.

Returns:

Newly constructed anytensor that takes storage from this.

__str__

__str__(self) -> String

Gets the tensor as a string.

Returns:

A compact string of the tensor.

write_to

write_to[W: Writer](self, inout writer: W)

Formats this Tensor to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.