Skip to content

Commit f108782

Browse files
Merge pull request #4930 from Narsil/allow_to_load_safetensors_file
Supporting `*.safetensors` format.
2 parents a89d7f4 + 0efffbb commit f108782

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)
@@ -173,7 +173,14 @@ def load_model_weights(model, checkpoint_info, vae_file="auto"):
173173
# load from file
174174
print(f"Loading weights [{sd_model_hash}] from {checkpoint_file}")
175175

176-
pl_sd = torch.load(checkpoint_file, map_location=shared.weight_load_location)
176+
if checkpoint_file.endswith(".safetensors"):
177+
try:
178+
from safetensors.torch import load_file
179+
except ImportError as e:
180+
raise ImportError(f"The model is in safetensors format and it is not installed, use `pip install safetensors`: {e}")
181+
pl_sd = load_file(checkpoint_file, device=shared.weight_load_location)
182+
else:
183+
pl_sd = torch.load(checkpoint_file, map_location=shared.weight_load_location)
177184
if "global_step" in pl_sd:
178185
print(f"Global Step: {pl_sd['global_step']}")
179186

0 commit comments

Comments
 (0)