Lazy Evaluation
By default, all inputs are evaluated before a node can be run. Sometimes, however, an input won’t necessarily be used and evaluating it would result in unnecessary processing. Here are some examples of nodes where lazy evaluation may be beneficial:- A
ModelMergeSimplenode where the ratio is either0.0(in which case the first model doesn’t need to be loaded) or1.0(in which case the second model doesn’t need to be loaded). - Interpolation between two images where the ratio (or mask) is either entirely
0.0or entirely1.0. - A Switch node where one input determines which of the other inputs will be passed through.
Creating Lazy Inputs
There are two steps to making an input a “lazy” input. They are:- Mark the input as lazy in the schema, by passing
lazy=Trueto itsInputdefinition - Define a class method named
check_lazy_statusthat will be called prior to evaluation to determine if any more inputs are necessary.
0.0, we don’t need to evaluate any part of the tree leading up to the second
image. If the entire mask is 1.0, we can skip evaluating the first image.
Defining the schema
Declaring that an input is lazy is as simple as passinglazy=True to the input’s definition.
image1 and image2 are both marked as lazy inputs, but mask will always be evaluated.
Defining check_lazy_status
A check_lazy_status method is called if there are one or more lazy inputs that are not yet available. It
receives the same arguments as execute. All available inputs are passed in with their final values while
unavailable lazy inputs have a value of None.
When a lazy input was defined with
INPUT_IS_LIST = True, an unevaluated input is passed to
check_lazy_status as (None,) rather than None, so an is None check would miss it. Instead, check for
the (None,) sentinel to ensure required inputs are not omitted.check_lazy_status is to return a list of the names of any lazy inputs that are
needed to proceed. If all lazy inputs are available, the function should return an empty list.
Note that check_lazy_status may be called multiple times. (For example, you might find after evaluating
one lazy input that you need to evaluate another.)
Full Example
Execution Blocking
While Lazy Evaluation is the recommended way to “disable” part of a graph, there are times when you want to disable a node that doesn’t implement lazy evaluation itself. If it’s an output node that you developed yourself, you should just add lazy evaluation as follows:- Add a required (if this is a new node) or optional (if you care about backward compatibility) input for
enabledthat defaults toTrue - Make all other inputs lazy inputs
- Only evaluate the other inputs if
enabledisTrue
io.NodeOutput with a block_execution message. Comfy replaces every output of the node with an
ExecutionBlocker carrying that message. Any nodes which receive an ExecutionBlocker as input will skip
execution and return that ExecutionBlocker for any outputs, so the message is reported to the user when a
blocked output is actually used.