← Questions

Implement a universal adversarial attack on GPT-2 from a paper

0
Scale AIAI/ML designLast asked Jul 2025

An AI/ML design interview question asked in Scale AI's system design round, last asked Jul 2025. It tests adversarial attacks, llm safety and paper implementation.

A long take-home notebook. Read the paper on universal and transferable adversarial attacks on aligned language models (the greedy coordinate gradient method, GCG) and implement its core method against GPT-2 in a controlled, offline setting: optimize a short suffix of tokens that, appended to a prompt, makes the model produce a given benign target string (for example a fixed phrase). You also need to get the notebook's environment running.

Requirements

  • Functional: load GPT-2, define the loss as the negative log-likelihood of the target tokens given prompt plus suffix, compute gradients with respect to a one-hot encoding of the suffix tokens, use them to propose top-k token swaps per position, evaluate a batch of candidates, keep the best, and repeat.
  • Functional: extend to a universal suffix that works across several prompts, and report how often it works on prompts held out from optimization.
  • Non-functional: runs on the provided hardware in reasonable time; results are reproducible (fixed seeds); targets are benign phrases, and no harmful strings are produced or shared.

Clarifying questions

  • What hardware is available, and is GPT-2 small enough or is a larger model expected?
  • What counts as success: exact generation of the target, or a loss threshold?
  • How many prompts for the universal version, and how many held out?
  • Is transfer to another model part of the task?

What a strong answer covers

  • Correct setup: tokenization of prompt, suffix and target; the slice of logits that predicts the target tokens; gradients taken through the embedding matrix via one-hot inputs.
  • The GCG loop: candidate swaps from the most negative gradient entries, random sampling of candidates, batched forward passes to pick the best, and why the gradient is only a guide for discrete tokens.
  • Universal version: sum the loss over several prompts, add prompts gradually.
  • Evaluation: success rate on held-out prompts, comparison with a random-suffix baseline, loss curves, and a note on transfer.
  • Practical notebook work: pinned library versions, memory limits, batching; the limits of the results and the safety controls (benign targets, isolated environment).

Follow-ups

  • Only black-box API access is available, with no gradients. What changes?
  • How do you keep the suffix from overfitting to the training prompts?
  • What defences would detect or blunt this kind of suffix (perplexity filters, adversarial training)?

Where it's asked

What it tests

adversarial attacks · llm safety · paper implementation

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing