AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
136.49k stars 26k forks source link

[Bug]:unable to load a safetensors model: Exception: device cpu is invalid #6890

Open DougStoker opened 1 year ago

DougStoker commented 1 year ago

Is there an existing issue for this?

What happened?

Unable to load a safetensors format model. I saw some stuff in recent commits about loading them with specific features, but i'm unable to get it to load on its own at all.

Steps to reproduce the problem

switch to a safetensors model in the dropdown menu

What should have happened?

file loads

Commit where the problem happens

4af3ca5393151d61363c30eef4965e694eeac15e

What platforms do you use to access UI ?

MacOS

What browsers do you use to access the UI ?

Google Chrome

Command Line Arguments

export COMMANDLINE_ARGS="--skip-torch-cuda-test --no-half --use-cpu interrogate --api"

Additional information, context and logs

Loading weights [943793fb] from /Users/doug/Documents/diffusion/stable-diffusion-webui/models/Stable-diffusion/modelv32.safetensors Traceback (most recent call last): File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/routes.py", line 284, in run_predict output = await app.blocks.process_api( File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/blocks.py", line 982, in process_api result = await self.call_function(fn_index, inputs, iterator) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/blocks.py", line 824, in call_function prediction = await anyio.to_thread.run_sync( File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync return await get_asynclib().run_sync_in_worker_thread( File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread return await future File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run result = context.run(func, args) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/ui.py", line 1646, in fn=lambda value, k=k: run_settings_single(value, key=k), File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/ui.py", line 1487, in run_settings_single if not opts.set(key, value): File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/shared.py", line 479, in set self.data_labels[key].onchange() File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/call_queue.py", line 15, in f res = func(args, **kwargs) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/webui.py", line 64, in shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: modules.sd_models.reload_model_weights())) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/sd_models.py", line 355, in reload_model_weights load_model_weights(sd_model, checkpoint_info) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/sd_models.py", line 197, in load_model_weights sd = read_state_dict(checkpoint_file) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/modules/sd_models.py", line 172, in read_state_dict pl_sd = safetensors.torch.load_file(checkpoint_file, device=map_location or shared.weight_load_location) File "/Users/doug/Documents/diffusion/stable-diffusion-webui/venv/lib/python3.10/site-packages/safetensors/torch.py", line 98, in load_file with safe_open(filename, framework="pt", device=device) as f: Exception: device cpu is invalid

DougStoker commented 1 year ago

found a bandaid fix changing pl_sd = safetensors.torch.load_file(checkpoint_file, device=device) to pl_sd = safetensors.torch.load_file(checkpoint_file, device='cpu')

in function read_state_dict of /modules/sd_models.py made it work