multimodalart HF Staff commited on
Commit
8dc6263
·
verified ·
1 Parent(s): bd5ae07

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. sansi/hub.py +18 -2
sansi/hub.py CHANGED
@@ -66,10 +66,26 @@ def load(repo_or_dir, device="cuda", revision=None, backbone=None):
66
  lm.config.use_cache = False
67
  hidden_size = lm.lm_head.in_features
68
  letter_ids, _ = option_token_ids(tok)
69
- lm = PeftModel.from_pretrained(lm, path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  model = OuroSanSi(lm, letter_ids, hidden_size, n_loop_params=cfg["n_loop_params"],
71
  readout_rank=cfg["readout_rank"])
72
- model.load_readout({"readout": load_file(os.path.join(path, "readout.safetensors"))})
73
  model.loops = cfg["loops"]
74
  return model.to(device).eval(), tok
75
 
 
66
  lm.config.use_cache = False
67
  hidden_size = lm.lm_head.in_features
68
  letter_ids, _ = option_token_ids(tok)
69
+ # On ZeroGPU there is no GPU at import time: peft's infer_device() would route the
70
+ # adapter safetensors onto CUDA and fail. Load the adapter on CPU; model.to(device)
71
+ # below moves everything at once.
72
+ import peft as _peft
73
+ _infer = _peft.peft_model.infer_device
74
+ _peft.peft_model.infer_device = lambda: "cpu"
75
+ try:
76
+ lm = PeftModel.from_pretrained(lm, path)
77
+ finally:
78
+ _peft.peft_model.infer_device = _infer
79
+ # Same for the readout: load_file must not touch CUDA before a GPU is attached.
80
+ from safetensors.torch import load_file as _load_file
81
+ from safetensors import safe_open
82
+ _readout = {}
83
+ with safe_open(os.path.join(path, "readout.safetensors"), framework="pt", device="cpu") as f:
84
+ for k in f.keys():
85
+ _readout[k] = f.get_tensor(k)
86
  model = OuroSanSi(lm, letter_ids, hidden_size, n_loop_params=cfg["n_loop_params"],
87
  readout_rank=cfg["readout_rank"])
88
+ model.load_readout({"readout": _readout})
89
  model.loops = cfg["loops"]
90
  return model.to(device).eval(), tok
91