Skip to content

Commit

Permalink
Fix the bug when loading model from local path. (Closes #21)
Browse files Browse the repository at this point in the history
* Update snac.py

* Update snac.py

* Refactor

---------

Co-authored-by: Hubert Siuzdak <[email protected]>
  • Loading branch information
Pongking and hubertsiuzdak committed Sep 11, 2024
1 parent 7bfe7ec commit e6daa14
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions snac/snac.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import math
import os
from typing import List, Tuple

import numpy as np
Expand Down Expand Up @@ -100,10 +101,14 @@ def from_config(cls, config_path):
def from_pretrained(cls, repo_id, **kwargs):
from huggingface_hub import hf_hub_download

config_path = hf_hub_download(repo_id=repo_id, filename="config.json", **kwargs)
model_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin", **kwargs)
model = cls.from_config(config_path)
state_dict = torch.load(model_path, map_location="cpu")
if not os.path.isdir(repo_id):
config_path = hf_hub_download(repo_id=repo_id, filename="config.json", **kwargs)
model_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin", **kwargs)
model = cls.from_config(config_path)
state_dict = torch.load(model_path, map_location="cpu")
else:
model = cls.from_config(os.path.join(repo_id, "config.json"))
state_dict = torch.load(os.path.join(repo_id, "pytorch_model.bin"), map_location="cpu")
model.load_state_dict(state_dict)
model.eval()
return model

0 comments on commit e6daa14

Please sign in to comment.