Upload 6 files
Browse files
tokenizer/__init__.py
ADDED
|
File without changes
|
tokenizer/__pycache__/__init__.cpython-314.pyc
ADDED
|
Binary file (168 Bytes). View file
|
|
|
tokenizer/__pycache__/tokenizer_utils.cpython-314.pyc
ADDED
|
Binary file (3.46 kB). View file
|
|
|
tokenizer/__pycache__/train_tokenizer.cpython-314.pyc
ADDED
|
Binary file (2.2 kB). View file
|
|
|
tokenizer/tokenizer_utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
from typing import Iterable, List
|
| 3 |
+
|
| 4 |
+
from tokenizers import Tokenizer
|
| 5 |
+
from tokenizers.models import BPE
|
| 6 |
+
from tokenizers.normalizers import NFKC
|
| 7 |
+
from tokenizers.pre_tokenizers import Whitespace
|
| 8 |
+
from tokenizers.trainers import BpeTrainer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def train_bpe_tokenizer(input_files: Iterable[Path], output_path: Path, vocab_size: int = 32000) -> Tokenizer:
|
| 12 |
+
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
|
| 13 |
+
tokenizer.normalizer = NFKC()
|
| 14 |
+
tokenizer.pre_tokenizer = Whitespace()
|
| 15 |
+
trainer = BpeTrainer(vocab_size=vocab_size, special_tokens=["[PAD]", "[UNK]", "[BOS]", "[EOS]"])
|
| 16 |
+
tokenizer.train_from_iterator((Path(path).read_text(encoding="utf-8") for path in input_files), trainer=trainer)
|
| 17 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
tokenizer.save(str(output_path))
|
| 19 |
+
return tokenizer
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_tokenizer(path: Path) -> Tokenizer:
|
| 23 |
+
return Tokenizer.from_file(str(path))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def encode_text(tokenizer: Tokenizer, text: str, add_special_tokens: bool = True) -> List[int]:
|
| 27 |
+
ids = tokenizer.encode(text, add_special_tokens=False).ids
|
| 28 |
+
if not add_special_tokens:
|
| 29 |
+
return ids
|
| 30 |
+
bos_id = tokenizer.token_to_id("[BOS]")
|
| 31 |
+
eos_id = tokenizer.token_to_id("[EOS]")
|
| 32 |
+
if bos_id is not None:
|
| 33 |
+
ids = [bos_id] + ids
|
| 34 |
+
if eos_id is not None:
|
| 35 |
+
ids = ids + [eos_id]
|
| 36 |
+
return ids
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def decode_ids(tokenizer: Tokenizer, ids: List[int]) -> str:
|
| 40 |
+
return tokenizer.decode(ids)
|
tokenizer/train_tokenizer.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 6 |
+
if str(ROOT) not in sys.path:
|
| 7 |
+
sys.path.append(str(ROOT))
|
| 8 |
+
|
| 9 |
+
from tokenizer.tokenizer_utils import train_bpe_tokenizer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
parser = argparse.ArgumentParser(description="Train a BPE tokenizer for MarsLM")
|
| 14 |
+
parser.add_argument("--input", nargs="+", required=True, help="Paths to raw text files")
|
| 15 |
+
parser.add_argument("--model-prefix", default="artifacts/tokenizer/marslm", help="Destination prefix")
|
| 16 |
+
parser.add_argument("--vocab-size", type=int, default=32000)
|
| 17 |
+
parser.add_argument("--model-type", default="bpe", help="Retained for compatibility")
|
| 18 |
+
args = parser.parse_args()
|
| 19 |
+
|
| 20 |
+
input_files = [Path(path).resolve() for path in args.input]
|
| 21 |
+
for path in input_files:
|
| 22 |
+
if not path.exists():
|
| 23 |
+
raise FileNotFoundError(f"Input file not found: {path}")
|
| 24 |
+
|
| 25 |
+
output_prefix = Path(args.model_prefix).resolve()
|
| 26 |
+
output_path = output_prefix.with_suffix(".json")
|
| 27 |
+
train_bpe_tokenizer(input_files, output_path, vocab_size=args.vocab_size)
|
| 28 |
+
print(f"Tokenizer saved to {output_path}")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
main()
|