Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright © 2023-2026 ValidMind Inc. All rights reserved.
# Refer to the LICENSE file in the root of this repository for details.
# SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial

"""
Unit tests for model type detection in validmind.vm_models.model
"""

import sys
import unittest

from sklearn.linear_model import LogisticRegression

from validmind.vm_models.model import is_pytorch_model


class BrokenTorchFinder:
"""Simulates a broken torch install where importing torch raises an
error other than ImportError (e.g. OSError WinError 1114 when c10.dll
fails to load on Windows)."""

def find_spec(self, fullname, path=None, target=None):
if fullname == "torch" or fullname.startswith("torch."):
raise OSError(
"[WinError 1114] A dynamic link library (DLL) initialization "
"routine failed. Error loading c10.dll"
)
return None


class TestIsPyTorchModel(unittest.TestCase):
def test_non_torch_model_returns_false(self):
self.assertFalse(is_pytorch_model(LogisticRegression()))

def test_broken_torch_install_returns_false(self):
saved_modules = {
name: module
for name, module in sys.modules.items()
if name == "torch" or name.startswith("torch.")
}
for name in saved_modules:
del sys.modules[name]

finder = BrokenTorchFinder()
sys.meta_path.insert(0, finder)

try:
self.assertFalse(is_pytorch_model(LogisticRegression()))
finally:
sys.meta_path.remove(finder)
sys.modules.update(saved_modules)


if __name__ == "__main__":
unittest.main()
6 changes: 4 additions & 2 deletions validmind/vm_models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ def is_pytorch_model(model):
Checks if the model is a PyTorch model. Need to extend this
method to check for all ways a PyTorch model can be created
"""
# if we can't import torch, then it's not a PyTorch model
# if we can't import torch, then it's not a PyTorch model. Broken torch
# installs can fail with errors other than ImportError (e.g. OSError
# WinError 1114 when a native DLL fails to load on Windows)
try:
import torch.nn as nn
except ImportError:
except Exception:
return False

# return False
Expand Down
Loading