File size: 6,718 Bytes
f24563f |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
"""
Tokenizer for LLM model.
"""
import os
import json
import regex as re
from typing import Dict, List, Optional, Tuple, Union
import sentencepiece as spm
from abc import ABC, abstractmethod
class Tokenizer(ABC):
"""
Abstract base class for tokenizers.
"""
@abstractmethod
def encode(self, text: str, add_bos: bool = True, add_eos: bool = False) -> List[int]:
"""
Encode text to token IDs.
Args:
text: Input text
add_bos: Whether to add beginning of sequence token
add_eos: Whether to add end of sequence token
Returns:
List of token IDs
"""
pass
@abstractmethod
def decode(self, token_ids: List[int], skip_special_tokens: bool = True) -> str:
"""
Decode token IDs to text.
Args:
token_ids: List of token IDs
skip_special_tokens: Whether to skip special tokens
Returns:
Decoded text
"""
pass
@property
@abstractmethod
def vocab_size(self) -> int:
"""
Get vocabulary size.
Returns:
Vocabulary size
"""
pass
@property
@abstractmethod
def bos_token_id(self) -> int:
"""
Get beginning of sequence token ID.
Returns:
Beginning of sequence token ID
"""
pass
@property
@abstractmethod
def eos_token_id(self) -> int:
"""
Get end of sequence token ID.
Returns:
End of sequence token ID
"""
pass
@property
@abstractmethod
def pad_token_id(self) -> int:
"""
Get padding token ID.
Returns:
Padding token ID
"""
pass
class SentencePieceTokenizer(Tokenizer):
"""
SentencePiece tokenizer.
Attributes:
model_path: Path to SentencePiece model
bos_id: Beginning of sequence token ID
eos_id: End of sequence token ID
pad_id: Padding token ID
"""
def __init__(
self,
model_path: str,
bos_id: int = 1,
eos_id: int = 2,
pad_id: int = 0
):
"""
Initialize tokenizer.
Args:
model_path: Path to SentencePiece model
bos_id: Beginning of sequence token ID
eos_id: End of sequence token ID
pad_id: Padding token ID
"""
self.model_path = model_path
self._bos_id = bos_id
self._eos_id = eos_id
self._pad_id = pad_id
# Load SentencePiece model
self.sp_model = spm.SentencePieceProcessor()
self.sp_model.Load(model_path)
def encode(self, text: str, add_bos: bool = True, add_eos: bool = False) -> List[int]:
"""
Encode text to token IDs.
Args:
text: Input text
add_bos: Whether to add beginning of sequence token
add_eos: Whether to add end of sequence token
Returns:
List of token IDs
"""
# Encode text
token_ids = self.sp_model.EncodeAsIds(text)
# Add special tokens
if add_bos:
token_ids = [self.bos_token_id] + token_ids
if add_eos:
token_ids = token_ids + [self.eos_token_id]
return token_ids
def decode(self, token_ids: List[int], skip_special_tokens: bool = True) -> str:
"""
Decode token IDs to text.
Args:
token_ids: List of token IDs
skip_special_tokens: Whether to skip special tokens
Returns:
Decoded text
"""
# Filter special tokens if requested
if skip_special_tokens:
token_ids = [
token_id for token_id in token_ids
if token_id not in [self.bos_token_id, self.eos_token_id, self.pad_token_id]
]
# Decode token IDs
text = self.sp_model.DecodeIds(token_ids)
return text
@property
def vocab_size(self) -> int:
"""
Get vocabulary size.
Returns:
Vocabulary size
"""
return self.sp_model.GetPieceSize()
@property
def bos_token_id(self) -> int:
"""
Get beginning of sequence token ID.
Returns:
Beginning of sequence token ID
"""
return self._bos_id
@property
def eos_token_id(self) -> int:
"""
Get end of sequence token ID.
Returns:
End of sequence token ID
"""
return self._eos_id
@property
def pad_token_id(self) -> int:
"""
Get padding token ID.
Returns:
Padding token ID
"""
return self._pad_id
def train_sentencepiece_tokenizer(
texts: List[str],
vocab_size: int,
model_prefix: str,
character_coverage: float = 0.9995,
model_type: str = "bpe",
user_defined_symbols: Optional[List[str]] = None
) -> str:
"""
Train SentencePiece tokenizer.
Args:
texts: List of training texts
vocab_size: Vocabulary size
model_prefix: Prefix for model files
character_coverage: Character coverage
model_type: Model type (bpe, unigram, char, word)
user_defined_symbols: List of user-defined symbols
Returns:
Path to trained model
"""
# Write training data to temporary file
with open(f"{model_prefix}.txt", "w", encoding="utf-8") as f:
for text in texts:
f.write(text + "\n")
# Set training arguments
args = [
f"--input={model_prefix}.txt",
f"--model_prefix={model_prefix}",
f"--vocab_size={vocab_size}",
f"--character_coverage={character_coverage}",
f"--model_type={model_type}",
"--pad_id=0",
"--bos_id=1",
"--eos_id=2",
"--unk_id=3",
"--pad_piece=<pad>",
"--bos_piece=<bos>",
"--eos_piece=<eos>",
"--unk_piece=<unk>",
"--normalization_rule_name=nmt_nfkc_cf",
]
# Add user-defined symbols if provided
if user_defined_symbols:
args.append(f"--user_defined_symbols={','.join(user_defined_symbols)}")
# Train tokenizer
spm.SentencePieceTrainer.Train(" ".join(args))
# Remove temporary file
os.remove(f"{model_prefix}.txt")
return f"{model_prefix}.model"
|