Spaces:
Sleeping
Sleeping
Commit ·
40bdaf7
1
Parent(s): 3ca7ae0
fix: load peft base model with explicit cache
Browse filesLoad the LoRA base model and adapter separately so both Hugging Face downloads use the Space writable cache instead of falling back to /.cache.
Made-with: Cursor
environment/model_inference.py
CHANGED
|
@@ -65,8 +65,8 @@ def _load_model():
|
|
| 65 |
if _MODEL is not None and _TOKENIZER is not None:
|
| 66 |
return _MODEL, _TOKENIZER
|
| 67 |
|
| 68 |
-
from peft import
|
| 69 |
-
from transformers import AutoTokenizer, BitsAndBytesConfig
|
| 70 |
|
| 71 |
quantization_config = None
|
| 72 |
if torch.cuda.is_available():
|
|
@@ -77,15 +77,21 @@ def _load_model():
|
|
| 77 |
bnb_4bit_use_double_quant=True,
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
device_map="auto" if torch.cuda.is_available() else "cpu",
|
| 84 |
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 85 |
quantization_config=quantization_config,
|
| 86 |
low_cpu_mem_usage=True,
|
| 87 |
cache_dir=str(_CACHE_DIR),
|
| 88 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
_MODEL.eval()
|
| 90 |
return _MODEL, _TOKENIZER
|
| 91 |
|
|
|
|
| 65 |
if _MODEL is not None and _TOKENIZER is not None:
|
| 66 |
return _MODEL, _TOKENIZER
|
| 67 |
|
| 68 |
+
from peft import PeftConfig, PeftModel
|
| 69 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 70 |
|
| 71 |
quantization_config = None
|
| 72 |
if torch.cuda.is_available():
|
|
|
|
| 77 |
bnb_4bit_use_double_quant=True,
|
| 78 |
)
|
| 79 |
|
| 80 |
+
peft_config = PeftConfig.from_pretrained(MODEL_ID, cache_dir=str(_CACHE_DIR))
|
| 81 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 82 |
+
peft_config.base_model_name_or_path,
|
| 83 |
device_map="auto" if torch.cuda.is_available() else "cpu",
|
| 84 |
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 85 |
quantization_config=quantization_config,
|
| 86 |
low_cpu_mem_usage=True,
|
| 87 |
cache_dir=str(_CACHE_DIR),
|
| 88 |
)
|
| 89 |
+
_TOKENIZER = AutoTokenizer.from_pretrained(MODEL_ID, cache_dir=str(_CACHE_DIR))
|
| 90 |
+
_MODEL = PeftModel.from_pretrained(
|
| 91 |
+
base_model,
|
| 92 |
+
MODEL_ID,
|
| 93 |
+
cache_dir=str(_CACHE_DIR),
|
| 94 |
+
)
|
| 95 |
_MODEL.eval()
|
| 96 |
return _MODEL, _TOKENIZER
|
| 97 |
|