Loop Operations

Looping Operations allow users to iterate over a sub-graph multiple times. This operation is similar to the While loop in Python, where a set of statements is executed as long as a condition is true.

Components of a Loop

A Loop is comprised of two essential subgraphs:

Initialization

This subgraph defines the variables that will be looped, setting the stage for the iteration process.

Loop Body

This subgraph contains the logic that will be executed repeatedly, based on the conditions and variables defined in the Initialization subgraph.

Components of a Loop

Setting Up Loop Initialization

This subgraph defines the variables that will be looped. However, since Graphbook does not permit direct data transfer from inputs to outputs, an Identity operation is employed to link the connection. This Identity operation is commonly renamed to Initialize Iteration for clarity.

Setting Multiple Looping Variables

Under Initialization, you can set multiple looping variables, as shown in the GPT-2 Text Generator example below. The loop initialization not only sets the iteration but also the next generated token and the concatenated text, including the next token.

Setting Multiple Looping Variables

Initialization Confirmation

Once the looping data is initiated, a green check mark appears within the initialization bracket, confirming that the initialization is complete.

Initialization Confirmation

Setting Up the Loop Body

After the data is initialized, the loop body subgraph can be updated. You need to connect the condition to the Run Again, a Boolean variable under the outputs.

Setting Up the Loop Body

Loop Body Confirmation

When Run Again is supplied, a play button will appear within the loop body icon, indicating that the loop is ready to run.

Track Iterations

Use an Add operation to keep track of the iteration count. The actual output will also be displayed, providing real-time feedback.

Track Iterations

Executing Loop Operation

Loop operation will not run by default. To run the loop, click the Run Loop option in the operation overflow menu.

Always Run Loop

Always Run Loop

To enable the loop to run automatically whenever it receives data, select Toggle always run loop under the operation overflow menu.

When enabled, a green outline will appear around the loop body icon, indicating that the loop is active. The GPT-2 Text Generator and some other pre-built loop operations have the always run loop enabled by default.