From 9764119337d1d8be2fed3b78e03ebab322411b1d Mon Sep 17 00:00:00 2001 From: Rick Love Date: Tue, 20 Feb 2024 08:01:32 -0600 Subject: [PATCH 1/3] FIX recursive_will_execute performance --- execution.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/execution.py b/execution.py index 00908eadd46b..4d062fe196cf 100644 --- a/execution.py +++ b/execution.py @@ -194,12 +194,16 @@ def recursive_execute(server, prompt, outputs, current_item, extra_data, execute return (True, None, None) -def recursive_will_execute(prompt, outputs, current_item): +def recursive_will_execute_len(prompt, outputs, current_item, memo): unique_id = current_item + + if unique_id in memo: + return memo[unique_id] + inputs = prompt[unique_id]['inputs'] - will_execute = [] + will_execute = 0 if unique_id in outputs: - return [] + return 0 for x in inputs: input_data = inputs[x] @@ -207,9 +211,10 @@ def recursive_will_execute(prompt, outputs, current_item): input_unique_id = input_data[0] output_index = input_data[1] if input_unique_id not in outputs: - will_execute += recursive_will_execute(prompt, outputs, input_unique_id) + will_execute += recursive_will_execute_len(prompt, outputs, input_unique_id, memo) - return will_execute + [unique_id] + memo[unique_id] = will_execute + 1 + return memo[unique_id] def recursive_output_delete_if_changed(prompt, old_prompt, outputs, current_item): unique_id = current_item @@ -377,7 +382,8 @@ def execute(self, prompt, prompt_id, extra_data={}, execute_outputs=[]): while len(to_execute) > 0: #always execute the output that depends on the least amount of unexecuted nodes first - to_execute = sorted(list(map(lambda a: (len(recursive_will_execute(prompt, self.outputs, a[-1])), a[-1]), to_execute))) + memo = {} + to_execute = sorted(list(map(lambda a: (recursive_will_execute_len(prompt, self.outputs, a[-1], memo), a[-1]), to_execute))) output_node_id = to_execute.pop(0)[-1] # This call shouldn't raise anything if there's an error deep in From 82a45d274077e6e0498464a8f48df4d198916372 Mon Sep 17 00:00:00 2001 From: Rick Love Date: Tue, 20 Feb 2024 11:20:13 -0600 Subject: [PATCH 2/3] Minimize code changes --- execution.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/execution.py b/execution.py index 4d062fe196cf..702621862b18 100644 --- a/execution.py +++ b/execution.py @@ -194,16 +194,16 @@ def recursive_execute(server, prompt, outputs, current_item, extra_data, execute return (True, None, None) -def recursive_will_execute_len(prompt, outputs, current_item, memo): +def recursive_will_execute(prompt, outputs, current_item, memo={}): unique_id = current_item if unique_id in memo: return memo[unique_id] inputs = prompt[unique_id]['inputs'] - will_execute = 0 + will_execute = [] if unique_id in outputs: - return 0 + return [] for x in inputs: input_data = inputs[x] @@ -211,9 +211,9 @@ def recursive_will_execute_len(prompt, outputs, current_item, memo): input_unique_id = input_data[0] output_index = input_data[1] if input_unique_id not in outputs: - will_execute += recursive_will_execute_len(prompt, outputs, input_unique_id, memo) + will_execute += recursive_will_execute(prompt, outputs, input_unique_id, memo) - memo[unique_id] = will_execute + 1 + memo[unique_id] = will_execute + [unique_id] return memo[unique_id] def recursive_output_delete_if_changed(prompt, old_prompt, outputs, current_item): @@ -382,8 +382,7 @@ def execute(self, prompt, prompt_id, extra_data={}, execute_outputs=[]): while len(to_execute) > 0: #always execute the output that depends on the least amount of unexecuted nodes first - memo = {} - to_execute = sorted(list(map(lambda a: (recursive_will_execute_len(prompt, self.outputs, a[-1], memo), a[-1]), to_execute))) + to_execute = sorted(list(map(lambda a: (len(recursive_will_execute(prompt, self.outputs, a[-1])), a[-1]), to_execute))) output_node_id = to_execute.pop(0)[-1] # This call shouldn't raise anything if there's an error deep in From 9d9b7c95db1e3540e9a09fd683d6ff3f68d8bb35 Mon Sep 17 00:00:00 2001 From: Rick Love Date: Tue, 20 Feb 2024 15:20:04 -0600 Subject: [PATCH 3/3] memo must be created outside lambda --- execution.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/execution.py b/execution.py index 702621862b18..3e9d53b0ed25 100644 --- a/execution.py +++ b/execution.py @@ -382,7 +382,8 @@ def execute(self, prompt, prompt_id, extra_data={}, execute_outputs=[]): while len(to_execute) > 0: #always execute the output that depends on the least amount of unexecuted nodes first - to_execute = sorted(list(map(lambda a: (len(recursive_will_execute(prompt, self.outputs, a[-1])), a[-1]), to_execute))) + memo = {} + to_execute = sorted(list(map(lambda a: (len(recursive_will_execute(prompt, self.outputs, a[-1], memo)), a[-1]), to_execute))) output_node_id = to_execute.pop(0)[-1] # This call shouldn't raise anything if there's an error deep in