-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from kinfey/main
Update Phi-3.5 Content
- Loading branch information
Showing
24 changed files
with
1,757 additions
and
0 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions
134
code/09.UpdateSamples/Aug/intel-phi35-instruct-zh.ipynb
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,134 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Lab-1-Introduce Phi-3.5 Instruct" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Convert Phi-3-mini-4k-instruct model to INT4 with optimum-cli" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pathlib import Path\n", | ||
"\n", | ||
"llm_model_id = \"microsoft/Phi-3.5-mini-instruct\"\n", | ||
"llm_model_path = \"../model/phi-3.5-mini-instruct-ov\"\n", | ||
"\n", | ||
"if not Path(llm_model_path).exists():\n", | ||
" !optimum-cli export openvino --model {llm_model_id} --task text-generation-with-past --weight-format int4 --group-size 128 --ratio 0.6 --sym --trust-remote-code {llm_model_path}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from transformers import AutoConfig, AutoTokenizer\n", | ||
"from optimum.intel.openvino import OVModelForCausalLM" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ov_config = {\"PERFORMANCE_HINT\": \"LATENCY\", \"NUM_STREAMS\": \"1\", \"CACHE_DIR\": \"\"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ov_model = OVModelForCausalLM.from_pretrained(\n", | ||
" llm_model_path,\n", | ||
" device='GPU',\n", | ||
" ov_config=ov_config,\n", | ||
" config=AutoConfig.from_pretrained(llm_model_path, trust_remote_code=True),\n", | ||
" trust_remote_code=True,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tok = AutoTokenizer.from_pretrained(llm_model_path, trust_remote_code=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tokenizer_kwargs = {\"add_special_tokens\": False}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"prompt = \"<|user|>\\n你了解 .NET 吗?\\n<|end|><|assistant|>\\n\"\n", | ||
"input_tokens = tok(prompt, return_tensors=\"pt\", **tokenizer_kwargs)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"answer = ov_model.generate(**input_tokens, max_new_tokens=1024)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tok.batch_decode(answer, skip_special_tokens=True)[0]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "openvinodev", | ||
"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.8" | ||
} | ||
}, | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Lab-2-Introduce Phi-3.5 Vision" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"from pathlib import Path" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if not Path(\"ov_phi3_vision.py\").exists():\n", | ||
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/phi-3-vision/ov_phi3_vision.py\")\n", | ||
" open(\"ov_phi3_vision.py\", \"w\").write(r.text)\n", | ||
"\n", | ||
"\n", | ||
"if not Path(\"gradio_helper.py\").exists():\n", | ||
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/phi-3-vision/gradio_helper.py\")\n", | ||
" open(\"gradio_helper.py\", \"w\").write(r.text)\n", | ||
"\n", | ||
"if not Path(\"notebook_utils.py\").exists():\n", | ||
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py\")\n", | ||
" open(\"notebook_utils.py\", \"w\").write(r.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from ov_phi3_vision import convert_phi3_model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pathlib import Path\n", | ||
"import nncf\n", | ||
"\n", | ||
"\n", | ||
"model_id = \"microsoft/Phi-3.5-vision-instruct\"\n", | ||
"out_dir = Path(\"../model/phi-3.5-vision-128k-instruct-ov\")\n", | ||
"compression_configuration = {\n", | ||
" \"mode\": nncf.CompressWeightsMode.INT4_SYM,\n", | ||
" \"group_size\": 64,\n", | ||
" \"ratio\": 0.6,\n", | ||
"}\n", | ||
"if not out_dir.exists():\n", | ||
" convert_phi3_model(model_id, out_dir, compression_configuration)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from notebook_utils import device_widget\n", | ||
"\n", | ||
"device = device_widget(default=\"GPU\", exclude=[\"NPU\"])\n", | ||
"\n", | ||
"device" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from ov_phi3_vision import OvPhi3Vision" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"out_dir" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = OvPhi3Vision(out_dir, device.value)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"from PIL import Image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"image = Image.open(r\"./imgs/demo.png\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from transformers import AutoProcessor, TextStreamer\n", | ||
"\n", | ||
"messages = [\n", | ||
" {\"role\": \"user\", \"content\": \"<|image_1|>\\nPlease create Python code for image, and use plt to save the new picture under imgs/ and name it phi-3-vision.jpg.\"},\n", | ||
"]\n", | ||
"\n", | ||
"processor = AutoProcessor.from_pretrained(out_dir, trust_remote_code=True)\n", | ||
"\n", | ||
"prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n", | ||
"\n", | ||
"inputs = processor(prompt, [image], return_tensors=\"pt\")\n", | ||
"\n", | ||
"generation_args = {\"max_new_tokens\": 3072, \"do_sample\": False, \"streamer\": TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True)}\n", | ||
"\n", | ||
"print(\"Coding:\")\n", | ||
"generate_ids = model.generate(**inputs, eos_token_id=processor.tokenizer.eos_token_id, **generation_args)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "openvinodev", | ||
"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.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.