Eval: Using nested graphs¶
To create this graph we need only to install the tierkreis package:
pip install tierkreis
Graph¶
We can run graphs from within other graphs by using Graph.eval.
Recall the fib_step graph that we wrote in the previous tutorial
from typing import NamedTuple
from tierkreis.builder import Graph
from tierkreis.builtins import iadd
from tierkreis.models import TKR
class FibData(NamedTuple):
a: TKR[int]
b: TKR[int]
builder = Graph(FibData, FibData)
sum = builder.task(iadd(builder.inputs.a, builder.inputs.b))
fib_step = builder.finish_with_outputs(FibData(builder.inputs.b, sum))
We create a graph fib4 that calls fib_step three times.
The graph will have no inputs and gives a single integer as output:
from tierkreis.models import EmptyModel
fib4 = Graph(EmptyModel, TKR[int])
The Graph.eval method takes a Workflow object as its first argument
and the appropriately typed input data as the second object.
second = fib4.eval(fib_step, FibData(a=fib4.const(0), b=fib4.const(1)))
We can iterate manually as follows:
third = fib4.eval(fib_step, second)
fourth = fib4.eval(fib_step, third)
workflow = fib4.finish_with_outputs(fourth.b)
In the next tutorial we will see how to iterate programmatically.
Execution¶
Since we still only use built-in functions, we execute the graph in the same way as before.
from tierkreis.runtime import new_in_memory
runtime = await new_in_memory()
with runtime:
workflow_id = await runtime.save_workflow("Nested graphs using Eval", workflow)
run_id = await runtime.start_new_run(workflow_id, {})
await runtime.wait_for(run_id)
print(await runtime.get_outputs(run_id))
3