showeed commited on
Commit
e4b9abb
·
verified ·
1 Parent(s): 3869dd3

Upload LMF_ONNX_test.ipynb

Browse files
Files changed (1) hide show
  1. LMF_ONNX_test.ipynb +515 -0
LMF_ONNX_test.ipynb ADDED
@@ -0,0 +1,515 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "3c7864eb",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import numpy as np\n",
11
+ "import onnxruntime as ort\n",
12
+ "from huggingface_hub import hf_hub_download\n",
13
+ "from transformers import AutoTokenizer\n",
14
+ "\n",
15
+ "# Download model (Q4 recommended)\n",
16
+ "model_id = \"LiquidAI/LFM2.5-1.2B-JP-ONNX\"\n",
17
+ "model_path = hf_hub_download(model_id, \"onnx/model_q4.onnx\")\n",
18
+ "\n",
19
+ "# Download all data files (handles multiple splits for large models)\n",
20
+ "from huggingface_hub import list_repo_files\n",
21
+ "for f in list_repo_files(model_id):\n",
22
+ " if f.startswith(\"onnx/model_q4.onnx_data\"):\n",
23
+ " hf_hub_download(model_id, f)\n",
24
+ "\n",
25
+ "# Load model and tokenizer\n",
26
+ "session = ort.InferenceSession(model_path)\n",
27
+ "tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "markdown",
32
+ "id": "2275f454",
33
+ "metadata": {},
34
+ "source": []
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": 4,
39
+ "id": "5788adf8",
40
+ "metadata": {},
41
+ "outputs": [
42
+ {
43
+ "name": "stdout",
44
+ "output_type": "stream",
45
+ "text": [
46
+ "日本の総理大臣は、総理大臣(総理大臣)または首相(首相)とも呼ばれる。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣の長である。総理大臣は、内閣\n"
47
+ ]
48
+ }
49
+ ],
50
+ "source": [
51
+ "\n",
52
+ "# Prepare text completion input (Japanese)\n",
53
+ "prompt = \"日本の総理大臣は\"\n",
54
+ "input_ids = np.array([tokenizer.encode(prompt, add_special_tokens=True)], dtype=np.int64)\n",
55
+ "\n",
56
+ "# Initialize KV cache\n",
57
+ "ONNX_DTYPE = {\"tensor(float)\": np.float32, \"tensor(float16)\": np.float16, \"tensor(int64)\": np.int64}\n",
58
+ "cache = {}\n",
59
+ "for inp in session.get_inputs():\n",
60
+ " if inp.name in {\"input_ids\", \"attention_mask\", \"position_ids\"}:\n",
61
+ " continue\n",
62
+ " shape = [d if isinstance(d, int) else 1 for d in inp.shape]\n",
63
+ " for i, d in enumerate(inp.shape):\n",
64
+ " if isinstance(d, str) and \"sequence\" in d.lower():\n",
65
+ " shape[i] = 0\n",
66
+ " cache[inp.name] = np.zeros(shape, dtype=ONNX_DTYPE.get(inp.type, np.float32))\n",
67
+ "\n",
68
+ "# Check if model uses position_ids\n",
69
+ "input_names = {inp.name for inp in session.get_inputs()}\n",
70
+ "use_position_ids = \"position_ids\" in input_names\n",
71
+ "\n",
72
+ "# Generate tokens\n",
73
+ "seq_len = input_ids.shape[1]\n",
74
+ "generated_tokens = []\n",
75
+ "\n",
76
+ "for step in range(300): # max tokens\n",
77
+ " if step == 0:\n",
78
+ " ids = input_ids\n",
79
+ " pos = np.arange(seq_len, dtype=np.int64).reshape(1, -1)\n",
80
+ " else:\n",
81
+ " ids = np.array([[generated_tokens[-1]]], dtype=np.int64)\n",
82
+ " pos = np.array([[seq_len + len(generated_tokens) - 1]], dtype=np.int64)\n",
83
+ "\n",
84
+ " attn_mask = np.ones((1, seq_len + len(generated_tokens)), dtype=np.int64)\n",
85
+ " feed = {\"input_ids\": ids, \"attention_mask\": attn_mask, **cache}\n",
86
+ " if use_position_ids:\n",
87
+ " feed[\"position_ids\"] = pos\n",
88
+ "\n",
89
+ " outputs = session.run(None, feed)\n",
90
+ " next_token = int(np.argmax(outputs[0][0, -1]))\n",
91
+ " generated_tokens.append(next_token)\n",
92
+ "\n",
93
+ " # Update cache\n",
94
+ " for i, out in enumerate(session.get_outputs()[1:], 1):\n",
95
+ " name = out.name.replace(\"present_conv\", \"past_conv\").replace(\"present.\", \"past_key_values.\")\n",
96
+ " if name in cache:\n",
97
+ " cache[name] = outputs[i]\n",
98
+ "\n",
99
+ " if next_token == tokenizer.eos_token_id:\n",
100
+ " break\n",
101
+ "\n",
102
+ "\n",
103
+ "print(prompt + tokenizer.decode(generated_tokens, skip_special_tokens=True))\n"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": 1,
109
+ "id": "85caf150",
110
+ "metadata": {},
111
+ "outputs": [
112
+ {
113
+ "name": "stderr",
114
+ "output_type": "stream",
115
+ "text": [
116
+ "c:\\Users\\showe\\Desktop\\仕事関連\\venv\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
117
+ " from .autonotebook import tqdm as notebook_tqdm\n"
118
+ ]
119
+ }
120
+ ],
121
+ "source": [
122
+ "import numpy as np\n",
123
+ "import onnxruntime as ort\n",
124
+ "from huggingface_hub import hf_hub_download\n",
125
+ "from transformers import AutoProcessor\n",
126
+ "from PIL import Image\n",
127
+ "\n",
128
+ "# Download model files (fp16 encoder + q4 decoder recommended)\n",
129
+ "model_id = \"LiquidAI/LFM2.5-VL-1.6B-ONNX\"\n",
130
+ "embed_tokens_path = hf_hub_download(model_id, \"onnx/embed_tokens_fp16.onnx\")\n",
131
+ "embed_images_path = hf_hub_download(model_id, \"onnx/embed_images_fp16.onnx\")\n",
132
+ "decoder_path = hf_hub_download(model_id, \"onnx/decoder_q4.onnx\")\n",
133
+ "\n",
134
+ "# Download all data files (handles multiple splits for large models)\n",
135
+ "from huggingface_hub import list_repo_files\n",
136
+ "for f in list_repo_files(model_id):\n",
137
+ " if any(f.startswith(f\"onnx/{name}\") for name in [\n",
138
+ " \"embed_tokens_fp16.onnx_data\",\n",
139
+ " \"embed_images_fp16.onnx_data\",\n",
140
+ " \"decoder_q4.onnx_data\"\n",
141
+ " ]):\n",
142
+ " hf_hub_download(model_id, f)\n",
143
+ "\n",
144
+ "# Load ONNX sessions\n",
145
+ "embed_tokens = ort.InferenceSession(embed_tokens_path)\n",
146
+ "embed_images = ort.InferenceSession(embed_images_path)\n",
147
+ "decoder = ort.InferenceSession(decoder_path)\n",
148
+ "\n",
149
+ "# Load processor\n",
150
+ "processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)\n"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 5,
156
+ "id": "73769fbf",
157
+ "metadata": {},
158
+ "outputs": [
159
+ {
160
+ "name": "stdout",
161
+ "output_type": "stream",
162
+ "text": [
163
+ "この\n",
164
+ "画像\n",
165
+ "は\n",
166
+ "、\n",
167
+ "雪\n",
168
+ "に\n",
169
+ "覆\n",
170
+ "われた\n",
171
+ "�\n",
172
+ "�\n",
173
+ "面\n",
174
+ "で\n",
175
+ "、\n",
176
+ "晴\n",
177
+ "れた\n",
178
+ "日\n",
179
+ "を\n",
180
+ "楽し\n",
181
+ "んでいる\n",
182
+ "人\n",
183
+ "物を\n",
184
+ "描\n",
185
+ "いて\n",
186
+ "います\n",
187
+ "。\n",
188
+ "太\n",
189
+ "陽\n",
190
+ "が\n",
191
+ "空\n",
192
+ "を\n",
193
+ "�\n",
194
+ "�\n",
195
+ "か\n",
196
+ "せ\n",
197
+ "、\n",
198
+ "雪\n",
199
+ "に\n",
200
+ "長\n",
201
+ "い\n",
202
+ "影\n",
203
+ "を\n",
204
+ "落\n",
205
+ "として\n",
206
+ "います\n",
207
+ "。\n",
208
+ "背景\n",
209
+ "には\n",
210
+ "、\n",
211
+ "木\n",
212
+ "々\n",
213
+ "や\n",
214
+ "山\n",
215
+ "々\n",
216
+ "が\n",
217
+ "広\n",
218
+ "が\n",
219
+ "り\n",
220
+ "、\n",
221
+ "冬\n",
222
+ "の\n",
223
+ "風\n",
224
+ "景\n",
225
+ "を\n",
226
+ "完成\n",
227
+ "させて\n",
228
+ "います\n",
229
+ "。\n"
230
+ ]
231
+ }
232
+ ],
233
+ "source": [
234
+ "# Prepare input\n",
235
+ "path = r\"C:\\Users\\showe\\Desktop\\tiikawa.png\"\n",
236
+ "path = r\"C:\\Users\\showe\\Pictures\\1487423322121.jpg\"\n",
237
+ "image = Image.open(path)\n",
238
+ "messages = [{\"role\": \"user\", \"content\": [\n",
239
+ " {\"type\": \"image\"},\n",
240
+ " {\"type\": \"text\", \"text\": \"この画像について説明してください。\"}\n",
241
+ "]}]\n",
242
+ "\n",
243
+ "# Process inputs\n",
244
+ "prompt = processor.apply_chat_template(messages, add_generation_prompt=True)\n",
245
+ "inputs = processor(images=[image], text=prompt, return_tensors=\"pt\")\n",
246
+ "\n",
247
+ "# Convert to numpy with correct dtypes\n",
248
+ "pixel_values = inputs[\"pixel_values\"].numpy().astype(np.float32)\n",
249
+ "pixel_attention_mask = inputs[\"pixel_attention_mask\"].numpy().astype(np.int64)\n",
250
+ "spatial_shapes = inputs[\"spatial_shapes\"].numpy().astype(np.int64)\n",
251
+ "input_ids = inputs[\"input_ids\"].numpy().astype(np.int64)\n",
252
+ "\n",
253
+ "# Get image embeddings\n",
254
+ "image_outputs = embed_images.run(None, {\n",
255
+ " \"pixel_values\": pixel_values,\n",
256
+ " \"pixel_attention_mask\": pixel_attention_mask,\n",
257
+ " \"spatial_shapes\": spatial_shapes,\n",
258
+ "})\n",
259
+ "image_embeds = image_outputs[0]\n",
260
+ "\n",
261
+ "# Get token embeddings\n",
262
+ "token_outputs = embed_tokens.run(None, {\"input_ids\": input_ids})\n",
263
+ "token_embeds = token_outputs[0]\n",
264
+ "\n",
265
+ "# Replace <image> tokens with image embeddings\n",
266
+ "image_token_id = processor.tokenizer.convert_tokens_to_ids(\"<image>\")\n",
267
+ "image_positions = np.where(input_ids[0] == image_token_id)[0]\n",
268
+ "for i, pos in enumerate(image_positions):\n",
269
+ " if i < len(image_embeds):\n",
270
+ " token_embeds[0, pos] = image_embeds[i]\n",
271
+ "\n",
272
+ "# Initialize KV cache for stateful decoding\n",
273
+ "ONNX_DTYPE = {\"tensor(float)\": np.float32, \"tensor(float16)\": np.float16, \"tensor(int64)\": np.int64}\n",
274
+ "cache = {}\n",
275
+ "for inp in decoder.get_inputs():\n",
276
+ " if inp.name in {\"inputs_embeds\", \"attention_mask\", \"position_ids\"}:\n",
277
+ " continue\n",
278
+ " shape = [d if isinstance(d, int) else 1 for d in inp.shape]\n",
279
+ " for i, d in enumerate(inp.shape):\n",
280
+ " if isinstance(d, str) and \"sequence\" in d.lower():\n",
281
+ " shape[i] = 0\n",
282
+ " cache[inp.name] = np.zeros(shape, dtype=ONNX_DTYPE.get(inp.type, np.float32))\n",
283
+ "\n",
284
+ "# Generate tokens\n",
285
+ "seq_len = token_embeds.shape[1]\n",
286
+ "generated_tokens = []\n",
287
+ "\n",
288
+ "for step in range(100): # max tokens\n",
289
+ " if step == 0:\n",
290
+ " embeds = token_embeds.astype(np.float32)\n",
291
+ " else:\n",
292
+ " last_token = np.array([[generated_tokens[-1]]], dtype=np.int64)\n",
293
+ " embeds = embed_tokens.run(None, {\"input_ids\": last_token})[0].astype(np.float32)\n",
294
+ "\n",
295
+ " attn_mask = np.ones((1, seq_len + len(generated_tokens)), dtype=np.int64)\n",
296
+ " feed = {\"inputs_embeds\": embeds, \"attention_mask\": attn_mask, **cache}\n",
297
+ "\n",
298
+ " outputs = decoder.run(None, feed)\n",
299
+ " next_token = int(np.argmax(outputs[0][0, -1]))\n",
300
+ " generated_tokens.append(next_token)\n",
301
+ "\n",
302
+ " # Update cache\n",
303
+ " for i, out in enumerate(decoder.get_outputs()[1:], 1):\n",
304
+ " name = out.name.replace(\"present_conv\", \"past_conv\").replace(\"present.\", \"past_key_values.\")\n",
305
+ " if name in cache:\n",
306
+ " cache[name] = outputs[i]\n",
307
+ "\n",
308
+ " if next_token == processor.tokenizer.eos_token_id:\n",
309
+ " break\n",
310
+ "\n",
311
+ " print(processor.tokenizer.decode(next_token, skip_special_tokens=True))\n",
312
+ "# print(processor.tokenizer.decode(generated_tokens, skip_special_tokens=True))\n"
313
+ ]
314
+ },
315
+ {
316
+ "cell_type": "code",
317
+ "execution_count": 6,
318
+ "id": "9144e5cb",
319
+ "metadata": {},
320
+ "outputs": [
321
+ {
322
+ "name": "stdout",
323
+ "output_type": "stream",
324
+ "text": [
325
+ "この画像は、雪に覆われた斜面で、晴れた日を楽しんでいる人物を描いています。太陽が空を輝かせ、雪に長い影を落としています。背景には、木々や山々が広がり、冬の風景を完成させています。\n"
326
+ ]
327
+ }
328
+ ],
329
+ "source": [
330
+ "print(processor.tokenizer.decode(generated_tokens, skip_special_tokens=True))"
331
+ ]
332
+ },
333
+ {
334
+ "cell_type": "code",
335
+ "execution_count": null,
336
+ "id": "c3864c06",
337
+ "metadata": {},
338
+ "outputs": [],
339
+ "source": [
340
+ "np.array(image)"
341
+ ]
342
+ },
343
+ {
344
+ "cell_type": "code",
345
+ "execution_count": 13,
346
+ "id": "1f9373fd",
347
+ "metadata": {},
348
+ "outputs": [],
349
+ "source": [
350
+ "import numpy as np\n",
351
+ "import onnxruntime as ort\n",
352
+ "from huggingface_hub import hf_hub_download\n",
353
+ "from transformers import AutoTokenizer\n",
354
+ "from IPython.display import display, HTML, clear_output, Markdown\n",
355
+ "\n",
356
+ "# Download model (Q4 recommended)\n",
357
+ "model_id = \"LiquidAI/LFM2.5-1.2B-Instruct-ONNX\"\n",
358
+ "model_path = hf_hub_download(model_id, \"onnx/model_q4.onnx\")\n",
359
+ "\n",
360
+ "# Download all data files (handles multiple splits for large models)\n",
361
+ "from huggingface_hub import list_repo_files\n",
362
+ "for f in list_repo_files(model_id):\n",
363
+ " if f.startswith(\"onnx/model_q4.onnx_data\"):\n",
364
+ " hf_hub_download(model_id, f)\n",
365
+ "\n",
366
+ "# Load model and tokenizer\n",
367
+ "session = ort.InferenceSession(model_path)\n",
368
+ "tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)\n",
369
+ "\n"
370
+ ]
371
+ },
372
+ {
373
+ "cell_type": "code",
374
+ "execution_count": 28,
375
+ "id": "2b426e38",
376
+ "metadata": {},
377
+ "outputs": [
378
+ {
379
+ "data": {
380
+ "text/markdown": [
381
+ "<translation>会議やインタビュー、録音を検索可能なテキストに自動変換します。Nottaで効率的に作業できます</translation>"
382
+ ],
383
+ "text/plain": [
384
+ "<IPython.core.display.Markdown object>"
385
+ ]
386
+ },
387
+ "execution_count": 28,
388
+ "metadata": {},
389
+ "output_type": "execute_result"
390
+ }
391
+ ],
392
+ "source": [
393
+ "content = '''\n",
394
+ "以下の文章を流暢な日本語に翻訳してください。\n",
395
+ "出力は<translate>タグで囲んでください。\n",
396
+ "タグ以外の出力は絶対やめてください。\n",
397
+ "\n",
398
+ "【例】\n",
399
+ "<translate>this is translated text</translate>\n",
400
+ "\n",
401
+ "【翻訳前文章】\n",
402
+ "Automatically convert your meetings, interviews, or recordings into searchable text with Notta. Transcribe, edit, summarize, and collaborate all in a single workflow to stay productive.\n",
403
+ "'''\n",
404
+ "\n",
405
+ "# Prepare chat input\n",
406
+ "\n",
407
+ "messages = [{\"role\": \"user\", \"content\": content}]\n",
408
+ "prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
409
+ "input_ids = np.array([tokenizer.encode(prompt, add_special_tokens=False)], dtype=np.int64)\n",
410
+ "\n",
411
+ "# Initialize KV cache\n",
412
+ "ONNX_DTYPE = {\"tensor(float)\": np.float32, \"tensor(float16)\": np.float16, \"tensor(int64)\": np.int64}\n",
413
+ "cache = {}\n",
414
+ "for inp in session.get_inputs():\n",
415
+ " if inp.name in {\"input_ids\", \"attention_mask\", \"position_ids\"}:\n",
416
+ " continue\n",
417
+ " shape = [d if isinstance(d, int) else 1 for d in inp.shape]\n",
418
+ " for i, d in enumerate(inp.shape):\n",
419
+ " if isinstance(d, str) and \"sequence\" in d.lower():\n",
420
+ " shape[i] = 0\n",
421
+ " cache[inp.name] = np.zeros(shape, dtype=ONNX_DTYPE.get(inp.type, np.float32))\n",
422
+ "\n",
423
+ "# Check if model uses position_ids\n",
424
+ "input_names = {inp.name for inp in session.get_inputs()}\n",
425
+ "use_position_ids = \"position_ids\" in input_names\n",
426
+ "\n",
427
+ "# Generate tokens\n",
428
+ "seq_len = input_ids.shape[1]\n",
429
+ "generated_tokens = []\n",
430
+ "\n",
431
+ "for step in range(512): # max tokens\n",
432
+ " if step == 0:\n",
433
+ " ids = input_ids\n",
434
+ " pos = np.arange(seq_len, dtype=np.int64).reshape(1, -1)\n",
435
+ " else:\n",
436
+ " ids = np.array([[generated_tokens[-1]]], dtype=np.int64)\n",
437
+ " pos = np.array([[seq_len + len(generated_tokens) - 1]], dtype=np.int64)\n",
438
+ "\n",
439
+ " attn_mask = np.ones((1, seq_len + len(generated_tokens)), dtype=np.int64)\n",
440
+ " feed = {\"input_ids\": ids, \"attention_mask\": attn_mask, **cache}\n",
441
+ " if use_position_ids:\n",
442
+ " feed[\"position_ids\"] = pos\n",
443
+ "\n",
444
+ " outputs = session.run(None, feed)\n",
445
+ " next_token = int(np.argmax(outputs[0][0, -1]))\n",
446
+ " generated_tokens.append(next_token)\n",
447
+ "\n",
448
+ " # Update cache\n",
449
+ " for i, out in enumerate(session.get_outputs()[1:], 1):\n",
450
+ " name = out.name.replace(\"present_conv\", \"past_conv\").replace(\"present.\", \"past_key_values.\")\n",
451
+ " if name in cache:\n",
452
+ " cache[name] = outputs[i]\n",
453
+ "\n",
454
+ " token_str = tokenizer.decode([next_token], skip_special_tokens=True)\n",
455
+ " print(token_str, end=\"\", flush=True)\n",
456
+ "\n",
457
+ " if next_token == tokenizer.eos_token_id:\n",
458
+ " break\n",
459
+ "\n",
460
+ "clear_output()\n",
461
+ "Markdown(tokenizer.decode(generated_tokens, skip_special_tokens=True))"
462
+ ]
463
+ },
464
+ {
465
+ "cell_type": "code",
466
+ "execution_count": 25,
467
+ "id": "1186c8f5",
468
+ "metadata": {},
469
+ "outputs": [
470
+ {
471
+ "data": {
472
+ "text/plain": [
473
+ "'<translate>LFM2.5は、乗算ゲートと短い畳み込みを組み合わせたハイブリッドアーキテクチャで、CPU、GPU、NPUハードウェアでの高速推論に最適化されています</translate>'"
474
+ ]
475
+ },
476
+ "execution_count": 25,
477
+ "metadata": {},
478
+ "output_type": "execute_result"
479
+ }
480
+ ],
481
+ "source": [
482
+ "tokenizer.decode(generated_tokens, skip_special_tokens=True)"
483
+ ]
484
+ },
485
+ {
486
+ "cell_type": "code",
487
+ "execution_count": null,
488
+ "id": "ddc378d4",
489
+ "metadata": {},
490
+ "outputs": [],
491
+ "source": []
492
+ }
493
+ ],
494
+ "metadata": {
495
+ "kernelspec": {
496
+ "display_name": "venv",
497
+ "language": "python",
498
+ "name": "python3"
499
+ },
500
+ "language_info": {
501
+ "codemirror_mode": {
502
+ "name": "ipython",
503
+ "version": 3
504
+ },
505
+ "file_extension": ".py",
506
+ "mimetype": "text/x-python",
507
+ "name": "python",
508
+ "nbconvert_exporter": "python",
509
+ "pygments_lexer": "ipython3",
510
+ "version": "3.10.11"
511
+ }
512
+ },
513
+ "nbformat": 4,
514
+ "nbformat_minor": 5
515
+ }