Spaces:
Running on Zero
Running on Zero
Force adapter load onto CPU (PEFT infer_device picks cuda on ZeroGPU)
Browse files
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
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
#
|
|
|
|
|
|
|
|
|
|
| 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>")
|