Skip to content
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

Create subsection "Nested workflow" in the docs #277

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

agoscinski
Copy link
Contributor

@agoscinski agoscinski commented Aug 27, 2024

Moves the graph builder, if task and while task examples into this subsection. Also removed the graph_builder.ipynb since it is now generated. Solves issue #195

@agoscinski agoscinski marked this pull request as ready for review August 27, 2024 12:19
@agoscinski agoscinski force-pushed the create-docs-nested-workflows branch 2 times, most recently from 94e0a29 to b4ed841 Compare August 27, 2024 12:26
@codecov-commenter
Copy link

codecov-commenter commented Aug 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.60%. Comparing base (5937b88) to head (3146393).
Report is 84 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #277      +/-   ##
==========================================
+ Coverage   75.75%   80.60%   +4.85%     
==========================================
  Files          70       66       -4     
  Lines        4615     5135     +520     
==========================================
+ Hits         3496     4139     +643     
+ Misses       1119      996     -123     
Flag Coverage Δ
python-3.11 80.52% <ø> (+4.85%) ⬆️
python-3.12 80.52% <ø> (?)
python-3.9 80.56% <ø> (+4.82%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@agoscinski agoscinski force-pushed the create-docs-nested-workflows branch 3 times, most recently from 16e2070 to b059def Compare August 27, 2024 13:34
@agoscinski
Copy link
Contributor Author

test_pause_task_after_submit seems a bit unstable it failed already two times in different jobs

Copy link
Member

@superstar54 superstar54 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the if and while task again, because we now have new If task and while task, thus there are two ways to implement if

  • if task, dose not need nested workgraph
  • use graph_builder, thus nested workgraph.

thus it's better to keep if and while separately, and not move to the nested_workgraph.

I suggest at the end of the graph_builder.py, we make a link to show the application in the if and while.

@agoscinski agoscinski force-pushed the create-docs-nested-workflows branch 3 times, most recently from fb9410e to c344a45 Compare August 28, 2024 14:00
@agoscinski
Copy link
Contributor Author

I dont think the graph_builder example is really about the dynamic part as it just forwards inputs like any task does it. We could make another example that runs different tasks depending on the inputs. Something like

@task.calcfunction()
def add_one(x):
    return x.value+1

@task.calcfunction()
def modulo_five(x):
    return x % 5

@graph_builder(outputs = [...])
def my_modular(i: orm.Int):
    wg = WorkGraph()
    if i.value < 5:
        task = wg.add_task(add_one)
    else:
        task = wg.add_task(modulo_five)
    # need wg.selector to expose for linking?
    return wg

Then one could say that this does not preserve provenance and directly interlude to the If workgraph

@agoscinski
Copy link
Contributor Author

Added an example for a dynamic use case of the graph_builder

Copy link
Member

@superstar54 superstar54 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @agoscinski , Thanks for the work.

I would suggest not creating a dynamic-workflows section, but moving all three notebooks into the howto, so that user can see the dynamic, if and while immediately.

Comment on lines 41 to 51
@task.graph_builder(outputs=[{"name": "result", "from": "context.out"}])
def add_modulo(i: Int):
wg = WorkGraph()
if i.value < 2:
task = wg.add_task(add_one, x=i)
else:
task = wg.add_task(modulo_two, x=i)

task.set_context({"result": "out"})
return wg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate as in the if. I would suggest using a for loop inside the graph_builder so that the number of tasks depends on an input value, making the work graph dynamic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have quite a few for loops in the examples too therefore I don't see duplication as a reason to rather use for loops. I feel this example is more educative because you are changing the type of task, you are in different branch of your program depending on your input which might be more intuitive to be dynamic.

But I can also just add both.

@superstar54
Copy link
Member

Then one could say that this does not preserve provenance and directly interlude to the If workgraph

Why ti does not preserve provenance?

@agoscinski
Copy link
Contributor Author

I would suggest not creating a dynamic-workflows section, but moving all three notebooks into the howto, so that user can see the dynamic, if and while immediately.

Okay, but the howto's starting to get messy and need some structure at some point, but I think for now one still can put everything there

@agoscinski
Copy link
Contributor Author

Why ti does not preserve provenance?

I mean more that it does not store provenance as transparently as using the If WorkGraph, but looking at the provenance graph, It is not really capturing the if-then-else logic, but just only some additional information about the condition. But at least in the GUI the if-then-else is capture more transparently.

@agoscinski
Copy link
Contributor Author

I merged now the nested and dynamic example because it seemed strange to see them separate

Copy link
Member

@superstar54 superstar54 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @agoscinski , thanks for the update!

The dynamic_graph_builder.py is not used, I think you want to remove it.

docs/source/howto/index.rst Outdated Show resolved Hide resolved
Comment on lines 8 to 10
# Nested workflows
# ================
# The `Graph Builder` allow user to create nested workflows from an input.
Copy link
Member

@superstar54 superstar54 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to give an overview: user can create nested WorkGraph in two ways:

  • Create a Task from the workgraph
  • Use graph builder.

Also discuss what's the upside and downside of the two approaches, or later in each section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have done something similar

Comment on lines 70 to 72
# For that use case we need to use the graph builder

# Create a graph builder function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line, otherwise the format will be wrong.

Comment on lines 21 to 22
# Suppose we want a WorkGraph which includes another WorkGraph`(x+y)*z` inside it.
# We can actually add a WorkGraph to another WorkGraph
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs to the Create a Task from the workgraph

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is at the beginning now

Comment on lines 57 to 67
# However linking the two WorkGraphs will not work

wg = WorkGraph("nested_workgraph")
add_multiply1 = wg.add_task(add_multiply(x=Int(2), y=Int(3), z=Int(4)))

try:
wg.add_task(
add_multiply(x=add_multiply1.outputs["multiply.result"], y=Int(3), z=Int(4))
)
except Exception as err:
print(err)
Copy link
Member

@superstar54 superstar54 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, one can link the two WorkGraph tasks. But we need to write the code in a different way:

def add_multiply(x=None, y=None, z=None):
    wg = WorkGraph()
    wg.add_task(add, name="add", x=x, y=y)
    wg.add_task(multiply, name="multiply", x=z)
    wg.add_link(wg.tasks["add"].outputs[0], wg.tasks["multiply"].inputs["y"])
    return wg

wg = WorkGraph("nested_workgraph")
add_multiply1 = wg.add_task(add_multiply(x=2, y=3, z=4), name="add_multiply1")
add_multiply2 = wg.add_task(add_multiply(y=5, z=6), name="add_multiply2")
wg.add_link(add_multiply1.outputs["multiply.result"], add_multiply2.inputs["add.x"])
wg.run()

The difference between the above code and a graph_builder task is that in graph_builder, the workgraph is dynamic and only created during execution, while the above workgraph is static, so that we can access the socket directly, e.g., add.x.
Both can used to create nested workgrpah, but graph_builder is a black box, which is the downside. The upside is that it allows dynamic workgraph generation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay used that example and explained a bit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. it is not properly working

# define add task
@task.calcfunction()
def add(x, y):
    return x + y


# define multiply task
@task.calcfunction()
def multiply(x, y):
    return x * y


def add_multiply(x=None, y=None, z=None):
    wg = WorkGraph()
    wg.add_task(add, name="add", x=x, y=y)
    wg.add_task(multiply, name="multiply", x=z)
    wg.add_link(wg.tasks["add"].outputs[0], wg.tasks["multiply"].inputs["y"])
    return wg


wg = WorkGraph("nested_workgraph")
# Creating a task from the WorkGraph
add_multiply1 = wg.add_task(add_multiply(x=Int(2), y=Int(3), z=Int(4)))
add_multiply2 = wg.add_task(add_multiply(x=Int(2), y=Int(3)))
# link the output of a task to the input of another task
wg.add_link(add_multiply1.outputs[0], add_multiply2.inputs["multiply.x"])
wg.to_html()

# %%
# Run the workgraph

wg.run()

Gives

Error: Error in task multiply: Cannot convert value of type <class 'aiida.orm.utils.managers.NodeLinksManager'> to AiiDA type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay now it works!

Comment on lines 149 to 150
# Create a Task from the workgraph (Experimental)
# -----------------------------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you mentioned this part at the beginning, so better to move this section before graph_builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is now integrated into the first part

docs/gallery/howto/autogen/graph_builder.py Outdated Show resolved Hide resolved
docs/gallery/howto/autogen/graph_builder.py Outdated Show resolved Hide resolved
agoscinski and others added 2 commits September 23, 2024 09:07
Co-authored-by: Xing Wang <xingwang1991@gmail.com>
@superstar54 superstar54 self-requested a review September 23, 2024 12:29
Copy link
Member

@superstar54 superstar54 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants