Spaces:
Sleeping
Sleeping
AutomationAuditsEnergies Claude Sonnet 4.5 commited on
Commit ·
9f8397c
1
Parent(s): 09b6051
Fix: Support CPU mode with 8-bit quantization
Browse files- Auto-detect GPU vs CPU environment
- GPU mode: float16 + device_map=auto (optimal)
- CPU mode: 8-bit quantization + explicit device_map (no offloading)
- Added bitsandbytes for 8-bit quantization support
- Fixes KeyError 'base_model.model.model.lm_head' on CPU free tier
⚠️ CPU inference will be slower (30-60s per response) but functional
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- main.py +23 -8
- requirements.txt +1 -0
main.py
CHANGED
|
@@ -65,14 +65,29 @@ def load_model():
|
|
| 65 |
print("📥 Téléchargement du modèle fine-tuné et adapters depuis le Hub...")
|
| 66 |
print("⏳ Cela peut prendre 30-60 secondes...")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
model.config.use_cache = True # Activer le cache pour la génération
|
| 77 |
print("✅ Modèle LoRA chargé avec succès depuis Hugging Face Hub!")
|
| 78 |
|
|
|
|
| 65 |
print("📥 Téléchargement du modèle fine-tuné et adapters depuis le Hub...")
|
| 66 |
print("⏳ Cela peut prendre 30-60 secondes...")
|
| 67 |
|
| 68 |
+
# Détecter GPU vs CPU
|
| 69 |
+
if torch.cuda.is_available():
|
| 70 |
+
print("🎮 Mode GPU: Chargement en float16 avec device_map=auto")
|
| 71 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 72 |
+
config.LORA_MODEL_PATH,
|
| 73 |
+
dtype=torch.float16,
|
| 74 |
+
device_map="auto",
|
| 75 |
+
trust_remote_code=True,
|
| 76 |
+
low_cpu_mem_usage=True,
|
| 77 |
+
cache_dir="/tmp/transformers_cache"
|
| 78 |
+
)
|
| 79 |
+
else:
|
| 80 |
+
print("💻 Mode CPU: Chargement en 8-bit pour réduire la mémoire")
|
| 81 |
+
print("⚠️ Inference sera plus lente (~30-60s par réponse)")
|
| 82 |
+
# Sur CPU: forcer device_map explicite + 8bit pour éviter offloading vers disk
|
| 83 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 84 |
+
config.LORA_MODEL_PATH,
|
| 85 |
+
load_in_8bit=True, # Quantization 8-bit pour CPU
|
| 86 |
+
device_map={"": "cpu"}, # Forcer CPU sans offloading
|
| 87 |
+
trust_remote_code=True,
|
| 88 |
+
cache_dir="/tmp/transformers_cache"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
model.config.use_cache = True # Activer le cache pour la génération
|
| 92 |
print("✅ Modèle LoRA chargé avec succès depuis Hugging Face Hub!")
|
| 93 |
|
requirements.txt
CHANGED
|
@@ -11,6 +11,7 @@ torch==2.5.0
|
|
| 11 |
transformers==5.0.0rc0 # Transformers v5 RC (requis pour PEFT 0.18.0)
|
| 12 |
peft==0.18.0 # Version utilisée pour l'entraînement
|
| 13 |
accelerate==1.2.0
|
|
|
|
| 14 |
|
| 15 |
# Utilities
|
| 16 |
python-dotenv==1.0.1
|
|
|
|
| 11 |
transformers==5.0.0rc0 # Transformers v5 RC (requis pour PEFT 0.18.0)
|
| 12 |
peft==0.18.0 # Version utilisée pour l'entraînement
|
| 13 |
accelerate==1.2.0
|
| 14 |
+
bitsandbytes==0.44.1 # Pour quantization 8-bit sur CPU (load_in_8bit)
|
| 15 |
|
| 16 |
# Utilities
|
| 17 |
python-dotenv==1.0.1
|