minterp/tasks.py
134 lines · View on GitHub · Linked from 1. Read
1"""
2The behavior that every step explains.
3
4 One day, Lily and Tom were playing in the garden. Lily said hello to
5
6Two names are introduced, one of them acts, and the text stops where a
7receiver is due. A reader expects the other name, and so does the model: it
8continues with " Tom". The task is small enough to take apart and has a
9clean measure, the logit of the right name minus the logit of the wrong one
10at the last position.
11
12Each prompt comes in three versions that differ in one token, the name that
13acts (S):
14
15 clean S = A, the first name the answer is B
16 flipped S = B, the second name the answer is A
17 neutral S = C, a third name neither A nor B is due more
18
19Two more versions change a name and leave who acts alone. `other` is the
20clean prompt with the third name C in place of B, and `other_first` is the
21flipped prompt with C in place of A. In both, C is due. They show which way
22a name travels, where the first three show which way "who acts" travels.
23
24Each prompt begins with the token that opens every story in the training
25data, as the stories the model saw did.
26
27Later steps move activations from one version into another. That works
28position by position only because every name is one token, so the three
29versions have the same length and the same token at every other position.
30"""
31
32import itertools
33from dataclasses import dataclass
34
35import torch
36
37from .common import eot
38
39
40
41CANDIDATES = ["Lily", "Tom", "Tim", "Ben", "Mia", "Sam", "Lucy", "Max", "Anna", "Sue", "Jack", "Sara"]
42
43
44
45
46
47
48TEMPLATES = [
49 "Once upon a time, {A} and {B} went to the park. {S} gave a toy to",
50 "One day, {A} and {B} were playing in the garden. {S} said hello to",
51 "Once there were two friends, {A} and {B}. One day, {S} gave a big hug to",
52 "One sunny day, {A} and {B} had a red ball. {S} threw the ball to",
53 "One morning, {A} and {B} went to the zoo. {S} showed the map to",
54]
55
56
57@dataclass
58class Batch:
59 template: str
60 names: list
61 clean: torch.Tensor
62 flipped: torch.Tensor
63 neutral: torch.Tensor
64 other: torch.Tensor
65 other_first: torch.Tensor
66 right: torch.Tensor
67 wrong: torch.Tensor
68 third: torch.Tensor
69 pos: dict
70 labels: list
71
72 def __len__(self):
73 return len(self.names)
74
75
76def names(tok, candidates=CANDIDATES):
77 """The candidates that are one token after a space."""
78 return [n for n in candidates if len(tok.encode_ordinary(" " + n)) == 1]
79
80
81def build(tok, template, people, device="cpu"):
82 """All ordered pairs of names in one template, in its three versions."""
83 start = eot(tok)
84 ident = {n: tok.encode_ordinary(" " + n)[0] for n in people}
85
86 def encode(a, b, s):
87 return [start] + tok.encode_ordinary(template.format(A=a, B=b, S=s))
88
89 triples, clean, flipped, neutral, other, other_first = [], [], [], [], [], []
90 for k, (a, b) in enumerate(itertools.permutations(people, 2)):
91
92
93 others = [n for n in people if n not in (a, b)]
94 c = others[k % len(others)]
95 triples.append((a, b, c))
96 clean.append(encode(a, b, a))
97 flipped.append(encode(a, b, b))
98 neutral.append(encode(a, b, c))
99 other.append(encode(a, c, a))
100 other_first.append(encode(c, b, b))
101 assert len({len(p) for p in clean + flipped + neutral + other + other_first}) == 1, "a name is more than one token"
102
103 a, b, _ = triples[0]
104 first = clean[0]
105 at_a = [i for i, t in enumerate(first) if t == ident[a]]
106 at_b = [i for i, t in enumerate(first) if t == ident[b]]
107 assert len(at_a) == 2 and len(at_b) == 1, "the template must name A twice and B once"
108 pos = {"A": at_a[0], "B": at_b[0], "S": at_a[1], "end": len(first) - 1}
109 labels = [tok.decode([t]).strip() or "·" for t in first]
110 for key in ("A", "B", "S"):
111 labels[pos[key]] = key
112
113 ids = lambda rows: torch.tensor(rows, device=device)
114 return Batch(
115 template, triples, ids(clean), ids(flipped), ids(neutral), ids(other), ids(other_first),
116 right=ids([ident[b] for _, b, _ in triples]), wrong=ids([ident[a] for a, _, _ in triples]),
117 third=ids([ident[c] for _, _, c in triples]), pos=pos, labels=labels,
118 )
119
120
121def batches(tok, device="cpu", templates=TEMPLATES, people=None):
122 people = people or names(tok)
123 return [build(tok, t, people, device) for t in templates]
124
125
126def logit_diff(logits, right, wrong):
127 """
128 The logit of the right name minus the logit of the wrong one, per prompt.
129 logits is (prompts, seq, vocab), read at the last position, or already
130 (prompts, vocab). A difference of 2.3 means the model holds the right
131 name ten times as likely as the wrong one, whatever else it considers.
132 """
133 last = logits[:, -1] if logits.dim() == 3 else logits
134 return last.gather(1, right[:, None])[:, 0] - last.gather(1, wrong[:, None])[:, 0]