|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Stop-token handling for checkpoints that name more than one EOS id.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from ..config import ModelConfig |
| 14 | +from ..model import _eos_token_ids, _runtime_config |
| 15 | + |
| 16 | + |
| 17 | +def _config(eos: object) -> ModelConfig: |
| 18 | + return ModelConfig.from_json( |
| 19 | + json.dumps( |
| 20 | + { |
| 21 | + "model_type": "llama", |
| 22 | + "hidden_size": 8, |
| 23 | + "num_hidden_layers": 2, |
| 24 | + "num_attention_heads": 2, |
| 25 | + "num_key_value_heads": 1, |
| 26 | + "head_dim": 4, |
| 27 | + "vocab_size": 32, |
| 28 | + "bos_token_id": 0, |
| 29 | + "eos_token_id": eos, |
| 30 | + "pad_token_id": 0, |
| 31 | + } |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def test_a_single_stop_token_is_normalised_to_one_entry() -> None: |
| 37 | + assert _eos_token_ids(2) == [2] |
| 38 | + assert _eos_token_ids([2]) == [2] |
| 39 | + |
| 40 | + |
| 41 | +def test_several_stop_tokens_keep_their_order() -> None: |
| 42 | + """MiniCPM5 names two; the second is the one it actually emits.""" |
| 43 | + assert _eos_token_ids([1, 130073]) == [1, 130073] |
| 44 | + |
| 45 | + |
| 46 | +def test_a_boolean_is_not_a_token_id() -> None: |
| 47 | + """`bool` is an `int` subclass, so it would otherwise pass silently.""" |
| 48 | + with pytest.raises(ValueError, match="must be an integer or a list of integers"): |
| 49 | + _eos_token_ids(True) |
| 50 | + with pytest.raises(ValueError, match="must be an integer or a list of integers"): |
| 51 | + _eos_token_ids([2, False]) |
| 52 | + |
| 53 | + |
| 54 | +def test_a_non_integer_stop_token_is_refused() -> None: |
| 55 | + with pytest.raises(ValueError, match="must be an integer or a list of integers"): |
| 56 | + _eos_token_ids("</s>") |
| 57 | + |
| 58 | + |
| 59 | +def test_an_empty_stop_token_list_is_refused() -> None: |
| 60 | + with pytest.raises(ValueError, match="must name at least one token"): |
| 61 | + _eos_token_ids([]) |
| 62 | + |
| 63 | + |
| 64 | +def test_one_stop_token_writes_only_the_scalar(tmp_path: Path) -> None: |
| 65 | + """A single-stop bundle keeps the field set it had before multi-EOS.""" |
| 66 | + runtime = _runtime_config(tmp_path, _config(2)) |
| 67 | + assert runtime["eos_token_id"] == 2 |
| 68 | + assert "eos_token_ids" not in runtime |
| 69 | + |
| 70 | + |
| 71 | +def test_several_stop_tokens_write_both_fields(tmp_path: Path) -> None: |
| 72 | + """The scalar stays readable by a runtime that predates the list.""" |
| 73 | + runtime = _runtime_config(tmp_path, _config([1, 130073])) |
| 74 | + assert runtime["eos_token_id"] == 1 |
| 75 | + assert runtime["eos_token_ids"] == [1, 130073] |
| 76 | + |
| 77 | + |
| 78 | +def test_generation_config_overrides_the_model_config(tmp_path: Path) -> None: |
| 79 | + """A checkpoint may widen its stop set in generation_config.json.""" |
| 80 | + (tmp_path / "generation_config.json").write_text( |
| 81 | + json.dumps({"eos_token_id": [7, 8, 9]}), encoding="utf-8" |
| 82 | + ) |
| 83 | + runtime = _runtime_config(tmp_path, _config(2)) |
| 84 | + assert runtime["eos_token_id"] == 7 |
| 85 | + assert runtime["eos_token_ids"] == [7, 8, 9] |
| 86 | + |
| 87 | + |
| 88 | +def test_generation_config_must_hold_one_object(tmp_path: Path) -> None: |
| 89 | + (tmp_path / "generation_config.json").write_text(json.dumps([1, 2]), encoding="utf-8") |
| 90 | + with pytest.raises(ValueError, match="must contain one JSON object"): |
| 91 | + _runtime_config(tmp_path, _config(2)) |
0 commit comments