summaryrefslogtreecommitdiff
path: root/llm/tokenbuffer_test.go
blob: 789caa6f5cde32de1a5abd8d1b1ef00c14529095 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package llm

import (
	"fmt"
	"slices"
	"testing"
)

type byteTokenizer struct{}

func (bt byteTokenizer) Tokenize(text string) []int {
	r := make([]int, len(text))

	for i := 0; i < len(text); i++ {
		r[i] = int(text[i])
	}

	return r
}

func TestTokenBuffer_Push(t *testing.T) {
	type gold struct {
		window   int
		stride   int
		text     string
		expected [][]int64
	}

	tests := []gold{
		{
			window: 5, stride: 5,
			text:     "abcdefghij",
			expected: [][]int64{{97, 98, 99, 100, 101}, {102, 103, 104, 105, 106}},
		},
		{
			window: 4, stride: 2,
			text:     "abcdef",
			expected: [][]int64{{97, 98, 99, 100}, {99, 100, 101, 102}},
		},
		{
			window: 10, stride: 5,
			text:     "abcde",
			expected: nil,
		},
	}

	for _, tt := range tests {
		t.Run(
			fmt.Sprintf("window%d_stride%d", tt.window, tt.stride),

			func(t *testing.T) {
				tb := NewTokenBuffer(byteTokenizer{}, tt.window, tt.stride)

				tb.SetIncludeTail(false)

				var got [][]int64

				for w := range tb.Push(0, tt.text) {
					got = append(got, w.Tokens)
				}

				if len(got) != len(tt.expected) {
					t.Fatalf("expected %d windows but got %d", len(tt.expected), len(got))
				}

				for i := range got {
					if !slices.Equal(got[i], tt.expected[i]) {
						t.Errorf("window %d: expected %v but got %v", i, tt.expected[i], got[i])
					}
				}
			},
		)
	}
}

func TestTokenBuffer_PushAccumulates(t *testing.T) {
	tb := NewTokenBuffer(byteTokenizer{}, 4, 4)

	var got [][]int64

	for w := range tb.Push(0, "ab") {
		got = append(got, w.Tokens)
	}

	if len(got) != 0 {
		t.Fatalf("expected 0 windows but got %d", len(got))
	}

	for w := range tb.Push(0, "cd") {
		got = append(got, w.Tokens)
	}

	expected := [][]int64{{97, 98, 99, 100}}

	if len(got) != len(expected) {
		t.Fatalf("expected %d windows but got %d", len(expected), len(got))
	}

	for i := range got {
		if !slices.Equal(got[i], expected[i]) {
			t.Errorf("window %d: expected %v but got %v", i, expected[i], got[i])
		}
	}
}

func TestTokenBuffer_Tail(t *testing.T) {
	tb := NewTokenBuffer(byteTokenizer{}, 5, 5)

	tb.SetIncludeTail(true)

	var got [][]int64

	for w := range tb.Push(0, "abcdef") {
		got = append(got, w.Tokens)
	}

	if w, ok := tb.Tail(); ok {
		got = append(got, w.Tokens)
	}

	expected := [][]int64{{97, 98, 99, 100, 101}, {102}}

	if len(got) != len(expected) {
		t.Fatalf("expected %d windows but got %d", len(expected), len(got))
	}

	for i := range got {
		if !slices.Equal(got[i], expected[i]) {
			t.Errorf("window %d: expected %v but got %v", i, expected[i], got[i])
		}
	}
}

func TestTokenBuffer_Position(t *testing.T) {
	tb := NewTokenBuffer(byteTokenizer{}, 4, 2)

	tb.SetIncludeTail(false)

	var positions []int

	for range tb.Push(0, "abcdefgh") {
		positions = append(positions, tb.Position())
	}

	expected := []int{0, 2, 4}

	if len(positions) != len(expected) {
		t.Fatalf("expected %d positions but got %d", len(expected), len(positions))
	}

	for i := range positions {
		if positions[i] != expected[i] {
			t.Errorf("window %d: expected positions=%d but got positions=%d", i, expected[i], positions[i])
		}
	}

	tail, _ := tb.Tail()

	if tailPosition := positions[len(positions)-1] + tail.Seen; tailPosition != 6 {
		t.Errorf("expected tail positions=6 but got %d", tailPosition)
	}
}

func TestTokenBuffer_DocumentBoundaryYieldsTail(t *testing.T) {
	tb := NewTokenBuffer(byteTokenizer{}, 10, 10)

	tb.SetIncludeTail(true)

	var a [][]int64

	for w := range tb.Push(0, "abcde") {
		a = append(a, w.Tokens)
	}

	if len(a) != 0 {
		t.Fatalf("expected 0 windows but got %d", len(a))
	}

	var b [][]int64

	for w := range tb.Push(2, "fghij") {
		b = append(b, w.Tokens)
	}

	if len(b) != 1 {
		t.Fatalf("expected 1 window but got %d", len(b))
	}

	expected := toInt64(byteTokenizer{}.Tokenize("abcde"))

	if !slices.Equal(b[0], expected) {
		t.Errorf("expected %v but got %v", expected, b[0])
	}
}

func TestTokenBuffer_TailPosition(t *testing.T) {
	tb := NewTokenBuffer(byteTokenizer{}, 4, 2)

	tb.SetIncludeTail(true)

	var lastPosition int

	for range tb.Push(0, "abcdefgh") {
		lastPosition = tb.Position()
	}

	if lastPosition != 4 {
		t.Errorf("expected last position=4 but got %d", lastPosition)
	}

	tail, _ := tb.Tail()

	tailPosition := lastPosition + tail.Seen

	if !slices.Equal(tail.Tokens, toInt64(byteTokenizer{}.Tokenize("gh"))) {
		t.Errorf("unexpected tail: %v", tail.Tokens)
	}

	if tailPosition != 6 {
		t.Errorf("expected tail position=6 but got %d", tailPosition)
	}
}