Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
Fix #1429: `tokenizer train` saves a complete byte-level BPE (decoder, byte alphabet, no hard-coded unk) (#1455)
Kratos8bfeaa2e8288
diff --git a/changelog.d/0.75.1/1455.fixed.md b/changelog.d/0.75.1/1455.fixed.mdnew file mode 100644index 00000000..6d42d17a--- /dev/null+++ b/changelog.d/0.75.1/1455.fixed.md@@ -0,0 +1 @@+- `soup tokenizer train` now saves a complete byte-level BPE: a matching `ByteLevel` decoder (so `decode(encode(x)) == x`, no more `Ġ` for spaces), the 256-byte initial alphabet (so unseen characters never become `<unk>`), no hard-coded `unk_token`, and a `--vocab-size` floor that accounts for the byte alphabet plus special tokens (#1429 by @wanjinhao1 in #1455).diff --git a/src/soup_cli/commands/tokenizer.py b/src/soup_cli/commands/tokenizer.pyindex 68069309..2ff268f2 100644--- a/src/soup_cli/commands/tokenizer.py+++ b/src/soup_cli/commands/tokenizer.py@@ -163,7 +163,7 @@ def train(output_path = Path(output_real)try:- from tokenizers import Tokenizer, models, pre_tokenizers, trainers+ from tokenizers import Tokenizer, decoders, models, pre_tokenizers, trainersexcept ImportError as exc:console.print("[red]The 'tokenizers' package is required.[/]\n"@@ -194,6 +194,15 @@ def train(seen.add(tok)cleaned.append(tok)+ # A byte-level BPE needs the 256-byte initial alphabet plus every+ # special token to fit inside --vocab-size, or the trainer errors out+ # (or silently drops merges). Checked after the special tokens are known.+ if vocab_size < _MIN_VOCAB_SIZE + len(cleaned) + 1:+ raise typer.BadParameter(+ f"--vocab-size must be >= {_MIN_VOCAB_SIZE + len(cleaned) + 1} "+ f"(256-byte alphabet + {len(cleaned)} special tokens + merges)"+ )+console.print(Panel(f"Input: [bold]{resolved_input.name}[/]\n"@@ -204,12 +213,18 @@ def train())- tokenizer = Tokenizer(models.BPE(unk_token="<unk>"))+ # Complete byte-level BPE: no unk_token (the 256-byte alphabet can+ # encode anything), a matching ByteLevel decoder so decode() reverses+ # the pre-tokenizer, and the full byte alphabet seeded into the trainer+ # so unseen characters (accents, emoji, other scripts) never map to <unk>.+ tokenizer = Tokenizer(models.BPE())tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)+ tokenizer.decoder = decoders.ByteLevel()trainer = trainers.BpeTrainer(vocab_size=vocab_size,min_frequency=min_frequency,special_tokens=cleaned,+ initial_alphabet=pre_tokenizers.ByteLevel.alphabet(),show_progress=False,)tokenizer.train_from_iterator(texts, trainer=trainer)diff --git a/tests/test_issue1429_bytelevel_tokenizer.py b/tests/test_issue1429_bytelevel_tokenizer.pynew file mode 100644index 00000000..3ade9212--- /dev/null+++ b/tests/test_issue1429_bytelevel_tokenizer.py@@ -0,0 +1,134 @@+"""Issue #1429: `soup tokenizer train` must save a complete byte-level BPE.++A ByteLevel pre-tokenizer without a matching decoder decodes spaces as+"Ġ"; training without the 256-byte initial alphabet turns every character+absent from the corpus into `<unk>`. These tests pin the round-trip for+ASCII and unseen characters, both raw `tokenizers` and+`PreTrainedTokenizerFast`, and with a custom `--special-token` list.+"""++from __future__ import annotations++import json+from pathlib import Path++import pytest+from typer.testing import CliRunner++from soup_cli.cli import app+from tests.conftest import strip_ansi++_CORPUS = [+ "hello world number 1",+ "hello world number 2",+ "the quick brown fox jumps",+ "number 3 the quick fox",+] * 50++# Absent from the corpus: accented, emoji, CJK.+_UNSEEN = ["héllo", "\U0001F600", "世界"]+++def _write_corpus(tmp_path: Path) -> str:+ corpus = tmp_path / "corpus.jsonl"+ corpus.write_text(+ "\n".join(json.dumps({"text": t}) for t in _CORPUS) + "\n",+ encoding="utf-8",+ )+ return "corpus.jsonl"+++def _train(tmp_path, monkeypatch, *extra: str) -> Path:+ monkeypatch.chdir(tmp_path)+ _write_corpus(tmp_path)+ result = CliRunner().invoke(+ app,+ ["tokenizer", "train", "-i", "corpus.jsonl", "-v", "500",+ "-o", "tok", *extra],+ )+ assert result.exit_code == 0, result.output+ return tmp_path / "tok" / "tokenizer.json"+++def test_round_trip_default_special_tokens(tmp_path, monkeypatch):+ tok_path = _train(tmp_path, monkeypatch)+ from tokenizers import Tokenizer++ tok = Tokenizer.from_file(str(tok_path))+ for text in ["hello world", "the quick brown fox", *_UNSEEN]:+ assert tok.decode(tok.encode(text).ids) == text+++def test_round_trip_pretrained_tokenizer_fast(tmp_path, monkeypatch):+ from transformers import PreTrainedTokenizerFast++ monkeypatch.chdir(tmp_path)+ _write_corpus(tmp_path)+ result = CliRunner().invoke(+ app, ["tokenizer", "train", "-i", "corpus.jsonl", "-v", "500",+ "-o", "tok2"],+ )+ assert result.exit_code == 0, result.output+ fast = PreTrainedTokenizerFast(+ tokenizer_file=str(tmp_path / "tok2" / "tokenizer.json")+ )+ for text in ["hello world", *_UNSEEN]:+ assert fast.decode(fast(text)["input_ids"]) == text+++def test_custom_special_tokens_no_unk_still_encodes(tmp_path, monkeypatch):+ tok_path = _train(tmp_path, monkeypatch, "--special-token", "<|endoftext|>")+ from tokenizers import Tokenizer++ tok = Tokenizer.from_file(str(tok_path))+ assert "<unk>" not in tok.get_vocab()+ for text in _UNSEEN:+ assert "<unk>" not in tok.encode(text).tokens+++def test_no_unk_token_in_any_encoded_sequence(tmp_path, monkeypatch):+ tok_path = _train(tmp_path, monkeypatch)+ from tokenizers import Tokenizer++ tok = Tokenizer.from_file(str(tok_path))+ for text in ["hello world", *_UNSEEN]:+ assert "<unk>" not in tok.encode(text).tokens+++def test_saved_model_declares_no_unk_token(tmp_path, monkeypatch):+ monkeypatch.chdir(tmp_path)+ _write_corpus(tmp_path)+ result = CliRunner().invoke(+ app,+ ["tokenizer", "train", "-i", "corpus.jsonl", "-v", "400", "-o", "tok",+ "--special-token", "<|endoftext|>"],+ )+ assert result.exit_code == 0, result.output+ model = json.loads((tmp_path / "tok" / "tokenizer.json").read_text(encoding="utf-8"))["model"]+ assert model["unk_token"] is None+++ "vocab, extra_specials, ok",+ [+ (258, ("a", "b"), False), # 256 + 2 specials = 258: zero merges+ (259, ("a", "b"), True), # one merge fits+ (260, (), False), # default four specials: 260 = zero merges+ (261, (), True),+ ],+)+def test_vocab_floor_is_alphabet_plus_specials_plus_one_merge(+ tmp_path, monkeypatch, vocab, extra_specials, ok+):+ monkeypatch.chdir(tmp_path)+ _write_corpus(tmp_path)+ args = ["tokenizer", "train", "-i", "corpus.jsonl", "-v", str(vocab), "-o", "tok"]+ for tok in extra_specials:+ args += ["--special-token", tok]+ result = CliRunner().invoke(app, args)+ text = " ".join(strip_ansi(result.output).split())+ if ok:+ assert result.exit_code == 0, text+ else:+ assert result.exit_code == 2, text+ assert f"--vocab-size must be >= {vocab + 1}" in text