Mojo 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
,
Formattable
,
Movable
,
Stringable
Methodsβ
__getitem__
β
__getitem__(self: Self, i: Variant[Symbol, Int], axis: Int = 0, keep_dims: Bool = 0) -> Self
Symbolic slicing - indexes a value by a single index.
Uses the mo.slice
op.
Args:
- βi (
Variant[Symbol, Int]
): The index value. - βaxis (
Int
): The axis to index at. - βkeep_dims (
Bool
): Returns a tensor with the same rank as the input if set.
Returns:
The slicing result.
__getitem__(self: Self, *s: SymbolicSlice, *, out_dims: List[Dim, 0]) -> 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:end:step triple for axisi
. - βout_dims (
List[Dim, 0]
): The expected output dimensions returned by slicing. These will be assert at graph execution time to be correct.
Returns:
The slicing result.
__getitem__(self: Self, *slices: Slice, *, out_dims: List[Dim, 0] = List()) -> Self
Shorthand for symbolic slicing with Int
ranges.
Args:
- β*slices (
Slice
): The slice values for each dimension respectively. If fewer thanrank()
slices are provided, the remaining dimensions will be trivially sliced. In other wordss[:, :2]
is equivalent tos[:, :2, :, :]
for a tensor of rank 4. Currently indexing and slicing may not be mixed. - βout_dims (
List[Dim, 0]
): The expected output dimensions returned by slicing. These will be assert at graph execution time to be correct.
Returns:
The slicing result.
Raises:
An exception if out_dims is empty and can't be calculated at graph build time.
__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
.
shape
β
shape(self: Self) -> List[Dim, 0]
Returns this Symbol
's tensor shape, as List[Dim]
.
Implicitly asserts that the type is indeed TensorType
, and raises an
error otherwise.
Returns:
The tensor shape 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
.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this symbol to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
rebind
β
rebind(self: Self, *dims: Dim) -> Self
rebind(self: Self, dims: List[Dim, 0]) -> Self
reshape
β
reshape(self: Self) -> Self
reshape(self: Self, *dims: Int) -> Self
Reshapes this Symbol
.
Uses the mo.reshape
op. Requires the symbol to be a TensorType
.
Args:
- β*dims (
Int
): The new dimensions.
Returns:
A new Symbol
that has the given shape.
reshape(self: Self, *dims: Dim) -> Self
Reshapes this Symbol
.
Uses the mo.reshape
op. Requires the symbol to be a TensorType
.
Args:
- β*dims (
Dim
): The new dimensions.
Returns:
A new Symbol
that has the given shape.
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
.
broadcast_to
β
broadcast_to(self: Self, *dims: Dim) -> Self
Broadcasts this Symbol
to the specified dims.
Uses the mo.broadcast_to
op. Requires the symbol to be a TensorType
.
Args:
- β*dims (
Dim
): The target dimensions.
Returns:
A new Symbol
that is broadcast to the given shape.
broadcast_to(self: Self, shape: List[Dim, 0], location: Optional[_SourceLocation] = #kgen.none) -> Self
Broadcasts this Symbol
to the specified shape.
Uses the mo.broadcast_to
op. Requires the symbol to be a TensorType
.
Args:
- βshape (
List[Dim, 0]
): The target shape. - βlocation (
Optional[_SourceLocation]
): An optional location for a more specific error message.
Returns:
A new Symbol
that is broadcast to the given shape.
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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?