72 Commits

Author SHA1 Message Date
guill
f12ab2e978 Execution Model Inversion (#2666)
* Execution Model Inversion

This PR inverts the execution model -- from recursively calling nodes to
using a topological sort of the nodes. This change allows for
modification of the node graph during execution. This allows for two
major advantages:

    1. The implementation of lazy evaluation in nodes. For example, if a
    "Mix Images" node has a mix factor of exactly 0.0, the second image
    input doesn't even need to be evaluated (and visa-versa if the mix
    factor is 1.0).

    2. Dynamic expansion of nodes. This allows for the creation of dynamic
    "node groups". Specifically, custom nodes can return subgraphs that
    replace the original node in the graph. This is an incredibly
    powerful concept. Using this functionality, it was easy to
    implement:
        a. Components (a.k.a. node groups)
        b. Flow control (i.e. while loops) via tail recursion
        c. All-in-one nodes that replicate the WebUI functionality
        d. and more
    All of those were able to be implemented entirely via custom nodes,
    so those features are *not* a part of this PR. (There are some
    front-end changes that should occur before that functionality is
    made widely available, particularly around variant sockets.)

The custom nodes associated with this PR can be found at:
https://github.com/BadCafeCode/execution-inversion-demo-comfyui

Note that some of them require that variant socket types ("*") be
enabled.

* Allow `input_info` to be of type `None`

* Handle errors (like OOM) more gracefully

* Add a command-line argument to enable variants

This allows the use of nodes that have sockets of type '*' without
applying a patch to the code.

* Fix an overly aggressive assertion.

This could happen when attempting to evaluate `IS_CHANGED` for a node
during the creation of the cache (in order to create the cache key).

* Fix Pyright warnings

* Add execution model unit tests

* Fix issue with unused literals

Behavior should now match the master branch with regard to undeclared
inputs. Undeclared inputs that are socket connections will be used while
undeclared inputs that are literals will be ignored.

* Make custom VALIDATE_INPUTS skip normal validation

Additionally, if `VALIDATE_INPUTS` takes an argument named `input_types`,
that variable will be a dictionary of the socket type of all incoming
connections. If that argument exists, normal socket type validation will
not occur. This removes the last hurdle for enabling variant types
entirely from custom nodes, so I've removed that command-line option.

I've added appropriate unit tests for these changes.

* Fix example in unit test

This wouldn't have caused any issues in the unit test, but it would have
bugged the UI if someone copy+pasted it into their own node pack.

* Use fstrings instead of '%' formatting syntax

* Use custom exception types.

* Display an error for dependency cycles

Previously, dependency cycles that were created during node expansion
would cause the application to quit (due to an uncaught exception). Now,
we'll throw a proper error to the UI. We also make an attempt to 'blame'
the most relevant node in the UI.

* Add docs on when ExecutionBlocker should be used

* Remove unused functionality

* Rename ExecutionResult.SLEEPING to PENDING

* Remove superfluous function parameter

* Pass None for uneval inputs instead of default

This applies to `VALIDATE_INPUTS`, `check_lazy_status`, and lazy values
in evaluation functions.

* Add a test for mixed node expansion

This test ensures that a node that returns a combination of expanded
subgraphs and literal values functions correctly.

* Raise exception for bad get_node calls.

* Minor refactor of IsChangedCache.get

* Refactor `map_node_over_list` function

* Fix ui output for duplicated nodes

* Add documentation on `check_lazy_status`

* Add file for execution model unit tests

* Clean up Javascript code as per review

* Improve documentation

Converted some comments to docstrings as per review

* Add a new unit test for mixed lazy results

This test validates that when an output list is fed to a lazy node, the
node will properly evaluate previous nodes that are needed by any inputs
to the lazy node.

No code in the execution model has been changed. The test already
passes.

* Allow kwargs in VALIDATE_INPUTS functions

When kwargs are used, validation is skipped for all inputs as if they
had been mentioned explicitly.

* List cached nodes in `execution_cached` message

