|
| 1 | +/* |
| 2 | +Copyright 2025 The llm-d Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//nolint:testpackage // need to test internal types |
| 18 | +package tokenization |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/daulet/tokenizers" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/mock" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +// MockTokenizer implements the Tokenizer interface for testing. |
| 32 | +type MockTokenizer struct { |
| 33 | + mock.Mock |
| 34 | +} |
| 35 | + |
| 36 | +func (m *MockTokenizer) Encode(input, modelName string) ([]uint32, []tokenizers.Offset, error) { |
| 37 | + args := m.Called(input, modelName) |
| 38 | + return args.Get(0).([]uint32), args.Get(1).([]tokenizers.Offset), args.Error(2) //nolint:errcheck // return mocked values |
| 39 | +} |
| 40 | + |
| 41 | +// MockIndexer implements the prefixstore.Indexer interface for testing. |
| 42 | +type MockIndexer struct { |
| 43 | + mock.Mock |
| 44 | +} |
| 45 | + |
| 46 | +func (m *MockIndexer) AddTokenization(modelName, prompt string, tokens []uint32, offsets []tokenizers.Offset) error { |
| 47 | + args := m.Called(modelName, prompt, tokens, offsets) |
| 48 | + return args.Error(0) |
| 49 | +} |
| 50 | + |
| 51 | +func (m *MockIndexer) FindLongestContainedTokens(prompt, modelName string) []uint32 { |
| 52 | + args := m.Called(prompt, modelName) |
| 53 | + return args.Get(0).([]uint32) //nolint:errcheck // unused mock |
| 54 | +} |
| 55 | + |
| 56 | +func TestPool_ProcessTask(t *testing.T) { |
| 57 | + mockIndexer := &MockIndexer{} |
| 58 | + mockTokenizer := &MockTokenizer{} |
| 59 | + |
| 60 | + pool := &Pool{ |
| 61 | + workers: 1, |
| 62 | + indexer: mockIndexer, |
| 63 | + tokenizer: mockTokenizer, |
| 64 | + } |
| 65 | + |
| 66 | + task := Task{ |
| 67 | + Prompt: "hello world", |
| 68 | + ModelName: testModelName, |
| 69 | + } |
| 70 | + |
| 71 | + // Setup specific mock return values |
| 72 | + expectedTokens := []uint32{12345, 67890, 11111} |
| 73 | + expectedOffsets := []tokenizers.Offset{{0, 5}, {6, 11}} |
| 74 | + |
| 75 | + mockTokenizer.On("Encode", task.Prompt, task.ModelName).Return(expectedTokens, expectedOffsets, nil) |
| 76 | + |
| 77 | + // Verify that indexer receives exactly the same tokens and offsets that tokenizer returned |
| 78 | + mockIndexer.On("AddTokenization", task.ModelName, task.Prompt, expectedTokens, expectedOffsets).Return(nil) |
| 79 | + |
| 80 | + // Execute |
| 81 | + err := pool.processTask(task) |
| 82 | + |
| 83 | + // Assert |
| 84 | + assert.NoError(t, err) |
| 85 | + mockTokenizer.AssertExpectations(t) |
| 86 | + mockIndexer.AssertExpectations(t) |
| 87 | +} |
| 88 | + |
| 89 | +func TestPool_RunIntegration(t *testing.T) { |
| 90 | + if testing.Short() { |
| 91 | + t.Skip("Skipping tokenizer integration test in short mode") |
| 92 | + } |
| 93 | + |
| 94 | + mockIndexer := &MockIndexer{} |
| 95 | + |
| 96 | + prompts := []string{"hello world", "this is a test", "unicode test: 世界"} |
| 97 | + |
| 98 | + // Setup mock expectations for each prompt |
| 99 | + for _, prompt := range prompts { |
| 100 | + mockIndexer.On("AddTokenization", testModelName, prompt, |
| 101 | + mock.Anything, mock.Anything).Return(nil).Once() |
| 102 | + } |
| 103 | + |
| 104 | + config := &Config{ |
| 105 | + WorkersCount: 2, |
| 106 | + HFTokenizerConfig: &HFTokenizerConfig{ |
| 107 | + TokenizersCacheDir: t.TempDir(), |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + pool, err := NewTokenizationPool(config, mockIndexer) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + // Create context for the pool |
| 115 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 116 | + defer cancel() |
| 117 | + |
| 118 | + for _, prompt := range prompts { |
| 119 | + pool.AddTask(prompt, testModelName) |
| 120 | + } |
| 121 | + |
| 122 | + // Run pool |
| 123 | + done := make(chan struct{}) |
| 124 | + go func() { |
| 125 | + defer close(done) |
| 126 | + pool.Run(ctx) |
| 127 | + }() |
| 128 | + |
| 129 | + time.Sleep(2 * time.Second) |
| 130 | + cancel() |
| 131 | + <-done |
| 132 | + |
| 133 | + mockIndexer.AssertExpectations(t) |
| 134 | +} |
0 commit comments