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

add unused node check for compile_dag_node #49382

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

LydiaXwQ
Copy link

Why are these changes needed?

This will optimize DAG execution by conserving memory through the release of unused resources. Additionally, it enhances error detection by providing immediate and clear feedback on unused inputs, enabling developers to debug and refine their DAGs more effectively.
We currently store all user-accessed keys from theinput.attribute_nodeinto the accessed_keys, then traverse backward from the DAG’s output node to find all user input (see below). An input is considered used if a path exists from that input node to output. Finally, we compare accessed_keys to used_keys, and report errors for any unused_keys. Changes we did in compiled_dag_node.py can be quickly searched by cmd +f: “handle_unused_keys”.

Update: This is the latest version of #49268

Code Snippet

def traverse(node):
            if node in visited_nodes:
                return
            visited_nodes.add(node)

            if isinstance(node, InputAttributeNode):
                used_keys.add(node.key)

            for upstream_node in node._upstream_nodes:
                traverse(upstream_node)
        traverse(output_node)

        unused_keys = accessed_keys - used_keys

        if unused_keys:
            unused_keys_str = ', '.join(str(key) for key in unused_keys)
            accessed_keys_str = ', '.join(str(key) for key in accessed_keys)
            unused_phrase = "is unused" if len(unused_keys) == 1 else "are unused"

            raise ValueError(
                f"DAG expects input: {accessed_keys_str}, "
                f"but {unused_keys_str} {unused_phrase}. "
                "Ensure all accessed inputs from InputNode are connected to the output."
            )

Related issue number

Closes #47165

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

LydiaXwQ and others added 8 commits December 20, 2024 13:26
Signed-off-by: Lydia <wenqix3@uw.edu>
Signed-off-by: Lydia <wenqix3@uw.edu>
Signed-off-by: Lydia <wenqix3@uw.edu>
Signed-off-by: Lydia <wenqix3@uw.edu>
@jcotant1 jcotant1 added the core Issues that should be addressed in Ray Core label Dec 20, 2024
@ruisearch42 ruisearch42 self-assigned this Dec 30, 2024
Comment on lines +964 to +966
used_attributes = set()
visited_nodes = set()
stack = [output_node]
Copy link
Contributor

Choose a reason for hiding this comment

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

type annotations

"Ensure all input attributes are used and contribute "
"to the computation of the Compiled Graph output.",
):
dag.experimental_compile()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this test to test_accelerated_dag.py? test_input_node.py is for general DAG, not necessarily compiled graph

def f(self, input):
return input

def combine(self, a, b):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add instead of combine

Comment on lines +1195 to +1211
unused_attributes = self.find_unused_input_attributes(
output_node, input_attributes
)

if unused_attributes:
unused_attributes_str = ", ".join(str(key) for key in unused_attributes)
input_attributes_str = ", ".join(str(key) for key in input_attributes)
unused_phrase = "is unused" if len(unused_attributes) == 1 else "are unused"

raise ValueError(
"Compiled Graph expects input to be accessed "
f"using all of attributes {input_attributes_str}, "
f"but {unused_attributes_str} {unused_phrase}. "
"Ensure all input attributes are used and contribute "
"to the computation of the Compiled Graph output."
)

Copy link
Contributor

Choose a reason for hiding this comment

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

The validation part can be moved into find_unused_input_attributes and rename it as check_unused_input_attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Issues that should be addressed in Ray Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[aDAG][Core] Throw error if not all InputNodes appear in the DAG
3 participants