-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes to parser and explicitly print parsing error
- Loading branch information
1 parent
a034a36
commit a88b744
Showing
8 changed files
with
178 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"/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", | ||
" from .autonotebook import tqdm as notebook_tqdm\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import torch\n", | ||
"from syncode import SyncodeLogitsProcessor\n", | ||
"from syncode import Grammar\n", | ||
"from transformers import AutoModelForCausalLM, AutoTokenizer\n", | ||
"import os\n", | ||
"\n", | ||
"HF_CACHE = os.environ['HF_CACHE'] if 'HF_CACHE' in os.environ else 'cache/'\n", | ||
"HF_ACCESS_TOKEN = os.environ['HF_ACCESS_TOKEN'] if 'HF_ACCESS_TOKEN' in os.environ else None\n", | ||
"\n", | ||
"device = 'cuda'\n", | ||
"model_name = \"meta-llama/Llama-3.2-1B-Instruct\"\n", | ||
"# model_name = \"meta-llama/Llama-3.1-8B-Instruct\"\n", | ||
"\n", | ||
"model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, cache_dir=HF_CACHE, token=HF_ACCESS_TOKEN, trust_remote_code=True).eval().to(device)\n", | ||
"tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=HF_CACHE, token=HF_ACCESS_TOKEN, trust_remote_code=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[PROMPT] <|begin_of_text|><|start_header_id|>system<|end_header_id|>\n", | ||
"\n", | ||
"Cutting Knowledge Date: December 2023\n", | ||
"Today Date: 26 Jul 2024\n", | ||
"\n", | ||
"<|eot_id|><|start_header_id|>user<|end_header_id|>\n", | ||
"\n", | ||
"Write a java function that prints 'hello world' in reverse.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", | ||
"\n", | ||
" \n", | ||
"\n", | ||
"[OUTPUT] public class HelloWorld {\n", | ||
" public static void main(String[] args) {\n", | ||
" System.out.println(\"Hello World\");\n", | ||
" } \n", | ||
"\n", | ||
" public static void printReverse(String str) {\n", | ||
" char[] arr = str.toCharArray();\n", | ||
" int start = 0;\n", | ||
" int end = arr.length - 1;\n", | ||
"\n", | ||
" while (start < end) {\n", | ||
" System.out.print(arr[start]);\n", | ||
" System.out.print(arr[end]);\n", | ||
" start++;\n", | ||
" end--;\n", | ||
" } \n", | ||
" System.out.println();\n", | ||
" } \n", | ||
"}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# grammar_str = \"python\"\n", | ||
"# grammar_str = \"go\"\n", | ||
"grammar_str = \"java\"\n", | ||
"\n", | ||
"grammar = Grammar(grammar_str)\n", | ||
"syncode_logits_processor = SyncodeLogitsProcessor(grammar=grammar, tokenizer=tokenizer, parse_output_only=True)\n", | ||
"\n", | ||
"prompt = f\"Write a {grammar_str} function that prints 'hello world' in reverse.\"\n", | ||
"messages = [{\"role\": \"user\", \"content\": prompt}]\n", | ||
"prompt = tokenizer.apply_chat_template(\n", | ||
" messages, tokenize=False, add_generation_prompt=True\n", | ||
" )\n", | ||
"print(\"[PROMPT]\", prompt, \"\\n\")\n", | ||
"\n", | ||
"syncode_logits_processor.reset(prompt)\n", | ||
"\n", | ||
"inputs = tokenizer(prompt, return_tensors='pt').input_ids.to(device)\n", | ||
"\n", | ||
"attention_mask = torch.ones_like(inputs)\n", | ||
"output = model.generate(\n", | ||
" inputs,\n", | ||
" attention_mask=attention_mask,\n", | ||
" max_length=512, \n", | ||
" num_return_sequences=1, \n", | ||
" pad_token_id=tokenizer.eos_token_id, \n", | ||
" logits_processor=[syncode_logits_processor]\n", | ||
" )\n", | ||
"output_str = tokenizer.decode(output[0][len(inputs[0]):], skip_special_tokens=True)\n", | ||
"print(\"[OUTPUT]\", output_str)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "codex", | ||
"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" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.