Skip to content

Commit 0efffbb

Browse files
committed
Supporting *.safetensors format.
If a model file exists with extension `.safetensors` then we can load it more safely than with PyTorch weights.
1 parent 98947d1 commit 0efffbb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/sd_models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def checkpoint_tiles():
4545

4646
def list_models():
4747
checkpoints_list.clear()
48-
model_list = modelloader.load_models(model_path=model_path, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt"])
48+
model_list = modelloader.load_models(model_path=model_path, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"])
4949

5050
def modeltitle(path, shorthash):
5151
abspath = os.path.abspath(path)
@@ -180,7 +180,14 @@ def load_model_weights(model, checkpoint_info, vae_file="auto"):
180180
# load from file
181181
print(f"Loading weights [{sd_model_hash}] from {checkpoint_file}")
182182

183-
pl_sd = torch.load(checkpoint_file, map_location=shared.weight_load_location)
183+
if checkpoint_file.endswith(".safetensors"):
184+
try:
185+
from safetensors.torch import load_file
186+
except ImportError as e:
187+
raise ImportError(f"The model is in safetensors format and it is not installed, use `pip install safetensors`: {e}")
188+
pl_sd = load_file(checkpoint_file, device=shared.weight_load_location)
189+
else:
190+
pl_sd = torch.load(checkpoint_file, map_location=shared.weight_load_location)
184191
if "global_step" in pl_sd:
185192
print(f"Global Step: {pl_sd['global_step']}")
186193

0 commit comments

Comments
 (0)