Vai al contenuto
← AI red teaming: techniques, open-source tools and automated pipelines

Lezione 2 di 18 · 9 min

How generative models work, and where they break

Follow a model from training to tokens and context, and see why safety behaviour layered on afterwards fails in two predictable ways.

Per ora questa pagina è in inglese.

Attacks make sense once you know what the model is doing. Earlier machine-learning systems were built for one job, such as classifying emails or recognising faces. Generative models, in the words of the series’ second episode, do not classify or score: they generate. They sample from learned distributions and produce new text, code, images or audio by recombining patterns from training data, fitted to the prompt and context. A useful picture is a very capable autocomplete [1].

Training happens in stages

Traditional models usually have a single training stage. Generative models go through several, and the order matters for security.

  1. Pre-training. The model reads very large collections of text and code and learns to predict what comes next, mostly without human labels. This takes months and large amounts of computing. It also bakes in whatever bias, misinformation or harmful content was in the data, so “the unsafe stuff goes in early”, as the episode puts it.
  2. Post-training. Supervised fine-tuning turns a general language model into one that answers questions and follows instructions.
  3. Safety post-training. Here the model is taught what to say and what not to say. Refusals and topic avoidance are mostly layered on at this stage, not built in from the start, which is why they can seem brittle: small changes in wording, tone or context can break them [1].
  4. Red teaming, just before release, as part of a break-fix loop: testers find failures, builders patch them, and testers try again.

This is only the model side. Application developers add their own testing, guardrails and mitigations, which later lessons examine.

Tokens, vectors and attention

Models do not read letters or words. A tokenizer breaks text into tokens: whole words, parts of words, punctuation, numbers or emoji. Each token becomes a vector, a list of numbers that places it near related concepts, so “cat” and “kitten” sit close together. A transformer then lets the model attend to many parts of the input at once, tracking relationships across long stretches of text.

Two consequences matter to a red teamer. The way text is chopped into tokens can be exploited to slip content past filters that match strings. And the same ability to model context across a whole sequence is what makes models vulnerable to attacks that tamper with that context. The next lessons take these in turn.

Animated diagram. A system prompt, the user input and retrieved data are shown as three coloured sources. Their tokens flatten into one grey stream in the context window, so the model cannot tell where each part came from. Orange tokens then show an attacker’s text arriving through the user input (direct injection) and through retrieved data (indirect injection), turning the model’s reply, tool calls and next steps red.
Everything the model reads is one stream of tokens, whatever its source.

Why safety training fails: two modes

Wei, Haghtalab and Steinhardt studied why jailbreaks succeed against models that have been safety-trained. They hypothesise two failure modes: “competing objectives and mismatched generalization” [2].

  • Competing objectives: the model is trained both to be capable and helpful and to be safe, and a prompt can set these goals against each other, so that following instructions wins over refusing.
  • Mismatched generalisation: safety training does not generalise to a domain in which the model nonetheless has capabilities, so the model understands an unusual input that its safety training never covered.

The authors argue that these modes point to a need for “safety-capability parity” and that scaling alone will not fix them [2]. This is the theory behind much of what follows. Many single-turn techniques work by setting capabilities against safety (personas, framing) or by moving to a form the safety training saw little of (encodings, unusual languages).

Outputs vary, so tests repeat

Generation is sampled, so the same input can give different outputs. A test that fails once may pass the next time, and the reverse. The series’ discussion of automation returns to this: repeatable, code-driven workflows are valuable precisely because the models are not always deterministic [3]. Lesson 14 shows how to report results honestly under that variation.

Da ricordare

  • Generative models sample from learned distributions; they generate, they do not classify.
  • Safety behaviour such as refusal is layered on in post-training, so it can be brittle.
  • Tokenisation and context handling create attack surface: filters on strings can be bypassed and context can be tampered with.
  • Jailbreaks work through competing objectives and mismatched generalisation (Wei et al., 2023).
  • Outputs vary between runs, so one trial proves little.

Mettiti alla prova

  1. 1. In which stage are refusals and topic avoidance mostly layered on?

    • Pre-training
    • Safety post-training — Esatto.
    • Tokenisation
    • Embedding

    Safety behaviour is taught after the general capabilities exist, which can make it brittle.

  2. 2. What does “mismatched generalisation” mean?

    • The model refuses everything
    • Safety training does not extend to a domain where the model has capabilities — Esatto.
    • Two models disagree
    • The tokenizer fails

    Wei et al. describe it as safety training failing to generalise to a domain for which capabilities exist.

  3. 3. Why can token handling matter to an attacker?

    • Tokens are encrypted
    • Filters that match strings can be bypassed by changing how text is split or encoded — Esatto.
    • Tokens cannot be read by models
    • It speeds up the model

    A filter that matches exact strings may miss text the model still understands.

  4. 4. Why does a single failed test prove little?

    • Models are sampled, so outputs vary between runs — Esatto.
    • Tests are always wrong
    • Models never change
    • Failures are logged only once

    Because outputs vary, results need repeated trials and honest reporting.

Mettilo in pratica con FireAI

Metti in pratica questa lezione sul tuo Mac.

Fonti

Mettilo in pratica sul tuo Mac

Prova tutte le funzioni gratis per 17 giorni, senza carta.

Scarica per Mac Guide