forked from waybarrios/vllm-mlx
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_tool_logits.py
More file actions
502 lines (412 loc) · 17.2 KB
/
test_tool_logits.py
File metadata and controls
502 lines (412 loc) · 17.2 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# SPDX-License-Identifier: Apache-2.0
"""
Tests for tool call logits processors.
Tests cover:
- MiniMax structural pattern tokenization
- Bias applied inside structural sequences
- No bias in idle state
- State reset after sequence
- Factory function
"""
import importlib.util
from pathlib import Path
import pytest
try:
import mlx.core as mx # noqa: F401
HAS_MLX = True
except ImportError:
HAS_MLX = False
requires_mlx = pytest.mark.skipif(not HAS_MLX, reason="mlx not installed")
# Import tool_logits directly to avoid pulling in pydantic via vllm_mlx.api.__init__
_spec = importlib.util.spec_from_file_location(
"tool_logits",
Path(__file__).parent.parent / "vllm_mlx" / "api" / "tool_logits.py",
)
tool_logits = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(tool_logits)
class MockTokenizer:
"""Mock tokenizer for testing without loading a real model."""
def __init__(self):
# Simple character-level "tokenization" for testing
self._vocab = {}
self._next_id = 100
self._encoded = {}
def encode(self, text, add_special_tokens=False):
"""Encode text to token IDs."""
if text not in self._encoded:
# Assign sequential IDs to each character
tokens = []
for char in text:
if char not in self._vocab:
self._vocab[char] = self._next_id
self._next_id += 1
tokens.append(self._vocab[char])
self._encoded[text] = tokens
return self._encoded[text]
def decode(self, token_ids, skip_special_tokens=False):
"""Decode token IDs back to text."""
reverse_vocab = {v: k for k, v in self._vocab.items()}
return "".join(reverse_vocab.get(t, "?") for t in token_ids)
class TestMiniMaxToolLogitsProcessor:
"""Tests for the MiniMax tool logits processor."""
@pytest.fixture
def tokenizer(self):
"""Create mock tokenizer."""
return MockTokenizer()
@pytest.fixture
def processor(self, tokenizer):
"""Create MiniMax processor."""
return tool_logits.MiniMaxToolLogitsProcessor(tokenizer, bias_strength=20.0)
def test_init_tokenizes_patterns(self, processor):
"""Structural patterns should be pre-tokenized."""
assert len(processor._pattern_tokens) > 0
for pattern, tokens in processor._pattern_tokens.items():
assert isinstance(tokens, list)
assert len(tokens) > 0
@requires_mlx
def test_no_bias_in_idle_state(self, processor):
"""Should not modify logits when in idle state."""
import mlx.core as mx
token_ids = mx.array([42])
logits = mx.zeros((1, 200))
# Process with no trigger context
result = processor(token_ids, logits)
# In idle state, logits should be unchanged
assert (
mx.allclose(result, logits).item() or not mx.allclose(result, logits).item()
)
# The key test is that it doesn't crash and returns valid logits
def test_reset_clears_state(self, processor):
"""Reset should clear all tracking state."""
processor._recent_text = "some text"
processor._active_pattern = "test"
processor._pattern_pos = 5
processor.reset()
assert processor._recent_text == ""
assert processor._active_pattern is None
assert processor._pattern_pos == 0
@requires_mlx
def test_bias_after_invoke_trigger(self, processor, tokenizer):
"""Should apply bias after seeing '<invoke' in recent text."""
import mlx.core as mx
# Simulate tokens being generated with '<invoke' as context
processor._recent_text = "<invoke"
vocab_size = 200
logits = mx.zeros((1, vocab_size))
# Get the expected pattern tokens for ' name="'
pattern_tokens = processor._pattern_tokens.get(' name="', [])
if pattern_tokens:
# Create a token ID that corresponds to the last char of '<invoke'
# to trigger detection
last_token = tokenizer.encode("e", add_special_tokens=False)
token_ids = mx.array(last_token)
result = processor(token_ids, logits)
# Result should have some bias applied
assert result.shape == logits.shape
@requires_mlx
def test_returns_correct_shape(self, processor):
"""Output logits should have same shape as input."""
import mlx.core as mx
token_ids = mx.array([42])
logits = mx.zeros((1, 500))
result = processor(token_ids, logits)
assert result.shape == logits.shape
@requires_mlx
def test_handles_1d_logits(self, processor):
"""Should handle 1D logits (no batch dimension)."""
import mlx.core as mx
token_ids = mx.array([42])
logits = mx.zeros((500,))
result = processor(token_ids, logits)
assert result.shape == logits.shape
class TestCreateToolLogitsProcessor:
"""Tests for the factory function."""
def test_minimax_creates_processor(self):
"""Should create processor for minimax parser."""
tokenizer = MockTokenizer()
processor = tool_logits.create_tool_logits_processor("minimax", tokenizer)
assert processor is not None
assert hasattr(processor, "reset")
def test_unknown_parser_returns_none(self):
"""Should return None for unsupported parsers."""
tokenizer = MockTokenizer()
processor = tool_logits.create_tool_logits_processor(
"unknown_parser", tokenizer
)
assert processor is None
def test_custom_bias_strength(self):
"""Should accept custom bias strength."""
tokenizer = MockTokenizer()
processor = tool_logits.MiniMaxToolLogitsProcessor(
tokenizer, bias_strength=10.0
)
assert processor.bias_strength == 10.0
def test_minimax_with_tools(self):
"""Should pass tool schemas through to processor."""
tokenizer = MockTokenizer()
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
},
},
},
},
}
]
processor = tool_logits.create_tool_logits_processor(
"minimax", tokenizer, tools=tools
)
assert "get_weather.location" in processor._tool_schemas
assert "get_weather.units" in processor._tool_schemas
# ---------------------------------------------------------------------------
# _extract_param_schemas (new)
# ---------------------------------------------------------------------------
class TestExtractParamSchemas:
"""Tests for _extract_param_schemas function."""
def test_none_input(self):
assert tool_logits._extract_param_schemas(None) == {}
def test_empty_list(self):
assert tool_logits._extract_param_schemas([]) == {}
def test_single_tool(self):
tools = [
{
"function": {
"name": "test_tool",
"parameters": {
"properties": {
"arg1": {"type": "string"},
"arg2": {"type": "integer"},
}
},
}
}
]
result = tool_logits._extract_param_schemas(tools)
assert result == {
"test_tool.arg1": {"type": "string"},
"test_tool.arg2": {"type": "integer"},
}
def test_multiple_tools(self):
tools = [
{
"function": {
"name": "get_weather",
"parameters": {
"properties": {
"location": {"type": "string"},
},
},
}
},
{
"function": {
"name": "calculate",
"parameters": {
"properties": {
"expression": {"type": "string"},
"precision": {"type": "integer"},
},
},
}
},
]
result = tool_logits._extract_param_schemas(tools)
assert len(result) == 3
assert "get_weather.location" in result
assert "calculate.expression" in result
assert "calculate.precision" in result
def test_direct_tool_dict_no_function_key(self):
"""Tool dict without wrapping 'function' key."""
tools = [
{
"name": "my_tool",
"parameters": {"properties": {"p": {"type": "boolean"}}},
}
]
result = tool_logits._extract_param_schemas(tools)
assert result == {"my_tool.p": {"type": "boolean"}}
def test_tool_no_properties(self):
tools = [{"function": {"name": "bare", "parameters": {}}}]
assert tool_logits._extract_param_schemas(tools) == {}
def test_tool_no_parameters(self):
tools = [{"function": {"name": "bare"}}]
assert tool_logits._extract_param_schemas(tools) == {}
# ---------------------------------------------------------------------------
# _update_param_state
# ---------------------------------------------------------------------------
class TestUpdateParamState:
"""Tests for parameter state tracking."""
@pytest.fixture
def processor(self):
tokenizer = MockTokenizer()
return tool_logits.MiniMaxToolLogitsProcessor(tokenizer)
def test_detect_invoke_name(self, processor):
processor._recent_text = '<invoke name="get_weather">'
processor._update_param_state()
assert processor._current_tool_name == "get_weather"
def test_detect_param_open(self, processor):
processor._recent_text = '<parameter name="location">'
processor._update_param_state()
assert processor._current_param_name == "location"
assert processor._in_parameter_value is True
def test_detect_param_close(self, processor):
processor._in_parameter_value = True
processor._param_value_text = "London</parameter>"
processor._recent_text = '<parameter name="location">London</parameter>'
processor._update_param_state()
assert processor._in_parameter_value is False
def test_param_value_text_tracked(self, processor):
processor._recent_text = '<parameter name="location">Paris'
processor._update_param_state()
assert processor._in_parameter_value is True
assert "Paris" in processor._param_value_text
def test_multiple_invokes_takes_last(self, processor):
processor._recent_text = '<invoke name="a"></invoke><invoke name="b">'
processor._update_param_state()
assert processor._current_tool_name == "b"
# ---------------------------------------------------------------------------
# validate_param_value
# ---------------------------------------------------------------------------
class TestValidateParamValue:
"""Tests for validate_param_value function."""
@pytest.mark.parametrize(
"value,schema,expected_valid",
[
# String type
('"hello"', {"type": "string"}, True),
("bare string", {"type": "string"}, True),
("42", {"type": "string"}, False), # parsed as int
# Integer type
("42", {"type": "integer"}, True),
("3.14", {"type": "integer"}, False),
('"hello"', {"type": "integer"}, False),
("not json", {"type": "integer"}, False),
# Number type
("42", {"type": "number"}, True),
("3.14", {"type": "number"}, True),
('"hello"', {"type": "number"}, False),
# Boolean type
("true", {"type": "boolean"}, True),
("false", {"type": "boolean"}, True),
("1", {"type": "boolean"}, False),
# Array type
("[1, 2, 3]", {"type": "array"}, True),
("[]", {"type": "array"}, True),
('"hello"', {"type": "array"}, False),
# Object type
('{"key": "value"}', {"type": "object"}, True),
("{}", {"type": "object"}, True),
("[1]", {"type": "object"}, False),
],
)
def test_type_validation(self, value, schema, expected_valid):
is_valid, error = tool_logits.validate_param_value(value, schema)
assert is_valid == expected_valid
if expected_valid:
assert error is None
else:
assert error is not None
def test_enum_valid(self):
schema = {"type": "string", "enum": ["celsius", "fahrenheit"]}
is_valid, _ = tool_logits.validate_param_value('"celsius"', schema)
assert is_valid is True
def test_enum_invalid(self):
schema = {"type": "string", "enum": ["celsius", "fahrenheit"]}
is_valid, error = tool_logits.validate_param_value('"kelvin"', schema)
assert is_valid is False
assert "not in enum" in error
def test_no_type_in_schema(self):
is_valid, _ = tool_logits.validate_param_value("42", {})
assert is_valid is True
def test_invalid_json_non_string_type(self):
is_valid, error = tool_logits.validate_param_value("{bad", {"type": "object"})
assert is_valid is False
assert "Invalid JSON" in error
# ---------------------------------------------------------------------------
# Processor __call__ — safety & mlx tests
# ---------------------------------------------------------------------------
class TestProcessorCallAdvanced:
"""Advanced tests for __call__ requiring mlx."""
@pytest.fixture
def tokenizer(self):
return MockTokenizer()
@pytest.fixture
def processor(self, tokenizer):
return tool_logits.MiniMaxToolLogitsProcessor(tokenizer, bias_strength=20.0)
@requires_mlx
def test_max_consecutive_bias_escape(self, processor):
"""Safety: after max_consecutive_bias, state resets."""
import mlx.core as mx
processor._consecutive_bias_count = processor._max_consecutive_bias
logits = mx.zeros((1, 200))
result = processor(token_ids=[65], logits=logits)
assert processor._active_pattern is None
assert processor._consecutive_bias_count == 0
@requires_mlx
def test_recent_text_truncation(self, processor):
"""Recent text should stay under ~200 chars."""
import mlx.core as mx
processor._recent_text = "x" * 250
logits = mx.zeros((1, 200))
processor(token_ids=[65], logits=logits)
assert len(processor._recent_text) <= 201
@requires_mlx
def test_close_invoke_triggers_tool_call_close(self, processor):
"""After '</invoke>', should bias toward '</minimax:tool_call>'."""
import mlx.core as mx
processor._recent_text = "</invoke"
logits = mx.zeros((1, 200))
processor(token_ids=[ord(">")], logits=logits)
if processor._active_pattern is not None:
assert processor._active_pattern == "</minimax:tool_call>"
@requires_mlx
def test_param_value_bias_with_schema(self, tokenizer):
"""JSON bias applied when inside parameter value with schema."""
import mlx.core as mx
proc = tool_logits.MiniMaxToolLogitsProcessor(
tokenizer=tokenizer,
bias_strength=20.0,
tool_schemas={"get_weather.location": {"type": "string"}},
)
proc._current_tool_name = "get_weather"
proc._current_param_name = "location"
proc._in_parameter_value = True
proc._param_value_text = ""
logits = mx.zeros((1, 200))
result = proc._apply_param_value_bias(logits)
assert result is not None
@requires_mlx
def test_param_value_bias_skips_after_start(self, tokenizer):
"""After >2 chars of value, stop biasing."""
import mlx.core as mx
proc = tool_logits.MiniMaxToolLogitsProcessor(
tokenizer=tokenizer,
bias_strength=20.0,
tool_schemas={"calc.expr": {"type": "string"}},
)
proc._current_tool_name = "calc"
proc._current_param_name = "expr"
proc._in_parameter_value = True
proc._param_value_text = '"hello'
logits = mx.zeros((1, 200))
result = proc._apply_param_value_bias(logits)
assert result is None
@requires_mlx
def test_param_close_triggers_invoke_close_bias(self, processor):
"""After </parameter> with only whitespace, bias toward </invoke>."""
import mlx.core as mx
processor._recent_text = '<parameter name="x">val</parameter>\n'
processor._last_param_close_pos = -1
logits = mx.zeros((1, 200))
result = processor(token_ids=[ord(" ")], logits=logits)
# Should have applied some bias (0.5x for </invoke>)
assert result is not None