1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// Command windowoverlap reports how much the RDD estimation windows of different
// alignment levels contain the same tokens.
//
// The window is a slice of vocabulary-id space, so each alignment puts different
// tokens in it. If the windows overlap little, the bias estimator is reading a
// largely different token population for each tokenizer, which bears on whether
// their estimates are comparable.
//
// Membership depends only on the counterfactual vocabulary and its merges, so
// this runs in seconds and never touches the corpus.
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
"go.jknobloc.com/x/research/knobloch"
"go.jknobloc.com/x/shelf"
"go.jknobloc.com/x/tokenizer/bpe"
)
var alphas = []int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
func name(alpha int, inverted bool) string {
if inverted {
return fmt.Sprintf("mi%03d", alpha)
}
return fmt.Sprintf("m%03d", alpha)
}
func set(v []string) map[string]struct{} {
s := make(map[string]struct{}, len(v))
for _, x := range v {
s[x] = struct{}{}
}
return s
}
// share of a that is also in b
func overlap(a []string, b map[string]struct{}) float64 {
if len(a) == 0 {
return 0
}
n := 0
for _, x := range a {
if _, ok := b[x]; ok {
n++
}
}
return 100 * float64(n) / float64(len(a))
}
type entry struct {
name string
alignment string
observed []string
oov []string
}
func main() {
ctrl := flag.String("ctrl", "results/knobloch/minipile_19_ctrl/%s_minipile", "counterfactual tokenizer directory, %s is the alignment name")
cutoff := flag.Int("cutoff", 50256, "RDD cutoff")
window := flag.Int("window", 5000, "half-width of the estimation window in token ids")
out := flag.String("out", "window_overlap.csv", "output CSV")
flag.Parse()
knobloch.LesciCutoff = *cutoff
knobloch.LesciWindow = *window
log.Printf("cutoff %d, window ids [%d, %d)", *cutoff, *cutoff-*window, *cutoff+*window)
var entries []entry
for _, inv := range []bool{false, true} {
for _, a := range alphas {
n := name(a, inv)
dir := shelf.Item(fmt.Sprintf(*ctrl, n))
tok, err := bpe.NewTokenizerFromFiles(
shelf.Abs(dir+"/vocab.json"), shelf.Abs(dir+"/merges.txt"),
bpe.Config{Recover: false})
if err != nil {
log.Fatal(err)
}
obs, oov := knobloch.WindowTokens(tok)
entries = append(entries, entry{n, fmt.Sprintf("%.1f", float64(a)/100), obs, oov})
}
}
// whole-window membership, ignoring which side of the cutoff a token landed
// on, plus the side-matched version; the gap between them is tokens that are
// in both windows but have crossed the cutoff
all := func(e entry) []string { return append(append([]string{}, e.observed...), e.oov...) }
interAll := set(all(entries[0]))
interObs := set(entries[0].observed)
interOOV := set(entries[0].oov)
for _, e := range entries[1:] {
cur := set(all(e))
for t := range interAll {
if _, ok := cur[t]; !ok {
delete(interAll, t)
}
}
cur = set(e.observed)
for t := range interObs {
if _, ok := cur[t]; !ok {
delete(interObs, t)
}
}
cur = set(e.oov)
for t := range interOOV {
if _, ok := cur[t]; !ok {
delete(interOOV, t)
}
}
}
baseAll := set(all(entries[0]))
baseObs := set(entries[0].observed)
baseOOV := set(entries[0].oov)
log.Printf("intersection across %d tokenizers: %d whole window, %d observed, %d oov",
len(entries), len(interAll), len(interObs), len(interOOV))
file, err := os.Create(*out)
if err != nil {
log.Fatal(err)
}
defer file.Close()
w := csv.NewWriter(file)
if err := w.Write([]string{
"tokenizer", "alignment", "cutoff", "window",
"n_window", "n_observed", "n_oov",
"window_shared_base", "window_shared_all",
"observed_shared_base", "oov_shared_base",
"observed_shared_all", "oov_shared_all",
}); err != nil {
log.Fatal(err)
}
for _, e := range entries {
if err := w.Write([]string{
e.name, e.alignment, strconv.Itoa(*cutoff), strconv.Itoa(*window),
strconv.Itoa(len(e.observed) + len(e.oov)),
strconv.Itoa(len(e.observed)), strconv.Itoa(len(e.oov)),
strconv.FormatFloat(overlap(all(e), baseAll), 'f', 2, 64),
strconv.FormatFloat(overlap(all(e), interAll), 'f', 2, 64),
strconv.FormatFloat(overlap(e.observed, baseObs), 'f', 2, 64),
strconv.FormatFloat(overlap(e.oov, baseOOV), 'f', 2, 64),
strconv.FormatFloat(overlap(e.observed, interObs), 'f', 2, 64),
strconv.FormatFloat(overlap(e.oov, interOOV), 'f', 2, 64),
}); err != nil {
log.Fatal(err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
log.Printf("wrote %s", *out)
}
|