Skip to main content

symbol

Symbolic value primitives.

A Symbol can represent the output of a Node, the arguments of a Graph (as seen from within its body), and more generally any symbolic value available within the Graph. Other Nodes receive Symbols as inputs to form a computation graph.

Symbols may also be used to refer to an existing input or output of a Node, and one may change these, for instance by swapping a new Symbol.

Conceptually, Symbols are the equivalent of variables in Mojo. A Symbol can also be thought of as the end of an edge in the dataflow graph (the other end being one use of that symbol).

Similar to regular variables, Symbols have a data type.

Note: All the helpers in this module are documented as "Creates foo". This is a shorthand notation for "Adds a node representing an op that returns foo".

Symbol

Represents a symbolic value within a Graph.

Fields:

  • handle (Value): An handle to this Symbol's internal representation.

Implemented traits:

AnyType, CollectionElement, Copyable, Movable, Stringable

Methods:

__getitem__

__getitem__(self: Self, i: Self, axis: Int) -> Self

Symbolic slicing - indexes a value by a single index.

Uses the mo.slice op.

Args:

  • i (Self): The index value.
  • axis (Int): The axis to index at.

Returns:

The slicing result.

__getitem__(self: Self, i: Int, axis: Int) -> Self

Symbolic slicing - indexes a value by a single constant index.

Uses the mo.slice op and automatically wraps i inside a mo.constant.

Args:

  • i (Int): The index value.
  • axis (Int): The axis to index at.

Returns:

The slicing result.

__getitem__(self: Self, *s: SymbolicSlice) -> Self

Range-based slicing.

Uses the mo.slice op. Slicing along multiple dimensions is supported.

Args:

  • s (*SymbolicSlice): The slice values. The ith SymbolicSlice in the variadic list represents the begin🔚step triple for axis i.

Returns:

The slicing result.

__getitem__(self: Self, s: Slice) -> Self

Shorthand for symbolic slicing with an Int range.

This overload only supports slicing along the first axis.

Args:

  • s (Slice): The slice value.

Returns:

The slicing result.

__neg__

__neg__(self: Self) -> Self

Numerical negative, element-wise.

Returns:

The operation result.

__add__

__add__(self: Self, rhs: Self) -> Self

Element-wise addition.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__add__(self: Self, rhs: Int) -> Self

Element-wise addition by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__add__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise addition by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__sub__

__sub__(self: Self, rhs: Self) -> Self

Element-wise subtraction.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__sub__(self: Self, rhs: Int) -> Self

Element-wise subtraction by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__sub__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise subtraction by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__mul__

__mul__(self: Self, rhs: Self) -> Self

Element-wise multiplication.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__mul__(self: Self, rhs: Int) -> Self

Element-wise multiplication by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__mul__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise multiplication by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__matmul__

__matmul__(self: Self, rhs: Self) -> Self

Matrix multiplication.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__truediv__

__truediv__(self: Self, rhs: Self) -> Self

Element-wise division.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__truediv__(self: Self, rhs: Int) -> Self

Element-wise division by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__truediv__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise division by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__pow__

__pow__(self: Self, rhs: Self) -> Self

Element-wise raise to power.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__pow__(self: Self, rhs: Int) -> Self

Element-wise raise to power by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__pow__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise raise to power by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__radd__

__radd__(self: Self, rhs: Self) -> Self

Element-wise addition.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__radd__(self: Self, rhs: Int) -> Self

Element-wise addition by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__radd__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise addition by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__rsub__

__rsub__(self: Self, rhs: Self) -> Self

Element-wise subtraction.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__rsub__(self: Self, rhs: Int) -> Self

Element-wise subtraction by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__rsub__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise subtraction by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__rmul__

__rmul__(self: Self, rhs: Self) -> Self

Element-wise multiplication.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__rmul__(self: Self, rhs: Int) -> Self

Element-wise multiplication by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__rmul__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise multiplication by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__rtruediv__

__rtruediv__(self: Self, rhs: Self) -> Self

Element-wise division.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__rtruediv__(self: Self, rhs: Int) -> Self

Element-wise division by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__rtruediv__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise division by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

__rpow__

__rpow__(self: Self, rhs: Self) -> Self

Element-wise raise to power.

Args:

  • rhs (Self): The right hand side operand.

Returns:

The operation result.

__rpow__(self: Self, rhs: Int) -> Self

Element-wise raise to power by an Int literal.

Args:

  • rhs (Int): The right hand side operand.

Returns:

The operation result.

__rpow__(self: Self, rhs: SIMD[f64, 1]) -> Self

Element-wise raise to power by a Float64.

Args:

  • rhs (SIMD[f64, 1]): The right hand side operand.

