qewton.graphs.control_nodes package

Module contents

Submodules

qewton.graphs.control_nodes.data_processing_node module

class qewton.graphs.control_nodes.data_processing_node.DataProcessingNode(data_source_node: Node, name: str | None = 'DataProcessingNode', backend: type[Backend[TensorType]] = <class 'qewton.backends.base.DeepLearningBackend'>, **kwargs)[source]

Bases: GraphAwareNode[TensorType]

A node that processes data from a data source node. The data source node is typically a DataNode, but can be any node that provides data. The idea is that, when it is built, the data is run through the graph to collect all input data to this node, and then the data is processed in the fit-method.

Parameters:
  • data_source_node (Node) – The node where the original data comes from. This node is expected to be a DataNode. Note, that it does not have to be the node which is directly connected to this node!

  • name (str | None, optional) – Defaults to “DataProcessingNode”.

  • backend (type[Backend[TensorType]], optional) – Defaults to DEFAULT_DL_BACKEND.

build(graph) → None[source]

Build the node from the data of the graph it belongs to: run the graph up to this node to collect its input data, then fit to it.

Parameters:

graph (Graph) – The graph this node belongs to.

fit(data_batch: list[TensorType]) → None[source]

The main fitting behavior of the node. This method is called in the setup phase or can also be called manually to fit the node to a batch of data. The batch is expected to be a list of tensors, where each entry corresponds to a batch of data from the data source node.

Parameters:

data_batch (list[TensorType]) – The batch of data to fit the node to.

qewton.graphs.control_nodes.graph_node module

class qewton.graphs.control_nodes.graph_node.GraphNode(graph: ~qewton.graphs.graphs.Graph | None, input_ports: list[~qewton.graphs.nodes.InputPort] | dict[~qewton.graphs.nodes.InputPort, list[~qewton.graphs.nodes.InputPort]], output_ports: list[~qewton.graphs.nodes.OutputPort] | dict[~qewton.graphs.nodes.OutputPort, ~qewton.graphs.nodes.Port], name: str = 'GraphNode', backend: type[~qewton.backends.base.Backend[~qewton.backends.base.TensorType]] = <class 'qewton.backends.base.Backend'>, **kwargs)[source]

Bases: Node[TensorType]

A node that encapsulates an entire graph.

The inner graph is executed when the run method of this node is called. Input and output ports of the inner graph are mapped to the input and output ports of this GraphNode, allowing a complete graph to be used as a single node within a larger graph structure.

Parameters:
  • graph (Graph | None) – The graph to be encapsulated by this node; None for a node that builds it later (in build, e.g. once sizes are inferred). Its forward then declares all ports.

  • input_ports (list[InputPort] | dict[InputPort, list[InputPort]]) – Defines how the input ports of this GraphNode map to the input ports of the inner graph. If a list, new input ports for this node are created, and inner graph inputs are mapped by order. If a dict, keys are this GraphNode’s input ports, values are lists of corresponding inner graph input ports.

  • output_ports (list[OutputPort] | dict[OutputPort, Port]) – Defines how the output ports of this GraphNode map to the output ports of the inner graph. If a list, new output ports for this node are created, and inner graph outputs are mapped by order. If a dict, keys are this GraphNode’s output ports, values are corresponding inner graph output ports.

  • name (str, optional) – The name of this node. Defaults to “GraphNode”.

  • backend (type[Backend[TensorType]], optional) – The backend type for tensor operations. Defaults to Backend.

inner_graph

The encapsulated graph.

Type:

Graph

upgrade(version: int) → None[source]

See Node.upgrade(); the inner nodes are upgraded first, and ports not declared in forward() copy the configuration of the inner port they lead to.

Parameters:

version – The format version of the file.

update_inner_input_ports(new_input_ports: list[InputPort] | dict[InputPort, list[InputPort]])[source]

Updates the connections for the inner graph’s input ports.

This method is typically used during graph setup establish how external inputs connect to the encapsulated graph.

Parameters:

new_input_ports (list[InputPort] | dict[InputPort, list[InputPort]]) – The new input port configuration.

update_inner_output_ports(new_output_ports: list[OutputPort] | dict[OutputPort, OutputPort])[source]

Updates the connections for the inner graph’s output ports.

Similar to update_inner_input_ports, this method allows changing how the encapsulated graph’s outputs are exposed as this GraphNode’s outputs.

Parameters:

new_output_ports (list[OutputPort] | dict[OutputPort, OutputPort]) – The new output port configuration.

property inner_graph
property hyperparameters: list[HyperParameter]

Collects and returns all hyperparameters from the nodes within the encapsulated graph.

Returns:

A list of all hyperparameters found in the inner graph.

Return type:

list[HyperParameter]

property trainable_parameters: _TrainableParameterBase

Collects and returns all trainable parameters from the nodes within the encapsulated graph.

Returns:

A collection of trainable parameters from the inner

graph.

Return type:

_TrainableParameterBase

