Spaces:
Sleeping
Sleeping
Commit
·
70a8a19
1
Parent(s):
e5d40e3
fix: Update model initialization for CPU support
Browse files- src/configs/settings.py +11 -1
- src/models/llava_model.py +33 -14
src/configs/settings.py
CHANGED
|
@@ -3,7 +3,11 @@ Configuration settings for the LLaVA implementation.
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 6 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Project paths
|
| 9 |
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|
@@ -14,7 +18,13 @@ EXAMPLES_DIR = PROJECT_ROOT / "examples"
|
|
| 14 |
# Model settings
|
| 15 |
MODEL_NAME = "liuhaotian/llava-v1.5-7b"
|
| 16 |
MODEL_REVISION = "main"
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Generation settings
|
| 20 |
DEFAULT_MAX_NEW_TOKENS = 512
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
+
import torch
|
| 7 |
from pathlib import Path
|
| 8 |
+
from ..utils.logging import get_logger
|
| 9 |
+
|
| 10 |
+
logger = get_logger(__name__)
|
| 11 |
|
| 12 |
# Project paths
|
| 13 |
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|
|
|
| 18 |
# Model settings
|
| 19 |
MODEL_NAME = "liuhaotian/llava-v1.5-7b"
|
| 20 |
MODEL_REVISION = "main"
|
| 21 |
+
|
| 22 |
+
# Device detection
|
| 23 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
if DEVICE == "cuda":
|
| 25 |
+
logger.info(f"Using CUDA device: {torch.cuda.get_device_name(0)}")
|
| 26 |
+
else:
|
| 27 |
+
logger.info("CUDA not available, using CPU")
|
| 28 |
|
| 29 |
# Generation settings
|
| 30 |
DEFAULT_MAX_NEW_TOKENS = 512
|
src/models/llava_model.py
CHANGED
|
@@ -16,20 +16,39 @@ class LLaVAModel:
|
|
| 16 |
|
| 17 |
def __init__(self):
|
| 18 |
"""Initialize the LLaVA model and processor."""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def generate_response(
|
| 35 |
self,
|
|
|
|
| 16 |
|
| 17 |
def __init__(self):
|
| 18 |
"""Initialize the LLaVA model and processor."""
|
| 19 |
+
try:
|
| 20 |
+
logger.info(f"Initializing LLaVA model from {MODEL_NAME}")
|
| 21 |
+
logger.info(f"Using device: {DEVICE}")
|
| 22 |
+
|
| 23 |
+
# Initialize processor
|
| 24 |
+
self.processor = AutoProcessor.from_pretrained(
|
| 25 |
+
MODEL_NAME,
|
| 26 |
+
revision=MODEL_REVISION,
|
| 27 |
+
trust_remote_code=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Set model dtype based on device
|
| 31 |
+
model_dtype = torch.float32 if DEVICE == "cpu" else torch.float16
|
| 32 |
+
|
| 33 |
+
# Initialize model with appropriate settings
|
| 34 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
+
MODEL_NAME,
|
| 36 |
+
revision=MODEL_REVISION,
|
| 37 |
+
torch_dtype=model_dtype,
|
| 38 |
+
device_map="auto" if DEVICE == "cuda" else None,
|
| 39 |
+
trust_remote_code=True,
|
| 40 |
+
low_cpu_mem_usage=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Move model to device if not using device_map
|
| 44 |
+
if DEVICE == "cpu":
|
| 45 |
+
self.model = self.model.to(DEVICE)
|
| 46 |
+
|
| 47 |
+
logger.info("Model initialization complete")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Error initializing model: {str(e)}")
|
| 51 |
+
raise
|
| 52 |
|
| 53 |
def generate_response(
|
| 54 |
self,
|