adelelsayed1991 commited on
Commit
ecd77d9
·
verified ·
1 Parent(s): a7cc8ca

Fix: use PEFT torch_device=cpu (not device_map, which was silently ignored) to force CPU-side adapter load

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -49,16 +49,21 @@ SYSTEM_PROMPT = SYSTEM_PROMPT_TEMPLATE.format(
49
 
50
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
51
  base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype="auto")
52
- # device_map="cpu" is required here: PeftModel.from_pretrained's internal
53
- # safetensors load otherwise infers a CUDA target device from
54
- # torch.cuda.is_available() (which ZeroGPU's emulation layer reports as
55
- # True even at module-load time, before a real GPU is allocated), and that
56
- # specific direct-to-device safetensors load path is not covered by the
57
- # emulation, causing "RuntimeError: No CUDA GPUs are available". Loading
58
- # on CPU here and moving the whole composed model to CUDA afterward avoids
59
- # that code path entirely.
 
 
 
 
 
60
  model = PeftModel.from_pretrained(
61
- base_model, ADAPTER, subfolder=ADAPTER_SUBFOLDER, device_map="cpu"
62
  )
63
  model.to("cuda")
64
  model.eval()
 
49
 
50
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
51
  base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype="auto")
52
+ # torch_device="cpu" is required here: PeftModel.from_pretrained forwards
53
+ # unrecognized kwargs to load_adapter(), whose `torch_device` parameter
54
+ # controls what device load_peft_weights()/safetensors loads the adapter
55
+ # weights onto. Left unset, it calls infer_device(), which sees
56
+ # torch.cuda.is_available() == True (ZeroGPU's emulation layer reports
57
+ # this as True even at module-load time, before a real GPU is allocated)
58
+ # and picks "cuda" -- a direct-to-device safetensors load that the
59
+ # emulation layer does not cover, causing "RuntimeError: No CUDA GPUs are
60
+ # available". Forcing torch_device="cpu" here and moving the whole
61
+ # composed model to CUDA afterward avoids that code path entirely. Note
62
+ # this is NOT the same kwarg as transformers' `device_map` (which is
63
+ # unrelated to this PEFT-internal loading path and is silently ignored if
64
+ # passed here instead).
65
  model = PeftModel.from_pretrained(
66
+ base_model, ADAPTER, subfolder=ADAPTER_SUBFOLDER, torch_device="cpu"
67
  )
68
  model.to("cuda")
69
  model.eval()