File size: 24,925 Bytes
db5855f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ef2ed242-3561-464c-8d1c-cc3862e23702",
   "metadata": {},
   "source": [
    "# Text Generation via Speculative Sampling, KV Caching, and OpenVINO™\n",
    "\n",
    "As model sizes grow, Generative AI implementations require significant inference resources. This not only increases the cost per generation from a prompt, but also increases the power consumption used to serve such requests.\n",
    "\n",
    "Inference optimizations for text generation are essential for reducing costs and power consumption. When optimizing the inference process, the amount of time and energy required to generate text can be significantly reduced. This can lead to cost savings in terms of hardware and software, as well as reduced power consumption. Additionally, inference optimizations can help improve the accuracy of text generation as well as the speed at which it can be generated. This can lead to an improved user experience and increased efficiency in text-generation tasks. In summary, inference optimizations for text generation are essential to reduce costs and power consumption, while also improving the accuracy and speed of text generation.\n",
    "\n",
    "Another necessary condition is that the optimizations are compatible with each other. That is, implementing a certain optimization should not preclude other optimizations. There are several levels of optimizations that can provide significant speedup without \"bumping into each other\" in a way that will compromise overall efficiency.\n",
    "\n",
    "For details on this method, please refer to the paper by Chen et al, http://arxiv.org/abs/2302.01318. Additionally, there's an interesting proof of correctness of speculative sampling (showing that the original distribution is preserved) by Leviathan et al, http://arxiv.org/abs/2211.17192\n",
    "\n",
    "Our blog article describing this implementation with OpenVino is available at openvino.ai"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f97c435a",
   "metadata": {},
   "source": [
    "#### Table of contents:\n",
    "\n",
    "- [Prerequisites](#Prerequisites)\n",
    "    - [Select inference device](#Select-inference-device)\n",
    "- [Create autoregressive and speculative forms of sampling with KV Cache support](#Create-autoregressive-and-speculative-forms-of-sampling-with-KV-Cache-support)\n",
    "    - [Setup imports](#Setup-imports)\n",
    "    - [Prepare autoregressive sampling](#Prepare-autoregressive-sampling)\n",
    "    - [Prepare speculative sampling](#Prepare-speculative-sampling)\n",
    "- [Main generation function](#Main-generation-function)\n",
    "    - [Download and Convert Model](#Download-and-Convert-Model)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "08aa16b1-d2f6-4a3a-abfb-5ec278133c80",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "\n",
    "First, we should install the [Hugging Face Optimum](https://huggingface.co/docs/optimum/installation) library accelerated by OpenVINO integration.\n",
    "The Hugging Face Optimum Intel API is a high-level API that enables us to convert and quantize models from the Hugging Face Transformers library to the OpenVINO™ IR format. For more details, refer to the [Hugging Face Optimum Intel documentation](https://huggingface.co/docs/optimum/intel/inference).\n",
    "\n",
    "We will also need to install transformers (HuggingFace) and some other useful modules."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4421fc85-bed6-4a62-b8fa-19c7ba474891",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%pip install -Uq pip\n",
    "%pip uninstall -q -y optimum optimum-intel\n",
    "%pip install --pre -Uq openvino openvino-tokenizers[transformers] --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly\n",
    "%pip install -q --upgrade transformers \"torch>=2.1\" \"gradio>=4.19\" accelerate onnx ipywidgets \"peft==0.6.2\" --extra-index-url https://download.pytorch.org/whl/cpu\n",
    "%pip install -q \"git+https://github.com/huggingface/optimum-intel.git\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "367f84f8-33e8-4ad6-bd40-e6fd41d2d703",
   "metadata": {},
   "source": [
    "### Select inference device\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "\n",
    "Select the device from dropdown list for running inference using OpenVINO."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6ddd57de-9f41-403c-bccc-8d3118654a24",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "271cc0d2e97b494cb57df890114cb682",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Device:', options=('CPU', 'GPU.0', 'GPU.1', 'AUTO'), value='CPU')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import ipywidgets as widgets\n",
    "import openvino as ov\n",
    "\n",
    "core = ov.Core()\n",
    "\n",
    "device = widgets.Dropdown(\n",
    "    options=core.available_devices + [\"AUTO\"],\n",
    "    value=\"CPU\",\n",
    "    description=\"Device:\",\n",
    "    disabled=False,\n",
    ")\n",
    "\n",
    "device"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b6d9c4a5-ef75-4076-9f1c-f45a2259ec46",
   "metadata": {},
   "source": [
    "## Create autoregressive and speculative forms of sampling with KV Cache support\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    " \n",
    "Text generation is often done in an autoregressive fashion. We will all support a KV cache (aka Past Value Cache) in the code. Note that we are using greedy sampling. We do not adjust other text generation parameters (e.g. temperature) so keep this illustration of speculative sampling as simple and understandable as possible."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b9b5da4d-d2fd-440b-b204-7fbc6966dd1f",
   "metadata": {},
   "source": [
    "### Setup imports\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6f976094-8603-42c4-8f18-a32ba6d7192e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "import gradio as gr"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c58611d6-0a91-4efd-976e-4221acbb43cd",
   "metadata": {},
   "source": [
    "### Prepare autoregressive sampling\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "52ac10a5-3141-4227-8f0b-0617acd027c8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def autoregressive_sampling_with_pkv(input, model, N=30):\n",
    "    input_ids, attention_mask = input.input_ids, input.attention_mask\n",
    "    seq_len = input_ids.shape[-1]\n",
    "    position_ids = np.arange(0, seq_len, dtype=np.int64).reshape([-1, seq_len])\n",
    "\n",
    "    # in all subsequent inferences we feed tokens one by one,\n",
    "    # but for the first one we feed the whole encoded prompt\n",
    "    request = model.create_infer_request()\n",
    "    request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "    next_token = np.argmax(request.results[\"logits\"][:, -1]).reshape([1])\n",
    "\n",
    "    all_tokens = []\n",
    "    all_tokens.extend(input_ids[0])\n",
    "    all_tokens.append(next_token[0])\n",
    "\n",
    "    while seq_len < N:\n",
    "        input_ids = next_token.reshape([1, 1])\n",
    "        attention_mask = np.concatenate((attention_mask, np.array([1]).reshape([1, 1])), axis=1)\n",
    "        position_ids = np.array([attention_mask.shape[1]]).reshape([1, 1])\n",
    "\n",
    "        request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "        next_token = np.argmax(request.results[\"logits\"][:, -1])\n",
    "        all_tokens.append(next_token)\n",
    "        seq_len += 1\n",
    "\n",
    "    return all_tokens"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "27a01739-1363-42ef-927f-6a340bdbe7ba",
   "metadata": {},
   "source": [
    "### Prepare speculative sampling\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "* Step 1: With speculative sampling, we first generate K samples from the draft model (in an autoregressive manner).\n",
    "* Step 2: These are now candidates to examine using the main model (step 2) using a batch size of K.\n",
    "* Step 3: We go through each K predicted tokens, and if tokens differ, we stop and keep the last token predicted by the main model.\n",
    "* Step 4: We update KV-cache dropping keys & values for differing tokens and repeat Step 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9fde1b3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_state(request, seq_len):\n",
    "    for state in request.query_state():\n",
    "        old_seq_len = state.state.shape[2]\n",
    "        if seq_len >= old_seq_len:\n",
    "            continue\n",
    "        # After the inference request, key/values have shape [BATCH_SIZE, seq_len + K, vocab_size].\n",
    "        # Increment the sequence length by the number of matched tokens, and\n",
    "        # trim the KV cache to match the new sequence length.\n",
    "        state.state = ov.Tensor(state.state.data[:, :, :seq_len])\n",
    "\n",
    "\n",
    "def speculative_sampling_with_pkv(input, draft_model, main_model, K, N=30, **kwargs):\n",
    "    input_ids, attention_mask = input.input_ids, input.attention_mask\n",
    "    # seq_len number of key/values or number of already processed input tokens\n",
    "    seq_len = input_ids.shape[-1]\n",
    "    position_ids = np.arange(0, seq_len, dtype=np.int64).reshape([-1, seq_len])\n",
    "\n",
    "    draft_request = draft_model.create_infer_request()\n",
    "    draft_request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "\n",
    "    main_request = main_model.create_infer_request()\n",
    "    main_request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "    first_token = np.argmax(main_request.results[\"logits\"][:, -1]).reshape([1])\n",
    "\n",
    "    all_tokens = []\n",
    "    all_tokens.extend(input_ids[0])\n",
    "    all_tokens.append(first_token[0])\n",
    "\n",
    "    accum_draft_tokens = []\n",
    "    while seq_len < N:\n",
    "        next_token = first_token\n",
    "        for i in range(K):\n",
    "            input_ids = next_token.reshape([1, 1])\n",
    "            attention_mask = np.concatenate((attention_mask, np.array([1]).reshape([1, 1])), axis=1)\n",
    "            position_ids = np.array([attention_mask.shape[1]]).reshape([1, 1])\n",
    "\n",
    "            draft_request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "            next_token = np.argmax(draft_request.results[\"logits\"][:, -1])\n",
    "            accum_draft_tokens.append(next_token)\n",
    "\n",
    "        # main model will give also K out tokens\n",
    "        # feed the same first token to the main model and do not give the last token generated by the draft\n",
    "        input_ids = np.concatenate((first_token.reshape([1]), accum_draft_tokens[:-1])).reshape([1, -1])\n",
    "        attention_mask = np.ones((1, seq_len + K))\n",
    "        position_ids = np.arange(seq_len, seq_len + K, dtype=np.int64).reshape([1, -1])\n",
    "\n",
    "        main_request.infer((input_ids, attention_mask, position_ids, np.array([0])))\n",
    "        next_tokens = np.argmax(main_request.results[\"logits\"], axis=-1)[0]\n",
    "\n",
    "        # if disagrees from the very beggining then context will be expanded only for one element\n",
    "        # all elements match then context will be expanded to K elements\n",
    "        for disagree_idx, (t1, t2) in enumerate(zip(accum_draft_tokens, next_tokens)):\n",
    "            if t1 != t2:\n",
    "                break\n",
    "\n",
    "        first_token = next_tokens[disagree_idx]\n",
    "        all_tokens.extend(next_tokens[: disagree_idx + 1])\n",
    "        seq_len += disagree_idx + 1\n",
    "\n",
    "        # cut key/values depending on the position where disagreement starts\n",
    "        update_state(draft_request, seq_len)\n",
    "        update_state(main_request, seq_len)\n",
    "\n",
    "        attention_mask = np.ones((1, seq_len))\n",
    "        accum_draft_tokens = []\n",
    "    all_tokens.extend(accum_draft_tokens)\n",
    "    return all_tokens"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "583202d2-6d29-4729-af2e-232d3ee0bc2c",
   "metadata": {},
   "source": [
    "## Main generation function\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d6993840-e8b2-4c26-8da4-e1b046c3f3cc",
   "metadata": {},
   "source": [
    "### Download and Convert Model\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "Optimum Intel can be used to load optimized models from the [Hugging Face Hub](https://huggingface.co/docs/optimum/intel/hf.co/models) and create pipelines to run an inference with OpenVINO Runtime using Hugging Face APIs. For speculative decoding we need to manually update states, therefore we will use directly openvino inference api, and optimum only for model conversion.\n",
    ">To download Llama-2-7b-chat-hf, you will need to accept license agreement. You must be a registered user in 🤗 Hugging Face Hub. Please visit HuggingFace model [card](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf), carefully read terms of usage and click accept button. You will need to use an access token for the code below to run. For more information on access tokens, refer to this section of the documentation.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "82596e66",
   "metadata": {
    "test_replace": {
     "main_model_id = \"meta-llama/Llama-2-7b-chat-hf\"": "main_model_id =\"TinyLlama/TinyLlama-1.1B-Chat-v1.0\""
    }
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "main_model_id = \"meta-llama/Llama-2-7b-chat-hf\"\n",
    "main_model_path = Path(\"Llama-2-7b-chat-hf\")\n",
    "draft_model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "draft_model_path = Path(\"TinyLlama-1.1B-Chat-v1.0\")\n",
    "\n",
    "from transformers import AutoTokenizer\n",
    "\n",
    "main_tokenizer = AutoTokenizer.from_pretrained(main_model_id)\n",
    "draft_tokenizer = AutoTokenizer.from_pretrained(draft_model_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "995dc418",
   "metadata": {},
   "outputs": [],
   "source": [
    "# In order for speculative sampling to work, both main and draft tokenizers should be the same.\n",
    "token_test_txt = \"text to ensure tokenizers work the same, as of 2024\"\n",
    "tokens_1 = draft_tokenizer(token_test_txt, return_tensors=\"pt\").input_ids\n",
    "tokens_2 = main_tokenizer(token_test_txt, return_tensors=\"pt\").input_ids\n",
    "\n",
    "assert all((tokens_1 - tokens_2)[0] == 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "78113302",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not main_model_path.exists():\n",
    "    !optimum-cli export openvino --model $main_model_id --weight-format fp16 $main_model_path\n",
    "if not draft_model_path.exists():\n",
    "    !optimum-cli export openvino --model $draft_model_id --weight-format fp16 $draft_model_path"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "824a2f25",
   "metadata": {},
   "source": [
    "Infer directly using OpenVINO Inference Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "67fb4f9d-5877-48d8-8eff-c30ff6974d7a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "core = ov.Core()\n",
    "draft_ov_model = core.read_model(draft_model_path / \"openvino_model.xml\")\n",
    "draft_model = core.compile_model(draft_ov_model, device_name=\"CPU\")\n",
    "\n",
    "main_ov_model = core.read_model(main_model_path / \"openvino_model.xml\")\n",
    "main_model = core.compile_model(main_ov_model, device_name=\"CPU\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4469807a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main(\n",
    "    prompt: str,\n",
    "    n_tokens_to_generate: int = 75,\n",
    "    K: int = 5,\n",
    "    seed: int = 5555,\n",
    "):\n",
    "    # seed numpy rng\n",
    "    np.random.seed(seed)\n",
    "    tokenized = main_tokenizer(prompt, return_tensors=\"pt\")\n",
    "\n",
    "    def run_autoregressive_sampling_fn(decode_fn, tokenized, **kwargs):\n",
    "        start = time.perf_counter()\n",
    "        output_ids = decode_fn(tokenized, **kwargs)\n",
    "        text = main_tokenizer.decode(output_ids, skip_special_tokens=True)\n",
    "        elapsed_time = time.perf_counter() - start\n",
    "        return text, elapsed_time\n",
    "\n",
    "    def run_speculative_sampling_fn(decode_fn, input_ids, **kwargs):\n",
    "        start = time.perf_counter()\n",
    "        output_ids = decode_fn(input_ids, **kwargs)\n",
    "        text = main_tokenizer.decode(output_ids, skip_special_tokens=True)\n",
    "        elapsed_time = time.perf_counter() - start\n",
    "        return text, elapsed_time\n",
    "\n",
    "    autoregressive_text, autoregressive_time = run_autoregressive_sampling_fn(\n",
    "        autoregressive_sampling_with_pkv,\n",
    "        tokenized,\n",
    "        model=main_model,\n",
    "        N=n_tokens_to_generate,\n",
    "    )\n",
    "\n",
    "    speculative_text, speculative_time = run_speculative_sampling_fn(\n",
    "        speculative_sampling_with_pkv,\n",
    "        tokenized,\n",
    "        main_model=main_model,\n",
    "        draft_model=draft_model,\n",
    "        N=n_tokens_to_generate,\n",
    "        K=K,\n",
    "    )\n",
    "\n",
    "    # Format results for output in gradio\n",
    "    out = \"\\n\" + \"Autoregressive Decode\" + \"\\n\" + \"---------------------\" + \"\\n\"\n",
    "    out = out + f\"Time = {autoregressive_time:.2f}s\" + \"\\n\" + f\"Text = {autoregressive_text}\" + \"\\n\"\n",
    "    out = out + \"\\n\" + \"Speculative Decode\" + \"\\n\" + \"------------------\" + \"\\n\"\n",
    "    out = out + f\"Time = {speculative_time:.2f}s\" + \"\\n\" + f\"Text = {speculative_text}\"\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "18eb331b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-04-17 10:21:41.642283: I tensorflow/core/util/port.cc:111] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
      "2024-04-17 10:21:41.644834: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n",
      "2024-04-17 10:21:41.677055: E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:9342] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
      "2024-04-17 10:21:41.677093: E tensorflow/compiler/xla/stream_executor/cuda/cuda_fft.cc:609] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
      "2024-04-17 10:21:41.677119: E tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:1518] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
      "2024-04-17 10:21:41.683198: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n",
      "2024-04-17 10:21:41.683977: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
      "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2024-04-17 10:21:42.477656: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Autoregressive Decode\n",
      "---------------------\n",
      "Time = 44.39s\n",
      "Text = Alan Turing was a British mathematician, computer scientist, and codebreaker who played a pivotal role in cracking the German Enigma code during World War II. He was also a pioneer in the field of artificial intelligence and made significant contributions to the development of computer science.\n",
      "\n",
      "Turing was born on June 23, 1912, in London, England. He was educated at Cambridge University, where he earned a degree in mathematics in \n",
      "\n",
      "Speculative Decode\n",
      "------------------\n",
      "Time = 22.96s\n",
      "Text = Alan Turing was a British mathematician, computer scientist, and codebreaker who played a pivotal role in cracking the German Enigma code during World War II. He was also a pioneer in the field of artificial intelligence and made significant contributions to the development of computer science.\n",
      "\n",
      "Turing was born on June 23, 1912, in London, England. He was educated at Cambridge University, where he earned a degree in mathematics in 1\n"
     ]
    }
   ],
   "source": [
    "res = main(\"Alan Turing was a\", n_tokens_to_generate=100)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "757d1d26",
   "metadata": {},
   "outputs": [],
   "source": [
    "with gr.Blocks() as demo:\n",
    "    gr.Markdown(\n",
    "        f\"\"\"\n",

    "        # Speculative Sampling Demo\n",

    "        ## The output will show a comparison of Autoregressive Sampling vs Speculative Sampling\n",

    "        - Main Model: {main_model_id}\n",

    "        - Draft Model: {draft_model_id}\n",

    "        - K = 5\n",

    "        \"\"\"\n",
    "    )\n",
    "    with gr.Row():\n",
    "        inp = gr.Textbox(\n",
    "            \"Alan Turing was a\",\n",
    "            placeholder=\"THIS CANNOT BE EMPTY\",\n",
    "            label=\"Input Prompt\",\n",
    "        )\n",
    "        out = gr.Textbox(label=\"Output\")\n",
    "    btn = gr.Button(\"Run\")\n",
    "    btn.click(fn=main, inputs=inp, outputs=out)\n",
    "\n",
    "demo.launch()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "openvino_notebooks": {
   "imageUrl": "",
   "tags": {
    "categories": [
     "Model Demos"
    ],
    "libraries": [],
    "other": [
     "LLM"
    ],
    "tasks": [
     "Text Generation"
    ]
   }
  },
  "vscode": {
   "interpreter": {
    "hash": "cec18e25feb9469b5ff1085a8097bdcd86db6a4ac301d6aeff87d0f3e7ce4ca5"
   }
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}