|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stderr", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "/home/shubham/anaconda3/envs/codex/lib/python3.11/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", |
| 13 | + " from .autonotebook import tqdm as notebook_tqdm\n", |
| 14 | + "Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 3.26it/s]\n" |
| 15 | + ] |
| 16 | + } |
| 17 | + ], |
| 18 | + "source": [ |
| 19 | + "from syncode.grammar_decoder import SyncodeLogitsProcessor\n", |
| 20 | + "from syncode.parsers.grammars import Grammar\n", |
| 21 | + "import torch\n", |
| 22 | + "from transformers import AutoModelForCausalLM\n", |
| 23 | + "from transformers import AutoTokenizer\n", |
| 24 | + "\n", |
| 25 | + "# Step 1. Load model and tokenizer\n", |
| 26 | + "model_name = \"microsoft/phi-2\"\n", |
| 27 | + "model = AutoModelForCausalLM.from_pretrained(model_name)\n", |
| 28 | + "tokenizer = AutoTokenizer.from_pretrained(model_name)" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stderr", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "name": "stdout", |
| 45 | + "output_type": "stream", |
| 46 | + "text": [ |
| 47 | + "Generated token id: 25 (:)\n", |
| 48 | + "Score of this token: 28.345928192138672\n", |
| 49 | + "Colon token id: 25 (:)\n", |
| 50 | + "Score of colon token: 28.345928192138672\n" |
| 51 | + ] |
| 52 | + } |
| 53 | + ], |
| 54 | + "source": [ |
| 55 | + "# Step 2. Set prompt\n", |
| 56 | + "prompt = \"\"\"def is_palindrome(n):\n", |
| 57 | + " if str(n) == str(n)[::-1]:\n", |
| 58 | + " return True\n", |
| 59 | + " else\"\"\"\n", |
| 60 | + "\n", |
| 61 | + "# Step 3. Initialize SyncodeLogitsProcessor\n", |
| 62 | + "syncode_processor = SyncodeLogitsProcessor(grammar=Grammar(\"python\"),\n", |
| 63 | + " tokenizer=tokenizer,\n", |
| 64 | + " parse_output_only=False)\n", |
| 65 | + "syncode_processor.reset(prompt)\n", |
| 66 | + "\n", |
| 67 | + "# Step 4. Generate scores with SyncodeLogitsProcessor\n", |
| 68 | + "inputs = tokenizer(prompt, return_tensors=\"pt\").input_ids\n", |
| 69 | + "outputs = model.generate(inputs,\n", |
| 70 | + " attention_mask=torch.ones_like(inputs),\n", |
| 71 | + " do_sample=True,\n", |
| 72 | + " logits_processor=[syncode_processor],\n", |
| 73 | + " return_dict_in_generate=True,\n", |
| 74 | + " output_scores=True,\n", |
| 75 | + " max_new_tokens=1)\n", |
| 76 | + "\n", |
| 77 | + "# Step 5. Print scores\n", |
| 78 | + "scores = outputs.scores\n", |
| 79 | + "generated_id = outputs.sequences[0, -1]\n", |
| 80 | + "generated_str = tokenizer.decode(generated_id, skip_special_tokens=True)\n", |
| 81 | + "print(f\"Generated token id: {generated_id} ({generated_str})\")\n", |
| 82 | + "print(f\"Score of this token: {scores[0][-1][generated_id]}\")\n", |
| 83 | + "\n", |
| 84 | + "colon_id = tokenizer.encode(':')[0]\n", |
| 85 | + "colon_str = tokenizer.decode(colon_id)\n", |
| 86 | + "print(f\"Colon token id: {colon_id} ({colon_str})\")\n", |
| 87 | + "print(f\"Score of colon token: {scores[0][-1][colon_id]}\")" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "metadata": { |
| 92 | + "kernelspec": { |
| 93 | + "display_name": "codex", |
| 94 | + "language": "python", |
| 95 | + "name": "python3" |
| 96 | + }, |
| 97 | + "language_info": { |
| 98 | + "codemirror_mode": { |
| 99 | + "name": "ipython", |
| 100 | + "version": 3 |
| 101 | + }, |
| 102 | + "file_extension": ".py", |
| 103 | + "mimetype": "text/x-python", |
| 104 | + "name": "python", |
| 105 | + "nbconvert_exporter": "python", |
| 106 | + "pygments_lexer": "ipython3", |
| 107 | + "version": "3.11.4" |
| 108 | + } |
| 109 | + }, |
| 110 | + "nbformat": 4, |
| 111 | + "nbformat_minor": 2 |
| 112 | +} |
0 commit comments