Skip to main content

Mojo struct

TensorShape

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

Implemented traits

AnyType, CollectionElement, Copyable, EqualityComparable, Movable, Stringable, Writable

Methods

__init__

__init__(out self)

Default initializer for TensorShape.

__init__[*Ts: CollectionElement](out self, shapes: Tuple[Ts])

Initializes a TensorShape from the values provided.

Args:

  • shapes (Tuple[Ts]): The shapes to initialize the shape with.

__init__(out self, *shapes: Int)

Initializes a TensorShape from the values provided.

Args:

  • *shapes (Int): The shapes to initialize the shape with.

__init__(out self, shapes: VariadicList[Int])

Initializes a TensorShape from the values provided.

Args:

  • shapes (VariadicList[Int]): The shapes to initialize the shape with.

__init__(out self, shapes: List[Int, hint_trivial_type])

Initializes a TensorShape from the list provided.

Args:

  • shapes (List[Int, hint_trivial_type]): The list to initialize the shape with.

__init__[rank: Int](out self, shapes: IndexList[rank])

Initializes a TensorShape from the values provided.

Parameters:

  • rank (Int): The rank.

Args:

  • shapes (IndexList[rank]): The shapes to initialize the shape with.

__copyinit__

__copyinit__(out self, other: Self)

Creates a deep copy of an existing shape.

Args:

  • other (Self): The shape to copy.

__del__

__del__(owned self)

Delete the shape and release any owned memory.

__getitem__

__getitem__(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, other: Self) -> Bool

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

Args:

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

Returns:

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

__ne__

__ne__(self, other: Self) -> Bool

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

Args:

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

Returns:

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

rank

rank(self) -> Int

Gets the rank of the shape.

Returns:

The rank of the shape.

num_elements

num_elements(self) -> Int

Gets the total number of elements in the shape.

Returns:

The total number of elements in the shape.

__repr__

__repr__(self) -> String

Returns the string representation of the shape.

Returns:

The string representation of the shape.

__str__

__str__(self) -> String

Returns the string representation of the shape.

Returns:

The string representation of the shape.

write_to

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

Formats this TensorShape to the provided Writer.

Parameters:

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

Args:

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