File size: 5,446 Bytes
5df4ae4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #!/usr/bin/env python3
"""
tokenizer/train_sp_tokenizer.py โ SentencePiece Unigram ํ๊ตญ์ด ํ ํฌ๋์ด์ ํ์ต.
ํ๊ตญ์ด 1์์ (UTF-8 3๋ฐ์ดํธ) = 1ํ ํฐ์ด ๋๋๋ก Unigram ๋ชจ๋ธ์ ์ฌ์ฉ.
character_coverage=0.9995๋ก ํ๊ธ 11,172 ์์ ์ ์ฒด ์ปค๋ฒ.
Usage:
python tokenizer/train_sp_tokenizer.py \
--input "data/raw/namuwiki_ko/*.txt,data/raw/ko_wiki_0000.txt" \
--vocab_size 64000 \
--output_dir tokenizer/korean_sp
Output:
tokenizer/korean_sp/tokenizer.model (SentencePiece ๋ชจ๋ธ)
tokenizer/korean_sp/tokenizer.vocab (์ดํ ๋ชฉ๋ก)
"""
from __future__ import annotations
import argparse
import glob
import os
import sys
import tempfile
from pathlib import Path
def expand_inputs(input_spec: str) -> list[str]:
"""์ฝค๋ง๋ก ๊ตฌ๋ถ๋ ๊ธ๋ก๋ธ ํจํด๋ค์ ์ค์ ํ์ผ ๊ฒฝ๋ก ๋ชฉ๋ก์ผ๋ก ํ์ฅ."""
files: list[str] = []
for pattern in input_spec.split(","):
pattern = pattern.strip()
if any(c in pattern for c in ("*", "?", "[")):
matched = sorted(glob.glob(pattern, recursive=True))
if not matched:
print(f"WARNING: ํจํด์ ์ผ์นํ๋ ํ์ผ ์์: {pattern!r}", file=sys.stderr)
files.extend(matched)
else:
if Path(pattern).exists():
files.append(pattern)
else:
print(f"WARNING: ํ์ผ ์์: {pattern!r}", file=sys.stderr)
return files
def train(
input_files: list[str],
output_dir: Path,
vocab_size: int,
num_threads: int,
input_sentence_size: int,
) -> None:
try:
import sentencepiece as spm
except ImportError:
print(
"ERROR: sentencepiece๊ฐ ์ค์น๋์ง ์์.\n"
" pip install --break-system-packages sentencepiece",
file=sys.stderr,
)
sys.exit(1)
output_dir.mkdir(parents=True, exist_ok=True)
model_prefix = str(output_dir / "tokenizer")
print(f"์
๋ ฅ ํ์ผ ์: {len(input_files)}")
for f in input_files[:5]:
print(f" {f}")
if len(input_files) > 5:
print(f" ... ์ธ {len(input_files) - 5}๊ฐ")
print(f"์ดํ ํฌ๊ธฐ: {vocab_size:,}")
print(f"์ถ๋ ฅ ๊ฒฝ๋ก: {model_prefix}.model / .vocab")
print()
# SentencePiece๋ ํ์ผ ๋ชฉ๋ก์ ์ฝค๋ง๋ก ๊ตฌ๋ถ๋ ๋จ์ผ ๋ฌธ์์ด๋ก ๋ฐ๋๋ค
input_str = ",".join(input_files)
spm.SentencePieceTrainer.train(
input=input_str,
model_prefix=model_prefix,
vocab_size=vocab_size,
model_type="unigram", # BPE๋ณด๋ค ํ๊ตญ์ด์ ์์ฐ์ค๋ฌ์
character_coverage=0.9995, # ํ๊ธ 11,172 ์์ ์์ ์ปค๋ฒ
normalization_rule_name="nfkc", # Unicode NFKC ์ ๊ทํ (ํ๊ตญ์ด ํธํ๋ฌธ์ ํต์ผ)
pad_id=0,
bos_id=1,
eos_id=2,
unk_id=3,
pad_piece="<pad>",
bos_piece="<s>",
eos_piece="</s>",
unk_piece="<unk>",
user_defined_symbols=[],
num_threads=num_threads,
input_sentence_size=input_sentence_size,
shuffle_input_sentence=True,
# ํ์ต ์์ ์ฑ
seed_sentencepiece_size=1_000_000,
shrinking_factor=0.75,
max_sentence_length=4096,
)
model_path = Path(f"{model_prefix}.model")
vocab_path = Path(f"{model_prefix}.vocab")
if model_path.exists():
size_mb = model_path.stat().st_size / 1e6
print(f"ํ์ต ์๋ฃ!")
print(f" ๋ชจ๋ธ: {model_path} ({size_mb:.1f} MB)")
print(f" ์ดํ: {vocab_path}")
print()
print("๋ค์ ๋จ๊ณ:")
print(f" python tokenizer/convert_sp_to_hf.py \\")
print(f" --model {model_path} \\")
print(f" --output {output_dir}/tokenizer.json")
else:
print("ERROR: ํ์ต ์คํจ โ ์ถ๋ ฅ ํ์ผ์ด ์์ฑ๋์ง ์์", file=sys.stderr)
sys.exit(1)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="SentencePiece Unigram ํ๊ตญ์ด ํ ํฌ๋์ด์ ํ์ต",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--input",
required=True,
help="์ฝค๋ง๋ก ๊ตฌ๋ถ๋ ํ์ผ/๊ธ๋ก๋ธ ํจํด (์: 'data/raw/ko/*.txt,data/raw/wiki.txt')",
)
parser.add_argument(
"--vocab_size",
type=int,
default=64000,
help="์ดํ ํฌ๊ธฐ",
)
parser.add_argument(
"--output_dir",
type=Path,
default=Path("tokenizer/korean_sp"),
help="๋ชจ๋ธ ์ ์ฅ ๋๋ ํ ๋ฆฌ",
)
parser.add_argument(
"--num_threads",
type=int,
default=64,
help="ํ์ต์ ์ฌ์ฉํ CPU ์ค๋ ๋ ์",
)
parser.add_argument(
"--input_sentence_size",
type=int,
default=10_000_000,
help="ํ์ต์ ์ฌ์ฉํ ์ต๋ ๋ฌธ์ฅ ์ (0 = ๋ฌด์ ํ)",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
input_files = expand_inputs(args.input)
if not input_files:
print("ERROR: ์
๋ ฅ ํ์ผ์ด ์์ต๋๋ค.", file=sys.stderr)
sys.exit(1)
train(
input_files=input_files,
output_dir=args.output_dir,
vocab_size=args.vocab_size,
num_threads=args.num_threads,
input_sentence_size=args.input_sentence_size,
)
if __name__ == "__main__":
main()
|