-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursive WorkGraph with Graph Builder can only be run for the amount of redefinition of it #333
Comments
The error message is otherwise
|
Nice try! The error is because inside the graph_builder (local scope), there is no In order to create recursive WorkGraph, you need to define One quick example is that you add this function into the @task.graph_builder(outputs = [{"name": "result", "from": "multiply.result"}])
def recursive(n):
from aiida_workgraph import WorkGraph
from aiida_workgraph.tasks.test import recursive
wg = WorkGraph()
if n == 1:
mytask = wg.add_task(identity, name="multiply", x=n)
return wg
t = wg.add_task(recursive, name="recursive", n=n-1)
wg.add_task(multiply, name="multiply", x=n, y=t.outputs["result"])
return wg |
Here is another working version; however, I am not very clear why it works. from aiida_workgraph import task, WorkGraph
from aiida import load_profile
load_profile()
@task.calcfunction
def identity(x):
return x.clone()
@task.calcfunction
def multiply(x, y):
return x*y
def recursive(n):
wg = WorkGraph()
if n == 1:
mytask = wg.add_task(identity, name="multiply", x=n)
return wg
Task = task.graph_builder(outputs = [{"name": "result", "from": "multiply.result"}])(recursive)
t = wg.add_task(Task, name=f"recursive_{n-1}", n=n-1)
wg.add_task(multiply, name="multiply", x=n, y=t.outputs["result"])
return wg
Task = task.graph_builder(outputs = [{"name": "result", "from": "multiply.result"}])(recursive)
wg = WorkGraph()
mytask = wg.add_task(Task, n=3, name="recursive_3")
wg.run() |
Because @giovannipizzi mentioned that for a project that need to use recursive workflows I wanted to do an example for recursive workflows. It kind of works, but there are some problems with referring to the the graph builder itself
I have to rerun the
recursive
definition the amount of time I want to use it in the recursionThe text was updated successfully, but these errors were encountered: