-
Notifications
You must be signed in to change notification settings - Fork 491
fix: lazy load jailbreak detection dependencies #1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Pouyanpi
merged 8 commits into
NVIDIA:develop
from
jeffreyscarpenter:lazy-load-jailbreak-deps-new
Jun 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3d05ad7
lazy load jailbreak detection heuristics dependencies
jeffreyscarpenter bb4c099
fix a wrong title (#1225)
miyoungc 1307ca6
review feedback: add type annotation and move pickle import for Jailb…
jeffreyscarpenter 8e3220e
review feedback: add tests for lazy-loading jailbreak detection depen…
jeffreyscarpenter c74883d
style: apply pre-commit formatting fixes
jeffreyscarpenter 1561854
refactor: remove unused imports from models.py
Pouyanpi d1892f6
test: add coverage for model-based jailbreak checks
Pouyanpi 3984041
test(model-based): mock file ops to fix Windows test issues
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
LLMs | ||
=== | ||
==== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import sys | ||
import types | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
# Test 1: Lazy import behavior | ||
|
||
|
||
def test_lazy_import_does_not_require_heavy_deps(): | ||
""" | ||
Importing the checks module should not require torch, transformers, or sklearn unless model-based classifier is used. | ||
""" | ||
with mock.patch.dict( | ||
sys.modules, {"torch": None, "transformers": None, "sklearn": None} | ||
): | ||
import nemoguardrails.library.jailbreak_detection.model_based.checks as checks | ||
|
||
# Just importing and calling unrelated functions should not raise ImportError | ||
assert hasattr(checks, "initialize_model") | ||
|
||
|
||
# Test 2: Model-based classifier instantiation requires dependencies | ||
|
||
|
||
def test_model_based_classifier_imports(monkeypatch): | ||
""" | ||
Instantiating JailbreakClassifier should require sklearn and pickle, and use SnowflakeEmbed which requires torch/transformers. | ||
""" | ||
# Mock dependencies | ||
fake_rf = mock.MagicMock() | ||
fake_embed = mock.MagicMock(return_value=[0.0 8000 ]) | ||
fake_pickle = types.SimpleNamespace(load=mock.MagicMock(return_value=fake_rf)) | ||
fake_snowflake = mock.MagicMock(return_value=fake_embed) | ||
|
||
monkeypatch.setitem( | ||
sys.modules, | ||
"sklearn.ensemble", | ||
types.SimpleNamespace(RandomForestClassifier=mock.MagicMock()), | ||
) | ||
monkeypatch.setitem(sys.modules, "pickle", fake_pickle) | ||
monkeypatch.setitem(sys.modules, "torch", mock.MagicMock()) | ||
monkeypatch.setitem(sys.modules, "transformers", mock.MagicMock()) | ||
|
||
# Patch SnowflakeEmbed to avoid real model loading | ||
import nemoguardrails.library.jailbreak_detection.model_based.models as models | ||
|
||
monkeypatch.setattr(models, "SnowflakeEmbed", fake_snowflake) | ||
|
||
# mocking file operations to avoid Windows permission issues | ||
mock_open = mock.mock_open() | ||
with mock.patch("builtins.open", mock_open): | ||
# Should not raise | ||
classifier = models.JailbreakClassifier("fake_model_path.pkl") | ||
assert classifier is not None | ||
# Should be callable | ||
result = classifier("test") | ||
assert isinstance(result, tuple) | ||
|
||
|
||
# Test 3: Error if dependencies missing when instantiating model-based classifier | ||
|
||
|
||
def test_model_based_classifier_missing_deps(monkeypatch): | ||
""" | ||
If sklearn is missing, instantiating JailbreakClassifier should raise ImportError. | ||
""" | ||
monkeypatch.setitem(sys.modules, "sklearn.ensemble", None) | ||
|
||
import nemoguardrails.library.jailbreak_detection.model_based.models as models | ||
|
||
# to avoid Windows permission issues | ||
mock_open = mock.mock_open() | ||
with mock.patch("builtins.open", mock_open): | ||
with pytest.raises(ImportError): | ||
models.JailbreakClassifier("fake_model_path.pkl") | ||
|
||
|
||
# Test 4: Error when classifier_path is None | ||
|
||
|
||
def test_initialize_model_with_none_classifier_path(): | ||
""" | ||
initialize_model should raise EnvironmentError when classifier_path is None. | ||
""" | ||
import nemoguardrails.library.jailbreak_detection.model_based.checks as checks | ||
|
||
with pytest.raises(EnvironmentError) as exc_info: | ||
checks.initialize_model(classifier_path=None) | ||
|
||
assert "Please set the EMBEDDING_CLASSIFIER_PATH environment variable" in str( | ||
exc_info.value | ||
) | ||
|
||
|
||
# Test 5: SnowflakeEmbed initialization and call with torch imports | ||
|
||
|
||
def test_snowflake_embed_torch_imports(monkeypatch): | ||
""" | ||
Test that SnowflakeEmbed properly imports torch and transformers when needed. | ||
""" | ||
# Mock torch and transformers | ||
mock_torch = mock.MagicMock() | ||
mock_torch.cuda.is_available.return_value = False | ||
mock_transformers = mock.MagicMock() | ||
|
||
mock_tokenizer = mock.MagicMock() | ||
mock_model = mock.MagicMock() | ||
mock_transformers.AutoTokenizer.from_pretrained.return_value = mock_tokenizer | ||
mock_transformers.AutoModel.from_pretrained.return_value = mock_model | ||
|
||
monkeypatch.setitem(sys.modules, "torch", mock_torch) | ||
monkeypatch.setitem(sys.modules, "transformers", mock_transformers) | ||
|
||
import nemoguardrails.library.jailbreak_detection.model_based.models as models | ||
|
||
embed = models.SnowflakeEmbed() | ||
assert embed.device == "cpu" # as we mocked cuda.is_available() = False | ||
|
||
mock_tokens = mock.MagicMock() | ||
mock_tokens.to.return_value = mock_tokens | ||
mock_tokenizer.return_value = mock_tokens | ||
|
||
import numpy as np | ||
|
||
fake_embedding = np.array([1.0, 2.0, 3.0]) | ||
|
||
# the code does self.model(**tokens)[0][:, 0] | ||
# so we need to mock this properly | ||
mock_tensor_output = mock.MagicMock() | ||
mock_tensor_output.detach.return_value.cpu.return_value.squeeze.return_value.numpy.return_value = ( | ||
fake_embedding | ||
) | ||
|
||
A93C mock_first_index = mock.MagicMock() | ||
mock_first_index.__getitem__.return_value = mock_tensor_output # for [:, 0] | ||
|
||
mock_model_output = mock.MagicMock() | ||
mock_model_output.__getitem__.return_value = mock_first_index # for [0] | ||
|
||
mock_model.return_value = mock_model_output | ||
|
||
result = embed("test text") | ||
assert isinstance(result, np.ndarray) | ||
assert np.array_equal(result, fake_embedding) | ||
|
||
|
||
# Test 6: Check jailbreak function with classifier parameter | ||
|
||
|
||
def test_check_jailbreak_with_classifier(): | ||
""" | ||
Test check_jailbreak function when classifier is provided. | ||
""" | ||
import nemoguardrails.library.jailbreak_detection.model_based.checks as checks | ||
|
||
mock_classifier = mock.MagicMock() | ||
# jailbreak detected with score 0.9 | ||
mock_classifier.return_value = (True, 0.9) | ||
|
||
result = checks.check_jailbreak("test prompt", classifier=mock_classifier) | ||
|
||
assert result == {"jailbreak": True, "score": 0.9} | ||
mock_classifier.assert_called_once_with("test prompt") | ||
|
||
|
||
# Test 7: Check jailbreak function without classifier parameter (uses initialize_model) | ||
|
||
|
||
def test_check_jailbreak_without_classifier(monkeypatch): | ||
""" | ||
Test check_jailbreak function when no classifier is provided, it should call initialize_model. | ||
""" | ||
import nemoguardrails.library.jailbreak_detection.model_based.checks as checks | ||
|
||
# mock initialize_model to return a mock classifier | ||
mock_classifier = mock.MagicMock() | ||
# no jailbreak | ||
mock_classifier.return_value = (False, -0.5) | ||
mock_initialize_model = mock.MagicMock(return_value=mock_classifier) | ||
|
||
monkeypatch.setattr(checks, "initialize_model", mock_initialize_model) | ||
|
||
result = checks.check_jailbreak("safe prompt") | ||
|
||
assert result == {"jailbreak": False, "score": -0.5} | ||
mock_initialize_model.assert_called_once() | ||
mock_classifier.assert_called_once_with("safe prompt") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.