Evaluate the attack performance of a given model on AdvBench. Args: model (object): The model object to be evaluated. tokenizer (object): The tokenizer object used for tokenization. num_sampled (int, optional): The number of samples to generate for each input. Defau
(
model,
tokenizer,
num_sampled=1,
add_sys_prompt=True,
prompt_template_style="base",
do_sample=True,
gcg=False,
include_inst=True,
save_attack_res=True,
filename="",
)
| 255 | |
| 256 | |
| 257 | def eval_attack( |
| 258 | model, |
| 259 | tokenizer, |
| 260 | num_sampled=1, |
| 261 | add_sys_prompt=True, |
| 262 | prompt_template_style="base", |
| 263 | do_sample=True, |
| 264 | gcg=False, |
| 265 | include_inst=True, |
| 266 | save_attack_res=True, |
| 267 | filename="", |
| 268 | ): |
| 269 | """ |
| 270 | Evaluate the attack performance of a given model on AdvBench. |
| 271 | |
| 272 | Args: |
| 273 | model (object): The model object to be evaluated. |
| 274 | tokenizer (object): The tokenizer object used for tokenization. |
| 275 | num_sampled (int, optional): The number of samples to generate for each input. Defaults to 5. |
| 276 | add_sys_prompt (bool, optional): Whether to add a system prompt to the input. Defaults to True. |
| 277 | do_sample (bool, optional): Whether to use sampling during generation. Defaults to True. |
| 278 | include_inst (bool, optional): Whether to include instructions in the prompt. Defaults to True. |
| 279 | save_attack_res (bool, optional): Whether to save the attack results. Defaults to True. |
| 280 | filename (str, optional): The filename to save the attack results. Required if save_attack_res is True. |
| 281 | |
| 282 | Returns: |
| 283 | float: The final attack score. |
| 284 | |
| 285 | Raises: |
| 286 | AssertionError: If save_attack_res is True but no filename is provided. |
| 287 | |
| 288 | """ |
| 289 | # Load data and prepare the prompt |
| 290 | # TODO: support other datasets |
| 291 | with open("./data/advbench.txt") as f: |
| 292 | lines = f.readlines()[:100] |
| 293 | lines = [l.strip("\n").strip() for l in lines] # remove \n and trailing spaces |
| 294 | if gcg: |
| 295 | assert add_sys_prompt == False |
| 296 | assert include_inst == True |
| 297 | assert do_sample == False |
| 298 | final_score_temp = [0, 0, 0] |
| 299 | for i in range(3): |
| 300 | dialogs = apply_prompt_template( |
| 301 | prompt_template_style="none", |
| 302 | dataset=lines, |
| 303 | include_inst=include_inst, |
| 304 | gcg_suffix_id=i, |
| 305 | ) |
| 306 | |
| 307 | # Generate outputs, check here for more options for the sampling params: https://github.com/vllm-project/vllm/blob/main/vllm/sampling_params.py |
| 308 | sampling_params = SamplingParams( |
| 309 | temperature=0, n=num_sampled, max_tokens=256 |
| 310 | ) # greedy decoding |
| 311 | start = time.time() |
| 312 | vllm_outputs = model.generate(dialogs, sampling_params) |
| 313 | end = time.time() |
| 314 | print("Attack finishes in {} seconds".format(end - start)) |
no test coverage detected