rajkumarrawal commited on
Commit
053d849
·
1 Parent(s): 607eca2

feat: monkey-patch open_clip to avoid meta tensors and load model on CPU

Browse files

- Patch open_clip.factory.create_model to force CPU device and fp32, preventing meta tensor creation
- Simplify model loading: trust_remote_code + torch_dtype=float32 then .to(device); streamline fallback
- Set HF_HOME to /tmp/hf_cache and update exception messages

Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -17,36 +17,38 @@ device = torch.device('cpu')
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
 
51
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
52
 
 
17
  import os
18
  os.environ['HF_HOME'] = '/tmp/hf_cache' # Use temporary cache directory
19
 
20
+ # Monkey patch open_clip to prevent meta tensor issues
21
+ try:
22
+ import open_clip
23
+ original_create_model = open_clip.factory.create_model
24
+
25
+ def patched_create_model(*args, **kwargs):
26
+ # Force device to CPU to prevent meta tensor creation
27
+ kwargs['device'] = 'cpu'
28
+ kwargs['precision'] = 'fp32' # Force float32 precision
29
+ return original_create_model(*args, **kwargs)
30
+
31
+ open_clip.factory.create_model = patched_create_model
32
+ except Exception as e:
33
+ print(f"Could not patch open_clip: {e}")
34
+
35
+ # Load model with patched open_clip to prevent meta tensor issues
36
  try:
 
37
  model = AutoModel.from_pretrained(
38
  model_name,
39
  trust_remote_code=True,
40
+ torch_dtype=torch.float32
 
 
41
  )
42
+ model = model.to(device)
43
 
44
  except Exception as e:
45
+ print(f"Model loading failed: {e}")
46
+ # Fallback - try loading with different configuration
47
+ model = AutoModel.from_pretrained(
48
+ model_name,
49
+ trust_remote_code=True
50
+ )
51
+ model = model.to(device)
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
54