Returns:

The operation result.

graph

graph(self: Self) -> Graph

Returns the Graph owning this Symbol.

Returns:

The parent Graph.

type

type(self: Self) -> AnyMOType

Returns this Symbol's type.

Returns:

The Symbol's type.

tensor_type

tensor_type(self: Self) -> MOTensor

Returns this Symbol's type, as MOTensor.

Implicitly asserts that the type is indeed MOTensor, and raises an error otherwise.

Returns:

The tensor type of this Symbol.

__str__

__str__(self: Self) -> String

Returns a String representation of this Symbol.

The representation uses an internal MLIR Assembly format, and typically shows the Node that outputs this Symbol. Its structure is subject to change without notice, and should not be used for serialization. For debugging purposes only.

Returns:

A textual representation of this Symbol.

reshape

reshape(self: Self, *dims: Variant[Symbol, Int]) -> Self

Reshapes this Symbol.

Uses the mo.reshape op. Requires the symbol to be a MOTensor.

Args:

  • dims (*Variant[Symbol, Int]): The new dimensions.

Returns:

A new Symbol that has the given shape.

swapaxes

swapaxes(self: Self, axis1: Int, axis2: Int) -> Self

Interchanges two axes of this Symbol.

Uses the mo.transpose op. Negative values are allowed, and represent the axis number counting from the last.

Args:

  • axis1 (Int): One of the axes to swap.
  • axis2 (Int): The other axis.

Returns:

A new transposed Symbol.

print

print(self: Self, label: String)

Prints this Symbol's value at runtime.

This uses mo.debug.tensor.print to enable printing the runtime value that this Symbol represents, at grpah execution time.

Args:

  • label (String): A label to accompany the printout.

insert_transformation

insert_transformation(self: Self, transform: fn(Symbol) raises -> Symbol)

Inserts nodes in between this Symbol and all its current uses.

This enables inserting ops in between this Symbol and all its uses, for example to modify an existing Graph.

Note: The function is called exactly once, even if self has no uses.

Args:

  • transform (fn(Symbol) raises -> Symbol): A function that creates a unary subgraph (single input, single output) and returns the result of the final node. The function will be called with this Symbol, and its return value will replace all of its uses.

SymbolicSlice

Slice-like struct with Symbol fields.

This struct enables the range slice (start:stop:end) operator with Symbol indices.

Fields:

  • start (Optional[Symbol]): The slice's start index.
  • stop (Optional[Symbol]): The slice's start index, exclusive.
  • step (Optional[Symbol]): The slice's step.

Implemented traits:

AnyType, CollectionElement, Copyable, Movable

Methods:

__init__

__init__(inout self: Self, owned g: Graph, owned s: Slice)

Convenience constructor from a Slice.

This wraps any indices in s into constant nodes (using mo.constant).

Args:

  • g (Graph): The Graph into which the constant nodes are created.
  • s (Slice): The Slice to turn into SymbolicSlice.

SymbolTuple

A tuple of Symbols.

This struct mainly offers the convenience of building a tuple of Symbols using the tuple literal ((a, b, c)) notation. It is largely equivalent to a List[Symbol].

Fields:

  • symbols (List[Symbol]): The actual list of symbols.

Implemented traits:

AnyType, Copyable, Movable, Sized

Methods:

__init__

__init__(inout self: Self, *symbols: Symbol)

Constructor from a variadic list of Symbols.

Args:

  • symbols (*Symbol): Symbols to initializae the tuple with.

__init__(inout self: Self, owned symbols: Tuple[])

Convenience constructor for an empty tuple.

Args:

  • symbols (Tuple[]): The empty tuple.

__init__(inout self: Self, owned symbols: Tuple[Symbol, Symbol])

Convenience constructor from a 2-tuple.

Args:

  • symbols (Tuple[Symbol, Symbol]): A tuple of two Symbols.

__init__(inout self: Self, owned symbols: Tuple[Symbol, Symbol, Symbol])

Convenience constructor from a 3-tuple.

Args:

  • symbols (Tuple[Symbol, Symbol, Symbol]): A tuple of three Symbols.

__init__(inout self: Self, owned symbols: Tuple[Symbol, Symbol, Symbol, Symbol])

Convenience constructor from a 4-tuple.

Args:

  • symbols (Tuple[Symbol, Symbol, Symbol, Symbol]): A tuple of four Symbols.

__getitem__

__getitem__(self: Self, i: Int) -> Symbol

Returns the Symbol at the specified index.

Args:

  • i (Int): The index to retrieve from.

Returns:

The Symbol at position i.

__len__

__len__(self: Self) -> Int

Returns the length of this SymbolTuple.