Skip to content

FEAT: Add a HarmBench classifier scorer - #4237

Merged
hannahwestra25 merged 8 commits into
microsoft:mainfrom
siddhant-rajhans:feat/harmbench-classifier-scorer
Nov 23, 2026
Merged

hannahwestra25 merged 8 commits into
microsoft:mainfrom
siddhant-rajhans:feat/harmbench-classifier-scorer

Conversation

@siddhant-rajhans

@siddhant-rajhans Siddhant Rajhans (siddhant-rajhans) commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

PyRIT loads HarmBench's behaviors but has no scorer for HarmBench's attack-success classifier (Mazeika et al., 2024, arXiv:2402.04249), the fine-tuned cais/HarmBench-Llama-2-13b-cls that answers whether a response is an instance of a behavior. This adds HarmBenchScorer, which reproduces HarmBench's classification step.

  • HarmBenchScorer sends HarmBench's classifier prompt, standard or contextual, to a target serving the classifier and returns True when it answers "Yes", so it can serve as an attack's objective scorer. The objective is the behavior; a contextual behavior is read the way harmbench_dataset.py builds it. HarmBench's copyright behaviors are scored by hashing and are out of scope.
  • The bundled prompts are HarmBench's (MIT), including the Llama 2 [INST] wrapper the classifier was trained on, so the classifier is served from a completions endpoint (OpenAICompletionTarget), as with WildGuardScorer.
  • Like HarmBench, each response is clipped to 512 tokens of the classifier's tokenizer (pinned revision, loaded on first use; needs the huggingface extra), which also keeps long responses inside the classifier's 2,048-token context. clip_generations=False sends full responses.
  • The parser reads "yes" or "no" case-insensitively, as HarmBench does, and raises InvalidJsonException otherwise, so the judge retry applies. Judgments replay like LlamaGuard's.

With the real classifier on the 395 human-labelled responses in scorer_evals/objective, the scorer gives the same label as HarmBench's own pipeline (evaluate_completions.py clipping, then vllm.LLM.generate at temperature 0) for 394 of 395 responses; the one difference is between vLLM's offline and server modes on an identical request. Against the human labels, ObjectiveScorerEvaluator gives accuracy 0.878, precision 0.918, recall 0.822 and F1 0.867.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

HarmBench measures attack success with a fine-tuned classifier
(cais/HarmBench-Llama-2-13b-cls) that answers whether a generation is an
instance of a behavior. HarmBenchScorer sends HarmBench's own classifier
prompts (MIT), standard and contextual, to a target serving the
classifier and returns True when it answers yes. The objective is the
behavior; a contextual behavior is read the way PyRIT's HarmBench dataset
builds it. Like HarmBench, it clips each response to 512 tokens of the
classifier's pinned tokenizer before classification.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…ctives up front

- The parser no longer writes a harmbench_raw_output metadata key: the rationale already quotes the
  classifier's answer and nothing read the key, so there is no namespaced key left without an owning model.
- Reject a whitespace-only objective in HarmBenchScorer._validate_expectation, after the shared check,
  instead of in the piece hook, so it raises MissingConditionError before any classifier call.
@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

Updated ahead of review, to match what #4229's review asked for (c751703, after merging main):

  • The parser no longer writes the harmbench_raw_output metadata key. The rationale already quotes the classifier's answer and nothing read the key, so no namespaced key is left without an owning model.
  • A whitespace-only objective is now rejected in a _validate_expectation override that runs after the shared check, raising MissingConditionError before any classifier call. Previously the piece hook checked it, and the error surfaced as a RuntimeError.

ty 0.0.105, now pinned on main, renamed unresolved-attribute to missing-attribute
and reports an annotated assignment from an Any value as unsound. Use the new rule
name and narrow the tokenizer's decode() result with cast, as main does elsewhere.

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about doc/code/scoring/0_scoring.ipynb: since this branch was cut, main (#4358) added a scorer reference table to that notebook. The table is generated from pyrit.score.__all__, so it will pick up HarmBenchScorer automatically, but the committed notebook output on main won't list it until the notebook is re-executed. Do you mind merging main and re-running this notebook so the committed table includes HarmBenchScorer?

@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

Done: merged main (9c26548; the conflicts were the StrongREJECT entries from #4229 in THIRD_PARTY_NOTICES.txt and the two shared scorer test lists, and both entries are kept) and re-ran the scorer reference table cell of 0_scoring.ipynb (48619ef). The table now lists HarmBenchScorer, and also StrongRejectScorer, which #4229 added without re-running the notebook; no other output changed. The HarmBench, StrongREJECT, scorer-info and shared scorer tests pass on the merge (180), as do ruff 0.17.5 and ty 0.0.105.

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.

@hannahwestra25
hannahwestra25 added this pull request to the merge queue Nov 23, 2026
Merged via the queue into microsoft:main with commit 3d6b9bb Nov 23, 2026
93 of 94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants