Davy592 commited on
Commit
4617060
·
1 Parent(s): df25ba9

Added patch to run models on CPU

Browse files
nygaardcodecommentclassification/api/models.py CHANGED
@@ -22,6 +22,16 @@ import torch
22
 
23
  from nygaardcodecommentclassification import config
24
 
 
 
 
 
 
 
 
 
 
 
25
  # Configure module logger with explicit handler to ensure visibility
26
  logger = logging.getLogger("nygaard.models")
27
  logger.setLevel(logging.DEBUG)
 
22
 
23
  from nygaardcodecommentclassification import config
24
 
25
+ # Patch torch.load to use CPU mapping by default if CUDA is not available
26
+ # This prevents "Attempting to deserialize object on a CUDA device" errors
27
+ _original_torch_load = torch.load
28
+ def _patched_torch_load(f, map_location=None, *args, **kwargs):
29
+ """Wrapper around torch.load that uses CPU mapping if CUDA unavailable."""
30
+ if map_location is None and not torch.cuda.is_available():
31
+ map_location = torch.device('cpu')
32
+ return _original_torch_load(f, map_location=map_location, *args, **kwargs)
33
+ torch.load = _patched_torch_load
34
+
35
  # Configure module logger with explicit handler to ensure visibility
36
  logger = logging.getLogger("nygaard.models")
37
  logger.setLevel(logging.DEBUG)