Skip to main content

Mojo struct

TensorSpec

A space efficient representation of a tensor shape and dtype. This struct implements value semantics and owns its underlying data.

Fields

  • shape (TensorShape): The underlying shape of the specification.

Implemented traits

AnyType, CollectionElement, Copyable, EqualityComparable, Formattable, Movable, Stringable

Methods

__init__

__init__(inout self: Self)

Default initializer for TensorShape.

__init__(inout self: Self, type: DType, *shapes: Int)

Initializes a Tensorspec from the dtype and shapes provided.

Args:

  • type (DType): The dtype of the specification.
  • *shapes (Int): The shapes to initialize the shape with.

__init__(inout self: Self, type: DType, shape: Tuple[element_types])

Initializes a Tensorspec from the dtype and shapes provided.

Args:

  • type (DType): The dtype of the specification.
  • shape (Tuple[element_types]): The shapes to initialize the shape with.

__init__(inout self: Self, type: DType, shapes: VariadicList[Int])

Initializes a Tensorspec from the dtype and shapes provided.

Args:

  • type (DType): The dtype of the specification.
  • shapes (VariadicList[Int]): The shapes to initialize the shape with.

__init__(inout self: Self, type: DType, shapes: List[Int, hint_trivial_type])

Initializes a Tensorspec from the dtype and shapes provided.

Args:

  • type (DType): The dtype of the specification.
  • shapes (List[Int, hint_trivial_type]): The shapes to initialize the shape with.

__init__(inout self: Self, type: DType, owned shape: TensorShape)

Initializes a Tensorspec from the dtype and shape provided.

Args:

  • type (DType): The dtype of the specification.
  • shape (TensorShape): The shapes to initialize the shape with.

__copyinit__

__copyinit__(inout self: Self, other: Self)

Creates a deep copy of an existing spec.

Args:

  • other (Self): The spec to copy.

__moveinit__

__moveinit__(inout self: Self, owned existing: Self)

Move initializer for the spec.

Args:

  • existing (Self): The spec to move.

__getitem__

__getitem__(self: Self, index: Int) -> Int

Gets the dimension at the specified index.

Args:

  • index (Int): The dimension index.

Returns:

The dimension at the specified index.

__eq__

__eq__(self: Self, other: Self) -> Bool

Returns True if the two values are the same and False otherwise.

Args:

  • other (Self): The other TensorSpec to compare against.

Returns:

True if the two specs are the same and False otherwise.

__ne__

__ne__(self: Self, other: Self) -> Bool

Returns True if the two values are not the same and False otherwise.

Args:

  • other (Self): The other TensorSpec to compare against.

Returns:

True if the two specs are the not the same and False otherwise.

rank

rank(self: Self) -> Int

Gets the rank of the spec.

Returns:

The rank of the spec.

dtype

dtype(self: Self) -> DType

Gets the DType of the spec.

Returns:

The DType of the spec.

num_elements

num_elements(self: Self) -> Int

Gets the total number of elements in the spec.

Returns:

The total number of elements in the spec.

bytecount

bytecount(self: Self) -> Int

Gets the total byte count.

Returns:

The total byte count.

__repr__

__repr__(self: Self) -> String

Returns the string representation of the spec.

Returns:

The string representation of the spec.

__str__

__str__(self: Self) -> String

Returns the string representation of the spec.

Returns:

The string representation of the spec.

format_to

format_to(self: Self, inout writer: Formatter)

Formats this TensorSpec to the provided formatter.

Args:

  • writer (Formatter): The formatter to write to.

from_bytes

static from_bytes(data: UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]) -> Self

Create a TensorSpec object from serialized bytes.

Args:

  • data (UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]): UnsafePointer to serialized bytes.

Returns:

Given bytes as TensorSpec.