Reubencf commited on
Commit
be5e6d2
·
verified ·
1 Parent(s): 6710fbf

Force adapter load onto CPU (PEFT infer_device picks cuda on ZeroGPU)

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -38,14 +38,17 @@ DEFAULT_INSTRUCTION = (
38
 
39
  processor = AutoProcessor.from_pretrained(BASE_MODEL_ID)
40
 
41
- # Build and merge entirely on CPU. With device_map="cuda" here, PEFT loads the adapter
42
- # safetensors straight onto CUDA, which doesn't exist yet on ZeroGPU at startup
43
- # ("No CUDA GPUs are available"). The single .to("cuda") below is the pattern `spaces`
44
- # intercepts and replays once a GPU is attached.
 
 
 
45
  model = Gemma3ForConditionalGeneration.from_pretrained(
46
  BASE_MODEL_ID, dtype=torch.bfloat16, device_map="cpu"
47
  )
48
- model = PeftModel.from_pretrained(model, ADAPTER_ID)
49
  model = model.merge_and_unload().eval().to("cuda")
50
 
51
  _eot = processor.tokenizer.convert_tokens_to_ids("<end_of_turn>")
 
38
 
39
  processor = AutoProcessor.from_pretrained(BASE_MODEL_ID)
40
 
41
+ # Build and merge entirely on CPU, then move once.
42
+ #
43
+ # torch_device="cpu" is load-bearing on ZeroGPU: PEFT picks the adapter's load device via
44
+ # infer_device(), which returns "cuda" because torch.cuda.is_available() reports True at
45
+ # startup even though no GPU is attached yet. safetensors then materialises straight onto
46
+ # CUDA and dies with "No CUDA GPUs are available". Module .to("cuda") is patched by
47
+ # `spaces` and replayed when a GPU attaches; a direct safetensors CUDA load is not.
48
  model = Gemma3ForConditionalGeneration.from_pretrained(
49
  BASE_MODEL_ID, dtype=torch.bfloat16, device_map="cpu"
50
  )
51
+ model = PeftModel.from_pretrained(model, ADAPTER_ID, torch_device="cpu")
52
  model = model.merge_and_unload().eval().to("cuda")
53
 
54
  _eot = processor.tokenizer.convert_tokens_to_ids("<end_of_turn>")