rajkumarrawal commited on
Commit
c10d8f3
·
1 Parent(s): 826cc00

fix: improve model loading with meta tensor handling and CPU fallbacks

Browse files

- Use torch_dtype to avoid meta tensor issues
- Check for to_empty method for device placement
- Add fallback methods for robust CPU loading

Files changed (1) hide show
  1. app.py +38 -6
app.py CHANGED
@@ -7,17 +7,49 @@ from io import BytesIO
7
 
8
  fashion_items = ['top', 'trousers', 'jumper']
9
 
10
- # Load model and processor with CPU device to avoid meta tensor issues
11
  model_name = 'Marqo/marqo-fashionSigLIP'
12
 
13
  # Force CPU usage to avoid device mapping issues
14
  device = torch.device('cpu')
15
 
16
- # Simple loading approach - let model handle device placement
17
- model = AutoModel.from_pretrained(
18
- model_name,
19
- trust_remote_code=True
20
- ).to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
23
 
 
7
 
8
  fashion_items = ['top', 'trousers', 'jumper']
9
 
10
+ # Load model and processor with proper meta tensor handling
11
  model_name = 'Marqo/marqo-fashionSigLIP'
12
 
13
  # Force CPU usage to avoid device mapping issues
14
  device = torch.device('cpu')
15
 
16
+ # Handle meta tensor initialization properly
17
+ try:
18
+ # Load model with empty weights initialization to avoid meta tensor issues
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 minimal configuration
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 - try loading with low CPU memory usage
47
+ model = AutoModel.from_pretrained(
48
+ model_name,
49
+ trust_remote_code=True,
50
+ low_cpu_mem_usage=False # Disable to avoid accelerate issues
51
+ )
52
+ model = model.to(device)
53
 
54
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
55