Upload gemma-tokenizer-test.ipynb
Browse files- gemma-tokenizer-test.ipynb +168 -0
gemma-tokenizer-test.ipynb
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "f9427918",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"## Load original and transformers tokenizers"
|
9 |
+
]
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"cell_type": "code",
|
13 |
+
"execution_count": 1,
|
14 |
+
"id": "48a7fddf",
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"from huggingface_hub import hf_hub_download\n",
|
19 |
+
"\n",
|
20 |
+
"original_path = hf_hub_download(repo_id=\"google/codegemma-1.1-2b\", filename=\"tokenizer.model\")"
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 2,
|
26 |
+
"id": "aae9d9de",
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"from gemma.tokenizer import Tokenizer\n",
|
31 |
+
"\n",
|
32 |
+
"original = Tokenizer(original_path)"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": 3,
|
38 |
+
"id": "06b063cf",
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"from transformers import GemmaTokenizer, AutoTokenizer"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": 4,
|
48 |
+
"id": "a584d69f",
|
49 |
+
"metadata": {},
|
50 |
+
"outputs": [],
|
51 |
+
"source": [
|
52 |
+
"# Fails for \"main\"\n",
|
53 |
+
"revision = \"refs/pr/4\"\n",
|
54 |
+
"\n",
|
55 |
+
"t_fast = AutoTokenizer.from_pretrained(\"google/codegemma-1.1-7b-it\", revision=revision)\n",
|
56 |
+
"t_slow = GemmaTokenizer.from_pretrained(\"google/codegemma-1.1-7b-it\", revision=revision)"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 5,
|
62 |
+
"id": "72a1c087",
|
63 |
+
"metadata": {},
|
64 |
+
"outputs": [],
|
65 |
+
"source": [
|
66 |
+
"for s in [\n",
|
67 |
+
" '<start_of_turn>', '<end_of_turn>', '<mask>',\n",
|
68 |
+
" '<|fim_prefix|>', '<|fim_suffix|>', '<|fim_middle|>', '<|file_separator|>'\n",
|
69 |
+
"]:\n",
|
70 |
+
" encoded = original.encode(s, bos=False, eos=False)\n",
|
71 |
+
" assert t_fast.encode(s, add_special_tokens=False) == encoded, f\"Failed: {s}\"\n",
|
72 |
+
" assert t_slow.encode(s, add_special_tokens=False) == encoded, f\"Failed: {s}\"\n",
|
73 |
+
" assert t_fast.decode(encoded) == s, f\"Failed: {s}\"\n",
|
74 |
+
" assert t_slow.decode(encoded) == s, f\"Failed: {s}\""
|
75 |
+
]
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "markdown",
|
79 |
+
"id": "8ab89d7b",
|
80 |
+
"metadata": {},
|
81 |
+
"source": [
|
82 |
+
"## Verify on XNLI (validation split)"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"execution_count": 6,
|
88 |
+
"id": "0160405a",
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [],
|
91 |
+
"source": [
|
92 |
+
"from datasets import load_dataset\n",
|
93 |
+
"from tqdm import tqdm"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"cell_type": "code",
|
98 |
+
"execution_count": 7,
|
99 |
+
"id": "a743115c",
|
100 |
+
"metadata": {},
|
101 |
+
"outputs": [],
|
102 |
+
"source": [
|
103 |
+
"xnli = load_dataset(\"xnli\", \"all_languages\", split=\"validation\")"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "code",
|
108 |
+
"execution_count": 8,
|
109 |
+
"id": "9a52691b",
|
110 |
+
"metadata": {},
|
111 |
+
"outputs": [],
|
112 |
+
"source": [
|
113 |
+
"def verify(lang, text):\n",
|
114 |
+
" encoded_original = original.encode(text, bos=True, eos=False)\n",
|
115 |
+
" encoded_fast = t_fast.encode(text)\n",
|
116 |
+
" encoded_slow = t_slow.encode(text)\n",
|
117 |
+
" assert encoded_fast == encoded_original, f\"Fast encode error: {lang} - {text}\"\n",
|
118 |
+
" assert encoded_slow == encoded_original, f\"Slow encode error: {lang} - {text}\"\n",
|
119 |
+
" decoded = original.decode(encoded_original)\n",
|
120 |
+
" decoded_fast = t_fast.decode(encoded_fast, skip_special_tokens=True)\n",
|
121 |
+
" decoded_slow = t_slow.decode(encoded_slow, skip_special_tokens=True)\n",
|
122 |
+
" assert decoded_fast == decoded, f\"Fast decode error: {lang} - {text}\"\n",
|
123 |
+
" assert decoded_slow == decoded, f\"Slow decode error: {lang} - {text}\""
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "code",
|
128 |
+
"execution_count": 9,
|
129 |
+
"id": "f3123ffd",
|
130 |
+
"metadata": {},
|
131 |
+
"outputs": [
|
132 |
+
{
|
133 |
+
"name": "stderr",
|
134 |
+
"output_type": "stream",
|
135 |
+
"text": [
|
136 |
+
"100%|ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 2490/2490 [00:30<00:00, 80.45it/s]\n"
|
137 |
+
]
|
138 |
+
}
|
139 |
+
],
|
140 |
+
"source": [
|
141 |
+
"for p in tqdm(xnli[\"premise\"]):\n",
|
142 |
+
" for lang, text in p.items():\n",
|
143 |
+
" verify(lang, text)"
|
144 |
+
]
|
145 |
+
}
|
146 |
+
],
|
147 |
+
"metadata": {
|
148 |
+
"kernelspec": {
|
149 |
+
"display_name": "Python 3 (ipykernel)",
|
150 |
+
"language": "python",
|
151 |
+
"name": "python3"
|
152 |
+
},
|
153 |
+
"language_info": {
|
154 |
+
"codemirror_mode": {
|
155 |
+
"name": "ipython",
|
156 |
+
"version": 3
|
157 |
+
},
|
158 |
+
"file_extension": ".py",
|
159 |
+
"mimetype": "text/x-python",
|
160 |
+
"name": "python",
|
161 |
+
"nbconvert_exporter": "python",
|
162 |
+
"pygments_lexer": "ipython3",
|
163 |
+
"version": "3.10.12"
|
164 |
+
}
|
165 |
+
},
|
166 |
+
"nbformat": 4,
|
167 |
+
"nbformat_minor": 5
|
168 |
+
}
|