Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion litgpt/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, checkpoint_dir: Path | str) -> None:
self.apply_decoding_fix = None
if (config_path := checkpoint_dir / "tokenizer_config.json").is_file():
with open(config_path, encoding="utf-8") as fp:
self.apply_decoding_fix = "LlamaTokenizer" in json.load(fp)["tokenizer_class"]
self.apply_decoding_fix = "LlamaTokenizer" in (json.load(fp).get("tokenizer_class") or "")

@property
def vocab_size(self) -> int:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright Lightning AI. Licensed under the Apache License 2.0, see LICENSE file.
import json
import os
import shutil
from pathlib import Path
Expand Down Expand Up @@ -112,6 +113,16 @@ def test_tokenizer_input_validation():
Tokenizer("cocofruit")


def test_tokenizer_config_without_tokenizer_class(tmp_path):
HFTokenizer(BPE()).save(str(tmp_path / "tokenizer.json"))
with open(tmp_path / "tokenizer_config.json", "w", encoding="utf-8") as fp:
json.dump({"add_bos_token": False}, fp)

tokenizer = Tokenizer(tmp_path)

assert tokenizer.apply_decoding_fix is False


@pytest.mark.parametrize("use_bos_by_default", (True, False))
@pytest.mark.parametrize("encode_use_bos", (None, True, False))
@pytest.mark.parametrize("encode_use_eos", (True, False))
Expand Down