Spaces:
Sleeping
Sleeping
Commit
·
607eca2
1
Parent(s):
c10d8f3
fix(cpu): simplify device mapping to prevent meta tensor issues
Browse filesRefactored model initialization by using explicit device_map instead of complex meta tensor handling. Added HF_HOME environment configuration and removed to_empty() method calls, resulting in cleaner and more reliable CPU device placement.
app.py
CHANGED
|
@@ -13,41 +13,38 @@ model_name = 'Marqo/marqo-fashionSigLIP'
|
|
| 13 |
# Force CPU usage to avoid device mapping issues
|
| 14 |
device = torch.device('cpu')
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
# Load model with
|
| 19 |
model = AutoModel.from_pretrained(
|
| 20 |
model_name,
|
| 21 |
trust_remote_code=True,
|
| 22 |
-
torch_dtype=torch.float32
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# Check if model has the to_empty method and use it for meta tensor initialization
|
| 26 |
-
if hasattr(model, 'model') and hasattr(model.model, 'to_empty'):
|
| 27 |
-
model.model.to_empty(device=device)
|
| 28 |
-
elif hasattr(model, 'to_empty'):
|
| 29 |
-
model.to_empty(device=device)
|
| 30 |
-
else:
|
| 31 |
-
# Fallback to regular to() method
|
| 32 |
-
model = model.to(device)
|
| 33 |
-
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Primary loading method failed: {e}")
|
| 36 |
-
# Fallback method - load with
|
| 37 |
try:
|
| 38 |
model = AutoModel.from_pretrained(
|
| 39 |
model_name,
|
| 40 |
-
trust_remote_code=True
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
-
# Move to CPU after loading
|
| 43 |
-
model = model.to(device)
|
| 44 |
except Exception as e2:
|
| 45 |
print(f"Fallback method also failed: {e2}")
|
| 46 |
-
# Last resort -
|
| 47 |
model = AutoModel.from_pretrained(
|
| 48 |
model_name,
|
| 49 |
trust_remote_code=True,
|
| 50 |
-
|
| 51 |
)
|
| 52 |
model = model.to(device)
|
| 53 |
|
|
|
|
| 13 |
# Force CPU usage to avoid device mapping issues
|
| 14 |
device = torch.device('cpu')
|
| 15 |
|
| 16 |
+
# Set environment variables to prevent meta tensor issues
|
| 17 |
+
import os
|
| 18 |
+
os.environ['HF_HOME'] = '/tmp/hf_cache' # Use temporary cache directory
|
| 19 |
+
|
| 20 |
+
# Handle meta tensor initialization properly by controlling device mapping at the source
|
| 21 |
try:
|
| 22 |
+
# Load model with specific configuration to prevent meta tensor creation
|
| 23 |
model = AutoModel.from_pretrained(
|
| 24 |
model_name,
|
| 25 |
trust_remote_code=True,
|
| 26 |
+
torch_dtype=torch.float32,
|
| 27 |
+
device_map={"": "cpu"}, # Explicitly map all modules to CPU to avoid meta tensors
|
| 28 |
+
low_cpu_mem_usage=False # Disable low CPU mem usage to avoid accelerate issues
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Primary loading method failed: {e}")
|
| 33 |
+
# Fallback method - load with explicit CPU device mapping
|
| 34 |
try:
|
| 35 |
model = AutoModel.from_pretrained(
|
| 36 |
model_name,
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
torch_dtype=torch.float32,
|
| 39 |
+
device_map="cpu" # Force CPU mapping
|
| 40 |
)
|
|
|
|
|
|
|
| 41 |
except Exception as e2:
|
| 42 |
print(f"Fallback method also failed: {e2}")
|
| 43 |
+
# Last resort - load with basic configuration and manual device placement
|
| 44 |
model = AutoModel.from_pretrained(
|
| 45 |
model_name,
|
| 46 |
trust_remote_code=True,
|
| 47 |
+
torch_dtype=torch.float32
|
| 48 |
)
|
| 49 |
model = model.to(device)
|
| 50 |
|