Skip to content

Commit a47fa0c

Browse files
Added automatic device detection for model loading in vulnscan
Resolved crash when loading PyTorch model on systems without CUDA by implementing auto-detection of device availability (CPU/GPU). torch.load now uses map_location to ensure compatibility across environments. Signed-off-by: Shahm Najeeb <[email protected]>
1 parent 1683db0 commit a47fa0c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

CODE/vulnscan.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ def _load_model(self) -> None:
5555
elif self.model_path.endswith('.pth'):
5656
with warnings.catch_warnings():
5757
warnings.filterwarnings("ignore", category=FutureWarning)
58-
self.model = torch.load(self.model_path, weights_only=False)
58+
self.model = torch.load(
59+
self.model_path,
60+
map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu"),
61+
weights_only=False
62+
)
63+
if not torch.cuda.is_available() and torch.version.cuda and torch.backends.cudnn.is_available():
64+
log.warning(
65+
"NVIDIA GPU detected but CUDA is not available. Check your PyTorch and CUDA installation to utilise as much power as possible.")
66+
log.debug(f"Model using device: {torch.device('cuda' if torch.cuda.is_available() else 'cpu')}")
5967
else:
6068
raise ValueError("Unsupported model file format")
6169

0 commit comments

Comments
 (0)