This was previously just bugged in this PR.
2024-08-15 11:21:11 -04:00
comfyanonymous
74b2ae7ac3 Unload all models if there's an OOM error. 2024-08-06 03:30:28 -04:00
Chenlei Hu
3b09469dd5 Add timestamp to execution messages (#4076)
* Add timestamp to execution messages

* Add execution_end message

* Rename to execution_success
2024-07-21 15:29:10 -04:00
comfyanonymous
b237e7bf95 Fix bug with workflow not registering change.
There was an issue when only the class type of a node changed with all the
inputs staying the same.
2024-07-15 20:01:49 -04:00
comfyanonymous
8f214e2a90 Print error when node is missing. 2024-05-20 07:04:08 -04:00
Dr.Lt.Data
d95662622e Provide a better error message when attempting to execute the workflow with a missing node. (#3517) 2024-05-20 06:58:46 -04:00
comfyanonymous
d5efb0cf00 Sync some minor changes from the other repo. 2024-04-19 03:43:09 -04:00
comfyanonymous
cd0be2a31e Fix some performance issues with weight loading and unloading.
Lower peak memory usage when changing model.

Fix case where model weights would be unloaded and reloaded.
2024-03-28 18:04:42 -04:00
comfyanonymous
cd92ad35a9 Move cleanup_models to improve performance. 2024-03-23 17:27:10 -04:00
comfyanonymous
b513a7681e Pass extra_pnginfo as None when not in input data. 2024-03-07 15:07:47 -05:00
Rick Love
7967dc360e FIX recursive_will_execute performance (simple ~300x performance increase} (#2852)
* FIX recursive_will_execute performance

* Minimize code changes

* memo must be created outside lambda
2024-02-21 20:21:24 -05:00
comfyanonymous
edacbb2656 Cleanups. 2024-01-26 21:31:13 -05:00
realazthat
82ab3b7f0f Don't use PEP 604 type hints, to stay compatible with Python<3.10. 2024-01-17 17:16:34 -05:00
comfyanonymous
28712a5446 Rename status notes to status messages.
I think message describes them better.
2024-01-12 18:17:06 -05:00
comfyanonymous
5a89871e0c Clear status notes on execution start. 2024-01-12 17:21:22 -05:00
realazthat
e57852b5e9 Add error, status to /history endpoint 2024-01-11 10:16:42 -05:00
comfyanonymous
7f99d58877 Add a /free route to unload models or free all memory.
A POST request to /free with: {"unload_models":true}
will unload models from vram.

A POST request to /free with: {"free_memory":true}
will unload models and free all cached data from the last run workflow.
2024-01-04 17:15:22 -05:00
comfyanonymous
94346bfe12 Fix VALIDATE_INPUTS getting called multiple times.
Allow VALIDATE_INPUTS to only validate specific inputs.
2023-12-29 17:36:40 -05:00
comfyanonymous
0baa3cc687 --disable-smart-memory now unloads everything like it did originally. 2023-12-23 04:25:06 -05:00
comfyanonymous
02b4cb5a7a Do a garbage collect after the interval even if nothing is running. 2023-11-30 15:22:32 -05:00
comfyanonymous
48388a8d4d Only show last 200 elements in the UI history tab. 2023-11-20 16:56:29 -05:00
comfyanonymous
dfd72217d7 Cap maximum history size at 10000. Delete oldest entry when reached. 2023-11-20 16:38:39 -05:00
comfyanonymous
18e4504de7 Pull some small changes from the other repo. 2023-10-11 20:38:48 -04:00
pythongosssss
4f7bad4bad fix crash on node with VALIDATE_INPUTS and actual inputs 2023-09-07 18:42:21 +01:00
comfyanonymous
a216b56591 Smarter memory management.
Try to keep models on the vram when possible.

Better lowvram mode for controlnets.
2023-08-17 01:06:34 -04:00
Michael Poutre
9329b1a6b2 fix(execution): Fix support for input-less nodes 2023-08-01 12:29:01 -07:00
Michael Poutre
fc8a738779 chore: Fix typo 2023-08-01 12:27:50 -07:00
comfyanonymous
c8a5d3363c Fix issue with lora in some cases when combined with model merging. 2023-07-21 21:27:27 -04:00
comfyanonymous
be226475e0 Persist node instances between executions instead of deleting them.
If the same node id with the same class exists between two executions the
same instance will be used.

This means you can now cache things in nodes for more efficiency.
2023-06-29 23:38:56 -04:00
comfyanonymous
051f79bc8e Send websocket message only when prompt is actually done executing. 2023-06-13 13:38:43 -04:00
comfyanonymous
314abc241f Add a /history/{prompt_id} endpoint. 2023-06-12 14:34:30 -04:00
comfyanonymous
224d79d812 Fix issue with cancelling prompt. 2023-05-28 00:32:26 -04:00
space-nuko
3ba39931a0 Rename exception message field 2023-05-27 21:06:07 -05:00
space-nuko
96b9364255 Exception message 2023-05-27 21:06:07 -05:00
space-nuko
49ec8a2b17 Catch typecast errors 2023-05-27 21:06:07 -05:00
space-nuko
2d885e7019 Fix 2023-05-27 21:06:07 -05:00
space-nuko
8260e16cc7 Return null for value format 2023-05-27 21:06:07 -05:00
space-nuko
cbd9aac327 Show message in the frontend if prompt execution raises an exception 2023-05-27 21:06:07 -05:00
space-nuko
f510feec01 Send back more information about exceptions that happen during execution 2023-05-27 21:06:07 -05:00
space-nuko
c742150520 Add missing input name/config 2023-05-27 21:06:07 -05:00
space-nuko
8d5245aace Improve invalid prompt error message 2023-05-27 21:06:07 -05:00
space-nuko
82e3940028 Improve error output for failed nodes 2023-05-27 21:06:07 -05:00
comfyanonymous
6169965734 Parsing error crash. 2023-05-22 20:51:30 -04:00
comfyanonymous
5ee1ea3988 Add a node_errors to the /prompt error json response.
"node_errors" contains a dict keyed by node ids. The contents are a message
and a list of dependent outputs.
2023-05-22 13:22:38 -04:00
comfyanonymous
ea32c601ea Print min and max values in validation error message. 2023-05-21 00:24:28 -04:00
comfyanonymous
5ab1c07017 Fix outputs gone from history. 2023-05-15 00:27:28 -04:00
comfyanonymous
08bea5f9a7 Print prompt execution time. 2023-05-14 01:34:25 -04:00
comfyanonymous
773d00ab0a Add the prompt id to some websocket messages. 2023-05-13 11:17:16 -04:00
BlenderNeko
dd1be4e992 Make nodes map over input lists (#579)
* allow nodes to map over lists

* make work with IS_CHANGED and VALIDATE_INPUTS

* give list outputs distinct socket shape

* add rebatch node

* add batch index logic

* add repeat latent batch

* deal with noise mask edge cases in latentfrombatch
2023-05-13 11:15:45 -04:00
comfyanonymous
b698ff5ce4 Add the prompt_id to some websocket messages. 2023-05-11 01:22:40 -04:00