struct
Symbol
Represents a symbolic value within a Graph
.
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 Symbol
values as inputs
to form a computation graph.
A Symbol
may also refer to an existing input or output of a node, and you
can change them, such as by swapping a new Symbol
.
Conceptually, a Symbol
is the equivalent of a variable 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 a regular variable, a Symbol
has a data type.
Note: All the methods in this type are documented as "Creates foo". This is a shorthand notation for "Adds a node representing an op that returns foo".
Fields
- handle (
Value
): A handle to thisSymbol
's internal representation.
Implemented traits
AnyType
,
CollectionElement
,
Copyable
,
Movable
,
Stringable
Methods
__getitem__
__getitem__(self: Self, i: Self, axis: Int = 0) -> 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 = 0) -> Self
Symbolic slicing - indexes a value by a single constant index.
Uses the mo.gather
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. Thei
thSymbolicSlice
in the variadic list represents the begin🔚step triple for axisi
.
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[float64, 1]) -> Self
Element-wise addition by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise subtraction by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise multiplication by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise division by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise raise to power by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise addition by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise subtraction by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise multiplication by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise division by a Float64
.
Args:
- rhs (
SIMD[float64, 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[float64, 1]) -> Self
Element-wise raise to power by a Float64
.
Args:
- rhs (
SIMD[float64, 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) -> Type
Returns this Symbol
's type.
Returns:
The Symbol
's type.
tensor_type
tensor_type(self: Self) -> TensorType
Returns this Symbol
's type, as TensorType
.
Implicitly asserts that the type is indeed TensorType
, and raises an
error otherwise.
Returns:
The tensor type of this Symbol
.
rebind
rebind(self: Self, *dims: Dim) -> Self
reshape
reshape(self: Self, *dims: Variant[Symbol, Int]) -> Self
Reshapes this Symbol
.
Uses the mo.reshape
op. Requires the symbol to be a TensorType
.
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 = "debug_tensor")
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 thisSymbol
, and its return value will replace all of its uses.