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

Reduce repeated calls of get_immediate_node_signature in cache #4871

Conversation

JettHu
Copy link
Contributor

@JettHu JettHu commented Sep 10, 2024

CacheKeySetInputSignature.get_immediate_node_signature is called multiple times for the same node as ancestors of different nodes.

Most nodes are fine, but when there are time-consuming operations in class_def.INPUT_TYPES(), such as get_filename_list, the performance of cache.set_prompt becomes quite bad on machines with poor disk performance.

On one of my devices, set_prompt of caches.outputs and caches.ui took 500+ms before modification, and it would be worse if there were more time-consuming nodes. This PR reduces the time to 80ms on average

@Trung0246
Copy link

@guill #4882

@@ -67,6 +67,7 @@ def __init__(self, dynprompt, node_ids, is_changed_cache):
super().__init__(dynprompt, node_ids, is_changed_cache)
self.dynprompt = dynprompt
self.is_changed_cache = is_changed_cache
self.immediate_node_signature = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't persist the signatures in the cache keyed by ID. The whole point of the cache is that it persists between executions and there's no guarantee that node signatures will be the same from one execution to the next.

Long term, I think the right solution is to cache entire node definitions somewhere. I have that change locally, but it's grouped with a bunch of others and I won't be able to split it out until this weekend.

Shorter term, if you just reset the .immediate_node_signature at the beginning of each execution, that should work.

Choose a reason for hiding this comment

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

My current hot fix right now is caching I/O calls within folder_paths.py (i.e os.path.*), which the I/O cache can reset whenever object_info api are called and it seems to work so far.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't persist the signatures in the cache keyed by ID. The whole point of the cache is that it persists between executions and there's no guarantee that node signatures will be the same from one execution to the next.

Long term, I think the right solution is to cache entire node definitions somewhere. I have that change locally, but it's grouped with a bunch of others and I won't be able to split it out until this weekend.

Shorter term, if you just reset the .immediate_node_signature at the beginning of each execution, that should work.

Currently, cache.set_prompt will reinitialize a cache_key_set each time see.

That is, the cache_key_set between each execution is two independent objects. So from the current implementation, .immediate_node_signature is indeed reset for each execution, because the cache_key_set is different.

This variable is added only to avoid repeated get_immediate_node_signature in get_node_signature of one workflow execution, and has nothing to do with other executions.

Of course, it would be better if this call could be further reduced, because the current implementation can only reduce one cache to be executed at a time, and there are also repeated get_immediate_node_signature between caches.outputs and caches.ui.

Copy link
Contributor Author

@JettHu JettHu Sep 12, 2024

Choose a reason for hiding this comment

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

My current hot fix right now is caching I/O calls within folder_paths.py (i.e os.path.*), which the I/O cache can reset whenever object_info api are called and it seems to work so far.

This is also a good approach. This PR takes into account more time-consuming INPUT_TYPES issues. I think I/O calls within folder_paths.py is just one of the exposed.

@mcmonkey4eva mcmonkey4eva added User Support A user needs help with something, probably not a bug. and removed User Support A user needs help with something, probably not a bug. labels Sep 12, 2024
Copy link
Contributor

@guill guill left a comment

Choose a reason for hiding this comment

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

You're totally right -- I forgot the CacheKeySet was separate from the cache itself. This seems like a good low-risk fix for this issue 👍

@comfyanonymous comfyanonymous added the Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now. label Sep 12, 2024
Copy link

(Automated Bot Message) CI Tests are running, you can view the results at https://ci.comfy.org/?branch=4871%2Fmerge

@comfyanonymous comfyanonymous added Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now. and removed Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now. labels Sep 13, 2024
Copy link

(Automated Bot Message) CI Tests are running, you can view the results at https://ci.comfy.org/?branch=4871%2Fmerge

@comfyanonymous comfyanonymous merged commit f6b7194 into comfyanonymous:master Sep 13, 2024
31 checks passed
comfyanonymous added a commit that referenced this pull request Sep 14, 2024
@comfyanonymous
Copy link
Owner

Had to revert this because: #4914

@JettHu
Copy link
Contributor Author

JettHu commented Sep 14, 2024

Had to revert this because: #4914

I found the problem and will submit a PR for another optimization method later.

doctorpangloss pushed a commit to hiddenswitch/ComfyUI that referenced this pull request Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants