Skip to content

Commit

Permalink
Simplify _find_components_that_will_receive_no_input
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Sep 10, 2024
1 parent cb07d59 commit 4d1406e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions haystack/core/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,21 @@ def _find_components_that_will_receive_no_input(
:param components_inputs: The current state of the inputs divided by Component name
:return: A set of Components that didn't receive any input from component_name
"""

# Simplifies the check if a Component is Variadic and received some input from other Components.
def is_variadic_with_existing_inputs(comp: Component) -> bool:
for receiver_socket in comp.__haystack_input__._sockets_dict.values(): # type: ignore
if component_name not in receiver_socket.senders:
continue
if (
receiver_socket.is_variadic
and len(components_inputs.get(receiver, {}).get(receiver_socket.name, [])) > 0
):
# This Component already received some input to its Variadic socket from other Components.
# It should be able to run even if it doesn't receive any input from component_name.
return True
return False

components = set()
instance: Component = self.graph.nodes[component_name]["instance"]
for socket_name, socket in instance.__haystack_output__._sockets_dict.items(): # type: ignore
Expand All @@ -1061,19 +1076,7 @@ def _find_components_that_will_receive_no_input(
for receiver in socket.receivers:
receiver_instance: Component = self.graph.nodes[receiver]["instance"]

is_variadic_with_existing_inputs = False
for receiver_socket in receiver_instance.__haystack_input__._sockets_dict.values(): # type: ignore
if component_name not in receiver_socket.senders:
continue
if (
receiver_socket.is_variadic
and len(components_inputs.get(receiver, {}).get(receiver_socket.name, [])) > 0
):
# This Component already received some input to its Variadic socket from other Components.
# It should be able to run even if it doesn't receive any input from component_name.
is_variadic_with_existing_inputs = True
break
if is_variadic_with_existing_inputs:
if is_variadic_with_existing_inputs(receiver_instance):
continue

components.add((receiver, receiver_instance))
Expand Down

0 comments on commit 4d1406e

Please sign in to comment.