forward(*args, **kwargs)[source]

Evaluate the inner graph on the inputs.

Subclasses override this method only to declare the data configurations of their ports with typing.Annotated; the body stays return super().forward(...). Inside a graph, the steps of the inner graph run in place of this node instead.

Parameters:
  • *args – The values of the input ports, in order.

  • **kwargs – The values of input ports by name.

Returns:

The value of the output port, or a tuple for several.

Return type:

Any

Raises:

RuntimeError – If the node is not built and waits for sizes to be inferred from connected data.

setup_graph(graph: Graph, input_ports: list[InputPort] | dict[InputPort, list[InputPort]], output_ports: list[OutputPort] | dict[OutputPort, OutputPort])[source]

Sets up the encapsulated graph and its port mappings.

This method is intended to be called during the setup phase of the node, not during initialization, to allow for dynamic graph construction.

Parameters:
setup(graph=None) → None[source]

Build this node (see Node.setup()), then set up its inner graph.

Parameters:

graph (Graph, optional) – The graph being set up.

set_mode(new_mode: EvaluationPhase)[source]

Set the current evaluation phase.

Some nodes behave differently depending on the phase, e.g. dropout is disabled during validation.

Parameters:

new_mode – The new evaluation phase.

reset()[source]

Make this node and every node of its inner graph unbuilt (see Node.reset()).

to(device)[source]

Moves all nodes within the encapsulated graph to a specified device (e.g., GPU, CPU).

Parameters:

device – The target device.

unfreeze()[source]

Unfreezes the node, allowing modifications. (Currently a placeholder).

property memory_consumption: int
class qewton.graphs.control_nodes.graph_node.FromFunctionNode(function: Callable, name='FromFunctionNode', backend: type[Backend[TensorType]] = <class 'qewton.backends.base.Backend'>)[source]

Bases: GraphNode

A GraphNode that is constructed by analyzing the signature and execution flow of a provided function.

This node automatically builds an internal graph based on how `TrackingObject`s are used within the given function.

Parameters:
  • function (Callable) – The function to analyze for building the graph.

  • name (str, optional) – The name of this node. Defaults to “FromFunctionNode”.

  • backend (type[Backend[TensorType]], optional) – The backend type for tensor operations. Defaults to Backend.

class qewton.graphs.control_nodes.graph_node.TrackedNode(name='TrackedNode', backend: type[Backend[TensorType]] = <class 'qewton.backends.base.Backend'>)[source]

Bases: FromFunctionNode

A GraphNode where the internal graph is automatically built by tracking the forward method’s execution.

Parameters:
  • name (str, optional) – The name of this node. Defaults to “TrackedNode”.

  • backend (type[Backend[TensorType]], optional) – The backend type for tensor operations. Defaults to Backend.

unfreeze()[source]

Unfreezes the node. For a TrackedNode, this currently does nothing, but could be extended to convert it into a modifiable GraphNode.

build() → None[source]

Create what the node computes with, e.g. the weights or the inner graph of a network, from the current hyperparameter values.

Called by setup() for a node that is not built yet; a tuner makes a node unbuilt again (reset()) before trying other hyperparameter values. Does nothing by default.

class qewton.graphs.control_nodes.graph_node.CopiedNode(node_to_copy: Node)[source]

Bases: GraphNode

A special type of GraphNode that encapsulates a copy of another Node.

This allows for creating instances of existing nodes within a single graph context without directly modifying the original node.

Parameters:

node_to_copy (Node) – The node to be copied and encapsulated.

copy()[source]

Returns a new CopiedNode instance that refers to the same original node.

This prevents infinite recursion if copy is called on an already copied node.

qewton.graphs.control_nodes.wrapper_node module

class qewton.graphs.control_nodes.wrapper_node.FunctionWrappingNode(function: Callable, name: str | None = None, backend: type[Backend[TensorType]] = <class 'qewton.backends.base.Backend'>)[source]

Bases: Node

A Node that wraps a plain Python callable and executes it directly (eagerly) at evaluation time, without tracing its execution into an internal graph of sub-nodes.

In contrast to FromFunctionNode, which analyzes a function by calling it once with TrackingObject`s (and therefore only supports operations understood by that tracing mechanism), `FunctionWrappingNode treats the function as an opaque, atomic operation: its input ports are derived from the function’s signature and type hints (e.g. Variable annotations), but the function body itself can be arbitrary Python code, since it is simply called with the real input tensors at evaluation time. This trades away automatic decomposition into a sub-graph (used e.g. for structural introspection) for full flexibility of the function body.

If the function has no return type annotation, a single output port with an empty (dynamically resolved) DataConfiguration is created, since the output shape cannot be inferred without tracing.

Parameters:
  • function (Callable) – The function to wrap. Called directly with the real input tensors at evaluation time.

  • name (str, optional) – The name of this node. Defaults to the function’s __name__.

  • backend (type[Backend[TensorType]], optional) – The backend type for tensor operations. Defaults to Backend.