test_max_new_tokens.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. """Host-side tests for the DSV41_MAX_NEW_TOKENS default/cap."""
  3. import os
  4. import sys
  5. from pathlib import Path
  6. from types import SimpleNamespace
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / 'adapter'))
  9. from encoding_compat import ( # noqa: E402
  10. DEFAULT_MAX_NEW_TOKENS,
  11. apply_request_max_tokens,
  12. configured_max_new_tokens,
  13. install_serving_chat,
  14. )
  15. def _with_env(value, fn):
  16. old = os.environ.get('DSV41_MAX_NEW_TOKENS')
  17. if value is None:
  18. os.environ.pop('DSV41_MAX_NEW_TOKENS', None)
  19. else:
  20. os.environ['DSV41_MAX_NEW_TOKENS'] = value
  21. try:
  22. return fn()
  23. finally:
  24. if old is None:
  25. os.environ.pop('DSV41_MAX_NEW_TOKENS', None)
  26. else:
  27. os.environ['DSV41_MAX_NEW_TOKENS'] = old
  28. def test_default_limit():
  29. assert _with_env(None, configured_max_new_tokens) == DEFAULT_MAX_NEW_TOKENS
  30. assert _with_env('65536', configured_max_new_tokens) == 65536
  31. assert _with_env('0', configured_max_new_tokens) == 0
  32. def test_omitted_fills_default():
  33. req = SimpleNamespace(max_tokens=None, max_completion_tokens=None)
  34. _with_env(None, lambda: apply_request_max_tokens(req))
  35. assert req.max_tokens == DEFAULT_MAX_NEW_TOKENS
  36. def test_under_cap_is_kept():
  37. req = SimpleNamespace(max_tokens=256, max_completion_tokens=None)
  38. _with_env(None, lambda: apply_request_max_tokens(req))
  39. assert req.max_tokens == 256
  40. def test_over_cap_is_clamped():
  41. req = SimpleNamespace(max_tokens=1_000_000, max_completion_tokens=None)
  42. _with_env(None, lambda: apply_request_max_tokens(req))
  43. assert req.max_tokens == DEFAULT_MAX_NEW_TOKENS
  44. def test_max_completion_tokens_wins_and_clamps():
  45. req = SimpleNamespace(max_tokens=16, max_completion_tokens=999_999)
  46. _with_env('1024', lambda: apply_request_max_tokens(req))
  47. assert req.max_completion_tokens == 1024
  48. assert req.max_tokens == 16 # unused sibling left alone
  49. def test_zero_disables():
  50. req = SimpleNamespace(max_tokens=None, max_completion_tokens=None)
  51. _with_env('0', lambda: apply_request_max_tokens(req))
  52. assert req.max_tokens is None
  53. def test_install_convert_applies_cap():
  54. class FakeServing:
  55. class OpenAIServingChat:
  56. def _process_messages(self, request, is_multimodal):
  57. return request.chat_template_kwargs
  58. def _convert_to_internal_request(self, request, raw_request=None):
  59. return request.max_tokens, request
  60. def run():
  61. install_serving_chat(FakeServing)
  62. serving = FakeServing.OpenAIServingChat()
  63. req = SimpleNamespace(max_tokens=None, max_completion_tokens=None,
  64. chat_template_kwargs={'thinking': False})
  65. return serving._convert_to_internal_request(req)
  66. tokens, out = _with_env('4096', run)
  67. assert tokens == 4096
  68. assert out.max_tokens == 4096
  69. if __name__ == '__main__':
  70. tests = [v for k, v in list(globals().items()) if k.startswith('test_')]
  71. failed = 0
  72. for fn in tests:
  73. try:
  74. fn()
  75. print(f'[OK] {fn.__name__}')
  76. except Exception as exc:
  77. failed += 1
  78. print(f'[FAIL] {fn.__name__}: {exc}')
  79. print(f'{len(tests) - failed}/{len(tests)} passed')
  80. sys.exit(1 if failed else 0)