summaryrefslogtreecommitdiff
path: root/research/lesci/lesci.go
blob: 4c3ad85229fe6246730a50b5889463d66fce1a6b (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package lesci

import (
	"fmt"

	"go.jknobloc.com/x/tensor"
)

// ExtractData
//
// https://github.com/pietrolesci/tokenisation-bias/blob/376abc0ed6924986cbaf696ea10fdda71e550e45/notebooks/01_extract_data.ipynb
func ExtractData(rules tensor.Dense[int64], valid []bool, cutoff, window int64) []bool {
	shape := rules.Shape()

	if len(shape) != 2 || shape[0] != len(valid) || shape[1] != 3 {
		panic("shape mismatch")
	}

	// clamped := Window(rules, valid, cutoff, window)

	clamped := valid // collect everything for now

	filtered := Filter(rules, clamped, cutoff)
	oov := OutOfVocab(rules, filtered, cutoff)

	num := func(mask []bool) int {
		n := 0

		for _, v := range mask {
			if !v {
				continue
			}

			n++
		}

		return n
	}

	fmt.Println("clamped:", num(clamped))
	fmt.Println("filtered:", num(filtered))
	fmt.Println("oov:", num(oov))

	return oov
}

// Window
//
// # Filter merges based on the window size and vocab size
// merges_df = (
//
//	merges_df.filter((pl.col("tok") < vocab_size + window_size) & (pl.col("tok") >= vocab_size - window_size))
//	.sort("tok")
//	.drop("count")
//
// )
func Window(rules tensor.Dense[int64], mask []bool, cutoff, window int64) []bool {
	shape := rules.Shape()

	if len(shape) != 2 {
		// TODO
	}

	rows := shape[0]
	cols := shape[1]

	if cols != 3 {
		// TODO
	}

	windowed := make([]bool, rows)

	for i, m := range mask {
		if !m {
			continue
		}

		token := rules.At([]int{i, 2})

		if (token >= cutoff-window) && (token < cutoff+window) {
			windowed[i] = true
		}
	}

	return windowed
}

// Filter
//
// # Find tokens (in-vocab) that got merged into others, either as first or second part of the token
// to_drop = pl.concat(
//
//	[
//	    merges_df.filter(pl.col("tok") < vocab_size).join(
//	        merges_df.select(["tok", col]), left_on="tok", right_on=col, how="inner", suffix="_new"
//	    )
//	    for col in ["tok_a", "tok_b"]
//	]
//
// ).select(["tok", "tok_new"])
// print(f"{len(to_drop)} tokens dropped because are part of other tokens in the window (window size: {window_size} * 2)")
// merges_df = merges_df.filter(pl.col("tok").is_in(to_drop["tok"].implode()).not_())
func Filter(rules tensor.Dense[int64], mask []bool, cutoff int64) []bool {
	shape := rules.Shape()

	if len(shape) != 2 {
		// TODO
	}

	rows := shape[0]
	cols := shape[1]

	if len(mask) != rows {
		panic("") // TODO
	}

	if cols != 3 {
		panic("") // TODO
	}

	// drop all rules a + b -> c where c is a or b elsewhere in window

	intermediate := make(map[int64]struct{})

	for i, m := range mask {
		if !m {
			continue
		}

		var a, b int64

		if row, ok := rules.Select(0, i).Contiguous().Data(); !ok {
			panic("") // TODO
		} else {
			a, b = row[0], row[1]
		}

		if _, ok := intermediate[a]; !ok {
			intermediate[a] = struct{}{}
		}

		if _, ok := intermediate[b]; !ok {
			intermediate[b] = struct{}{}
		}
	}

	filtered := make([]bool, rows)

	for i, m := range mask {
		if !m {
			continue
		}

		c := rules.At([]int{i, 2})

		if _, ok := intermediate[c]; ok {
			if c < cutoff {
				continue // only drop in-vocab tokens
			}
		}

		filtered[i] = true
	}

	return filtered
}

// OutOfVocab
//
// # We only need this to get the tokens composing the OOV tokens
// merges_df = merges_df.filter(pl.col("tok") >= vocab_size)  # notice the '='
func OutOfVocab(rules tensor.Dense[int64], mask []bool, cutoff int64) []bool {
	shape := rules.Shape()

	if len(shape) != 2 {
		// TODO
	}

	rows := shape[0]
	cols := shape[1]

	if len(mask) != rows {
		panic("") // TODO
	}

	if cols != 3 {
		panic("") // TODO
	}

	oov := make([]bool, rows)

	for i, m := range mask {
		if !m {
			continue
		}

		c := rules.At([]int{i, 2})

		if c < cutoff {
			continue
		}

		oov[i] = true
	}

	return oov
}