Support Laoyao BPE tokenizer in SFT heldout builder

This commit is contained in:
2026-07-01 15:32:42 +08:00
parent 069f891ceb
commit 9b584742c4
2 changed files with 21 additions and 3 deletions

View File

@@ -56,8 +56,10 @@ dataset_building/generated/training_mix_v4_train1m_test2p8k_noupsample_nobbh_202
``` ```
Use `OUT=/path/to/output` and `TOKENIZER=/path/to/tokenizer` to place the Use `OUT=/path/to/output` and `TOKENIZER=/path/to/tokenizer` to place the
validation set outside the repo or build it with a specific tokenizer. Generated validation set outside the repo or build it with a specific tokenizer. For
outputs stay ignored by git. Laoyao custom BPE tokenizers, set `LAOYAO_MODEL_ROOT=/path/to/laoyao/repo` so
the builder can import `laoyaomodel.tokenization.bpe`. Generated outputs stay
ignored by git.
## SFT Builders ## SFT Builders

View File

@@ -7,6 +7,7 @@ import json
import os import os
import random import random
import re import re
import sys
from collections import Counter, defaultdict from collections import Counter, defaultdict
from pathlib import Path from pathlib import Path
@@ -145,15 +146,30 @@ def prompt_hash(user):
def load_tokenizer(path): def load_tokenizer(path):
path_obj = Path(path)
try: try:
return AutoTokenizer.from_pretrained(path, trust_remote_code=True) return AutoTokenizer.from_pretrained(path, trust_remote_code=True)
except Exception: except Exception:
return Tokenizer.from_file(str(Path(path) / "tokenizer.json")) tokenizer_path = path_obj if path_obj.is_file() else path_obj / "tokenizer.json"
try:
return Tokenizer.from_file(str(tokenizer_path))
except Exception:
payload = json.loads(tokenizer_path.read_text(encoding="utf-8"))
if payload.get("implementation") != "laoyaomodel.bytes_bpe":
raise
model_root = os.environ.get("LAOYAO_MODEL_ROOT")
if model_root:
sys.path.insert(0, model_root)
from laoyaomodel.tokenization.bpe import BPETokenizer
return BPETokenizer.from_tokenizer_json(tokenizer_path)
def token_len(tok, text): def token_len(tok, text):
if isinstance(tok, Tokenizer): if isinstance(tok, Tokenizer):
return len(tok.encode(text, add_special_tokens=False).ids) return len(tok.encode(text, add_special_tokens=False).ids)
if tok.__class__.__name__ == "BPETokenizer":
return len(tok.encode(text))
return len(tok(text, add_special_tokens=False)["input_ids"]) return len(tok(text, add_special_tokens=False)["input_ids"])