Skip to content
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

enable tokenizer customization in HFDetector #855

Merged
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
5 changes: 4 additions & 1 deletion garak/detectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def detect(self, attempt: garak.attempt.Attempt) -> Iterable[float]:
class HFDetector(Detector, HFCompatible):
"""Detector using a Hugging Face model"""

DEFAULT_PARAMS = Detector.DEFAULT_PARAMS | {"hf_args": {"device": "cpu"}}
DEFAULT_PARAMS = Detector.DEFAULT_PARAMS | {
"hf_args": {"device": "cpu"},
"tokenizer_kwargs": {"padding": True, "truncation": True},
}

def __init__(self, config_root=_config):
from transformers import (
Expand Down
17 changes: 3 additions & 14 deletions garak/detectors/misleading.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ class MustRefuteClaimModel(HFDetector):
bcp47 = "en"
accuracy = 0.9506 # 50:50 class balance in training data :/

def __init__(self, config_root=_config):
self.tokenizer_kwargs = {"padding": True, "truncation": True, "max_length": 512}
super().__init__(config_root=_config)

def detect(self, attempt: Attempt) -> List[float]:
results = super().detect(attempt)
return [1 - i for i in results] # we want refutations (label 1)
Expand Down Expand Up @@ -61,31 +57,24 @@ class MustContradictNLI(HFDetector):
DEFAULT_PARAMS = HFDetector.DEFAULT_PARAMS | {
"detector_model_path": "ynie/roberta-large-snli_mnli_fever_anli_R1_R2_R3-nli",
"detector_target_class": "label",
"tokenizer_kwargs": {"truncation": True, "max_length": 256},
}

bcp47 = "en"
active = False # this one is slow, skip by default

def __init__(self, config_root=_config):
self.tokenizer_kwargs = {"padding": True, "truncation": True, "max_length": 512}
leondz marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(
config_root=config_root,
)

def detect(self, attempt: Attempt) -> List[float]:
import torch

max_length = 256

premise = attempt.prompt
results = []
for hypothesis in attempt.all_outputs:
tokenized_input_seq_pair = self.detector_tokenizer.encode_plus(
premise,
hypothesis,
max_length=max_length,
max_length=self.tokenizer_kwargs["max_length"],
return_token_type_ids=True,
truncation=True,
truncation=self.tokenizer_kwargs["truncation"],
)
input_ids = (
torch.Tensor(tokenized_input_seq_pair["input_ids"])
Expand Down
4 changes: 0 additions & 4 deletions garak/detectors/toxicity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ class ToxicCommentModel(HFDetector):

bcp47 = "en"
tags = ["avid-effect:ethics:E0301"]

def __init__(self, config_root=_config):
super().__init__(config_root=config_root)
self.tokenizer_kwargs = {"padding": True, "truncation": True, "max_length": 512}
Loading