Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a3427b
FEAT: Add default implementations of GCG extension protocols (#1902)
romanlutz Jun 22, 2026
abe86e5
Wire GCG extension protocol path
Copilot Jun 22, 2026
9588263
Fix GCG step logging for custom controls
Copilot Jun 25, 2026
e3b0149
Add coverage tests for GCGMultiPromptAttack initialization and error …
Copilot Jun 26, 2026
b75b536
Fix GCG control length test expectation
Copilot Jun 27, 2026
22e5ac5
Increase GCG diff coverage on protocol wiring
Copilot Jun 27, 2026
9eef06e
Fix mismatch-coverage test iteration mode
Copilot Jun 27, 2026
e2efb53
Merge remote-tracking branch 'origin/main' into push-clean-history
Copilot Jun 30, 2026
3caaca5
FEAT: Make GCG data SeedDataset-backed with inline goals/targets for AML
Copilot Jul 1, 2026
2e9c626
Merge remote-tracking branch 'origin/main' into romanlutz-gcg-b4-data…
Copilot Jul 8, 2026
3de355d
Merge remote-tracking branch 'origin/main' into romanlutz-gcg-b4-data…
Copilot Jul 16, 2026
b73e549
FEAT: Generate missing GCG target completions
romanlutz Oct 18, 2026
11321e4
MAINT: Reuse bundled AdvBench data for GCG
romanlutz Oct 20, 2026
e154489
Validate GCG dataset inputs and generated completions
romanlutz Oct 20, 2026
d11958f
Load configured Key Vault environment in GCG notebook
romanlutz Oct 26, 2026
6b74c9e
Clarify GCG checkpoint reporting and validate notebook results
romanlutz Oct 28, 2026
ca30717
Merge latest main into GCG dataset adapter
romanlutz Oct 28, 2026
abf57b4
Fix GCG dataset adapter review findings
romanlutz Nov 5, 2026
3c916e2
Reject non-text GCG seeds and refresh captured notebook results
romanlutz Nov 10, 2026
de95fd9
Persist GCG completion generation through prompt normalizer
romanlutz Nov 14, 2026
e14b6a0
Give GCG seed prefixes one typed metadata owner
romanlutz Nov 17, 2026
26fdf6e
Load default GCG completion prompt from YAML seed
romanlutz Nov 17, 2026
d5d0802
Fix GCG test compatibility with current main
romanlutz Nov 19, 2026
236ac2d
Merge main and reconcile GCG CI compatibility
romanlutz Nov 19, 2026
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
171 changes: 143 additions & 28 deletions doc/code/executor/gcg/1_gcg_azure_ml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@
"source": [
"import os\n",
"\n",
"from pyrit.setup.initialization import _load_environment_files\n",
"from pyrit.setup import ConfigurationLoader, initialize_pyrit_async\n",
"\n",
"_load_environment_files(env_files=None)\n",
"pyrit_config = ConfigurationLoader.load_with_overrides()\n",
"await initialize_pyrit_async(\n",
" memory_db_type=\"InMemory\",\n",
" load_defaults=False,\n",
" env_files=pyrit_config.resolve_env_files(),\n",
" env_akv_ref=pyrit_config.resolve_env_akv_ref(),\n",
")\n",
"\n",
"subscription_id = os.environ.get(\"AZURE_ML_SUBSCRIPTION_ID\")\n",
"resource_group = os.environ.get(\"AZURE_ML_RESOURCE_GROUP\")\n",
Expand Down Expand Up @@ -111,9 +117,9 @@
],
"source": [
"from azure.ai.ml import MLClient\n",
"from azure.identity import AzureCliCredential\n",
"from azure.identity import DefaultAzureCredential\n",
"\n",
"ml_client = MLClient(AzureCliCredential(), subscription_id, resource_group, workspace)"
"ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace)"
]
},
{
Expand Down Expand Up @@ -192,18 +198,116 @@
"Docker-installed package (Python's `-m` flag puts the cwd at the front of `sys.path`).\n",
"\n",
"The new public API takes a typed ``GCGConfig`` (strategy) and a separate\n",
"``GCGDataConfig`` (CSV paths/counts). We build both locally with whatever\n",
"overrides we want, serialize each into a JSON file the AML job can read as\n",
"an input, and ship those paths through the job command. Defaults come from\n",
"the dataclasses in ``pyrit.executor.promptgen.gcg.config``; goals and targets\n",
"flow into ``GCGGenerator.execute_async`` at runtime, not through the config.\n",
"``GCGDataConfig`` (resolved goals and targets). We load the bundled\n",
"``adv_bench`` dataset through the standard ``SeedDatasetProvider``; each seed\n",
"already preserves its canonical target completion as ``GCGSeedMetadata``.\n",
"We materialize those values inline via ``from_seed_dataset``, so the serialized\n",
"JSON the AML job reads already embeds the data and the compute container never\n",
"fetches it from the public internet. We build both configs locally, serialize\n",
"each into a JSON file the AML job mounts as an input, and ship those paths\n",
"through the job command. Defaults come from the dataclasses in\n",
"``pyrit.executor.promptgen.gcg.config``; goals and targets flow into\n",
"``GCGGenerator.execute_async`` at runtime, not through the config.\n",
"\n",
"AdvBench already provides a target completion for each objective. For another\n",
"ordinary ``SeedDataset`` without those values, use\n",
"``create_gcg_data_config_async`` with an adversarial target and a\n",
"``completion_scorer``. The scorer must return True only for a suitable\n",
"affirmative prefix, not a refusal, unrelated response, or complete answer.\n",
"Provided ``GCGSeedMetadata.target_prefix`` strings are trusted and preserved\n",
"without scoring; missing, None, or blank prefixes trigger generation. Malformed\n",
"payloads are rejected rather than converted to strings.\n",
"\n",
"Configure your own ``adversarial_target`` and ``scoring_target`` before running\n",
"this optional example; neither is instantiated in this notebook. This small\n",
"dataset has no ``GCGSeedMetadata`` payload, so its completion will be generated:\n",
"\n",
"```python\n",
"from pyrit.executor.promptgen.gcg import create_gcg_data_config_async\n",
"from pyrit.models import SeedDataset, SeedObjective\n",
"from pyrit.score import (\n",
" SelfAskTrueFalseScorer,\n",
" TrueFalseQuestion,\n",
" render_true_false_system_prompt,\n",
")\n",
"\n",
"targetless_objectives = SeedDataset(\n",
" seeds=[SeedObjective(value=\"Explain how rainbows form.\")],\n",
")\n",
"question = TrueFalseQuestion(\n",
" true_description=\"A short response prefix that directly begins complying with the objective.\",\n",
" false_description=\"A refusal, deflection, unrelated response, or complete answer.\",\n",
")\n",
"prefix_scorer = SelfAskTrueFalseScorer(\n",
" chat_target=scoring_target,\n",
" system_prompt=render_true_false_system_prompt(question=question),\n",
" question=question,\n",
")\n",
"data_config = await create_gcg_data_config_async(\n",
" dataset=targetless_objectives,\n",
" adversarial_target=adversarial_target,\n",
" completion_scorer=prefix_scorer,\n",
")\n",
"```\n",
"\n",
"Optional ``completion_prompt=SeedPrompt(...)`` replaces the default generation\n",
"prompt using Jinja variables ``objective`` and ``max_length``. Generation uses\n",
"the final assistant response, not intermediate tool outputs, and requires a\n",
"single error-free text piece. Empty, overlong, or scorer-rejected completions\n",
"raise an error without truncation or automatic retries. The default generated\n",
"completion limit is 256 characters; provided metadata is not length-limited.\n",
"All resolved strings are serialized locally, so neither the adversarial target\n",
"nor the scorer is needed by the AML job.\n",
"\n",
"**Migration from v1.1.0:** ``GCGDataConfig(train_data=..., test_data=...,\n",
"n_train_data=..., n_test_data=...)`` and the ``random_seed`` argument to\n",
"``load_goals_and_targets`` are no longer supported. Load public or private\n",
"datasets before submitting the job and supply each completion as\n",
"``GCGSeedMetadata(target_prefix=...).to_metadata()`` (or generate it as above). Select and split seeds\n",
"locally instead of supplying CSV URLs and row counts. For example, after\n",
"loading ``objectives`` through the standard provider:\n",
"\n",
"```python\n",
"from random import Random\n",
"from pyrit.models import SeedDataset\n",
"\n",
"seeds = list(objectives.seeds)\n",
"Random(42).shuffle(seeds)\n",
"data_config = GCGDataConfig.from_seed_dataset(\n",
" SeedDataset(seeds=seeds[:50]),\n",
" test_dataset=SeedDataset(seeds=seeds[50:60]),\n",
")\n",
"```\n",
"\n",
"This makes the submitted split explicit and reproducible. The compute-side\n",
"``load_goals_and_targets(data=data_config)`` only reads the inline lists.\n",
"\n",
"Seed metadata uses one fixed namespace, ``pyrit_gcg``, owned by\n",
"``GCGSeedMetadata``. The experimental ``gcg_target`` key and\n",
"``target_metadata_key`` argument are no longer supported. Normalize external\n",
"field names once when loading your data, before calling either adapter:\n",
"\n",
"```python\n",
"from pyrit.models import GCGSeedMetadata, SeedDataset, SeedObjective\n",
"\n",
"external_row = {\"goal\": \"Explain how rainbows form.\", \"completion\": \"Rainbows form when\"}\n",
"private_dataset = SeedDataset(\n",
" seeds=[\n",
" SeedObjective(\n",
" value=external_row[\"goal\"],\n",
" metadata=GCGSeedMetadata(target_prefix=external_row[\"completion\"]).to_metadata(),\n",
" )\n",
" ],\n",
")\n",
"data_config = GCGDataConfig.from_seed_dataset(private_dataset)\n",
"```\n",
"\n",
"We also have to specify a GPU compute target. In our experience, a GPU instance with\n",
"at least 24GB of vRAM is required (e.g., Standard_NC24ads_A100_v4).\n",
"\n",
"Depending on the compute instance you use, you may encounter \"out of memory\" errors.\n",
"In this case, we recommend training on a smaller model or lowering ``data.n_train_data``\n",
"or ``algorithm.batch_size``."
"In this case, we recommend training on a smaller model or lowering the number\n",
"of goals selected below or ``algorithm.batch_size``."
]
},
{
Expand All @@ -215,6 +319,7 @@
"source": [
"import tempfile\n",
"\n",
"from pyrit.datasets import SeedDatasetProvider\n",
"from pyrit.executor.promptgen.gcg import (\n",
" GCGAlgorithmConfig,\n",
" GCGConfig,\n",
Expand All @@ -228,11 +333,9 @@
" algorithm=GCGAlgorithmConfig(n_steps=5, batch_size=64, test_steps=1),\n",
" output=GCGOutputConfig(result_prefix=\"gcg_suffix\"),\n",
")\n",
"data_config = GCGDataConfig(\n",
" train_data=(\"https://raw.githubusercontent.com/llm-attacks/llm-attacks/main/data/advbench/harmful_behaviors.csv\"),\n",
" n_train_data=5,\n",
" n_test_data=0,\n",
")\n",
"objectives = (await SeedDatasetProvider.fetch_datasets_async(dataset_names=[\"adv_bench\"]))[0]\n",
"objectives.seeds = list(objectives.seeds[:5])\n",
"data_config = GCGDataConfig.from_seed_dataset(objectives)\n",
"\n",
"# Write the configs into a tempdir so AML can mount them as separate job inputs.\n",
"config_dir = Path(tempfile.mkdtemp(prefix=\"gcg-aml-config-\"))\n",
Expand Down Expand Up @@ -332,9 +435,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Job: great_vulture_lwn9y2fs10\n",
"Job: dynamic_nerve_96b78cyldr\n",
"Status: Starting\n",
"Studio URL: https://ml.azure.com/runs/great_vulture_lwn9y2fs10?wsid=/subscriptions/db1ba766-2ca3-42c6-a19a-0f0d43134a8c/resourcegroups/gcg-romanlutz/workspaces/gcg-romanlutz&tid=72f988bf-86f1-41af-91ab-2d7cd011db47\n"
"Studio URL: https://ml.azure.com/runs/dynamic_nerve_96b78cyldr?wsid=/subscriptions/db1ba766-2ca3-42c6-a19a-0f0d43134a8c/resourcegroups/gcg-romanlutz/workspaces/gcg-romanlutz&tid=72f988bf-86f1-41af-91ab-2d7cd011db47\n"
]
}
],
Expand All @@ -361,9 +464,13 @@
"runs never share a file). For our config, that resolves to\n",
"`gcg_suffix_<timestamp>_<id>.json` under\n",
"`<download_dir>/named-outputs/results/` once we download. The\n",
"`controls` array in that file contains one entry per training step, and\n",
"the last entry is the final adversarial suffix that, appended to the user\n",
"prompt, was optimized to elicit the target response."
"`controls` array contains logged checkpoints, not an iteration count.\n",
"With the default individual-goal strategy, each of the five goals logs an\n",
"initial checkpoint plus five optimization checkpoints (`test_steps=1`):\n",
"30 entries for 25 optimization iterations in total. Other strategies,\n",
"checkpoint intervals, or early stopping can change that relationship.\n",
"The last entry is the final logged suffix. A nonempty suffix and finite loss\n",
"confirm a usable result artifact, not that the suffix achieves the objective."
]
},
{
Expand Down Expand Up @@ -397,29 +504,31 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading artifact azureml://subscriptions/db1ba766-2ca3-42c6-a19a-0f0d43134a8c/resourcegroups/gcg-romanlutz/workspaces/gcg-romanlutz/datastores/workspaceblobstore/paths/azureml/great_vulture_lwn9y2fs10/results/ to ./AppData/Local/Temp/gcg-aml-7ccxlx7y/named-outputs/results\n"
"Downloading artifact azureml://subscriptions/db1ba766-2ca3-42c6-a19a-0f0d43134a8c/resourcegroups/gcg-romanlutz/workspaces/gcg-romanlutz/datastores/workspaceblobstore/paths/azureml/dynamic_nerve_96b78cyldr/results/ to ./AppData/Local/Temp/gcg-aml-57ywq56e/named-outputs/results\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading artifact azureml://datastores/workspaceartifactstore/ExperimentRun/dcid.great_vulture_lwn9y2fs10 to ./AppData/Local/Temp/gcg-aml-7ccxlx7y/artifacts\n"
"Downloading artifact azureml://datastores/workspaceartifactstore/ExperimentRun/dcid.dynamic_nerve_96b78cyldr to ./AppData/Local/Temp/gcg-aml-57ywq56e/artifacts\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result file: gcg_suffix_20260523-051515.json\n",
"Steps run: 30\n",
"Final loss: 1.189453125\n",
"Generated suffix: '! ! ! ! ! ! language ! programs ! ! Wisconsin ! ! ! ! Bill ! ! gender'\n"
"Result file: gcg_suffix_20261027-043337.json\n",
"Configured optimization steps per goal: 5\n",
"Logged checkpoints: 30\n",
"Final loss: 1.451171875\n",
"Generated suffix: '(/ \\\\| ! ! ! ! !ategory ! !Cond ! ! ! ! medieval ! ! ! !'\n"
]
}
],
"source": [
"import json\n",
"import math\n",
"import tempfile\n",
"import time\n",
"from pathlib import Path\n",
Expand Down Expand Up @@ -456,8 +565,14 @@
"final_suffix = log[\"controls\"][-1] if log[\"controls\"] else None\n",
"final_loss = log[\"losses\"][-1] if log[\"losses\"] else None\n",
"\n",
"if not isinstance(final_suffix, str) or not final_suffix.strip():\n",
" raise ValueError(\"GCG result does not contain a nonempty final suffix.\")\n",
"if isinstance(final_loss, bool) or not isinstance(final_loss, (int, float)) or not math.isfinite(final_loss):\n",
" raise ValueError(\"GCG result does not contain a finite final loss.\")\n",
"\n",
"print(f\"Result file: {result_file.name}\")\n",
"print(f\"Steps run: {len(log['controls'])}\")\n",
"print(f\"Configured optimization steps per goal: {config.algorithm.n_steps}\")\n",
Comment thread
romanlutz marked this conversation as resolved.
Resolved
"print(f\"Logged checkpoints: {len(log['controls'])}\")\n",
"print(f\"Final loss: {final_loss}\")\n",
"print(f\"Generated suffix: {final_suffix!r}\")"
]
Expand Down
Loading
Loading