tinygrad shape.symbolic
Note
You likely want the upstream tinygrad, not tinygrab. Tinygrab contains AI generated docstrings for a tinygrad snapshot. Upstream: https://tinygrad.org
- class tinygrad.shape.symbolic.AndNode(nodes: List[Node])[source]
Bases:
RedNode
AndNode class for creating AND operations on a list of nodes.
- RedNode
Parent class for this class.
- Type:
Class
- class tinygrad.shape.symbolic.DivNode(a: Node, b: Node | int)[source]
Bases:
OpNode
The DivNode class is a subclass of OpNode. It defines the behavior of division operations on nodes.
- b
The divisor integer.
- Type:
int
- get_bounds() Tuple[int, int] [source]
Get the lower and upper bounds of a DivNode.
- Returns:
A tuple with the lower and upper bounds.
- substitute(var_vals: Dict[Variable | NumNode, Node]) Node [source]
Substitute the variables in a DivNode with their corresponding nodes.
- Parameters:
var_vals – A dictionary containing the variables as keys and the corresponding nodes as values.
- Returns:
A new DivNode with the variables substituted by their nodes.
- class tinygrad.shape.symbolic.LtNode(a: Node, b: Node | int)[source]
Bases:
OpNode
This class represents the lower than operator for nodes.
- get_bounds() Tuple[int, int] [source]
Get the bounds of the LtNode.
- Parameters:
self (LtNode) – The instance of LtNode.
- Returns:
A tuple representing the lower and upper bounds.
- Return type:
Tuple[int, int]
- class tinygrad.shape.symbolic.ModNode(a: Node, b: Node | int)[source]
Bases:
OpNode
The ModNode class is a subclass of OpNode. It defines the behavior of modulo operations on nodes.
- b
The divisor integer.
- Type:
int
- get_bounds() Tuple[int, int] [source]
Get the lower and upper bounds of a ModNode.
- Returns:
A tuple with the lower and upper bounds.
- substitute(var_vals: Dict[Variable | NumNode, Node]) Node [source]
Substitute the variables in a ModNode with their corresponding nodes.
- Parameters:
var_vals – A dictionary containing the variables as keys and the corresponding nodes as values.
- Returns:
A new ModNode with the variables substituted by their nodes.
- class tinygrad.shape.symbolic.MulNode(a: Node, b: Node | int)[source]
Bases:
OpNode
This class represents a multiplication node.
- get_bounds() Tuple[int, int] [source]
This method returns the lower and upper bounds of the current node.
- Returns:
A tuple containing the lower and upper bounds.
- Return type:
Tuple[int, int]
- class tinygrad.shape.symbolic.Node[source]
Bases:
object
The base class for a node in the expression tree. This class represents a single point in the grid of possible expressions.
- min
The minimum value of this node.
- Type:
int
- max
The maximum value of this node.
- Type:
int
- static ands(nodes: List[Node]) Node [source]
This function takes a list of nodes as input and returns a single node.
- Returns:
A single node that represents the logical AND of all input nodes. If any node in the input list is False, it will return a NumNode(0). If all nodes are True, it will return the last node in the list. If there’s only one unique node (excluding NumNode(1)), it will return this node.
- Return type:
- expand(idxs: Tuple[Variable | NumNode, ...] | None = None) List[Node] [source]
Expand a Node into List[Node] that enumerates the underlying Variables from min to max.
The expansion increments earlier variables faster than later variables (as specified in the argument).
- Parameters:
idxs (Optional[Tuple[VariableOrNum, ...]]) – The indices to expand. Defaults to None.
- Returns:
The expanded nodes.
- Return type:
List[Node]
- expand_idx() Variable | NumNode [source]
Expand a Node into a single Variable or an integer if the underlying Variables are not defined.
- Returns:
The expanded node.
- Return type:
VariableOrNum
- property hash: int
Returns a hash value for the node based on its key.
- Returns:
A hash value of the node’s key.
- Return type:
int
- static iter_idxs(idxs: Tuple[Variable | NumNode, ...]) Iterator[Tuple[int, ...]] [source]
Get an iterator over the indices from min to max for each Variable.
- Parameters:
idxs (Tuple[VariableOrNum, ...]) – The indices to iterate over.
- Returns:
An iterator over the indices.
- Return type:
Iterator[Tuple[int, …]]
- property key: str
Returns a string representation of the node.
- ctx
The context for rendering the node. Defaults to “DEBUG”.
- Type:
str
- Returns:
A string representation of the node.
- Return type:
str
- max: int
- min: int
- render(ops=None, ctx=None) Any [source]
Render the expression tree as a string or other object depending on the output type.
- Parameters:
ops (Optional[Dict[Type[Node], Callable]]) – The dictionary of rendering functions for each node type. Defaults to render_python.
ctx (Optional[Any]) – The context for the rendering function.
- Returns:
The rendered object.
- Return type:
Any
- substitute(var_vals: Dict[Variable | NumNode, Node]) Node [source]
Substitute Variables with the values in var_vals.
- class tinygrad.shape.symbolic.NumNode(num: int)[source]
Bases:
Node
Node class to represent a number.
- b
The numeric value of the node.
- Type:
int
- min
Minimum possible value for this node.
- Type:
int
- max
Maximum possible value for this node.
- Type:
int
- bind(val)[source]
Bind the value of this node to another value.
- Parameters:
val – The value to bind this node to.
- Returns:
This node with its value bound to val.
- Return type:
- Raises:
AssertionError – If self.b is not equal to val.
- class tinygrad.shape.symbolic.OpNode(a: Node, b: Node | int)[source]
Bases:
Node
An operation node class that represents a node with an operation involving two nodes or a node and an integer value.
- class tinygrad.shape.symbolic.RedNode(nodes: List[Node])[source]
Bases:
Node
Base class for all “red” nodes.
- class tinygrad.shape.symbolic.SumNode(nodes: List[Node])[source]
Bases:
RedNode
SumNode class for representing sum of nodes.
- property flat_components
Get the flattened components of a SumNode.
If a component is an instance of SumNode, it will be expanded; otherwise, it remains unchanged. This method is used to flatten a nested sum of nodes into a flat list of nodes.
- Returns:
The flattened components of the SumNode.
- Return type:
List[Node]
- class tinygrad.shape.symbolic.Variable(expr: str | None, nmin: int, nmax: int)[source]
Bases:
Node
Variable class.
- expr
Optional string representation of the variable.
- Type:
Optional[str]
- nmin
Minimum value for the variable.
- Type:
int
- nmax
Maximum value for the variable.
- Type:
int
- bind(val)[source]
Bind the Variable to a specific value.
- Parameters:
self – Self reference.
val (int) – The value to bind the Variable to.
- Returns:
The bound Variable.
- Return type:
- Raises:
AssertionError – If the Variable is already bound or the value is outside the specified range.
- substitute(var_vals: Dict[Variable | NumNode, Node]) Node [source]
Substitute the Variable with its value or return itself if not in var_vals.
- unbind() Tuple[Variable, int] [source]
Unbind the Variable from its value and return a new Variable with the same properties and its value.
- Parameters:
self – Self reference.
- Returns:
A tuple containing a new Variable object and the value it was bound to.
- Return type:
Tuple[Variable, int]
- Raises:
AssertionError – If the Variable is not bound.
- property val
Get the value of the Variable.
- Parameters:
self – Self reference.
- Returns:
The value of the Variable.
- Return type:
int
- Raises:
AssertionError – If the Variable is not bound to a value.
- tinygrad.shape.symbolic.create_node(ret: Node)[source]
Create a node object based on the provided parameters.
- tinygrad.shape.symbolic.create_rednode(typ: Type[RedNode], nodes: List[Node])[source]
Create a new red node.
- tinygrad.shape.symbolic.is_sym_int(x: Any) bool [source]
Check if an object is either of type int or an instance of the Node class.
- tinygrad.shape.symbolic.x
The object to check.
- Type:
Any
- Returns:
True if x is of type int or an instance of Node, False otherwise.
- Return type:
bool
- tinygrad.shape.symbolic.sym_infer(a: Node | int, var_vals: Dict[Variable, int]) int [source]
Perform inference on the given node or integer with the provided variable values.
- tinygrad.shape.symbolic.sym_render(a: Node | int, ops=None, ctx=None) str [source]
Render a symbolic representation of the given node or integer.
- Parameters:
a (Union[Node, int]) – The node or integer to be rendered.
ops (Any) – Optional operations for rendering. Default is None.
ctx (Any) – Optional context for rendering. Default is None.
- Returns:
A string representation of the given node or integer.
- Return type:
str