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

When using .ckpt, "comfy/sd.py": KeyError: 'state_dict' #1

Closed
valconius opened this issue Jan 25, 2023 · 3 comments
Closed

When using .ckpt, "comfy/sd.py": KeyError: 'state_dict' #1

valconius opened this issue Jan 25, 2023 · 3 comments

Comments

@valconius
Copy link

valconius commented Jan 25, 2023

This will probably be a long-term issue others encounter and can serve as a convenient reference. It might not be a ComfyUI issue at all. tl;dr Running the error'ing model through any model merger or converter (such as WebUI's) fixes the issue, see below for details.

When using some models, but not others, seemingly only model merges created months ago in WebUI, the following error occurs:

making attention of type 'vanilla' with 512 in_channels
Working with z of shape (1, 4, 32, 32) = 4096 dimensions.
making attention of type 'vanilla' with 512 in_channels
Loading model from /home/user/ComfyUI/models/checkpoints/model1.ckpt
Traceback (most recent call last):
  File "/home/user/ComfyUI/main.py", line 160, in execute
    executed += recursive_execute(prompt, self.outputs, x, extra_data)
  File "/home/user/ComfyUI/main.py", line 54, in recursive_execute
    executed += recursive_execute(prompt, outputs, input_unique_id, extra_data)
  File "/home/user/ComfyUI/main.py", line 54, in recursive_execute
    executed += recursive_execute(prompt, outputs, input_unique_id, extra_data)
  File "/home/user/ComfyUI/main.py", line 54, in recursive_execute
    executed += recursive_execute(prompt, outputs, input_unique_id, extra_data)
  File "/home/user/ComfyUI/main.py", line 59, in recursive_execute
    outputs[unique_id] = getattr(obj, obj.FUNCTION)(**input_data_all)
  File "/home/user/ComfyUI/nodes.py", line 83, in load_checkpoint
    return comfy.sd.load_checkpoint(config_path, ckpt_path, output_vae=True, output_clip=True)
  File "/home/user/ComfyUI/comfy/sd.py", line 123, in load_checkpoint
    model = load_model_from_config(config, ckpt_path, verbose=False, load_state_dict_to=load_state_dict_to)
  File "/home/user/ComfyUI/comfy/sd.py", line 20, in load_model_from_config
    sd = pl_sd["state_dict"]
KeyError: 'state_dict'

A reminder, the code for pl_sd is...

def load_model_from_config ...
    ...
    if ckpt.lower().endswith(".safetensors"):
        import safetensors.torch
        sd = safetensors.torch.load_file(ckpt, device="cpu")
    else:
        pl_sd = torch.load(ckpt, map_location="cpu")
        if "global_step" in pl_sd:
            print(f"Global Step: {pl_sd['global_step']}")
        sd = pl_sd["state_dict"] # <-- PROBLEM OCCCURS HERE
    model = instantiate_from_config(config.model)
...
model = load_model_from_config(config, ckpt_path, verbose=False, load_state_dict_to=load_state_dict_to)

... and as the error says, ckpt and ckpt_path are certainly correct.

This error is resolved by entering WebUI and running either of the following, after which ComfyUI can use the model. FP16 and Pruning are not necessary, but just provided as examples (as they are now the default used in WebUI's model merge utility):

It seems the offending models were made with PyTorch 12.x, and both WebUI and ComfyUI are running on PyTorch 13.1. WebUI can read these (malformed?) ckpt files, but ComfyUI cannot.

EDIT: This seems to be a common problem with a simple compatibility fix:

@comfyanonymous
Copy link
Owner

acdc6f4

Should be fixed now.

@valconius
Copy link
Author

Well holy cow that was a fast fix. Indeed, my broken models work again. I wonder:

@comfyanonymous
Copy link
Owner

How could a provisional fix be put on git so that rather than doing "git pull" a branch could be pulled instead?

You can switch to another branch then: git pull https://github.com/comfyanonymous/ComfyUI other_branch
Replacing the repo url or branch name with the right one.

I don't think this model format is ok because it makes them incompatible with upstream SD:
https://github.com/Stability-AI/stablediffusion/blob/main/scripts/txt2img.py#L28

comfyanonymous pushed a commit that referenced this issue Apr 3, 2023
comfyanonymous pushed a commit that referenced this issue Sep 19, 2023
* Add inference tests

* Clean up

* Rename test graph file

* Add readme for tests

* Separate server fixture

* test file name change

* Assert images are generated

* Clean up comments

* Add __init__.py so tests can run with command line `pytest`

* Fix command line args for pytest

* Loop all samplers/schedulers in test_inference.py

* Ci quality workflows compare (#1)

* Add image comparison tests

* Comparison tests do not pass with empty metadata

* Ensure tests are run in correct order

* Save image files  with test name

* Update tests readme

* Reduce step counts in tests to ~halve runtime

* Ci quality workflows build (#2)

* Add build test github workflow
HorusElohim pushed a commit to HorusElohim/PyfyUI that referenced this issue Sep 22, 2023
* Add inference tests

* Clean up

* Rename test graph file

* Add readme for tests

* Separate server fixture

* test file name change

* Assert images are generated

* Clean up comments

* Add __init__.py so tests can run with command line `pytest`

* Fix command line args for pytest

* Loop all samplers/schedulers in test_inference.py

* Ci quality workflows compare (comfyanonymous#1)

* Add image comparison tests

* Comparison tests do not pass with empty metadata

* Ensure tests are run in correct order

* Save image files  with test name

* Update tests readme

* Reduce step counts in tests to ~halve runtime

* Ci quality workflows build (comfyanonymous#2)

* Add build test github workflow
Omer-ler referenced this issue in uglabs/ComfyUI Nov 14, 2023
1iyiwei pushed a commit to 1iyiwei/ComfyUI that referenced this issue Feb 9, 2024
…pping

Add command line option for devbox port mapping
mid-dev-media referenced this issue in mid-dev-media/ComfyUI Mar 16, 2024
mid-dev-media referenced this issue in mid-dev-media/ComfyUI Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants