Skip to content

ApplyVisualStyle node mutates the model input in place #34

@ljleb

Description

@ljleb

The input model is mutated in place, because the forward functions are replaced by monkey patching. This makes the input model unusable in alternative paths in the workflow, for example to denoise 15 steps with visual style applied and 5 steps without.

I tried to load the model twice like this for it to work, it takes 2x the memory as a result:

image

But it doesn't even work: comfyui ignores the second model and reuses the first one. A really dirty workaround, which has the same memory requirements as I intended above, is to add this at the start of the node fn:

        model = model.clone()
        model.model = copy.deepcopy(model.model)

As a rule of thumb, nodes should never mutate their input in place. This will lead to some very awkward situations and strange bugs.

If you really need to patch the forward functions and can't do it in any other way, I suggest deepcopying the shell of the model object and subobjects while reusing the weights at the leafs. Off the top of my head, you can do this by creating a second model on the meta device, and then loading the weights in from the existing model. Maybe there's an easier way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions