Skip to main content

Mojo struct

AnyMemory

A generic representation which can either be a Driver Tensor or Mojo object.

Implemented traits

AnyType, CollectionElement, Copyable, Movable, Writable

Methods

__init__

__init__(out self)

Default constructor for AnyMemory.

__init__(out self, owned device_tensor: DeviceTensor)

Creates AnyMemory from a DeviceTensor.

Args:

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

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

Creates AnyMemory from a Tensor.

Args:

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

__init__(out self, owned tensor: AnyTensor)

Creates AnyMemory from a AnyTensor.

Args:

  • tensor (AnyTensor): AnyTensor to construct AnyMemory from.

__init__(out self, owned value: AnyMojoValue)

Creates AnyMemory from AnyMojoValue.

Args:

  • value (AnyMojoValue): AnyMojoValue to construct AnyMemory from.

is_tensor

is_tensor(self) -> Bool

Check whether this contains a tensor.

Returns:

True if contains tensor.

take_tensor

take_tensor(inout self) -> AnyTensor

Take tensor from object. Further access to this object is undefined behavior.

Returns:

The tensor inside the memory as AnyTensor.

take

take(inout self) -> Self

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

Returns:

Newly constructed AnyMemory that takes storage from this.

to_device_tensor

to_device_tensor(owned self) -> DeviceTensor

Consume this object and produces and instance of DeviceTensor. Only valid if this was created from DeviceTensor.

Returns:

DeviceTensor representation of AnyMemory.

to

to[T: Movable](owned self) -> $0

Consume this object and produces an instance of T. This doesn't do any type check beyond whether this is a AnyTensor or not, and if not assume this was created from T.

Returns:

An instance of type T.

take_value

take_value(inout self) -> AnyMojoValue

Take value from object. Further access to this object is undefined behavior.

Returns:

The value inside the memory as AnyMojoValue.

__str__

__str__(self) -> String

Gets this value as a string.

write_to

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

Formats the string representation of this value to the provided Writer.

Parameters:

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

Args:

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