Text Generation
Transformers
Safetensors
taonet
trust-remote-code
sentencepiece
custom-architecture
custom_code
Instructions to use TaoTern/TaoNet-mini-A2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use TaoTern/TaoNet-mini-A2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="TaoTern/TaoNet-mini-A2", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("TaoTern/TaoNet-mini-A2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use TaoTern/TaoNet-mini-A2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "TaoTern/TaoNet-mini-A2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TaoTern/TaoNet-mini-A2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/TaoTern/TaoNet-mini-A2
- SGLang
How to use TaoTern/TaoNet-mini-A2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "TaoTern/TaoNet-mini-A2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TaoTern/TaoNet-mini-A2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "TaoTern/TaoNet-mini-A2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TaoTern/TaoNet-mini-A2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use TaoTern/TaoNet-mini-A2 with Docker Model Runner:
docker model run hf.co/TaoTern/TaoNet-mini-A2
Upload folder using huggingface_hub
Browse files- src/taoTrain/__pycache__/__init__.cpython-313.pyc +0 -0
- src/taoTrain/__pycache__/config.cpython-313.pyc +0 -0
- src/taoTrain/checkpointing/__pycache__/__init__.cpython-313.pyc +0 -0
- src/taoTrain/checkpointing/__pycache__/checkpoint.cpython-313.pyc +0 -0
- src/taoTrain/core/__pycache__/__init__.cpython-313.pyc +0 -0
- src/taoTrain/core/__pycache__/base.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/__init__.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/async_loader.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/chunk_manager.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/factory.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/hf_base.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/hf_pretrain.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/hf_rl.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/hf_sft.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/jsonl_base.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/loaders.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/pretrain_jsonl.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/rl_jsonl.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/sft_jsonl.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/sft_utils.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/tokenization_queue.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/tokenizer.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/vlm_jsonl.cpython-313.pyc +0 -0
- src/taoTrain/data/__pycache__/vlm_utils.cpython-313.pyc +0 -0
- src/taoTrain/inference/inferencer.py +3 -1
- src/taoTrain/models/__pycache__/__init__.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/cnn_encoder.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/embeddings.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/gamma_net.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/mla_components.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/multimodal_wrapper.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/registry.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/taonet.cpython-313.pyc +0 -0
- src/taoTrain/models/__pycache__/transformer.cpython-313.pyc +0 -0
- src/taoTrain/models/taonet.py +5 -5
- tokenization_taonet.py +28 -0
- verify_tokenizer_match.py +99 -0
src/taoTrain/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (365 Bytes). View file
|
|
|
src/taoTrain/__pycache__/config.cpython-313.pyc
ADDED
|
Binary file (37.7 kB). View file
|
|
|
src/taoTrain/checkpointing/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (306 Bytes). View file
|
|
|
src/taoTrain/checkpointing/__pycache__/checkpoint.cpython-313.pyc
ADDED
|
Binary file (7.41 kB). View file
|
|
|
src/taoTrain/core/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (399 Bytes). View file
|
|
|
src/taoTrain/core/__pycache__/base.cpython-313.pyc
ADDED
|
Binary file (11.2 kB). View file
|
|
|
src/taoTrain/data/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (1.22 kB). View file
|
|
|
src/taoTrain/data/__pycache__/async_loader.cpython-313.pyc
ADDED
|
Binary file (9.14 kB). View file
|
|
|
src/taoTrain/data/__pycache__/chunk_manager.cpython-313.pyc
ADDED
|
Binary file (21.2 kB). View file
|
|
|
src/taoTrain/data/__pycache__/factory.cpython-313.pyc
ADDED
|
Binary file (4.02 kB). View file
|
|
|
src/taoTrain/data/__pycache__/hf_base.cpython-313.pyc
ADDED
|
Binary file (3.81 kB). View file
|
|
|
src/taoTrain/data/__pycache__/hf_pretrain.cpython-313.pyc
ADDED
|
Binary file (3.53 kB). View file
|
|
|
src/taoTrain/data/__pycache__/hf_rl.cpython-313.pyc
ADDED
|
Binary file (3.19 kB). View file
|
|
|
src/taoTrain/data/__pycache__/hf_sft.cpython-313.pyc
ADDED
|
Binary file (3.66 kB). View file
|
|
|
src/taoTrain/data/__pycache__/jsonl_base.cpython-313.pyc
ADDED
|
Binary file (8.57 kB). View file
|
|
|
src/taoTrain/data/__pycache__/loaders.cpython-313.pyc
ADDED
|
Binary file (3.71 kB). View file
|
|
|
src/taoTrain/data/__pycache__/pretrain_jsonl.cpython-313.pyc
ADDED
|
Binary file (2.99 kB). View file
|
|
|
src/taoTrain/data/__pycache__/rl_jsonl.cpython-313.pyc
ADDED
|
Binary file (2.7 kB). View file
|
|
|
src/taoTrain/data/__pycache__/sft_jsonl.cpython-313.pyc
ADDED
|
Binary file (5.95 kB). View file
|
|
|
src/taoTrain/data/__pycache__/sft_utils.cpython-313.pyc
ADDED
|
Binary file (5.51 kB). View file
|
|
|
src/taoTrain/data/__pycache__/tokenization_queue.cpython-313.pyc
ADDED
|
Binary file (17.4 kB). View file
|
|
|
src/taoTrain/data/__pycache__/tokenizer.cpython-313.pyc
ADDED
|
Binary file (13.7 kB). View file
|
|
|
src/taoTrain/data/__pycache__/vlm_jsonl.cpython-313.pyc
ADDED
|
Binary file (4.91 kB). View file
|
|
|
src/taoTrain/data/__pycache__/vlm_utils.cpython-313.pyc
ADDED
|
Binary file (13 kB). View file
|
|
|
src/taoTrain/inference/inferencer.py
CHANGED
|
@@ -464,8 +464,10 @@ class Inferencer:
|
|
| 464 |
else:
|
| 465 |
input_ids = self._encode_prompt(prompt)
|
| 466 |
|
|
|
|
| 467 |
prompt_length = input_ids.shape[1]
|
| 468 |
-
|
|
|
|
| 469 |
generated_token_ids: list[int] = []
|
| 470 |
stop_reason = "max_length"
|
| 471 |
|
|
|
|
| 464 |
else:
|
| 465 |
input_ids = self._encode_prompt(prompt)
|
| 466 |
|
| 467 |
+
prompt_token_ids = input_ids.squeeze(0).tolist()
|
| 468 |
prompt_length = input_ids.shape[1]
|
| 469 |
+
special_token_ids = self._get_special_token_ids()
|
| 470 |
+
special_token_id_values = set(special_token_ids.values())
|
| 471 |
generated_token_ids: list[int] = []
|
| 472 |
stop_reason = "max_length"
|
| 473 |
|
src/taoTrain/models/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (342 Bytes). View file
|
|
|
src/taoTrain/models/__pycache__/cnn_encoder.cpython-313.pyc
ADDED
|
Binary file (2.65 kB). View file
|
|
|
src/taoTrain/models/__pycache__/embeddings.cpython-313.pyc
ADDED
|
Binary file (2.55 kB). View file
|
|
|
src/taoTrain/models/__pycache__/gamma_net.cpython-313.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
src/taoTrain/models/__pycache__/mla_components.cpython-313.pyc
ADDED
|
Binary file (13.6 kB). View file
|
|
|
src/taoTrain/models/__pycache__/multimodal_wrapper.cpython-313.pyc
ADDED
|
Binary file (13.7 kB). View file
|
|
|
src/taoTrain/models/__pycache__/registry.cpython-313.pyc
ADDED
|
Binary file (2.97 kB). View file
|
|
|
src/taoTrain/models/__pycache__/taonet.cpython-313.pyc
ADDED
|
Binary file (14.2 kB). View file
|
|
|
src/taoTrain/models/__pycache__/transformer.cpython-313.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
src/taoTrain/models/taonet.py
CHANGED
|
@@ -137,7 +137,7 @@ class SimpleLLM(BaseModel):
|
|
| 137 |
trainable_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
|
| 138 |
|
| 139 |
print(f"\n{'='*70}")
|
| 140 |
-
print("MODEL ARCHITECTURE -
|
| 141 |
print(f"{'='*70}")
|
| 142 |
print(f"Embedding:")
|
| 143 |
if self.use_factorized_embedding:
|
|
@@ -153,10 +153,10 @@ class SimpleLLM(BaseModel):
|
|
| 153 |
|
| 154 |
output_params = self.d_model * self.vocab_size
|
| 155 |
print(f"Output Head: {output_params/1e6:>8.2f}M")
|
| 156 |
-
print(f"Attention Blocks: {len(self.blocks):>10} layers
|
| 157 |
-
print(f"{'
|
| 158 |
-
print(f"Total Parameters: {total_params/1e6:>8.2f}M (trainable: {trainable_params/1e6:.2f}M)")
|
| 159 |
-
print(f"{'
|
| 160 |
print(f"Configuration:")
|
| 161 |
print(f" Model dimension (d_model): {self.d_model}")
|
| 162 |
print(f" KV latent dimension (d_latent_kv): {self.d_latent_kv}")
|
|
|
|
| 137 |
trainable_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
|
| 138 |
|
| 139 |
print(f"\n{'='*70}")
|
| 140 |
+
print("MODEL ARCHITECTURE - TAONET (DeepSeek MLA + RoPE)")
|
| 141 |
print(f"{'='*70}")
|
| 142 |
print(f"Embedding:")
|
| 143 |
if self.use_factorized_embedding:
|
|
|
|
| 153 |
|
| 154 |
output_params = self.d_model * self.vocab_size
|
| 155 |
print(f"Output Head: {output_params/1e6:>8.2f}M")
|
| 156 |
+
print(f"Attention Blocks: {len(self.blocks):>10} layers x AttentionBlock")
|
| 157 |
+
print(f"{'-'*70}")
|
| 158 |
+
print(f"Total Parameters: {total_params/1e6:>8.2f}M (trainable: {trainable_params/1e6:.2f}M)")
|
| 159 |
+
print(f"{'-'*70}")
|
| 160 |
print(f"Configuration:")
|
| 161 |
print(f" Model dimension (d_model): {self.d_model}")
|
| 162 |
print(f" KV latent dimension (d_latent_kv): {self.d_latent_kv}")
|
tokenization_taonet.py
CHANGED
|
@@ -30,6 +30,9 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 30 |
except ImportError as exc:
|
| 31 |
raise ImportError("TaoNetTokenizer requires sentencepiece to be installed") from exc
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
self.vocab_file = vocab_file
|
| 34 |
self.special_tokens_file = special_tokens_file
|
| 35 |
self.sp_model = spm.SentencePieceProcessor()
|
|
@@ -50,9 +53,14 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 50 |
}
|
| 51 |
|
| 52 |
merged_additional_tokens = list(additional_special_tokens or [])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
for token in configured_special_tokens:
|
| 54 |
if token not in {bos_token, eos_token, unk_token, pad_token} and token not in merged_additional_tokens:
|
| 55 |
merged_additional_tokens.append(token)
|
|
|
|
| 56 |
|
| 57 |
super().__init__(
|
| 58 |
bos_token=bos_token,
|
|
@@ -125,6 +133,26 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 125 |
return ""
|
| 126 |
return self.sp_model.decode_pieces(tokens)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 129 |
if token_ids_1 is None:
|
| 130 |
return list(token_ids_0)
|
|
|
|
| 30 |
except ImportError as exc:
|
| 31 |
raise ImportError("TaoNetTokenizer requires sentencepiece to be installed") from exc
|
| 32 |
|
| 33 |
+
# Newer Transformers versions may round-trip this field from tokenizer_config.json
|
| 34 |
+
# as either a dict or a list. TaoNet only needs the token strings here.
|
| 35 |
+
extra_special_tokens = kwargs.pop("extra_special_tokens", None)
|
| 36 |
self.vocab_file = vocab_file
|
| 37 |
self.special_tokens_file = special_tokens_file
|
| 38 |
self.sp_model = spm.SentencePieceProcessor()
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
merged_additional_tokens = list(additional_special_tokens or [])
|
| 56 |
+
if isinstance(extra_special_tokens, dict):
|
| 57 |
+
merged_additional_tokens.extend(str(token) for token in extra_special_tokens.keys())
|
| 58 |
+
elif isinstance(extra_special_tokens, (list, tuple)):
|
| 59 |
+
merged_additional_tokens.extend(str(token) for token in extra_special_tokens)
|
| 60 |
for token in configured_special_tokens:
|
| 61 |
if token not in {bos_token, eos_token, unk_token, pad_token} and token not in merged_additional_tokens:
|
| 62 |
merged_additional_tokens.append(token)
|
| 63 |
+
merged_additional_tokens = list(dict.fromkeys(merged_additional_tokens))
|
| 64 |
|
| 65 |
super().__init__(
|
| 66 |
bos_token=bos_token,
|
|
|
|
| 133 |
return ""
|
| 134 |
return self.sp_model.decode_pieces(tokens)
|
| 135 |
|
| 136 |
+
def decode(self, token_ids, skip_special_tokens=False, **kwargs):
|
| 137 |
+
del kwargs
|
| 138 |
+
|
| 139 |
+
if hasattr(token_ids, "tolist"):
|
| 140 |
+
token_ids = token_ids.tolist()
|
| 141 |
+
|
| 142 |
+
if isinstance(token_ids, (list, tuple)) and token_ids and isinstance(token_ids[0], (list, tuple)):
|
| 143 |
+
token_ids = token_ids[0]
|
| 144 |
+
|
| 145 |
+
if not isinstance(token_ids, list):
|
| 146 |
+
token_ids = [int(token_ids)]
|
| 147 |
+
else:
|
| 148 |
+
token_ids = [int(token_id) for token_id in token_ids]
|
| 149 |
+
|
| 150 |
+
if skip_special_tokens:
|
| 151 |
+
special_token_ids = {int(token_id) for token_id in self.special_token_ids.values()}
|
| 152 |
+
token_ids = [token_id for token_id in token_ids if token_id not in special_token_ids]
|
| 153 |
+
|
| 154 |
+
return self.sp_model.decode(token_ids)
|
| 155 |
+
|
| 156 |
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 157 |
if token_ids_1 is None:
|
| 158 |
return list(token_ids_0)
|
verify_tokenizer_match.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Verify that the exported HF tokenizer matches the training-time tokenizer wrapper."""
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
import sentencepiece as spm
|
| 7 |
+
|
| 8 |
+
from tokenization_taonet import TaoNetTokenizer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
SAMPLES = [
|
| 12 |
+
"Explain why compact language models can still be useful.",
|
| 13 |
+
"Fruit is now expensive so we should",
|
| 14 |
+
"Hello world",
|
| 15 |
+
"<user>",
|
| 16 |
+
"<assistant>",
|
| 17 |
+
"\n",
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def main():
|
| 22 |
+
repo_dir = Path(__file__).resolve().parent
|
| 23 |
+
sys.path.insert(0, str(repo_dir / "src"))
|
| 24 |
+
from taoTrain.data.tokenizer import SentencePieceTokenizerWrapper, load_special_token_metadata
|
| 25 |
+
|
| 26 |
+
tokenizer_model = repo_dir / "tokenizer" / "tokenizer.model"
|
| 27 |
+
if not tokenizer_model.exists():
|
| 28 |
+
tokenizer_model = repo_dir / "tokenizer.model"
|
| 29 |
+
|
| 30 |
+
sp = spm.SentencePieceProcessor()
|
| 31 |
+
sp.Load(str(tokenizer_model))
|
| 32 |
+
special_token_ids = load_special_token_metadata(tokenizer_model)
|
| 33 |
+
|
| 34 |
+
train_tokenizer = SentencePieceTokenizerWrapper(sp, special_token_ids=special_token_ids)
|
| 35 |
+
hf_tokenizer = TaoNetTokenizer.from_pretrained(str(repo_dir))
|
| 36 |
+
|
| 37 |
+
print(f"train vocab_size: {train_tokenizer.vocab_size}")
|
| 38 |
+
print(f"hf vocab_size: {hf_tokenizer.vocab_size}")
|
| 39 |
+
|
| 40 |
+
for token in ["<UNK>", "<BOS>", "<EOS>", "<PAD>", "<think>", "<user>", "<assistant>", "<image>", "\n"]:
|
| 41 |
+
train_id = train_tokenizer.get_special_token_id(token)
|
| 42 |
+
hf_id = hf_tokenizer.get_special_token_id(token)
|
| 43 |
+
print(f"{token!r}: train={train_id}, hf={hf_id}")
|
| 44 |
+
if train_id != hf_id:
|
| 45 |
+
raise SystemExit(f"Special token mismatch for {token}: train={train_id}, hf={hf_id}")
|
| 46 |
+
|
| 47 |
+
print("\nChecking ID -> token mapping...")
|
| 48 |
+
for token_id in range(sp.vocab_size()):
|
| 49 |
+
train_piece = sp.id_to_piece(token_id)
|
| 50 |
+
hf_piece = hf_tokenizer._convert_id_to_token(token_id)
|
| 51 |
+
if token_id in special_token_ids.values():
|
| 52 |
+
expected = next(token for token, value in special_token_ids.items() if value == token_id)
|
| 53 |
+
if hf_piece != expected:
|
| 54 |
+
raise SystemExit(
|
| 55 |
+
f"HF id->token mismatch at id={token_id}: expected special token {expected!r}, got {hf_piece!r}"
|
| 56 |
+
)
|
| 57 |
+
else:
|
| 58 |
+
if hf_piece != train_piece:
|
| 59 |
+
raise SystemExit(
|
| 60 |
+
f"HF id->token mismatch at id={token_id}: train={train_piece!r}, hf={hf_piece!r}"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
print("ID -> token mapping matches.")
|
| 64 |
+
|
| 65 |
+
print("\nChecking sample encodes/decodes...")
|
| 66 |
+
for sample in SAMPLES:
|
| 67 |
+
train_ids = train_tokenizer(sample, return_attention_mask=True)
|
| 68 |
+
hf_ids = hf_tokenizer(sample, return_attention_mask=True)
|
| 69 |
+
print(f"sample: {sample!r}")
|
| 70 |
+
print(f" train ids: {train_ids['input_ids']}")
|
| 71 |
+
print(f" hf ids: {hf_ids['input_ids']}")
|
| 72 |
+
if train_ids["input_ids"] != hf_ids["input_ids"]:
|
| 73 |
+
raise SystemExit(f"Encoding mismatch for sample {sample!r}")
|
| 74 |
+
|
| 75 |
+
train_decoded = train_tokenizer.decode(train_ids["input_ids"], skip_special_tokens=True)
|
| 76 |
+
hf_decoded = hf_tokenizer.decode(hf_ids["input_ids"], skip_special_tokens=True)
|
| 77 |
+
print(f" train decode: {train_decoded!r}")
|
| 78 |
+
print(f" hf decode: {hf_decoded!r}")
|
| 79 |
+
if train_decoded != hf_decoded:
|
| 80 |
+
raise SystemExit(f"Decode mismatch for sample {sample!r}")
|
| 81 |
+
|
| 82 |
+
prompt = "Explain why compact language models can still be useful."
|
| 83 |
+
chat_ids = [
|
| 84 |
+
train_tokenizer.get_special_token_id("<user>"),
|
| 85 |
+
*train_tokenizer(prompt)["input_ids"],
|
| 86 |
+
train_tokenizer.get_special_token_id("<assistant>"),
|
| 87 |
+
]
|
| 88 |
+
hf_chat = hf_tokenizer.build_chat_inputs(prompt)
|
| 89 |
+
print("\nChecking chat prompt construction...")
|
| 90 |
+
print(f" train-style chat ids: {chat_ids}")
|
| 91 |
+
print(f" hf chat ids: {hf_chat['input_ids']}")
|
| 92 |
+
if chat_ids != hf_chat["input_ids"]:
|
| 93 |
+
raise SystemExit("Chat prompt IDs do not match training-time construction.")
|
| 94 |
+
|
| 95 |
+
print("\nTokenizer verification passed.")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|