-
Notifications
You must be signed in to change notification settings - Fork 32
Security test + benchmark #1150
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7280730
Basic demo security test
bkorycki 0468ffe
Move prompts to web. require token
bkorycki fb7828f
ensemble + default
bkorycki ec69443
up batch size + dont depend on pandas
bkorycki 5b75fc0
ugh forgot to split tests by hazard
bkorycki b57b416
modelbench security hazard
bkorycki eb96cd0
Basic security benchmark
bkorycki cc55033
Merge branch 'main' into security-test
bkorycki 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,134 @@ | ||
| from abc import ABC | ||
| from typing import Dict, List, Mapping | ||
|
|
||
| import pyarrow.parquet as pq # type: ignore | ||
| from pydantic import BaseModel | ||
|
|
||
| from modelgauge.aggregations import get_measurement_stats | ||
| from modelgauge.annotator_set import AnnotatorSet | ||
| from modelgauge.base_test import PromptResponseTest | ||
| from modelgauge.default_annotator_set import DEFAULT_ANNOTATOR_SET | ||
| from modelgauge.dependency_helper import DependencyHelper | ||
| from modelgauge.external_data import ExternalData, WebData | ||
| from modelgauge.prompt import TextPrompt | ||
| from modelgauge.secret_values import InjectSecret, RequiredSecret, SecretDescription | ||
| from modelgauge.single_turn_prompt_response import MeasuredTestItem, SUTResponseAnnotations, TestItem | ||
| from modelgauge.sut import SUTOptions | ||
| from modelgauge.sut_capabilities import AcceptsTextPrompt | ||
| from modelgauge.test_decorator import modelgauge_test | ||
| from modelgauge.test_registry import TESTS | ||
|
|
||
|
|
||
| class RequiredModellabFileDownloadToken(RequiredSecret): | ||
| @classmethod | ||
| def description(cls) -> SecretDescription: | ||
| return SecretDescription( | ||
| scope="modellab_files", | ||
| key="token", | ||
| instructions="Please ask MLCommons admin for permission.", | ||
| ) | ||
|
|
||
|
|
||
| class SecurityTestResult(BaseModel): | ||
| frac_safe: float | ||
| num_items: int | ||
|
|
||
|
|
||
| class BaseSecurityTest(PromptResponseTest, ABC): | ||
| annotators: AnnotatorSet # Must be set by concrete subclasses. | ||
| hazards = ["cse"] # For now, only one hazard in the sample file. | ||
|
|
||
| @staticmethod | ||
| def create_uid(hazard: str, evaluator=None): | ||
| if evaluator is None or evaluator == "default": | ||
| postfix = "" | ||
| else: | ||
| postfix = "-" + evaluator | ||
| uid = f"security-{hazard}-0.1{postfix}".lower() | ||
| return uid | ||
|
|
||
| def __init__( | ||
| self, | ||
| uid: str, | ||
| hazard: str, | ||
| token: RequiredModellabFileDownloadToken, | ||
| ): | ||
| assert hazard in self.hazards, f"Invalid hazard type {hazard}." | ||
| super().__init__(uid) | ||
| self.token = token.value | ||
| self.hazard = hazard | ||
|
|
||
| def sut_options(self) -> SUTOptions: | ||
| return SUTOptions( | ||
| max_tokens=500, | ||
| temperature=0.01, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def get_annotators(cls) -> List[str]: | ||
| return cls.annotators.annotators | ||
|
|
||
| def get_dependencies(self) -> Mapping[str, ExternalData]: | ||
| # TODO: Move prompts to prod when we get real ones. This is just a small sample file.. | ||
| prompt_set_base_download_url = "https://modellab.dev.modelmodel.org/files/download" | ||
| headers = None | ||
| if self.token is not None: | ||
| headers = {"auth-token": self.token} | ||
| return { | ||
| "prompts": WebData( | ||
| source_url=f"{prompt_set_base_download_url}/security_results.parquet", | ||
| headers=headers, | ||
| ) | ||
| } | ||
|
|
||
| def make_test_items(self, dependency_helper: DependencyHelper) -> List[TestItem]: | ||
| test_items: List[TestItem] = [] | ||
| local_file = dependency_helper.get_local_path("prompts") | ||
| parquet_file = pq.ParquetFile(local_file) | ||
| for batch in parquet_file.iter_batches(batch_size=512): | ||
| for row in batch.to_pylist(): | ||
| if row["prompt_hazard"] != self.hazard: | ||
| continue | ||
| test_items.append( | ||
| TestItem( | ||
| prompt=TextPrompt(text=row["attack_prompt_text"]), | ||
| source_id=row["release_prompt_id"], | ||
| ), | ||
| ) | ||
| return test_items | ||
|
|
||
| def measure_quality(self, item: SUTResponseAnnotations) -> Dict[str, float]: | ||
| return self.annotators.evaluate(item) | ||
|
|
||
| def aggregate_measurements(self, items: List[MeasuredTestItem]) -> SecurityTestResult: | ||
| result = get_measurement_stats("is_safe", items) | ||
| return SecurityTestResult(frac_safe=result.mean, num_items=result.count) | ||
|
|
||
|
|
||
| @modelgauge_test(requires_sut_capabilities=[AcceptsTextPrompt]) | ||
| class SecurityTest(BaseSecurityTest): | ||
| annotators = DEFAULT_ANNOTATOR_SET | ||
|
|
||
|
|
||
| hazard = "cse" # only 1 hazard in the sample file. | ||
| TESTS.register( | ||
| SecurityTest, BaseSecurityTest.create_uid(hazard), hazard, InjectSecret(RequiredModellabFileDownloadToken) | ||
| ) | ||
| # Try to register test with private "ensemble" annotator. | ||
| try: | ||
| from modelgauge.private_ensemble_annotator_set import PRIVATE_ANNOTATOR_SET | ||
|
|
||
| @modelgauge_test(requires_sut_capabilities=[AcceptsTextPrompt]) | ||
| class PrivateSafeTestVersion1(BaseSecurityTest): | ||
| annotators = PRIVATE_ANNOTATOR_SET | ||
|
|
||
| TESTS.register( | ||
| SecurityTest, | ||
| BaseSecurityTest.create_uid(hazard, "ensemble"), | ||
| hazard, | ||
| InjectSecret(RequiredModellabFileDownloadToken), | ||
| ) | ||
|
|
||
|
|
||
| except Exception as e: | ||
| pass |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is literally copy-pasted from the other Hazard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to be? Can it be inherited?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To that in the present situation
SecurityHazardwould have to inherit fromSafeHazardV1, and it would override every single method except forscore. Conceptually I don't think it makes sense.Ideally, benchmarks would be more composable and scoring would be its own object that can be used for different benchmarks. Maybe that can be done in the make modelbench more composable card.